blob: c8ba73f95e9582d9a60317a358d7d15fe0a3b8a5 [file] [log] [blame]
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -07001/* Copyright (c) 2002,2007-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
Steve Mucklef132c6c2012-06-06 18:30:57 -070013#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070014#include <linux/uaccess.h>
15#include <linux/vmalloc.h>
16#include <linux/ioctl.h>
17#include <linux/sched.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060018#include <linux/of.h>
19#include <linux/of_device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020
21#include <mach/socinfo.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060022#include <mach/msm_bus_board.h>
23#include <mach/msm_bus.h>
24#include <mach/msm_dcvs.h>
25#include <mach/msm_dcvs_scm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27#include "kgsl.h"
28#include "kgsl_pwrscale.h"
29#include "kgsl_cffdump.h"
30#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060031#include "kgsl_iommu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
33#include "adreno.h"
34#include "adreno_pm4types.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070036#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070037#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
39#define DRIVER_VERSION_MAJOR 3
40#define DRIVER_VERSION_MINOR 1
41
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042/* Adreno MH arbiter config*/
43#define ADRENO_CFG_MHARB \
44 (0x10 \
45 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
46 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
47 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
48 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
52 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
53 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
54 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
55 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
56 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
57 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
58 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
59
60#define ADRENO_MMU_CONFIG \
61 (0x01 \
62 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
69 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
70 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
71 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
72 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
73
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074static const struct kgsl_functable adreno_functable;
75
76static struct adreno_device device_3d0 = {
77 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070078 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079 .name = DEVICE_3D0_NAME,
80 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060081 .mh = {
82 .mharb = ADRENO_CFG_MHARB,
83 /* Remove 1k boundary check in z470 to avoid a GPU
84 * hang. Notice that this solution won't work if
85 * both EBI and SMI are used
86 */
87 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088 /* turn off memory protection unit by setting
89 acceptable physical address range to include
90 all pages. */
91 .mpu_base = 0x00000000,
92 .mpu_range = 0xFFFFF000,
93 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060094 .mmu = {
95 .config = ADRENO_MMU_CONFIG,
96 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 .iomemname = KGSL_3D0_REG_MEMORY,
101 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600103 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
105 .suspend = kgsl_early_suspend_driver,
106 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600108#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600110 .gmem_base = 0,
111 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 .pfp_fw = NULL,
113 .pm4_fw = NULL,
Jordan Crouse21f75a02012-08-09 15:08:59 -0600114 .wait_timeout = 0, /* in milliseconds, 0 means disabled */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600115 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116};
117
Tarun Karra3335f142012-06-19 14:11:48 -0700118/* This set of registers are used for Hang detection
119 * If the values of these registers are same after
120 * KGSL_TIMEOUT_PART time, GPU hang is reported in
121 * kernel log.
122 */
123unsigned int hang_detect_regs[] = {
124 A3XX_RBBM_STATUS,
125 REG_CP_RB_RPTR,
126 REG_CP_IB1_BASE,
127 REG_CP_IB1_BUFSZ,
128 REG_CP_IB2_BASE,
129 REG_CP_IB2_BUFSZ,
Jordan Crouseb5c80482012-10-03 09:38:41 -0600130 0,
131 0
Tarun Karra3335f142012-06-19 14:11:48 -0700132};
133
134const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700135
Jordan Crouse505df9c2011-07-28 08:37:59 -0600136/*
137 * This is the master list of all GPU cores that are supported by this
138 * driver.
139 */
140
141#define ANY_ID (~0)
Tarun Karra9c070822012-11-27 16:43:51 -0700142#define NO_VER (~0)
Jordan Crouse505df9c2011-07-28 08:37:59 -0600143
144static const struct {
145 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600146 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600147 const char *pm4fw;
148 const char *pfpfw;
149 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700150 unsigned int istore_size;
151 unsigned int pix_shader_start;
Tarun Karra9c070822012-11-27 16:43:51 -0700152 /* Size of an instruction in dwords */
153 unsigned int instruction_size;
154 /* size of gmem for gpu*/
155 unsigned int gmem_size;
156 /* version of pm4 microcode that supports sync_lock
157 between CPU and GPU for SMMU-v1 programming */
158 unsigned int sync_lock_pm4_ver;
159 /* version of pfp microcode that supports sync_lock
160 between CPU and GPU for SMMU-v1 programming */
161 unsigned int sync_lock_pfp_ver;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600162} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600163 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700164 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700165 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530166 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
167 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700168 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600169 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700170 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700171 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600172 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700173 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700174 512, 384, 3, SZ_512K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600175 /*
176 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
177 * a hardware problem.
178 */
179 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700180 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700181 1536, 768, 3, SZ_512K, NO_VER, NO_VER },
Carter Cooperf27ec722011-11-17 15:20:38 -0700182 { ADRENO_REV_A225, 2, 2, 0, 6,
183 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700184 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600185 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700186 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700187 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530188 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530189 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530190 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700191 512, 0, 2, SZ_256K, 0x3FF037, 0x3FF016 },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700192 /* A3XX doesn't use the pix_shader_start */
Carter Cooper95f7f792012-08-19 13:40:34 -0600193 { ADRENO_REV_A320, 3, 2, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700194 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700195 512, 0, 2, SZ_512K, 0x3FF037, 0x3FF016 },
liu zhongfd42e622012-05-01 19:18:30 -0700196 { ADRENO_REV_A330, 3, 3, 0, 0,
197 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700198 512, 0, 2, SZ_1M, NO_VER, NO_VER },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600199};
200
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600201static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202{
Jordan Crousea78c9172011-07-11 13:14:09 -0600203 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600204 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205
Jordan Crousea78c9172011-07-11 13:14:09 -0600206 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207
208 if (device->requested_state == KGSL_STATE_NONE) {
209 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700210 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 queue_work(device->work_queue, &device->idle_check_ws);
212 } else if (device->pwrscale.policy != NULL) {
213 queue_work(device->work_queue, &device->idle_check_ws);
214 }
215 }
216
217 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800218 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219 jiffies + device->pwrctrl.interval_timeout);
220 return result;
221}
222
Jordan Crouse9f739212011-07-28 08:37:57 -0600223static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 struct kgsl_pagetable *pagetable)
225{
226 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
227 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
228
229 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
230
231 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
232
233 kgsl_mmu_unmap(pagetable, &device->memstore);
234
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600235 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236}
237
238static int adreno_setup_pt(struct kgsl_device *device,
239 struct kgsl_pagetable *pagetable)
240{
241 int result = 0;
242 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
243 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
244
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
246 GSL_PT_PAGE_RV);
247 if (result)
248 goto error;
249
250 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
251 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
252 if (result)
253 goto unmap_buffer_desc;
254
255 result = kgsl_mmu_map_global(pagetable, &device->memstore,
256 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
257 if (result)
258 goto unmap_memptrs_desc;
259
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600260 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
262 if (result)
263 goto unmap_memstore_desc;
264
265 return result;
266
267unmap_memstore_desc:
268 kgsl_mmu_unmap(pagetable, &device->memstore);
269
270unmap_memptrs_desc:
271 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
272
273unmap_buffer_desc:
274 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
275
276error:
277 return result;
278}
279
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600280static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600281 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600282 uint32_t flags)
283{
284 unsigned int pt_val, reg_pt_val;
Tarun Karra9c070822012-11-27 16:43:51 -0700285 unsigned int link[250];
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600286 unsigned int *cmds = &link[0];
287 int sizedwords = 0;
288 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600289 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600290 struct kgsl_context *context;
291 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600292
293 if (!adreno_dev->drawctxt_active)
294 return kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700295 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600296
297 context = idr_find(&device->context_idr, context_id);
298 adreno_ctx = context->devctxt;
299
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600300 if (kgsl_mmu_enable_clk(&device->mmu,
301 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700302 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600303
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600304 cmds += __adreno_add_idle_indirect_cmds(cmds,
305 device->mmu.setstate_memory.gpuaddr +
306 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
307
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600308 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600309 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
310 device->mmu.setstate_memory.gpuaddr +
311 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
312 else
313 cmds += adreno_add_bank_change_cmds(cmds,
314 KGSL_IOMMU_CONTEXT_USER,
315 device->mmu.setstate_memory.gpuaddr +
316 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
317
Tarun Karra9c070822012-11-27 16:43:51 -0700318 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
319
320 /* Acquire GPU-CPU sync Lock here */
321 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
322
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700323 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
324 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600325 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600326 /*
327 * We need to perfrom the following operations for all
328 * IOMMU units
329 */
330 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700331 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
332 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600333 /*
334 * Set address of the new pagetable by writng to IOMMU
335 * TTBR0 register
336 */
337 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700338 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
339 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600340 *cmds++ = reg_pt_val;
341 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
342 *cmds++ = 0x00000000;
343
344 /*
345 * Read back the ttbr0 register as a barrier to ensure
346 * above writes have completed
347 */
348 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700349 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
350 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600351 reg_pt_val,
352 device->mmu.setstate_memory.gpuaddr +
353 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600354 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600355 }
356 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
357 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700358 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600359 */
360 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700361 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
362 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700363
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600364 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700365 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
366 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700367 KGSL_IOMMU_CTX_TLBIALL);
368 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600369
370 cmds += __adreno_add_idle_indirect_cmds(cmds,
371 device->mmu.setstate_memory.gpuaddr +
372 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
373
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600374 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700375 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
376 KGSL_IOMMU_CONTEXT_USER,
377 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700378 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600379 device->mmu.setstate_memory.gpuaddr +
380 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
381 }
382 }
383
Tarun Karra9c070822012-11-27 16:43:51 -0700384 /* Release GPU-CPU sync Lock here */
385 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
386
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600387 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600388 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700389 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
390 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600391 device->mmu.setstate_memory.gpuaddr +
392 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
393 else
394 cmds += adreno_add_bank_change_cmds(cmds,
395 KGSL_IOMMU_CONTEXT_PRIV,
396 device->mmu.setstate_memory.gpuaddr +
397 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
398
Tarun Karra9c070822012-11-27 16:43:51 -0700399 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
400
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600401 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600402 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700403 /* invalidate all base pointers */
404 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
405 *cmds++ = 0x7fff;
406 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600407 /* This returns the per context timestamp but we need to
408 * use the global timestamp for iommu clock disablement */
409 adreno_ringbuffer_issuecmds(device, adreno_ctx,
410 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600411 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600412 kgsl_mmu_disable_clk_on_ts(&device->mmu,
413 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600414 }
Tarun Karra9c070822012-11-27 16:43:51 -0700415
416 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
417 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
418 BUG();
419 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600420}
421
422static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600423 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600424 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425{
426 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
427 unsigned int link[32];
428 unsigned int *cmds = &link[0];
429 int sizedwords = 0;
430 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600431 struct kgsl_context *context;
432 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600434 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530435 * Fix target freeze issue by adding TLB flush for each submit
436 * on A20X based targets.
437 */
438 if (adreno_is_a20x(adreno_dev))
439 flags |= KGSL_MMUFLAGS_TLBFLUSH;
440 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600441 * If possible, then set the state via the command stream to avoid
442 * a CPU idle. Otherwise, use the default setstate which uses register
443 * writes For CFF dump we must idle and use the registers so that it is
444 * easier to filter out the mmu accesses from the dump
445 */
446 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600447 context = idr_find(&device->context_idr, context_id);
448 adreno_ctx = context->devctxt;
449
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
451 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600452 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 *cmds++ = 0x00000000;
454
455 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600456 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700457 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600458 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 sizedwords += 4;
460 }
461
462 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
463 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600464 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465 1);
466 *cmds++ = 0x00000000;
467 sizedwords += 2;
468 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600469 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470 *cmds++ = mh_mmu_invalidate;
471 sizedwords += 2;
472 }
473
474 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600475 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476 /* HW workaround: to resolve MMU page fault interrupts
477 * caused by the VGT.It prevents the CP PFP from filling
478 * the VGT DMA request fifo too early,thereby ensuring
479 * that the VGT will not fetch vertex/bin data until
480 * after the page table base register has been updated.
481 *
482 * Two null DRAW_INDX_BIN packets are inserted right
483 * after the page table base update, followed by a
484 * wait for idle. The null packets will fill up the
485 * VGT DMA request fifo and prevent any further
486 * vertex/bin updates from occurring until the wait
487 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600488 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489 *cmds++ = (0x4 << 16) |
490 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
491 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600492 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600493 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600494 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 *cmds++ = 0; /* viz query info */
496 *cmds++ = 0x0003C004; /* draw indicator */
497 *cmds++ = 0; /* bin base */
498 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600499 *cmds++ =
500 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600502 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 *cmds++ = 0; /* viz query info */
504 *cmds++ = 0x0003C004; /* draw indicator */
505 *cmds++ = 0; /* bin base */
506 *cmds++ = 3; /* bin size */
507 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600508 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600510 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 *cmds++ = 0x00000000;
512 sizedwords += 21;
513 }
514
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600515
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600517 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518 *cmds++ = 0x7fff; /* invalidate all base pointers */
519 sizedwords += 2;
520 }
521
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600522 adreno_ringbuffer_issuecmds(device, adreno_ctx,
523 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600525 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600526 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600527 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528}
529
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600530static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600531 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600532 uint32_t flags)
533{
534 /* call the mmu specific handler */
535 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600536 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600537 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600538 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600539}
540
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700542a3xx_getchipid(struct kgsl_device *device)
543{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600544 struct kgsl_device_platform_data *pdata =
545 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700546
Jordan Crouse54154c62012-03-27 16:33:26 -0600547 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600548 * All current A3XX chipids are detected at the SOC level. Leave this
549 * function here to support any future GPUs that have working
550 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600551 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700552
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600553 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700554}
555
556static unsigned int
557a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558{
559 unsigned int chipid = 0;
560 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600561 struct kgsl_device_platform_data *pdata =
562 kgsl_device_get_drvdata(device);
563
564 /* If the chip id is set at the platform level, then just use that */
565
566 if (pdata->chipid != 0)
567 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568
569 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
570 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
571 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
572
573 /*
574 * adreno 22x gpus are indicated by coreid 2,
575 * but REG_RBBM_PERIPHID1 always contains 0 for this field
576 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600577 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578 chipid = 2 << 24;
579 else
580 chipid = (coreid & 0xF) << 24;
581
582 chipid |= ((majorid >> 4) & 0xF) << 16;
583
584 minorid = ((revid >> 0) & 0xFF);
585
586 patchid = ((revid >> 16) & 0xFF);
587
588 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530589 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590 if (cpu_is_qsd8x50())
591 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530592 else if (cpu_is_msm8625() && minorid == 0)
593 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594
595 chipid |= (minorid << 8) | patchid;
596
597 return chipid;
598}
599
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700600static unsigned int
601adreno_getchipid(struct kgsl_device *device)
602{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600603 struct kgsl_device_platform_data *pdata =
604 kgsl_device_get_drvdata(device);
605
606 /*
607 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
608 * an A2XX processor
609 */
610
611 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700612 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600613 else
614 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700615}
616
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617static inline bool _rev_match(unsigned int id, unsigned int entry)
618{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600619 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621
622static void
623adreno_identify_gpu(struct adreno_device *adreno_dev)
624{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600625 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626
627 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
628
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600629 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
630 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
631 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
632 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633
Jordan Crouse505df9c2011-07-28 08:37:59 -0600634 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
635 if (core == adreno_gpulist[i].core &&
636 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600637 _rev_match(minor, adreno_gpulist[i].minor) &&
638 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 }
641
Jordan Crouse505df9c2011-07-28 08:37:59 -0600642 if (i == ARRAY_SIZE(adreno_gpulist)) {
643 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
644 return;
645 }
646
647 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
648 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
649 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
650 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700651 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
652 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700653 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600654 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Tarun Karra9c070822012-11-27 16:43:51 -0700655 adreno_dev->gpulist_index = i;
656
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657}
658
Lokesh Batra805e1e12012-08-03 08:34:06 -0600659static struct platform_device_id adreno_id_table[] = {
660 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
661 {},
662};
663
664MODULE_DEVICE_TABLE(platform, adreno_id_table);
665
666static struct of_device_id adreno_match_table[] = {
667 { .compatible = "qcom,kgsl-3d0", },
668 {}
669};
670
671static inline int adreno_of_read_property(struct device_node *node,
672 const char *prop, unsigned int *ptr)
673{
674 int ret = of_property_read_u32(node, prop, ptr);
675 if (ret)
676 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
677 return ret;
678}
679
680static struct device_node *adreno_of_find_subnode(struct device_node *parent,
681 const char *name)
682{
683 struct device_node *child;
684
685 for_each_child_of_node(parent, child) {
686 if (of_device_is_compatible(child, name))
687 return child;
688 }
689
690 return NULL;
691}
692
693static int adreno_of_get_pwrlevels(struct device_node *parent,
694 struct kgsl_device_platform_data *pdata)
695{
696 struct device_node *node, *child;
697 int ret = -EINVAL;
698
699 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
700
701 if (node == NULL) {
702 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
703 return -EINVAL;
704 }
705
706 pdata->num_levels = 0;
707
708 for_each_child_of_node(node, child) {
709 unsigned int index;
710 struct kgsl_pwrlevel *level;
711
712 if (adreno_of_read_property(child, "reg", &index))
713 goto done;
714
715 if (index >= KGSL_MAX_PWRLEVELS) {
716 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
717 index);
718 continue;
719 }
720
721 if (index >= pdata->num_levels)
722 pdata->num_levels = index + 1;
723
724 level = &pdata->pwrlevel[index];
725
726 if (adreno_of_read_property(child, "qcom,gpu-freq",
727 &level->gpu_freq))
728 goto done;
729
730 if (adreno_of_read_property(child, "qcom,bus-freq",
731 &level->bus_freq))
732 goto done;
733
734 if (adreno_of_read_property(child, "qcom,io-fraction",
735 &level->io_fraction))
736 level->io_fraction = 0;
737 }
738
739 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
740 &pdata->init_level))
741 pdata->init_level = 1;
742
743 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
744 KGSL_CORE_ERR("Initial power level out of range\n");
745 pdata->init_level = 1;
746 }
747
748 ret = 0;
749done:
750 return ret;
751
752}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600753
754static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
755{
756 struct device_node *node, *child;
757 struct msm_dcvs_core_info *info = NULL;
758 int count = 0;
759 int ret = -EINVAL;
760
761 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
762 if (node == NULL)
763 return ERR_PTR(-EINVAL);
764
765 info = kzalloc(sizeof(*info), GFP_KERNEL);
766
767 if (info == NULL) {
768 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
769 ret = -ENOMEM;
770 goto err;
771 }
772
773 for_each_child_of_node(node, child)
774 count++;
775
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700776 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600777
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700778 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600779 sizeof(struct msm_dcvs_freq_entry),
780 GFP_KERNEL);
781
782 if (info->freq_tbl == NULL) {
783 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700784 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600785 sizeof(struct msm_dcvs_freq_entry));
786 ret = -ENOMEM;
787 goto err;
788 }
789
790 for_each_child_of_node(node, child) {
791 unsigned int index;
792
793 if (adreno_of_read_property(child, "reg", &index))
794 goto err;
795
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700796 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600797 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
798 index);
799 continue;
800 }
801
802 if (adreno_of_read_property(child, "qcom,freq",
803 &info->freq_tbl[index].freq))
804 goto err;
805
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700806 if (adreno_of_read_property(child, "qcom,voltage",
807 &info->freq_tbl[index].voltage))
808 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600809
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700810 if (adreno_of_read_property(child, "qcom,is_trans_level",
811 &info->freq_tbl[index].is_trans_level))
812 info->freq_tbl[index].is_trans_level = 0;
813
814 if (adreno_of_read_property(child, "qcom,active-energy-offset",
815 &info->freq_tbl[index].active_energy_offset))
816 info->freq_tbl[index].active_energy_offset = 0;
817
818 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
819 &info->freq_tbl[index].leakage_energy_offset))
820 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600821 }
822
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700823 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
824 goto err;
825
826 info->sensors = kzalloc(info->num_cores *
827 sizeof(int),
828 GFP_KERNEL);
829
830 for (count = 0; count < info->num_cores; count++) {
831 if (adreno_of_read_property(node, "qcom,sensors",
832 &(info->sensors[count])))
833 goto err;
834 }
835
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700836 if (adreno_of_read_property(node, "qcom,core-core-type",
837 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600838 goto err;
839
840 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
841 &info->algo_param.disable_pc_threshold))
842 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700843 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
844 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600845 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700846 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
847 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600848 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600849 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
850 &info->algo_param.em_max_util_pct))
851 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700852 if (adreno_of_read_property(node, "qcom,algo-group-id",
853 &info->algo_param.group_id))
854 goto err;
855 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
856 &info->algo_param.max_freq_chg_time_us))
857 goto err;
858 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
859 &info->algo_param.slack_mode_dynamic))
860 goto err;
861 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
862 &info->algo_param.slack_weight_thresh_pct))
863 goto err;
864 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
865 &info->algo_param.slack_time_min_us))
866 goto err;
867 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
868 &info->algo_param.slack_time_max_us))
869 goto err;
870 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
871 &info->algo_param.ss_win_size_min_us))
872 goto err;
873 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
874 &info->algo_param.ss_win_size_max_us))
875 goto err;
876 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
877 &info->algo_param.ss_util_pct))
878 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800879 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
880 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600881 goto err;
882
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700883 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
884 &info->energy_coeffs.active_coeff_a))
885 goto err;
886 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
887 &info->energy_coeffs.active_coeff_b))
888 goto err;
889 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
890 &info->energy_coeffs.active_coeff_c))
891 goto err;
892 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
893 &info->energy_coeffs.leakage_coeff_a))
894 goto err;
895 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
896 &info->energy_coeffs.leakage_coeff_b))
897 goto err;
898 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
899 &info->energy_coeffs.leakage_coeff_c))
900 goto err;
901 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
902 &info->energy_coeffs.leakage_coeff_d))
903 goto err;
904
905 if (adreno_of_read_property(node, "qcom,power-current-temp",
906 &info->power_param.current_temp))
907 goto err;
908
Lokesh Batra805e1e12012-08-03 08:34:06 -0600909 return info;
910
911err:
912 if (info)
913 kfree(info->freq_tbl);
914
915 kfree(info);
916
917 return ERR_PTR(ret);
918}
919
920static int adreno_of_get_iommu(struct device_node *parent,
921 struct kgsl_device_platform_data *pdata)
922{
923 struct device_node *node, *child;
924 struct kgsl_device_iommu_data *data = NULL;
925 struct kgsl_iommu_ctx *ctxs = NULL;
926 u32 reg_val[2];
927 int ctx_index = 0;
928
929 node = of_parse_phandle(parent, "iommu", 0);
930 if (node == NULL)
931 return -EINVAL;
932
933 data = kzalloc(sizeof(*data), GFP_KERNEL);
934 if (data == NULL) {
935 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
936 goto err;
937 }
938
939 if (of_property_read_u32_array(node, "reg", reg_val, 2))
940 goto err;
941
942 data->physstart = reg_val[0];
943 data->physend = data->physstart + reg_val[1] - 1;
944
945 data->iommu_ctx_count = 0;
946
947 for_each_child_of_node(node, child)
948 data->iommu_ctx_count++;
949
950 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
951 GFP_KERNEL);
952
953 if (ctxs == NULL) {
954 KGSL_CORE_ERR("kzalloc(%d) failed\n",
955 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
956 goto err;
957 }
958
959 for_each_child_of_node(node, child) {
960 int ret = of_property_read_string(child, "label",
961 &ctxs[ctx_index].iommu_ctx_name);
962
963 if (ret) {
964 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
965 goto err;
966 }
967
968 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
969 &ctxs[ctx_index].ctx_id))
970 goto err;
971
972 ctx_index++;
973 }
974
975 data->iommu_ctxs = ctxs;
976
977 pdata->iommu_data = data;
978 pdata->iommu_count = 1;
979
980 return 0;
981
982err:
983 kfree(ctxs);
984 kfree(data);
985
986 return -EINVAL;
987}
988
989static int adreno_of_get_pdata(struct platform_device *pdev)
990{
991 struct kgsl_device_platform_data *pdata = NULL;
992 struct kgsl_device *device;
993 int ret = -EINVAL;
994
995 pdev->id_entry = adreno_id_table;
996
997 pdata = pdev->dev.platform_data;
998 if (pdata)
999 return 0;
1000
1001 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1002 KGSL_CORE_ERR("Unable to read 'label'\n");
1003 goto err;
1004 }
1005
1006 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1007 goto err;
1008
1009 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1010 if (pdata == NULL) {
1011 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1012 ret = -ENOMEM;
1013 goto err;
1014 }
1015
1016 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1017 &pdata->chipid))
1018 goto err;
1019
1020 /* pwrlevel Data */
1021 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1022 if (ret)
1023 goto err;
1024
1025 /* Default value is 83, if not found in DT */
1026 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1027 &pdata->idle_timeout))
1028 pdata->idle_timeout = 83;
1029
1030 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1031 &pdata->nap_allowed))
1032 pdata->nap_allowed = 1;
1033
1034 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1035 &pdata->clk_map))
1036 goto err;
1037
1038 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1039
1040 if (device->id != KGSL_DEVICE_3D0)
1041 goto err;
1042
1043 /* Bus Scale Data */
1044
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001045 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001046 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1047 ret = PTR_ERR(pdata->bus_scale_table);
1048 goto err;
1049 }
1050
1051 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1052 if (IS_ERR_OR_NULL(pdata->core_info)) {
1053 ret = PTR_ERR(pdata->core_info);
1054 goto err;
1055 }
1056
1057 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1058 if (ret)
1059 goto err;
1060
1061 pdev->dev.platform_data = pdata;
1062 return 0;
1063
1064err:
1065 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001066 if (pdata->core_info)
1067 kfree(pdata->core_info->freq_tbl);
1068 kfree(pdata->core_info);
1069
1070 if (pdata->iommu_data)
1071 kfree(pdata->iommu_data->iommu_ctxs);
1072
1073 kfree(pdata->iommu_data);
1074 }
1075
1076 kfree(pdata);
1077
1078 return ret;
1079}
1080
liu zhong7dfa2a32012-04-27 19:11:01 -07001081#ifdef CONFIG_MSM_OCMEM
1082static int
1083adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1084{
Jordan Crousec0978202012-08-29 14:35:51 -06001085 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001086 return 0;
1087
1088 /* OCMEM is only needed once, do not support consective allocation */
1089 if (adreno_dev->ocmem_hdl != NULL)
1090 return 0;
1091
1092 adreno_dev->ocmem_hdl =
1093 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1094 if (adreno_dev->ocmem_hdl == NULL)
1095 return -ENOMEM;
1096
1097 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001098 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001099
1100 return 0;
1101}
1102
1103static void
1104adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1105{
Jordan Crousec0978202012-08-29 14:35:51 -06001106 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001107 return;
1108
1109 if (adreno_dev->ocmem_hdl == NULL)
1110 return;
1111
1112 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1113 adreno_dev->ocmem_hdl = NULL;
1114}
1115#else
1116static int
1117adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1118{
1119 return 0;
1120}
1121
1122static void
1123adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1124{
1125}
1126#endif
1127
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001128static int __devinit
1129adreno_probe(struct platform_device *pdev)
1130{
1131 struct kgsl_device *device;
1132 struct adreno_device *adreno_dev;
1133 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001134 bool is_dt;
1135
1136 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1137
1138 if (is_dt && pdev->dev.of_node) {
1139 status = adreno_of_get_pdata(pdev);
1140 if (status)
1141 goto error_return;
1142 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143
1144 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1145 adreno_dev = ADRENO_DEVICE(device);
1146 device->parentdev = &pdev->dev;
1147
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148 status = adreno_ringbuffer_init(device);
1149 if (status != 0)
1150 goto error;
1151
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001152 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153 if (status)
1154 goto error_close_rb;
1155
1156 adreno_debugfs_init(device);
1157
1158 kgsl_pwrscale_init(device);
1159 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1160
1161 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1162 return 0;
1163
1164error_close_rb:
1165 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1166error:
1167 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001168error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169 return status;
1170}
1171
1172static int __devexit adreno_remove(struct platform_device *pdev)
1173{
1174 struct kgsl_device *device;
1175 struct adreno_device *adreno_dev;
1176
1177 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1178 adreno_dev = ADRENO_DEVICE(device);
1179
1180 kgsl_pwrscale_detach_policy(device);
1181 kgsl_pwrscale_close(device);
1182
1183 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1184 kgsl_device_platform_remove(device);
1185
1186 return 0;
1187}
1188
1189static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1190{
1191 int status = -EINVAL;
1192 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001194 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1195 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196
1197 /* Power up the device */
1198 kgsl_pwrctrl_enable(device);
1199
1200 /* Identify the specific GPU */
1201 adreno_identify_gpu(adreno_dev);
1202
Tarun Karra9c070822012-11-27 16:43:51 -07001203 if (adreno_ringbuffer_read_pm4_ucode(device)) {
1204 KGSL_DRV_ERR(device, "Reading pm4 microcode failed %s\n",
1205 adreno_dev->pm4_fwfile);
1206 BUG_ON(1);
1207 }
1208
1209 if (adreno_ringbuffer_read_pfp_ucode(device)) {
1210 KGSL_DRV_ERR(device, "Reading pfp microcode failed %s\n",
1211 adreno_dev->pfp_fwfile);
1212 BUG_ON(1);
1213 }
1214
Jordan Crouse505df9c2011-07-28 08:37:59 -06001215 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1216 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1217 adreno_dev->chip_id);
1218 goto error_clk_off;
1219 }
1220
Tarun Karra9c070822012-11-27 16:43:51 -07001221
1222 /*
1223 * Check if firmware supports the sync lock PM4 packets needed
1224 * for IOMMUv1
1225 */
1226
1227 if ((adreno_dev->pm4_fw_version >=
1228 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pm4_ver) &&
1229 (adreno_dev->pfp_fw_version >=
1230 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pfp_ver))
1231 device->mmu.flags |= KGSL_MMU_FLAGS_IOMMU_SYNC;
1232
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001233 /* Set up the MMU */
1234 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001235 /*
1236 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1237 * on older gpus
1238 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001239 if (adreno_is_a20x(adreno_dev)) {
1240 device->mh.mh_intf_cfg1 = 0;
1241 device->mh.mh_intf_cfg2 = 0;
1242 }
1243
1244 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001245 }
1246
Tarun Karra3335f142012-06-19 14:11:48 -07001247 /* Assign correct RBBM status register to hang detect regs
1248 */
1249 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1250
Jordan Crouseb5c80482012-10-03 09:38:41 -06001251 /* Add A3XX specific registers for hang detection */
1252 if (adreno_is_a3xx(adreno_dev)) {
1253 hang_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1254 hang_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
1255 }
1256
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001257 status = kgsl_mmu_start(device);
1258 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 goto error_clk_off;
1260
liu zhong7dfa2a32012-04-27 19:11:01 -07001261 status = adreno_ocmem_gmem_malloc(adreno_dev);
1262 if (status) {
1263 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1264 goto error_mmu_off;
1265 }
1266
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001267 /* Start the GPU */
1268 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269
1270 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001271 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272
1273 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001274 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001275 /* While recovery is on we do not want timer to
1276 * fire and attempt to change any device state */
1277 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1278 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001279 return 0;
1280 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001282 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001283
1284error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001285 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001286
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001287error_clk_off:
1288 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289
1290 return status;
1291}
1292
1293static int adreno_stop(struct kgsl_device *device)
1294{
1295 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1296
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297 adreno_dev->drawctxt_active = NULL;
1298
1299 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1300
Shubhraprakash Das79447952012-04-26 18:12:23 -06001301 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001303 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301304 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001305 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001306
liu zhong7dfa2a32012-04-27 19:11:01 -07001307 adreno_ocmem_gmem_free(adreno_dev);
1308
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309 /* Power down the device */
1310 kgsl_pwrctrl_disable(device);
1311
1312 return 0;
1313}
1314
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001315static void adreno_mark_context_status(struct kgsl_device *device,
1316 int recovery_status)
1317{
1318 struct kgsl_context *context;
1319 int next = 0;
1320 /*
1321 * Set the reset status of all contexts to
1322 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1323 * since thats the guilty party, if recovery failed then
1324 * mark all as guilty
1325 */
1326 while ((context = idr_get_next(&device->context_idr, &next))) {
1327 struct adreno_context *adreno_context = context->devctxt;
1328 if (recovery_status) {
1329 context->reset_status =
1330 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1331 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1332 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1333 context->reset_status) {
1334 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1335 CTXT_FLAGS_GPU_HANG_RECOVERED))
1336 context->reset_status =
1337 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1338 else
1339 context->reset_status =
1340 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1341 }
1342 next = next + 1;
1343 }
1344}
1345
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001346static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1347{
1348 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1349 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1350 struct kgsl_context *context;
1351 struct adreno_context *temp_adreno_context;
1352 int next = 0;
1353
1354 while ((context = idr_get_next(&device->context_idr, &next))) {
1355 temp_adreno_context = context->devctxt;
1356 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1357 kgsl_sharedmem_writel(&device->memstore,
1358 KGSL_MEMSTORE_OFFSET(context->id,
1359 soptimestamp),
1360 rb->timestamp[context->id]);
1361 kgsl_sharedmem_writel(&device->memstore,
1362 KGSL_MEMSTORE_OFFSET(context->id,
1363 eoptimestamp),
1364 rb->timestamp[context->id]);
1365 }
1366 next = next + 1;
1367 }
1368}
1369
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001370static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1371{
1372 vfree(rec_data->rb_buffer);
1373 vfree(rec_data->bad_rb_buffer);
1374}
1375
1376static int adreno_setup_recovery_data(struct kgsl_device *device,
1377 struct adreno_recovery_data *rec_data)
1378{
1379 int ret = 0;
1380 unsigned int ib1_sz, ib2_sz;
1381 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1382 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1383
1384 memset(rec_data, 0, sizeof(*rec_data));
1385
1386 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1387 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1388 if (ib1_sz || ib2_sz)
1389 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1390
1391 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1392 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1393 current_context));
1394
1395 kgsl_sharedmem_readl(&device->memstore,
1396 &rec_data->global_eop,
1397 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1398 eoptimestamp));
1399
1400 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1401 if (!rec_data->rb_buffer) {
1402 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1403 rb->buffer_desc.size);
1404 return -ENOMEM;
1405 }
1406
1407 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1408 if (!rec_data->bad_rb_buffer) {
1409 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1410 rb->buffer_desc.size);
1411 ret = -ENOMEM;
1412 goto done;
1413 }
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001414 rec_data->fault = device->mmu.fault;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001415
1416done:
1417 if (ret) {
1418 vfree(rec_data->rb_buffer);
1419 vfree(rec_data->bad_rb_buffer);
1420 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001421 return ret;
1422}
1423
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001424static int
1425_adreno_recover_hang(struct kgsl_device *device,
1426 struct adreno_recovery_data *rec_data,
1427 bool try_bad_commands)
1428{
1429 int ret;
1430 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1431 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1432 struct kgsl_context *context;
1433 struct adreno_context *adreno_context = NULL;
1434 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1435
1436 context = idr_find(&device->context_idr, rec_data->context_id);
1437 if (context == NULL) {
1438 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1439 rec_data->context_id);
1440 } else {
1441 adreno_context = context->devctxt;
1442 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001443 /*
1444 * set the invalid ts flag to 0 for this context since we have
1445 * detected a hang for it
1446 */
1447 context->wait_on_invalid_ts = false;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001448 }
1449
1450 /* Extract valid contents from rb which can still be executed after
1451 * hang */
1452 ret = adreno_ringbuffer_extract(rb, rec_data);
1453 if (ret)
1454 goto done;
1455
1456 /* restart device */
1457 ret = adreno_stop(device);
1458 if (ret) {
1459 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1460 goto done;
1461 }
1462
1463 ret = adreno_start(device, true);
1464 if (ret) {
1465 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1466 goto done;
1467 }
1468
1469 if (context)
1470 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1471 KGSL_MEMSTORE_GLOBAL);
1472
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001473 /* If iommu is used then we need to make sure that the iommu clocks
1474 * are on since there could be commands in pipeline that touch iommu */
1475 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1476 ret = kgsl_mmu_enable_clk(&device->mmu,
1477 KGSL_IOMMU_CONTEXT_USER);
1478 if (ret)
1479 goto done;
1480 }
1481
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001482 /* Do not try the bad commands if recovery has failed bad commands
1483 * once already or if hang is due to a fault */
1484 if (!try_bad_commands || rec_data->fault)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001485 rec_data->bad_rb_size = 0;
1486
1487 if (rec_data->bad_rb_size) {
1488 int idle_ret;
1489 /* submit the bad and good context commands and wait for
1490 * them to pass */
1491 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1492 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001493 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001494 if (idle_ret) {
1495 ret = adreno_stop(device);
1496 if (ret) {
1497 KGSL_DRV_ERR(device,
1498 "Device stop failed in recovery\n");
1499 goto done;
1500 }
1501 ret = adreno_start(device, true);
1502 if (ret) {
1503 KGSL_DRV_ERR(device,
1504 "Device start failed in recovery\n");
1505 goto done;
1506 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001507 if (context)
1508 kgsl_mmu_setstate(&device->mmu,
1509 adreno_context->pagetable,
1510 KGSL_MEMSTORE_GLOBAL);
1511
1512 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1513 ret = kgsl_mmu_enable_clk(&device->mmu,
1514 KGSL_IOMMU_CONTEXT_USER);
1515 if (ret)
1516 goto done;
1517 }
1518
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001519 ret = idle_ret;
1520 KGSL_DRV_ERR(device,
1521 "Bad context commands hung in recovery\n");
1522 } else {
1523 KGSL_DRV_ERR(device,
1524 "Bad context commands succeeded in recovery\n");
1525 if (adreno_context)
1526 adreno_context->flags = (adreno_context->flags &
1527 ~CTXT_FLAGS_GPU_HANG) |
1528 CTXT_FLAGS_GPU_HANG_RECOVERED;
1529 adreno_dev->drawctxt_active = last_active_ctx;
1530 }
1531 }
1532 /* If either the bad command sequence failed or we did not play it */
1533 if (ret || !rec_data->bad_rb_size) {
1534 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1535 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001536 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001537 if (ret) {
1538 /* If we fail here we can try to invalidate another
1539 * context and try recovering again */
1540 ret = -EAGAIN;
1541 goto done;
1542 }
1543 /* ringbuffer now has data from the last valid context id,
1544 * so restore the active_ctx to the last valid context */
1545 if (rec_data->last_valid_ctx_id) {
1546 struct kgsl_context *last_ctx =
1547 idr_find(&device->context_idr,
1548 rec_data->last_valid_ctx_id);
1549 if (last_ctx)
1550 adreno_dev->drawctxt_active = last_ctx->devctxt;
1551 }
1552 }
1553done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001554 /* Turn off iommu clocks */
1555 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1556 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001557 return ret;
1558}
1559
1560static int
1561adreno_recover_hang(struct kgsl_device *device,
1562 struct adreno_recovery_data *rec_data)
1563{
1564 int ret = 0;
1565 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1566 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1567 unsigned int timestamp;
1568
1569 KGSL_DRV_ERR(device,
1570 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1571 "Bad context_id: %u, global_eop: 0x%x\n",
1572 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1573
1574 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1575 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1576
1577 /* We may need to replay commands multiple times based on whether
1578 * multiple contexts hang the GPU */
1579 while (true) {
1580 if (!ret)
1581 ret = _adreno_recover_hang(device, rec_data, true);
1582 else
1583 ret = _adreno_recover_hang(device, rec_data, false);
1584
1585 if (-EAGAIN == ret) {
1586 /* setup new recovery parameters and retry, this
1587 * means more than 1 contexts are causing hang */
1588 adreno_destroy_recovery_data(rec_data);
1589 adreno_setup_recovery_data(device, rec_data);
1590 KGSL_DRV_ERR(device,
1591 "Retry recovery from 3D GPU hang. Recovery parameters: "
1592 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1593 rec_data->ib1, rec_data->context_id,
1594 rec_data->global_eop);
1595 } else {
1596 break;
1597 }
1598 }
1599
1600 if (ret)
1601 goto done;
1602
1603 /* Restore correct states after recovery */
1604 if (adreno_dev->drawctxt_active)
1605 device->mmu.hwpagetable =
1606 adreno_dev->drawctxt_active->pagetable;
1607 else
1608 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1609 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1610 kgsl_sharedmem_writel(&device->memstore,
1611 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1612 eoptimestamp),
1613 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1614done:
1615 adreno_set_max_ts_for_bad_ctxs(device);
1616 adreno_mark_context_status(device, ret);
1617 if (!ret)
1618 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1619 else
1620 KGSL_DRV_ERR(device, "Recovery failed\n");
1621 return ret;
1622}
1623
1624int
1625adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001626{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001627 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001628 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001629
1630 if (device->state == KGSL_STATE_HUNG)
1631 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001632 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001633 mutex_unlock(&device->mutex);
1634 wait_for_completion(&device->recovery_gate);
1635 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001636 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637 result = 0;
1638 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001639 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001640 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001641 /* Detected a hang */
1642
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001643 /* Get the recovery data as soon as hang is detected */
1644 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001645 /*
1646 * Trigger an automatic dump of the state to
1647 * the console
1648 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001649 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001650
1651 /*
1652 * Make a GPU snapshot. For now, do it after the PM dump so we
1653 * can at least be sure the PM dump will work as it always has
1654 */
1655 kgsl_device_snapshot(device, 1);
1656
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001657 result = adreno_recover_hang(device, &rec_data);
1658 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001659 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001660 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001661 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001662 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001663 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1664 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001665 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666 }
1667done:
1668 return result;
1669}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001670EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001671
1672static int adreno_getproperty(struct kgsl_device *device,
1673 enum kgsl_property_type type,
1674 void *value,
1675 unsigned int sizebytes)
1676{
1677 int status = -EINVAL;
1678 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1679
1680 switch (type) {
1681 case KGSL_PROP_DEVICE_INFO:
1682 {
1683 struct kgsl_devinfo devinfo;
1684
1685 if (sizebytes != sizeof(devinfo)) {
1686 status = -EINVAL;
1687 break;
1688 }
1689
1690 memset(&devinfo, 0, sizeof(devinfo));
1691 devinfo.device_id = device->id+1;
1692 devinfo.chip_id = adreno_dev->chip_id;
1693 devinfo.mmu_enabled = kgsl_mmu_enabled();
1694 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001695 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1696 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001697
1698 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1699 0) {
1700 status = -EFAULT;
1701 break;
1702 }
1703 status = 0;
1704 }
1705 break;
1706 case KGSL_PROP_DEVICE_SHADOW:
1707 {
1708 struct kgsl_shadowprop shadowprop;
1709
1710 if (sizebytes != sizeof(shadowprop)) {
1711 status = -EINVAL;
1712 break;
1713 }
1714 memset(&shadowprop, 0, sizeof(shadowprop));
1715 if (device->memstore.hostptr) {
1716 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1717 * anything to mmap().
1718 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001719 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001720 shadowprop.size = device->memstore.size;
1721 /* GSL needs this to be set, even if it
1722 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001723 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1724 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001725 }
1726 if (copy_to_user(value, &shadowprop,
1727 sizeof(shadowprop))) {
1728 status = -EFAULT;
1729 break;
1730 }
1731 status = 0;
1732 }
1733 break;
1734 case KGSL_PROP_MMU_ENABLE:
1735 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001736 int mmu_prop = kgsl_mmu_enabled();
1737
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001738 if (sizebytes != sizeof(int)) {
1739 status = -EINVAL;
1740 break;
1741 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001742 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001743 status = -EFAULT;
1744 break;
1745 }
1746 status = 0;
1747 }
1748 break;
1749 case KGSL_PROP_INTERRUPT_WAITS:
1750 {
1751 int int_waits = 1;
1752 if (sizebytes != sizeof(int)) {
1753 status = -EINVAL;
1754 break;
1755 }
1756 if (copy_to_user(value, &int_waits, sizeof(int))) {
1757 status = -EFAULT;
1758 break;
1759 }
1760 status = 0;
1761 }
1762 break;
1763 default:
1764 status = -EINVAL;
1765 }
1766
1767 return status;
1768}
1769
Jordan Crousef7370f82012-04-18 09:31:07 -06001770static int adreno_setproperty(struct kgsl_device *device,
1771 enum kgsl_property_type type,
1772 void *value,
1773 unsigned int sizebytes)
1774{
1775 int status = -EINVAL;
1776
1777 switch (type) {
1778 case KGSL_PROP_PWRCTRL: {
1779 unsigned int enable;
1780 struct kgsl_device_platform_data *pdata =
1781 kgsl_device_get_drvdata(device);
1782
1783 if (sizebytes != sizeof(enable))
1784 break;
1785
1786 if (copy_from_user(&enable, (void __user *) value,
1787 sizeof(enable))) {
1788 status = -EFAULT;
1789 break;
1790 }
1791
1792 if (enable) {
1793 if (pdata->nap_allowed)
1794 device->pwrctrl.nap_allowed = true;
1795
1796 kgsl_pwrscale_enable(device);
1797 } else {
1798 device->pwrctrl.nap_allowed = false;
1799 kgsl_pwrscale_disable(device);
1800 }
1801
1802 status = 0;
1803 }
1804 break;
1805 default:
1806 break;
1807 }
1808
1809 return status;
1810}
1811
Jordan Crousea29a2e02012-08-14 09:09:23 -06001812static int adreno_ringbuffer_drain(struct kgsl_device *device,
1813 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001814{
1815 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1816 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001817 unsigned long wait;
1818 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1819
1820 if (!(rb->flags & KGSL_FLAGS_STARTED))
1821 return 0;
1822
1823 /*
1824 * The first time into the loop, wait for 100 msecs and kick wptr again
1825 * to ensure that the hardware has updated correctly. After that, kick
1826 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1827 * expires
1828 */
1829
1830 wait = jiffies + msecs_to_jiffies(100);
1831
Jordan Crousea29a2e02012-08-14 09:09:23 -06001832 do {
1833 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001834 /* Check to see if the core is hung */
1835 if (adreno_hang_detect(device, regs))
1836 return -ETIMEDOUT;
1837
1838 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1839 }
1840 GSL_RB_GET_READPTR(rb, &rb->rptr);
1841
1842 if (time_after(jiffies, timeout)) {
1843 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1844 rb->rptr, rb->wptr);
1845 return -ETIMEDOUT;
1846 }
1847 } while (rb->rptr != rb->wptr);
1848
1849 return 0;
1850}
1851
1852/* Caller must hold the device mutex. */
1853int adreno_idle(struct kgsl_device *device)
1854{
1855 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001856 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301857 unsigned long wait_time;
1858 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001859 unsigned int prev_reg_val[hang_detect_regs_count];
1860
1861 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001862
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001863 kgsl_cffdump_regpoll(device->id,
1864 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001865 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001866
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001867retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001868 /* First, wait for the ringbuffer to drain */
1869 if (adreno_ringbuffer_drain(device, prev_reg_val))
1870 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871
1872 /* now, wait for the GPU to finish its operations */
Jordan Crousea29a2e02012-08-14 09:09:23 -06001873 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
1874 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1875
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001876 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001877 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1878 &rbbm_status);
1879 if (adreno_is_a2xx(adreno_dev)) {
1880 if (rbbm_status == 0x110)
1881 return 0;
1882 } else {
1883 if (!(rbbm_status & 0x80000000))
1884 return 0;
1885 }
Tarun Karra3335f142012-06-19 14:11:48 -07001886
1887 /* Dont wait for timeout, detect hang faster.
1888 */
1889 if (time_after(jiffies, wait_time_part)) {
1890 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001891 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001892 if ((adreno_hang_detect(device, prev_reg_val)))
1893 goto err;
1894 }
1895
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001896 }
1897
1898err:
1899 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001900 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1901 !adreno_dump_and_recover(device)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001902 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001903 goto retry;
1904 }
1905 return -ETIMEDOUT;
1906}
1907
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08001908/**
1909 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
1910 * rbbm_status register
1911 * @device - Pointer to the GPU device whose idle status is to be
1912 * checked
1913 * @returns - Returns whether the core is idle (based on rbbm_status)
1914 * false if the core is active, true if the core is idle
1915 */
1916static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
1917{
1918 unsigned int reg_rbbm_status;
1919 bool status = false;
1920 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1921
1922 /* Is the core idle? */
1923 adreno_regread(device,
1924 adreno_dev->gpudev->reg_rbbm_status,
1925 &reg_rbbm_status);
1926
1927 if (adreno_is_a2xx(adreno_dev)) {
1928 if (reg_rbbm_status == 0x110)
1929 status = true;
1930 } else {
1931 if (!(reg_rbbm_status & 0x80000000))
1932 status = true;
1933 }
1934 return status;
1935}
1936
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001937static unsigned int adreno_isidle(struct kgsl_device *device)
1938{
1939 int status = false;
1940 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1941 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001942
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001943 WARN_ON(device->state == KGSL_STATE_INIT);
1944 /* If the device isn't active, don't force it on. */
1945 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001946 /* Is the ring buffer is empty? */
1947 GSL_RB_GET_READPTR(rb, &rb->rptr);
1948 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1949 /* Is the core idle? */
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08001950 status = is_adreno_rbbm_status_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001951 }
1952 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001953 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001954 }
1955 return status;
1956}
1957
1958/* Caller must hold the device mutex. */
1959static int adreno_suspend_context(struct kgsl_device *device)
1960{
1961 int status = 0;
1962 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1963
1964 /* switch to NULL ctxt */
1965 if (adreno_dev->drawctxt_active != NULL) {
1966 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001967 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001968 }
1969
1970 return status;
1971}
1972
Jordan Crouse233b2092012-04-18 09:31:09 -06001973/* Find a memory structure attached to an adreno context */
1974
1975struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1976 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1977{
1978 struct kgsl_context *context;
1979 struct adreno_context *adreno_context = NULL;
1980 int next = 0;
1981
1982 while (1) {
1983 context = idr_get_next(&device->context_idr, &next);
1984 if (context == NULL)
1985 break;
1986
1987 adreno_context = (struct adreno_context *)context->devctxt;
1988
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07001989 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
1990 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06001991 struct kgsl_memdesc *desc;
1992
1993 desc = &adreno_context->gpustate;
1994 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1995 return desc;
1996
1997 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1998 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1999 return desc;
2000 }
2001 next = next + 1;
2002 }
2003
2004 return NULL;
2005}
2006
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002007struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002008 unsigned int pt_base,
2009 unsigned int gpuaddr,
2010 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002011{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002012 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002013 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2014 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2015
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002016 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2017 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002018
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002019 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2020 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002021
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002022 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2023 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002024
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002025 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2026 size))
2027 return &device->mmu.setstate_memory;
2028
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002029 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002030
2031 if (entry)
2032 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002033
Jordan Crouse233b2092012-04-18 09:31:09 -06002034 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002035}
2036
2037uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2038 unsigned int gpuaddr, unsigned int size)
2039{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002040 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002041
2042 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2043
2044 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002045}
2046
2047void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2048 unsigned int *value)
2049{
2050 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002051 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2052 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002053
2054 if (!in_interrupt())
2055 kgsl_pre_hwaccess(device);
2056
2057 /*ensure this read finishes before the next one.
2058 * i.e. act like normal readl() */
2059 *value = __raw_readl(reg);
2060 rmb();
2061}
2062
2063void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2064 unsigned int value)
2065{
2066 unsigned int *reg;
2067
Jordan Crouse7501d452012-04-19 08:58:44 -06002068 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002069
2070 if (!in_interrupt())
2071 kgsl_pre_hwaccess(device);
2072
2073 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002074 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002075
2076 /*ensure previous writes post before this one,
2077 * i.e. act like normal writel() */
2078 wmb();
2079 __raw_writel(value, reg);
2080}
2081
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002082static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2083{
2084 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002085 if (k_ctxt != NULL) {
2086 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002087 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2088 context_id = KGSL_CONTEXT_INVALID;
2089 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2090 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002091 }
2092
2093 return context_id;
2094}
2095
Jordan Crouse313faf62012-11-20 15:12:28 -07002096static void adreno_next_event(struct kgsl_device *device,
2097 struct kgsl_event *event)
2098{
2099 int status;
2100 unsigned int ref_ts, enableflag;
2101 unsigned int context_id = _get_context_id(event->context);
2102 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2103
2104 status = kgsl_check_timestamp(device, event->context, event->timestamp);
2105 if (!status) {
2106 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2107 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2108 /*
2109 * Barrier is needed here to make sure the read from memstore
2110 * has posted
2111 */
2112
2113 mb();
2114
2115 if (enableflag) {
2116 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
2117 KGSL_MEMSTORE_OFFSET(context_id,
2118 ref_wait_ts));
2119
2120 /* Make sure the memstore read has posted */
2121 mb();
2122 if (timestamp_cmp(ref_ts, event->timestamp) >= 0) {
2123 kgsl_sharedmem_writel(&device->memstore,
2124 KGSL_MEMSTORE_OFFSET(context_id,
2125 ref_wait_ts), event->timestamp);
2126 /* Make sure the memstore write is posted */
2127 wmb();
2128 }
2129 } else {
2130 unsigned int cmds[2];
2131 kgsl_sharedmem_writel(&device->memstore,
2132 KGSL_MEMSTORE_OFFSET(context_id,
2133 ref_wait_ts), event->timestamp);
2134 enableflag = 1;
2135 kgsl_sharedmem_writel(&device->memstore,
2136 KGSL_MEMSTORE_OFFSET(context_id,
2137 ts_cmp_enable), enableflag);
2138
2139 /* Make sure the memstore write gets posted */
2140 wmb();
2141
2142 /*
2143 * submit a dummy packet so that even if all
2144 * commands upto timestamp get executed we will still
2145 * get an interrupt
2146 */
2147 cmds[0] = cp_type3_packet(CP_NOP, 1);
2148 cmds[1] = 0;
2149
2150 if (adreno_dev->drawctxt_active)
2151 adreno_ringbuffer_issuecmds_intr(device,
2152 event->context, &cmds[0], 2);
2153 }
2154 }
2155}
2156
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002157static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002158 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002159{
2160 int status;
2161 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002162 unsigned int context_id;
2163
2164 mutex_lock(&device->mutex);
2165 context_id = _get_context_id(context);
2166 /*
2167 * If the context ID is invalid, we are in a race with
2168 * the context being destroyed by userspace so bail.
2169 */
2170 if (context_id == KGSL_CONTEXT_INVALID) {
2171 KGSL_DRV_WARN(device, "context was detached");
2172 status = -EINVAL;
2173 goto unlock;
2174 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002175
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002176 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002177 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002179 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002180 mb();
2181
2182 if (enableflag) {
2183 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002184 KGSL_MEMSTORE_OFFSET(context_id,
2185 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002187 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002188 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002189 KGSL_MEMSTORE_OFFSET(context_id,
2190 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002191 wmb();
2192 }
2193 } else {
2194 unsigned int cmds[2];
2195 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002196 KGSL_MEMSTORE_OFFSET(context_id,
2197 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002198 enableflag = 1;
2199 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002200 KGSL_MEMSTORE_OFFSET(context_id,
2201 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002202 wmb();
2203 /* submit a dummy packet so that even if all
2204 * commands upto timestamp get executed we will still
2205 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002206 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002207 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002208
Vladimir Razgulin38345302013-01-22 18:41:59 -07002209 if (context && device->state != KGSL_STATE_SLUMBER)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002210 adreno_ringbuffer_issuecmds_intr(device,
2211 context, &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002212 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002213 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002214unlock:
2215 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002216
2217 return status;
2218}
2219
2220/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002221 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002222 placing a process in wait q. For conditional interrupts we expect the
2223 process to already be in its wait q when its exit condition checking
2224 function is called.
2225*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002226#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002227({ \
2228 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002229 if (io) \
2230 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2231 else \
2232 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002233 __ret; \
2234})
2235
Tarun Karra3335f142012-06-19 14:11:48 -07002236
2237
2238unsigned int adreno_hang_detect(struct kgsl_device *device,
2239 unsigned int *prev_reg_val)
2240{
2241 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2242 unsigned int curr_reg_val[hang_detect_regs_count];
2243 unsigned int hang_detected = 1;
2244 unsigned int i;
2245
2246 if (!adreno_dev->fast_hang_detect)
2247 return 0;
2248
Jordan Crousecca61142012-11-20 10:54:24 -07002249 if (is_adreno_rbbm_status_idle(device)) {
2250
2251 /*
2252 * On A20X if the RPTR != WPTR and the device is idle, then
2253 * the last write to WPTR probably failed to latch so write it
2254 * again
2255 */
2256
2257 if (adreno_is_a2xx(adreno_dev)) {
2258 unsigned int rptr;
2259 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2260 if (rptr != adreno_dev->ringbuffer.wptr)
2261 adreno_regwrite(device, REG_CP_RB_WPTR,
2262 adreno_dev->ringbuffer.wptr);
2263 }
2264
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002265 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002266 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002267
Tarun Karra3335f142012-06-19 14:11:48 -07002268 for (i = 0; i < hang_detect_regs_count; i++) {
Jordan Crouseb5c80482012-10-03 09:38:41 -06002269
2270 if (hang_detect_regs[i] == 0)
2271 continue;
2272
Tarun Karra3335f142012-06-19 14:11:48 -07002273 adreno_regread(device, hang_detect_regs[i],
2274 &curr_reg_val[i]);
2275 if (curr_reg_val[i] != prev_reg_val[i]) {
2276 prev_reg_val[i] = curr_reg_val[i];
2277 hang_detected = 0;
2278 }
2279 }
2280
2281 return hang_detected;
2282}
2283
Jordan Crouse92446a62012-11-15 11:00:06 -07002284/**
2285 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2286 * @device - pointer to a KGSL device structure
2287 * @context - pointer to the active KGSL context
2288 * @timestamp - the timestamp that the process was waiting for
2289 *
2290 * Process a possible GPU hang and try to recover from it cleanly
2291 */
2292static int adreno_handle_hang(struct kgsl_device *device,
2293 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002294{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002295 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002296 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002297 unsigned int ts_issued;
Tarun Karra3335f142012-06-19 14:11:48 -07002298
Jordan Crouse92446a62012-11-15 11:00:06 -07002299 /* Do one last check to see if we somehow made it through */
2300 if (kgsl_check_timestamp(device, context, timestamp))
2301 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002302
2303 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002304
Jeremy Gebben63904832012-02-07 16:10:55 -07002305 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002306 "Device hang detected while waiting for timestamp: "
2307 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2308 "wptr: 0x%x\n",
2309 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002310 adreno_dev->ringbuffer.wptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002311
2312 /* Return 0 after a successful recovery */
2313 if (!adreno_dump_and_recover(device))
2314 return 0;
2315
2316 return -ETIMEDOUT;
2317}
2318
2319static int _check_pending_timestamp(struct kgsl_device *device,
2320 struct kgsl_context *context, unsigned int timestamp)
2321{
2322 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2323 unsigned int context_id = _get_context_id(context);
2324 unsigned int ts_issued;
2325
2326 if (context_id == KGSL_CONTEXT_INVALID)
2327 return -EINVAL;
2328
2329 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2330
2331 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2332 return 0;
2333
2334 if (context && !context->wait_on_invalid_ts) {
2335 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2336 context_id, timestamp, context_id, ts_issued);
2337
2338 /* Only print this message once */
2339 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002340 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002341
2342 return -EINVAL;
2343}
2344
2345/**
2346 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2347 * @device - pointer to a KGSL device structure
2348 * @context - pointer to the active kgsl context
2349 * @timestamp - GPU timestamp to wait for
2350 * @msecs - amount of time to wait (in milliseconds)
2351 *
2352 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2353 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2354 * one if it happened. Otherwise, spend most of our time in an interruptible
2355 * wait for the timestamp interrupt to be processed. This function must be
2356 * called with the mutex already held.
2357 */
2358static int adreno_waittimestamp(struct kgsl_device *device,
2359 struct kgsl_context *context,
2360 unsigned int timestamp,
2361 unsigned int msecs)
2362{
2363 static unsigned int io_cnt;
2364 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2365 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2366 unsigned int context_id = _get_context_id(context);
2367 unsigned int prev_reg_val[hang_detect_regs_count];
2368 unsigned int time_elapsed = 0;
2369 unsigned int wait;
2370 int ts_compare = 1;
2371 int io, ret = -ETIMEDOUT;
2372
2373 /* Get out early if the context has already been destroyed */
2374
2375 if (context_id == KGSL_CONTEXT_INVALID) {
2376 KGSL_DRV_WARN(device, "context was detached");
2377 return -EINVAL;
2378 }
2379
2380 /*
2381 * Check to see if the requested timestamp is "newer" then the last
2382 * timestamp issued. If it is complain once and return error. Only
2383 * print the message once per context so that badly behaving
2384 * applications don't spam the logs
2385 */
2386
2387 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2388 if (_check_pending_timestamp(device, context, timestamp))
2389 return -EINVAL;
2390
2391 /* Reset the invalid timestamp flag on a valid wait */
2392 context->wait_on_invalid_ts = false;
2393 }
2394
2395
2396 /* Clear the registers used for hang detection */
2397 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2398
2399 /*
2400 * On the first time through the loop only wait 100ms.
2401 * this gives enough time for the engine to start moving and oddly
2402 * provides better hang detection results than just going the full
2403 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2404 * is if msecs happens to be < 100ms then just use the full timeout
2405 */
2406
2407 wait = 100;
2408
2409 do {
2410 long status;
2411
2412 if (wait > (msecs - time_elapsed))
2413 wait = msecs - time_elapsed;
2414
2415 /*
2416 * if the timestamp happens while we're not
2417 * waiting, there's a chance that an interrupt
2418 * will not be generated and thus the timestamp
2419 * work needs to be queued.
2420 */
2421
2422 if (kgsl_check_timestamp(device, context, timestamp)) {
2423 queue_work(device->work_queue, &device->ts_expired_ws);
2424 ret = 0;
2425 break;
2426 }
2427
2428 /* Check to see if the GPU is hung */
2429 if (adreno_hang_detect(device, prev_reg_val)) {
2430 ret = adreno_handle_hang(device, context, timestamp);
2431 break;
2432 }
2433
2434 /*
2435 * For proper power accounting sometimes we need to call
2436 * io_wait_interruptible_timeout and sometimes we need to call
2437 * plain old wait_interruptible_timeout. We call the regular
2438 * timeout N times out of 100, where N is a number specified by
2439 * the current power level
2440 */
2441
2442 io_cnt = (io_cnt + 1) % 100;
2443 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2444 ? 0 : 1;
2445
2446 mutex_unlock(&device->mutex);
2447
2448 /* Wait for a timestamp event */
2449 status = kgsl_wait_event_interruptible_timeout(
2450 device->wait_queue,
2451 kgsl_check_interrupt_timestamp(device, context,
2452 timestamp), msecs_to_jiffies(wait), io);
2453
2454 mutex_lock(&device->mutex);
2455
2456 /*
2457 * If status is non zero then either the condition was satisfied
2458 * or there was an error. In either event, this is the end of
2459 * the line for us
2460 */
2461
2462 if (status != 0) {
2463 ret = (status > 0) ? 0 : (int) status;
2464 break;
2465 }
2466
2467 time_elapsed += wait;
2468
2469 /* If user specified timestamps are being used, wait at least
2470 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
2471 * issue a IB for a timestamp before checking to see if the
2472 * current timestamp we are waiting for is valid or not
2473 */
2474
2475 if (ts_compare && (adreno_ctx &&
2476 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
2477 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
2478 ret = _check_pending_timestamp(device, context,
2479 timestamp);
2480 if (ret)
2481 break;
2482
2483 /* Don't do this check again */
2484 ts_compare = 0;
2485
2486 /*
2487 * Reset the invalid timestamp flag on a valid
2488 * wait
2489 */
2490 context->wait_on_invalid_ts = false;
2491 }
2492 }
2493
2494 /*
2495 * all subsequent trips through the loop wait the full
2496 * KGSL_TIMEOUT_PART interval
2497 */
2498 wait = KGSL_TIMEOUT_PART;
2499
2500 } while (!msecs || time_elapsed < msecs);
2501
2502 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002503}
2504
2505static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002506 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002507{
2508 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002509 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002510
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002511 /*
2512 * If the context ID is invalid, we are in a race with
2513 * the context being destroyed by userspace so bail.
2514 */
2515 if (context_id == KGSL_CONTEXT_INVALID) {
2516 KGSL_DRV_WARN(device, "context was detached");
2517 return timestamp;
2518 }
Jordan Crousec659f382012-04-16 11:10:41 -06002519 switch (type) {
2520 case KGSL_TIMESTAMP_QUEUED: {
2521 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2522 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2523
2524 timestamp = rb->timestamp[context_id];
2525 break;
2526 }
2527 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002528 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002529 break;
2530 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002531 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002532 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2533 break;
2534 }
2535
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002536 rmb();
2537
2538 return timestamp;
2539}
2540
2541static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2542 unsigned int cmd, void *data)
2543{
2544 int result = 0;
2545 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2546 struct kgsl_context *context;
2547
2548 switch (cmd) {
2549 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2550 binbase = data;
2551
2552 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2553 if (context) {
2554 adreno_drawctxt_set_bin_base_offset(
2555 dev_priv->device, context, binbase->offset);
2556 } else {
2557 result = -EINVAL;
2558 KGSL_DRV_ERR(dev_priv->device,
2559 "invalid drawctxt drawctxt_id %d "
2560 "device_id=%d\n",
2561 binbase->drawctxt_id, dev_priv->device->id);
2562 }
2563 break;
2564
2565 default:
2566 KGSL_DRV_INFO(dev_priv->device,
2567 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002568 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002569 break;
2570 }
2571 return result;
2572
2573}
2574
2575static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2576{
2577 gpu_freq /= 1000000;
2578 return ticks / gpu_freq;
2579}
2580
2581static void adreno_power_stats(struct kgsl_device *device,
2582 struct kgsl_power_stats *stats)
2583{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002584 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002585 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002586 unsigned int cycles;
2587
2588 /* Get the busy cycles counted since the counter was last reset */
2589 /* Calling this function also resets and restarts the counter */
2590
2591 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002592
2593 /* In order to calculate idle you have to have run the algorithm *
2594 * at least once to get a start time. */
2595 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002596 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002597 stats->total_time = tmp - pwr->time;
2598 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002599 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600 pwrlevels[device->pwrctrl.active_pwrlevel].
2601 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002602 } else {
2603 stats->total_time = 0;
2604 stats->busy_time = 0;
2605 pwr->time = ktime_to_us(ktime_get());
2606 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002607}
2608
2609void adreno_irqctrl(struct kgsl_device *device, int state)
2610{
Jordan Crousea78c9172011-07-11 13:14:09 -06002611 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2612 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002613}
2614
Jordan Croused6535882012-06-20 08:22:16 -06002615static unsigned int adreno_gpuid(struct kgsl_device *device,
2616 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002617{
2618 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2619
Jordan Croused6535882012-06-20 08:22:16 -06002620 /* Some applications need to know the chip ID too, so pass
2621 * that as a parameter */
2622
2623 if (chipid != NULL)
2624 *chipid = adreno_dev->chip_id;
2625
Jordan Crousea0758f22011-12-07 11:19:22 -07002626 /* Standard KGSL gpuid format:
2627 * top word is 0x0002 for 2D or 0x0003 for 3D
2628 * Bottom word is core specific identifer
2629 */
2630
2631 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2632}
2633
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002634static const struct kgsl_functable adreno_functable = {
2635 /* Mandatory functions */
2636 .regread = adreno_regread,
2637 .regwrite = adreno_regwrite,
2638 .idle = adreno_idle,
2639 .isidle = adreno_isidle,
2640 .suspend_context = adreno_suspend_context,
2641 .start = adreno_start,
2642 .stop = adreno_stop,
2643 .getproperty = adreno_getproperty,
2644 .waittimestamp = adreno_waittimestamp,
2645 .readtimestamp = adreno_readtimestamp,
2646 .issueibcmds = adreno_ringbuffer_issueibcmds,
2647 .ioctl = adreno_ioctl,
2648 .setup_pt = adreno_setup_pt,
2649 .cleanup_pt = adreno_cleanup_pt,
2650 .power_stats = adreno_power_stats,
2651 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002652 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002653 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002654 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002655 /* Optional functions */
2656 .setstate = adreno_setstate,
2657 .drawctxt_create = adreno_drawctxt_create,
2658 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002659 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002660 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07002661 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002662};
2663
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002664static struct platform_driver adreno_platform_driver = {
2665 .probe = adreno_probe,
2666 .remove = __devexit_p(adreno_remove),
2667 .suspend = kgsl_suspend_driver,
2668 .resume = kgsl_resume_driver,
2669 .id_table = adreno_id_table,
2670 .driver = {
2671 .owner = THIS_MODULE,
2672 .name = DEVICE_3D_NAME,
2673 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002674 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002675 }
2676};
2677
2678static int __init kgsl_3d_init(void)
2679{
2680 return platform_driver_register(&adreno_platform_driver);
2681}
2682
2683static void __exit kgsl_3d_exit(void)
2684{
2685 platform_driver_unregister(&adreno_platform_driver);
2686}
2687
2688module_init(kgsl_3d_init);
2689module_exit(kgsl_3d_exit);
2690
2691MODULE_DESCRIPTION("3D Graphics driver");
2692MODULE_VERSION("1.2");
2693MODULE_LICENSE("GPL v2");
2694MODULE_ALIAS("platform:kgsl_3d");