blob: 5fd12302d9631a52b1d57b5d926cea1cd8a29f18 [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);
1386}
1387
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001388static int _find_start_of_cmd_seq(struct adreno_ringbuffer *rb,
1389 unsigned int *ptr,
1390 bool inc)
1391{
1392 int status = -EINVAL;
1393 unsigned int val1;
1394 unsigned int size = rb->buffer_desc.size;
1395 unsigned int start_ptr = *ptr;
1396
1397 while ((start_ptr / sizeof(unsigned int)) != rb->wptr) {
1398 if (inc)
1399 start_ptr = adreno_ringbuffer_inc_wrapped(start_ptr,
1400 size);
1401 else
1402 start_ptr = adreno_ringbuffer_dec_wrapped(start_ptr,
1403 size);
1404 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, start_ptr);
1405 if (KGSL_CMD_IDENTIFIER == val1) {
1406 if ((start_ptr / sizeof(unsigned int)) != rb->wptr)
1407 start_ptr = adreno_ringbuffer_dec_wrapped(
1408 start_ptr, size);
1409 *ptr = start_ptr;
1410 status = 0;
1411 break;
1412 }
1413 }
1414 return status;
1415}
1416
1417static int _find_cmd_seq_after_eop_ts(struct adreno_ringbuffer *rb,
1418 unsigned int *rb_rptr,
1419 unsigned int global_eop,
1420 bool inc)
1421{
1422 int status = -EINVAL;
1423 unsigned int temp_rb_rptr = *rb_rptr;
1424 unsigned int size = rb->buffer_desc.size;
1425 unsigned int val[3];
1426 int i = 0;
1427 bool check = false;
1428
1429 if (inc && temp_rb_rptr / sizeof(unsigned int) != rb->wptr)
1430 return status;
1431
1432 do {
1433 /*
1434 * when decrementing we need to decrement first and
1435 * then read make sure we cover all the data
1436 */
1437 if (!inc)
1438 temp_rb_rptr = adreno_ringbuffer_dec_wrapped(
1439 temp_rb_rptr, size);
1440 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i],
1441 temp_rb_rptr);
1442
1443 if (check && ((inc && val[i] == global_eop) ||
1444 (!inc && (val[i] ==
1445 cp_type3_packet(CP_MEM_WRITE, 2) ||
1446 val[i] == CACHE_FLUSH_TS)))) {
1447 /* decrement i, i.e i = (i - 1 + 3) % 3 if
1448 * we are going forward, else increment i */
1449 i = (i + 2) % 3;
1450 if (val[i] == rb->device->memstore.gpuaddr +
1451 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1452 eoptimestamp)) {
1453 int j = ((i + 2) % 3);
1454 if ((inc && (val[j] == CACHE_FLUSH_TS ||
1455 val[j] == cp_type3_packet(
1456 CP_MEM_WRITE, 2))) ||
1457 (!inc && val[j] == global_eop)) {
1458 /* Found the global eop */
1459 status = 0;
1460 break;
1461 }
1462 }
1463 /* if no match found then increment i again
1464 * since we decremented before matching */
1465 i = (i + 1) % 3;
1466 }
1467 if (inc)
1468 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(
1469 temp_rb_rptr, size);
1470
1471 i = (i + 1) % 3;
1472 if (2 == i)
1473 check = true;
1474 } while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr);
1475 /* temp_rb_rptr points to the command stream after global eop,
1476 * move backward till the start of command sequence */
1477 if (!status) {
1478 status = _find_start_of_cmd_seq(rb, &temp_rb_rptr, false);
1479 if (!status) {
1480 *rb_rptr = temp_rb_rptr;
1481 KGSL_DRV_ERR(rb->device,
1482 "Offset of cmd sequence after eop timestamp: 0x%x\n",
1483 temp_rb_rptr / sizeof(unsigned int));
1484 }
1485 }
1486 if (status)
1487 KGSL_DRV_ERR(rb->device,
1488 "Failed to find the command sequence after eop timestamp\n");
1489 return status;
1490}
1491
1492static int _find_hanging_ib_sequence(struct adreno_ringbuffer *rb,
1493 unsigned int *rb_rptr,
1494 unsigned int ib1)
1495{
1496 int status = -EINVAL;
1497 unsigned int temp_rb_rptr = *rb_rptr;
1498 unsigned int size = rb->buffer_desc.size;
1499 unsigned int val[2];
1500 int i = 0;
1501 bool check = false;
1502 bool ctx_switch = false;
1503
1504 while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr) {
1505 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i], temp_rb_rptr);
1506
1507 if (check && val[i] == ib1) {
1508 /* decrement i, i.e i = (i - 1 + 2) % 2 */
1509 i = (i + 1) % 2;
1510 if (adreno_cmd_is_ib(val[i])) {
1511 /* go till start of command sequence */
1512 status = _find_start_of_cmd_seq(rb,
1513 &temp_rb_rptr, false);
1514 KGSL_DRV_INFO(rb->device,
1515 "Found the hanging IB at offset 0x%x\n",
1516 temp_rb_rptr / sizeof(unsigned int));
1517 break;
1518 }
1519 /* if no match the increment i since we decremented
1520 * before checking */
1521 i = (i + 1) % 2;
1522 }
1523 /* Make sure you do not encounter a context switch twice, we can
1524 * encounter it once for the bad context as the start of search
1525 * can point to the context switch */
1526 if (val[i] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
1527 if (ctx_switch) {
1528 KGSL_DRV_ERR(rb->device,
1529 "Context switch encountered before bad "
1530 "IB found\n");
1531 break;
1532 }
1533 ctx_switch = true;
1534 }
1535 i = (i + 1) % 2;
1536 if (1 == i)
1537 check = true;
1538 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(temp_rb_rptr,
1539 size);
1540 }
1541 if (!status)
1542 *rb_rptr = temp_rb_rptr;
1543 return status;
1544}
1545
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001546static int adreno_setup_recovery_data(struct kgsl_device *device,
1547 struct adreno_recovery_data *rec_data)
1548{
1549 int ret = 0;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001550 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1551 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001552 struct kgsl_context *context;
1553 struct adreno_context *adreno_context;
1554 unsigned int rb_rptr = rb->wptr * sizeof(unsigned int);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001555
1556 memset(rec_data, 0, sizeof(*rec_data));
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001557 rec_data->start_of_replay_cmds = 0xFFFFFFFF;
1558 rec_data->replay_for_snapshot = 0xFFFFFFFF;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001559
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001560 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001561
1562 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1563 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1564 current_context));
1565
1566 kgsl_sharedmem_readl(&device->memstore,
1567 &rec_data->global_eop,
1568 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1569 eoptimestamp));
1570
1571 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1572 if (!rec_data->rb_buffer) {
1573 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1574 rb->buffer_desc.size);
1575 return -ENOMEM;
1576 }
1577
1578 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1579 if (!rec_data->bad_rb_buffer) {
1580 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1581 rb->buffer_desc.size);
1582 ret = -ENOMEM;
1583 goto done;
1584 }
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001585 rec_data->fault = device->mmu.fault;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001586 /* find the start of bad command sequence in rb */
1587 context = idr_find(&device->context_idr, rec_data->context_id);
1588 /* Look for the command stream that is right after the global eop */
1589
1590 if (!context) {
1591 /*
1592 * If there is no context then recovery does not need to
1593 * replay anything, just reset GPU and thats it
1594 */
1595 goto done;
1596 }
1597 ret = _find_cmd_seq_after_eop_ts(rb, &rb_rptr,
1598 rec_data->global_eop + 1, false);
1599 if (ret)
1600 goto done;
1601
1602 rec_data->start_of_replay_cmds = rb_rptr;
1603
1604 adreno_context = context->devctxt;
1605 if (adreno_context->flags & CTXT_FLAGS_PREAMBLE) {
1606 if (rec_data->ib1) {
1607 ret = _find_hanging_ib_sequence(rb,
1608 &rb_rptr, rec_data->ib1);
1609 if (ret) {
1610 KGSL_DRV_ERR(device,
1611 "Start not found for replay IB sequence\n");
1612 ret = 0;
1613 goto done;
1614 }
1615 rec_data->start_of_replay_cmds = rb_rptr;
1616 rec_data->replay_for_snapshot = rb_rptr;
1617 }
1618 }
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001619
1620done:
1621 if (ret) {
1622 vfree(rec_data->rb_buffer);
1623 vfree(rec_data->bad_rb_buffer);
1624 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001625 return ret;
1626}
1627
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001628static int
1629_adreno_recover_hang(struct kgsl_device *device,
1630 struct adreno_recovery_data *rec_data,
1631 bool try_bad_commands)
1632{
1633 int ret;
1634 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1635 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1636 struct kgsl_context *context;
1637 struct adreno_context *adreno_context = NULL;
1638 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1639
1640 context = idr_find(&device->context_idr, rec_data->context_id);
1641 if (context == NULL) {
1642 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1643 rec_data->context_id);
1644 } else {
1645 adreno_context = context->devctxt;
1646 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001647 /*
1648 * set the invalid ts flag to 0 for this context since we have
1649 * detected a hang for it
1650 */
1651 context->wait_on_invalid_ts = false;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001652 }
1653
1654 /* Extract valid contents from rb which can still be executed after
1655 * hang */
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001656 adreno_ringbuffer_extract(rb, rec_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001657
1658 /* restart device */
1659 ret = adreno_stop(device);
1660 if (ret) {
1661 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1662 goto done;
1663 }
1664
1665 ret = adreno_start(device, true);
1666 if (ret) {
1667 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1668 goto done;
1669 }
1670
1671 if (context)
1672 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1673 KGSL_MEMSTORE_GLOBAL);
1674
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001675 /* If iommu is used then we need to make sure that the iommu clocks
1676 * are on since there could be commands in pipeline that touch iommu */
1677 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1678 ret = kgsl_mmu_enable_clk(&device->mmu,
1679 KGSL_IOMMU_CONTEXT_USER);
1680 if (ret)
1681 goto done;
1682 }
1683
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001684 /* Do not try the bad commands if recovery has failed bad commands
1685 * once already or if hang is due to a fault */
1686 if (!try_bad_commands || rec_data->fault)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001687 rec_data->bad_rb_size = 0;
1688
1689 if (rec_data->bad_rb_size) {
1690 int idle_ret;
1691 /* submit the bad and good context commands and wait for
1692 * them to pass */
1693 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1694 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001695 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001696 if (idle_ret) {
1697 ret = adreno_stop(device);
1698 if (ret) {
1699 KGSL_DRV_ERR(device,
1700 "Device stop failed in recovery\n");
1701 goto done;
1702 }
1703 ret = adreno_start(device, true);
1704 if (ret) {
1705 KGSL_DRV_ERR(device,
1706 "Device start failed in recovery\n");
1707 goto done;
1708 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001709 if (context)
1710 kgsl_mmu_setstate(&device->mmu,
1711 adreno_context->pagetable,
1712 KGSL_MEMSTORE_GLOBAL);
1713
1714 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1715 ret = kgsl_mmu_enable_clk(&device->mmu,
1716 KGSL_IOMMU_CONTEXT_USER);
1717 if (ret)
1718 goto done;
1719 }
1720
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001721 ret = idle_ret;
1722 KGSL_DRV_ERR(device,
1723 "Bad context commands hung in recovery\n");
1724 } else {
1725 KGSL_DRV_ERR(device,
1726 "Bad context commands succeeded in recovery\n");
1727 if (adreno_context)
1728 adreno_context->flags = (adreno_context->flags &
1729 ~CTXT_FLAGS_GPU_HANG) |
1730 CTXT_FLAGS_GPU_HANG_RECOVERED;
1731 adreno_dev->drawctxt_active = last_active_ctx;
1732 }
1733 }
1734 /* If either the bad command sequence failed or we did not play it */
1735 if (ret || !rec_data->bad_rb_size) {
1736 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1737 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001738 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001739 if (ret) {
1740 /* If we fail here we can try to invalidate another
1741 * context and try recovering again */
1742 ret = -EAGAIN;
1743 goto done;
1744 }
1745 /* ringbuffer now has data from the last valid context id,
1746 * so restore the active_ctx to the last valid context */
1747 if (rec_data->last_valid_ctx_id) {
1748 struct kgsl_context *last_ctx =
1749 idr_find(&device->context_idr,
1750 rec_data->last_valid_ctx_id);
1751 if (last_ctx)
1752 adreno_dev->drawctxt_active = last_ctx->devctxt;
1753 }
1754 }
1755done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001756 /* Turn off iommu clocks */
1757 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1758 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001759 return ret;
1760}
1761
1762static int
1763adreno_recover_hang(struct kgsl_device *device,
1764 struct adreno_recovery_data *rec_data)
1765{
1766 int ret = 0;
1767 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1768 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1769 unsigned int timestamp;
1770
1771 KGSL_DRV_ERR(device,
1772 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1773 "Bad context_id: %u, global_eop: 0x%x\n",
1774 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1775
1776 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1777 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1778
1779 /* We may need to replay commands multiple times based on whether
1780 * multiple contexts hang the GPU */
1781 while (true) {
1782 if (!ret)
1783 ret = _adreno_recover_hang(device, rec_data, true);
1784 else
1785 ret = _adreno_recover_hang(device, rec_data, false);
1786
1787 if (-EAGAIN == ret) {
1788 /* setup new recovery parameters and retry, this
1789 * means more than 1 contexts are causing hang */
1790 adreno_destroy_recovery_data(rec_data);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001791 ret = adreno_setup_recovery_data(device, rec_data);
1792 if (ret)
1793 goto done;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001794 KGSL_DRV_ERR(device,
1795 "Retry recovery from 3D GPU hang. Recovery parameters: "
1796 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1797 rec_data->ib1, rec_data->context_id,
1798 rec_data->global_eop);
1799 } else {
1800 break;
1801 }
1802 }
1803
1804 if (ret)
1805 goto done;
1806
1807 /* Restore correct states after recovery */
1808 if (adreno_dev->drawctxt_active)
1809 device->mmu.hwpagetable =
1810 adreno_dev->drawctxt_active->pagetable;
1811 else
1812 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1813 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1814 kgsl_sharedmem_writel(&device->memstore,
1815 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1816 eoptimestamp),
1817 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07001818
1819 /* switch to NULL ctxt */
1820 if (adreno_dev->drawctxt_active != NULL)
1821 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1822
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001823done:
1824 adreno_set_max_ts_for_bad_ctxs(device);
1825 adreno_mark_context_status(device, ret);
1826 if (!ret)
1827 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1828 else
1829 KGSL_DRV_ERR(device, "Recovery failed\n");
1830 return ret;
1831}
1832
1833int
1834adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001835{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001836 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001837 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001838
1839 if (device->state == KGSL_STATE_HUNG)
1840 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001841 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 mutex_unlock(&device->mutex);
1843 wait_for_completion(&device->recovery_gate);
1844 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001845 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001846 result = 0;
1847 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001848 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001850 /* Detected a hang */
1851
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001852 /* Get the recovery data as soon as hang is detected */
1853 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001854 /*
1855 * Trigger an automatic dump of the state to
1856 * the console
1857 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001858 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001859
1860 /*
1861 * Make a GPU snapshot. For now, do it after the PM dump so we
1862 * can at least be sure the PM dump will work as it always has
1863 */
1864 kgsl_device_snapshot(device, 1);
1865
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001866 if (!result) {
1867 result = adreno_recover_hang(device, &rec_data);
1868 adreno_destroy_recovery_data(&rec_data);
1869 }
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001870 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001871 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001872 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001873 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001874 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1875 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001876 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001877 }
1878done:
1879 return result;
1880}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001881EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882
1883static int adreno_getproperty(struct kgsl_device *device,
1884 enum kgsl_property_type type,
1885 void *value,
1886 unsigned int sizebytes)
1887{
1888 int status = -EINVAL;
1889 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1890
1891 switch (type) {
1892 case KGSL_PROP_DEVICE_INFO:
1893 {
1894 struct kgsl_devinfo devinfo;
1895
1896 if (sizebytes != sizeof(devinfo)) {
1897 status = -EINVAL;
1898 break;
1899 }
1900
1901 memset(&devinfo, 0, sizeof(devinfo));
1902 devinfo.device_id = device->id+1;
1903 devinfo.chip_id = adreno_dev->chip_id;
1904 devinfo.mmu_enabled = kgsl_mmu_enabled();
1905 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001906 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1907 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908
1909 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1910 0) {
1911 status = -EFAULT;
1912 break;
1913 }
1914 status = 0;
1915 }
1916 break;
1917 case KGSL_PROP_DEVICE_SHADOW:
1918 {
1919 struct kgsl_shadowprop shadowprop;
1920
1921 if (sizebytes != sizeof(shadowprop)) {
1922 status = -EINVAL;
1923 break;
1924 }
1925 memset(&shadowprop, 0, sizeof(shadowprop));
1926 if (device->memstore.hostptr) {
1927 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1928 * anything to mmap().
1929 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001930 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001931 shadowprop.size = device->memstore.size;
1932 /* GSL needs this to be set, even if it
1933 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001934 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1935 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001936 }
1937 if (copy_to_user(value, &shadowprop,
1938 sizeof(shadowprop))) {
1939 status = -EFAULT;
1940 break;
1941 }
1942 status = 0;
1943 }
1944 break;
1945 case KGSL_PROP_MMU_ENABLE:
1946 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001947 int mmu_prop = kgsl_mmu_enabled();
1948
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001949 if (sizebytes != sizeof(int)) {
1950 status = -EINVAL;
1951 break;
1952 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001953 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001954 status = -EFAULT;
1955 break;
1956 }
1957 status = 0;
1958 }
1959 break;
1960 case KGSL_PROP_INTERRUPT_WAITS:
1961 {
1962 int int_waits = 1;
1963 if (sizebytes != sizeof(int)) {
1964 status = -EINVAL;
1965 break;
1966 }
1967 if (copy_to_user(value, &int_waits, sizeof(int))) {
1968 status = -EFAULT;
1969 break;
1970 }
1971 status = 0;
1972 }
1973 break;
1974 default:
1975 status = -EINVAL;
1976 }
1977
1978 return status;
1979}
1980
Jordan Crousef7370f82012-04-18 09:31:07 -06001981static int adreno_setproperty(struct kgsl_device *device,
1982 enum kgsl_property_type type,
1983 void *value,
1984 unsigned int sizebytes)
1985{
1986 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08001987 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06001988
1989 switch (type) {
1990 case KGSL_PROP_PWRCTRL: {
1991 unsigned int enable;
1992 struct kgsl_device_platform_data *pdata =
1993 kgsl_device_get_drvdata(device);
1994
1995 if (sizebytes != sizeof(enable))
1996 break;
1997
1998 if (copy_from_user(&enable, (void __user *) value,
1999 sizeof(enable))) {
2000 status = -EFAULT;
2001 break;
2002 }
2003
2004 if (enable) {
2005 if (pdata->nap_allowed)
2006 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002007 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002008 kgsl_pwrscale_enable(device);
2009 } else {
2010 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002011 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002012 kgsl_pwrscale_disable(device);
2013 }
2014
2015 status = 0;
2016 }
2017 break;
2018 default:
2019 break;
2020 }
2021
2022 return status;
2023}
2024
Jordan Crousea29a2e02012-08-14 09:09:23 -06002025static int adreno_ringbuffer_drain(struct kgsl_device *device,
2026 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002027{
2028 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2029 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002030 unsigned long wait;
2031 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2032
2033 if (!(rb->flags & KGSL_FLAGS_STARTED))
2034 return 0;
2035
2036 /*
2037 * The first time into the loop, wait for 100 msecs and kick wptr again
2038 * to ensure that the hardware has updated correctly. After that, kick
2039 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2040 * expires
2041 */
2042
2043 wait = jiffies + msecs_to_jiffies(100);
2044
Jordan Crousea29a2e02012-08-14 09:09:23 -06002045 do {
2046 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002047 /* Check to see if the core is hung */
2048 if (adreno_hang_detect(device, regs))
2049 return -ETIMEDOUT;
2050
2051 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2052 }
2053 GSL_RB_GET_READPTR(rb, &rb->rptr);
2054
2055 if (time_after(jiffies, timeout)) {
2056 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2057 rb->rptr, rb->wptr);
2058 return -ETIMEDOUT;
2059 }
2060 } while (rb->rptr != rb->wptr);
2061
2062 return 0;
2063}
2064
2065/* Caller must hold the device mutex. */
2066int adreno_idle(struct kgsl_device *device)
2067{
2068 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002069 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302070 unsigned long wait_time;
2071 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07002072 unsigned int prev_reg_val[hang_detect_regs_count];
2073
2074 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002075
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002076 kgsl_cffdump_regpoll(device->id,
2077 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002078 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002079
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002080retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002081 /* First, wait for the ringbuffer to drain */
2082 if (adreno_ringbuffer_drain(device, prev_reg_val))
2083 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002084
2085 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002086 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002087 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002089 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002090 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2091 &rbbm_status);
2092 if (adreno_is_a2xx(adreno_dev)) {
2093 if (rbbm_status == 0x110)
2094 return 0;
2095 } else {
2096 if (!(rbbm_status & 0x80000000))
2097 return 0;
2098 }
Tarun Karra3335f142012-06-19 14:11:48 -07002099
2100 /* Dont wait for timeout, detect hang faster.
2101 */
2102 if (time_after(jiffies, wait_time_part)) {
2103 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002104 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07002105 if ((adreno_hang_detect(device, prev_reg_val)))
2106 goto err;
2107 }
2108
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002109 }
2110
2111err:
2112 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002113 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
2114 !adreno_dump_and_recover(device)) {
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002115 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002116 goto retry;
2117 }
2118 return -ETIMEDOUT;
2119}
2120
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002121/**
2122 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2123 * rbbm_status register
2124 * @device - Pointer to the GPU device whose idle status is to be
2125 * checked
2126 * @returns - Returns whether the core is idle (based on rbbm_status)
2127 * false if the core is active, true if the core is idle
2128 */
2129static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2130{
2131 unsigned int reg_rbbm_status;
2132 bool status = false;
2133 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2134
2135 /* Is the core idle? */
2136 adreno_regread(device,
2137 adreno_dev->gpudev->reg_rbbm_status,
2138 &reg_rbbm_status);
2139
2140 if (adreno_is_a2xx(adreno_dev)) {
2141 if (reg_rbbm_status == 0x110)
2142 status = true;
2143 } else {
2144 if (!(reg_rbbm_status & 0x80000000))
2145 status = true;
2146 }
2147 return status;
2148}
2149
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002150static unsigned int adreno_isidle(struct kgsl_device *device)
2151{
2152 int status = false;
2153 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2154 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002155
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002156 WARN_ON(device->state == KGSL_STATE_INIT);
2157 /* If the device isn't active, don't force it on. */
2158 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002159 /* Is the ring buffer is empty? */
2160 GSL_RB_GET_READPTR(rb, &rb->rptr);
2161 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
2162 /* Is the core idle? */
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002163 status = is_adreno_rbbm_status_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002164 }
2165 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002166 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002167 }
2168 return status;
2169}
2170
2171/* Caller must hold the device mutex. */
2172static int adreno_suspend_context(struct kgsl_device *device)
2173{
2174 int status = 0;
2175 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2176
2177 /* switch to NULL ctxt */
2178 if (adreno_dev->drawctxt_active != NULL) {
2179 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002180 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181 }
2182
2183 return status;
2184}
2185
Jordan Crouse233b2092012-04-18 09:31:09 -06002186/* Find a memory structure attached to an adreno context */
2187
2188struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2189 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2190{
2191 struct kgsl_context *context;
2192 struct adreno_context *adreno_context = NULL;
2193 int next = 0;
2194
2195 while (1) {
2196 context = idr_get_next(&device->context_idr, &next);
2197 if (context == NULL)
2198 break;
2199
2200 adreno_context = (struct adreno_context *)context->devctxt;
2201
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002202 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2203 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002204 struct kgsl_memdesc *desc;
2205
2206 desc = &adreno_context->gpustate;
2207 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2208 return desc;
2209
2210 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2211 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2212 return desc;
2213 }
2214 next = next + 1;
2215 }
2216
2217 return NULL;
2218}
2219
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002220struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002221 unsigned int pt_base,
2222 unsigned int gpuaddr,
2223 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002224{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002225 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002226 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2227 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2228
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002229 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2230 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002231
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002232 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2233 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002234
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002235 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2236 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002237
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002238 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2239 size))
2240 return &device->mmu.setstate_memory;
2241
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002242 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002243
2244 if (entry)
2245 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002246
Jordan Crouse233b2092012-04-18 09:31:09 -06002247 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002248}
2249
2250uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2251 unsigned int gpuaddr, unsigned int size)
2252{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002253 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002254
2255 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2256
2257 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002258}
2259
2260void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2261 unsigned int *value)
2262{
2263 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002264 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2265 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002266
2267 if (!in_interrupt())
2268 kgsl_pre_hwaccess(device);
2269
2270 /*ensure this read finishes before the next one.
2271 * i.e. act like normal readl() */
2272 *value = __raw_readl(reg);
2273 rmb();
2274}
2275
2276void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2277 unsigned int value)
2278{
2279 unsigned int *reg;
2280
Jordan Crouse7501d452012-04-19 08:58:44 -06002281 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002282
2283 if (!in_interrupt())
2284 kgsl_pre_hwaccess(device);
2285
2286 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002287 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002288
2289 /*ensure previous writes post before this one,
2290 * i.e. act like normal writel() */
2291 wmb();
2292 __raw_writel(value, reg);
2293}
2294
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002295static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2296{
2297 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002298 if (k_ctxt != NULL) {
2299 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002300 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2301 context_id = KGSL_CONTEXT_INVALID;
2302 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2303 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002304 }
2305
2306 return context_id;
2307}
2308
Jordan Crouse313faf62012-11-20 15:12:28 -07002309static void adreno_next_event(struct kgsl_device *device,
2310 struct kgsl_event *event)
2311{
2312 int status;
2313 unsigned int ref_ts, enableflag;
2314 unsigned int context_id = _get_context_id(event->context);
2315 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2316
2317 status = kgsl_check_timestamp(device, event->context, event->timestamp);
2318 if (!status) {
2319 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2320 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2321 /*
2322 * Barrier is needed here to make sure the read from memstore
2323 * has posted
2324 */
2325
2326 mb();
2327
2328 if (enableflag) {
2329 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
2330 KGSL_MEMSTORE_OFFSET(context_id,
2331 ref_wait_ts));
2332
2333 /* Make sure the memstore read has posted */
2334 mb();
2335 if (timestamp_cmp(ref_ts, event->timestamp) >= 0) {
2336 kgsl_sharedmem_writel(&device->memstore,
2337 KGSL_MEMSTORE_OFFSET(context_id,
2338 ref_wait_ts), event->timestamp);
2339 /* Make sure the memstore write is posted */
2340 wmb();
2341 }
2342 } else {
2343 unsigned int cmds[2];
2344 kgsl_sharedmem_writel(&device->memstore,
2345 KGSL_MEMSTORE_OFFSET(context_id,
2346 ref_wait_ts), event->timestamp);
2347 enableflag = 1;
2348 kgsl_sharedmem_writel(&device->memstore,
2349 KGSL_MEMSTORE_OFFSET(context_id,
2350 ts_cmp_enable), enableflag);
2351
2352 /* Make sure the memstore write gets posted */
2353 wmb();
2354
2355 /*
2356 * submit a dummy packet so that even if all
2357 * commands upto timestamp get executed we will still
2358 * get an interrupt
2359 */
2360 cmds[0] = cp_type3_packet(CP_NOP, 1);
2361 cmds[1] = 0;
2362
2363 if (adreno_dev->drawctxt_active)
2364 adreno_ringbuffer_issuecmds_intr(device,
2365 event->context, &cmds[0], 2);
2366 }
2367 }
2368}
2369
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002370static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002371 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002372{
2373 int status;
2374 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002375 unsigned int context_id;
2376
2377 mutex_lock(&device->mutex);
2378 context_id = _get_context_id(context);
2379 /*
2380 * If the context ID is invalid, we are in a race with
2381 * the context being destroyed by userspace so bail.
2382 */
2383 if (context_id == KGSL_CONTEXT_INVALID) {
2384 KGSL_DRV_WARN(device, "context was detached");
2385 status = -EINVAL;
2386 goto unlock;
2387 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002388
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002389 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002390 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002391 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002392 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002393 mb();
2394
2395 if (enableflag) {
2396 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002397 KGSL_MEMSTORE_OFFSET(context_id,
2398 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002399 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002400 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002401 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002402 KGSL_MEMSTORE_OFFSET(context_id,
2403 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002404 wmb();
2405 }
2406 } else {
2407 unsigned int cmds[2];
2408 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002409 KGSL_MEMSTORE_OFFSET(context_id,
2410 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002411 enableflag = 1;
2412 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002413 KGSL_MEMSTORE_OFFSET(context_id,
2414 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002415 wmb();
2416 /* submit a dummy packet so that even if all
2417 * commands upto timestamp get executed we will still
2418 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002419 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002420 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002421
Vladimir Razgulin38345302013-01-22 18:41:59 -07002422 if (context && device->state != KGSL_STATE_SLUMBER)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002423 adreno_ringbuffer_issuecmds_intr(device,
2424 context, &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002425 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002426 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002427unlock:
2428 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002429
2430 return status;
2431}
2432
2433/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002434 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002435 placing a process in wait q. For conditional interrupts we expect the
2436 process to already be in its wait q when its exit condition checking
2437 function is called.
2438*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002439#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002440({ \
2441 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002442 if (io) \
2443 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2444 else \
2445 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002446 __ret; \
2447})
2448
Tarun Karra3335f142012-06-19 14:11:48 -07002449
2450
2451unsigned int adreno_hang_detect(struct kgsl_device *device,
2452 unsigned int *prev_reg_val)
2453{
2454 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2455 unsigned int curr_reg_val[hang_detect_regs_count];
2456 unsigned int hang_detected = 1;
2457 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002458 static unsigned long next_hang_detect_time;
Tarun Karra3335f142012-06-19 14:11:48 -07002459
2460 if (!adreno_dev->fast_hang_detect)
2461 return 0;
2462
Jordan Crousecca61142012-11-20 10:54:24 -07002463 if (is_adreno_rbbm_status_idle(device)) {
2464
2465 /*
2466 * On A20X if the RPTR != WPTR and the device is idle, then
2467 * the last write to WPTR probably failed to latch so write it
2468 * again
2469 */
2470
2471 if (adreno_is_a2xx(adreno_dev)) {
2472 unsigned int rptr;
2473 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2474 if (rptr != adreno_dev->ringbuffer.wptr)
2475 adreno_regwrite(device, REG_CP_RB_WPTR,
2476 adreno_dev->ringbuffer.wptr);
2477 }
2478
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002479 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002480 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002481
Tarun Karra4648db92013-02-01 17:03:40 -08002482 /*
2483 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2484 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2485 * from the last time do nothing.
2486 */
2487 if ((next_hang_detect_time) &&
2488 (time_before(jiffies, next_hang_detect_time)))
2489 return 0;
2490 else
2491 next_hang_detect_time = (jiffies +
2492 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2493
Tarun Karra3335f142012-06-19 14:11:48 -07002494 for (i = 0; i < hang_detect_regs_count; i++) {
Jordan Crouseb5c80482012-10-03 09:38:41 -06002495
2496 if (hang_detect_regs[i] == 0)
2497 continue;
2498
Tarun Karra3335f142012-06-19 14:11:48 -07002499 adreno_regread(device, hang_detect_regs[i],
2500 &curr_reg_val[i]);
2501 if (curr_reg_val[i] != prev_reg_val[i]) {
2502 prev_reg_val[i] = curr_reg_val[i];
2503 hang_detected = 0;
2504 }
2505 }
2506
2507 return hang_detected;
2508}
2509
Jordan Crouse92446a62012-11-15 11:00:06 -07002510/**
2511 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2512 * @device - pointer to a KGSL device structure
2513 * @context - pointer to the active KGSL context
2514 * @timestamp - the timestamp that the process was waiting for
2515 *
2516 * Process a possible GPU hang and try to recover from it cleanly
2517 */
2518static int adreno_handle_hang(struct kgsl_device *device,
2519 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002520{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002521 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002522 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002523 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002524 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002525
Jordan Crouse92446a62012-11-15 11:00:06 -07002526 /* Do one last check to see if we somehow made it through */
2527 if (kgsl_check_timestamp(device, context, timestamp))
2528 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002529
2530 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002531
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002532 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2533 mb();
2534
Jeremy Gebben63904832012-02-07 16:10:55 -07002535 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002536 "Device hang detected while waiting for timestamp: "
2537 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002538 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2539 context_id, timestamp, context_id, ts_issued, context_id,
2540 kgsl_readtimestamp(device, context,
2541 KGSL_TIMESTAMP_RETIRED),
2542 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002543
2544 /* Return 0 after a successful recovery */
2545 if (!adreno_dump_and_recover(device))
2546 return 0;
2547
2548 return -ETIMEDOUT;
2549}
2550
2551static int _check_pending_timestamp(struct kgsl_device *device,
2552 struct kgsl_context *context, unsigned int timestamp)
2553{
2554 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2555 unsigned int context_id = _get_context_id(context);
2556 unsigned int ts_issued;
2557
2558 if (context_id == KGSL_CONTEXT_INVALID)
2559 return -EINVAL;
2560
2561 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2562
2563 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2564 return 0;
2565
2566 if (context && !context->wait_on_invalid_ts) {
2567 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2568 context_id, timestamp, context_id, ts_issued);
2569
2570 /* Only print this message once */
2571 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002572 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002573
2574 return -EINVAL;
2575}
2576
2577/**
2578 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2579 * @device - pointer to a KGSL device structure
2580 * @context - pointer to the active kgsl context
2581 * @timestamp - GPU timestamp to wait for
2582 * @msecs - amount of time to wait (in milliseconds)
2583 *
2584 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2585 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2586 * one if it happened. Otherwise, spend most of our time in an interruptible
2587 * wait for the timestamp interrupt to be processed. This function must be
2588 * called with the mutex already held.
2589 */
2590static int adreno_waittimestamp(struct kgsl_device *device,
2591 struct kgsl_context *context,
2592 unsigned int timestamp,
2593 unsigned int msecs)
2594{
2595 static unsigned int io_cnt;
2596 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2597 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2598 unsigned int context_id = _get_context_id(context);
2599 unsigned int prev_reg_val[hang_detect_regs_count];
2600 unsigned int time_elapsed = 0;
2601 unsigned int wait;
2602 int ts_compare = 1;
2603 int io, ret = -ETIMEDOUT;
2604
2605 /* Get out early if the context has already been destroyed */
2606
2607 if (context_id == KGSL_CONTEXT_INVALID) {
2608 KGSL_DRV_WARN(device, "context was detached");
2609 return -EINVAL;
2610 }
2611
2612 /*
2613 * Check to see if the requested timestamp is "newer" then the last
2614 * timestamp issued. If it is complain once and return error. Only
2615 * print the message once per context so that badly behaving
2616 * applications don't spam the logs
2617 */
2618
2619 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2620 if (_check_pending_timestamp(device, context, timestamp))
2621 return -EINVAL;
2622
2623 /* Reset the invalid timestamp flag on a valid wait */
2624 context->wait_on_invalid_ts = false;
2625 }
2626
2627
2628 /* Clear the registers used for hang detection */
2629 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2630
2631 /*
2632 * On the first time through the loop only wait 100ms.
2633 * this gives enough time for the engine to start moving and oddly
2634 * provides better hang detection results than just going the full
2635 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2636 * is if msecs happens to be < 100ms then just use the full timeout
2637 */
2638
2639 wait = 100;
2640
2641 do {
2642 long status;
2643
Jordan Crouse92446a62012-11-15 11:00:06 -07002644 /*
2645 * if the timestamp happens while we're not
2646 * waiting, there's a chance that an interrupt
2647 * will not be generated and thus the timestamp
2648 * work needs to be queued.
2649 */
2650
2651 if (kgsl_check_timestamp(device, context, timestamp)) {
2652 queue_work(device->work_queue, &device->ts_expired_ws);
2653 ret = 0;
2654 break;
2655 }
2656
2657 /* Check to see if the GPU is hung */
2658 if (adreno_hang_detect(device, prev_reg_val)) {
2659 ret = adreno_handle_hang(device, context, timestamp);
2660 break;
2661 }
2662
2663 /*
2664 * For proper power accounting sometimes we need to call
2665 * io_wait_interruptible_timeout and sometimes we need to call
2666 * plain old wait_interruptible_timeout. We call the regular
2667 * timeout N times out of 100, where N is a number specified by
2668 * the current power level
2669 */
2670
2671 io_cnt = (io_cnt + 1) % 100;
2672 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2673 ? 0 : 1;
2674
2675 mutex_unlock(&device->mutex);
2676
2677 /* Wait for a timestamp event */
2678 status = kgsl_wait_event_interruptible_timeout(
2679 device->wait_queue,
2680 kgsl_check_interrupt_timestamp(device, context,
2681 timestamp), msecs_to_jiffies(wait), io);
2682
2683 mutex_lock(&device->mutex);
2684
2685 /*
2686 * If status is non zero then either the condition was satisfied
2687 * or there was an error. In either event, this is the end of
2688 * the line for us
2689 */
2690
2691 if (status != 0) {
2692 ret = (status > 0) ? 0 : (int) status;
2693 break;
2694 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002695 time_elapsed += wait;
2696
2697 /* If user specified timestamps are being used, wait at least
2698 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
2699 * issue a IB for a timestamp before checking to see if the
2700 * current timestamp we are waiting for is valid or not
2701 */
2702
2703 if (ts_compare && (adreno_ctx &&
2704 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
2705 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
2706 ret = _check_pending_timestamp(device, context,
2707 timestamp);
2708 if (ret)
2709 break;
2710
2711 /* Don't do this check again */
2712 ts_compare = 0;
2713
2714 /*
2715 * Reset the invalid timestamp flag on a valid
2716 * wait
2717 */
2718 context->wait_on_invalid_ts = false;
2719 }
2720 }
2721
2722 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07002723 * We want to wait the floor of KGSL_TIMEOUT_PART
2724 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07002725 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07002726
2727 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
2728 wait = KGSL_TIMEOUT_PART;
2729 else
2730 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07002731
2732 } while (!msecs || time_elapsed < msecs);
2733
2734 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002735}
2736
2737static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002738 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002739{
2740 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002741 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002742
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002743 /*
2744 * If the context ID is invalid, we are in a race with
2745 * the context being destroyed by userspace so bail.
2746 */
2747 if (context_id == KGSL_CONTEXT_INVALID) {
2748 KGSL_DRV_WARN(device, "context was detached");
2749 return timestamp;
2750 }
Jordan Crousec659f382012-04-16 11:10:41 -06002751 switch (type) {
2752 case KGSL_TIMESTAMP_QUEUED: {
2753 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2754 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2755
2756 timestamp = rb->timestamp[context_id];
2757 break;
2758 }
2759 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002760 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002761 break;
2762 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002763 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002764 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2765 break;
2766 }
2767
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002768 rmb();
2769
2770 return timestamp;
2771}
2772
2773static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2774 unsigned int cmd, void *data)
2775{
2776 int result = 0;
2777 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2778 struct kgsl_context *context;
2779
2780 switch (cmd) {
2781 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2782 binbase = data;
2783
2784 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2785 if (context) {
2786 adreno_drawctxt_set_bin_base_offset(
2787 dev_priv->device, context, binbase->offset);
2788 } else {
2789 result = -EINVAL;
2790 KGSL_DRV_ERR(dev_priv->device,
2791 "invalid drawctxt drawctxt_id %d "
2792 "device_id=%d\n",
2793 binbase->drawctxt_id, dev_priv->device->id);
2794 }
2795 break;
2796
2797 default:
2798 KGSL_DRV_INFO(dev_priv->device,
2799 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002800 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002801 break;
2802 }
2803 return result;
2804
2805}
2806
2807static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2808{
2809 gpu_freq /= 1000000;
2810 return ticks / gpu_freq;
2811}
2812
2813static void adreno_power_stats(struct kgsl_device *device,
2814 struct kgsl_power_stats *stats)
2815{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002816 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002817 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002818 unsigned int cycles;
2819
2820 /* Get the busy cycles counted since the counter was last reset */
2821 /* Calling this function also resets and restarts the counter */
2822
2823 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002824
2825 /* In order to calculate idle you have to have run the algorithm *
2826 * at least once to get a start time. */
2827 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002828 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002829 stats->total_time = tmp - pwr->time;
2830 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002831 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002832 pwrlevels[device->pwrctrl.active_pwrlevel].
2833 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002834 } else {
2835 stats->total_time = 0;
2836 stats->busy_time = 0;
2837 pwr->time = ktime_to_us(ktime_get());
2838 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002839}
2840
2841void adreno_irqctrl(struct kgsl_device *device, int state)
2842{
Jordan Crousea78c9172011-07-11 13:14:09 -06002843 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2844 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002845}
2846
Jordan Croused6535882012-06-20 08:22:16 -06002847static unsigned int adreno_gpuid(struct kgsl_device *device,
2848 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002849{
2850 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2851
Jordan Croused6535882012-06-20 08:22:16 -06002852 /* Some applications need to know the chip ID too, so pass
2853 * that as a parameter */
2854
2855 if (chipid != NULL)
2856 *chipid = adreno_dev->chip_id;
2857
Jordan Crousea0758f22011-12-07 11:19:22 -07002858 /* Standard KGSL gpuid format:
2859 * top word is 0x0002 for 2D or 0x0003 for 3D
2860 * Bottom word is core specific identifer
2861 */
2862
2863 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2864}
2865
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002866static const struct kgsl_functable adreno_functable = {
2867 /* Mandatory functions */
2868 .regread = adreno_regread,
2869 .regwrite = adreno_regwrite,
2870 .idle = adreno_idle,
2871 .isidle = adreno_isidle,
2872 .suspend_context = adreno_suspend_context,
2873 .start = adreno_start,
2874 .stop = adreno_stop,
2875 .getproperty = adreno_getproperty,
2876 .waittimestamp = adreno_waittimestamp,
2877 .readtimestamp = adreno_readtimestamp,
2878 .issueibcmds = adreno_ringbuffer_issueibcmds,
2879 .ioctl = adreno_ioctl,
2880 .setup_pt = adreno_setup_pt,
2881 .cleanup_pt = adreno_cleanup_pt,
2882 .power_stats = adreno_power_stats,
2883 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002884 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002885 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002886 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002887 /* Optional functions */
2888 .setstate = adreno_setstate,
2889 .drawctxt_create = adreno_drawctxt_create,
2890 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002891 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002892 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07002893 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002894};
2895
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002896static struct platform_driver adreno_platform_driver = {
2897 .probe = adreno_probe,
2898 .remove = __devexit_p(adreno_remove),
2899 .suspend = kgsl_suspend_driver,
2900 .resume = kgsl_resume_driver,
2901 .id_table = adreno_id_table,
2902 .driver = {
2903 .owner = THIS_MODULE,
2904 .name = DEVICE_3D_NAME,
2905 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002906 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002907 }
2908};
2909
2910static int __init kgsl_3d_init(void)
2911{
2912 return platform_driver_register(&adreno_platform_driver);
2913}
2914
2915static void __exit kgsl_3d_exit(void)
2916{
2917 platform_driver_unregister(&adreno_platform_driver);
2918}
2919
2920module_init(kgsl_3d_init);
2921module_exit(kgsl_3d_exit);
2922
2923MODULE_DESCRIPTION("3D Graphics driver");
2924MODULE_VERSION("1.2");
2925MODULE_LICENSE("GPL v2");
2926MODULE_ALIAS("platform:kgsl_3d");