blob: 7a6fc24876eaa509527ff078a1ac9d47d90b1112 [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
306 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
307 pt_val = kgsl_mmu_pt_get_base_addr(device->mmu.hwpagetable);
308 /*
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);
341
342 /* set the asid */
343 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
344 *cmds++ = reg_map_desc[i]->gpuaddr +
345 (KGSL_IOMMU_CONTEXT_USER <<
346 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_CONTEXTIDR;
347 *cmds++ = kgsl_mmu_get_hwpagetable_asid(&device->mmu);
348 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
349 *cmds++ = 0x00000000;
350
351 /* Read back asid to ensure above write completes */
352 cmds += adreno_add_read_cmds(device, cmds,
353 reg_map_desc[i]->gpuaddr +
354 (KGSL_IOMMU_CONTEXT_USER <<
355 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_CONTEXTIDR,
356 kgsl_mmu_get_hwpagetable_asid(&device->mmu),
357 device->mmu.setstate_memory.gpuaddr +
358 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
359 }
360 /* invalidate all base pointers */
361 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
362 *cmds++ = 0x7fff;
363
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600364 cmds += __adreno_add_idle_indirect_cmds(cmds,
365 device->mmu.setstate_memory.gpuaddr +
366 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600367 }
368 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
369 /*
370 * tlb flush based on asid, no need to flush entire tlb
371 */
372 for (i = 0; i < num_iommu_units; i++) {
373 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
374 *cmds++ = (reg_map_desc[i]->gpuaddr +
375 (KGSL_IOMMU_CONTEXT_USER <<
376 KGSL_IOMMU_CTX_SHIFT) +
377 KGSL_IOMMU_CTX_TLBIASID);
378 *cmds++ = kgsl_mmu_get_hwpagetable_asid(&device->mmu);
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600379
380 cmds += __adreno_add_idle_indirect_cmds(cmds,
381 device->mmu.setstate_memory.gpuaddr +
382 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
383
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600384 cmds += adreno_add_read_cmds(device, cmds,
385 reg_map_desc[i]->gpuaddr +
386 (KGSL_IOMMU_CONTEXT_USER <<
387 KGSL_IOMMU_CTX_SHIFT) +
388 KGSL_IOMMU_CONTEXTIDR,
389 kgsl_mmu_get_hwpagetable_asid(&device->mmu),
390 device->mmu.setstate_memory.gpuaddr +
391 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
392 }
393 }
394
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600395 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600396 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
397 reg_map_desc[num_iommu_units - 1]->gpuaddr - PAGE_SIZE,
398 device->mmu.setstate_memory.gpuaddr +
399 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
400 else
401 cmds += adreno_add_bank_change_cmds(cmds,
402 KGSL_IOMMU_CONTEXT_PRIV,
403 device->mmu.setstate_memory.gpuaddr +
404 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
405
406 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600407 if (sizedwords) {
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600408 /*
409 * add an interrupt at the end of commands so that the smmu
410 * disable clock off function will get called
411 */
412 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
413 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
414 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600415 /* This returns the per context timestamp but we need to
416 * use the global timestamp for iommu clock disablement */
417 adreno_ringbuffer_issuecmds(device, adreno_ctx,
418 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600419 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600420 kgsl_mmu_disable_clk_on_ts(&device->mmu,
421 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600422 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600423done:
424 if (num_iommu_units)
425 kfree(reg_map_array);
426}
427
428static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600429 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600430 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431{
432 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
433 unsigned int link[32];
434 unsigned int *cmds = &link[0];
435 int sizedwords = 0;
436 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600437 struct kgsl_context *context;
438 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600440 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530441 * Fix target freeze issue by adding TLB flush for each submit
442 * on A20X based targets.
443 */
444 if (adreno_is_a20x(adreno_dev))
445 flags |= KGSL_MMUFLAGS_TLBFLUSH;
446 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600447 * If possible, then set the state via the command stream to avoid
448 * a CPU idle. Otherwise, use the default setstate which uses register
449 * writes For CFF dump we must idle and use the registers so that it is
450 * easier to filter out the mmu accesses from the dump
451 */
452 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600453 context = idr_find(&device->context_idr, context_id);
454 adreno_ctx = context->devctxt;
455
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
457 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600458 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 *cmds++ = 0x00000000;
460
461 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600462 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600463 *cmds++ = kgsl_mmu_pt_get_base_addr(
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600464 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465 sizedwords += 4;
466 }
467
468 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
469 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600470 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 1);
472 *cmds++ = 0x00000000;
473 sizedwords += 2;
474 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600475 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476 *cmds++ = mh_mmu_invalidate;
477 sizedwords += 2;
478 }
479
480 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600481 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 /* HW workaround: to resolve MMU page fault interrupts
483 * caused by the VGT.It prevents the CP PFP from filling
484 * the VGT DMA request fifo too early,thereby ensuring
485 * that the VGT will not fetch vertex/bin data until
486 * after the page table base register has been updated.
487 *
488 * Two null DRAW_INDX_BIN packets are inserted right
489 * after the page table base update, followed by a
490 * wait for idle. The null packets will fill up the
491 * VGT DMA request fifo and prevent any further
492 * vertex/bin updates from occurring until the wait
493 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600494 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 *cmds++ = (0x4 << 16) |
496 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
497 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600498 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600499 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600500 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501 *cmds++ = 0; /* viz query info */
502 *cmds++ = 0x0003C004; /* draw indicator */
503 *cmds++ = 0; /* bin base */
504 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600505 *cmds++ =
506 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600508 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 *cmds++ = 0; /* viz query info */
510 *cmds++ = 0x0003C004; /* draw indicator */
511 *cmds++ = 0; /* bin base */
512 *cmds++ = 3; /* bin size */
513 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600514 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600516 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 *cmds++ = 0x00000000;
518 sizedwords += 21;
519 }
520
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600521
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600523 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 *cmds++ = 0x7fff; /* invalidate all base pointers */
525 sizedwords += 2;
526 }
527
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600528 adreno_ringbuffer_issuecmds(device, adreno_ctx,
529 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600531 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600532 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600533 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534}
535
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600536static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600537 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600538 uint32_t flags)
539{
540 /* call the mmu specific handler */
541 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600542 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600543 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600544 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600545}
546
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700548a3xx_getchipid(struct kgsl_device *device)
549{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600550 struct kgsl_device_platform_data *pdata =
551 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700552
Jordan Crouse54154c62012-03-27 16:33:26 -0600553 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600554 * All current A3XX chipids are detected at the SOC level. Leave this
555 * function here to support any future GPUs that have working
556 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600557 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700558
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600559 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700560}
561
562static unsigned int
563a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564{
565 unsigned int chipid = 0;
566 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600567 struct kgsl_device_platform_data *pdata =
568 kgsl_device_get_drvdata(device);
569
570 /* If the chip id is set at the platform level, then just use that */
571
572 if (pdata->chipid != 0)
573 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574
575 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
576 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
577 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
578
579 /*
580 * adreno 22x gpus are indicated by coreid 2,
581 * but REG_RBBM_PERIPHID1 always contains 0 for this field
582 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600583 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 chipid = 2 << 24;
585 else
586 chipid = (coreid & 0xF) << 24;
587
588 chipid |= ((majorid >> 4) & 0xF) << 16;
589
590 minorid = ((revid >> 0) & 0xFF);
591
592 patchid = ((revid >> 16) & 0xFF);
593
594 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530595 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596 if (cpu_is_qsd8x50())
597 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530598 else if (cpu_is_msm8625() && minorid == 0)
599 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600
601 chipid |= (minorid << 8) | patchid;
602
603 return chipid;
604}
605
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700606static unsigned int
607adreno_getchipid(struct kgsl_device *device)
608{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600609 struct kgsl_device_platform_data *pdata =
610 kgsl_device_get_drvdata(device);
611
612 /*
613 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
614 * an A2XX processor
615 */
616
617 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700618 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600619 else
620 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700621}
622
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623static inline bool _rev_match(unsigned int id, unsigned int entry)
624{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600625 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627
628static void
629adreno_identify_gpu(struct adreno_device *adreno_dev)
630{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600631 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632
633 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
634
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600635 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
636 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
637 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
638 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639
Jordan Crouse505df9c2011-07-28 08:37:59 -0600640 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
641 if (core == adreno_gpulist[i].core &&
642 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600643 _rev_match(minor, adreno_gpulist[i].minor) &&
644 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646 }
647
Jordan Crouse505df9c2011-07-28 08:37:59 -0600648 if (i == ARRAY_SIZE(adreno_gpulist)) {
649 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
650 return;
651 }
652
653 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
654 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
655 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
656 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700657 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
658 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700659 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600660 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661}
662
663static int __devinit
664adreno_probe(struct platform_device *pdev)
665{
666 struct kgsl_device *device;
667 struct adreno_device *adreno_dev;
668 int status = -EINVAL;
669
670 device = (struct kgsl_device *)pdev->id_entry->driver_data;
671 adreno_dev = ADRENO_DEVICE(device);
672 device->parentdev = &pdev->dev;
673
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 status = adreno_ringbuffer_init(device);
675 if (status != 0)
676 goto error;
677
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600678 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 if (status)
680 goto error_close_rb;
681
682 adreno_debugfs_init(device);
683
684 kgsl_pwrscale_init(device);
685 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
686
687 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
688 return 0;
689
690error_close_rb:
691 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
692error:
693 device->parentdev = NULL;
694 return status;
695}
696
697static int __devexit adreno_remove(struct platform_device *pdev)
698{
699 struct kgsl_device *device;
700 struct adreno_device *adreno_dev;
701
702 device = (struct kgsl_device *)pdev->id_entry->driver_data;
703 adreno_dev = ADRENO_DEVICE(device);
704
705 kgsl_pwrscale_detach_policy(device);
706 kgsl_pwrscale_close(device);
707
708 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
709 kgsl_device_platform_remove(device);
710
711 return 0;
712}
713
714static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
715{
716 int status = -EINVAL;
717 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -0600719 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
720 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721
722 /* Power up the device */
723 kgsl_pwrctrl_enable(device);
724
725 /* Identify the specific GPU */
726 adreno_identify_gpu(adreno_dev);
727
Jordan Crouse505df9c2011-07-28 08:37:59 -0600728 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
729 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
730 adreno_dev->chip_id);
731 goto error_clk_off;
732 }
733
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700734 /* Set up the MMU */
735 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600736 /*
737 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
738 * on older gpus
739 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700740 if (adreno_is_a20x(adreno_dev)) {
741 device->mh.mh_intf_cfg1 = 0;
742 device->mh.mh_intf_cfg2 = 0;
743 }
744
745 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600746 }
747
Tarun Karra3335f142012-06-19 14:11:48 -0700748 /* Assign correct RBBM status register to hang detect regs
749 */
750 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
751
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700752 status = kgsl_mmu_start(device);
753 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 goto error_clk_off;
755
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700756 /* Start the GPU */
757 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758
759 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700760 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761
762 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700763 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -0600764 /* While recovery is on we do not want timer to
765 * fire and attempt to change any device state */
766 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
767 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700768 return 0;
769 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600772 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773error_clk_off:
774 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775
776 return status;
777}
778
779static int adreno_stop(struct kgsl_device *device)
780{
781 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
782
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783 adreno_dev->drawctxt_active = NULL;
784
785 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
786
Shubhraprakash Das79447952012-04-26 18:12:23 -0600787 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700789 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530790 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800791 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600792
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793 /* Power down the device */
794 kgsl_pwrctrl_disable(device);
795
796 return 0;
797}
798
799static int
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600800adreno_recover_hang(struct kgsl_device *device,
801 struct adreno_recovery_data *rec_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802{
803 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
805 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
806 unsigned int timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 unsigned int reftimestamp;
808 unsigned int enable_ts;
809 unsigned int soptimestamp;
810 unsigned int eoptimestamp;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700811 struct kgsl_context *context;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700812 struct adreno_context *adreno_context;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700813 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600815 KGSL_DRV_ERR(device,
816 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
817 "Bad context_id: %u, global_eop: 0x%x\n", rec_data->ib1,
818 rec_data->context_id, rec_data->global_eop);
819
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 /* Extract valid contents from rb which can stil be executed after
821 * hang */
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600822 ret = adreno_ringbuffer_extract(rb, rec_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 if (ret)
824 goto done;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600825
826 context = idr_find(&device->context_idr, rec_data->context_id);
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700827 if (context == NULL) {
828 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600829 rec_data->context_id);
830 rec_data->context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700831 }
832
833 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
834 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
835
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600837 KGSL_MEMSTORE_OFFSET(rec_data->context_id,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700838 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600840 KGSL_MEMSTORE_OFFSET(rec_data->context_id,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700841 ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600843 KGSL_MEMSTORE_OFFSET(rec_data->context_id,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700844 soptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600846 KGSL_MEMSTORE_OFFSET(rec_data->context_id,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700847 eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848 /* Make sure memory is synchronized before restarting the GPU */
849 mb();
850 KGSL_CTXT_ERR(device,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600851 "Context id that caused a GPU hang: %d\n",
852 rec_data->context_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853 /* restart device */
854 ret = adreno_stop(device);
855 if (ret)
856 goto done;
857 ret = adreno_start(device, true);
858 if (ret)
859 goto done;
860 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
861 /* Restore timestamp states */
862 kgsl_sharedmem_writel(&device->memstore,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600863 KGSL_MEMSTORE_OFFSET(rec_data->context_id,
864 soptimestamp), soptimestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865 kgsl_sharedmem_writel(&device->memstore,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600866 KGSL_MEMSTORE_OFFSET(rec_data->context_id,
867 eoptimestamp), eoptimestamp);
Carter Cooperae4c7bc2012-04-10 09:40:49 -0600868
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600869 if (rec_data->rb_size) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870 kgsl_sharedmem_writel(&device->memstore,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600871 KGSL_MEMSTORE_OFFSET(rec_data->context_id,
872 ref_wait_ts), reftimestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700873 kgsl_sharedmem_writel(&device->memstore,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600874 KGSL_MEMSTORE_OFFSET(rec_data->context_id,
875 ts_cmp_enable), enable_ts);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 }
877 /* Make sure all writes are posted before the GPU reads them */
878 wmb();
879 /* Mark the invalid context so no more commands are accepted from
880 * that context */
881
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700882 adreno_context = context->devctxt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883
884 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700885 "Context that caused a GPU hang: %d\n", adreno_context->id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700886
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700887 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700889 /*
890 * Set the reset status of all contexts to
891 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
892 * since thats the guilty party
893 */
894 while ((context = idr_get_next(&device->context_idr, &next))) {
895 if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
896 context->reset_status) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600897 if (context->id != rec_data->context_id)
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700898 context->reset_status =
899 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
900 else
901 context->reset_status =
902 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
903 }
904 next = next + 1;
905 }
906
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907 /* Restore valid commands in ringbuffer */
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600908 adreno_ringbuffer_restore(rb, rec_data->rb_buffer, rec_data->rb_size);
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700909 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -0600910 /* wait for idle */
911 ret = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912done:
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600913 return ret;
914}
915
916static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
917{
918 vfree(rec_data->rb_buffer);
919 vfree(rec_data->bad_rb_buffer);
920}
921
922static int adreno_setup_recovery_data(struct kgsl_device *device,
923 struct adreno_recovery_data *rec_data)
924{
925 int ret = 0;
926 unsigned int ib1_sz, ib2_sz;
927 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
928 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
929
930 memset(rec_data, 0, sizeof(*rec_data));
931
932 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
933 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
934 if (ib1_sz || ib2_sz)
935 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
936
937 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
938 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
939 current_context));
940
941 kgsl_sharedmem_readl(&device->memstore,
942 &rec_data->global_eop,
943 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
944 eoptimestamp));
945
946 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
947 if (!rec_data->rb_buffer) {
948 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
949 rb->buffer_desc.size);
950 return -ENOMEM;
951 }
952
953 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
954 if (!rec_data->bad_rb_buffer) {
955 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
956 rb->buffer_desc.size);
957 ret = -ENOMEM;
958 goto done;
959 }
960
961done:
962 if (ret) {
963 vfree(rec_data->rb_buffer);
964 vfree(rec_data->bad_rb_buffer);
965 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 return ret;
967}
968
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600969int adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600972 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973
974 if (device->state == KGSL_STATE_HUNG)
975 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700976 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977 mutex_unlock(&device->mutex);
978 wait_for_completion(&device->recovery_gate);
979 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700980 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981 result = 0;
982 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700983 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700985 /* Detected a hang */
986
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -0600987 /* Get the recovery data as soon as hang is detected */
988 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700989 /*
990 * Trigger an automatic dump of the state to
991 * the console
992 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700994
995 /*
996 * Make a GPU snapshot. For now, do it after the PM dump so we
997 * can at least be sure the PM dump will work as it always has
998 */
999 kgsl_device_snapshot(device, 1);
1000
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001001 result = adreno_recover_hang(device, &rec_data);
1002 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001003 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001004 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001005 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001006 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001007 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1008 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001009 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010 }
1011done:
1012 return result;
1013}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001014EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015
1016static int adreno_getproperty(struct kgsl_device *device,
1017 enum kgsl_property_type type,
1018 void *value,
1019 unsigned int sizebytes)
1020{
1021 int status = -EINVAL;
1022 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1023
1024 switch (type) {
1025 case KGSL_PROP_DEVICE_INFO:
1026 {
1027 struct kgsl_devinfo devinfo;
1028
1029 if (sizebytes != sizeof(devinfo)) {
1030 status = -EINVAL;
1031 break;
1032 }
1033
1034 memset(&devinfo, 0, sizeof(devinfo));
1035 devinfo.device_id = device->id+1;
1036 devinfo.chip_id = adreno_dev->chip_id;
1037 devinfo.mmu_enabled = kgsl_mmu_enabled();
1038 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001039 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1040 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041
1042 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1043 0) {
1044 status = -EFAULT;
1045 break;
1046 }
1047 status = 0;
1048 }
1049 break;
1050 case KGSL_PROP_DEVICE_SHADOW:
1051 {
1052 struct kgsl_shadowprop shadowprop;
1053
1054 if (sizebytes != sizeof(shadowprop)) {
1055 status = -EINVAL;
1056 break;
1057 }
1058 memset(&shadowprop, 0, sizeof(shadowprop));
1059 if (device->memstore.hostptr) {
1060 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1061 * anything to mmap().
1062 */
1063 shadowprop.gpuaddr = device->memstore.physaddr;
1064 shadowprop.size = device->memstore.size;
1065 /* GSL needs this to be set, even if it
1066 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001067 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1068 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069 }
1070 if (copy_to_user(value, &shadowprop,
1071 sizeof(shadowprop))) {
1072 status = -EFAULT;
1073 break;
1074 }
1075 status = 0;
1076 }
1077 break;
1078 case KGSL_PROP_MMU_ENABLE:
1079 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001080 int mmu_prop = kgsl_mmu_enabled();
1081
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 if (sizebytes != sizeof(int)) {
1083 status = -EINVAL;
1084 break;
1085 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001086 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087 status = -EFAULT;
1088 break;
1089 }
1090 status = 0;
1091 }
1092 break;
1093 case KGSL_PROP_INTERRUPT_WAITS:
1094 {
1095 int int_waits = 1;
1096 if (sizebytes != sizeof(int)) {
1097 status = -EINVAL;
1098 break;
1099 }
1100 if (copy_to_user(value, &int_waits, sizeof(int))) {
1101 status = -EFAULT;
1102 break;
1103 }
1104 status = 0;
1105 }
1106 break;
1107 default:
1108 status = -EINVAL;
1109 }
1110
1111 return status;
1112}
1113
Jordan Crousef7370f82012-04-18 09:31:07 -06001114static int adreno_setproperty(struct kgsl_device *device,
1115 enum kgsl_property_type type,
1116 void *value,
1117 unsigned int sizebytes)
1118{
1119 int status = -EINVAL;
1120
1121 switch (type) {
1122 case KGSL_PROP_PWRCTRL: {
1123 unsigned int enable;
1124 struct kgsl_device_platform_data *pdata =
1125 kgsl_device_get_drvdata(device);
1126
1127 if (sizebytes != sizeof(enable))
1128 break;
1129
1130 if (copy_from_user(&enable, (void __user *) value,
1131 sizeof(enable))) {
1132 status = -EFAULT;
1133 break;
1134 }
1135
1136 if (enable) {
1137 if (pdata->nap_allowed)
1138 device->pwrctrl.nap_allowed = true;
1139
1140 kgsl_pwrscale_enable(device);
1141 } else {
1142 device->pwrctrl.nap_allowed = false;
1143 kgsl_pwrscale_disable(device);
1144 }
1145
1146 status = 0;
1147 }
1148 break;
1149 default:
1150 break;
1151 }
1152
1153 return status;
1154}
1155
Lynus Vaz06a9a902011-10-04 19:25:33 +05301156static inline void adreno_poke(struct kgsl_device *device)
1157{
1158 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1159 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1160}
1161
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162/* Caller must hold the device mutex. */
1163int adreno_idle(struct kgsl_device *device, unsigned int timeout)
1164{
1165 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1166 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1167 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301168 unsigned long wait_timeout =
1169 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +05301170 unsigned long wait_time;
1171 unsigned long wait_time_part;
1172 unsigned int msecs;
1173 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07001174 unsigned int msecs_part = KGSL_TIMEOUT_PART;
1175 unsigned int prev_reg_val[hang_detect_regs_count];
1176
1177 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001179 kgsl_cffdump_regpoll(device->id,
1180 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001181 0x00000000, 0x80000000);
1182 /* first, wait until the CP has consumed all the commands in
1183 * the ring buffer
1184 */
1185retry:
1186 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +05301187 msecs = adreno_dev->wait_timeout;
1188 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Lynus Vaz284d1042012-01-31 16:32:31 +05301189 wait_time = jiffies + wait_timeout;
1190 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -07001191 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001192 do {
Lynus Vaz284d1042012-01-31 16:32:31 +05301193 if (time_after(jiffies, wait_time_part)) {
1194 adreno_poke(device);
1195 wait_time_part = jiffies +
1196 msecs_to_jiffies(msecs_part);
Tarun Karra3335f142012-06-19 14:11:48 -07001197 if ((adreno_hang_detect(device, prev_reg_val)))
1198 goto err;
Lynus Vaz284d1042012-01-31 16:32:31 +05301199 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 GSL_RB_GET_READPTR(rb, &rb->rptr);
1201 if (time_after(jiffies, wait_time)) {
1202 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1203 rb->rptr, rb->wptr);
1204 goto err;
1205 }
1206 } while (rb->rptr != rb->wptr);
1207 }
1208
1209 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301210 wait_time = jiffies + wait_timeout;
Tarun Karra3335f142012-06-19 14:11:48 -07001211 wait_time_part = jiffies + msecs_to_jiffies(msecs_part);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001212 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001213 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1214 &rbbm_status);
1215 if (adreno_is_a2xx(adreno_dev)) {
1216 if (rbbm_status == 0x110)
1217 return 0;
1218 } else {
1219 if (!(rbbm_status & 0x80000000))
1220 return 0;
1221 }
Tarun Karra3335f142012-06-19 14:11:48 -07001222
1223 /* Dont wait for timeout, detect hang faster.
1224 */
1225 if (time_after(jiffies, wait_time_part)) {
1226 wait_time_part = jiffies +
1227 msecs_to_jiffies(msecs_part);
1228 if ((adreno_hang_detect(device, prev_reg_val)))
1229 goto err;
1230 }
1231
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001232 }
1233
1234err:
1235 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001236 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1237 !adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301238 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001239 goto retry;
1240 }
1241 return -ETIMEDOUT;
1242}
1243
1244static unsigned int adreno_isidle(struct kgsl_device *device)
1245{
1246 int status = false;
1247 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1248 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1249 unsigned int rbbm_status;
1250
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001251 WARN_ON(device->state == KGSL_STATE_INIT);
1252 /* If the device isn't active, don't force it on. */
1253 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001254 /* Is the ring buffer is empty? */
1255 GSL_RB_GET_READPTR(rb, &rb->rptr);
1256 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1257 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001258 adreno_regread(device,
1259 adreno_dev->gpudev->reg_rbbm_status,
1260 &rbbm_status);
1261
1262 if (adreno_is_a2xx(adreno_dev)) {
1263 if (rbbm_status == 0x110)
1264 status = true;
1265 } else {
1266 if (!(rbbm_status & 0x80000000))
1267 status = true;
1268 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269 }
1270 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001271 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272 }
1273 return status;
1274}
1275
1276/* Caller must hold the device mutex. */
1277static int adreno_suspend_context(struct kgsl_device *device)
1278{
1279 int status = 0;
1280 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1281
1282 /* switch to NULL ctxt */
1283 if (adreno_dev->drawctxt_active != NULL) {
1284 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1285 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1286 }
1287
1288 return status;
1289}
1290
Jordan Crouse233b2092012-04-18 09:31:09 -06001291/* Find a memory structure attached to an adreno context */
1292
1293struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1294 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1295{
1296 struct kgsl_context *context;
1297 struct adreno_context *adreno_context = NULL;
1298 int next = 0;
1299
1300 while (1) {
1301 context = idr_get_next(&device->context_idr, &next);
1302 if (context == NULL)
1303 break;
1304
1305 adreno_context = (struct adreno_context *)context->devctxt;
1306
1307 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1308 struct kgsl_memdesc *desc;
1309
1310 desc = &adreno_context->gpustate;
1311 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1312 return desc;
1313
1314 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1315 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1316 return desc;
1317 }
1318 next = next + 1;
1319 }
1320
1321 return NULL;
1322}
1323
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001324struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001325 unsigned int pt_base,
1326 unsigned int gpuaddr,
1327 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001328{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1331 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1332
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001333 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1334 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001336 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1337 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001338
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001339 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1340 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001341
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001342 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1343 size))
1344 return &device->mmu.setstate_memory;
1345
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001346 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1347
1348 if (entry)
1349 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350
Jordan Crouse233b2092012-04-18 09:31:09 -06001351 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001352}
1353
1354uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1355 unsigned int gpuaddr, unsigned int size)
1356{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001357 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001358
1359 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1360
1361 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001362}
1363
1364void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1365 unsigned int *value)
1366{
1367 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06001368 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
1369 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001370
1371 if (!in_interrupt())
1372 kgsl_pre_hwaccess(device);
1373
1374 /*ensure this read finishes before the next one.
1375 * i.e. act like normal readl() */
1376 *value = __raw_readl(reg);
1377 rmb();
1378}
1379
1380void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1381 unsigned int value)
1382{
1383 unsigned int *reg;
1384
Jordan Crouse7501d452012-04-19 08:58:44 -06001385 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386
1387 if (!in_interrupt())
1388 kgsl_pre_hwaccess(device);
1389
1390 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06001391 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392
1393 /*ensure previous writes post before this one,
1394 * i.e. act like normal writel() */
1395 wmb();
1396 __raw_writel(value, reg);
1397}
1398
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001399static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1400{
1401 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001402 if (k_ctxt != NULL) {
1403 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001404 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
1405 context_id = KGSL_CONTEXT_INVALID;
1406 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
1407 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001408 }
1409
1410 return context_id;
1411}
1412
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001413static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001414 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001415{
1416 int status;
1417 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001418 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001419 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001420
1421 mutex_lock(&device->mutex);
1422 context_id = _get_context_id(context);
1423 /*
1424 * If the context ID is invalid, we are in a race with
1425 * the context being destroyed by userspace so bail.
1426 */
1427 if (context_id == KGSL_CONTEXT_INVALID) {
1428 KGSL_DRV_WARN(device, "context was detached");
1429 status = -EINVAL;
1430 goto unlock;
1431 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001433 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001434 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001436 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001437 mb();
1438
1439 if (enableflag) {
1440 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001441 KGSL_MEMSTORE_OFFSET(context_id,
1442 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001443 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001444 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001446 KGSL_MEMSTORE_OFFSET(context_id,
1447 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001448 wmb();
1449 }
1450 } else {
1451 unsigned int cmds[2];
1452 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001453 KGSL_MEMSTORE_OFFSET(context_id,
1454 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455 enableflag = 1;
1456 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001457 KGSL_MEMSTORE_OFFSET(context_id,
1458 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001459 wmb();
1460 /* submit a dummy packet so that even if all
1461 * commands upto timestamp get executed we will still
1462 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001463 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001464 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001465
1466 if (adreno_dev->drawctxt_active)
1467 adreno_ringbuffer_issuecmds(device,
1468 adreno_dev->drawctxt_active,
1469 KGSL_CMD_FLAGS_NONE, &cmds[0], 2);
1470 else
1471 /* We would never call this function if there
1472 * was no active contexts running */
1473 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001474 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001475 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001476unlock:
1477 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478
1479 return status;
1480}
1481
1482/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001483 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001484 placing a process in wait q. For conditional interrupts we expect the
1485 process to already be in its wait q when its exit condition checking
1486 function is called.
1487*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001488#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001489({ \
1490 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001491 if (io) \
1492 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1493 else \
1494 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001495 __ret; \
1496})
1497
Tarun Karra3335f142012-06-19 14:11:48 -07001498
1499
1500unsigned int adreno_hang_detect(struct kgsl_device *device,
1501 unsigned int *prev_reg_val)
1502{
1503 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1504 unsigned int curr_reg_val[hang_detect_regs_count];
1505 unsigned int hang_detected = 1;
1506 unsigned int i;
1507
1508 if (!adreno_dev->fast_hang_detect)
1509 return 0;
1510
1511 for (i = 0; i < hang_detect_regs_count; i++) {
1512 adreno_regread(device, hang_detect_regs[i],
1513 &curr_reg_val[i]);
1514 if (curr_reg_val[i] != prev_reg_val[i]) {
1515 prev_reg_val[i] = curr_reg_val[i];
1516 hang_detected = 0;
1517 }
1518 }
1519
1520 return hang_detected;
1521}
1522
1523
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001524/* MUST be called with the device mutex held */
1525static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001526 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001527 unsigned int timestamp,
1528 unsigned int msecs)
1529{
1530 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001531 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001532 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001533 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001534 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07001535 int retries = 0;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301536 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07001537 unsigned int msecs_part = KGSL_TIMEOUT_PART;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001538 unsigned int ts_issued;
1539 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07001540 unsigned int time_elapsed = 0;
1541 unsigned int prev_reg_val[hang_detect_regs_count];
1542
1543 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001544
1545 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001546
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301547 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07001548 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301549 msecs = adreno_dev->wait_timeout;
1550
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001551 if (timestamp_cmp(timestamp, ts_issued) > 0) {
1552 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
1553 "last issued ts <%d:0x%x>\n",
1554 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001555 status = -EINVAL;
1556 goto done;
1557 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001558
Lynus Vaz06a9a902011-10-04 19:25:33 +05301559 /* Keep the first timeout as 100msecs before rewriting
1560 * the WPTR. Less visible impact if the WPTR has not
1561 * been updated properly.
1562 */
1563 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Tarun Karra3335f142012-06-19 14:11:48 -07001564 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001565 /*
1566 * If the context ID is invalid, we are in a race with
1567 * the context being destroyed by userspace so bail.
1568 */
1569 if (context_id == KGSL_CONTEXT_INVALID) {
1570 KGSL_DRV_WARN(device, "context was detached");
1571 status = -EINVAL;
1572 goto done;
1573 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001574 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001575 /* if the timestamp happens while we're not
1576 * waiting, there's a chance that an interrupt
1577 * will not be generated and thus the timestamp
1578 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301579 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001580 queue_work(device->work_queue, &device->ts_expired_ws);
1581 status = 0;
1582 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001583 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001584 adreno_poke(device);
1585 io_cnt = (io_cnt + 1) % 100;
1586 if (io_cnt <
1587 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1588 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07001589
1590 if ((retries > 0) &&
1591 (adreno_hang_detect(device, prev_reg_val)))
1592 goto hang_dump;
1593
Jeremy Gebben63904832012-02-07 16:10:55 -07001594 mutex_unlock(&device->mutex);
1595 /* We need to make sure that the process is
1596 * placed in wait-q before its condition is called
1597 */
1598 status = kgsl_wait_event_interruptible_timeout(
1599 device->wait_queue,
1600 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001601 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001602 msecs_to_jiffies(retries ?
1603 msecs_part : msecs_first), io);
1604 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001605
Jeremy Gebben63904832012-02-07 16:10:55 -07001606 if (status > 0) {
1607 /*completed before the wait finished */
1608 status = 0;
1609 goto done;
1610 } else if (status < 0) {
1611 /*an error occurred*/
1612 goto done;
1613 }
1614 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07001615
1616 time_elapsed = time_elapsed +
1617 (retries ? msecs_part : msecs_first);
1618 retries++;
1619
1620 } while (time_elapsed < msecs);
1621
1622hang_dump:
Jeremy Gebben63904832012-02-07 16:10:55 -07001623 status = -ETIMEDOUT;
1624 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001625 "Device hang detected while waiting for timestamp: "
1626 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
1627 "wptr: 0x%x\n",
1628 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07001629 adreno_dev->ringbuffer.wptr);
1630 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001631 /* The timestamp that this process wanted
1632 * to wait on may be invalid or expired now
1633 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07001634 status = 0;
1635 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001636done:
1637 return (int)status;
1638}
1639
1640static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001641 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001642{
1643 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001644 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001645
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001646 /*
1647 * If the context ID is invalid, we are in a race with
1648 * the context being destroyed by userspace so bail.
1649 */
1650 if (context_id == KGSL_CONTEXT_INVALID) {
1651 KGSL_DRV_WARN(device, "context was detached");
1652 return timestamp;
1653 }
Jordan Crousec659f382012-04-16 11:10:41 -06001654 switch (type) {
1655 case KGSL_TIMESTAMP_QUEUED: {
1656 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1657 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1658
1659 timestamp = rb->timestamp[context_id];
1660 break;
1661 }
1662 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001663 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06001664 break;
1665 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06001667 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
1668 break;
1669 }
1670
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001671 rmb();
1672
1673 return timestamp;
1674}
1675
1676static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1677 unsigned int cmd, void *data)
1678{
1679 int result = 0;
1680 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1681 struct kgsl_context *context;
1682
1683 switch (cmd) {
1684 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1685 binbase = data;
1686
1687 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1688 if (context) {
1689 adreno_drawctxt_set_bin_base_offset(
1690 dev_priv->device, context, binbase->offset);
1691 } else {
1692 result = -EINVAL;
1693 KGSL_DRV_ERR(dev_priv->device,
1694 "invalid drawctxt drawctxt_id %d "
1695 "device_id=%d\n",
1696 binbase->drawctxt_id, dev_priv->device->id);
1697 }
1698 break;
1699
1700 default:
1701 KGSL_DRV_INFO(dev_priv->device,
1702 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001703 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001704 break;
1705 }
1706 return result;
1707
1708}
1709
1710static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1711{
1712 gpu_freq /= 1000000;
1713 return ticks / gpu_freq;
1714}
1715
1716static void adreno_power_stats(struct kgsl_device *device,
1717 struct kgsl_power_stats *stats)
1718{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001719 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001720 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001721 unsigned int cycles;
1722
1723 /* Get the busy cycles counted since the counter was last reset */
1724 /* Calling this function also resets and restarts the counter */
1725
1726 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001727
1728 /* In order to calculate idle you have to have run the algorithm *
1729 * at least once to get a start time. */
1730 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001731 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001732 stats->total_time = tmp - pwr->time;
1733 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001734 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001735 pwrlevels[device->pwrctrl.active_pwrlevel].
1736 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001737 } else {
1738 stats->total_time = 0;
1739 stats->busy_time = 0;
1740 pwr->time = ktime_to_us(ktime_get());
1741 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001742}
1743
1744void adreno_irqctrl(struct kgsl_device *device, int state)
1745{
Jordan Crousea78c9172011-07-11 13:14:09 -06001746 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1747 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001748}
1749
Jordan Croused6535882012-06-20 08:22:16 -06001750static unsigned int adreno_gpuid(struct kgsl_device *device,
1751 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07001752{
1753 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1754
Jordan Croused6535882012-06-20 08:22:16 -06001755 /* Some applications need to know the chip ID too, so pass
1756 * that as a parameter */
1757
1758 if (chipid != NULL)
1759 *chipid = adreno_dev->chip_id;
1760
Jordan Crousea0758f22011-12-07 11:19:22 -07001761 /* Standard KGSL gpuid format:
1762 * top word is 0x0002 for 2D or 0x0003 for 3D
1763 * Bottom word is core specific identifer
1764 */
1765
1766 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1767}
1768
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769static const struct kgsl_functable adreno_functable = {
1770 /* Mandatory functions */
1771 .regread = adreno_regread,
1772 .regwrite = adreno_regwrite,
1773 .idle = adreno_idle,
1774 .isidle = adreno_isidle,
1775 .suspend_context = adreno_suspend_context,
1776 .start = adreno_start,
1777 .stop = adreno_stop,
1778 .getproperty = adreno_getproperty,
1779 .waittimestamp = adreno_waittimestamp,
1780 .readtimestamp = adreno_readtimestamp,
1781 .issueibcmds = adreno_ringbuffer_issueibcmds,
1782 .ioctl = adreno_ioctl,
1783 .setup_pt = adreno_setup_pt,
1784 .cleanup_pt = adreno_cleanup_pt,
1785 .power_stats = adreno_power_stats,
1786 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001787 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001788 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001789 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790 /* Optional functions */
1791 .setstate = adreno_setstate,
1792 .drawctxt_create = adreno_drawctxt_create,
1793 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06001794 .setproperty = adreno_setproperty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001795};
1796
1797static struct platform_device_id adreno_id_table[] = {
1798 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1799 { },
1800};
1801MODULE_DEVICE_TABLE(platform, adreno_id_table);
1802
1803static struct platform_driver adreno_platform_driver = {
1804 .probe = adreno_probe,
1805 .remove = __devexit_p(adreno_remove),
1806 .suspend = kgsl_suspend_driver,
1807 .resume = kgsl_resume_driver,
1808 .id_table = adreno_id_table,
1809 .driver = {
1810 .owner = THIS_MODULE,
1811 .name = DEVICE_3D_NAME,
1812 .pm = &kgsl_pm_ops,
1813 }
1814};
1815
1816static int __init kgsl_3d_init(void)
1817{
1818 return platform_driver_register(&adreno_platform_driver);
1819}
1820
1821static void __exit kgsl_3d_exit(void)
1822{
1823 platform_driver_unregister(&adreno_platform_driver);
1824}
1825
1826module_init(kgsl_3d_init);
1827module_exit(kgsl_3d_exit);
1828
1829MODULE_DESCRIPTION("3D Graphics driver");
1830MODULE_VERSION("1.2");
1831MODULE_LICENSE("GPL v2");
1832MODULE_ALIAS("platform:kgsl_3d");