blob: 6e80907d940f12ea8f8ab209b899713f1028c6dc [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>
18
19#include <mach/socinfo.h>
20
21#include "kgsl.h"
22#include "kgsl_pwrscale.h"
23#include "kgsl_cffdump.h"
24#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060025#include "kgsl_iommu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27#include "adreno.h"
28#include "adreno_pm4types.h"
29#include "adreno_debugfs.h"
30#include "adreno_postmortem.h"
31
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070032#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070033#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034
35#define DRIVER_VERSION_MAJOR 3
36#define DRIVER_VERSION_MINOR 1
37
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038/* Adreno MH arbiter config*/
39#define ADRENO_CFG_MHARB \
40 (0x10 \
41 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
42 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
43 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
44 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
45 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
46 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
47 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
48 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
49 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
53 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
54 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
55
56#define ADRENO_MMU_CONFIG \
57 (0x01 \
58 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
59 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
60 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
61 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
62 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
69
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070static const struct kgsl_functable adreno_functable;
71
72static struct adreno_device device_3d0 = {
73 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070074 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075 .name = DEVICE_3D0_NAME,
76 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060077 .mh = {
78 .mharb = ADRENO_CFG_MHARB,
79 /* Remove 1k boundary check in z470 to avoid a GPU
80 * hang. Notice that this solution won't work if
81 * both EBI and SMI are used
82 */
83 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084 /* turn off memory protection unit by setting
85 acceptable physical address range to include
86 all pages. */
87 .mpu_base = 0x00000000,
88 .mpu_range = 0xFFFFF000,
89 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060090 .mmu = {
91 .config = ADRENO_MMU_CONFIG,
92 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 .iomemname = KGSL_3D0_REG_MEMORY,
97 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -060099 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
101 .suspend = kgsl_early_suspend_driver,
102 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600104#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600106 .gmem_base = 0,
107 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108 .pfp_fw = NULL,
109 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700110 .wait_timeout = 10000, /* in milliseconds */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600111 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112};
113
Tarun Karra3335f142012-06-19 14:11:48 -0700114/* This set of registers are used for Hang detection
115 * If the values of these registers are same after
116 * KGSL_TIMEOUT_PART time, GPU hang is reported in
117 * kernel log.
118 */
119unsigned int hang_detect_regs[] = {
120 A3XX_RBBM_STATUS,
121 REG_CP_RB_RPTR,
122 REG_CP_IB1_BASE,
123 REG_CP_IB1_BUFSZ,
124 REG_CP_IB2_BASE,
125 REG_CP_IB2_BUFSZ,
126};
127
128const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700129
Jordan Crouse505df9c2011-07-28 08:37:59 -0600130/*
131 * This is the master list of all GPU cores that are supported by this
132 * driver.
133 */
134
135#define ANY_ID (~0)
136
137static const struct {
138 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600139 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600140 const char *pm4fw;
141 const char *pfpfw;
142 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700143 unsigned int istore_size;
144 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700145 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530146 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600147} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600148 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700149 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530150 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530151 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
152 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530153 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600154 { ADRENO_REV_A205, 0, 1, 0, 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 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600157 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700158 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530159 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600160 /*
161 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
162 * a hardware problem.
163 */
164 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700165 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530166 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700167 { ADRENO_REV_A225, 2, 2, 0, 6,
168 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530169 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600170 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700171 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530172 1536, 768, 3, SZ_512K },
173 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530174 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530175 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
176 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700177 /* A3XX doesn't use the pix_shader_start */
Jordan Croused2b30d22012-05-21 08:41:51 -0600178 { ADRENO_REV_A320, 3, 2, 0, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700179 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530180 512, 0, 2, SZ_512K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700181
Jordan Crouse505df9c2011-07-28 08:37:59 -0600182};
183
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600184static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185{
Jordan Crousea78c9172011-07-11 13:14:09 -0600186 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600187 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
Jordan Crousea78c9172011-07-11 13:14:09 -0600189 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
191 if (device->requested_state == KGSL_STATE_NONE) {
192 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700193 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194 queue_work(device->work_queue, &device->idle_check_ws);
195 } else if (device->pwrscale.policy != NULL) {
196 queue_work(device->work_queue, &device->idle_check_ws);
197 }
198 }
199
200 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800201 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 jiffies + device->pwrctrl.interval_timeout);
203 return result;
204}
205
Jordan Crouse9f739212011-07-28 08:37:57 -0600206static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207 struct kgsl_pagetable *pagetable)
208{
209 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
210 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
211
212 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
213
214 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
215
216 kgsl_mmu_unmap(pagetable, &device->memstore);
217
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600218 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219}
220
221static int adreno_setup_pt(struct kgsl_device *device,
222 struct kgsl_pagetable *pagetable)
223{
224 int result = 0;
225 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
226 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
229 GSL_PT_PAGE_RV);
230 if (result)
231 goto error;
232
233 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
234 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
235 if (result)
236 goto unmap_buffer_desc;
237
238 result = kgsl_mmu_map_global(pagetable, &device->memstore,
239 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
240 if (result)
241 goto unmap_memptrs_desc;
242
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600243 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
245 if (result)
246 goto unmap_memstore_desc;
247
248 return result;
249
250unmap_memstore_desc:
251 kgsl_mmu_unmap(pagetable, &device->memstore);
252
253unmap_memptrs_desc:
254 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
255
256unmap_buffer_desc:
257 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
258
259error:
260 return result;
261}
262
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600263static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600264 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600265 uint32_t flags)
266{
267 unsigned int pt_val, reg_pt_val;
268 unsigned int link[200];
269 unsigned int *cmds = &link[0];
270 int sizedwords = 0;
271 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
272 struct kgsl_memdesc **reg_map_desc;
Pu Chened8cbb52012-06-04 18:18:48 -0700273 void *reg_map_array = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600274 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600275 struct kgsl_context *context;
276 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600277
278 if (!adreno_dev->drawctxt_active)
279 return kgsl_mmu_device_setstate(&device->mmu, flags);
280 num_iommu_units = kgsl_mmu_get_reg_map_desc(&device->mmu,
281 &reg_map_array);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600282
283 context = idr_find(&device->context_idr, context_id);
284 adreno_ctx = context->devctxt;
285
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600286 reg_map_desc = reg_map_array;
287
288 if (kgsl_mmu_enable_clk(&device->mmu,
289 KGSL_IOMMU_CONTEXT_USER))
290 goto done;
291
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600292 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600293 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
294 device->mmu.setstate_memory.gpuaddr +
295 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
296 else
297 cmds += adreno_add_bank_change_cmds(cmds,
298 KGSL_IOMMU_CONTEXT_USER,
299 device->mmu.setstate_memory.gpuaddr +
300 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
301
302 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
303 pt_val = kgsl_mmu_pt_get_base_addr(device->mmu.hwpagetable);
304 /*
305 * We need to perfrom the following operations for all
306 * IOMMU units
307 */
308 for (i = 0; i < num_iommu_units; i++) {
309 reg_pt_val = (pt_val &
310 (KGSL_IOMMU_TTBR0_PA_MASK <<
311 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
312 kgsl_mmu_get_pt_lsb(&device->mmu, i,
313 KGSL_IOMMU_CONTEXT_USER);
314 /*
315 * Set address of the new pagetable by writng to IOMMU
316 * TTBR0 register
317 */
318 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
319 *cmds++ = reg_map_desc[i]->gpuaddr +
320 (KGSL_IOMMU_CONTEXT_USER <<
321 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0;
322 *cmds++ = reg_pt_val;
323 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
324 *cmds++ = 0x00000000;
325
326 /*
327 * Read back the ttbr0 register as a barrier to ensure
328 * above writes have completed
329 */
330 cmds += adreno_add_read_cmds(device, cmds,
331 reg_map_desc[i]->gpuaddr +
332 (KGSL_IOMMU_CONTEXT_USER <<
333 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
334 reg_pt_val,
335 device->mmu.setstate_memory.gpuaddr +
336 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
337
338 /* set the asid */
339 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
340 *cmds++ = reg_map_desc[i]->gpuaddr +
341 (KGSL_IOMMU_CONTEXT_USER <<
342 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_CONTEXTIDR;
343 *cmds++ = kgsl_mmu_get_hwpagetable_asid(&device->mmu);
344 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
345 *cmds++ = 0x00000000;
346
347 /* Read back asid to ensure above write completes */
348 cmds += adreno_add_read_cmds(device, cmds,
349 reg_map_desc[i]->gpuaddr +
350 (KGSL_IOMMU_CONTEXT_USER <<
351 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_CONTEXTIDR,
352 kgsl_mmu_get_hwpagetable_asid(&device->mmu),
353 device->mmu.setstate_memory.gpuaddr +
354 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
355 }
356 /* invalidate all base pointers */
357 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
358 *cmds++ = 0x7fff;
359
Sunil Josephcf21e442012-07-10 15:23:13 +0530360 if (flags & KGSL_MMUFLAGS_TLBFLUSH)
361 cmds += __adreno_add_idle_indirect_cmds(cmds,
362 device->mmu.setstate_memory.gpuaddr +
363 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600364 }
365 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
366 /*
367 * tlb flush based on asid, no need to flush entire tlb
368 */
369 for (i = 0; i < num_iommu_units; i++) {
370 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
371 *cmds++ = (reg_map_desc[i]->gpuaddr +
372 (KGSL_IOMMU_CONTEXT_USER <<
373 KGSL_IOMMU_CTX_SHIFT) +
374 KGSL_IOMMU_CTX_TLBIASID);
375 *cmds++ = kgsl_mmu_get_hwpagetable_asid(&device->mmu);
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600376
377 cmds += __adreno_add_idle_indirect_cmds(cmds,
378 device->mmu.setstate_memory.gpuaddr +
379 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
380
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600381 cmds += adreno_add_read_cmds(device, cmds,
382 reg_map_desc[i]->gpuaddr +
383 (KGSL_IOMMU_CONTEXT_USER <<
384 KGSL_IOMMU_CTX_SHIFT) +
385 KGSL_IOMMU_CONTEXTIDR,
386 kgsl_mmu_get_hwpagetable_asid(&device->mmu),
387 device->mmu.setstate_memory.gpuaddr +
388 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
389 }
390 }
391
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600392 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600393 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
394 reg_map_desc[num_iommu_units - 1]->gpuaddr - PAGE_SIZE,
395 device->mmu.setstate_memory.gpuaddr +
396 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
397 else
398 cmds += adreno_add_bank_change_cmds(cmds,
399 KGSL_IOMMU_CONTEXT_PRIV,
400 device->mmu.setstate_memory.gpuaddr +
401 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
402
403 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600404 if (sizedwords) {
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600405 /*
406 * add an interrupt at the end of commands so that the smmu
407 * disable clock off function will get called
408 */
409 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
410 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
411 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600412 /* This returns the per context timestamp but we need to
413 * use the global timestamp for iommu clock disablement */
414 adreno_ringbuffer_issuecmds(device, adreno_ctx,
415 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600416 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600417 kgsl_mmu_disable_clk_on_ts(&device->mmu,
418 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600419 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600420done:
421 if (num_iommu_units)
422 kfree(reg_map_array);
423}
424
425static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600426 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600427 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700428{
429 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
430 unsigned int link[32];
431 unsigned int *cmds = &link[0];
432 int sizedwords = 0;
433 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600434 struct kgsl_context *context;
435 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600437 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530438 * Fix target freeze issue by adding TLB flush for each submit
439 * on A20X based targets.
440 */
441 if (adreno_is_a20x(adreno_dev))
442 flags |= KGSL_MMUFLAGS_TLBFLUSH;
443 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600444 * If possible, then set the state via the command stream to avoid
445 * a CPU idle. Otherwise, use the default setstate which uses register
446 * writes For CFF dump we must idle and use the registers so that it is
447 * easier to filter out the mmu accesses from the dump
448 */
449 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600450 context = idr_find(&device->context_idr, context_id);
451 adreno_ctx = context->devctxt;
452
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
454 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600455 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456 *cmds++ = 0x00000000;
457
458 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600459 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600460 *cmds++ = kgsl_mmu_pt_get_base_addr(
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600461 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462 sizedwords += 4;
463 }
464
465 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
466 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600467 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468 1);
469 *cmds++ = 0x00000000;
470 sizedwords += 2;
471 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600472 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 *cmds++ = mh_mmu_invalidate;
474 sizedwords += 2;
475 }
476
477 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600478 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479 /* HW workaround: to resolve MMU page fault interrupts
480 * caused by the VGT.It prevents the CP PFP from filling
481 * the VGT DMA request fifo too early,thereby ensuring
482 * that the VGT will not fetch vertex/bin data until
483 * after the page table base register has been updated.
484 *
485 * Two null DRAW_INDX_BIN packets are inserted right
486 * after the page table base update, followed by a
487 * wait for idle. The null packets will fill up the
488 * VGT DMA request fifo and prevent any further
489 * vertex/bin updates from occurring until the wait
490 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600491 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 *cmds++ = (0x4 << 16) |
493 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
494 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600495 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600496 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600497 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 *cmds++ = 0; /* viz query info */
499 *cmds++ = 0x0003C004; /* draw indicator */
500 *cmds++ = 0; /* bin base */
501 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600502 *cmds++ =
503 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600505 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 *cmds++ = 0; /* viz query info */
507 *cmds++ = 0x0003C004; /* draw indicator */
508 *cmds++ = 0; /* bin base */
509 *cmds++ = 3; /* bin size */
510 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600511 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600513 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 *cmds++ = 0x00000000;
515 sizedwords += 21;
516 }
517
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600518
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600520 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521 *cmds++ = 0x7fff; /* invalidate all base pointers */
522 sizedwords += 2;
523 }
524
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600525 adreno_ringbuffer_issuecmds(device, adreno_ctx,
526 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600528 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600529 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600530 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531}
532
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600533static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600534 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600535 uint32_t flags)
536{
537 /* call the mmu specific handler */
538 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600539 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600540 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600541 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600542}
543
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700545a3xx_getchipid(struct kgsl_device *device)
546{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700547 unsigned int majorid = 0, minorid = 0, patchid = 0;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700548
Jordan Crouse54154c62012-03-27 16:33:26 -0600549 /*
550 * We could detect the chipID from the hardware but it takes multiple
551 * registers to find the right combination. Since we traffic exclusively
552 * in system on chips, we can be (mostly) confident that a SOC version
553 * will match a GPU (at this juncture at least). So do the lazy/quick
554 * thing and set the chip_id based on the SoC
555 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700556
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530557 unsigned int version = socinfo_get_version();
558
Jordan Crouse54154c62012-03-27 16:33:26 -0600559 if (cpu_is_apq8064()) {
Jordan Croused2b30d22012-05-21 08:41:51 -0600560
Jordan Crouse54154c62012-03-27 16:33:26 -0600561 /* A320 */
562 majorid = 2;
563 minorid = 0;
Jordan Croused2b30d22012-05-21 08:41:51 -0600564
565 /*
566 * V1.1 has some GPU work arounds that we need to communicate
567 * up to user space via the patchid
568 */
569
570 if ((SOCINFO_VERSION_MAJOR(version) == 1) &&
571 (SOCINFO_VERSION_MINOR(version) == 1))
572 patchid = 1;
573 else
574 patchid = 0;
Stepan Moskovchenko0df9bb22012-07-06 18:19:15 -0700575 } else if (cpu_is_msm8930() || cpu_is_msm8930aa() || cpu_is_msm8627()) {
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530576
Jordan Crouse54154c62012-03-27 16:33:26 -0600577 /* A305 */
578 majorid = 0;
579 minorid = 5;
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530580
581 /*
582 * V1.2 has some GPU work arounds that we need to communicate
583 * up to user space via the patchid
584 */
585
586 if ((SOCINFO_VERSION_MAJOR(version) == 1) &&
587 (SOCINFO_VERSION_MINOR(version) == 2))
588 patchid = 2;
589 else
590 patchid = 0;
Jordan Crouse54154c62012-03-27 16:33:26 -0600591 }
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700592
Jordan Crouse54154c62012-03-27 16:33:26 -0600593 return (0x03 << 24) | (majorid << 16) | (minorid << 8) | patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700594}
595
596static unsigned int
597a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598{
599 unsigned int chipid = 0;
600 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700601 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
604 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
605 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
606
607 /*
608 * adreno 22x gpus are indicated by coreid 2,
609 * but REG_RBBM_PERIPHID1 always contains 0 for this field
610 */
Sudhakara Rao Tentudaebac22012-04-02 14:51:29 -0700611 if (cpu_is_msm8960() || cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612 chipid = 2 << 24;
613 else
614 chipid = (coreid & 0xF) << 24;
615
616 chipid |= ((majorid >> 4) & 0xF) << 16;
617
618 minorid = ((revid >> 0) & 0xFF);
619
620 patchid = ((revid >> 16) & 0xFF);
621
622 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700623 /* 8960v3 returns 5 for patch release, but it should be 6 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530624 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 if (cpu_is_qsd8x50())
626 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700627 else if (cpu_is_msm8960() &&
628 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
629 patchid = 6;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530630 else if (cpu_is_msm8625() && minorid == 0)
631 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632
633 chipid |= (minorid << 8) | patchid;
634
635 return chipid;
636}
637
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700638static unsigned int
639adreno_getchipid(struct kgsl_device *device)
640{
Stepan Moskovchenko0df9bb22012-07-06 18:19:15 -0700641 if (cpu_is_apq8064() || cpu_is_msm8930() || cpu_is_msm8930aa() ||
642 cpu_is_msm8627())
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700643 return a3xx_getchipid(device);
644 else
645 return a2xx_getchipid(device);
646}
647
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648static inline bool _rev_match(unsigned int id, unsigned int entry)
649{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600650 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652
653static void
654adreno_identify_gpu(struct adreno_device *adreno_dev)
655{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600656 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657
658 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
659
660 core = (adreno_dev->chip_id >> 24) & 0xff;
661 major = (adreno_dev->chip_id >> 16) & 0xff;
662 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600663 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664
Jordan Crouse505df9c2011-07-28 08:37:59 -0600665 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
666 if (core == adreno_gpulist[i].core &&
667 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600668 _rev_match(minor, adreno_gpulist[i].minor) &&
669 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671 }
672
Jordan Crouse505df9c2011-07-28 08:37:59 -0600673 if (i == ARRAY_SIZE(adreno_gpulist)) {
674 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
675 return;
676 }
677
678 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
679 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
680 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
681 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700682 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
683 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700684 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600685 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686}
687
688static int __devinit
689adreno_probe(struct platform_device *pdev)
690{
691 struct kgsl_device *device;
692 struct adreno_device *adreno_dev;
693 int status = -EINVAL;
694
695 device = (struct kgsl_device *)pdev->id_entry->driver_data;
696 adreno_dev = ADRENO_DEVICE(device);
697 device->parentdev = &pdev->dev;
698
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 status = adreno_ringbuffer_init(device);
700 if (status != 0)
701 goto error;
702
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600703 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700704 if (status)
705 goto error_close_rb;
706
707 adreno_debugfs_init(device);
708
709 kgsl_pwrscale_init(device);
710 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
711
712 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
713 return 0;
714
715error_close_rb:
716 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
717error:
718 device->parentdev = NULL;
719 return status;
720}
721
722static int __devexit adreno_remove(struct platform_device *pdev)
723{
724 struct kgsl_device *device;
725 struct adreno_device *adreno_dev;
726
727 device = (struct kgsl_device *)pdev->id_entry->driver_data;
728 adreno_dev = ADRENO_DEVICE(device);
729
730 kgsl_pwrscale_detach_policy(device);
731 kgsl_pwrscale_close(device);
732
733 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
734 kgsl_device_platform_remove(device);
735
736 return 0;
737}
738
739static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
740{
741 int status = -EINVAL;
742 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -0600744 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
745 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746
747 /* Power up the device */
748 kgsl_pwrctrl_enable(device);
749
750 /* Identify the specific GPU */
751 adreno_identify_gpu(adreno_dev);
752
Jordan Crouse505df9c2011-07-28 08:37:59 -0600753 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
754 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
755 adreno_dev->chip_id);
756 goto error_clk_off;
757 }
758
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700759 /* Set up the MMU */
760 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600761 /*
762 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
763 * on older gpus
764 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700765 if (adreno_is_a20x(adreno_dev)) {
766 device->mh.mh_intf_cfg1 = 0;
767 device->mh.mh_intf_cfg2 = 0;
768 }
769
770 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600771 }
772
Tarun Karra3335f142012-06-19 14:11:48 -0700773 /* Assign correct RBBM status register to hang detect regs
774 */
775 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
776
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700777 status = kgsl_mmu_start(device);
778 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 goto error_clk_off;
780
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700781 /* Start the GPU */
782 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783
784 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700785 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786
787 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700788 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -0600789 /* While recovery is on we do not want timer to
790 * fire and attempt to change any device state */
791 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
792 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700793 return 0;
794 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600797 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798error_clk_off:
799 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800
801 return status;
802}
803
804static int adreno_stop(struct kgsl_device *device)
805{
806 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 adreno_dev->drawctxt_active = NULL;
809
810 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
811
Shubhraprakash Das79447952012-04-26 18:12:23 -0600812 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700814 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530815 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800816 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600817
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818 /* Power down the device */
819 kgsl_pwrctrl_disable(device);
820
821 return 0;
822}
823
824static int
825adreno_recover_hang(struct kgsl_device *device)
826{
827 int ret;
828 unsigned int *rb_buffer;
829 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
830 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
831 unsigned int timestamp;
832 unsigned int num_rb_contents;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 unsigned int reftimestamp;
834 unsigned int enable_ts;
835 unsigned int soptimestamp;
836 unsigned int eoptimestamp;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700837 unsigned int context_id;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700838 struct kgsl_context *context;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700839 struct adreno_context *adreno_context;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700840 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841
842 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
843 rb_buffer = vmalloc(rb->buffer_desc.size);
844 if (!rb_buffer) {
845 KGSL_MEM_ERR(device,
846 "Failed to allocate memory for recovery: %x\n",
847 rb->buffer_desc.size);
848 return -ENOMEM;
849 }
850 /* Extract valid contents from rb which can stil be executed after
851 * hang */
852 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
853 if (ret)
854 goto done;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700855 kgsl_sharedmem_readl(&device->memstore, &context_id,
856 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
857 current_context));
858 context = idr_find(&device->context_idr, context_id);
859 if (context == NULL) {
860 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
861 context_id);
862 context_id = KGSL_MEMSTORE_GLOBAL;
863 }
864
865 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
866 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
867
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700868 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700869 KGSL_MEMSTORE_OFFSET(context_id,
870 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700872 KGSL_MEMSTORE_OFFSET(context_id,
873 ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700875 KGSL_MEMSTORE_OFFSET(context_id,
876 soptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700878 KGSL_MEMSTORE_OFFSET(context_id,
879 eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 /* Make sure memory is synchronized before restarting the GPU */
881 mb();
882 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700883 "Context id that caused a GPU hang: %d\n", context_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884 /* restart device */
885 ret = adreno_stop(device);
886 if (ret)
887 goto done;
888 ret = adreno_start(device, true);
889 if (ret)
890 goto done;
891 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
892 /* Restore timestamp states */
893 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700894 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895 soptimestamp);
896 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700897 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898 eoptimestamp);
Carter Cooperae4c7bc2012-04-10 09:40:49 -0600899
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900 if (num_rb_contents) {
901 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700902 KGSL_MEMSTORE_OFFSET(context_id, ref_wait_ts),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903 reftimestamp);
904 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700905 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 enable_ts);
907 }
908 /* Make sure all writes are posted before the GPU reads them */
909 wmb();
910 /* Mark the invalid context so no more commands are accepted from
911 * that context */
912
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700913 adreno_context = context->devctxt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914
915 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700916 "Context that caused a GPU hang: %d\n", adreno_context->id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700918 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700920 /*
921 * Set the reset status of all contexts to
922 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
923 * since thats the guilty party
924 */
925 while ((context = idr_get_next(&device->context_idr, &next))) {
926 if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
927 context->reset_status) {
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700928 if (context->id != context_id)
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700929 context->reset_status =
930 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
931 else
932 context->reset_status =
933 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
934 }
935 next = next + 1;
936 }
937
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938 /* Restore valid commands in ringbuffer */
939 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700940 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -0600941 /* wait for idle */
942 ret = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943done:
944 vfree(rb_buffer);
945 return ret;
946}
947
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600948int adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950 int result = -ETIMEDOUT;
951
952 if (device->state == KGSL_STATE_HUNG)
953 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700954 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700955 mutex_unlock(&device->mutex);
956 wait_for_completion(&device->recovery_gate);
957 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700958 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959 result = 0;
960 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700961 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700963 /* Detected a hang */
964
965
966 /*
967 * Trigger an automatic dump of the state to
968 * the console
969 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700971
972 /*
973 * Make a GPU snapshot. For now, do it after the PM dump so we
974 * can at least be sure the PM dump will work as it always has
975 */
976 kgsl_device_snapshot(device, 1);
977
Jeremy Gebben388c2972011-12-16 09:05:07 -0700978 result = adreno_recover_hang(device);
979 if (result)
980 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
981 else
982 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
983 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 }
985done:
986 return result;
987}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600988EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989
990static int adreno_getproperty(struct kgsl_device *device,
991 enum kgsl_property_type type,
992 void *value,
993 unsigned int sizebytes)
994{
995 int status = -EINVAL;
996 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
997
998 switch (type) {
999 case KGSL_PROP_DEVICE_INFO:
1000 {
1001 struct kgsl_devinfo devinfo;
1002
1003 if (sizebytes != sizeof(devinfo)) {
1004 status = -EINVAL;
1005 break;
1006 }
1007
1008 memset(&devinfo, 0, sizeof(devinfo));
1009 devinfo.device_id = device->id+1;
1010 devinfo.chip_id = adreno_dev->chip_id;
1011 devinfo.mmu_enabled = kgsl_mmu_enabled();
1012 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001013 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1014 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015
1016 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1017 0) {
1018 status = -EFAULT;
1019 break;
1020 }
1021 status = 0;
1022 }
1023 break;
1024 case KGSL_PROP_DEVICE_SHADOW:
1025 {
1026 struct kgsl_shadowprop shadowprop;
1027
1028 if (sizebytes != sizeof(shadowprop)) {
1029 status = -EINVAL;
1030 break;
1031 }
1032 memset(&shadowprop, 0, sizeof(shadowprop));
1033 if (device->memstore.hostptr) {
1034 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1035 * anything to mmap().
1036 */
1037 shadowprop.gpuaddr = device->memstore.physaddr;
1038 shadowprop.size = device->memstore.size;
1039 /* GSL needs this to be set, even if it
1040 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001041 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1042 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043 }
1044 if (copy_to_user(value, &shadowprop,
1045 sizeof(shadowprop))) {
1046 status = -EFAULT;
1047 break;
1048 }
1049 status = 0;
1050 }
1051 break;
1052 case KGSL_PROP_MMU_ENABLE:
1053 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001054 int mmu_prop = kgsl_mmu_enabled();
1055
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 if (sizebytes != sizeof(int)) {
1057 status = -EINVAL;
1058 break;
1059 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001060 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061 status = -EFAULT;
1062 break;
1063 }
1064 status = 0;
1065 }
1066 break;
1067 case KGSL_PROP_INTERRUPT_WAITS:
1068 {
1069 int int_waits = 1;
1070 if (sizebytes != sizeof(int)) {
1071 status = -EINVAL;
1072 break;
1073 }
1074 if (copy_to_user(value, &int_waits, sizeof(int))) {
1075 status = -EFAULT;
1076 break;
1077 }
1078 status = 0;
1079 }
1080 break;
1081 default:
1082 status = -EINVAL;
1083 }
1084
1085 return status;
1086}
1087
Jordan Crousef7370f82012-04-18 09:31:07 -06001088static int adreno_setproperty(struct kgsl_device *device,
1089 enum kgsl_property_type type,
1090 void *value,
1091 unsigned int sizebytes)
1092{
1093 int status = -EINVAL;
1094
1095 switch (type) {
1096 case KGSL_PROP_PWRCTRL: {
1097 unsigned int enable;
1098 struct kgsl_device_platform_data *pdata =
1099 kgsl_device_get_drvdata(device);
1100
1101 if (sizebytes != sizeof(enable))
1102 break;
1103
1104 if (copy_from_user(&enable, (void __user *) value,
1105 sizeof(enable))) {
1106 status = -EFAULT;
1107 break;
1108 }
1109
1110 if (enable) {
1111 if (pdata->nap_allowed)
1112 device->pwrctrl.nap_allowed = true;
1113
1114 kgsl_pwrscale_enable(device);
1115 } else {
1116 device->pwrctrl.nap_allowed = false;
1117 kgsl_pwrscale_disable(device);
1118 }
1119
1120 status = 0;
1121 }
1122 break;
1123 default:
1124 break;
1125 }
1126
1127 return status;
1128}
1129
Lynus Vaz06a9a902011-10-04 19:25:33 +05301130static inline void adreno_poke(struct kgsl_device *device)
1131{
1132 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1133 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1134}
1135
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001136/* Caller must hold the device mutex. */
1137int adreno_idle(struct kgsl_device *device, unsigned int timeout)
1138{
1139 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1140 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1141 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301142 unsigned long wait_timeout =
1143 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +05301144 unsigned long wait_time;
1145 unsigned long wait_time_part;
1146 unsigned int msecs;
1147 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07001148 unsigned int msecs_part = KGSL_TIMEOUT_PART;
1149 unsigned int prev_reg_val[hang_detect_regs_count];
1150
1151 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001153 kgsl_cffdump_regpoll(device->id,
1154 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155 0x00000000, 0x80000000);
1156 /* first, wait until the CP has consumed all the commands in
1157 * the ring buffer
1158 */
1159retry:
1160 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +05301161 msecs = adreno_dev->wait_timeout;
1162 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Lynus Vaz284d1042012-01-31 16:32:31 +05301163 wait_time = jiffies + wait_timeout;
1164 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -07001165 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001166 do {
Lynus Vaz284d1042012-01-31 16:32:31 +05301167 if (time_after(jiffies, wait_time_part)) {
1168 adreno_poke(device);
1169 wait_time_part = jiffies +
1170 msecs_to_jiffies(msecs_part);
Tarun Karra3335f142012-06-19 14:11:48 -07001171 if ((adreno_hang_detect(device, prev_reg_val)))
1172 goto err;
Lynus Vaz284d1042012-01-31 16:32:31 +05301173 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174 GSL_RB_GET_READPTR(rb, &rb->rptr);
1175 if (time_after(jiffies, wait_time)) {
1176 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1177 rb->rptr, rb->wptr);
1178 goto err;
1179 }
1180 } while (rb->rptr != rb->wptr);
1181 }
1182
1183 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301184 wait_time = jiffies + wait_timeout;
Tarun Karra3335f142012-06-19 14:11:48 -07001185 wait_time_part = jiffies + msecs_to_jiffies(msecs_part);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001187 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1188 &rbbm_status);
1189 if (adreno_is_a2xx(adreno_dev)) {
1190 if (rbbm_status == 0x110)
1191 return 0;
1192 } else {
1193 if (!(rbbm_status & 0x80000000))
1194 return 0;
1195 }
Tarun Karra3335f142012-06-19 14:11:48 -07001196
1197 /* Dont wait for timeout, detect hang faster.
1198 */
1199 if (time_after(jiffies, wait_time_part)) {
1200 wait_time_part = jiffies +
1201 msecs_to_jiffies(msecs_part);
1202 if ((adreno_hang_detect(device, prev_reg_val)))
1203 goto err;
1204 }
1205
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001206 }
1207
1208err:
1209 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001210 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1211 !adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301212 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213 goto retry;
1214 }
1215 return -ETIMEDOUT;
1216}
1217
1218static unsigned int adreno_isidle(struct kgsl_device *device)
1219{
1220 int status = false;
1221 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1222 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1223 unsigned int rbbm_status;
1224
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001225 WARN_ON(device->state == KGSL_STATE_INIT);
1226 /* If the device isn't active, don't force it on. */
1227 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228 /* Is the ring buffer is empty? */
1229 GSL_RB_GET_READPTR(rb, &rb->rptr);
1230 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1231 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001232 adreno_regread(device,
1233 adreno_dev->gpudev->reg_rbbm_status,
1234 &rbbm_status);
1235
1236 if (adreno_is_a2xx(adreno_dev)) {
1237 if (rbbm_status == 0x110)
1238 status = true;
1239 } else {
1240 if (!(rbbm_status & 0x80000000))
1241 status = true;
1242 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001243 }
1244 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001245 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246 }
1247 return status;
1248}
1249
1250/* Caller must hold the device mutex. */
1251static int adreno_suspend_context(struct kgsl_device *device)
1252{
1253 int status = 0;
1254 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1255
1256 /* switch to NULL ctxt */
1257 if (adreno_dev->drawctxt_active != NULL) {
1258 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1259 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1260 }
1261
1262 return status;
1263}
1264
Jordan Crouse233b2092012-04-18 09:31:09 -06001265/* Find a memory structure attached to an adreno context */
1266
1267struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1268 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1269{
1270 struct kgsl_context *context;
1271 struct adreno_context *adreno_context = NULL;
1272 int next = 0;
1273
1274 while (1) {
1275 context = idr_get_next(&device->context_idr, &next);
1276 if (context == NULL)
1277 break;
1278
1279 adreno_context = (struct adreno_context *)context->devctxt;
1280
1281 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1282 struct kgsl_memdesc *desc;
1283
1284 desc = &adreno_context->gpustate;
1285 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1286 return desc;
1287
1288 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1289 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1290 return desc;
1291 }
1292 next = next + 1;
1293 }
1294
1295 return NULL;
1296}
1297
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001298struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001299 unsigned int pt_base,
1300 unsigned int gpuaddr,
1301 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001303 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1305 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1306
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001307 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1308 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001310 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1311 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001313 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1314 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001316 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1317 size))
1318 return &device->mmu.setstate_memory;
1319
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001320 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1321
1322 if (entry)
1323 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001324
Jordan Crouse233b2092012-04-18 09:31:09 -06001325 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001326}
1327
1328uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1329 unsigned int gpuaddr, unsigned int size)
1330{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001331 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001332
1333 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1334
1335 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001336}
1337
1338void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1339 unsigned int *value)
1340{
1341 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06001342 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
1343 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001344
1345 if (!in_interrupt())
1346 kgsl_pre_hwaccess(device);
1347
1348 /*ensure this read finishes before the next one.
1349 * i.e. act like normal readl() */
1350 *value = __raw_readl(reg);
1351 rmb();
1352}
1353
1354void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1355 unsigned int value)
1356{
1357 unsigned int *reg;
1358
Jordan Crouse7501d452012-04-19 08:58:44 -06001359 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360
1361 if (!in_interrupt())
1362 kgsl_pre_hwaccess(device);
1363
1364 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06001365 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001366
1367 /*ensure previous writes post before this one,
1368 * i.e. act like normal writel() */
1369 wmb();
1370 __raw_writel(value, reg);
1371}
1372
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001373static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1374{
1375 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001376 if (k_ctxt != NULL) {
1377 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001378 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
1379 context_id = KGSL_CONTEXT_INVALID;
1380 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
1381 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001382 }
1383
1384 return context_id;
1385}
1386
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001387static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001388 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001389{
1390 int status;
1391 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001392 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001393 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001394
1395 mutex_lock(&device->mutex);
1396 context_id = _get_context_id(context);
1397 /*
1398 * If the context ID is invalid, we are in a race with
1399 * the context being destroyed by userspace so bail.
1400 */
1401 if (context_id == KGSL_CONTEXT_INVALID) {
1402 KGSL_DRV_WARN(device, "context was detached");
1403 status = -EINVAL;
1404 goto unlock;
1405 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001406
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001407 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001408 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001410 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001411 mb();
1412
1413 if (enableflag) {
1414 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001415 KGSL_MEMSTORE_OFFSET(context_id,
1416 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001418 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001420 KGSL_MEMSTORE_OFFSET(context_id,
1421 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001422 wmb();
1423 }
1424 } else {
1425 unsigned int cmds[2];
1426 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001427 KGSL_MEMSTORE_OFFSET(context_id,
1428 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001429 enableflag = 1;
1430 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001431 KGSL_MEMSTORE_OFFSET(context_id,
1432 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 wmb();
1434 /* submit a dummy packet so that even if all
1435 * commands upto timestamp get executed we will still
1436 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001437 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001438 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001439
1440 if (adreno_dev->drawctxt_active)
1441 adreno_ringbuffer_issuecmds(device,
1442 adreno_dev->drawctxt_active,
1443 KGSL_CMD_FLAGS_NONE, &cmds[0], 2);
1444 else
1445 /* We would never call this function if there
1446 * was no active contexts running */
1447 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001448 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001449 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001450unlock:
1451 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001452
1453 return status;
1454}
1455
1456/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001457 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001458 placing a process in wait q. For conditional interrupts we expect the
1459 process to already be in its wait q when its exit condition checking
1460 function is called.
1461*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001462#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001463({ \
1464 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001465 if (io) \
1466 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1467 else \
1468 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001469 __ret; \
1470})
1471
Tarun Karra3335f142012-06-19 14:11:48 -07001472
1473
1474unsigned int adreno_hang_detect(struct kgsl_device *device,
1475 unsigned int *prev_reg_val)
1476{
1477 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1478 unsigned int curr_reg_val[hang_detect_regs_count];
1479 unsigned int hang_detected = 1;
1480 unsigned int i;
1481
1482 if (!adreno_dev->fast_hang_detect)
1483 return 0;
1484
1485 for (i = 0; i < hang_detect_regs_count; i++) {
1486 adreno_regread(device, hang_detect_regs[i],
1487 &curr_reg_val[i]);
1488 if (curr_reg_val[i] != prev_reg_val[i]) {
1489 prev_reg_val[i] = curr_reg_val[i];
1490 hang_detected = 0;
1491 }
1492 }
1493
1494 return hang_detected;
1495}
1496
1497
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001498/* MUST be called with the device mutex held */
1499static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001500 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001501 unsigned int timestamp,
1502 unsigned int msecs)
1503{
1504 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001505 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001506 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001507 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001508 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07001509 int retries = 0;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301510 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07001511 unsigned int msecs_part = KGSL_TIMEOUT_PART;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001512 unsigned int ts_issued;
1513 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07001514 unsigned int time_elapsed = 0;
1515 unsigned int prev_reg_val[hang_detect_regs_count];
1516
1517 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001518
1519 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001520
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301521 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07001522 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301523 msecs = adreno_dev->wait_timeout;
1524
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001525 if (timestamp_cmp(timestamp, ts_issued) > 0) {
1526 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
1527 "last issued ts <%d:0x%x>\n",
1528 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001529 status = -EINVAL;
1530 goto done;
1531 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001532
Lynus Vaz06a9a902011-10-04 19:25:33 +05301533 /* Keep the first timeout as 100msecs before rewriting
1534 * the WPTR. Less visible impact if the WPTR has not
1535 * been updated properly.
1536 */
1537 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Tarun Karra3335f142012-06-19 14:11:48 -07001538 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001539 /*
1540 * If the context ID is invalid, we are in a race with
1541 * the context being destroyed by userspace so bail.
1542 */
1543 if (context_id == KGSL_CONTEXT_INVALID) {
1544 KGSL_DRV_WARN(device, "context was detached");
1545 status = -EINVAL;
1546 goto done;
1547 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001548 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001549 /* if the timestamp happens while we're not
1550 * waiting, there's a chance that an interrupt
1551 * will not be generated and thus the timestamp
1552 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301553 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001554 queue_work(device->work_queue, &device->ts_expired_ws);
1555 status = 0;
1556 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001557 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001558 adreno_poke(device);
1559 io_cnt = (io_cnt + 1) % 100;
1560 if (io_cnt <
1561 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1562 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07001563
1564 if ((retries > 0) &&
1565 (adreno_hang_detect(device, prev_reg_val)))
1566 goto hang_dump;
1567
Jeremy Gebben63904832012-02-07 16:10:55 -07001568 mutex_unlock(&device->mutex);
1569 /* We need to make sure that the process is
1570 * placed in wait-q before its condition is called
1571 */
1572 status = kgsl_wait_event_interruptible_timeout(
1573 device->wait_queue,
1574 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001575 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001576 msecs_to_jiffies(retries ?
1577 msecs_part : msecs_first), io);
1578 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001579
Jeremy Gebben63904832012-02-07 16:10:55 -07001580 if (status > 0) {
1581 /*completed before the wait finished */
1582 status = 0;
1583 goto done;
1584 } else if (status < 0) {
1585 /*an error occurred*/
1586 goto done;
1587 }
1588 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07001589
1590 time_elapsed = time_elapsed +
1591 (retries ? msecs_part : msecs_first);
1592 retries++;
1593
1594 } while (time_elapsed < msecs);
1595
1596hang_dump:
Jeremy Gebben63904832012-02-07 16:10:55 -07001597 status = -ETIMEDOUT;
1598 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001599 "Device hang detected while waiting for timestamp: "
1600 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
1601 "wptr: 0x%x\n",
1602 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07001603 adreno_dev->ringbuffer.wptr);
1604 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001605 /* The timestamp that this process wanted
1606 * to wait on may be invalid or expired now
1607 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07001608 status = 0;
1609 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001610done:
1611 return (int)status;
1612}
1613
1614static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001615 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001616{
1617 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001618 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001619
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001620 /*
1621 * If the context ID is invalid, we are in a race with
1622 * the context being destroyed by userspace so bail.
1623 */
1624 if (context_id == KGSL_CONTEXT_INVALID) {
1625 KGSL_DRV_WARN(device, "context was detached");
1626 return timestamp;
1627 }
Jordan Crousec659f382012-04-16 11:10:41 -06001628 switch (type) {
1629 case KGSL_TIMESTAMP_QUEUED: {
1630 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1631 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1632
1633 timestamp = rb->timestamp[context_id];
1634 break;
1635 }
1636 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06001638 break;
1639 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001640 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06001641 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
1642 break;
1643 }
1644
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001645 rmb();
1646
1647 return timestamp;
1648}
1649
1650static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1651 unsigned int cmd, void *data)
1652{
1653 int result = 0;
1654 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1655 struct kgsl_context *context;
1656
1657 switch (cmd) {
1658 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1659 binbase = data;
1660
1661 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1662 if (context) {
1663 adreno_drawctxt_set_bin_base_offset(
1664 dev_priv->device, context, binbase->offset);
1665 } else {
1666 result = -EINVAL;
1667 KGSL_DRV_ERR(dev_priv->device,
1668 "invalid drawctxt drawctxt_id %d "
1669 "device_id=%d\n",
1670 binbase->drawctxt_id, dev_priv->device->id);
1671 }
1672 break;
1673
1674 default:
1675 KGSL_DRV_INFO(dev_priv->device,
1676 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001677 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001678 break;
1679 }
1680 return result;
1681
1682}
1683
1684static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1685{
1686 gpu_freq /= 1000000;
1687 return ticks / gpu_freq;
1688}
1689
1690static void adreno_power_stats(struct kgsl_device *device,
1691 struct kgsl_power_stats *stats)
1692{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001693 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001694 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001695 unsigned int cycles;
1696
1697 /* Get the busy cycles counted since the counter was last reset */
1698 /* Calling this function also resets and restarts the counter */
1699
1700 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001701
1702 /* In order to calculate idle you have to have run the algorithm *
1703 * at least once to get a start time. */
1704 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001705 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001706 stats->total_time = tmp - pwr->time;
1707 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001708 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001709 pwrlevels[device->pwrctrl.active_pwrlevel].
1710 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001711 } else {
1712 stats->total_time = 0;
1713 stats->busy_time = 0;
1714 pwr->time = ktime_to_us(ktime_get());
1715 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001716}
1717
1718void adreno_irqctrl(struct kgsl_device *device, int state)
1719{
Jordan Crousea78c9172011-07-11 13:14:09 -06001720 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1721 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722}
1723
Jordan Croused6535882012-06-20 08:22:16 -06001724static unsigned int adreno_gpuid(struct kgsl_device *device,
1725 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07001726{
1727 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1728
Jordan Croused6535882012-06-20 08:22:16 -06001729 /* Some applications need to know the chip ID too, so pass
1730 * that as a parameter */
1731
1732 if (chipid != NULL)
1733 *chipid = adreno_dev->chip_id;
1734
Jordan Crousea0758f22011-12-07 11:19:22 -07001735 /* Standard KGSL gpuid format:
1736 * top word is 0x0002 for 2D or 0x0003 for 3D
1737 * Bottom word is core specific identifer
1738 */
1739
1740 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1741}
1742
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001743static const struct kgsl_functable adreno_functable = {
1744 /* Mandatory functions */
1745 .regread = adreno_regread,
1746 .regwrite = adreno_regwrite,
1747 .idle = adreno_idle,
1748 .isidle = adreno_isidle,
1749 .suspend_context = adreno_suspend_context,
1750 .start = adreno_start,
1751 .stop = adreno_stop,
1752 .getproperty = adreno_getproperty,
1753 .waittimestamp = adreno_waittimestamp,
1754 .readtimestamp = adreno_readtimestamp,
1755 .issueibcmds = adreno_ringbuffer_issueibcmds,
1756 .ioctl = adreno_ioctl,
1757 .setup_pt = adreno_setup_pt,
1758 .cleanup_pt = adreno_cleanup_pt,
1759 .power_stats = adreno_power_stats,
1760 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001761 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001762 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001763 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001764 /* Optional functions */
1765 .setstate = adreno_setstate,
1766 .drawctxt_create = adreno_drawctxt_create,
1767 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06001768 .setproperty = adreno_setproperty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769};
1770
1771static struct platform_device_id adreno_id_table[] = {
1772 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1773 { },
1774};
1775MODULE_DEVICE_TABLE(platform, adreno_id_table);
1776
1777static struct platform_driver adreno_platform_driver = {
1778 .probe = adreno_probe,
1779 .remove = __devexit_p(adreno_remove),
1780 .suspend = kgsl_suspend_driver,
1781 .resume = kgsl_resume_driver,
1782 .id_table = adreno_id_table,
1783 .driver = {
1784 .owner = THIS_MODULE,
1785 .name = DEVICE_3D_NAME,
1786 .pm = &kgsl_pm_ops,
1787 }
1788};
1789
1790static int __init kgsl_3d_init(void)
1791{
1792 return platform_driver_register(&adreno_platform_driver);
1793}
1794
1795static void __exit kgsl_3d_exit(void)
1796{
1797 platform_driver_unregister(&adreno_platform_driver);
1798}
1799
1800module_init(kgsl_3d_init);
1801module_exit(kgsl_3d_exit);
1802
1803MODULE_DESCRIPTION("3D Graphics driver");
1804MODULE_VERSION("1.2");
1805MODULE_LICENSE("GPL v2");
1806MODULE_ALIAS("platform:kgsl_3d");