blob: 3d1fbc8ff35d24abbcd6494e60e67f62d082c3e3 [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"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070036#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070037#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
39#define DRIVER_VERSION_MAJOR 3
40#define DRIVER_VERSION_MINOR 1
41
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042/* Adreno MH arbiter config*/
43#define ADRENO_CFG_MHARB \
44 (0x10 \
45 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
46 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
47 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
48 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
52 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
53 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
54 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
55 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
56 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
57 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
58 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
59
60#define ADRENO_MMU_CONFIG \
61 (0x01 \
62 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
69 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
70 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
71 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
72 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
73
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074static const struct kgsl_functable adreno_functable;
75
76static struct adreno_device device_3d0 = {
77 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070078 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079 .name = DEVICE_3D0_NAME,
80 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060081 .mh = {
82 .mharb = ADRENO_CFG_MHARB,
83 /* Remove 1k boundary check in z470 to avoid a GPU
84 * hang. Notice that this solution won't work if
85 * both EBI and SMI are used
86 */
87 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088 /* turn off memory protection unit by setting
89 acceptable physical address range to include
90 all pages. */
91 .mpu_base = 0x00000000,
92 .mpu_range = 0xFFFFF000,
93 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060094 .mmu = {
95 .config = ADRENO_MMU_CONFIG,
96 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 .iomemname = KGSL_3D0_REG_MEMORY,
101 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600103 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
105 .suspend = kgsl_early_suspend_driver,
106 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600108#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600110 .gmem_base = 0,
111 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 .pfp_fw = NULL,
113 .pm4_fw = NULL,
Jordan Crouse21f75a02012-08-09 15:08:59 -0600114 .wait_timeout = 0, /* in milliseconds, 0 means disabled */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600115 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116};
117
Tarun Karra3335f142012-06-19 14:11:48 -0700118/* This set of registers are used for Hang detection
119 * If the values of these registers are same after
120 * KGSL_TIMEOUT_PART time, GPU hang is reported in
121 * kernel log.
122 */
123unsigned int hang_detect_regs[] = {
124 A3XX_RBBM_STATUS,
125 REG_CP_RB_RPTR,
126 REG_CP_IB1_BASE,
127 REG_CP_IB1_BUFSZ,
128 REG_CP_IB2_BASE,
129 REG_CP_IB2_BUFSZ,
130};
131
132const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700133
Jordan Crouse505df9c2011-07-28 08:37:59 -0600134/*
135 * This is the master list of all GPU cores that are supported by this
136 * driver.
137 */
138
139#define ANY_ID (~0)
140
141static const struct {
142 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600143 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600144 const char *pm4fw;
145 const char *pfpfw;
146 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700147 unsigned int istore_size;
148 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700149 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530150 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600151} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600152 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700153 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530154 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530155 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
156 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530157 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600158 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700159 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530160 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600161 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700162 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530163 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600164 /*
165 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
166 * a hardware problem.
167 */
168 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700169 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530170 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700171 { ADRENO_REV_A225, 2, 2, 0, 6,
172 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530173 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600174 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700175 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530176 1536, 768, 3, SZ_512K },
177 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530178 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530179 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
180 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700181 /* A3XX doesn't use the pix_shader_start */
Jordan Croused2b30d22012-05-21 08:41:51 -0600182 { ADRENO_REV_A320, 3, 2, 0, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700183 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530184 512, 0, 2, SZ_512K },
liu zhongfd42e622012-05-01 19:18:30 -0700185 { ADRENO_REV_A330, 3, 3, 0, 0,
186 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
187 512, 0, 2, SZ_1M },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600188};
189
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600190static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191{
Jordan Crousea78c9172011-07-11 13:14:09 -0600192 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600193 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
Jordan Crousea78c9172011-07-11 13:14:09 -0600195 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196
197 if (device->requested_state == KGSL_STATE_NONE) {
198 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700199 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 queue_work(device->work_queue, &device->idle_check_ws);
201 } else if (device->pwrscale.policy != NULL) {
202 queue_work(device->work_queue, &device->idle_check_ws);
203 }
204 }
205
206 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800207 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 jiffies + device->pwrctrl.interval_timeout);
209 return result;
210}
211
Jordan Crouse9f739212011-07-28 08:37:57 -0600212static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 struct kgsl_pagetable *pagetable)
214{
215 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
216 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
217
218 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
219
220 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
221
222 kgsl_mmu_unmap(pagetable, &device->memstore);
223
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600224 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225}
226
227static int adreno_setup_pt(struct kgsl_device *device,
228 struct kgsl_pagetable *pagetable)
229{
230 int result = 0;
231 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
232 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
235 GSL_PT_PAGE_RV);
236 if (result)
237 goto error;
238
239 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
240 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
241 if (result)
242 goto unmap_buffer_desc;
243
244 result = kgsl_mmu_map_global(pagetable, &device->memstore,
245 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
246 if (result)
247 goto unmap_memptrs_desc;
248
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600249 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
251 if (result)
252 goto unmap_memstore_desc;
253
254 return result;
255
256unmap_memstore_desc:
257 kgsl_mmu_unmap(pagetable, &device->memstore);
258
259unmap_memptrs_desc:
260 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
261
262unmap_buffer_desc:
263 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
264
265error:
266 return result;
267}
268
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600269static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600270 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600271 uint32_t flags)
272{
273 unsigned int pt_val, reg_pt_val;
274 unsigned int link[200];
275 unsigned int *cmds = &link[0];
276 int sizedwords = 0;
277 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
278 struct kgsl_memdesc **reg_map_desc;
Pu Chened8cbb52012-06-04 18:18:48 -0700279 void *reg_map_array = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600280 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600281 struct kgsl_context *context;
282 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600283
284 if (!adreno_dev->drawctxt_active)
285 return kgsl_mmu_device_setstate(&device->mmu, flags);
286 num_iommu_units = kgsl_mmu_get_reg_map_desc(&device->mmu,
287 &reg_map_array);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600288
289 context = idr_find(&device->context_idr, context_id);
290 adreno_ctx = context->devctxt;
291
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600292 reg_map_desc = reg_map_array;
293
294 if (kgsl_mmu_enable_clk(&device->mmu,
295 KGSL_IOMMU_CONTEXT_USER))
296 goto done;
297
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600298 cmds += __adreno_add_idle_indirect_cmds(cmds,
299 device->mmu.setstate_memory.gpuaddr +
300 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
301
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600302 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600303 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
304 device->mmu.setstate_memory.gpuaddr +
305 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
306 else
307 cmds += adreno_add_bank_change_cmds(cmds,
308 KGSL_IOMMU_CONTEXT_USER,
309 device->mmu.setstate_memory.gpuaddr +
310 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
311
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700312 pt_val = kgsl_mmu_pt_get_base_addr(device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600313 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600314 /*
315 * We need to perfrom the following operations for all
316 * IOMMU units
317 */
318 for (i = 0; i < num_iommu_units; i++) {
319 reg_pt_val = (pt_val &
320 (KGSL_IOMMU_TTBR0_PA_MASK <<
321 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
322 kgsl_mmu_get_pt_lsb(&device->mmu, i,
323 KGSL_IOMMU_CONTEXT_USER);
324 /*
325 * Set address of the new pagetable by writng to IOMMU
326 * TTBR0 register
327 */
328 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
329 *cmds++ = reg_map_desc[i]->gpuaddr +
330 (KGSL_IOMMU_CONTEXT_USER <<
331 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0;
332 *cmds++ = reg_pt_val;
333 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
334 *cmds++ = 0x00000000;
335
336 /*
337 * Read back the ttbr0 register as a barrier to ensure
338 * above writes have completed
339 */
340 cmds += adreno_add_read_cmds(device, cmds,
341 reg_map_desc[i]->gpuaddr +
342 (KGSL_IOMMU_CONTEXT_USER <<
343 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
344 reg_pt_val,
345 device->mmu.setstate_memory.gpuaddr +
346 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600347 }
348 /* invalidate all base pointers */
349 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
350 *cmds++ = 0x7fff;
351
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600352 cmds += __adreno_add_idle_indirect_cmds(cmds,
353 device->mmu.setstate_memory.gpuaddr +
354 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600355 }
356 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
357 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700358 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600359 */
360 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700361 reg_pt_val = (pt_val &
362 (KGSL_IOMMU_TTBR0_PA_MASK <<
363 KGSL_IOMMU_TTBR0_PA_SHIFT)) +
364 kgsl_mmu_get_pt_lsb(&device->mmu, i,
365 KGSL_IOMMU_CONTEXT_USER);
366
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600367 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
368 *cmds++ = (reg_map_desc[i]->gpuaddr +
369 (KGSL_IOMMU_CONTEXT_USER <<
370 KGSL_IOMMU_CTX_SHIFT) +
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700371 KGSL_IOMMU_CTX_TLBIALL);
372 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600373
374 cmds += __adreno_add_idle_indirect_cmds(cmds,
375 device->mmu.setstate_memory.gpuaddr +
376 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
377
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600378 cmds += adreno_add_read_cmds(device, cmds,
379 reg_map_desc[i]->gpuaddr +
380 (KGSL_IOMMU_CONTEXT_USER <<
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700381 KGSL_IOMMU_CTX_SHIFT) + KGSL_IOMMU_TTBR0,
382 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600383 device->mmu.setstate_memory.gpuaddr +
384 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
385 }
386 }
387
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600388 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600389 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
390 reg_map_desc[num_iommu_units - 1]->gpuaddr - PAGE_SIZE,
391 device->mmu.setstate_memory.gpuaddr +
392 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
393 else
394 cmds += adreno_add_bank_change_cmds(cmds,
395 KGSL_IOMMU_CONTEXT_PRIV,
396 device->mmu.setstate_memory.gpuaddr +
397 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
398
399 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600400 if (sizedwords) {
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600401 /*
402 * add an interrupt at the end of commands so that the smmu
403 * disable clock off function will get called
404 */
405 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
406 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
407 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600408 /* This returns the per context timestamp but we need to
409 * use the global timestamp for iommu clock disablement */
410 adreno_ringbuffer_issuecmds(device, adreno_ctx,
411 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600412 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600413 kgsl_mmu_disable_clk_on_ts(&device->mmu,
414 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600415 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600416done:
417 if (num_iommu_units)
418 kfree(reg_map_array);
419}
420
421static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600422 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600423 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424{
425 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
426 unsigned int link[32];
427 unsigned int *cmds = &link[0];
428 int sizedwords = 0;
429 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600430 struct kgsl_context *context;
431 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600433 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530434 * Fix target freeze issue by adding TLB flush for each submit
435 * on A20X based targets.
436 */
437 if (adreno_is_a20x(adreno_dev))
438 flags |= KGSL_MMUFLAGS_TLBFLUSH;
439 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600440 * If possible, then set the state via the command stream to avoid
441 * a CPU idle. Otherwise, use the default setstate which uses register
442 * writes For CFF dump we must idle and use the registers so that it is
443 * easier to filter out the mmu accesses from the dump
444 */
445 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600446 context = idr_find(&device->context_idr, context_id);
447 adreno_ctx = context->devctxt;
448
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
450 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600451 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 *cmds++ = 0x00000000;
453
454 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600455 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600456 *cmds++ = kgsl_mmu_pt_get_base_addr(
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600457 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 sizedwords += 4;
459 }
460
461 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
462 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600463 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 1);
465 *cmds++ = 0x00000000;
466 sizedwords += 2;
467 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600468 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 *cmds++ = mh_mmu_invalidate;
470 sizedwords += 2;
471 }
472
473 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600474 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 /* HW workaround: to resolve MMU page fault interrupts
476 * caused by the VGT.It prevents the CP PFP from filling
477 * the VGT DMA request fifo too early,thereby ensuring
478 * that the VGT will not fetch vertex/bin data until
479 * after the page table base register has been updated.
480 *
481 * Two null DRAW_INDX_BIN packets are inserted right
482 * after the page table base update, followed by a
483 * wait for idle. The null packets will fill up the
484 * VGT DMA request fifo and prevent any further
485 * vertex/bin updates from occurring until the wait
486 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600487 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488 *cmds++ = (0x4 << 16) |
489 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
490 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600491 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600492 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600493 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494 *cmds++ = 0; /* viz query info */
495 *cmds++ = 0x0003C004; /* draw indicator */
496 *cmds++ = 0; /* bin base */
497 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600498 *cmds++ =
499 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600501 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 *cmds++ = 0; /* viz query info */
503 *cmds++ = 0x0003C004; /* draw indicator */
504 *cmds++ = 0; /* bin base */
505 *cmds++ = 3; /* bin size */
506 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600507 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600509 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 *cmds++ = 0x00000000;
511 sizedwords += 21;
512 }
513
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600514
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600516 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 *cmds++ = 0x7fff; /* invalidate all base pointers */
518 sizedwords += 2;
519 }
520
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600521 adreno_ringbuffer_issuecmds(device, adreno_ctx,
522 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600524 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600525 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600526 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527}
528
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600529static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600530 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600531 uint32_t flags)
532{
533 /* call the mmu specific handler */
534 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600535 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600536 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600537 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600538}
539
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700541a3xx_getchipid(struct kgsl_device *device)
542{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600543 struct kgsl_device_platform_data *pdata =
544 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700545
Jordan Crouse54154c62012-03-27 16:33:26 -0600546 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600547 * All current A3XX chipids are detected at the SOC level. Leave this
548 * function here to support any future GPUs that have working
549 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600550 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700551
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600552 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700553}
554
555static unsigned int
556a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557{
558 unsigned int chipid = 0;
559 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600560 struct kgsl_device_platform_data *pdata =
561 kgsl_device_get_drvdata(device);
562
563 /* If the chip id is set at the platform level, then just use that */
564
565 if (pdata->chipid != 0)
566 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567
568 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
569 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
570 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
571
572 /*
573 * adreno 22x gpus are indicated by coreid 2,
574 * but REG_RBBM_PERIPHID1 always contains 0 for this field
575 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600576 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577 chipid = 2 << 24;
578 else
579 chipid = (coreid & 0xF) << 24;
580
581 chipid |= ((majorid >> 4) & 0xF) << 16;
582
583 minorid = ((revid >> 0) & 0xFF);
584
585 patchid = ((revid >> 16) & 0xFF);
586
587 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530588 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589 if (cpu_is_qsd8x50())
590 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530591 else if (cpu_is_msm8625() && minorid == 0)
592 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593
594 chipid |= (minorid << 8) | patchid;
595
596 return chipid;
597}
598
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700599static unsigned int
600adreno_getchipid(struct kgsl_device *device)
601{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600602 struct kgsl_device_platform_data *pdata =
603 kgsl_device_get_drvdata(device);
604
605 /*
606 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
607 * an A2XX processor
608 */
609
610 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700611 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600612 else
613 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700614}
615
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616static inline bool _rev_match(unsigned int id, unsigned int entry)
617{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600618 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620
621static void
622adreno_identify_gpu(struct adreno_device *adreno_dev)
623{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600624 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625
626 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
627
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600628 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
629 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
630 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
631 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632
Jordan Crouse505df9c2011-07-28 08:37:59 -0600633 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
634 if (core == adreno_gpulist[i].core &&
635 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600636 _rev_match(minor, adreno_gpulist[i].minor) &&
637 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 }
640
Jordan Crouse505df9c2011-07-28 08:37:59 -0600641 if (i == ARRAY_SIZE(adreno_gpulist)) {
642 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
643 return;
644 }
645
646 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
647 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
648 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
649 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700650 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
651 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700652 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600653 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654}
655
Lokesh Batra805e1e12012-08-03 08:34:06 -0600656static struct platform_device_id adreno_id_table[] = {
657 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
658 {},
659};
660
661MODULE_DEVICE_TABLE(platform, adreno_id_table);
662
663static struct of_device_id adreno_match_table[] = {
664 { .compatible = "qcom,kgsl-3d0", },
665 {}
666};
667
668static inline int adreno_of_read_property(struct device_node *node,
669 const char *prop, unsigned int *ptr)
670{
671 int ret = of_property_read_u32(node, prop, ptr);
672 if (ret)
673 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
674 return ret;
675}
676
677static struct device_node *adreno_of_find_subnode(struct device_node *parent,
678 const char *name)
679{
680 struct device_node *child;
681
682 for_each_child_of_node(parent, child) {
683 if (of_device_is_compatible(child, name))
684 return child;
685 }
686
687 return NULL;
688}
689
690static int adreno_of_get_pwrlevels(struct device_node *parent,
691 struct kgsl_device_platform_data *pdata)
692{
693 struct device_node *node, *child;
694 int ret = -EINVAL;
695
696 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
697
698 if (node == NULL) {
699 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
700 return -EINVAL;
701 }
702
703 pdata->num_levels = 0;
704
705 for_each_child_of_node(node, child) {
706 unsigned int index;
707 struct kgsl_pwrlevel *level;
708
709 if (adreno_of_read_property(child, "reg", &index))
710 goto done;
711
712 if (index >= KGSL_MAX_PWRLEVELS) {
713 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
714 index);
715 continue;
716 }
717
718 if (index >= pdata->num_levels)
719 pdata->num_levels = index + 1;
720
721 level = &pdata->pwrlevel[index];
722
723 if (adreno_of_read_property(child, "qcom,gpu-freq",
724 &level->gpu_freq))
725 goto done;
726
727 if (adreno_of_read_property(child, "qcom,bus-freq",
728 &level->bus_freq))
729 goto done;
730
731 if (adreno_of_read_property(child, "qcom,io-fraction",
732 &level->io_fraction))
733 level->io_fraction = 0;
734 }
735
736 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
737 &pdata->init_level))
738 pdata->init_level = 1;
739
740 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
741 KGSL_CORE_ERR("Initial power level out of range\n");
742 pdata->init_level = 1;
743 }
744
745 ret = 0;
746done:
747 return ret;
748
749}
750static void adreno_of_free_bus_scale_info(struct msm_bus_scale_pdata *pdata)
751{
752 int i;
753
754 if (pdata == NULL)
755 return;
756
757 for (i = 0; pdata->usecase && i < pdata->num_usecases; i++)
758 kfree(pdata->usecase[i].vectors);
759
760 kfree(pdata->usecase);
761 kfree(pdata);
762}
763
764struct msm_bus_scale_pdata *adreno_of_get_bus_scale(struct device_node *node)
765{
766 static int bus_vectors_src[3] = {MSM_BUS_MASTER_GRAPHICS_3D,
767 MSM_BUS_MASTER_GRAPHICS_3D_PORT1, MSM_BUS_MASTER_V_OCMEM_GFX3D};
768 static int bus_vectors_dst[2] = {MSM_BUS_SLAVE_EBI_CH0,
769 MSM_BUS_SLAVE_OCMEM};
770 const unsigned int *vectors;
771 struct msm_bus_scale_pdata *pdata;
772 int i, j, len, num_paths;
773 int ret = -EINVAL;
774
775 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
776
777 if (!pdata) {
778 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
779 return ERR_PTR(-ENOMEM);
780 }
781
782 if (adreno_of_read_property(node, "qcom,grp3d-num-bus-scale-usecases",
783 &pdata->num_usecases)) {
784 pdata->num_usecases = 0;
785 goto err;
786 }
787
788 pdata->usecase = kzalloc(pdata->num_usecases *
789 sizeof(struct msm_bus_paths), GFP_KERNEL);
790
791 if (pdata->usecase == NULL) {
792 KGSL_CORE_ERR("kzalloc (%d) failed\n",
793 pdata->num_usecases * sizeof(struct msm_bus_paths));
794 ret = -ENOMEM;
795 goto err;
796 }
797
798 if (adreno_of_read_property(node, "qcom,grp3d-num-vectors-per-usecase",
799 &num_paths))
800 goto err;
801
802 vectors = of_get_property(node, "qcom,grp3d-vectors", &len);
803
804 if (len != pdata->num_usecases * num_paths *
805 sizeof(struct msm_bus_vectors)) {
806 KGSL_CORE_ERR("Invalid size for the bus scale vectors\n");
807 goto err;
808 }
809
810 for (i = 0; i < pdata->num_usecases; i++) {
811 pdata->usecase[i].num_paths = num_paths;
812 pdata->usecase[i].vectors = kzalloc(num_paths *
813 sizeof(struct msm_bus_vectors),
814 GFP_KERNEL);
815 if (!pdata->usecase[i].vectors) {
816 KGSL_CORE_ERR("kzalloc(%d) failed\n",
817 num_paths * sizeof(struct msm_bus_vectors));
818 ret = -ENOMEM;
819 goto err;
820 }
821 for (j = 0; j < num_paths; j++) {
822 int index = (i * num_paths + j) * 4;
823 pdata->usecase[i].vectors[j].src =
824 bus_vectors_src[be32_to_cpu(vectors[index])];
825 pdata->usecase[i].vectors[j].dst =
826 bus_vectors_dst[
827 be32_to_cpu(vectors[index + 1])];
828 pdata->usecase[i].vectors[j].ab =
829 be32_to_cpu(vectors[index + 2]);
830 pdata->usecase[i].vectors[j].ib =
831 KGSL_CONVERT_TO_MBPS(
832 be32_to_cpu(vectors[index + 3]));
833 }
834 }
835
836 pdata->name = "grp3d";
837
838 return pdata;
839
840err:
841 adreno_of_free_bus_scale_info(pdata);
842
843 return ERR_PTR(ret);
844}
845
846static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
847{
848 struct device_node *node, *child;
849 struct msm_dcvs_core_info *info = NULL;
850 int count = 0;
851 int ret = -EINVAL;
852
853 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
854 if (node == NULL)
855 return ERR_PTR(-EINVAL);
856
857 info = kzalloc(sizeof(*info), GFP_KERNEL);
858
859 if (info == NULL) {
860 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
861 ret = -ENOMEM;
862 goto err;
863 }
864
865 for_each_child_of_node(node, child)
866 count++;
867
868 info->core_param.num_freq = count;
869
870 info->freq_tbl = kzalloc(info->core_param.num_freq *
871 sizeof(struct msm_dcvs_freq_entry),
872 GFP_KERNEL);
873
874 if (info->freq_tbl == NULL) {
875 KGSL_CORE_ERR("kzalloc(%d) failed\n",
876 info->core_param.num_freq *
877 sizeof(struct msm_dcvs_freq_entry));
878 ret = -ENOMEM;
879 goto err;
880 }
881
882 for_each_child_of_node(node, child) {
883 unsigned int index;
884
885 if (adreno_of_read_property(child, "reg", &index))
886 goto err;
887
888 if (index >= info->core_param.num_freq) {
889 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
890 index);
891 continue;
892 }
893
894 if (adreno_of_read_property(child, "qcom,freq",
895 &info->freq_tbl[index].freq))
896 goto err;
897
898 if (adreno_of_read_property(child, "qcom,idle-energy",
899 &info->freq_tbl[index].idle_energy))
900 info->freq_tbl[index].idle_energy = 0;
901
902 if (adreno_of_read_property(child, "qcom,active-energy",
903 &info->freq_tbl[index].active_energy))
904 info->freq_tbl[index].active_energy = 0;
905 }
906
907 if (adreno_of_read_property(node, "qcom,core-max-time-us",
908 &info->core_param.max_time_us))
909 goto err;
910
911 if (adreno_of_read_property(node, "qcom,algo-slack-time-us",
912 &info->algo_param.slack_time_us))
913 goto err;
914
915 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
916 &info->algo_param.disable_pc_threshold))
917 goto err;
918
919 if (adreno_of_read_property(node, "qcom,algo-ss-window-size",
920 &info->algo_param.ss_window_size))
921 goto err;
922
923 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
924 &info->algo_param.ss_util_pct))
925 goto err;
926
927 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
928 &info->algo_param.em_max_util_pct))
929 goto err;
930
931 if (adreno_of_read_property(node, "qcom,algo-ss-iobusy-conv",
932 &info->algo_param.ss_iobusy_conv))
933 goto err;
934
935 return info;
936
937err:
938 if (info)
939 kfree(info->freq_tbl);
940
941 kfree(info);
942
943 return ERR_PTR(ret);
944}
945
946static int adreno_of_get_iommu(struct device_node *parent,
947 struct kgsl_device_platform_data *pdata)
948{
949 struct device_node *node, *child;
950 struct kgsl_device_iommu_data *data = NULL;
951 struct kgsl_iommu_ctx *ctxs = NULL;
952 u32 reg_val[2];
953 int ctx_index = 0;
954
955 node = of_parse_phandle(parent, "iommu", 0);
956 if (node == NULL)
957 return -EINVAL;
958
959 data = kzalloc(sizeof(*data), GFP_KERNEL);
960 if (data == NULL) {
961 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
962 goto err;
963 }
964
965 if (of_property_read_u32_array(node, "reg", reg_val, 2))
966 goto err;
967
968 data->physstart = reg_val[0];
969 data->physend = data->physstart + reg_val[1] - 1;
970
971 data->iommu_ctx_count = 0;
972
973 for_each_child_of_node(node, child)
974 data->iommu_ctx_count++;
975
976 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
977 GFP_KERNEL);
978
979 if (ctxs == NULL) {
980 KGSL_CORE_ERR("kzalloc(%d) failed\n",
981 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
982 goto err;
983 }
984
985 for_each_child_of_node(node, child) {
986 int ret = of_property_read_string(child, "label",
987 &ctxs[ctx_index].iommu_ctx_name);
988
989 if (ret) {
990 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
991 goto err;
992 }
993
994 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
995 &ctxs[ctx_index].ctx_id))
996 goto err;
997
998 ctx_index++;
999 }
1000
1001 data->iommu_ctxs = ctxs;
1002
1003 pdata->iommu_data = data;
1004 pdata->iommu_count = 1;
1005
1006 return 0;
1007
1008err:
1009 kfree(ctxs);
1010 kfree(data);
1011
1012 return -EINVAL;
1013}
1014
1015static int adreno_of_get_pdata(struct platform_device *pdev)
1016{
1017 struct kgsl_device_platform_data *pdata = NULL;
1018 struct kgsl_device *device;
1019 int ret = -EINVAL;
1020
1021 pdev->id_entry = adreno_id_table;
1022
1023 pdata = pdev->dev.platform_data;
1024 if (pdata)
1025 return 0;
1026
1027 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1028 KGSL_CORE_ERR("Unable to read 'label'\n");
1029 goto err;
1030 }
1031
1032 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1033 goto err;
1034
1035 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1036 if (pdata == NULL) {
1037 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1038 ret = -ENOMEM;
1039 goto err;
1040 }
1041
1042 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1043 &pdata->chipid))
1044 goto err;
1045
1046 /* pwrlevel Data */
1047 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1048 if (ret)
1049 goto err;
1050
1051 /* Default value is 83, if not found in DT */
1052 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1053 &pdata->idle_timeout))
1054 pdata->idle_timeout = 83;
1055
1056 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1057 &pdata->nap_allowed))
1058 pdata->nap_allowed = 1;
1059
1060 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1061 &pdata->clk_map))
1062 goto err;
1063
1064 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1065
1066 if (device->id != KGSL_DEVICE_3D0)
1067 goto err;
1068
1069 /* Bus Scale Data */
1070
1071 pdata->bus_scale_table = adreno_of_get_bus_scale(pdev->dev.of_node);
1072 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1073 ret = PTR_ERR(pdata->bus_scale_table);
1074 goto err;
1075 }
1076
1077 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1078 if (IS_ERR_OR_NULL(pdata->core_info)) {
1079 ret = PTR_ERR(pdata->core_info);
1080 goto err;
1081 }
1082
1083 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1084 if (ret)
1085 goto err;
1086
1087 pdev->dev.platform_data = pdata;
1088 return 0;
1089
1090err:
1091 if (pdata) {
1092 adreno_of_free_bus_scale_info(pdata->bus_scale_table);
1093 if (pdata->core_info)
1094 kfree(pdata->core_info->freq_tbl);
1095 kfree(pdata->core_info);
1096
1097 if (pdata->iommu_data)
1098 kfree(pdata->iommu_data->iommu_ctxs);
1099
1100 kfree(pdata->iommu_data);
1101 }
1102
1103 kfree(pdata);
1104
1105 return ret;
1106}
1107
liu zhong7dfa2a32012-04-27 19:11:01 -07001108#ifdef CONFIG_MSM_OCMEM
1109static int
1110adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1111{
1112 if (adreno_dev->gpurev != ADRENO_REV_A330)
1113 return 0;
1114
1115 /* OCMEM is only needed once, do not support consective allocation */
1116 if (adreno_dev->ocmem_hdl != NULL)
1117 return 0;
1118
1119 adreno_dev->ocmem_hdl =
1120 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1121 if (adreno_dev->ocmem_hdl == NULL)
1122 return -ENOMEM;
1123
1124 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
1125 adreno_dev->gmem_base = adreno_dev->ocmem_hdl->addr;
1126
1127 return 0;
1128}
1129
1130static void
1131adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1132{
1133 if (adreno_dev->gpurev != ADRENO_REV_A330)
1134 return;
1135
1136 if (adreno_dev->ocmem_hdl == NULL)
1137 return;
1138
1139 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1140 adreno_dev->ocmem_hdl = NULL;
1141}
1142#else
1143static int
1144adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1145{
1146 return 0;
1147}
1148
1149static void
1150adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1151{
1152}
1153#endif
1154
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155static int __devinit
1156adreno_probe(struct platform_device *pdev)
1157{
1158 struct kgsl_device *device;
1159 struct adreno_device *adreno_dev;
1160 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001161 bool is_dt;
1162
1163 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1164
1165 if (is_dt && pdev->dev.of_node) {
1166 status = adreno_of_get_pdata(pdev);
1167 if (status)
1168 goto error_return;
1169 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001170
1171 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1172 adreno_dev = ADRENO_DEVICE(device);
1173 device->parentdev = &pdev->dev;
1174
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175 status = adreno_ringbuffer_init(device);
1176 if (status != 0)
1177 goto error;
1178
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001179 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001180 if (status)
1181 goto error_close_rb;
1182
1183 adreno_debugfs_init(device);
1184
1185 kgsl_pwrscale_init(device);
1186 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1187
1188 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1189 return 0;
1190
1191error_close_rb:
1192 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1193error:
1194 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001195error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196 return status;
1197}
1198
1199static int __devexit adreno_remove(struct platform_device *pdev)
1200{
1201 struct kgsl_device *device;
1202 struct adreno_device *adreno_dev;
1203
1204 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1205 adreno_dev = ADRENO_DEVICE(device);
1206
1207 kgsl_pwrscale_detach_policy(device);
1208 kgsl_pwrscale_close(device);
1209
1210 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1211 kgsl_device_platform_remove(device);
1212
1213 return 0;
1214}
1215
1216static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1217{
1218 int status = -EINVAL;
1219 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001221 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1222 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223
1224 /* Power up the device */
1225 kgsl_pwrctrl_enable(device);
1226
1227 /* Identify the specific GPU */
1228 adreno_identify_gpu(adreno_dev);
1229
Jordan Crouse505df9c2011-07-28 08:37:59 -06001230 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1231 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1232 adreno_dev->chip_id);
1233 goto error_clk_off;
1234 }
1235
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001236 /* Set up the MMU */
1237 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001238 /*
1239 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1240 * on older gpus
1241 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001242 if (adreno_is_a20x(adreno_dev)) {
1243 device->mh.mh_intf_cfg1 = 0;
1244 device->mh.mh_intf_cfg2 = 0;
1245 }
1246
1247 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001248 }
1249
Tarun Karra3335f142012-06-19 14:11:48 -07001250 /* Assign correct RBBM status register to hang detect regs
1251 */
1252 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1253
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001254 status = kgsl_mmu_start(device);
1255 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001256 goto error_clk_off;
1257
liu zhong7dfa2a32012-04-27 19:11:01 -07001258 status = adreno_ocmem_gmem_malloc(adreno_dev);
1259 if (status) {
1260 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1261 goto error_mmu_off;
1262 }
1263
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001264 /* Start the GPU */
1265 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001266
1267 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001268 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269
1270 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001271 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001272 /* While recovery is on we do not want timer to
1273 * fire and attempt to change any device state */
1274 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1275 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001276 return 0;
1277 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001279 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001280
1281error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001282 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001283
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001284error_clk_off:
1285 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286
1287 return status;
1288}
1289
1290static int adreno_stop(struct kgsl_device *device)
1291{
1292 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1293
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294 adreno_dev->drawctxt_active = NULL;
1295
1296 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1297
Shubhraprakash Das79447952012-04-26 18:12:23 -06001298 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001299
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001300 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301301 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001302 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001303
liu zhong7dfa2a32012-04-27 19:11:01 -07001304 adreno_ocmem_gmem_free(adreno_dev);
1305
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306 /* Power down the device */
1307 kgsl_pwrctrl_disable(device);
1308
1309 return 0;
1310}
1311
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001312static void adreno_mark_context_status(struct kgsl_device *device,
1313 int recovery_status)
1314{
1315 struct kgsl_context *context;
1316 int next = 0;
1317 /*
1318 * Set the reset status of all contexts to
1319 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1320 * since thats the guilty party, if recovery failed then
1321 * mark all as guilty
1322 */
1323 while ((context = idr_get_next(&device->context_idr, &next))) {
1324 struct adreno_context *adreno_context = context->devctxt;
1325 if (recovery_status) {
1326 context->reset_status =
1327 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1328 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1329 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1330 context->reset_status) {
1331 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1332 CTXT_FLAGS_GPU_HANG_RECOVERED))
1333 context->reset_status =
1334 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1335 else
1336 context->reset_status =
1337 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1338 }
1339 next = next + 1;
1340 }
1341}
1342
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001343static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1344{
1345 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1346 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1347 struct kgsl_context *context;
1348 struct adreno_context *temp_adreno_context;
1349 int next = 0;
1350
1351 while ((context = idr_get_next(&device->context_idr, &next))) {
1352 temp_adreno_context = context->devctxt;
1353 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1354 kgsl_sharedmem_writel(&device->memstore,
1355 KGSL_MEMSTORE_OFFSET(context->id,
1356 soptimestamp),
1357 rb->timestamp[context->id]);
1358 kgsl_sharedmem_writel(&device->memstore,
1359 KGSL_MEMSTORE_OFFSET(context->id,
1360 eoptimestamp),
1361 rb->timestamp[context->id]);
1362 }
1363 next = next + 1;
1364 }
1365}
1366
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001367static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1368{
1369 vfree(rec_data->rb_buffer);
1370 vfree(rec_data->bad_rb_buffer);
1371}
1372
1373static int adreno_setup_recovery_data(struct kgsl_device *device,
1374 struct adreno_recovery_data *rec_data)
1375{
1376 int ret = 0;
1377 unsigned int ib1_sz, ib2_sz;
1378 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1379 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1380
1381 memset(rec_data, 0, sizeof(*rec_data));
1382
1383 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1384 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1385 if (ib1_sz || ib2_sz)
1386 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1387
1388 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1389 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1390 current_context));
1391
1392 kgsl_sharedmem_readl(&device->memstore,
1393 &rec_data->global_eop,
1394 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1395 eoptimestamp));
1396
1397 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1398 if (!rec_data->rb_buffer) {
1399 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1400 rb->buffer_desc.size);
1401 return -ENOMEM;
1402 }
1403
1404 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1405 if (!rec_data->bad_rb_buffer) {
1406 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1407 rb->buffer_desc.size);
1408 ret = -ENOMEM;
1409 goto done;
1410 }
1411
1412done:
1413 if (ret) {
1414 vfree(rec_data->rb_buffer);
1415 vfree(rec_data->bad_rb_buffer);
1416 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417 return ret;
1418}
1419
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001420static int
1421_adreno_recover_hang(struct kgsl_device *device,
1422 struct adreno_recovery_data *rec_data,
1423 bool try_bad_commands)
1424{
1425 int ret;
1426 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1427 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1428 struct kgsl_context *context;
1429 struct adreno_context *adreno_context = NULL;
1430 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1431
1432 context = idr_find(&device->context_idr, rec_data->context_id);
1433 if (context == NULL) {
1434 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1435 rec_data->context_id);
1436 } else {
1437 adreno_context = context->devctxt;
1438 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1439 }
1440
1441 /* Extract valid contents from rb which can still be executed after
1442 * hang */
1443 ret = adreno_ringbuffer_extract(rb, rec_data);
1444 if (ret)
1445 goto done;
1446
1447 /* restart device */
1448 ret = adreno_stop(device);
1449 if (ret) {
1450 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1451 goto done;
1452 }
1453
1454 ret = adreno_start(device, true);
1455 if (ret) {
1456 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1457 goto done;
1458 }
1459
1460 if (context)
1461 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1462 KGSL_MEMSTORE_GLOBAL);
1463
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001464 /* If iommu is used then we need to make sure that the iommu clocks
1465 * are on since there could be commands in pipeline that touch iommu */
1466 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1467 ret = kgsl_mmu_enable_clk(&device->mmu,
1468 KGSL_IOMMU_CONTEXT_USER);
1469 if (ret)
1470 goto done;
1471 }
1472
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001473 /* Do not try the bad caommands if recovery has failed bad commands
1474 * once already */
1475 if (!try_bad_commands)
1476 rec_data->bad_rb_size = 0;
1477
1478 if (rec_data->bad_rb_size) {
1479 int idle_ret;
1480 /* submit the bad and good context commands and wait for
1481 * them to pass */
1482 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1483 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001484 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001485 if (idle_ret) {
1486 ret = adreno_stop(device);
1487 if (ret) {
1488 KGSL_DRV_ERR(device,
1489 "Device stop failed in recovery\n");
1490 goto done;
1491 }
1492 ret = adreno_start(device, true);
1493 if (ret) {
1494 KGSL_DRV_ERR(device,
1495 "Device start failed in recovery\n");
1496 goto done;
1497 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001498 if (context)
1499 kgsl_mmu_setstate(&device->mmu,
1500 adreno_context->pagetable,
1501 KGSL_MEMSTORE_GLOBAL);
1502
1503 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1504 ret = kgsl_mmu_enable_clk(&device->mmu,
1505 KGSL_IOMMU_CONTEXT_USER);
1506 if (ret)
1507 goto done;
1508 }
1509
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001510 ret = idle_ret;
1511 KGSL_DRV_ERR(device,
1512 "Bad context commands hung in recovery\n");
1513 } else {
1514 KGSL_DRV_ERR(device,
1515 "Bad context commands succeeded in recovery\n");
1516 if (adreno_context)
1517 adreno_context->flags = (adreno_context->flags &
1518 ~CTXT_FLAGS_GPU_HANG) |
1519 CTXT_FLAGS_GPU_HANG_RECOVERED;
1520 adreno_dev->drawctxt_active = last_active_ctx;
1521 }
1522 }
1523 /* If either the bad command sequence failed or we did not play it */
1524 if (ret || !rec_data->bad_rb_size) {
1525 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1526 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001527 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001528 if (ret) {
1529 /* If we fail here we can try to invalidate another
1530 * context and try recovering again */
1531 ret = -EAGAIN;
1532 goto done;
1533 }
1534 /* ringbuffer now has data from the last valid context id,
1535 * so restore the active_ctx to the last valid context */
1536 if (rec_data->last_valid_ctx_id) {
1537 struct kgsl_context *last_ctx =
1538 idr_find(&device->context_idr,
1539 rec_data->last_valid_ctx_id);
1540 if (last_ctx)
1541 adreno_dev->drawctxt_active = last_ctx->devctxt;
1542 }
1543 }
1544done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001545 /* Turn off iommu clocks */
1546 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1547 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001548 return ret;
1549}
1550
1551static int
1552adreno_recover_hang(struct kgsl_device *device,
1553 struct adreno_recovery_data *rec_data)
1554{
1555 int ret = 0;
1556 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1557 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1558 unsigned int timestamp;
1559
1560 KGSL_DRV_ERR(device,
1561 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1562 "Bad context_id: %u, global_eop: 0x%x\n",
1563 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1564
1565 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1566 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1567
1568 /* We may need to replay commands multiple times based on whether
1569 * multiple contexts hang the GPU */
1570 while (true) {
1571 if (!ret)
1572 ret = _adreno_recover_hang(device, rec_data, true);
1573 else
1574 ret = _adreno_recover_hang(device, rec_data, false);
1575
1576 if (-EAGAIN == ret) {
1577 /* setup new recovery parameters and retry, this
1578 * means more than 1 contexts are causing hang */
1579 adreno_destroy_recovery_data(rec_data);
1580 adreno_setup_recovery_data(device, rec_data);
1581 KGSL_DRV_ERR(device,
1582 "Retry recovery from 3D GPU hang. Recovery parameters: "
1583 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1584 rec_data->ib1, rec_data->context_id,
1585 rec_data->global_eop);
1586 } else {
1587 break;
1588 }
1589 }
1590
1591 if (ret)
1592 goto done;
1593
1594 /* Restore correct states after recovery */
1595 if (adreno_dev->drawctxt_active)
1596 device->mmu.hwpagetable =
1597 adreno_dev->drawctxt_active->pagetable;
1598 else
1599 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1600 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1601 kgsl_sharedmem_writel(&device->memstore,
1602 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1603 eoptimestamp),
1604 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1605done:
1606 adreno_set_max_ts_for_bad_ctxs(device);
1607 adreno_mark_context_status(device, ret);
1608 if (!ret)
1609 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1610 else
1611 KGSL_DRV_ERR(device, "Recovery failed\n");
1612 return ret;
1613}
1614
1615int
1616adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001617{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001618 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001619 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001620
1621 if (device->state == KGSL_STATE_HUNG)
1622 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001623 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001624 mutex_unlock(&device->mutex);
1625 wait_for_completion(&device->recovery_gate);
1626 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001627 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001628 result = 0;
1629 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001630 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001631 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001632 /* Detected a hang */
1633
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001634 /* Get the recovery data as soon as hang is detected */
1635 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001636 /*
1637 * Trigger an automatic dump of the state to
1638 * the console
1639 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001640 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001641
1642 /*
1643 * Make a GPU snapshot. For now, do it after the PM dump so we
1644 * can at least be sure the PM dump will work as it always has
1645 */
1646 kgsl_device_snapshot(device, 1);
1647
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001648 result = adreno_recover_hang(device, &rec_data);
1649 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001650 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001651 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001652 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001653 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001654 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1655 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001656 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001657 }
1658done:
1659 return result;
1660}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001661EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001662
1663static int adreno_getproperty(struct kgsl_device *device,
1664 enum kgsl_property_type type,
1665 void *value,
1666 unsigned int sizebytes)
1667{
1668 int status = -EINVAL;
1669 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1670
1671 switch (type) {
1672 case KGSL_PROP_DEVICE_INFO:
1673 {
1674 struct kgsl_devinfo devinfo;
1675
1676 if (sizebytes != sizeof(devinfo)) {
1677 status = -EINVAL;
1678 break;
1679 }
1680
1681 memset(&devinfo, 0, sizeof(devinfo));
1682 devinfo.device_id = device->id+1;
1683 devinfo.chip_id = adreno_dev->chip_id;
1684 devinfo.mmu_enabled = kgsl_mmu_enabled();
1685 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001686 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1687 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001688
1689 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1690 0) {
1691 status = -EFAULT;
1692 break;
1693 }
1694 status = 0;
1695 }
1696 break;
1697 case KGSL_PROP_DEVICE_SHADOW:
1698 {
1699 struct kgsl_shadowprop shadowprop;
1700
1701 if (sizebytes != sizeof(shadowprop)) {
1702 status = -EINVAL;
1703 break;
1704 }
1705 memset(&shadowprop, 0, sizeof(shadowprop));
1706 if (device->memstore.hostptr) {
1707 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1708 * anything to mmap().
1709 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001710 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001711 shadowprop.size = device->memstore.size;
1712 /* GSL needs this to be set, even if it
1713 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001714 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1715 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001716 }
1717 if (copy_to_user(value, &shadowprop,
1718 sizeof(shadowprop))) {
1719 status = -EFAULT;
1720 break;
1721 }
1722 status = 0;
1723 }
1724 break;
1725 case KGSL_PROP_MMU_ENABLE:
1726 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001727 int mmu_prop = kgsl_mmu_enabled();
1728
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001729 if (sizebytes != sizeof(int)) {
1730 status = -EINVAL;
1731 break;
1732 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001733 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734 status = -EFAULT;
1735 break;
1736 }
1737 status = 0;
1738 }
1739 break;
1740 case KGSL_PROP_INTERRUPT_WAITS:
1741 {
1742 int int_waits = 1;
1743 if (sizebytes != sizeof(int)) {
1744 status = -EINVAL;
1745 break;
1746 }
1747 if (copy_to_user(value, &int_waits, sizeof(int))) {
1748 status = -EFAULT;
1749 break;
1750 }
1751 status = 0;
1752 }
1753 break;
1754 default:
1755 status = -EINVAL;
1756 }
1757
1758 return status;
1759}
1760
Jordan Crousef7370f82012-04-18 09:31:07 -06001761static int adreno_setproperty(struct kgsl_device *device,
1762 enum kgsl_property_type type,
1763 void *value,
1764 unsigned int sizebytes)
1765{
1766 int status = -EINVAL;
1767
1768 switch (type) {
1769 case KGSL_PROP_PWRCTRL: {
1770 unsigned int enable;
1771 struct kgsl_device_platform_data *pdata =
1772 kgsl_device_get_drvdata(device);
1773
1774 if (sizebytes != sizeof(enable))
1775 break;
1776
1777 if (copy_from_user(&enable, (void __user *) value,
1778 sizeof(enable))) {
1779 status = -EFAULT;
1780 break;
1781 }
1782
1783 if (enable) {
1784 if (pdata->nap_allowed)
1785 device->pwrctrl.nap_allowed = true;
1786
1787 kgsl_pwrscale_enable(device);
1788 } else {
1789 device->pwrctrl.nap_allowed = false;
1790 kgsl_pwrscale_disable(device);
1791 }
1792
1793 status = 0;
1794 }
1795 break;
1796 default:
1797 break;
1798 }
1799
1800 return status;
1801}
1802
Lynus Vaz06a9a902011-10-04 19:25:33 +05301803static inline void adreno_poke(struct kgsl_device *device)
1804{
1805 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1806 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1807}
1808
Jordan Crousea29a2e02012-08-14 09:09:23 -06001809static int adreno_ringbuffer_drain(struct kgsl_device *device,
1810 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001811{
1812 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1813 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001814 unsigned long wait;
1815 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1816
1817 if (!(rb->flags & KGSL_FLAGS_STARTED))
1818 return 0;
1819
1820 /*
1821 * The first time into the loop, wait for 100 msecs and kick wptr again
1822 * to ensure that the hardware has updated correctly. After that, kick
1823 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1824 * expires
1825 */
1826
1827 wait = jiffies + msecs_to_jiffies(100);
1828
1829 adreno_poke(device);
1830
1831 do {
1832 if (time_after(jiffies, wait)) {
1833 adreno_poke(device);
1834
1835 /* Check to see if the core is hung */
1836 if (adreno_hang_detect(device, regs))
1837 return -ETIMEDOUT;
1838
1839 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1840 }
1841 GSL_RB_GET_READPTR(rb, &rb->rptr);
1842
1843 if (time_after(jiffies, timeout)) {
1844 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1845 rb->rptr, rb->wptr);
1846 return -ETIMEDOUT;
1847 }
1848 } while (rb->rptr != rb->wptr);
1849
1850 return 0;
1851}
1852
1853/* Caller must hold the device mutex. */
1854int adreno_idle(struct kgsl_device *device)
1855{
1856 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301858 unsigned long wait_time;
1859 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001860 unsigned int prev_reg_val[hang_detect_regs_count];
1861
1862 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001864 kgsl_cffdump_regpoll(device->id,
1865 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001866 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001867
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001868retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001869 /* First, wait for the ringbuffer to drain */
1870 if (adreno_ringbuffer_drain(device, prev_reg_val))
1871 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001872
1873 /* now, wait for the GPU to finish its operations */
Jordan Crousea29a2e02012-08-14 09:09:23 -06001874 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
1875 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1876
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001877 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001878 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1879 &rbbm_status);
1880 if (adreno_is_a2xx(adreno_dev)) {
1881 if (rbbm_status == 0x110)
1882 return 0;
1883 } else {
1884 if (!(rbbm_status & 0x80000000))
1885 return 0;
1886 }
Tarun Karra3335f142012-06-19 14:11:48 -07001887
1888 /* Dont wait for timeout, detect hang faster.
1889 */
1890 if (time_after(jiffies, wait_time_part)) {
1891 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001892 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001893 if ((adreno_hang_detect(device, prev_reg_val)))
1894 goto err;
1895 }
1896
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897 }
1898
1899err:
1900 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001901 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1902 !adreno_dump_and_recover(device)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001903 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001904 goto retry;
1905 }
1906 return -ETIMEDOUT;
1907}
1908
1909static unsigned int adreno_isidle(struct kgsl_device *device)
1910{
1911 int status = false;
1912 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1913 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1914 unsigned int rbbm_status;
1915
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001916 WARN_ON(device->state == KGSL_STATE_INIT);
1917 /* If the device isn't active, don't force it on. */
1918 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001919 /* Is the ring buffer is empty? */
1920 GSL_RB_GET_READPTR(rb, &rb->rptr);
1921 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1922 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001923 adreno_regread(device,
1924 adreno_dev->gpudev->reg_rbbm_status,
1925 &rbbm_status);
1926
1927 if (adreno_is_a2xx(adreno_dev)) {
1928 if (rbbm_status == 0x110)
1929 status = true;
1930 } else {
1931 if (!(rbbm_status & 0x80000000))
1932 status = true;
1933 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934 }
1935 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001936 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001937 }
1938 return status;
1939}
1940
1941/* Caller must hold the device mutex. */
1942static int adreno_suspend_context(struct kgsl_device *device)
1943{
1944 int status = 0;
1945 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1946
1947 /* switch to NULL ctxt */
1948 if (adreno_dev->drawctxt_active != NULL) {
1949 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001950 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001951 }
1952
1953 return status;
1954}
1955
Jordan Crouse233b2092012-04-18 09:31:09 -06001956/* Find a memory structure attached to an adreno context */
1957
1958struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1959 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1960{
1961 struct kgsl_context *context;
1962 struct adreno_context *adreno_context = NULL;
1963 int next = 0;
1964
1965 while (1) {
1966 context = idr_get_next(&device->context_idr, &next);
1967 if (context == NULL)
1968 break;
1969
1970 adreno_context = (struct adreno_context *)context->devctxt;
1971
1972 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1973 struct kgsl_memdesc *desc;
1974
1975 desc = &adreno_context->gpustate;
1976 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1977 return desc;
1978
1979 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1980 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1981 return desc;
1982 }
1983 next = next + 1;
1984 }
1985
1986 return NULL;
1987}
1988
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001989struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001990 unsigned int pt_base,
1991 unsigned int gpuaddr,
1992 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001993{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001994 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001995 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1996 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1997
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001998 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1999 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002000
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002001 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2002 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002003
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002004 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2005 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002006
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002007 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2008 size))
2009 return &device->mmu.setstate_memory;
2010
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002011 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
2012
2013 if (entry)
2014 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002015
Jordan Crouse233b2092012-04-18 09:31:09 -06002016 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002017}
2018
2019uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2020 unsigned int gpuaddr, unsigned int size)
2021{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002022 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002023
2024 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2025
2026 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002027}
2028
2029void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2030 unsigned int *value)
2031{
2032 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002033 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2034 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002035
2036 if (!in_interrupt())
2037 kgsl_pre_hwaccess(device);
2038
2039 /*ensure this read finishes before the next one.
2040 * i.e. act like normal readl() */
2041 *value = __raw_readl(reg);
2042 rmb();
2043}
2044
2045void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2046 unsigned int value)
2047{
2048 unsigned int *reg;
2049
Jordan Crouse7501d452012-04-19 08:58:44 -06002050 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002051
2052 if (!in_interrupt())
2053 kgsl_pre_hwaccess(device);
2054
2055 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002056 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002057
2058 /*ensure previous writes post before this one,
2059 * i.e. act like normal writel() */
2060 wmb();
2061 __raw_writel(value, reg);
2062}
2063
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002064static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2065{
2066 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002067 if (k_ctxt != NULL) {
2068 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002069 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2070 context_id = KGSL_CONTEXT_INVALID;
2071 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2072 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002073 }
2074
2075 return context_id;
2076}
2077
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002078static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002079 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002080{
2081 int status;
2082 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002083 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002084 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002085
2086 mutex_lock(&device->mutex);
2087 context_id = _get_context_id(context);
2088 /*
2089 * If the context ID is invalid, we are in a race with
2090 * the context being destroyed by userspace so bail.
2091 */
2092 if (context_id == KGSL_CONTEXT_INVALID) {
2093 KGSL_DRV_WARN(device, "context was detached");
2094 status = -EINVAL;
2095 goto unlock;
2096 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002097
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002098 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002099 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002100 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002101 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002102 mb();
2103
2104 if (enableflag) {
2105 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002106 KGSL_MEMSTORE_OFFSET(context_id,
2107 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002108 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002109 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002110 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002111 KGSL_MEMSTORE_OFFSET(context_id,
2112 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002113 wmb();
2114 }
2115 } else {
2116 unsigned int cmds[2];
2117 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002118 KGSL_MEMSTORE_OFFSET(context_id,
2119 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002120 enableflag = 1;
2121 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002122 KGSL_MEMSTORE_OFFSET(context_id,
2123 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002124 wmb();
2125 /* submit a dummy packet so that even if all
2126 * commands upto timestamp get executed we will still
2127 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002128 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002129 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002130
2131 if (adreno_dev->drawctxt_active)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002132 adreno_ringbuffer_issuecmds_intr(device,
2133 context, &cmds[0], 2);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002134 else
2135 /* We would never call this function if there
2136 * was no active contexts running */
2137 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002138 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002139 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002140unlock:
2141 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002142
2143 return status;
2144}
2145
2146/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002147 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002148 placing a process in wait q. For conditional interrupts we expect the
2149 process to already be in its wait q when its exit condition checking
2150 function is called.
2151*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002152#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002153({ \
2154 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002155 if (io) \
2156 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2157 else \
2158 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002159 __ret; \
2160})
2161
Tarun Karra3335f142012-06-19 14:11:48 -07002162
2163
2164unsigned int adreno_hang_detect(struct kgsl_device *device,
2165 unsigned int *prev_reg_val)
2166{
2167 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2168 unsigned int curr_reg_val[hang_detect_regs_count];
2169 unsigned int hang_detected = 1;
2170 unsigned int i;
2171
2172 if (!adreno_dev->fast_hang_detect)
2173 return 0;
2174
2175 for (i = 0; i < hang_detect_regs_count; i++) {
2176 adreno_regread(device, hang_detect_regs[i],
2177 &curr_reg_val[i]);
2178 if (curr_reg_val[i] != prev_reg_val[i]) {
2179 prev_reg_val[i] = curr_reg_val[i];
2180 hang_detected = 0;
2181 }
2182 }
2183
2184 return hang_detected;
2185}
2186
2187
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002188/* MUST be called with the device mutex held */
2189static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002190 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002191 unsigned int timestamp,
2192 unsigned int msecs)
2193{
2194 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06002195 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06002196 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06002198 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07002199 int retries = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002200 unsigned int ts_issued;
2201 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07002202 unsigned int time_elapsed = 0;
2203 unsigned int prev_reg_val[hang_detect_regs_count];
Jordan Crouse21f75a02012-08-09 15:08:59 -06002204 unsigned int wait;
Tarun Karra3335f142012-06-19 14:11:48 -07002205
2206 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002207
2208 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002209
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302210 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07002211 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302212 msecs = adreno_dev->wait_timeout;
2213
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002214 if (timestamp_cmp(timestamp, ts_issued) > 0) {
2215 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
2216 "last issued ts <%d:0x%x>\n",
2217 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002218 status = -EINVAL;
2219 goto done;
2220 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002221
Jordan Crouse21f75a02012-08-09 15:08:59 -06002222 /*
2223 * Make the first timeout interval 100 msecs and then try to kick the
2224 * wptr again. This helps to ensure the wptr is updated properly. If
2225 * the requested timeout is less than 100 msecs, then wait 20msecs which
2226 * is the minimum amount of time we can safely wait at 100HZ
Lynus Vaz06a9a902011-10-04 19:25:33 +05302227 */
Jordan Crouse21f75a02012-08-09 15:08:59 -06002228
2229 if (msecs == 0 || msecs >= 100)
2230 wait = 100;
2231 else
2232 wait = 20;
2233
Tarun Karra3335f142012-06-19 14:11:48 -07002234 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002235 /*
2236 * If the context ID is invalid, we are in a race with
2237 * the context being destroyed by userspace so bail.
2238 */
2239 if (context_id == KGSL_CONTEXT_INVALID) {
2240 KGSL_DRV_WARN(device, "context was detached");
2241 status = -EINVAL;
2242 goto done;
2243 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002244 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07002245 /* if the timestamp happens while we're not
2246 * waiting, there's a chance that an interrupt
2247 * will not be generated and thus the timestamp
2248 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05302249 */
Jeremy Gebben63904832012-02-07 16:10:55 -07002250 queue_work(device->work_queue, &device->ts_expired_ws);
2251 status = 0;
2252 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002253 }
Jeremy Gebben63904832012-02-07 16:10:55 -07002254 adreno_poke(device);
2255 io_cnt = (io_cnt + 1) % 100;
2256 if (io_cnt <
2257 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2258 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002259
2260 if ((retries > 0) &&
2261 (adreno_hang_detect(device, prev_reg_val)))
2262 goto hang_dump;
2263
Jeremy Gebben63904832012-02-07 16:10:55 -07002264 mutex_unlock(&device->mutex);
2265 /* We need to make sure that the process is
2266 * placed in wait-q before its condition is called
2267 */
2268 status = kgsl_wait_event_interruptible_timeout(
2269 device->wait_queue,
2270 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002271 context, timestamp),
Jordan Crouse21f75a02012-08-09 15:08:59 -06002272 msecs_to_jiffies(wait), io);
2273
Jeremy Gebben63904832012-02-07 16:10:55 -07002274 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002275
Jeremy Gebben63904832012-02-07 16:10:55 -07002276 if (status > 0) {
2277 /*completed before the wait finished */
2278 status = 0;
2279 goto done;
2280 } else if (status < 0) {
2281 /*an error occurred*/
2282 goto done;
2283 }
2284 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07002285
Jordan Crouse21f75a02012-08-09 15:08:59 -06002286 time_elapsed += wait;
2287 wait = KGSL_TIMEOUT_PART;
2288
Tarun Karra3335f142012-06-19 14:11:48 -07002289 retries++;
2290
Jordan Crouse21f75a02012-08-09 15:08:59 -06002291 } while (!msecs || time_elapsed < msecs);
Tarun Karra3335f142012-06-19 14:11:48 -07002292
2293hang_dump:
Shubhraprakash Das54396e52012-03-10 13:24:54 -07002294 /*
2295 * Check if timestamp has retired here because we may have hit
2296 * recovery which can take some time and cause waiting threads
2297 * to timeout
2298 */
2299 if (kgsl_check_timestamp(device, context, timestamp))
2300 goto done;
Jeremy Gebben63904832012-02-07 16:10:55 -07002301 status = -ETIMEDOUT;
2302 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002303 "Device hang detected while waiting for timestamp: "
2304 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2305 "wptr: 0x%x\n",
2306 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002307 adreno_dev->ringbuffer.wptr);
2308 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002309 /* The timestamp that this process wanted
2310 * to wait on may be invalid or expired now
2311 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07002312 status = 0;
2313 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002314done:
2315 return (int)status;
2316}
2317
2318static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002319 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002320{
2321 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002322 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002323
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002324 /*
2325 * If the context ID is invalid, we are in a race with
2326 * the context being destroyed by userspace so bail.
2327 */
2328 if (context_id == KGSL_CONTEXT_INVALID) {
2329 KGSL_DRV_WARN(device, "context was detached");
2330 return timestamp;
2331 }
Jordan Crousec659f382012-04-16 11:10:41 -06002332 switch (type) {
2333 case KGSL_TIMESTAMP_QUEUED: {
2334 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2335 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2336
2337 timestamp = rb->timestamp[context_id];
2338 break;
2339 }
2340 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002341 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002342 break;
2343 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002344 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002345 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2346 break;
2347 }
2348
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002349 rmb();
2350
2351 return timestamp;
2352}
2353
2354static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2355 unsigned int cmd, void *data)
2356{
2357 int result = 0;
2358 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2359 struct kgsl_context *context;
2360
2361 switch (cmd) {
2362 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2363 binbase = data;
2364
2365 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2366 if (context) {
2367 adreno_drawctxt_set_bin_base_offset(
2368 dev_priv->device, context, binbase->offset);
2369 } else {
2370 result = -EINVAL;
2371 KGSL_DRV_ERR(dev_priv->device,
2372 "invalid drawctxt drawctxt_id %d "
2373 "device_id=%d\n",
2374 binbase->drawctxt_id, dev_priv->device->id);
2375 }
2376 break;
2377
2378 default:
2379 KGSL_DRV_INFO(dev_priv->device,
2380 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002381 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002382 break;
2383 }
2384 return result;
2385
2386}
2387
2388static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2389{
2390 gpu_freq /= 1000000;
2391 return ticks / gpu_freq;
2392}
2393
2394static void adreno_power_stats(struct kgsl_device *device,
2395 struct kgsl_power_stats *stats)
2396{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002397 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002398 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002399 unsigned int cycles;
2400
2401 /* Get the busy cycles counted since the counter was last reset */
2402 /* Calling this function also resets and restarts the counter */
2403
2404 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002405
2406 /* In order to calculate idle you have to have run the algorithm *
2407 * at least once to get a start time. */
2408 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002409 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002410 stats->total_time = tmp - pwr->time;
2411 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002412 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002413 pwrlevels[device->pwrctrl.active_pwrlevel].
2414 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002415 } else {
2416 stats->total_time = 0;
2417 stats->busy_time = 0;
2418 pwr->time = ktime_to_us(ktime_get());
2419 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002420}
2421
2422void adreno_irqctrl(struct kgsl_device *device, int state)
2423{
Jordan Crousea78c9172011-07-11 13:14:09 -06002424 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2425 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002426}
2427
Jordan Croused6535882012-06-20 08:22:16 -06002428static unsigned int adreno_gpuid(struct kgsl_device *device,
2429 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002430{
2431 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2432
Jordan Croused6535882012-06-20 08:22:16 -06002433 /* Some applications need to know the chip ID too, so pass
2434 * that as a parameter */
2435
2436 if (chipid != NULL)
2437 *chipid = adreno_dev->chip_id;
2438
Jordan Crousea0758f22011-12-07 11:19:22 -07002439 /* Standard KGSL gpuid format:
2440 * top word is 0x0002 for 2D or 0x0003 for 3D
2441 * Bottom word is core specific identifer
2442 */
2443
2444 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2445}
2446
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002447static const struct kgsl_functable adreno_functable = {
2448 /* Mandatory functions */
2449 .regread = adreno_regread,
2450 .regwrite = adreno_regwrite,
2451 .idle = adreno_idle,
2452 .isidle = adreno_isidle,
2453 .suspend_context = adreno_suspend_context,
2454 .start = adreno_start,
2455 .stop = adreno_stop,
2456 .getproperty = adreno_getproperty,
2457 .waittimestamp = adreno_waittimestamp,
2458 .readtimestamp = adreno_readtimestamp,
2459 .issueibcmds = adreno_ringbuffer_issueibcmds,
2460 .ioctl = adreno_ioctl,
2461 .setup_pt = adreno_setup_pt,
2462 .cleanup_pt = adreno_cleanup_pt,
2463 .power_stats = adreno_power_stats,
2464 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002465 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002466 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002467 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002468 /* Optional functions */
2469 .setstate = adreno_setstate,
2470 .drawctxt_create = adreno_drawctxt_create,
2471 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002472 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002473 .postmortem_dump = adreno_dump,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002474};
2475
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002476static struct platform_driver adreno_platform_driver = {
2477 .probe = adreno_probe,
2478 .remove = __devexit_p(adreno_remove),
2479 .suspend = kgsl_suspend_driver,
2480 .resume = kgsl_resume_driver,
2481 .id_table = adreno_id_table,
2482 .driver = {
2483 .owner = THIS_MODULE,
2484 .name = DEVICE_3D_NAME,
2485 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002486 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002487 }
2488};
2489
2490static int __init kgsl_3d_init(void)
2491{
2492 return platform_driver_register(&adreno_platform_driver);
2493}
2494
2495static void __exit kgsl_3d_exit(void)
2496{
2497 platform_driver_unregister(&adreno_platform_driver);
2498}
2499
2500module_init(kgsl_3d_init);
2501module_exit(kgsl_3d_exit);
2502
2503MODULE_DESCRIPTION("3D Graphics driver");
2504MODULE_VERSION("1.2");
2505MODULE_LICENSE("GPL v2");
2506MODULE_ALIAS("platform:kgsl_3d");