blob: 3274b9fd99b9763689b4571b3f7406c981c3cbf1 [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,
Jordan Crouseb5c80482012-10-03 09:38:41 -0600130 0,
131 0
Tarun Karra3335f142012-06-19 14:11:48 -0700132};
133
134const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700135
Jordan Crouse505df9c2011-07-28 08:37:59 -0600136/*
137 * This is the master list of all GPU cores that are supported by this
138 * driver.
139 */
140
141#define ANY_ID (~0)
142
143static const struct {
144 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600145 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600146 const char *pm4fw;
147 const char *pfpfw;
148 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700149 unsigned int istore_size;
150 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700151 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530152 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600153} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600154 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700155 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530156 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530157 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
158 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530159 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600160 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700161 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530162 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600163 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700164 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530165 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600166 /*
167 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
168 * a hardware problem.
169 */
170 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700171 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530172 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700173 { ADRENO_REV_A225, 2, 2, 0, 6,
174 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530175 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600176 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700177 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530178 1536, 768, 3, SZ_512K },
179 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530180 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530181 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
182 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700183 /* A3XX doesn't use the pix_shader_start */
Carter Cooper95f7f792012-08-19 13:40:34 -0600184 { ADRENO_REV_A320, 3, 2, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700185 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530186 512, 0, 2, SZ_512K },
liu zhongfd42e622012-05-01 19:18:30 -0700187 { ADRENO_REV_A330, 3, 3, 0, 0,
188 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
189 512, 0, 2, SZ_1M },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600190};
191
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600192static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193{
Jordan Crousea78c9172011-07-11 13:14:09 -0600194 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600195 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196
Jordan Crousea78c9172011-07-11 13:14:09 -0600197 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198
199 if (device->requested_state == KGSL_STATE_NONE) {
200 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700201 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 queue_work(device->work_queue, &device->idle_check_ws);
203 } else if (device->pwrscale.policy != NULL) {
204 queue_work(device->work_queue, &device->idle_check_ws);
205 }
206 }
207
208 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800209 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 jiffies + device->pwrctrl.interval_timeout);
211 return result;
212}
213
Jordan Crouse9f739212011-07-28 08:37:57 -0600214static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 struct kgsl_pagetable *pagetable)
216{
217 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
218 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
219
220 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
221
222 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
223
224 kgsl_mmu_unmap(pagetable, &device->memstore);
225
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600226 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227}
228
229static int adreno_setup_pt(struct kgsl_device *device,
230 struct kgsl_pagetable *pagetable)
231{
232 int result = 0;
233 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
234 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
235
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
237 GSL_PT_PAGE_RV);
238 if (result)
239 goto error;
240
241 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
242 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
243 if (result)
244 goto unmap_buffer_desc;
245
246 result = kgsl_mmu_map_global(pagetable, &device->memstore,
247 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
248 if (result)
249 goto unmap_memptrs_desc;
250
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600251 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
253 if (result)
254 goto unmap_memstore_desc;
255
256 return result;
257
258unmap_memstore_desc:
259 kgsl_mmu_unmap(pagetable, &device->memstore);
260
261unmap_memptrs_desc:
262 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
263
264unmap_buffer_desc:
265 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
266
267error:
268 return result;
269}
270
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600271static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600272 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600273 uint32_t flags)
274{
275 unsigned int pt_val, reg_pt_val;
276 unsigned int link[200];
277 unsigned int *cmds = &link[0];
278 int sizedwords = 0;
279 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
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);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700286 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600287
288 context = idr_find(&device->context_idr, context_id);
289 adreno_ctx = context->devctxt;
290
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600291 if (kgsl_mmu_enable_clk(&device->mmu,
292 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700293 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600294
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600295 cmds += __adreno_add_idle_indirect_cmds(cmds,
296 device->mmu.setstate_memory.gpuaddr +
297 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
298
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600299 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600300 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
301 device->mmu.setstate_memory.gpuaddr +
302 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
303 else
304 cmds += adreno_add_bank_change_cmds(cmds,
305 KGSL_IOMMU_CONTEXT_USER,
306 device->mmu.setstate_memory.gpuaddr +
307 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
308
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700309 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
310 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600311 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600312 /*
313 * We need to perfrom the following operations for all
314 * IOMMU units
315 */
316 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700317 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
318 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600319 /*
320 * Set address of the new pagetable by writng to IOMMU
321 * TTBR0 register
322 */
323 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700324 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
325 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600326 *cmds++ = reg_pt_val;
327 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
328 *cmds++ = 0x00000000;
329
330 /*
331 * Read back the ttbr0 register as a barrier to ensure
332 * above writes have completed
333 */
334 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700335 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
336 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600337 reg_pt_val,
338 device->mmu.setstate_memory.gpuaddr +
339 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600340 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600341 }
342 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
343 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700344 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600345 */
346 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700347 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
348 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700349
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600350 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700351 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
352 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700353 KGSL_IOMMU_CTX_TLBIALL);
354 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600355
356 cmds += __adreno_add_idle_indirect_cmds(cmds,
357 device->mmu.setstate_memory.gpuaddr +
358 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
359
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600360 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700361 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
362 KGSL_IOMMU_CONTEXT_USER,
363 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700364 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600365 device->mmu.setstate_memory.gpuaddr +
366 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
367 }
368 }
369
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600370 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600371 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700372 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
373 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600374 device->mmu.setstate_memory.gpuaddr +
375 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
376 else
377 cmds += adreno_add_bank_change_cmds(cmds,
378 KGSL_IOMMU_CONTEXT_PRIV,
379 device->mmu.setstate_memory.gpuaddr +
380 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
381
382 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600383 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700384 /* invalidate all base pointers */
385 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
386 *cmds++ = 0x7fff;
387 sizedwords += 2;
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600388 /*
389 * add an interrupt at the end of commands so that the smmu
390 * disable clock off function will get called
391 */
392 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
393 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
394 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600395 /* This returns the per context timestamp but we need to
396 * use the global timestamp for iommu clock disablement */
397 adreno_ringbuffer_issuecmds(device, adreno_ctx,
398 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600399 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600400 kgsl_mmu_disable_clk_on_ts(&device->mmu,
401 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600402 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600403}
404
405static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600406 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600407 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408{
409 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
410 unsigned int link[32];
411 unsigned int *cmds = &link[0];
412 int sizedwords = 0;
413 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600414 struct kgsl_context *context;
415 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600417 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530418 * Fix target freeze issue by adding TLB flush for each submit
419 * on A20X based targets.
420 */
421 if (adreno_is_a20x(adreno_dev))
422 flags |= KGSL_MMUFLAGS_TLBFLUSH;
423 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600424 * If possible, then set the state via the command stream to avoid
425 * a CPU idle. Otherwise, use the default setstate which uses register
426 * writes For CFF dump we must idle and use the registers so that it is
427 * easier to filter out the mmu accesses from the dump
428 */
429 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600430 context = idr_find(&device->context_idr, context_id);
431 adreno_ctx = context->devctxt;
432
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
434 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600435 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436 *cmds++ = 0x00000000;
437
438 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600439 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700440 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600441 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442 sizedwords += 4;
443 }
444
445 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
446 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600447 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448 1);
449 *cmds++ = 0x00000000;
450 sizedwords += 2;
451 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600452 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 *cmds++ = mh_mmu_invalidate;
454 sizedwords += 2;
455 }
456
457 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600458 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 /* HW workaround: to resolve MMU page fault interrupts
460 * caused by the VGT.It prevents the CP PFP from filling
461 * the VGT DMA request fifo too early,thereby ensuring
462 * that the VGT will not fetch vertex/bin data until
463 * after the page table base register has been updated.
464 *
465 * Two null DRAW_INDX_BIN packets are inserted right
466 * after the page table base update, followed by a
467 * wait for idle. The null packets will fill up the
468 * VGT DMA request fifo and prevent any further
469 * vertex/bin updates from occurring until the wait
470 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600471 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472 *cmds++ = (0x4 << 16) |
473 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
474 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600475 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600476 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600477 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 *cmds++ = 0; /* viz query info */
479 *cmds++ = 0x0003C004; /* draw indicator */
480 *cmds++ = 0; /* bin base */
481 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600482 *cmds++ =
483 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600485 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 *cmds++ = 0; /* viz query info */
487 *cmds++ = 0x0003C004; /* draw indicator */
488 *cmds++ = 0; /* bin base */
489 *cmds++ = 3; /* bin size */
490 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600491 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600493 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494 *cmds++ = 0x00000000;
495 sizedwords += 21;
496 }
497
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600498
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600500 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501 *cmds++ = 0x7fff; /* invalidate all base pointers */
502 sizedwords += 2;
503 }
504
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600505 adreno_ringbuffer_issuecmds(device, adreno_ctx,
506 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600508 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600509 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600510 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511}
512
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600513static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600514 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600515 uint32_t flags)
516{
517 /* call the mmu specific handler */
518 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600519 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600520 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600521 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600522}
523
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700525a3xx_getchipid(struct kgsl_device *device)
526{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600527 struct kgsl_device_platform_data *pdata =
528 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700529
Jordan Crouse54154c62012-03-27 16:33:26 -0600530 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600531 * All current A3XX chipids are detected at the SOC level. Leave this
532 * function here to support any future GPUs that have working
533 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600534 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700535
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600536 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700537}
538
539static unsigned int
540a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541{
542 unsigned int chipid = 0;
543 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600544 struct kgsl_device_platform_data *pdata =
545 kgsl_device_get_drvdata(device);
546
547 /* If the chip id is set at the platform level, then just use that */
548
549 if (pdata->chipid != 0)
550 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551
552 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
553 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
554 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
555
556 /*
557 * adreno 22x gpus are indicated by coreid 2,
558 * but REG_RBBM_PERIPHID1 always contains 0 for this field
559 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600560 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 chipid = 2 << 24;
562 else
563 chipid = (coreid & 0xF) << 24;
564
565 chipid |= ((majorid >> 4) & 0xF) << 16;
566
567 minorid = ((revid >> 0) & 0xFF);
568
569 patchid = ((revid >> 16) & 0xFF);
570
571 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530572 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 if (cpu_is_qsd8x50())
574 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530575 else if (cpu_is_msm8625() && minorid == 0)
576 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577
578 chipid |= (minorid << 8) | patchid;
579
580 return chipid;
581}
582
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700583static unsigned int
584adreno_getchipid(struct kgsl_device *device)
585{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600586 struct kgsl_device_platform_data *pdata =
587 kgsl_device_get_drvdata(device);
588
589 /*
590 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
591 * an A2XX processor
592 */
593
594 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700595 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600596 else
597 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700598}
599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600static inline bool _rev_match(unsigned int id, unsigned int entry)
601{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600602 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604
605static void
606adreno_identify_gpu(struct adreno_device *adreno_dev)
607{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600608 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609
610 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
611
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600612 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
613 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
614 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
615 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616
Jordan Crouse505df9c2011-07-28 08:37:59 -0600617 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
618 if (core == adreno_gpulist[i].core &&
619 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600620 _rev_match(minor, adreno_gpulist[i].minor) &&
621 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 }
624
Jordan Crouse505df9c2011-07-28 08:37:59 -0600625 if (i == ARRAY_SIZE(adreno_gpulist)) {
626 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
627 return;
628 }
629
630 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
631 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
632 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
633 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700634 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
635 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700636 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600637 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638}
639
Lokesh Batra805e1e12012-08-03 08:34:06 -0600640static struct platform_device_id adreno_id_table[] = {
641 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
642 {},
643};
644
645MODULE_DEVICE_TABLE(platform, adreno_id_table);
646
647static struct of_device_id adreno_match_table[] = {
648 { .compatible = "qcom,kgsl-3d0", },
649 {}
650};
651
652static inline int adreno_of_read_property(struct device_node *node,
653 const char *prop, unsigned int *ptr)
654{
655 int ret = of_property_read_u32(node, prop, ptr);
656 if (ret)
657 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
658 return ret;
659}
660
661static struct device_node *adreno_of_find_subnode(struct device_node *parent,
662 const char *name)
663{
664 struct device_node *child;
665
666 for_each_child_of_node(parent, child) {
667 if (of_device_is_compatible(child, name))
668 return child;
669 }
670
671 return NULL;
672}
673
674static int adreno_of_get_pwrlevels(struct device_node *parent,
675 struct kgsl_device_platform_data *pdata)
676{
677 struct device_node *node, *child;
678 int ret = -EINVAL;
679
680 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
681
682 if (node == NULL) {
683 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
684 return -EINVAL;
685 }
686
687 pdata->num_levels = 0;
688
689 for_each_child_of_node(node, child) {
690 unsigned int index;
691 struct kgsl_pwrlevel *level;
692
693 if (adreno_of_read_property(child, "reg", &index))
694 goto done;
695
696 if (index >= KGSL_MAX_PWRLEVELS) {
697 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
698 index);
699 continue;
700 }
701
702 if (index >= pdata->num_levels)
703 pdata->num_levels = index + 1;
704
705 level = &pdata->pwrlevel[index];
706
707 if (adreno_of_read_property(child, "qcom,gpu-freq",
708 &level->gpu_freq))
709 goto done;
710
711 if (adreno_of_read_property(child, "qcom,bus-freq",
712 &level->bus_freq))
713 goto done;
714
715 if (adreno_of_read_property(child, "qcom,io-fraction",
716 &level->io_fraction))
717 level->io_fraction = 0;
718 }
719
720 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
721 &pdata->init_level))
722 pdata->init_level = 1;
723
724 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
725 KGSL_CORE_ERR("Initial power level out of range\n");
726 pdata->init_level = 1;
727 }
728
729 ret = 0;
730done:
731 return ret;
732
733}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600734
735static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
736{
737 struct device_node *node, *child;
738 struct msm_dcvs_core_info *info = NULL;
739 int count = 0;
740 int ret = -EINVAL;
741
742 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
743 if (node == NULL)
744 return ERR_PTR(-EINVAL);
745
746 info = kzalloc(sizeof(*info), GFP_KERNEL);
747
748 if (info == NULL) {
749 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
750 ret = -ENOMEM;
751 goto err;
752 }
753
754 for_each_child_of_node(node, child)
755 count++;
756
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700757 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600758
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700759 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600760 sizeof(struct msm_dcvs_freq_entry),
761 GFP_KERNEL);
762
763 if (info->freq_tbl == NULL) {
764 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700765 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600766 sizeof(struct msm_dcvs_freq_entry));
767 ret = -ENOMEM;
768 goto err;
769 }
770
771 for_each_child_of_node(node, child) {
772 unsigned int index;
773
774 if (adreno_of_read_property(child, "reg", &index))
775 goto err;
776
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700777 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600778 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
779 index);
780 continue;
781 }
782
783 if (adreno_of_read_property(child, "qcom,freq",
784 &info->freq_tbl[index].freq))
785 goto err;
786
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700787 if (adreno_of_read_property(child, "qcom,voltage",
788 &info->freq_tbl[index].voltage))
789 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600790
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700791 if (adreno_of_read_property(child, "qcom,is_trans_level",
792 &info->freq_tbl[index].is_trans_level))
793 info->freq_tbl[index].is_trans_level = 0;
794
795 if (adreno_of_read_property(child, "qcom,active-energy-offset",
796 &info->freq_tbl[index].active_energy_offset))
797 info->freq_tbl[index].active_energy_offset = 0;
798
799 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
800 &info->freq_tbl[index].leakage_energy_offset))
801 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600802 }
803
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700804 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
805 goto err;
806
807 info->sensors = kzalloc(info->num_cores *
808 sizeof(int),
809 GFP_KERNEL);
810
811 for (count = 0; count < info->num_cores; count++) {
812 if (adreno_of_read_property(node, "qcom,sensors",
813 &(info->sensors[count])))
814 goto err;
815 }
816
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700817 if (adreno_of_read_property(node, "qcom,core-core-type",
818 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600819 goto err;
820
821 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
822 &info->algo_param.disable_pc_threshold))
823 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700824 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
825 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600826 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700827 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
828 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600829 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600830 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
831 &info->algo_param.em_max_util_pct))
832 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700833 if (adreno_of_read_property(node, "qcom,algo-group-id",
834 &info->algo_param.group_id))
835 goto err;
836 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
837 &info->algo_param.max_freq_chg_time_us))
838 goto err;
839 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
840 &info->algo_param.slack_mode_dynamic))
841 goto err;
842 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
843 &info->algo_param.slack_weight_thresh_pct))
844 goto err;
845 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
846 &info->algo_param.slack_time_min_us))
847 goto err;
848 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
849 &info->algo_param.slack_time_max_us))
850 goto err;
851 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
852 &info->algo_param.ss_win_size_min_us))
853 goto err;
854 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
855 &info->algo_param.ss_win_size_max_us))
856 goto err;
857 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
858 &info->algo_param.ss_util_pct))
859 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800860 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
861 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600862 goto err;
863
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700864 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
865 &info->energy_coeffs.active_coeff_a))
866 goto err;
867 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
868 &info->energy_coeffs.active_coeff_b))
869 goto err;
870 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
871 &info->energy_coeffs.active_coeff_c))
872 goto err;
873 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
874 &info->energy_coeffs.leakage_coeff_a))
875 goto err;
876 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
877 &info->energy_coeffs.leakage_coeff_b))
878 goto err;
879 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
880 &info->energy_coeffs.leakage_coeff_c))
881 goto err;
882 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
883 &info->energy_coeffs.leakage_coeff_d))
884 goto err;
885
886 if (adreno_of_read_property(node, "qcom,power-current-temp",
887 &info->power_param.current_temp))
888 goto err;
889
Lokesh Batra805e1e12012-08-03 08:34:06 -0600890 return info;
891
892err:
893 if (info)
894 kfree(info->freq_tbl);
895
896 kfree(info);
897
898 return ERR_PTR(ret);
899}
900
901static int adreno_of_get_iommu(struct device_node *parent,
902 struct kgsl_device_platform_data *pdata)
903{
904 struct device_node *node, *child;
905 struct kgsl_device_iommu_data *data = NULL;
906 struct kgsl_iommu_ctx *ctxs = NULL;
907 u32 reg_val[2];
908 int ctx_index = 0;
909
910 node = of_parse_phandle(parent, "iommu", 0);
911 if (node == NULL)
912 return -EINVAL;
913
914 data = kzalloc(sizeof(*data), GFP_KERNEL);
915 if (data == NULL) {
916 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
917 goto err;
918 }
919
920 if (of_property_read_u32_array(node, "reg", reg_val, 2))
921 goto err;
922
923 data->physstart = reg_val[0];
924 data->physend = data->physstart + reg_val[1] - 1;
925
926 data->iommu_ctx_count = 0;
927
928 for_each_child_of_node(node, child)
929 data->iommu_ctx_count++;
930
931 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
932 GFP_KERNEL);
933
934 if (ctxs == NULL) {
935 KGSL_CORE_ERR("kzalloc(%d) failed\n",
936 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
937 goto err;
938 }
939
940 for_each_child_of_node(node, child) {
941 int ret = of_property_read_string(child, "label",
942 &ctxs[ctx_index].iommu_ctx_name);
943
944 if (ret) {
945 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
946 goto err;
947 }
948
949 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
950 &ctxs[ctx_index].ctx_id))
951 goto err;
952
953 ctx_index++;
954 }
955
956 data->iommu_ctxs = ctxs;
957
958 pdata->iommu_data = data;
959 pdata->iommu_count = 1;
960
961 return 0;
962
963err:
964 kfree(ctxs);
965 kfree(data);
966
967 return -EINVAL;
968}
969
970static int adreno_of_get_pdata(struct platform_device *pdev)
971{
972 struct kgsl_device_platform_data *pdata = NULL;
973 struct kgsl_device *device;
974 int ret = -EINVAL;
975
976 pdev->id_entry = adreno_id_table;
977
978 pdata = pdev->dev.platform_data;
979 if (pdata)
980 return 0;
981
982 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
983 KGSL_CORE_ERR("Unable to read 'label'\n");
984 goto err;
985 }
986
987 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
988 goto err;
989
990 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
991 if (pdata == NULL) {
992 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
993 ret = -ENOMEM;
994 goto err;
995 }
996
997 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
998 &pdata->chipid))
999 goto err;
1000
1001 /* pwrlevel Data */
1002 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1003 if (ret)
1004 goto err;
1005
1006 /* Default value is 83, if not found in DT */
1007 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1008 &pdata->idle_timeout))
1009 pdata->idle_timeout = 83;
1010
1011 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1012 &pdata->nap_allowed))
1013 pdata->nap_allowed = 1;
1014
1015 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1016 &pdata->clk_map))
1017 goto err;
1018
1019 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1020
1021 if (device->id != KGSL_DEVICE_3D0)
1022 goto err;
1023
1024 /* Bus Scale Data */
1025
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001026 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001027 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1028 ret = PTR_ERR(pdata->bus_scale_table);
1029 goto err;
1030 }
1031
1032 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1033 if (IS_ERR_OR_NULL(pdata->core_info)) {
1034 ret = PTR_ERR(pdata->core_info);
1035 goto err;
1036 }
1037
1038 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1039 if (ret)
1040 goto err;
1041
1042 pdev->dev.platform_data = pdata;
1043 return 0;
1044
1045err:
1046 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001047 if (pdata->core_info)
1048 kfree(pdata->core_info->freq_tbl);
1049 kfree(pdata->core_info);
1050
1051 if (pdata->iommu_data)
1052 kfree(pdata->iommu_data->iommu_ctxs);
1053
1054 kfree(pdata->iommu_data);
1055 }
1056
1057 kfree(pdata);
1058
1059 return ret;
1060}
1061
liu zhong7dfa2a32012-04-27 19:11:01 -07001062#ifdef CONFIG_MSM_OCMEM
1063static int
1064adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1065{
Jordan Crousec0978202012-08-29 14:35:51 -06001066 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001067 return 0;
1068
1069 /* OCMEM is only needed once, do not support consective allocation */
1070 if (adreno_dev->ocmem_hdl != NULL)
1071 return 0;
1072
1073 adreno_dev->ocmem_hdl =
1074 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1075 if (adreno_dev->ocmem_hdl == NULL)
1076 return -ENOMEM;
1077
1078 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001079 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001080
1081 return 0;
1082}
1083
1084static void
1085adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1086{
Jordan Crousec0978202012-08-29 14:35:51 -06001087 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001088 return;
1089
1090 if (adreno_dev->ocmem_hdl == NULL)
1091 return;
1092
1093 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1094 adreno_dev->ocmem_hdl = NULL;
1095}
1096#else
1097static int
1098adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1099{
1100 return 0;
1101}
1102
1103static void
1104adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1105{
1106}
1107#endif
1108
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109static int __devinit
1110adreno_probe(struct platform_device *pdev)
1111{
1112 struct kgsl_device *device;
1113 struct adreno_device *adreno_dev;
1114 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001115 bool is_dt;
1116
1117 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1118
1119 if (is_dt && pdev->dev.of_node) {
1120 status = adreno_of_get_pdata(pdev);
1121 if (status)
1122 goto error_return;
1123 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001124
1125 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1126 adreno_dev = ADRENO_DEVICE(device);
1127 device->parentdev = &pdev->dev;
1128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001129 status = adreno_ringbuffer_init(device);
1130 if (status != 0)
1131 goto error;
1132
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001133 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001134 if (status)
1135 goto error_close_rb;
1136
1137 adreno_debugfs_init(device);
1138
1139 kgsl_pwrscale_init(device);
1140 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1141
1142 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1143 return 0;
1144
1145error_close_rb:
1146 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1147error:
1148 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001149error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001150 return status;
1151}
1152
1153static int __devexit adreno_remove(struct platform_device *pdev)
1154{
1155 struct kgsl_device *device;
1156 struct adreno_device *adreno_dev;
1157
1158 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1159 adreno_dev = ADRENO_DEVICE(device);
1160
1161 kgsl_pwrscale_detach_policy(device);
1162 kgsl_pwrscale_close(device);
1163
1164 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1165 kgsl_device_platform_remove(device);
1166
1167 return 0;
1168}
1169
1170static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1171{
1172 int status = -EINVAL;
1173 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001175 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1176 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177
1178 /* Power up the device */
1179 kgsl_pwrctrl_enable(device);
1180
1181 /* Identify the specific GPU */
1182 adreno_identify_gpu(adreno_dev);
1183
Jordan Crouse505df9c2011-07-28 08:37:59 -06001184 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1185 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1186 adreno_dev->chip_id);
1187 goto error_clk_off;
1188 }
1189
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001190 /* Set up the MMU */
1191 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001192 /*
1193 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1194 * on older gpus
1195 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001196 if (adreno_is_a20x(adreno_dev)) {
1197 device->mh.mh_intf_cfg1 = 0;
1198 device->mh.mh_intf_cfg2 = 0;
1199 }
1200
1201 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001202 }
1203
Tarun Karra3335f142012-06-19 14:11:48 -07001204 /* Assign correct RBBM status register to hang detect regs
1205 */
1206 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1207
Jordan Crouseb5c80482012-10-03 09:38:41 -06001208 /* Add A3XX specific registers for hang detection */
1209 if (adreno_is_a3xx(adreno_dev)) {
1210 hang_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1211 hang_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
1212 }
1213
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001214 status = kgsl_mmu_start(device);
1215 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216 goto error_clk_off;
1217
liu zhong7dfa2a32012-04-27 19:11:01 -07001218 status = adreno_ocmem_gmem_malloc(adreno_dev);
1219 if (status) {
1220 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1221 goto error_mmu_off;
1222 }
1223
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001224 /* Start the GPU */
1225 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226
1227 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001228 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001229
1230 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001231 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001232 /* While recovery is on we do not want timer to
1233 * fire and attempt to change any device state */
1234 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1235 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001236 return 0;
1237 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001239 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001240
1241error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001242 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001243
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001244error_clk_off:
1245 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246
1247 return status;
1248}
1249
1250static int adreno_stop(struct kgsl_device *device)
1251{
1252 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1253
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001254 adreno_dev->drawctxt_active = NULL;
1255
1256 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1257
Shubhraprakash Das79447952012-04-26 18:12:23 -06001258 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001260 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301261 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001262 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001263
liu zhong7dfa2a32012-04-27 19:11:01 -07001264 adreno_ocmem_gmem_free(adreno_dev);
1265
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001266 /* Power down the device */
1267 kgsl_pwrctrl_disable(device);
1268
1269 return 0;
1270}
1271
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001272static void adreno_mark_context_status(struct kgsl_device *device,
1273 int recovery_status)
1274{
1275 struct kgsl_context *context;
1276 int next = 0;
1277 /*
1278 * Set the reset status of all contexts to
1279 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1280 * since thats the guilty party, if recovery failed then
1281 * mark all as guilty
1282 */
1283 while ((context = idr_get_next(&device->context_idr, &next))) {
1284 struct adreno_context *adreno_context = context->devctxt;
1285 if (recovery_status) {
1286 context->reset_status =
1287 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1288 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1289 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1290 context->reset_status) {
1291 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1292 CTXT_FLAGS_GPU_HANG_RECOVERED))
1293 context->reset_status =
1294 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1295 else
1296 context->reset_status =
1297 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1298 }
1299 next = next + 1;
1300 }
1301}
1302
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001303static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1304{
1305 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1306 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1307 struct kgsl_context *context;
1308 struct adreno_context *temp_adreno_context;
1309 int next = 0;
1310
1311 while ((context = idr_get_next(&device->context_idr, &next))) {
1312 temp_adreno_context = context->devctxt;
1313 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1314 kgsl_sharedmem_writel(&device->memstore,
1315 KGSL_MEMSTORE_OFFSET(context->id,
1316 soptimestamp),
1317 rb->timestamp[context->id]);
1318 kgsl_sharedmem_writel(&device->memstore,
1319 KGSL_MEMSTORE_OFFSET(context->id,
1320 eoptimestamp),
1321 rb->timestamp[context->id]);
1322 }
1323 next = next + 1;
1324 }
1325}
1326
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001327static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1328{
1329 vfree(rec_data->rb_buffer);
1330 vfree(rec_data->bad_rb_buffer);
1331}
1332
1333static int adreno_setup_recovery_data(struct kgsl_device *device,
1334 struct adreno_recovery_data *rec_data)
1335{
1336 int ret = 0;
1337 unsigned int ib1_sz, ib2_sz;
1338 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1339 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1340
1341 memset(rec_data, 0, sizeof(*rec_data));
1342
1343 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1344 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1345 if (ib1_sz || ib2_sz)
1346 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1347
1348 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1349 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1350 current_context));
1351
1352 kgsl_sharedmem_readl(&device->memstore,
1353 &rec_data->global_eop,
1354 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1355 eoptimestamp));
1356
1357 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1358 if (!rec_data->rb_buffer) {
1359 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1360 rb->buffer_desc.size);
1361 return -ENOMEM;
1362 }
1363
1364 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1365 if (!rec_data->bad_rb_buffer) {
1366 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1367 rb->buffer_desc.size);
1368 ret = -ENOMEM;
1369 goto done;
1370 }
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001371 rec_data->fault = device->mmu.fault;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001372
1373done:
1374 if (ret) {
1375 vfree(rec_data->rb_buffer);
1376 vfree(rec_data->bad_rb_buffer);
1377 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001378 return ret;
1379}
1380
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001381static int
1382_adreno_recover_hang(struct kgsl_device *device,
1383 struct adreno_recovery_data *rec_data,
1384 bool try_bad_commands)
1385{
1386 int ret;
1387 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1388 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1389 struct kgsl_context *context;
1390 struct adreno_context *adreno_context = NULL;
1391 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1392
1393 context = idr_find(&device->context_idr, rec_data->context_id);
1394 if (context == NULL) {
1395 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1396 rec_data->context_id);
1397 } else {
1398 adreno_context = context->devctxt;
1399 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1400 }
1401
1402 /* Extract valid contents from rb which can still be executed after
1403 * hang */
1404 ret = adreno_ringbuffer_extract(rb, rec_data);
1405 if (ret)
1406 goto done;
1407
1408 /* restart device */
1409 ret = adreno_stop(device);
1410 if (ret) {
1411 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1412 goto done;
1413 }
1414
1415 ret = adreno_start(device, true);
1416 if (ret) {
1417 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1418 goto done;
1419 }
1420
1421 if (context)
1422 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1423 KGSL_MEMSTORE_GLOBAL);
1424
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001425 /* If iommu is used then we need to make sure that the iommu clocks
1426 * are on since there could be commands in pipeline that touch iommu */
1427 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1428 ret = kgsl_mmu_enable_clk(&device->mmu,
1429 KGSL_IOMMU_CONTEXT_USER);
1430 if (ret)
1431 goto done;
1432 }
1433
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001434 /* Do not try the bad commands if recovery has failed bad commands
1435 * once already or if hang is due to a fault */
1436 if (!try_bad_commands || rec_data->fault)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001437 rec_data->bad_rb_size = 0;
1438
1439 if (rec_data->bad_rb_size) {
1440 int idle_ret;
1441 /* submit the bad and good context commands and wait for
1442 * them to pass */
1443 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1444 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001445 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001446 if (idle_ret) {
1447 ret = adreno_stop(device);
1448 if (ret) {
1449 KGSL_DRV_ERR(device,
1450 "Device stop failed in recovery\n");
1451 goto done;
1452 }
1453 ret = adreno_start(device, true);
1454 if (ret) {
1455 KGSL_DRV_ERR(device,
1456 "Device start failed in recovery\n");
1457 goto done;
1458 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001459 if (context)
1460 kgsl_mmu_setstate(&device->mmu,
1461 adreno_context->pagetable,
1462 KGSL_MEMSTORE_GLOBAL);
1463
1464 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1465 ret = kgsl_mmu_enable_clk(&device->mmu,
1466 KGSL_IOMMU_CONTEXT_USER);
1467 if (ret)
1468 goto done;
1469 }
1470
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001471 ret = idle_ret;
1472 KGSL_DRV_ERR(device,
1473 "Bad context commands hung in recovery\n");
1474 } else {
1475 KGSL_DRV_ERR(device,
1476 "Bad context commands succeeded in recovery\n");
1477 if (adreno_context)
1478 adreno_context->flags = (adreno_context->flags &
1479 ~CTXT_FLAGS_GPU_HANG) |
1480 CTXT_FLAGS_GPU_HANG_RECOVERED;
1481 adreno_dev->drawctxt_active = last_active_ctx;
1482 }
1483 }
1484 /* If either the bad command sequence failed or we did not play it */
1485 if (ret || !rec_data->bad_rb_size) {
1486 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1487 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001488 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001489 if (ret) {
1490 /* If we fail here we can try to invalidate another
1491 * context and try recovering again */
1492 ret = -EAGAIN;
1493 goto done;
1494 }
1495 /* ringbuffer now has data from the last valid context id,
1496 * so restore the active_ctx to the last valid context */
1497 if (rec_data->last_valid_ctx_id) {
1498 struct kgsl_context *last_ctx =
1499 idr_find(&device->context_idr,
1500 rec_data->last_valid_ctx_id);
1501 if (last_ctx)
1502 adreno_dev->drawctxt_active = last_ctx->devctxt;
1503 }
1504 }
1505done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001506 /* Turn off iommu clocks */
1507 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1508 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001509 return ret;
1510}
1511
1512static int
1513adreno_recover_hang(struct kgsl_device *device,
1514 struct adreno_recovery_data *rec_data)
1515{
1516 int ret = 0;
1517 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1518 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1519 unsigned int timestamp;
1520
1521 KGSL_DRV_ERR(device,
1522 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1523 "Bad context_id: %u, global_eop: 0x%x\n",
1524 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1525
1526 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1527 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1528
1529 /* We may need to replay commands multiple times based on whether
1530 * multiple contexts hang the GPU */
1531 while (true) {
1532 if (!ret)
1533 ret = _adreno_recover_hang(device, rec_data, true);
1534 else
1535 ret = _adreno_recover_hang(device, rec_data, false);
1536
1537 if (-EAGAIN == ret) {
1538 /* setup new recovery parameters and retry, this
1539 * means more than 1 contexts are causing hang */
1540 adreno_destroy_recovery_data(rec_data);
1541 adreno_setup_recovery_data(device, rec_data);
1542 KGSL_DRV_ERR(device,
1543 "Retry recovery from 3D GPU hang. Recovery parameters: "
1544 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1545 rec_data->ib1, rec_data->context_id,
1546 rec_data->global_eop);
1547 } else {
1548 break;
1549 }
1550 }
1551
1552 if (ret)
1553 goto done;
1554
1555 /* Restore correct states after recovery */
1556 if (adreno_dev->drawctxt_active)
1557 device->mmu.hwpagetable =
1558 adreno_dev->drawctxt_active->pagetable;
1559 else
1560 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1561 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1562 kgsl_sharedmem_writel(&device->memstore,
1563 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1564 eoptimestamp),
1565 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1566done:
1567 adreno_set_max_ts_for_bad_ctxs(device);
1568 adreno_mark_context_status(device, ret);
1569 if (!ret)
1570 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1571 else
1572 KGSL_DRV_ERR(device, "Recovery failed\n");
1573 return ret;
1574}
1575
1576int
1577adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001578{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001579 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001580 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001581
1582 if (device->state == KGSL_STATE_HUNG)
1583 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001584 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001585 mutex_unlock(&device->mutex);
1586 wait_for_completion(&device->recovery_gate);
1587 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001588 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001589 result = 0;
1590 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001591 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001592 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001593 /* Detected a hang */
1594
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001595 /* Get the recovery data as soon as hang is detected */
1596 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001597 /*
1598 * Trigger an automatic dump of the state to
1599 * the console
1600 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001601 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001602
1603 /*
1604 * Make a GPU snapshot. For now, do it after the PM dump so we
1605 * can at least be sure the PM dump will work as it always has
1606 */
1607 kgsl_device_snapshot(device, 1);
1608
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001609 result = adreno_recover_hang(device, &rec_data);
1610 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001611 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001612 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001613 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001614 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001615 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1616 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001617 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001618 }
1619done:
1620 return result;
1621}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001622EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623
1624static int adreno_getproperty(struct kgsl_device *device,
1625 enum kgsl_property_type type,
1626 void *value,
1627 unsigned int sizebytes)
1628{
1629 int status = -EINVAL;
1630 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1631
1632 switch (type) {
1633 case KGSL_PROP_DEVICE_INFO:
1634 {
1635 struct kgsl_devinfo devinfo;
1636
1637 if (sizebytes != sizeof(devinfo)) {
1638 status = -EINVAL;
1639 break;
1640 }
1641
1642 memset(&devinfo, 0, sizeof(devinfo));
1643 devinfo.device_id = device->id+1;
1644 devinfo.chip_id = adreno_dev->chip_id;
1645 devinfo.mmu_enabled = kgsl_mmu_enabled();
1646 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001647 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1648 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001649
1650 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1651 0) {
1652 status = -EFAULT;
1653 break;
1654 }
1655 status = 0;
1656 }
1657 break;
1658 case KGSL_PROP_DEVICE_SHADOW:
1659 {
1660 struct kgsl_shadowprop shadowprop;
1661
1662 if (sizebytes != sizeof(shadowprop)) {
1663 status = -EINVAL;
1664 break;
1665 }
1666 memset(&shadowprop, 0, sizeof(shadowprop));
1667 if (device->memstore.hostptr) {
1668 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1669 * anything to mmap().
1670 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001671 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001672 shadowprop.size = device->memstore.size;
1673 /* GSL needs this to be set, even if it
1674 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001675 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1676 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001677 }
1678 if (copy_to_user(value, &shadowprop,
1679 sizeof(shadowprop))) {
1680 status = -EFAULT;
1681 break;
1682 }
1683 status = 0;
1684 }
1685 break;
1686 case KGSL_PROP_MMU_ENABLE:
1687 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001688 int mmu_prop = kgsl_mmu_enabled();
1689
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001690 if (sizebytes != sizeof(int)) {
1691 status = -EINVAL;
1692 break;
1693 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001694 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001695 status = -EFAULT;
1696 break;
1697 }
1698 status = 0;
1699 }
1700 break;
1701 case KGSL_PROP_INTERRUPT_WAITS:
1702 {
1703 int int_waits = 1;
1704 if (sizebytes != sizeof(int)) {
1705 status = -EINVAL;
1706 break;
1707 }
1708 if (copy_to_user(value, &int_waits, sizeof(int))) {
1709 status = -EFAULT;
1710 break;
1711 }
1712 status = 0;
1713 }
1714 break;
1715 default:
1716 status = -EINVAL;
1717 }
1718
1719 return status;
1720}
1721
Jordan Crousef7370f82012-04-18 09:31:07 -06001722static int adreno_setproperty(struct kgsl_device *device,
1723 enum kgsl_property_type type,
1724 void *value,
1725 unsigned int sizebytes)
1726{
1727 int status = -EINVAL;
1728
1729 switch (type) {
1730 case KGSL_PROP_PWRCTRL: {
1731 unsigned int enable;
1732 struct kgsl_device_platform_data *pdata =
1733 kgsl_device_get_drvdata(device);
1734
1735 if (sizebytes != sizeof(enable))
1736 break;
1737
1738 if (copy_from_user(&enable, (void __user *) value,
1739 sizeof(enable))) {
1740 status = -EFAULT;
1741 break;
1742 }
1743
1744 if (enable) {
1745 if (pdata->nap_allowed)
1746 device->pwrctrl.nap_allowed = true;
1747
1748 kgsl_pwrscale_enable(device);
1749 } else {
1750 device->pwrctrl.nap_allowed = false;
1751 kgsl_pwrscale_disable(device);
1752 }
1753
1754 status = 0;
1755 }
1756 break;
1757 default:
1758 break;
1759 }
1760
1761 return status;
1762}
1763
Lynus Vaz06a9a902011-10-04 19:25:33 +05301764static inline void adreno_poke(struct kgsl_device *device)
1765{
1766 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1767 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1768}
1769
Jordan Crousea29a2e02012-08-14 09:09:23 -06001770static int adreno_ringbuffer_drain(struct kgsl_device *device,
1771 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001772{
1773 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1774 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001775 unsigned long wait;
1776 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1777
1778 if (!(rb->flags & KGSL_FLAGS_STARTED))
1779 return 0;
1780
1781 /*
1782 * The first time into the loop, wait for 100 msecs and kick wptr again
1783 * to ensure that the hardware has updated correctly. After that, kick
1784 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1785 * expires
1786 */
1787
1788 wait = jiffies + msecs_to_jiffies(100);
1789
1790 adreno_poke(device);
1791
1792 do {
1793 if (time_after(jiffies, wait)) {
1794 adreno_poke(device);
1795
1796 /* Check to see if the core is hung */
1797 if (adreno_hang_detect(device, regs))
1798 return -ETIMEDOUT;
1799
1800 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1801 }
1802 GSL_RB_GET_READPTR(rb, &rb->rptr);
1803
1804 if (time_after(jiffies, timeout)) {
1805 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1806 rb->rptr, rb->wptr);
1807 return -ETIMEDOUT;
1808 }
1809 } while (rb->rptr != rb->wptr);
1810
1811 return 0;
1812}
1813
1814/* Caller must hold the device mutex. */
1815int adreno_idle(struct kgsl_device *device)
1816{
1817 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001818 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301819 unsigned long wait_time;
1820 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001821 unsigned int prev_reg_val[hang_detect_regs_count];
1822
1823 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001824
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001825 kgsl_cffdump_regpoll(device->id,
1826 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001827 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001828
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001829retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001830 /* First, wait for the ringbuffer to drain */
1831 if (adreno_ringbuffer_drain(device, prev_reg_val))
1832 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001833
1834 /* now, wait for the GPU to finish its operations */
Jordan Crousea29a2e02012-08-14 09:09:23 -06001835 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
1836 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1837
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001838 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001839 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1840 &rbbm_status);
1841 if (adreno_is_a2xx(adreno_dev)) {
1842 if (rbbm_status == 0x110)
1843 return 0;
1844 } else {
1845 if (!(rbbm_status & 0x80000000))
1846 return 0;
1847 }
Tarun Karra3335f142012-06-19 14:11:48 -07001848
1849 /* Dont wait for timeout, detect hang faster.
1850 */
1851 if (time_after(jiffies, wait_time_part)) {
1852 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001853 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001854 if ((adreno_hang_detect(device, prev_reg_val)))
1855 goto err;
1856 }
1857
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858 }
1859
1860err:
1861 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001862 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1863 !adreno_dump_and_recover(device)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001864 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001865 goto retry;
1866 }
1867 return -ETIMEDOUT;
1868}
1869
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08001870/**
1871 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
1872 * rbbm_status register
1873 * @device - Pointer to the GPU device whose idle status is to be
1874 * checked
1875 * @returns - Returns whether the core is idle (based on rbbm_status)
1876 * false if the core is active, true if the core is idle
1877 */
1878static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
1879{
1880 unsigned int reg_rbbm_status;
1881 bool status = false;
1882 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1883
1884 /* Is the core idle? */
1885 adreno_regread(device,
1886 adreno_dev->gpudev->reg_rbbm_status,
1887 &reg_rbbm_status);
1888
1889 if (adreno_is_a2xx(adreno_dev)) {
1890 if (reg_rbbm_status == 0x110)
1891 status = true;
1892 } else {
1893 if (!(reg_rbbm_status & 0x80000000))
1894 status = true;
1895 }
1896 return status;
1897}
1898
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001899static unsigned int adreno_isidle(struct kgsl_device *device)
1900{
1901 int status = false;
1902 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1903 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001904
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001905 WARN_ON(device->state == KGSL_STATE_INIT);
1906 /* If the device isn't active, don't force it on. */
1907 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 /* Is the ring buffer is empty? */
1909 GSL_RB_GET_READPTR(rb, &rb->rptr);
1910 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1911 /* Is the core idle? */
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08001912 status = is_adreno_rbbm_status_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001913 }
1914 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001915 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001916 }
1917 return status;
1918}
1919
1920/* Caller must hold the device mutex. */
1921static int adreno_suspend_context(struct kgsl_device *device)
1922{
1923 int status = 0;
1924 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1925
1926 /* switch to NULL ctxt */
1927 if (adreno_dev->drawctxt_active != NULL) {
1928 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001929 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930 }
1931
1932 return status;
1933}
1934
Jordan Crouse233b2092012-04-18 09:31:09 -06001935/* Find a memory structure attached to an adreno context */
1936
1937struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1938 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1939{
1940 struct kgsl_context *context;
1941 struct adreno_context *adreno_context = NULL;
1942 int next = 0;
1943
1944 while (1) {
1945 context = idr_get_next(&device->context_idr, &next);
1946 if (context == NULL)
1947 break;
1948
1949 adreno_context = (struct adreno_context *)context->devctxt;
1950
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07001951 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
1952 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06001953 struct kgsl_memdesc *desc;
1954
1955 desc = &adreno_context->gpustate;
1956 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1957 return desc;
1958
1959 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1960 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1961 return desc;
1962 }
1963 next = next + 1;
1964 }
1965
1966 return NULL;
1967}
1968
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001969struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001970 unsigned int pt_base,
1971 unsigned int gpuaddr,
1972 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001973{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001974 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001975 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1976 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1977
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001978 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1979 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001980
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001981 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1982 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001983
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001984 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1985 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001986
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001987 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1988 size))
1989 return &device->mmu.setstate_memory;
1990
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07001991 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001992
1993 if (entry)
1994 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001995
Jordan Crouse233b2092012-04-18 09:31:09 -06001996 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001997}
1998
1999uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2000 unsigned int gpuaddr, unsigned int size)
2001{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002002 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002003
2004 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2005
2006 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002007}
2008
2009void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2010 unsigned int *value)
2011{
2012 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002013 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2014 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002015
2016 if (!in_interrupt())
2017 kgsl_pre_hwaccess(device);
2018
2019 /*ensure this read finishes before the next one.
2020 * i.e. act like normal readl() */
2021 *value = __raw_readl(reg);
2022 rmb();
2023}
2024
2025void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2026 unsigned int value)
2027{
2028 unsigned int *reg;
2029
Jordan Crouse7501d452012-04-19 08:58:44 -06002030 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002031
2032 if (!in_interrupt())
2033 kgsl_pre_hwaccess(device);
2034
2035 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002036 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002037
2038 /*ensure previous writes post before this one,
2039 * i.e. act like normal writel() */
2040 wmb();
2041 __raw_writel(value, reg);
2042}
2043
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002044static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2045{
2046 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002047 if (k_ctxt != NULL) {
2048 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002049 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2050 context_id = KGSL_CONTEXT_INVALID;
2051 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2052 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002053 }
2054
2055 return context_id;
2056}
2057
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002058static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002059 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002060{
2061 int status;
2062 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002063 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002064 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002065
2066 mutex_lock(&device->mutex);
2067 context_id = _get_context_id(context);
2068 /*
2069 * If the context ID is invalid, we are in a race with
2070 * the context being destroyed by userspace so bail.
2071 */
2072 if (context_id == KGSL_CONTEXT_INVALID) {
2073 KGSL_DRV_WARN(device, "context was detached");
2074 status = -EINVAL;
2075 goto unlock;
2076 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002077
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002078 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002079 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002080 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002081 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002082 mb();
2083
2084 if (enableflag) {
2085 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002086 KGSL_MEMSTORE_OFFSET(context_id,
2087 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002089 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002090 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002091 KGSL_MEMSTORE_OFFSET(context_id,
2092 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002093 wmb();
2094 }
2095 } else {
2096 unsigned int cmds[2];
2097 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002098 KGSL_MEMSTORE_OFFSET(context_id,
2099 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002100 enableflag = 1;
2101 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002102 KGSL_MEMSTORE_OFFSET(context_id,
2103 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002104 wmb();
2105 /* submit a dummy packet so that even if all
2106 * commands upto timestamp get executed we will still
2107 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002108 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002109 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002110
2111 if (adreno_dev->drawctxt_active)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002112 adreno_ringbuffer_issuecmds_intr(device,
2113 context, &cmds[0], 2);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002114 else
2115 /* We would never call this function if there
2116 * was no active contexts running */
2117 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002118 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002119 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002120unlock:
2121 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002122
2123 return status;
2124}
2125
2126/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002127 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002128 placing a process in wait q. For conditional interrupts we expect the
2129 process to already be in its wait q when its exit condition checking
2130 function is called.
2131*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002132#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002133({ \
2134 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002135 if (io) \
2136 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2137 else \
2138 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002139 __ret; \
2140})
2141
Tarun Karra3335f142012-06-19 14:11:48 -07002142
2143
2144unsigned int adreno_hang_detect(struct kgsl_device *device,
2145 unsigned int *prev_reg_val)
2146{
2147 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2148 unsigned int curr_reg_val[hang_detect_regs_count];
2149 unsigned int hang_detected = 1;
2150 unsigned int i;
2151
2152 if (!adreno_dev->fast_hang_detect)
2153 return 0;
2154
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002155 if (is_adreno_rbbm_status_idle(device))
2156 return 0;
2157
Tarun Karra3335f142012-06-19 14:11:48 -07002158 for (i = 0; i < hang_detect_regs_count; i++) {
Jordan Crouseb5c80482012-10-03 09:38:41 -06002159
2160 if (hang_detect_regs[i] == 0)
2161 continue;
2162
Tarun Karra3335f142012-06-19 14:11:48 -07002163 adreno_regread(device, hang_detect_regs[i],
2164 &curr_reg_val[i]);
2165 if (curr_reg_val[i] != prev_reg_val[i]) {
2166 prev_reg_val[i] = curr_reg_val[i];
2167 hang_detected = 0;
2168 }
2169 }
2170
2171 return hang_detected;
2172}
2173
2174
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002175/* MUST be called with the device mutex held */
2176static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002177 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178 unsigned int timestamp,
2179 unsigned int msecs)
2180{
2181 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06002182 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06002183 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002184 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06002185 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07002186 int retries = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002187 unsigned int ts_issued;
2188 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07002189 unsigned int time_elapsed = 0;
2190 unsigned int prev_reg_val[hang_detect_regs_count];
Jordan Crouse21f75a02012-08-09 15:08:59 -06002191 unsigned int wait;
Tarun Karra3335f142012-06-19 14:11:48 -07002192
2193 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002194
2195 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002196
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302197 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07002198 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302199 msecs = adreno_dev->wait_timeout;
2200
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002201 if (timestamp_cmp(timestamp, ts_issued) > 0) {
2202 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
2203 "last issued ts <%d:0x%x>\n",
2204 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002205 status = -EINVAL;
2206 goto done;
2207 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208
Jordan Crouse21f75a02012-08-09 15:08:59 -06002209 /*
2210 * Make the first timeout interval 100 msecs and then try to kick the
2211 * wptr again. This helps to ensure the wptr is updated properly. If
2212 * the requested timeout is less than 100 msecs, then wait 20msecs which
2213 * is the minimum amount of time we can safely wait at 100HZ
Lynus Vaz06a9a902011-10-04 19:25:33 +05302214 */
Jordan Crouse21f75a02012-08-09 15:08:59 -06002215
2216 if (msecs == 0 || msecs >= 100)
2217 wait = 100;
2218 else
2219 wait = 20;
2220
Tarun Karra3335f142012-06-19 14:11:48 -07002221 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002222 /*
2223 * If the context ID is invalid, we are in a race with
2224 * the context being destroyed by userspace so bail.
2225 */
2226 if (context_id == KGSL_CONTEXT_INVALID) {
2227 KGSL_DRV_WARN(device, "context was detached");
2228 status = -EINVAL;
2229 goto done;
2230 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002231 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07002232 /* if the timestamp happens while we're not
2233 * waiting, there's a chance that an interrupt
2234 * will not be generated and thus the timestamp
2235 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05302236 */
Jeremy Gebben63904832012-02-07 16:10:55 -07002237 queue_work(device->work_queue, &device->ts_expired_ws);
2238 status = 0;
2239 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002240 }
Jeremy Gebben63904832012-02-07 16:10:55 -07002241 adreno_poke(device);
2242 io_cnt = (io_cnt + 1) % 100;
2243 if (io_cnt <
2244 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2245 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002246
2247 if ((retries > 0) &&
2248 (adreno_hang_detect(device, prev_reg_val)))
2249 goto hang_dump;
2250
Jeremy Gebben63904832012-02-07 16:10:55 -07002251 mutex_unlock(&device->mutex);
2252 /* We need to make sure that the process is
2253 * placed in wait-q before its condition is called
2254 */
2255 status = kgsl_wait_event_interruptible_timeout(
2256 device->wait_queue,
2257 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002258 context, timestamp),
Jordan Crouse21f75a02012-08-09 15:08:59 -06002259 msecs_to_jiffies(wait), io);
2260
Jeremy Gebben63904832012-02-07 16:10:55 -07002261 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002262
Jeremy Gebben63904832012-02-07 16:10:55 -07002263 if (status > 0) {
2264 /*completed before the wait finished */
2265 status = 0;
2266 goto done;
2267 } else if (status < 0) {
2268 /*an error occurred*/
2269 goto done;
2270 }
2271 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07002272
Jordan Crouse21f75a02012-08-09 15:08:59 -06002273 time_elapsed += wait;
2274 wait = KGSL_TIMEOUT_PART;
2275
Tarun Karra3335f142012-06-19 14:11:48 -07002276 retries++;
2277
Jordan Crouse21f75a02012-08-09 15:08:59 -06002278 } while (!msecs || time_elapsed < msecs);
Tarun Karra3335f142012-06-19 14:11:48 -07002279
2280hang_dump:
Shubhraprakash Das54396e52012-03-10 13:24:54 -07002281 /*
2282 * Check if timestamp has retired here because we may have hit
2283 * recovery which can take some time and cause waiting threads
2284 * to timeout
2285 */
2286 if (kgsl_check_timestamp(device, context, timestamp))
2287 goto done;
Jeremy Gebben63904832012-02-07 16:10:55 -07002288 status = -ETIMEDOUT;
2289 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002290 "Device hang detected while waiting for timestamp: "
2291 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2292 "wptr: 0x%x\n",
2293 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002294 adreno_dev->ringbuffer.wptr);
2295 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002296 /* The timestamp that this process wanted
2297 * to wait on may be invalid or expired now
2298 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07002299 status = 0;
2300 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002301done:
2302 return (int)status;
2303}
2304
2305static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002306 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002307{
2308 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002309 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002310
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002311 /*
2312 * If the context ID is invalid, we are in a race with
2313 * the context being destroyed by userspace so bail.
2314 */
2315 if (context_id == KGSL_CONTEXT_INVALID) {
2316 KGSL_DRV_WARN(device, "context was detached");
2317 return timestamp;
2318 }
Jordan Crousec659f382012-04-16 11:10:41 -06002319 switch (type) {
2320 case KGSL_TIMESTAMP_QUEUED: {
2321 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2322 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2323
2324 timestamp = rb->timestamp[context_id];
2325 break;
2326 }
2327 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002328 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002329 break;
2330 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002331 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002332 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2333 break;
2334 }
2335
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002336 rmb();
2337
2338 return timestamp;
2339}
2340
2341static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2342 unsigned int cmd, void *data)
2343{
2344 int result = 0;
2345 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2346 struct kgsl_context *context;
2347
2348 switch (cmd) {
2349 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2350 binbase = data;
2351
2352 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2353 if (context) {
2354 adreno_drawctxt_set_bin_base_offset(
2355 dev_priv->device, context, binbase->offset);
2356 } else {
2357 result = -EINVAL;
2358 KGSL_DRV_ERR(dev_priv->device,
2359 "invalid drawctxt drawctxt_id %d "
2360 "device_id=%d\n",
2361 binbase->drawctxt_id, dev_priv->device->id);
2362 }
2363 break;
2364
2365 default:
2366 KGSL_DRV_INFO(dev_priv->device,
2367 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002368 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002369 break;
2370 }
2371 return result;
2372
2373}
2374
2375static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2376{
2377 gpu_freq /= 1000000;
2378 return ticks / gpu_freq;
2379}
2380
2381static void adreno_power_stats(struct kgsl_device *device,
2382 struct kgsl_power_stats *stats)
2383{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002384 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002385 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002386 unsigned int cycles;
2387
2388 /* Get the busy cycles counted since the counter was last reset */
2389 /* Calling this function also resets and restarts the counter */
2390
2391 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002392
2393 /* In order to calculate idle you have to have run the algorithm *
2394 * at least once to get a start time. */
2395 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002396 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002397 stats->total_time = tmp - pwr->time;
2398 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002399 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002400 pwrlevels[device->pwrctrl.active_pwrlevel].
2401 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002402 } else {
2403 stats->total_time = 0;
2404 stats->busy_time = 0;
2405 pwr->time = ktime_to_us(ktime_get());
2406 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002407}
2408
2409void adreno_irqctrl(struct kgsl_device *device, int state)
2410{
Jordan Crousea78c9172011-07-11 13:14:09 -06002411 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2412 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002413}
2414
Jordan Croused6535882012-06-20 08:22:16 -06002415static unsigned int adreno_gpuid(struct kgsl_device *device,
2416 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002417{
2418 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2419
Jordan Croused6535882012-06-20 08:22:16 -06002420 /* Some applications need to know the chip ID too, so pass
2421 * that as a parameter */
2422
2423 if (chipid != NULL)
2424 *chipid = adreno_dev->chip_id;
2425
Jordan Crousea0758f22011-12-07 11:19:22 -07002426 /* Standard KGSL gpuid format:
2427 * top word is 0x0002 for 2D or 0x0003 for 3D
2428 * Bottom word is core specific identifer
2429 */
2430
2431 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2432}
2433
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002434static const struct kgsl_functable adreno_functable = {
2435 /* Mandatory functions */
2436 .regread = adreno_regread,
2437 .regwrite = adreno_regwrite,
2438 .idle = adreno_idle,
2439 .isidle = adreno_isidle,
2440 .suspend_context = adreno_suspend_context,
2441 .start = adreno_start,
2442 .stop = adreno_stop,
2443 .getproperty = adreno_getproperty,
2444 .waittimestamp = adreno_waittimestamp,
2445 .readtimestamp = adreno_readtimestamp,
2446 .issueibcmds = adreno_ringbuffer_issueibcmds,
2447 .ioctl = adreno_ioctl,
2448 .setup_pt = adreno_setup_pt,
2449 .cleanup_pt = adreno_cleanup_pt,
2450 .power_stats = adreno_power_stats,
2451 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002452 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002453 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002454 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002455 /* Optional functions */
2456 .setstate = adreno_setstate,
2457 .drawctxt_create = adreno_drawctxt_create,
2458 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002459 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002460 .postmortem_dump = adreno_dump,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002461};
2462
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002463static struct platform_driver adreno_platform_driver = {
2464 .probe = adreno_probe,
2465 .remove = __devexit_p(adreno_remove),
2466 .suspend = kgsl_suspend_driver,
2467 .resume = kgsl_resume_driver,
2468 .id_table = adreno_id_table,
2469 .driver = {
2470 .owner = THIS_MODULE,
2471 .name = DEVICE_3D_NAME,
2472 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002473 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002474 }
2475};
2476
2477static int __init kgsl_3d_init(void)
2478{
2479 return platform_driver_register(&adreno_platform_driver);
2480}
2481
2482static void __exit kgsl_3d_exit(void)
2483{
2484 platform_driver_unregister(&adreno_platform_driver);
2485}
2486
2487module_init(kgsl_3d_init);
2488module_exit(kgsl_3d_exit);
2489
2490MODULE_DESCRIPTION("3D Graphics driver");
2491MODULE_VERSION("1.2");
2492MODULE_LICENSE("GPL v2");
2493MODULE_ALIAS("platform:kgsl_3d");