blob: 71bd4a22b366bdf7b9164e2a91f83647a31a8787 [file] [log] [blame]
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -07001/* Copyright (c) 2002,2007-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
Steve Mucklef132c6c2012-06-06 18:30:57 -070013#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070014#include <linux/uaccess.h>
15#include <linux/vmalloc.h>
16#include <linux/ioctl.h>
17#include <linux/sched.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060018#include <linux/of.h>
19#include <linux/of_device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020
21#include <mach/socinfo.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060022#include <mach/msm_bus_board.h>
23#include <mach/msm_bus.h>
24#include <mach/msm_dcvs.h>
25#include <mach/msm_dcvs_scm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27#include "kgsl.h"
28#include "kgsl_pwrscale.h"
29#include "kgsl_cffdump.h"
30#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060031#include "kgsl_iommu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
33#include "adreno.h"
34#include "adreno_pm4types.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070036#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070037#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
39#define DRIVER_VERSION_MAJOR 3
40#define DRIVER_VERSION_MINOR 1
41
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042/* Adreno MH arbiter config*/
43#define ADRENO_CFG_MHARB \
44 (0x10 \
45 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
46 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
47 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
48 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
52 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
53 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
54 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
55 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
56 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
57 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
58 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
59
60#define ADRENO_MMU_CONFIG \
61 (0x01 \
62 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
69 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
70 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
71 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
72 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
73
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074static const struct kgsl_functable adreno_functable;
75
76static struct adreno_device device_3d0 = {
77 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070078 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079 .name = DEVICE_3D0_NAME,
80 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060081 .mh = {
82 .mharb = ADRENO_CFG_MHARB,
83 /* Remove 1k boundary check in z470 to avoid a GPU
84 * hang. Notice that this solution won't work if
85 * both EBI and SMI are used
86 */
87 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088 /* turn off memory protection unit by setting
89 acceptable physical address range to include
90 all pages. */
91 .mpu_base = 0x00000000,
92 .mpu_range = 0xFFFFF000,
93 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060094 .mmu = {
95 .config = ADRENO_MMU_CONFIG,
96 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 .iomemname = KGSL_3D0_REG_MEMORY,
101 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600103 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
105 .suspend = kgsl_early_suspend_driver,
106 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600108#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600110 .gmem_base = 0,
111 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 .pfp_fw = NULL,
113 .pm4_fw = NULL,
Jordan Crouse21f75a02012-08-09 15:08:59 -0600114 .wait_timeout = 0, /* in milliseconds, 0 means disabled */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600115 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116};
117
Tarun Karra3335f142012-06-19 14:11:48 -0700118/* This set of registers are used for Hang detection
119 * If the values of these registers are same after
120 * KGSL_TIMEOUT_PART time, GPU hang is reported in
121 * kernel log.
122 */
123unsigned int hang_detect_regs[] = {
124 A3XX_RBBM_STATUS,
125 REG_CP_RB_RPTR,
126 REG_CP_IB1_BASE,
127 REG_CP_IB1_BUFSZ,
128 REG_CP_IB2_BASE,
129 REG_CP_IB2_BUFSZ,
130};
131
132const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700133
Jordan Crouse505df9c2011-07-28 08:37:59 -0600134/*
135 * This is the master list of all GPU cores that are supported by this
136 * driver.
137 */
138
139#define ANY_ID (~0)
140
141static const struct {
142 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600143 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600144 const char *pm4fw;
145 const char *pfpfw;
146 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700147 unsigned int istore_size;
148 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700149 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530150 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600151} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600152 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700153 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530154 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530155 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
156 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530157 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600158 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700159 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530160 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600161 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700162 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530163 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600164 /*
165 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
166 * a hardware problem.
167 */
168 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700169 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530170 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700171 { ADRENO_REV_A225, 2, 2, 0, 6,
172 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530173 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600174 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700175 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530176 1536, 768, 3, SZ_512K },
177 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530178 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530179 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
180 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700181 /* A3XX doesn't use the pix_shader_start */
Carter Cooper95f7f792012-08-19 13:40:34 -0600182 { ADRENO_REV_A320, 3, 2, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700183 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530184 512, 0, 2, SZ_512K },
liu zhongfd42e622012-05-01 19:18:30 -0700185 { ADRENO_REV_A330, 3, 3, 0, 0,
186 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
187 512, 0, 2, SZ_1M },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600188};
189
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600190static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191{
Jordan Crousea78c9172011-07-11 13:14:09 -0600192 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600193 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
Jordan Crousea78c9172011-07-11 13:14:09 -0600195 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196
197 if (device->requested_state == KGSL_STATE_NONE) {
198 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700199 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 queue_work(device->work_queue, &device->idle_check_ws);
201 } else if (device->pwrscale.policy != NULL) {
202 queue_work(device->work_queue, &device->idle_check_ws);
203 }
204 }
205
206 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800207 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 jiffies + device->pwrctrl.interval_timeout);
209 return result;
210}
211
Jordan Crouse9f739212011-07-28 08:37:57 -0600212static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 struct kgsl_pagetable *pagetable)
214{
215 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
216 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
217
218 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
219
220 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
221
222 kgsl_mmu_unmap(pagetable, &device->memstore);
223
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600224 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225}
226
227static int adreno_setup_pt(struct kgsl_device *device,
228 struct kgsl_pagetable *pagetable)
229{
230 int result = 0;
231 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
232 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
235 GSL_PT_PAGE_RV);
236 if (result)
237 goto error;
238
239 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
240 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
241 if (result)
242 goto unmap_buffer_desc;
243
244 result = kgsl_mmu_map_global(pagetable, &device->memstore,
245 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
246 if (result)
247 goto unmap_memptrs_desc;
248
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600249 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
251 if (result)
252 goto unmap_memstore_desc;
253
254 return result;
255
256unmap_memstore_desc:
257 kgsl_mmu_unmap(pagetable, &device->memstore);
258
259unmap_memptrs_desc:
260 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
261
262unmap_buffer_desc:
263 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
264
265error:
266 return result;
267}
268
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600269static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600270 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600271 uint32_t flags)
272{
273 unsigned int pt_val, reg_pt_val;
274 unsigned int link[200];
275 unsigned int *cmds = &link[0];
276 int sizedwords = 0;
277 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600278 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600279 struct kgsl_context *context;
280 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600281
282 if (!adreno_dev->drawctxt_active)
283 return kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700284 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600285
286 context = idr_find(&device->context_idr, context_id);
287 adreno_ctx = context->devctxt;
288
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600289 if (kgsl_mmu_enable_clk(&device->mmu,
290 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700291 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600292
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600293 cmds += __adreno_add_idle_indirect_cmds(cmds,
294 device->mmu.setstate_memory.gpuaddr +
295 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
296
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600297 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600298 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
299 device->mmu.setstate_memory.gpuaddr +
300 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
301 else
302 cmds += adreno_add_bank_change_cmds(cmds,
303 KGSL_IOMMU_CONTEXT_USER,
304 device->mmu.setstate_memory.gpuaddr +
305 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
306
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700307 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
308 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600309 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600310 /*
311 * We need to perfrom the following operations for all
312 * IOMMU units
313 */
314 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700315 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
316 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600317 /*
318 * Set address of the new pagetable by writng to IOMMU
319 * TTBR0 register
320 */
321 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700322 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
323 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600324 *cmds++ = reg_pt_val;
325 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
326 *cmds++ = 0x00000000;
327
328 /*
329 * Read back the ttbr0 register as a barrier to ensure
330 * above writes have completed
331 */
332 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700333 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
334 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600335 reg_pt_val,
336 device->mmu.setstate_memory.gpuaddr +
337 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600338 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600339 }
340 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
341 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700342 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600343 */
344 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700345 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
346 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700347
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600348 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700349 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
350 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700351 KGSL_IOMMU_CTX_TLBIALL);
352 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600353
354 cmds += __adreno_add_idle_indirect_cmds(cmds,
355 device->mmu.setstate_memory.gpuaddr +
356 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
357
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600358 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700359 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
360 KGSL_IOMMU_CONTEXT_USER,
361 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700362 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600363 device->mmu.setstate_memory.gpuaddr +
364 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
365 }
366 }
367
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600368 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600369 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700370 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
371 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600372 device->mmu.setstate_memory.gpuaddr +
373 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
374 else
375 cmds += adreno_add_bank_change_cmds(cmds,
376 KGSL_IOMMU_CONTEXT_PRIV,
377 device->mmu.setstate_memory.gpuaddr +
378 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
379
380 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600381 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700382 /* invalidate all base pointers */
383 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
384 *cmds++ = 0x7fff;
385 sizedwords += 2;
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600386 /*
387 * add an interrupt at the end of commands so that the smmu
388 * disable clock off function will get called
389 */
390 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
391 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
392 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600393 /* This returns the per context timestamp but we need to
394 * use the global timestamp for iommu clock disablement */
395 adreno_ringbuffer_issuecmds(device, adreno_ctx,
396 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600397 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600398 kgsl_mmu_disable_clk_on_ts(&device->mmu,
399 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600400 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600401}
402
403static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600404 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600405 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406{
407 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
408 unsigned int link[32];
409 unsigned int *cmds = &link[0];
410 int sizedwords = 0;
411 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600412 struct kgsl_context *context;
413 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600415 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530416 * Fix target freeze issue by adding TLB flush for each submit
417 * on A20X based targets.
418 */
419 if (adreno_is_a20x(adreno_dev))
420 flags |= KGSL_MMUFLAGS_TLBFLUSH;
421 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600422 * If possible, then set the state via the command stream to avoid
423 * a CPU idle. Otherwise, use the default setstate which uses register
424 * writes For CFF dump we must idle and use the registers so that it is
425 * easier to filter out the mmu accesses from the dump
426 */
427 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600428 context = idr_find(&device->context_idr, context_id);
429 adreno_ctx = context->devctxt;
430
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
432 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600433 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434 *cmds++ = 0x00000000;
435
436 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600437 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700438 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600439 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440 sizedwords += 4;
441 }
442
443 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
444 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600445 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446 1);
447 *cmds++ = 0x00000000;
448 sizedwords += 2;
449 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600450 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 *cmds++ = mh_mmu_invalidate;
452 sizedwords += 2;
453 }
454
455 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600456 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 /* HW workaround: to resolve MMU page fault interrupts
458 * caused by the VGT.It prevents the CP PFP from filling
459 * the VGT DMA request fifo too early,thereby ensuring
460 * that the VGT will not fetch vertex/bin data until
461 * after the page table base register has been updated.
462 *
463 * Two null DRAW_INDX_BIN packets are inserted right
464 * after the page table base update, followed by a
465 * wait for idle. The null packets will fill up the
466 * VGT DMA request fifo and prevent any further
467 * vertex/bin updates from occurring until the wait
468 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600469 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470 *cmds++ = (0x4 << 16) |
471 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
472 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600473 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600474 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600475 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476 *cmds++ = 0; /* viz query info */
477 *cmds++ = 0x0003C004; /* draw indicator */
478 *cmds++ = 0; /* bin base */
479 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600480 *cmds++ =
481 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600483 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 *cmds++ = 0; /* viz query info */
485 *cmds++ = 0x0003C004; /* draw indicator */
486 *cmds++ = 0; /* bin base */
487 *cmds++ = 3; /* bin size */
488 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600489 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600491 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 *cmds++ = 0x00000000;
493 sizedwords += 21;
494 }
495
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600496
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600498 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499 *cmds++ = 0x7fff; /* invalidate all base pointers */
500 sizedwords += 2;
501 }
502
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600503 adreno_ringbuffer_issuecmds(device, adreno_ctx,
504 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600506 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600507 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600508 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509}
510
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600511static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600512 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600513 uint32_t flags)
514{
515 /* call the mmu specific handler */
516 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600517 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600518 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600519 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600520}
521
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700523a3xx_getchipid(struct kgsl_device *device)
524{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600525 struct kgsl_device_platform_data *pdata =
526 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700527
Jordan Crouse54154c62012-03-27 16:33:26 -0600528 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600529 * All current A3XX chipids are detected at the SOC level. Leave this
530 * function here to support any future GPUs that have working
531 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600532 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700533
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600534 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700535}
536
537static unsigned int
538a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539{
540 unsigned int chipid = 0;
541 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600542 struct kgsl_device_platform_data *pdata =
543 kgsl_device_get_drvdata(device);
544
545 /* If the chip id is set at the platform level, then just use that */
546
547 if (pdata->chipid != 0)
548 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549
550 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
551 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
552 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
553
554 /*
555 * adreno 22x gpus are indicated by coreid 2,
556 * but REG_RBBM_PERIPHID1 always contains 0 for this field
557 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600558 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 chipid = 2 << 24;
560 else
561 chipid = (coreid & 0xF) << 24;
562
563 chipid |= ((majorid >> 4) & 0xF) << 16;
564
565 minorid = ((revid >> 0) & 0xFF);
566
567 patchid = ((revid >> 16) & 0xFF);
568
569 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530570 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 if (cpu_is_qsd8x50())
572 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530573 else if (cpu_is_msm8625() && minorid == 0)
574 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575
576 chipid |= (minorid << 8) | patchid;
577
578 return chipid;
579}
580
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700581static unsigned int
582adreno_getchipid(struct kgsl_device *device)
583{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600584 struct kgsl_device_platform_data *pdata =
585 kgsl_device_get_drvdata(device);
586
587 /*
588 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
589 * an A2XX processor
590 */
591
592 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700593 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600594 else
595 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700596}
597
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598static inline bool _rev_match(unsigned int id, unsigned int entry)
599{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600600 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603static void
604adreno_identify_gpu(struct adreno_device *adreno_dev)
605{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600606 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607
608 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
609
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600610 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
611 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
612 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
613 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614
Jordan Crouse505df9c2011-07-28 08:37:59 -0600615 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
616 if (core == adreno_gpulist[i].core &&
617 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600618 _rev_match(minor, adreno_gpulist[i].minor) &&
619 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621 }
622
Jordan Crouse505df9c2011-07-28 08:37:59 -0600623 if (i == ARRAY_SIZE(adreno_gpulist)) {
624 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
625 return;
626 }
627
628 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
629 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
630 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
631 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700632 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
633 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700634 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600635 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636}
637
Lokesh Batra805e1e12012-08-03 08:34:06 -0600638static struct platform_device_id adreno_id_table[] = {
639 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
640 {},
641};
642
643MODULE_DEVICE_TABLE(platform, adreno_id_table);
644
645static struct of_device_id adreno_match_table[] = {
646 { .compatible = "qcom,kgsl-3d0", },
647 {}
648};
649
650static inline int adreno_of_read_property(struct device_node *node,
651 const char *prop, unsigned int *ptr)
652{
653 int ret = of_property_read_u32(node, prop, ptr);
654 if (ret)
655 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
656 return ret;
657}
658
659static struct device_node *adreno_of_find_subnode(struct device_node *parent,
660 const char *name)
661{
662 struct device_node *child;
663
664 for_each_child_of_node(parent, child) {
665 if (of_device_is_compatible(child, name))
666 return child;
667 }
668
669 return NULL;
670}
671
672static int adreno_of_get_pwrlevels(struct device_node *parent,
673 struct kgsl_device_platform_data *pdata)
674{
675 struct device_node *node, *child;
676 int ret = -EINVAL;
677
678 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
679
680 if (node == NULL) {
681 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
682 return -EINVAL;
683 }
684
685 pdata->num_levels = 0;
686
687 for_each_child_of_node(node, child) {
688 unsigned int index;
689 struct kgsl_pwrlevel *level;
690
691 if (adreno_of_read_property(child, "reg", &index))
692 goto done;
693
694 if (index >= KGSL_MAX_PWRLEVELS) {
695 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
696 index);
697 continue;
698 }
699
700 if (index >= pdata->num_levels)
701 pdata->num_levels = index + 1;
702
703 level = &pdata->pwrlevel[index];
704
705 if (adreno_of_read_property(child, "qcom,gpu-freq",
706 &level->gpu_freq))
707 goto done;
708
709 if (adreno_of_read_property(child, "qcom,bus-freq",
710 &level->bus_freq))
711 goto done;
712
713 if (adreno_of_read_property(child, "qcom,io-fraction",
714 &level->io_fraction))
715 level->io_fraction = 0;
716 }
717
718 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
719 &pdata->init_level))
720 pdata->init_level = 1;
721
722 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
723 KGSL_CORE_ERR("Initial power level out of range\n");
724 pdata->init_level = 1;
725 }
726
727 ret = 0;
728done:
729 return ret;
730
731}
732static void adreno_of_free_bus_scale_info(struct msm_bus_scale_pdata *pdata)
733{
734 int i;
735
736 if (pdata == NULL)
737 return;
738
739 for (i = 0; pdata->usecase && i < pdata->num_usecases; i++)
740 kfree(pdata->usecase[i].vectors);
741
742 kfree(pdata->usecase);
743 kfree(pdata);
744}
745
746struct msm_bus_scale_pdata *adreno_of_get_bus_scale(struct device_node *node)
747{
748 static int bus_vectors_src[3] = {MSM_BUS_MASTER_GRAPHICS_3D,
749 MSM_BUS_MASTER_GRAPHICS_3D_PORT1, MSM_BUS_MASTER_V_OCMEM_GFX3D};
750 static int bus_vectors_dst[2] = {MSM_BUS_SLAVE_EBI_CH0,
751 MSM_BUS_SLAVE_OCMEM};
752 const unsigned int *vectors;
753 struct msm_bus_scale_pdata *pdata;
754 int i, j, len, num_paths;
755 int ret = -EINVAL;
756
757 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
758
759 if (!pdata) {
760 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
761 return ERR_PTR(-ENOMEM);
762 }
763
764 if (adreno_of_read_property(node, "qcom,grp3d-num-bus-scale-usecases",
765 &pdata->num_usecases)) {
766 pdata->num_usecases = 0;
767 goto err;
768 }
769
770 pdata->usecase = kzalloc(pdata->num_usecases *
771 sizeof(struct msm_bus_paths), GFP_KERNEL);
772
773 if (pdata->usecase == NULL) {
774 KGSL_CORE_ERR("kzalloc (%d) failed\n",
775 pdata->num_usecases * sizeof(struct msm_bus_paths));
776 ret = -ENOMEM;
777 goto err;
778 }
779
780 if (adreno_of_read_property(node, "qcom,grp3d-num-vectors-per-usecase",
781 &num_paths))
782 goto err;
783
784 vectors = of_get_property(node, "qcom,grp3d-vectors", &len);
785
786 if (len != pdata->num_usecases * num_paths *
787 sizeof(struct msm_bus_vectors)) {
788 KGSL_CORE_ERR("Invalid size for the bus scale vectors\n");
789 goto err;
790 }
791
792 for (i = 0; i < pdata->num_usecases; i++) {
793 pdata->usecase[i].num_paths = num_paths;
794 pdata->usecase[i].vectors = kzalloc(num_paths *
795 sizeof(struct msm_bus_vectors),
796 GFP_KERNEL);
797 if (!pdata->usecase[i].vectors) {
798 KGSL_CORE_ERR("kzalloc(%d) failed\n",
799 num_paths * sizeof(struct msm_bus_vectors));
800 ret = -ENOMEM;
801 goto err;
802 }
803 for (j = 0; j < num_paths; j++) {
804 int index = (i * num_paths + j) * 4;
805 pdata->usecase[i].vectors[j].src =
806 bus_vectors_src[be32_to_cpu(vectors[index])];
807 pdata->usecase[i].vectors[j].dst =
808 bus_vectors_dst[
809 be32_to_cpu(vectors[index + 1])];
810 pdata->usecase[i].vectors[j].ab =
811 be32_to_cpu(vectors[index + 2]);
812 pdata->usecase[i].vectors[j].ib =
813 KGSL_CONVERT_TO_MBPS(
814 be32_to_cpu(vectors[index + 3]));
815 }
816 }
817
818 pdata->name = "grp3d";
819
820 return pdata;
821
822err:
823 adreno_of_free_bus_scale_info(pdata);
824
825 return ERR_PTR(ret);
826}
827
828static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
829{
830 struct device_node *node, *child;
831 struct msm_dcvs_core_info *info = NULL;
832 int count = 0;
833 int ret = -EINVAL;
834
835 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
836 if (node == NULL)
837 return ERR_PTR(-EINVAL);
838
839 info = kzalloc(sizeof(*info), GFP_KERNEL);
840
841 if (info == NULL) {
842 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
843 ret = -ENOMEM;
844 goto err;
845 }
846
847 for_each_child_of_node(node, child)
848 count++;
849
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700850 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600851
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700852 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600853 sizeof(struct msm_dcvs_freq_entry),
854 GFP_KERNEL);
855
856 if (info->freq_tbl == NULL) {
857 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700858 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600859 sizeof(struct msm_dcvs_freq_entry));
860 ret = -ENOMEM;
861 goto err;
862 }
863
864 for_each_child_of_node(node, child) {
865 unsigned int index;
866
867 if (adreno_of_read_property(child, "reg", &index))
868 goto err;
869
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700870 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600871 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
872 index);
873 continue;
874 }
875
876 if (adreno_of_read_property(child, "qcom,freq",
877 &info->freq_tbl[index].freq))
878 goto err;
879
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700880 if (adreno_of_read_property(child, "qcom,voltage",
881 &info->freq_tbl[index].voltage))
882 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600883
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700884 if (adreno_of_read_property(child, "qcom,is_trans_level",
885 &info->freq_tbl[index].is_trans_level))
886 info->freq_tbl[index].is_trans_level = 0;
887
888 if (adreno_of_read_property(child, "qcom,active-energy-offset",
889 &info->freq_tbl[index].active_energy_offset))
890 info->freq_tbl[index].active_energy_offset = 0;
891
892 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
893 &info->freq_tbl[index].leakage_energy_offset))
894 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600895 }
896
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700897 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
898 goto err;
899
900 info->sensors = kzalloc(info->num_cores *
901 sizeof(int),
902 GFP_KERNEL);
903
904 for (count = 0; count < info->num_cores; count++) {
905 if (adreno_of_read_property(node, "qcom,sensors",
906 &(info->sensors[count])))
907 goto err;
908 }
909
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700910 if (adreno_of_read_property(node, "qcom,core-core-type",
911 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600912 goto err;
913
914 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
915 &info->algo_param.disable_pc_threshold))
916 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700917 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
918 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600919 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700920 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
921 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600922 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600923 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
924 &info->algo_param.em_max_util_pct))
925 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700926 if (adreno_of_read_property(node, "qcom,algo-group-id",
927 &info->algo_param.group_id))
928 goto err;
929 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
930 &info->algo_param.max_freq_chg_time_us))
931 goto err;
932 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
933 &info->algo_param.slack_mode_dynamic))
934 goto err;
935 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
936 &info->algo_param.slack_weight_thresh_pct))
937 goto err;
938 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
939 &info->algo_param.slack_time_min_us))
940 goto err;
941 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
942 &info->algo_param.slack_time_max_us))
943 goto err;
944 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
945 &info->algo_param.ss_win_size_min_us))
946 goto err;
947 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
948 &info->algo_param.ss_win_size_max_us))
949 goto err;
950 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
951 &info->algo_param.ss_util_pct))
952 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600953 if (adreno_of_read_property(node, "qcom,algo-ss-iobusy-conv",
954 &info->algo_param.ss_iobusy_conv))
955 goto err;
956
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700957 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
958 &info->energy_coeffs.active_coeff_a))
959 goto err;
960 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
961 &info->energy_coeffs.active_coeff_b))
962 goto err;
963 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
964 &info->energy_coeffs.active_coeff_c))
965 goto err;
966 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
967 &info->energy_coeffs.leakage_coeff_a))
968 goto err;
969 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
970 &info->energy_coeffs.leakage_coeff_b))
971 goto err;
972 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
973 &info->energy_coeffs.leakage_coeff_c))
974 goto err;
975 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
976 &info->energy_coeffs.leakage_coeff_d))
977 goto err;
978
979 if (adreno_of_read_property(node, "qcom,power-current-temp",
980 &info->power_param.current_temp))
981 goto err;
982
Lokesh Batra805e1e12012-08-03 08:34:06 -0600983 return info;
984
985err:
986 if (info)
987 kfree(info->freq_tbl);
988
989 kfree(info);
990
991 return ERR_PTR(ret);
992}
993
994static int adreno_of_get_iommu(struct device_node *parent,
995 struct kgsl_device_platform_data *pdata)
996{
997 struct device_node *node, *child;
998 struct kgsl_device_iommu_data *data = NULL;
999 struct kgsl_iommu_ctx *ctxs = NULL;
1000 u32 reg_val[2];
1001 int ctx_index = 0;
1002
1003 node = of_parse_phandle(parent, "iommu", 0);
1004 if (node == NULL)
1005 return -EINVAL;
1006
1007 data = kzalloc(sizeof(*data), GFP_KERNEL);
1008 if (data == NULL) {
1009 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
1010 goto err;
1011 }
1012
1013 if (of_property_read_u32_array(node, "reg", reg_val, 2))
1014 goto err;
1015
1016 data->physstart = reg_val[0];
1017 data->physend = data->physstart + reg_val[1] - 1;
1018
1019 data->iommu_ctx_count = 0;
1020
1021 for_each_child_of_node(node, child)
1022 data->iommu_ctx_count++;
1023
1024 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
1025 GFP_KERNEL);
1026
1027 if (ctxs == NULL) {
1028 KGSL_CORE_ERR("kzalloc(%d) failed\n",
1029 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
1030 goto err;
1031 }
1032
1033 for_each_child_of_node(node, child) {
1034 int ret = of_property_read_string(child, "label",
1035 &ctxs[ctx_index].iommu_ctx_name);
1036
1037 if (ret) {
1038 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
1039 goto err;
1040 }
1041
1042 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
1043 &ctxs[ctx_index].ctx_id))
1044 goto err;
1045
1046 ctx_index++;
1047 }
1048
1049 data->iommu_ctxs = ctxs;
1050
1051 pdata->iommu_data = data;
1052 pdata->iommu_count = 1;
1053
1054 return 0;
1055
1056err:
1057 kfree(ctxs);
1058 kfree(data);
1059
1060 return -EINVAL;
1061}
1062
1063static int adreno_of_get_pdata(struct platform_device *pdev)
1064{
1065 struct kgsl_device_platform_data *pdata = NULL;
1066 struct kgsl_device *device;
1067 int ret = -EINVAL;
1068
1069 pdev->id_entry = adreno_id_table;
1070
1071 pdata = pdev->dev.platform_data;
1072 if (pdata)
1073 return 0;
1074
1075 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1076 KGSL_CORE_ERR("Unable to read 'label'\n");
1077 goto err;
1078 }
1079
1080 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1081 goto err;
1082
1083 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1084 if (pdata == NULL) {
1085 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1086 ret = -ENOMEM;
1087 goto err;
1088 }
1089
1090 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1091 &pdata->chipid))
1092 goto err;
1093
1094 /* pwrlevel Data */
1095 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1096 if (ret)
1097 goto err;
1098
1099 /* Default value is 83, if not found in DT */
1100 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1101 &pdata->idle_timeout))
1102 pdata->idle_timeout = 83;
1103
1104 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1105 &pdata->nap_allowed))
1106 pdata->nap_allowed = 1;
1107
1108 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1109 &pdata->clk_map))
1110 goto err;
1111
1112 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1113
1114 if (device->id != KGSL_DEVICE_3D0)
1115 goto err;
1116
1117 /* Bus Scale Data */
1118
1119 pdata->bus_scale_table = adreno_of_get_bus_scale(pdev->dev.of_node);
1120 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1121 ret = PTR_ERR(pdata->bus_scale_table);
1122 goto err;
1123 }
1124
1125 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1126 if (IS_ERR_OR_NULL(pdata->core_info)) {
1127 ret = PTR_ERR(pdata->core_info);
1128 goto err;
1129 }
1130
1131 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1132 if (ret)
1133 goto err;
1134
1135 pdev->dev.platform_data = pdata;
1136 return 0;
1137
1138err:
1139 if (pdata) {
1140 adreno_of_free_bus_scale_info(pdata->bus_scale_table);
1141 if (pdata->core_info)
1142 kfree(pdata->core_info->freq_tbl);
1143 kfree(pdata->core_info);
1144
1145 if (pdata->iommu_data)
1146 kfree(pdata->iommu_data->iommu_ctxs);
1147
1148 kfree(pdata->iommu_data);
1149 }
1150
1151 kfree(pdata);
1152
1153 return ret;
1154}
1155
liu zhong7dfa2a32012-04-27 19:11:01 -07001156#ifdef CONFIG_MSM_OCMEM
1157static int
1158adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1159{
Jordan Crousec0978202012-08-29 14:35:51 -06001160 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001161 return 0;
1162
1163 /* OCMEM is only needed once, do not support consective allocation */
1164 if (adreno_dev->ocmem_hdl != NULL)
1165 return 0;
1166
1167 adreno_dev->ocmem_hdl =
1168 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1169 if (adreno_dev->ocmem_hdl == NULL)
1170 return -ENOMEM;
1171
1172 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001173 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001174
1175 return 0;
1176}
1177
1178static void
1179adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1180{
Jordan Crousec0978202012-08-29 14:35:51 -06001181 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001182 return;
1183
1184 if (adreno_dev->ocmem_hdl == NULL)
1185 return;
1186
1187 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1188 adreno_dev->ocmem_hdl = NULL;
1189}
1190#else
1191static int
1192adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1193{
1194 return 0;
1195}
1196
1197static void
1198adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1199{
1200}
1201#endif
1202
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001203static int __devinit
1204adreno_probe(struct platform_device *pdev)
1205{
1206 struct kgsl_device *device;
1207 struct adreno_device *adreno_dev;
1208 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001209 bool is_dt;
1210
1211 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1212
1213 if (is_dt && pdev->dev.of_node) {
1214 status = adreno_of_get_pdata(pdev);
1215 if (status)
1216 goto error_return;
1217 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218
1219 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1220 adreno_dev = ADRENO_DEVICE(device);
1221 device->parentdev = &pdev->dev;
1222
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223 status = adreno_ringbuffer_init(device);
1224 if (status != 0)
1225 goto error;
1226
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001227 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228 if (status)
1229 goto error_close_rb;
1230
1231 adreno_debugfs_init(device);
1232
1233 kgsl_pwrscale_init(device);
1234 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1235
1236 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1237 return 0;
1238
1239error_close_rb:
1240 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1241error:
1242 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001243error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001244 return status;
1245}
1246
1247static int __devexit adreno_remove(struct platform_device *pdev)
1248{
1249 struct kgsl_device *device;
1250 struct adreno_device *adreno_dev;
1251
1252 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1253 adreno_dev = ADRENO_DEVICE(device);
1254
1255 kgsl_pwrscale_detach_policy(device);
1256 kgsl_pwrscale_close(device);
1257
1258 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1259 kgsl_device_platform_remove(device);
1260
1261 return 0;
1262}
1263
1264static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1265{
1266 int status = -EINVAL;
1267 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001268
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001269 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1270 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271
1272 /* Power up the device */
1273 kgsl_pwrctrl_enable(device);
1274
1275 /* Identify the specific GPU */
1276 adreno_identify_gpu(adreno_dev);
1277
Jordan Crouse505df9c2011-07-28 08:37:59 -06001278 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1279 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1280 adreno_dev->chip_id);
1281 goto error_clk_off;
1282 }
1283
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001284 /* Set up the MMU */
1285 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001286 /*
1287 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1288 * on older gpus
1289 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001290 if (adreno_is_a20x(adreno_dev)) {
1291 device->mh.mh_intf_cfg1 = 0;
1292 device->mh.mh_intf_cfg2 = 0;
1293 }
1294
1295 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001296 }
1297
Tarun Karra3335f142012-06-19 14:11:48 -07001298 /* Assign correct RBBM status register to hang detect regs
1299 */
1300 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1301
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001302 status = kgsl_mmu_start(device);
1303 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 goto error_clk_off;
1305
liu zhong7dfa2a32012-04-27 19:11:01 -07001306 status = adreno_ocmem_gmem_malloc(adreno_dev);
1307 if (status) {
1308 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1309 goto error_mmu_off;
1310 }
1311
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001312 /* Start the GPU */
1313 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314
1315 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001316 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317
1318 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001319 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001320 /* While recovery is on we do not want timer to
1321 * fire and attempt to change any device state */
1322 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1323 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001324 return 0;
1325 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001326
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001327 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001328
1329error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001330 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001331
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001332error_clk_off:
1333 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001334
1335 return status;
1336}
1337
1338static int adreno_stop(struct kgsl_device *device)
1339{
1340 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1341
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001342 adreno_dev->drawctxt_active = NULL;
1343
1344 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1345
Shubhraprakash Das79447952012-04-26 18:12:23 -06001346 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001347
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001348 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301349 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001350 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001351
liu zhong7dfa2a32012-04-27 19:11:01 -07001352 adreno_ocmem_gmem_free(adreno_dev);
1353
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001354 /* Power down the device */
1355 kgsl_pwrctrl_disable(device);
1356
1357 return 0;
1358}
1359
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001360static void adreno_mark_context_status(struct kgsl_device *device,
1361 int recovery_status)
1362{
1363 struct kgsl_context *context;
1364 int next = 0;
1365 /*
1366 * Set the reset status of all contexts to
1367 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1368 * since thats the guilty party, if recovery failed then
1369 * mark all as guilty
1370 */
1371 while ((context = idr_get_next(&device->context_idr, &next))) {
1372 struct adreno_context *adreno_context = context->devctxt;
1373 if (recovery_status) {
1374 context->reset_status =
1375 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1376 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1377 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1378 context->reset_status) {
1379 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1380 CTXT_FLAGS_GPU_HANG_RECOVERED))
1381 context->reset_status =
1382 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1383 else
1384 context->reset_status =
1385 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1386 }
1387 next = next + 1;
1388 }
1389}
1390
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001391static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1392{
1393 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1394 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1395 struct kgsl_context *context;
1396 struct adreno_context *temp_adreno_context;
1397 int next = 0;
1398
1399 while ((context = idr_get_next(&device->context_idr, &next))) {
1400 temp_adreno_context = context->devctxt;
1401 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1402 kgsl_sharedmem_writel(&device->memstore,
1403 KGSL_MEMSTORE_OFFSET(context->id,
1404 soptimestamp),
1405 rb->timestamp[context->id]);
1406 kgsl_sharedmem_writel(&device->memstore,
1407 KGSL_MEMSTORE_OFFSET(context->id,
1408 eoptimestamp),
1409 rb->timestamp[context->id]);
1410 }
1411 next = next + 1;
1412 }
1413}
1414
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001415static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1416{
1417 vfree(rec_data->rb_buffer);
1418 vfree(rec_data->bad_rb_buffer);
1419}
1420
1421static int adreno_setup_recovery_data(struct kgsl_device *device,
1422 struct adreno_recovery_data *rec_data)
1423{
1424 int ret = 0;
1425 unsigned int ib1_sz, ib2_sz;
1426 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1427 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1428
1429 memset(rec_data, 0, sizeof(*rec_data));
1430
1431 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1432 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1433 if (ib1_sz || ib2_sz)
1434 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1435
1436 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1437 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1438 current_context));
1439
1440 kgsl_sharedmem_readl(&device->memstore,
1441 &rec_data->global_eop,
1442 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1443 eoptimestamp));
1444
1445 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1446 if (!rec_data->rb_buffer) {
1447 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1448 rb->buffer_desc.size);
1449 return -ENOMEM;
1450 }
1451
1452 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1453 if (!rec_data->bad_rb_buffer) {
1454 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1455 rb->buffer_desc.size);
1456 ret = -ENOMEM;
1457 goto done;
1458 }
1459
1460done:
1461 if (ret) {
1462 vfree(rec_data->rb_buffer);
1463 vfree(rec_data->bad_rb_buffer);
1464 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001465 return ret;
1466}
1467
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001468static int
1469_adreno_recover_hang(struct kgsl_device *device,
1470 struct adreno_recovery_data *rec_data,
1471 bool try_bad_commands)
1472{
1473 int ret;
1474 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1475 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1476 struct kgsl_context *context;
1477 struct adreno_context *adreno_context = NULL;
1478 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1479
1480 context = idr_find(&device->context_idr, rec_data->context_id);
1481 if (context == NULL) {
1482 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1483 rec_data->context_id);
1484 } else {
1485 adreno_context = context->devctxt;
1486 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1487 }
1488
1489 /* Extract valid contents from rb which can still be executed after
1490 * hang */
1491 ret = adreno_ringbuffer_extract(rb, rec_data);
1492 if (ret)
1493 goto done;
1494
1495 /* restart device */
1496 ret = adreno_stop(device);
1497 if (ret) {
1498 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1499 goto done;
1500 }
1501
1502 ret = adreno_start(device, true);
1503 if (ret) {
1504 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1505 goto done;
1506 }
1507
1508 if (context)
1509 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1510 KGSL_MEMSTORE_GLOBAL);
1511
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001512 /* If iommu is used then we need to make sure that the iommu clocks
1513 * are on since there could be commands in pipeline that touch iommu */
1514 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1515 ret = kgsl_mmu_enable_clk(&device->mmu,
1516 KGSL_IOMMU_CONTEXT_USER);
1517 if (ret)
1518 goto done;
1519 }
1520
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001521 /* Do not try the bad caommands if recovery has failed bad commands
1522 * once already */
1523 if (!try_bad_commands)
1524 rec_data->bad_rb_size = 0;
1525
1526 if (rec_data->bad_rb_size) {
1527 int idle_ret;
1528 /* submit the bad and good context commands and wait for
1529 * them to pass */
1530 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1531 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001532 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001533 if (idle_ret) {
1534 ret = adreno_stop(device);
1535 if (ret) {
1536 KGSL_DRV_ERR(device,
1537 "Device stop failed in recovery\n");
1538 goto done;
1539 }
1540 ret = adreno_start(device, true);
1541 if (ret) {
1542 KGSL_DRV_ERR(device,
1543 "Device start failed in recovery\n");
1544 goto done;
1545 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001546 if (context)
1547 kgsl_mmu_setstate(&device->mmu,
1548 adreno_context->pagetable,
1549 KGSL_MEMSTORE_GLOBAL);
1550
1551 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1552 ret = kgsl_mmu_enable_clk(&device->mmu,
1553 KGSL_IOMMU_CONTEXT_USER);
1554 if (ret)
1555 goto done;
1556 }
1557
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001558 ret = idle_ret;
1559 KGSL_DRV_ERR(device,
1560 "Bad context commands hung in recovery\n");
1561 } else {
1562 KGSL_DRV_ERR(device,
1563 "Bad context commands succeeded in recovery\n");
1564 if (adreno_context)
1565 adreno_context->flags = (adreno_context->flags &
1566 ~CTXT_FLAGS_GPU_HANG) |
1567 CTXT_FLAGS_GPU_HANG_RECOVERED;
1568 adreno_dev->drawctxt_active = last_active_ctx;
1569 }
1570 }
1571 /* If either the bad command sequence failed or we did not play it */
1572 if (ret || !rec_data->bad_rb_size) {
1573 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1574 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001575 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001576 if (ret) {
1577 /* If we fail here we can try to invalidate another
1578 * context and try recovering again */
1579 ret = -EAGAIN;
1580 goto done;
1581 }
1582 /* ringbuffer now has data from the last valid context id,
1583 * so restore the active_ctx to the last valid context */
1584 if (rec_data->last_valid_ctx_id) {
1585 struct kgsl_context *last_ctx =
1586 idr_find(&device->context_idr,
1587 rec_data->last_valid_ctx_id);
1588 if (last_ctx)
1589 adreno_dev->drawctxt_active = last_ctx->devctxt;
1590 }
1591 }
1592done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001593 /* Turn off iommu clocks */
1594 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1595 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001596 return ret;
1597}
1598
1599static int
1600adreno_recover_hang(struct kgsl_device *device,
1601 struct adreno_recovery_data *rec_data)
1602{
1603 int ret = 0;
1604 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1605 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1606 unsigned int timestamp;
1607
1608 KGSL_DRV_ERR(device,
1609 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1610 "Bad context_id: %u, global_eop: 0x%x\n",
1611 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1612
1613 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1614 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1615
1616 /* We may need to replay commands multiple times based on whether
1617 * multiple contexts hang the GPU */
1618 while (true) {
1619 if (!ret)
1620 ret = _adreno_recover_hang(device, rec_data, true);
1621 else
1622 ret = _adreno_recover_hang(device, rec_data, false);
1623
1624 if (-EAGAIN == ret) {
1625 /* setup new recovery parameters and retry, this
1626 * means more than 1 contexts are causing hang */
1627 adreno_destroy_recovery_data(rec_data);
1628 adreno_setup_recovery_data(device, rec_data);
1629 KGSL_DRV_ERR(device,
1630 "Retry recovery from 3D GPU hang. Recovery parameters: "
1631 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1632 rec_data->ib1, rec_data->context_id,
1633 rec_data->global_eop);
1634 } else {
1635 break;
1636 }
1637 }
1638
1639 if (ret)
1640 goto done;
1641
1642 /* Restore correct states after recovery */
1643 if (adreno_dev->drawctxt_active)
1644 device->mmu.hwpagetable =
1645 adreno_dev->drawctxt_active->pagetable;
1646 else
1647 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1648 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1649 kgsl_sharedmem_writel(&device->memstore,
1650 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1651 eoptimestamp),
1652 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1653done:
1654 adreno_set_max_ts_for_bad_ctxs(device);
1655 adreno_mark_context_status(device, ret);
1656 if (!ret)
1657 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1658 else
1659 KGSL_DRV_ERR(device, "Recovery failed\n");
1660 return ret;
1661}
1662
1663int
1664adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001667 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668
1669 if (device->state == KGSL_STATE_HUNG)
1670 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001671 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001672 mutex_unlock(&device->mutex);
1673 wait_for_completion(&device->recovery_gate);
1674 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001675 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001676 result = 0;
1677 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001678 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001679 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001680 /* Detected a hang */
1681
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001682 /* Get the recovery data as soon as hang is detected */
1683 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001684 /*
1685 * Trigger an automatic dump of the state to
1686 * the console
1687 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001688 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001689
1690 /*
1691 * Make a GPU snapshot. For now, do it after the PM dump so we
1692 * can at least be sure the PM dump will work as it always has
1693 */
1694 kgsl_device_snapshot(device, 1);
1695
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001696 result = adreno_recover_hang(device, &rec_data);
1697 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001698 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001699 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001700 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001701 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001702 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1703 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001704 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001705 }
1706done:
1707 return result;
1708}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001709EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001710
1711static int adreno_getproperty(struct kgsl_device *device,
1712 enum kgsl_property_type type,
1713 void *value,
1714 unsigned int sizebytes)
1715{
1716 int status = -EINVAL;
1717 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1718
1719 switch (type) {
1720 case KGSL_PROP_DEVICE_INFO:
1721 {
1722 struct kgsl_devinfo devinfo;
1723
1724 if (sizebytes != sizeof(devinfo)) {
1725 status = -EINVAL;
1726 break;
1727 }
1728
1729 memset(&devinfo, 0, sizeof(devinfo));
1730 devinfo.device_id = device->id+1;
1731 devinfo.chip_id = adreno_dev->chip_id;
1732 devinfo.mmu_enabled = kgsl_mmu_enabled();
1733 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001734 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1735 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001736
1737 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1738 0) {
1739 status = -EFAULT;
1740 break;
1741 }
1742 status = 0;
1743 }
1744 break;
1745 case KGSL_PROP_DEVICE_SHADOW:
1746 {
1747 struct kgsl_shadowprop shadowprop;
1748
1749 if (sizebytes != sizeof(shadowprop)) {
1750 status = -EINVAL;
1751 break;
1752 }
1753 memset(&shadowprop, 0, sizeof(shadowprop));
1754 if (device->memstore.hostptr) {
1755 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1756 * anything to mmap().
1757 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001758 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001759 shadowprop.size = device->memstore.size;
1760 /* GSL needs this to be set, even if it
1761 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001762 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1763 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001764 }
1765 if (copy_to_user(value, &shadowprop,
1766 sizeof(shadowprop))) {
1767 status = -EFAULT;
1768 break;
1769 }
1770 status = 0;
1771 }
1772 break;
1773 case KGSL_PROP_MMU_ENABLE:
1774 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001775 int mmu_prop = kgsl_mmu_enabled();
1776
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001777 if (sizebytes != sizeof(int)) {
1778 status = -EINVAL;
1779 break;
1780 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001781 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782 status = -EFAULT;
1783 break;
1784 }
1785 status = 0;
1786 }
1787 break;
1788 case KGSL_PROP_INTERRUPT_WAITS:
1789 {
1790 int int_waits = 1;
1791 if (sizebytes != sizeof(int)) {
1792 status = -EINVAL;
1793 break;
1794 }
1795 if (copy_to_user(value, &int_waits, sizeof(int))) {
1796 status = -EFAULT;
1797 break;
1798 }
1799 status = 0;
1800 }
1801 break;
1802 default:
1803 status = -EINVAL;
1804 }
1805
1806 return status;
1807}
1808
Jordan Crousef7370f82012-04-18 09:31:07 -06001809static int adreno_setproperty(struct kgsl_device *device,
1810 enum kgsl_property_type type,
1811 void *value,
1812 unsigned int sizebytes)
1813{
1814 int status = -EINVAL;
1815
1816 switch (type) {
1817 case KGSL_PROP_PWRCTRL: {
1818 unsigned int enable;
1819 struct kgsl_device_platform_data *pdata =
1820 kgsl_device_get_drvdata(device);
1821
1822 if (sizebytes != sizeof(enable))
1823 break;
1824
1825 if (copy_from_user(&enable, (void __user *) value,
1826 sizeof(enable))) {
1827 status = -EFAULT;
1828 break;
1829 }
1830
1831 if (enable) {
1832 if (pdata->nap_allowed)
1833 device->pwrctrl.nap_allowed = true;
1834
1835 kgsl_pwrscale_enable(device);
1836 } else {
1837 device->pwrctrl.nap_allowed = false;
1838 kgsl_pwrscale_disable(device);
1839 }
1840
1841 status = 0;
1842 }
1843 break;
1844 default:
1845 break;
1846 }
1847
1848 return status;
1849}
1850
Lynus Vaz06a9a902011-10-04 19:25:33 +05301851static inline void adreno_poke(struct kgsl_device *device)
1852{
1853 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1854 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1855}
1856
Jordan Crousea29a2e02012-08-14 09:09:23 -06001857static int adreno_ringbuffer_drain(struct kgsl_device *device,
1858 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001859{
1860 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1861 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001862 unsigned long wait;
1863 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1864
1865 if (!(rb->flags & KGSL_FLAGS_STARTED))
1866 return 0;
1867
1868 /*
1869 * The first time into the loop, wait for 100 msecs and kick wptr again
1870 * to ensure that the hardware has updated correctly. After that, kick
1871 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1872 * expires
1873 */
1874
1875 wait = jiffies + msecs_to_jiffies(100);
1876
1877 adreno_poke(device);
1878
1879 do {
1880 if (time_after(jiffies, wait)) {
1881 adreno_poke(device);
1882
1883 /* Check to see if the core is hung */
1884 if (adreno_hang_detect(device, regs))
1885 return -ETIMEDOUT;
1886
1887 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1888 }
1889 GSL_RB_GET_READPTR(rb, &rb->rptr);
1890
1891 if (time_after(jiffies, timeout)) {
1892 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1893 rb->rptr, rb->wptr);
1894 return -ETIMEDOUT;
1895 }
1896 } while (rb->rptr != rb->wptr);
1897
1898 return 0;
1899}
1900
1901/* Caller must hold the device mutex. */
1902int adreno_idle(struct kgsl_device *device)
1903{
1904 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001905 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301906 unsigned long wait_time;
1907 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001908 unsigned int prev_reg_val[hang_detect_regs_count];
1909
1910 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001911
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001912 kgsl_cffdump_regpoll(device->id,
1913 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001914 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001915
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001916retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001917 /* First, wait for the ringbuffer to drain */
1918 if (adreno_ringbuffer_drain(device, prev_reg_val))
1919 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001920
1921 /* now, wait for the GPU to finish its operations */
Jordan Crousea29a2e02012-08-14 09:09:23 -06001922 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
1923 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1924
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001925 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001926 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1927 &rbbm_status);
1928 if (adreno_is_a2xx(adreno_dev)) {
1929 if (rbbm_status == 0x110)
1930 return 0;
1931 } else {
1932 if (!(rbbm_status & 0x80000000))
1933 return 0;
1934 }
Tarun Karra3335f142012-06-19 14:11:48 -07001935
1936 /* Dont wait for timeout, detect hang faster.
1937 */
1938 if (time_after(jiffies, wait_time_part)) {
1939 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001940 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001941 if ((adreno_hang_detect(device, prev_reg_val)))
1942 goto err;
1943 }
1944
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001945 }
1946
1947err:
1948 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001949 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1950 !adreno_dump_and_recover(device)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001951 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001952 goto retry;
1953 }
1954 return -ETIMEDOUT;
1955}
1956
1957static unsigned int adreno_isidle(struct kgsl_device *device)
1958{
1959 int status = false;
1960 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1961 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1962 unsigned int rbbm_status;
1963
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001964 WARN_ON(device->state == KGSL_STATE_INIT);
1965 /* If the device isn't active, don't force it on. */
1966 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001967 /* Is the ring buffer is empty? */
1968 GSL_RB_GET_READPTR(rb, &rb->rptr);
1969 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1970 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001971 adreno_regread(device,
1972 adreno_dev->gpudev->reg_rbbm_status,
1973 &rbbm_status);
1974
1975 if (adreno_is_a2xx(adreno_dev)) {
1976 if (rbbm_status == 0x110)
1977 status = true;
1978 } else {
1979 if (!(rbbm_status & 0x80000000))
1980 status = true;
1981 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001982 }
1983 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001984 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001985 }
1986 return status;
1987}
1988
1989/* Caller must hold the device mutex. */
1990static int adreno_suspend_context(struct kgsl_device *device)
1991{
1992 int status = 0;
1993 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1994
1995 /* switch to NULL ctxt */
1996 if (adreno_dev->drawctxt_active != NULL) {
1997 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001998 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001999 }
2000
2001 return status;
2002}
2003
Jordan Crouse233b2092012-04-18 09:31:09 -06002004/* Find a memory structure attached to an adreno context */
2005
2006struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2007 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2008{
2009 struct kgsl_context *context;
2010 struct adreno_context *adreno_context = NULL;
2011 int next = 0;
2012
2013 while (1) {
2014 context = idr_get_next(&device->context_idr, &next);
2015 if (context == NULL)
2016 break;
2017
2018 adreno_context = (struct adreno_context *)context->devctxt;
2019
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002020 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2021 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002022 struct kgsl_memdesc *desc;
2023
2024 desc = &adreno_context->gpustate;
2025 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2026 return desc;
2027
2028 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2029 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2030 return desc;
2031 }
2032 next = next + 1;
2033 }
2034
2035 return NULL;
2036}
2037
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002038struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002039 unsigned int pt_base,
2040 unsigned int gpuaddr,
2041 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002042{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002043 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002044 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2045 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2046
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002047 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2048 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002049
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002050 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2051 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002052
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002053 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2054 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002055
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002056 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2057 size))
2058 return &device->mmu.setstate_memory;
2059
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002060 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002061
2062 if (entry)
2063 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002064
Jordan Crouse233b2092012-04-18 09:31:09 -06002065 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002066}
2067
2068uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2069 unsigned int gpuaddr, unsigned int size)
2070{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002071 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002072
2073 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2074
2075 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002076}
2077
2078void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2079 unsigned int *value)
2080{
2081 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002082 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2083 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002084
2085 if (!in_interrupt())
2086 kgsl_pre_hwaccess(device);
2087
2088 /*ensure this read finishes before the next one.
2089 * i.e. act like normal readl() */
2090 *value = __raw_readl(reg);
2091 rmb();
2092}
2093
2094void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2095 unsigned int value)
2096{
2097 unsigned int *reg;
2098
Jordan Crouse7501d452012-04-19 08:58:44 -06002099 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002100
2101 if (!in_interrupt())
2102 kgsl_pre_hwaccess(device);
2103
2104 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002105 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002106
2107 /*ensure previous writes post before this one,
2108 * i.e. act like normal writel() */
2109 wmb();
2110 __raw_writel(value, reg);
2111}
2112
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002113static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2114{
2115 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002116 if (k_ctxt != NULL) {
2117 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002118 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2119 context_id = KGSL_CONTEXT_INVALID;
2120 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2121 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002122 }
2123
2124 return context_id;
2125}
2126
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002127static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002128 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002129{
2130 int status;
2131 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002132 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002133 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002134
2135 mutex_lock(&device->mutex);
2136 context_id = _get_context_id(context);
2137 /*
2138 * If the context ID is invalid, we are in a race with
2139 * the context being destroyed by userspace so bail.
2140 */
2141 if (context_id == KGSL_CONTEXT_INVALID) {
2142 KGSL_DRV_WARN(device, "context was detached");
2143 status = -EINVAL;
2144 goto unlock;
2145 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002146
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002147 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002148 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002149 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002150 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002151 mb();
2152
2153 if (enableflag) {
2154 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002155 KGSL_MEMSTORE_OFFSET(context_id,
2156 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002157 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002158 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002159 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002160 KGSL_MEMSTORE_OFFSET(context_id,
2161 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002162 wmb();
2163 }
2164 } else {
2165 unsigned int cmds[2];
2166 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002167 KGSL_MEMSTORE_OFFSET(context_id,
2168 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002169 enableflag = 1;
2170 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002171 KGSL_MEMSTORE_OFFSET(context_id,
2172 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002173 wmb();
2174 /* submit a dummy packet so that even if all
2175 * commands upto timestamp get executed we will still
2176 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002177 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002179
2180 if (adreno_dev->drawctxt_active)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002181 adreno_ringbuffer_issuecmds_intr(device,
2182 context, &cmds[0], 2);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002183 else
2184 /* We would never call this function if there
2185 * was no active contexts running */
2186 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002187 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002188 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002189unlock:
2190 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002191
2192 return status;
2193}
2194
2195/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002196 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197 placing a process in wait q. For conditional interrupts we expect the
2198 process to already be in its wait q when its exit condition checking
2199 function is called.
2200*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002201#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002202({ \
2203 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002204 if (io) \
2205 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2206 else \
2207 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208 __ret; \
2209})
2210
Tarun Karra3335f142012-06-19 14:11:48 -07002211
2212
2213unsigned int adreno_hang_detect(struct kgsl_device *device,
2214 unsigned int *prev_reg_val)
2215{
2216 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2217 unsigned int curr_reg_val[hang_detect_regs_count];
2218 unsigned int hang_detected = 1;
2219 unsigned int i;
2220
2221 if (!adreno_dev->fast_hang_detect)
2222 return 0;
2223
2224 for (i = 0; i < hang_detect_regs_count; i++) {
2225 adreno_regread(device, hang_detect_regs[i],
2226 &curr_reg_val[i]);
2227 if (curr_reg_val[i] != prev_reg_val[i]) {
2228 prev_reg_val[i] = curr_reg_val[i];
2229 hang_detected = 0;
2230 }
2231 }
2232
2233 return hang_detected;
2234}
2235
2236
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002237/* MUST be called with the device mutex held */
2238static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002239 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002240 unsigned int timestamp,
2241 unsigned int msecs)
2242{
2243 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06002244 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06002245 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002246 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06002247 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07002248 int retries = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002249 unsigned int ts_issued;
2250 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07002251 unsigned int time_elapsed = 0;
2252 unsigned int prev_reg_val[hang_detect_regs_count];
Jordan Crouse21f75a02012-08-09 15:08:59 -06002253 unsigned int wait;
Tarun Karra3335f142012-06-19 14:11:48 -07002254
2255 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002256
2257 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002258
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302259 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07002260 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302261 msecs = adreno_dev->wait_timeout;
2262
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002263 if (timestamp_cmp(timestamp, ts_issued) > 0) {
2264 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
2265 "last issued ts <%d:0x%x>\n",
2266 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002267 status = -EINVAL;
2268 goto done;
2269 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002270
Jordan Crouse21f75a02012-08-09 15:08:59 -06002271 /*
2272 * Make the first timeout interval 100 msecs and then try to kick the
2273 * wptr again. This helps to ensure the wptr is updated properly. If
2274 * the requested timeout is less than 100 msecs, then wait 20msecs which
2275 * is the minimum amount of time we can safely wait at 100HZ
Lynus Vaz06a9a902011-10-04 19:25:33 +05302276 */
Jordan Crouse21f75a02012-08-09 15:08:59 -06002277
2278 if (msecs == 0 || msecs >= 100)
2279 wait = 100;
2280 else
2281 wait = 20;
2282
Tarun Karra3335f142012-06-19 14:11:48 -07002283 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002284 /*
2285 * If the context ID is invalid, we are in a race with
2286 * the context being destroyed by userspace so bail.
2287 */
2288 if (context_id == KGSL_CONTEXT_INVALID) {
2289 KGSL_DRV_WARN(device, "context was detached");
2290 status = -EINVAL;
2291 goto done;
2292 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002293 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07002294 /* if the timestamp happens while we're not
2295 * waiting, there's a chance that an interrupt
2296 * will not be generated and thus the timestamp
2297 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05302298 */
Jeremy Gebben63904832012-02-07 16:10:55 -07002299 queue_work(device->work_queue, &device->ts_expired_ws);
2300 status = 0;
2301 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002302 }
Jeremy Gebben63904832012-02-07 16:10:55 -07002303 adreno_poke(device);
2304 io_cnt = (io_cnt + 1) % 100;
2305 if (io_cnt <
2306 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2307 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002308
2309 if ((retries > 0) &&
2310 (adreno_hang_detect(device, prev_reg_val)))
2311 goto hang_dump;
2312
Jeremy Gebben63904832012-02-07 16:10:55 -07002313 mutex_unlock(&device->mutex);
2314 /* We need to make sure that the process is
2315 * placed in wait-q before its condition is called
2316 */
2317 status = kgsl_wait_event_interruptible_timeout(
2318 device->wait_queue,
2319 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002320 context, timestamp),
Jordan Crouse21f75a02012-08-09 15:08:59 -06002321 msecs_to_jiffies(wait), io);
2322
Jeremy Gebben63904832012-02-07 16:10:55 -07002323 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002324
Jeremy Gebben63904832012-02-07 16:10:55 -07002325 if (status > 0) {
2326 /*completed before the wait finished */
2327 status = 0;
2328 goto done;
2329 } else if (status < 0) {
2330 /*an error occurred*/
2331 goto done;
2332 }
2333 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07002334
Jordan Crouse21f75a02012-08-09 15:08:59 -06002335 time_elapsed += wait;
2336 wait = KGSL_TIMEOUT_PART;
2337
Tarun Karra3335f142012-06-19 14:11:48 -07002338 retries++;
2339
Jordan Crouse21f75a02012-08-09 15:08:59 -06002340 } while (!msecs || time_elapsed < msecs);
Tarun Karra3335f142012-06-19 14:11:48 -07002341
2342hang_dump:
Shubhraprakash Das54396e52012-03-10 13:24:54 -07002343 /*
2344 * Check if timestamp has retired here because we may have hit
2345 * recovery which can take some time and cause waiting threads
2346 * to timeout
2347 */
2348 if (kgsl_check_timestamp(device, context, timestamp))
2349 goto done;
Jeremy Gebben63904832012-02-07 16:10:55 -07002350 status = -ETIMEDOUT;
2351 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002352 "Device hang detected while waiting for timestamp: "
2353 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2354 "wptr: 0x%x\n",
2355 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002356 adreno_dev->ringbuffer.wptr);
2357 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002358 /* The timestamp that this process wanted
2359 * to wait on may be invalid or expired now
2360 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07002361 status = 0;
2362 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002363done:
2364 return (int)status;
2365}
2366
2367static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002368 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002369{
2370 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002371 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002372
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002373 /*
2374 * If the context ID is invalid, we are in a race with
2375 * the context being destroyed by userspace so bail.
2376 */
2377 if (context_id == KGSL_CONTEXT_INVALID) {
2378 KGSL_DRV_WARN(device, "context was detached");
2379 return timestamp;
2380 }
Jordan Crousec659f382012-04-16 11:10:41 -06002381 switch (type) {
2382 case KGSL_TIMESTAMP_QUEUED: {
2383 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2384 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2385
2386 timestamp = rb->timestamp[context_id];
2387 break;
2388 }
2389 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002390 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002391 break;
2392 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002393 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002394 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2395 break;
2396 }
2397
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002398 rmb();
2399
2400 return timestamp;
2401}
2402
2403static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2404 unsigned int cmd, void *data)
2405{
2406 int result = 0;
2407 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2408 struct kgsl_context *context;
2409
2410 switch (cmd) {
2411 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2412 binbase = data;
2413
2414 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2415 if (context) {
2416 adreno_drawctxt_set_bin_base_offset(
2417 dev_priv->device, context, binbase->offset);
2418 } else {
2419 result = -EINVAL;
2420 KGSL_DRV_ERR(dev_priv->device,
2421 "invalid drawctxt drawctxt_id %d "
2422 "device_id=%d\n",
2423 binbase->drawctxt_id, dev_priv->device->id);
2424 }
2425 break;
2426
2427 default:
2428 KGSL_DRV_INFO(dev_priv->device,
2429 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002430 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002431 break;
2432 }
2433 return result;
2434
2435}
2436
2437static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2438{
2439 gpu_freq /= 1000000;
2440 return ticks / gpu_freq;
2441}
2442
2443static void adreno_power_stats(struct kgsl_device *device,
2444 struct kgsl_power_stats *stats)
2445{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002446 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002447 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002448 unsigned int cycles;
2449
2450 /* Get the busy cycles counted since the counter was last reset */
2451 /* Calling this function also resets and restarts the counter */
2452
2453 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002454
2455 /* In order to calculate idle you have to have run the algorithm *
2456 * at least once to get a start time. */
2457 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002458 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002459 stats->total_time = tmp - pwr->time;
2460 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002461 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002462 pwrlevels[device->pwrctrl.active_pwrlevel].
2463 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002464 } else {
2465 stats->total_time = 0;
2466 stats->busy_time = 0;
2467 pwr->time = ktime_to_us(ktime_get());
2468 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002469}
2470
2471void adreno_irqctrl(struct kgsl_device *device, int state)
2472{
Jordan Crousea78c9172011-07-11 13:14:09 -06002473 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2474 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002475}
2476
Jordan Croused6535882012-06-20 08:22:16 -06002477static unsigned int adreno_gpuid(struct kgsl_device *device,
2478 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002479{
2480 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2481
Jordan Croused6535882012-06-20 08:22:16 -06002482 /* Some applications need to know the chip ID too, so pass
2483 * that as a parameter */
2484
2485 if (chipid != NULL)
2486 *chipid = adreno_dev->chip_id;
2487
Jordan Crousea0758f22011-12-07 11:19:22 -07002488 /* Standard KGSL gpuid format:
2489 * top word is 0x0002 for 2D or 0x0003 for 3D
2490 * Bottom word is core specific identifer
2491 */
2492
2493 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2494}
2495
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002496static const struct kgsl_functable adreno_functable = {
2497 /* Mandatory functions */
2498 .regread = adreno_regread,
2499 .regwrite = adreno_regwrite,
2500 .idle = adreno_idle,
2501 .isidle = adreno_isidle,
2502 .suspend_context = adreno_suspend_context,
2503 .start = adreno_start,
2504 .stop = adreno_stop,
2505 .getproperty = adreno_getproperty,
2506 .waittimestamp = adreno_waittimestamp,
2507 .readtimestamp = adreno_readtimestamp,
2508 .issueibcmds = adreno_ringbuffer_issueibcmds,
2509 .ioctl = adreno_ioctl,
2510 .setup_pt = adreno_setup_pt,
2511 .cleanup_pt = adreno_cleanup_pt,
2512 .power_stats = adreno_power_stats,
2513 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002514 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002515 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002516 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002517 /* Optional functions */
2518 .setstate = adreno_setstate,
2519 .drawctxt_create = adreno_drawctxt_create,
2520 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002521 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002522 .postmortem_dump = adreno_dump,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002523};
2524
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002525static struct platform_driver adreno_platform_driver = {
2526 .probe = adreno_probe,
2527 .remove = __devexit_p(adreno_remove),
2528 .suspend = kgsl_suspend_driver,
2529 .resume = kgsl_resume_driver,
2530 .id_table = adreno_id_table,
2531 .driver = {
2532 .owner = THIS_MODULE,
2533 .name = DEVICE_3D_NAME,
2534 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002535 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002536 }
2537};
2538
2539static int __init kgsl_3d_init(void)
2540{
2541 return platform_driver_register(&adreno_platform_driver);
2542}
2543
2544static void __exit kgsl_3d_exit(void)
2545{
2546 platform_driver_unregister(&adreno_platform_driver);
2547}
2548
2549module_init(kgsl_3d_init);
2550module_exit(kgsl_3d_exit);
2551
2552MODULE_DESCRIPTION("3D Graphics driver");
2553MODULE_VERSION("1.2");
2554MODULE_LICENSE("GPL v2");
2555MODULE_ALIAS("platform:kgsl_3d");