blob: 3b715d946e316464b788b39c54422074ef258c8a [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 */
Carter Cooper95f7f792012-08-19 13:40:34 -0600182 { ADRENO_REV_A320, 3, 2, ANY_ID, 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 Dharmapurikarb6c05772012-08-26 18:27:53 -0700915 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
916 goto err;
917
918 info->sensors = kzalloc(info->num_cores *
919 sizeof(int),
920 GFP_KERNEL);
921
922 for (count = 0; count < info->num_cores; count++) {
923 if (adreno_of_read_property(node, "qcom,sensors",
924 &(info->sensors[count])))
925 goto err;
926 }
927
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700928 if (adreno_of_read_property(node, "qcom,core-core-type",
929 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600930 goto err;
931
932 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
933 &info->algo_param.disable_pc_threshold))
934 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700935 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
936 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600937 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700938 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
939 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600940 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600941 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
942 &info->algo_param.em_max_util_pct))
943 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700944 if (adreno_of_read_property(node, "qcom,algo-group-id",
945 &info->algo_param.group_id))
946 goto err;
947 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
948 &info->algo_param.max_freq_chg_time_us))
949 goto err;
950 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
951 &info->algo_param.slack_mode_dynamic))
952 goto err;
953 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
954 &info->algo_param.slack_weight_thresh_pct))
955 goto err;
956 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
957 &info->algo_param.slack_time_min_us))
958 goto err;
959 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
960 &info->algo_param.slack_time_max_us))
961 goto err;
962 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
963 &info->algo_param.ss_win_size_min_us))
964 goto err;
965 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
966 &info->algo_param.ss_win_size_max_us))
967 goto err;
968 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
969 &info->algo_param.ss_util_pct))
970 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600971 if (adreno_of_read_property(node, "qcom,algo-ss-iobusy-conv",
972 &info->algo_param.ss_iobusy_conv))
973 goto err;
974
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700975 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
976 &info->energy_coeffs.active_coeff_a))
977 goto err;
978 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
979 &info->energy_coeffs.active_coeff_b))
980 goto err;
981 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
982 &info->energy_coeffs.active_coeff_c))
983 goto err;
984 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
985 &info->energy_coeffs.leakage_coeff_a))
986 goto err;
987 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
988 &info->energy_coeffs.leakage_coeff_b))
989 goto err;
990 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
991 &info->energy_coeffs.leakage_coeff_c))
992 goto err;
993 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
994 &info->energy_coeffs.leakage_coeff_d))
995 goto err;
996
997 if (adreno_of_read_property(node, "qcom,power-current-temp",
998 &info->power_param.current_temp))
999 goto err;
1000
Lokesh Batra805e1e12012-08-03 08:34:06 -06001001 return info;
1002
1003err:
1004 if (info)
1005 kfree(info->freq_tbl);
1006
1007 kfree(info);
1008
1009 return ERR_PTR(ret);
1010}
1011
1012static int adreno_of_get_iommu(struct device_node *parent,
1013 struct kgsl_device_platform_data *pdata)
1014{
1015 struct device_node *node, *child;
1016 struct kgsl_device_iommu_data *data = NULL;
1017 struct kgsl_iommu_ctx *ctxs = NULL;
1018 u32 reg_val[2];
1019 int ctx_index = 0;
1020
1021 node = of_parse_phandle(parent, "iommu", 0);
1022 if (node == NULL)
1023 return -EINVAL;
1024
1025 data = kzalloc(sizeof(*data), GFP_KERNEL);
1026 if (data == NULL) {
1027 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
1028 goto err;
1029 }
1030
1031 if (of_property_read_u32_array(node, "reg", reg_val, 2))
1032 goto err;
1033
1034 data->physstart = reg_val[0];
1035 data->physend = data->physstart + reg_val[1] - 1;
1036
1037 data->iommu_ctx_count = 0;
1038
1039 for_each_child_of_node(node, child)
1040 data->iommu_ctx_count++;
1041
1042 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
1043 GFP_KERNEL);
1044
1045 if (ctxs == NULL) {
1046 KGSL_CORE_ERR("kzalloc(%d) failed\n",
1047 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
1048 goto err;
1049 }
1050
1051 for_each_child_of_node(node, child) {
1052 int ret = of_property_read_string(child, "label",
1053 &ctxs[ctx_index].iommu_ctx_name);
1054
1055 if (ret) {
1056 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
1057 goto err;
1058 }
1059
1060 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
1061 &ctxs[ctx_index].ctx_id))
1062 goto err;
1063
1064 ctx_index++;
1065 }
1066
1067 data->iommu_ctxs = ctxs;
1068
1069 pdata->iommu_data = data;
1070 pdata->iommu_count = 1;
1071
1072 return 0;
1073
1074err:
1075 kfree(ctxs);
1076 kfree(data);
1077
1078 return -EINVAL;
1079}
1080
1081static int adreno_of_get_pdata(struct platform_device *pdev)
1082{
1083 struct kgsl_device_platform_data *pdata = NULL;
1084 struct kgsl_device *device;
1085 int ret = -EINVAL;
1086
1087 pdev->id_entry = adreno_id_table;
1088
1089 pdata = pdev->dev.platform_data;
1090 if (pdata)
1091 return 0;
1092
1093 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1094 KGSL_CORE_ERR("Unable to read 'label'\n");
1095 goto err;
1096 }
1097
1098 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1099 goto err;
1100
1101 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1102 if (pdata == NULL) {
1103 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1104 ret = -ENOMEM;
1105 goto err;
1106 }
1107
1108 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1109 &pdata->chipid))
1110 goto err;
1111
1112 /* pwrlevel Data */
1113 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1114 if (ret)
1115 goto err;
1116
1117 /* Default value is 83, if not found in DT */
1118 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1119 &pdata->idle_timeout))
1120 pdata->idle_timeout = 83;
1121
1122 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1123 &pdata->nap_allowed))
1124 pdata->nap_allowed = 1;
1125
1126 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1127 &pdata->clk_map))
1128 goto err;
1129
1130 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1131
1132 if (device->id != KGSL_DEVICE_3D0)
1133 goto err;
1134
1135 /* Bus Scale Data */
1136
1137 pdata->bus_scale_table = adreno_of_get_bus_scale(pdev->dev.of_node);
1138 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1139 ret = PTR_ERR(pdata->bus_scale_table);
1140 goto err;
1141 }
1142
1143 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1144 if (IS_ERR_OR_NULL(pdata->core_info)) {
1145 ret = PTR_ERR(pdata->core_info);
1146 goto err;
1147 }
1148
1149 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1150 if (ret)
1151 goto err;
1152
1153 pdev->dev.platform_data = pdata;
1154 return 0;
1155
1156err:
1157 if (pdata) {
1158 adreno_of_free_bus_scale_info(pdata->bus_scale_table);
1159 if (pdata->core_info)
1160 kfree(pdata->core_info->freq_tbl);
1161 kfree(pdata->core_info);
1162
1163 if (pdata->iommu_data)
1164 kfree(pdata->iommu_data->iommu_ctxs);
1165
1166 kfree(pdata->iommu_data);
1167 }
1168
1169 kfree(pdata);
1170
1171 return ret;
1172}
1173
liu zhong7dfa2a32012-04-27 19:11:01 -07001174#ifdef CONFIG_MSM_OCMEM
1175static int
1176adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1177{
1178 if (adreno_dev->gpurev != ADRENO_REV_A330)
1179 return 0;
1180
1181 /* OCMEM is only needed once, do not support consective allocation */
1182 if (adreno_dev->ocmem_hdl != NULL)
1183 return 0;
1184
1185 adreno_dev->ocmem_hdl =
1186 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1187 if (adreno_dev->ocmem_hdl == NULL)
1188 return -ENOMEM;
1189
1190 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
1191 adreno_dev->gmem_base = adreno_dev->ocmem_hdl->addr;
1192
1193 return 0;
1194}
1195
1196static void
1197adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1198{
1199 if (adreno_dev->gpurev != ADRENO_REV_A330)
1200 return;
1201
1202 if (adreno_dev->ocmem_hdl == NULL)
1203 return;
1204
1205 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1206 adreno_dev->ocmem_hdl = NULL;
1207}
1208#else
1209static int
1210adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1211{
1212 return 0;
1213}
1214
1215static void
1216adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1217{
1218}
1219#endif
1220
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221static int __devinit
1222adreno_probe(struct platform_device *pdev)
1223{
1224 struct kgsl_device *device;
1225 struct adreno_device *adreno_dev;
1226 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001227 bool is_dt;
1228
1229 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1230
1231 if (is_dt && pdev->dev.of_node) {
1232 status = adreno_of_get_pdata(pdev);
1233 if (status)
1234 goto error_return;
1235 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001236
1237 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1238 adreno_dev = ADRENO_DEVICE(device);
1239 device->parentdev = &pdev->dev;
1240
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001241 status = adreno_ringbuffer_init(device);
1242 if (status != 0)
1243 goto error;
1244
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001245 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246 if (status)
1247 goto error_close_rb;
1248
1249 adreno_debugfs_init(device);
1250
1251 kgsl_pwrscale_init(device);
1252 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1253
1254 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1255 return 0;
1256
1257error_close_rb:
1258 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1259error:
1260 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001261error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001262 return status;
1263}
1264
1265static int __devexit adreno_remove(struct platform_device *pdev)
1266{
1267 struct kgsl_device *device;
1268 struct adreno_device *adreno_dev;
1269
1270 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1271 adreno_dev = ADRENO_DEVICE(device);
1272
1273 kgsl_pwrscale_detach_policy(device);
1274 kgsl_pwrscale_close(device);
1275
1276 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1277 kgsl_device_platform_remove(device);
1278
1279 return 0;
1280}
1281
1282static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1283{
1284 int status = -EINVAL;
1285 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001287 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1288 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289
1290 /* Power up the device */
1291 kgsl_pwrctrl_enable(device);
1292
1293 /* Identify the specific GPU */
1294 adreno_identify_gpu(adreno_dev);
1295
Jordan Crouse505df9c2011-07-28 08:37:59 -06001296 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1297 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1298 adreno_dev->chip_id);
1299 goto error_clk_off;
1300 }
1301
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001302 /* Set up the MMU */
1303 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001304 /*
1305 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1306 * on older gpus
1307 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001308 if (adreno_is_a20x(adreno_dev)) {
1309 device->mh.mh_intf_cfg1 = 0;
1310 device->mh.mh_intf_cfg2 = 0;
1311 }
1312
1313 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001314 }
1315
Tarun Karra3335f142012-06-19 14:11:48 -07001316 /* Assign correct RBBM status register to hang detect regs
1317 */
1318 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1319
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001320 status = kgsl_mmu_start(device);
1321 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322 goto error_clk_off;
1323
liu zhong7dfa2a32012-04-27 19:11:01 -07001324 status = adreno_ocmem_gmem_malloc(adreno_dev);
1325 if (status) {
1326 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1327 goto error_mmu_off;
1328 }
1329
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001330 /* Start the GPU */
1331 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001332
1333 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001334 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335
1336 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001337 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001338 /* While recovery is on we do not want timer to
1339 * fire and attempt to change any device state */
1340 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1341 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001342 return 0;
1343 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001344
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001345 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001346
1347error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001348 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001349
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350error_clk_off:
1351 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001352
1353 return status;
1354}
1355
1356static int adreno_stop(struct kgsl_device *device)
1357{
1358 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1359
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360 adreno_dev->drawctxt_active = NULL;
1361
1362 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1363
Shubhraprakash Das79447952012-04-26 18:12:23 -06001364 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001366 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301367 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001368 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001369
liu zhong7dfa2a32012-04-27 19:11:01 -07001370 adreno_ocmem_gmem_free(adreno_dev);
1371
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001372 /* Power down the device */
1373 kgsl_pwrctrl_disable(device);
1374
1375 return 0;
1376}
1377
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001378static void adreno_mark_context_status(struct kgsl_device *device,
1379 int recovery_status)
1380{
1381 struct kgsl_context *context;
1382 int next = 0;
1383 /*
1384 * Set the reset status of all contexts to
1385 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1386 * since thats the guilty party, if recovery failed then
1387 * mark all as guilty
1388 */
1389 while ((context = idr_get_next(&device->context_idr, &next))) {
1390 struct adreno_context *adreno_context = context->devctxt;
1391 if (recovery_status) {
1392 context->reset_status =
1393 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1394 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1395 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1396 context->reset_status) {
1397 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1398 CTXT_FLAGS_GPU_HANG_RECOVERED))
1399 context->reset_status =
1400 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1401 else
1402 context->reset_status =
1403 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1404 }
1405 next = next + 1;
1406 }
1407}
1408
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001409static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1410{
1411 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1412 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1413 struct kgsl_context *context;
1414 struct adreno_context *temp_adreno_context;
1415 int next = 0;
1416
1417 while ((context = idr_get_next(&device->context_idr, &next))) {
1418 temp_adreno_context = context->devctxt;
1419 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1420 kgsl_sharedmem_writel(&device->memstore,
1421 KGSL_MEMSTORE_OFFSET(context->id,
1422 soptimestamp),
1423 rb->timestamp[context->id]);
1424 kgsl_sharedmem_writel(&device->memstore,
1425 KGSL_MEMSTORE_OFFSET(context->id,
1426 eoptimestamp),
1427 rb->timestamp[context->id]);
1428 }
1429 next = next + 1;
1430 }
1431}
1432
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001433static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1434{
1435 vfree(rec_data->rb_buffer);
1436 vfree(rec_data->bad_rb_buffer);
1437}
1438
1439static int adreno_setup_recovery_data(struct kgsl_device *device,
1440 struct adreno_recovery_data *rec_data)
1441{
1442 int ret = 0;
1443 unsigned int ib1_sz, ib2_sz;
1444 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1445 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1446
1447 memset(rec_data, 0, sizeof(*rec_data));
1448
1449 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1450 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1451 if (ib1_sz || ib2_sz)
1452 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1453
1454 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1455 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1456 current_context));
1457
1458 kgsl_sharedmem_readl(&device->memstore,
1459 &rec_data->global_eop,
1460 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1461 eoptimestamp));
1462
1463 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1464 if (!rec_data->rb_buffer) {
1465 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1466 rb->buffer_desc.size);
1467 return -ENOMEM;
1468 }
1469
1470 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1471 if (!rec_data->bad_rb_buffer) {
1472 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1473 rb->buffer_desc.size);
1474 ret = -ENOMEM;
1475 goto done;
1476 }
1477
1478done:
1479 if (ret) {
1480 vfree(rec_data->rb_buffer);
1481 vfree(rec_data->bad_rb_buffer);
1482 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001483 return ret;
1484}
1485
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001486static int
1487_adreno_recover_hang(struct kgsl_device *device,
1488 struct adreno_recovery_data *rec_data,
1489 bool try_bad_commands)
1490{
1491 int ret;
1492 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1493 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1494 struct kgsl_context *context;
1495 struct adreno_context *adreno_context = NULL;
1496 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1497
1498 context = idr_find(&device->context_idr, rec_data->context_id);
1499 if (context == NULL) {
1500 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1501 rec_data->context_id);
1502 } else {
1503 adreno_context = context->devctxt;
1504 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1505 }
1506
1507 /* Extract valid contents from rb which can still be executed after
1508 * hang */
1509 ret = adreno_ringbuffer_extract(rb, rec_data);
1510 if (ret)
1511 goto done;
1512
1513 /* restart device */
1514 ret = adreno_stop(device);
1515 if (ret) {
1516 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1517 goto done;
1518 }
1519
1520 ret = adreno_start(device, true);
1521 if (ret) {
1522 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1523 goto done;
1524 }
1525
1526 if (context)
1527 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1528 KGSL_MEMSTORE_GLOBAL);
1529
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001530 /* If iommu is used then we need to make sure that the iommu clocks
1531 * are on since there could be commands in pipeline that touch iommu */
1532 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1533 ret = kgsl_mmu_enable_clk(&device->mmu,
1534 KGSL_IOMMU_CONTEXT_USER);
1535 if (ret)
1536 goto done;
1537 }
1538
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001539 /* Do not try the bad caommands if recovery has failed bad commands
1540 * once already */
1541 if (!try_bad_commands)
1542 rec_data->bad_rb_size = 0;
1543
1544 if (rec_data->bad_rb_size) {
1545 int idle_ret;
1546 /* submit the bad and good context commands and wait for
1547 * them to pass */
1548 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1549 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001550 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001551 if (idle_ret) {
1552 ret = adreno_stop(device);
1553 if (ret) {
1554 KGSL_DRV_ERR(device,
1555 "Device stop failed in recovery\n");
1556 goto done;
1557 }
1558 ret = adreno_start(device, true);
1559 if (ret) {
1560 KGSL_DRV_ERR(device,
1561 "Device start failed in recovery\n");
1562 goto done;
1563 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001564 if (context)
1565 kgsl_mmu_setstate(&device->mmu,
1566 adreno_context->pagetable,
1567 KGSL_MEMSTORE_GLOBAL);
1568
1569 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1570 ret = kgsl_mmu_enable_clk(&device->mmu,
1571 KGSL_IOMMU_CONTEXT_USER);
1572 if (ret)
1573 goto done;
1574 }
1575
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001576 ret = idle_ret;
1577 KGSL_DRV_ERR(device,
1578 "Bad context commands hung in recovery\n");
1579 } else {
1580 KGSL_DRV_ERR(device,
1581 "Bad context commands succeeded in recovery\n");
1582 if (adreno_context)
1583 adreno_context->flags = (adreno_context->flags &
1584 ~CTXT_FLAGS_GPU_HANG) |
1585 CTXT_FLAGS_GPU_HANG_RECOVERED;
1586 adreno_dev->drawctxt_active = last_active_ctx;
1587 }
1588 }
1589 /* If either the bad command sequence failed or we did not play it */
1590 if (ret || !rec_data->bad_rb_size) {
1591 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1592 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001593 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001594 if (ret) {
1595 /* If we fail here we can try to invalidate another
1596 * context and try recovering again */
1597 ret = -EAGAIN;
1598 goto done;
1599 }
1600 /* ringbuffer now has data from the last valid context id,
1601 * so restore the active_ctx to the last valid context */
1602 if (rec_data->last_valid_ctx_id) {
1603 struct kgsl_context *last_ctx =
1604 idr_find(&device->context_idr,
1605 rec_data->last_valid_ctx_id);
1606 if (last_ctx)
1607 adreno_dev->drawctxt_active = last_ctx->devctxt;
1608 }
1609 }
1610done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001611 /* Turn off iommu clocks */
1612 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1613 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001614 return ret;
1615}
1616
1617static int
1618adreno_recover_hang(struct kgsl_device *device,
1619 struct adreno_recovery_data *rec_data)
1620{
1621 int ret = 0;
1622 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1623 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1624 unsigned int timestamp;
1625
1626 KGSL_DRV_ERR(device,
1627 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1628 "Bad context_id: %u, global_eop: 0x%x\n",
1629 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1630
1631 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1632 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1633
1634 /* We may need to replay commands multiple times based on whether
1635 * multiple contexts hang the GPU */
1636 while (true) {
1637 if (!ret)
1638 ret = _adreno_recover_hang(device, rec_data, true);
1639 else
1640 ret = _adreno_recover_hang(device, rec_data, false);
1641
1642 if (-EAGAIN == ret) {
1643 /* setup new recovery parameters and retry, this
1644 * means more than 1 contexts are causing hang */
1645 adreno_destroy_recovery_data(rec_data);
1646 adreno_setup_recovery_data(device, rec_data);
1647 KGSL_DRV_ERR(device,
1648 "Retry recovery from 3D GPU hang. Recovery parameters: "
1649 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1650 rec_data->ib1, rec_data->context_id,
1651 rec_data->global_eop);
1652 } else {
1653 break;
1654 }
1655 }
1656
1657 if (ret)
1658 goto done;
1659
1660 /* Restore correct states after recovery */
1661 if (adreno_dev->drawctxt_active)
1662 device->mmu.hwpagetable =
1663 adreno_dev->drawctxt_active->pagetable;
1664 else
1665 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1666 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1667 kgsl_sharedmem_writel(&device->memstore,
1668 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1669 eoptimestamp),
1670 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1671done:
1672 adreno_set_max_ts_for_bad_ctxs(device);
1673 adreno_mark_context_status(device, ret);
1674 if (!ret)
1675 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1676 else
1677 KGSL_DRV_ERR(device, "Recovery failed\n");
1678 return ret;
1679}
1680
1681int
1682adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001683{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001684 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001685 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001686
1687 if (device->state == KGSL_STATE_HUNG)
1688 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001689 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001690 mutex_unlock(&device->mutex);
1691 wait_for_completion(&device->recovery_gate);
1692 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001693 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001694 result = 0;
1695 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001696 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001697 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001698 /* Detected a hang */
1699
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001700 /* Get the recovery data as soon as hang is detected */
1701 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001702 /*
1703 * Trigger an automatic dump of the state to
1704 * the console
1705 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001706 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001707
1708 /*
1709 * Make a GPU snapshot. For now, do it after the PM dump so we
1710 * can at least be sure the PM dump will work as it always has
1711 */
1712 kgsl_device_snapshot(device, 1);
1713
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001714 result = adreno_recover_hang(device, &rec_data);
1715 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001716 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001717 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001718 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001719 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001720 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1721 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001722 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001723 }
1724done:
1725 return result;
1726}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001727EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001728
1729static int adreno_getproperty(struct kgsl_device *device,
1730 enum kgsl_property_type type,
1731 void *value,
1732 unsigned int sizebytes)
1733{
1734 int status = -EINVAL;
1735 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1736
1737 switch (type) {
1738 case KGSL_PROP_DEVICE_INFO:
1739 {
1740 struct kgsl_devinfo devinfo;
1741
1742 if (sizebytes != sizeof(devinfo)) {
1743 status = -EINVAL;
1744 break;
1745 }
1746
1747 memset(&devinfo, 0, sizeof(devinfo));
1748 devinfo.device_id = device->id+1;
1749 devinfo.chip_id = adreno_dev->chip_id;
1750 devinfo.mmu_enabled = kgsl_mmu_enabled();
1751 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001752 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1753 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001754
1755 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1756 0) {
1757 status = -EFAULT;
1758 break;
1759 }
1760 status = 0;
1761 }
1762 break;
1763 case KGSL_PROP_DEVICE_SHADOW:
1764 {
1765 struct kgsl_shadowprop shadowprop;
1766
1767 if (sizebytes != sizeof(shadowprop)) {
1768 status = -EINVAL;
1769 break;
1770 }
1771 memset(&shadowprop, 0, sizeof(shadowprop));
1772 if (device->memstore.hostptr) {
1773 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1774 * anything to mmap().
1775 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001776 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001777 shadowprop.size = device->memstore.size;
1778 /* GSL needs this to be set, even if it
1779 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001780 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1781 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782 }
1783 if (copy_to_user(value, &shadowprop,
1784 sizeof(shadowprop))) {
1785 status = -EFAULT;
1786 break;
1787 }
1788 status = 0;
1789 }
1790 break;
1791 case KGSL_PROP_MMU_ENABLE:
1792 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001793 int mmu_prop = kgsl_mmu_enabled();
1794
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001795 if (sizebytes != sizeof(int)) {
1796 status = -EINVAL;
1797 break;
1798 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001799 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001800 status = -EFAULT;
1801 break;
1802 }
1803 status = 0;
1804 }
1805 break;
1806 case KGSL_PROP_INTERRUPT_WAITS:
1807 {
1808 int int_waits = 1;
1809 if (sizebytes != sizeof(int)) {
1810 status = -EINVAL;
1811 break;
1812 }
1813 if (copy_to_user(value, &int_waits, sizeof(int))) {
1814 status = -EFAULT;
1815 break;
1816 }
1817 status = 0;
1818 }
1819 break;
1820 default:
1821 status = -EINVAL;
1822 }
1823
1824 return status;
1825}
1826
Jordan Crousef7370f82012-04-18 09:31:07 -06001827static int adreno_setproperty(struct kgsl_device *device,
1828 enum kgsl_property_type type,
1829 void *value,
1830 unsigned int sizebytes)
1831{
1832 int status = -EINVAL;
1833
1834 switch (type) {
1835 case KGSL_PROP_PWRCTRL: {
1836 unsigned int enable;
1837 struct kgsl_device_platform_data *pdata =
1838 kgsl_device_get_drvdata(device);
1839
1840 if (sizebytes != sizeof(enable))
1841 break;
1842
1843 if (copy_from_user(&enable, (void __user *) value,
1844 sizeof(enable))) {
1845 status = -EFAULT;
1846 break;
1847 }
1848
1849 if (enable) {
1850 if (pdata->nap_allowed)
1851 device->pwrctrl.nap_allowed = true;
1852
1853 kgsl_pwrscale_enable(device);
1854 } else {
1855 device->pwrctrl.nap_allowed = false;
1856 kgsl_pwrscale_disable(device);
1857 }
1858
1859 status = 0;
1860 }
1861 break;
1862 default:
1863 break;
1864 }
1865
1866 return status;
1867}
1868
Lynus Vaz06a9a902011-10-04 19:25:33 +05301869static inline void adreno_poke(struct kgsl_device *device)
1870{
1871 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1872 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1873}
1874
Jordan Crousea29a2e02012-08-14 09:09:23 -06001875static int adreno_ringbuffer_drain(struct kgsl_device *device,
1876 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001877{
1878 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1879 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001880 unsigned long wait;
1881 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1882
1883 if (!(rb->flags & KGSL_FLAGS_STARTED))
1884 return 0;
1885
1886 /*
1887 * The first time into the loop, wait for 100 msecs and kick wptr again
1888 * to ensure that the hardware has updated correctly. After that, kick
1889 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1890 * expires
1891 */
1892
1893 wait = jiffies + msecs_to_jiffies(100);
1894
1895 adreno_poke(device);
1896
1897 do {
1898 if (time_after(jiffies, wait)) {
1899 adreno_poke(device);
1900
1901 /* Check to see if the core is hung */
1902 if (adreno_hang_detect(device, regs))
1903 return -ETIMEDOUT;
1904
1905 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1906 }
1907 GSL_RB_GET_READPTR(rb, &rb->rptr);
1908
1909 if (time_after(jiffies, timeout)) {
1910 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1911 rb->rptr, rb->wptr);
1912 return -ETIMEDOUT;
1913 }
1914 } while (rb->rptr != rb->wptr);
1915
1916 return 0;
1917}
1918
1919/* Caller must hold the device mutex. */
1920int adreno_idle(struct kgsl_device *device)
1921{
1922 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001923 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301924 unsigned long wait_time;
1925 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001926 unsigned int prev_reg_val[hang_detect_regs_count];
1927
1928 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001930 kgsl_cffdump_regpoll(device->id,
1931 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001932 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001933
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001935 /* First, wait for the ringbuffer to drain */
1936 if (adreno_ringbuffer_drain(device, prev_reg_val))
1937 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001938
1939 /* now, wait for the GPU to finish its operations */
Jordan Crousea29a2e02012-08-14 09:09:23 -06001940 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
1941 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1942
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001943 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001944 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1945 &rbbm_status);
1946 if (adreno_is_a2xx(adreno_dev)) {
1947 if (rbbm_status == 0x110)
1948 return 0;
1949 } else {
1950 if (!(rbbm_status & 0x80000000))
1951 return 0;
1952 }
Tarun Karra3335f142012-06-19 14:11:48 -07001953
1954 /* Dont wait for timeout, detect hang faster.
1955 */
1956 if (time_after(jiffies, wait_time_part)) {
1957 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001958 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001959 if ((adreno_hang_detect(device, prev_reg_val)))
1960 goto err;
1961 }
1962
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001963 }
1964
1965err:
1966 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001967 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1968 !adreno_dump_and_recover(device)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001969 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001970 goto retry;
1971 }
1972 return -ETIMEDOUT;
1973}
1974
1975static unsigned int adreno_isidle(struct kgsl_device *device)
1976{
1977 int status = false;
1978 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1979 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1980 unsigned int rbbm_status;
1981
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001982 WARN_ON(device->state == KGSL_STATE_INIT);
1983 /* If the device isn't active, don't force it on. */
1984 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001985 /* Is the ring buffer is empty? */
1986 GSL_RB_GET_READPTR(rb, &rb->rptr);
1987 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1988 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001989 adreno_regread(device,
1990 adreno_dev->gpudev->reg_rbbm_status,
1991 &rbbm_status);
1992
1993 if (adreno_is_a2xx(adreno_dev)) {
1994 if (rbbm_status == 0x110)
1995 status = true;
1996 } else {
1997 if (!(rbbm_status & 0x80000000))
1998 status = true;
1999 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002000 }
2001 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002002 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002003 }
2004 return status;
2005}
2006
2007/* Caller must hold the device mutex. */
2008static int adreno_suspend_context(struct kgsl_device *device)
2009{
2010 int status = 0;
2011 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2012
2013 /* switch to NULL ctxt */
2014 if (adreno_dev->drawctxt_active != NULL) {
2015 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002016 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002017 }
2018
2019 return status;
2020}
2021
Jordan Crouse233b2092012-04-18 09:31:09 -06002022/* Find a memory structure attached to an adreno context */
2023
2024struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2025 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2026{
2027 struct kgsl_context *context;
2028 struct adreno_context *adreno_context = NULL;
2029 int next = 0;
2030
2031 while (1) {
2032 context = idr_get_next(&device->context_idr, &next);
2033 if (context == NULL)
2034 break;
2035
2036 adreno_context = (struct adreno_context *)context->devctxt;
2037
2038 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
2039 struct kgsl_memdesc *desc;
2040
2041 desc = &adreno_context->gpustate;
2042 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2043 return desc;
2044
2045 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2046 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2047 return desc;
2048 }
2049 next = next + 1;
2050 }
2051
2052 return NULL;
2053}
2054
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002055struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002056 unsigned int pt_base,
2057 unsigned int gpuaddr,
2058 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002059{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002060 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002061 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2062 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2063
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002064 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2065 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002066
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002067 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2068 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002069
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002070 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2071 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002072
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002073 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2074 size))
2075 return &device->mmu.setstate_memory;
2076
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002077 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
2078
2079 if (entry)
2080 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002081
Jordan Crouse233b2092012-04-18 09:31:09 -06002082 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002083}
2084
2085uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2086 unsigned int gpuaddr, unsigned int size)
2087{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002088 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002089
2090 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2091
2092 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002093}
2094
2095void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2096 unsigned int *value)
2097{
2098 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002099 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2100 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002101
2102 if (!in_interrupt())
2103 kgsl_pre_hwaccess(device);
2104
2105 /*ensure this read finishes before the next one.
2106 * i.e. act like normal readl() */
2107 *value = __raw_readl(reg);
2108 rmb();
2109}
2110
2111void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2112 unsigned int value)
2113{
2114 unsigned int *reg;
2115
Jordan Crouse7501d452012-04-19 08:58:44 -06002116 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002117
2118 if (!in_interrupt())
2119 kgsl_pre_hwaccess(device);
2120
2121 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002122 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002123
2124 /*ensure previous writes post before this one,
2125 * i.e. act like normal writel() */
2126 wmb();
2127 __raw_writel(value, reg);
2128}
2129
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002130static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2131{
2132 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002133 if (k_ctxt != NULL) {
2134 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002135 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2136 context_id = KGSL_CONTEXT_INVALID;
2137 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2138 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002139 }
2140
2141 return context_id;
2142}
2143
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002144static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002145 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002146{
2147 int status;
2148 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002149 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002150 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002151
2152 mutex_lock(&device->mutex);
2153 context_id = _get_context_id(context);
2154 /*
2155 * If the context ID is invalid, we are in a race with
2156 * the context being destroyed by userspace so bail.
2157 */
2158 if (context_id == KGSL_CONTEXT_INVALID) {
2159 KGSL_DRV_WARN(device, "context was detached");
2160 status = -EINVAL;
2161 goto unlock;
2162 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002163
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002164 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002165 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002166 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002167 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002168 mb();
2169
2170 if (enableflag) {
2171 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002172 KGSL_MEMSTORE_OFFSET(context_id,
2173 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002174 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002175 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002176 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002177 KGSL_MEMSTORE_OFFSET(context_id,
2178 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002179 wmb();
2180 }
2181 } else {
2182 unsigned int cmds[2];
2183 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002184 KGSL_MEMSTORE_OFFSET(context_id,
2185 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186 enableflag = 1;
2187 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002188 KGSL_MEMSTORE_OFFSET(context_id,
2189 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002190 wmb();
2191 /* submit a dummy packet so that even if all
2192 * commands upto timestamp get executed we will still
2193 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002194 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002195 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002196
2197 if (adreno_dev->drawctxt_active)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002198 adreno_ringbuffer_issuecmds_intr(device,
2199 context, &cmds[0], 2);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002200 else
2201 /* We would never call this function if there
2202 * was no active contexts running */
2203 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002204 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002205 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002206unlock:
2207 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208
2209 return status;
2210}
2211
2212/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002213 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002214 placing a process in wait q. For conditional interrupts we expect the
2215 process to already be in its wait q when its exit condition checking
2216 function is called.
2217*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002218#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002219({ \
2220 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002221 if (io) \
2222 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2223 else \
2224 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002225 __ret; \
2226})
2227
Tarun Karra3335f142012-06-19 14:11:48 -07002228
2229
2230unsigned int adreno_hang_detect(struct kgsl_device *device,
2231 unsigned int *prev_reg_val)
2232{
2233 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2234 unsigned int curr_reg_val[hang_detect_regs_count];
2235 unsigned int hang_detected = 1;
2236 unsigned int i;
2237
2238 if (!adreno_dev->fast_hang_detect)
2239 return 0;
2240
2241 for (i = 0; i < hang_detect_regs_count; i++) {
2242 adreno_regread(device, hang_detect_regs[i],
2243 &curr_reg_val[i]);
2244 if (curr_reg_val[i] != prev_reg_val[i]) {
2245 prev_reg_val[i] = curr_reg_val[i];
2246 hang_detected = 0;
2247 }
2248 }
2249
2250 return hang_detected;
2251}
2252
2253
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002254/* MUST be called with the device mutex held */
2255static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002256 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002257 unsigned int timestamp,
2258 unsigned int msecs)
2259{
2260 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06002261 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06002262 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002263 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06002264 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07002265 int retries = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002266 unsigned int ts_issued;
2267 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07002268 unsigned int time_elapsed = 0;
2269 unsigned int prev_reg_val[hang_detect_regs_count];
Jordan Crouse21f75a02012-08-09 15:08:59 -06002270 unsigned int wait;
Tarun Karra3335f142012-06-19 14:11:48 -07002271
2272 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002273
2274 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002275
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302276 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07002277 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302278 msecs = adreno_dev->wait_timeout;
2279
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002280 if (timestamp_cmp(timestamp, ts_issued) > 0) {
2281 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
2282 "last issued ts <%d:0x%x>\n",
2283 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002284 status = -EINVAL;
2285 goto done;
2286 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002287
Jordan Crouse21f75a02012-08-09 15:08:59 -06002288 /*
2289 * Make the first timeout interval 100 msecs and then try to kick the
2290 * wptr again. This helps to ensure the wptr is updated properly. If
2291 * the requested timeout is less than 100 msecs, then wait 20msecs which
2292 * is the minimum amount of time we can safely wait at 100HZ
Lynus Vaz06a9a902011-10-04 19:25:33 +05302293 */
Jordan Crouse21f75a02012-08-09 15:08:59 -06002294
2295 if (msecs == 0 || msecs >= 100)
2296 wait = 100;
2297 else
2298 wait = 20;
2299
Tarun Karra3335f142012-06-19 14:11:48 -07002300 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002301 /*
2302 * If the context ID is invalid, we are in a race with
2303 * the context being destroyed by userspace so bail.
2304 */
2305 if (context_id == KGSL_CONTEXT_INVALID) {
2306 KGSL_DRV_WARN(device, "context was detached");
2307 status = -EINVAL;
2308 goto done;
2309 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002310 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07002311 /* if the timestamp happens while we're not
2312 * waiting, there's a chance that an interrupt
2313 * will not be generated and thus the timestamp
2314 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05302315 */
Jeremy Gebben63904832012-02-07 16:10:55 -07002316 queue_work(device->work_queue, &device->ts_expired_ws);
2317 status = 0;
2318 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002319 }
Jeremy Gebben63904832012-02-07 16:10:55 -07002320 adreno_poke(device);
2321 io_cnt = (io_cnt + 1) % 100;
2322 if (io_cnt <
2323 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2324 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002325
2326 if ((retries > 0) &&
2327 (adreno_hang_detect(device, prev_reg_val)))
2328 goto hang_dump;
2329
Jeremy Gebben63904832012-02-07 16:10:55 -07002330 mutex_unlock(&device->mutex);
2331 /* We need to make sure that the process is
2332 * placed in wait-q before its condition is called
2333 */
2334 status = kgsl_wait_event_interruptible_timeout(
2335 device->wait_queue,
2336 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002337 context, timestamp),
Jordan Crouse21f75a02012-08-09 15:08:59 -06002338 msecs_to_jiffies(wait), io);
2339
Jeremy Gebben63904832012-02-07 16:10:55 -07002340 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002341
Jeremy Gebben63904832012-02-07 16:10:55 -07002342 if (status > 0) {
2343 /*completed before the wait finished */
2344 status = 0;
2345 goto done;
2346 } else if (status < 0) {
2347 /*an error occurred*/
2348 goto done;
2349 }
2350 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07002351
Jordan Crouse21f75a02012-08-09 15:08:59 -06002352 time_elapsed += wait;
2353 wait = KGSL_TIMEOUT_PART;
2354
Tarun Karra3335f142012-06-19 14:11:48 -07002355 retries++;
2356
Jordan Crouse21f75a02012-08-09 15:08:59 -06002357 } while (!msecs || time_elapsed < msecs);
Tarun Karra3335f142012-06-19 14:11:48 -07002358
2359hang_dump:
Shubhraprakash Das54396e52012-03-10 13:24:54 -07002360 /*
2361 * Check if timestamp has retired here because we may have hit
2362 * recovery which can take some time and cause waiting threads
2363 * to timeout
2364 */
2365 if (kgsl_check_timestamp(device, context, timestamp))
2366 goto done;
Jeremy Gebben63904832012-02-07 16:10:55 -07002367 status = -ETIMEDOUT;
2368 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002369 "Device hang detected while waiting for timestamp: "
2370 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2371 "wptr: 0x%x\n",
2372 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002373 adreno_dev->ringbuffer.wptr);
2374 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002375 /* The timestamp that this process wanted
2376 * to wait on may be invalid or expired now
2377 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07002378 status = 0;
2379 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002380done:
2381 return (int)status;
2382}
2383
2384static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002385 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002386{
2387 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002388 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002389
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002390 /*
2391 * If the context ID is invalid, we are in a race with
2392 * the context being destroyed by userspace so bail.
2393 */
2394 if (context_id == KGSL_CONTEXT_INVALID) {
2395 KGSL_DRV_WARN(device, "context was detached");
2396 return timestamp;
2397 }
Jordan Crousec659f382012-04-16 11:10:41 -06002398 switch (type) {
2399 case KGSL_TIMESTAMP_QUEUED: {
2400 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2401 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2402
2403 timestamp = rb->timestamp[context_id];
2404 break;
2405 }
2406 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002407 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002408 break;
2409 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002410 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002411 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2412 break;
2413 }
2414
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002415 rmb();
2416
2417 return timestamp;
2418}
2419
2420static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2421 unsigned int cmd, void *data)
2422{
2423 int result = 0;
2424 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2425 struct kgsl_context *context;
2426
2427 switch (cmd) {
2428 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2429 binbase = data;
2430
2431 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2432 if (context) {
2433 adreno_drawctxt_set_bin_base_offset(
2434 dev_priv->device, context, binbase->offset);
2435 } else {
2436 result = -EINVAL;
2437 KGSL_DRV_ERR(dev_priv->device,
2438 "invalid drawctxt drawctxt_id %d "
2439 "device_id=%d\n",
2440 binbase->drawctxt_id, dev_priv->device->id);
2441 }
2442 break;
2443
2444 default:
2445 KGSL_DRV_INFO(dev_priv->device,
2446 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002447 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002448 break;
2449 }
2450 return result;
2451
2452}
2453
2454static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2455{
2456 gpu_freq /= 1000000;
2457 return ticks / gpu_freq;
2458}
2459
2460static void adreno_power_stats(struct kgsl_device *device,
2461 struct kgsl_power_stats *stats)
2462{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002463 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002464 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002465 unsigned int cycles;
2466
2467 /* Get the busy cycles counted since the counter was last reset */
2468 /* Calling this function also resets and restarts the counter */
2469
2470 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002471
2472 /* In order to calculate idle you have to have run the algorithm *
2473 * at least once to get a start time. */
2474 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002475 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002476 stats->total_time = tmp - pwr->time;
2477 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002478 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002479 pwrlevels[device->pwrctrl.active_pwrlevel].
2480 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481 } else {
2482 stats->total_time = 0;
2483 stats->busy_time = 0;
2484 pwr->time = ktime_to_us(ktime_get());
2485 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002486}
2487
2488void adreno_irqctrl(struct kgsl_device *device, int state)
2489{
Jordan Crousea78c9172011-07-11 13:14:09 -06002490 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2491 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002492}
2493
Jordan Croused6535882012-06-20 08:22:16 -06002494static unsigned int adreno_gpuid(struct kgsl_device *device,
2495 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002496{
2497 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2498
Jordan Croused6535882012-06-20 08:22:16 -06002499 /* Some applications need to know the chip ID too, so pass
2500 * that as a parameter */
2501
2502 if (chipid != NULL)
2503 *chipid = adreno_dev->chip_id;
2504
Jordan Crousea0758f22011-12-07 11:19:22 -07002505 /* Standard KGSL gpuid format:
2506 * top word is 0x0002 for 2D or 0x0003 for 3D
2507 * Bottom word is core specific identifer
2508 */
2509
2510 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2511}
2512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002513static const struct kgsl_functable adreno_functable = {
2514 /* Mandatory functions */
2515 .regread = adreno_regread,
2516 .regwrite = adreno_regwrite,
2517 .idle = adreno_idle,
2518 .isidle = adreno_isidle,
2519 .suspend_context = adreno_suspend_context,
2520 .start = adreno_start,
2521 .stop = adreno_stop,
2522 .getproperty = adreno_getproperty,
2523 .waittimestamp = adreno_waittimestamp,
2524 .readtimestamp = adreno_readtimestamp,
2525 .issueibcmds = adreno_ringbuffer_issueibcmds,
2526 .ioctl = adreno_ioctl,
2527 .setup_pt = adreno_setup_pt,
2528 .cleanup_pt = adreno_cleanup_pt,
2529 .power_stats = adreno_power_stats,
2530 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002531 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002532 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002533 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002534 /* Optional functions */
2535 .setstate = adreno_setstate,
2536 .drawctxt_create = adreno_drawctxt_create,
2537 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002538 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002539 .postmortem_dump = adreno_dump,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002540};
2541
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002542static struct platform_driver adreno_platform_driver = {
2543 .probe = adreno_probe,
2544 .remove = __devexit_p(adreno_remove),
2545 .suspend = kgsl_suspend_driver,
2546 .resume = kgsl_resume_driver,
2547 .id_table = adreno_id_table,
2548 .driver = {
2549 .owner = THIS_MODULE,
2550 .name = DEVICE_3D_NAME,
2551 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002552 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002553 }
2554};
2555
2556static int __init kgsl_3d_init(void)
2557{
2558 return platform_driver_register(&adreno_platform_driver);
2559}
2560
2561static void __exit kgsl_3d_exit(void)
2562{
2563 platform_driver_unregister(&adreno_platform_driver);
2564}
2565
2566module_init(kgsl_3d_init);
2567module_exit(kgsl_3d_exit);
2568
2569MODULE_DESCRIPTION("3D Graphics driver");
2570MODULE_VERSION("1.2");
2571MODULE_LICENSE("GPL v2");
2572MODULE_ALIAS("platform:kgsl_3d");