blob: fc519709f6f26c23a4b9f2e15658e79ce8d4291f [file] [log] [blame]
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001/* Copyright (c) 2002,2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
Steve Mucklef132c6c2012-06-06 18:30:57 -070013#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070014#include <linux/uaccess.h>
15#include <linux/vmalloc.h>
16#include <linux/ioctl.h>
17#include <linux/sched.h>
18
19#include <mach/socinfo.h>
20
21#include "kgsl.h"
22#include "kgsl_pwrscale.h"
23#include "kgsl_cffdump.h"
24#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060025#include "kgsl_iommu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27#include "adreno.h"
28#include "adreno_pm4types.h"
29#include "adreno_debugfs.h"
30#include "adreno_postmortem.h"
31
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070032#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070033#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034
35#define DRIVER_VERSION_MAJOR 3
36#define DRIVER_VERSION_MINOR 1
37
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038/* Adreno MH arbiter config*/
39#define ADRENO_CFG_MHARB \
40 (0x10 \
41 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
42 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
43 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
44 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
45 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
46 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
47 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
48 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
49 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
53 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
54 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
55
56#define ADRENO_MMU_CONFIG \
57 (0x01 \
58 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
59 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
60 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
61 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
62 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
69
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070static const struct kgsl_functable adreno_functable;
71
72static struct adreno_device device_3d0 = {
73 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070074 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075 .name = DEVICE_3D0_NAME,
76 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060077 .mh = {
78 .mharb = ADRENO_CFG_MHARB,
79 /* Remove 1k boundary check in z470 to avoid a GPU
80 * hang. Notice that this solution won't work if
81 * both EBI and SMI are used
82 */
83 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084 /* turn off memory protection unit by setting
85 acceptable physical address range to include
86 all pages. */
87 .mpu_base = 0x00000000,
88 .mpu_range = 0xFFFFF000,
89 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060090 .mmu = {
91 .config = ADRENO_MMU_CONFIG,
92 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 .iomemname = KGSL_3D0_REG_MEMORY,
97 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -060099 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
101 .suspend = kgsl_early_suspend_driver,
102 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600104#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600106 .gmem_base = 0,
107 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108 .pfp_fw = NULL,
109 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700110 .wait_timeout = 10000, /* in milliseconds */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600111 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112};
113
Tarun Karra3335f142012-06-19 14:11:48 -0700114/* This set of registers are used for Hang detection
115 * If the values of these registers are same after
116 * KGSL_TIMEOUT_PART time, GPU hang is reported in
117 * kernel log.
118 */
119unsigned int hang_detect_regs[] = {
120 A3XX_RBBM_STATUS,
121 REG_CP_RB_RPTR,
122 REG_CP_IB1_BASE,
123 REG_CP_IB1_BUFSZ,
124 REG_CP_IB2_BASE,
125 REG_CP_IB2_BUFSZ,
126};
127
128const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700129
Jordan Crouse505df9c2011-07-28 08:37:59 -0600130/*
131 * This is the master list of all GPU cores that are supported by this
132 * driver.
133 */
134
135#define ANY_ID (~0)
136
137static const struct {
138 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600139 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600140 const char *pm4fw;
141 const char *pfpfw;
142 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700143 unsigned int istore_size;
144 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700145 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530146 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600147} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600148 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700149 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530150 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530151 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
152 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530153 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600154 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700155 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530156 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600157 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700158 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530159 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600160 /*
161 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
162 * a hardware problem.
163 */
164 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700165 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530166 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700167 { ADRENO_REV_A225, 2, 2, 0, 6,
168 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530169 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600170 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700171 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530172 1536, 768, 3, SZ_512K },
173 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530174 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530175 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
176 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700177 /* A3XX doesn't use the pix_shader_start */
Jordan Croused2b30d22012-05-21 08:41:51 -0600178 { ADRENO_REV_A320, 3, 2, 0, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700179 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530180 512, 0, 2, SZ_512K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700181
Jordan Crouse505df9c2011-07-28 08:37:59 -0600182};
183
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600184static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185{
Jordan Crousea78c9172011-07-11 13:14:09 -0600186 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600187 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
Jordan Crousea78c9172011-07-11 13:14:09 -0600189 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
191 if (device->requested_state == KGSL_STATE_NONE) {
192 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700193 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194 queue_work(device->work_queue, &device->idle_check_ws);
195 } else if (device->pwrscale.policy != NULL) {
196 queue_work(device->work_queue, &device->idle_check_ws);
197 }
198 }
199
200 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800201 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 jiffies + device->pwrctrl.interval_timeout);
203 return result;
204}
205
Jordan Crouse9f739212011-07-28 08:37:57 -0600206static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207 struct kgsl_pagetable *pagetable)
208{
209 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
210 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
211
212 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
213
214 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
215
216 kgsl_mmu_unmap(pagetable, &device->memstore);
217
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600218 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219}
220
221static int adreno_setup_pt(struct kgsl_device *device,
222 struct kgsl_pagetable *pagetable)
223{
224 int result = 0;
225 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
226 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
229 GSL_PT_PAGE_RV);
230 if (result)
231 goto error;
232
233 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
234 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
235 if (result)
236 goto unmap_buffer_desc;
237
238 result = kgsl_mmu_map_global(pagetable, &device->memstore,
239 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
240 if (result)
241 goto unmap_memptrs_desc;
242
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600243 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
245 if (result)
246 goto unmap_memstore_desc;
247
248 return result;
249
250unmap_memstore_desc:
251 kgsl_mmu_unmap(pagetable, &device->memstore);
252
253unmap_memptrs_desc:
254 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
255
256unmap_buffer_desc:
257 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
258
259error:
260 return result;
261}
262
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600263static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600264 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600265 uint32_t flags)
266{
267 unsigned int pt_val, reg_pt_val;
268 unsigned int link[200];
269 unsigned int *cmds = &link[0];
270 int sizedwords = 0;
271 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
272 struct kgsl_memdesc **reg_map_desc;
Pu Chened8cbb52012-06-04 18:18:48 -0700273 void *reg_map_array = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600274 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600275 struct kgsl_context *context;
276 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600277
278 if (!adreno_dev->drawctxt_active)
279 return kgsl_mmu_device_setstate(&device->mmu, flags);
280 num_iommu_units = kgsl_mmu_get_reg_map_desc(&device->mmu,
281 &reg_map_array);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600282
283 context = idr_find(&device->context_idr, context_id);
284 adreno_ctx = context->devctxt;
285
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600286 reg_map_desc = reg_map_array;
287
288 if (kgsl_mmu_enable_clk(&device->mmu,
289 KGSL_IOMMU_CONTEXT_USER))
290 goto done;
291
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600292 cmds += __adreno_add_idle_indirect_cmds(cmds,
293 device->mmu.setstate_memory.gpuaddr +
294 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
295
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600296 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600297 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
298 device->mmu.setstate_memory.gpuaddr +
299 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
300 else
301 cmds += adreno_add_bank_change_cmds(cmds,
302 KGSL_IOMMU_CONTEXT_USER,
303 device->mmu.setstate_memory.gpuaddr +
304 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
305
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700306 pt_val = kgsl_mmu_pt_get_base_addr(device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600307 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600308 /*
309 * We need to perfrom the following operations for all
310 * IOMMU units
311 */
312 for (i = 0; i < num_iommu_units; i++) {
313 reg_pt_val = (pt_val &
314 (KGSL_IOMMU_TTBR0_PA_MASK <<
315 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
316 kgsl_mmu_get_pt_lsb(&device->mmu, i,
317 KGSL_IOMMU_CONTEXT_USER);
318 /*
319 * Set address of the new pagetable by writng to IOMMU
320 * TTBR0 register
321 */
322 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
323 *cmds++ = reg_map_desc[i]->gpuaddr +
324 (KGSL_IOMMU_CONTEXT_USER <<
325 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0;
326 *cmds++ = reg_pt_val;
327 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
328 *cmds++ = 0x00000000;
329
330 /*
331 * Read back the ttbr0 register as a barrier to ensure
332 * above writes have completed
333 */
334 cmds += adreno_add_read_cmds(device, cmds,
335 reg_map_desc[i]->gpuaddr +
336 (KGSL_IOMMU_CONTEXT_USER <<
337 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
338 reg_pt_val,
339 device->mmu.setstate_memory.gpuaddr +
340 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600341 }
342 /* invalidate all base pointers */
343 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
344 *cmds++ = 0x7fff;
345
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600346 cmds += __adreno_add_idle_indirect_cmds(cmds,
347 device->mmu.setstate_memory.gpuaddr +
348 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600349 }
350 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
351 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700352 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600353 */
354 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700355 reg_pt_val = (pt_val &
356 (KGSL_IOMMU_TTBR0_PA_MASK <<
357 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
358 kgsl_mmu_get_pt_lsb(&device->mmu, i,
359 KGSL_IOMMU_CONTEXT_USER);
360
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600361 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
362 *cmds++ = (reg_map_desc[i]->gpuaddr +
363 (KGSL_IOMMU_CONTEXT_USER <<
364 KGSL_IOMMU_CTX_SHIFT) +
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700365 KGSL_IOMMU_CTX_TLBIALL);
366 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600367
368 cmds += __adreno_add_idle_indirect_cmds(cmds,
369 device->mmu.setstate_memory.gpuaddr +
370 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
371
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600372 cmds += adreno_add_read_cmds(device, cmds,
373 reg_map_desc[i]->gpuaddr +
374 (KGSL_IOMMU_CONTEXT_USER <<
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700375 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
376 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600377 device->mmu.setstate_memory.gpuaddr +
378 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
379 }
380 }
381
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600382 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600383 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
384 reg_map_desc[num_iommu_units - 1]->gpuaddr - PAGE_SIZE,
385 device->mmu.setstate_memory.gpuaddr +
386 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
387 else
388 cmds += adreno_add_bank_change_cmds(cmds,
389 KGSL_IOMMU_CONTEXT_PRIV,
390 device->mmu.setstate_memory.gpuaddr +
391 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
392
393 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600394 if (sizedwords) {
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600395 /*
396 * add an interrupt at the end of commands so that the smmu
397 * disable clock off function will get called
398 */
399 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
400 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
401 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600402 /* This returns the per context timestamp but we need to
403 * use the global timestamp for iommu clock disablement */
404 adreno_ringbuffer_issuecmds(device, adreno_ctx,
405 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600406 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600407 kgsl_mmu_disable_clk_on_ts(&device->mmu,
408 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600409 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600410done:
411 if (num_iommu_units)
412 kfree(reg_map_array);
413}
414
415static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600416 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600417 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700418{
419 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
420 unsigned int link[32];
421 unsigned int *cmds = &link[0];
422 int sizedwords = 0;
423 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600424 struct kgsl_context *context;
425 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600427 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530428 * Fix target freeze issue by adding TLB flush for each submit
429 * on A20X based targets.
430 */
431 if (adreno_is_a20x(adreno_dev))
432 flags |= KGSL_MMUFLAGS_TLBFLUSH;
433 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600434 * If possible, then set the state via the command stream to avoid
435 * a CPU idle. Otherwise, use the default setstate which uses register
436 * writes For CFF dump we must idle and use the registers so that it is
437 * easier to filter out the mmu accesses from the dump
438 */
439 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600440 context = idr_find(&device->context_idr, context_id);
441 adreno_ctx = context->devctxt;
442
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
444 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600445 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446 *cmds++ = 0x00000000;
447
448 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600449 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600450 *cmds++ = kgsl_mmu_pt_get_base_addr(
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600451 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 sizedwords += 4;
453 }
454
455 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
456 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600457 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 1);
459 *cmds++ = 0x00000000;
460 sizedwords += 2;
461 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600462 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463 *cmds++ = mh_mmu_invalidate;
464 sizedwords += 2;
465 }
466
467 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600468 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 /* HW workaround: to resolve MMU page fault interrupts
470 * caused by the VGT.It prevents the CP PFP from filling
471 * the VGT DMA request fifo too early,thereby ensuring
472 * that the VGT will not fetch vertex/bin data until
473 * after the page table base register has been updated.
474 *
475 * Two null DRAW_INDX_BIN packets are inserted right
476 * after the page table base update, followed by a
477 * wait for idle. The null packets will fill up the
478 * VGT DMA request fifo and prevent any further
479 * vertex/bin updates from occurring until the wait
480 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600481 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 *cmds++ = (0x4 << 16) |
483 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
484 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600485 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600486 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600487 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488 *cmds++ = 0; /* viz query info */
489 *cmds++ = 0x0003C004; /* draw indicator */
490 *cmds++ = 0; /* bin base */
491 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600492 *cmds++ =
493 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600495 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496 *cmds++ = 0; /* viz query info */
497 *cmds++ = 0x0003C004; /* draw indicator */
498 *cmds++ = 0; /* bin base */
499 *cmds++ = 3; /* bin size */
500 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600501 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600503 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 *cmds++ = 0x00000000;
505 sizedwords += 21;
506 }
507
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600508
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600510 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 *cmds++ = 0x7fff; /* invalidate all base pointers */
512 sizedwords += 2;
513 }
514
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600515 adreno_ringbuffer_issuecmds(device, adreno_ctx,
516 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600518 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600519 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600520 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521}
522
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600523static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600524 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600525 uint32_t flags)
526{
527 /* call the mmu specific handler */
528 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600529 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600530 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600531 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600532}
533
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700535a3xx_getchipid(struct kgsl_device *device)
536{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600537 struct kgsl_device_platform_data *pdata =
538 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700539
Jordan Crouse54154c62012-03-27 16:33:26 -0600540 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600541 * All current A3XX chipids are detected at the SOC level. Leave this
542 * function here to support any future GPUs that have working
543 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600544 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700545
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600546 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700547}
548
549static unsigned int
550a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551{
552 unsigned int chipid = 0;
553 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600554 struct kgsl_device_platform_data *pdata =
555 kgsl_device_get_drvdata(device);
556
557 /* If the chip id is set at the platform level, then just use that */
558
559 if (pdata->chipid != 0)
560 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561
562 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
563 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
564 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
565
566 /*
567 * adreno 22x gpus are indicated by coreid 2,
568 * but REG_RBBM_PERIPHID1 always contains 0 for this field
569 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600570 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 chipid = 2 << 24;
572 else
573 chipid = (coreid & 0xF) << 24;
574
575 chipid |= ((majorid >> 4) & 0xF) << 16;
576
577 minorid = ((revid >> 0) & 0xFF);
578
579 patchid = ((revid >> 16) & 0xFF);
580
581 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530582 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583 if (cpu_is_qsd8x50())
584 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530585 else if (cpu_is_msm8625() && minorid == 0)
586 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587
588 chipid |= (minorid << 8) | patchid;
589
590 return chipid;
591}
592
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700593static unsigned int
594adreno_getchipid(struct kgsl_device *device)
595{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600596 struct kgsl_device_platform_data *pdata =
597 kgsl_device_get_drvdata(device);
598
599 /*
600 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
601 * an A2XX processor
602 */
603
604 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700605 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600606 else
607 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700608}
609
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610static inline bool _rev_match(unsigned int id, unsigned int entry)
611{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600612 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700613}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614
615static void
616adreno_identify_gpu(struct adreno_device *adreno_dev)
617{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600618 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619
620 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
621
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600622 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
623 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
624 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
625 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626
Jordan Crouse505df9c2011-07-28 08:37:59 -0600627 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
628 if (core == adreno_gpulist[i].core &&
629 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600630 _rev_match(minor, adreno_gpulist[i].minor) &&
631 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633 }
634
Jordan Crouse505df9c2011-07-28 08:37:59 -0600635 if (i == ARRAY_SIZE(adreno_gpulist)) {
636 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
637 return;
638 }
639
640 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
641 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
642 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
643 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700644 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
645 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700646 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600647 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648}
649
650static int __devinit
651adreno_probe(struct platform_device *pdev)
652{
653 struct kgsl_device *device;
654 struct adreno_device *adreno_dev;
655 int status = -EINVAL;
656
657 device = (struct kgsl_device *)pdev->id_entry->driver_data;
658 adreno_dev = ADRENO_DEVICE(device);
659 device->parentdev = &pdev->dev;
660
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 status = adreno_ringbuffer_init(device);
662 if (status != 0)
663 goto error;
664
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600665 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666 if (status)
667 goto error_close_rb;
668
669 adreno_debugfs_init(device);
670
671 kgsl_pwrscale_init(device);
672 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
673
674 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
675 return 0;
676
677error_close_rb:
678 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
679error:
680 device->parentdev = NULL;
681 return status;
682}
683
684static int __devexit adreno_remove(struct platform_device *pdev)
685{
686 struct kgsl_device *device;
687 struct adreno_device *adreno_dev;
688
689 device = (struct kgsl_device *)pdev->id_entry->driver_data;
690 adreno_dev = ADRENO_DEVICE(device);
691
692 kgsl_pwrscale_detach_policy(device);
693 kgsl_pwrscale_close(device);
694
695 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
696 kgsl_device_platform_remove(device);
697
698 return 0;
699}
700
701static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
702{
703 int status = -EINVAL;
704 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -0600706 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
707 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708
709 /* Power up the device */
710 kgsl_pwrctrl_enable(device);
711
712 /* Identify the specific GPU */
713 adreno_identify_gpu(adreno_dev);
714
Jordan Crouse505df9c2011-07-28 08:37:59 -0600715 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
716 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
717 adreno_dev->chip_id);
718 goto error_clk_off;
719 }
720
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700721 /* Set up the MMU */
722 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600723 /*
724 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
725 * on older gpus
726 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700727 if (adreno_is_a20x(adreno_dev)) {
728 device->mh.mh_intf_cfg1 = 0;
729 device->mh.mh_intf_cfg2 = 0;
730 }
731
732 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600733 }
734
Tarun Karra3335f142012-06-19 14:11:48 -0700735 /* Assign correct RBBM status register to hang detect regs
736 */
737 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
738
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700739 status = kgsl_mmu_start(device);
740 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741 goto error_clk_off;
742
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700743 /* Start the GPU */
744 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745
746 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700747 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748
749 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700750 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -0600751 /* While recovery is on we do not want timer to
752 * fire and attempt to change any device state */
753 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
754 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700755 return 0;
756 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600759 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760error_clk_off:
761 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762
763 return status;
764}
765
766static int adreno_stop(struct kgsl_device *device)
767{
768 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
769
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 adreno_dev->drawctxt_active = NULL;
771
772 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
773
Shubhraprakash Das79447952012-04-26 18:12:23 -0600774 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700776 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530777 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800778 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600779
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 /* Power down the device */
781 kgsl_pwrctrl_disable(device);
782
783 return 0;
784}
785
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -0600786static void adreno_mark_context_status(struct kgsl_device *device,
787 int recovery_status)
788{
789 struct kgsl_context *context;
790 int next = 0;
791 /*
792 * Set the reset status of all contexts to
793 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
794 * since thats the guilty party, if recovery failed then
795 * mark all as guilty
796 */
797 while ((context = idr_get_next(&device->context_idr, &next))) {
798 struct adreno_context *adreno_context = context->devctxt;
799 if (recovery_status) {
800 context->reset_status =
801 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
802 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
803 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
804 context->reset_status) {
805 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
806 CTXT_FLAGS_GPU_HANG_RECOVERED))
807 context->reset_status =
808 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
809 else
810 context->reset_status =
811 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
812 }
813 next = next + 1;
814 }
815}
816
Shubhraprakash Das5f085f42012-06-06 02:01:24 -0600817static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
818{
819 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
820 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
821 struct kgsl_context *context;
822 struct adreno_context *temp_adreno_context;
823 int next = 0;
824
825 while ((context = idr_get_next(&device->context_idr, &next))) {
826 temp_adreno_context = context->devctxt;
827 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
828 kgsl_sharedmem_writel(&device->memstore,
829 KGSL_MEMSTORE_OFFSET(context->id,
830 soptimestamp),
831 rb->timestamp[context->id]);
832 kgsl_sharedmem_writel(&device->memstore,
833 KGSL_MEMSTORE_OFFSET(context->id,
834 eoptimestamp),
835 rb->timestamp[context->id]);
836 }
837 next = next + 1;
838 }
839}
840
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600841static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
842{
843 vfree(rec_data->rb_buffer);
844 vfree(rec_data->bad_rb_buffer);
845}
846
847static int adreno_setup_recovery_data(struct kgsl_device *device,
848 struct adreno_recovery_data *rec_data)
849{
850 int ret = 0;
851 unsigned int ib1_sz, ib2_sz;
852 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
853 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
854
855 memset(rec_data, 0, sizeof(*rec_data));
856
857 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
858 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
859 if (ib1_sz || ib2_sz)
860 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
861
862 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
863 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
864 current_context));
865
866 kgsl_sharedmem_readl(&device->memstore,
867 &rec_data->global_eop,
868 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
869 eoptimestamp));
870
871 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
872 if (!rec_data->rb_buffer) {
873 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
874 rb->buffer_desc.size);
875 return -ENOMEM;
876 }
877
878 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
879 if (!rec_data->bad_rb_buffer) {
880 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
881 rb->buffer_desc.size);
882 ret = -ENOMEM;
883 goto done;
884 }
885
886done:
887 if (ret) {
888 vfree(rec_data->rb_buffer);
889 vfree(rec_data->bad_rb_buffer);
890 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700891 return ret;
892}
893
Shubhraprakash Das32240ef2012-06-06 20:27:46 -0600894static int
895_adreno_recover_hang(struct kgsl_device *device,
896 struct adreno_recovery_data *rec_data,
897 bool try_bad_commands)
898{
899 int ret;
900 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
901 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
902 struct kgsl_context *context;
903 struct adreno_context *adreno_context = NULL;
904 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
905
906 context = idr_find(&device->context_idr, rec_data->context_id);
907 if (context == NULL) {
908 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
909 rec_data->context_id);
910 } else {
911 adreno_context = context->devctxt;
912 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
913 }
914
915 /* Extract valid contents from rb which can still be executed after
916 * hang */
917 ret = adreno_ringbuffer_extract(rb, rec_data);
918 if (ret)
919 goto done;
920
921 /* restart device */
922 ret = adreno_stop(device);
923 if (ret) {
924 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
925 goto done;
926 }
927
928 ret = adreno_start(device, true);
929 if (ret) {
930 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
931 goto done;
932 }
933
934 if (context)
935 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
936 KGSL_MEMSTORE_GLOBAL);
937
Shubhraprakash Dasbd396692012-06-15 14:19:34 -0600938 /* If iommu is used then we need to make sure that the iommu clocks
939 * are on since there could be commands in pipeline that touch iommu */
940 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
941 ret = kgsl_mmu_enable_clk(&device->mmu,
942 KGSL_IOMMU_CONTEXT_USER);
943 if (ret)
944 goto done;
945 }
946
Shubhraprakash Das32240ef2012-06-06 20:27:46 -0600947 /* Do not try the bad caommands if recovery has failed bad commands
948 * once already */
949 if (!try_bad_commands)
950 rec_data->bad_rb_size = 0;
951
952 if (rec_data->bad_rb_size) {
953 int idle_ret;
954 /* submit the bad and good context commands and wait for
955 * them to pass */
956 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
957 rec_data->bad_rb_size);
958 idle_ret = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
959 if (idle_ret) {
960 ret = adreno_stop(device);
961 if (ret) {
962 KGSL_DRV_ERR(device,
963 "Device stop failed in recovery\n");
964 goto done;
965 }
966 ret = adreno_start(device, true);
967 if (ret) {
968 KGSL_DRV_ERR(device,
969 "Device start failed in recovery\n");
970 goto done;
971 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -0600972 if (context)
973 kgsl_mmu_setstate(&device->mmu,
974 adreno_context->pagetable,
975 KGSL_MEMSTORE_GLOBAL);
976
977 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
978 ret = kgsl_mmu_enable_clk(&device->mmu,
979 KGSL_IOMMU_CONTEXT_USER);
980 if (ret)
981 goto done;
982 }
983
Shubhraprakash Das32240ef2012-06-06 20:27:46 -0600984 ret = idle_ret;
985 KGSL_DRV_ERR(device,
986 "Bad context commands hung in recovery\n");
987 } else {
988 KGSL_DRV_ERR(device,
989 "Bad context commands succeeded in recovery\n");
990 if (adreno_context)
991 adreno_context->flags = (adreno_context->flags &
992 ~CTXT_FLAGS_GPU_HANG) |
993 CTXT_FLAGS_GPU_HANG_RECOVERED;
994 adreno_dev->drawctxt_active = last_active_ctx;
995 }
996 }
997 /* If either the bad command sequence failed or we did not play it */
998 if (ret || !rec_data->bad_rb_size) {
999 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1000 rec_data->rb_size);
1001 ret = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1002 if (ret) {
1003 /* If we fail here we can try to invalidate another
1004 * context and try recovering again */
1005 ret = -EAGAIN;
1006 goto done;
1007 }
1008 /* ringbuffer now has data from the last valid context id,
1009 * so restore the active_ctx to the last valid context */
1010 if (rec_data->last_valid_ctx_id) {
1011 struct kgsl_context *last_ctx =
1012 idr_find(&device->context_idr,
1013 rec_data->last_valid_ctx_id);
1014 if (last_ctx)
1015 adreno_dev->drawctxt_active = last_ctx->devctxt;
1016 }
1017 }
1018done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001019 /* Turn off iommu clocks */
1020 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1021 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001022 return ret;
1023}
1024
1025static int
1026adreno_recover_hang(struct kgsl_device *device,
1027 struct adreno_recovery_data *rec_data)
1028{
1029 int ret = 0;
1030 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1031 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1032 unsigned int timestamp;
1033
1034 KGSL_DRV_ERR(device,
1035 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1036 "Bad context_id: %u, global_eop: 0x%x\n",
1037 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1038
1039 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1040 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1041
1042 /* We may need to replay commands multiple times based on whether
1043 * multiple contexts hang the GPU */
1044 while (true) {
1045 if (!ret)
1046 ret = _adreno_recover_hang(device, rec_data, true);
1047 else
1048 ret = _adreno_recover_hang(device, rec_data, false);
1049
1050 if (-EAGAIN == ret) {
1051 /* setup new recovery parameters and retry, this
1052 * means more than 1 contexts are causing hang */
1053 adreno_destroy_recovery_data(rec_data);
1054 adreno_setup_recovery_data(device, rec_data);
1055 KGSL_DRV_ERR(device,
1056 "Retry recovery from 3D GPU hang. Recovery parameters: "
1057 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1058 rec_data->ib1, rec_data->context_id,
1059 rec_data->global_eop);
1060 } else {
1061 break;
1062 }
1063 }
1064
1065 if (ret)
1066 goto done;
1067
1068 /* Restore correct states after recovery */
1069 if (adreno_dev->drawctxt_active)
1070 device->mmu.hwpagetable =
1071 adreno_dev->drawctxt_active->pagetable;
1072 else
1073 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1074 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1075 kgsl_sharedmem_writel(&device->memstore,
1076 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1077 eoptimestamp),
1078 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1079done:
1080 adreno_set_max_ts_for_bad_ctxs(device);
1081 adreno_mark_context_status(device, ret);
1082 if (!ret)
1083 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1084 else
1085 KGSL_DRV_ERR(device, "Recovery failed\n");
1086 return ret;
1087}
1088
1089int
1090adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001093 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094
1095 if (device->state == KGSL_STATE_HUNG)
1096 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001097 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 mutex_unlock(&device->mutex);
1099 wait_for_completion(&device->recovery_gate);
1100 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001101 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102 result = 0;
1103 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001104 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001106 /* Detected a hang */
1107
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001108 /* Get the recovery data as soon as hang is detected */
1109 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001110 /*
1111 * Trigger an automatic dump of the state to
1112 * the console
1113 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001115
1116 /*
1117 * Make a GPU snapshot. For now, do it after the PM dump so we
1118 * can at least be sure the PM dump will work as it always has
1119 */
1120 kgsl_device_snapshot(device, 1);
1121
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001122 result = adreno_recover_hang(device, &rec_data);
1123 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001124 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001125 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001126 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001127 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001128 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1129 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001130 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001131 }
1132done:
1133 return result;
1134}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001135EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001136
1137static int adreno_getproperty(struct kgsl_device *device,
1138 enum kgsl_property_type type,
1139 void *value,
1140 unsigned int sizebytes)
1141{
1142 int status = -EINVAL;
1143 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1144
1145 switch (type) {
1146 case KGSL_PROP_DEVICE_INFO:
1147 {
1148 struct kgsl_devinfo devinfo;
1149
1150 if (sizebytes != sizeof(devinfo)) {
1151 status = -EINVAL;
1152 break;
1153 }
1154
1155 memset(&devinfo, 0, sizeof(devinfo));
1156 devinfo.device_id = device->id+1;
1157 devinfo.chip_id = adreno_dev->chip_id;
1158 devinfo.mmu_enabled = kgsl_mmu_enabled();
1159 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001160 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1161 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162
1163 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1164 0) {
1165 status = -EFAULT;
1166 break;
1167 }
1168 status = 0;
1169 }
1170 break;
1171 case KGSL_PROP_DEVICE_SHADOW:
1172 {
1173 struct kgsl_shadowprop shadowprop;
1174
1175 if (sizebytes != sizeof(shadowprop)) {
1176 status = -EINVAL;
1177 break;
1178 }
1179 memset(&shadowprop, 0, sizeof(shadowprop));
1180 if (device->memstore.hostptr) {
1181 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1182 * anything to mmap().
1183 */
1184 shadowprop.gpuaddr = device->memstore.physaddr;
1185 shadowprop.size = device->memstore.size;
1186 /* GSL needs this to be set, even if it
1187 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001188 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1189 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190 }
1191 if (copy_to_user(value, &shadowprop,
1192 sizeof(shadowprop))) {
1193 status = -EFAULT;
1194 break;
1195 }
1196 status = 0;
1197 }
1198 break;
1199 case KGSL_PROP_MMU_ENABLE:
1200 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001201 int mmu_prop = kgsl_mmu_enabled();
1202
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001203 if (sizebytes != sizeof(int)) {
1204 status = -EINVAL;
1205 break;
1206 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001207 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 status = -EFAULT;
1209 break;
1210 }
1211 status = 0;
1212 }
1213 break;
1214 case KGSL_PROP_INTERRUPT_WAITS:
1215 {
1216 int int_waits = 1;
1217 if (sizebytes != sizeof(int)) {
1218 status = -EINVAL;
1219 break;
1220 }
1221 if (copy_to_user(value, &int_waits, sizeof(int))) {
1222 status = -EFAULT;
1223 break;
1224 }
1225 status = 0;
1226 }
1227 break;
1228 default:
1229 status = -EINVAL;
1230 }
1231
1232 return status;
1233}
1234
Jordan Crousef7370f82012-04-18 09:31:07 -06001235static int adreno_setproperty(struct kgsl_device *device,
1236 enum kgsl_property_type type,
1237 void *value,
1238 unsigned int sizebytes)
1239{
1240 int status = -EINVAL;
1241
1242 switch (type) {
1243 case KGSL_PROP_PWRCTRL: {
1244 unsigned int enable;
1245 struct kgsl_device_platform_data *pdata =
1246 kgsl_device_get_drvdata(device);
1247
1248 if (sizebytes != sizeof(enable))
1249 break;
1250
1251 if (copy_from_user(&enable, (void __user *) value,
1252 sizeof(enable))) {
1253 status = -EFAULT;
1254 break;
1255 }
1256
1257 if (enable) {
1258 if (pdata->nap_allowed)
1259 device->pwrctrl.nap_allowed = true;
1260
1261 kgsl_pwrscale_enable(device);
1262 } else {
1263 device->pwrctrl.nap_allowed = false;
1264 kgsl_pwrscale_disable(device);
1265 }
1266
1267 status = 0;
1268 }
1269 break;
1270 default:
1271 break;
1272 }
1273
1274 return status;
1275}
1276
Lynus Vaz06a9a902011-10-04 19:25:33 +05301277static inline void adreno_poke(struct kgsl_device *device)
1278{
1279 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1280 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1281}
1282
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001283/* Caller must hold the device mutex. */
1284int adreno_idle(struct kgsl_device *device, unsigned int timeout)
1285{
1286 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1287 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1288 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301289 unsigned long wait_timeout =
1290 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +05301291 unsigned long wait_time;
1292 unsigned long wait_time_part;
1293 unsigned int msecs;
1294 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07001295 unsigned int msecs_part = KGSL_TIMEOUT_PART;
1296 unsigned int prev_reg_val[hang_detect_regs_count];
1297
1298 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001299
Tarun Karrad526d1e2012-06-11 17:51:09 -07001300 /* Restrict timeout value between adreno_dev->wait_timeout and 0 */
1301 if ((timeout == 0) || (timeout > adreno_dev->wait_timeout))
1302 msecs = adreno_dev->wait_timeout;
1303 else
1304 msecs = timeout;
1305
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001306 kgsl_cffdump_regpoll(device->id,
1307 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001308 0x00000000, 0x80000000);
1309 /* first, wait until the CP has consumed all the commands in
1310 * the ring buffer
1311 */
1312retry:
1313 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +05301314 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Lynus Vaz284d1042012-01-31 16:32:31 +05301315 wait_time = jiffies + wait_timeout;
1316 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -07001317 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318 do {
Lynus Vaz284d1042012-01-31 16:32:31 +05301319 if (time_after(jiffies, wait_time_part)) {
1320 adreno_poke(device);
1321 wait_time_part = jiffies +
1322 msecs_to_jiffies(msecs_part);
Tarun Karra3335f142012-06-19 14:11:48 -07001323 if ((adreno_hang_detect(device, prev_reg_val)))
1324 goto err;
Lynus Vaz284d1042012-01-31 16:32:31 +05301325 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001326 GSL_RB_GET_READPTR(rb, &rb->rptr);
1327 if (time_after(jiffies, wait_time)) {
1328 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1329 rb->rptr, rb->wptr);
1330 goto err;
1331 }
1332 } while (rb->rptr != rb->wptr);
1333 }
1334
1335 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301336 wait_time = jiffies + wait_timeout;
Tarun Karra3335f142012-06-19 14:11:48 -07001337 wait_time_part = jiffies + msecs_to_jiffies(msecs_part);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001338 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001339 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1340 &rbbm_status);
1341 if (adreno_is_a2xx(adreno_dev)) {
1342 if (rbbm_status == 0x110)
1343 return 0;
1344 } else {
1345 if (!(rbbm_status & 0x80000000))
1346 return 0;
1347 }
Tarun Karra3335f142012-06-19 14:11:48 -07001348
1349 /* Dont wait for timeout, detect hang faster.
1350 */
1351 if (time_after(jiffies, wait_time_part)) {
1352 wait_time_part = jiffies +
1353 msecs_to_jiffies(msecs_part);
1354 if ((adreno_hang_detect(device, prev_reg_val)))
1355 goto err;
1356 }
1357
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001358 }
1359
1360err:
1361 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001362 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1363 !adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301364 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365 goto retry;
1366 }
1367 return -ETIMEDOUT;
1368}
1369
1370static unsigned int adreno_isidle(struct kgsl_device *device)
1371{
1372 int status = false;
1373 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1374 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1375 unsigned int rbbm_status;
1376
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001377 WARN_ON(device->state == KGSL_STATE_INIT);
1378 /* If the device isn't active, don't force it on. */
1379 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001380 /* Is the ring buffer is empty? */
1381 GSL_RB_GET_READPTR(rb, &rb->rptr);
1382 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1383 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001384 adreno_regread(device,
1385 adreno_dev->gpudev->reg_rbbm_status,
1386 &rbbm_status);
1387
1388 if (adreno_is_a2xx(adreno_dev)) {
1389 if (rbbm_status == 0x110)
1390 status = true;
1391 } else {
1392 if (!(rbbm_status & 0x80000000))
1393 status = true;
1394 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001395 }
1396 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001397 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001398 }
1399 return status;
1400}
1401
1402/* Caller must hold the device mutex. */
1403static int adreno_suspend_context(struct kgsl_device *device)
1404{
1405 int status = 0;
1406 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1407
1408 /* switch to NULL ctxt */
1409 if (adreno_dev->drawctxt_active != NULL) {
1410 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1411 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1412 }
1413
1414 return status;
1415}
1416
Jordan Crouse233b2092012-04-18 09:31:09 -06001417/* Find a memory structure attached to an adreno context */
1418
1419struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1420 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1421{
1422 struct kgsl_context *context;
1423 struct adreno_context *adreno_context = NULL;
1424 int next = 0;
1425
1426 while (1) {
1427 context = idr_get_next(&device->context_idr, &next);
1428 if (context == NULL)
1429 break;
1430
1431 adreno_context = (struct adreno_context *)context->devctxt;
1432
1433 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1434 struct kgsl_memdesc *desc;
1435
1436 desc = &adreno_context->gpustate;
1437 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1438 return desc;
1439
1440 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1441 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1442 return desc;
1443 }
1444 next = next + 1;
1445 }
1446
1447 return NULL;
1448}
1449
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001450struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001451 unsigned int pt_base,
1452 unsigned int gpuaddr,
1453 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001454{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001456 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1457 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1458
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001459 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1460 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001461
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001462 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1463 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001464
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001465 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1466 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001467
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001468 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1469 size))
1470 return &device->mmu.setstate_memory;
1471
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001472 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1473
1474 if (entry)
1475 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001476
Jordan Crouse233b2092012-04-18 09:31:09 -06001477 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001478}
1479
1480uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1481 unsigned int gpuaddr, unsigned int size)
1482{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001483 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001484
1485 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1486
1487 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001488}
1489
1490void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1491 unsigned int *value)
1492{
1493 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06001494 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
1495 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001496
1497 if (!in_interrupt())
1498 kgsl_pre_hwaccess(device);
1499
1500 /*ensure this read finishes before the next one.
1501 * i.e. act like normal readl() */
1502 *value = __raw_readl(reg);
1503 rmb();
1504}
1505
1506void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1507 unsigned int value)
1508{
1509 unsigned int *reg;
1510
Jordan Crouse7501d452012-04-19 08:58:44 -06001511 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001512
1513 if (!in_interrupt())
1514 kgsl_pre_hwaccess(device);
1515
1516 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06001517 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001518
1519 /*ensure previous writes post before this one,
1520 * i.e. act like normal writel() */
1521 wmb();
1522 __raw_writel(value, reg);
1523}
1524
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001525static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1526{
1527 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001528 if (k_ctxt != NULL) {
1529 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001530 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
1531 context_id = KGSL_CONTEXT_INVALID;
1532 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
1533 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001534 }
1535
1536 return context_id;
1537}
1538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001539static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001540 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001541{
1542 int status;
1543 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001544 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001545 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001546
1547 mutex_lock(&device->mutex);
1548 context_id = _get_context_id(context);
1549 /*
1550 * If the context ID is invalid, we are in a race with
1551 * the context being destroyed by userspace so bail.
1552 */
1553 if (context_id == KGSL_CONTEXT_INVALID) {
1554 KGSL_DRV_WARN(device, "context was detached");
1555 status = -EINVAL;
1556 goto unlock;
1557 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001558
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001559 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001560 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001561 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001562 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001563 mb();
1564
1565 if (enableflag) {
1566 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001567 KGSL_MEMSTORE_OFFSET(context_id,
1568 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001569 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001570 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001571 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001572 KGSL_MEMSTORE_OFFSET(context_id,
1573 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001574 wmb();
1575 }
1576 } else {
1577 unsigned int cmds[2];
1578 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001579 KGSL_MEMSTORE_OFFSET(context_id,
1580 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001581 enableflag = 1;
1582 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001583 KGSL_MEMSTORE_OFFSET(context_id,
1584 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001585 wmb();
1586 /* submit a dummy packet so that even if all
1587 * commands upto timestamp get executed we will still
1588 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001589 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001590 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001591
1592 if (adreno_dev->drawctxt_active)
1593 adreno_ringbuffer_issuecmds(device,
1594 adreno_dev->drawctxt_active,
1595 KGSL_CMD_FLAGS_NONE, &cmds[0], 2);
1596 else
1597 /* We would never call this function if there
1598 * was no active contexts running */
1599 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001600 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001601 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001602unlock:
1603 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001604
1605 return status;
1606}
1607
1608/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001609 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001610 placing a process in wait q. For conditional interrupts we expect the
1611 process to already be in its wait q when its exit condition checking
1612 function is called.
1613*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001614#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001615({ \
1616 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001617 if (io) \
1618 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1619 else \
1620 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001621 __ret; \
1622})
1623
Tarun Karra3335f142012-06-19 14:11:48 -07001624
1625
1626unsigned int adreno_hang_detect(struct kgsl_device *device,
1627 unsigned int *prev_reg_val)
1628{
1629 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1630 unsigned int curr_reg_val[hang_detect_regs_count];
1631 unsigned int hang_detected = 1;
1632 unsigned int i;
1633
1634 if (!adreno_dev->fast_hang_detect)
1635 return 0;
1636
1637 for (i = 0; i < hang_detect_regs_count; i++) {
1638 adreno_regread(device, hang_detect_regs[i],
1639 &curr_reg_val[i]);
1640 if (curr_reg_val[i] != prev_reg_val[i]) {
1641 prev_reg_val[i] = curr_reg_val[i];
1642 hang_detected = 0;
1643 }
1644 }
1645
1646 return hang_detected;
1647}
1648
1649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001650/* MUST be called with the device mutex held */
1651static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001652 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001653 unsigned int timestamp,
1654 unsigned int msecs)
1655{
1656 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001657 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001658 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001659 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001660 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07001661 int retries = 0;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301662 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07001663 unsigned int msecs_part = KGSL_TIMEOUT_PART;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001664 unsigned int ts_issued;
1665 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07001666 unsigned int time_elapsed = 0;
1667 unsigned int prev_reg_val[hang_detect_regs_count];
1668
1669 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001670
1671 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001672
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301673 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07001674 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301675 msecs = adreno_dev->wait_timeout;
1676
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001677 if (timestamp_cmp(timestamp, ts_issued) > 0) {
1678 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
1679 "last issued ts <%d:0x%x>\n",
1680 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001681 status = -EINVAL;
1682 goto done;
1683 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001684
Lynus Vaz06a9a902011-10-04 19:25:33 +05301685 /* Keep the first timeout as 100msecs before rewriting
1686 * the WPTR. Less visible impact if the WPTR has not
1687 * been updated properly.
1688 */
1689 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Tarun Karra3335f142012-06-19 14:11:48 -07001690 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001691 /*
1692 * If the context ID is invalid, we are in a race with
1693 * the context being destroyed by userspace so bail.
1694 */
1695 if (context_id == KGSL_CONTEXT_INVALID) {
1696 KGSL_DRV_WARN(device, "context was detached");
1697 status = -EINVAL;
1698 goto done;
1699 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001700 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001701 /* if the timestamp happens while we're not
1702 * waiting, there's a chance that an interrupt
1703 * will not be generated and thus the timestamp
1704 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301705 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001706 queue_work(device->work_queue, &device->ts_expired_ws);
1707 status = 0;
1708 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001709 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001710 adreno_poke(device);
1711 io_cnt = (io_cnt + 1) % 100;
1712 if (io_cnt <
1713 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1714 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07001715
1716 if ((retries > 0) &&
1717 (adreno_hang_detect(device, prev_reg_val)))
1718 goto hang_dump;
1719
Jeremy Gebben63904832012-02-07 16:10:55 -07001720 mutex_unlock(&device->mutex);
1721 /* We need to make sure that the process is
1722 * placed in wait-q before its condition is called
1723 */
1724 status = kgsl_wait_event_interruptible_timeout(
1725 device->wait_queue,
1726 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001727 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001728 msecs_to_jiffies(retries ?
1729 msecs_part : msecs_first), io);
1730 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001731
Jeremy Gebben63904832012-02-07 16:10:55 -07001732 if (status > 0) {
1733 /*completed before the wait finished */
1734 status = 0;
1735 goto done;
1736 } else if (status < 0) {
1737 /*an error occurred*/
1738 goto done;
1739 }
1740 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07001741
1742 time_elapsed = time_elapsed +
1743 (retries ? msecs_part : msecs_first);
1744 retries++;
1745
1746 } while (time_elapsed < msecs);
1747
1748hang_dump:
Jeremy Gebben63904832012-02-07 16:10:55 -07001749 status = -ETIMEDOUT;
1750 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001751 "Device hang detected while waiting for timestamp: "
1752 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
1753 "wptr: 0x%x\n",
1754 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07001755 adreno_dev->ringbuffer.wptr);
1756 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001757 /* The timestamp that this process wanted
1758 * to wait on may be invalid or expired now
1759 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07001760 status = 0;
1761 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001762done:
1763 return (int)status;
1764}
1765
1766static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001767 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001768{
1769 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001770 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001771
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001772 /*
1773 * If the context ID is invalid, we are in a race with
1774 * the context being destroyed by userspace so bail.
1775 */
1776 if (context_id == KGSL_CONTEXT_INVALID) {
1777 KGSL_DRV_WARN(device, "context was detached");
1778 return timestamp;
1779 }
Jordan Crousec659f382012-04-16 11:10:41 -06001780 switch (type) {
1781 case KGSL_TIMESTAMP_QUEUED: {
1782 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1783 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1784
1785 timestamp = rb->timestamp[context_id];
1786 break;
1787 }
1788 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001789 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06001790 break;
1791 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001792 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06001793 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
1794 break;
1795 }
1796
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797 rmb();
1798
1799 return timestamp;
1800}
1801
1802static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1803 unsigned int cmd, void *data)
1804{
1805 int result = 0;
1806 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1807 struct kgsl_context *context;
1808
1809 switch (cmd) {
1810 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1811 binbase = data;
1812
1813 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1814 if (context) {
1815 adreno_drawctxt_set_bin_base_offset(
1816 dev_priv->device, context, binbase->offset);
1817 } else {
1818 result = -EINVAL;
1819 KGSL_DRV_ERR(dev_priv->device,
1820 "invalid drawctxt drawctxt_id %d "
1821 "device_id=%d\n",
1822 binbase->drawctxt_id, dev_priv->device->id);
1823 }
1824 break;
1825
1826 default:
1827 KGSL_DRV_INFO(dev_priv->device,
1828 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001829 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001830 break;
1831 }
1832 return result;
1833
1834}
1835
1836static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1837{
1838 gpu_freq /= 1000000;
1839 return ticks / gpu_freq;
1840}
1841
1842static void adreno_power_stats(struct kgsl_device *device,
1843 struct kgsl_power_stats *stats)
1844{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001845 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001846 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001847 unsigned int cycles;
1848
1849 /* Get the busy cycles counted since the counter was last reset */
1850 /* Calling this function also resets and restarts the counter */
1851
1852 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001853
1854 /* In order to calculate idle you have to have run the algorithm *
1855 * at least once to get a start time. */
1856 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001857 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858 stats->total_time = tmp - pwr->time;
1859 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001860 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001861 pwrlevels[device->pwrctrl.active_pwrlevel].
1862 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863 } else {
1864 stats->total_time = 0;
1865 stats->busy_time = 0;
1866 pwr->time = ktime_to_us(ktime_get());
1867 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001868}
1869
1870void adreno_irqctrl(struct kgsl_device *device, int state)
1871{
Jordan Crousea78c9172011-07-11 13:14:09 -06001872 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1873 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001874}
1875
Jordan Croused6535882012-06-20 08:22:16 -06001876static unsigned int adreno_gpuid(struct kgsl_device *device,
1877 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07001878{
1879 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1880
Jordan Croused6535882012-06-20 08:22:16 -06001881 /* Some applications need to know the chip ID too, so pass
1882 * that as a parameter */
1883
1884 if (chipid != NULL)
1885 *chipid = adreno_dev->chip_id;
1886
Jordan Crousea0758f22011-12-07 11:19:22 -07001887 /* Standard KGSL gpuid format:
1888 * top word is 0x0002 for 2D or 0x0003 for 3D
1889 * Bottom word is core specific identifer
1890 */
1891
1892 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1893}
1894
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001895static const struct kgsl_functable adreno_functable = {
1896 /* Mandatory functions */
1897 .regread = adreno_regread,
1898 .regwrite = adreno_regwrite,
1899 .idle = adreno_idle,
1900 .isidle = adreno_isidle,
1901 .suspend_context = adreno_suspend_context,
1902 .start = adreno_start,
1903 .stop = adreno_stop,
1904 .getproperty = adreno_getproperty,
1905 .waittimestamp = adreno_waittimestamp,
1906 .readtimestamp = adreno_readtimestamp,
1907 .issueibcmds = adreno_ringbuffer_issueibcmds,
1908 .ioctl = adreno_ioctl,
1909 .setup_pt = adreno_setup_pt,
1910 .cleanup_pt = adreno_cleanup_pt,
1911 .power_stats = adreno_power_stats,
1912 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001913 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001914 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001915 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001916 /* Optional functions */
1917 .setstate = adreno_setstate,
1918 .drawctxt_create = adreno_drawctxt_create,
1919 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06001920 .setproperty = adreno_setproperty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001921};
1922
1923static struct platform_device_id adreno_id_table[] = {
1924 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1925 { },
1926};
1927MODULE_DEVICE_TABLE(platform, adreno_id_table);
1928
1929static struct platform_driver adreno_platform_driver = {
1930 .probe = adreno_probe,
1931 .remove = __devexit_p(adreno_remove),
1932 .suspend = kgsl_suspend_driver,
1933 .resume = kgsl_resume_driver,
1934 .id_table = adreno_id_table,
1935 .driver = {
1936 .owner = THIS_MODULE,
1937 .name = DEVICE_3D_NAME,
1938 .pm = &kgsl_pm_ops,
1939 }
1940};
1941
1942static int __init kgsl_3d_init(void)
1943{
1944 return platform_driver_register(&adreno_platform_driver);
1945}
1946
1947static void __exit kgsl_3d_exit(void)
1948{
1949 platform_driver_unregister(&adreno_platform_driver);
1950}
1951
1952module_init(kgsl_3d_init);
1953module_exit(kgsl_3d_exit);
1954
1955MODULE_DESCRIPTION("3D Graphics driver");
1956MODULE_VERSION("1.2");
1957MODULE_LICENSE("GPL v2");
1958MODULE_ALIAS("platform:kgsl_3d");