blob: f7d1e59c9d9ebdb5a1637f5217a50f4258bfb9eb [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 },
liu zhongfd42e622012-05-01 19:18:30 -0700187 { ADRENO_REV_A330, 3, 3, 0, 0,
188 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
189 512, 0, 2, SZ_1M },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600190};
191
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600192static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193{
Jordan Crousea78c9172011-07-11 13:14:09 -0600194 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600195 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196
Jordan Crousea78c9172011-07-11 13:14:09 -0600197 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198
199 if (device->requested_state == KGSL_STATE_NONE) {
200 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700201 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 queue_work(device->work_queue, &device->idle_check_ws);
203 } else if (device->pwrscale.policy != NULL) {
204 queue_work(device->work_queue, &device->idle_check_ws);
205 }
206 }
207
208 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800209 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 jiffies + device->pwrctrl.interval_timeout);
211 return result;
212}
213
Jordan Crouse9f739212011-07-28 08:37:57 -0600214static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 struct kgsl_pagetable *pagetable)
216{
217 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
218 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
219
220 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
221
222 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
223
224 kgsl_mmu_unmap(pagetable, &device->memstore);
225
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600226 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227}
228
229static int adreno_setup_pt(struct kgsl_device *device,
230 struct kgsl_pagetable *pagetable)
231{
232 int result = 0;
233 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
234 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
235
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
237 GSL_PT_PAGE_RV);
238 if (result)
239 goto error;
240
241 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
242 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
243 if (result)
244 goto unmap_buffer_desc;
245
246 result = kgsl_mmu_map_global(pagetable, &device->memstore,
247 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
248 if (result)
249 goto unmap_memptrs_desc;
250
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600251 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
253 if (result)
254 goto unmap_memstore_desc;
255
256 return result;
257
258unmap_memstore_desc:
259 kgsl_mmu_unmap(pagetable, &device->memstore);
260
261unmap_memptrs_desc:
262 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
263
264unmap_buffer_desc:
265 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
266
267error:
268 return result;
269}
270
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600271static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600272 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600273 uint32_t flags)
274{
275 unsigned int pt_val, reg_pt_val;
276 unsigned int link[200];
277 unsigned int *cmds = &link[0];
278 int sizedwords = 0;
279 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
280 struct kgsl_memdesc **reg_map_desc;
Pu Chened8cbb52012-06-04 18:18:48 -0700281 void *reg_map_array = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600282 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600283 struct kgsl_context *context;
284 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600285
286 if (!adreno_dev->drawctxt_active)
287 return kgsl_mmu_device_setstate(&device->mmu, flags);
288 num_iommu_units = kgsl_mmu_get_reg_map_desc(&device->mmu,
289 &reg_map_array);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600290
291 context = idr_find(&device->context_idr, context_id);
292 adreno_ctx = context->devctxt;
293
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600294 reg_map_desc = reg_map_array;
295
296 if (kgsl_mmu_enable_clk(&device->mmu,
297 KGSL_IOMMU_CONTEXT_USER))
298 goto done;
299
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600300 cmds += __adreno_add_idle_indirect_cmds(cmds,
301 device->mmu.setstate_memory.gpuaddr +
302 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
303
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600304 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600305 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
306 device->mmu.setstate_memory.gpuaddr +
307 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
308 else
309 cmds += adreno_add_bank_change_cmds(cmds,
310 KGSL_IOMMU_CONTEXT_USER,
311 device->mmu.setstate_memory.gpuaddr +
312 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
313
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700314 pt_val = kgsl_mmu_pt_get_base_addr(device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600315 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600316 /*
317 * We need to perfrom the following operations for all
318 * IOMMU units
319 */
320 for (i = 0; i < num_iommu_units; i++) {
321 reg_pt_val = (pt_val &
322 (KGSL_IOMMU_TTBR0_PA_MASK <<
323 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
324 kgsl_mmu_get_pt_lsb(&device->mmu, i,
325 KGSL_IOMMU_CONTEXT_USER);
326 /*
327 * Set address of the new pagetable by writng to IOMMU
328 * TTBR0 register
329 */
330 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
331 *cmds++ = reg_map_desc[i]->gpuaddr +
332 (KGSL_IOMMU_CONTEXT_USER <<
333 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0;
334 *cmds++ = reg_pt_val;
335 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
336 *cmds++ = 0x00000000;
337
338 /*
339 * Read back the ttbr0 register as a barrier to ensure
340 * above writes have completed
341 */
342 cmds += adreno_add_read_cmds(device, cmds,
343 reg_map_desc[i]->gpuaddr +
344 (KGSL_IOMMU_CONTEXT_USER <<
345 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
346 reg_pt_val,
347 device->mmu.setstate_memory.gpuaddr +
348 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600349 }
350 /* invalidate all base pointers */
351 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
352 *cmds++ = 0x7fff;
353
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600354 cmds += __adreno_add_idle_indirect_cmds(cmds,
355 device->mmu.setstate_memory.gpuaddr +
356 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600357 }
358 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
359 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700360 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600361 */
362 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700363 reg_pt_val = (pt_val &
364 (KGSL_IOMMU_TTBR0_PA_MASK <<
365 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
366 kgsl_mmu_get_pt_lsb(&device->mmu, i,
367 KGSL_IOMMU_CONTEXT_USER);
368
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600369 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
370 *cmds++ = (reg_map_desc[i]->gpuaddr +
371 (KGSL_IOMMU_CONTEXT_USER <<
372 KGSL_IOMMU_CTX_SHIFT) +
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700373 KGSL_IOMMU_CTX_TLBIALL);
374 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600375
376 cmds += __adreno_add_idle_indirect_cmds(cmds,
377 device->mmu.setstate_memory.gpuaddr +
378 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
379
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600380 cmds += adreno_add_read_cmds(device, cmds,
381 reg_map_desc[i]->gpuaddr +
382 (KGSL_IOMMU_CONTEXT_USER <<
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700383 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
384 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600385 device->mmu.setstate_memory.gpuaddr +
386 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
387 }
388 }
389
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600390 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600391 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
392 reg_map_desc[num_iommu_units - 1]->gpuaddr - PAGE_SIZE,
393 device->mmu.setstate_memory.gpuaddr +
394 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
395 else
396 cmds += adreno_add_bank_change_cmds(cmds,
397 KGSL_IOMMU_CONTEXT_PRIV,
398 device->mmu.setstate_memory.gpuaddr +
399 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
400
401 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600402 if (sizedwords) {
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600403 /*
404 * add an interrupt at the end of commands so that the smmu
405 * disable clock off function will get called
406 */
407 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
408 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
409 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600410 /* This returns the per context timestamp but we need to
411 * use the global timestamp for iommu clock disablement */
412 adreno_ringbuffer_issuecmds(device, adreno_ctx,
413 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600414 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600415 kgsl_mmu_disable_clk_on_ts(&device->mmu,
416 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600417 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600418done:
419 if (num_iommu_units)
420 kfree(reg_map_array);
421}
422
423static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600424 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600425 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426{
427 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
428 unsigned int link[32];
429 unsigned int *cmds = &link[0];
430 int sizedwords = 0;
431 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600432 struct kgsl_context *context;
433 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600435 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530436 * Fix target freeze issue by adding TLB flush for each submit
437 * on A20X based targets.
438 */
439 if (adreno_is_a20x(adreno_dev))
440 flags |= KGSL_MMUFLAGS_TLBFLUSH;
441 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600442 * If possible, then set the state via the command stream to avoid
443 * a CPU idle. Otherwise, use the default setstate which uses register
444 * writes For CFF dump we must idle and use the registers so that it is
445 * easier to filter out the mmu accesses from the dump
446 */
447 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600448 context = idr_find(&device->context_idr, context_id);
449 adreno_ctx = context->devctxt;
450
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
452 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600453 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 *cmds++ = 0x00000000;
455
456 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600457 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600458 *cmds++ = kgsl_mmu_pt_get_base_addr(
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600459 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460 sizedwords += 4;
461 }
462
463 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
464 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600465 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466 1);
467 *cmds++ = 0x00000000;
468 sizedwords += 2;
469 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600470 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 *cmds++ = mh_mmu_invalidate;
472 sizedwords += 2;
473 }
474
475 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600476 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477 /* HW workaround: to resolve MMU page fault interrupts
478 * caused by the VGT.It prevents the CP PFP from filling
479 * the VGT DMA request fifo too early,thereby ensuring
480 * that the VGT will not fetch vertex/bin data until
481 * after the page table base register has been updated.
482 *
483 * Two null DRAW_INDX_BIN packets are inserted right
484 * after the page table base update, followed by a
485 * wait for idle. The null packets will fill up the
486 * VGT DMA request fifo and prevent any further
487 * vertex/bin updates from occurring until the wait
488 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600489 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 *cmds++ = (0x4 << 16) |
491 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
492 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600493 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600494 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600495 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496 *cmds++ = 0; /* viz query info */
497 *cmds++ = 0x0003C004; /* draw indicator */
498 *cmds++ = 0; /* bin base */
499 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600500 *cmds++ =
501 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600503 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 *cmds++ = 0; /* viz query info */
505 *cmds++ = 0x0003C004; /* draw indicator */
506 *cmds++ = 0; /* bin base */
507 *cmds++ = 3; /* bin size */
508 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600509 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600511 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512 *cmds++ = 0x00000000;
513 sizedwords += 21;
514 }
515
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600516
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600518 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 *cmds++ = 0x7fff; /* invalidate all base pointers */
520 sizedwords += 2;
521 }
522
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600523 adreno_ringbuffer_issuecmds(device, adreno_ctx,
524 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600526 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600527 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600528 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529}
530
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600531static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600532 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600533 uint32_t flags)
534{
535 /* call the mmu specific handler */
536 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600537 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600538 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600539 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600540}
541
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700543a3xx_getchipid(struct kgsl_device *device)
544{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600545 struct kgsl_device_platform_data *pdata =
546 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700547
Jordan Crouse54154c62012-03-27 16:33:26 -0600548 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600549 * All current A3XX chipids are detected at the SOC level. Leave this
550 * function here to support any future GPUs that have working
551 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600552 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700553
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600554 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700555}
556
557static unsigned int
558a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559{
560 unsigned int chipid = 0;
561 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600562 struct kgsl_device_platform_data *pdata =
563 kgsl_device_get_drvdata(device);
564
565 /* If the chip id is set at the platform level, then just use that */
566
567 if (pdata->chipid != 0)
568 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569
570 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
571 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
572 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
573
574 /*
575 * adreno 22x gpus are indicated by coreid 2,
576 * but REG_RBBM_PERIPHID1 always contains 0 for this field
577 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600578 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 chipid = 2 << 24;
580 else
581 chipid = (coreid & 0xF) << 24;
582
583 chipid |= ((majorid >> 4) & 0xF) << 16;
584
585 minorid = ((revid >> 0) & 0xFF);
586
587 patchid = ((revid >> 16) & 0xFF);
588
589 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530590 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 if (cpu_is_qsd8x50())
592 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530593 else if (cpu_is_msm8625() && minorid == 0)
594 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595
596 chipid |= (minorid << 8) | patchid;
597
598 return chipid;
599}
600
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700601static unsigned int
602adreno_getchipid(struct kgsl_device *device)
603{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600604 struct kgsl_device_platform_data *pdata =
605 kgsl_device_get_drvdata(device);
606
607 /*
608 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
609 * an A2XX processor
610 */
611
612 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700613 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600614 else
615 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700616}
617
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618static inline bool _rev_match(unsigned int id, unsigned int entry)
619{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600620 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622
623static void
624adreno_identify_gpu(struct adreno_device *adreno_dev)
625{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600626 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627
628 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
629
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600630 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
631 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
632 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
633 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634
Jordan Crouse505df9c2011-07-28 08:37:59 -0600635 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
636 if (core == adreno_gpulist[i].core &&
637 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600638 _rev_match(minor, adreno_gpulist[i].minor) &&
639 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641 }
642
Jordan Crouse505df9c2011-07-28 08:37:59 -0600643 if (i == ARRAY_SIZE(adreno_gpulist)) {
644 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
645 return;
646 }
647
648 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
649 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
650 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
651 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700652 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
653 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700654 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600655 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656}
657
Lokesh Batra805e1e12012-08-03 08:34:06 -0600658static struct platform_device_id adreno_id_table[] = {
659 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
660 {},
661};
662
663MODULE_DEVICE_TABLE(platform, adreno_id_table);
664
665static struct of_device_id adreno_match_table[] = {
666 { .compatible = "qcom,kgsl-3d0", },
667 {}
668};
669
670static inline int adreno_of_read_property(struct device_node *node,
671 const char *prop, unsigned int *ptr)
672{
673 int ret = of_property_read_u32(node, prop, ptr);
674 if (ret)
675 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
676 return ret;
677}
678
679static struct device_node *adreno_of_find_subnode(struct device_node *parent,
680 const char *name)
681{
682 struct device_node *child;
683
684 for_each_child_of_node(parent, child) {
685 if (of_device_is_compatible(child, name))
686 return child;
687 }
688
689 return NULL;
690}
691
692static int adreno_of_get_pwrlevels(struct device_node *parent,
693 struct kgsl_device_platform_data *pdata)
694{
695 struct device_node *node, *child;
696 int ret = -EINVAL;
697
698 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
699
700 if (node == NULL) {
701 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
702 return -EINVAL;
703 }
704
705 pdata->num_levels = 0;
706
707 for_each_child_of_node(node, child) {
708 unsigned int index;
709 struct kgsl_pwrlevel *level;
710
711 if (adreno_of_read_property(child, "reg", &index))
712 goto done;
713
714 if (index >= KGSL_MAX_PWRLEVELS) {
715 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
716 index);
717 continue;
718 }
719
720 if (index >= pdata->num_levels)
721 pdata->num_levels = index + 1;
722
723 level = &pdata->pwrlevel[index];
724
725 if (adreno_of_read_property(child, "qcom,gpu-freq",
726 &level->gpu_freq))
727 goto done;
728
729 if (adreno_of_read_property(child, "qcom,bus-freq",
730 &level->bus_freq))
731 goto done;
732
733 if (adreno_of_read_property(child, "qcom,io-fraction",
734 &level->io_fraction))
735 level->io_fraction = 0;
736 }
737
738 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
739 &pdata->init_level))
740 pdata->init_level = 1;
741
742 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
743 KGSL_CORE_ERR("Initial power level out of range\n");
744 pdata->init_level = 1;
745 }
746
747 ret = 0;
748done:
749 return ret;
750
751}
752static void adreno_of_free_bus_scale_info(struct msm_bus_scale_pdata *pdata)
753{
754 int i;
755
756 if (pdata == NULL)
757 return;
758
759 for (i = 0; pdata->usecase && i < pdata->num_usecases; i++)
760 kfree(pdata->usecase[i].vectors);
761
762 kfree(pdata->usecase);
763 kfree(pdata);
764}
765
766struct msm_bus_scale_pdata *adreno_of_get_bus_scale(struct device_node *node)
767{
768 static int bus_vectors_src[3] = {MSM_BUS_MASTER_GRAPHICS_3D,
769 MSM_BUS_MASTER_GRAPHICS_3D_PORT1, MSM_BUS_MASTER_V_OCMEM_GFX3D};
770 static int bus_vectors_dst[2] = {MSM_BUS_SLAVE_EBI_CH0,
771 MSM_BUS_SLAVE_OCMEM};
772 const unsigned int *vectors;
773 struct msm_bus_scale_pdata *pdata;
774 int i, j, len, num_paths;
775 int ret = -EINVAL;
776
777 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
778
779 if (!pdata) {
780 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
781 return ERR_PTR(-ENOMEM);
782 }
783
784 if (adreno_of_read_property(node, "qcom,grp3d-num-bus-scale-usecases",
785 &pdata->num_usecases)) {
786 pdata->num_usecases = 0;
787 goto err;
788 }
789
790 pdata->usecase = kzalloc(pdata->num_usecases *
791 sizeof(struct msm_bus_paths), GFP_KERNEL);
792
793 if (pdata->usecase == NULL) {
794 KGSL_CORE_ERR("kzalloc (%d) failed\n",
795 pdata->num_usecases * sizeof(struct msm_bus_paths));
796 ret = -ENOMEM;
797 goto err;
798 }
799
800 if (adreno_of_read_property(node, "qcom,grp3d-num-vectors-per-usecase",
801 &num_paths))
802 goto err;
803
804 vectors = of_get_property(node, "qcom,grp3d-vectors", &len);
805
806 if (len != pdata->num_usecases * num_paths *
807 sizeof(struct msm_bus_vectors)) {
808 KGSL_CORE_ERR("Invalid size for the bus scale vectors\n");
809 goto err;
810 }
811
812 for (i = 0; i < pdata->num_usecases; i++) {
813 pdata->usecase[i].num_paths = num_paths;
814 pdata->usecase[i].vectors = kzalloc(num_paths *
815 sizeof(struct msm_bus_vectors),
816 GFP_KERNEL);
817 if (!pdata->usecase[i].vectors) {
818 KGSL_CORE_ERR("kzalloc(%d) failed\n",
819 num_paths * sizeof(struct msm_bus_vectors));
820 ret = -ENOMEM;
821 goto err;
822 }
823 for (j = 0; j < num_paths; j++) {
824 int index = (i * num_paths + j) * 4;
825 pdata->usecase[i].vectors[j].src =
826 bus_vectors_src[be32_to_cpu(vectors[index])];
827 pdata->usecase[i].vectors[j].dst =
828 bus_vectors_dst[
829 be32_to_cpu(vectors[index + 1])];
830 pdata->usecase[i].vectors[j].ab =
831 be32_to_cpu(vectors[index + 2]);
832 pdata->usecase[i].vectors[j].ib =
833 KGSL_CONVERT_TO_MBPS(
834 be32_to_cpu(vectors[index + 3]));
835 }
836 }
837
838 pdata->name = "grp3d";
839
840 return pdata;
841
842err:
843 adreno_of_free_bus_scale_info(pdata);
844
845 return ERR_PTR(ret);
846}
847
848static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
849{
850 struct device_node *node, *child;
851 struct msm_dcvs_core_info *info = NULL;
852 int count = 0;
853 int ret = -EINVAL;
854
855 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
856 if (node == NULL)
857 return ERR_PTR(-EINVAL);
858
859 info = kzalloc(sizeof(*info), GFP_KERNEL);
860
861 if (info == NULL) {
862 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
863 ret = -ENOMEM;
864 goto err;
865 }
866
867 for_each_child_of_node(node, child)
868 count++;
869
870 info->core_param.num_freq = count;
871
872 info->freq_tbl = kzalloc(info->core_param.num_freq *
873 sizeof(struct msm_dcvs_freq_entry),
874 GFP_KERNEL);
875
876 if (info->freq_tbl == NULL) {
877 KGSL_CORE_ERR("kzalloc(%d) failed\n",
878 info->core_param.num_freq *
879 sizeof(struct msm_dcvs_freq_entry));
880 ret = -ENOMEM;
881 goto err;
882 }
883
884 for_each_child_of_node(node, child) {
885 unsigned int index;
886
887 if (adreno_of_read_property(child, "reg", &index))
888 goto err;
889
890 if (index >= info->core_param.num_freq) {
891 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
892 index);
893 continue;
894 }
895
896 if (adreno_of_read_property(child, "qcom,freq",
897 &info->freq_tbl[index].freq))
898 goto err;
899
900 if (adreno_of_read_property(child, "qcom,idle-energy",
901 &info->freq_tbl[index].idle_energy))
902 info->freq_tbl[index].idle_energy = 0;
903
904 if (adreno_of_read_property(child, "qcom,active-energy",
905 &info->freq_tbl[index].active_energy))
906 info->freq_tbl[index].active_energy = 0;
907 }
908
909 if (adreno_of_read_property(node, "qcom,core-max-time-us",
910 &info->core_param.max_time_us))
911 goto err;
912
913 if (adreno_of_read_property(node, "qcom,algo-slack-time-us",
914 &info->algo_param.slack_time_us))
915 goto err;
916
917 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
918 &info->algo_param.disable_pc_threshold))
919 goto err;
920
921 if (adreno_of_read_property(node, "qcom,algo-ss-window-size",
922 &info->algo_param.ss_window_size))
923 goto err;
924
925 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
926 &info->algo_param.ss_util_pct))
927 goto err;
928
929 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
930 &info->algo_param.em_max_util_pct))
931 goto err;
932
933 if (adreno_of_read_property(node, "qcom,algo-ss-iobusy-conv",
934 &info->algo_param.ss_iobusy_conv))
935 goto err;
936
937 return info;
938
939err:
940 if (info)
941 kfree(info->freq_tbl);
942
943 kfree(info);
944
945 return ERR_PTR(ret);
946}
947
948static int adreno_of_get_iommu(struct device_node *parent,
949 struct kgsl_device_platform_data *pdata)
950{
951 struct device_node *node, *child;
952 struct kgsl_device_iommu_data *data = NULL;
953 struct kgsl_iommu_ctx *ctxs = NULL;
954 u32 reg_val[2];
955 int ctx_index = 0;
956
957 node = of_parse_phandle(parent, "iommu", 0);
958 if (node == NULL)
959 return -EINVAL;
960
961 data = kzalloc(sizeof(*data), GFP_KERNEL);
962 if (data == NULL) {
963 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
964 goto err;
965 }
966
967 if (of_property_read_u32_array(node, "reg", reg_val, 2))
968 goto err;
969
970 data->physstart = reg_val[0];
971 data->physend = data->physstart + reg_val[1] - 1;
972
973 data->iommu_ctx_count = 0;
974
975 for_each_child_of_node(node, child)
976 data->iommu_ctx_count++;
977
978 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
979 GFP_KERNEL);
980
981 if (ctxs == NULL) {
982 KGSL_CORE_ERR("kzalloc(%d) failed\n",
983 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
984 goto err;
985 }
986
987 for_each_child_of_node(node, child) {
988 int ret = of_property_read_string(child, "label",
989 &ctxs[ctx_index].iommu_ctx_name);
990
991 if (ret) {
992 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
993 goto err;
994 }
995
996 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
997 &ctxs[ctx_index].ctx_id))
998 goto err;
999
1000 ctx_index++;
1001 }
1002
1003 data->iommu_ctxs = ctxs;
1004
1005 pdata->iommu_data = data;
1006 pdata->iommu_count = 1;
1007
1008 return 0;
1009
1010err:
1011 kfree(ctxs);
1012 kfree(data);
1013
1014 return -EINVAL;
1015}
1016
1017static int adreno_of_get_pdata(struct platform_device *pdev)
1018{
1019 struct kgsl_device_platform_data *pdata = NULL;
1020 struct kgsl_device *device;
1021 int ret = -EINVAL;
1022
1023 pdev->id_entry = adreno_id_table;
1024
1025 pdata = pdev->dev.platform_data;
1026 if (pdata)
1027 return 0;
1028
1029 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1030 KGSL_CORE_ERR("Unable to read 'label'\n");
1031 goto err;
1032 }
1033
1034 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1035 goto err;
1036
1037 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1038 if (pdata == NULL) {
1039 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1040 ret = -ENOMEM;
1041 goto err;
1042 }
1043
1044 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1045 &pdata->chipid))
1046 goto err;
1047
1048 /* pwrlevel Data */
1049 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1050 if (ret)
1051 goto err;
1052
1053 /* Default value is 83, if not found in DT */
1054 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1055 &pdata->idle_timeout))
1056 pdata->idle_timeout = 83;
1057
1058 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1059 &pdata->nap_allowed))
1060 pdata->nap_allowed = 1;
1061
1062 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1063 &pdata->clk_map))
1064 goto err;
1065
1066 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1067
1068 if (device->id != KGSL_DEVICE_3D0)
1069 goto err;
1070
1071 /* Bus Scale Data */
1072
1073 pdata->bus_scale_table = adreno_of_get_bus_scale(pdev->dev.of_node);
1074 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1075 ret = PTR_ERR(pdata->bus_scale_table);
1076 goto err;
1077 }
1078
1079 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1080 if (IS_ERR_OR_NULL(pdata->core_info)) {
1081 ret = PTR_ERR(pdata->core_info);
1082 goto err;
1083 }
1084
1085 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1086 if (ret)
1087 goto err;
1088
1089 pdev->dev.platform_data = pdata;
1090 return 0;
1091
1092err:
1093 if (pdata) {
1094 adreno_of_free_bus_scale_info(pdata->bus_scale_table);
1095 if (pdata->core_info)
1096 kfree(pdata->core_info->freq_tbl);
1097 kfree(pdata->core_info);
1098
1099 if (pdata->iommu_data)
1100 kfree(pdata->iommu_data->iommu_ctxs);
1101
1102 kfree(pdata->iommu_data);
1103 }
1104
1105 kfree(pdata);
1106
1107 return ret;
1108}
1109
liu zhong7dfa2a32012-04-27 19:11:01 -07001110#ifdef CONFIG_MSM_OCMEM
1111static int
1112adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1113{
1114 if (adreno_dev->gpurev != ADRENO_REV_A330)
1115 return 0;
1116
1117 /* OCMEM is only needed once, do not support consective allocation */
1118 if (adreno_dev->ocmem_hdl != NULL)
1119 return 0;
1120
1121 adreno_dev->ocmem_hdl =
1122 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1123 if (adreno_dev->ocmem_hdl == NULL)
1124 return -ENOMEM;
1125
1126 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
1127 adreno_dev->gmem_base = adreno_dev->ocmem_hdl->addr;
1128
1129 return 0;
1130}
1131
1132static void
1133adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1134{
1135 if (adreno_dev->gpurev != ADRENO_REV_A330)
1136 return;
1137
1138 if (adreno_dev->ocmem_hdl == NULL)
1139 return;
1140
1141 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1142 adreno_dev->ocmem_hdl = NULL;
1143}
1144#else
1145static int
1146adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1147{
1148 return 0;
1149}
1150
1151static void
1152adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1153{
1154}
1155#endif
1156
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157static int __devinit
1158adreno_probe(struct platform_device *pdev)
1159{
1160 struct kgsl_device *device;
1161 struct adreno_device *adreno_dev;
1162 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001163 bool is_dt;
1164
1165 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1166
1167 if (is_dt && pdev->dev.of_node) {
1168 status = adreno_of_get_pdata(pdev);
1169 if (status)
1170 goto error_return;
1171 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172
1173 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1174 adreno_dev = ADRENO_DEVICE(device);
1175 device->parentdev = &pdev->dev;
1176
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 status = adreno_ringbuffer_init(device);
1178 if (status != 0)
1179 goto error;
1180
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001181 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182 if (status)
1183 goto error_close_rb;
1184
1185 adreno_debugfs_init(device);
1186
1187 kgsl_pwrscale_init(device);
1188 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1189
1190 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1191 return 0;
1192
1193error_close_rb:
1194 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1195error:
1196 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001197error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198 return status;
1199}
1200
1201static int __devexit adreno_remove(struct platform_device *pdev)
1202{
1203 struct kgsl_device *device;
1204 struct adreno_device *adreno_dev;
1205
1206 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1207 adreno_dev = ADRENO_DEVICE(device);
1208
1209 kgsl_pwrscale_detach_policy(device);
1210 kgsl_pwrscale_close(device);
1211
1212 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1213 kgsl_device_platform_remove(device);
1214
1215 return 0;
1216}
1217
1218static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1219{
1220 int status = -EINVAL;
1221 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001223 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1224 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225
1226 /* Power up the device */
1227 kgsl_pwrctrl_enable(device);
1228
1229 /* Identify the specific GPU */
1230 adreno_identify_gpu(adreno_dev);
1231
Jordan Crouse505df9c2011-07-28 08:37:59 -06001232 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1233 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1234 adreno_dev->chip_id);
1235 goto error_clk_off;
1236 }
1237
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001238 /* Set up the MMU */
1239 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001240 /*
1241 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1242 * on older gpus
1243 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001244 if (adreno_is_a20x(adreno_dev)) {
1245 device->mh.mh_intf_cfg1 = 0;
1246 device->mh.mh_intf_cfg2 = 0;
1247 }
1248
1249 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001250 }
1251
Tarun Karra3335f142012-06-19 14:11:48 -07001252 /* Assign correct RBBM status register to hang detect regs
1253 */
1254 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1255
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001256 status = kgsl_mmu_start(device);
1257 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001258 goto error_clk_off;
1259
liu zhong7dfa2a32012-04-27 19:11:01 -07001260 status = adreno_ocmem_gmem_malloc(adreno_dev);
1261 if (status) {
1262 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1263 goto error_mmu_off;
1264 }
1265
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001266 /* Start the GPU */
1267 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001268
1269 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001270 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271
1272 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001273 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001274 /* While recovery is on we do not want timer to
1275 * fire and attempt to change any device state */
1276 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1277 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001278 return 0;
1279 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001280
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001282
1283error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001284 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001285
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286error_clk_off:
1287 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288
1289 return status;
1290}
1291
1292static int adreno_stop(struct kgsl_device *device)
1293{
1294 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1295
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001296 adreno_dev->drawctxt_active = NULL;
1297
1298 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1299
Shubhraprakash Das79447952012-04-26 18:12:23 -06001300 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001302 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301303 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001304 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001305
liu zhong7dfa2a32012-04-27 19:11:01 -07001306 adreno_ocmem_gmem_free(adreno_dev);
1307
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001308 /* Power down the device */
1309 kgsl_pwrctrl_disable(device);
1310
1311 return 0;
1312}
1313
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001314static void adreno_mark_context_status(struct kgsl_device *device,
1315 int recovery_status)
1316{
1317 struct kgsl_context *context;
1318 int next = 0;
1319 /*
1320 * Set the reset status of all contexts to
1321 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1322 * since thats the guilty party, if recovery failed then
1323 * mark all as guilty
1324 */
1325 while ((context = idr_get_next(&device->context_idr, &next))) {
1326 struct adreno_context *adreno_context = context->devctxt;
1327 if (recovery_status) {
1328 context->reset_status =
1329 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1330 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1331 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1332 context->reset_status) {
1333 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1334 CTXT_FLAGS_GPU_HANG_RECOVERED))
1335 context->reset_status =
1336 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1337 else
1338 context->reset_status =
1339 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1340 }
1341 next = next + 1;
1342 }
1343}
1344
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001345static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1346{
1347 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1348 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1349 struct kgsl_context *context;
1350 struct adreno_context *temp_adreno_context;
1351 int next = 0;
1352
1353 while ((context = idr_get_next(&device->context_idr, &next))) {
1354 temp_adreno_context = context->devctxt;
1355 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1356 kgsl_sharedmem_writel(&device->memstore,
1357 KGSL_MEMSTORE_OFFSET(context->id,
1358 soptimestamp),
1359 rb->timestamp[context->id]);
1360 kgsl_sharedmem_writel(&device->memstore,
1361 KGSL_MEMSTORE_OFFSET(context->id,
1362 eoptimestamp),
1363 rb->timestamp[context->id]);
1364 }
1365 next = next + 1;
1366 }
1367}
1368
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001369static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1370{
1371 vfree(rec_data->rb_buffer);
1372 vfree(rec_data->bad_rb_buffer);
1373}
1374
1375static int adreno_setup_recovery_data(struct kgsl_device *device,
1376 struct adreno_recovery_data *rec_data)
1377{
1378 int ret = 0;
1379 unsigned int ib1_sz, ib2_sz;
1380 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1381 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1382
1383 memset(rec_data, 0, sizeof(*rec_data));
1384
1385 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1386 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1387 if (ib1_sz || ib2_sz)
1388 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1389
1390 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1391 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1392 current_context));
1393
1394 kgsl_sharedmem_readl(&device->memstore,
1395 &rec_data->global_eop,
1396 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1397 eoptimestamp));
1398
1399 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1400 if (!rec_data->rb_buffer) {
1401 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1402 rb->buffer_desc.size);
1403 return -ENOMEM;
1404 }
1405
1406 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1407 if (!rec_data->bad_rb_buffer) {
1408 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1409 rb->buffer_desc.size);
1410 ret = -ENOMEM;
1411 goto done;
1412 }
1413
1414done:
1415 if (ret) {
1416 vfree(rec_data->rb_buffer);
1417 vfree(rec_data->bad_rb_buffer);
1418 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419 return ret;
1420}
1421
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001422static int
1423_adreno_recover_hang(struct kgsl_device *device,
1424 struct adreno_recovery_data *rec_data,
1425 bool try_bad_commands)
1426{
1427 int ret;
1428 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1429 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1430 struct kgsl_context *context;
1431 struct adreno_context *adreno_context = NULL;
1432 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1433
1434 context = idr_find(&device->context_idr, rec_data->context_id);
1435 if (context == NULL) {
1436 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1437 rec_data->context_id);
1438 } else {
1439 adreno_context = context->devctxt;
1440 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1441 }
1442
1443 /* Extract valid contents from rb which can still be executed after
1444 * hang */
1445 ret = adreno_ringbuffer_extract(rb, rec_data);
1446 if (ret)
1447 goto done;
1448
1449 /* restart device */
1450 ret = adreno_stop(device);
1451 if (ret) {
1452 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1453 goto done;
1454 }
1455
1456 ret = adreno_start(device, true);
1457 if (ret) {
1458 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1459 goto done;
1460 }
1461
1462 if (context)
1463 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1464 KGSL_MEMSTORE_GLOBAL);
1465
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001466 /* If iommu is used then we need to make sure that the iommu clocks
1467 * are on since there could be commands in pipeline that touch iommu */
1468 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1469 ret = kgsl_mmu_enable_clk(&device->mmu,
1470 KGSL_IOMMU_CONTEXT_USER);
1471 if (ret)
1472 goto done;
1473 }
1474
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001475 /* Do not try the bad caommands if recovery has failed bad commands
1476 * once already */
1477 if (!try_bad_commands)
1478 rec_data->bad_rb_size = 0;
1479
1480 if (rec_data->bad_rb_size) {
1481 int idle_ret;
1482 /* submit the bad and good context commands and wait for
1483 * them to pass */
1484 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1485 rec_data->bad_rb_size);
1486 idle_ret = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1487 if (idle_ret) {
1488 ret = adreno_stop(device);
1489 if (ret) {
1490 KGSL_DRV_ERR(device,
1491 "Device stop failed in recovery\n");
1492 goto done;
1493 }
1494 ret = adreno_start(device, true);
1495 if (ret) {
1496 KGSL_DRV_ERR(device,
1497 "Device start failed in recovery\n");
1498 goto done;
1499 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001500 if (context)
1501 kgsl_mmu_setstate(&device->mmu,
1502 adreno_context->pagetable,
1503 KGSL_MEMSTORE_GLOBAL);
1504
1505 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1506 ret = kgsl_mmu_enable_clk(&device->mmu,
1507 KGSL_IOMMU_CONTEXT_USER);
1508 if (ret)
1509 goto done;
1510 }
1511
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001512 ret = idle_ret;
1513 KGSL_DRV_ERR(device,
1514 "Bad context commands hung in recovery\n");
1515 } else {
1516 KGSL_DRV_ERR(device,
1517 "Bad context commands succeeded in recovery\n");
1518 if (adreno_context)
1519 adreno_context->flags = (adreno_context->flags &
1520 ~CTXT_FLAGS_GPU_HANG) |
1521 CTXT_FLAGS_GPU_HANG_RECOVERED;
1522 adreno_dev->drawctxt_active = last_active_ctx;
1523 }
1524 }
1525 /* If either the bad command sequence failed or we did not play it */
1526 if (ret || !rec_data->bad_rb_size) {
1527 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1528 rec_data->rb_size);
1529 ret = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1530 if (ret) {
1531 /* If we fail here we can try to invalidate another
1532 * context and try recovering again */
1533 ret = -EAGAIN;
1534 goto done;
1535 }
1536 /* ringbuffer now has data from the last valid context id,
1537 * so restore the active_ctx to the last valid context */
1538 if (rec_data->last_valid_ctx_id) {
1539 struct kgsl_context *last_ctx =
1540 idr_find(&device->context_idr,
1541 rec_data->last_valid_ctx_id);
1542 if (last_ctx)
1543 adreno_dev->drawctxt_active = last_ctx->devctxt;
1544 }
1545 }
1546done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001547 /* Turn off iommu clocks */
1548 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1549 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001550 return ret;
1551}
1552
1553static int
1554adreno_recover_hang(struct kgsl_device *device,
1555 struct adreno_recovery_data *rec_data)
1556{
1557 int ret = 0;
1558 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1559 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1560 unsigned int timestamp;
1561
1562 KGSL_DRV_ERR(device,
1563 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1564 "Bad context_id: %u, global_eop: 0x%x\n",
1565 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1566
1567 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1568 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1569
1570 /* We may need to replay commands multiple times based on whether
1571 * multiple contexts hang the GPU */
1572 while (true) {
1573 if (!ret)
1574 ret = _adreno_recover_hang(device, rec_data, true);
1575 else
1576 ret = _adreno_recover_hang(device, rec_data, false);
1577
1578 if (-EAGAIN == ret) {
1579 /* setup new recovery parameters and retry, this
1580 * means more than 1 contexts are causing hang */
1581 adreno_destroy_recovery_data(rec_data);
1582 adreno_setup_recovery_data(device, rec_data);
1583 KGSL_DRV_ERR(device,
1584 "Retry recovery from 3D GPU hang. Recovery parameters: "
1585 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1586 rec_data->ib1, rec_data->context_id,
1587 rec_data->global_eop);
1588 } else {
1589 break;
1590 }
1591 }
1592
1593 if (ret)
1594 goto done;
1595
1596 /* Restore correct states after recovery */
1597 if (adreno_dev->drawctxt_active)
1598 device->mmu.hwpagetable =
1599 adreno_dev->drawctxt_active->pagetable;
1600 else
1601 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1602 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1603 kgsl_sharedmem_writel(&device->memstore,
1604 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1605 eoptimestamp),
1606 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1607done:
1608 adreno_set_max_ts_for_bad_ctxs(device);
1609 adreno_mark_context_status(device, ret);
1610 if (!ret)
1611 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1612 else
1613 KGSL_DRV_ERR(device, "Recovery failed\n");
1614 return ret;
1615}
1616
1617int
1618adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001619{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001620 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001621 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001622
1623 if (device->state == KGSL_STATE_HUNG)
1624 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001625 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001626 mutex_unlock(&device->mutex);
1627 wait_for_completion(&device->recovery_gate);
1628 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001629 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001630 result = 0;
1631 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001632 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001633 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001634 /* Detected a hang */
1635
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001636 /* Get the recovery data as soon as hang is detected */
1637 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001638 /*
1639 * Trigger an automatic dump of the state to
1640 * the console
1641 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001642 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001643
1644 /*
1645 * Make a GPU snapshot. For now, do it after the PM dump so we
1646 * can at least be sure the PM dump will work as it always has
1647 */
1648 kgsl_device_snapshot(device, 1);
1649
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001650 result = adreno_recover_hang(device, &rec_data);
1651 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001652 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001653 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001654 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001655 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001656 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1657 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001658 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001659 }
1660done:
1661 return result;
1662}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001663EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001664
1665static int adreno_getproperty(struct kgsl_device *device,
1666 enum kgsl_property_type type,
1667 void *value,
1668 unsigned int sizebytes)
1669{
1670 int status = -EINVAL;
1671 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1672
1673 switch (type) {
1674 case KGSL_PROP_DEVICE_INFO:
1675 {
1676 struct kgsl_devinfo devinfo;
1677
1678 if (sizebytes != sizeof(devinfo)) {
1679 status = -EINVAL;
1680 break;
1681 }
1682
1683 memset(&devinfo, 0, sizeof(devinfo));
1684 devinfo.device_id = device->id+1;
1685 devinfo.chip_id = adreno_dev->chip_id;
1686 devinfo.mmu_enabled = kgsl_mmu_enabled();
1687 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001688 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1689 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001690
1691 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1692 0) {
1693 status = -EFAULT;
1694 break;
1695 }
1696 status = 0;
1697 }
1698 break;
1699 case KGSL_PROP_DEVICE_SHADOW:
1700 {
1701 struct kgsl_shadowprop shadowprop;
1702
1703 if (sizebytes != sizeof(shadowprop)) {
1704 status = -EINVAL;
1705 break;
1706 }
1707 memset(&shadowprop, 0, sizeof(shadowprop));
1708 if (device->memstore.hostptr) {
1709 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1710 * anything to mmap().
1711 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001712 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713 shadowprop.size = device->memstore.size;
1714 /* GSL needs this to be set, even if it
1715 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001716 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1717 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001718 }
1719 if (copy_to_user(value, &shadowprop,
1720 sizeof(shadowprop))) {
1721 status = -EFAULT;
1722 break;
1723 }
1724 status = 0;
1725 }
1726 break;
1727 case KGSL_PROP_MMU_ENABLE:
1728 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001729 int mmu_prop = kgsl_mmu_enabled();
1730
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001731 if (sizebytes != sizeof(int)) {
1732 status = -EINVAL;
1733 break;
1734 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001735 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001736 status = -EFAULT;
1737 break;
1738 }
1739 status = 0;
1740 }
1741 break;
1742 case KGSL_PROP_INTERRUPT_WAITS:
1743 {
1744 int int_waits = 1;
1745 if (sizebytes != sizeof(int)) {
1746 status = -EINVAL;
1747 break;
1748 }
1749 if (copy_to_user(value, &int_waits, sizeof(int))) {
1750 status = -EFAULT;
1751 break;
1752 }
1753 status = 0;
1754 }
1755 break;
1756 default:
1757 status = -EINVAL;
1758 }
1759
1760 return status;
1761}
1762
Jordan Crousef7370f82012-04-18 09:31:07 -06001763static int adreno_setproperty(struct kgsl_device *device,
1764 enum kgsl_property_type type,
1765 void *value,
1766 unsigned int sizebytes)
1767{
1768 int status = -EINVAL;
1769
1770 switch (type) {
1771 case KGSL_PROP_PWRCTRL: {
1772 unsigned int enable;
1773 struct kgsl_device_platform_data *pdata =
1774 kgsl_device_get_drvdata(device);
1775
1776 if (sizebytes != sizeof(enable))
1777 break;
1778
1779 if (copy_from_user(&enable, (void __user *) value,
1780 sizeof(enable))) {
1781 status = -EFAULT;
1782 break;
1783 }
1784
1785 if (enable) {
1786 if (pdata->nap_allowed)
1787 device->pwrctrl.nap_allowed = true;
1788
1789 kgsl_pwrscale_enable(device);
1790 } else {
1791 device->pwrctrl.nap_allowed = false;
1792 kgsl_pwrscale_disable(device);
1793 }
1794
1795 status = 0;
1796 }
1797 break;
1798 default:
1799 break;
1800 }
1801
1802 return status;
1803}
1804
Lynus Vaz06a9a902011-10-04 19:25:33 +05301805static inline void adreno_poke(struct kgsl_device *device)
1806{
1807 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1808 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1809}
1810
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001811/* Caller must hold the device mutex. */
1812int adreno_idle(struct kgsl_device *device, unsigned int timeout)
1813{
1814 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1815 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1816 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301817 unsigned long wait_timeout =
1818 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +05301819 unsigned long wait_time;
1820 unsigned long wait_time_part;
1821 unsigned int msecs;
1822 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07001823 unsigned int msecs_part = KGSL_TIMEOUT_PART;
1824 unsigned int prev_reg_val[hang_detect_regs_count];
1825
1826 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001827
Tarun Karrad526d1e2012-06-11 17:51:09 -07001828 /* Restrict timeout value between adreno_dev->wait_timeout and 0 */
1829 if ((timeout == 0) || (timeout > adreno_dev->wait_timeout))
1830 msecs = adreno_dev->wait_timeout;
1831 else
1832 msecs = timeout;
1833
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001834 kgsl_cffdump_regpoll(device->id,
1835 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001836 0x00000000, 0x80000000);
1837 /* first, wait until the CP has consumed all the commands in
1838 * the ring buffer
1839 */
1840retry:
1841 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +05301842 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Lynus Vaz284d1042012-01-31 16:32:31 +05301843 wait_time = jiffies + wait_timeout;
1844 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -07001845 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001846 do {
Lynus Vaz284d1042012-01-31 16:32:31 +05301847 if (time_after(jiffies, wait_time_part)) {
1848 adreno_poke(device);
1849 wait_time_part = jiffies +
1850 msecs_to_jiffies(msecs_part);
Tarun Karra3335f142012-06-19 14:11:48 -07001851 if ((adreno_hang_detect(device, prev_reg_val)))
1852 goto err;
Lynus Vaz284d1042012-01-31 16:32:31 +05301853 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001854 GSL_RB_GET_READPTR(rb, &rb->rptr);
1855 if (time_after(jiffies, wait_time)) {
1856 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1857 rb->rptr, rb->wptr);
1858 goto err;
1859 }
1860 } while (rb->rptr != rb->wptr);
1861 }
1862
1863 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301864 wait_time = jiffies + wait_timeout;
Tarun Karra3335f142012-06-19 14:11:48 -07001865 wait_time_part = jiffies + msecs_to_jiffies(msecs_part);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001866 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001867 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1868 &rbbm_status);
1869 if (adreno_is_a2xx(adreno_dev)) {
1870 if (rbbm_status == 0x110)
1871 return 0;
1872 } else {
1873 if (!(rbbm_status & 0x80000000))
1874 return 0;
1875 }
Tarun Karra3335f142012-06-19 14:11:48 -07001876
1877 /* Dont wait for timeout, detect hang faster.
1878 */
1879 if (time_after(jiffies, wait_time_part)) {
1880 wait_time_part = jiffies +
1881 msecs_to_jiffies(msecs_part);
1882 if ((adreno_hang_detect(device, prev_reg_val)))
1883 goto err;
1884 }
1885
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886 }
1887
1888err:
1889 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001890 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1891 !adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301892 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001893 goto retry;
1894 }
1895 return -ETIMEDOUT;
1896}
1897
1898static unsigned int adreno_isidle(struct kgsl_device *device)
1899{
1900 int status = false;
1901 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1902 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1903 unsigned int rbbm_status;
1904
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001905 WARN_ON(device->state == KGSL_STATE_INIT);
1906 /* If the device isn't active, don't force it on. */
1907 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 /* Is the ring buffer is empty? */
1909 GSL_RB_GET_READPTR(rb, &rb->rptr);
1910 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1911 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001912 adreno_regread(device,
1913 adreno_dev->gpudev->reg_rbbm_status,
1914 &rbbm_status);
1915
1916 if (adreno_is_a2xx(adreno_dev)) {
1917 if (rbbm_status == 0x110)
1918 status = true;
1919 } else {
1920 if (!(rbbm_status & 0x80000000))
1921 status = true;
1922 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001923 }
1924 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001925 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001926 }
1927 return status;
1928}
1929
1930/* Caller must hold the device mutex. */
1931static int adreno_suspend_context(struct kgsl_device *device)
1932{
1933 int status = 0;
1934 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1935
1936 /* switch to NULL ctxt */
1937 if (adreno_dev->drawctxt_active != NULL) {
1938 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1939 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1940 }
1941
1942 return status;
1943}
1944
Jordan Crouse233b2092012-04-18 09:31:09 -06001945/* Find a memory structure attached to an adreno context */
1946
1947struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1948 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1949{
1950 struct kgsl_context *context;
1951 struct adreno_context *adreno_context = NULL;
1952 int next = 0;
1953
1954 while (1) {
1955 context = idr_get_next(&device->context_idr, &next);
1956 if (context == NULL)
1957 break;
1958
1959 adreno_context = (struct adreno_context *)context->devctxt;
1960
1961 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1962 struct kgsl_memdesc *desc;
1963
1964 desc = &adreno_context->gpustate;
1965 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1966 return desc;
1967
1968 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1969 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1970 return desc;
1971 }
1972 next = next + 1;
1973 }
1974
1975 return NULL;
1976}
1977
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001978struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001979 unsigned int pt_base,
1980 unsigned int gpuaddr,
1981 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001982{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001983 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001984 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1985 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1986
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001987 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1988 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001989
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001990 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1991 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001992
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001993 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1994 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001995
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001996 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1997 size))
1998 return &device->mmu.setstate_memory;
1999
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002000 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
2001
2002 if (entry)
2003 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002004
Jordan Crouse233b2092012-04-18 09:31:09 -06002005 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002006}
2007
2008uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2009 unsigned int gpuaddr, unsigned int size)
2010{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002011 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002012
2013 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2014
2015 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002016}
2017
2018void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2019 unsigned int *value)
2020{
2021 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002022 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2023 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002024
2025 if (!in_interrupt())
2026 kgsl_pre_hwaccess(device);
2027
2028 /*ensure this read finishes before the next one.
2029 * i.e. act like normal readl() */
2030 *value = __raw_readl(reg);
2031 rmb();
2032}
2033
2034void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2035 unsigned int value)
2036{
2037 unsigned int *reg;
2038
Jordan Crouse7501d452012-04-19 08:58:44 -06002039 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002040
2041 if (!in_interrupt())
2042 kgsl_pre_hwaccess(device);
2043
2044 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002045 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002046
2047 /*ensure previous writes post before this one,
2048 * i.e. act like normal writel() */
2049 wmb();
2050 __raw_writel(value, reg);
2051}
2052
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002053static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2054{
2055 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002056 if (k_ctxt != NULL) {
2057 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002058 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2059 context_id = KGSL_CONTEXT_INVALID;
2060 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2061 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002062 }
2063
2064 return context_id;
2065}
2066
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002067static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002068 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002069{
2070 int status;
2071 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002072 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002073 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002074
2075 mutex_lock(&device->mutex);
2076 context_id = _get_context_id(context);
2077 /*
2078 * If the context ID is invalid, we are in a race with
2079 * the context being destroyed by userspace so bail.
2080 */
2081 if (context_id == KGSL_CONTEXT_INVALID) {
2082 KGSL_DRV_WARN(device, "context was detached");
2083 status = -EINVAL;
2084 goto unlock;
2085 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002086
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002087 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002089 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002090 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002091 mb();
2092
2093 if (enableflag) {
2094 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002095 KGSL_MEMSTORE_OFFSET(context_id,
2096 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002097 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002098 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002099 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002100 KGSL_MEMSTORE_OFFSET(context_id,
2101 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002102 wmb();
2103 }
2104 } else {
2105 unsigned int cmds[2];
2106 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002107 KGSL_MEMSTORE_OFFSET(context_id,
2108 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002109 enableflag = 1;
2110 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002111 KGSL_MEMSTORE_OFFSET(context_id,
2112 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002113 wmb();
2114 /* submit a dummy packet so that even if all
2115 * commands upto timestamp get executed we will still
2116 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002117 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002118 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002119
2120 if (adreno_dev->drawctxt_active)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002121 adreno_ringbuffer_issuecmds_intr(device,
2122 context, &cmds[0], 2);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002123 else
2124 /* We would never call this function if there
2125 * was no active contexts running */
2126 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002127 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002128 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002129unlock:
2130 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002131
2132 return status;
2133}
2134
2135/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002136 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002137 placing a process in wait q. For conditional interrupts we expect the
2138 process to already be in its wait q when its exit condition checking
2139 function is called.
2140*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002141#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002142({ \
2143 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002144 if (io) \
2145 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2146 else \
2147 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002148 __ret; \
2149})
2150
Tarun Karra3335f142012-06-19 14:11:48 -07002151
2152
2153unsigned int adreno_hang_detect(struct kgsl_device *device,
2154 unsigned int *prev_reg_val)
2155{
2156 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2157 unsigned int curr_reg_val[hang_detect_regs_count];
2158 unsigned int hang_detected = 1;
2159 unsigned int i;
2160
2161 if (!adreno_dev->fast_hang_detect)
2162 return 0;
2163
2164 for (i = 0; i < hang_detect_regs_count; i++) {
2165 adreno_regread(device, hang_detect_regs[i],
2166 &curr_reg_val[i]);
2167 if (curr_reg_val[i] != prev_reg_val[i]) {
2168 prev_reg_val[i] = curr_reg_val[i];
2169 hang_detected = 0;
2170 }
2171 }
2172
2173 return hang_detected;
2174}
2175
2176
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002177/* MUST be called with the device mutex held */
2178static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002179 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002180 unsigned int timestamp,
2181 unsigned int msecs)
2182{
2183 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06002184 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06002185 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06002187 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07002188 int retries = 0;
Lynus Vaz06a9a902011-10-04 19:25:33 +05302189 unsigned int msecs_first;
Tarun Karra3335f142012-06-19 14:11:48 -07002190 unsigned int msecs_part = KGSL_TIMEOUT_PART;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002191 unsigned int ts_issued;
2192 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07002193 unsigned int time_elapsed = 0;
2194 unsigned int prev_reg_val[hang_detect_regs_count];
2195
2196 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002197
2198 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002199
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302200 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07002201 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302202 msecs = adreno_dev->wait_timeout;
2203
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002204 if (timestamp_cmp(timestamp, ts_issued) > 0) {
2205 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
2206 "last issued ts <%d:0x%x>\n",
2207 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208 status = -EINVAL;
2209 goto done;
2210 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002211
Lynus Vaz06a9a902011-10-04 19:25:33 +05302212 /* Keep the first timeout as 100msecs before rewriting
2213 * the WPTR. Less visible impact if the WPTR has not
2214 * been updated properly.
2215 */
2216 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
Tarun Karra3335f142012-06-19 14:11:48 -07002217 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002218 /*
2219 * If the context ID is invalid, we are in a race with
2220 * the context being destroyed by userspace so bail.
2221 */
2222 if (context_id == KGSL_CONTEXT_INVALID) {
2223 KGSL_DRV_WARN(device, "context was detached");
2224 status = -EINVAL;
2225 goto done;
2226 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002227 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07002228 /* if the timestamp happens while we're not
2229 * waiting, there's a chance that an interrupt
2230 * will not be generated and thus the timestamp
2231 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05302232 */
Jeremy Gebben63904832012-02-07 16:10:55 -07002233 queue_work(device->work_queue, &device->ts_expired_ws);
2234 status = 0;
2235 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002236 }
Jeremy Gebben63904832012-02-07 16:10:55 -07002237 adreno_poke(device);
2238 io_cnt = (io_cnt + 1) % 100;
2239 if (io_cnt <
2240 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2241 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002242
2243 if ((retries > 0) &&
2244 (adreno_hang_detect(device, prev_reg_val)))
2245 goto hang_dump;
2246
Jeremy Gebben63904832012-02-07 16:10:55 -07002247 mutex_unlock(&device->mutex);
2248 /* We need to make sure that the process is
2249 * placed in wait-q before its condition is called
2250 */
2251 status = kgsl_wait_event_interruptible_timeout(
2252 device->wait_queue,
2253 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002254 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07002255 msecs_to_jiffies(retries ?
2256 msecs_part : msecs_first), io);
2257 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002258
Jeremy Gebben63904832012-02-07 16:10:55 -07002259 if (status > 0) {
2260 /*completed before the wait finished */
2261 status = 0;
2262 goto done;
2263 } else if (status < 0) {
2264 /*an error occurred*/
2265 goto done;
2266 }
2267 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07002268
2269 time_elapsed = time_elapsed +
2270 (retries ? msecs_part : msecs_first);
2271 retries++;
2272
2273 } while (time_elapsed < msecs);
2274
2275hang_dump:
Shubhraprakash Das54396e52012-03-10 13:24:54 -07002276 /*
2277 * Check if timestamp has retired here because we may have hit
2278 * recovery which can take some time and cause waiting threads
2279 * to timeout
2280 */
2281 if (kgsl_check_timestamp(device, context, timestamp))
2282 goto done;
Jeremy Gebben63904832012-02-07 16:10:55 -07002283 status = -ETIMEDOUT;
2284 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002285 "Device hang detected while waiting for timestamp: "
2286 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2287 "wptr: 0x%x\n",
2288 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002289 adreno_dev->ringbuffer.wptr);
2290 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002291 /* The timestamp that this process wanted
2292 * to wait on may be invalid or expired now
2293 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07002294 status = 0;
2295 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002296done:
2297 return (int)status;
2298}
2299
2300static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002301 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002302{
2303 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002304 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002305
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002306 /*
2307 * If the context ID is invalid, we are in a race with
2308 * the context being destroyed by userspace so bail.
2309 */
2310 if (context_id == KGSL_CONTEXT_INVALID) {
2311 KGSL_DRV_WARN(device, "context was detached");
2312 return timestamp;
2313 }
Jordan Crousec659f382012-04-16 11:10:41 -06002314 switch (type) {
2315 case KGSL_TIMESTAMP_QUEUED: {
2316 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2317 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2318
2319 timestamp = rb->timestamp[context_id];
2320 break;
2321 }
2322 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002323 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002324 break;
2325 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002326 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002327 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2328 break;
2329 }
2330
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002331 rmb();
2332
2333 return timestamp;
2334}
2335
2336static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2337 unsigned int cmd, void *data)
2338{
2339 int result = 0;
2340 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2341 struct kgsl_context *context;
2342
2343 switch (cmd) {
2344 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2345 binbase = data;
2346
2347 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2348 if (context) {
2349 adreno_drawctxt_set_bin_base_offset(
2350 dev_priv->device, context, binbase->offset);
2351 } else {
2352 result = -EINVAL;
2353 KGSL_DRV_ERR(dev_priv->device,
2354 "invalid drawctxt drawctxt_id %d "
2355 "device_id=%d\n",
2356 binbase->drawctxt_id, dev_priv->device->id);
2357 }
2358 break;
2359
2360 default:
2361 KGSL_DRV_INFO(dev_priv->device,
2362 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002363 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002364 break;
2365 }
2366 return result;
2367
2368}
2369
2370static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2371{
2372 gpu_freq /= 1000000;
2373 return ticks / gpu_freq;
2374}
2375
2376static void adreno_power_stats(struct kgsl_device *device,
2377 struct kgsl_power_stats *stats)
2378{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002379 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002380 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002381 unsigned int cycles;
2382
2383 /* Get the busy cycles counted since the counter was last reset */
2384 /* Calling this function also resets and restarts the counter */
2385
2386 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002387
2388 /* In order to calculate idle you have to have run the algorithm *
2389 * at least once to get a start time. */
2390 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002391 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002392 stats->total_time = tmp - pwr->time;
2393 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002394 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002395 pwrlevels[device->pwrctrl.active_pwrlevel].
2396 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002397 } else {
2398 stats->total_time = 0;
2399 stats->busy_time = 0;
2400 pwr->time = ktime_to_us(ktime_get());
2401 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002402}
2403
2404void adreno_irqctrl(struct kgsl_device *device, int state)
2405{
Jordan Crousea78c9172011-07-11 13:14:09 -06002406 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2407 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002408}
2409
Jordan Croused6535882012-06-20 08:22:16 -06002410static unsigned int adreno_gpuid(struct kgsl_device *device,
2411 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002412{
2413 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2414
Jordan Croused6535882012-06-20 08:22:16 -06002415 /* Some applications need to know the chip ID too, so pass
2416 * that as a parameter */
2417
2418 if (chipid != NULL)
2419 *chipid = adreno_dev->chip_id;
2420
Jordan Crousea0758f22011-12-07 11:19:22 -07002421 /* Standard KGSL gpuid format:
2422 * top word is 0x0002 for 2D or 0x0003 for 3D
2423 * Bottom word is core specific identifer
2424 */
2425
2426 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2427}
2428
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002429static const struct kgsl_functable adreno_functable = {
2430 /* Mandatory functions */
2431 .regread = adreno_regread,
2432 .regwrite = adreno_regwrite,
2433 .idle = adreno_idle,
2434 .isidle = adreno_isidle,
2435 .suspend_context = adreno_suspend_context,
2436 .start = adreno_start,
2437 .stop = adreno_stop,
2438 .getproperty = adreno_getproperty,
2439 .waittimestamp = adreno_waittimestamp,
2440 .readtimestamp = adreno_readtimestamp,
2441 .issueibcmds = adreno_ringbuffer_issueibcmds,
2442 .ioctl = adreno_ioctl,
2443 .setup_pt = adreno_setup_pt,
2444 .cleanup_pt = adreno_cleanup_pt,
2445 .power_stats = adreno_power_stats,
2446 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002447 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002448 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002449 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002450 /* Optional functions */
2451 .setstate = adreno_setstate,
2452 .drawctxt_create = adreno_drawctxt_create,
2453 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002454 .setproperty = adreno_setproperty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002455};
2456
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002457static struct platform_driver adreno_platform_driver = {
2458 .probe = adreno_probe,
2459 .remove = __devexit_p(adreno_remove),
2460 .suspend = kgsl_suspend_driver,
2461 .resume = kgsl_resume_driver,
2462 .id_table = adreno_id_table,
2463 .driver = {
2464 .owner = THIS_MODULE,
2465 .name = DEVICE_3D_NAME,
2466 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002467 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002468 }
2469};
2470
2471static int __init kgsl_3d_init(void)
2472{
2473 return platform_driver_register(&adreno_platform_driver);
2474}
2475
2476static void __exit kgsl_3d_exit(void)
2477{
2478 platform_driver_unregister(&adreno_platform_driver);
2479}
2480
2481module_init(kgsl_3d_init);
2482module_exit(kgsl_3d_exit);
2483
2484MODULE_DESCRIPTION("3D Graphics driver");
2485MODULE_VERSION("1.2");
2486MODULE_LICENSE("GPL v2");
2487MODULE_ALIAS("platform:kgsl_3d");