blob: 85b30932890354027fe71b18d1bfbdd879eded15 [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,
Tarun Karra6e750d72013-01-04 10:28:40 -0800131 0,
132 0,
133 0,
134 0,
Jordan Crouseb5c80482012-10-03 09:38:41 -0600135 0
Tarun Karra3335f142012-06-19 14:11:48 -0700136};
137
138const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700139
Jordan Crouse505df9c2011-07-28 08:37:59 -0600140/*
141 * This is the master list of all GPU cores that are supported by this
142 * driver.
143 */
144
145#define ANY_ID (~0)
Tarun Karra9c070822012-11-27 16:43:51 -0700146#define NO_VER (~0)
Jordan Crouse505df9c2011-07-28 08:37:59 -0600147
148static const struct {
149 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600150 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600151 const char *pm4fw;
152 const char *pfpfw;
153 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700154 unsigned int istore_size;
155 unsigned int pix_shader_start;
Tarun Karra9c070822012-11-27 16:43:51 -0700156 /* Size of an instruction in dwords */
157 unsigned int instruction_size;
158 /* size of gmem for gpu*/
159 unsigned int gmem_size;
160 /* version of pm4 microcode that supports sync_lock
161 between CPU and GPU for SMMU-v1 programming */
162 unsigned int sync_lock_pm4_ver;
163 /* version of pfp microcode that supports sync_lock
164 between CPU and GPU for SMMU-v1 programming */
165 unsigned int sync_lock_pfp_ver;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600166} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600167 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700168 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700169 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530170 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
171 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700172 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600173 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700174 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700175 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600176 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700177 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700178 512, 384, 3, SZ_512K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600179 /*
180 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
181 * a hardware problem.
182 */
183 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700184 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700185 1536, 768, 3, SZ_512K, NO_VER, NO_VER },
Carter Cooperf27ec722011-11-17 15:20:38 -0700186 { ADRENO_REV_A225, 2, 2, 0, 6,
187 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700188 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600189 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700190 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700191 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530192 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530193 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530194 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700195 512, 0, 2, SZ_256K, 0x3FF037, 0x3FF016 },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700196 /* A3XX doesn't use the pix_shader_start */
Carter Cooper95f7f792012-08-19 13:40:34 -0600197 { ADRENO_REV_A320, 3, 2, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700198 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700199 512, 0, 2, SZ_512K, 0x3FF037, 0x3FF016 },
liu zhongfd42e622012-05-01 19:18:30 -0700200 { ADRENO_REV_A330, 3, 3, 0, 0,
201 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700202 512, 0, 2, SZ_1M, NO_VER, NO_VER },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600203};
204
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600205static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206{
Jordan Crousea78c9172011-07-11 13:14:09 -0600207 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600208 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209
Jordan Crousea78c9172011-07-11 13:14:09 -0600210 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211
212 if (device->requested_state == KGSL_STATE_NONE) {
213 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700214 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 queue_work(device->work_queue, &device->idle_check_ws);
216 } else if (device->pwrscale.policy != NULL) {
217 queue_work(device->work_queue, &device->idle_check_ws);
218 }
219 }
220
221 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800222 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 jiffies + device->pwrctrl.interval_timeout);
224 return result;
225}
226
Jordan Crouse9f739212011-07-28 08:37:57 -0600227static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 struct kgsl_pagetable *pagetable)
229{
230 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
231 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
232
233 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
234
235 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
236
237 kgsl_mmu_unmap(pagetable, &device->memstore);
238
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600239 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240}
241
242static int adreno_setup_pt(struct kgsl_device *device,
243 struct kgsl_pagetable *pagetable)
244{
245 int result = 0;
246 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
247 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
248
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
250 GSL_PT_PAGE_RV);
251 if (result)
252 goto error;
253
254 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
255 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
256 if (result)
257 goto unmap_buffer_desc;
258
259 result = kgsl_mmu_map_global(pagetable, &device->memstore,
260 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
261 if (result)
262 goto unmap_memptrs_desc;
263
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600264 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
266 if (result)
267 goto unmap_memstore_desc;
268
269 return result;
270
271unmap_memstore_desc:
272 kgsl_mmu_unmap(pagetable, &device->memstore);
273
274unmap_memptrs_desc:
275 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
276
277unmap_buffer_desc:
278 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
279
280error:
281 return result;
282}
283
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600284static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600285 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600286 uint32_t flags)
287{
288 unsigned int pt_val, reg_pt_val;
Tarun Karra9c070822012-11-27 16:43:51 -0700289 unsigned int link[250];
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600290 unsigned int *cmds = &link[0];
291 int sizedwords = 0;
292 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600293 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600294 struct kgsl_context *context;
295 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600296
297 if (!adreno_dev->drawctxt_active)
298 return kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700299 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600300
301 context = idr_find(&device->context_idr, context_id);
302 adreno_ctx = context->devctxt;
303
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600304 if (kgsl_mmu_enable_clk(&device->mmu,
305 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700306 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600307
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600308 cmds += __adreno_add_idle_indirect_cmds(cmds,
309 device->mmu.setstate_memory.gpuaddr +
310 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
311
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600312 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600313 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
314 device->mmu.setstate_memory.gpuaddr +
315 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
316 else
317 cmds += adreno_add_bank_change_cmds(cmds,
318 KGSL_IOMMU_CONTEXT_USER,
319 device->mmu.setstate_memory.gpuaddr +
320 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
321
Tarun Karra9c070822012-11-27 16:43:51 -0700322 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
323
324 /* Acquire GPU-CPU sync Lock here */
325 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
326
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700327 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
328 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600329 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600330 /*
331 * We need to perfrom the following operations for all
332 * IOMMU units
333 */
334 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700335 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
336 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600337 /*
338 * Set address of the new pagetable by writng to IOMMU
339 * TTBR0 register
340 */
341 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700342 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
343 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600344 *cmds++ = reg_pt_val;
345 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
346 *cmds++ = 0x00000000;
347
348 /*
349 * Read back the ttbr0 register as a barrier to ensure
350 * above writes have completed
351 */
352 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700353 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
354 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600355 reg_pt_val,
356 device->mmu.setstate_memory.gpuaddr +
357 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600358 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600359 }
360 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
361 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700362 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600363 */
364 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700365 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
366 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700367
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600368 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700369 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
370 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700371 KGSL_IOMMU_CTX_TLBIALL);
372 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600373
374 cmds += __adreno_add_idle_indirect_cmds(cmds,
375 device->mmu.setstate_memory.gpuaddr +
376 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
377
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600378 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700379 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
380 KGSL_IOMMU_CONTEXT_USER,
381 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700382 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600383 device->mmu.setstate_memory.gpuaddr +
384 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
385 }
386 }
387
Tarun Karra9c070822012-11-27 16:43:51 -0700388 /* Release GPU-CPU sync Lock here */
389 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
390
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600391 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600392 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700393 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
394 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600395 device->mmu.setstate_memory.gpuaddr +
396 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
397 else
398 cmds += adreno_add_bank_change_cmds(cmds,
399 KGSL_IOMMU_CONTEXT_PRIV,
400 device->mmu.setstate_memory.gpuaddr +
401 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
402
Tarun Karra9c070822012-11-27 16:43:51 -0700403 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
404
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600405 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600406 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700407 /* invalidate all base pointers */
408 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
409 *cmds++ = 0x7fff;
410 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600411 /* This returns the per context timestamp but we need to
412 * use the global timestamp for iommu clock disablement */
413 adreno_ringbuffer_issuecmds(device, adreno_ctx,
414 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600415 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600416 kgsl_mmu_disable_clk_on_ts(&device->mmu,
417 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600418 }
Tarun Karra9c070822012-11-27 16:43:51 -0700419
420 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
421 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
422 BUG();
423 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600424}
425
426static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600427 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600428 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429{
430 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
431 unsigned int link[32];
432 unsigned int *cmds = &link[0];
433 int sizedwords = 0;
434 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600435 struct kgsl_context *context;
436 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600438 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530439 * Fix target freeze issue by adding TLB flush for each submit
440 * on A20X based targets.
441 */
442 if (adreno_is_a20x(adreno_dev))
443 flags |= KGSL_MMUFLAGS_TLBFLUSH;
444 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600445 * If possible, then set the state via the command stream to avoid
446 * a CPU idle. Otherwise, use the default setstate which uses register
447 * writes For CFF dump we must idle and use the registers so that it is
448 * easier to filter out the mmu accesses from the dump
449 */
450 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600451 context = idr_find(&device->context_idr, context_id);
452 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;
Tarun Karra6e750d72013-01-04 10:28:40 -08001259 hang_detect_regs[8] = A3XX_RBBM_PERFCTR_SP_6_LO;
1260 hang_detect_regs[9] = A3XX_RBBM_PERFCTR_SP_6_HI;
1261 hang_detect_regs[10] = A3XX_RBBM_PERFCTR_SP_5_LO;
1262 hang_detect_regs[11] = A3XX_RBBM_PERFCTR_SP_5_HI;
Jordan Crouseb5c80482012-10-03 09:38:41 -06001263 }
1264
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001265 status = kgsl_mmu_start(device);
1266 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001267 goto error_clk_off;
1268
liu zhong7dfa2a32012-04-27 19:11:01 -07001269 status = adreno_ocmem_gmem_malloc(adreno_dev);
1270 if (status) {
1271 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1272 goto error_mmu_off;
1273 }
1274
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001275 /* Start the GPU */
1276 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277
1278 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001279 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001280
1281 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001282 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001283 /* While recovery is on we do not want timer to
1284 * fire and attempt to change any device state */
1285 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1286 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001287 return 0;
1288 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001291
1292error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001293 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001294
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295error_clk_off:
1296 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297
1298 return status;
1299}
1300
1301static int adreno_stop(struct kgsl_device *device)
1302{
1303 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1304
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305 adreno_dev->drawctxt_active = NULL;
1306
1307 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1308
Shubhraprakash Das79447952012-04-26 18:12:23 -06001309 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001311 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301312 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001313 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001314
liu zhong7dfa2a32012-04-27 19:11:01 -07001315 adreno_ocmem_gmem_free(adreno_dev);
1316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317 /* Power down the device */
1318 kgsl_pwrctrl_disable(device);
1319
1320 return 0;
1321}
1322
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001323static void adreno_mark_context_status(struct kgsl_device *device,
1324 int recovery_status)
1325{
1326 struct kgsl_context *context;
1327 int next = 0;
1328 /*
1329 * Set the reset status of all contexts to
1330 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1331 * since thats the guilty party, if recovery failed then
1332 * mark all as guilty
1333 */
1334 while ((context = idr_get_next(&device->context_idr, &next))) {
1335 struct adreno_context *adreno_context = context->devctxt;
1336 if (recovery_status) {
1337 context->reset_status =
1338 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1339 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1340 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1341 context->reset_status) {
1342 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1343 CTXT_FLAGS_GPU_HANG_RECOVERED))
1344 context->reset_status =
1345 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1346 else
1347 context->reset_status =
1348 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1349 }
1350 next = next + 1;
1351 }
1352}
1353
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001354static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1355{
1356 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1357 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1358 struct kgsl_context *context;
1359 struct adreno_context *temp_adreno_context;
1360 int next = 0;
1361
1362 while ((context = idr_get_next(&device->context_idr, &next))) {
1363 temp_adreno_context = context->devctxt;
1364 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1365 kgsl_sharedmem_writel(&device->memstore,
1366 KGSL_MEMSTORE_OFFSET(context->id,
1367 soptimestamp),
1368 rb->timestamp[context->id]);
1369 kgsl_sharedmem_writel(&device->memstore,
1370 KGSL_MEMSTORE_OFFSET(context->id,
1371 eoptimestamp),
1372 rb->timestamp[context->id]);
1373 }
1374 next = next + 1;
1375 }
1376}
1377
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001378static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1379{
1380 vfree(rec_data->rb_buffer);
1381 vfree(rec_data->bad_rb_buffer);
1382}
1383
1384static int adreno_setup_recovery_data(struct kgsl_device *device,
1385 struct adreno_recovery_data *rec_data)
1386{
1387 int ret = 0;
1388 unsigned int ib1_sz, ib2_sz;
1389 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1390 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1391
1392 memset(rec_data, 0, sizeof(*rec_data));
1393
1394 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1395 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1396 if (ib1_sz || ib2_sz)
1397 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1398
1399 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1400 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1401 current_context));
1402
1403 kgsl_sharedmem_readl(&device->memstore,
1404 &rec_data->global_eop,
1405 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1406 eoptimestamp));
1407
1408 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1409 if (!rec_data->rb_buffer) {
1410 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1411 rb->buffer_desc.size);
1412 return -ENOMEM;
1413 }
1414
1415 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1416 if (!rec_data->bad_rb_buffer) {
1417 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1418 rb->buffer_desc.size);
1419 ret = -ENOMEM;
1420 goto done;
1421 }
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001422 rec_data->fault = device->mmu.fault;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001423
1424done:
1425 if (ret) {
1426 vfree(rec_data->rb_buffer);
1427 vfree(rec_data->bad_rb_buffer);
1428 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001429 return ret;
1430}
1431
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001432static int
1433_adreno_recover_hang(struct kgsl_device *device,
1434 struct adreno_recovery_data *rec_data,
1435 bool try_bad_commands)
1436{
1437 int ret;
1438 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1439 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1440 struct kgsl_context *context;
1441 struct adreno_context *adreno_context = NULL;
1442 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1443
1444 context = idr_find(&device->context_idr, rec_data->context_id);
1445 if (context == NULL) {
1446 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1447 rec_data->context_id);
1448 } else {
1449 adreno_context = context->devctxt;
1450 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001451 /*
1452 * set the invalid ts flag to 0 for this context since we have
1453 * detected a hang for it
1454 */
1455 context->wait_on_invalid_ts = false;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001456 }
1457
1458 /* Extract valid contents from rb which can still be executed after
1459 * hang */
1460 ret = adreno_ringbuffer_extract(rb, rec_data);
1461 if (ret)
1462 goto done;
1463
1464 /* restart device */
1465 ret = adreno_stop(device);
1466 if (ret) {
1467 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1468 goto done;
1469 }
1470
1471 ret = adreno_start(device, true);
1472 if (ret) {
1473 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1474 goto done;
1475 }
1476
1477 if (context)
1478 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1479 KGSL_MEMSTORE_GLOBAL);
1480
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001481 /* If iommu is used then we need to make sure that the iommu clocks
1482 * are on since there could be commands in pipeline that touch iommu */
1483 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1484 ret = kgsl_mmu_enable_clk(&device->mmu,
1485 KGSL_IOMMU_CONTEXT_USER);
1486 if (ret)
1487 goto done;
1488 }
1489
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001490 /* Do not try the bad commands if recovery has failed bad commands
1491 * once already or if hang is due to a fault */
1492 if (!try_bad_commands || rec_data->fault)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001493 rec_data->bad_rb_size = 0;
1494
1495 if (rec_data->bad_rb_size) {
1496 int idle_ret;
1497 /* submit the bad and good context commands and wait for
1498 * them to pass */
1499 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1500 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001501 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001502 if (idle_ret) {
1503 ret = adreno_stop(device);
1504 if (ret) {
1505 KGSL_DRV_ERR(device,
1506 "Device stop failed in recovery\n");
1507 goto done;
1508 }
1509 ret = adreno_start(device, true);
1510 if (ret) {
1511 KGSL_DRV_ERR(device,
1512 "Device start failed in recovery\n");
1513 goto done;
1514 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001515 if (context)
1516 kgsl_mmu_setstate(&device->mmu,
1517 adreno_context->pagetable,
1518 KGSL_MEMSTORE_GLOBAL);
1519
1520 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1521 ret = kgsl_mmu_enable_clk(&device->mmu,
1522 KGSL_IOMMU_CONTEXT_USER);
1523 if (ret)
1524 goto done;
1525 }
1526
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001527 ret = idle_ret;
1528 KGSL_DRV_ERR(device,
1529 "Bad context commands hung in recovery\n");
1530 } else {
1531 KGSL_DRV_ERR(device,
1532 "Bad context commands succeeded in recovery\n");
1533 if (adreno_context)
1534 adreno_context->flags = (adreno_context->flags &
1535 ~CTXT_FLAGS_GPU_HANG) |
1536 CTXT_FLAGS_GPU_HANG_RECOVERED;
1537 adreno_dev->drawctxt_active = last_active_ctx;
1538 }
1539 }
1540 /* If either the bad command sequence failed or we did not play it */
1541 if (ret || !rec_data->bad_rb_size) {
1542 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1543 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001544 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001545 if (ret) {
1546 /* If we fail here we can try to invalidate another
1547 * context and try recovering again */
1548 ret = -EAGAIN;
1549 goto done;
1550 }
1551 /* ringbuffer now has data from the last valid context id,
1552 * so restore the active_ctx to the last valid context */
1553 if (rec_data->last_valid_ctx_id) {
1554 struct kgsl_context *last_ctx =
1555 idr_find(&device->context_idr,
1556 rec_data->last_valid_ctx_id);
1557 if (last_ctx)
1558 adreno_dev->drawctxt_active = last_ctx->devctxt;
1559 }
1560 }
1561done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001562 /* Turn off iommu clocks */
1563 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1564 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001565 return ret;
1566}
1567
1568static int
1569adreno_recover_hang(struct kgsl_device *device,
1570 struct adreno_recovery_data *rec_data)
1571{
1572 int ret = 0;
1573 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1574 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1575 unsigned int timestamp;
1576
1577 KGSL_DRV_ERR(device,
1578 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1579 "Bad context_id: %u, global_eop: 0x%x\n",
1580 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1581
1582 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1583 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1584
1585 /* We may need to replay commands multiple times based on whether
1586 * multiple contexts hang the GPU */
1587 while (true) {
1588 if (!ret)
1589 ret = _adreno_recover_hang(device, rec_data, true);
1590 else
1591 ret = _adreno_recover_hang(device, rec_data, false);
1592
1593 if (-EAGAIN == ret) {
1594 /* setup new recovery parameters and retry, this
1595 * means more than 1 contexts are causing hang */
1596 adreno_destroy_recovery_data(rec_data);
1597 adreno_setup_recovery_data(device, rec_data);
1598 KGSL_DRV_ERR(device,
1599 "Retry recovery from 3D GPU hang. Recovery parameters: "
1600 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1601 rec_data->ib1, rec_data->context_id,
1602 rec_data->global_eop);
1603 } else {
1604 break;
1605 }
1606 }
1607
1608 if (ret)
1609 goto done;
1610
1611 /* Restore correct states after recovery */
1612 if (adreno_dev->drawctxt_active)
1613 device->mmu.hwpagetable =
1614 adreno_dev->drawctxt_active->pagetable;
1615 else
1616 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1617 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1618 kgsl_sharedmem_writel(&device->memstore,
1619 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1620 eoptimestamp),
1621 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1622done:
1623 adreno_set_max_ts_for_bad_ctxs(device);
1624 adreno_mark_context_status(device, ret);
1625 if (!ret)
1626 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1627 else
1628 KGSL_DRV_ERR(device, "Recovery failed\n");
1629 return ret;
1630}
1631
1632int
1633adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001634{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001635 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001636 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637
1638 if (device->state == KGSL_STATE_HUNG)
1639 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001640 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001641 mutex_unlock(&device->mutex);
1642 wait_for_completion(&device->recovery_gate);
1643 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001644 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001645 result = 0;
1646 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001647 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001648 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001649 /* Detected a hang */
1650
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001651 /* Get the recovery data as soon as hang is detected */
1652 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001653 /*
1654 * Trigger an automatic dump of the state to
1655 * the console
1656 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001657 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001658
1659 /*
1660 * Make a GPU snapshot. For now, do it after the PM dump so we
1661 * can at least be sure the PM dump will work as it always has
1662 */
1663 kgsl_device_snapshot(device, 1);
1664
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001665 result = adreno_recover_hang(device, &rec_data);
1666 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001667 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001668 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001669 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001670 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001671 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1672 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001673 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001674 }
1675done:
1676 return result;
1677}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001678EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001679
1680static int adreno_getproperty(struct kgsl_device *device,
1681 enum kgsl_property_type type,
1682 void *value,
1683 unsigned int sizebytes)
1684{
1685 int status = -EINVAL;
1686 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1687
1688 switch (type) {
1689 case KGSL_PROP_DEVICE_INFO:
1690 {
1691 struct kgsl_devinfo devinfo;
1692
1693 if (sizebytes != sizeof(devinfo)) {
1694 status = -EINVAL;
1695 break;
1696 }
1697
1698 memset(&devinfo, 0, sizeof(devinfo));
1699 devinfo.device_id = device->id+1;
1700 devinfo.chip_id = adreno_dev->chip_id;
1701 devinfo.mmu_enabled = kgsl_mmu_enabled();
1702 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001703 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1704 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001705
1706 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1707 0) {
1708 status = -EFAULT;
1709 break;
1710 }
1711 status = 0;
1712 }
1713 break;
1714 case KGSL_PROP_DEVICE_SHADOW:
1715 {
1716 struct kgsl_shadowprop shadowprop;
1717
1718 if (sizebytes != sizeof(shadowprop)) {
1719 status = -EINVAL;
1720 break;
1721 }
1722 memset(&shadowprop, 0, sizeof(shadowprop));
1723 if (device->memstore.hostptr) {
1724 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1725 * anything to mmap().
1726 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001727 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001728 shadowprop.size = device->memstore.size;
1729 /* GSL needs this to be set, even if it
1730 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001731 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1732 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001733 }
1734 if (copy_to_user(value, &shadowprop,
1735 sizeof(shadowprop))) {
1736 status = -EFAULT;
1737 break;
1738 }
1739 status = 0;
1740 }
1741 break;
1742 case KGSL_PROP_MMU_ENABLE:
1743 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001744 int mmu_prop = kgsl_mmu_enabled();
1745
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001746 if (sizebytes != sizeof(int)) {
1747 status = -EINVAL;
1748 break;
1749 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001750 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001751 status = -EFAULT;
1752 break;
1753 }
1754 status = 0;
1755 }
1756 break;
1757 case KGSL_PROP_INTERRUPT_WAITS:
1758 {
1759 int int_waits = 1;
1760 if (sizebytes != sizeof(int)) {
1761 status = -EINVAL;
1762 break;
1763 }
1764 if (copy_to_user(value, &int_waits, sizeof(int))) {
1765 status = -EFAULT;
1766 break;
1767 }
1768 status = 0;
1769 }
1770 break;
1771 default:
1772 status = -EINVAL;
1773 }
1774
1775 return status;
1776}
1777
Jordan Crousef7370f82012-04-18 09:31:07 -06001778static int adreno_setproperty(struct kgsl_device *device,
1779 enum kgsl_property_type type,
1780 void *value,
1781 unsigned int sizebytes)
1782{
1783 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08001784 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06001785
1786 switch (type) {
1787 case KGSL_PROP_PWRCTRL: {
1788 unsigned int enable;
1789 struct kgsl_device_platform_data *pdata =
1790 kgsl_device_get_drvdata(device);
1791
1792 if (sizebytes != sizeof(enable))
1793 break;
1794
1795 if (copy_from_user(&enable, (void __user *) value,
1796 sizeof(enable))) {
1797 status = -EFAULT;
1798 break;
1799 }
1800
1801 if (enable) {
1802 if (pdata->nap_allowed)
1803 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08001804 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06001805 kgsl_pwrscale_enable(device);
1806 } else {
1807 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08001808 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06001809 kgsl_pwrscale_disable(device);
1810 }
1811
1812 status = 0;
1813 }
1814 break;
1815 default:
1816 break;
1817 }
1818
1819 return status;
1820}
1821
Jordan Crousea29a2e02012-08-14 09:09:23 -06001822static int adreno_ringbuffer_drain(struct kgsl_device *device,
1823 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001824{
1825 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1826 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001827 unsigned long wait;
1828 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1829
1830 if (!(rb->flags & KGSL_FLAGS_STARTED))
1831 return 0;
1832
1833 /*
1834 * The first time into the loop, wait for 100 msecs and kick wptr again
1835 * to ensure that the hardware has updated correctly. After that, kick
1836 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1837 * expires
1838 */
1839
1840 wait = jiffies + msecs_to_jiffies(100);
1841
Jordan Crousea29a2e02012-08-14 09:09:23 -06001842 do {
1843 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001844 /* Check to see if the core is hung */
1845 if (adreno_hang_detect(device, regs))
1846 return -ETIMEDOUT;
1847
1848 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1849 }
1850 GSL_RB_GET_READPTR(rb, &rb->rptr);
1851
1852 if (time_after(jiffies, timeout)) {
1853 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1854 rb->rptr, rb->wptr);
1855 return -ETIMEDOUT;
1856 }
1857 } while (rb->rptr != rb->wptr);
1858
1859 return 0;
1860}
1861
1862/* Caller must hold the device mutex. */
1863int adreno_idle(struct kgsl_device *device)
1864{
1865 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001866 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301867 unsigned long wait_time;
1868 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001869 unsigned int prev_reg_val[hang_detect_regs_count];
1870
1871 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001872
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001873 kgsl_cffdump_regpoll(device->id,
1874 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001875 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001876
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001877retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001878 /* First, wait for the ringbuffer to drain */
1879 if (adreno_ringbuffer_drain(device, prev_reg_val))
1880 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001881
1882 /* now, wait for the GPU to finish its operations */
Jordan Crousea29a2e02012-08-14 09:09:23 -06001883 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
1884 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1885
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001887 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1888 &rbbm_status);
1889 if (adreno_is_a2xx(adreno_dev)) {
1890 if (rbbm_status == 0x110)
1891 return 0;
1892 } else {
1893 if (!(rbbm_status & 0x80000000))
1894 return 0;
1895 }
Tarun Karra3335f142012-06-19 14:11:48 -07001896
1897 /* Dont wait for timeout, detect hang faster.
1898 */
1899 if (time_after(jiffies, wait_time_part)) {
1900 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001901 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001902 if ((adreno_hang_detect(device, prev_reg_val)))
1903 goto err;
1904 }
1905
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001906 }
1907
1908err:
1909 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001910 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1911 !adreno_dump_and_recover(device)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001912 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001913 goto retry;
1914 }
1915 return -ETIMEDOUT;
1916}
1917
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08001918/**
1919 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
1920 * rbbm_status register
1921 * @device - Pointer to the GPU device whose idle status is to be
1922 * checked
1923 * @returns - Returns whether the core is idle (based on rbbm_status)
1924 * false if the core is active, true if the core is idle
1925 */
1926static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
1927{
1928 unsigned int reg_rbbm_status;
1929 bool status = false;
1930 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1931
1932 /* Is the core idle? */
1933 adreno_regread(device,
1934 adreno_dev->gpudev->reg_rbbm_status,
1935 &reg_rbbm_status);
1936
1937 if (adreno_is_a2xx(adreno_dev)) {
1938 if (reg_rbbm_status == 0x110)
1939 status = true;
1940 } else {
1941 if (!(reg_rbbm_status & 0x80000000))
1942 status = true;
1943 }
1944 return status;
1945}
1946
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001947static unsigned int adreno_isidle(struct kgsl_device *device)
1948{
1949 int status = false;
1950 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1951 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001952
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001953 WARN_ON(device->state == KGSL_STATE_INIT);
1954 /* If the device isn't active, don't force it on. */
1955 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001956 /* Is the ring buffer is empty? */
1957 GSL_RB_GET_READPTR(rb, &rb->rptr);
1958 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1959 /* Is the core idle? */
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08001960 status = is_adreno_rbbm_status_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001961 }
1962 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001963 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001964 }
1965 return status;
1966}
1967
1968/* Caller must hold the device mutex. */
1969static int adreno_suspend_context(struct kgsl_device *device)
1970{
1971 int status = 0;
1972 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1973
1974 /* switch to NULL ctxt */
1975 if (adreno_dev->drawctxt_active != NULL) {
1976 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001977 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001978 }
1979
1980 return status;
1981}
1982
Jordan Crouse233b2092012-04-18 09:31:09 -06001983/* Find a memory structure attached to an adreno context */
1984
1985struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1986 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1987{
1988 struct kgsl_context *context;
1989 struct adreno_context *adreno_context = NULL;
1990 int next = 0;
1991
1992 while (1) {
1993 context = idr_get_next(&device->context_idr, &next);
1994 if (context == NULL)
1995 break;
1996
1997 adreno_context = (struct adreno_context *)context->devctxt;
1998
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07001999 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2000 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002001 struct kgsl_memdesc *desc;
2002
2003 desc = &adreno_context->gpustate;
2004 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2005 return desc;
2006
2007 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2008 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2009 return desc;
2010 }
2011 next = next + 1;
2012 }
2013
2014 return NULL;
2015}
2016
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002017struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002018 unsigned int pt_base,
2019 unsigned int gpuaddr,
2020 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002021{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002022 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002023 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2024 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2025
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002026 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2027 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002028
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002029 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2030 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002031
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002032 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2033 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002034
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002035 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2036 size))
2037 return &device->mmu.setstate_memory;
2038
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002039 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002040
2041 if (entry)
2042 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002043
Jordan Crouse233b2092012-04-18 09:31:09 -06002044 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002045}
2046
2047uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2048 unsigned int gpuaddr, unsigned int size)
2049{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002050 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002051
2052 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2053
2054 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002055}
2056
2057void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2058 unsigned int *value)
2059{
2060 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002061 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2062 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002063
2064 if (!in_interrupt())
2065 kgsl_pre_hwaccess(device);
2066
2067 /*ensure this read finishes before the next one.
2068 * i.e. act like normal readl() */
2069 *value = __raw_readl(reg);
2070 rmb();
2071}
2072
2073void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2074 unsigned int value)
2075{
2076 unsigned int *reg;
2077
Jordan Crouse7501d452012-04-19 08:58:44 -06002078 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002079
2080 if (!in_interrupt())
2081 kgsl_pre_hwaccess(device);
2082
2083 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002084 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002085
2086 /*ensure previous writes post before this one,
2087 * i.e. act like normal writel() */
2088 wmb();
2089 __raw_writel(value, reg);
2090}
2091
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002092static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2093{
2094 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002095 if (k_ctxt != NULL) {
2096 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002097 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2098 context_id = KGSL_CONTEXT_INVALID;
2099 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2100 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002101 }
2102
2103 return context_id;
2104}
2105
Jordan Crouse313faf62012-11-20 15:12:28 -07002106static void adreno_next_event(struct kgsl_device *device,
2107 struct kgsl_event *event)
2108{
2109 int status;
2110 unsigned int ref_ts, enableflag;
2111 unsigned int context_id = _get_context_id(event->context);
2112 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2113
2114 status = kgsl_check_timestamp(device, event->context, event->timestamp);
2115 if (!status) {
2116 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2117 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2118 /*
2119 * Barrier is needed here to make sure the read from memstore
2120 * has posted
2121 */
2122
2123 mb();
2124
2125 if (enableflag) {
2126 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
2127 KGSL_MEMSTORE_OFFSET(context_id,
2128 ref_wait_ts));
2129
2130 /* Make sure the memstore read has posted */
2131 mb();
2132 if (timestamp_cmp(ref_ts, event->timestamp) >= 0) {
2133 kgsl_sharedmem_writel(&device->memstore,
2134 KGSL_MEMSTORE_OFFSET(context_id,
2135 ref_wait_ts), event->timestamp);
2136 /* Make sure the memstore write is posted */
2137 wmb();
2138 }
2139 } else {
2140 unsigned int cmds[2];
2141 kgsl_sharedmem_writel(&device->memstore,
2142 KGSL_MEMSTORE_OFFSET(context_id,
2143 ref_wait_ts), event->timestamp);
2144 enableflag = 1;
2145 kgsl_sharedmem_writel(&device->memstore,
2146 KGSL_MEMSTORE_OFFSET(context_id,
2147 ts_cmp_enable), enableflag);
2148
2149 /* Make sure the memstore write gets posted */
2150 wmb();
2151
2152 /*
2153 * submit a dummy packet so that even if all
2154 * commands upto timestamp get executed we will still
2155 * get an interrupt
2156 */
2157 cmds[0] = cp_type3_packet(CP_NOP, 1);
2158 cmds[1] = 0;
2159
2160 if (adreno_dev->drawctxt_active)
2161 adreno_ringbuffer_issuecmds_intr(device,
2162 event->context, &cmds[0], 2);
2163 }
2164 }
2165}
2166
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002167static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002168 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002169{
2170 int status;
2171 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002172 unsigned int context_id;
2173
2174 mutex_lock(&device->mutex);
2175 context_id = _get_context_id(context);
2176 /*
2177 * If the context ID is invalid, we are in a race with
2178 * the context being destroyed by userspace so bail.
2179 */
2180 if (context_id == KGSL_CONTEXT_INVALID) {
2181 KGSL_DRV_WARN(device, "context was detached");
2182 status = -EINVAL;
2183 goto unlock;
2184 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002185
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002186 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002187 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002188 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002189 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002190 mb();
2191
2192 if (enableflag) {
2193 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002194 KGSL_MEMSTORE_OFFSET(context_id,
2195 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002196 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002197 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002198 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002199 KGSL_MEMSTORE_OFFSET(context_id,
2200 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002201 wmb();
2202 }
2203 } else {
2204 unsigned int cmds[2];
2205 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002206 KGSL_MEMSTORE_OFFSET(context_id,
2207 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208 enableflag = 1;
2209 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002210 KGSL_MEMSTORE_OFFSET(context_id,
2211 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002212 wmb();
2213 /* submit a dummy packet so that even if all
2214 * commands upto timestamp get executed we will still
2215 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002216 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002217 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002218
Harsh Vardhan Dwivedi32f968d2012-12-05 10:35:13 -07002219 if (context)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002220 adreno_ringbuffer_issuecmds_intr(device,
2221 context, &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002222 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002223 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002224unlock:
2225 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002226
2227 return status;
2228}
2229
2230/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002231 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002232 placing a process in wait q. For conditional interrupts we expect the
2233 process to already be in its wait q when its exit condition checking
2234 function is called.
2235*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002236#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002237({ \
2238 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002239 if (io) \
2240 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2241 else \
2242 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002243 __ret; \
2244})
2245
Tarun Karra3335f142012-06-19 14:11:48 -07002246
2247
2248unsigned int adreno_hang_detect(struct kgsl_device *device,
2249 unsigned int *prev_reg_val)
2250{
2251 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2252 unsigned int curr_reg_val[hang_detect_regs_count];
2253 unsigned int hang_detected = 1;
2254 unsigned int i;
2255
2256 if (!adreno_dev->fast_hang_detect)
2257 return 0;
2258
Jordan Crousecca61142012-11-20 10:54:24 -07002259 if (is_adreno_rbbm_status_idle(device)) {
2260
2261 /*
2262 * On A20X if the RPTR != WPTR and the device is idle, then
2263 * the last write to WPTR probably failed to latch so write it
2264 * again
2265 */
2266
2267 if (adreno_is_a2xx(adreno_dev)) {
2268 unsigned int rptr;
2269 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2270 if (rptr != adreno_dev->ringbuffer.wptr)
2271 adreno_regwrite(device, REG_CP_RB_WPTR,
2272 adreno_dev->ringbuffer.wptr);
2273 }
2274
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002275 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002276 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002277
Tarun Karra3335f142012-06-19 14:11:48 -07002278 for (i = 0; i < hang_detect_regs_count; i++) {
Jordan Crouseb5c80482012-10-03 09:38:41 -06002279
2280 if (hang_detect_regs[i] == 0)
2281 continue;
2282
Tarun Karra3335f142012-06-19 14:11:48 -07002283 adreno_regread(device, hang_detect_regs[i],
2284 &curr_reg_val[i]);
2285 if (curr_reg_val[i] != prev_reg_val[i]) {
2286 prev_reg_val[i] = curr_reg_val[i];
2287 hang_detected = 0;
2288 }
2289 }
2290
2291 return hang_detected;
2292}
2293
Jordan Crouse92446a62012-11-15 11:00:06 -07002294/**
2295 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2296 * @device - pointer to a KGSL device structure
2297 * @context - pointer to the active KGSL context
2298 * @timestamp - the timestamp that the process was waiting for
2299 *
2300 * Process a possible GPU hang and try to recover from it cleanly
2301 */
2302static int adreno_handle_hang(struct kgsl_device *device,
2303 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002304{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002305 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002306 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002307 unsigned int ts_issued;
Tarun Karra3335f142012-06-19 14:11:48 -07002308
Jordan Crouse92446a62012-11-15 11:00:06 -07002309 /* Do one last check to see if we somehow made it through */
2310 if (kgsl_check_timestamp(device, context, timestamp))
2311 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002312
2313 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002314
Jeremy Gebben63904832012-02-07 16:10:55 -07002315 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002316 "Device hang detected while waiting for timestamp: "
2317 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2318 "wptr: 0x%x\n",
2319 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002320 adreno_dev->ringbuffer.wptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002321
2322 /* Return 0 after a successful recovery */
2323 if (!adreno_dump_and_recover(device))
2324 return 0;
2325
2326 return -ETIMEDOUT;
2327}
2328
2329static int _check_pending_timestamp(struct kgsl_device *device,
2330 struct kgsl_context *context, unsigned int timestamp)
2331{
2332 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2333 unsigned int context_id = _get_context_id(context);
2334 unsigned int ts_issued;
2335
2336 if (context_id == KGSL_CONTEXT_INVALID)
2337 return -EINVAL;
2338
2339 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2340
2341 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2342 return 0;
2343
2344 if (context && !context->wait_on_invalid_ts) {
2345 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2346 context_id, timestamp, context_id, ts_issued);
2347
2348 /* Only print this message once */
2349 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002350 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002351
2352 return -EINVAL;
2353}
2354
2355/**
2356 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2357 * @device - pointer to a KGSL device structure
2358 * @context - pointer to the active kgsl context
2359 * @timestamp - GPU timestamp to wait for
2360 * @msecs - amount of time to wait (in milliseconds)
2361 *
2362 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2363 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2364 * one if it happened. Otherwise, spend most of our time in an interruptible
2365 * wait for the timestamp interrupt to be processed. This function must be
2366 * called with the mutex already held.
2367 */
2368static int adreno_waittimestamp(struct kgsl_device *device,
2369 struct kgsl_context *context,
2370 unsigned int timestamp,
2371 unsigned int msecs)
2372{
2373 static unsigned int io_cnt;
2374 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2375 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2376 unsigned int context_id = _get_context_id(context);
2377 unsigned int prev_reg_val[hang_detect_regs_count];
2378 unsigned int time_elapsed = 0;
2379 unsigned int wait;
2380 int ts_compare = 1;
2381 int io, ret = -ETIMEDOUT;
2382
2383 /* Get out early if the context has already been destroyed */
2384
2385 if (context_id == KGSL_CONTEXT_INVALID) {
2386 KGSL_DRV_WARN(device, "context was detached");
2387 return -EINVAL;
2388 }
2389
2390 /*
2391 * Check to see if the requested timestamp is "newer" then the last
2392 * timestamp issued. If it is complain once and return error. Only
2393 * print the message once per context so that badly behaving
2394 * applications don't spam the logs
2395 */
2396
2397 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2398 if (_check_pending_timestamp(device, context, timestamp))
2399 return -EINVAL;
2400
2401 /* Reset the invalid timestamp flag on a valid wait */
2402 context->wait_on_invalid_ts = false;
2403 }
2404
2405
2406 /* Clear the registers used for hang detection */
2407 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2408
2409 /*
2410 * On the first time through the loop only wait 100ms.
2411 * this gives enough time for the engine to start moving and oddly
2412 * provides better hang detection results than just going the full
2413 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2414 * is if msecs happens to be < 100ms then just use the full timeout
2415 */
2416
2417 wait = 100;
2418
2419 do {
2420 long status;
2421
2422 if (wait > (msecs - time_elapsed))
2423 wait = msecs - time_elapsed;
2424
2425 /*
2426 * if the timestamp happens while we're not
2427 * waiting, there's a chance that an interrupt
2428 * will not be generated and thus the timestamp
2429 * work needs to be queued.
2430 */
2431
2432 if (kgsl_check_timestamp(device, context, timestamp)) {
2433 queue_work(device->work_queue, &device->ts_expired_ws);
2434 ret = 0;
2435 break;
2436 }
2437
2438 /* Check to see if the GPU is hung */
2439 if (adreno_hang_detect(device, prev_reg_val)) {
2440 ret = adreno_handle_hang(device, context, timestamp);
2441 break;
2442 }
2443
2444 /*
2445 * For proper power accounting sometimes we need to call
2446 * io_wait_interruptible_timeout and sometimes we need to call
2447 * plain old wait_interruptible_timeout. We call the regular
2448 * timeout N times out of 100, where N is a number specified by
2449 * the current power level
2450 */
2451
2452 io_cnt = (io_cnt + 1) % 100;
2453 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2454 ? 0 : 1;
2455
2456 mutex_unlock(&device->mutex);
2457
2458 /* Wait for a timestamp event */
2459 status = kgsl_wait_event_interruptible_timeout(
2460 device->wait_queue,
2461 kgsl_check_interrupt_timestamp(device, context,
2462 timestamp), msecs_to_jiffies(wait), io);
2463
2464 mutex_lock(&device->mutex);
2465
2466 /*
2467 * If status is non zero then either the condition was satisfied
2468 * or there was an error. In either event, this is the end of
2469 * the line for us
2470 */
2471
2472 if (status != 0) {
2473 ret = (status > 0) ? 0 : (int) status;
2474 break;
2475 }
2476
2477 time_elapsed += wait;
2478
2479 /* If user specified timestamps are being used, wait at least
2480 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
2481 * issue a IB for a timestamp before checking to see if the
2482 * current timestamp we are waiting for is valid or not
2483 */
2484
2485 if (ts_compare && (adreno_ctx &&
2486 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
2487 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
2488 ret = _check_pending_timestamp(device, context,
2489 timestamp);
2490 if (ret)
2491 break;
2492
2493 /* Don't do this check again */
2494 ts_compare = 0;
2495
2496 /*
2497 * Reset the invalid timestamp flag on a valid
2498 * wait
2499 */
2500 context->wait_on_invalid_ts = false;
2501 }
2502 }
2503
2504 /*
2505 * all subsequent trips through the loop wait the full
2506 * KGSL_TIMEOUT_PART interval
2507 */
2508 wait = KGSL_TIMEOUT_PART;
2509
2510 } while (!msecs || time_elapsed < msecs);
2511
2512 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002513}
2514
2515static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002516 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002517{
2518 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002519 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002520
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002521 /*
2522 * If the context ID is invalid, we are in a race with
2523 * the context being destroyed by userspace so bail.
2524 */
2525 if (context_id == KGSL_CONTEXT_INVALID) {
2526 KGSL_DRV_WARN(device, "context was detached");
2527 return timestamp;
2528 }
Jordan Crousec659f382012-04-16 11:10:41 -06002529 switch (type) {
2530 case KGSL_TIMESTAMP_QUEUED: {
2531 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2532 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2533
2534 timestamp = rb->timestamp[context_id];
2535 break;
2536 }
2537 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002538 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002539 break;
2540 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002541 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002542 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2543 break;
2544 }
2545
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002546 rmb();
2547
2548 return timestamp;
2549}
2550
2551static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2552 unsigned int cmd, void *data)
2553{
2554 int result = 0;
2555 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2556 struct kgsl_context *context;
2557
2558 switch (cmd) {
2559 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2560 binbase = data;
2561
2562 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2563 if (context) {
2564 adreno_drawctxt_set_bin_base_offset(
2565 dev_priv->device, context, binbase->offset);
2566 } else {
2567 result = -EINVAL;
2568 KGSL_DRV_ERR(dev_priv->device,
2569 "invalid drawctxt drawctxt_id %d "
2570 "device_id=%d\n",
2571 binbase->drawctxt_id, dev_priv->device->id);
2572 }
2573 break;
2574
2575 default:
2576 KGSL_DRV_INFO(dev_priv->device,
2577 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002578 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002579 break;
2580 }
2581 return result;
2582
2583}
2584
2585static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2586{
2587 gpu_freq /= 1000000;
2588 return ticks / gpu_freq;
2589}
2590
2591static void adreno_power_stats(struct kgsl_device *device,
2592 struct kgsl_power_stats *stats)
2593{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002594 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002595 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002596 unsigned int cycles;
2597
2598 /* Get the busy cycles counted since the counter was last reset */
2599 /* Calling this function also resets and restarts the counter */
2600
2601 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002602
2603 /* In order to calculate idle you have to have run the algorithm *
2604 * at least once to get a start time. */
2605 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002606 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002607 stats->total_time = tmp - pwr->time;
2608 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002609 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002610 pwrlevels[device->pwrctrl.active_pwrlevel].
2611 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002612 } else {
2613 stats->total_time = 0;
2614 stats->busy_time = 0;
2615 pwr->time = ktime_to_us(ktime_get());
2616 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002617}
2618
2619void adreno_irqctrl(struct kgsl_device *device, int state)
2620{
Jordan Crousea78c9172011-07-11 13:14:09 -06002621 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2622 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002623}
2624
Jordan Croused6535882012-06-20 08:22:16 -06002625static unsigned int adreno_gpuid(struct kgsl_device *device,
2626 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002627{
2628 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2629
Jordan Croused6535882012-06-20 08:22:16 -06002630 /* Some applications need to know the chip ID too, so pass
2631 * that as a parameter */
2632
2633 if (chipid != NULL)
2634 *chipid = adreno_dev->chip_id;
2635
Jordan Crousea0758f22011-12-07 11:19:22 -07002636 /* Standard KGSL gpuid format:
2637 * top word is 0x0002 for 2D or 0x0003 for 3D
2638 * Bottom word is core specific identifer
2639 */
2640
2641 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2642}
2643
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002644static const struct kgsl_functable adreno_functable = {
2645 /* Mandatory functions */
2646 .regread = adreno_regread,
2647 .regwrite = adreno_regwrite,
2648 .idle = adreno_idle,
2649 .isidle = adreno_isidle,
2650 .suspend_context = adreno_suspend_context,
2651 .start = adreno_start,
2652 .stop = adreno_stop,
2653 .getproperty = adreno_getproperty,
2654 .waittimestamp = adreno_waittimestamp,
2655 .readtimestamp = adreno_readtimestamp,
2656 .issueibcmds = adreno_ringbuffer_issueibcmds,
2657 .ioctl = adreno_ioctl,
2658 .setup_pt = adreno_setup_pt,
2659 .cleanup_pt = adreno_cleanup_pt,
2660 .power_stats = adreno_power_stats,
2661 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002662 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002663 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002664 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002665 /* Optional functions */
2666 .setstate = adreno_setstate,
2667 .drawctxt_create = adreno_drawctxt_create,
2668 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002669 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002670 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07002671 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002672};
2673
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002674static struct platform_driver adreno_platform_driver = {
2675 .probe = adreno_probe,
2676 .remove = __devexit_p(adreno_remove),
2677 .suspend = kgsl_suspend_driver,
2678 .resume = kgsl_resume_driver,
2679 .id_table = adreno_id_table,
2680 .driver = {
2681 .owner = THIS_MODULE,
2682 .name = DEVICE_3D_NAME,
2683 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002684 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002685 }
2686};
2687
2688static int __init kgsl_3d_init(void)
2689{
2690 return platform_driver_register(&adreno_platform_driver);
2691}
2692
2693static void __exit kgsl_3d_exit(void)
2694{
2695 platform_driver_unregister(&adreno_platform_driver);
2696}
2697
2698module_init(kgsl_3d_init);
2699module_exit(kgsl_3d_exit);
2700
2701MODULE_DESCRIPTION("3D Graphics driver");
2702MODULE_VERSION("1.2");
2703MODULE_LICENSE("GPL v2");
2704MODULE_ALIAS("platform:kgsl_3d");