blob: b280183bab923fbd67bcabfbb4aa17a6c0109def [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
Jordan Crouse95b33272011-11-11 14:50:12 -0700114
Jordan Crouse505df9c2011-07-28 08:37:59 -0600115/*
116 * This is the master list of all GPU cores that are supported by this
117 * driver.
118 */
119
120#define ANY_ID (~0)
121
122static const struct {
123 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600124 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600125 const char *pm4fw;
126 const char *pfpfw;
127 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700128 unsigned int istore_size;
129 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700130 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530131 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600132} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600133 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700134 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530135 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530136 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
137 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530138 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600139 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700140 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530141 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600142 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700143 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530144 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600145 /*
146 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
147 * a hardware problem.
148 */
149 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700150 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530151 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700152 { ADRENO_REV_A225, 2, 2, 0, 6,
153 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530154 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600155 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700156 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530157 1536, 768, 3, SZ_512K },
158 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530159 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530160 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
161 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700162 /* A3XX doesn't use the pix_shader_start */
Jordan Croused2b30d22012-05-21 08:41:51 -0600163 { ADRENO_REV_A320, 3, 2, 0, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700164 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530165 512, 0, 2, SZ_512K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700166
Jordan Crouse505df9c2011-07-28 08:37:59 -0600167};
168
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600169static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170{
Jordan Crousea78c9172011-07-11 13:14:09 -0600171 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600172 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173
Jordan Crousea78c9172011-07-11 13:14:09 -0600174 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175
176 if (device->requested_state == KGSL_STATE_NONE) {
177 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700178 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 queue_work(device->work_queue, &device->idle_check_ws);
180 } else if (device->pwrscale.policy != NULL) {
181 queue_work(device->work_queue, &device->idle_check_ws);
182 }
183 }
184
185 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800186 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187 jiffies + device->pwrctrl.interval_timeout);
188 return result;
189}
190
Jordan Crouse9f739212011-07-28 08:37:57 -0600191static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192 struct kgsl_pagetable *pagetable)
193{
194 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
195 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
196
197 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
198
199 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
200
201 kgsl_mmu_unmap(pagetable, &device->memstore);
202
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600203 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204}
205
206static int adreno_setup_pt(struct kgsl_device *device,
207 struct kgsl_pagetable *pagetable)
208{
209 int result = 0;
210 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
211 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
212
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
214 GSL_PT_PAGE_RV);
215 if (result)
216 goto error;
217
218 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
219 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
220 if (result)
221 goto unmap_buffer_desc;
222
223 result = kgsl_mmu_map_global(pagetable, &device->memstore,
224 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
225 if (result)
226 goto unmap_memptrs_desc;
227
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600228 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
230 if (result)
231 goto unmap_memstore_desc;
232
233 return result;
234
235unmap_memstore_desc:
236 kgsl_mmu_unmap(pagetable, &device->memstore);
237
238unmap_memptrs_desc:
239 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
240
241unmap_buffer_desc:
242 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
243
244error:
245 return result;
246}
247
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600248static void adreno_iommu_setstate(struct kgsl_device *device,
249 uint32_t flags)
250{
251 unsigned int pt_val, reg_pt_val;
252 unsigned int link[200];
253 unsigned int *cmds = &link[0];
254 int sizedwords = 0;
255 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
256 struct kgsl_memdesc **reg_map_desc;
Pu Chened8cbb52012-06-04 18:18:48 -0700257 void *reg_map_array = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600258 int num_iommu_units, i;
259
260 if (!adreno_dev->drawctxt_active)
261 return kgsl_mmu_device_setstate(&device->mmu, flags);
262 num_iommu_units = kgsl_mmu_get_reg_map_desc(&device->mmu,
263 &reg_map_array);
264 reg_map_desc = reg_map_array;
265
266 if (kgsl_mmu_enable_clk(&device->mmu,
267 KGSL_IOMMU_CONTEXT_USER))
268 goto done;
269
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600270 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600271 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
272 device->mmu.setstate_memory.gpuaddr +
273 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
274 else
275 cmds += adreno_add_bank_change_cmds(cmds,
276 KGSL_IOMMU_CONTEXT_USER,
277 device->mmu.setstate_memory.gpuaddr +
278 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
279
280 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
281 pt_val = kgsl_mmu_pt_get_base_addr(device->mmu.hwpagetable);
282 /*
283 * We need to perfrom the following operations for all
284 * IOMMU units
285 */
286 for (i = 0; i < num_iommu_units; i++) {
287 reg_pt_val = (pt_val &
288 (KGSL_IOMMU_TTBR0_PA_MASK <<
289 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
290 kgsl_mmu_get_pt_lsb(&device->mmu, i,
291 KGSL_IOMMU_CONTEXT_USER);
292 /*
293 * Set address of the new pagetable by writng to IOMMU
294 * TTBR0 register
295 */
296 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
297 *cmds++ = reg_map_desc[i]->gpuaddr +
298 (KGSL_IOMMU_CONTEXT_USER <<
299 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0;
300 *cmds++ = reg_pt_val;
301 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
302 *cmds++ = 0x00000000;
303
304 /*
305 * Read back the ttbr0 register as a barrier to ensure
306 * above writes have completed
307 */
308 cmds += adreno_add_read_cmds(device, cmds,
309 reg_map_desc[i]->gpuaddr +
310 (KGSL_IOMMU_CONTEXT_USER <<
311 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
312 reg_pt_val,
313 device->mmu.setstate_memory.gpuaddr +
314 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
315
316 /* set the asid */
317 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
318 *cmds++ = reg_map_desc[i]->gpuaddr +
319 (KGSL_IOMMU_CONTEXT_USER <<
320 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_CONTEXTIDR;
321 *cmds++ = kgsl_mmu_get_hwpagetable_asid(&device->mmu);
322 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
323 *cmds++ = 0x00000000;
324
325 /* Read back asid to ensure above write completes */
326 cmds += adreno_add_read_cmds(device, cmds,
327 reg_map_desc[i]->gpuaddr +
328 (KGSL_IOMMU_CONTEXT_USER <<
329 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_CONTEXTIDR,
330 kgsl_mmu_get_hwpagetable_asid(&device->mmu),
331 device->mmu.setstate_memory.gpuaddr +
332 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
333 }
334 /* invalidate all base pointers */
335 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
336 *cmds++ = 0x7fff;
337
338 if (flags & KGSL_MMUFLAGS_TLBFLUSH)
339 cmds += __adreno_add_idle_indirect_cmds(cmds,
340 device->mmu.setstate_memory.gpuaddr +
341 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
342 }
343 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
344 /*
345 * tlb flush based on asid, no need to flush entire tlb
346 */
347 for (i = 0; i < num_iommu_units; i++) {
348 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
349 *cmds++ = (reg_map_desc[i]->gpuaddr +
350 (KGSL_IOMMU_CONTEXT_USER <<
351 KGSL_IOMMU_CTX_SHIFT) +
352 KGSL_IOMMU_CTX_TLBIASID);
353 *cmds++ = kgsl_mmu_get_hwpagetable_asid(&device->mmu);
354 cmds += adreno_add_read_cmds(device, cmds,
355 reg_map_desc[i]->gpuaddr +
356 (KGSL_IOMMU_CONTEXT_USER <<
357 KGSL_IOMMU_CTX_SHIFT) +
358 KGSL_IOMMU_CONTEXTIDR,
359 kgsl_mmu_get_hwpagetable_asid(&device->mmu),
360 device->mmu.setstate_memory.gpuaddr +
361 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
362 }
363 }
364
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600365 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600366 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
367 reg_map_desc[num_iommu_units - 1]->gpuaddr - PAGE_SIZE,
368 device->mmu.setstate_memory.gpuaddr +
369 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
370 else
371 cmds += adreno_add_bank_change_cmds(cmds,
372 KGSL_IOMMU_CONTEXT_PRIV,
373 device->mmu.setstate_memory.gpuaddr +
374 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
375
376 sizedwords += (cmds - &link[0]);
377 if (sizedwords)
378 adreno_ringbuffer_issuecmds(device,
379 KGSL_CMD_FLAGS_PMODE, &link[0], sizedwords);
380done:
381 if (num_iommu_units)
382 kfree(reg_map_array);
383}
384
385static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600386 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387{
388 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
389 unsigned int link[32];
390 unsigned int *cmds = &link[0];
391 int sizedwords = 0;
392 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
393
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600394 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530395 * Fix target freeze issue by adding TLB flush for each submit
396 * on A20X based targets.
397 */
398 if (adreno_is_a20x(adreno_dev))
399 flags |= KGSL_MMUFLAGS_TLBFLUSH;
400 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600401 * If possible, then set the state via the command stream to avoid
402 * a CPU idle. Otherwise, use the default setstate which uses register
403 * writes For CFF dump we must idle and use the registers so that it is
404 * easier to filter out the mmu accesses from the dump
405 */
406 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
408 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600409 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410 *cmds++ = 0x00000000;
411
412 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600413 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600414 *cmds++ = kgsl_mmu_pt_get_base_addr(
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600415 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416 sizedwords += 4;
417 }
418
419 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
420 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600421 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422 1);
423 *cmds++ = 0x00000000;
424 sizedwords += 2;
425 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600426 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 *cmds++ = mh_mmu_invalidate;
428 sizedwords += 2;
429 }
430
431 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600432 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 /* HW workaround: to resolve MMU page fault interrupts
434 * caused by the VGT.It prevents the CP PFP from filling
435 * the VGT DMA request fifo too early,thereby ensuring
436 * that the VGT will not fetch vertex/bin data until
437 * after the page table base register has been updated.
438 *
439 * Two null DRAW_INDX_BIN packets are inserted right
440 * after the page table base update, followed by a
441 * wait for idle. The null packets will fill up the
442 * VGT DMA request fifo and prevent any further
443 * vertex/bin updates from occurring until the wait
444 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600445 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446 *cmds++ = (0x4 << 16) |
447 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
448 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600449 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600450 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600451 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 *cmds++ = 0; /* viz query info */
453 *cmds++ = 0x0003C004; /* draw indicator */
454 *cmds++ = 0; /* bin base */
455 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600456 *cmds++ =
457 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600459 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460 *cmds++ = 0; /* viz query info */
461 *cmds++ = 0x0003C004; /* draw indicator */
462 *cmds++ = 0; /* bin base */
463 *cmds++ = 3; /* bin size */
464 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600465 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600467 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468 *cmds++ = 0x00000000;
469 sizedwords += 21;
470 }
471
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600472
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600474 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 *cmds++ = 0x7fff; /* invalidate all base pointers */
476 sizedwords += 2;
477 }
478
479 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
480 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600481 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600482 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600483 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484}
485
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600486static void adreno_setstate(struct kgsl_device *device,
487 uint32_t flags)
488{
489 /* call the mmu specific handler */
490 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
491 return adreno_gpummu_setstate(device, flags);
492 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
493 return adreno_iommu_setstate(device, flags);
494}
495
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700497a3xx_getchipid(struct kgsl_device *device)
498{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700499 unsigned int majorid = 0, minorid = 0, patchid = 0;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700500
Jordan Crouse54154c62012-03-27 16:33:26 -0600501 /*
502 * We could detect the chipID from the hardware but it takes multiple
503 * registers to find the right combination. Since we traffic exclusively
504 * in system on chips, we can be (mostly) confident that a SOC version
505 * will match a GPU (at this juncture at least). So do the lazy/quick
506 * thing and set the chip_id based on the SoC
507 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700508
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530509 unsigned int version = socinfo_get_version();
510
Jordan Crouse54154c62012-03-27 16:33:26 -0600511 if (cpu_is_apq8064()) {
Jordan Croused2b30d22012-05-21 08:41:51 -0600512
Jordan Crouse54154c62012-03-27 16:33:26 -0600513 /* A320 */
514 majorid = 2;
515 minorid = 0;
Jordan Croused2b30d22012-05-21 08:41:51 -0600516
517 /*
518 * V1.1 has some GPU work arounds that we need to communicate
519 * up to user space via the patchid
520 */
521
522 if ((SOCINFO_VERSION_MAJOR(version) == 1) &&
523 (SOCINFO_VERSION_MINOR(version) == 1))
524 patchid = 1;
525 else
526 patchid = 0;
Jordan Crouse54154c62012-03-27 16:33:26 -0600527 } else if (cpu_is_msm8930()) {
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530528
Jordan Crouse54154c62012-03-27 16:33:26 -0600529 /* A305 */
530 majorid = 0;
531 minorid = 5;
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530532
533 /*
534 * V1.2 has some GPU work arounds that we need to communicate
535 * up to user space via the patchid
536 */
537
538 if ((SOCINFO_VERSION_MAJOR(version) == 1) &&
539 (SOCINFO_VERSION_MINOR(version) == 2))
540 patchid = 2;
541 else
542 patchid = 0;
Jordan Crouse54154c62012-03-27 16:33:26 -0600543 }
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700544
Jordan Crouse54154c62012-03-27 16:33:26 -0600545 return (0x03 << 24) | (majorid << 16) | (minorid << 8) | patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700546}
547
548static unsigned int
549a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550{
551 unsigned int chipid = 0;
552 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700553 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554
555 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
556 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
557 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
558
559 /*
560 * adreno 22x gpus are indicated by coreid 2,
561 * but REG_RBBM_PERIPHID1 always contains 0 for this field
562 */
Sudhakara Rao Tentudaebac22012-04-02 14:51:29 -0700563 if (cpu_is_msm8960() || cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 chipid = 2 << 24;
565 else
566 chipid = (coreid & 0xF) << 24;
567
568 chipid |= ((majorid >> 4) & 0xF) << 16;
569
570 minorid = ((revid >> 0) & 0xFF);
571
572 patchid = ((revid >> 16) & 0xFF);
573
574 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700575 /* 8960v3 returns 5 for patch release, but it should be 6 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530576 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577 if (cpu_is_qsd8x50())
578 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700579 else if (cpu_is_msm8960() &&
580 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
581 patchid = 6;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530582 else if (cpu_is_msm8625() && minorid == 0)
583 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584
585 chipid |= (minorid << 8) | patchid;
586
587 return chipid;
588}
589
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700590static unsigned int
591adreno_getchipid(struct kgsl_device *device)
592{
Sudhakara Rao Tentu8ebb2282012-03-06 14:52:58 +0530593 if (cpu_is_apq8064() || cpu_is_msm8930())
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700594 return a3xx_getchipid(device);
595 else
596 return a2xx_getchipid(device);
597}
598
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599static inline bool _rev_match(unsigned int id, unsigned int entry)
600{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600601 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603
604static void
605adreno_identify_gpu(struct adreno_device *adreno_dev)
606{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600607 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608
609 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
610
611 core = (adreno_dev->chip_id >> 24) & 0xff;
612 major = (adreno_dev->chip_id >> 16) & 0xff;
613 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600614 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615
Jordan Crouse505df9c2011-07-28 08:37:59 -0600616 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
617 if (core == adreno_gpulist[i].core &&
618 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600619 _rev_match(minor, adreno_gpulist[i].minor) &&
620 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622 }
623
Jordan Crouse505df9c2011-07-28 08:37:59 -0600624 if (i == ARRAY_SIZE(adreno_gpulist)) {
625 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
626 return;
627 }
628
629 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
630 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
631 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
632 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700633 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
634 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700635 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600636 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637}
638
639static int __devinit
640adreno_probe(struct platform_device *pdev)
641{
642 struct kgsl_device *device;
643 struct adreno_device *adreno_dev;
644 int status = -EINVAL;
645
646 device = (struct kgsl_device *)pdev->id_entry->driver_data;
647 adreno_dev = ADRENO_DEVICE(device);
648 device->parentdev = &pdev->dev;
649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 status = adreno_ringbuffer_init(device);
651 if (status != 0)
652 goto error;
653
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600654 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 if (status)
656 goto error_close_rb;
657
658 adreno_debugfs_init(device);
659
660 kgsl_pwrscale_init(device);
661 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
662
663 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
664 return 0;
665
666error_close_rb:
667 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
668error:
669 device->parentdev = NULL;
670 return status;
671}
672
673static int __devexit adreno_remove(struct platform_device *pdev)
674{
675 struct kgsl_device *device;
676 struct adreno_device *adreno_dev;
677
678 device = (struct kgsl_device *)pdev->id_entry->driver_data;
679 adreno_dev = ADRENO_DEVICE(device);
680
681 kgsl_pwrscale_detach_policy(device);
682 kgsl_pwrscale_close(device);
683
684 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
685 kgsl_device_platform_remove(device);
686
687 return 0;
688}
689
690static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
691{
692 int status = -EINVAL;
693 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694
Jeremy Gebben388c2972011-12-16 09:05:07 -0700695 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696
697 /* Power up the device */
698 kgsl_pwrctrl_enable(device);
699
700 /* Identify the specific GPU */
701 adreno_identify_gpu(adreno_dev);
702
Jordan Crouse505df9c2011-07-28 08:37:59 -0600703 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
704 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
705 adreno_dev->chip_id);
706 goto error_clk_off;
707 }
708
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700709 /* Set up the MMU */
710 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600711 /*
712 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
713 * on older gpus
714 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700715 if (adreno_is_a20x(adreno_dev)) {
716 device->mh.mh_intf_cfg1 = 0;
717 device->mh.mh_intf_cfg2 = 0;
718 }
719
720 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600721 }
722
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700723 status = kgsl_mmu_start(device);
724 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 goto error_clk_off;
726
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700727 /* Start the GPU */
728 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729
730 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700731 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732
733 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700734 if (status == 0) {
735 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
736 return 0;
737 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600740 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741error_clk_off:
742 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743
744 return status;
745}
746
747static int adreno_stop(struct kgsl_device *device)
748{
749 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
750
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751 adreno_dev->drawctxt_active = NULL;
752
753 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
754
Shubhraprakash Das79447952012-04-26 18:12:23 -0600755 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700757 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530758 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800759 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600760
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 /* Power down the device */
762 kgsl_pwrctrl_disable(device);
763
764 return 0;
765}
766
767static int
768adreno_recover_hang(struct kgsl_device *device)
769{
770 int ret;
771 unsigned int *rb_buffer;
772 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
773 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
774 unsigned int timestamp;
775 unsigned int num_rb_contents;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 unsigned int reftimestamp;
777 unsigned int enable_ts;
778 unsigned int soptimestamp;
779 unsigned int eoptimestamp;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700780 unsigned int context_id;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700781 struct kgsl_context *context;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700782 struct adreno_context *adreno_context;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700783 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784
785 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
786 rb_buffer = vmalloc(rb->buffer_desc.size);
787 if (!rb_buffer) {
788 KGSL_MEM_ERR(device,
789 "Failed to allocate memory for recovery: %x\n",
790 rb->buffer_desc.size);
791 return -ENOMEM;
792 }
793 /* Extract valid contents from rb which can stil be executed after
794 * hang */
795 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
796 if (ret)
797 goto done;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700798 kgsl_sharedmem_readl(&device->memstore, &context_id,
799 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
800 current_context));
801 context = idr_find(&device->context_idr, context_id);
802 if (context == NULL) {
803 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
804 context_id);
805 context_id = KGSL_MEMSTORE_GLOBAL;
806 }
807
808 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
809 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
810
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700812 KGSL_MEMSTORE_OFFSET(context_id,
813 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700815 KGSL_MEMSTORE_OFFSET(context_id,
816 ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700818 KGSL_MEMSTORE_OFFSET(context_id,
819 soptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700821 KGSL_MEMSTORE_OFFSET(context_id,
822 eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 /* Make sure memory is synchronized before restarting the GPU */
824 mb();
825 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700826 "Context id that caused a GPU hang: %d\n", context_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 /* restart device */
828 ret = adreno_stop(device);
829 if (ret)
830 goto done;
831 ret = adreno_start(device, true);
832 if (ret)
833 goto done;
834 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
835 /* Restore timestamp states */
836 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700837 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 soptimestamp);
839 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700840 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 eoptimestamp);
Carter Cooperae4c7bc2012-04-10 09:40:49 -0600842
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843 if (num_rb_contents) {
844 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700845 KGSL_MEMSTORE_OFFSET(context_id, ref_wait_ts),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846 reftimestamp);
847 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700848 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849 enable_ts);
850 }
851 /* Make sure all writes are posted before the GPU reads them */
852 wmb();
853 /* Mark the invalid context so no more commands are accepted from
854 * that context */
855
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700856 adreno_context = context->devctxt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857
858 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700859 "Context that caused a GPU hang: %d\n", adreno_context->id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700861 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700863 /*
864 * Set the reset status of all contexts to
865 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
866 * since thats the guilty party
867 */
868 while ((context = idr_get_next(&device->context_idr, &next))) {
869 if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
870 context->reset_status) {
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700871 if (context->id != context_id)
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700872 context->reset_status =
873 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
874 else
875 context->reset_status =
876 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
877 }
878 next = next + 1;
879 }
880
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 /* Restore valid commands in ringbuffer */
882 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700883 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884done:
885 vfree(rb_buffer);
886 return ret;
887}
888
889static int
890adreno_dump_and_recover(struct kgsl_device *device)
891{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 int result = -ETIMEDOUT;
893
894 if (device->state == KGSL_STATE_HUNG)
895 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700896 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 mutex_unlock(&device->mutex);
898 wait_for_completion(&device->recovery_gate);
899 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700900 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 result = 0;
902 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700903 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700905 /* Detected a hang */
906
907
908 /*
909 * Trigger an automatic dump of the state to
910 * the console
911 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700913
914 /*
915 * Make a GPU snapshot. For now, do it after the PM dump so we
916 * can at least be sure the PM dump will work as it always has
917 */
918 kgsl_device_snapshot(device, 1);
919
Jeremy Gebben388c2972011-12-16 09:05:07 -0700920 result = adreno_recover_hang(device);
921 if (result)
922 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
923 else
924 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
925 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 }
927done:
928 return result;
929}
930
931static int adreno_getproperty(struct kgsl_device *device,
932 enum kgsl_property_type type,
933 void *value,
934 unsigned int sizebytes)
935{
936 int status = -EINVAL;
937 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
938
939 switch (type) {
940 case KGSL_PROP_DEVICE_INFO:
941 {
942 struct kgsl_devinfo devinfo;
943
944 if (sizebytes != sizeof(devinfo)) {
945 status = -EINVAL;
946 break;
947 }
948
949 memset(&devinfo, 0, sizeof(devinfo));
950 devinfo.device_id = device->id+1;
951 devinfo.chip_id = adreno_dev->chip_id;
952 devinfo.mmu_enabled = kgsl_mmu_enabled();
953 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -0600954 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
955 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956
957 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
958 0) {
959 status = -EFAULT;
960 break;
961 }
962 status = 0;
963 }
964 break;
965 case KGSL_PROP_DEVICE_SHADOW:
966 {
967 struct kgsl_shadowprop shadowprop;
968
969 if (sizebytes != sizeof(shadowprop)) {
970 status = -EINVAL;
971 break;
972 }
973 memset(&shadowprop, 0, sizeof(shadowprop));
974 if (device->memstore.hostptr) {
975 /*NOTE: with mmu enabled, gpuaddr doesn't mean
976 * anything to mmap().
977 */
978 shadowprop.gpuaddr = device->memstore.physaddr;
979 shadowprop.size = device->memstore.size;
980 /* GSL needs this to be set, even if it
981 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700982 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
983 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 }
985 if (copy_to_user(value, &shadowprop,
986 sizeof(shadowprop))) {
987 status = -EFAULT;
988 break;
989 }
990 status = 0;
991 }
992 break;
993 case KGSL_PROP_MMU_ENABLE:
994 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600995 int mmu_prop = kgsl_mmu_enabled();
996
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997 if (sizebytes != sizeof(int)) {
998 status = -EINVAL;
999 break;
1000 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001001 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002 status = -EFAULT;
1003 break;
1004 }
1005 status = 0;
1006 }
1007 break;
1008 case KGSL_PROP_INTERRUPT_WAITS:
1009 {
1010 int int_waits = 1;
1011 if (sizebytes != sizeof(int)) {
1012 status = -EINVAL;
1013 break;
1014 }
1015 if (copy_to_user(value, &int_waits, sizeof(int))) {
1016 status = -EFAULT;
1017 break;
1018 }
1019 status = 0;
1020 }
1021 break;
1022 default:
1023 status = -EINVAL;
1024 }
1025
1026 return status;
1027}
1028
Jordan Crousef7370f82012-04-18 09:31:07 -06001029static int adreno_setproperty(struct kgsl_device *device,
1030 enum kgsl_property_type type,
1031 void *value,
1032 unsigned int sizebytes)
1033{
1034 int status = -EINVAL;
1035
1036 switch (type) {
1037 case KGSL_PROP_PWRCTRL: {
1038 unsigned int enable;
1039 struct kgsl_device_platform_data *pdata =
1040 kgsl_device_get_drvdata(device);
1041
1042 if (sizebytes != sizeof(enable))
1043 break;
1044
1045 if (copy_from_user(&enable, (void __user *) value,
1046 sizeof(enable))) {
1047 status = -EFAULT;
1048 break;
1049 }
1050
1051 if (enable) {
1052 if (pdata->nap_allowed)
1053 device->pwrctrl.nap_allowed = true;
1054
1055 kgsl_pwrscale_enable(device);
1056 } else {
1057 device->pwrctrl.nap_allowed = false;
1058 kgsl_pwrscale_disable(device);
1059 }
1060
1061 status = 0;
1062 }
1063 break;
1064 default:
1065 break;
1066 }
1067
1068 return status;
1069}
1070
Lynus Vaz06a9a902011-10-04 19:25:33 +05301071static inline void adreno_poke(struct kgsl_device *device)
1072{
1073 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1074 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1075}
1076
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077/* Caller must hold the device mutex. */
1078int adreno_idle(struct kgsl_device *device, unsigned int timeout)
1079{
1080 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1081 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1082 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301083 unsigned long wait_timeout =
1084 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +05301085 unsigned long wait_time;
1086 unsigned long wait_time_part;
1087 unsigned int msecs;
1088 unsigned int msecs_first;
1089 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001091 kgsl_cffdump_regpoll(device->id,
1092 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093 0x00000000, 0x80000000);
1094 /* first, wait until the CP has consumed all the commands in
1095 * the ring buffer
1096 */
1097retry:
1098 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +05301099 msecs = adreno_dev->wait_timeout;
1100 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1101 msecs_part = (msecs - msecs_first + 3) / 4;
1102 wait_time = jiffies + wait_timeout;
1103 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -07001104 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105 do {
Lynus Vaz284d1042012-01-31 16:32:31 +05301106 if (time_after(jiffies, wait_time_part)) {
1107 adreno_poke(device);
1108 wait_time_part = jiffies +
1109 msecs_to_jiffies(msecs_part);
1110 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111 GSL_RB_GET_READPTR(rb, &rb->rptr);
1112 if (time_after(jiffies, wait_time)) {
1113 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1114 rb->rptr, rb->wptr);
1115 goto err;
1116 }
1117 } while (rb->rptr != rb->wptr);
1118 }
1119
1120 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301121 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001122 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001123 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1124 &rbbm_status);
1125 if (adreno_is_a2xx(adreno_dev)) {
1126 if (rbbm_status == 0x110)
1127 return 0;
1128 } else {
1129 if (!(rbbm_status & 0x80000000))
1130 return 0;
1131 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001132 }
1133
1134err:
1135 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
1136 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301137 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138 goto retry;
1139 }
1140 return -ETIMEDOUT;
1141}
1142
1143static unsigned int adreno_isidle(struct kgsl_device *device)
1144{
1145 int status = false;
1146 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1147 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1148 unsigned int rbbm_status;
1149
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001150 WARN_ON(device->state == KGSL_STATE_INIT);
1151 /* If the device isn't active, don't force it on. */
1152 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153 /* Is the ring buffer is empty? */
1154 GSL_RB_GET_READPTR(rb, &rb->rptr);
1155 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1156 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001157 adreno_regread(device,
1158 adreno_dev->gpudev->reg_rbbm_status,
1159 &rbbm_status);
1160
1161 if (adreno_is_a2xx(adreno_dev)) {
1162 if (rbbm_status == 0x110)
1163 status = true;
1164 } else {
1165 if (!(rbbm_status & 0x80000000))
1166 status = true;
1167 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001168 }
1169 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001170 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001171 }
1172 return status;
1173}
1174
1175/* Caller must hold the device mutex. */
1176static int adreno_suspend_context(struct kgsl_device *device)
1177{
1178 int status = 0;
1179 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1180
1181 /* switch to NULL ctxt */
1182 if (adreno_dev->drawctxt_active != NULL) {
1183 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1184 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1185 }
1186
1187 return status;
1188}
1189
Jordan Crouse233b2092012-04-18 09:31:09 -06001190/* Find a memory structure attached to an adreno context */
1191
1192struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1193 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1194{
1195 struct kgsl_context *context;
1196 struct adreno_context *adreno_context = NULL;
1197 int next = 0;
1198
1199 while (1) {
1200 context = idr_get_next(&device->context_idr, &next);
1201 if (context == NULL)
1202 break;
1203
1204 adreno_context = (struct adreno_context *)context->devctxt;
1205
1206 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1207 struct kgsl_memdesc *desc;
1208
1209 desc = &adreno_context->gpustate;
1210 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1211 return desc;
1212
1213 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1214 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1215 return desc;
1216 }
1217 next = next + 1;
1218 }
1219
1220 return NULL;
1221}
1222
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001223struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001224 unsigned int pt_base,
1225 unsigned int gpuaddr,
1226 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001229 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1230 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1231
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001232 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1233 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001234
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001235 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1236 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001237
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001238 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1239 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001240
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001241 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1242 size))
1243 return &device->mmu.setstate_memory;
1244
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001245 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1246
1247 if (entry)
1248 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001249
Jordan Crouse233b2092012-04-18 09:31:09 -06001250 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001251}
1252
1253uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1254 unsigned int gpuaddr, unsigned int size)
1255{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001256 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001257
1258 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1259
1260 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001261}
1262
1263void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1264 unsigned int *value)
1265{
1266 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06001267 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
1268 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269
1270 if (!in_interrupt())
1271 kgsl_pre_hwaccess(device);
1272
1273 /*ensure this read finishes before the next one.
1274 * i.e. act like normal readl() */
1275 *value = __raw_readl(reg);
1276 rmb();
1277}
1278
1279void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1280 unsigned int value)
1281{
1282 unsigned int *reg;
1283
Jordan Crouse7501d452012-04-19 08:58:44 -06001284 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001285
1286 if (!in_interrupt())
1287 kgsl_pre_hwaccess(device);
1288
1289 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06001290 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001291
1292 /*ensure previous writes post before this one,
1293 * i.e. act like normal writel() */
1294 wmb();
1295 __raw_writel(value, reg);
1296}
1297
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001298static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1299{
1300 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001301 if (k_ctxt != NULL) {
1302 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001303 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
1304 context_id = KGSL_CONTEXT_INVALID;
1305 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
1306 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001307 }
1308
1309 return context_id;
1310}
1311
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001313 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314{
1315 int status;
1316 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001317 unsigned int context_id;
1318
1319 mutex_lock(&device->mutex);
1320 context_id = _get_context_id(context);
1321 /*
1322 * If the context ID is invalid, we are in a race with
1323 * the context being destroyed by userspace so bail.
1324 */
1325 if (context_id == KGSL_CONTEXT_INVALID) {
1326 KGSL_DRV_WARN(device, "context was detached");
1327 status = -EINVAL;
1328 goto unlock;
1329 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001331 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001332 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001333 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001334 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335 mb();
1336
1337 if (enableflag) {
1338 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001339 KGSL_MEMSTORE_OFFSET(context_id,
1340 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001341 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001342 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001343 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001344 KGSL_MEMSTORE_OFFSET(context_id,
1345 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001346 wmb();
1347 }
1348 } else {
1349 unsigned int cmds[2];
1350 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001351 KGSL_MEMSTORE_OFFSET(context_id,
1352 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001353 enableflag = 1;
1354 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001355 KGSL_MEMSTORE_OFFSET(context_id,
1356 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001357 wmb();
1358 /* submit a dummy packet so that even if all
1359 * commands upto timestamp get executed we will still
1360 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001361 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001362 cmds[1] = 0;
Zhoulu Luo552905e2012-06-21 15:21:52 -07001363 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
Jordan Crousee0ea7622012-01-24 09:32:04 -07001364 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001366 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001367unlock:
1368 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001369
1370 return status;
1371}
1372
1373/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001374 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001375 placing a process in wait q. For conditional interrupts we expect the
1376 process to already be in its wait q when its exit condition checking
1377 function is called.
1378*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001379#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001380({ \
1381 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001382 if (io) \
1383 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1384 else \
1385 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386 __ret; \
1387})
1388
1389/* MUST be called with the device mutex held */
1390static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001391 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392 unsigned int timestamp,
1393 unsigned int msecs)
1394{
1395 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001396 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001397 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001398 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001399 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301400 int retries;
1401 unsigned int msecs_first;
1402 unsigned int msecs_part;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001403 unsigned int ts_issued;
1404 unsigned int context_id = _get_context_id(context);
1405
1406 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001407
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301408 /* Don't wait forever, set a max value for now */
1409 if (msecs == -1)
1410 msecs = adreno_dev->wait_timeout;
1411
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001412 if (timestamp_cmp(timestamp, ts_issued) > 0) {
1413 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
1414 "last issued ts <%d:0x%x>\n",
1415 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001416 status = -EINVAL;
1417 goto done;
1418 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419
Lynus Vaz06a9a902011-10-04 19:25:33 +05301420 /* Keep the first timeout as 100msecs before rewriting
1421 * the WPTR. Less visible impact if the WPTR has not
1422 * been updated properly.
1423 */
1424 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1425 msecs_part = (msecs - msecs_first + 3) / 4;
1426 for (retries = 0; retries < 5; retries++) {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001427 /*
1428 * If the context ID is invalid, we are in a race with
1429 * the context being destroyed by userspace so bail.
1430 */
1431 if (context_id == KGSL_CONTEXT_INVALID) {
1432 KGSL_DRV_WARN(device, "context was detached");
1433 status = -EINVAL;
1434 goto done;
1435 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001436 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001437 /* if the timestamp happens while we're not
1438 * waiting, there's a chance that an interrupt
1439 * will not be generated and thus the timestamp
1440 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301441 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001442 queue_work(device->work_queue, &device->ts_expired_ws);
1443 status = 0;
1444 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001446 adreno_poke(device);
1447 io_cnt = (io_cnt + 1) % 100;
1448 if (io_cnt <
1449 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1450 io = 0;
1451 mutex_unlock(&device->mutex);
1452 /* We need to make sure that the process is
1453 * placed in wait-q before its condition is called
1454 */
1455 status = kgsl_wait_event_interruptible_timeout(
1456 device->wait_queue,
1457 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001458 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001459 msecs_to_jiffies(retries ?
1460 msecs_part : msecs_first), io);
1461 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001462
Jeremy Gebben63904832012-02-07 16:10:55 -07001463 if (status > 0) {
1464 /*completed before the wait finished */
1465 status = 0;
1466 goto done;
1467 } else if (status < 0) {
1468 /*an error occurred*/
1469 goto done;
1470 }
1471 /*this wait timed out*/
1472 }
1473 status = -ETIMEDOUT;
1474 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001475 "Device hang detected while waiting for timestamp: "
1476 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
1477 "wptr: 0x%x\n",
1478 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07001479 adreno_dev->ringbuffer.wptr);
1480 if (!adreno_dump_and_recover(device)) {
1481 /* wait for idle after recovery as the
1482 * timestamp that this process wanted
1483 * to wait on may be invalid */
1484 if (!adreno_idle(device, KGSL_TIMEOUT_DEFAULT))
1485 status = 0;
1486 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001487done:
1488 return (int)status;
1489}
1490
1491static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001492 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001493{
1494 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001495 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001496
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001497 /*
1498 * If the context ID is invalid, we are in a race with
1499 * the context being destroyed by userspace so bail.
1500 */
1501 if (context_id == KGSL_CONTEXT_INVALID) {
1502 KGSL_DRV_WARN(device, "context was detached");
1503 return timestamp;
1504 }
Jordan Crousec659f382012-04-16 11:10:41 -06001505 switch (type) {
1506 case KGSL_TIMESTAMP_QUEUED: {
1507 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1508 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1509
1510 timestamp = rb->timestamp[context_id];
1511 break;
1512 }
1513 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001514 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06001515 break;
1516 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001517 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06001518 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
1519 break;
1520 }
1521
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001522 rmb();
1523
1524 return timestamp;
1525}
1526
1527static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1528 unsigned int cmd, void *data)
1529{
1530 int result = 0;
1531 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1532 struct kgsl_context *context;
1533
1534 switch (cmd) {
1535 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1536 binbase = data;
1537
1538 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1539 if (context) {
1540 adreno_drawctxt_set_bin_base_offset(
1541 dev_priv->device, context, binbase->offset);
1542 } else {
1543 result = -EINVAL;
1544 KGSL_DRV_ERR(dev_priv->device,
1545 "invalid drawctxt drawctxt_id %d "
1546 "device_id=%d\n",
1547 binbase->drawctxt_id, dev_priv->device->id);
1548 }
1549 break;
1550
1551 default:
1552 KGSL_DRV_INFO(dev_priv->device,
1553 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001554 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001555 break;
1556 }
1557 return result;
1558
1559}
1560
1561static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1562{
1563 gpu_freq /= 1000000;
1564 return ticks / gpu_freq;
1565}
1566
1567static void adreno_power_stats(struct kgsl_device *device,
1568 struct kgsl_power_stats *stats)
1569{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001570 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001571 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001572 unsigned int cycles;
1573
1574 /* Get the busy cycles counted since the counter was last reset */
1575 /* Calling this function also resets and restarts the counter */
1576
1577 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001578
1579 /* In order to calculate idle you have to have run the algorithm *
1580 * at least once to get a start time. */
1581 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001582 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001583 stats->total_time = tmp - pwr->time;
1584 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001585 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001586 pwrlevels[device->pwrctrl.active_pwrlevel].
1587 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001588 } else {
1589 stats->total_time = 0;
1590 stats->busy_time = 0;
1591 pwr->time = ktime_to_us(ktime_get());
1592 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001593}
1594
1595void adreno_irqctrl(struct kgsl_device *device, int state)
1596{
Jordan Crousea78c9172011-07-11 13:14:09 -06001597 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1598 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001599}
1600
Jordan Croused6535882012-06-20 08:22:16 -06001601static unsigned int adreno_gpuid(struct kgsl_device *device,
1602 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07001603{
1604 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1605
Jordan Croused6535882012-06-20 08:22:16 -06001606 /* Some applications need to know the chip ID too, so pass
1607 * that as a parameter */
1608
1609 if (chipid != NULL)
1610 *chipid = adreno_dev->chip_id;
1611
Jordan Crousea0758f22011-12-07 11:19:22 -07001612 /* Standard KGSL gpuid format:
1613 * top word is 0x0002 for 2D or 0x0003 for 3D
1614 * Bottom word is core specific identifer
1615 */
1616
1617 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1618}
1619
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001620static const struct kgsl_functable adreno_functable = {
1621 /* Mandatory functions */
1622 .regread = adreno_regread,
1623 .regwrite = adreno_regwrite,
1624 .idle = adreno_idle,
1625 .isidle = adreno_isidle,
1626 .suspend_context = adreno_suspend_context,
1627 .start = adreno_start,
1628 .stop = adreno_stop,
1629 .getproperty = adreno_getproperty,
1630 .waittimestamp = adreno_waittimestamp,
1631 .readtimestamp = adreno_readtimestamp,
1632 .issueibcmds = adreno_ringbuffer_issueibcmds,
1633 .ioctl = adreno_ioctl,
1634 .setup_pt = adreno_setup_pt,
1635 .cleanup_pt = adreno_cleanup_pt,
1636 .power_stats = adreno_power_stats,
1637 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001638 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001639 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001640 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001641 /* Optional functions */
1642 .setstate = adreno_setstate,
1643 .drawctxt_create = adreno_drawctxt_create,
1644 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06001645 .setproperty = adreno_setproperty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001646};
1647
1648static struct platform_device_id adreno_id_table[] = {
1649 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1650 { },
1651};
1652MODULE_DEVICE_TABLE(platform, adreno_id_table);
1653
1654static struct platform_driver adreno_platform_driver = {
1655 .probe = adreno_probe,
1656 .remove = __devexit_p(adreno_remove),
1657 .suspend = kgsl_suspend_driver,
1658 .resume = kgsl_resume_driver,
1659 .id_table = adreno_id_table,
1660 .driver = {
1661 .owner = THIS_MODULE,
1662 .name = DEVICE_3D_NAME,
1663 .pm = &kgsl_pm_ops,
1664 }
1665};
1666
1667static int __init kgsl_3d_init(void)
1668{
1669 return platform_driver_register(&adreno_platform_driver);
1670}
1671
1672static void __exit kgsl_3d_exit(void)
1673{
1674 platform_driver_unregister(&adreno_platform_driver);
1675}
1676
1677module_init(kgsl_3d_init);
1678module_exit(kgsl_3d_exit);
1679
1680MODULE_DESCRIPTION("3D Graphics driver");
1681MODULE_VERSION("1.2");
1682MODULE_LICENSE("GPL v2");
1683MODULE_ALIAS("platform:kgsl_3d");