blob: ec740da0a080c8c155f7552d192886029193e42c [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);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700298 if (context == NULL)
299 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600300 adreno_ctx = context->devctxt;
301
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600302 if (kgsl_mmu_enable_clk(&device->mmu,
303 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700304 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600305
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600306 cmds += __adreno_add_idle_indirect_cmds(cmds,
307 device->mmu.setstate_memory.gpuaddr +
308 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
309
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600310 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600311 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
312 device->mmu.setstate_memory.gpuaddr +
313 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
314 else
315 cmds += adreno_add_bank_change_cmds(cmds,
316 KGSL_IOMMU_CONTEXT_USER,
317 device->mmu.setstate_memory.gpuaddr +
318 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
319
Tarun Karra9c070822012-11-27 16:43:51 -0700320 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
321
322 /* Acquire GPU-CPU sync Lock here */
323 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
324
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700325 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
326 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600327 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600328 /*
329 * We need to perfrom the following operations for all
330 * IOMMU units
331 */
332 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700333 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
334 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600335 /*
336 * Set address of the new pagetable by writng to IOMMU
337 * TTBR0 register
338 */
339 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700340 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
341 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600342 *cmds++ = reg_pt_val;
343 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
344 *cmds++ = 0x00000000;
345
346 /*
347 * Read back the ttbr0 register as a barrier to ensure
348 * above writes have completed
349 */
350 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700351 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
352 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600353 reg_pt_val,
354 device->mmu.setstate_memory.gpuaddr +
355 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600356 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600357 }
358 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
359 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700360 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600361 */
362 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700363 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
364 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700365
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600366 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700367 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
368 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700369 KGSL_IOMMU_CTX_TLBIALL);
370 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600371
372 cmds += __adreno_add_idle_indirect_cmds(cmds,
373 device->mmu.setstate_memory.gpuaddr +
374 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
375
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600376 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700377 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
378 KGSL_IOMMU_CONTEXT_USER,
379 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700380 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600381 device->mmu.setstate_memory.gpuaddr +
382 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
383 }
384 }
385
Tarun Karra9c070822012-11-27 16:43:51 -0700386 /* Release GPU-CPU sync Lock here */
387 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
388
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600389 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600390 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700391 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
392 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600393 device->mmu.setstate_memory.gpuaddr +
394 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
395 else
396 cmds += adreno_add_bank_change_cmds(cmds,
397 KGSL_IOMMU_CONTEXT_PRIV,
398 device->mmu.setstate_memory.gpuaddr +
399 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
400
Tarun Karra9c070822012-11-27 16:43:51 -0700401 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
402
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600403 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600404 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700405 /* invalidate all base pointers */
406 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
407 *cmds++ = 0x7fff;
408 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600409 /* This returns the per context timestamp but we need to
410 * use the global timestamp for iommu clock disablement */
411 adreno_ringbuffer_issuecmds(device, adreno_ctx,
412 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600413 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600414 kgsl_mmu_disable_clk_on_ts(&device->mmu,
415 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600416 }
Tarun Karra9c070822012-11-27 16:43:51 -0700417
418 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
419 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
420 BUG();
421 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600422}
423
424static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600425 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600426 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427{
428 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
429 unsigned int link[32];
430 unsigned int *cmds = &link[0];
431 int sizedwords = 0;
432 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600433 struct kgsl_context *context;
434 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600436 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530437 * Fix target freeze issue by adding TLB flush for each submit
438 * on A20X based targets.
439 */
440 if (adreno_is_a20x(adreno_dev))
441 flags |= KGSL_MMUFLAGS_TLBFLUSH;
442 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600443 * If possible, then set the state via the command stream to avoid
444 * a CPU idle. Otherwise, use the default setstate which uses register
445 * writes For CFF dump we must idle and use the registers so that it is
446 * easier to filter out the mmu accesses from the dump
447 */
448 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600449 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700450 if (context == NULL)
451 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600452 adreno_ctx = context->devctxt;
453
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
455 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600456 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 *cmds++ = 0x00000000;
458
459 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600460 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700461 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600462 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463 sizedwords += 4;
464 }
465
466 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
467 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600468 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 1);
470 *cmds++ = 0x00000000;
471 sizedwords += 2;
472 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600473 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474 *cmds++ = mh_mmu_invalidate;
475 sizedwords += 2;
476 }
477
478 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600479 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 /* HW workaround: to resolve MMU page fault interrupts
481 * caused by the VGT.It prevents the CP PFP from filling
482 * the VGT DMA request fifo too early,thereby ensuring
483 * that the VGT will not fetch vertex/bin data until
484 * after the page table base register has been updated.
485 *
486 * Two null DRAW_INDX_BIN packets are inserted right
487 * after the page table base update, followed by a
488 * wait for idle. The null packets will fill up the
489 * VGT DMA request fifo and prevent any further
490 * vertex/bin updates from occurring until the wait
491 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600492 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 *cmds++ = (0x4 << 16) |
494 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
495 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600496 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600497 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600498 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499 *cmds++ = 0; /* viz query info */
500 *cmds++ = 0x0003C004; /* draw indicator */
501 *cmds++ = 0; /* bin base */
502 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600503 *cmds++ =
504 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600506 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 *cmds++ = 0; /* viz query info */
508 *cmds++ = 0x0003C004; /* draw indicator */
509 *cmds++ = 0; /* bin base */
510 *cmds++ = 3; /* bin size */
511 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600512 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600514 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515 *cmds++ = 0x00000000;
516 sizedwords += 21;
517 }
518
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600521 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 *cmds++ = 0x7fff; /* invalidate all base pointers */
523 sizedwords += 2;
524 }
525
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600526 adreno_ringbuffer_issuecmds(device, adreno_ctx,
527 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600529 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600530 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600531 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532}
533
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600534static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600535 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600536 uint32_t flags)
537{
538 /* call the mmu specific handler */
539 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600540 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600541 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600542 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600543}
544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700546a3xx_getchipid(struct kgsl_device *device)
547{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600548 struct kgsl_device_platform_data *pdata =
549 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700550
Jordan Crouse54154c62012-03-27 16:33:26 -0600551 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600552 * All current A3XX chipids are detected at the SOC level. Leave this
553 * function here to support any future GPUs that have working
554 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600555 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700556
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600557 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700558}
559
560static unsigned int
561a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562{
563 unsigned int chipid = 0;
564 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600565 struct kgsl_device_platform_data *pdata =
566 kgsl_device_get_drvdata(device);
567
568 /* If the chip id is set at the platform level, then just use that */
569
570 if (pdata->chipid != 0)
571 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572
573 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
574 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
575 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
576
577 /*
578 * adreno 22x gpus are indicated by coreid 2,
579 * but REG_RBBM_PERIPHID1 always contains 0 for this field
580 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600581 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 chipid = 2 << 24;
583 else
584 chipid = (coreid & 0xF) << 24;
585
586 chipid |= ((majorid >> 4) & 0xF) << 16;
587
588 minorid = ((revid >> 0) & 0xFF);
589
590 patchid = ((revid >> 16) & 0xFF);
591
592 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530593 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 if (cpu_is_qsd8x50())
595 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530596 else if (cpu_is_msm8625() && minorid == 0)
597 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598
599 chipid |= (minorid << 8) | patchid;
600
601 return chipid;
602}
603
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700604static unsigned int
605adreno_getchipid(struct kgsl_device *device)
606{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600607 struct kgsl_device_platform_data *pdata =
608 kgsl_device_get_drvdata(device);
609
610 /*
611 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
612 * an A2XX processor
613 */
614
615 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700616 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600617 else
618 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700619}
620
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621static inline bool _rev_match(unsigned int id, unsigned int entry)
622{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600623 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625
626static void
627adreno_identify_gpu(struct adreno_device *adreno_dev)
628{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600629 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630
631 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
632
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600633 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
634 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
635 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
636 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637
Jordan Crouse505df9c2011-07-28 08:37:59 -0600638 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
639 if (core == adreno_gpulist[i].core &&
640 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600641 _rev_match(minor, adreno_gpulist[i].minor) &&
642 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 }
645
Jordan Crouse505df9c2011-07-28 08:37:59 -0600646 if (i == ARRAY_SIZE(adreno_gpulist)) {
647 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
648 return;
649 }
650
651 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
652 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
653 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
654 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700655 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
656 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700657 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600658 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Tarun Karra9c070822012-11-27 16:43:51 -0700659 adreno_dev->gpulist_index = i;
660
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661}
662
Lokesh Batra805e1e12012-08-03 08:34:06 -0600663static struct platform_device_id adreno_id_table[] = {
664 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
665 {},
666};
667
668MODULE_DEVICE_TABLE(platform, adreno_id_table);
669
670static struct of_device_id adreno_match_table[] = {
671 { .compatible = "qcom,kgsl-3d0", },
672 {}
673};
674
675static inline int adreno_of_read_property(struct device_node *node,
676 const char *prop, unsigned int *ptr)
677{
678 int ret = of_property_read_u32(node, prop, ptr);
679 if (ret)
680 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
681 return ret;
682}
683
684static struct device_node *adreno_of_find_subnode(struct device_node *parent,
685 const char *name)
686{
687 struct device_node *child;
688
689 for_each_child_of_node(parent, child) {
690 if (of_device_is_compatible(child, name))
691 return child;
692 }
693
694 return NULL;
695}
696
697static int adreno_of_get_pwrlevels(struct device_node *parent,
698 struct kgsl_device_platform_data *pdata)
699{
700 struct device_node *node, *child;
701 int ret = -EINVAL;
702
703 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
704
705 if (node == NULL) {
706 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
707 return -EINVAL;
708 }
709
710 pdata->num_levels = 0;
711
712 for_each_child_of_node(node, child) {
713 unsigned int index;
714 struct kgsl_pwrlevel *level;
715
716 if (adreno_of_read_property(child, "reg", &index))
717 goto done;
718
719 if (index >= KGSL_MAX_PWRLEVELS) {
720 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
721 index);
722 continue;
723 }
724
725 if (index >= pdata->num_levels)
726 pdata->num_levels = index + 1;
727
728 level = &pdata->pwrlevel[index];
729
730 if (adreno_of_read_property(child, "qcom,gpu-freq",
731 &level->gpu_freq))
732 goto done;
733
734 if (adreno_of_read_property(child, "qcom,bus-freq",
735 &level->bus_freq))
736 goto done;
737
738 if (adreno_of_read_property(child, "qcom,io-fraction",
739 &level->io_fraction))
740 level->io_fraction = 0;
741 }
742
743 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
744 &pdata->init_level))
745 pdata->init_level = 1;
746
747 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
748 KGSL_CORE_ERR("Initial power level out of range\n");
749 pdata->init_level = 1;
750 }
751
752 ret = 0;
753done:
754 return ret;
755
756}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600757
758static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
759{
760 struct device_node *node, *child;
761 struct msm_dcvs_core_info *info = NULL;
762 int count = 0;
763 int ret = -EINVAL;
764
765 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
766 if (node == NULL)
767 return ERR_PTR(-EINVAL);
768
769 info = kzalloc(sizeof(*info), GFP_KERNEL);
770
771 if (info == NULL) {
772 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
773 ret = -ENOMEM;
774 goto err;
775 }
776
777 for_each_child_of_node(node, child)
778 count++;
779
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700780 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600781
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700782 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600783 sizeof(struct msm_dcvs_freq_entry),
784 GFP_KERNEL);
785
786 if (info->freq_tbl == NULL) {
787 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700788 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600789 sizeof(struct msm_dcvs_freq_entry));
790 ret = -ENOMEM;
791 goto err;
792 }
793
794 for_each_child_of_node(node, child) {
795 unsigned int index;
796
797 if (adreno_of_read_property(child, "reg", &index))
798 goto err;
799
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700800 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600801 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
802 index);
803 continue;
804 }
805
806 if (adreno_of_read_property(child, "qcom,freq",
807 &info->freq_tbl[index].freq))
808 goto err;
809
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700810 if (adreno_of_read_property(child, "qcom,voltage",
811 &info->freq_tbl[index].voltage))
812 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600813
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700814 if (adreno_of_read_property(child, "qcom,is_trans_level",
815 &info->freq_tbl[index].is_trans_level))
816 info->freq_tbl[index].is_trans_level = 0;
817
818 if (adreno_of_read_property(child, "qcom,active-energy-offset",
819 &info->freq_tbl[index].active_energy_offset))
820 info->freq_tbl[index].active_energy_offset = 0;
821
822 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
823 &info->freq_tbl[index].leakage_energy_offset))
824 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600825 }
826
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700827 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
828 goto err;
829
830 info->sensors = kzalloc(info->num_cores *
831 sizeof(int),
832 GFP_KERNEL);
833
834 for (count = 0; count < info->num_cores; count++) {
835 if (adreno_of_read_property(node, "qcom,sensors",
836 &(info->sensors[count])))
837 goto err;
838 }
839
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700840 if (adreno_of_read_property(node, "qcom,core-core-type",
841 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600842 goto err;
843
844 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
845 &info->algo_param.disable_pc_threshold))
846 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700847 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
848 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600849 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700850 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
851 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600852 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600853 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
854 &info->algo_param.em_max_util_pct))
855 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700856 if (adreno_of_read_property(node, "qcom,algo-group-id",
857 &info->algo_param.group_id))
858 goto err;
859 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
860 &info->algo_param.max_freq_chg_time_us))
861 goto err;
862 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
863 &info->algo_param.slack_mode_dynamic))
864 goto err;
865 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
866 &info->algo_param.slack_weight_thresh_pct))
867 goto err;
868 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
869 &info->algo_param.slack_time_min_us))
870 goto err;
871 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
872 &info->algo_param.slack_time_max_us))
873 goto err;
874 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
875 &info->algo_param.ss_win_size_min_us))
876 goto err;
877 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
878 &info->algo_param.ss_win_size_max_us))
879 goto err;
880 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
881 &info->algo_param.ss_util_pct))
882 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800883 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
884 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600885 goto err;
886
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700887 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
888 &info->energy_coeffs.active_coeff_a))
889 goto err;
890 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
891 &info->energy_coeffs.active_coeff_b))
892 goto err;
893 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
894 &info->energy_coeffs.active_coeff_c))
895 goto err;
896 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
897 &info->energy_coeffs.leakage_coeff_a))
898 goto err;
899 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
900 &info->energy_coeffs.leakage_coeff_b))
901 goto err;
902 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
903 &info->energy_coeffs.leakage_coeff_c))
904 goto err;
905 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
906 &info->energy_coeffs.leakage_coeff_d))
907 goto err;
908
909 if (adreno_of_read_property(node, "qcom,power-current-temp",
910 &info->power_param.current_temp))
911 goto err;
912
Lokesh Batra805e1e12012-08-03 08:34:06 -0600913 return info;
914
915err:
916 if (info)
917 kfree(info->freq_tbl);
918
919 kfree(info);
920
921 return ERR_PTR(ret);
922}
923
924static int adreno_of_get_iommu(struct device_node *parent,
925 struct kgsl_device_platform_data *pdata)
926{
927 struct device_node *node, *child;
928 struct kgsl_device_iommu_data *data = NULL;
929 struct kgsl_iommu_ctx *ctxs = NULL;
930 u32 reg_val[2];
931 int ctx_index = 0;
932
933 node = of_parse_phandle(parent, "iommu", 0);
934 if (node == NULL)
935 return -EINVAL;
936
937 data = kzalloc(sizeof(*data), GFP_KERNEL);
938 if (data == NULL) {
939 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
940 goto err;
941 }
942
943 if (of_property_read_u32_array(node, "reg", reg_val, 2))
944 goto err;
945
946 data->physstart = reg_val[0];
947 data->physend = data->physstart + reg_val[1] - 1;
948
949 data->iommu_ctx_count = 0;
950
951 for_each_child_of_node(node, child)
952 data->iommu_ctx_count++;
953
954 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
955 GFP_KERNEL);
956
957 if (ctxs == NULL) {
958 KGSL_CORE_ERR("kzalloc(%d) failed\n",
959 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
960 goto err;
961 }
962
963 for_each_child_of_node(node, child) {
964 int ret = of_property_read_string(child, "label",
965 &ctxs[ctx_index].iommu_ctx_name);
966
967 if (ret) {
968 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
969 goto err;
970 }
971
972 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
973 &ctxs[ctx_index].ctx_id))
974 goto err;
975
976 ctx_index++;
977 }
978
979 data->iommu_ctxs = ctxs;
980
981 pdata->iommu_data = data;
982 pdata->iommu_count = 1;
983
984 return 0;
985
986err:
987 kfree(ctxs);
988 kfree(data);
989
990 return -EINVAL;
991}
992
993static int adreno_of_get_pdata(struct platform_device *pdev)
994{
995 struct kgsl_device_platform_data *pdata = NULL;
996 struct kgsl_device *device;
997 int ret = -EINVAL;
998
999 pdev->id_entry = adreno_id_table;
1000
1001 pdata = pdev->dev.platform_data;
1002 if (pdata)
1003 return 0;
1004
1005 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1006 KGSL_CORE_ERR("Unable to read 'label'\n");
1007 goto err;
1008 }
1009
1010 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1011 goto err;
1012
1013 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1014 if (pdata == NULL) {
1015 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1016 ret = -ENOMEM;
1017 goto err;
1018 }
1019
1020 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1021 &pdata->chipid))
1022 goto err;
1023
1024 /* pwrlevel Data */
1025 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1026 if (ret)
1027 goto err;
1028
1029 /* Default value is 83, if not found in DT */
1030 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1031 &pdata->idle_timeout))
1032 pdata->idle_timeout = 83;
1033
1034 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1035 &pdata->nap_allowed))
1036 pdata->nap_allowed = 1;
1037
1038 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1039 &pdata->clk_map))
1040 goto err;
1041
1042 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1043
1044 if (device->id != KGSL_DEVICE_3D0)
1045 goto err;
1046
1047 /* Bus Scale Data */
1048
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001049 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001050 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1051 ret = PTR_ERR(pdata->bus_scale_table);
1052 goto err;
1053 }
1054
1055 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1056 if (IS_ERR_OR_NULL(pdata->core_info)) {
1057 ret = PTR_ERR(pdata->core_info);
1058 goto err;
1059 }
1060
1061 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1062 if (ret)
1063 goto err;
1064
1065 pdev->dev.platform_data = pdata;
1066 return 0;
1067
1068err:
1069 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001070 if (pdata->core_info)
1071 kfree(pdata->core_info->freq_tbl);
1072 kfree(pdata->core_info);
1073
1074 if (pdata->iommu_data)
1075 kfree(pdata->iommu_data->iommu_ctxs);
1076
1077 kfree(pdata->iommu_data);
1078 }
1079
1080 kfree(pdata);
1081
1082 return ret;
1083}
1084
liu zhong7dfa2a32012-04-27 19:11:01 -07001085#ifdef CONFIG_MSM_OCMEM
1086static int
1087adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1088{
Jordan Crousec0978202012-08-29 14:35:51 -06001089 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001090 return 0;
1091
1092 /* OCMEM is only needed once, do not support consective allocation */
1093 if (adreno_dev->ocmem_hdl != NULL)
1094 return 0;
1095
1096 adreno_dev->ocmem_hdl =
1097 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1098 if (adreno_dev->ocmem_hdl == NULL)
1099 return -ENOMEM;
1100
1101 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001102 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001103
1104 return 0;
1105}
1106
1107static void
1108adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1109{
Jordan Crousec0978202012-08-29 14:35:51 -06001110 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001111 return;
1112
1113 if (adreno_dev->ocmem_hdl == NULL)
1114 return;
1115
1116 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1117 adreno_dev->ocmem_hdl = NULL;
1118}
1119#else
1120static int
1121adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1122{
1123 return 0;
1124}
1125
1126static void
1127adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1128{
1129}
1130#endif
1131
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001132static int __devinit
1133adreno_probe(struct platform_device *pdev)
1134{
1135 struct kgsl_device *device;
1136 struct adreno_device *adreno_dev;
1137 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001138 bool is_dt;
1139
1140 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1141
1142 if (is_dt && pdev->dev.of_node) {
1143 status = adreno_of_get_pdata(pdev);
1144 if (status)
1145 goto error_return;
1146 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001147
1148 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1149 adreno_dev = ADRENO_DEVICE(device);
1150 device->parentdev = &pdev->dev;
1151
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 status = adreno_ringbuffer_init(device);
1153 if (status != 0)
1154 goto error;
1155
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001156 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 if (status)
1158 goto error_close_rb;
1159
1160 adreno_debugfs_init(device);
1161
1162 kgsl_pwrscale_init(device);
1163 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1164
1165 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1166 return 0;
1167
1168error_close_rb:
1169 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1170error:
1171 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001172error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173 return status;
1174}
1175
1176static int __devexit adreno_remove(struct platform_device *pdev)
1177{
1178 struct kgsl_device *device;
1179 struct adreno_device *adreno_dev;
1180
1181 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1182 adreno_dev = ADRENO_DEVICE(device);
1183
1184 kgsl_pwrscale_detach_policy(device);
1185 kgsl_pwrscale_close(device);
1186
1187 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1188 kgsl_device_platform_remove(device);
1189
1190 return 0;
1191}
1192
1193static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1194{
1195 int status = -EINVAL;
1196 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001197
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001198 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1199 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200
1201 /* Power up the device */
1202 kgsl_pwrctrl_enable(device);
1203
1204 /* Identify the specific GPU */
1205 adreno_identify_gpu(adreno_dev);
1206
Tarun Karra9c070822012-11-27 16:43:51 -07001207 if (adreno_ringbuffer_read_pm4_ucode(device)) {
1208 KGSL_DRV_ERR(device, "Reading pm4 microcode failed %s\n",
1209 adreno_dev->pm4_fwfile);
1210 BUG_ON(1);
1211 }
1212
1213 if (adreno_ringbuffer_read_pfp_ucode(device)) {
1214 KGSL_DRV_ERR(device, "Reading pfp microcode failed %s\n",
1215 adreno_dev->pfp_fwfile);
1216 BUG_ON(1);
1217 }
1218
Jordan Crouse505df9c2011-07-28 08:37:59 -06001219 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1220 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1221 adreno_dev->chip_id);
1222 goto error_clk_off;
1223 }
1224
Tarun Karra9c070822012-11-27 16:43:51 -07001225
1226 /*
1227 * Check if firmware supports the sync lock PM4 packets needed
1228 * for IOMMUv1
1229 */
1230
1231 if ((adreno_dev->pm4_fw_version >=
1232 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pm4_ver) &&
1233 (adreno_dev->pfp_fw_version >=
1234 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pfp_ver))
1235 device->mmu.flags |= KGSL_MMU_FLAGS_IOMMU_SYNC;
1236
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001237 /* Set up the MMU */
1238 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001239 /*
1240 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1241 * on older gpus
1242 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001243 if (adreno_is_a20x(adreno_dev)) {
1244 device->mh.mh_intf_cfg1 = 0;
1245 device->mh.mh_intf_cfg2 = 0;
1246 }
1247
1248 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001249 }
1250
Tarun Karra3335f142012-06-19 14:11:48 -07001251 /* Assign correct RBBM status register to hang detect regs
1252 */
1253 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1254
Jordan Crouseb5c80482012-10-03 09:38:41 -06001255 /* Add A3XX specific registers for hang detection */
1256 if (adreno_is_a3xx(adreno_dev)) {
1257 hang_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1258 hang_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
1259 }
1260
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001261 status = kgsl_mmu_start(device);
1262 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263 goto error_clk_off;
1264
liu zhong7dfa2a32012-04-27 19:11:01 -07001265 status = adreno_ocmem_gmem_malloc(adreno_dev);
1266 if (status) {
1267 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1268 goto error_mmu_off;
1269 }
1270
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001271 /* Start the GPU */
1272 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001273
1274 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001275 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001276
1277 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001278 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001279 /* While recovery is on we do not want timer to
1280 * fire and attempt to change any device state */
1281 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1282 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001283 return 0;
1284 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001285
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001287
1288error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001289 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001290
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001291error_clk_off:
1292 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293
1294 return status;
1295}
1296
1297static int adreno_stop(struct kgsl_device *device)
1298{
1299 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301 adreno_dev->drawctxt_active = NULL;
1302
1303 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1304
Shubhraprakash Das79447952012-04-26 18:12:23 -06001305 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001307 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301308 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001309 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001310
liu zhong7dfa2a32012-04-27 19:11:01 -07001311 adreno_ocmem_gmem_free(adreno_dev);
1312
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001313 /* Power down the device */
1314 kgsl_pwrctrl_disable(device);
1315
1316 return 0;
1317}
1318
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001319static void adreno_mark_context_status(struct kgsl_device *device,
1320 int recovery_status)
1321{
1322 struct kgsl_context *context;
1323 int next = 0;
1324 /*
1325 * Set the reset status of all contexts to
1326 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1327 * since thats the guilty party, if recovery failed then
1328 * mark all as guilty
1329 */
1330 while ((context = idr_get_next(&device->context_idr, &next))) {
1331 struct adreno_context *adreno_context = context->devctxt;
1332 if (recovery_status) {
1333 context->reset_status =
1334 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1335 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1336 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1337 context->reset_status) {
1338 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1339 CTXT_FLAGS_GPU_HANG_RECOVERED))
1340 context->reset_status =
1341 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1342 else
1343 context->reset_status =
1344 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1345 }
1346 next = next + 1;
1347 }
1348}
1349
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001350static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1351{
1352 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1353 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1354 struct kgsl_context *context;
1355 struct adreno_context *temp_adreno_context;
1356 int next = 0;
1357
1358 while ((context = idr_get_next(&device->context_idr, &next))) {
1359 temp_adreno_context = context->devctxt;
1360 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1361 kgsl_sharedmem_writel(&device->memstore,
1362 KGSL_MEMSTORE_OFFSET(context->id,
1363 soptimestamp),
1364 rb->timestamp[context->id]);
1365 kgsl_sharedmem_writel(&device->memstore,
1366 KGSL_MEMSTORE_OFFSET(context->id,
1367 eoptimestamp),
1368 rb->timestamp[context->id]);
1369 }
1370 next = next + 1;
1371 }
1372}
1373
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001374static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1375{
1376 vfree(rec_data->rb_buffer);
1377 vfree(rec_data->bad_rb_buffer);
1378}
1379
1380static int adreno_setup_recovery_data(struct kgsl_device *device,
1381 struct adreno_recovery_data *rec_data)
1382{
1383 int ret = 0;
1384 unsigned int ib1_sz, ib2_sz;
1385 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1386 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1387
1388 memset(rec_data, 0, sizeof(*rec_data));
1389
1390 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1391 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1392 if (ib1_sz || ib2_sz)
1393 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1394
1395 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1396 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1397 current_context));
1398
1399 kgsl_sharedmem_readl(&device->memstore,
1400 &rec_data->global_eop,
1401 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1402 eoptimestamp));
1403
1404 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1405 if (!rec_data->rb_buffer) {
1406 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1407 rb->buffer_desc.size);
1408 return -ENOMEM;
1409 }
1410
1411 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1412 if (!rec_data->bad_rb_buffer) {
1413 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1414 rb->buffer_desc.size);
1415 ret = -ENOMEM;
1416 goto done;
1417 }
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001418 rec_data->fault = device->mmu.fault;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001419
1420done:
1421 if (ret) {
1422 vfree(rec_data->rb_buffer);
1423 vfree(rec_data->bad_rb_buffer);
1424 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001425 return ret;
1426}
1427
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001428static int
1429_adreno_recover_hang(struct kgsl_device *device,
1430 struct adreno_recovery_data *rec_data,
1431 bool try_bad_commands)
1432{
1433 int ret;
1434 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1435 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1436 struct kgsl_context *context;
1437 struct adreno_context *adreno_context = NULL;
1438 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1439
1440 context = idr_find(&device->context_idr, rec_data->context_id);
1441 if (context == NULL) {
1442 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1443 rec_data->context_id);
1444 } else {
1445 adreno_context = context->devctxt;
1446 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001447 /*
1448 * set the invalid ts flag to 0 for this context since we have
1449 * detected a hang for it
1450 */
1451 context->wait_on_invalid_ts = false;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001452 }
1453
1454 /* Extract valid contents from rb which can still be executed after
1455 * hang */
1456 ret = adreno_ringbuffer_extract(rb, rec_data);
1457 if (ret)
1458 goto done;
1459
1460 /* restart device */
1461 ret = adreno_stop(device);
1462 if (ret) {
1463 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1464 goto done;
1465 }
1466
1467 ret = adreno_start(device, true);
1468 if (ret) {
1469 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1470 goto done;
1471 }
1472
1473 if (context)
1474 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1475 KGSL_MEMSTORE_GLOBAL);
1476
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001477 /* If iommu is used then we need to make sure that the iommu clocks
1478 * are on since there could be commands in pipeline that touch iommu */
1479 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1480 ret = kgsl_mmu_enable_clk(&device->mmu,
1481 KGSL_IOMMU_CONTEXT_USER);
1482 if (ret)
1483 goto done;
1484 }
1485
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001486 /* Do not try the bad commands if recovery has failed bad commands
1487 * once already or if hang is due to a fault */
1488 if (!try_bad_commands || rec_data->fault)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001489 rec_data->bad_rb_size = 0;
1490
1491 if (rec_data->bad_rb_size) {
1492 int idle_ret;
1493 /* submit the bad and good context commands and wait for
1494 * them to pass */
1495 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1496 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001497 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001498 if (idle_ret) {
1499 ret = adreno_stop(device);
1500 if (ret) {
1501 KGSL_DRV_ERR(device,
1502 "Device stop failed in recovery\n");
1503 goto done;
1504 }
1505 ret = adreno_start(device, true);
1506 if (ret) {
1507 KGSL_DRV_ERR(device,
1508 "Device start failed in recovery\n");
1509 goto done;
1510 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001511 if (context)
1512 kgsl_mmu_setstate(&device->mmu,
1513 adreno_context->pagetable,
1514 KGSL_MEMSTORE_GLOBAL);
1515
1516 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1517 ret = kgsl_mmu_enable_clk(&device->mmu,
1518 KGSL_IOMMU_CONTEXT_USER);
1519 if (ret)
1520 goto done;
1521 }
1522
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001523 ret = idle_ret;
1524 KGSL_DRV_ERR(device,
1525 "Bad context commands hung in recovery\n");
1526 } else {
1527 KGSL_DRV_ERR(device,
1528 "Bad context commands succeeded in recovery\n");
1529 if (adreno_context)
1530 adreno_context->flags = (adreno_context->flags &
1531 ~CTXT_FLAGS_GPU_HANG) |
1532 CTXT_FLAGS_GPU_HANG_RECOVERED;
1533 adreno_dev->drawctxt_active = last_active_ctx;
1534 }
1535 }
1536 /* If either the bad command sequence failed or we did not play it */
1537 if (ret || !rec_data->bad_rb_size) {
1538 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1539 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001540 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001541 if (ret) {
1542 /* If we fail here we can try to invalidate another
1543 * context and try recovering again */
1544 ret = -EAGAIN;
1545 goto done;
1546 }
1547 /* ringbuffer now has data from the last valid context id,
1548 * so restore the active_ctx to the last valid context */
1549 if (rec_data->last_valid_ctx_id) {
1550 struct kgsl_context *last_ctx =
1551 idr_find(&device->context_idr,
1552 rec_data->last_valid_ctx_id);
1553 if (last_ctx)
1554 adreno_dev->drawctxt_active = last_ctx->devctxt;
1555 }
1556 }
1557done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001558 /* Turn off iommu clocks */
1559 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1560 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001561 return ret;
1562}
1563
1564static int
1565adreno_recover_hang(struct kgsl_device *device,
1566 struct adreno_recovery_data *rec_data)
1567{
1568 int ret = 0;
1569 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1570 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1571 unsigned int timestamp;
1572
1573 KGSL_DRV_ERR(device,
1574 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1575 "Bad context_id: %u, global_eop: 0x%x\n",
1576 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1577
1578 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1579 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1580
1581 /* We may need to replay commands multiple times based on whether
1582 * multiple contexts hang the GPU */
1583 while (true) {
1584 if (!ret)
1585 ret = _adreno_recover_hang(device, rec_data, true);
1586 else
1587 ret = _adreno_recover_hang(device, rec_data, false);
1588
1589 if (-EAGAIN == ret) {
1590 /* setup new recovery parameters and retry, this
1591 * means more than 1 contexts are causing hang */
1592 adreno_destroy_recovery_data(rec_data);
1593 adreno_setup_recovery_data(device, rec_data);
1594 KGSL_DRV_ERR(device,
1595 "Retry recovery from 3D GPU hang. Recovery parameters: "
1596 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1597 rec_data->ib1, rec_data->context_id,
1598 rec_data->global_eop);
1599 } else {
1600 break;
1601 }
1602 }
1603
1604 if (ret)
1605 goto done;
1606
1607 /* Restore correct states after recovery */
1608 if (adreno_dev->drawctxt_active)
1609 device->mmu.hwpagetable =
1610 adreno_dev->drawctxt_active->pagetable;
1611 else
1612 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1613 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1614 kgsl_sharedmem_writel(&device->memstore,
1615 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1616 eoptimestamp),
1617 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1618done:
1619 adreno_set_max_ts_for_bad_ctxs(device);
1620 adreno_mark_context_status(device, ret);
1621 if (!ret)
1622 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1623 else
1624 KGSL_DRV_ERR(device, "Recovery failed\n");
1625 return ret;
1626}
1627
1628int
1629adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001630{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001631 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001632 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001633
1634 if (device->state == KGSL_STATE_HUNG)
1635 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001636 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637 mutex_unlock(&device->mutex);
1638 wait_for_completion(&device->recovery_gate);
1639 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001640 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001641 result = 0;
1642 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001643 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001644 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001645 /* Detected a hang */
1646
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001647 /* Get the recovery data as soon as hang is detected */
1648 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001649 /*
1650 * Trigger an automatic dump of the state to
1651 * the console
1652 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001653 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001654
1655 /*
1656 * Make a GPU snapshot. For now, do it after the PM dump so we
1657 * can at least be sure the PM dump will work as it always has
1658 */
1659 kgsl_device_snapshot(device, 1);
1660
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001661 result = adreno_recover_hang(device, &rec_data);
1662 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001663 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001664 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001665 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001666 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001667 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1668 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001669 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 }
1671done:
1672 return result;
1673}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001674EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675
1676static int adreno_getproperty(struct kgsl_device *device,
1677 enum kgsl_property_type type,
1678 void *value,
1679 unsigned int sizebytes)
1680{
1681 int status = -EINVAL;
1682 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1683
1684 switch (type) {
1685 case KGSL_PROP_DEVICE_INFO:
1686 {
1687 struct kgsl_devinfo devinfo;
1688
1689 if (sizebytes != sizeof(devinfo)) {
1690 status = -EINVAL;
1691 break;
1692 }
1693
1694 memset(&devinfo, 0, sizeof(devinfo));
1695 devinfo.device_id = device->id+1;
1696 devinfo.chip_id = adreno_dev->chip_id;
1697 devinfo.mmu_enabled = kgsl_mmu_enabled();
1698 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001699 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1700 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001701
1702 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1703 0) {
1704 status = -EFAULT;
1705 break;
1706 }
1707 status = 0;
1708 }
1709 break;
1710 case KGSL_PROP_DEVICE_SHADOW:
1711 {
1712 struct kgsl_shadowprop shadowprop;
1713
1714 if (sizebytes != sizeof(shadowprop)) {
1715 status = -EINVAL;
1716 break;
1717 }
1718 memset(&shadowprop, 0, sizeof(shadowprop));
1719 if (device->memstore.hostptr) {
1720 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1721 * anything to mmap().
1722 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001723 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001724 shadowprop.size = device->memstore.size;
1725 /* GSL needs this to be set, even if it
1726 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001727 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1728 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001729 }
1730 if (copy_to_user(value, &shadowprop,
1731 sizeof(shadowprop))) {
1732 status = -EFAULT;
1733 break;
1734 }
1735 status = 0;
1736 }
1737 break;
1738 case KGSL_PROP_MMU_ENABLE:
1739 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001740 int mmu_prop = kgsl_mmu_enabled();
1741
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001742 if (sizebytes != sizeof(int)) {
1743 status = -EINVAL;
1744 break;
1745 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001746 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001747 status = -EFAULT;
1748 break;
1749 }
1750 status = 0;
1751 }
1752 break;
1753 case KGSL_PROP_INTERRUPT_WAITS:
1754 {
1755 int int_waits = 1;
1756 if (sizebytes != sizeof(int)) {
1757 status = -EINVAL;
1758 break;
1759 }
1760 if (copy_to_user(value, &int_waits, sizeof(int))) {
1761 status = -EFAULT;
1762 break;
1763 }
1764 status = 0;
1765 }
1766 break;
1767 default:
1768 status = -EINVAL;
1769 }
1770
1771 return status;
1772}
1773
Jordan Crousef7370f82012-04-18 09:31:07 -06001774static int adreno_setproperty(struct kgsl_device *device,
1775 enum kgsl_property_type type,
1776 void *value,
1777 unsigned int sizebytes)
1778{
1779 int status = -EINVAL;
1780
1781 switch (type) {
1782 case KGSL_PROP_PWRCTRL: {
1783 unsigned int enable;
1784 struct kgsl_device_platform_data *pdata =
1785 kgsl_device_get_drvdata(device);
1786
1787 if (sizebytes != sizeof(enable))
1788 break;
1789
1790 if (copy_from_user(&enable, (void __user *) value,
1791 sizeof(enable))) {
1792 status = -EFAULT;
1793 break;
1794 }
1795
1796 if (enable) {
1797 if (pdata->nap_allowed)
1798 device->pwrctrl.nap_allowed = true;
1799
1800 kgsl_pwrscale_enable(device);
1801 } else {
1802 device->pwrctrl.nap_allowed = false;
1803 kgsl_pwrscale_disable(device);
1804 }
1805
1806 status = 0;
1807 }
1808 break;
1809 default:
1810 break;
1811 }
1812
1813 return status;
1814}
1815
Jordan Crousea29a2e02012-08-14 09:09:23 -06001816static int adreno_ringbuffer_drain(struct kgsl_device *device,
1817 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001818{
1819 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1820 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001821 unsigned long wait;
1822 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1823
1824 if (!(rb->flags & KGSL_FLAGS_STARTED))
1825 return 0;
1826
1827 /*
1828 * The first time into the loop, wait for 100 msecs and kick wptr again
1829 * to ensure that the hardware has updated correctly. After that, kick
1830 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1831 * expires
1832 */
1833
1834 wait = jiffies + msecs_to_jiffies(100);
1835
Jordan Crousea29a2e02012-08-14 09:09:23 -06001836 do {
1837 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001838 /* Check to see if the core is hung */
1839 if (adreno_hang_detect(device, regs))
1840 return -ETIMEDOUT;
1841
1842 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1843 }
1844 GSL_RB_GET_READPTR(rb, &rb->rptr);
1845
1846 if (time_after(jiffies, timeout)) {
1847 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1848 rb->rptr, rb->wptr);
1849 return -ETIMEDOUT;
1850 }
1851 } while (rb->rptr != rb->wptr);
1852
1853 return 0;
1854}
1855
1856/* Caller must hold the device mutex. */
1857int adreno_idle(struct kgsl_device *device)
1858{
1859 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001860 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301861 unsigned long wait_time;
1862 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001863 unsigned int prev_reg_val[hang_detect_regs_count];
1864
1865 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001866
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001867 kgsl_cffdump_regpoll(device->id,
1868 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001869 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001870
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001872 /* First, wait for the ringbuffer to drain */
1873 if (adreno_ringbuffer_drain(device, prev_reg_val))
1874 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001875
1876 /* now, wait for the GPU to finish its operations */
Jordan Crousea29a2e02012-08-14 09:09:23 -06001877 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
1878 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1879
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001880 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001881 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1882 &rbbm_status);
1883 if (adreno_is_a2xx(adreno_dev)) {
1884 if (rbbm_status == 0x110)
1885 return 0;
1886 } else {
1887 if (!(rbbm_status & 0x80000000))
1888 return 0;
1889 }
Tarun Karra3335f142012-06-19 14:11:48 -07001890
1891 /* Dont wait for timeout, detect hang faster.
1892 */
1893 if (time_after(jiffies, wait_time_part)) {
1894 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001895 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001896 if ((adreno_hang_detect(device, prev_reg_val)))
1897 goto err;
1898 }
1899
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001900 }
1901
1902err:
1903 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001904 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1905 !adreno_dump_and_recover(device)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001906 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001907 goto retry;
1908 }
1909 return -ETIMEDOUT;
1910}
1911
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08001912/**
1913 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
1914 * rbbm_status register
1915 * @device - Pointer to the GPU device whose idle status is to be
1916 * checked
1917 * @returns - Returns whether the core is idle (based on rbbm_status)
1918 * false if the core is active, true if the core is idle
1919 */
1920static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
1921{
1922 unsigned int reg_rbbm_status;
1923 bool status = false;
1924 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1925
1926 /* Is the core idle? */
1927 adreno_regread(device,
1928 adreno_dev->gpudev->reg_rbbm_status,
1929 &reg_rbbm_status);
1930
1931 if (adreno_is_a2xx(adreno_dev)) {
1932 if (reg_rbbm_status == 0x110)
1933 status = true;
1934 } else {
1935 if (!(reg_rbbm_status & 0x80000000))
1936 status = true;
1937 }
1938 return status;
1939}
1940
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001941static unsigned int adreno_isidle(struct kgsl_device *device)
1942{
1943 int status = false;
1944 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1945 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001946
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001947 WARN_ON(device->state == KGSL_STATE_INIT);
1948 /* If the device isn't active, don't force it on. */
1949 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001950 /* Is the ring buffer is empty? */
1951 GSL_RB_GET_READPTR(rb, &rb->rptr);
1952 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1953 /* Is the core idle? */
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08001954 status = is_adreno_rbbm_status_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001955 }
1956 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001957 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001958 }
1959 return status;
1960}
1961
1962/* Caller must hold the device mutex. */
1963static int adreno_suspend_context(struct kgsl_device *device)
1964{
1965 int status = 0;
1966 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1967
1968 /* switch to NULL ctxt */
1969 if (adreno_dev->drawctxt_active != NULL) {
1970 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001971 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001972 }
1973
1974 return status;
1975}
1976
Jordan Crouse233b2092012-04-18 09:31:09 -06001977/* Find a memory structure attached to an adreno context */
1978
1979struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1980 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1981{
1982 struct kgsl_context *context;
1983 struct adreno_context *adreno_context = NULL;
1984 int next = 0;
1985
1986 while (1) {
1987 context = idr_get_next(&device->context_idr, &next);
1988 if (context == NULL)
1989 break;
1990
1991 adreno_context = (struct adreno_context *)context->devctxt;
1992
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07001993 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
1994 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06001995 struct kgsl_memdesc *desc;
1996
1997 desc = &adreno_context->gpustate;
1998 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1999 return desc;
2000
2001 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2002 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2003 return desc;
2004 }
2005 next = next + 1;
2006 }
2007
2008 return NULL;
2009}
2010
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002011struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002012 unsigned int pt_base,
2013 unsigned int gpuaddr,
2014 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002015{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002016 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002017 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2018 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2019
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002020 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2021 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002022
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002023 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2024 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002025
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002026 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2027 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002028
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002029 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2030 size))
2031 return &device->mmu.setstate_memory;
2032
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002033 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002034
2035 if (entry)
2036 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002037
Jordan Crouse233b2092012-04-18 09:31:09 -06002038 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002039}
2040
2041uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2042 unsigned int gpuaddr, unsigned int size)
2043{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002044 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002045
2046 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2047
2048 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002049}
2050
2051void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2052 unsigned int *value)
2053{
2054 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002055 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2056 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002057
2058 if (!in_interrupt())
2059 kgsl_pre_hwaccess(device);
2060
2061 /*ensure this read finishes before the next one.
2062 * i.e. act like normal readl() */
2063 *value = __raw_readl(reg);
2064 rmb();
2065}
2066
2067void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2068 unsigned int value)
2069{
2070 unsigned int *reg;
2071
Jordan Crouse7501d452012-04-19 08:58:44 -06002072 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002073
2074 if (!in_interrupt())
2075 kgsl_pre_hwaccess(device);
2076
2077 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002078 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002079
2080 /*ensure previous writes post before this one,
2081 * i.e. act like normal writel() */
2082 wmb();
2083 __raw_writel(value, reg);
2084}
2085
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002086static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2087{
2088 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002089 if (k_ctxt != NULL) {
2090 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002091 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2092 context_id = KGSL_CONTEXT_INVALID;
2093 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2094 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002095 }
2096
2097 return context_id;
2098}
2099
Jordan Crouse313faf62012-11-20 15:12:28 -07002100static void adreno_next_event(struct kgsl_device *device,
2101 struct kgsl_event *event)
2102{
2103 int status;
2104 unsigned int ref_ts, enableflag;
2105 unsigned int context_id = _get_context_id(event->context);
2106 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2107
2108 status = kgsl_check_timestamp(device, event->context, event->timestamp);
2109 if (!status) {
2110 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2111 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2112 /*
2113 * Barrier is needed here to make sure the read from memstore
2114 * has posted
2115 */
2116
2117 mb();
2118
2119 if (enableflag) {
2120 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
2121 KGSL_MEMSTORE_OFFSET(context_id,
2122 ref_wait_ts));
2123
2124 /* Make sure the memstore read has posted */
2125 mb();
2126 if (timestamp_cmp(ref_ts, event->timestamp) >= 0) {
2127 kgsl_sharedmem_writel(&device->memstore,
2128 KGSL_MEMSTORE_OFFSET(context_id,
2129 ref_wait_ts), event->timestamp);
2130 /* Make sure the memstore write is posted */
2131 wmb();
2132 }
2133 } else {
2134 unsigned int cmds[2];
2135 kgsl_sharedmem_writel(&device->memstore,
2136 KGSL_MEMSTORE_OFFSET(context_id,
2137 ref_wait_ts), event->timestamp);
2138 enableflag = 1;
2139 kgsl_sharedmem_writel(&device->memstore,
2140 KGSL_MEMSTORE_OFFSET(context_id,
2141 ts_cmp_enable), enableflag);
2142
2143 /* Make sure the memstore write gets posted */
2144 wmb();
2145
2146 /*
2147 * submit a dummy packet so that even if all
2148 * commands upto timestamp get executed we will still
2149 * get an interrupt
2150 */
2151 cmds[0] = cp_type3_packet(CP_NOP, 1);
2152 cmds[1] = 0;
2153
2154 if (adreno_dev->drawctxt_active)
2155 adreno_ringbuffer_issuecmds_intr(device,
2156 event->context, &cmds[0], 2);
2157 }
2158 }
2159}
2160
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002161static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002162 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002163{
2164 int status;
2165 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002166 unsigned int context_id;
2167
2168 mutex_lock(&device->mutex);
2169 context_id = _get_context_id(context);
2170 /*
2171 * If the context ID is invalid, we are in a race with
2172 * the context being destroyed by userspace so bail.
2173 */
2174 if (context_id == KGSL_CONTEXT_INVALID) {
2175 KGSL_DRV_WARN(device, "context was detached");
2176 status = -EINVAL;
2177 goto unlock;
2178 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002179
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002180 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002182 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002183 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002184 mb();
2185
2186 if (enableflag) {
2187 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002188 KGSL_MEMSTORE_OFFSET(context_id,
2189 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002190 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002191 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002192 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002193 KGSL_MEMSTORE_OFFSET(context_id,
2194 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002195 wmb();
2196 }
2197 } else {
2198 unsigned int cmds[2];
2199 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002200 KGSL_MEMSTORE_OFFSET(context_id,
2201 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002202 enableflag = 1;
2203 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002204 KGSL_MEMSTORE_OFFSET(context_id,
2205 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002206 wmb();
2207 /* submit a dummy packet so that even if all
2208 * commands upto timestamp get executed we will still
2209 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002210 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002211 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002212
Harsh Vardhan Dwivedi32f968d2012-12-05 10:35:13 -07002213 if (context)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002214 adreno_ringbuffer_issuecmds_intr(device,
2215 context, &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002216 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002217 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002218unlock:
2219 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002220
2221 return status;
2222}
2223
2224/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002225 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002226 placing a process in wait q. For conditional interrupts we expect the
2227 process to already be in its wait q when its exit condition checking
2228 function is called.
2229*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002230#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002231({ \
2232 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002233 if (io) \
2234 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2235 else \
2236 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002237 __ret; \
2238})
2239
Tarun Karra3335f142012-06-19 14:11:48 -07002240
2241
2242unsigned int adreno_hang_detect(struct kgsl_device *device,
2243 unsigned int *prev_reg_val)
2244{
2245 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2246 unsigned int curr_reg_val[hang_detect_regs_count];
2247 unsigned int hang_detected = 1;
2248 unsigned int i;
2249
2250 if (!adreno_dev->fast_hang_detect)
2251 return 0;
2252
Jordan Crousecca61142012-11-20 10:54:24 -07002253 if (is_adreno_rbbm_status_idle(device)) {
2254
2255 /*
2256 * On A20X if the RPTR != WPTR and the device is idle, then
2257 * the last write to WPTR probably failed to latch so write it
2258 * again
2259 */
2260
2261 if (adreno_is_a2xx(adreno_dev)) {
2262 unsigned int rptr;
2263 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2264 if (rptr != adreno_dev->ringbuffer.wptr)
2265 adreno_regwrite(device, REG_CP_RB_WPTR,
2266 adreno_dev->ringbuffer.wptr);
2267 }
2268
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002269 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002270 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002271
Tarun Karra3335f142012-06-19 14:11:48 -07002272 for (i = 0; i < hang_detect_regs_count; i++) {
Jordan Crouseb5c80482012-10-03 09:38:41 -06002273
2274 if (hang_detect_regs[i] == 0)
2275 continue;
2276
Tarun Karra3335f142012-06-19 14:11:48 -07002277 adreno_regread(device, hang_detect_regs[i],
2278 &curr_reg_val[i]);
2279 if (curr_reg_val[i] != prev_reg_val[i]) {
2280 prev_reg_val[i] = curr_reg_val[i];
2281 hang_detected = 0;
2282 }
2283 }
2284
2285 return hang_detected;
2286}
2287
Jordan Crouse92446a62012-11-15 11:00:06 -07002288/**
2289 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2290 * @device - pointer to a KGSL device structure
2291 * @context - pointer to the active KGSL context
2292 * @timestamp - the timestamp that the process was waiting for
2293 *
2294 * Process a possible GPU hang and try to recover from it cleanly
2295 */
2296static int adreno_handle_hang(struct kgsl_device *device,
2297 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002298{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002299 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002300 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002301 unsigned int ts_issued;
Tarun Karra3335f142012-06-19 14:11:48 -07002302
Jordan Crouse92446a62012-11-15 11:00:06 -07002303 /* Do one last check to see if we somehow made it through */
2304 if (kgsl_check_timestamp(device, context, timestamp))
2305 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002306
2307 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002308
Jeremy Gebben63904832012-02-07 16:10:55 -07002309 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002310 "Device hang detected while waiting for timestamp: "
2311 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2312 "wptr: 0x%x\n",
2313 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002314 adreno_dev->ringbuffer.wptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002315
2316 /* Return 0 after a successful recovery */
2317 if (!adreno_dump_and_recover(device))
2318 return 0;
2319
2320 return -ETIMEDOUT;
2321}
2322
2323static int _check_pending_timestamp(struct kgsl_device *device,
2324 struct kgsl_context *context, unsigned int timestamp)
2325{
2326 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2327 unsigned int context_id = _get_context_id(context);
2328 unsigned int ts_issued;
2329
2330 if (context_id == KGSL_CONTEXT_INVALID)
2331 return -EINVAL;
2332
2333 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2334
2335 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2336 return 0;
2337
2338 if (context && !context->wait_on_invalid_ts) {
2339 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2340 context_id, timestamp, context_id, ts_issued);
2341
2342 /* Only print this message once */
2343 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002344 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002345
2346 return -EINVAL;
2347}
2348
2349/**
2350 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2351 * @device - pointer to a KGSL device structure
2352 * @context - pointer to the active kgsl context
2353 * @timestamp - GPU timestamp to wait for
2354 * @msecs - amount of time to wait (in milliseconds)
2355 *
2356 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2357 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2358 * one if it happened. Otherwise, spend most of our time in an interruptible
2359 * wait for the timestamp interrupt to be processed. This function must be
2360 * called with the mutex already held.
2361 */
2362static int adreno_waittimestamp(struct kgsl_device *device,
2363 struct kgsl_context *context,
2364 unsigned int timestamp,
2365 unsigned int msecs)
2366{
2367 static unsigned int io_cnt;
2368 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2369 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2370 unsigned int context_id = _get_context_id(context);
2371 unsigned int prev_reg_val[hang_detect_regs_count];
2372 unsigned int time_elapsed = 0;
2373 unsigned int wait;
2374 int ts_compare = 1;
2375 int io, ret = -ETIMEDOUT;
2376
2377 /* Get out early if the context has already been destroyed */
2378
2379 if (context_id == KGSL_CONTEXT_INVALID) {
2380 KGSL_DRV_WARN(device, "context was detached");
2381 return -EINVAL;
2382 }
2383
2384 /*
2385 * Check to see if the requested timestamp is "newer" then the last
2386 * timestamp issued. If it is complain once and return error. Only
2387 * print the message once per context so that badly behaving
2388 * applications don't spam the logs
2389 */
2390
2391 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2392 if (_check_pending_timestamp(device, context, timestamp))
2393 return -EINVAL;
2394
2395 /* Reset the invalid timestamp flag on a valid wait */
2396 context->wait_on_invalid_ts = false;
2397 }
2398
2399
2400 /* Clear the registers used for hang detection */
2401 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2402
2403 /*
2404 * On the first time through the loop only wait 100ms.
2405 * this gives enough time for the engine to start moving and oddly
2406 * provides better hang detection results than just going the full
2407 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2408 * is if msecs happens to be < 100ms then just use the full timeout
2409 */
2410
2411 wait = 100;
2412
2413 do {
2414 long status;
2415
2416 if (wait > (msecs - time_elapsed))
2417 wait = msecs - time_elapsed;
2418
2419 /*
2420 * if the timestamp happens while we're not
2421 * waiting, there's a chance that an interrupt
2422 * will not be generated and thus the timestamp
2423 * work needs to be queued.
2424 */
2425
2426 if (kgsl_check_timestamp(device, context, timestamp)) {
2427 queue_work(device->work_queue, &device->ts_expired_ws);
2428 ret = 0;
2429 break;
2430 }
2431
2432 /* Check to see if the GPU is hung */
2433 if (adreno_hang_detect(device, prev_reg_val)) {
2434 ret = adreno_handle_hang(device, context, timestamp);
2435 break;
2436 }
2437
2438 /*
2439 * For proper power accounting sometimes we need to call
2440 * io_wait_interruptible_timeout and sometimes we need to call
2441 * plain old wait_interruptible_timeout. We call the regular
2442 * timeout N times out of 100, where N is a number specified by
2443 * the current power level
2444 */
2445
2446 io_cnt = (io_cnt + 1) % 100;
2447 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2448 ? 0 : 1;
2449
2450 mutex_unlock(&device->mutex);
2451
2452 /* Wait for a timestamp event */
2453 status = kgsl_wait_event_interruptible_timeout(
2454 device->wait_queue,
2455 kgsl_check_interrupt_timestamp(device, context,
2456 timestamp), msecs_to_jiffies(wait), io);
2457
2458 mutex_lock(&device->mutex);
2459
2460 /*
2461 * If status is non zero then either the condition was satisfied
2462 * or there was an error. In either event, this is the end of
2463 * the line for us
2464 */
2465
2466 if (status != 0) {
2467 ret = (status > 0) ? 0 : (int) status;
2468 break;
2469 }
2470
2471 time_elapsed += wait;
2472
2473 /* If user specified timestamps are being used, wait at least
2474 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
2475 * issue a IB for a timestamp before checking to see if the
2476 * current timestamp we are waiting for is valid or not
2477 */
2478
2479 if (ts_compare && (adreno_ctx &&
2480 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
2481 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
2482 ret = _check_pending_timestamp(device, context,
2483 timestamp);
2484 if (ret)
2485 break;
2486
2487 /* Don't do this check again */
2488 ts_compare = 0;
2489
2490 /*
2491 * Reset the invalid timestamp flag on a valid
2492 * wait
2493 */
2494 context->wait_on_invalid_ts = false;
2495 }
2496 }
2497
2498 /*
2499 * all subsequent trips through the loop wait the full
2500 * KGSL_TIMEOUT_PART interval
2501 */
2502 wait = KGSL_TIMEOUT_PART;
2503
2504 } while (!msecs || time_elapsed < msecs);
2505
2506 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002507}
2508
2509static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002510 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002511{
2512 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002513 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002514
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002515 /*
2516 * If the context ID is invalid, we are in a race with
2517 * the context being destroyed by userspace so bail.
2518 */
2519 if (context_id == KGSL_CONTEXT_INVALID) {
2520 KGSL_DRV_WARN(device, "context was detached");
2521 return timestamp;
2522 }
Jordan Crousec659f382012-04-16 11:10:41 -06002523 switch (type) {
2524 case KGSL_TIMESTAMP_QUEUED: {
2525 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2526 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2527
2528 timestamp = rb->timestamp[context_id];
2529 break;
2530 }
2531 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002532 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002533 break;
2534 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002535 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002536 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2537 break;
2538 }
2539
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002540 rmb();
2541
2542 return timestamp;
2543}
2544
2545static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2546 unsigned int cmd, void *data)
2547{
2548 int result = 0;
2549 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2550 struct kgsl_context *context;
2551
2552 switch (cmd) {
2553 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2554 binbase = data;
2555
2556 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2557 if (context) {
2558 adreno_drawctxt_set_bin_base_offset(
2559 dev_priv->device, context, binbase->offset);
2560 } else {
2561 result = -EINVAL;
2562 KGSL_DRV_ERR(dev_priv->device,
2563 "invalid drawctxt drawctxt_id %d "
2564 "device_id=%d\n",
2565 binbase->drawctxt_id, dev_priv->device->id);
2566 }
2567 break;
2568
2569 default:
2570 KGSL_DRV_INFO(dev_priv->device,
2571 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002572 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002573 break;
2574 }
2575 return result;
2576
2577}
2578
2579static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2580{
2581 gpu_freq /= 1000000;
2582 return ticks / gpu_freq;
2583}
2584
2585static void adreno_power_stats(struct kgsl_device *device,
2586 struct kgsl_power_stats *stats)
2587{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002588 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002589 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002590 unsigned int cycles;
2591
2592 /* Get the busy cycles counted since the counter was last reset */
2593 /* Calling this function also resets and restarts the counter */
2594
2595 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002596
2597 /* In order to calculate idle you have to have run the algorithm *
2598 * at least once to get a start time. */
2599 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002600 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002601 stats->total_time = tmp - pwr->time;
2602 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002603 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002604 pwrlevels[device->pwrctrl.active_pwrlevel].
2605 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002606 } else {
2607 stats->total_time = 0;
2608 stats->busy_time = 0;
2609 pwr->time = ktime_to_us(ktime_get());
2610 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002611}
2612
2613void adreno_irqctrl(struct kgsl_device *device, int state)
2614{
Jordan Crousea78c9172011-07-11 13:14:09 -06002615 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2616 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002617}
2618
Jordan Croused6535882012-06-20 08:22:16 -06002619static unsigned int adreno_gpuid(struct kgsl_device *device,
2620 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002621{
2622 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2623
Jordan Croused6535882012-06-20 08:22:16 -06002624 /* Some applications need to know the chip ID too, so pass
2625 * that as a parameter */
2626
2627 if (chipid != NULL)
2628 *chipid = adreno_dev->chip_id;
2629
Jordan Crousea0758f22011-12-07 11:19:22 -07002630 /* Standard KGSL gpuid format:
2631 * top word is 0x0002 for 2D or 0x0003 for 3D
2632 * Bottom word is core specific identifer
2633 */
2634
2635 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2636}
2637
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002638static const struct kgsl_functable adreno_functable = {
2639 /* Mandatory functions */
2640 .regread = adreno_regread,
2641 .regwrite = adreno_regwrite,
2642 .idle = adreno_idle,
2643 .isidle = adreno_isidle,
2644 .suspend_context = adreno_suspend_context,
2645 .start = adreno_start,
2646 .stop = adreno_stop,
2647 .getproperty = adreno_getproperty,
2648 .waittimestamp = adreno_waittimestamp,
2649 .readtimestamp = adreno_readtimestamp,
2650 .issueibcmds = adreno_ringbuffer_issueibcmds,
2651 .ioctl = adreno_ioctl,
2652 .setup_pt = adreno_setup_pt,
2653 .cleanup_pt = adreno_cleanup_pt,
2654 .power_stats = adreno_power_stats,
2655 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002656 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002657 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002658 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002659 /* Optional functions */
2660 .setstate = adreno_setstate,
2661 .drawctxt_create = adreno_drawctxt_create,
2662 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002663 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002664 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07002665 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002666};
2667
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002668static struct platform_driver adreno_platform_driver = {
2669 .probe = adreno_probe,
2670 .remove = __devexit_p(adreno_remove),
2671 .suspend = kgsl_suspend_driver,
2672 .resume = kgsl_resume_driver,
2673 .id_table = adreno_id_table,
2674 .driver = {
2675 .owner = THIS_MODULE,
2676 .name = DEVICE_3D_NAME,
2677 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002678 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002679 }
2680};
2681
2682static int __init kgsl_3d_init(void)
2683{
2684 return platform_driver_register(&adreno_platform_driver);
2685}
2686
2687static void __exit kgsl_3d_exit(void)
2688{
2689 platform_driver_unregister(&adreno_platform_driver);
2690}
2691
2692module_init(kgsl_3d_init);
2693module_exit(kgsl_3d_exit);
2694
2695MODULE_DESCRIPTION("3D Graphics driver");
2696MODULE_VERSION("1.2");
2697MODULE_LICENSE("GPL v2");
2698MODULE_ALIAS("platform:kgsl_3d");