blob: dac9f47300eb1bf28efee2b2bcfc37748d0a4401 [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>
Lokesh Batra805e1e12012-08-03 08:34:06 -060018#include <linux/of.h>
19#include <linux/of_device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020
21#include <mach/socinfo.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060022#include <mach/msm_bus_board.h>
23#include <mach/msm_bus.h>
24#include <mach/msm_dcvs.h>
25#include <mach/msm_dcvs_scm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27#include "kgsl.h"
28#include "kgsl_pwrscale.h"
29#include "kgsl_cffdump.h"
30#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060031#include "kgsl_iommu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
33#include "adreno.h"
34#include "adreno_pm4types.h"
35#include "adreno_debugfs.h"
36#include "adreno_postmortem.h"
37
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070038#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070039#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040
41#define DRIVER_VERSION_MAJOR 3
42#define DRIVER_VERSION_MINOR 1
43
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044/* Adreno MH arbiter config*/
45#define ADRENO_CFG_MHARB \
46 (0x10 \
47 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
48 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
50 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
53 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
54 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
55 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
56 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
57 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
58 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
59 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
60 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
61
62#define ADRENO_MMU_CONFIG \
63 (0x01 \
64 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
69 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
70 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
71 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
72 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
73 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
74 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
75
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076static const struct kgsl_functable adreno_functable;
77
78static struct adreno_device device_3d0 = {
79 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070080 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081 .name = DEVICE_3D0_NAME,
82 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060083 .mh = {
84 .mharb = ADRENO_CFG_MHARB,
85 /* Remove 1k boundary check in z470 to avoid a GPU
86 * hang. Notice that this solution won't work if
87 * both EBI and SMI are used
88 */
89 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090 /* turn off memory protection unit by setting
91 acceptable physical address range to include
92 all pages. */
93 .mpu_base = 0x00000000,
94 .mpu_range = 0xFFFFF000,
95 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060096 .mmu = {
97 .config = ADRENO_MMU_CONFIG,
98 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102 .iomemname = KGSL_3D0_REG_MEMORY,
103 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600105 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
107 .suspend = kgsl_early_suspend_driver,
108 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600110#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600112 .gmem_base = 0,
113 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114 .pfp_fw = NULL,
115 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700116 .wait_timeout = 10000, /* in milliseconds */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600117 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118};
119
Tarun Karra3335f142012-06-19 14:11:48 -0700120/* This set of registers are used for Hang detection
121 * If the values of these registers are same after
122 * KGSL_TIMEOUT_PART time, GPU hang is reported in
123 * kernel log.
124 */
125unsigned int hang_detect_regs[] = {
126 A3XX_RBBM_STATUS,
127 REG_CP_RB_RPTR,
128 REG_CP_IB1_BASE,
129 REG_CP_IB1_BUFSZ,
130 REG_CP_IB2_BASE,
131 REG_CP_IB2_BUFSZ,
132};
133
134const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700135
Jordan Crouse505df9c2011-07-28 08:37:59 -0600136/*
137 * This is the master list of all GPU cores that are supported by this
138 * driver.
139 */
140
141#define ANY_ID (~0)
142
143static const struct {
144 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600145 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600146 const char *pm4fw;
147 const char *pfpfw;
148 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700149 unsigned int istore_size;
150 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700151 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530152 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600153} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600154 { ADRENO_REV_A200, 0, 2, ANY_ID, 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 },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530157 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
158 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530159 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600160 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700161 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530162 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600163 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700164 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530165 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600166 /*
167 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
168 * a hardware problem.
169 */
170 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700171 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530172 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700173 { ADRENO_REV_A225, 2, 2, 0, 6,
174 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530175 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600176 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700177 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530178 1536, 768, 3, SZ_512K },
179 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530180 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530181 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
182 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700183 /* A3XX doesn't use the pix_shader_start */
Jordan Croused2b30d22012-05-21 08:41:51 -0600184 { ADRENO_REV_A320, 3, 2, 0, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700185 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530186 512, 0, 2, SZ_512K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700187
Jordan Crouse505df9c2011-07-28 08:37:59 -0600188};
189
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600190static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191{
Jordan Crousea78c9172011-07-11 13:14:09 -0600192 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600193 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
Jordan Crousea78c9172011-07-11 13:14:09 -0600195 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196
197 if (device->requested_state == KGSL_STATE_NONE) {
198 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700199 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 queue_work(device->work_queue, &device->idle_check_ws);
201 } else if (device->pwrscale.policy != NULL) {
202 queue_work(device->work_queue, &device->idle_check_ws);
203 }
204 }
205
206 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800207 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 jiffies + device->pwrctrl.interval_timeout);
209 return result;
210}
211
Jordan Crouse9f739212011-07-28 08:37:57 -0600212static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 struct kgsl_pagetable *pagetable)
214{
215 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
216 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
217
218 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
219
220 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
221
222 kgsl_mmu_unmap(pagetable, &device->memstore);
223
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600224 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225}
226
227static int adreno_setup_pt(struct kgsl_device *device,
228 struct kgsl_pagetable *pagetable)
229{
230 int result = 0;
231 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
232 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
235 GSL_PT_PAGE_RV);
236 if (result)
237 goto error;
238
239 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
240 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
241 if (result)
242 goto unmap_buffer_desc;
243
244 result = kgsl_mmu_map_global(pagetable, &device->memstore,
245 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
246 if (result)
247 goto unmap_memptrs_desc;
248
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600249 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
251 if (result)
252 goto unmap_memstore_desc;
253
254 return result;
255
256unmap_memstore_desc:
257 kgsl_mmu_unmap(pagetable, &device->memstore);
258
259unmap_memptrs_desc:
260 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
261
262unmap_buffer_desc:
263 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
264
265error:
266 return result;
267}
268
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600269static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600270 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600271 uint32_t flags)
272{
273 unsigned int pt_val, reg_pt_val;
274 unsigned int link[200];
275 unsigned int *cmds = &link[0];
276 int sizedwords = 0;
277 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
278 struct kgsl_memdesc **reg_map_desc;
Pu Chened8cbb52012-06-04 18:18:48 -0700279 void *reg_map_array = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600280 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600281 struct kgsl_context *context;
282 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600283
284 if (!adreno_dev->drawctxt_active)
285 return kgsl_mmu_device_setstate(&device->mmu, flags);
286 num_iommu_units = kgsl_mmu_get_reg_map_desc(&device->mmu,
287 &reg_map_array);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600288
289 context = idr_find(&device->context_idr, context_id);
290 adreno_ctx = context->devctxt;
291
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600292 reg_map_desc = reg_map_array;
293
294 if (kgsl_mmu_enable_clk(&device->mmu,
295 KGSL_IOMMU_CONTEXT_USER))
296 goto done;
297
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600298 cmds += __adreno_add_idle_indirect_cmds(cmds,
299 device->mmu.setstate_memory.gpuaddr +
300 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
301
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600302 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600303 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
304 device->mmu.setstate_memory.gpuaddr +
305 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
306 else
307 cmds += adreno_add_bank_change_cmds(cmds,
308 KGSL_IOMMU_CONTEXT_USER,
309 device->mmu.setstate_memory.gpuaddr +
310 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
311
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700312 pt_val = kgsl_mmu_pt_get_base_addr(device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600313 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600314 /*
315 * We need to perfrom the following operations for all
316 * IOMMU units
317 */
318 for (i = 0; i < num_iommu_units; i++) {
319 reg_pt_val = (pt_val &
320 (KGSL_IOMMU_TTBR0_PA_MASK <<
321 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
322 kgsl_mmu_get_pt_lsb(&device->mmu, i,
323 KGSL_IOMMU_CONTEXT_USER);
324 /*
325 * Set address of the new pagetable by writng to IOMMU
326 * TTBR0 register
327 */
328 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
329 *cmds++ = reg_map_desc[i]->gpuaddr +
330 (KGSL_IOMMU_CONTEXT_USER <<
331 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0;
332 *cmds++ = reg_pt_val;
333 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
334 *cmds++ = 0x00000000;
335
336 /*
337 * Read back the ttbr0 register as a barrier to ensure
338 * above writes have completed
339 */
340 cmds += adreno_add_read_cmds(device, cmds,
341 reg_map_desc[i]->gpuaddr +
342 (KGSL_IOMMU_CONTEXT_USER <<
343 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
344 reg_pt_val,
345 device->mmu.setstate_memory.gpuaddr +
346 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600347 }
348 /* invalidate all base pointers */
349 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
350 *cmds++ = 0x7fff;
351
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600352 cmds += __adreno_add_idle_indirect_cmds(cmds,
353 device->mmu.setstate_memory.gpuaddr +
354 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600355 }
356 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
357 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700358 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600359 */
360 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700361 reg_pt_val = (pt_val &
362 (KGSL_IOMMU_TTBR0_PA_MASK <<
363 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
364 kgsl_mmu_get_pt_lsb(&device->mmu, i,
365 KGSL_IOMMU_CONTEXT_USER);
366
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600367 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
368 *cmds++ = (reg_map_desc[i]->gpuaddr +
369 (KGSL_IOMMU_CONTEXT_USER <<
370 KGSL_IOMMU_CTX_SHIFT) +
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700371 KGSL_IOMMU_CTX_TLBIALL);
372 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600373
374 cmds += __adreno_add_idle_indirect_cmds(cmds,
375 device->mmu.setstate_memory.gpuaddr +
376 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
377
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600378 cmds += adreno_add_read_cmds(device, cmds,
379 reg_map_desc[i]->gpuaddr +
380 (KGSL_IOMMU_CONTEXT_USER <<
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700381 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
382 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600383 device->mmu.setstate_memory.gpuaddr +
384 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
385 }
386 }
387
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600388 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600389 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
390 reg_map_desc[num_iommu_units - 1]->gpuaddr - PAGE_SIZE,
391 device->mmu.setstate_memory.gpuaddr +
392 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
393 else
394 cmds += adreno_add_bank_change_cmds(cmds,
395 KGSL_IOMMU_CONTEXT_PRIV,
396 device->mmu.setstate_memory.gpuaddr +
397 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
398
399 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600400 if (sizedwords) {
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600401 /*
402 * add an interrupt at the end of commands so that the smmu
403 * disable clock off function will get called
404 */
405 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
406 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
407 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600408 /* This returns the per context timestamp but we need to
409 * use the global timestamp for iommu clock disablement */
410 adreno_ringbuffer_issuecmds(device, adreno_ctx,
411 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600412 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600413 kgsl_mmu_disable_clk_on_ts(&device->mmu,
414 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600415 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600416done:
417 if (num_iommu_units)
418 kfree(reg_map_array);
419}
420
421static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600422 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600423 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424{
425 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
426 unsigned int link[32];
427 unsigned int *cmds = &link[0];
428 int sizedwords = 0;
429 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600430 struct kgsl_context *context;
431 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600433 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530434 * Fix target freeze issue by adding TLB flush for each submit
435 * on A20X based targets.
436 */
437 if (adreno_is_a20x(adreno_dev))
438 flags |= KGSL_MMUFLAGS_TLBFLUSH;
439 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600440 * If possible, then set the state via the command stream to avoid
441 * a CPU idle. Otherwise, use the default setstate which uses register
442 * writes For CFF dump we must idle and use the registers so that it is
443 * easier to filter out the mmu accesses from the dump
444 */
445 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600446 context = idr_find(&device->context_idr, context_id);
447 adreno_ctx = context->devctxt;
448
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
450 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600451 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 *cmds++ = 0x00000000;
453
454 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600455 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600456 *cmds++ = kgsl_mmu_pt_get_base_addr(
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600457 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 sizedwords += 4;
459 }
460
461 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
462 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600463 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 1);
465 *cmds++ = 0x00000000;
466 sizedwords += 2;
467 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600468 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 *cmds++ = mh_mmu_invalidate;
470 sizedwords += 2;
471 }
472
473 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600474 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 /* HW workaround: to resolve MMU page fault interrupts
476 * caused by the VGT.It prevents the CP PFP from filling
477 * the VGT DMA request fifo too early,thereby ensuring
478 * that the VGT will not fetch vertex/bin data until
479 * after the page table base register has been updated.
480 *
481 * Two null DRAW_INDX_BIN packets are inserted right
482 * after the page table base update, followed by a
483 * wait for idle. The null packets will fill up the
484 * VGT DMA request fifo and prevent any further
485 * vertex/bin updates from occurring until the wait
486 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600487 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488 *cmds++ = (0x4 << 16) |
489 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
490 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600491 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600492 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600493 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494 *cmds++ = 0; /* viz query info */
495 *cmds++ = 0x0003C004; /* draw indicator */
496 *cmds++ = 0; /* bin base */
497 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600498 *cmds++ =
499 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600501 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 *cmds++ = 0; /* viz query info */
503 *cmds++ = 0x0003C004; /* draw indicator */
504 *cmds++ = 0; /* bin base */
505 *cmds++ = 3; /* bin size */
506 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600507 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600509 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 *cmds++ = 0x00000000;
511 sizedwords += 21;
512 }
513
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600514
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600516 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 *cmds++ = 0x7fff; /* invalidate all base pointers */
518 sizedwords += 2;
519 }
520
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600521 adreno_ringbuffer_issuecmds(device, adreno_ctx,
522 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600524 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600525 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600526 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527}
528
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600529static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600530 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600531 uint32_t flags)
532{
533 /* call the mmu specific handler */
534 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600535 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600536 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600537 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600538}
539
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700541a3xx_getchipid(struct kgsl_device *device)
542{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600543 struct kgsl_device_platform_data *pdata =
544 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700545
Jordan Crouse54154c62012-03-27 16:33:26 -0600546 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600547 * All current A3XX chipids are detected at the SOC level. Leave this
548 * function here to support any future GPUs that have working
549 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600550 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700551
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600552 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700553}
554
555static unsigned int
556a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557{
558 unsigned int chipid = 0;
559 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600560 struct kgsl_device_platform_data *pdata =
561 kgsl_device_get_drvdata(device);
562
563 /* If the chip id is set at the platform level, then just use that */
564
565 if (pdata->chipid != 0)
566 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567
568 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
569 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
570 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
571
572 /*
573 * adreno 22x gpus are indicated by coreid 2,
574 * but REG_RBBM_PERIPHID1 always contains 0 for this field
575 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600576 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577 chipid = 2 << 24;
578 else
579 chipid = (coreid & 0xF) << 24;
580
581 chipid |= ((majorid >> 4) & 0xF) << 16;
582
583 minorid = ((revid >> 0) & 0xFF);
584
585 patchid = ((revid >> 16) & 0xFF);
586
587 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530588 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589 if (cpu_is_qsd8x50())
590 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530591 else if (cpu_is_msm8625() && minorid == 0)
592 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593
594 chipid |= (minorid << 8) | patchid;
595
596 return chipid;
597}
598
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700599static unsigned int
600adreno_getchipid(struct kgsl_device *device)
601{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600602 struct kgsl_device_platform_data *pdata =
603 kgsl_device_get_drvdata(device);
604
605 /*
606 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
607 * an A2XX processor
608 */
609
610 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700611 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600612 else
613 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700614}
615
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616static inline bool _rev_match(unsigned int id, unsigned int entry)
617{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600618 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620
621static void
622adreno_identify_gpu(struct adreno_device *adreno_dev)
623{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600624 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625
626 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
627
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600628 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
629 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
630 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
631 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632
Jordan Crouse505df9c2011-07-28 08:37:59 -0600633 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
634 if (core == adreno_gpulist[i].core &&
635 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600636 _rev_match(minor, adreno_gpulist[i].minor) &&
637 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 }
640
Jordan Crouse505df9c2011-07-28 08:37:59 -0600641 if (i == ARRAY_SIZE(adreno_gpulist)) {
642 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
643 return;
644 }
645
646 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
647 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
648 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
649 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700650 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
651 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700652 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600653 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654}
655
Lokesh Batra805e1e12012-08-03 08:34:06 -0600656static struct platform_device_id adreno_id_table[] = {
657 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
658 {},
659};
660
661MODULE_DEVICE_TABLE(platform, adreno_id_table);
662
663static struct of_device_id adreno_match_table[] = {
664 { .compatible = "qcom,kgsl-3d0", },
665 {}
666};
667
668static inline int adreno_of_read_property(struct device_node *node,
669 const char *prop, unsigned int *ptr)
670{
671 int ret = of_property_read_u32(node, prop, ptr);
672 if (ret)
673 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
674 return ret;
675}
676
677static struct device_node *adreno_of_find_subnode(struct device_node *parent,
678 const char *name)
679{
680 struct device_node *child;
681
682 for_each_child_of_node(parent, child) {
683 if (of_device_is_compatible(child, name))
684 return child;
685 }
686
687 return NULL;
688}
689
690static int adreno_of_get_pwrlevels(struct device_node *parent,
691 struct kgsl_device_platform_data *pdata)
692{
693 struct device_node *node, *child;
694 int ret = -EINVAL;
695
696 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
697
698 if (node == NULL) {
699 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
700 return -EINVAL;
701 }
702
703 pdata->num_levels = 0;
704
705 for_each_child_of_node(node, child) {
706 unsigned int index;
707 struct kgsl_pwrlevel *level;
708
709 if (adreno_of_read_property(child, "reg", &index))
710 goto done;
711
712 if (index >= KGSL_MAX_PWRLEVELS) {
713 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
714 index);
715 continue;
716 }
717
718 if (index >= pdata->num_levels)
719 pdata->num_levels = index + 1;
720
721 level = &pdata->pwrlevel[index];
722
723 if (adreno_of_read_property(child, "qcom,gpu-freq",
724 &level->gpu_freq))
725 goto done;
726
727 if (adreno_of_read_property(child, "qcom,bus-freq",
728 &level->bus_freq))
729 goto done;
730
731 if (adreno_of_read_property(child, "qcom,io-fraction",
732 &level->io_fraction))
733 level->io_fraction = 0;
734 }
735
736 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
737 &pdata->init_level))
738 pdata->init_level = 1;
739
740 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
741 KGSL_CORE_ERR("Initial power level out of range\n");
742 pdata->init_level = 1;
743 }
744
745 ret = 0;
746done:
747 return ret;
748
749}
750static void adreno_of_free_bus_scale_info(struct msm_bus_scale_pdata *pdata)
751{
752 int i;
753
754 if (pdata == NULL)
755 return;
756
757 for (i = 0; pdata->usecase && i < pdata->num_usecases; i++)
758 kfree(pdata->usecase[i].vectors);
759
760 kfree(pdata->usecase);
761 kfree(pdata);
762}
763
764struct msm_bus_scale_pdata *adreno_of_get_bus_scale(struct device_node *node)
765{
766 static int bus_vectors_src[3] = {MSM_BUS_MASTER_GRAPHICS_3D,
767 MSM_BUS_MASTER_GRAPHICS_3D_PORT1, MSM_BUS_MASTER_V_OCMEM_GFX3D};
768 static int bus_vectors_dst[2] = {MSM_BUS_SLAVE_EBI_CH0,
769 MSM_BUS_SLAVE_OCMEM};
770 const unsigned int *vectors;
771 struct msm_bus_scale_pdata *pdata;
772 int i, j, len, num_paths;
773 int ret = -EINVAL;
774
775 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
776
777 if (!pdata) {
778 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
779 return ERR_PTR(-ENOMEM);
780 }
781
782 if (adreno_of_read_property(node, "qcom,grp3d-num-bus-scale-usecases",
783 &pdata->num_usecases)) {
784 pdata->num_usecases = 0;
785 goto err;
786 }
787
788 pdata->usecase = kzalloc(pdata->num_usecases *
789 sizeof(struct msm_bus_paths), GFP_KERNEL);
790
791 if (pdata->usecase == NULL) {
792 KGSL_CORE_ERR("kzalloc (%d) failed\n",
793 pdata->num_usecases * sizeof(struct msm_bus_paths));
794 ret = -ENOMEM;
795 goto err;
796 }
797
798 if (adreno_of_read_property(node, "qcom,grp3d-num-vectors-per-usecase",
799 &num_paths))
800 goto err;
801
802 vectors = of_get_property(node, "qcom,grp3d-vectors", &len);
803
804 if (len != pdata->num_usecases * num_paths *
805 sizeof(struct msm_bus_vectors)) {
806 KGSL_CORE_ERR("Invalid size for the bus scale vectors\n");
807 goto err;
808 }
809
810 for (i = 0; i < pdata->num_usecases; i++) {
811 pdata->usecase[i].num_paths = num_paths;
812 pdata->usecase[i].vectors = kzalloc(num_paths *
813 sizeof(struct msm_bus_vectors),
814 GFP_KERNEL);
815 if (!pdata->usecase[i].vectors) {
816 KGSL_CORE_ERR("kzalloc(%d) failed\n",
817 num_paths * sizeof(struct msm_bus_vectors));
818 ret = -ENOMEM;
819 goto err;
820 }
821 for (j = 0; j < num_paths; j++) {
822 int index = (i * num_paths + j) * 4;
823 pdata->usecase[i].vectors[j].src =
824 bus_vectors_src[be32_to_cpu(vectors[index])];
825 pdata->usecase[i].vectors[j].dst =
826 bus_vectors_dst[
827 be32_to_cpu(vectors[index + 1])];
828 pdata->usecase[i].vectors[j].ab =
829 be32_to_cpu(vectors[index + 2]);
830 pdata->usecase[i].vectors[j].ib =
831 KGSL_CONVERT_TO_MBPS(
832 be32_to_cpu(vectors[index + 3]));
833 }
834 }
835
836 pdata->name = "grp3d";
837
838 return pdata;
839
840err:
841 adreno_of_free_bus_scale_info(pdata);
842
843 return ERR_PTR(ret);
844}
845
846static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
847{
848 struct device_node *node, *child;
849 struct msm_dcvs_core_info *info = NULL;
850 int count = 0;
851 int ret = -EINVAL;
852
853 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
854 if (node == NULL)
855 return ERR_PTR(-EINVAL);
856
857 info = kzalloc(sizeof(*info), GFP_KERNEL);
858
859 if (info == NULL) {
860 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
861 ret = -ENOMEM;
862 goto err;
863 }
864
865 for_each_child_of_node(node, child)
866 count++;
867
868 info->core_param.num_freq = count;
869
870 info->freq_tbl = kzalloc(info->core_param.num_freq *
871 sizeof(struct msm_dcvs_freq_entry),
872 GFP_KERNEL);
873
874 if (info->freq_tbl == NULL) {
875 KGSL_CORE_ERR("kzalloc(%d) failed\n",
876 info->core_param.num_freq *
877 sizeof(struct msm_dcvs_freq_entry));
878 ret = -ENOMEM;
879 goto err;
880 }
881
882 for_each_child_of_node(node, child) {
883 unsigned int index;
884
885 if (adreno_of_read_property(child, "reg", &index))
886 goto err;
887
888 if (index >= info->core_param.num_freq) {
889 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
890 index);
891 continue;
892 }
893
894 if (adreno_of_read_property(child, "qcom,freq",
895 &info->freq_tbl[index].freq))
896 goto err;
897
898 if (adreno_of_read_property(child, "qcom,idle-energy",
899 &info->freq_tbl[index].idle_energy))
900 info->freq_tbl[index].idle_energy = 0;
901
902 if (adreno_of_read_property(child, "qcom,active-energy",
903 &info->freq_tbl[index].active_energy))
904 info->freq_tbl[index].active_energy = 0;
905 }
906
907 if (adreno_of_read_property(node, "qcom,core-max-time-us",
908 &info->core_param.max_time_us))
909 goto err;
910
911 if (adreno_of_read_property(node, "qcom,algo-slack-time-us",
912 &info->algo_param.slack_time_us))
913 goto err;
914
915 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
916 &info->algo_param.disable_pc_threshold))
917 goto err;
918
919 if (adreno_of_read_property(node, "qcom,algo-ss-window-size",
920 &info->algo_param.ss_window_size))
921 goto err;
922
923 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
924 &info->algo_param.ss_util_pct))
925 goto err;
926
927 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
928 &info->algo_param.em_max_util_pct))
929 goto err;
930
931 if (adreno_of_read_property(node, "qcom,algo-ss-iobusy-conv",
932 &info->algo_param.ss_iobusy_conv))
933 goto err;
934
935 return info;
936
937err:
938 if (info)
939 kfree(info->freq_tbl);
940
941 kfree(info);
942
943 return ERR_PTR(ret);
944}
945
946static int adreno_of_get_iommu(struct device_node *parent,
947 struct kgsl_device_platform_data *pdata)
948{
949 struct device_node *node, *child;
950 struct kgsl_device_iommu_data *data = NULL;
951 struct kgsl_iommu_ctx *ctxs = NULL;
952 u32 reg_val[2];
953 int ctx_index = 0;
954
955 node = of_parse_phandle(parent, "iommu", 0);
956 if (node == NULL)
957 return -EINVAL;
958
959 data = kzalloc(sizeof(*data), GFP_KERNEL);
960 if (data == NULL) {
961 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
962 goto err;
963 }
964
965 if (of_property_read_u32_array(node, "reg", reg_val, 2))
966 goto err;
967
968 data->physstart = reg_val[0];
969 data->physend = data->physstart + reg_val[1] - 1;
970
971 data->iommu_ctx_count = 0;
972
973 for_each_child_of_node(node, child)
974 data->iommu_ctx_count++;
975
976 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
977 GFP_KERNEL);
978
979 if (ctxs == NULL) {
980 KGSL_CORE_ERR("kzalloc(%d) failed\n",
981 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
982 goto err;
983 }
984
985 for_each_child_of_node(node, child) {
986 int ret = of_property_read_string(child, "label",
987 &ctxs[ctx_index].iommu_ctx_name);
988
989 if (ret) {
990 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
991 goto err;
992 }
993
994 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
995 &ctxs[ctx_index].ctx_id))
996 goto err;
997
998 ctx_index++;
999 }
1000
1001 data->iommu_ctxs = ctxs;
1002
1003 pdata->iommu_data = data;
1004 pdata->iommu_count = 1;
1005
1006 return 0;
1007
1008err:
1009 kfree(ctxs);
1010 kfree(data);
1011
1012 return -EINVAL;
1013}
1014
1015static int adreno_of_get_pdata(struct platform_device *pdev)
1016{
1017 struct kgsl_device_platform_data *pdata = NULL;
1018 struct kgsl_device *device;
1019 int ret = -EINVAL;
1020
1021 pdev->id_entry = adreno_id_table;
1022
1023 pdata = pdev->dev.platform_data;
1024 if (pdata)
1025 return 0;
1026
1027 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1028 KGSL_CORE_ERR("Unable to read 'label'\n");
1029 goto err;
1030 }
1031
1032 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1033 goto err;
1034
1035 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1036 if (pdata == NULL) {
1037 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1038 ret = -ENOMEM;
1039 goto err;
1040 }
1041
1042 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1043 &pdata->chipid))
1044 goto err;
1045
1046 /* pwrlevel Data */
1047 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1048 if (ret)
1049 goto err;
1050
1051 /* Default value is 83, if not found in DT */
1052 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1053 &pdata->idle_timeout))
1054 pdata->idle_timeout = 83;
1055
1056 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1057 &pdata->nap_allowed))
1058 pdata->nap_allowed = 1;
1059
1060 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1061 &pdata->clk_map))
1062 goto err;
1063
1064 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1065
1066 if (device->id != KGSL_DEVICE_3D0)
1067 goto err;
1068
1069 /* Bus Scale Data */
1070
1071 pdata->bus_scale_table = adreno_of_get_bus_scale(pdev->dev.of_node);
1072 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1073 ret = PTR_ERR(pdata->bus_scale_table);
1074 goto err;
1075 }
1076
1077 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1078 if (IS_ERR_OR_NULL(pdata->core_info)) {
1079 ret = PTR_ERR(pdata->core_info);
1080 goto err;
1081 }
1082
1083 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1084 if (ret)
1085 goto err;
1086
1087 pdev->dev.platform_data = pdata;
1088 return 0;
1089
1090err:
1091 if (pdata) {
1092 adreno_of_free_bus_scale_info(pdata->bus_scale_table);
1093 if (pdata->core_info)
1094 kfree(pdata->core_info->freq_tbl);
1095 kfree(pdata->core_info);
1096
1097 if (pdata->iommu_data)
1098 kfree(pdata->iommu_data->iommu_ctxs);
1099
1100 kfree(pdata->iommu_data);
1101 }
1102
1103 kfree(pdata);
1104
1105 return ret;
1106}
1107
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108static int __devinit
1109adreno_probe(struct platform_device *pdev)
1110{
1111 struct kgsl_device *device;
1112 struct adreno_device *adreno_dev;
1113 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001114 bool is_dt;
1115
1116 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1117
1118 if (is_dt && pdev->dev.of_node) {
1119 status = adreno_of_get_pdata(pdev);
1120 if (status)
1121 goto error_return;
1122 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123
1124 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1125 adreno_dev = ADRENO_DEVICE(device);
1126 device->parentdev = &pdev->dev;
1127
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001128 status = adreno_ringbuffer_init(device);
1129 if (status != 0)
1130 goto error;
1131
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001132 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001133 if (status)
1134 goto error_close_rb;
1135
1136 adreno_debugfs_init(device);
1137
1138 kgsl_pwrscale_init(device);
1139 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1140
1141 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1142 return 0;
1143
1144error_close_rb:
1145 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1146error:
1147 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001148error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149 return status;
1150}
1151
1152static int __devexit adreno_remove(struct platform_device *pdev)
1153{
1154 struct kgsl_device *device;
1155 struct adreno_device *adreno_dev;
1156
1157 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1158 adreno_dev = ADRENO_DEVICE(device);
1159
1160 kgsl_pwrscale_detach_policy(device);
1161 kgsl_pwrscale_close(device);
1162
1163 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1164 kgsl_device_platform_remove(device);
1165
1166 return 0;
1167}
1168
1169static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1170{
1171 int status = -EINVAL;
1172 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001174 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1175 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176
1177 /* Power up the device */
1178 kgsl_pwrctrl_enable(device);
1179
1180 /* Identify the specific GPU */
1181 adreno_identify_gpu(adreno_dev);
1182
Jordan Crouse505df9c2011-07-28 08:37:59 -06001183 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1184 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1185 adreno_dev->chip_id);
1186 goto error_clk_off;
1187 }
1188
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001189 /* Set up the MMU */
1190 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001191 /*
1192 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1193 * on older gpus
1194 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001195 if (adreno_is_a20x(adreno_dev)) {
1196 device->mh.mh_intf_cfg1 = 0;
1197 device->mh.mh_intf_cfg2 = 0;
1198 }
1199
1200 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001201 }
1202
Tarun Karra3335f142012-06-19 14:11:48 -07001203 /* Assign correct RBBM status register to hang detect regs
1204 */
1205 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1206
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001207 status = kgsl_mmu_start(device);
1208 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 goto error_clk_off;
1210
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001211 /* Start the GPU */
1212 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213
1214 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001215 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216
1217 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001218 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001219 /* While recovery is on we do not want timer to
1220 * fire and attempt to change any device state */
1221 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1222 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001223 return 0;
1224 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Shubhraprakash Das79447952012-04-26 18:12:23 -06001227 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228error_clk_off:
1229 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230
1231 return status;
1232}
1233
1234static int adreno_stop(struct kgsl_device *device)
1235{
1236 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238 adreno_dev->drawctxt_active = NULL;
1239
1240 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1241
Shubhraprakash Das79447952012-04-26 18:12:23 -06001242 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001243
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001244 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301245 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001246 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001247
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001248 /* Power down the device */
1249 kgsl_pwrctrl_disable(device);
1250
1251 return 0;
1252}
1253
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001254static void adreno_mark_context_status(struct kgsl_device *device,
1255 int recovery_status)
1256{
1257 struct kgsl_context *context;
1258 int next = 0;
1259 /*
1260 * Set the reset status of all contexts to
1261 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1262 * since thats the guilty party, if recovery failed then
1263 * mark all as guilty
1264 */
1265 while ((context = idr_get_next(&device->context_idr, &next))) {
1266 struct adreno_context *adreno_context = context->devctxt;
1267 if (recovery_status) {
1268 context->reset_status =
1269 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1270 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1271 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1272 context->reset_status) {
1273 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1274 CTXT_FLAGS_GPU_HANG_RECOVERED))
1275 context->reset_status =
1276 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1277 else
1278 context->reset_status =
1279 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1280 }
1281 next = next + 1;
1282 }
1283}
1284
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001285static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1286{
1287 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1288 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1289 struct kgsl_context *context;
1290 struct adreno_context *temp_adreno_context;
1291 int next = 0;
1292
1293 while ((context = idr_get_next(&device->context_idr, &next))) {
1294 temp_adreno_context = context->devctxt;
1295 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1296 kgsl_sharedmem_writel(&device->memstore,
1297 KGSL_MEMSTORE_OFFSET(context->id,
1298 soptimestamp),
1299 rb->timestamp[context->id]);
1300 kgsl_sharedmem_writel(&device->memstore,
1301 KGSL_MEMSTORE_OFFSET(context->id,
1302 eoptimestamp),
1303 rb->timestamp[context->id]);
1304 }
1305 next = next + 1;
1306 }
1307}
1308
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001309static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1310{
1311 vfree(rec_data->rb_buffer);
1312 vfree(rec_data->bad_rb_buffer);
1313}
1314
1315static int adreno_setup_recovery_data(struct kgsl_device *device,
1316 struct adreno_recovery_data *rec_data)
1317{
1318 int ret = 0;
1319 unsigned int ib1_sz, ib2_sz;
1320 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1321 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1322
1323 memset(rec_data, 0, sizeof(*rec_data));
1324
1325 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1326 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1327 if (ib1_sz || ib2_sz)
1328 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1329
1330 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1331 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1332 current_context));
1333
1334 kgsl_sharedmem_readl(&device->memstore,
1335 &rec_data->global_eop,
1336 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1337 eoptimestamp));
1338
1339 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1340 if (!rec_data->rb_buffer) {
1341 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1342 rb->buffer_desc.size);
1343 return -ENOMEM;
1344 }
1345
1346 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1347 if (!rec_data->bad_rb_buffer) {
1348 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1349 rb->buffer_desc.size);
1350 ret = -ENOMEM;
1351 goto done;
1352 }
1353
1354done:
1355 if (ret) {
1356 vfree(rec_data->rb_buffer);
1357 vfree(rec_data->bad_rb_buffer);
1358 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001359 return ret;
1360}
1361
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001362static int
1363_adreno_recover_hang(struct kgsl_device *device,
1364 struct adreno_recovery_data *rec_data,
1365 bool try_bad_commands)
1366{
1367 int ret;
1368 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1369 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1370 struct kgsl_context *context;
1371 struct adreno_context *adreno_context = NULL;
1372 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1373
1374 context = idr_find(&device->context_idr, rec_data->context_id);
1375 if (context == NULL) {
1376 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1377 rec_data->context_id);
1378 } else {
1379 adreno_context = context->devctxt;
1380 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1381 }
1382
1383 /* Extract valid contents from rb which can still be executed after
1384 * hang */
1385 ret = adreno_ringbuffer_extract(rb, rec_data);
1386 if (ret)
1387 goto done;
1388
1389 /* restart device */
1390 ret = adreno_stop(device);
1391 if (ret) {
1392 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1393 goto done;
1394 }
1395
1396 ret = adreno_start(device, true);
1397 if (ret) {
1398 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1399 goto done;
1400 }
1401
1402 if (context)
1403 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1404 KGSL_MEMSTORE_GLOBAL);
1405
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001406 /* If iommu is used then we need to make sure that the iommu clocks
1407 * are on since there could be commands in pipeline that touch iommu */
1408 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1409 ret = kgsl_mmu_enable_clk(&device->mmu,
1410 KGSL_IOMMU_CONTEXT_USER);
1411 if (ret)
1412 goto done;
1413 }
1414
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001415 /* Do not try the bad caommands if recovery has failed bad commands
1416 * once already */
1417 if (!try_bad_commands)
1418 rec_data->bad_rb_size = 0;
1419
1420 if (rec_data->bad_rb_size) {
1421 int idle_ret;
1422 /* submit the bad and good context commands and wait for
1423 * them to pass */
1424 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1425 rec_data->bad_rb_size);
1426 idle_ret = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1427 if (idle_ret) {
1428 ret = adreno_stop(device);
1429 if (ret) {
1430 KGSL_DRV_ERR(device,
1431 "Device stop failed in recovery\n");
1432 goto done;
1433 }
1434 ret = adreno_start(device, true);
1435 if (ret) {
1436 KGSL_DRV_ERR(device,
1437 "Device start failed in recovery\n");
1438 goto done;
1439 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001440 if (context)
1441 kgsl_mmu_setstate(&device->mmu,
1442 adreno_context->pagetable,
1443 KGSL_MEMSTORE_GLOBAL);
1444
1445 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1446 ret = kgsl_mmu_enable_clk(&device->mmu,
1447 KGSL_IOMMU_CONTEXT_USER);
1448 if (ret)
1449 goto done;
1450 }
1451
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001452 ret = idle_ret;
1453 KGSL_DRV_ERR(device,
1454 "Bad context commands hung in recovery\n");
1455 } else {
1456 KGSL_DRV_ERR(device,
1457 "Bad context commands succeeded in recovery\n");
1458 if (adreno_context)
1459 adreno_context->flags = (adreno_context->flags &
1460 ~CTXT_FLAGS_GPU_HANG) |
1461 CTXT_FLAGS_GPU_HANG_RECOVERED;
1462 adreno_dev->drawctxt_active = last_active_ctx;
1463 }
1464 }
1465 /* If either the bad command sequence failed or we did not play it */
1466 if (ret || !rec_data->bad_rb_size) {
1467 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1468 rec_data->rb_size);
1469 ret = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1470 if (ret) {
1471 /* If we fail here we can try to invalidate another
1472 * context and try recovering again */
1473 ret = -EAGAIN;
1474 goto done;
1475 }
1476 /* ringbuffer now has data from the last valid context id,
1477 * so restore the active_ctx to the last valid context */
1478 if (rec_data->last_valid_ctx_id) {
1479 struct kgsl_context *last_ctx =
1480 idr_find(&device->context_idr,
1481 rec_data->last_valid_ctx_id);
1482 if (last_ctx)
1483 adreno_dev->drawctxt_active = last_ctx->devctxt;
1484 }
1485 }
1486done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001487 /* Turn off iommu clocks */
1488 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1489 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001490 return ret;
1491}
1492
1493static int
1494adreno_recover_hang(struct kgsl_device *device,
1495 struct adreno_recovery_data *rec_data)
1496{
1497 int ret = 0;
1498 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1499 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1500 unsigned int timestamp;
1501
1502 KGSL_DRV_ERR(device,
1503 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1504 "Bad context_id: %u, global_eop: 0x%x\n",
1505 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1506
1507 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1508 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1509
1510 /* We may need to replay commands multiple times based on whether
1511 * multiple contexts hang the GPU */
1512 while (true) {
1513 if (!ret)
1514 ret = _adreno_recover_hang(device, rec_data, true);
1515 else
1516 ret = _adreno_recover_hang(device, rec_data, false);
1517
1518 if (-EAGAIN == ret) {
1519 /* setup new recovery parameters and retry, this
1520 * means more than 1 contexts are causing hang */
1521 adreno_destroy_recovery_data(rec_data);
1522 adreno_setup_recovery_data(device, rec_data);
1523 KGSL_DRV_ERR(device,
1524 "Retry recovery from 3D GPU hang. Recovery parameters: "
1525 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1526 rec_data->ib1, rec_data->context_id,
1527 rec_data->global_eop);
1528 } else {
1529 break;
1530 }
1531 }
1532
1533 if (ret)
1534 goto done;
1535
1536 /* Restore correct states after recovery */
1537 if (adreno_dev->drawctxt_active)
1538 device->mmu.hwpagetable =
1539 adreno_dev->drawctxt_active->pagetable;
1540 else
1541 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1542 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1543 kgsl_sharedmem_writel(&device->memstore,
1544 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1545 eoptimestamp),
1546 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1547done:
1548 adreno_set_max_ts_for_bad_ctxs(device);
1549 adreno_mark_context_status(device, ret);
1550 if (!ret)
1551 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1552 else
1553 KGSL_DRV_ERR(device, "Recovery failed\n");
1554 return ret;
1555}
1556
1557int
1558adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001559{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001560 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001561 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001562
1563 if (device->state == KGSL_STATE_HUNG)
1564 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001565 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001566 mutex_unlock(&device->mutex);
1567 wait_for_completion(&device->recovery_gate);
1568 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001569 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001570 result = 0;
1571 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001572 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001573 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001574 /* Detected a hang */
1575
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001576 /* Get the recovery data as soon as hang is detected */
1577 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001578 /*
1579 * Trigger an automatic dump of the state to
1580 * the console
1581 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001582 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001583
1584 /*
1585 * Make a GPU snapshot. For now, do it after the PM dump so we
1586 * can at least be sure the PM dump will work as it always has
1587 */
1588 kgsl_device_snapshot(device, 1);
1589
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001590 result = adreno_recover_hang(device, &rec_data);
1591 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001592 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001593 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001594 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001595 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001596 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1597 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001598 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001599 }
1600done:
1601 return result;
1602}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001603EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001604
1605static int adreno_getproperty(struct kgsl_device *device,
1606 enum kgsl_property_type type,
1607 void *value,
1608 unsigned int sizebytes)
1609{
1610 int status = -EINVAL;
1611 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1612
1613 switch (type) {
1614 case KGSL_PROP_DEVICE_INFO:
1615 {
1616 struct kgsl_devinfo devinfo;
1617
1618 if (sizebytes != sizeof(devinfo)) {
1619 status = -EINVAL;
1620 break;
1621 }
1622
1623 memset(&devinfo, 0, sizeof(devinfo));
1624 devinfo.device_id = device->id+1;
1625 devinfo.chip_id = adreno_dev->chip_id;
1626 devinfo.mmu_enabled = kgsl_mmu_enabled();
1627 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001628 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1629 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001630
1631 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1632 0) {
1633 status = -EFAULT;
1634 break;
1635 }
1636 status = 0;
1637 }
1638 break;
1639 case KGSL_PROP_DEVICE_SHADOW:
1640 {
1641 struct kgsl_shadowprop shadowprop;
1642
1643 if (sizebytes != sizeof(shadowprop)) {
1644 status = -EINVAL;
1645 break;
1646 }
1647 memset(&shadowprop, 0, sizeof(shadowprop));
1648 if (device->memstore.hostptr) {
1649 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1650 * anything to mmap().
1651 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001652 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001653 shadowprop.size = device->memstore.size;
1654 /* GSL needs this to be set, even if it
1655 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001656 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1657 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001658 }
1659 if (copy_to_user(value, &shadowprop,
1660 sizeof(shadowprop))) {
1661 status = -EFAULT;
1662 break;
1663 }
1664 status = 0;
1665 }
1666 break;
1667 case KGSL_PROP_MMU_ENABLE:
1668 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001669 int mmu_prop = kgsl_mmu_enabled();
1670
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001671 if (sizebytes != sizeof(int)) {
1672 status = -EINVAL;
1673 break;
1674 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001675 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001676 status = -EFAULT;
1677 break;
1678 }
1679 status = 0;
1680 }
1681 break;
1682 case KGSL_PROP_INTERRUPT_WAITS:
1683 {
1684 int int_waits = 1;
1685 if (sizebytes != sizeof(int)) {
1686 status = -EINVAL;
1687 break;
1688 }
1689 if (copy_to_user(value, &int_waits, sizeof(int))) {
1690 status = -EFAULT;
1691 break;
1692 }
1693 status = 0;
1694 }
1695 break;
1696 default:
1697 status = -EINVAL;
1698 }
1699
1700 return status;
1701}
1702
Jordan Crousef7370f82012-04-18 09:31:07 -06001703static int adreno_setproperty(struct kgsl_device *device,
1704 enum kgsl_property_type type,
1705 void *value,
1706 unsigned int sizebytes)
1707{
1708 int status = -EINVAL;
1709
1710 switch (type) {
1711 case KGSL_PROP_PWRCTRL: {
1712 unsigned int enable;
1713 struct kgsl_device_platform_data *pdata =
1714 kgsl_device_get_drvdata(device);
1715
1716 if (sizebytes != sizeof(enable))
1717 break;
1718
1719 if (copy_from_user(&enable, (void __user *) value,
1720 sizeof(enable))) {
1721 status = -EFAULT;
1722 break;
1723 }
1724
1725 if (enable) {
1726 if (pdata->nap_allowed)
1727 device->pwrctrl.nap_allowed = true;
1728
1729 kgsl_pwrscale_enable(device);
1730 } else {
1731 device->pwrctrl.nap_allowed = false;
1732 kgsl_pwrscale_disable(device);
1733 }
1734
1735 status = 0;
1736 }
1737 break;
1738 default:
1739 break;
1740 }
1741
1742 return status;
1743}
1744
Lynus Vaz06a9a902011-10-04 19:25:33 +05301745static inline void adreno_poke(struct kgsl_device *device)
1746{
1747 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1748 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1749}
1750
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001751/* Caller must hold the device mutex. */
1752int adreno_idle(struct kgsl_device *device, unsigned int timeout)
1753{
1754 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1755 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1756 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301757 unsigned long wait_timeout =
1758 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +05301759 unsigned long wait_time;
1760 unsigned long wait_time_part;
1761 unsigned int msecs;
1762 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07001763 unsigned int msecs_part = KGSL_TIMEOUT_PART;
1764 unsigned int prev_reg_val[hang_detect_regs_count];
1765
1766 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001767
Tarun Karrad526d1e2012-06-11 17:51:09 -07001768 /* Restrict timeout value between adreno_dev->wait_timeout and 0 */
1769 if ((timeout == 0) || (timeout > adreno_dev->wait_timeout))
1770 msecs = adreno_dev->wait_timeout;
1771 else
1772 msecs = timeout;
1773
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001774 kgsl_cffdump_regpoll(device->id,
1775 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776 0x00000000, 0x80000000);
1777 /* first, wait until the CP has consumed all the commands in
1778 * the ring buffer
1779 */
1780retry:
1781 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +05301782 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Lynus Vaz284d1042012-01-31 16:32:31 +05301783 wait_time = jiffies + wait_timeout;
1784 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -07001785 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786 do {
Lynus Vaz284d1042012-01-31 16:32:31 +05301787 if (time_after(jiffies, wait_time_part)) {
1788 adreno_poke(device);
1789 wait_time_part = jiffies +
1790 msecs_to_jiffies(msecs_part);
Tarun Karra3335f142012-06-19 14:11:48 -07001791 if ((adreno_hang_detect(device, prev_reg_val)))
1792 goto err;
Lynus Vaz284d1042012-01-31 16:32:31 +05301793 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001794 GSL_RB_GET_READPTR(rb, &rb->rptr);
1795 if (time_after(jiffies, wait_time)) {
1796 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1797 rb->rptr, rb->wptr);
1798 goto err;
1799 }
1800 } while (rb->rptr != rb->wptr);
1801 }
1802
1803 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301804 wait_time = jiffies + wait_timeout;
Tarun Karra3335f142012-06-19 14:11:48 -07001805 wait_time_part = jiffies + msecs_to_jiffies(msecs_part);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001806 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001807 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1808 &rbbm_status);
1809 if (adreno_is_a2xx(adreno_dev)) {
1810 if (rbbm_status == 0x110)
1811 return 0;
1812 } else {
1813 if (!(rbbm_status & 0x80000000))
1814 return 0;
1815 }
Tarun Karra3335f142012-06-19 14:11:48 -07001816
1817 /* Dont wait for timeout, detect hang faster.
1818 */
1819 if (time_after(jiffies, wait_time_part)) {
1820 wait_time_part = jiffies +
1821 msecs_to_jiffies(msecs_part);
1822 if ((adreno_hang_detect(device, prev_reg_val)))
1823 goto err;
1824 }
1825
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001826 }
1827
1828err:
1829 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001830 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1831 !adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301832 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001833 goto retry;
1834 }
1835 return -ETIMEDOUT;
1836}
1837
1838static unsigned int adreno_isidle(struct kgsl_device *device)
1839{
1840 int status = false;
1841 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1842 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1843 unsigned int rbbm_status;
1844
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001845 WARN_ON(device->state == KGSL_STATE_INIT);
1846 /* If the device isn't active, don't force it on. */
1847 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001848 /* Is the ring buffer is empty? */
1849 GSL_RB_GET_READPTR(rb, &rb->rptr);
1850 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1851 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001852 adreno_regread(device,
1853 adreno_dev->gpudev->reg_rbbm_status,
1854 &rbbm_status);
1855
1856 if (adreno_is_a2xx(adreno_dev)) {
1857 if (rbbm_status == 0x110)
1858 status = true;
1859 } else {
1860 if (!(rbbm_status & 0x80000000))
1861 status = true;
1862 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863 }
1864 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001865 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001866 }
1867 return status;
1868}
1869
1870/* Caller must hold the device mutex. */
1871static int adreno_suspend_context(struct kgsl_device *device)
1872{
1873 int status = 0;
1874 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1875
1876 /* switch to NULL ctxt */
1877 if (adreno_dev->drawctxt_active != NULL) {
1878 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1879 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1880 }
1881
1882 return status;
1883}
1884
Jordan Crouse233b2092012-04-18 09:31:09 -06001885/* Find a memory structure attached to an adreno context */
1886
1887struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1888 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1889{
1890 struct kgsl_context *context;
1891 struct adreno_context *adreno_context = NULL;
1892 int next = 0;
1893
1894 while (1) {
1895 context = idr_get_next(&device->context_idr, &next);
1896 if (context == NULL)
1897 break;
1898
1899 adreno_context = (struct adreno_context *)context->devctxt;
1900
1901 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1902 struct kgsl_memdesc *desc;
1903
1904 desc = &adreno_context->gpustate;
1905 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1906 return desc;
1907
1908 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1909 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1910 return desc;
1911 }
1912 next = next + 1;
1913 }
1914
1915 return NULL;
1916}
1917
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001918struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001919 unsigned int pt_base,
1920 unsigned int gpuaddr,
1921 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001922{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001923 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1925 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1926
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001927 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1928 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001930 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1931 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001932
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001933 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1934 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001935
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001936 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1937 size))
1938 return &device->mmu.setstate_memory;
1939
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001940 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1941
1942 if (entry)
1943 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001944
Jordan Crouse233b2092012-04-18 09:31:09 -06001945 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001946}
1947
1948uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1949 unsigned int gpuaddr, unsigned int size)
1950{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001951 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001952
1953 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1954
1955 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001956}
1957
1958void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1959 unsigned int *value)
1960{
1961 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06001962 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
1963 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001964
1965 if (!in_interrupt())
1966 kgsl_pre_hwaccess(device);
1967
1968 /*ensure this read finishes before the next one.
1969 * i.e. act like normal readl() */
1970 *value = __raw_readl(reg);
1971 rmb();
1972}
1973
1974void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1975 unsigned int value)
1976{
1977 unsigned int *reg;
1978
Jordan Crouse7501d452012-04-19 08:58:44 -06001979 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001980
1981 if (!in_interrupt())
1982 kgsl_pre_hwaccess(device);
1983
1984 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06001985 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001986
1987 /*ensure previous writes post before this one,
1988 * i.e. act like normal writel() */
1989 wmb();
1990 __raw_writel(value, reg);
1991}
1992
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001993static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1994{
1995 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001996 if (k_ctxt != NULL) {
1997 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001998 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
1999 context_id = KGSL_CONTEXT_INVALID;
2000 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2001 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002002 }
2003
2004 return context_id;
2005}
2006
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002007static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002008 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009{
2010 int status;
2011 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002012 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002013 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002014
2015 mutex_lock(&device->mutex);
2016 context_id = _get_context_id(context);
2017 /*
2018 * If the context ID is invalid, we are in a race with
2019 * the context being destroyed by userspace so bail.
2020 */
2021 if (context_id == KGSL_CONTEXT_INVALID) {
2022 KGSL_DRV_WARN(device, "context was detached");
2023 status = -EINVAL;
2024 goto unlock;
2025 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002026
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002027 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002028 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002029 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002030 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002031 mb();
2032
2033 if (enableflag) {
2034 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002035 KGSL_MEMSTORE_OFFSET(context_id,
2036 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002037 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002038 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002039 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002040 KGSL_MEMSTORE_OFFSET(context_id,
2041 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002042 wmb();
2043 }
2044 } else {
2045 unsigned int cmds[2];
2046 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002047 KGSL_MEMSTORE_OFFSET(context_id,
2048 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002049 enableflag = 1;
2050 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002051 KGSL_MEMSTORE_OFFSET(context_id,
2052 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002053 wmb();
2054 /* submit a dummy packet so that even if all
2055 * commands upto timestamp get executed we will still
2056 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002057 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002058 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002059
2060 if (adreno_dev->drawctxt_active)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002061 adreno_ringbuffer_issuecmds_intr(device,
2062 context, &cmds[0], 2);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002063 else
2064 /* We would never call this function if there
2065 * was no active contexts running */
2066 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002067 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002068 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002069unlock:
2070 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002071
2072 return status;
2073}
2074
2075/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002076 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002077 placing a process in wait q. For conditional interrupts we expect the
2078 process to already be in its wait q when its exit condition checking
2079 function is called.
2080*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002081#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002082({ \
2083 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002084 if (io) \
2085 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2086 else \
2087 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088 __ret; \
2089})
2090
Tarun Karra3335f142012-06-19 14:11:48 -07002091
2092
2093unsigned int adreno_hang_detect(struct kgsl_device *device,
2094 unsigned int *prev_reg_val)
2095{
2096 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2097 unsigned int curr_reg_val[hang_detect_regs_count];
2098 unsigned int hang_detected = 1;
2099 unsigned int i;
2100
2101 if (!adreno_dev->fast_hang_detect)
2102 return 0;
2103
2104 for (i = 0; i < hang_detect_regs_count; i++) {
2105 adreno_regread(device, hang_detect_regs[i],
2106 &curr_reg_val[i]);
2107 if (curr_reg_val[i] != prev_reg_val[i]) {
2108 prev_reg_val[i] = curr_reg_val[i];
2109 hang_detected = 0;
2110 }
2111 }
2112
2113 return hang_detected;
2114}
2115
2116
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002117/* MUST be called with the device mutex held */
2118static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002119 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002120 unsigned int timestamp,
2121 unsigned int msecs)
2122{
2123 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06002124 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06002125 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002126 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06002127 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07002128 int retries = 0;
Lynus Vaz06a9a902011-10-04 19:25:33 +05302129 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07002130 unsigned int msecs_part = KGSL_TIMEOUT_PART;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002131 unsigned int ts_issued;
2132 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07002133 unsigned int time_elapsed = 0;
2134 unsigned int prev_reg_val[hang_detect_regs_count];
2135
2136 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002137
2138 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002139
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302140 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07002141 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302142 msecs = adreno_dev->wait_timeout;
2143
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002144 if (timestamp_cmp(timestamp, ts_issued) > 0) {
2145 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
2146 "last issued ts <%d:0x%x>\n",
2147 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002148 status = -EINVAL;
2149 goto done;
2150 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002151
Lynus Vaz06a9a902011-10-04 19:25:33 +05302152 /* Keep the first timeout as 100msecs before rewriting
2153 * the WPTR. Less visible impact if the WPTR has not
2154 * been updated properly.
2155 */
2156 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Tarun Karra3335f142012-06-19 14:11:48 -07002157 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002158 /*
2159 * If the context ID is invalid, we are in a race with
2160 * the context being destroyed by userspace so bail.
2161 */
2162 if (context_id == KGSL_CONTEXT_INVALID) {
2163 KGSL_DRV_WARN(device, "context was detached");
2164 status = -EINVAL;
2165 goto done;
2166 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002167 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07002168 /* if the timestamp happens while we're not
2169 * waiting, there's a chance that an interrupt
2170 * will not be generated and thus the timestamp
2171 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05302172 */
Jeremy Gebben63904832012-02-07 16:10:55 -07002173 queue_work(device->work_queue, &device->ts_expired_ws);
2174 status = 0;
2175 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002176 }
Jeremy Gebben63904832012-02-07 16:10:55 -07002177 adreno_poke(device);
2178 io_cnt = (io_cnt + 1) % 100;
2179 if (io_cnt <
2180 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2181 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002182
2183 if ((retries > 0) &&
2184 (adreno_hang_detect(device, prev_reg_val)))
2185 goto hang_dump;
2186
Jeremy Gebben63904832012-02-07 16:10:55 -07002187 mutex_unlock(&device->mutex);
2188 /* We need to make sure that the process is
2189 * placed in wait-q before its condition is called
2190 */
2191 status = kgsl_wait_event_interruptible_timeout(
2192 device->wait_queue,
2193 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002194 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07002195 msecs_to_jiffies(retries ?
2196 msecs_part : msecs_first), io);
2197 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002198
Jeremy Gebben63904832012-02-07 16:10:55 -07002199 if (status > 0) {
2200 /*completed before the wait finished */
2201 status = 0;
2202 goto done;
2203 } else if (status < 0) {
2204 /*an error occurred*/
2205 goto done;
2206 }
2207 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07002208
2209 time_elapsed = time_elapsed +
2210 (retries ? msecs_part : msecs_first);
2211 retries++;
2212
2213 } while (time_elapsed < msecs);
2214
2215hang_dump:
Shubhraprakash Das54396e52012-03-10 13:24:54 -07002216 /*
2217 * Check if timestamp has retired here because we may have hit
2218 * recovery which can take some time and cause waiting threads
2219 * to timeout
2220 */
2221 if (kgsl_check_timestamp(device, context, timestamp))
2222 goto done;
Jeremy Gebben63904832012-02-07 16:10:55 -07002223 status = -ETIMEDOUT;
2224 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002225 "Device hang detected while waiting for timestamp: "
2226 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2227 "wptr: 0x%x\n",
2228 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002229 adreno_dev->ringbuffer.wptr);
2230 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002231 /* The timestamp that this process wanted
2232 * to wait on may be invalid or expired now
2233 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07002234 status = 0;
2235 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002236done:
2237 return (int)status;
2238}
2239
2240static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002241 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002242{
2243 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002244 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002245
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002246 /*
2247 * If the context ID is invalid, we are in a race with
2248 * the context being destroyed by userspace so bail.
2249 */
2250 if (context_id == KGSL_CONTEXT_INVALID) {
2251 KGSL_DRV_WARN(device, "context was detached");
2252 return timestamp;
2253 }
Jordan Crousec659f382012-04-16 11:10:41 -06002254 switch (type) {
2255 case KGSL_TIMESTAMP_QUEUED: {
2256 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2257 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2258
2259 timestamp = rb->timestamp[context_id];
2260 break;
2261 }
2262 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002263 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002264 break;
2265 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002266 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002267 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2268 break;
2269 }
2270
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002271 rmb();
2272
2273 return timestamp;
2274}
2275
2276static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2277 unsigned int cmd, void *data)
2278{
2279 int result = 0;
2280 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2281 struct kgsl_context *context;
2282
2283 switch (cmd) {
2284 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2285 binbase = data;
2286
2287 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2288 if (context) {
2289 adreno_drawctxt_set_bin_base_offset(
2290 dev_priv->device, context, binbase->offset);
2291 } else {
2292 result = -EINVAL;
2293 KGSL_DRV_ERR(dev_priv->device,
2294 "invalid drawctxt drawctxt_id %d "
2295 "device_id=%d\n",
2296 binbase->drawctxt_id, dev_priv->device->id);
2297 }
2298 break;
2299
2300 default:
2301 KGSL_DRV_INFO(dev_priv->device,
2302 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002303 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002304 break;
2305 }
2306 return result;
2307
2308}
2309
2310static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2311{
2312 gpu_freq /= 1000000;
2313 return ticks / gpu_freq;
2314}
2315
2316static void adreno_power_stats(struct kgsl_device *device,
2317 struct kgsl_power_stats *stats)
2318{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002319 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002320 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002321 unsigned int cycles;
2322
2323 /* Get the busy cycles counted since the counter was last reset */
2324 /* Calling this function also resets and restarts the counter */
2325
2326 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002327
2328 /* In order to calculate idle you have to have run the algorithm *
2329 * at least once to get a start time. */
2330 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002331 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002332 stats->total_time = tmp - pwr->time;
2333 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002334 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002335 pwrlevels[device->pwrctrl.active_pwrlevel].
2336 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002337 } else {
2338 stats->total_time = 0;
2339 stats->busy_time = 0;
2340 pwr->time = ktime_to_us(ktime_get());
2341 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002342}
2343
2344void adreno_irqctrl(struct kgsl_device *device, int state)
2345{
Jordan Crousea78c9172011-07-11 13:14:09 -06002346 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2347 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002348}
2349
Jordan Croused6535882012-06-20 08:22:16 -06002350static unsigned int adreno_gpuid(struct kgsl_device *device,
2351 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002352{
2353 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2354
Jordan Croused6535882012-06-20 08:22:16 -06002355 /* Some applications need to know the chip ID too, so pass
2356 * that as a parameter */
2357
2358 if (chipid != NULL)
2359 *chipid = adreno_dev->chip_id;
2360
Jordan Crousea0758f22011-12-07 11:19:22 -07002361 /* Standard KGSL gpuid format:
2362 * top word is 0x0002 for 2D or 0x0003 for 3D
2363 * Bottom word is core specific identifer
2364 */
2365
2366 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2367}
2368
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002369static const struct kgsl_functable adreno_functable = {
2370 /* Mandatory functions */
2371 .regread = adreno_regread,
2372 .regwrite = adreno_regwrite,
2373 .idle = adreno_idle,
2374 .isidle = adreno_isidle,
2375 .suspend_context = adreno_suspend_context,
2376 .start = adreno_start,
2377 .stop = adreno_stop,
2378 .getproperty = adreno_getproperty,
2379 .waittimestamp = adreno_waittimestamp,
2380 .readtimestamp = adreno_readtimestamp,
2381 .issueibcmds = adreno_ringbuffer_issueibcmds,
2382 .ioctl = adreno_ioctl,
2383 .setup_pt = adreno_setup_pt,
2384 .cleanup_pt = adreno_cleanup_pt,
2385 .power_stats = adreno_power_stats,
2386 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002387 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002388 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002389 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002390 /* Optional functions */
2391 .setstate = adreno_setstate,
2392 .drawctxt_create = adreno_drawctxt_create,
2393 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002394 .setproperty = adreno_setproperty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002395};
2396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002397static struct platform_driver adreno_platform_driver = {
2398 .probe = adreno_probe,
2399 .remove = __devexit_p(adreno_remove),
2400 .suspend = kgsl_suspend_driver,
2401 .resume = kgsl_resume_driver,
2402 .id_table = adreno_id_table,
2403 .driver = {
2404 .owner = THIS_MODULE,
2405 .name = DEVICE_3D_NAME,
2406 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002407 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002408 }
2409};
2410
2411static int __init kgsl_3d_init(void)
2412{
2413 return platform_driver_register(&adreno_platform_driver);
2414}
2415
2416static void __exit kgsl_3d_exit(void)
2417{
2418 platform_driver_unregister(&adreno_platform_driver);
2419}
2420
2421module_init(kgsl_3d_init);
2422module_exit(kgsl_3d_exit);
2423
2424MODULE_DESCRIPTION("3D Graphics driver");
2425MODULE_VERSION("1.2");
2426MODULE_LICENSE("GPL v2");
2427MODULE_ALIAS("platform:kgsl_3d");