blob: 88756c0be48bccf6f192e7bfbb737ee16c438ddf [file] [log] [blame]
Carter Cooper8179f5a2012-12-17 11:32:27 -07001/* Copyright (c) 2002,2007-2013, 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);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700302 if (context == NULL)
303 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600304 adreno_ctx = context->devctxt;
305
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600306 if (kgsl_mmu_enable_clk(&device->mmu,
307 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700308 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600309
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600310 cmds += __adreno_add_idle_indirect_cmds(cmds,
311 device->mmu.setstate_memory.gpuaddr +
312 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
313
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600314 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600315 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
316 device->mmu.setstate_memory.gpuaddr +
317 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
318 else
319 cmds += adreno_add_bank_change_cmds(cmds,
320 KGSL_IOMMU_CONTEXT_USER,
321 device->mmu.setstate_memory.gpuaddr +
322 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
323
Tarun Karra9c070822012-11-27 16:43:51 -0700324 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
325
326 /* Acquire GPU-CPU sync Lock here */
327 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
328
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700329 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
330 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600331 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600332 /*
333 * We need to perfrom the following operations for all
334 * IOMMU units
335 */
336 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700337 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
338 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600339 /*
340 * Set address of the new pagetable by writng to IOMMU
341 * TTBR0 register
342 */
343 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700344 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
345 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600346 *cmds++ = reg_pt_val;
347 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
348 *cmds++ = 0x00000000;
349
350 /*
351 * Read back the ttbr0 register as a barrier to ensure
352 * above writes have completed
353 */
354 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700355 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
356 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600357 reg_pt_val,
358 device->mmu.setstate_memory.gpuaddr +
359 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600360 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600361 }
362 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
363 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700364 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600365 */
366 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700367 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
368 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700369
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600370 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700371 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
372 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700373 KGSL_IOMMU_CTX_TLBIALL);
374 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600375
376 cmds += __adreno_add_idle_indirect_cmds(cmds,
377 device->mmu.setstate_memory.gpuaddr +
378 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
379
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600380 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700381 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
382 KGSL_IOMMU_CONTEXT_USER,
383 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700384 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600385 device->mmu.setstate_memory.gpuaddr +
386 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
387 }
388 }
389
Tarun Karra9c070822012-11-27 16:43:51 -0700390 /* Release GPU-CPU sync Lock here */
391 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
392
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600393 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600394 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700395 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
396 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600397 device->mmu.setstate_memory.gpuaddr +
398 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
399 else
400 cmds += adreno_add_bank_change_cmds(cmds,
401 KGSL_IOMMU_CONTEXT_PRIV,
402 device->mmu.setstate_memory.gpuaddr +
403 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
404
Tarun Karra9c070822012-11-27 16:43:51 -0700405 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
406
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600407 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600408 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700409 /* invalidate all base pointers */
410 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
411 *cmds++ = 0x7fff;
412 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600413 /* This returns the per context timestamp but we need to
414 * use the global timestamp for iommu clock disablement */
415 adreno_ringbuffer_issuecmds(device, adreno_ctx,
416 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600417 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600418 kgsl_mmu_disable_clk_on_ts(&device->mmu,
419 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600420 }
Tarun Karra9c070822012-11-27 16:43:51 -0700421
422 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
423 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
424 BUG();
425 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600426}
427
428static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600429 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600430 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431{
432 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
433 unsigned int link[32];
434 unsigned int *cmds = &link[0];
435 int sizedwords = 0;
436 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600437 struct kgsl_context *context;
438 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600440 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530441 * Fix target freeze issue by adding TLB flush for each submit
442 * on A20X based targets.
443 */
444 if (adreno_is_a20x(adreno_dev))
445 flags |= KGSL_MMUFLAGS_TLBFLUSH;
446 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600447 * If possible, then set the state via the command stream to avoid
448 * a CPU idle. Otherwise, use the default setstate which uses register
449 * writes For CFF dump we must idle and use the registers so that it is
450 * easier to filter out the mmu accesses from the dump
451 */
452 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600453 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700454 if (context == NULL)
455 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600456 adreno_ctx = context->devctxt;
457
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
459 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600460 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461 *cmds++ = 0x00000000;
462
463 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600464 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700465 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600466 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467 sizedwords += 4;
468 }
469
470 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
471 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600472 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 1);
474 *cmds++ = 0x00000000;
475 sizedwords += 2;
476 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600477 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 *cmds++ = mh_mmu_invalidate;
479 sizedwords += 2;
480 }
481
482 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600483 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 /* HW workaround: to resolve MMU page fault interrupts
485 * caused by the VGT.It prevents the CP PFP from filling
486 * the VGT DMA request fifo too early,thereby ensuring
487 * that the VGT will not fetch vertex/bin data until
488 * after the page table base register has been updated.
489 *
490 * Two null DRAW_INDX_BIN packets are inserted right
491 * after the page table base update, followed by a
492 * wait for idle. The null packets will fill up the
493 * VGT DMA request fifo and prevent any further
494 * vertex/bin updates from occurring until the wait
495 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600496 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 *cmds++ = (0x4 << 16) |
498 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
499 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600500 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600501 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600502 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 *cmds++ = 0; /* viz query info */
504 *cmds++ = 0x0003C004; /* draw indicator */
505 *cmds++ = 0; /* bin base */
506 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600507 *cmds++ =
508 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600510 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 *cmds++ = 0; /* viz query info */
512 *cmds++ = 0x0003C004; /* draw indicator */
513 *cmds++ = 0; /* bin base */
514 *cmds++ = 3; /* bin size */
515 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600516 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600518 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 *cmds++ = 0x00000000;
520 sizedwords += 21;
521 }
522
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600523
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600525 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 *cmds++ = 0x7fff; /* invalidate all base pointers */
527 sizedwords += 2;
528 }
529
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600530 adreno_ringbuffer_issuecmds(device, adreno_ctx,
531 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600533 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600534 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600535 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536}
537
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600538static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600539 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600540 uint32_t flags)
541{
542 /* call the mmu specific handler */
543 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600544 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600545 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600546 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600547}
548
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700550a3xx_getchipid(struct kgsl_device *device)
551{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600552 struct kgsl_device_platform_data *pdata =
553 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700554
Jordan Crouse54154c62012-03-27 16:33:26 -0600555 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600556 * All current A3XX chipids are detected at the SOC level. Leave this
557 * function here to support any future GPUs that have working
558 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600559 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700560
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600561 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700562}
563
564static unsigned int
565a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566{
567 unsigned int chipid = 0;
568 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600569 struct kgsl_device_platform_data *pdata =
570 kgsl_device_get_drvdata(device);
571
572 /* If the chip id is set at the platform level, then just use that */
573
574 if (pdata->chipid != 0)
575 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576
577 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
578 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
579 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
580
581 /*
582 * adreno 22x gpus are indicated by coreid 2,
583 * but REG_RBBM_PERIPHID1 always contains 0 for this field
584 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600585 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586 chipid = 2 << 24;
587 else
588 chipid = (coreid & 0xF) << 24;
589
590 chipid |= ((majorid >> 4) & 0xF) << 16;
591
592 minorid = ((revid >> 0) & 0xFF);
593
594 patchid = ((revid >> 16) & 0xFF);
595
596 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530597 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598 if (cpu_is_qsd8x50())
599 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530600 else if (cpu_is_msm8625() && minorid == 0)
601 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 chipid |= (minorid << 8) | patchid;
604
605 return chipid;
606}
607
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700608static unsigned int
609adreno_getchipid(struct kgsl_device *device)
610{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600611 struct kgsl_device_platform_data *pdata =
612 kgsl_device_get_drvdata(device);
613
614 /*
615 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
616 * an A2XX processor
617 */
618
619 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700620 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600621 else
622 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700623}
624
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625static inline bool _rev_match(unsigned int id, unsigned int entry)
626{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600627 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700629
630static void
631adreno_identify_gpu(struct adreno_device *adreno_dev)
632{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600633 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634
635 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
636
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600637 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
638 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
639 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
640 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641
Jordan Crouse505df9c2011-07-28 08:37:59 -0600642 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
643 if (core == adreno_gpulist[i].core &&
644 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600645 _rev_match(minor, adreno_gpulist[i].minor) &&
646 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 }
649
Jordan Crouse505df9c2011-07-28 08:37:59 -0600650 if (i == ARRAY_SIZE(adreno_gpulist)) {
651 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
652 return;
653 }
654
655 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
656 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
657 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
658 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700659 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
660 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700661 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600662 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Tarun Karra9c070822012-11-27 16:43:51 -0700663 adreno_dev->gpulist_index = i;
664
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665}
666
Lokesh Batra805e1e12012-08-03 08:34:06 -0600667static struct platform_device_id adreno_id_table[] = {
668 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
669 {},
670};
671
672MODULE_DEVICE_TABLE(platform, adreno_id_table);
673
674static struct of_device_id adreno_match_table[] = {
675 { .compatible = "qcom,kgsl-3d0", },
676 {}
677};
678
679static inline int adreno_of_read_property(struct device_node *node,
680 const char *prop, unsigned int *ptr)
681{
682 int ret = of_property_read_u32(node, prop, ptr);
683 if (ret)
684 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
685 return ret;
686}
687
688static struct device_node *adreno_of_find_subnode(struct device_node *parent,
689 const char *name)
690{
691 struct device_node *child;
692
693 for_each_child_of_node(parent, child) {
694 if (of_device_is_compatible(child, name))
695 return child;
696 }
697
698 return NULL;
699}
700
701static int adreno_of_get_pwrlevels(struct device_node *parent,
702 struct kgsl_device_platform_data *pdata)
703{
704 struct device_node *node, *child;
705 int ret = -EINVAL;
706
707 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
708
709 if (node == NULL) {
710 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
711 return -EINVAL;
712 }
713
714 pdata->num_levels = 0;
715
716 for_each_child_of_node(node, child) {
717 unsigned int index;
718 struct kgsl_pwrlevel *level;
719
720 if (adreno_of_read_property(child, "reg", &index))
721 goto done;
722
723 if (index >= KGSL_MAX_PWRLEVELS) {
724 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
725 index);
726 continue;
727 }
728
729 if (index >= pdata->num_levels)
730 pdata->num_levels = index + 1;
731
732 level = &pdata->pwrlevel[index];
733
734 if (adreno_of_read_property(child, "qcom,gpu-freq",
735 &level->gpu_freq))
736 goto done;
737
738 if (adreno_of_read_property(child, "qcom,bus-freq",
739 &level->bus_freq))
740 goto done;
741
742 if (adreno_of_read_property(child, "qcom,io-fraction",
743 &level->io_fraction))
744 level->io_fraction = 0;
745 }
746
747 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
748 &pdata->init_level))
749 pdata->init_level = 1;
750
751 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
752 KGSL_CORE_ERR("Initial power level out of range\n");
753 pdata->init_level = 1;
754 }
755
756 ret = 0;
757done:
758 return ret;
759
760}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600761
762static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
763{
764 struct device_node *node, *child;
765 struct msm_dcvs_core_info *info = NULL;
766 int count = 0;
767 int ret = -EINVAL;
768
769 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
770 if (node == NULL)
771 return ERR_PTR(-EINVAL);
772
773 info = kzalloc(sizeof(*info), GFP_KERNEL);
774
775 if (info == NULL) {
776 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
777 ret = -ENOMEM;
778 goto err;
779 }
780
781 for_each_child_of_node(node, child)
782 count++;
783
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700784 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600785
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700786 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600787 sizeof(struct msm_dcvs_freq_entry),
788 GFP_KERNEL);
789
790 if (info->freq_tbl == NULL) {
791 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700792 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600793 sizeof(struct msm_dcvs_freq_entry));
794 ret = -ENOMEM;
795 goto err;
796 }
797
798 for_each_child_of_node(node, child) {
799 unsigned int index;
800
801 if (adreno_of_read_property(child, "reg", &index))
802 goto err;
803
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700804 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600805 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
806 index);
807 continue;
808 }
809
810 if (adreno_of_read_property(child, "qcom,freq",
811 &info->freq_tbl[index].freq))
812 goto err;
813
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700814 if (adreno_of_read_property(child, "qcom,voltage",
815 &info->freq_tbl[index].voltage))
816 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600817
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700818 if (adreno_of_read_property(child, "qcom,is_trans_level",
819 &info->freq_tbl[index].is_trans_level))
820 info->freq_tbl[index].is_trans_level = 0;
821
822 if (adreno_of_read_property(child, "qcom,active-energy-offset",
823 &info->freq_tbl[index].active_energy_offset))
824 info->freq_tbl[index].active_energy_offset = 0;
825
826 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
827 &info->freq_tbl[index].leakage_energy_offset))
828 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600829 }
830
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700831 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
832 goto err;
833
834 info->sensors = kzalloc(info->num_cores *
835 sizeof(int),
836 GFP_KERNEL);
837
838 for (count = 0; count < info->num_cores; count++) {
839 if (adreno_of_read_property(node, "qcom,sensors",
840 &(info->sensors[count])))
841 goto err;
842 }
843
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700844 if (adreno_of_read_property(node, "qcom,core-core-type",
845 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600846 goto err;
847
848 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
849 &info->algo_param.disable_pc_threshold))
850 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700851 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
852 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600853 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700854 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
855 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600856 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600857 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
858 &info->algo_param.em_max_util_pct))
859 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700860 if (adreno_of_read_property(node, "qcom,algo-group-id",
861 &info->algo_param.group_id))
862 goto err;
863 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
864 &info->algo_param.max_freq_chg_time_us))
865 goto err;
866 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
867 &info->algo_param.slack_mode_dynamic))
868 goto err;
869 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
870 &info->algo_param.slack_weight_thresh_pct))
871 goto err;
872 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
873 &info->algo_param.slack_time_min_us))
874 goto err;
875 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
876 &info->algo_param.slack_time_max_us))
877 goto err;
878 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
879 &info->algo_param.ss_win_size_min_us))
880 goto err;
881 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
882 &info->algo_param.ss_win_size_max_us))
883 goto err;
884 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
885 &info->algo_param.ss_util_pct))
886 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800887 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
888 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600889 goto err;
890
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700891 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
892 &info->energy_coeffs.active_coeff_a))
893 goto err;
894 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
895 &info->energy_coeffs.active_coeff_b))
896 goto err;
897 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
898 &info->energy_coeffs.active_coeff_c))
899 goto err;
900 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
901 &info->energy_coeffs.leakage_coeff_a))
902 goto err;
903 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
904 &info->energy_coeffs.leakage_coeff_b))
905 goto err;
906 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
907 &info->energy_coeffs.leakage_coeff_c))
908 goto err;
909 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
910 &info->energy_coeffs.leakage_coeff_d))
911 goto err;
912
913 if (adreno_of_read_property(node, "qcom,power-current-temp",
914 &info->power_param.current_temp))
915 goto err;
916
Lokesh Batra805e1e12012-08-03 08:34:06 -0600917 return info;
918
919err:
920 if (info)
921 kfree(info->freq_tbl);
922
923 kfree(info);
924
925 return ERR_PTR(ret);
926}
927
928static int adreno_of_get_iommu(struct device_node *parent,
929 struct kgsl_device_platform_data *pdata)
930{
931 struct device_node *node, *child;
932 struct kgsl_device_iommu_data *data = NULL;
933 struct kgsl_iommu_ctx *ctxs = NULL;
934 u32 reg_val[2];
935 int ctx_index = 0;
936
937 node = of_parse_phandle(parent, "iommu", 0);
938 if (node == NULL)
939 return -EINVAL;
940
941 data = kzalloc(sizeof(*data), GFP_KERNEL);
942 if (data == NULL) {
943 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
944 goto err;
945 }
946
947 if (of_property_read_u32_array(node, "reg", reg_val, 2))
948 goto err;
949
950 data->physstart = reg_val[0];
951 data->physend = data->physstart + reg_val[1] - 1;
952
953 data->iommu_ctx_count = 0;
954
955 for_each_child_of_node(node, child)
956 data->iommu_ctx_count++;
957
958 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
959 GFP_KERNEL);
960
961 if (ctxs == NULL) {
962 KGSL_CORE_ERR("kzalloc(%d) failed\n",
963 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
964 goto err;
965 }
966
967 for_each_child_of_node(node, child) {
968 int ret = of_property_read_string(child, "label",
969 &ctxs[ctx_index].iommu_ctx_name);
970
971 if (ret) {
972 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
973 goto err;
974 }
975
976 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
977 &ctxs[ctx_index].ctx_id))
978 goto err;
979
980 ctx_index++;
981 }
982
983 data->iommu_ctxs = ctxs;
984
985 pdata->iommu_data = data;
986 pdata->iommu_count = 1;
987
988 return 0;
989
990err:
991 kfree(ctxs);
992 kfree(data);
993
994 return -EINVAL;
995}
996
997static int adreno_of_get_pdata(struct platform_device *pdev)
998{
999 struct kgsl_device_platform_data *pdata = NULL;
1000 struct kgsl_device *device;
1001 int ret = -EINVAL;
1002
1003 pdev->id_entry = adreno_id_table;
1004
1005 pdata = pdev->dev.platform_data;
1006 if (pdata)
1007 return 0;
1008
1009 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1010 KGSL_CORE_ERR("Unable to read 'label'\n");
1011 goto err;
1012 }
1013
1014 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1015 goto err;
1016
1017 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1018 if (pdata == NULL) {
1019 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1020 ret = -ENOMEM;
1021 goto err;
1022 }
1023
1024 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1025 &pdata->chipid))
1026 goto err;
1027
1028 /* pwrlevel Data */
1029 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1030 if (ret)
1031 goto err;
1032
1033 /* Default value is 83, if not found in DT */
1034 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1035 &pdata->idle_timeout))
1036 pdata->idle_timeout = 83;
1037
1038 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1039 &pdata->nap_allowed))
1040 pdata->nap_allowed = 1;
1041
1042 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1043 &pdata->clk_map))
1044 goto err;
1045
1046 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1047
1048 if (device->id != KGSL_DEVICE_3D0)
1049 goto err;
1050
1051 /* Bus Scale Data */
1052
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001053 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001054 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1055 ret = PTR_ERR(pdata->bus_scale_table);
1056 goto err;
1057 }
1058
1059 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1060 if (IS_ERR_OR_NULL(pdata->core_info)) {
1061 ret = PTR_ERR(pdata->core_info);
1062 goto err;
1063 }
1064
1065 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1066 if (ret)
1067 goto err;
1068
1069 pdev->dev.platform_data = pdata;
1070 return 0;
1071
1072err:
1073 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001074 if (pdata->core_info)
1075 kfree(pdata->core_info->freq_tbl);
1076 kfree(pdata->core_info);
1077
1078 if (pdata->iommu_data)
1079 kfree(pdata->iommu_data->iommu_ctxs);
1080
1081 kfree(pdata->iommu_data);
1082 }
1083
1084 kfree(pdata);
1085
1086 return ret;
1087}
1088
liu zhong7dfa2a32012-04-27 19:11:01 -07001089#ifdef CONFIG_MSM_OCMEM
1090static int
1091adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1092{
Jordan Crousec0978202012-08-29 14:35:51 -06001093 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001094 return 0;
1095
1096 /* OCMEM is only needed once, do not support consective allocation */
1097 if (adreno_dev->ocmem_hdl != NULL)
1098 return 0;
1099
1100 adreno_dev->ocmem_hdl =
1101 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1102 if (adreno_dev->ocmem_hdl == NULL)
1103 return -ENOMEM;
1104
1105 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001106 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001107
1108 return 0;
1109}
1110
1111static void
1112adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1113{
Jordan Crousec0978202012-08-29 14:35:51 -06001114 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001115 return;
1116
1117 if (adreno_dev->ocmem_hdl == NULL)
1118 return;
1119
1120 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1121 adreno_dev->ocmem_hdl = NULL;
1122}
1123#else
1124static int
1125adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1126{
1127 return 0;
1128}
1129
1130static void
1131adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1132{
1133}
1134#endif
1135
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001136static int __devinit
1137adreno_probe(struct platform_device *pdev)
1138{
1139 struct kgsl_device *device;
1140 struct adreno_device *adreno_dev;
1141 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001142 bool is_dt;
1143
1144 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1145
1146 if (is_dt && pdev->dev.of_node) {
1147 status = adreno_of_get_pdata(pdev);
1148 if (status)
1149 goto error_return;
1150 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151
1152 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1153 adreno_dev = ADRENO_DEVICE(device);
1154 device->parentdev = &pdev->dev;
1155
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001156 status = adreno_ringbuffer_init(device);
1157 if (status != 0)
1158 goto error;
1159
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001160 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161 if (status)
1162 goto error_close_rb;
1163
1164 adreno_debugfs_init(device);
1165
1166 kgsl_pwrscale_init(device);
1167 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1168
1169 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1170 return 0;
1171
1172error_close_rb:
1173 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1174error:
1175 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001176error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 return status;
1178}
1179
1180static int __devexit adreno_remove(struct platform_device *pdev)
1181{
1182 struct kgsl_device *device;
1183 struct adreno_device *adreno_dev;
1184
1185 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1186 adreno_dev = ADRENO_DEVICE(device);
1187
1188 kgsl_pwrscale_detach_policy(device);
1189 kgsl_pwrscale_close(device);
1190
1191 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1192 kgsl_device_platform_remove(device);
1193
1194 return 0;
1195}
1196
1197static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1198{
1199 int status = -EINVAL;
1200 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001202 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1203 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001204
1205 /* Power up the device */
1206 kgsl_pwrctrl_enable(device);
1207
1208 /* Identify the specific GPU */
1209 adreno_identify_gpu(adreno_dev);
1210
Tarun Karra9c070822012-11-27 16:43:51 -07001211 if (adreno_ringbuffer_read_pm4_ucode(device)) {
1212 KGSL_DRV_ERR(device, "Reading pm4 microcode failed %s\n",
1213 adreno_dev->pm4_fwfile);
1214 BUG_ON(1);
1215 }
1216
1217 if (adreno_ringbuffer_read_pfp_ucode(device)) {
1218 KGSL_DRV_ERR(device, "Reading pfp microcode failed %s\n",
1219 adreno_dev->pfp_fwfile);
1220 BUG_ON(1);
1221 }
1222
Jordan Crouse505df9c2011-07-28 08:37:59 -06001223 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1224 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1225 adreno_dev->chip_id);
1226 goto error_clk_off;
1227 }
1228
Tarun Karra9c070822012-11-27 16:43:51 -07001229
1230 /*
1231 * Check if firmware supports the sync lock PM4 packets needed
1232 * for IOMMUv1
1233 */
1234
1235 if ((adreno_dev->pm4_fw_version >=
1236 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pm4_ver) &&
1237 (adreno_dev->pfp_fw_version >=
1238 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pfp_ver))
1239 device->mmu.flags |= KGSL_MMU_FLAGS_IOMMU_SYNC;
1240
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001241 /* Set up the MMU */
1242 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001243 /*
1244 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1245 * on older gpus
1246 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001247 if (adreno_is_a20x(adreno_dev)) {
1248 device->mh.mh_intf_cfg1 = 0;
1249 device->mh.mh_intf_cfg2 = 0;
1250 }
1251
1252 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001253 }
1254
Tarun Karra3335f142012-06-19 14:11:48 -07001255 /* Assign correct RBBM status register to hang detect regs
1256 */
1257 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1258
Jordan Crouseb5c80482012-10-03 09:38:41 -06001259 /* Add A3XX specific registers for hang detection */
1260 if (adreno_is_a3xx(adreno_dev)) {
1261 hang_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1262 hang_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
Tarun Karra6e750d72013-01-04 10:28:40 -08001263 hang_detect_regs[8] = A3XX_RBBM_PERFCTR_SP_6_LO;
1264 hang_detect_regs[9] = A3XX_RBBM_PERFCTR_SP_6_HI;
1265 hang_detect_regs[10] = A3XX_RBBM_PERFCTR_SP_5_LO;
1266 hang_detect_regs[11] = A3XX_RBBM_PERFCTR_SP_5_HI;
Jordan Crouseb5c80482012-10-03 09:38:41 -06001267 }
1268
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001269 status = kgsl_mmu_start(device);
1270 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271 goto error_clk_off;
1272
liu zhong7dfa2a32012-04-27 19:11:01 -07001273 status = adreno_ocmem_gmem_malloc(adreno_dev);
1274 if (status) {
1275 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1276 goto error_mmu_off;
1277 }
1278
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001279 /* Start the GPU */
1280 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281
1282 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001283 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001284
1285 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001286 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001287 /* While recovery is on we do not want timer to
1288 * fire and attempt to change any device state */
1289 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1290 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001291 return 0;
1292 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001295
1296error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001297 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001298
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001299error_clk_off:
1300 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301
1302 return status;
1303}
1304
1305static int adreno_stop(struct kgsl_device *device)
1306{
1307 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1308
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309 adreno_dev->drawctxt_active = NULL;
1310
1311 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1312
Shubhraprakash Das79447952012-04-26 18:12:23 -06001313 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001315 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301316 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001317 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001318
liu zhong7dfa2a32012-04-27 19:11:01 -07001319 adreno_ocmem_gmem_free(adreno_dev);
1320
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001321 /* Power down the device */
1322 kgsl_pwrctrl_disable(device);
1323
1324 return 0;
1325}
1326
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001327static void adreno_mark_context_status(struct kgsl_device *device,
1328 int recovery_status)
1329{
1330 struct kgsl_context *context;
1331 int next = 0;
1332 /*
1333 * Set the reset status of all contexts to
1334 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1335 * since thats the guilty party, if recovery failed then
1336 * mark all as guilty
1337 */
1338 while ((context = idr_get_next(&device->context_idr, &next))) {
1339 struct adreno_context *adreno_context = context->devctxt;
1340 if (recovery_status) {
1341 context->reset_status =
1342 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1343 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1344 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1345 context->reset_status) {
Carter Cooper8179f5a2012-12-17 11:32:27 -07001346 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG |
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001347 CTXT_FLAGS_GPU_HANG_RECOVERED))
1348 context->reset_status =
1349 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1350 else
1351 context->reset_status =
1352 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1353 }
1354 next = next + 1;
1355 }
1356}
1357
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001358static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1359{
1360 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1361 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1362 struct kgsl_context *context;
1363 struct adreno_context *temp_adreno_context;
1364 int next = 0;
1365
1366 while ((context = idr_get_next(&device->context_idr, &next))) {
1367 temp_adreno_context = context->devctxt;
1368 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1369 kgsl_sharedmem_writel(&device->memstore,
1370 KGSL_MEMSTORE_OFFSET(context->id,
1371 soptimestamp),
1372 rb->timestamp[context->id]);
1373 kgsl_sharedmem_writel(&device->memstore,
1374 KGSL_MEMSTORE_OFFSET(context->id,
1375 eoptimestamp),
1376 rb->timestamp[context->id]);
1377 }
1378 next = next + 1;
1379 }
1380}
1381
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001382static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1383{
1384 vfree(rec_data->rb_buffer);
1385 vfree(rec_data->bad_rb_buffer);
Tarun Karradeeecc02013-01-21 23:42:17 -08001386 vfree(rec_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001387}
1388
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001389static int _find_start_of_cmd_seq(struct adreno_ringbuffer *rb,
1390 unsigned int *ptr,
1391 bool inc)
1392{
1393 int status = -EINVAL;
1394 unsigned int val1;
1395 unsigned int size = rb->buffer_desc.size;
1396 unsigned int start_ptr = *ptr;
1397
1398 while ((start_ptr / sizeof(unsigned int)) != rb->wptr) {
1399 if (inc)
1400 start_ptr = adreno_ringbuffer_inc_wrapped(start_ptr,
1401 size);
1402 else
1403 start_ptr = adreno_ringbuffer_dec_wrapped(start_ptr,
1404 size);
1405 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, start_ptr);
1406 if (KGSL_CMD_IDENTIFIER == val1) {
1407 if ((start_ptr / sizeof(unsigned int)) != rb->wptr)
1408 start_ptr = adreno_ringbuffer_dec_wrapped(
1409 start_ptr, size);
1410 *ptr = start_ptr;
1411 status = 0;
1412 break;
1413 }
1414 }
1415 return status;
1416}
1417
1418static int _find_cmd_seq_after_eop_ts(struct adreno_ringbuffer *rb,
1419 unsigned int *rb_rptr,
1420 unsigned int global_eop,
1421 bool inc)
1422{
1423 int status = -EINVAL;
1424 unsigned int temp_rb_rptr = *rb_rptr;
1425 unsigned int size = rb->buffer_desc.size;
1426 unsigned int val[3];
1427 int i = 0;
1428 bool check = false;
1429
1430 if (inc && temp_rb_rptr / sizeof(unsigned int) != rb->wptr)
1431 return status;
1432
1433 do {
1434 /*
1435 * when decrementing we need to decrement first and
1436 * then read make sure we cover all the data
1437 */
1438 if (!inc)
1439 temp_rb_rptr = adreno_ringbuffer_dec_wrapped(
1440 temp_rb_rptr, size);
1441 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i],
1442 temp_rb_rptr);
1443
1444 if (check && ((inc && val[i] == global_eop) ||
1445 (!inc && (val[i] ==
1446 cp_type3_packet(CP_MEM_WRITE, 2) ||
1447 val[i] == CACHE_FLUSH_TS)))) {
1448 /* decrement i, i.e i = (i - 1 + 3) % 3 if
1449 * we are going forward, else increment i */
1450 i = (i + 2) % 3;
1451 if (val[i] == rb->device->memstore.gpuaddr +
1452 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1453 eoptimestamp)) {
1454 int j = ((i + 2) % 3);
1455 if ((inc && (val[j] == CACHE_FLUSH_TS ||
1456 val[j] == cp_type3_packet(
1457 CP_MEM_WRITE, 2))) ||
1458 (!inc && val[j] == global_eop)) {
1459 /* Found the global eop */
1460 status = 0;
1461 break;
1462 }
1463 }
1464 /* if no match found then increment i again
1465 * since we decremented before matching */
1466 i = (i + 1) % 3;
1467 }
1468 if (inc)
1469 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(
1470 temp_rb_rptr, size);
1471
1472 i = (i + 1) % 3;
1473 if (2 == i)
1474 check = true;
1475 } while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr);
1476 /* temp_rb_rptr points to the command stream after global eop,
1477 * move backward till the start of command sequence */
1478 if (!status) {
1479 status = _find_start_of_cmd_seq(rb, &temp_rb_rptr, false);
1480 if (!status) {
1481 *rb_rptr = temp_rb_rptr;
1482 KGSL_DRV_ERR(rb->device,
1483 "Offset of cmd sequence after eop timestamp: 0x%x\n",
1484 temp_rb_rptr / sizeof(unsigned int));
1485 }
1486 }
1487 if (status)
1488 KGSL_DRV_ERR(rb->device,
1489 "Failed to find the command sequence after eop timestamp\n");
1490 return status;
1491}
1492
1493static int _find_hanging_ib_sequence(struct adreno_ringbuffer *rb,
1494 unsigned int *rb_rptr,
1495 unsigned int ib1)
1496{
1497 int status = -EINVAL;
1498 unsigned int temp_rb_rptr = *rb_rptr;
1499 unsigned int size = rb->buffer_desc.size;
1500 unsigned int val[2];
1501 int i = 0;
1502 bool check = false;
1503 bool ctx_switch = false;
1504
1505 while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr) {
1506 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i], temp_rb_rptr);
1507
1508 if (check && val[i] == ib1) {
1509 /* decrement i, i.e i = (i - 1 + 2) % 2 */
1510 i = (i + 1) % 2;
1511 if (adreno_cmd_is_ib(val[i])) {
1512 /* go till start of command sequence */
1513 status = _find_start_of_cmd_seq(rb,
1514 &temp_rb_rptr, false);
1515 KGSL_DRV_INFO(rb->device,
1516 "Found the hanging IB at offset 0x%x\n",
1517 temp_rb_rptr / sizeof(unsigned int));
1518 break;
1519 }
1520 /* if no match the increment i since we decremented
1521 * before checking */
1522 i = (i + 1) % 2;
1523 }
1524 /* Make sure you do not encounter a context switch twice, we can
1525 * encounter it once for the bad context as the start of search
1526 * can point to the context switch */
1527 if (val[i] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
1528 if (ctx_switch) {
1529 KGSL_DRV_ERR(rb->device,
1530 "Context switch encountered before bad "
1531 "IB found\n");
1532 break;
1533 }
1534 ctx_switch = true;
1535 }
1536 i = (i + 1) % 2;
1537 if (1 == i)
1538 check = true;
1539 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(temp_rb_rptr,
1540 size);
1541 }
1542 if (!status)
1543 *rb_rptr = temp_rb_rptr;
1544 return status;
1545}
1546
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001547static int adreno_setup_recovery_data(struct kgsl_device *device,
1548 struct adreno_recovery_data *rec_data)
1549{
1550 int ret = 0;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001551 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1552 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001553 struct kgsl_context *context;
1554 struct adreno_context *adreno_context;
1555 unsigned int rb_rptr = rb->wptr * sizeof(unsigned int);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001556
1557 memset(rec_data, 0, sizeof(*rec_data));
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001558 rec_data->start_of_replay_cmds = 0xFFFFFFFF;
1559 rec_data->replay_for_snapshot = 0xFFFFFFFF;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001560
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001561 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001562
1563 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1564 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1565 current_context));
1566
1567 kgsl_sharedmem_readl(&device->memstore,
Tarun Karradeeecc02013-01-21 23:42:17 -08001568 &rec_data->global_eop,
1569 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1570 eoptimestamp));
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001571
1572 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1573 if (!rec_data->rb_buffer) {
1574 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1575 rb->buffer_desc.size);
1576 return -ENOMEM;
1577 }
1578
1579 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1580 if (!rec_data->bad_rb_buffer) {
1581 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1582 rb->buffer_desc.size);
1583 ret = -ENOMEM;
1584 goto done;
1585 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001586
1587 rec_data->good_rb_buffer = vmalloc(rb->buffer_desc.size);
1588 if (!rec_data->good_rb_buffer) {
1589 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1590 rb->buffer_desc.size);
1591 ret = -ENOMEM;
1592 goto done;
1593 }
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001594 rec_data->fault = device->mmu.fault;
Tarun Karradeeecc02013-01-21 23:42:17 -08001595 rec_data->step = adreno_dev->ft_policy;
1596
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001597 /* find the start of bad command sequence in rb */
1598 context = idr_find(&device->context_idr, rec_data->context_id);
1599 /* Look for the command stream that is right after the global eop */
1600
1601 if (!context) {
1602 /*
1603 * If there is no context then recovery does not need to
1604 * replay anything, just reset GPU and thats it
1605 */
1606 goto done;
1607 }
1608 ret = _find_cmd_seq_after_eop_ts(rb, &rb_rptr,
1609 rec_data->global_eop + 1, false);
1610 if (ret)
1611 goto done;
1612
1613 rec_data->start_of_replay_cmds = rb_rptr;
1614
1615 adreno_context = context->devctxt;
1616 if (adreno_context->flags & CTXT_FLAGS_PREAMBLE) {
1617 if (rec_data->ib1) {
1618 ret = _find_hanging_ib_sequence(rb,
1619 &rb_rptr, rec_data->ib1);
1620 if (ret) {
1621 KGSL_DRV_ERR(device,
1622 "Start not found for replay IB sequence\n");
1623 ret = 0;
1624 goto done;
1625 }
1626 rec_data->start_of_replay_cmds = rb_rptr;
1627 rec_data->replay_for_snapshot = rb_rptr;
1628 }
1629 }
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001630
1631done:
1632 if (ret) {
1633 vfree(rec_data->rb_buffer);
1634 vfree(rec_data->bad_rb_buffer);
Tarun Karradeeecc02013-01-21 23:42:17 -08001635 vfree(rec_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001636 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637 return ret;
1638}
1639
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001640static int
Tarun Karradeeecc02013-01-21 23:42:17 -08001641_adreno_restart_device(struct kgsl_device *device,
1642 struct kgsl_context *context)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001643{
Tarun Karradeeecc02013-01-21 23:42:17 -08001644
1645 struct adreno_context *adreno_context = context->devctxt;
1646
1647 /* restart device */
1648 if (adreno_stop(device)) {
1649 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1650 return 1;
1651 }
1652
1653 if (adreno_start(device, true)) {
1654 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1655 return 1;
1656 }
1657
1658 if (context)
1659 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1660 KGSL_MEMSTORE_GLOBAL);
1661
1662 /* If iommu is used then we need to make sure that the iommu clocks
1663 * are on since there could be commands in pipeline that touch iommu */
1664 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1665 if (kgsl_mmu_enable_clk(&device->mmu,
1666 KGSL_IOMMU_CONTEXT_USER))
1667 return 1;
1668 }
1669
1670 return 0;
1671}
1672
1673static int
1674_adreno_recovery_resubmit(struct kgsl_device *device,
1675 struct adreno_ringbuffer *rb,
1676 struct kgsl_context *context,
1677 struct adreno_recovery_data *rec_data,
1678 unsigned int *buff, unsigned int size)
1679{
1680 unsigned int ret = 0;
1681
1682 if (_adreno_restart_device(device, context))
1683 return 1;
1684
1685 if (size) {
1686
1687 /* submit commands and wait for them to pass */
1688 adreno_ringbuffer_restore(rb, buff, size);
1689
1690 ret = adreno_idle(device);
1691 }
1692
1693 return ret;
1694}
1695
1696
1697static int
1698_adreno_recover_hang(struct kgsl_device *device,
1699 struct adreno_recovery_data *rec_data)
1700{
1701 int ret = 0, i;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001702 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1703 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1704 struct kgsl_context *context;
1705 struct adreno_context *adreno_context = NULL;
1706 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1707
1708 context = idr_find(&device->context_idr, rec_data->context_id);
1709 if (context == NULL) {
1710 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1711 rec_data->context_id);
1712 } else {
1713 adreno_context = context->devctxt;
1714 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001715 /*
1716 * set the invalid ts flag to 0 for this context since we have
1717 * detected a hang for it
1718 */
1719 context->wait_on_invalid_ts = false;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001720 }
1721
1722 /* Extract valid contents from rb which can still be executed after
1723 * hang */
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001724 adreno_ringbuffer_extract(rb, rec_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001725
Tarun Karradeeecc02013-01-21 23:42:17 -08001726 /* Do not try the bad commands if hang is due to a fault */
1727 if (rec_data->fault)
1728 goto play_good_cmds;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001729
Tarun Karradeeecc02013-01-21 23:42:17 -08001730 if (rec_data->step == FT_REPLAY_BAD_CTXT_CMDS) {
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001731
Tarun Karradeeecc02013-01-21 23:42:17 -08001732 ret = _adreno_recovery_resubmit(device, rb, context, rec_data,
1733 rec_data->bad_rb_buffer, rec_data->bad_rb_size);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001734
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001735 if (ret)
Tarun Karradeeecc02013-01-21 23:42:17 -08001736 rec_data->step = FT_NOT_IB_BAD_CTXT_CMDS;
1737 else
1738 goto play_good_cmds;
1739
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001740 }
1741
Tarun Karradeeecc02013-01-21 23:42:17 -08001742 if (rec_data->step == FT_NOT_IB_BAD_CTXT_CMDS) {
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001743
Tarun Karradeeecc02013-01-21 23:42:17 -08001744 for (i = 0; i < rec_data->bad_rb_size; i++) {
1745 if ((rec_data->bad_rb_buffer[i] ==
1746 CP_HDR_INDIRECT_BUFFER_PFD) &&
1747 (rec_data->bad_rb_buffer[i+1] ==
1748 rec_data->ib1)) {
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001749
Tarun Karradeeecc02013-01-21 23:42:17 -08001750 rec_data->bad_rb_buffer[i] = cp_nop_packet(2);
1751 rec_data->bad_rb_buffer[i+1] =
1752 KGSL_NOP_IB_IDENTIFIER;
1753 rec_data->bad_rb_buffer[i+2] =
1754 KGSL_NOP_IB_IDENTIFIER;
1755 break;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001756 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001757 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001758
1759 if ((i == (rec_data->bad_rb_size)) || (!rec_data->ib1)) {
1760 KGSL_DRV_ERR(device, "Bad IB to NOP not found\n");
1761 rec_data->step = FT_FAIL_BAD_CTXT_CMDS;
1762 goto play_good_cmds;
1763 }
1764
1765 ret = _adreno_recovery_resubmit(device, rb, context, rec_data,
1766 rec_data->bad_rb_buffer, rec_data->bad_rb_size);
1767
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001768 if (ret) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001769 KGSL_DRV_ERR(device, "NOP faulty IB unsuccessful\n");
1770 rec_data->step = FT_SKIP_EOF_BAD_CTXT_CMDS;
1771 } else
1772 goto play_good_cmds;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001773 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001774
1775 if (rec_data->step == FT_SKIP_EOF_BAD_CTXT_CMDS) {
1776
1777 for (i = 0; i < rec_data->bad_rb_size; i++) {
1778 if (rec_data->bad_rb_buffer[i] ==
1779 KGSL_END_OF_FRAME_IDENTIFIER) {
1780 rec_data->bad_rb_buffer[0] = cp_nop_packet(i);
1781 break;
1782 }
1783 }
1784
1785 /* EOF not found in RB, discard till EOF in
1786 next IB submission */
1787 if (i == rec_data->bad_rb_size) {
1788 adreno_context->flags |= CTXT_FLAGS_SKIP_EOF;
1789 rec_data->bad_rb_buffer[0] = cp_nop_packet(i);
1790 }
1791
1792 ret = _adreno_recovery_resubmit(device, rb, context, rec_data,
1793 rec_data->bad_rb_buffer, rec_data->bad_rb_size);
1794
1795 if (ret) {
1796 KGSL_DRV_ERR(device, "Skip EOF unsuccessful\n");
1797 rec_data->step = FT_FAIL_BAD_CTXT_CMDS;
1798 } else
1799 goto play_good_cmds;
1800 }
1801
1802play_good_cmds:
1803
1804 if (rec_data->step == FT_FAIL_BAD_CTXT_CMDS)
1805 KGSL_DRV_ERR(device, "Bad context commands failed\n");
1806 else {
1807
1808 if (adreno_context) {
1809 adreno_context->flags = (adreno_context->flags &
1810 ~CTXT_FLAGS_GPU_HANG) | CTXT_FLAGS_GPU_HANG_RECOVERED;
1811 }
1812 adreno_dev->drawctxt_active = last_active_ctx;
1813 }
1814
1815 ret = _adreno_recovery_resubmit(device, rb, context, rec_data,
1816 rec_data->good_rb_buffer, rec_data->good_rb_size);
1817
1818 if (ret) {
1819 /* If we fail here we can try to invalidate another
1820 * context and try fault tolerance again */
1821 ret = -EAGAIN;
1822 KGSL_DRV_ERR(device, "Playing good commands unsuccessful\n");
1823 goto done;
1824 }
1825
1826
1827 /* ringbuffer now has data from the last valid context id,
1828 * so restore the active_ctx to the last valid context */
1829 if (rec_data->last_valid_ctx_id) {
1830 struct kgsl_context *last_ctx =
1831 idr_find(&device->context_idr,
1832 rec_data->last_valid_ctx_id);
1833 if (last_ctx)
1834 adreno_dev->drawctxt_active = last_ctx->devctxt;
1835 }
1836
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001837done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001838 /* Turn off iommu clocks */
1839 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1840 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001841 return ret;
1842}
1843
1844static int
1845adreno_recover_hang(struct kgsl_device *device,
1846 struct adreno_recovery_data *rec_data)
1847{
1848 int ret = 0;
1849 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1850 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1851 unsigned int timestamp;
1852
1853 KGSL_DRV_ERR(device,
1854 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1855 "Bad context_id: %u, global_eop: 0x%x\n",
1856 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1857
1858 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1859 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1860
1861 /* We may need to replay commands multiple times based on whether
1862 * multiple contexts hang the GPU */
1863 while (true) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001864
1865 ret = _adreno_recover_hang(device, rec_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001866
1867 if (-EAGAIN == ret) {
1868 /* setup new recovery parameters and retry, this
1869 * means more than 1 contexts are causing hang */
1870 adreno_destroy_recovery_data(rec_data);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001871 ret = adreno_setup_recovery_data(device, rec_data);
1872 if (ret)
1873 goto done;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001874 KGSL_DRV_ERR(device,
1875 "Retry recovery from 3D GPU hang. Recovery parameters: "
1876 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1877 rec_data->ib1, rec_data->context_id,
1878 rec_data->global_eop);
1879 } else {
1880 break;
1881 }
1882 }
1883
1884 if (ret)
1885 goto done;
1886
1887 /* Restore correct states after recovery */
1888 if (adreno_dev->drawctxt_active)
1889 device->mmu.hwpagetable =
1890 adreno_dev->drawctxt_active->pagetable;
1891 else
1892 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1893 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1894 kgsl_sharedmem_writel(&device->memstore,
1895 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1896 eoptimestamp),
1897 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07001898
1899 /* switch to NULL ctxt */
1900 if (adreno_dev->drawctxt_active != NULL)
1901 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1902
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001903done:
1904 adreno_set_max_ts_for_bad_ctxs(device);
1905 adreno_mark_context_status(device, ret);
1906 if (!ret)
1907 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1908 else
1909 KGSL_DRV_ERR(device, "Recovery failed\n");
1910 return ret;
1911}
1912
1913int
1914adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001915{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001916 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001917 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001918
1919 if (device->state == KGSL_STATE_HUNG)
1920 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001921 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001922 mutex_unlock(&device->mutex);
1923 wait_for_completion(&device->recovery_gate);
1924 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001925 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001926 result = 0;
1927 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001928 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001930 /* Detected a hang */
1931
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001932 /* Get the recovery data as soon as hang is detected */
1933 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001934 /*
1935 * Trigger an automatic dump of the state to
1936 * the console
1937 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001938 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001939
1940 /*
1941 * Make a GPU snapshot. For now, do it after the PM dump so we
1942 * can at least be sure the PM dump will work as it always has
1943 */
1944 kgsl_device_snapshot(device, 1);
1945
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001946 if (!result) {
1947 result = adreno_recover_hang(device, &rec_data);
1948 adreno_destroy_recovery_data(&rec_data);
1949 }
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001950 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001951 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001952 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001953 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001954 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1955 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001956 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001957 }
1958done:
1959 return result;
1960}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001961EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001962
1963static int adreno_getproperty(struct kgsl_device *device,
1964 enum kgsl_property_type type,
1965 void *value,
1966 unsigned int sizebytes)
1967{
1968 int status = -EINVAL;
1969 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1970
1971 switch (type) {
1972 case KGSL_PROP_DEVICE_INFO:
1973 {
1974 struct kgsl_devinfo devinfo;
1975
1976 if (sizebytes != sizeof(devinfo)) {
1977 status = -EINVAL;
1978 break;
1979 }
1980
1981 memset(&devinfo, 0, sizeof(devinfo));
1982 devinfo.device_id = device->id+1;
1983 devinfo.chip_id = adreno_dev->chip_id;
1984 devinfo.mmu_enabled = kgsl_mmu_enabled();
1985 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001986 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1987 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001988
1989 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1990 0) {
1991 status = -EFAULT;
1992 break;
1993 }
1994 status = 0;
1995 }
1996 break;
1997 case KGSL_PROP_DEVICE_SHADOW:
1998 {
1999 struct kgsl_shadowprop shadowprop;
2000
2001 if (sizebytes != sizeof(shadowprop)) {
2002 status = -EINVAL;
2003 break;
2004 }
2005 memset(&shadowprop, 0, sizeof(shadowprop));
2006 if (device->memstore.hostptr) {
2007 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2008 * anything to mmap().
2009 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07002010 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002011 shadowprop.size = device->memstore.size;
2012 /* GSL needs this to be set, even if it
2013 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002014 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2015 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002016 }
2017 if (copy_to_user(value, &shadowprop,
2018 sizeof(shadowprop))) {
2019 status = -EFAULT;
2020 break;
2021 }
2022 status = 0;
2023 }
2024 break;
2025 case KGSL_PROP_MMU_ENABLE:
2026 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002027 int mmu_prop = kgsl_mmu_enabled();
2028
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002029 if (sizebytes != sizeof(int)) {
2030 status = -EINVAL;
2031 break;
2032 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002033 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002034 status = -EFAULT;
2035 break;
2036 }
2037 status = 0;
2038 }
2039 break;
2040 case KGSL_PROP_INTERRUPT_WAITS:
2041 {
2042 int int_waits = 1;
2043 if (sizebytes != sizeof(int)) {
2044 status = -EINVAL;
2045 break;
2046 }
2047 if (copy_to_user(value, &int_waits, sizeof(int))) {
2048 status = -EFAULT;
2049 break;
2050 }
2051 status = 0;
2052 }
2053 break;
2054 default:
2055 status = -EINVAL;
2056 }
2057
2058 return status;
2059}
2060
Jordan Crousef7370f82012-04-18 09:31:07 -06002061static int adreno_setproperty(struct kgsl_device *device,
2062 enum kgsl_property_type type,
2063 void *value,
2064 unsigned int sizebytes)
2065{
2066 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08002067 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06002068
2069 switch (type) {
2070 case KGSL_PROP_PWRCTRL: {
2071 unsigned int enable;
2072 struct kgsl_device_platform_data *pdata =
2073 kgsl_device_get_drvdata(device);
2074
2075 if (sizebytes != sizeof(enable))
2076 break;
2077
2078 if (copy_from_user(&enable, (void __user *) value,
2079 sizeof(enable))) {
2080 status = -EFAULT;
2081 break;
2082 }
2083
2084 if (enable) {
2085 if (pdata->nap_allowed)
2086 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002087 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002088 kgsl_pwrscale_enable(device);
2089 } else {
2090 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002091 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002092 kgsl_pwrscale_disable(device);
2093 }
2094
2095 status = 0;
2096 }
2097 break;
2098 default:
2099 break;
2100 }
2101
2102 return status;
2103}
2104
Jordan Crousea29a2e02012-08-14 09:09:23 -06002105static int adreno_ringbuffer_drain(struct kgsl_device *device,
2106 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002107{
2108 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2109 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002110 unsigned long wait;
2111 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2112
2113 if (!(rb->flags & KGSL_FLAGS_STARTED))
2114 return 0;
2115
2116 /*
2117 * The first time into the loop, wait for 100 msecs and kick wptr again
2118 * to ensure that the hardware has updated correctly. After that, kick
2119 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2120 * expires
2121 */
2122
2123 wait = jiffies + msecs_to_jiffies(100);
2124
Jordan Crousea29a2e02012-08-14 09:09:23 -06002125 do {
2126 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002127 /* Check to see if the core is hung */
2128 if (adreno_hang_detect(device, regs))
2129 return -ETIMEDOUT;
2130
2131 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2132 }
2133 GSL_RB_GET_READPTR(rb, &rb->rptr);
2134
2135 if (time_after(jiffies, timeout)) {
2136 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2137 rb->rptr, rb->wptr);
2138 return -ETIMEDOUT;
2139 }
2140 } while (rb->rptr != rb->wptr);
2141
2142 return 0;
2143}
2144
2145/* Caller must hold the device mutex. */
2146int adreno_idle(struct kgsl_device *device)
2147{
2148 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002149 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302150 unsigned long wait_time;
2151 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07002152 unsigned int prev_reg_val[hang_detect_regs_count];
2153
2154 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002155
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002156 kgsl_cffdump_regpoll(device->id,
2157 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002158 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002159
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002160retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002161 /* First, wait for the ringbuffer to drain */
2162 if (adreno_ringbuffer_drain(device, prev_reg_val))
2163 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002164
2165 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002166 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002167 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2168
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002169 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002170 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2171 &rbbm_status);
2172 if (adreno_is_a2xx(adreno_dev)) {
2173 if (rbbm_status == 0x110)
2174 return 0;
2175 } else {
2176 if (!(rbbm_status & 0x80000000))
2177 return 0;
2178 }
Tarun Karra3335f142012-06-19 14:11:48 -07002179
2180 /* Dont wait for timeout, detect hang faster.
2181 */
2182 if (time_after(jiffies, wait_time_part)) {
2183 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002184 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07002185 if ((adreno_hang_detect(device, prev_reg_val)))
2186 goto err;
2187 }
2188
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002189 }
2190
2191err:
2192 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002193 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
2194 !adreno_dump_and_recover(device)) {
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002195 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002196 goto retry;
2197 }
2198 return -ETIMEDOUT;
2199}
2200
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002201/**
2202 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2203 * rbbm_status register
2204 * @device - Pointer to the GPU device whose idle status is to be
2205 * checked
2206 * @returns - Returns whether the core is idle (based on rbbm_status)
2207 * false if the core is active, true if the core is idle
2208 */
2209static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2210{
2211 unsigned int reg_rbbm_status;
2212 bool status = false;
2213 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2214
2215 /* Is the core idle? */
2216 adreno_regread(device,
2217 adreno_dev->gpudev->reg_rbbm_status,
2218 &reg_rbbm_status);
2219
2220 if (adreno_is_a2xx(adreno_dev)) {
2221 if (reg_rbbm_status == 0x110)
2222 status = true;
2223 } else {
2224 if (!(reg_rbbm_status & 0x80000000))
2225 status = true;
2226 }
2227 return status;
2228}
2229
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002230static unsigned int adreno_isidle(struct kgsl_device *device)
2231{
2232 int status = false;
2233 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2234 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002235
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002236 WARN_ON(device->state == KGSL_STATE_INIT);
2237 /* If the device isn't active, don't force it on. */
2238 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002239 /* Is the ring buffer is empty? */
2240 GSL_RB_GET_READPTR(rb, &rb->rptr);
2241 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
2242 /* Is the core idle? */
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002243 status = is_adreno_rbbm_status_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002244 }
2245 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002246 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002247 }
2248 return status;
2249}
2250
2251/* Caller must hold the device mutex. */
2252static int adreno_suspend_context(struct kgsl_device *device)
2253{
2254 int status = 0;
2255 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2256
2257 /* switch to NULL ctxt */
2258 if (adreno_dev->drawctxt_active != NULL) {
2259 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002260 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002261 }
2262
2263 return status;
2264}
2265
Jordan Crouse233b2092012-04-18 09:31:09 -06002266/* Find a memory structure attached to an adreno context */
2267
2268struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2269 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2270{
2271 struct kgsl_context *context;
2272 struct adreno_context *adreno_context = NULL;
2273 int next = 0;
2274
2275 while (1) {
2276 context = idr_get_next(&device->context_idr, &next);
2277 if (context == NULL)
2278 break;
2279
2280 adreno_context = (struct adreno_context *)context->devctxt;
2281
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002282 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2283 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002284 struct kgsl_memdesc *desc;
2285
2286 desc = &adreno_context->gpustate;
2287 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2288 return desc;
2289
2290 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2291 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2292 return desc;
2293 }
2294 next = next + 1;
2295 }
2296
2297 return NULL;
2298}
2299
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002300struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002301 unsigned int pt_base,
2302 unsigned int gpuaddr,
2303 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002304{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002305 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002306 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2307 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2308
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002309 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2310 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002311
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002312 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2313 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002314
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002315 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2316 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002317
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002318 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2319 size))
2320 return &device->mmu.setstate_memory;
2321
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002322 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002323
2324 if (entry)
2325 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002326
Jordan Crouse233b2092012-04-18 09:31:09 -06002327 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002328}
2329
2330uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2331 unsigned int gpuaddr, unsigned int size)
2332{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002333 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002334
2335 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2336
2337 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002338}
2339
2340void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2341 unsigned int *value)
2342{
2343 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002344 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2345 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002346
2347 if (!in_interrupt())
2348 kgsl_pre_hwaccess(device);
2349
2350 /*ensure this read finishes before the next one.
2351 * i.e. act like normal readl() */
2352 *value = __raw_readl(reg);
2353 rmb();
2354}
2355
2356void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2357 unsigned int value)
2358{
2359 unsigned int *reg;
2360
Jordan Crouse7501d452012-04-19 08:58:44 -06002361 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002362
2363 if (!in_interrupt())
2364 kgsl_pre_hwaccess(device);
2365
2366 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002367 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002368
2369 /*ensure previous writes post before this one,
2370 * i.e. act like normal writel() */
2371 wmb();
2372 __raw_writel(value, reg);
2373}
2374
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002375static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2376{
2377 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002378 if (k_ctxt != NULL) {
2379 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002380 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2381 context_id = KGSL_CONTEXT_INVALID;
2382 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2383 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002384 }
2385
2386 return context_id;
2387}
2388
Jordan Crouse313faf62012-11-20 15:12:28 -07002389static void adreno_next_event(struct kgsl_device *device,
2390 struct kgsl_event *event)
2391{
2392 int status;
2393 unsigned int ref_ts, enableflag;
2394 unsigned int context_id = _get_context_id(event->context);
2395 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2396
2397 status = kgsl_check_timestamp(device, event->context, event->timestamp);
2398 if (!status) {
2399 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2400 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2401 /*
2402 * Barrier is needed here to make sure the read from memstore
2403 * has posted
2404 */
2405
2406 mb();
2407
2408 if (enableflag) {
2409 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
2410 KGSL_MEMSTORE_OFFSET(context_id,
2411 ref_wait_ts));
2412
2413 /* Make sure the memstore read has posted */
2414 mb();
2415 if (timestamp_cmp(ref_ts, event->timestamp) >= 0) {
2416 kgsl_sharedmem_writel(&device->memstore,
2417 KGSL_MEMSTORE_OFFSET(context_id,
2418 ref_wait_ts), event->timestamp);
2419 /* Make sure the memstore write is posted */
2420 wmb();
2421 }
2422 } else {
2423 unsigned int cmds[2];
2424 kgsl_sharedmem_writel(&device->memstore,
2425 KGSL_MEMSTORE_OFFSET(context_id,
2426 ref_wait_ts), event->timestamp);
2427 enableflag = 1;
2428 kgsl_sharedmem_writel(&device->memstore,
2429 KGSL_MEMSTORE_OFFSET(context_id,
2430 ts_cmp_enable), enableflag);
2431
2432 /* Make sure the memstore write gets posted */
2433 wmb();
2434
2435 /*
2436 * submit a dummy packet so that even if all
2437 * commands upto timestamp get executed we will still
2438 * get an interrupt
2439 */
2440 cmds[0] = cp_type3_packet(CP_NOP, 1);
2441 cmds[1] = 0;
2442
2443 if (adreno_dev->drawctxt_active)
2444 adreno_ringbuffer_issuecmds_intr(device,
2445 event->context, &cmds[0], 2);
2446 }
2447 }
2448}
2449
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002450static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002451 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002452{
2453 int status;
2454 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002455 unsigned int context_id;
2456
2457 mutex_lock(&device->mutex);
2458 context_id = _get_context_id(context);
2459 /*
2460 * If the context ID is invalid, we are in a race with
2461 * the context being destroyed by userspace so bail.
2462 */
2463 if (context_id == KGSL_CONTEXT_INVALID) {
2464 KGSL_DRV_WARN(device, "context was detached");
2465 status = -EINVAL;
2466 goto unlock;
2467 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002468
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002469 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002470 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002471 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002472 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002473 mb();
2474
2475 if (enableflag) {
2476 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002477 KGSL_MEMSTORE_OFFSET(context_id,
2478 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002479 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002480 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002482 KGSL_MEMSTORE_OFFSET(context_id,
2483 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002484 wmb();
2485 }
2486 } else {
2487 unsigned int cmds[2];
2488 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002489 KGSL_MEMSTORE_OFFSET(context_id,
2490 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002491 enableflag = 1;
2492 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002493 KGSL_MEMSTORE_OFFSET(context_id,
2494 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002495 wmb();
2496 /* submit a dummy packet so that even if all
2497 * commands upto timestamp get executed we will still
2498 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002499 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002500 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002501
Vladimir Razgulin38345302013-01-22 18:41:59 -07002502 if (context && device->state != KGSL_STATE_SLUMBER)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002503 adreno_ringbuffer_issuecmds_intr(device,
2504 context, &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002505 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002506 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002507unlock:
2508 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002509
2510 return status;
2511}
2512
2513/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002514 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002515 placing a process in wait q. For conditional interrupts we expect the
2516 process to already be in its wait q when its exit condition checking
2517 function is called.
2518*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002519#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002520({ \
2521 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002522 if (io) \
2523 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2524 else \
2525 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002526 __ret; \
2527})
2528
Tarun Karra3335f142012-06-19 14:11:48 -07002529
2530
2531unsigned int adreno_hang_detect(struct kgsl_device *device,
2532 unsigned int *prev_reg_val)
2533{
2534 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2535 unsigned int curr_reg_val[hang_detect_regs_count];
2536 unsigned int hang_detected = 1;
2537 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002538 static unsigned long next_hang_detect_time;
Tarun Karra3335f142012-06-19 14:11:48 -07002539
2540 if (!adreno_dev->fast_hang_detect)
2541 return 0;
2542
Jordan Crousecca61142012-11-20 10:54:24 -07002543 if (is_adreno_rbbm_status_idle(device)) {
2544
2545 /*
2546 * On A20X if the RPTR != WPTR and the device is idle, then
2547 * the last write to WPTR probably failed to latch so write it
2548 * again
2549 */
2550
2551 if (adreno_is_a2xx(adreno_dev)) {
2552 unsigned int rptr;
2553 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2554 if (rptr != adreno_dev->ringbuffer.wptr)
2555 adreno_regwrite(device, REG_CP_RB_WPTR,
2556 adreno_dev->ringbuffer.wptr);
2557 }
2558
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002559 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002560 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002561
Tarun Karra4648db92013-02-01 17:03:40 -08002562 /*
2563 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2564 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2565 * from the last time do nothing.
2566 */
2567 if ((next_hang_detect_time) &&
2568 (time_before(jiffies, next_hang_detect_time)))
2569 return 0;
2570 else
2571 next_hang_detect_time = (jiffies +
2572 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2573
Tarun Karra3335f142012-06-19 14:11:48 -07002574 for (i = 0; i < hang_detect_regs_count; i++) {
Jordan Crouseb5c80482012-10-03 09:38:41 -06002575
2576 if (hang_detect_regs[i] == 0)
2577 continue;
2578
Tarun Karra3335f142012-06-19 14:11:48 -07002579 adreno_regread(device, hang_detect_regs[i],
2580 &curr_reg_val[i]);
2581 if (curr_reg_val[i] != prev_reg_val[i]) {
2582 prev_reg_val[i] = curr_reg_val[i];
2583 hang_detected = 0;
2584 }
2585 }
2586
2587 return hang_detected;
2588}
2589
Jordan Crouse92446a62012-11-15 11:00:06 -07002590/**
2591 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2592 * @device - pointer to a KGSL device structure
2593 * @context - pointer to the active KGSL context
2594 * @timestamp - the timestamp that the process was waiting for
2595 *
2596 * Process a possible GPU hang and try to recover from it cleanly
2597 */
2598static int adreno_handle_hang(struct kgsl_device *device,
2599 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002601 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002602 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002603 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002604 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002605
Jordan Crouse92446a62012-11-15 11:00:06 -07002606 /* Do one last check to see if we somehow made it through */
2607 if (kgsl_check_timestamp(device, context, timestamp))
2608 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002609
2610 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002611
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002612 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2613 mb();
2614
Jeremy Gebben63904832012-02-07 16:10:55 -07002615 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002616 "Device hang detected while waiting for timestamp: "
2617 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002618 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2619 context_id, timestamp, context_id, ts_issued, context_id,
2620 kgsl_readtimestamp(device, context,
2621 KGSL_TIMESTAMP_RETIRED),
2622 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002623
2624 /* Return 0 after a successful recovery */
2625 if (!adreno_dump_and_recover(device))
2626 return 0;
2627
2628 return -ETIMEDOUT;
2629}
2630
2631static int _check_pending_timestamp(struct kgsl_device *device,
2632 struct kgsl_context *context, unsigned int timestamp)
2633{
2634 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2635 unsigned int context_id = _get_context_id(context);
2636 unsigned int ts_issued;
2637
2638 if (context_id == KGSL_CONTEXT_INVALID)
2639 return -EINVAL;
2640
2641 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2642
2643 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2644 return 0;
2645
2646 if (context && !context->wait_on_invalid_ts) {
2647 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2648 context_id, timestamp, context_id, ts_issued);
2649
2650 /* Only print this message once */
2651 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002652 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002653
2654 return -EINVAL;
2655}
2656
2657/**
2658 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2659 * @device - pointer to a KGSL device structure
2660 * @context - pointer to the active kgsl context
2661 * @timestamp - GPU timestamp to wait for
2662 * @msecs - amount of time to wait (in milliseconds)
2663 *
2664 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2665 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2666 * one if it happened. Otherwise, spend most of our time in an interruptible
2667 * wait for the timestamp interrupt to be processed. This function must be
2668 * called with the mutex already held.
2669 */
2670static int adreno_waittimestamp(struct kgsl_device *device,
2671 struct kgsl_context *context,
2672 unsigned int timestamp,
2673 unsigned int msecs)
2674{
2675 static unsigned int io_cnt;
2676 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2677 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2678 unsigned int context_id = _get_context_id(context);
2679 unsigned int prev_reg_val[hang_detect_regs_count];
2680 unsigned int time_elapsed = 0;
2681 unsigned int wait;
2682 int ts_compare = 1;
2683 int io, ret = -ETIMEDOUT;
2684
2685 /* Get out early if the context has already been destroyed */
2686
2687 if (context_id == KGSL_CONTEXT_INVALID) {
2688 KGSL_DRV_WARN(device, "context was detached");
2689 return -EINVAL;
2690 }
2691
2692 /*
2693 * Check to see if the requested timestamp is "newer" then the last
2694 * timestamp issued. If it is complain once and return error. Only
2695 * print the message once per context so that badly behaving
2696 * applications don't spam the logs
2697 */
2698
2699 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2700 if (_check_pending_timestamp(device, context, timestamp))
2701 return -EINVAL;
2702
2703 /* Reset the invalid timestamp flag on a valid wait */
2704 context->wait_on_invalid_ts = false;
2705 }
2706
2707
2708 /* Clear the registers used for hang detection */
2709 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2710
2711 /*
2712 * On the first time through the loop only wait 100ms.
2713 * this gives enough time for the engine to start moving and oddly
2714 * provides better hang detection results than just going the full
2715 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2716 * is if msecs happens to be < 100ms then just use the full timeout
2717 */
2718
2719 wait = 100;
2720
2721 do {
2722 long status;
2723
Jordan Crouse92446a62012-11-15 11:00:06 -07002724 /*
2725 * if the timestamp happens while we're not
2726 * waiting, there's a chance that an interrupt
2727 * will not be generated and thus the timestamp
2728 * work needs to be queued.
2729 */
2730
2731 if (kgsl_check_timestamp(device, context, timestamp)) {
2732 queue_work(device->work_queue, &device->ts_expired_ws);
2733 ret = 0;
2734 break;
2735 }
2736
2737 /* Check to see if the GPU is hung */
2738 if (adreno_hang_detect(device, prev_reg_val)) {
2739 ret = adreno_handle_hang(device, context, timestamp);
2740 break;
2741 }
2742
2743 /*
2744 * For proper power accounting sometimes we need to call
2745 * io_wait_interruptible_timeout and sometimes we need to call
2746 * plain old wait_interruptible_timeout. We call the regular
2747 * timeout N times out of 100, where N is a number specified by
2748 * the current power level
2749 */
2750
2751 io_cnt = (io_cnt + 1) % 100;
2752 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2753 ? 0 : 1;
2754
2755 mutex_unlock(&device->mutex);
2756
2757 /* Wait for a timestamp event */
2758 status = kgsl_wait_event_interruptible_timeout(
2759 device->wait_queue,
2760 kgsl_check_interrupt_timestamp(device, context,
2761 timestamp), msecs_to_jiffies(wait), io);
2762
2763 mutex_lock(&device->mutex);
2764
2765 /*
2766 * If status is non zero then either the condition was satisfied
2767 * or there was an error. In either event, this is the end of
2768 * the line for us
2769 */
2770
2771 if (status != 0) {
2772 ret = (status > 0) ? 0 : (int) status;
2773 break;
2774 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002775 time_elapsed += wait;
2776
2777 /* If user specified timestamps are being used, wait at least
2778 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
2779 * issue a IB for a timestamp before checking to see if the
2780 * current timestamp we are waiting for is valid or not
2781 */
2782
2783 if (ts_compare && (adreno_ctx &&
2784 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
2785 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
2786 ret = _check_pending_timestamp(device, context,
2787 timestamp);
2788 if (ret)
2789 break;
2790
2791 /* Don't do this check again */
2792 ts_compare = 0;
2793
2794 /*
2795 * Reset the invalid timestamp flag on a valid
2796 * wait
2797 */
2798 context->wait_on_invalid_ts = false;
2799 }
2800 }
2801
2802 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07002803 * We want to wait the floor of KGSL_TIMEOUT_PART
2804 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07002805 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07002806
2807 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
2808 wait = KGSL_TIMEOUT_PART;
2809 else
2810 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07002811
2812 } while (!msecs || time_elapsed < msecs);
2813
2814 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002815}
2816
2817static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002818 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002819{
2820 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002821 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002822
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002823 /*
2824 * If the context ID is invalid, we are in a race with
2825 * the context being destroyed by userspace so bail.
2826 */
2827 if (context_id == KGSL_CONTEXT_INVALID) {
2828 KGSL_DRV_WARN(device, "context was detached");
2829 return timestamp;
2830 }
Jordan Crousec659f382012-04-16 11:10:41 -06002831 switch (type) {
2832 case KGSL_TIMESTAMP_QUEUED: {
2833 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2834 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2835
2836 timestamp = rb->timestamp[context_id];
2837 break;
2838 }
2839 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002840 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002841 break;
2842 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002843 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002844 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2845 break;
2846 }
2847
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002848 rmb();
2849
2850 return timestamp;
2851}
2852
2853static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2854 unsigned int cmd, void *data)
2855{
2856 int result = 0;
2857 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2858 struct kgsl_context *context;
2859
2860 switch (cmd) {
2861 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2862 binbase = data;
2863
2864 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2865 if (context) {
2866 adreno_drawctxt_set_bin_base_offset(
2867 dev_priv->device, context, binbase->offset);
2868 } else {
2869 result = -EINVAL;
2870 KGSL_DRV_ERR(dev_priv->device,
2871 "invalid drawctxt drawctxt_id %d "
2872 "device_id=%d\n",
2873 binbase->drawctxt_id, dev_priv->device->id);
2874 }
2875 break;
2876
2877 default:
2878 KGSL_DRV_INFO(dev_priv->device,
2879 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002880 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002881 break;
2882 }
2883 return result;
2884
2885}
2886
2887static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2888{
2889 gpu_freq /= 1000000;
2890 return ticks / gpu_freq;
2891}
2892
2893static void adreno_power_stats(struct kgsl_device *device,
2894 struct kgsl_power_stats *stats)
2895{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002896 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002897 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002898 unsigned int cycles;
2899
2900 /* Get the busy cycles counted since the counter was last reset */
2901 /* Calling this function also resets and restarts the counter */
2902
2903 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002904
2905 /* In order to calculate idle you have to have run the algorithm *
2906 * at least once to get a start time. */
2907 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002908 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002909 stats->total_time = tmp - pwr->time;
2910 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002911 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002912 pwrlevels[device->pwrctrl.active_pwrlevel].
2913 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002914 } else {
2915 stats->total_time = 0;
2916 stats->busy_time = 0;
2917 pwr->time = ktime_to_us(ktime_get());
2918 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002919}
2920
2921void adreno_irqctrl(struct kgsl_device *device, int state)
2922{
Jordan Crousea78c9172011-07-11 13:14:09 -06002923 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2924 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002925}
2926
Jordan Croused6535882012-06-20 08:22:16 -06002927static unsigned int adreno_gpuid(struct kgsl_device *device,
2928 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002929{
2930 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2931
Jordan Croused6535882012-06-20 08:22:16 -06002932 /* Some applications need to know the chip ID too, so pass
2933 * that as a parameter */
2934
2935 if (chipid != NULL)
2936 *chipid = adreno_dev->chip_id;
2937
Jordan Crousea0758f22011-12-07 11:19:22 -07002938 /* Standard KGSL gpuid format:
2939 * top word is 0x0002 for 2D or 0x0003 for 3D
2940 * Bottom word is core specific identifer
2941 */
2942
2943 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2944}
2945
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002946static const struct kgsl_functable adreno_functable = {
2947 /* Mandatory functions */
2948 .regread = adreno_regread,
2949 .regwrite = adreno_regwrite,
2950 .idle = adreno_idle,
2951 .isidle = adreno_isidle,
2952 .suspend_context = adreno_suspend_context,
2953 .start = adreno_start,
2954 .stop = adreno_stop,
2955 .getproperty = adreno_getproperty,
2956 .waittimestamp = adreno_waittimestamp,
2957 .readtimestamp = adreno_readtimestamp,
2958 .issueibcmds = adreno_ringbuffer_issueibcmds,
2959 .ioctl = adreno_ioctl,
2960 .setup_pt = adreno_setup_pt,
2961 .cleanup_pt = adreno_cleanup_pt,
2962 .power_stats = adreno_power_stats,
2963 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002964 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002965 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002966 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002967 /* Optional functions */
2968 .setstate = adreno_setstate,
2969 .drawctxt_create = adreno_drawctxt_create,
2970 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002971 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002972 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07002973 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002974};
2975
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002976static struct platform_driver adreno_platform_driver = {
2977 .probe = adreno_probe,
2978 .remove = __devexit_p(adreno_remove),
2979 .suspend = kgsl_suspend_driver,
2980 .resume = kgsl_resume_driver,
2981 .id_table = adreno_id_table,
2982 .driver = {
2983 .owner = THIS_MODULE,
2984 .name = DEVICE_3D_NAME,
2985 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002986 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002987 }
2988};
2989
2990static int __init kgsl_3d_init(void)
2991{
2992 return platform_driver_register(&adreno_platform_driver);
2993}
2994
2995static void __exit kgsl_3d_exit(void)
2996{
2997 platform_driver_unregister(&adreno_platform_driver);
2998}
2999
3000module_init(kgsl_3d_init);
3001module_exit(kgsl_3d_exit);
3002
3003MODULE_DESCRIPTION("3D Graphics driver");
3004MODULE_VERSION("1.2");
3005MODULE_LICENSE("GPL v2");
3006MODULE_ALIAS("platform:kgsl_3d");