blob: 16ab97cfa689ca02baefd795231f0be7659e6361 [file] [log] [blame]
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001/* Copyright (c) 2002,2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
Steve Mucklef132c6c2012-06-06 18:30:57 -070013#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070014#include <linux/uaccess.h>
15#include <linux/vmalloc.h>
16#include <linux/ioctl.h>
17#include <linux/sched.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060018#include <linux/of.h>
19#include <linux/of_device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020
21#include <mach/socinfo.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060022#include <mach/msm_bus_board.h>
23#include <mach/msm_bus.h>
24#include <mach/msm_dcvs.h>
25#include <mach/msm_dcvs_scm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27#include "kgsl.h"
28#include "kgsl_pwrscale.h"
29#include "kgsl_cffdump.h"
30#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060031#include "kgsl_iommu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
33#include "adreno.h"
34#include "adreno_pm4types.h"
35#include "adreno_debugfs.h"
36#include "adreno_postmortem.h"
37
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070038#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070039#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040
41#define DRIVER_VERSION_MAJOR 3
42#define DRIVER_VERSION_MINOR 1
43
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044/* Adreno MH arbiter config*/
45#define ADRENO_CFG_MHARB \
46 (0x10 \
47 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
48 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
50 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
53 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
54 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
55 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
56 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
57 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
58 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
59 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
60 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
61
62#define ADRENO_MMU_CONFIG \
63 (0x01 \
64 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
69 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
70 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
71 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
72 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
73 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
74 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
75
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076static const struct kgsl_functable adreno_functable;
77
78static struct adreno_device device_3d0 = {
79 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070080 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081 .name = DEVICE_3D0_NAME,
82 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060083 .mh = {
84 .mharb = ADRENO_CFG_MHARB,
85 /* Remove 1k boundary check in z470 to avoid a GPU
86 * hang. Notice that this solution won't work if
87 * both EBI and SMI are used
88 */
89 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090 /* turn off memory protection unit by setting
91 acceptable physical address range to include
92 all pages. */
93 .mpu_base = 0x00000000,
94 .mpu_range = 0xFFFFF000,
95 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060096 .mmu = {
97 .config = ADRENO_MMU_CONFIG,
98 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102 .iomemname = KGSL_3D0_REG_MEMORY,
103 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600105 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
107 .suspend = kgsl_early_suspend_driver,
108 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600110#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600112 .gmem_base = 0,
113 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114 .pfp_fw = NULL,
115 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700116 .wait_timeout = 10000, /* in milliseconds */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600117 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118};
119
Tarun Karra3335f142012-06-19 14:11:48 -0700120/* This set of registers are used for Hang detection
121 * If the values of these registers are same after
122 * KGSL_TIMEOUT_PART time, GPU hang is reported in
123 * kernel log.
124 */
125unsigned int hang_detect_regs[] = {
126 A3XX_RBBM_STATUS,
127 REG_CP_RB_RPTR,
128 REG_CP_IB1_BASE,
129 REG_CP_IB1_BUFSZ,
130 REG_CP_IB2_BASE,
131 REG_CP_IB2_BUFSZ,
132};
133
134const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700135
Jordan Crouse505df9c2011-07-28 08:37:59 -0600136/*
137 * This is the master list of all GPU cores that are supported by this
138 * driver.
139 */
140
141#define ANY_ID (~0)
142
143static const struct {
144 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600145 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600146 const char *pm4fw;
147 const char *pfpfw;
148 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700149 unsigned int istore_size;
150 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700151 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530152 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600153} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600154 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700155 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530156 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530157 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
158 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530159 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600160 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700161 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530162 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600163 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700164 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530165 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600166 /*
167 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
168 * a hardware problem.
169 */
170 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700171 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530172 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700173 { ADRENO_REV_A225, 2, 2, 0, 6,
174 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530175 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600176 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700177 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530178 1536, 768, 3, SZ_512K },
179 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530180 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530181 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
182 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700183 /* A3XX doesn't use the pix_shader_start */
Jordan Croused2b30d22012-05-21 08:41:51 -0600184 { ADRENO_REV_A320, 3, 2, 0, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700185 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530186 512, 0, 2, SZ_512K },
liu zhongfd42e622012-05-01 19:18:30 -0700187 { ADRENO_REV_A330, 3, 3, 0, 0,
188 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
189 512, 0, 2, SZ_1M },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600190};
191
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600192static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193{
Jordan Crousea78c9172011-07-11 13:14:09 -0600194 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600195 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196
Jordan Crousea78c9172011-07-11 13:14:09 -0600197 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198
199 if (device->requested_state == KGSL_STATE_NONE) {
200 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700201 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 queue_work(device->work_queue, &device->idle_check_ws);
203 } else if (device->pwrscale.policy != NULL) {
204 queue_work(device->work_queue, &device->idle_check_ws);
205 }
206 }
207
208 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800209 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 jiffies + device->pwrctrl.interval_timeout);
211 return result;
212}
213
Jordan Crouse9f739212011-07-28 08:37:57 -0600214static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 struct kgsl_pagetable *pagetable)
216{
217 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
218 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
219
220 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
221
222 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
223
224 kgsl_mmu_unmap(pagetable, &device->memstore);
225
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600226 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227}
228
229static int adreno_setup_pt(struct kgsl_device *device,
230 struct kgsl_pagetable *pagetable)
231{
232 int result = 0;
233 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
234 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
235
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
237 GSL_PT_PAGE_RV);
238 if (result)
239 goto error;
240
241 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
242 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
243 if (result)
244 goto unmap_buffer_desc;
245
246 result = kgsl_mmu_map_global(pagetable, &device->memstore,
247 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
248 if (result)
249 goto unmap_memptrs_desc;
250
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600251 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
253 if (result)
254 goto unmap_memstore_desc;
255
256 return result;
257
258unmap_memstore_desc:
259 kgsl_mmu_unmap(pagetable, &device->memstore);
260
261unmap_memptrs_desc:
262 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
263
264unmap_buffer_desc:
265 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
266
267error:
268 return result;
269}
270
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600271static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600272 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600273 uint32_t flags)
274{
275 unsigned int pt_val, reg_pt_val;
276 unsigned int link[200];
277 unsigned int *cmds = &link[0];
278 int sizedwords = 0;
279 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
280 struct kgsl_memdesc **reg_map_desc;
Pu Chened8cbb52012-06-04 18:18:48 -0700281 void *reg_map_array = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600282 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600283 struct kgsl_context *context;
284 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600285
286 if (!adreno_dev->drawctxt_active)
287 return kgsl_mmu_device_setstate(&device->mmu, flags);
288 num_iommu_units = kgsl_mmu_get_reg_map_desc(&device->mmu,
289 &reg_map_array);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600290
291 context = idr_find(&device->context_idr, context_id);
292 adreno_ctx = context->devctxt;
293
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600294 reg_map_desc = reg_map_array;
295
296 if (kgsl_mmu_enable_clk(&device->mmu,
297 KGSL_IOMMU_CONTEXT_USER))
298 goto done;
299
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600300 cmds += __adreno_add_idle_indirect_cmds(cmds,
301 device->mmu.setstate_memory.gpuaddr +
302 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
303
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600304 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600305 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
306 device->mmu.setstate_memory.gpuaddr +
307 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
308 else
309 cmds += adreno_add_bank_change_cmds(cmds,
310 KGSL_IOMMU_CONTEXT_USER,
311 device->mmu.setstate_memory.gpuaddr +
312 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
313
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700314 pt_val = kgsl_mmu_pt_get_base_addr(device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600315 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600316 /*
317 * We need to perfrom the following operations for all
318 * IOMMU units
319 */
320 for (i = 0; i < num_iommu_units; i++) {
321 reg_pt_val = (pt_val &
322 (KGSL_IOMMU_TTBR0_PA_MASK <<
323 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
324 kgsl_mmu_get_pt_lsb(&device->mmu, i,
325 KGSL_IOMMU_CONTEXT_USER);
326 /*
327 * Set address of the new pagetable by writng to IOMMU
328 * TTBR0 register
329 */
330 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
331 *cmds++ = reg_map_desc[i]->gpuaddr +
332 (KGSL_IOMMU_CONTEXT_USER <<
333 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0;
334 *cmds++ = reg_pt_val;
335 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
336 *cmds++ = 0x00000000;
337
338 /*
339 * Read back the ttbr0 register as a barrier to ensure
340 * above writes have completed
341 */
342 cmds += adreno_add_read_cmds(device, cmds,
343 reg_map_desc[i]->gpuaddr +
344 (KGSL_IOMMU_CONTEXT_USER <<
345 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
346 reg_pt_val,
347 device->mmu.setstate_memory.gpuaddr +
348 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600349 }
350 /* invalidate all base pointers */
351 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
352 *cmds++ = 0x7fff;
353
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600354 cmds += __adreno_add_idle_indirect_cmds(cmds,
355 device->mmu.setstate_memory.gpuaddr +
356 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600357 }
358 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
359 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700360 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600361 */
362 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700363 reg_pt_val = (pt_val &
364 (KGSL_IOMMU_TTBR0_PA_MASK <<
365 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
366 kgsl_mmu_get_pt_lsb(&device->mmu, i,
367 KGSL_IOMMU_CONTEXT_USER);
368
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600369 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
370 *cmds++ = (reg_map_desc[i]->gpuaddr +
371 (KGSL_IOMMU_CONTEXT_USER <<
372 KGSL_IOMMU_CTX_SHIFT) +
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700373 KGSL_IOMMU_CTX_TLBIALL);
374 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600375
376 cmds += __adreno_add_idle_indirect_cmds(cmds,
377 device->mmu.setstate_memory.gpuaddr +
378 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
379
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600380 cmds += adreno_add_read_cmds(device, cmds,
381 reg_map_desc[i]->gpuaddr +
382 (KGSL_IOMMU_CONTEXT_USER <<
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700383 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
384 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600385 device->mmu.setstate_memory.gpuaddr +
386 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
387 }
388 }
389
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600390 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600391 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
392 reg_map_desc[num_iommu_units - 1]->gpuaddr - PAGE_SIZE,
393 device->mmu.setstate_memory.gpuaddr +
394 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
395 else
396 cmds += adreno_add_bank_change_cmds(cmds,
397 KGSL_IOMMU_CONTEXT_PRIV,
398 device->mmu.setstate_memory.gpuaddr +
399 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
400
401 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600402 if (sizedwords) {
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600403 /*
404 * add an interrupt at the end of commands so that the smmu
405 * disable clock off function will get called
406 */
407 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
408 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
409 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600410 /* This returns the per context timestamp but we need to
411 * use the global timestamp for iommu clock disablement */
412 adreno_ringbuffer_issuecmds(device, adreno_ctx,
413 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600414 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600415 kgsl_mmu_disable_clk_on_ts(&device->mmu,
416 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600417 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600418done:
419 if (num_iommu_units)
420 kfree(reg_map_array);
421}
422
423static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600424 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600425 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426{
427 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
428 unsigned int link[32];
429 unsigned int *cmds = &link[0];
430 int sizedwords = 0;
431 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600432 struct kgsl_context *context;
433 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600435 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530436 * Fix target freeze issue by adding TLB flush for each submit
437 * on A20X based targets.
438 */
439 if (adreno_is_a20x(adreno_dev))
440 flags |= KGSL_MMUFLAGS_TLBFLUSH;
441 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600442 * If possible, then set the state via the command stream to avoid
443 * a CPU idle. Otherwise, use the default setstate which uses register
444 * writes For CFF dump we must idle and use the registers so that it is
445 * easier to filter out the mmu accesses from the dump
446 */
447 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600448 context = idr_find(&device->context_idr, context_id);
449 adreno_ctx = context->devctxt;
450
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
452 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600453 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 *cmds++ = 0x00000000;
455
456 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600457 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600458 *cmds++ = kgsl_mmu_pt_get_base_addr(
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600459 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460 sizedwords += 4;
461 }
462
463 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
464 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600465 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466 1);
467 *cmds++ = 0x00000000;
468 sizedwords += 2;
469 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600470 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 *cmds++ = mh_mmu_invalidate;
472 sizedwords += 2;
473 }
474
475 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600476 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477 /* HW workaround: to resolve MMU page fault interrupts
478 * caused by the VGT.It prevents the CP PFP from filling
479 * the VGT DMA request fifo too early,thereby ensuring
480 * that the VGT will not fetch vertex/bin data until
481 * after the page table base register has been updated.
482 *
483 * Two null DRAW_INDX_BIN packets are inserted right
484 * after the page table base update, followed by a
485 * wait for idle. The null packets will fill up the
486 * VGT DMA request fifo and prevent any further
487 * vertex/bin updates from occurring until the wait
488 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600489 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 *cmds++ = (0x4 << 16) |
491 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
492 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600493 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600494 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600495 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496 *cmds++ = 0; /* viz query info */
497 *cmds++ = 0x0003C004; /* draw indicator */
498 *cmds++ = 0; /* bin base */
499 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600500 *cmds++ =
501 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600503 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 *cmds++ = 0; /* viz query info */
505 *cmds++ = 0x0003C004; /* draw indicator */
506 *cmds++ = 0; /* bin base */
507 *cmds++ = 3; /* bin size */
508 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600509 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600511 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512 *cmds++ = 0x00000000;
513 sizedwords += 21;
514 }
515
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600516
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600518 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 *cmds++ = 0x7fff; /* invalidate all base pointers */
520 sizedwords += 2;
521 }
522
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600523 adreno_ringbuffer_issuecmds(device, adreno_ctx,
524 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600526 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600527 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600528 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529}
530
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600531static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600532 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600533 uint32_t flags)
534{
535 /* call the mmu specific handler */
536 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600537 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600538 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600539 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600540}
541
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700543a3xx_getchipid(struct kgsl_device *device)
544{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600545 struct kgsl_device_platform_data *pdata =
546 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700547
Jordan Crouse54154c62012-03-27 16:33:26 -0600548 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600549 * All current A3XX chipids are detected at the SOC level. Leave this
550 * function here to support any future GPUs that have working
551 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600552 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700553
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600554 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700555}
556
557static unsigned int
558a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559{
560 unsigned int chipid = 0;
561 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600562 struct kgsl_device_platform_data *pdata =
563 kgsl_device_get_drvdata(device);
564
565 /* If the chip id is set at the platform level, then just use that */
566
567 if (pdata->chipid != 0)
568 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569
570 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
571 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
572 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
573
574 /*
575 * adreno 22x gpus are indicated by coreid 2,
576 * but REG_RBBM_PERIPHID1 always contains 0 for this field
577 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600578 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 chipid = 2 << 24;
580 else
581 chipid = (coreid & 0xF) << 24;
582
583 chipid |= ((majorid >> 4) & 0xF) << 16;
584
585 minorid = ((revid >> 0) & 0xFF);
586
587 patchid = ((revid >> 16) & 0xFF);
588
589 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530590 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 if (cpu_is_qsd8x50())
592 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530593 else if (cpu_is_msm8625() && minorid == 0)
594 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595
596 chipid |= (minorid << 8) | patchid;
597
598 return chipid;
599}
600
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700601static unsigned int
602adreno_getchipid(struct kgsl_device *device)
603{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600604 struct kgsl_device_platform_data *pdata =
605 kgsl_device_get_drvdata(device);
606
607 /*
608 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
609 * an A2XX processor
610 */
611
612 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700613 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600614 else
615 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700616}
617
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618static inline bool _rev_match(unsigned int id, unsigned int entry)
619{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600620 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622
623static void
624adreno_identify_gpu(struct adreno_device *adreno_dev)
625{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600626 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627
628 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
629
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600630 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
631 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
632 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
633 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634
Jordan Crouse505df9c2011-07-28 08:37:59 -0600635 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
636 if (core == adreno_gpulist[i].core &&
637 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600638 _rev_match(minor, adreno_gpulist[i].minor) &&
639 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641 }
642
Jordan Crouse505df9c2011-07-28 08:37:59 -0600643 if (i == ARRAY_SIZE(adreno_gpulist)) {
644 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
645 return;
646 }
647
648 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
649 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
650 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
651 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700652 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
653 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700654 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600655 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656}
657
Lokesh Batra805e1e12012-08-03 08:34:06 -0600658static struct platform_device_id adreno_id_table[] = {
659 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
660 {},
661};
662
663MODULE_DEVICE_TABLE(platform, adreno_id_table);
664
665static struct of_device_id adreno_match_table[] = {
666 { .compatible = "qcom,kgsl-3d0", },
667 {}
668};
669
670static inline int adreno_of_read_property(struct device_node *node,
671 const char *prop, unsigned int *ptr)
672{
673 int ret = of_property_read_u32(node, prop, ptr);
674 if (ret)
675 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
676 return ret;
677}
678
679static struct device_node *adreno_of_find_subnode(struct device_node *parent,
680 const char *name)
681{
682 struct device_node *child;
683
684 for_each_child_of_node(parent, child) {
685 if (of_device_is_compatible(child, name))
686 return child;
687 }
688
689 return NULL;
690}
691
692static int adreno_of_get_pwrlevels(struct device_node *parent,
693 struct kgsl_device_platform_data *pdata)
694{
695 struct device_node *node, *child;
696 int ret = -EINVAL;
697
698 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
699
700 if (node == NULL) {
701 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
702 return -EINVAL;
703 }
704
705 pdata->num_levels = 0;
706
707 for_each_child_of_node(node, child) {
708 unsigned int index;
709 struct kgsl_pwrlevel *level;
710
711 if (adreno_of_read_property(child, "reg", &index))
712 goto done;
713
714 if (index >= KGSL_MAX_PWRLEVELS) {
715 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
716 index);
717 continue;
718 }
719
720 if (index >= pdata->num_levels)
721 pdata->num_levels = index + 1;
722
723 level = &pdata->pwrlevel[index];
724
725 if (adreno_of_read_property(child, "qcom,gpu-freq",
726 &level->gpu_freq))
727 goto done;
728
729 if (adreno_of_read_property(child, "qcom,bus-freq",
730 &level->bus_freq))
731 goto done;
732
733 if (adreno_of_read_property(child, "qcom,io-fraction",
734 &level->io_fraction))
735 level->io_fraction = 0;
736 }
737
738 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
739 &pdata->init_level))
740 pdata->init_level = 1;
741
742 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
743 KGSL_CORE_ERR("Initial power level out of range\n");
744 pdata->init_level = 1;
745 }
746
747 ret = 0;
748done:
749 return ret;
750
751}
752static void adreno_of_free_bus_scale_info(struct msm_bus_scale_pdata *pdata)
753{
754 int i;
755
756 if (pdata == NULL)
757 return;
758
759 for (i = 0; pdata->usecase && i < pdata->num_usecases; i++)
760 kfree(pdata->usecase[i].vectors);
761
762 kfree(pdata->usecase);
763 kfree(pdata);
764}
765
766struct msm_bus_scale_pdata *adreno_of_get_bus_scale(struct device_node *node)
767{
768 static int bus_vectors_src[3] = {MSM_BUS_MASTER_GRAPHICS_3D,
769 MSM_BUS_MASTER_GRAPHICS_3D_PORT1, MSM_BUS_MASTER_V_OCMEM_GFX3D};
770 static int bus_vectors_dst[2] = {MSM_BUS_SLAVE_EBI_CH0,
771 MSM_BUS_SLAVE_OCMEM};
772 const unsigned int *vectors;
773 struct msm_bus_scale_pdata *pdata;
774 int i, j, len, num_paths;
775 int ret = -EINVAL;
776
777 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
778
779 if (!pdata) {
780 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
781 return ERR_PTR(-ENOMEM);
782 }
783
784 if (adreno_of_read_property(node, "qcom,grp3d-num-bus-scale-usecases",
785 &pdata->num_usecases)) {
786 pdata->num_usecases = 0;
787 goto err;
788 }
789
790 pdata->usecase = kzalloc(pdata->num_usecases *
791 sizeof(struct msm_bus_paths), GFP_KERNEL);
792
793 if (pdata->usecase == NULL) {
794 KGSL_CORE_ERR("kzalloc (%d) failed\n",
795 pdata->num_usecases * sizeof(struct msm_bus_paths));
796 ret = -ENOMEM;
797 goto err;
798 }
799
800 if (adreno_of_read_property(node, "qcom,grp3d-num-vectors-per-usecase",
801 &num_paths))
802 goto err;
803
804 vectors = of_get_property(node, "qcom,grp3d-vectors", &len);
805
806 if (len != pdata->num_usecases * num_paths *
807 sizeof(struct msm_bus_vectors)) {
808 KGSL_CORE_ERR("Invalid size for the bus scale vectors\n");
809 goto err;
810 }
811
812 for (i = 0; i < pdata->num_usecases; i++) {
813 pdata->usecase[i].num_paths = num_paths;
814 pdata->usecase[i].vectors = kzalloc(num_paths *
815 sizeof(struct msm_bus_vectors),
816 GFP_KERNEL);
817 if (!pdata->usecase[i].vectors) {
818 KGSL_CORE_ERR("kzalloc(%d) failed\n",
819 num_paths * sizeof(struct msm_bus_vectors));
820 ret = -ENOMEM;
821 goto err;
822 }
823 for (j = 0; j < num_paths; j++) {
824 int index = (i * num_paths + j) * 4;
825 pdata->usecase[i].vectors[j].src =
826 bus_vectors_src[be32_to_cpu(vectors[index])];
827 pdata->usecase[i].vectors[j].dst =
828 bus_vectors_dst[
829 be32_to_cpu(vectors[index + 1])];
830 pdata->usecase[i].vectors[j].ab =
831 be32_to_cpu(vectors[index + 2]);
832 pdata->usecase[i].vectors[j].ib =
833 KGSL_CONVERT_TO_MBPS(
834 be32_to_cpu(vectors[index + 3]));
835 }
836 }
837
838 pdata->name = "grp3d";
839
840 return pdata;
841
842err:
843 adreno_of_free_bus_scale_info(pdata);
844
845 return ERR_PTR(ret);
846}
847
848static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
849{
850 struct device_node *node, *child;
851 struct msm_dcvs_core_info *info = NULL;
852 int count = 0;
853 int ret = -EINVAL;
854
855 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
856 if (node == NULL)
857 return ERR_PTR(-EINVAL);
858
859 info = kzalloc(sizeof(*info), GFP_KERNEL);
860
861 if (info == NULL) {
862 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
863 ret = -ENOMEM;
864 goto err;
865 }
866
867 for_each_child_of_node(node, child)
868 count++;
869
870 info->core_param.num_freq = count;
871
872 info->freq_tbl = kzalloc(info->core_param.num_freq *
873 sizeof(struct msm_dcvs_freq_entry),
874 GFP_KERNEL);
875
876 if (info->freq_tbl == NULL) {
877 KGSL_CORE_ERR("kzalloc(%d) failed\n",
878 info->core_param.num_freq *
879 sizeof(struct msm_dcvs_freq_entry));
880 ret = -ENOMEM;
881 goto err;
882 }
883
884 for_each_child_of_node(node, child) {
885 unsigned int index;
886
887 if (adreno_of_read_property(child, "reg", &index))
888 goto err;
889
890 if (index >= info->core_param.num_freq) {
891 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
892 index);
893 continue;
894 }
895
896 if (adreno_of_read_property(child, "qcom,freq",
897 &info->freq_tbl[index].freq))
898 goto err;
899
900 if (adreno_of_read_property(child, "qcom,idle-energy",
901 &info->freq_tbl[index].idle_energy))
902 info->freq_tbl[index].idle_energy = 0;
903
904 if (adreno_of_read_property(child, "qcom,active-energy",
905 &info->freq_tbl[index].active_energy))
906 info->freq_tbl[index].active_energy = 0;
907 }
908
909 if (adreno_of_read_property(node, "qcom,core-max-time-us",
910 &info->core_param.max_time_us))
911 goto err;
912
913 if (adreno_of_read_property(node, "qcom,algo-slack-time-us",
914 &info->algo_param.slack_time_us))
915 goto err;
916
917 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
918 &info->algo_param.disable_pc_threshold))
919 goto err;
920
921 if (adreno_of_read_property(node, "qcom,algo-ss-window-size",
922 &info->algo_param.ss_window_size))
923 goto err;
924
925 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
926 &info->algo_param.ss_util_pct))
927 goto err;
928
929 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
930 &info->algo_param.em_max_util_pct))
931 goto err;
932
933 if (adreno_of_read_property(node, "qcom,algo-ss-iobusy-conv",
934 &info->algo_param.ss_iobusy_conv))
935 goto err;
936
937 return info;
938
939err:
940 if (info)
941 kfree(info->freq_tbl);
942
943 kfree(info);
944
945 return ERR_PTR(ret);
946}
947
948static int adreno_of_get_iommu(struct device_node *parent,
949 struct kgsl_device_platform_data *pdata)
950{
951 struct device_node *node, *child;
952 struct kgsl_device_iommu_data *data = NULL;
953 struct kgsl_iommu_ctx *ctxs = NULL;
954 u32 reg_val[2];
955 int ctx_index = 0;
956
957 node = of_parse_phandle(parent, "iommu", 0);
958 if (node == NULL)
959 return -EINVAL;
960
961 data = kzalloc(sizeof(*data), GFP_KERNEL);
962 if (data == NULL) {
963 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
964 goto err;
965 }
966
967 if (of_property_read_u32_array(node, "reg", reg_val, 2))
968 goto err;
969
970 data->physstart = reg_val[0];
971 data->physend = data->physstart + reg_val[1] - 1;
972
973 data->iommu_ctx_count = 0;
974
975 for_each_child_of_node(node, child)
976 data->iommu_ctx_count++;
977
978 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
979 GFP_KERNEL);
980
981 if (ctxs == NULL) {
982 KGSL_CORE_ERR("kzalloc(%d) failed\n",
983 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
984 goto err;
985 }
986
987 for_each_child_of_node(node, child) {
988 int ret = of_property_read_string(child, "label",
989 &ctxs[ctx_index].iommu_ctx_name);
990
991 if (ret) {
992 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
993 goto err;
994 }
995
996 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
997 &ctxs[ctx_index].ctx_id))
998 goto err;
999
1000 ctx_index++;
1001 }
1002
1003 data->iommu_ctxs = ctxs;
1004
1005 pdata->iommu_data = data;
1006 pdata->iommu_count = 1;
1007
1008 return 0;
1009
1010err:
1011 kfree(ctxs);
1012 kfree(data);
1013
1014 return -EINVAL;
1015}
1016
1017static int adreno_of_get_pdata(struct platform_device *pdev)
1018{
1019 struct kgsl_device_platform_data *pdata = NULL;
1020 struct kgsl_device *device;
1021 int ret = -EINVAL;
1022
1023 pdev->id_entry = adreno_id_table;
1024
1025 pdata = pdev->dev.platform_data;
1026 if (pdata)
1027 return 0;
1028
1029 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1030 KGSL_CORE_ERR("Unable to read 'label'\n");
1031 goto err;
1032 }
1033
1034 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1035 goto err;
1036
1037 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1038 if (pdata == NULL) {
1039 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1040 ret = -ENOMEM;
1041 goto err;
1042 }
1043
1044 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1045 &pdata->chipid))
1046 goto err;
1047
1048 /* pwrlevel Data */
1049 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1050 if (ret)
1051 goto err;
1052
1053 /* Default value is 83, if not found in DT */
1054 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1055 &pdata->idle_timeout))
1056 pdata->idle_timeout = 83;
1057
1058 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1059 &pdata->nap_allowed))
1060 pdata->nap_allowed = 1;
1061
1062 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1063 &pdata->clk_map))
1064 goto err;
1065
1066 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1067
1068 if (device->id != KGSL_DEVICE_3D0)
1069 goto err;
1070
1071 /* Bus Scale Data */
1072
1073 pdata->bus_scale_table = adreno_of_get_bus_scale(pdev->dev.of_node);
1074 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1075 ret = PTR_ERR(pdata->bus_scale_table);
1076 goto err;
1077 }
1078
1079 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1080 if (IS_ERR_OR_NULL(pdata->core_info)) {
1081 ret = PTR_ERR(pdata->core_info);
1082 goto err;
1083 }
1084
1085 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1086 if (ret)
1087 goto err;
1088
1089 pdev->dev.platform_data = pdata;
1090 return 0;
1091
1092err:
1093 if (pdata) {
1094 adreno_of_free_bus_scale_info(pdata->bus_scale_table);
1095 if (pdata->core_info)
1096 kfree(pdata->core_info->freq_tbl);
1097 kfree(pdata->core_info);
1098
1099 if (pdata->iommu_data)
1100 kfree(pdata->iommu_data->iommu_ctxs);
1101
1102 kfree(pdata->iommu_data);
1103 }
1104
1105 kfree(pdata);
1106
1107 return ret;
1108}
1109
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110static int __devinit
1111adreno_probe(struct platform_device *pdev)
1112{
1113 struct kgsl_device *device;
1114 struct adreno_device *adreno_dev;
1115 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001116 bool is_dt;
1117
1118 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1119
1120 if (is_dt && pdev->dev.of_node) {
1121 status = adreno_of_get_pdata(pdev);
1122 if (status)
1123 goto error_return;
1124 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125
1126 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1127 adreno_dev = ADRENO_DEVICE(device);
1128 device->parentdev = &pdev->dev;
1129
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001130 status = adreno_ringbuffer_init(device);
1131 if (status != 0)
1132 goto error;
1133
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001134 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135 if (status)
1136 goto error_close_rb;
1137
1138 adreno_debugfs_init(device);
1139
1140 kgsl_pwrscale_init(device);
1141 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1142
1143 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1144 return 0;
1145
1146error_close_rb:
1147 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1148error:
1149 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001150error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151 return status;
1152}
1153
1154static int __devexit adreno_remove(struct platform_device *pdev)
1155{
1156 struct kgsl_device *device;
1157 struct adreno_device *adreno_dev;
1158
1159 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1160 adreno_dev = ADRENO_DEVICE(device);
1161
1162 kgsl_pwrscale_detach_policy(device);
1163 kgsl_pwrscale_close(device);
1164
1165 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1166 kgsl_device_platform_remove(device);
1167
1168 return 0;
1169}
1170
1171static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1172{
1173 int status = -EINVAL;
1174 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001176 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1177 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178
1179 /* Power up the device */
1180 kgsl_pwrctrl_enable(device);
1181
1182 /* Identify the specific GPU */
1183 adreno_identify_gpu(adreno_dev);
1184
Jordan Crouse505df9c2011-07-28 08:37:59 -06001185 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1186 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1187 adreno_dev->chip_id);
1188 goto error_clk_off;
1189 }
1190
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001191 /* Set up the MMU */
1192 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001193 /*
1194 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1195 * on older gpus
1196 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001197 if (adreno_is_a20x(adreno_dev)) {
1198 device->mh.mh_intf_cfg1 = 0;
1199 device->mh.mh_intf_cfg2 = 0;
1200 }
1201
1202 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001203 }
1204
Tarun Karra3335f142012-06-19 14:11:48 -07001205 /* Assign correct RBBM status register to hang detect regs
1206 */
1207 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1208
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001209 status = kgsl_mmu_start(device);
1210 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211 goto error_clk_off;
1212
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001213 /* Start the GPU */
1214 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215
1216 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001217 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218
1219 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001220 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001221 /* While recovery is on we do not want timer to
1222 * fire and attempt to change any device state */
1223 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1224 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001225 return 0;
1226 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Shubhraprakash Das79447952012-04-26 18:12:23 -06001229 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230error_clk_off:
1231 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001232
1233 return status;
1234}
1235
1236static int adreno_stop(struct kgsl_device *device)
1237{
1238 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1239
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001240 adreno_dev->drawctxt_active = NULL;
1241
1242 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1243
Shubhraprakash Das79447952012-04-26 18:12:23 -06001244 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001245
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001246 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301247 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001248 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001249
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001250 /* Power down the device */
1251 kgsl_pwrctrl_disable(device);
1252
1253 return 0;
1254}
1255
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001256static void adreno_mark_context_status(struct kgsl_device *device,
1257 int recovery_status)
1258{
1259 struct kgsl_context *context;
1260 int next = 0;
1261 /*
1262 * Set the reset status of all contexts to
1263 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1264 * since thats the guilty party, if recovery failed then
1265 * mark all as guilty
1266 */
1267 while ((context = idr_get_next(&device->context_idr, &next))) {
1268 struct adreno_context *adreno_context = context->devctxt;
1269 if (recovery_status) {
1270 context->reset_status =
1271 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1272 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1273 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1274 context->reset_status) {
1275 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1276 CTXT_FLAGS_GPU_HANG_RECOVERED))
1277 context->reset_status =
1278 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1279 else
1280 context->reset_status =
1281 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1282 }
1283 next = next + 1;
1284 }
1285}
1286
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001287static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1288{
1289 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1290 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1291 struct kgsl_context *context;
1292 struct adreno_context *temp_adreno_context;
1293 int next = 0;
1294
1295 while ((context = idr_get_next(&device->context_idr, &next))) {
1296 temp_adreno_context = context->devctxt;
1297 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1298 kgsl_sharedmem_writel(&device->memstore,
1299 KGSL_MEMSTORE_OFFSET(context->id,
1300 soptimestamp),
1301 rb->timestamp[context->id]);
1302 kgsl_sharedmem_writel(&device->memstore,
1303 KGSL_MEMSTORE_OFFSET(context->id,
1304 eoptimestamp),
1305 rb->timestamp[context->id]);
1306 }
1307 next = next + 1;
1308 }
1309}
1310
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001311static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1312{
1313 vfree(rec_data->rb_buffer);
1314 vfree(rec_data->bad_rb_buffer);
1315}
1316
1317static int adreno_setup_recovery_data(struct kgsl_device *device,
1318 struct adreno_recovery_data *rec_data)
1319{
1320 int ret = 0;
1321 unsigned int ib1_sz, ib2_sz;
1322 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1323 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1324
1325 memset(rec_data, 0, sizeof(*rec_data));
1326
1327 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1328 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1329 if (ib1_sz || ib2_sz)
1330 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1331
1332 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1333 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1334 current_context));
1335
1336 kgsl_sharedmem_readl(&device->memstore,
1337 &rec_data->global_eop,
1338 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1339 eoptimestamp));
1340
1341 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1342 if (!rec_data->rb_buffer) {
1343 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1344 rb->buffer_desc.size);
1345 return -ENOMEM;
1346 }
1347
1348 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1349 if (!rec_data->bad_rb_buffer) {
1350 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1351 rb->buffer_desc.size);
1352 ret = -ENOMEM;
1353 goto done;
1354 }
1355
1356done:
1357 if (ret) {
1358 vfree(rec_data->rb_buffer);
1359 vfree(rec_data->bad_rb_buffer);
1360 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001361 return ret;
1362}
1363
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001364static int
1365_adreno_recover_hang(struct kgsl_device *device,
1366 struct adreno_recovery_data *rec_data,
1367 bool try_bad_commands)
1368{
1369 int ret;
1370 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1371 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1372 struct kgsl_context *context;
1373 struct adreno_context *adreno_context = NULL;
1374 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1375
1376 context = idr_find(&device->context_idr, rec_data->context_id);
1377 if (context == NULL) {
1378 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1379 rec_data->context_id);
1380 } else {
1381 adreno_context = context->devctxt;
1382 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1383 }
1384
1385 /* Extract valid contents from rb which can still be executed after
1386 * hang */
1387 ret = adreno_ringbuffer_extract(rb, rec_data);
1388 if (ret)
1389 goto done;
1390
1391 /* restart device */
1392 ret = adreno_stop(device);
1393 if (ret) {
1394 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1395 goto done;
1396 }
1397
1398 ret = adreno_start(device, true);
1399 if (ret) {
1400 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1401 goto done;
1402 }
1403
1404 if (context)
1405 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1406 KGSL_MEMSTORE_GLOBAL);
1407
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001408 /* If iommu is used then we need to make sure that the iommu clocks
1409 * are on since there could be commands in pipeline that touch iommu */
1410 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1411 ret = kgsl_mmu_enable_clk(&device->mmu,
1412 KGSL_IOMMU_CONTEXT_USER);
1413 if (ret)
1414 goto done;
1415 }
1416
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001417 /* Do not try the bad caommands if recovery has failed bad commands
1418 * once already */
1419 if (!try_bad_commands)
1420 rec_data->bad_rb_size = 0;
1421
1422 if (rec_data->bad_rb_size) {
1423 int idle_ret;
1424 /* submit the bad and good context commands and wait for
1425 * them to pass */
1426 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1427 rec_data->bad_rb_size);
1428 idle_ret = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1429 if (idle_ret) {
1430 ret = adreno_stop(device);
1431 if (ret) {
1432 KGSL_DRV_ERR(device,
1433 "Device stop failed in recovery\n");
1434 goto done;
1435 }
1436 ret = adreno_start(device, true);
1437 if (ret) {
1438 KGSL_DRV_ERR(device,
1439 "Device start failed in recovery\n");
1440 goto done;
1441 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001442 if (context)
1443 kgsl_mmu_setstate(&device->mmu,
1444 adreno_context->pagetable,
1445 KGSL_MEMSTORE_GLOBAL);
1446
1447 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1448 ret = kgsl_mmu_enable_clk(&device->mmu,
1449 KGSL_IOMMU_CONTEXT_USER);
1450 if (ret)
1451 goto done;
1452 }
1453
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001454 ret = idle_ret;
1455 KGSL_DRV_ERR(device,
1456 "Bad context commands hung in recovery\n");
1457 } else {
1458 KGSL_DRV_ERR(device,
1459 "Bad context commands succeeded in recovery\n");
1460 if (adreno_context)
1461 adreno_context->flags = (adreno_context->flags &
1462 ~CTXT_FLAGS_GPU_HANG) |
1463 CTXT_FLAGS_GPU_HANG_RECOVERED;
1464 adreno_dev->drawctxt_active = last_active_ctx;
1465 }
1466 }
1467 /* If either the bad command sequence failed or we did not play it */
1468 if (ret || !rec_data->bad_rb_size) {
1469 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1470 rec_data->rb_size);
1471 ret = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1472 if (ret) {
1473 /* If we fail here we can try to invalidate another
1474 * context and try recovering again */
1475 ret = -EAGAIN;
1476 goto done;
1477 }
1478 /* ringbuffer now has data from the last valid context id,
1479 * so restore the active_ctx to the last valid context */
1480 if (rec_data->last_valid_ctx_id) {
1481 struct kgsl_context *last_ctx =
1482 idr_find(&device->context_idr,
1483 rec_data->last_valid_ctx_id);
1484 if (last_ctx)
1485 adreno_dev->drawctxt_active = last_ctx->devctxt;
1486 }
1487 }
1488done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001489 /* Turn off iommu clocks */
1490 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1491 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001492 return ret;
1493}
1494
1495static int
1496adreno_recover_hang(struct kgsl_device *device,
1497 struct adreno_recovery_data *rec_data)
1498{
1499 int ret = 0;
1500 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1501 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1502 unsigned int timestamp;
1503
1504 KGSL_DRV_ERR(device,
1505 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1506 "Bad context_id: %u, global_eop: 0x%x\n",
1507 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1508
1509 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1510 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1511
1512 /* We may need to replay commands multiple times based on whether
1513 * multiple contexts hang the GPU */
1514 while (true) {
1515 if (!ret)
1516 ret = _adreno_recover_hang(device, rec_data, true);
1517 else
1518 ret = _adreno_recover_hang(device, rec_data, false);
1519
1520 if (-EAGAIN == ret) {
1521 /* setup new recovery parameters and retry, this
1522 * means more than 1 contexts are causing hang */
1523 adreno_destroy_recovery_data(rec_data);
1524 adreno_setup_recovery_data(device, rec_data);
1525 KGSL_DRV_ERR(device,
1526 "Retry recovery from 3D GPU hang. Recovery parameters: "
1527 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1528 rec_data->ib1, rec_data->context_id,
1529 rec_data->global_eop);
1530 } else {
1531 break;
1532 }
1533 }
1534
1535 if (ret)
1536 goto done;
1537
1538 /* Restore correct states after recovery */
1539 if (adreno_dev->drawctxt_active)
1540 device->mmu.hwpagetable =
1541 adreno_dev->drawctxt_active->pagetable;
1542 else
1543 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1544 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1545 kgsl_sharedmem_writel(&device->memstore,
1546 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1547 eoptimestamp),
1548 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1549done:
1550 adreno_set_max_ts_for_bad_ctxs(device);
1551 adreno_mark_context_status(device, ret);
1552 if (!ret)
1553 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1554 else
1555 KGSL_DRV_ERR(device, "Recovery failed\n");
1556 return ret;
1557}
1558
1559int
1560adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001561{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001562 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001563 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001564
1565 if (device->state == KGSL_STATE_HUNG)
1566 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001567 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001568 mutex_unlock(&device->mutex);
1569 wait_for_completion(&device->recovery_gate);
1570 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001571 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001572 result = 0;
1573 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001574 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001575 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001576 /* Detected a hang */
1577
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001578 /* Get the recovery data as soon as hang is detected */
1579 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001580 /*
1581 * Trigger an automatic dump of the state to
1582 * the console
1583 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001584 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001585
1586 /*
1587 * Make a GPU snapshot. For now, do it after the PM dump so we
1588 * can at least be sure the PM dump will work as it always has
1589 */
1590 kgsl_device_snapshot(device, 1);
1591
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001592 result = adreno_recover_hang(device, &rec_data);
1593 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001594 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001595 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001596 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001597 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001598 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1599 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001600 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001601 }
1602done:
1603 return result;
1604}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001605EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001606
1607static int adreno_getproperty(struct kgsl_device *device,
1608 enum kgsl_property_type type,
1609 void *value,
1610 unsigned int sizebytes)
1611{
1612 int status = -EINVAL;
1613 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1614
1615 switch (type) {
1616 case KGSL_PROP_DEVICE_INFO:
1617 {
1618 struct kgsl_devinfo devinfo;
1619
1620 if (sizebytes != sizeof(devinfo)) {
1621 status = -EINVAL;
1622 break;
1623 }
1624
1625 memset(&devinfo, 0, sizeof(devinfo));
1626 devinfo.device_id = device->id+1;
1627 devinfo.chip_id = adreno_dev->chip_id;
1628 devinfo.mmu_enabled = kgsl_mmu_enabled();
1629 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001630 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1631 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001632
1633 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1634 0) {
1635 status = -EFAULT;
1636 break;
1637 }
1638 status = 0;
1639 }
1640 break;
1641 case KGSL_PROP_DEVICE_SHADOW:
1642 {
1643 struct kgsl_shadowprop shadowprop;
1644
1645 if (sizebytes != sizeof(shadowprop)) {
1646 status = -EINVAL;
1647 break;
1648 }
1649 memset(&shadowprop, 0, sizeof(shadowprop));
1650 if (device->memstore.hostptr) {
1651 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1652 * anything to mmap().
1653 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001654 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001655 shadowprop.size = device->memstore.size;
1656 /* GSL needs this to be set, even if it
1657 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001658 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1659 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001660 }
1661 if (copy_to_user(value, &shadowprop,
1662 sizeof(shadowprop))) {
1663 status = -EFAULT;
1664 break;
1665 }
1666 status = 0;
1667 }
1668 break;
1669 case KGSL_PROP_MMU_ENABLE:
1670 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001671 int mmu_prop = kgsl_mmu_enabled();
1672
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001673 if (sizebytes != sizeof(int)) {
1674 status = -EINVAL;
1675 break;
1676 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001677 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001678 status = -EFAULT;
1679 break;
1680 }
1681 status = 0;
1682 }
1683 break;
1684 case KGSL_PROP_INTERRUPT_WAITS:
1685 {
1686 int int_waits = 1;
1687 if (sizebytes != sizeof(int)) {
1688 status = -EINVAL;
1689 break;
1690 }
1691 if (copy_to_user(value, &int_waits, sizeof(int))) {
1692 status = -EFAULT;
1693 break;
1694 }
1695 status = 0;
1696 }
1697 break;
1698 default:
1699 status = -EINVAL;
1700 }
1701
1702 return status;
1703}
1704
Jordan Crousef7370f82012-04-18 09:31:07 -06001705static int adreno_setproperty(struct kgsl_device *device,
1706 enum kgsl_property_type type,
1707 void *value,
1708 unsigned int sizebytes)
1709{
1710 int status = -EINVAL;
1711
1712 switch (type) {
1713 case KGSL_PROP_PWRCTRL: {
1714 unsigned int enable;
1715 struct kgsl_device_platform_data *pdata =
1716 kgsl_device_get_drvdata(device);
1717
1718 if (sizebytes != sizeof(enable))
1719 break;
1720
1721 if (copy_from_user(&enable, (void __user *) value,
1722 sizeof(enable))) {
1723 status = -EFAULT;
1724 break;
1725 }
1726
1727 if (enable) {
1728 if (pdata->nap_allowed)
1729 device->pwrctrl.nap_allowed = true;
1730
1731 kgsl_pwrscale_enable(device);
1732 } else {
1733 device->pwrctrl.nap_allowed = false;
1734 kgsl_pwrscale_disable(device);
1735 }
1736
1737 status = 0;
1738 }
1739 break;
1740 default:
1741 break;
1742 }
1743
1744 return status;
1745}
1746
Lynus Vaz06a9a902011-10-04 19:25:33 +05301747static inline void adreno_poke(struct kgsl_device *device)
1748{
1749 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1750 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1751}
1752
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001753/* Caller must hold the device mutex. */
1754int adreno_idle(struct kgsl_device *device, unsigned int timeout)
1755{
1756 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1757 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1758 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301759 unsigned long wait_timeout =
1760 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +05301761 unsigned long wait_time;
1762 unsigned long wait_time_part;
1763 unsigned int msecs;
1764 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07001765 unsigned int msecs_part = KGSL_TIMEOUT_PART;
1766 unsigned int prev_reg_val[hang_detect_regs_count];
1767
1768 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769
Tarun Karrad526d1e2012-06-11 17:51:09 -07001770 /* Restrict timeout value between adreno_dev->wait_timeout and 0 */
1771 if ((timeout == 0) || (timeout > adreno_dev->wait_timeout))
1772 msecs = adreno_dev->wait_timeout;
1773 else
1774 msecs = timeout;
1775
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001776 kgsl_cffdump_regpoll(device->id,
1777 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001778 0x00000000, 0x80000000);
1779 /* first, wait until the CP has consumed all the commands in
1780 * the ring buffer
1781 */
1782retry:
1783 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +05301784 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Lynus Vaz284d1042012-01-31 16:32:31 +05301785 wait_time = jiffies + wait_timeout;
1786 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -07001787 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001788 do {
Lynus Vaz284d1042012-01-31 16:32:31 +05301789 if (time_after(jiffies, wait_time_part)) {
1790 adreno_poke(device);
1791 wait_time_part = jiffies +
1792 msecs_to_jiffies(msecs_part);
Tarun Karra3335f142012-06-19 14:11:48 -07001793 if ((adreno_hang_detect(device, prev_reg_val)))
1794 goto err;
Lynus Vaz284d1042012-01-31 16:32:31 +05301795 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001796 GSL_RB_GET_READPTR(rb, &rb->rptr);
1797 if (time_after(jiffies, wait_time)) {
1798 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1799 rb->rptr, rb->wptr);
1800 goto err;
1801 }
1802 } while (rb->rptr != rb->wptr);
1803 }
1804
1805 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301806 wait_time = jiffies + wait_timeout;
Tarun Karra3335f142012-06-19 14:11:48 -07001807 wait_time_part = jiffies + msecs_to_jiffies(msecs_part);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001808 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001809 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1810 &rbbm_status);
1811 if (adreno_is_a2xx(adreno_dev)) {
1812 if (rbbm_status == 0x110)
1813 return 0;
1814 } else {
1815 if (!(rbbm_status & 0x80000000))
1816 return 0;
1817 }
Tarun Karra3335f142012-06-19 14:11:48 -07001818
1819 /* Dont wait for timeout, detect hang faster.
1820 */
1821 if (time_after(jiffies, wait_time_part)) {
1822 wait_time_part = jiffies +
1823 msecs_to_jiffies(msecs_part);
1824 if ((adreno_hang_detect(device, prev_reg_val)))
1825 goto err;
1826 }
1827
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001828 }
1829
1830err:
1831 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001832 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1833 !adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301834 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001835 goto retry;
1836 }
1837 return -ETIMEDOUT;
1838}
1839
1840static unsigned int adreno_isidle(struct kgsl_device *device)
1841{
1842 int status = false;
1843 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1844 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1845 unsigned int rbbm_status;
1846
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001847 WARN_ON(device->state == KGSL_STATE_INIT);
1848 /* If the device isn't active, don't force it on. */
1849 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001850 /* Is the ring buffer is empty? */
1851 GSL_RB_GET_READPTR(rb, &rb->rptr);
1852 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1853 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001854 adreno_regread(device,
1855 adreno_dev->gpudev->reg_rbbm_status,
1856 &rbbm_status);
1857
1858 if (adreno_is_a2xx(adreno_dev)) {
1859 if (rbbm_status == 0x110)
1860 status = true;
1861 } else {
1862 if (!(rbbm_status & 0x80000000))
1863 status = true;
1864 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001865 }
1866 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001867 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001868 }
1869 return status;
1870}
1871
1872/* Caller must hold the device mutex. */
1873static int adreno_suspend_context(struct kgsl_device *device)
1874{
1875 int status = 0;
1876 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1877
1878 /* switch to NULL ctxt */
1879 if (adreno_dev->drawctxt_active != NULL) {
1880 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1881 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1882 }
1883
1884 return status;
1885}
1886
Jordan Crouse233b2092012-04-18 09:31:09 -06001887/* Find a memory structure attached to an adreno context */
1888
1889struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1890 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1891{
1892 struct kgsl_context *context;
1893 struct adreno_context *adreno_context = NULL;
1894 int next = 0;
1895
1896 while (1) {
1897 context = idr_get_next(&device->context_idr, &next);
1898 if (context == NULL)
1899 break;
1900
1901 adreno_context = (struct adreno_context *)context->devctxt;
1902
1903 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1904 struct kgsl_memdesc *desc;
1905
1906 desc = &adreno_context->gpustate;
1907 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1908 return desc;
1909
1910 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1911 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1912 return desc;
1913 }
1914 next = next + 1;
1915 }
1916
1917 return NULL;
1918}
1919
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001920struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001921 unsigned int pt_base,
1922 unsigned int gpuaddr,
1923 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001925 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001926 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1927 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1928
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001929 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1930 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001931
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001932 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1933 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001935 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1936 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001937
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001938 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1939 size))
1940 return &device->mmu.setstate_memory;
1941
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001942 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1943
1944 if (entry)
1945 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001946
Jordan Crouse233b2092012-04-18 09:31:09 -06001947 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001948}
1949
1950uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1951 unsigned int gpuaddr, unsigned int size)
1952{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001953 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001954
1955 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1956
1957 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001958}
1959
1960void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1961 unsigned int *value)
1962{
1963 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06001964 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
1965 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001966
1967 if (!in_interrupt())
1968 kgsl_pre_hwaccess(device);
1969
1970 /*ensure this read finishes before the next one.
1971 * i.e. act like normal readl() */
1972 *value = __raw_readl(reg);
1973 rmb();
1974}
1975
1976void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1977 unsigned int value)
1978{
1979 unsigned int *reg;
1980
Jordan Crouse7501d452012-04-19 08:58:44 -06001981 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001982
1983 if (!in_interrupt())
1984 kgsl_pre_hwaccess(device);
1985
1986 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06001987 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001988
1989 /*ensure previous writes post before this one,
1990 * i.e. act like normal writel() */
1991 wmb();
1992 __raw_writel(value, reg);
1993}
1994
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001995static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1996{
1997 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001998 if (k_ctxt != NULL) {
1999 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002000 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2001 context_id = KGSL_CONTEXT_INVALID;
2002 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2003 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002004 }
2005
2006 return context_id;
2007}
2008
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002010 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002011{
2012 int status;
2013 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002014 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002015 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002016
2017 mutex_lock(&device->mutex);
2018 context_id = _get_context_id(context);
2019 /*
2020 * If the context ID is invalid, we are in a race with
2021 * the context being destroyed by userspace so bail.
2022 */
2023 if (context_id == KGSL_CONTEXT_INVALID) {
2024 KGSL_DRV_WARN(device, "context was detached");
2025 status = -EINVAL;
2026 goto unlock;
2027 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002028
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002029 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002030 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002031 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002032 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002033 mb();
2034
2035 if (enableflag) {
2036 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002037 KGSL_MEMSTORE_OFFSET(context_id,
2038 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002039 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002040 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002041 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002042 KGSL_MEMSTORE_OFFSET(context_id,
2043 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002044 wmb();
2045 }
2046 } else {
2047 unsigned int cmds[2];
2048 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002049 KGSL_MEMSTORE_OFFSET(context_id,
2050 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002051 enableflag = 1;
2052 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002053 KGSL_MEMSTORE_OFFSET(context_id,
2054 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002055 wmb();
2056 /* submit a dummy packet so that even if all
2057 * commands upto timestamp get executed we will still
2058 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002059 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002060 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002061
2062 if (adreno_dev->drawctxt_active)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002063 adreno_ringbuffer_issuecmds_intr(device,
2064 context, &cmds[0], 2);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002065 else
2066 /* We would never call this function if there
2067 * was no active contexts running */
2068 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002069 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002070 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002071unlock:
2072 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002073
2074 return status;
2075}
2076
2077/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002078 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002079 placing a process in wait q. For conditional interrupts we expect the
2080 process to already be in its wait q when its exit condition checking
2081 function is called.
2082*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002083#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002084({ \
2085 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002086 if (io) \
2087 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2088 else \
2089 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002090 __ret; \
2091})
2092
Tarun Karra3335f142012-06-19 14:11:48 -07002093
2094
2095unsigned int adreno_hang_detect(struct kgsl_device *device,
2096 unsigned int *prev_reg_val)
2097{
2098 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2099 unsigned int curr_reg_val[hang_detect_regs_count];
2100 unsigned int hang_detected = 1;
2101 unsigned int i;
2102
2103 if (!adreno_dev->fast_hang_detect)
2104 return 0;
2105
2106 for (i = 0; i < hang_detect_regs_count; i++) {
2107 adreno_regread(device, hang_detect_regs[i],
2108 &curr_reg_val[i]);
2109 if (curr_reg_val[i] != prev_reg_val[i]) {
2110 prev_reg_val[i] = curr_reg_val[i];
2111 hang_detected = 0;
2112 }
2113 }
2114
2115 return hang_detected;
2116}
2117
2118
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002119/* MUST be called with the device mutex held */
2120static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002121 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002122 unsigned int timestamp,
2123 unsigned int msecs)
2124{
2125 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06002126 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06002127 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002128 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06002129 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07002130 int retries = 0;
Lynus Vaz06a9a902011-10-04 19:25:33 +05302131 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07002132 unsigned int msecs_part = KGSL_TIMEOUT_PART;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002133 unsigned int ts_issued;
2134 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07002135 unsigned int time_elapsed = 0;
2136 unsigned int prev_reg_val[hang_detect_regs_count];
2137
2138 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002139
2140 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002141
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302142 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07002143 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302144 msecs = adreno_dev->wait_timeout;
2145
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002146 if (timestamp_cmp(timestamp, ts_issued) > 0) {
2147 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
2148 "last issued ts <%d:0x%x>\n",
2149 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002150 status = -EINVAL;
2151 goto done;
2152 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002153
Lynus Vaz06a9a902011-10-04 19:25:33 +05302154 /* Keep the first timeout as 100msecs before rewriting
2155 * the WPTR. Less visible impact if the WPTR has not
2156 * been updated properly.
2157 */
2158 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Tarun Karra3335f142012-06-19 14:11:48 -07002159 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002160 /*
2161 * If the context ID is invalid, we are in a race with
2162 * the context being destroyed by userspace so bail.
2163 */
2164 if (context_id == KGSL_CONTEXT_INVALID) {
2165 KGSL_DRV_WARN(device, "context was detached");
2166 status = -EINVAL;
2167 goto done;
2168 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002169 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07002170 /* if the timestamp happens while we're not
2171 * waiting, there's a chance that an interrupt
2172 * will not be generated and thus the timestamp
2173 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05302174 */
Jeremy Gebben63904832012-02-07 16:10:55 -07002175 queue_work(device->work_queue, &device->ts_expired_ws);
2176 status = 0;
2177 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178 }
Jeremy Gebben63904832012-02-07 16:10:55 -07002179 adreno_poke(device);
2180 io_cnt = (io_cnt + 1) % 100;
2181 if (io_cnt <
2182 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2183 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002184
2185 if ((retries > 0) &&
2186 (adreno_hang_detect(device, prev_reg_val)))
2187 goto hang_dump;
2188
Jeremy Gebben63904832012-02-07 16:10:55 -07002189 mutex_unlock(&device->mutex);
2190 /* We need to make sure that the process is
2191 * placed in wait-q before its condition is called
2192 */
2193 status = kgsl_wait_event_interruptible_timeout(
2194 device->wait_queue,
2195 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002196 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07002197 msecs_to_jiffies(retries ?
2198 msecs_part : msecs_first), io);
2199 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002200
Jeremy Gebben63904832012-02-07 16:10:55 -07002201 if (status > 0) {
2202 /*completed before the wait finished */
2203 status = 0;
2204 goto done;
2205 } else if (status < 0) {
2206 /*an error occurred*/
2207 goto done;
2208 }
2209 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07002210
2211 time_elapsed = time_elapsed +
2212 (retries ? msecs_part : msecs_first);
2213 retries++;
2214
2215 } while (time_elapsed < msecs);
2216
2217hang_dump:
Shubhraprakash Das54396e52012-03-10 13:24:54 -07002218 /*
2219 * Check if timestamp has retired here because we may have hit
2220 * recovery which can take some time and cause waiting threads
2221 * to timeout
2222 */
2223 if (kgsl_check_timestamp(device, context, timestamp))
2224 goto done;
Jeremy Gebben63904832012-02-07 16:10:55 -07002225 status = -ETIMEDOUT;
2226 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002227 "Device hang detected while waiting for timestamp: "
2228 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2229 "wptr: 0x%x\n",
2230 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002231 adreno_dev->ringbuffer.wptr);
2232 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002233 /* The timestamp that this process wanted
2234 * to wait on may be invalid or expired now
2235 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07002236 status = 0;
2237 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002238done:
2239 return (int)status;
2240}
2241
2242static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002243 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002244{
2245 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002246 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002247
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002248 /*
2249 * If the context ID is invalid, we are in a race with
2250 * the context being destroyed by userspace so bail.
2251 */
2252 if (context_id == KGSL_CONTEXT_INVALID) {
2253 KGSL_DRV_WARN(device, "context was detached");
2254 return timestamp;
2255 }
Jordan Crousec659f382012-04-16 11:10:41 -06002256 switch (type) {
2257 case KGSL_TIMESTAMP_QUEUED: {
2258 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2259 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2260
2261 timestamp = rb->timestamp[context_id];
2262 break;
2263 }
2264 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002265 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002266 break;
2267 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002268 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002269 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2270 break;
2271 }
2272
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002273 rmb();
2274
2275 return timestamp;
2276}
2277
2278static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2279 unsigned int cmd, void *data)
2280{
2281 int result = 0;
2282 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2283 struct kgsl_context *context;
2284
2285 switch (cmd) {
2286 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2287 binbase = data;
2288
2289 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2290 if (context) {
2291 adreno_drawctxt_set_bin_base_offset(
2292 dev_priv->device, context, binbase->offset);
2293 } else {
2294 result = -EINVAL;
2295 KGSL_DRV_ERR(dev_priv->device,
2296 "invalid drawctxt drawctxt_id %d "
2297 "device_id=%d\n",
2298 binbase->drawctxt_id, dev_priv->device->id);
2299 }
2300 break;
2301
2302 default:
2303 KGSL_DRV_INFO(dev_priv->device,
2304 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002305 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002306 break;
2307 }
2308 return result;
2309
2310}
2311
2312static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2313{
2314 gpu_freq /= 1000000;
2315 return ticks / gpu_freq;
2316}
2317
2318static void adreno_power_stats(struct kgsl_device *device,
2319 struct kgsl_power_stats *stats)
2320{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002321 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002322 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002323 unsigned int cycles;
2324
2325 /* Get the busy cycles counted since the counter was last reset */
2326 /* Calling this function also resets and restarts the counter */
2327
2328 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002329
2330 /* In order to calculate idle you have to have run the algorithm *
2331 * at least once to get a start time. */
2332 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002333 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002334 stats->total_time = tmp - pwr->time;
2335 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002336 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002337 pwrlevels[device->pwrctrl.active_pwrlevel].
2338 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002339 } else {
2340 stats->total_time = 0;
2341 stats->busy_time = 0;
2342 pwr->time = ktime_to_us(ktime_get());
2343 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002344}
2345
2346void adreno_irqctrl(struct kgsl_device *device, int state)
2347{
Jordan Crousea78c9172011-07-11 13:14:09 -06002348 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2349 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002350}
2351
Jordan Croused6535882012-06-20 08:22:16 -06002352static unsigned int adreno_gpuid(struct kgsl_device *device,
2353 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002354{
2355 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2356
Jordan Croused6535882012-06-20 08:22:16 -06002357 /* Some applications need to know the chip ID too, so pass
2358 * that as a parameter */
2359
2360 if (chipid != NULL)
2361 *chipid = adreno_dev->chip_id;
2362
Jordan Crousea0758f22011-12-07 11:19:22 -07002363 /* Standard KGSL gpuid format:
2364 * top word is 0x0002 for 2D or 0x0003 for 3D
2365 * Bottom word is core specific identifer
2366 */
2367
2368 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2369}
2370
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002371static const struct kgsl_functable adreno_functable = {
2372 /* Mandatory functions */
2373 .regread = adreno_regread,
2374 .regwrite = adreno_regwrite,
2375 .idle = adreno_idle,
2376 .isidle = adreno_isidle,
2377 .suspend_context = adreno_suspend_context,
2378 .start = adreno_start,
2379 .stop = adreno_stop,
2380 .getproperty = adreno_getproperty,
2381 .waittimestamp = adreno_waittimestamp,
2382 .readtimestamp = adreno_readtimestamp,
2383 .issueibcmds = adreno_ringbuffer_issueibcmds,
2384 .ioctl = adreno_ioctl,
2385 .setup_pt = adreno_setup_pt,
2386 .cleanup_pt = adreno_cleanup_pt,
2387 .power_stats = adreno_power_stats,
2388 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002389 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002390 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002391 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002392 /* Optional functions */
2393 .setstate = adreno_setstate,
2394 .drawctxt_create = adreno_drawctxt_create,
2395 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002396 .setproperty = adreno_setproperty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002397};
2398
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002399static struct platform_driver adreno_platform_driver = {
2400 .probe = adreno_probe,
2401 .remove = __devexit_p(adreno_remove),
2402 .suspend = kgsl_suspend_driver,
2403 .resume = kgsl_resume_driver,
2404 .id_table = adreno_id_table,
2405 .driver = {
2406 .owner = THIS_MODULE,
2407 .name = DEVICE_3D_NAME,
2408 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002409 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002410 }
2411};
2412
2413static int __init kgsl_3d_init(void)
2414{
2415 return platform_driver_register(&adreno_platform_driver);
2416}
2417
2418static void __exit kgsl_3d_exit(void)
2419{
2420 platform_driver_unregister(&adreno_platform_driver);
2421}
2422
2423module_init(kgsl_3d_init);
2424module_exit(kgsl_3d_exit);
2425
2426MODULE_DESCRIPTION("3D Graphics driver");
2427MODULE_VERSION("1.2");
2428MODULE_LICENSE("GPL v2");
2429MODULE_ALIAS("platform:kgsl_3d");