blob: 7119e03a4afd33a1fa05644c718b62ba1e829c0e [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 }
339 /* invalidate all base pointers */
340 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
341 *cmds++ = 0x7fff;
342
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600343 cmds += __adreno_add_idle_indirect_cmds(cmds,
344 device->mmu.setstate_memory.gpuaddr +
345 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600346 }
347 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
348 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700349 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600350 */
351 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700352 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
353 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700354
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600355 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700356 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
357 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700358 KGSL_IOMMU_CTX_TLBIALL);
359 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600360
361 cmds += __adreno_add_idle_indirect_cmds(cmds,
362 device->mmu.setstate_memory.gpuaddr +
363 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
364
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600365 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700366 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
367 KGSL_IOMMU_CONTEXT_USER,
368 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700369 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600370 device->mmu.setstate_memory.gpuaddr +
371 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
372 }
373 }
374
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600375 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600376 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700377 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
378 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600379 device->mmu.setstate_memory.gpuaddr +
380 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
381 else
382 cmds += adreno_add_bank_change_cmds(cmds,
383 KGSL_IOMMU_CONTEXT_PRIV,
384 device->mmu.setstate_memory.gpuaddr +
385 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
386
387 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600388 if (sizedwords) {
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600389 /*
390 * add an interrupt at the end of commands so that the smmu
391 * disable clock off function will get called
392 */
393 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
394 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
395 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600396 /* This returns the per context timestamp but we need to
397 * use the global timestamp for iommu clock disablement */
398 adreno_ringbuffer_issuecmds(device, adreno_ctx,
399 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600400 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600401 kgsl_mmu_disable_clk_on_ts(&device->mmu,
402 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600403 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600404}
405
406static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600407 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600408 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409{
410 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
411 unsigned int link[32];
412 unsigned int *cmds = &link[0];
413 int sizedwords = 0;
414 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600415 struct kgsl_context *context;
416 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600418 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530419 * Fix target freeze issue by adding TLB flush for each submit
420 * on A20X based targets.
421 */
422 if (adreno_is_a20x(adreno_dev))
423 flags |= KGSL_MMUFLAGS_TLBFLUSH;
424 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600425 * If possible, then set the state via the command stream to avoid
426 * a CPU idle. Otherwise, use the default setstate which uses register
427 * writes For CFF dump we must idle and use the registers so that it is
428 * easier to filter out the mmu accesses from the dump
429 */
430 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600431 context = idr_find(&device->context_idr, context_id);
432 adreno_ctx = context->devctxt;
433
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
435 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600436 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437 *cmds++ = 0x00000000;
438
439 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600440 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700441 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600442 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 sizedwords += 4;
444 }
445
446 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
447 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600448 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449 1);
450 *cmds++ = 0x00000000;
451 sizedwords += 2;
452 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600453 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 *cmds++ = mh_mmu_invalidate;
455 sizedwords += 2;
456 }
457
458 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600459 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460 /* HW workaround: to resolve MMU page fault interrupts
461 * caused by the VGT.It prevents the CP PFP from filling
462 * the VGT DMA request fifo too early,thereby ensuring
463 * that the VGT will not fetch vertex/bin data until
464 * after the page table base register has been updated.
465 *
466 * Two null DRAW_INDX_BIN packets are inserted right
467 * after the page table base update, followed by a
468 * wait for idle. The null packets will fill up the
469 * VGT DMA request fifo and prevent any further
470 * vertex/bin updates from occurring until the wait
471 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600472 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 *cmds++ = (0x4 << 16) |
474 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
475 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600476 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600477 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600478 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479 *cmds++ = 0; /* viz query info */
480 *cmds++ = 0x0003C004; /* draw indicator */
481 *cmds++ = 0; /* bin base */
482 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600483 *cmds++ =
484 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600486 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 *cmds++ = 0; /* viz query info */
488 *cmds++ = 0x0003C004; /* draw indicator */
489 *cmds++ = 0; /* bin base */
490 *cmds++ = 3; /* bin size */
491 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600492 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600494 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 *cmds++ = 0x00000000;
496 sizedwords += 21;
497 }
498
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600499
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600501 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 *cmds++ = 0x7fff; /* invalidate all base pointers */
503 sizedwords += 2;
504 }
505
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600506 adreno_ringbuffer_issuecmds(device, adreno_ctx,
507 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600509 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600510 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600511 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512}
513
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600514static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600515 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600516 uint32_t flags)
517{
518 /* call the mmu specific handler */
519 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600520 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600521 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600522 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600523}
524
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700526a3xx_getchipid(struct kgsl_device *device)
527{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600528 struct kgsl_device_platform_data *pdata =
529 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700530
Jordan Crouse54154c62012-03-27 16:33:26 -0600531 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600532 * All current A3XX chipids are detected at the SOC level. Leave this
533 * function here to support any future GPUs that have working
534 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600535 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700536
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600537 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700538}
539
540static unsigned int
541a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542{
543 unsigned int chipid = 0;
544 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600545 struct kgsl_device_platform_data *pdata =
546 kgsl_device_get_drvdata(device);
547
548 /* If the chip id is set at the platform level, then just use that */
549
550 if (pdata->chipid != 0)
551 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552
553 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
554 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
555 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
556
557 /*
558 * adreno 22x gpus are indicated by coreid 2,
559 * but REG_RBBM_PERIPHID1 always contains 0 for this field
560 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600561 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562 chipid = 2 << 24;
563 else
564 chipid = (coreid & 0xF) << 24;
565
566 chipid |= ((majorid >> 4) & 0xF) << 16;
567
568 minorid = ((revid >> 0) & 0xFF);
569
570 patchid = ((revid >> 16) & 0xFF);
571
572 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530573 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574 if (cpu_is_qsd8x50())
575 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530576 else if (cpu_is_msm8625() && minorid == 0)
577 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578
579 chipid |= (minorid << 8) | patchid;
580
581 return chipid;
582}
583
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700584static unsigned int
585adreno_getchipid(struct kgsl_device *device)
586{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600587 struct kgsl_device_platform_data *pdata =
588 kgsl_device_get_drvdata(device);
589
590 /*
591 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
592 * an A2XX processor
593 */
594
595 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700596 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600597 else
598 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700599}
600
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601static inline bool _rev_match(unsigned int id, unsigned int entry)
602{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600603 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605
606static void
607adreno_identify_gpu(struct adreno_device *adreno_dev)
608{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600609 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610
611 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
612
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600613 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
614 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
615 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
616 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617
Jordan Crouse505df9c2011-07-28 08:37:59 -0600618 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
619 if (core == adreno_gpulist[i].core &&
620 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600621 _rev_match(minor, adreno_gpulist[i].minor) &&
622 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624 }
625
Jordan Crouse505df9c2011-07-28 08:37:59 -0600626 if (i == ARRAY_SIZE(adreno_gpulist)) {
627 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
628 return;
629 }
630
631 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
632 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
633 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
634 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700635 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
636 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700637 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600638 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639}
640
Lokesh Batra805e1e12012-08-03 08:34:06 -0600641static struct platform_device_id adreno_id_table[] = {
642 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
643 {},
644};
645
646MODULE_DEVICE_TABLE(platform, adreno_id_table);
647
648static struct of_device_id adreno_match_table[] = {
649 { .compatible = "qcom,kgsl-3d0", },
650 {}
651};
652
653static inline int adreno_of_read_property(struct device_node *node,
654 const char *prop, unsigned int *ptr)
655{
656 int ret = of_property_read_u32(node, prop, ptr);
657 if (ret)
658 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
659 return ret;
660}
661
662static struct device_node *adreno_of_find_subnode(struct device_node *parent,
663 const char *name)
664{
665 struct device_node *child;
666
667 for_each_child_of_node(parent, child) {
668 if (of_device_is_compatible(child, name))
669 return child;
670 }
671
672 return NULL;
673}
674
675static int adreno_of_get_pwrlevels(struct device_node *parent,
676 struct kgsl_device_platform_data *pdata)
677{
678 struct device_node *node, *child;
679 int ret = -EINVAL;
680
681 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
682
683 if (node == NULL) {
684 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
685 return -EINVAL;
686 }
687
688 pdata->num_levels = 0;
689
690 for_each_child_of_node(node, child) {
691 unsigned int index;
692 struct kgsl_pwrlevel *level;
693
694 if (adreno_of_read_property(child, "reg", &index))
695 goto done;
696
697 if (index >= KGSL_MAX_PWRLEVELS) {
698 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
699 index);
700 continue;
701 }
702
703 if (index >= pdata->num_levels)
704 pdata->num_levels = index + 1;
705
706 level = &pdata->pwrlevel[index];
707
708 if (adreno_of_read_property(child, "qcom,gpu-freq",
709 &level->gpu_freq))
710 goto done;
711
712 if (adreno_of_read_property(child, "qcom,bus-freq",
713 &level->bus_freq))
714 goto done;
715
716 if (adreno_of_read_property(child, "qcom,io-fraction",
717 &level->io_fraction))
718 level->io_fraction = 0;
719 }
720
721 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
722 &pdata->init_level))
723 pdata->init_level = 1;
724
725 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
726 KGSL_CORE_ERR("Initial power level out of range\n");
727 pdata->init_level = 1;
728 }
729
730 ret = 0;
731done:
732 return ret;
733
734}
735static void adreno_of_free_bus_scale_info(struct msm_bus_scale_pdata *pdata)
736{
737 int i;
738
739 if (pdata == NULL)
740 return;
741
742 for (i = 0; pdata->usecase && i < pdata->num_usecases; i++)
743 kfree(pdata->usecase[i].vectors);
744
745 kfree(pdata->usecase);
746 kfree(pdata);
747}
748
749struct msm_bus_scale_pdata *adreno_of_get_bus_scale(struct device_node *node)
750{
751 static int bus_vectors_src[3] = {MSM_BUS_MASTER_GRAPHICS_3D,
752 MSM_BUS_MASTER_GRAPHICS_3D_PORT1, MSM_BUS_MASTER_V_OCMEM_GFX3D};
753 static int bus_vectors_dst[2] = {MSM_BUS_SLAVE_EBI_CH0,
754 MSM_BUS_SLAVE_OCMEM};
755 const unsigned int *vectors;
756 struct msm_bus_scale_pdata *pdata;
757 int i, j, len, num_paths;
758 int ret = -EINVAL;
759
760 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
761
762 if (!pdata) {
763 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
764 return ERR_PTR(-ENOMEM);
765 }
766
767 if (adreno_of_read_property(node, "qcom,grp3d-num-bus-scale-usecases",
768 &pdata->num_usecases)) {
769 pdata->num_usecases = 0;
770 goto err;
771 }
772
773 pdata->usecase = kzalloc(pdata->num_usecases *
774 sizeof(struct msm_bus_paths), GFP_KERNEL);
775
776 if (pdata->usecase == NULL) {
777 KGSL_CORE_ERR("kzalloc (%d) failed\n",
778 pdata->num_usecases * sizeof(struct msm_bus_paths));
779 ret = -ENOMEM;
780 goto err;
781 }
782
783 if (adreno_of_read_property(node, "qcom,grp3d-num-vectors-per-usecase",
784 &num_paths))
785 goto err;
786
787 vectors = of_get_property(node, "qcom,grp3d-vectors", &len);
788
789 if (len != pdata->num_usecases * num_paths *
790 sizeof(struct msm_bus_vectors)) {
791 KGSL_CORE_ERR("Invalid size for the bus scale vectors\n");
792 goto err;
793 }
794
795 for (i = 0; i < pdata->num_usecases; i++) {
796 pdata->usecase[i].num_paths = num_paths;
797 pdata->usecase[i].vectors = kzalloc(num_paths *
798 sizeof(struct msm_bus_vectors),
799 GFP_KERNEL);
800 if (!pdata->usecase[i].vectors) {
801 KGSL_CORE_ERR("kzalloc(%d) failed\n",
802 num_paths * sizeof(struct msm_bus_vectors));
803 ret = -ENOMEM;
804 goto err;
805 }
806 for (j = 0; j < num_paths; j++) {
807 int index = (i * num_paths + j) * 4;
808 pdata->usecase[i].vectors[j].src =
809 bus_vectors_src[be32_to_cpu(vectors[index])];
810 pdata->usecase[i].vectors[j].dst =
811 bus_vectors_dst[
812 be32_to_cpu(vectors[index + 1])];
813 pdata->usecase[i].vectors[j].ab =
814 be32_to_cpu(vectors[index + 2]);
815 pdata->usecase[i].vectors[j].ib =
816 KGSL_CONVERT_TO_MBPS(
817 be32_to_cpu(vectors[index + 3]));
818 }
819 }
820
821 pdata->name = "grp3d";
822
823 return pdata;
824
825err:
826 adreno_of_free_bus_scale_info(pdata);
827
828 return ERR_PTR(ret);
829}
830
831static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
832{
833 struct device_node *node, *child;
834 struct msm_dcvs_core_info *info = NULL;
835 int count = 0;
836 int ret = -EINVAL;
837
838 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
839 if (node == NULL)
840 return ERR_PTR(-EINVAL);
841
842 info = kzalloc(sizeof(*info), GFP_KERNEL);
843
844 if (info == NULL) {
845 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
846 ret = -ENOMEM;
847 goto err;
848 }
849
850 for_each_child_of_node(node, child)
851 count++;
852
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700853 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600854
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700855 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600856 sizeof(struct msm_dcvs_freq_entry),
857 GFP_KERNEL);
858
859 if (info->freq_tbl == NULL) {
860 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700861 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600862 sizeof(struct msm_dcvs_freq_entry));
863 ret = -ENOMEM;
864 goto err;
865 }
866
867 for_each_child_of_node(node, child) {
868 unsigned int index;
869
870 if (adreno_of_read_property(child, "reg", &index))
871 goto err;
872
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700873 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600874 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
875 index);
876 continue;
877 }
878
879 if (adreno_of_read_property(child, "qcom,freq",
880 &info->freq_tbl[index].freq))
881 goto err;
882
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700883 if (adreno_of_read_property(child, "qcom,voltage",
884 &info->freq_tbl[index].voltage))
885 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600886
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700887 if (adreno_of_read_property(child, "qcom,is_trans_level",
888 &info->freq_tbl[index].is_trans_level))
889 info->freq_tbl[index].is_trans_level = 0;
890
891 if (adreno_of_read_property(child, "qcom,active-energy-offset",
892 &info->freq_tbl[index].active_energy_offset))
893 info->freq_tbl[index].active_energy_offset = 0;
894
895 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
896 &info->freq_tbl[index].leakage_energy_offset))
897 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600898 }
899
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700900 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
901 goto err;
902
903 info->sensors = kzalloc(info->num_cores *
904 sizeof(int),
905 GFP_KERNEL);
906
907 for (count = 0; count < info->num_cores; count++) {
908 if (adreno_of_read_property(node, "qcom,sensors",
909 &(info->sensors[count])))
910 goto err;
911 }
912
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700913 if (adreno_of_read_property(node, "qcom,core-core-type",
914 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600915 goto err;
916
917 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
918 &info->algo_param.disable_pc_threshold))
919 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700920 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
921 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600922 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700923 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
924 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600925 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600926 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
927 &info->algo_param.em_max_util_pct))
928 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700929 if (adreno_of_read_property(node, "qcom,algo-group-id",
930 &info->algo_param.group_id))
931 goto err;
932 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
933 &info->algo_param.max_freq_chg_time_us))
934 goto err;
935 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
936 &info->algo_param.slack_mode_dynamic))
937 goto err;
938 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
939 &info->algo_param.slack_weight_thresh_pct))
940 goto err;
941 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
942 &info->algo_param.slack_time_min_us))
943 goto err;
944 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
945 &info->algo_param.slack_time_max_us))
946 goto err;
947 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
948 &info->algo_param.ss_win_size_min_us))
949 goto err;
950 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
951 &info->algo_param.ss_win_size_max_us))
952 goto err;
953 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
954 &info->algo_param.ss_util_pct))
955 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600956 if (adreno_of_read_property(node, "qcom,algo-ss-iobusy-conv",
957 &info->algo_param.ss_iobusy_conv))
958 goto err;
959
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700960 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
961 &info->energy_coeffs.active_coeff_a))
962 goto err;
963 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
964 &info->energy_coeffs.active_coeff_b))
965 goto err;
966 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
967 &info->energy_coeffs.active_coeff_c))
968 goto err;
969 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
970 &info->energy_coeffs.leakage_coeff_a))
971 goto err;
972 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
973 &info->energy_coeffs.leakage_coeff_b))
974 goto err;
975 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
976 &info->energy_coeffs.leakage_coeff_c))
977 goto err;
978 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
979 &info->energy_coeffs.leakage_coeff_d))
980 goto err;
981
982 if (adreno_of_read_property(node, "qcom,power-current-temp",
983 &info->power_param.current_temp))
984 goto err;
985
Lokesh Batra805e1e12012-08-03 08:34:06 -0600986 return info;
987
988err:
989 if (info)
990 kfree(info->freq_tbl);
991
992 kfree(info);
993
994 return ERR_PTR(ret);
995}
996
997static int adreno_of_get_iommu(struct device_node *parent,
998 struct kgsl_device_platform_data *pdata)
999{
1000 struct device_node *node, *child;
1001 struct kgsl_device_iommu_data *data = NULL;
1002 struct kgsl_iommu_ctx *ctxs = NULL;
1003 u32 reg_val[2];
1004 int ctx_index = 0;
1005
1006 node = of_parse_phandle(parent, "iommu", 0);
1007 if (node == NULL)
1008 return -EINVAL;
1009
1010 data = kzalloc(sizeof(*data), GFP_KERNEL);
1011 if (data == NULL) {
1012 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
1013 goto err;
1014 }
1015
1016 if (of_property_read_u32_array(node, "reg", reg_val, 2))
1017 goto err;
1018
1019 data->physstart = reg_val[0];
1020 data->physend = data->physstart + reg_val[1] - 1;
1021
1022 data->iommu_ctx_count = 0;
1023
1024 for_each_child_of_node(node, child)
1025 data->iommu_ctx_count++;
1026
1027 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
1028 GFP_KERNEL);
1029
1030 if (ctxs == NULL) {
1031 KGSL_CORE_ERR("kzalloc(%d) failed\n",
1032 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
1033 goto err;
1034 }
1035
1036 for_each_child_of_node(node, child) {
1037 int ret = of_property_read_string(child, "label",
1038 &ctxs[ctx_index].iommu_ctx_name);
1039
1040 if (ret) {
1041 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
1042 goto err;
1043 }
1044
1045 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
1046 &ctxs[ctx_index].ctx_id))
1047 goto err;
1048
1049 ctx_index++;
1050 }
1051
1052 data->iommu_ctxs = ctxs;
1053
1054 pdata->iommu_data = data;
1055 pdata->iommu_count = 1;
1056
1057 return 0;
1058
1059err:
1060 kfree(ctxs);
1061 kfree(data);
1062
1063 return -EINVAL;
1064}
1065
1066static int adreno_of_get_pdata(struct platform_device *pdev)
1067{
1068 struct kgsl_device_platform_data *pdata = NULL;
1069 struct kgsl_device *device;
1070 int ret = -EINVAL;
1071
1072 pdev->id_entry = adreno_id_table;
1073
1074 pdata = pdev->dev.platform_data;
1075 if (pdata)
1076 return 0;
1077
1078 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1079 KGSL_CORE_ERR("Unable to read 'label'\n");
1080 goto err;
1081 }
1082
1083 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1084 goto err;
1085
1086 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1087 if (pdata == NULL) {
1088 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1089 ret = -ENOMEM;
1090 goto err;
1091 }
1092
1093 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1094 &pdata->chipid))
1095 goto err;
1096
1097 /* pwrlevel Data */
1098 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1099 if (ret)
1100 goto err;
1101
1102 /* Default value is 83, if not found in DT */
1103 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1104 &pdata->idle_timeout))
1105 pdata->idle_timeout = 83;
1106
1107 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1108 &pdata->nap_allowed))
1109 pdata->nap_allowed = 1;
1110
1111 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1112 &pdata->clk_map))
1113 goto err;
1114
1115 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1116
1117 if (device->id != KGSL_DEVICE_3D0)
1118 goto err;
1119
1120 /* Bus Scale Data */
1121
1122 pdata->bus_scale_table = adreno_of_get_bus_scale(pdev->dev.of_node);
1123 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1124 ret = PTR_ERR(pdata->bus_scale_table);
1125 goto err;
1126 }
1127
1128 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1129 if (IS_ERR_OR_NULL(pdata->core_info)) {
1130 ret = PTR_ERR(pdata->core_info);
1131 goto err;
1132 }
1133
1134 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1135 if (ret)
1136 goto err;
1137
1138 pdev->dev.platform_data = pdata;
1139 return 0;
1140
1141err:
1142 if (pdata) {
1143 adreno_of_free_bus_scale_info(pdata->bus_scale_table);
1144 if (pdata->core_info)
1145 kfree(pdata->core_info->freq_tbl);
1146 kfree(pdata->core_info);
1147
1148 if (pdata->iommu_data)
1149 kfree(pdata->iommu_data->iommu_ctxs);
1150
1151 kfree(pdata->iommu_data);
1152 }
1153
1154 kfree(pdata);
1155
1156 return ret;
1157}
1158
liu zhong7dfa2a32012-04-27 19:11:01 -07001159#ifdef CONFIG_MSM_OCMEM
1160static int
1161adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1162{
Jordan Crousec0978202012-08-29 14:35:51 -06001163 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001164 return 0;
1165
1166 /* OCMEM is only needed once, do not support consective allocation */
1167 if (adreno_dev->ocmem_hdl != NULL)
1168 return 0;
1169
1170 adreno_dev->ocmem_hdl =
1171 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1172 if (adreno_dev->ocmem_hdl == NULL)
1173 return -ENOMEM;
1174
1175 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001176 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001177
1178 return 0;
1179}
1180
1181static void
1182adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1183{
Jordan Crousec0978202012-08-29 14:35:51 -06001184 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001185 return;
1186
1187 if (adreno_dev->ocmem_hdl == NULL)
1188 return;
1189
1190 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1191 adreno_dev->ocmem_hdl = NULL;
1192}
1193#else
1194static int
1195adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1196{
1197 return 0;
1198}
1199
1200static void
1201adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1202{
1203}
1204#endif
1205
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001206static int __devinit
1207adreno_probe(struct platform_device *pdev)
1208{
1209 struct kgsl_device *device;
1210 struct adreno_device *adreno_dev;
1211 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001212 bool is_dt;
1213
1214 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1215
1216 if (is_dt && pdev->dev.of_node) {
1217 status = adreno_of_get_pdata(pdev);
1218 if (status)
1219 goto error_return;
1220 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221
1222 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1223 adreno_dev = ADRENO_DEVICE(device);
1224 device->parentdev = &pdev->dev;
1225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226 status = adreno_ringbuffer_init(device);
1227 if (status != 0)
1228 goto error;
1229
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001230 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231 if (status)
1232 goto error_close_rb;
1233
1234 adreno_debugfs_init(device);
1235
1236 kgsl_pwrscale_init(device);
1237 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1238
1239 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1240 return 0;
1241
1242error_close_rb:
1243 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1244error:
1245 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001246error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001247 return status;
1248}
1249
1250static int __devexit adreno_remove(struct platform_device *pdev)
1251{
1252 struct kgsl_device *device;
1253 struct adreno_device *adreno_dev;
1254
1255 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1256 adreno_dev = ADRENO_DEVICE(device);
1257
1258 kgsl_pwrscale_detach_policy(device);
1259 kgsl_pwrscale_close(device);
1260
1261 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1262 kgsl_device_platform_remove(device);
1263
1264 return 0;
1265}
1266
1267static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1268{
1269 int status = -EINVAL;
1270 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001272 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1273 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001274
1275 /* Power up the device */
1276 kgsl_pwrctrl_enable(device);
1277
1278 /* Identify the specific GPU */
1279 adreno_identify_gpu(adreno_dev);
1280
Jordan Crouse505df9c2011-07-28 08:37:59 -06001281 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1282 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1283 adreno_dev->chip_id);
1284 goto error_clk_off;
1285 }
1286
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001287 /* Set up the MMU */
1288 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001289 /*
1290 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1291 * on older gpus
1292 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001293 if (adreno_is_a20x(adreno_dev)) {
1294 device->mh.mh_intf_cfg1 = 0;
1295 device->mh.mh_intf_cfg2 = 0;
1296 }
1297
1298 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001299 }
1300
Tarun Karra3335f142012-06-19 14:11:48 -07001301 /* Assign correct RBBM status register to hang detect regs
1302 */
1303 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1304
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001305 status = kgsl_mmu_start(device);
1306 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307 goto error_clk_off;
1308
liu zhong7dfa2a32012-04-27 19:11:01 -07001309 status = adreno_ocmem_gmem_malloc(adreno_dev);
1310 if (status) {
1311 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1312 goto error_mmu_off;
1313 }
1314
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001315 /* Start the GPU */
1316 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317
1318 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001319 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001320
1321 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001322 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001323 /* While recovery is on we do not want timer to
1324 * fire and attempt to change any device state */
1325 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1326 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001327 return 0;
1328 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001331
1332error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001333 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001334
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335error_clk_off:
1336 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337
1338 return status;
1339}
1340
1341static int adreno_stop(struct kgsl_device *device)
1342{
1343 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1344
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001345 adreno_dev->drawctxt_active = NULL;
1346
1347 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1348
Shubhraprakash Das79447952012-04-26 18:12:23 -06001349 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001351 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301352 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001353 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001354
liu zhong7dfa2a32012-04-27 19:11:01 -07001355 adreno_ocmem_gmem_free(adreno_dev);
1356
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001357 /* Power down the device */
1358 kgsl_pwrctrl_disable(device);
1359
1360 return 0;
1361}
1362
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001363static void adreno_mark_context_status(struct kgsl_device *device,
1364 int recovery_status)
1365{
1366 struct kgsl_context *context;
1367 int next = 0;
1368 /*
1369 * Set the reset status of all contexts to
1370 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1371 * since thats the guilty party, if recovery failed then
1372 * mark all as guilty
1373 */
1374 while ((context = idr_get_next(&device->context_idr, &next))) {
1375 struct adreno_context *adreno_context = context->devctxt;
1376 if (recovery_status) {
1377 context->reset_status =
1378 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1379 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1380 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1381 context->reset_status) {
1382 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1383 CTXT_FLAGS_GPU_HANG_RECOVERED))
1384 context->reset_status =
1385 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1386 else
1387 context->reset_status =
1388 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1389 }
1390 next = next + 1;
1391 }
1392}
1393
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001394static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1395{
1396 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1397 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1398 struct kgsl_context *context;
1399 struct adreno_context *temp_adreno_context;
1400 int next = 0;
1401
1402 while ((context = idr_get_next(&device->context_idr, &next))) {
1403 temp_adreno_context = context->devctxt;
1404 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1405 kgsl_sharedmem_writel(&device->memstore,
1406 KGSL_MEMSTORE_OFFSET(context->id,
1407 soptimestamp),
1408 rb->timestamp[context->id]);
1409 kgsl_sharedmem_writel(&device->memstore,
1410 KGSL_MEMSTORE_OFFSET(context->id,
1411 eoptimestamp),
1412 rb->timestamp[context->id]);
1413 }
1414 next = next + 1;
1415 }
1416}
1417
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001418static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1419{
1420 vfree(rec_data->rb_buffer);
1421 vfree(rec_data->bad_rb_buffer);
1422}
1423
1424static int adreno_setup_recovery_data(struct kgsl_device *device,
1425 struct adreno_recovery_data *rec_data)
1426{
1427 int ret = 0;
1428 unsigned int ib1_sz, ib2_sz;
1429 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1430 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1431
1432 memset(rec_data, 0, sizeof(*rec_data));
1433
1434 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1435 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1436 if (ib1_sz || ib2_sz)
1437 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1438
1439 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1440 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1441 current_context));
1442
1443 kgsl_sharedmem_readl(&device->memstore,
1444 &rec_data->global_eop,
1445 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1446 eoptimestamp));
1447
1448 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1449 if (!rec_data->rb_buffer) {
1450 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1451 rb->buffer_desc.size);
1452 return -ENOMEM;
1453 }
1454
1455 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1456 if (!rec_data->bad_rb_buffer) {
1457 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1458 rb->buffer_desc.size);
1459 ret = -ENOMEM;
1460 goto done;
1461 }
1462
1463done:
1464 if (ret) {
1465 vfree(rec_data->rb_buffer);
1466 vfree(rec_data->bad_rb_buffer);
1467 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001468 return ret;
1469}
1470
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001471static int
1472_adreno_recover_hang(struct kgsl_device *device,
1473 struct adreno_recovery_data *rec_data,
1474 bool try_bad_commands)
1475{
1476 int ret;
1477 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1478 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1479 struct kgsl_context *context;
1480 struct adreno_context *adreno_context = NULL;
1481 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1482
1483 context = idr_find(&device->context_idr, rec_data->context_id);
1484 if (context == NULL) {
1485 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1486 rec_data->context_id);
1487 } else {
1488 adreno_context = context->devctxt;
1489 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1490 }
1491
1492 /* Extract valid contents from rb which can still be executed after
1493 * hang */
1494 ret = adreno_ringbuffer_extract(rb, rec_data);
1495 if (ret)
1496 goto done;
1497
1498 /* restart device */
1499 ret = adreno_stop(device);
1500 if (ret) {
1501 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1502 goto done;
1503 }
1504
1505 ret = adreno_start(device, true);
1506 if (ret) {
1507 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1508 goto done;
1509 }
1510
1511 if (context)
1512 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1513 KGSL_MEMSTORE_GLOBAL);
1514
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001515 /* If iommu is used then we need to make sure that the iommu clocks
1516 * are on since there could be commands in pipeline that touch iommu */
1517 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1518 ret = kgsl_mmu_enable_clk(&device->mmu,
1519 KGSL_IOMMU_CONTEXT_USER);
1520 if (ret)
1521 goto done;
1522 }
1523
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001524 /* Do not try the bad caommands if recovery has failed bad commands
1525 * once already */
1526 if (!try_bad_commands)
1527 rec_data->bad_rb_size = 0;
1528
1529 if (rec_data->bad_rb_size) {
1530 int idle_ret;
1531 /* submit the bad and good context commands and wait for
1532 * them to pass */
1533 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1534 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001535 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001536 if (idle_ret) {
1537 ret = adreno_stop(device);
1538 if (ret) {
1539 KGSL_DRV_ERR(device,
1540 "Device stop failed in recovery\n");
1541 goto done;
1542 }
1543 ret = adreno_start(device, true);
1544 if (ret) {
1545 KGSL_DRV_ERR(device,
1546 "Device start failed in recovery\n");
1547 goto done;
1548 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001549 if (context)
1550 kgsl_mmu_setstate(&device->mmu,
1551 adreno_context->pagetable,
1552 KGSL_MEMSTORE_GLOBAL);
1553
1554 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1555 ret = kgsl_mmu_enable_clk(&device->mmu,
1556 KGSL_IOMMU_CONTEXT_USER);
1557 if (ret)
1558 goto done;
1559 }
1560
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001561 ret = idle_ret;
1562 KGSL_DRV_ERR(device,
1563 "Bad context commands hung in recovery\n");
1564 } else {
1565 KGSL_DRV_ERR(device,
1566 "Bad context commands succeeded in recovery\n");
1567 if (adreno_context)
1568 adreno_context->flags = (adreno_context->flags &
1569 ~CTXT_FLAGS_GPU_HANG) |
1570 CTXT_FLAGS_GPU_HANG_RECOVERED;
1571 adreno_dev->drawctxt_active = last_active_ctx;
1572 }
1573 }
1574 /* If either the bad command sequence failed or we did not play it */
1575 if (ret || !rec_data->bad_rb_size) {
1576 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1577 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001578 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001579 if (ret) {
1580 /* If we fail here we can try to invalidate another
1581 * context and try recovering again */
1582 ret = -EAGAIN;
1583 goto done;
1584 }
1585 /* ringbuffer now has data from the last valid context id,
1586 * so restore the active_ctx to the last valid context */
1587 if (rec_data->last_valid_ctx_id) {
1588 struct kgsl_context *last_ctx =
1589 idr_find(&device->context_idr,
1590 rec_data->last_valid_ctx_id);
1591 if (last_ctx)
1592 adreno_dev->drawctxt_active = last_ctx->devctxt;
1593 }
1594 }
1595done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001596 /* Turn off iommu clocks */
1597 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1598 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001599 return ret;
1600}
1601
1602static int
1603adreno_recover_hang(struct kgsl_device *device,
1604 struct adreno_recovery_data *rec_data)
1605{
1606 int ret = 0;
1607 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1608 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1609 unsigned int timestamp;
1610
1611 KGSL_DRV_ERR(device,
1612 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1613 "Bad context_id: %u, global_eop: 0x%x\n",
1614 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1615
1616 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1617 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1618
1619 /* We may need to replay commands multiple times based on whether
1620 * multiple contexts hang the GPU */
1621 while (true) {
1622 if (!ret)
1623 ret = _adreno_recover_hang(device, rec_data, true);
1624 else
1625 ret = _adreno_recover_hang(device, rec_data, false);
1626
1627 if (-EAGAIN == ret) {
1628 /* setup new recovery parameters and retry, this
1629 * means more than 1 contexts are causing hang */
1630 adreno_destroy_recovery_data(rec_data);
1631 adreno_setup_recovery_data(device, rec_data);
1632 KGSL_DRV_ERR(device,
1633 "Retry recovery from 3D GPU hang. Recovery parameters: "
1634 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1635 rec_data->ib1, rec_data->context_id,
1636 rec_data->global_eop);
1637 } else {
1638 break;
1639 }
1640 }
1641
1642 if (ret)
1643 goto done;
1644
1645 /* Restore correct states after recovery */
1646 if (adreno_dev->drawctxt_active)
1647 device->mmu.hwpagetable =
1648 adreno_dev->drawctxt_active->pagetable;
1649 else
1650 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1651 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1652 kgsl_sharedmem_writel(&device->memstore,
1653 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1654 eoptimestamp),
1655 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1656done:
1657 adreno_set_max_ts_for_bad_ctxs(device);
1658 adreno_mark_context_status(device, ret);
1659 if (!ret)
1660 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1661 else
1662 KGSL_DRV_ERR(device, "Recovery failed\n");
1663 return ret;
1664}
1665
1666int
1667adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001669 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001670 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001671
1672 if (device->state == KGSL_STATE_HUNG)
1673 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001674 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675 mutex_unlock(&device->mutex);
1676 wait_for_completion(&device->recovery_gate);
1677 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001678 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001679 result = 0;
1680 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001681 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001682 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001683 /* Detected a hang */
1684
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001685 /* Get the recovery data as soon as hang is detected */
1686 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001687 /*
1688 * Trigger an automatic dump of the state to
1689 * the console
1690 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001691 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001692
1693 /*
1694 * Make a GPU snapshot. For now, do it after the PM dump so we
1695 * can at least be sure the PM dump will work as it always has
1696 */
1697 kgsl_device_snapshot(device, 1);
1698
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001699 result = adreno_recover_hang(device, &rec_data);
1700 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001701 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001702 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001703 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001704 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001705 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1706 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001707 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001708 }
1709done:
1710 return result;
1711}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001712EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713
1714static int adreno_getproperty(struct kgsl_device *device,
1715 enum kgsl_property_type type,
1716 void *value,
1717 unsigned int sizebytes)
1718{
1719 int status = -EINVAL;
1720 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1721
1722 switch (type) {
1723 case KGSL_PROP_DEVICE_INFO:
1724 {
1725 struct kgsl_devinfo devinfo;
1726
1727 if (sizebytes != sizeof(devinfo)) {
1728 status = -EINVAL;
1729 break;
1730 }
1731
1732 memset(&devinfo, 0, sizeof(devinfo));
1733 devinfo.device_id = device->id+1;
1734 devinfo.chip_id = adreno_dev->chip_id;
1735 devinfo.mmu_enabled = kgsl_mmu_enabled();
1736 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001737 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1738 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001739
1740 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1741 0) {
1742 status = -EFAULT;
1743 break;
1744 }
1745 status = 0;
1746 }
1747 break;
1748 case KGSL_PROP_DEVICE_SHADOW:
1749 {
1750 struct kgsl_shadowprop shadowprop;
1751
1752 if (sizebytes != sizeof(shadowprop)) {
1753 status = -EINVAL;
1754 break;
1755 }
1756 memset(&shadowprop, 0, sizeof(shadowprop));
1757 if (device->memstore.hostptr) {
1758 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1759 * anything to mmap().
1760 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001761 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001762 shadowprop.size = device->memstore.size;
1763 /* GSL needs this to be set, even if it
1764 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001765 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1766 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001767 }
1768 if (copy_to_user(value, &shadowprop,
1769 sizeof(shadowprop))) {
1770 status = -EFAULT;
1771 break;
1772 }
1773 status = 0;
1774 }
1775 break;
1776 case KGSL_PROP_MMU_ENABLE:
1777 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001778 int mmu_prop = kgsl_mmu_enabled();
1779
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001780 if (sizebytes != sizeof(int)) {
1781 status = -EINVAL;
1782 break;
1783 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001784 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001785 status = -EFAULT;
1786 break;
1787 }
1788 status = 0;
1789 }
1790 break;
1791 case KGSL_PROP_INTERRUPT_WAITS:
1792 {
1793 int int_waits = 1;
1794 if (sizebytes != sizeof(int)) {
1795 status = -EINVAL;
1796 break;
1797 }
1798 if (copy_to_user(value, &int_waits, sizeof(int))) {
1799 status = -EFAULT;
1800 break;
1801 }
1802 status = 0;
1803 }
1804 break;
1805 default:
1806 status = -EINVAL;
1807 }
1808
1809 return status;
1810}
1811
Jordan Crousef7370f82012-04-18 09:31:07 -06001812static int adreno_setproperty(struct kgsl_device *device,
1813 enum kgsl_property_type type,
1814 void *value,
1815 unsigned int sizebytes)
1816{
1817 int status = -EINVAL;
1818
1819 switch (type) {
1820 case KGSL_PROP_PWRCTRL: {
1821 unsigned int enable;
1822 struct kgsl_device_platform_data *pdata =
1823 kgsl_device_get_drvdata(device);
1824
1825 if (sizebytes != sizeof(enable))
1826 break;
1827
1828 if (copy_from_user(&enable, (void __user *) value,
1829 sizeof(enable))) {
1830 status = -EFAULT;
1831 break;
1832 }
1833
1834 if (enable) {
1835 if (pdata->nap_allowed)
1836 device->pwrctrl.nap_allowed = true;
1837
1838 kgsl_pwrscale_enable(device);
1839 } else {
1840 device->pwrctrl.nap_allowed = false;
1841 kgsl_pwrscale_disable(device);
1842 }
1843
1844 status = 0;
1845 }
1846 break;
1847 default:
1848 break;
1849 }
1850
1851 return status;
1852}
1853
Lynus Vaz06a9a902011-10-04 19:25:33 +05301854static inline void adreno_poke(struct kgsl_device *device)
1855{
1856 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1857 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1858}
1859
Jordan Crousea29a2e02012-08-14 09:09:23 -06001860static int adreno_ringbuffer_drain(struct kgsl_device *device,
1861 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001862{
1863 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1864 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001865 unsigned long wait;
1866 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1867
1868 if (!(rb->flags & KGSL_FLAGS_STARTED))
1869 return 0;
1870
1871 /*
1872 * The first time into the loop, wait for 100 msecs and kick wptr again
1873 * to ensure that the hardware has updated correctly. After that, kick
1874 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1875 * expires
1876 */
1877
1878 wait = jiffies + msecs_to_jiffies(100);
1879
1880 adreno_poke(device);
1881
1882 do {
1883 if (time_after(jiffies, wait)) {
1884 adreno_poke(device);
1885
1886 /* Check to see if the core is hung */
1887 if (adreno_hang_detect(device, regs))
1888 return -ETIMEDOUT;
1889
1890 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1891 }
1892 GSL_RB_GET_READPTR(rb, &rb->rptr);
1893
1894 if (time_after(jiffies, timeout)) {
1895 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1896 rb->rptr, rb->wptr);
1897 return -ETIMEDOUT;
1898 }
1899 } while (rb->rptr != rb->wptr);
1900
1901 return 0;
1902}
1903
1904/* Caller must hold the device mutex. */
1905int adreno_idle(struct kgsl_device *device)
1906{
1907 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301909 unsigned long wait_time;
1910 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001911 unsigned int prev_reg_val[hang_detect_regs_count];
1912
1913 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001914
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001915 kgsl_cffdump_regpoll(device->id,
1916 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001917 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001918
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001919retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001920 /* First, wait for the ringbuffer to drain */
1921 if (adreno_ringbuffer_drain(device, prev_reg_val))
1922 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001923
1924 /* now, wait for the GPU to finish its operations */
Jordan Crousea29a2e02012-08-14 09:09:23 -06001925 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
1926 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1927
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001928 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001929 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1930 &rbbm_status);
1931 if (adreno_is_a2xx(adreno_dev)) {
1932 if (rbbm_status == 0x110)
1933 return 0;
1934 } else {
1935 if (!(rbbm_status & 0x80000000))
1936 return 0;
1937 }
Tarun Karra3335f142012-06-19 14:11:48 -07001938
1939 /* Dont wait for timeout, detect hang faster.
1940 */
1941 if (time_after(jiffies, wait_time_part)) {
1942 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001943 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001944 if ((adreno_hang_detect(device, prev_reg_val)))
1945 goto err;
1946 }
1947
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001948 }
1949
1950err:
1951 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001952 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1953 !adreno_dump_and_recover(device)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001954 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001955 goto retry;
1956 }
1957 return -ETIMEDOUT;
1958}
1959
1960static unsigned int adreno_isidle(struct kgsl_device *device)
1961{
1962 int status = false;
1963 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1964 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1965 unsigned int rbbm_status;
1966
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001967 WARN_ON(device->state == KGSL_STATE_INIT);
1968 /* If the device isn't active, don't force it on. */
1969 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001970 /* Is the ring buffer is empty? */
1971 GSL_RB_GET_READPTR(rb, &rb->rptr);
1972 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1973 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001974 adreno_regread(device,
1975 adreno_dev->gpudev->reg_rbbm_status,
1976 &rbbm_status);
1977
1978 if (adreno_is_a2xx(adreno_dev)) {
1979 if (rbbm_status == 0x110)
1980 status = true;
1981 } else {
1982 if (!(rbbm_status & 0x80000000))
1983 status = true;
1984 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001985 }
1986 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001987 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001988 }
1989 return status;
1990}
1991
1992/* Caller must hold the device mutex. */
1993static int adreno_suspend_context(struct kgsl_device *device)
1994{
1995 int status = 0;
1996 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1997
1998 /* switch to NULL ctxt */
1999 if (adreno_dev->drawctxt_active != NULL) {
2000 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002001 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002002 }
2003
2004 return status;
2005}
2006
Jordan Crouse233b2092012-04-18 09:31:09 -06002007/* Find a memory structure attached to an adreno context */
2008
2009struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2010 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2011{
2012 struct kgsl_context *context;
2013 struct adreno_context *adreno_context = NULL;
2014 int next = 0;
2015
2016 while (1) {
2017 context = idr_get_next(&device->context_idr, &next);
2018 if (context == NULL)
2019 break;
2020
2021 adreno_context = (struct adreno_context *)context->devctxt;
2022
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002023 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2024 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002025 struct kgsl_memdesc *desc;
2026
2027 desc = &adreno_context->gpustate;
2028 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2029 return desc;
2030
2031 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2032 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2033 return desc;
2034 }
2035 next = next + 1;
2036 }
2037
2038 return NULL;
2039}
2040
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002041struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002042 unsigned int pt_base,
2043 unsigned int gpuaddr,
2044 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002045{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002046 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002047 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2048 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2049
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002050 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2051 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002052
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002053 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2054 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002055
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002056 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2057 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002058
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002059 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2060 size))
2061 return &device->mmu.setstate_memory;
2062
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002063 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002064
2065 if (entry)
2066 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002067
Jordan Crouse233b2092012-04-18 09:31:09 -06002068 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002069}
2070
2071uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2072 unsigned int gpuaddr, unsigned int size)
2073{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002074 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002075
2076 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2077
2078 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002079}
2080
2081void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2082 unsigned int *value)
2083{
2084 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002085 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2086 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002087
2088 if (!in_interrupt())
2089 kgsl_pre_hwaccess(device);
2090
2091 /*ensure this read finishes before the next one.
2092 * i.e. act like normal readl() */
2093 *value = __raw_readl(reg);
2094 rmb();
2095}
2096
2097void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2098 unsigned int value)
2099{
2100 unsigned int *reg;
2101
Jordan Crouse7501d452012-04-19 08:58:44 -06002102 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002103
2104 if (!in_interrupt())
2105 kgsl_pre_hwaccess(device);
2106
2107 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002108 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002109
2110 /*ensure previous writes post before this one,
2111 * i.e. act like normal writel() */
2112 wmb();
2113 __raw_writel(value, reg);
2114}
2115
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002116static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2117{
2118 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002119 if (k_ctxt != NULL) {
2120 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002121 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2122 context_id = KGSL_CONTEXT_INVALID;
2123 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2124 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002125 }
2126
2127 return context_id;
2128}
2129
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002130static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002131 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002132{
2133 int status;
2134 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002135 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002136 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002137
2138 mutex_lock(&device->mutex);
2139 context_id = _get_context_id(context);
2140 /*
2141 * If the context ID is invalid, we are in a race with
2142 * the context being destroyed by userspace so bail.
2143 */
2144 if (context_id == KGSL_CONTEXT_INVALID) {
2145 KGSL_DRV_WARN(device, "context was detached");
2146 status = -EINVAL;
2147 goto unlock;
2148 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002149
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002150 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002151 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002152 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002153 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002154 mb();
2155
2156 if (enableflag) {
2157 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002158 KGSL_MEMSTORE_OFFSET(context_id,
2159 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002160 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002161 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002162 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002163 KGSL_MEMSTORE_OFFSET(context_id,
2164 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002165 wmb();
2166 }
2167 } else {
2168 unsigned int cmds[2];
2169 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002170 KGSL_MEMSTORE_OFFSET(context_id,
2171 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002172 enableflag = 1;
2173 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002174 KGSL_MEMSTORE_OFFSET(context_id,
2175 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002176 wmb();
2177 /* submit a dummy packet so that even if all
2178 * commands upto timestamp get executed we will still
2179 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002180 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002182
2183 if (adreno_dev->drawctxt_active)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002184 adreno_ringbuffer_issuecmds_intr(device,
2185 context, &cmds[0], 2);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002186 else
2187 /* We would never call this function if there
2188 * was no active contexts running */
2189 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002190 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002191 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002192unlock:
2193 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002194
2195 return status;
2196}
2197
2198/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002199 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002200 placing a process in wait q. For conditional interrupts we expect the
2201 process to already be in its wait q when its exit condition checking
2202 function is called.
2203*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002204#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002205({ \
2206 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002207 if (io) \
2208 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2209 else \
2210 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002211 __ret; \
2212})
2213
Tarun Karra3335f142012-06-19 14:11:48 -07002214
2215
2216unsigned int adreno_hang_detect(struct kgsl_device *device,
2217 unsigned int *prev_reg_val)
2218{
2219 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2220 unsigned int curr_reg_val[hang_detect_regs_count];
2221 unsigned int hang_detected = 1;
2222 unsigned int i;
2223
2224 if (!adreno_dev->fast_hang_detect)
2225 return 0;
2226
2227 for (i = 0; i < hang_detect_regs_count; i++) {
2228 adreno_regread(device, hang_detect_regs[i],
2229 &curr_reg_val[i]);
2230 if (curr_reg_val[i] != prev_reg_val[i]) {
2231 prev_reg_val[i] = curr_reg_val[i];
2232 hang_detected = 0;
2233 }
2234 }
2235
2236 return hang_detected;
2237}
2238
2239
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002240/* MUST be called with the device mutex held */
2241static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002242 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002243 unsigned int timestamp,
2244 unsigned int msecs)
2245{
2246 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06002247 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06002248 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002249 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06002250 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07002251 int retries = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002252 unsigned int ts_issued;
2253 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07002254 unsigned int time_elapsed = 0;
2255 unsigned int prev_reg_val[hang_detect_regs_count];
Jordan Crouse21f75a02012-08-09 15:08:59 -06002256 unsigned int wait;
Tarun Karra3335f142012-06-19 14:11:48 -07002257
2258 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002259
2260 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002261
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302262 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07002263 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302264 msecs = adreno_dev->wait_timeout;
2265
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002266 if (timestamp_cmp(timestamp, ts_issued) > 0) {
2267 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
2268 "last issued ts <%d:0x%x>\n",
2269 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002270 status = -EINVAL;
2271 goto done;
2272 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002273
Jordan Crouse21f75a02012-08-09 15:08:59 -06002274 /*
2275 * Make the first timeout interval 100 msecs and then try to kick the
2276 * wptr again. This helps to ensure the wptr is updated properly. If
2277 * the requested timeout is less than 100 msecs, then wait 20msecs which
2278 * is the minimum amount of time we can safely wait at 100HZ
Lynus Vaz06a9a902011-10-04 19:25:33 +05302279 */
Jordan Crouse21f75a02012-08-09 15:08:59 -06002280
2281 if (msecs == 0 || msecs >= 100)
2282 wait = 100;
2283 else
2284 wait = 20;
2285
Tarun Karra3335f142012-06-19 14:11:48 -07002286 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002287 /*
2288 * If the context ID is invalid, we are in a race with
2289 * the context being destroyed by userspace so bail.
2290 */
2291 if (context_id == KGSL_CONTEXT_INVALID) {
2292 KGSL_DRV_WARN(device, "context was detached");
2293 status = -EINVAL;
2294 goto done;
2295 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002296 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07002297 /* if the timestamp happens while we're not
2298 * waiting, there's a chance that an interrupt
2299 * will not be generated and thus the timestamp
2300 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05302301 */
Jeremy Gebben63904832012-02-07 16:10:55 -07002302 queue_work(device->work_queue, &device->ts_expired_ws);
2303 status = 0;
2304 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002305 }
Jeremy Gebben63904832012-02-07 16:10:55 -07002306 adreno_poke(device);
2307 io_cnt = (io_cnt + 1) % 100;
2308 if (io_cnt <
2309 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2310 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002311
2312 if ((retries > 0) &&
2313 (adreno_hang_detect(device, prev_reg_val)))
2314 goto hang_dump;
2315
Jeremy Gebben63904832012-02-07 16:10:55 -07002316 mutex_unlock(&device->mutex);
2317 /* We need to make sure that the process is
2318 * placed in wait-q before its condition is called
2319 */
2320 status = kgsl_wait_event_interruptible_timeout(
2321 device->wait_queue,
2322 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002323 context, timestamp),
Jordan Crouse21f75a02012-08-09 15:08:59 -06002324 msecs_to_jiffies(wait), io);
2325
Jeremy Gebben63904832012-02-07 16:10:55 -07002326 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002327
Jeremy Gebben63904832012-02-07 16:10:55 -07002328 if (status > 0) {
2329 /*completed before the wait finished */
2330 status = 0;
2331 goto done;
2332 } else if (status < 0) {
2333 /*an error occurred*/
2334 goto done;
2335 }
2336 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07002337
Jordan Crouse21f75a02012-08-09 15:08:59 -06002338 time_elapsed += wait;
2339 wait = KGSL_TIMEOUT_PART;
2340
Tarun Karra3335f142012-06-19 14:11:48 -07002341 retries++;
2342
Jordan Crouse21f75a02012-08-09 15:08:59 -06002343 } while (!msecs || time_elapsed < msecs);
Tarun Karra3335f142012-06-19 14:11:48 -07002344
2345hang_dump:
Shubhraprakash Das54396e52012-03-10 13:24:54 -07002346 /*
2347 * Check if timestamp has retired here because we may have hit
2348 * recovery which can take some time and cause waiting threads
2349 * to timeout
2350 */
2351 if (kgsl_check_timestamp(device, context, timestamp))
2352 goto done;
Jeremy Gebben63904832012-02-07 16:10:55 -07002353 status = -ETIMEDOUT;
2354 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002355 "Device hang detected while waiting for timestamp: "
2356 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2357 "wptr: 0x%x\n",
2358 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002359 adreno_dev->ringbuffer.wptr);
2360 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002361 /* The timestamp that this process wanted
2362 * to wait on may be invalid or expired now
2363 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07002364 status = 0;
2365 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002366done:
2367 return (int)status;
2368}
2369
2370static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002371 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002372{
2373 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002374 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002375
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002376 /*
2377 * If the context ID is invalid, we are in a race with
2378 * the context being destroyed by userspace so bail.
2379 */
2380 if (context_id == KGSL_CONTEXT_INVALID) {
2381 KGSL_DRV_WARN(device, "context was detached");
2382 return timestamp;
2383 }
Jordan Crousec659f382012-04-16 11:10:41 -06002384 switch (type) {
2385 case KGSL_TIMESTAMP_QUEUED: {
2386 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2387 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2388
2389 timestamp = rb->timestamp[context_id];
2390 break;
2391 }
2392 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002393 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002394 break;
2395 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002396 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002397 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2398 break;
2399 }
2400
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002401 rmb();
2402
2403 return timestamp;
2404}
2405
2406static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2407 unsigned int cmd, void *data)
2408{
2409 int result = 0;
2410 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2411 struct kgsl_context *context;
2412
2413 switch (cmd) {
2414 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2415 binbase = data;
2416
2417 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2418 if (context) {
2419 adreno_drawctxt_set_bin_base_offset(
2420 dev_priv->device, context, binbase->offset);
2421 } else {
2422 result = -EINVAL;
2423 KGSL_DRV_ERR(dev_priv->device,
2424 "invalid drawctxt drawctxt_id %d "
2425 "device_id=%d\n",
2426 binbase->drawctxt_id, dev_priv->device->id);
2427 }
2428 break;
2429
2430 default:
2431 KGSL_DRV_INFO(dev_priv->device,
2432 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002433 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002434 break;
2435 }
2436 return result;
2437
2438}
2439
2440static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2441{
2442 gpu_freq /= 1000000;
2443 return ticks / gpu_freq;
2444}
2445
2446static void adreno_power_stats(struct kgsl_device *device,
2447 struct kgsl_power_stats *stats)
2448{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002449 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002450 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002451 unsigned int cycles;
2452
2453 /* Get the busy cycles counted since the counter was last reset */
2454 /* Calling this function also resets and restarts the counter */
2455
2456 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002457
2458 /* In order to calculate idle you have to have run the algorithm *
2459 * at least once to get a start time. */
2460 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002461 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002462 stats->total_time = tmp - pwr->time;
2463 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002464 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002465 pwrlevels[device->pwrctrl.active_pwrlevel].
2466 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002467 } else {
2468 stats->total_time = 0;
2469 stats->busy_time = 0;
2470 pwr->time = ktime_to_us(ktime_get());
2471 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002472}
2473
2474void adreno_irqctrl(struct kgsl_device *device, int state)
2475{
Jordan Crousea78c9172011-07-11 13:14:09 -06002476 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2477 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002478}
2479
Jordan Croused6535882012-06-20 08:22:16 -06002480static unsigned int adreno_gpuid(struct kgsl_device *device,
2481 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002482{
2483 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2484
Jordan Croused6535882012-06-20 08:22:16 -06002485 /* Some applications need to know the chip ID too, so pass
2486 * that as a parameter */
2487
2488 if (chipid != NULL)
2489 *chipid = adreno_dev->chip_id;
2490
Jordan Crousea0758f22011-12-07 11:19:22 -07002491 /* Standard KGSL gpuid format:
2492 * top word is 0x0002 for 2D or 0x0003 for 3D
2493 * Bottom word is core specific identifer
2494 */
2495
2496 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2497}
2498
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002499static const struct kgsl_functable adreno_functable = {
2500 /* Mandatory functions */
2501 .regread = adreno_regread,
2502 .regwrite = adreno_regwrite,
2503 .idle = adreno_idle,
2504 .isidle = adreno_isidle,
2505 .suspend_context = adreno_suspend_context,
2506 .start = adreno_start,
2507 .stop = adreno_stop,
2508 .getproperty = adreno_getproperty,
2509 .waittimestamp = adreno_waittimestamp,
2510 .readtimestamp = adreno_readtimestamp,
2511 .issueibcmds = adreno_ringbuffer_issueibcmds,
2512 .ioctl = adreno_ioctl,
2513 .setup_pt = adreno_setup_pt,
2514 .cleanup_pt = adreno_cleanup_pt,
2515 .power_stats = adreno_power_stats,
2516 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002517 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002518 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002519 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002520 /* Optional functions */
2521 .setstate = adreno_setstate,
2522 .drawctxt_create = adreno_drawctxt_create,
2523 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002524 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002525 .postmortem_dump = adreno_dump,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002526};
2527
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002528static struct platform_driver adreno_platform_driver = {
2529 .probe = adreno_probe,
2530 .remove = __devexit_p(adreno_remove),
2531 .suspend = kgsl_suspend_driver,
2532 .resume = kgsl_resume_driver,
2533 .id_table = adreno_id_table,
2534 .driver = {
2535 .owner = THIS_MODULE,
2536 .name = DEVICE_3D_NAME,
2537 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002538 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002539 }
2540};
2541
2542static int __init kgsl_3d_init(void)
2543{
2544 return platform_driver_register(&adreno_platform_driver);
2545}
2546
2547static void __exit kgsl_3d_exit(void)
2548{
2549 platform_driver_unregister(&adreno_platform_driver);
2550}
2551
2552module_init(kgsl_3d_init);
2553module_exit(kgsl_3d_exit);
2554
2555MODULE_DESCRIPTION("3D Graphics driver");
2556MODULE_VERSION("1.2");
2557MODULE_LICENSE("GPL v2");
2558MODULE_ALIAS("platform:kgsl_3d");