blob: ff0a38fcb53800f0a14f72512ac37e7179c35f2d [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 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070013#include <linux/uaccess.h>
14#include <linux/vmalloc.h>
15#include <linux/ioctl.h>
16#include <linux/sched.h>
17
18#include <mach/socinfo.h>
19
20#include "kgsl.h"
21#include "kgsl_pwrscale.h"
22#include "kgsl_cffdump.h"
23#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060024#include "kgsl_iommu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025
26#include "adreno.h"
27#include "adreno_pm4types.h"
28#include "adreno_debugfs.h"
29#include "adreno_postmortem.h"
30
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070031#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070032#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34#define DRIVER_VERSION_MAJOR 3
35#define DRIVER_VERSION_MINOR 1
36
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037/* Adreno MH arbiter config*/
38#define ADRENO_CFG_MHARB \
39 (0x10 \
40 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
41 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
42 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
43 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
44 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
45 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
46 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
47 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
48 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
53 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
54
55#define ADRENO_MMU_CONFIG \
56 (0x01 \
57 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
58 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
59 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
60 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
61 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
62 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
68
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069static const struct kgsl_functable adreno_functable;
70
71static struct adreno_device device_3d0 = {
72 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070073 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074 .name = DEVICE_3D0_NAME,
75 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060076 .mh = {
77 .mharb = ADRENO_CFG_MHARB,
78 /* Remove 1k boundary check in z470 to avoid a GPU
79 * hang. Notice that this solution won't work if
80 * both EBI and SMI are used
81 */
82 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083 /* turn off memory protection unit by setting
84 acceptable physical address range to include
85 all pages. */
86 .mpu_base = 0x00000000,
87 .mpu_range = 0xFFFFF000,
88 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060089 .mmu = {
90 .config = ADRENO_MMU_CONFIG,
91 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095 .iomemname = KGSL_3D0_REG_MEMORY,
96 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -060098 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
100 .suspend = kgsl_early_suspend_driver,
101 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600103#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600105 .gmem_base = 0,
106 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 .pfp_fw = NULL,
108 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700109 .wait_timeout = 10000, /* in milliseconds */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600110 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111};
112
Jordan Crouse95b33272011-11-11 14:50:12 -0700113
Jordan Crouse505df9c2011-07-28 08:37:59 -0600114/*
115 * This is the master list of all GPU cores that are supported by this
116 * driver.
117 */
118
119#define ANY_ID (~0)
120
121static const struct {
122 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600123 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600124 const char *pm4fw;
125 const char *pfpfw;
126 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700127 unsigned int istore_size;
128 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700129 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530130 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600131} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600132 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700133 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530134 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530135 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
136 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530137 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600138 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700139 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530140 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600141 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700142 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530143 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600144 /*
145 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
146 * a hardware problem.
147 */
148 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700149 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530150 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700151 { ADRENO_REV_A225, 2, 2, 0, 6,
152 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530153 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600154 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700155 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530156 1536, 768, 3, SZ_512K },
157 /* A3XX doesn't use the pix_shader_start */
Jordan Crouse54154c62012-03-27 16:33:26 -0600158 { ADRENO_REV_A305, 3, 0, 5, 0,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530159 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
160 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700161 /* A3XX doesn't use the pix_shader_start */
Jordan Croused2b30d22012-05-21 08:41:51 -0600162 { ADRENO_REV_A320, 3, 2, 0, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700163 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530164 512, 0, 2, SZ_512K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700165
Jordan Crouse505df9c2011-07-28 08:37:59 -0600166};
167
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600168static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169{
Jordan Crousea78c9172011-07-11 13:14:09 -0600170 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600171 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172
Jordan Crousea78c9172011-07-11 13:14:09 -0600173 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174
175 if (device->requested_state == KGSL_STATE_NONE) {
176 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700177 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 queue_work(device->work_queue, &device->idle_check_ws);
179 } else if (device->pwrscale.policy != NULL) {
180 queue_work(device->work_queue, &device->idle_check_ws);
181 }
182 }
183
184 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800185 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186 jiffies + device->pwrctrl.interval_timeout);
187 return result;
188}
189
Jordan Crouse9f739212011-07-28 08:37:57 -0600190static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 struct kgsl_pagetable *pagetable)
192{
193 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
194 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
195
196 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
197
198 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
199
200 kgsl_mmu_unmap(pagetable, &device->memstore);
201
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600202 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203}
204
205static int adreno_setup_pt(struct kgsl_device *device,
206 struct kgsl_pagetable *pagetable)
207{
208 int result = 0;
209 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
210 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
211
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
213 GSL_PT_PAGE_RV);
214 if (result)
215 goto error;
216
217 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
218 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
219 if (result)
220 goto unmap_buffer_desc;
221
222 result = kgsl_mmu_map_global(pagetable, &device->memstore,
223 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
224 if (result)
225 goto unmap_memptrs_desc;
226
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600227 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
229 if (result)
230 goto unmap_memstore_desc;
231
232 return result;
233
234unmap_memstore_desc:
235 kgsl_mmu_unmap(pagetable, &device->memstore);
236
237unmap_memptrs_desc:
238 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
239
240unmap_buffer_desc:
241 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
242
243error:
244 return result;
245}
246
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600247static void adreno_iommu_setstate(struct kgsl_device *device,
248 uint32_t flags)
249{
250 unsigned int pt_val, reg_pt_val;
251 unsigned int link[200];
252 unsigned int *cmds = &link[0];
253 int sizedwords = 0;
254 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
255 struct kgsl_memdesc **reg_map_desc;
Pu Chened8cbb52012-06-04 18:18:48 -0700256 void *reg_map_array = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600257 int num_iommu_units, i;
258
259 if (!adreno_dev->drawctxt_active)
260 return kgsl_mmu_device_setstate(&device->mmu, flags);
261 num_iommu_units = kgsl_mmu_get_reg_map_desc(&device->mmu,
262 &reg_map_array);
263 reg_map_desc = reg_map_array;
264
265 if (kgsl_mmu_enable_clk(&device->mmu,
266 KGSL_IOMMU_CONTEXT_USER))
267 goto done;
268
269 if (adreno_is_a225(adreno_dev))
270 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
271 device->mmu.setstate_memory.gpuaddr +
272 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
273 else
274 cmds += adreno_add_bank_change_cmds(cmds,
275 KGSL_IOMMU_CONTEXT_USER,
276 device->mmu.setstate_memory.gpuaddr +
277 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
278
279 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
280 pt_val = kgsl_mmu_pt_get_base_addr(device->mmu.hwpagetable);
281 /*
282 * We need to perfrom the following operations for all
283 * IOMMU units
284 */
285 for (i = 0; i < num_iommu_units; i++) {
286 reg_pt_val = (pt_val &
287 (KGSL_IOMMU_TTBR0_PA_MASK <<
288 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
289 kgsl_mmu_get_pt_lsb(&device->mmu, i,
290 KGSL_IOMMU_CONTEXT_USER);
291 /*
292 * Set address of the new pagetable by writng to IOMMU
293 * TTBR0 register
294 */
295 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
296 *cmds++ = reg_map_desc[i]->gpuaddr +
297 (KGSL_IOMMU_CONTEXT_USER <<
298 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0;
299 *cmds++ = reg_pt_val;
300 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
301 *cmds++ = 0x00000000;
302
303 /*
304 * Read back the ttbr0 register as a barrier to ensure
305 * above writes have completed
306 */
307 cmds += adreno_add_read_cmds(device, cmds,
308 reg_map_desc[i]->gpuaddr +
309 (KGSL_IOMMU_CONTEXT_USER <<
310 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
311 reg_pt_val,
312 device->mmu.setstate_memory.gpuaddr +
313 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
314
315 /* set the asid */
316 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
317 *cmds++ = reg_map_desc[i]->gpuaddr +
318 (KGSL_IOMMU_CONTEXT_USER <<
319 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_CONTEXTIDR;
320 *cmds++ = kgsl_mmu_get_hwpagetable_asid(&device->mmu);
321 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
322 *cmds++ = 0x00000000;
323
324 /* Read back asid to ensure above write completes */
325 cmds += adreno_add_read_cmds(device, cmds,
326 reg_map_desc[i]->gpuaddr +
327 (KGSL_IOMMU_CONTEXT_USER <<
328 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_CONTEXTIDR,
329 kgsl_mmu_get_hwpagetable_asid(&device->mmu),
330 device->mmu.setstate_memory.gpuaddr +
331 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
332 }
333 /* invalidate all base pointers */
334 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
335 *cmds++ = 0x7fff;
336
337 if (flags & KGSL_MMUFLAGS_TLBFLUSH)
338 cmds += __adreno_add_idle_indirect_cmds(cmds,
339 device->mmu.setstate_memory.gpuaddr +
340 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
341 }
342 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
343 /*
344 * tlb flush based on asid, no need to flush entire tlb
345 */
346 for (i = 0; i < num_iommu_units; i++) {
347 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
348 *cmds++ = (reg_map_desc[i]->gpuaddr +
349 (KGSL_IOMMU_CONTEXT_USER <<
350 KGSL_IOMMU_CTX_SHIFT) +
351 KGSL_IOMMU_CTX_TLBIASID);
352 *cmds++ = kgsl_mmu_get_hwpagetable_asid(&device->mmu);
353 cmds += adreno_add_read_cmds(device, cmds,
354 reg_map_desc[i]->gpuaddr +
355 (KGSL_IOMMU_CONTEXT_USER <<
356 KGSL_IOMMU_CTX_SHIFT) +
357 KGSL_IOMMU_CONTEXTIDR,
358 kgsl_mmu_get_hwpagetable_asid(&device->mmu),
359 device->mmu.setstate_memory.gpuaddr +
360 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
361 }
362 }
363
364 if (adreno_is_a225(adreno_dev))
365 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
366 reg_map_desc[num_iommu_units - 1]->gpuaddr - PAGE_SIZE,
367 device->mmu.setstate_memory.gpuaddr +
368 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
369 else
370 cmds += adreno_add_bank_change_cmds(cmds,
371 KGSL_IOMMU_CONTEXT_PRIV,
372 device->mmu.setstate_memory.gpuaddr +
373 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
374
375 sizedwords += (cmds - &link[0]);
376 if (sizedwords)
377 adreno_ringbuffer_issuecmds(device,
378 KGSL_CMD_FLAGS_PMODE, &link[0], sizedwords);
379done:
380 if (num_iommu_units)
381 kfree(reg_map_array);
382}
383
384static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600385 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386{
387 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
388 unsigned int link[32];
389 unsigned int *cmds = &link[0];
390 int sizedwords = 0;
391 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
392
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600393 /*
394 * If possible, then set the state via the command stream to avoid
395 * a CPU idle. Otherwise, use the default setstate which uses register
396 * writes For CFF dump we must idle and use the registers so that it is
397 * easier to filter out the mmu accesses from the dump
398 */
399 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
401 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600402 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 *cmds++ = 0x00000000;
404
405 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600406 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600407 *cmds++ = kgsl_mmu_pt_get_base_addr(
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600408 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409 sizedwords += 4;
410 }
411
412 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
413 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600414 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415 1);
416 *cmds++ = 0x00000000;
417 sizedwords += 2;
418 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600419 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420 *cmds++ = mh_mmu_invalidate;
421 sizedwords += 2;
422 }
423
424 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600425 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 /* HW workaround: to resolve MMU page fault interrupts
427 * caused by the VGT.It prevents the CP PFP from filling
428 * the VGT DMA request fifo too early,thereby ensuring
429 * that the VGT will not fetch vertex/bin data until
430 * after the page table base register has been updated.
431 *
432 * Two null DRAW_INDX_BIN packets are inserted right
433 * after the page table base update, followed by a
434 * wait for idle. The null packets will fill up the
435 * VGT DMA request fifo and prevent any further
436 * vertex/bin updates from occurring until the wait
437 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600438 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439 *cmds++ = (0x4 << 16) |
440 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
441 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600442 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600443 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600444 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445 *cmds++ = 0; /* viz query info */
446 *cmds++ = 0x0003C004; /* draw indicator */
447 *cmds++ = 0; /* bin base */
448 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600449 *cmds++ =
450 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600452 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 *cmds++ = 0; /* viz query info */
454 *cmds++ = 0x0003C004; /* draw indicator */
455 *cmds++ = 0; /* bin base */
456 *cmds++ = 3; /* bin size */
457 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600458 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600460 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461 *cmds++ = 0x00000000;
462 sizedwords += 21;
463 }
464
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600465
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600467 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468 *cmds++ = 0x7fff; /* invalidate all base pointers */
469 sizedwords += 2;
470 }
471
472 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
473 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600474 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600475 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600476 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477}
478
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600479static void adreno_setstate(struct kgsl_device *device,
480 uint32_t flags)
481{
482 /* call the mmu specific handler */
483 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
484 return adreno_gpummu_setstate(device, flags);
485 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
486 return adreno_iommu_setstate(device, flags);
487}
488
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700490a3xx_getchipid(struct kgsl_device *device)
491{
Jordan Crouse54154c62012-03-27 16:33:26 -0600492 unsigned int majorid, minorid, patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700493
Jordan Crouse54154c62012-03-27 16:33:26 -0600494 /*
495 * We could detect the chipID from the hardware but it takes multiple
496 * registers to find the right combination. Since we traffic exclusively
497 * in system on chips, we can be (mostly) confident that a SOC version
498 * will match a GPU (at this juncture at least). So do the lazy/quick
499 * thing and set the chip_id based on the SoC
500 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700501
Jordan Crouse54154c62012-03-27 16:33:26 -0600502 if (cpu_is_apq8064()) {
Jordan Croused2b30d22012-05-21 08:41:51 -0600503 unsigned int version = socinfo_get_version();
504
Jordan Crouse54154c62012-03-27 16:33:26 -0600505 /* A320 */
506 majorid = 2;
507 minorid = 0;
Jordan Croused2b30d22012-05-21 08:41:51 -0600508
509 /*
510 * V1.1 has some GPU work arounds that we need to communicate
511 * up to user space via the patchid
512 */
513
514 if ((SOCINFO_VERSION_MAJOR(version) == 1) &&
515 (SOCINFO_VERSION_MINOR(version) == 1))
516 patchid = 1;
517 else
518 patchid = 0;
Jordan Crouse54154c62012-03-27 16:33:26 -0600519 } else if (cpu_is_msm8930()) {
520 /* A305 */
521 majorid = 0;
522 minorid = 5;
523 patchid = 0;
524 }
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700525
Jordan Crouse54154c62012-03-27 16:33:26 -0600526 return (0x03 << 24) | (majorid << 16) | (minorid << 8) | patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700527}
528
529static unsigned int
530a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531{
532 unsigned int chipid = 0;
533 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700534 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535
536 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
537 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
538 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
539
540 /*
541 * adreno 22x gpus are indicated by coreid 2,
542 * but REG_RBBM_PERIPHID1 always contains 0 for this field
543 */
Sudhakara Rao Tentudaebac22012-04-02 14:51:29 -0700544 if (cpu_is_msm8960() || cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 chipid = 2 << 24;
546 else
547 chipid = (coreid & 0xF) << 24;
548
549 chipid |= ((majorid >> 4) & 0xF) << 16;
550
551 minorid = ((revid >> 0) & 0xFF);
552
553 patchid = ((revid >> 16) & 0xFF);
554
555 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700556 /* 8960v3 returns 5 for patch release, but it should be 6 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530557 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 if (cpu_is_qsd8x50())
559 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700560 else if (cpu_is_msm8960() &&
561 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
562 patchid = 6;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530563 else if (cpu_is_msm8625() && minorid == 0)
564 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565
566 chipid |= (minorid << 8) | patchid;
567
568 return chipid;
569}
570
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700571static unsigned int
572adreno_getchipid(struct kgsl_device *device)
573{
Sudhakara Rao Tentu8ebb2282012-03-06 14:52:58 +0530574 if (cpu_is_apq8064() || cpu_is_msm8930())
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700575 return a3xx_getchipid(device);
576 else
577 return a2xx_getchipid(device);
578}
579
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580static inline bool _rev_match(unsigned int id, unsigned int entry)
581{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600582 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584
585static void
586adreno_identify_gpu(struct adreno_device *adreno_dev)
587{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600588 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589
590 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
591
592 core = (adreno_dev->chip_id >> 24) & 0xff;
593 major = (adreno_dev->chip_id >> 16) & 0xff;
594 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600595 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596
Jordan Crouse505df9c2011-07-28 08:37:59 -0600597 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
598 if (core == adreno_gpulist[i].core &&
599 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600600 _rev_match(minor, adreno_gpulist[i].minor) &&
601 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 }
604
Jordan Crouse505df9c2011-07-28 08:37:59 -0600605 if (i == ARRAY_SIZE(adreno_gpulist)) {
606 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
607 return;
608 }
609
610 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
611 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
612 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
613 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700614 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
615 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700616 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600617 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618}
619
620static int __devinit
621adreno_probe(struct platform_device *pdev)
622{
623 struct kgsl_device *device;
624 struct adreno_device *adreno_dev;
625 int status = -EINVAL;
626
627 device = (struct kgsl_device *)pdev->id_entry->driver_data;
628 adreno_dev = ADRENO_DEVICE(device);
629 device->parentdev = &pdev->dev;
630
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631 status = adreno_ringbuffer_init(device);
632 if (status != 0)
633 goto error;
634
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600635 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636 if (status)
637 goto error_close_rb;
638
639 adreno_debugfs_init(device);
640
641 kgsl_pwrscale_init(device);
642 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
643
644 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
645 return 0;
646
647error_close_rb:
648 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
649error:
650 device->parentdev = NULL;
651 return status;
652}
653
654static int __devexit adreno_remove(struct platform_device *pdev)
655{
656 struct kgsl_device *device;
657 struct adreno_device *adreno_dev;
658
659 device = (struct kgsl_device *)pdev->id_entry->driver_data;
660 adreno_dev = ADRENO_DEVICE(device);
661
662 kgsl_pwrscale_detach_policy(device);
663 kgsl_pwrscale_close(device);
664
665 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
666 kgsl_device_platform_remove(device);
667
668 return 0;
669}
670
671static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
672{
673 int status = -EINVAL;
674 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675
Jeremy Gebben388c2972011-12-16 09:05:07 -0700676 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677
678 /* Power up the device */
679 kgsl_pwrctrl_enable(device);
680
681 /* Identify the specific GPU */
682 adreno_identify_gpu(adreno_dev);
683
Jordan Crouse505df9c2011-07-28 08:37:59 -0600684 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
685 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
686 adreno_dev->chip_id);
687 goto error_clk_off;
688 }
689
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700690 /* Set up the MMU */
691 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600692 /*
693 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
694 * on older gpus
695 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700696 if (adreno_is_a20x(adreno_dev)) {
697 device->mh.mh_intf_cfg1 = 0;
698 device->mh.mh_intf_cfg2 = 0;
699 }
700
701 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600702 }
703
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700704 status = kgsl_mmu_start(device);
705 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706 goto error_clk_off;
707
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700708 /* Start the GPU */
709 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710
711 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700712 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713
714 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700715 if (status == 0) {
716 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
717 return 0;
718 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600721 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722error_clk_off:
723 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724
725 return status;
726}
727
728static int adreno_stop(struct kgsl_device *device)
729{
730 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
731
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 adreno_dev->drawctxt_active = NULL;
733
734 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
735
Shubhraprakash Das79447952012-04-26 18:12:23 -0600736 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700738 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530739 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800740 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600741
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 /* Power down the device */
743 kgsl_pwrctrl_disable(device);
744
745 return 0;
746}
747
748static int
749adreno_recover_hang(struct kgsl_device *device)
750{
751 int ret;
752 unsigned int *rb_buffer;
753 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
754 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
755 unsigned int timestamp;
756 unsigned int num_rb_contents;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757 unsigned int reftimestamp;
758 unsigned int enable_ts;
759 unsigned int soptimestamp;
760 unsigned int eoptimestamp;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700761 unsigned int context_id;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700762 struct kgsl_context *context;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700763 struct adreno_context *adreno_context;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700764 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765
766 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
767 rb_buffer = vmalloc(rb->buffer_desc.size);
768 if (!rb_buffer) {
769 KGSL_MEM_ERR(device,
770 "Failed to allocate memory for recovery: %x\n",
771 rb->buffer_desc.size);
772 return -ENOMEM;
773 }
774 /* Extract valid contents from rb which can stil be executed after
775 * hang */
776 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
777 if (ret)
778 goto done;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700779 kgsl_sharedmem_readl(&device->memstore, &context_id,
780 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
781 current_context));
782 context = idr_find(&device->context_idr, context_id);
783 if (context == NULL) {
784 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
785 context_id);
786 context_id = KGSL_MEMSTORE_GLOBAL;
787 }
788
789 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
790 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
791
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700793 KGSL_MEMSTORE_OFFSET(context_id,
794 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700796 KGSL_MEMSTORE_OFFSET(context_id,
797 ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700799 KGSL_MEMSTORE_OFFSET(context_id,
800 soptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700802 KGSL_MEMSTORE_OFFSET(context_id,
803 eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 /* Make sure memory is synchronized before restarting the GPU */
805 mb();
806 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700807 "Context id that caused a GPU hang: %d\n", context_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 /* restart device */
809 ret = adreno_stop(device);
810 if (ret)
811 goto done;
812 ret = adreno_start(device, true);
813 if (ret)
814 goto done;
815 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
816 /* Restore timestamp states */
817 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700818 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 soptimestamp);
820 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700821 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822 eoptimestamp);
Carter Cooperae4c7bc2012-04-10 09:40:49 -0600823
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 if (num_rb_contents) {
825 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700826 KGSL_MEMSTORE_OFFSET(context_id, ref_wait_ts),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 reftimestamp);
828 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700829 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 enable_ts);
831 }
832 /* Make sure all writes are posted before the GPU reads them */
833 wmb();
834 /* Mark the invalid context so no more commands are accepted from
835 * that context */
836
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700837 adreno_context = context->devctxt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838
839 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700840 "Context that caused a GPU hang: %d\n", adreno_context->id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700842 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700844 /*
845 * Set the reset status of all contexts to
846 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
847 * since thats the guilty party
848 */
849 while ((context = idr_get_next(&device->context_idr, &next))) {
850 if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
851 context->reset_status) {
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700852 if (context->id != context_id)
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700853 context->reset_status =
854 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
855 else
856 context->reset_status =
857 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
858 }
859 next = next + 1;
860 }
861
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862 /* Restore valid commands in ringbuffer */
863 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700864 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865done:
866 vfree(rb_buffer);
867 return ret;
868}
869
870static int
871adreno_dump_and_recover(struct kgsl_device *device)
872{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700873 int result = -ETIMEDOUT;
874
875 if (device->state == KGSL_STATE_HUNG)
876 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700877 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 mutex_unlock(&device->mutex);
879 wait_for_completion(&device->recovery_gate);
880 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700881 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882 result = 0;
883 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700884 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700886 /* Detected a hang */
887
888
889 /*
890 * Trigger an automatic dump of the state to
891 * the console
892 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700894
895 /*
896 * Make a GPU snapshot. For now, do it after the PM dump so we
897 * can at least be sure the PM dump will work as it always has
898 */
899 kgsl_device_snapshot(device, 1);
900
Jeremy Gebben388c2972011-12-16 09:05:07 -0700901 result = adreno_recover_hang(device);
902 if (result)
903 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
904 else
905 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
906 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907 }
908done:
909 return result;
910}
911
912static int adreno_getproperty(struct kgsl_device *device,
913 enum kgsl_property_type type,
914 void *value,
915 unsigned int sizebytes)
916{
917 int status = -EINVAL;
918 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
919
920 switch (type) {
921 case KGSL_PROP_DEVICE_INFO:
922 {
923 struct kgsl_devinfo devinfo;
924
925 if (sizebytes != sizeof(devinfo)) {
926 status = -EINVAL;
927 break;
928 }
929
930 memset(&devinfo, 0, sizeof(devinfo));
931 devinfo.device_id = device->id+1;
932 devinfo.chip_id = adreno_dev->chip_id;
933 devinfo.mmu_enabled = kgsl_mmu_enabled();
934 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -0600935 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
936 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937
938 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
939 0) {
940 status = -EFAULT;
941 break;
942 }
943 status = 0;
944 }
945 break;
946 case KGSL_PROP_DEVICE_SHADOW:
947 {
948 struct kgsl_shadowprop shadowprop;
949
950 if (sizebytes != sizeof(shadowprop)) {
951 status = -EINVAL;
952 break;
953 }
954 memset(&shadowprop, 0, sizeof(shadowprop));
955 if (device->memstore.hostptr) {
956 /*NOTE: with mmu enabled, gpuaddr doesn't mean
957 * anything to mmap().
958 */
959 shadowprop.gpuaddr = device->memstore.physaddr;
960 shadowprop.size = device->memstore.size;
961 /* GSL needs this to be set, even if it
962 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700963 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
964 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 }
966 if (copy_to_user(value, &shadowprop,
967 sizeof(shadowprop))) {
968 status = -EFAULT;
969 break;
970 }
971 status = 0;
972 }
973 break;
974 case KGSL_PROP_MMU_ENABLE:
975 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600976 int mmu_prop = kgsl_mmu_enabled();
977
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978 if (sizebytes != sizeof(int)) {
979 status = -EINVAL;
980 break;
981 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600982 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 status = -EFAULT;
984 break;
985 }
986 status = 0;
987 }
988 break;
989 case KGSL_PROP_INTERRUPT_WAITS:
990 {
991 int int_waits = 1;
992 if (sizebytes != sizeof(int)) {
993 status = -EINVAL;
994 break;
995 }
996 if (copy_to_user(value, &int_waits, sizeof(int))) {
997 status = -EFAULT;
998 break;
999 }
1000 status = 0;
1001 }
1002 break;
1003 default:
1004 status = -EINVAL;
1005 }
1006
1007 return status;
1008}
1009
Jordan Crousef7370f82012-04-18 09:31:07 -06001010static int adreno_setproperty(struct kgsl_device *device,
1011 enum kgsl_property_type type,
1012 void *value,
1013 unsigned int sizebytes)
1014{
1015 int status = -EINVAL;
1016
1017 switch (type) {
1018 case KGSL_PROP_PWRCTRL: {
1019 unsigned int enable;
1020 struct kgsl_device_platform_data *pdata =
1021 kgsl_device_get_drvdata(device);
1022
1023 if (sizebytes != sizeof(enable))
1024 break;
1025
1026 if (copy_from_user(&enable, (void __user *) value,
1027 sizeof(enable))) {
1028 status = -EFAULT;
1029 break;
1030 }
1031
1032 if (enable) {
1033 if (pdata->nap_allowed)
1034 device->pwrctrl.nap_allowed = true;
1035
1036 kgsl_pwrscale_enable(device);
1037 } else {
1038 device->pwrctrl.nap_allowed = false;
1039 kgsl_pwrscale_disable(device);
1040 }
1041
1042 status = 0;
1043 }
1044 break;
1045 default:
1046 break;
1047 }
1048
1049 return status;
1050}
1051
Lynus Vaz06a9a902011-10-04 19:25:33 +05301052static inline void adreno_poke(struct kgsl_device *device)
1053{
1054 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1055 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1056}
1057
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058/* Caller must hold the device mutex. */
1059int adreno_idle(struct kgsl_device *device, unsigned int timeout)
1060{
1061 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1062 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1063 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301064 unsigned long wait_timeout =
1065 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +05301066 unsigned long wait_time;
1067 unsigned long wait_time_part;
1068 unsigned int msecs;
1069 unsigned int msecs_first;
1070 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001071
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001072 kgsl_cffdump_regpoll(device->id,
1073 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074 0x00000000, 0x80000000);
1075 /* first, wait until the CP has consumed all the commands in
1076 * the ring buffer
1077 */
1078retry:
1079 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +05301080 msecs = adreno_dev->wait_timeout;
1081 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1082 msecs_part = (msecs - msecs_first + 3) / 4;
1083 wait_time = jiffies + wait_timeout;
1084 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -07001085 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086 do {
Lynus Vaz284d1042012-01-31 16:32:31 +05301087 if (time_after(jiffies, wait_time_part)) {
1088 adreno_poke(device);
1089 wait_time_part = jiffies +
1090 msecs_to_jiffies(msecs_part);
1091 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 GSL_RB_GET_READPTR(rb, &rb->rptr);
1093 if (time_after(jiffies, wait_time)) {
1094 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1095 rb->rptr, rb->wptr);
1096 goto err;
1097 }
1098 } while (rb->rptr != rb->wptr);
1099 }
1100
1101 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301102 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001103 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001104 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1105 &rbbm_status);
1106 if (adreno_is_a2xx(adreno_dev)) {
1107 if (rbbm_status == 0x110)
1108 return 0;
1109 } else {
1110 if (!(rbbm_status & 0x80000000))
1111 return 0;
1112 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113 }
1114
1115err:
1116 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
1117 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301118 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119 goto retry;
1120 }
1121 return -ETIMEDOUT;
1122}
1123
1124static unsigned int adreno_isidle(struct kgsl_device *device)
1125{
1126 int status = false;
1127 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1128 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1129 unsigned int rbbm_status;
1130
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001131 WARN_ON(device->state == KGSL_STATE_INIT);
1132 /* If the device isn't active, don't force it on. */
1133 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001134 /* Is the ring buffer is empty? */
1135 GSL_RB_GET_READPTR(rb, &rb->rptr);
1136 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1137 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001138 adreno_regread(device,
1139 adreno_dev->gpudev->reg_rbbm_status,
1140 &rbbm_status);
1141
1142 if (adreno_is_a2xx(adreno_dev)) {
1143 if (rbbm_status == 0x110)
1144 status = true;
1145 } else {
1146 if (!(rbbm_status & 0x80000000))
1147 status = true;
1148 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149 }
1150 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001151 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 }
1153 return status;
1154}
1155
1156/* Caller must hold the device mutex. */
1157static int adreno_suspend_context(struct kgsl_device *device)
1158{
1159 int status = 0;
1160 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1161
1162 /* switch to NULL ctxt */
1163 if (adreno_dev->drawctxt_active != NULL) {
1164 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1165 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1166 }
1167
1168 return status;
1169}
1170
Jordan Crouse233b2092012-04-18 09:31:09 -06001171/* Find a memory structure attached to an adreno context */
1172
1173struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1174 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1175{
1176 struct kgsl_context *context;
1177 struct adreno_context *adreno_context = NULL;
1178 int next = 0;
1179
1180 while (1) {
1181 context = idr_get_next(&device->context_idr, &next);
1182 if (context == NULL)
1183 break;
1184
1185 adreno_context = (struct adreno_context *)context->devctxt;
1186
1187 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1188 struct kgsl_memdesc *desc;
1189
1190 desc = &adreno_context->gpustate;
1191 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1192 return desc;
1193
1194 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1195 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1196 return desc;
1197 }
1198 next = next + 1;
1199 }
1200
1201 return NULL;
1202}
1203
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001204struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001205 unsigned int pt_base,
1206 unsigned int gpuaddr,
1207 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1211 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1212
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001213 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1214 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001216 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1217 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001219 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1220 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001222 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1223 size))
1224 return &device->mmu.setstate_memory;
1225
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001226 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1227
1228 if (entry)
1229 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230
Jordan Crouse233b2092012-04-18 09:31:09 -06001231 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001232}
1233
1234uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1235 unsigned int gpuaddr, unsigned int size)
1236{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001237 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001238
1239 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1240
1241 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242}
1243
1244void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1245 unsigned int *value)
1246{
1247 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06001248 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
1249 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001250
1251 if (!in_interrupt())
1252 kgsl_pre_hwaccess(device);
1253
1254 /*ensure this read finishes before the next one.
1255 * i.e. act like normal readl() */
1256 *value = __raw_readl(reg);
1257 rmb();
1258}
1259
1260void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1261 unsigned int value)
1262{
1263 unsigned int *reg;
1264
Jordan Crouse7501d452012-04-19 08:58:44 -06001265 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001266
1267 if (!in_interrupt())
1268 kgsl_pre_hwaccess(device);
1269
1270 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06001271 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272
1273 /*ensure previous writes post before this one,
1274 * i.e. act like normal writel() */
1275 wmb();
1276 __raw_writel(value, reg);
1277}
1278
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001279static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1280{
1281 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001282 if (k_ctxt != NULL) {
1283 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001284 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
1285 context_id = KGSL_CONTEXT_INVALID;
1286 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
1287 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001288 }
1289
1290 return context_id;
1291}
1292
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001294 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295{
1296 int status;
1297 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001298 unsigned int context_id;
1299
1300 mutex_lock(&device->mutex);
1301 context_id = _get_context_id(context);
1302 /*
1303 * If the context ID is invalid, we are in a race with
1304 * the context being destroyed by userspace so bail.
1305 */
1306 if (context_id == KGSL_CONTEXT_INVALID) {
1307 KGSL_DRV_WARN(device, "context was detached");
1308 status = -EINVAL;
1309 goto unlock;
1310 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001312 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001313 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001315 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001316 mb();
1317
1318 if (enableflag) {
1319 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001320 KGSL_MEMSTORE_OFFSET(context_id,
1321 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001323 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001324 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001325 KGSL_MEMSTORE_OFFSET(context_id,
1326 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001327 wmb();
1328 }
1329 } else {
1330 unsigned int cmds[2];
1331 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001332 KGSL_MEMSTORE_OFFSET(context_id,
1333 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001334 enableflag = 1;
1335 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001336 KGSL_MEMSTORE_OFFSET(context_id,
1337 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001338 wmb();
1339 /* submit a dummy packet so that even if all
1340 * commands upto timestamp get executed we will still
1341 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001342 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001343 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001344 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1345 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001346 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001347 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001348unlock:
1349 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350
1351 return status;
1352}
1353
1354/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001355 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001356 placing a process in wait q. For conditional interrupts we expect the
1357 process to already be in its wait q when its exit condition checking
1358 function is called.
1359*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001360#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001361({ \
1362 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001363 if (io) \
1364 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1365 else \
1366 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001367 __ret; \
1368})
1369
1370/* MUST be called with the device mutex held */
1371static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001372 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001373 unsigned int timestamp,
1374 unsigned int msecs)
1375{
1376 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001377 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001378 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001379 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001380 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301381 int retries;
1382 unsigned int msecs_first;
1383 unsigned int msecs_part;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001384 unsigned int ts_issued;
1385 unsigned int context_id = _get_context_id(context);
1386
1387 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001388
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301389 /* Don't wait forever, set a max value for now */
1390 if (msecs == -1)
1391 msecs = adreno_dev->wait_timeout;
1392
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001393 if (timestamp_cmp(timestamp, ts_issued) > 0) {
1394 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
1395 "last issued ts <%d:0x%x>\n",
1396 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001397 status = -EINVAL;
1398 goto done;
1399 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001400
Lynus Vaz06a9a902011-10-04 19:25:33 +05301401 /* Keep the first timeout as 100msecs before rewriting
1402 * the WPTR. Less visible impact if the WPTR has not
1403 * been updated properly.
1404 */
1405 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1406 msecs_part = (msecs - msecs_first + 3) / 4;
1407 for (retries = 0; retries < 5; retries++) {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001408 /*
1409 * If the context ID is invalid, we are in a race with
1410 * the context being destroyed by userspace so bail.
1411 */
1412 if (context_id == KGSL_CONTEXT_INVALID) {
1413 KGSL_DRV_WARN(device, "context was detached");
1414 status = -EINVAL;
1415 goto done;
1416 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001417 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001418 /* if the timestamp happens while we're not
1419 * waiting, there's a chance that an interrupt
1420 * will not be generated and thus the timestamp
1421 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301422 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001423 queue_work(device->work_queue, &device->ts_expired_ws);
1424 status = 0;
1425 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001426 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001427 adreno_poke(device);
1428 io_cnt = (io_cnt + 1) % 100;
1429 if (io_cnt <
1430 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1431 io = 0;
1432 mutex_unlock(&device->mutex);
1433 /* We need to make sure that the process is
1434 * placed in wait-q before its condition is called
1435 */
1436 status = kgsl_wait_event_interruptible_timeout(
1437 device->wait_queue,
1438 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001439 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001440 msecs_to_jiffies(retries ?
1441 msecs_part : msecs_first), io);
1442 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001443
Jeremy Gebben63904832012-02-07 16:10:55 -07001444 if (status > 0) {
1445 /*completed before the wait finished */
1446 status = 0;
1447 goto done;
1448 } else if (status < 0) {
1449 /*an error occurred*/
1450 goto done;
1451 }
1452 /*this wait timed out*/
1453 }
1454 status = -ETIMEDOUT;
1455 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001456 "Device hang detected while waiting for timestamp: "
1457 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
1458 "wptr: 0x%x\n",
1459 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07001460 adreno_dev->ringbuffer.wptr);
1461 if (!adreno_dump_and_recover(device)) {
1462 /* wait for idle after recovery as the
1463 * timestamp that this process wanted
1464 * to wait on may be invalid */
1465 if (!adreno_idle(device, KGSL_TIMEOUT_DEFAULT))
1466 status = 0;
1467 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001468done:
1469 return (int)status;
1470}
1471
1472static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001473 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001474{
1475 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001476 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001477
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001478 /*
1479 * If the context ID is invalid, we are in a race with
1480 * the context being destroyed by userspace so bail.
1481 */
1482 if (context_id == KGSL_CONTEXT_INVALID) {
1483 KGSL_DRV_WARN(device, "context was detached");
1484 return timestamp;
1485 }
Jordan Crousec659f382012-04-16 11:10:41 -06001486 switch (type) {
1487 case KGSL_TIMESTAMP_QUEUED: {
1488 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1489 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1490
1491 timestamp = rb->timestamp[context_id];
1492 break;
1493 }
1494 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001495 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06001496 break;
1497 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001498 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06001499 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
1500 break;
1501 }
1502
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001503 rmb();
1504
1505 return timestamp;
1506}
1507
1508static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1509 unsigned int cmd, void *data)
1510{
1511 int result = 0;
1512 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1513 struct kgsl_context *context;
1514
1515 switch (cmd) {
1516 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1517 binbase = data;
1518
1519 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1520 if (context) {
1521 adreno_drawctxt_set_bin_base_offset(
1522 dev_priv->device, context, binbase->offset);
1523 } else {
1524 result = -EINVAL;
1525 KGSL_DRV_ERR(dev_priv->device,
1526 "invalid drawctxt drawctxt_id %d "
1527 "device_id=%d\n",
1528 binbase->drawctxt_id, dev_priv->device->id);
1529 }
1530 break;
1531
1532 default:
1533 KGSL_DRV_INFO(dev_priv->device,
1534 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001535 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001536 break;
1537 }
1538 return result;
1539
1540}
1541
1542static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1543{
1544 gpu_freq /= 1000000;
1545 return ticks / gpu_freq;
1546}
1547
1548static void adreno_power_stats(struct kgsl_device *device,
1549 struct kgsl_power_stats *stats)
1550{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001551 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001552 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001553 unsigned int cycles;
1554
1555 /* Get the busy cycles counted since the counter was last reset */
1556 /* Calling this function also resets and restarts the counter */
1557
1558 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001559
1560 /* In order to calculate idle you have to have run the algorithm *
1561 * at least once to get a start time. */
1562 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001563 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001564 stats->total_time = tmp - pwr->time;
1565 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001566 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001567 pwrlevels[device->pwrctrl.active_pwrlevel].
1568 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001569 } else {
1570 stats->total_time = 0;
1571 stats->busy_time = 0;
1572 pwr->time = ktime_to_us(ktime_get());
1573 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001574}
1575
1576void adreno_irqctrl(struct kgsl_device *device, int state)
1577{
Jordan Crousea78c9172011-07-11 13:14:09 -06001578 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1579 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001580}
1581
Jordan Crousea0758f22011-12-07 11:19:22 -07001582static unsigned int adreno_gpuid(struct kgsl_device *device)
1583{
1584 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1585
1586 /* Standard KGSL gpuid format:
1587 * top word is 0x0002 for 2D or 0x0003 for 3D
1588 * Bottom word is core specific identifer
1589 */
1590
1591 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1592}
1593
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001594static const struct kgsl_functable adreno_functable = {
1595 /* Mandatory functions */
1596 .regread = adreno_regread,
1597 .regwrite = adreno_regwrite,
1598 .idle = adreno_idle,
1599 .isidle = adreno_isidle,
1600 .suspend_context = adreno_suspend_context,
1601 .start = adreno_start,
1602 .stop = adreno_stop,
1603 .getproperty = adreno_getproperty,
1604 .waittimestamp = adreno_waittimestamp,
1605 .readtimestamp = adreno_readtimestamp,
1606 .issueibcmds = adreno_ringbuffer_issueibcmds,
1607 .ioctl = adreno_ioctl,
1608 .setup_pt = adreno_setup_pt,
1609 .cleanup_pt = adreno_cleanup_pt,
1610 .power_stats = adreno_power_stats,
1611 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001612 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001613 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001614 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001615 /* Optional functions */
1616 .setstate = adreno_setstate,
1617 .drawctxt_create = adreno_drawctxt_create,
1618 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06001619 .setproperty = adreno_setproperty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001620};
1621
1622static struct platform_device_id adreno_id_table[] = {
1623 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1624 { },
1625};
1626MODULE_DEVICE_TABLE(platform, adreno_id_table);
1627
1628static struct platform_driver adreno_platform_driver = {
1629 .probe = adreno_probe,
1630 .remove = __devexit_p(adreno_remove),
1631 .suspend = kgsl_suspend_driver,
1632 .resume = kgsl_resume_driver,
1633 .id_table = adreno_id_table,
1634 .driver = {
1635 .owner = THIS_MODULE,
1636 .name = DEVICE_3D_NAME,
1637 .pm = &kgsl_pm_ops,
1638 }
1639};
1640
1641static int __init kgsl_3d_init(void)
1642{
1643 return platform_driver_register(&adreno_platform_driver);
1644}
1645
1646static void __exit kgsl_3d_exit(void)
1647{
1648 platform_driver_unregister(&adreno_platform_driver);
1649}
1650
1651module_init(kgsl_3d_init);
1652module_exit(kgsl_3d_exit);
1653
1654MODULE_DESCRIPTION("3D Graphics driver");
1655MODULE_VERSION("1.2");
1656MODULE_LICENSE("GPL v2");
1657MODULE_ALIAS("platform:kgsl_3d");