blob: 83614300c9d976557f901db421a0d017abd90ec6 [file] [log] [blame]
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -07001/* Copyright (c) 2002,2007-2012, The Linux Foundation. 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
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700868 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600869
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700870 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600871 sizeof(struct msm_dcvs_freq_entry),
872 GFP_KERNEL);
873
874 if (info->freq_tbl == NULL) {
875 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700876 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600877 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
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700888 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600889 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
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700898 if (adreno_of_read_property(child, "qcom,voltage",
899 &info->freq_tbl[index].voltage))
900 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600901
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700902 if (adreno_of_read_property(child, "qcom,is_trans_level",
903 &info->freq_tbl[index].is_trans_level))
904 info->freq_tbl[index].is_trans_level = 0;
905
906 if (adreno_of_read_property(child, "qcom,active-energy-offset",
907 &info->freq_tbl[index].active_energy_offset))
908 info->freq_tbl[index].active_energy_offset = 0;
909
910 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
911 &info->freq_tbl[index].leakage_energy_offset))
912 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600913 }
914
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700915 if (adreno_of_read_property(node, "qcom,core-core-type",
916 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600917 goto err;
918
919 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
920 &info->algo_param.disable_pc_threshold))
921 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700922 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
923 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600924 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700925 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
926 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600927 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600928 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
929 &info->algo_param.em_max_util_pct))
930 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700931 if (adreno_of_read_property(node, "qcom,algo-group-id",
932 &info->algo_param.group_id))
933 goto err;
934 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
935 &info->algo_param.max_freq_chg_time_us))
936 goto err;
937 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
938 &info->algo_param.slack_mode_dynamic))
939 goto err;
940 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
941 &info->algo_param.slack_weight_thresh_pct))
942 goto err;
943 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
944 &info->algo_param.slack_time_min_us))
945 goto err;
946 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
947 &info->algo_param.slack_time_max_us))
948 goto err;
949 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
950 &info->algo_param.ss_win_size_min_us))
951 goto err;
952 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
953 &info->algo_param.ss_win_size_max_us))
954 goto err;
955 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
956 &info->algo_param.ss_util_pct))
957 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600958 if (adreno_of_read_property(node, "qcom,algo-ss-iobusy-conv",
959 &info->algo_param.ss_iobusy_conv))
960 goto err;
961
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700962 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
963 &info->energy_coeffs.active_coeff_a))
964 goto err;
965 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
966 &info->energy_coeffs.active_coeff_b))
967 goto err;
968 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
969 &info->energy_coeffs.active_coeff_c))
970 goto err;
971 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
972 &info->energy_coeffs.leakage_coeff_a))
973 goto err;
974 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
975 &info->energy_coeffs.leakage_coeff_b))
976 goto err;
977 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
978 &info->energy_coeffs.leakage_coeff_c))
979 goto err;
980 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
981 &info->energy_coeffs.leakage_coeff_d))
982 goto err;
983
984 if (adreno_of_read_property(node, "qcom,power-current-temp",
985 &info->power_param.current_temp))
986 goto err;
987
Lokesh Batra805e1e12012-08-03 08:34:06 -0600988 return info;
989
990err:
991 if (info)
992 kfree(info->freq_tbl);
993
994 kfree(info);
995
996 return ERR_PTR(ret);
997}
998
999static int adreno_of_get_iommu(struct device_node *parent,
1000 struct kgsl_device_platform_data *pdata)
1001{
1002 struct device_node *node, *child;
1003 struct kgsl_device_iommu_data *data = NULL;
1004 struct kgsl_iommu_ctx *ctxs = NULL;
1005 u32 reg_val[2];
1006 int ctx_index = 0;
1007
1008 node = of_parse_phandle(parent, "iommu", 0);
1009 if (node == NULL)
1010 return -EINVAL;
1011
1012 data = kzalloc(sizeof(*data), GFP_KERNEL);
1013 if (data == NULL) {
1014 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
1015 goto err;
1016 }
1017
1018 if (of_property_read_u32_array(node, "reg", reg_val, 2))
1019 goto err;
1020
1021 data->physstart = reg_val[0];
1022 data->physend = data->physstart + reg_val[1] - 1;
1023
1024 data->iommu_ctx_count = 0;
1025
1026 for_each_child_of_node(node, child)
1027 data->iommu_ctx_count++;
1028
1029 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
1030 GFP_KERNEL);
1031
1032 if (ctxs == NULL) {
1033 KGSL_CORE_ERR("kzalloc(%d) failed\n",
1034 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
1035 goto err;
1036 }
1037
1038 for_each_child_of_node(node, child) {
1039 int ret = of_property_read_string(child, "label",
1040 &ctxs[ctx_index].iommu_ctx_name);
1041
1042 if (ret) {
1043 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
1044 goto err;
1045 }
1046
1047 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
1048 &ctxs[ctx_index].ctx_id))
1049 goto err;
1050
1051 ctx_index++;
1052 }
1053
1054 data->iommu_ctxs = ctxs;
1055
1056 pdata->iommu_data = data;
1057 pdata->iommu_count = 1;
1058
1059 return 0;
1060
1061err:
1062 kfree(ctxs);
1063 kfree(data);
1064
1065 return -EINVAL;
1066}
1067
1068static int adreno_of_get_pdata(struct platform_device *pdev)
1069{
1070 struct kgsl_device_platform_data *pdata = NULL;
1071 struct kgsl_device *device;
1072 int ret = -EINVAL;
1073
1074 pdev->id_entry = adreno_id_table;
1075
1076 pdata = pdev->dev.platform_data;
1077 if (pdata)
1078 return 0;
1079
1080 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1081 KGSL_CORE_ERR("Unable to read 'label'\n");
1082 goto err;
1083 }
1084
1085 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1086 goto err;
1087
1088 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1089 if (pdata == NULL) {
1090 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1091 ret = -ENOMEM;
1092 goto err;
1093 }
1094
1095 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1096 &pdata->chipid))
1097 goto err;
1098
1099 /* pwrlevel Data */
1100 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1101 if (ret)
1102 goto err;
1103
1104 /* Default value is 83, if not found in DT */
1105 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1106 &pdata->idle_timeout))
1107 pdata->idle_timeout = 83;
1108
1109 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1110 &pdata->nap_allowed))
1111 pdata->nap_allowed = 1;
1112
1113 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1114 &pdata->clk_map))
1115 goto err;
1116
1117 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1118
1119 if (device->id != KGSL_DEVICE_3D0)
1120 goto err;
1121
1122 /* Bus Scale Data */
1123
1124 pdata->bus_scale_table = adreno_of_get_bus_scale(pdev->dev.of_node);
1125 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1126 ret = PTR_ERR(pdata->bus_scale_table);
1127 goto err;
1128 }
1129
1130 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1131 if (IS_ERR_OR_NULL(pdata->core_info)) {
1132 ret = PTR_ERR(pdata->core_info);
1133 goto err;
1134 }
1135
1136 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1137 if (ret)
1138 goto err;
1139
1140 pdev->dev.platform_data = pdata;
1141 return 0;
1142
1143err:
1144 if (pdata) {
1145 adreno_of_free_bus_scale_info(pdata->bus_scale_table);
1146 if (pdata->core_info)
1147 kfree(pdata->core_info->freq_tbl);
1148 kfree(pdata->core_info);
1149
1150 if (pdata->iommu_data)
1151 kfree(pdata->iommu_data->iommu_ctxs);
1152
1153 kfree(pdata->iommu_data);
1154 }
1155
1156 kfree(pdata);
1157
1158 return ret;
1159}
1160
liu zhong7dfa2a32012-04-27 19:11:01 -07001161#ifdef CONFIG_MSM_OCMEM
1162static int
1163adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1164{
1165 if (adreno_dev->gpurev != ADRENO_REV_A330)
1166 return 0;
1167
1168 /* OCMEM is only needed once, do not support consective allocation */
1169 if (adreno_dev->ocmem_hdl != NULL)
1170 return 0;
1171
1172 adreno_dev->ocmem_hdl =
1173 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1174 if (adreno_dev->ocmem_hdl == NULL)
1175 return -ENOMEM;
1176
1177 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
1178 adreno_dev->gmem_base = adreno_dev->ocmem_hdl->addr;
1179
1180 return 0;
1181}
1182
1183static void
1184adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1185{
1186 if (adreno_dev->gpurev != ADRENO_REV_A330)
1187 return;
1188
1189 if (adreno_dev->ocmem_hdl == NULL)
1190 return;
1191
1192 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1193 adreno_dev->ocmem_hdl = NULL;
1194}
1195#else
1196static int
1197adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1198{
1199 return 0;
1200}
1201
1202static void
1203adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1204{
1205}
1206#endif
1207
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208static int __devinit
1209adreno_probe(struct platform_device *pdev)
1210{
1211 struct kgsl_device *device;
1212 struct adreno_device *adreno_dev;
1213 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001214 bool is_dt;
1215
1216 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1217
1218 if (is_dt && pdev->dev.of_node) {
1219 status = adreno_of_get_pdata(pdev);
1220 if (status)
1221 goto error_return;
1222 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223
1224 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1225 adreno_dev = ADRENO_DEVICE(device);
1226 device->parentdev = &pdev->dev;
1227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228 status = adreno_ringbuffer_init(device);
1229 if (status != 0)
1230 goto error;
1231
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001232 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233 if (status)
1234 goto error_close_rb;
1235
1236 adreno_debugfs_init(device);
1237
1238 kgsl_pwrscale_init(device);
1239 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1240
1241 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1242 return 0;
1243
1244error_close_rb:
1245 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1246error:
1247 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001248error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001249 return status;
1250}
1251
1252static int __devexit adreno_remove(struct platform_device *pdev)
1253{
1254 struct kgsl_device *device;
1255 struct adreno_device *adreno_dev;
1256
1257 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1258 adreno_dev = ADRENO_DEVICE(device);
1259
1260 kgsl_pwrscale_detach_policy(device);
1261 kgsl_pwrscale_close(device);
1262
1263 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1264 kgsl_device_platform_remove(device);
1265
1266 return 0;
1267}
1268
1269static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1270{
1271 int status = -EINVAL;
1272 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001273
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001274 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1275 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001276
1277 /* Power up the device */
1278 kgsl_pwrctrl_enable(device);
1279
1280 /* Identify the specific GPU */
1281 adreno_identify_gpu(adreno_dev);
1282
Jordan Crouse505df9c2011-07-28 08:37:59 -06001283 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1284 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1285 adreno_dev->chip_id);
1286 goto error_clk_off;
1287 }
1288
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001289 /* Set up the MMU */
1290 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001291 /*
1292 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1293 * on older gpus
1294 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001295 if (adreno_is_a20x(adreno_dev)) {
1296 device->mh.mh_intf_cfg1 = 0;
1297 device->mh.mh_intf_cfg2 = 0;
1298 }
1299
1300 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001301 }
1302
Tarun Karra3335f142012-06-19 14:11:48 -07001303 /* Assign correct RBBM status register to hang detect regs
1304 */
1305 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1306
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001307 status = kgsl_mmu_start(device);
1308 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309 goto error_clk_off;
1310
liu zhong7dfa2a32012-04-27 19:11:01 -07001311 status = adreno_ocmem_gmem_malloc(adreno_dev);
1312 if (status) {
1313 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1314 goto error_mmu_off;
1315 }
1316
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001317 /* Start the GPU */
1318 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001319
1320 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001321 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322
1323 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001324 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001325 /* While recovery is on we do not want timer to
1326 * fire and attempt to change any device state */
1327 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1328 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001329 return 0;
1330 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001332 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001333
1334error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001335 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001336
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337error_clk_off:
1338 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339
1340 return status;
1341}
1342
1343static int adreno_stop(struct kgsl_device *device)
1344{
1345 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1346
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001347 adreno_dev->drawctxt_active = NULL;
1348
1349 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1350
Shubhraprakash Das79447952012-04-26 18:12:23 -06001351 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001352
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001353 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301354 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001355 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001356
liu zhong7dfa2a32012-04-27 19:11:01 -07001357 adreno_ocmem_gmem_free(adreno_dev);
1358
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001359 /* Power down the device */
1360 kgsl_pwrctrl_disable(device);
1361
1362 return 0;
1363}
1364
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001365static void adreno_mark_context_status(struct kgsl_device *device,
1366 int recovery_status)
1367{
1368 struct kgsl_context *context;
1369 int next = 0;
1370 /*
1371 * Set the reset status of all contexts to
1372 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1373 * since thats the guilty party, if recovery failed then
1374 * mark all as guilty
1375 */
1376 while ((context = idr_get_next(&device->context_idr, &next))) {
1377 struct adreno_context *adreno_context = context->devctxt;
1378 if (recovery_status) {
1379 context->reset_status =
1380 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1381 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1382 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1383 context->reset_status) {
1384 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1385 CTXT_FLAGS_GPU_HANG_RECOVERED))
1386 context->reset_status =
1387 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1388 else
1389 context->reset_status =
1390 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1391 }
1392 next = next + 1;
1393 }
1394}
1395
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001396static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1397{
1398 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1399 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1400 struct kgsl_context *context;
1401 struct adreno_context *temp_adreno_context;
1402 int next = 0;
1403
1404 while ((context = idr_get_next(&device->context_idr, &next))) {
1405 temp_adreno_context = context->devctxt;
1406 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1407 kgsl_sharedmem_writel(&device->memstore,
1408 KGSL_MEMSTORE_OFFSET(context->id,
1409 soptimestamp),
1410 rb->timestamp[context->id]);
1411 kgsl_sharedmem_writel(&device->memstore,
1412 KGSL_MEMSTORE_OFFSET(context->id,
1413 eoptimestamp),
1414 rb->timestamp[context->id]);
1415 }
1416 next = next + 1;
1417 }
1418}
1419
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001420static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1421{
1422 vfree(rec_data->rb_buffer);
1423 vfree(rec_data->bad_rb_buffer);
1424}
1425
1426static int adreno_setup_recovery_data(struct kgsl_device *device,
1427 struct adreno_recovery_data *rec_data)
1428{
1429 int ret = 0;
1430 unsigned int ib1_sz, ib2_sz;
1431 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1432 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1433
1434 memset(rec_data, 0, sizeof(*rec_data));
1435
1436 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1437 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1438 if (ib1_sz || ib2_sz)
1439 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1440
1441 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1442 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1443 current_context));
1444
1445 kgsl_sharedmem_readl(&device->memstore,
1446 &rec_data->global_eop,
1447 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1448 eoptimestamp));
1449
1450 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1451 if (!rec_data->rb_buffer) {
1452 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1453 rb->buffer_desc.size);
1454 return -ENOMEM;
1455 }
1456
1457 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1458 if (!rec_data->bad_rb_buffer) {
1459 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1460 rb->buffer_desc.size);
1461 ret = -ENOMEM;
1462 goto done;
1463 }
1464
1465done:
1466 if (ret) {
1467 vfree(rec_data->rb_buffer);
1468 vfree(rec_data->bad_rb_buffer);
1469 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001470 return ret;
1471}
1472
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001473static int
1474_adreno_recover_hang(struct kgsl_device *device,
1475 struct adreno_recovery_data *rec_data,
1476 bool try_bad_commands)
1477{
1478 int ret;
1479 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1480 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1481 struct kgsl_context *context;
1482 struct adreno_context *adreno_context = NULL;
1483 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1484
1485 context = idr_find(&device->context_idr, rec_data->context_id);
1486 if (context == NULL) {
1487 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1488 rec_data->context_id);
1489 } else {
1490 adreno_context = context->devctxt;
1491 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1492 }
1493
1494 /* Extract valid contents from rb which can still be executed after
1495 * hang */
1496 ret = adreno_ringbuffer_extract(rb, rec_data);
1497 if (ret)
1498 goto done;
1499
1500 /* restart device */
1501 ret = adreno_stop(device);
1502 if (ret) {
1503 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1504 goto done;
1505 }
1506
1507 ret = adreno_start(device, true);
1508 if (ret) {
1509 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1510 goto done;
1511 }
1512
1513 if (context)
1514 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1515 KGSL_MEMSTORE_GLOBAL);
1516
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001517 /* If iommu is used then we need to make sure that the iommu clocks
1518 * are on since there could be commands in pipeline that touch iommu */
1519 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1520 ret = kgsl_mmu_enable_clk(&device->mmu,
1521 KGSL_IOMMU_CONTEXT_USER);
1522 if (ret)
1523 goto done;
1524 }
1525
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001526 /* Do not try the bad caommands if recovery has failed bad commands
1527 * once already */
1528 if (!try_bad_commands)
1529 rec_data->bad_rb_size = 0;
1530
1531 if (rec_data->bad_rb_size) {
1532 int idle_ret;
1533 /* submit the bad and good context commands and wait for
1534 * them to pass */
1535 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1536 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001537 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001538 if (idle_ret) {
1539 ret = adreno_stop(device);
1540 if (ret) {
1541 KGSL_DRV_ERR(device,
1542 "Device stop failed in recovery\n");
1543 goto done;
1544 }
1545 ret = adreno_start(device, true);
1546 if (ret) {
1547 KGSL_DRV_ERR(device,
1548 "Device start failed in recovery\n");
1549 goto done;
1550 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001551 if (context)
1552 kgsl_mmu_setstate(&device->mmu,
1553 adreno_context->pagetable,
1554 KGSL_MEMSTORE_GLOBAL);
1555
1556 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1557 ret = kgsl_mmu_enable_clk(&device->mmu,
1558 KGSL_IOMMU_CONTEXT_USER);
1559 if (ret)
1560 goto done;
1561 }
1562
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001563 ret = idle_ret;
1564 KGSL_DRV_ERR(device,
1565 "Bad context commands hung in recovery\n");
1566 } else {
1567 KGSL_DRV_ERR(device,
1568 "Bad context commands succeeded in recovery\n");
1569 if (adreno_context)
1570 adreno_context->flags = (adreno_context->flags &
1571 ~CTXT_FLAGS_GPU_HANG) |
1572 CTXT_FLAGS_GPU_HANG_RECOVERED;
1573 adreno_dev->drawctxt_active = last_active_ctx;
1574 }
1575 }
1576 /* If either the bad command sequence failed or we did not play it */
1577 if (ret || !rec_data->bad_rb_size) {
1578 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1579 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001580 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001581 if (ret) {
1582 /* If we fail here we can try to invalidate another
1583 * context and try recovering again */
1584 ret = -EAGAIN;
1585 goto done;
1586 }
1587 /* ringbuffer now has data from the last valid context id,
1588 * so restore the active_ctx to the last valid context */
1589 if (rec_data->last_valid_ctx_id) {
1590 struct kgsl_context *last_ctx =
1591 idr_find(&device->context_idr,
1592 rec_data->last_valid_ctx_id);
1593 if (last_ctx)
1594 adreno_dev->drawctxt_active = last_ctx->devctxt;
1595 }
1596 }
1597done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001598 /* Turn off iommu clocks */
1599 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1600 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001601 return ret;
1602}
1603
1604static int
1605adreno_recover_hang(struct kgsl_device *device,
1606 struct adreno_recovery_data *rec_data)
1607{
1608 int ret = 0;
1609 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1610 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1611 unsigned int timestamp;
1612
1613 KGSL_DRV_ERR(device,
1614 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1615 "Bad context_id: %u, global_eop: 0x%x\n",
1616 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1617
1618 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1619 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1620
1621 /* We may need to replay commands multiple times based on whether
1622 * multiple contexts hang the GPU */
1623 while (true) {
1624 if (!ret)
1625 ret = _adreno_recover_hang(device, rec_data, true);
1626 else
1627 ret = _adreno_recover_hang(device, rec_data, false);
1628
1629 if (-EAGAIN == ret) {
1630 /* setup new recovery parameters and retry, this
1631 * means more than 1 contexts are causing hang */
1632 adreno_destroy_recovery_data(rec_data);
1633 adreno_setup_recovery_data(device, rec_data);
1634 KGSL_DRV_ERR(device,
1635 "Retry recovery from 3D GPU hang. Recovery parameters: "
1636 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1637 rec_data->ib1, rec_data->context_id,
1638 rec_data->global_eop);
1639 } else {
1640 break;
1641 }
1642 }
1643
1644 if (ret)
1645 goto done;
1646
1647 /* Restore correct states after recovery */
1648 if (adreno_dev->drawctxt_active)
1649 device->mmu.hwpagetable =
1650 adreno_dev->drawctxt_active->pagetable;
1651 else
1652 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1653 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1654 kgsl_sharedmem_writel(&device->memstore,
1655 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1656 eoptimestamp),
1657 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1658done:
1659 adreno_set_max_ts_for_bad_ctxs(device);
1660 adreno_mark_context_status(device, ret);
1661 if (!ret)
1662 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1663 else
1664 KGSL_DRV_ERR(device, "Recovery failed\n");
1665 return ret;
1666}
1667
1668int
1669adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001671 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001672 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001673
1674 if (device->state == KGSL_STATE_HUNG)
1675 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001676 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001677 mutex_unlock(&device->mutex);
1678 wait_for_completion(&device->recovery_gate);
1679 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001680 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001681 result = 0;
1682 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001683 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001684 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001685 /* Detected a hang */
1686
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001687 /* Get the recovery data as soon as hang is detected */
1688 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001689 /*
1690 * Trigger an automatic dump of the state to
1691 * the console
1692 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001693 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001694
1695 /*
1696 * Make a GPU snapshot. For now, do it after the PM dump so we
1697 * can at least be sure the PM dump will work as it always has
1698 */
1699 kgsl_device_snapshot(device, 1);
1700
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001701 result = adreno_recover_hang(device, &rec_data);
1702 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001703 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001704 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001705 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001706 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001707 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1708 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001709 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001710 }
1711done:
1712 return result;
1713}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001714EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001715
1716static int adreno_getproperty(struct kgsl_device *device,
1717 enum kgsl_property_type type,
1718 void *value,
1719 unsigned int sizebytes)
1720{
1721 int status = -EINVAL;
1722 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1723
1724 switch (type) {
1725 case KGSL_PROP_DEVICE_INFO:
1726 {
1727 struct kgsl_devinfo devinfo;
1728
1729 if (sizebytes != sizeof(devinfo)) {
1730 status = -EINVAL;
1731 break;
1732 }
1733
1734 memset(&devinfo, 0, sizeof(devinfo));
1735 devinfo.device_id = device->id+1;
1736 devinfo.chip_id = adreno_dev->chip_id;
1737 devinfo.mmu_enabled = kgsl_mmu_enabled();
1738 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001739 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1740 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001741
1742 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1743 0) {
1744 status = -EFAULT;
1745 break;
1746 }
1747 status = 0;
1748 }
1749 break;
1750 case KGSL_PROP_DEVICE_SHADOW:
1751 {
1752 struct kgsl_shadowprop shadowprop;
1753
1754 if (sizebytes != sizeof(shadowprop)) {
1755 status = -EINVAL;
1756 break;
1757 }
1758 memset(&shadowprop, 0, sizeof(shadowprop));
1759 if (device->memstore.hostptr) {
1760 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1761 * anything to mmap().
1762 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001763 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001764 shadowprop.size = device->memstore.size;
1765 /* GSL needs this to be set, even if it
1766 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001767 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1768 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769 }
1770 if (copy_to_user(value, &shadowprop,
1771 sizeof(shadowprop))) {
1772 status = -EFAULT;
1773 break;
1774 }
1775 status = 0;
1776 }
1777 break;
1778 case KGSL_PROP_MMU_ENABLE:
1779 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001780 int mmu_prop = kgsl_mmu_enabled();
1781
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782 if (sizebytes != sizeof(int)) {
1783 status = -EINVAL;
1784 break;
1785 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001786 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001787 status = -EFAULT;
1788 break;
1789 }
1790 status = 0;
1791 }
1792 break;
1793 case KGSL_PROP_INTERRUPT_WAITS:
1794 {
1795 int int_waits = 1;
1796 if (sizebytes != sizeof(int)) {
1797 status = -EINVAL;
1798 break;
1799 }
1800 if (copy_to_user(value, &int_waits, sizeof(int))) {
1801 status = -EFAULT;
1802 break;
1803 }
1804 status = 0;
1805 }
1806 break;
1807 default:
1808 status = -EINVAL;
1809 }
1810
1811 return status;
1812}
1813
Jordan Crousef7370f82012-04-18 09:31:07 -06001814static int adreno_setproperty(struct kgsl_device *device,
1815 enum kgsl_property_type type,
1816 void *value,
1817 unsigned int sizebytes)
1818{
1819 int status = -EINVAL;
1820
1821 switch (type) {
1822 case KGSL_PROP_PWRCTRL: {
1823 unsigned int enable;
1824 struct kgsl_device_platform_data *pdata =
1825 kgsl_device_get_drvdata(device);
1826
1827 if (sizebytes != sizeof(enable))
1828 break;
1829
1830 if (copy_from_user(&enable, (void __user *) value,
1831 sizeof(enable))) {
1832 status = -EFAULT;
1833 break;
1834 }
1835
1836 if (enable) {
1837 if (pdata->nap_allowed)
1838 device->pwrctrl.nap_allowed = true;
1839
1840 kgsl_pwrscale_enable(device);
1841 } else {
1842 device->pwrctrl.nap_allowed = false;
1843 kgsl_pwrscale_disable(device);
1844 }
1845
1846 status = 0;
1847 }
1848 break;
1849 default:
1850 break;
1851 }
1852
1853 return status;
1854}
1855
Lynus Vaz06a9a902011-10-04 19:25:33 +05301856static inline void adreno_poke(struct kgsl_device *device)
1857{
1858 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1859 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1860}
1861
Jordan Crousea29a2e02012-08-14 09:09:23 -06001862static int adreno_ringbuffer_drain(struct kgsl_device *device,
1863 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001864{
1865 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1866 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001867 unsigned long wait;
1868 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1869
1870 if (!(rb->flags & KGSL_FLAGS_STARTED))
1871 return 0;
1872
1873 /*
1874 * The first time into the loop, wait for 100 msecs and kick wptr again
1875 * to ensure that the hardware has updated correctly. After that, kick
1876 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1877 * expires
1878 */
1879
1880 wait = jiffies + msecs_to_jiffies(100);
1881
1882 adreno_poke(device);
1883
1884 do {
1885 if (time_after(jiffies, wait)) {
1886 adreno_poke(device);
1887
1888 /* Check to see if the core is hung */
1889 if (adreno_hang_detect(device, regs))
1890 return -ETIMEDOUT;
1891
1892 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1893 }
1894 GSL_RB_GET_READPTR(rb, &rb->rptr);
1895
1896 if (time_after(jiffies, timeout)) {
1897 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1898 rb->rptr, rb->wptr);
1899 return -ETIMEDOUT;
1900 }
1901 } while (rb->rptr != rb->wptr);
1902
1903 return 0;
1904}
1905
1906/* Caller must hold the device mutex. */
1907int adreno_idle(struct kgsl_device *device)
1908{
1909 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001910 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301911 unsigned long wait_time;
1912 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001913 unsigned int prev_reg_val[hang_detect_regs_count];
1914
1915 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001916
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001917 kgsl_cffdump_regpoll(device->id,
1918 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001919 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001920
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001921retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001922 /* First, wait for the ringbuffer to drain */
1923 if (adreno_ringbuffer_drain(device, prev_reg_val))
1924 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001925
1926 /* now, wait for the GPU to finish its operations */
Jordan Crousea29a2e02012-08-14 09:09:23 -06001927 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
1928 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1929
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001931 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1932 &rbbm_status);
1933 if (adreno_is_a2xx(adreno_dev)) {
1934 if (rbbm_status == 0x110)
1935 return 0;
1936 } else {
1937 if (!(rbbm_status & 0x80000000))
1938 return 0;
1939 }
Tarun Karra3335f142012-06-19 14:11:48 -07001940
1941 /* Dont wait for timeout, detect hang faster.
1942 */
1943 if (time_after(jiffies, wait_time_part)) {
1944 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001945 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001946 if ((adreno_hang_detect(device, prev_reg_val)))
1947 goto err;
1948 }
1949
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001950 }
1951
1952err:
1953 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001954 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1955 !adreno_dump_and_recover(device)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001956 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001957 goto retry;
1958 }
1959 return -ETIMEDOUT;
1960}
1961
1962static unsigned int adreno_isidle(struct kgsl_device *device)
1963{
1964 int status = false;
1965 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1966 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1967 unsigned int rbbm_status;
1968
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001969 WARN_ON(device->state == KGSL_STATE_INIT);
1970 /* If the device isn't active, don't force it on. */
1971 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001972 /* Is the ring buffer is empty? */
1973 GSL_RB_GET_READPTR(rb, &rb->rptr);
1974 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1975 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001976 adreno_regread(device,
1977 adreno_dev->gpudev->reg_rbbm_status,
1978 &rbbm_status);
1979
1980 if (adreno_is_a2xx(adreno_dev)) {
1981 if (rbbm_status == 0x110)
1982 status = true;
1983 } else {
1984 if (!(rbbm_status & 0x80000000))
1985 status = true;
1986 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001987 }
1988 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001989 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001990 }
1991 return status;
1992}
1993
1994/* Caller must hold the device mutex. */
1995static int adreno_suspend_context(struct kgsl_device *device)
1996{
1997 int status = 0;
1998 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1999
2000 /* switch to NULL ctxt */
2001 if (adreno_dev->drawctxt_active != NULL) {
2002 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002003 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002004 }
2005
2006 return status;
2007}
2008
Jordan Crouse233b2092012-04-18 09:31:09 -06002009/* Find a memory structure attached to an adreno context */
2010
2011struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2012 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2013{
2014 struct kgsl_context *context;
2015 struct adreno_context *adreno_context = NULL;
2016 int next = 0;
2017
2018 while (1) {
2019 context = idr_get_next(&device->context_idr, &next);
2020 if (context == NULL)
2021 break;
2022
2023 adreno_context = (struct adreno_context *)context->devctxt;
2024
2025 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
2026 struct kgsl_memdesc *desc;
2027
2028 desc = &adreno_context->gpustate;
2029 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2030 return desc;
2031
2032 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2033 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2034 return desc;
2035 }
2036 next = next + 1;
2037 }
2038
2039 return NULL;
2040}
2041
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002042struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002043 unsigned int pt_base,
2044 unsigned int gpuaddr,
2045 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002046{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002047 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002048 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2049 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2050
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002051 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2052 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002053
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002054 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2055 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002056
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002057 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2058 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002059
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002060 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2061 size))
2062 return &device->mmu.setstate_memory;
2063
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002064 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
2065
2066 if (entry)
2067 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002068
Jordan Crouse233b2092012-04-18 09:31:09 -06002069 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002070}
2071
2072uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2073 unsigned int gpuaddr, unsigned int size)
2074{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002075 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002076
2077 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2078
2079 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002080}
2081
2082void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2083 unsigned int *value)
2084{
2085 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002086 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2087 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088
2089 if (!in_interrupt())
2090 kgsl_pre_hwaccess(device);
2091
2092 /*ensure this read finishes before the next one.
2093 * i.e. act like normal readl() */
2094 *value = __raw_readl(reg);
2095 rmb();
2096}
2097
2098void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2099 unsigned int value)
2100{
2101 unsigned int *reg;
2102
Jordan Crouse7501d452012-04-19 08:58:44 -06002103 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002104
2105 if (!in_interrupt())
2106 kgsl_pre_hwaccess(device);
2107
2108 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002109 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002110
2111 /*ensure previous writes post before this one,
2112 * i.e. act like normal writel() */
2113 wmb();
2114 __raw_writel(value, reg);
2115}
2116
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002117static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2118{
2119 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002120 if (k_ctxt != NULL) {
2121 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002122 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2123 context_id = KGSL_CONTEXT_INVALID;
2124 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2125 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002126 }
2127
2128 return context_id;
2129}
2130
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002131static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002132 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002133{
2134 int status;
2135 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002136 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002137 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002138
2139 mutex_lock(&device->mutex);
2140 context_id = _get_context_id(context);
2141 /*
2142 * If the context ID is invalid, we are in a race with
2143 * the context being destroyed by userspace so bail.
2144 */
2145 if (context_id == KGSL_CONTEXT_INVALID) {
2146 KGSL_DRV_WARN(device, "context was detached");
2147 status = -EINVAL;
2148 goto unlock;
2149 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002150
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002151 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002152 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002153 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002154 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002155 mb();
2156
2157 if (enableflag) {
2158 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002159 KGSL_MEMSTORE_OFFSET(context_id,
2160 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002161 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002162 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002163 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002164 KGSL_MEMSTORE_OFFSET(context_id,
2165 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002166 wmb();
2167 }
2168 } else {
2169 unsigned int cmds[2];
2170 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002171 KGSL_MEMSTORE_OFFSET(context_id,
2172 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002173 enableflag = 1;
2174 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002175 KGSL_MEMSTORE_OFFSET(context_id,
2176 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002177 wmb();
2178 /* submit a dummy packet so that even if all
2179 * commands upto timestamp get executed we will still
2180 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002181 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002182 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002183
2184 if (adreno_dev->drawctxt_active)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002185 adreno_ringbuffer_issuecmds_intr(device,
2186 context, &cmds[0], 2);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002187 else
2188 /* We would never call this function if there
2189 * was no active contexts running */
2190 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002191 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002192 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002193unlock:
2194 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002195
2196 return status;
2197}
2198
2199/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002200 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002201 placing a process in wait q. For conditional interrupts we expect the
2202 process to already be in its wait q when its exit condition checking
2203 function is called.
2204*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002205#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002206({ \
2207 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002208 if (io) \
2209 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2210 else \
2211 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002212 __ret; \
2213})
2214
Tarun Karra3335f142012-06-19 14:11:48 -07002215
2216
2217unsigned int adreno_hang_detect(struct kgsl_device *device,
2218 unsigned int *prev_reg_val)
2219{
2220 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2221 unsigned int curr_reg_val[hang_detect_regs_count];
2222 unsigned int hang_detected = 1;
2223 unsigned int i;
2224
2225 if (!adreno_dev->fast_hang_detect)
2226 return 0;
2227
2228 for (i = 0; i < hang_detect_regs_count; i++) {
2229 adreno_regread(device, hang_detect_regs[i],
2230 &curr_reg_val[i]);
2231 if (curr_reg_val[i] != prev_reg_val[i]) {
2232 prev_reg_val[i] = curr_reg_val[i];
2233 hang_detected = 0;
2234 }
2235 }
2236
2237 return hang_detected;
2238}
2239
2240
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002241/* MUST be called with the device mutex held */
2242static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002243 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002244 unsigned int timestamp,
2245 unsigned int msecs)
2246{
2247 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06002248 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06002249 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002250 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06002251 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07002252 int retries = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002253 unsigned int ts_issued;
2254 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07002255 unsigned int time_elapsed = 0;
2256 unsigned int prev_reg_val[hang_detect_regs_count];
Jordan Crouse21f75a02012-08-09 15:08:59 -06002257 unsigned int wait;
Tarun Karra3335f142012-06-19 14:11:48 -07002258
2259 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002260
2261 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002262
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302263 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07002264 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302265 msecs = adreno_dev->wait_timeout;
2266
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002267 if (timestamp_cmp(timestamp, ts_issued) > 0) {
2268 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
2269 "last issued ts <%d:0x%x>\n",
2270 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002271 status = -EINVAL;
2272 goto done;
2273 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002274
Jordan Crouse21f75a02012-08-09 15:08:59 -06002275 /*
2276 * Make the first timeout interval 100 msecs and then try to kick the
2277 * wptr again. This helps to ensure the wptr is updated properly. If
2278 * the requested timeout is less than 100 msecs, then wait 20msecs which
2279 * is the minimum amount of time we can safely wait at 100HZ
Lynus Vaz06a9a902011-10-04 19:25:33 +05302280 */
Jordan Crouse21f75a02012-08-09 15:08:59 -06002281
2282 if (msecs == 0 || msecs >= 100)
2283 wait = 100;
2284 else
2285 wait = 20;
2286
Tarun Karra3335f142012-06-19 14:11:48 -07002287 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002288 /*
2289 * If the context ID is invalid, we are in a race with
2290 * the context being destroyed by userspace so bail.
2291 */
2292 if (context_id == KGSL_CONTEXT_INVALID) {
2293 KGSL_DRV_WARN(device, "context was detached");
2294 status = -EINVAL;
2295 goto done;
2296 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002297 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07002298 /* if the timestamp happens while we're not
2299 * waiting, there's a chance that an interrupt
2300 * will not be generated and thus the timestamp
2301 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05302302 */
Jeremy Gebben63904832012-02-07 16:10:55 -07002303 queue_work(device->work_queue, &device->ts_expired_ws);
2304 status = 0;
2305 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002306 }
Jeremy Gebben63904832012-02-07 16:10:55 -07002307 adreno_poke(device);
2308 io_cnt = (io_cnt + 1) % 100;
2309 if (io_cnt <
2310 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2311 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002312
2313 if ((retries > 0) &&
2314 (adreno_hang_detect(device, prev_reg_val)))
2315 goto hang_dump;
2316
Jeremy Gebben63904832012-02-07 16:10:55 -07002317 mutex_unlock(&device->mutex);
2318 /* We need to make sure that the process is
2319 * placed in wait-q before its condition is called
2320 */
2321 status = kgsl_wait_event_interruptible_timeout(
2322 device->wait_queue,
2323 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002324 context, timestamp),
Jordan Crouse21f75a02012-08-09 15:08:59 -06002325 msecs_to_jiffies(wait), io);
2326
Jeremy Gebben63904832012-02-07 16:10:55 -07002327 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002328
Jeremy Gebben63904832012-02-07 16:10:55 -07002329 if (status > 0) {
2330 /*completed before the wait finished */
2331 status = 0;
2332 goto done;
2333 } else if (status < 0) {
2334 /*an error occurred*/
2335 goto done;
2336 }
2337 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07002338
Jordan Crouse21f75a02012-08-09 15:08:59 -06002339 time_elapsed += wait;
2340 wait = KGSL_TIMEOUT_PART;
2341
Tarun Karra3335f142012-06-19 14:11:48 -07002342 retries++;
2343
Jordan Crouse21f75a02012-08-09 15:08:59 -06002344 } while (!msecs || time_elapsed < msecs);
Tarun Karra3335f142012-06-19 14:11:48 -07002345
2346hang_dump:
Shubhraprakash Das54396e52012-03-10 13:24:54 -07002347 /*
2348 * Check if timestamp has retired here because we may have hit
2349 * recovery which can take some time and cause waiting threads
2350 * to timeout
2351 */
2352 if (kgsl_check_timestamp(device, context, timestamp))
2353 goto done;
Jeremy Gebben63904832012-02-07 16:10:55 -07002354 status = -ETIMEDOUT;
2355 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002356 "Device hang detected while waiting for timestamp: "
2357 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2358 "wptr: 0x%x\n",
2359 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002360 adreno_dev->ringbuffer.wptr);
2361 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002362 /* The timestamp that this process wanted
2363 * to wait on may be invalid or expired now
2364 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07002365 status = 0;
2366 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002367done:
2368 return (int)status;
2369}
2370
2371static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002372 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002373{
2374 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002375 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002376
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002377 /*
2378 * If the context ID is invalid, we are in a race with
2379 * the context being destroyed by userspace so bail.
2380 */
2381 if (context_id == KGSL_CONTEXT_INVALID) {
2382 KGSL_DRV_WARN(device, "context was detached");
2383 return timestamp;
2384 }
Jordan Crousec659f382012-04-16 11:10:41 -06002385 switch (type) {
2386 case KGSL_TIMESTAMP_QUEUED: {
2387 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2388 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2389
2390 timestamp = rb->timestamp[context_id];
2391 break;
2392 }
2393 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002394 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002395 break;
2396 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002397 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002398 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2399 break;
2400 }
2401
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002402 rmb();
2403
2404 return timestamp;
2405}
2406
2407static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2408 unsigned int cmd, void *data)
2409{
2410 int result = 0;
2411 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2412 struct kgsl_context *context;
2413
2414 switch (cmd) {
2415 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2416 binbase = data;
2417
2418 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2419 if (context) {
2420 adreno_drawctxt_set_bin_base_offset(
2421 dev_priv->device, context, binbase->offset);
2422 } else {
2423 result = -EINVAL;
2424 KGSL_DRV_ERR(dev_priv->device,
2425 "invalid drawctxt drawctxt_id %d "
2426 "device_id=%d\n",
2427 binbase->drawctxt_id, dev_priv->device->id);
2428 }
2429 break;
2430
2431 default:
2432 KGSL_DRV_INFO(dev_priv->device,
2433 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002434 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002435 break;
2436 }
2437 return result;
2438
2439}
2440
2441static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2442{
2443 gpu_freq /= 1000000;
2444 return ticks / gpu_freq;
2445}
2446
2447static void adreno_power_stats(struct kgsl_device *device,
2448 struct kgsl_power_stats *stats)
2449{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002450 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002451 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002452 unsigned int cycles;
2453
2454 /* Get the busy cycles counted since the counter was last reset */
2455 /* Calling this function also resets and restarts the counter */
2456
2457 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002458
2459 /* In order to calculate idle you have to have run the algorithm *
2460 * at least once to get a start time. */
2461 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002462 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002463 stats->total_time = tmp - pwr->time;
2464 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002465 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002466 pwrlevels[device->pwrctrl.active_pwrlevel].
2467 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002468 } else {
2469 stats->total_time = 0;
2470 stats->busy_time = 0;
2471 pwr->time = ktime_to_us(ktime_get());
2472 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002473}
2474
2475void adreno_irqctrl(struct kgsl_device *device, int state)
2476{
Jordan Crousea78c9172011-07-11 13:14:09 -06002477 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2478 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002479}
2480
Jordan Croused6535882012-06-20 08:22:16 -06002481static unsigned int adreno_gpuid(struct kgsl_device *device,
2482 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002483{
2484 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2485
Jordan Croused6535882012-06-20 08:22:16 -06002486 /* Some applications need to know the chip ID too, so pass
2487 * that as a parameter */
2488
2489 if (chipid != NULL)
2490 *chipid = adreno_dev->chip_id;
2491
Jordan Crousea0758f22011-12-07 11:19:22 -07002492 /* Standard KGSL gpuid format:
2493 * top word is 0x0002 for 2D or 0x0003 for 3D
2494 * Bottom word is core specific identifer
2495 */
2496
2497 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2498}
2499
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002500static const struct kgsl_functable adreno_functable = {
2501 /* Mandatory functions */
2502 .regread = adreno_regread,
2503 .regwrite = adreno_regwrite,
2504 .idle = adreno_idle,
2505 .isidle = adreno_isidle,
2506 .suspend_context = adreno_suspend_context,
2507 .start = adreno_start,
2508 .stop = adreno_stop,
2509 .getproperty = adreno_getproperty,
2510 .waittimestamp = adreno_waittimestamp,
2511 .readtimestamp = adreno_readtimestamp,
2512 .issueibcmds = adreno_ringbuffer_issueibcmds,
2513 .ioctl = adreno_ioctl,
2514 .setup_pt = adreno_setup_pt,
2515 .cleanup_pt = adreno_cleanup_pt,
2516 .power_stats = adreno_power_stats,
2517 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002518 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002519 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002520 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002521 /* Optional functions */
2522 .setstate = adreno_setstate,
2523 .drawctxt_create = adreno_drawctxt_create,
2524 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002525 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002526 .postmortem_dump = adreno_dump,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002527};
2528
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529static struct platform_driver adreno_platform_driver = {
2530 .probe = adreno_probe,
2531 .remove = __devexit_p(adreno_remove),
2532 .suspend = kgsl_suspend_driver,
2533 .resume = kgsl_resume_driver,
2534 .id_table = adreno_id_table,
2535 .driver = {
2536 .owner = THIS_MODULE,
2537 .name = DEVICE_3D_NAME,
2538 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002539 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002540 }
2541};
2542
2543static int __init kgsl_3d_init(void)
2544{
2545 return platform_driver_register(&adreno_platform_driver);
2546}
2547
2548static void __exit kgsl_3d_exit(void)
2549{
2550 platform_driver_unregister(&adreno_platform_driver);
2551}
2552
2553module_init(kgsl_3d_init);
2554module_exit(kgsl_3d_exit);
2555
2556MODULE_DESCRIPTION("3D Graphics driver");
2557MODULE_VERSION("1.2");
2558MODULE_LICENSE("GPL v2");
2559MODULE_ALIAS("platform:kgsl_3d");