blob: 893a6edc76b11ab099973f6753f39af0f03d2487 [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
Tarun Karrad20d71a2013-01-25 15:38:57 -08001202 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001203 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) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001287 /* While fault tolerance is on we do not want timer to
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001288 * fire and attempt to change any device state */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001289 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001290 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,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001328 int ft_status)
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001329{
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
Tarun Karrad20d71a2013-01-25 15:38:57 -08001335 * since thats the guilty party, if fault tolerance failed then
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001336 * mark all as guilty
1337 */
1338 while ((context = idr_get_next(&device->context_idr, &next))) {
1339 struct adreno_context *adreno_context = context->devctxt;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001340 if (ft_status) {
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001341 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 |
Tarun Karrad20d71a2013-01-25 15:38:57 -08001347 CTXT_FLAGS_GPU_HANG_FT))
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001348 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
Tarun Karrad20d71a2013-01-25 15:38:57 -08001382static void adreno_destroy_ft_data(struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001383{
Tarun Karrad20d71a2013-01-25 15:38:57 -08001384 vfree(ft_data->rb_buffer);
1385 vfree(ft_data->bad_rb_buffer);
1386 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001387}
1388
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001389static int _find_start_of_cmd_seq(struct adreno_ringbuffer *rb,
1390 unsigned int *ptr,
1391 bool inc)
1392{
1393 int status = -EINVAL;
1394 unsigned int val1;
1395 unsigned int size = rb->buffer_desc.size;
1396 unsigned int start_ptr = *ptr;
1397
1398 while ((start_ptr / sizeof(unsigned int)) != rb->wptr) {
1399 if (inc)
1400 start_ptr = adreno_ringbuffer_inc_wrapped(start_ptr,
1401 size);
1402 else
1403 start_ptr = adreno_ringbuffer_dec_wrapped(start_ptr,
1404 size);
1405 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, start_ptr);
1406 if (KGSL_CMD_IDENTIFIER == val1) {
1407 if ((start_ptr / sizeof(unsigned int)) != rb->wptr)
1408 start_ptr = adreno_ringbuffer_dec_wrapped(
1409 start_ptr, size);
1410 *ptr = start_ptr;
1411 status = 0;
1412 break;
1413 }
1414 }
1415 return status;
1416}
1417
1418static int _find_cmd_seq_after_eop_ts(struct adreno_ringbuffer *rb,
1419 unsigned int *rb_rptr,
1420 unsigned int global_eop,
1421 bool inc)
1422{
1423 int status = -EINVAL;
1424 unsigned int temp_rb_rptr = *rb_rptr;
1425 unsigned int size = rb->buffer_desc.size;
1426 unsigned int val[3];
1427 int i = 0;
1428 bool check = false;
1429
1430 if (inc && temp_rb_rptr / sizeof(unsigned int) != rb->wptr)
1431 return status;
1432
1433 do {
1434 /*
1435 * when decrementing we need to decrement first and
1436 * then read make sure we cover all the data
1437 */
1438 if (!inc)
1439 temp_rb_rptr = adreno_ringbuffer_dec_wrapped(
1440 temp_rb_rptr, size);
1441 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i],
1442 temp_rb_rptr);
1443
1444 if (check && ((inc && val[i] == global_eop) ||
1445 (!inc && (val[i] ==
1446 cp_type3_packet(CP_MEM_WRITE, 2) ||
1447 val[i] == CACHE_FLUSH_TS)))) {
1448 /* decrement i, i.e i = (i - 1 + 3) % 3 if
1449 * we are going forward, else increment i */
1450 i = (i + 2) % 3;
1451 if (val[i] == rb->device->memstore.gpuaddr +
1452 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1453 eoptimestamp)) {
1454 int j = ((i + 2) % 3);
1455 if ((inc && (val[j] == CACHE_FLUSH_TS ||
1456 val[j] == cp_type3_packet(
1457 CP_MEM_WRITE, 2))) ||
1458 (!inc && val[j] == global_eop)) {
1459 /* Found the global eop */
1460 status = 0;
1461 break;
1462 }
1463 }
1464 /* if no match found then increment i again
1465 * since we decremented before matching */
1466 i = (i + 1) % 3;
1467 }
1468 if (inc)
1469 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(
1470 temp_rb_rptr, size);
1471
1472 i = (i + 1) % 3;
1473 if (2 == i)
1474 check = true;
1475 } while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr);
1476 /* temp_rb_rptr points to the command stream after global eop,
1477 * move backward till the start of command sequence */
1478 if (!status) {
1479 status = _find_start_of_cmd_seq(rb, &temp_rb_rptr, false);
1480 if (!status) {
1481 *rb_rptr = temp_rb_rptr;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001482 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001483 "Offset of cmd sequence after eop timestamp: 0x%x\n",
1484 temp_rb_rptr / sizeof(unsigned int));
1485 }
1486 }
1487 if (status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001488 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001489 "Failed to find the command sequence after eop timestamp\n");
1490 return status;
1491}
1492
1493static int _find_hanging_ib_sequence(struct adreno_ringbuffer *rb,
1494 unsigned int *rb_rptr,
1495 unsigned int ib1)
1496{
1497 int status = -EINVAL;
1498 unsigned int temp_rb_rptr = *rb_rptr;
1499 unsigned int size = rb->buffer_desc.size;
1500 unsigned int val[2];
1501 int i = 0;
1502 bool check = false;
1503 bool ctx_switch = false;
1504
1505 while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr) {
1506 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i], temp_rb_rptr);
1507
1508 if (check && val[i] == ib1) {
1509 /* decrement i, i.e i = (i - 1 + 2) % 2 */
1510 i = (i + 1) % 2;
1511 if (adreno_cmd_is_ib(val[i])) {
1512 /* go till start of command sequence */
1513 status = _find_start_of_cmd_seq(rb,
1514 &temp_rb_rptr, false);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001515
1516 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001517 "Found the hanging IB at offset 0x%x\n",
1518 temp_rb_rptr / sizeof(unsigned int));
1519 break;
1520 }
1521 /* if no match the increment i since we decremented
1522 * before checking */
1523 i = (i + 1) % 2;
1524 }
1525 /* Make sure you do not encounter a context switch twice, we can
1526 * encounter it once for the bad context as the start of search
1527 * can point to the context switch */
1528 if (val[i] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
1529 if (ctx_switch) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001530 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001531 "Context switch encountered before bad "
1532 "IB found\n");
1533 break;
1534 }
1535 ctx_switch = true;
1536 }
1537 i = (i + 1) % 2;
1538 if (1 == i)
1539 check = true;
1540 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(temp_rb_rptr,
1541 size);
1542 }
1543 if (!status)
1544 *rb_rptr = temp_rb_rptr;
1545 return status;
1546}
1547
Tarun Karrad20d71a2013-01-25 15:38:57 -08001548static int adreno_setup_ft_data(struct kgsl_device *device,
1549 struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001550{
1551 int ret = 0;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001552 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1553 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001554 struct kgsl_context *context;
1555 struct adreno_context *adreno_context;
1556 unsigned int rb_rptr = rb->wptr * sizeof(unsigned int);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001557
Tarun Karrad20d71a2013-01-25 15:38:57 -08001558 memset(ft_data, 0, sizeof(*ft_data));
1559 ft_data->start_of_replay_cmds = 0xFFFFFFFF;
1560 ft_data->replay_for_snapshot = 0xFFFFFFFF;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001561
Tarun Karrad20d71a2013-01-25 15:38:57 -08001562 adreno_regread(device, REG_CP_IB1_BASE, &ft_data->ib1);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001563
Tarun Karrad20d71a2013-01-25 15:38:57 -08001564 kgsl_sharedmem_readl(&device->memstore, &ft_data->context_id,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001565 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1566 current_context));
1567
1568 kgsl_sharedmem_readl(&device->memstore,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001569 &ft_data->global_eop,
Tarun Karradeeecc02013-01-21 23:42:17 -08001570 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1571 eoptimestamp));
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001572
Tarun Karrad20d71a2013-01-25 15:38:57 -08001573 ft_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1574 if (!ft_data->rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001575 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1576 rb->buffer_desc.size);
1577 return -ENOMEM;
1578 }
1579
Tarun Karrad20d71a2013-01-25 15:38:57 -08001580 ft_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1581 if (!ft_data->bad_rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001582 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1583 rb->buffer_desc.size);
1584 ret = -ENOMEM;
1585 goto done;
1586 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001587
Tarun Karrad20d71a2013-01-25 15:38:57 -08001588 ft_data->good_rb_buffer = vmalloc(rb->buffer_desc.size);
1589 if (!ft_data->good_rb_buffer) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001590 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1591 rb->buffer_desc.size);
1592 ret = -ENOMEM;
1593 goto done;
1594 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001595 ft_data->fault = device->mmu.fault;
1596 ft_data->step = adreno_dev->ft_policy;
Tarun Karradeeecc02013-01-21 23:42:17 -08001597
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001598 /* find the start of bad command sequence in rb */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001599 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001600 /* Look for the command stream that is right after the global eop */
1601
1602 if (!context) {
1603 /*
Tarun Karrad20d71a2013-01-25 15:38:57 -08001604 * If there is no context then fault tolerance does not need to
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001605 * replay anything, just reset GPU and thats it
1606 */
1607 goto done;
1608 }
1609 ret = _find_cmd_seq_after_eop_ts(rb, &rb_rptr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001610 ft_data->global_eop + 1, false);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001611 if (ret)
1612 goto done;
1613
Tarun Karrad20d71a2013-01-25 15:38:57 -08001614 ft_data->start_of_replay_cmds = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001615
1616 adreno_context = context->devctxt;
1617 if (adreno_context->flags & CTXT_FLAGS_PREAMBLE) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001618 if (ft_data->ib1) {
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001619 ret = _find_hanging_ib_sequence(rb,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001620 &rb_rptr, ft_data->ib1);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001621 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001622 KGSL_FT_ERR(device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001623 "Start not found for replay IB sequence\n");
1624 ret = 0;
1625 goto done;
1626 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001627 ft_data->start_of_replay_cmds = rb_rptr;
1628 ft_data->replay_for_snapshot = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001629 }
1630 }
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001631
1632done:
1633 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001634 vfree(ft_data->rb_buffer);
1635 vfree(ft_data->bad_rb_buffer);
1636 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001637 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001638 return ret;
1639}
1640
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001641static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001642_adreno_ft_restart_device(struct kgsl_device *device,
1643 struct kgsl_context *context,
1644 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001645{
Tarun Karradeeecc02013-01-21 23:42:17 -08001646
1647 struct adreno_context *adreno_context = context->devctxt;
1648
1649 /* restart device */
1650 if (adreno_stop(device)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001651 KGSL_FT_ERR(device, "Device stop failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001652 return 1;
1653 }
1654
1655 if (adreno_start(device, true)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001656 KGSL_FT_ERR(device, "Device start failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001657 return 1;
1658 }
1659
1660 if (context)
1661 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1662 KGSL_MEMSTORE_GLOBAL);
1663
1664 /* If iommu is used then we need to make sure that the iommu clocks
1665 * are on since there could be commands in pipeline that touch iommu */
1666 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1667 if (kgsl_mmu_enable_clk(&device->mmu,
1668 KGSL_IOMMU_CONTEXT_USER))
1669 return 1;
1670 }
1671
1672 return 0;
1673}
1674
1675static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001676_adreno_ft_resubmit_rb(struct kgsl_device *device,
Tarun Karradeeecc02013-01-21 23:42:17 -08001677 struct adreno_ringbuffer *rb,
1678 struct kgsl_context *context,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001679 struct adreno_ft_data *ft_data,
Tarun Karradeeecc02013-01-21 23:42:17 -08001680 unsigned int *buff, unsigned int size)
1681{
1682 unsigned int ret = 0;
1683
Tarun Karrad20d71a2013-01-25 15:38:57 -08001684 if (_adreno_ft_restart_device(device, context, ft_data))
Tarun Karradeeecc02013-01-21 23:42:17 -08001685 return 1;
1686
1687 if (size) {
1688
1689 /* submit commands and wait for them to pass */
1690 adreno_ringbuffer_restore(rb, buff, size);
1691
1692 ret = adreno_idle(device);
1693 }
1694
1695 return ret;
1696}
1697
1698
1699static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001700_adreno_ft(struct kgsl_device *device,
1701 struct adreno_ft_data *ft_data)
Tarun Karradeeecc02013-01-21 23:42:17 -08001702{
1703 int ret = 0, i;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001704 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1705 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1706 struct kgsl_context *context;
1707 struct adreno_context *adreno_context = NULL;
1708 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1709
Tarun Karrad20d71a2013-01-25 15:38:57 -08001710 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001711 if (context == NULL) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001712 KGSL_FT_CRIT(device, "Last context unknown id:%d\n",
1713 ft_data->context_id);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001714 } else {
1715 adreno_context = context->devctxt;
1716 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001717 /*
1718 * set the invalid ts flag to 0 for this context since we have
1719 * detected a hang for it
1720 */
1721 context->wait_on_invalid_ts = false;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001722
1723 KGSL_FT_INFO(device, "Context found\n");
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001724 }
1725
1726 /* Extract valid contents from rb which can still be executed after
1727 * hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001728 adreno_ringbuffer_extract(rb, ft_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001729
Tarun Karradeeecc02013-01-21 23:42:17 -08001730 /* Do not try the bad commands if hang is due to a fault */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001731 if (ft_data->fault) {
1732 KGSL_FT_ERR(device, "Page fault no FT for bad context\n");
1733
Tarun Karradeeecc02013-01-21 23:42:17 -08001734 goto play_good_cmds;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001735 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001736
Tarun Karrad20d71a2013-01-25 15:38:57 -08001737 if (ft_data->step == FT_REPLAY_BAD_CTXT_CMDS) {
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001738
Tarun Karrad20d71a2013-01-25 15:38:57 -08001739 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1740 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001741
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001742 if (ret)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001743 KGSL_FT_INFO(device, "Replay unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001744 else
1745 goto play_good_cmds;
1746
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001747 }
1748
Tarun Karrad20d71a2013-01-25 15:38:57 -08001749 if (ft_data->step == FT_NOP_IB_BAD_CTXT_CMDS) {
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001750
Tarun Karrad20d71a2013-01-25 15:38:57 -08001751 for (i = 0; i < ft_data->bad_rb_size; i++) {
1752 if ((ft_data->bad_rb_buffer[i] ==
1753 CP_HDR_INDIRECT_BUFFER_PFD) &&
1754 (ft_data->bad_rb_buffer[i+1] == ft_data->ib1)) {
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001755
Tarun Karrad20d71a2013-01-25 15:38:57 -08001756 ft_data->bad_rb_buffer[i] = cp_nop_packet(2);
1757 ft_data->bad_rb_buffer[i+1] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001758 KGSL_NOP_IB_IDENTIFIER;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001759 ft_data->bad_rb_buffer[i+2] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001760 KGSL_NOP_IB_IDENTIFIER;
1761 break;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001762 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001763 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001764
Tarun Karrad20d71a2013-01-25 15:38:57 -08001765 if ((i == (ft_data->bad_rb_size)) || (!ft_data->ib1)) {
1766 KGSL_FT_ERR(device, "Bad IB to NOP not found\n");
1767 ft_data->step = FT_FAIL_BAD_CTXT_CMDS;
Tarun Karradeeecc02013-01-21 23:42:17 -08001768 goto play_good_cmds;
1769 }
1770
Tarun Karrad20d71a2013-01-25 15:38:57 -08001771 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1772 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001773
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001774 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001775 KGSL_FT_INFO(device, "NOP faulty IB unsuccessful\n");
1776 ft_data->step = FT_SKIP_EOF_BAD_CTXT_CMDS;
Tarun Karradeeecc02013-01-21 23:42:17 -08001777 } else
1778 goto play_good_cmds;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001779 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001780
Tarun Karrad20d71a2013-01-25 15:38:57 -08001781 if (ft_data->step == FT_SKIP_EOF_BAD_CTXT_CMDS) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001782
Tarun Karrad20d71a2013-01-25 15:38:57 -08001783 for (i = 0; i < ft_data->bad_rb_size; i++) {
1784 if (ft_data->bad_rb_buffer[i] ==
1785 KGSL_END_OF_FRAME_IDENTIFIER) {
1786 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001787 break;
1788 }
1789 }
1790
1791 /* EOF not found in RB, discard till EOF in
1792 next IB submission */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001793 if (i == ft_data->bad_rb_size) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001794 adreno_context->flags |= CTXT_FLAGS_SKIP_EOF;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001795 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001796 }
1797
Tarun Karrad20d71a2013-01-25 15:38:57 -08001798 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1799 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001800
1801 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001802 KGSL_FT_INFO(device, "Skip EOF unsuccessful\n");
1803 ft_data->step = FT_FAIL_BAD_CTXT_CMDS;
Tarun Karradeeecc02013-01-21 23:42:17 -08001804 } else
1805 goto play_good_cmds;
1806 }
1807
1808play_good_cmds:
1809
Tarun Karrad20d71a2013-01-25 15:38:57 -08001810 if (ft_data->step == FT_FAIL_BAD_CTXT_CMDS)
1811 KGSL_FT_ERR(device, "Bad context commands failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001812 else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001813 KGSL_FT_INFO(device, "Bad context commands success\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001814
1815 if (adreno_context) {
1816 adreno_context->flags = (adreno_context->flags &
Tarun Karrad20d71a2013-01-25 15:38:57 -08001817 ~CTXT_FLAGS_GPU_HANG) | CTXT_FLAGS_GPU_HANG_FT;
Tarun Karradeeecc02013-01-21 23:42:17 -08001818 }
1819 adreno_dev->drawctxt_active = last_active_ctx;
1820 }
1821
Tarun Karrad20d71a2013-01-25 15:38:57 -08001822 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1823 ft_data->good_rb_buffer, ft_data->good_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001824
1825 if (ret) {
1826 /* If we fail here we can try to invalidate another
1827 * context and try fault tolerance again */
1828 ret = -EAGAIN;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001829 KGSL_FT_ERR(device, "Playing good commands unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001830 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001831 } else
1832 KGSL_FT_INFO(device, "Playing good commands successful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001833
1834 /* ringbuffer now has data from the last valid context id,
1835 * so restore the active_ctx to the last valid context */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001836 if (ft_data->last_valid_ctx_id) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001837 struct kgsl_context *last_ctx =
1838 idr_find(&device->context_idr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001839 ft_data->last_valid_ctx_id);
Tarun Karradeeecc02013-01-21 23:42:17 -08001840 if (last_ctx)
1841 adreno_dev->drawctxt_active = last_ctx->devctxt;
1842 }
1843
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001844done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001845 /* Turn off iommu clocks */
1846 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1847 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001848 return ret;
1849}
1850
1851static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001852adreno_ft(struct kgsl_device *device,
1853 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001854{
1855 int ret = 0;
1856 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1857 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1858 unsigned int timestamp;
1859
Tarun Karrad20d71a2013-01-25 15:38:57 -08001860 KGSL_FT_INFO(device,
1861 "Start Parameters: IB1: 0x%X, "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001862 "Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001863 ft_data->ib1, ft_data->context_id, ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001864
1865 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
Tarun Karrad20d71a2013-01-25 15:38:57 -08001866 KGSL_FT_INFO(device, "Last issued global timestamp: %x\n", timestamp);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001867
1868 /* We may need to replay commands multiple times based on whether
1869 * multiple contexts hang the GPU */
1870 while (true) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001871
Tarun Karrad20d71a2013-01-25 15:38:57 -08001872 ret = _adreno_ft(device, ft_data);
1873
1874 KGSL_FT_CRIT(device, "POLICY: 0x%X\n", ft_data->step);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001875
1876 if (-EAGAIN == ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001877 /* setup new fault tolerance parameters and retry, this
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001878 * means more than 1 contexts are causing hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001879 adreno_destroy_ft_data(ft_data);
1880 ret = adreno_setup_ft_data(device, ft_data);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001881 if (ret)
1882 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001883 KGSL_FT_INFO(device,
1884 "Retry. Parameters: "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001885 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001886 ft_data->ib1, ft_data->context_id,
1887 ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001888 } else {
1889 break;
1890 }
1891 }
1892
1893 if (ret)
1894 goto done;
1895
Tarun Karrad20d71a2013-01-25 15:38:57 -08001896 /* Restore correct states after fault tolerance */
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001897 if (adreno_dev->drawctxt_active)
1898 device->mmu.hwpagetable =
1899 adreno_dev->drawctxt_active->pagetable;
1900 else
1901 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1902 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1903 kgsl_sharedmem_writel(&device->memstore,
1904 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1905 eoptimestamp),
1906 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07001907
1908 /* switch to NULL ctxt */
1909 if (adreno_dev->drawctxt_active != NULL)
1910 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1911
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001912done:
1913 adreno_set_max_ts_for_bad_ctxs(device);
1914 adreno_mark_context_status(device, ret);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001915 if (ret)
1916 KGSL_FT_ERR(device, "Fault Tolerance failed\n");
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001917 return ret;
1918}
1919
1920int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001921adreno_dump_and_exec_ft(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001922{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001923 int result = -ETIMEDOUT;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001924 struct adreno_ft_data ft_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001925
1926 if (device->state == KGSL_STATE_HUNG)
1927 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001928 if (device->state == KGSL_STATE_DUMP_AND_FT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929 mutex_unlock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001930 wait_for_completion(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001931 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001932 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001933 result = 0;
1934 } else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001935 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_FT);
1936 INIT_COMPLETION(device->ft_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001937 /* Detected a hang */
1938
Tarun Karrad20d71a2013-01-25 15:38:57 -08001939 /* Get the fault tolerance data as soon as hang is detected */
1940 result = adreno_setup_ft_data(device, &ft_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001941 /*
1942 * Trigger an automatic dump of the state to
1943 * the console
1944 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001945 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001946
1947 /*
1948 * Make a GPU snapshot. For now, do it after the PM dump so we
1949 * can at least be sure the PM dump will work as it always has
1950 */
1951 kgsl_device_snapshot(device, 1);
1952
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001953 if (!result) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001954 result = adreno_ft(device, &ft_data);
1955 adreno_destroy_ft_data(&ft_data);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001956 }
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001957 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001958 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001959 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001960 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001961 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1962 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001963 complete_all(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001964 }
1965done:
1966 return result;
1967}
Tarun Karrad20d71a2013-01-25 15:38:57 -08001968EXPORT_SYMBOL(adreno_dump_and_exec_ft);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001969
1970static int adreno_getproperty(struct kgsl_device *device,
1971 enum kgsl_property_type type,
1972 void *value,
1973 unsigned int sizebytes)
1974{
1975 int status = -EINVAL;
1976 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1977
1978 switch (type) {
1979 case KGSL_PROP_DEVICE_INFO:
1980 {
1981 struct kgsl_devinfo devinfo;
1982
1983 if (sizebytes != sizeof(devinfo)) {
1984 status = -EINVAL;
1985 break;
1986 }
1987
1988 memset(&devinfo, 0, sizeof(devinfo));
1989 devinfo.device_id = device->id+1;
1990 devinfo.chip_id = adreno_dev->chip_id;
1991 devinfo.mmu_enabled = kgsl_mmu_enabled();
1992 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001993 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1994 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001995
1996 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1997 0) {
1998 status = -EFAULT;
1999 break;
2000 }
2001 status = 0;
2002 }
2003 break;
2004 case KGSL_PROP_DEVICE_SHADOW:
2005 {
2006 struct kgsl_shadowprop shadowprop;
2007
2008 if (sizebytes != sizeof(shadowprop)) {
2009 status = -EINVAL;
2010 break;
2011 }
2012 memset(&shadowprop, 0, sizeof(shadowprop));
2013 if (device->memstore.hostptr) {
2014 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2015 * anything to mmap().
2016 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07002017 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002018 shadowprop.size = device->memstore.size;
2019 /* GSL needs this to be set, even if it
2020 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002021 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2022 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002023 }
2024 if (copy_to_user(value, &shadowprop,
2025 sizeof(shadowprop))) {
2026 status = -EFAULT;
2027 break;
2028 }
2029 status = 0;
2030 }
2031 break;
2032 case KGSL_PROP_MMU_ENABLE:
2033 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002034 int mmu_prop = kgsl_mmu_enabled();
2035
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002036 if (sizebytes != sizeof(int)) {
2037 status = -EINVAL;
2038 break;
2039 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002040 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002041 status = -EFAULT;
2042 break;
2043 }
2044 status = 0;
2045 }
2046 break;
2047 case KGSL_PROP_INTERRUPT_WAITS:
2048 {
2049 int int_waits = 1;
2050 if (sizebytes != sizeof(int)) {
2051 status = -EINVAL;
2052 break;
2053 }
2054 if (copy_to_user(value, &int_waits, sizeof(int))) {
2055 status = -EFAULT;
2056 break;
2057 }
2058 status = 0;
2059 }
2060 break;
2061 default:
2062 status = -EINVAL;
2063 }
2064
2065 return status;
2066}
2067
Jordan Crousef7370f82012-04-18 09:31:07 -06002068static int adreno_setproperty(struct kgsl_device *device,
2069 enum kgsl_property_type type,
2070 void *value,
2071 unsigned int sizebytes)
2072{
2073 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08002074 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06002075
2076 switch (type) {
2077 case KGSL_PROP_PWRCTRL: {
2078 unsigned int enable;
2079 struct kgsl_device_platform_data *pdata =
2080 kgsl_device_get_drvdata(device);
2081
2082 if (sizebytes != sizeof(enable))
2083 break;
2084
2085 if (copy_from_user(&enable, (void __user *) value,
2086 sizeof(enable))) {
2087 status = -EFAULT;
2088 break;
2089 }
2090
2091 if (enable) {
2092 if (pdata->nap_allowed)
2093 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002094 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002095 kgsl_pwrscale_enable(device);
2096 } else {
2097 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002098 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002099 kgsl_pwrscale_disable(device);
2100 }
2101
2102 status = 0;
2103 }
2104 break;
2105 default:
2106 break;
2107 }
2108
2109 return status;
2110}
2111
Jordan Crousea29a2e02012-08-14 09:09:23 -06002112static int adreno_ringbuffer_drain(struct kgsl_device *device,
2113 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002114{
2115 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2116 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002117 unsigned long wait;
2118 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2119
2120 if (!(rb->flags & KGSL_FLAGS_STARTED))
2121 return 0;
2122
2123 /*
2124 * The first time into the loop, wait for 100 msecs and kick wptr again
2125 * to ensure that the hardware has updated correctly. After that, kick
2126 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2127 * expires
2128 */
2129
2130 wait = jiffies + msecs_to_jiffies(100);
2131
Jordan Crousea29a2e02012-08-14 09:09:23 -06002132 do {
2133 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002134 /* Check to see if the core is hung */
2135 if (adreno_hang_detect(device, regs))
2136 return -ETIMEDOUT;
2137
2138 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2139 }
2140 GSL_RB_GET_READPTR(rb, &rb->rptr);
2141
2142 if (time_after(jiffies, timeout)) {
2143 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2144 rb->rptr, rb->wptr);
2145 return -ETIMEDOUT;
2146 }
2147 } while (rb->rptr != rb->wptr);
2148
2149 return 0;
2150}
2151
2152/* Caller must hold the device mutex. */
2153int adreno_idle(struct kgsl_device *device)
2154{
2155 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002156 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302157 unsigned long wait_time;
2158 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07002159 unsigned int prev_reg_val[hang_detect_regs_count];
2160
2161 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002162
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002163 kgsl_cffdump_regpoll(device->id,
2164 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002165 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002166
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002167retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002168 /* First, wait for the ringbuffer to drain */
2169 if (adreno_ringbuffer_drain(device, prev_reg_val))
2170 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002171
2172 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002173 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002174 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2175
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002176 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002177 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2178 &rbbm_status);
2179 if (adreno_is_a2xx(adreno_dev)) {
2180 if (rbbm_status == 0x110)
2181 return 0;
2182 } else {
2183 if (!(rbbm_status & 0x80000000))
2184 return 0;
2185 }
Tarun Karra3335f142012-06-19 14:11:48 -07002186
2187 /* Dont wait for timeout, detect hang faster.
2188 */
2189 if (time_after(jiffies, wait_time_part)) {
2190 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002191 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07002192 if ((adreno_hang_detect(device, prev_reg_val)))
2193 goto err;
2194 }
2195
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002196 }
2197
2198err:
2199 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08002200 if (KGSL_STATE_DUMP_AND_FT != device->state &&
2201 !adreno_dump_and_exec_ft(device)) {
2202 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002203 goto retry;
2204 }
2205 return -ETIMEDOUT;
2206}
2207
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002208/**
2209 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2210 * rbbm_status register
2211 * @device - Pointer to the GPU device whose idle status is to be
2212 * checked
2213 * @returns - Returns whether the core is idle (based on rbbm_status)
2214 * false if the core is active, true if the core is idle
2215 */
2216static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2217{
2218 unsigned int reg_rbbm_status;
2219 bool status = false;
2220 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2221
2222 /* Is the core idle? */
2223 adreno_regread(device,
2224 adreno_dev->gpudev->reg_rbbm_status,
2225 &reg_rbbm_status);
2226
2227 if (adreno_is_a2xx(adreno_dev)) {
2228 if (reg_rbbm_status == 0x110)
2229 status = true;
2230 } else {
2231 if (!(reg_rbbm_status & 0x80000000))
2232 status = true;
2233 }
2234 return status;
2235}
2236
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002237static unsigned int adreno_isidle(struct kgsl_device *device)
2238{
2239 int status = false;
2240 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2241 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002242
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002243 WARN_ON(device->state == KGSL_STATE_INIT);
2244 /* If the device isn't active, don't force it on. */
2245 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002246 /* Is the ring buffer is empty? */
2247 GSL_RB_GET_READPTR(rb, &rb->rptr);
2248 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
2249 /* Is the core idle? */
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002250 status = is_adreno_rbbm_status_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002251 }
2252 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002253 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002254 }
2255 return status;
2256}
2257
2258/* Caller must hold the device mutex. */
2259static int adreno_suspend_context(struct kgsl_device *device)
2260{
2261 int status = 0;
2262 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2263
2264 /* switch to NULL ctxt */
2265 if (adreno_dev->drawctxt_active != NULL) {
2266 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002267 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002268 }
2269
2270 return status;
2271}
2272
Jordan Crouse233b2092012-04-18 09:31:09 -06002273/* Find a memory structure attached to an adreno context */
2274
2275struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2276 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2277{
2278 struct kgsl_context *context;
2279 struct adreno_context *adreno_context = NULL;
2280 int next = 0;
2281
2282 while (1) {
2283 context = idr_get_next(&device->context_idr, &next);
2284 if (context == NULL)
2285 break;
2286
2287 adreno_context = (struct adreno_context *)context->devctxt;
2288
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002289 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2290 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002291 struct kgsl_memdesc *desc;
2292
2293 desc = &adreno_context->gpustate;
2294 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2295 return desc;
2296
2297 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2298 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2299 return desc;
2300 }
2301 next = next + 1;
2302 }
2303
2304 return NULL;
2305}
2306
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002307struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002308 unsigned int pt_base,
2309 unsigned int gpuaddr,
2310 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002311{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002312 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002313 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2314 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2315
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002316 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2317 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002318
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002319 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2320 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002321
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002322 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2323 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002324
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002325 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2326 size))
2327 return &device->mmu.setstate_memory;
2328
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002329 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002330
2331 if (entry)
2332 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002333
Jordan Crouse233b2092012-04-18 09:31:09 -06002334 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002335}
2336
2337uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2338 unsigned int gpuaddr, unsigned int size)
2339{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002340 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002341
2342 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2343
2344 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002345}
2346
2347void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2348 unsigned int *value)
2349{
2350 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002351 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2352 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002353
2354 if (!in_interrupt())
2355 kgsl_pre_hwaccess(device);
2356
2357 /*ensure this read finishes before the next one.
2358 * i.e. act like normal readl() */
2359 *value = __raw_readl(reg);
2360 rmb();
2361}
2362
2363void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2364 unsigned int value)
2365{
2366 unsigned int *reg;
2367
Jordan Crouse7501d452012-04-19 08:58:44 -06002368 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002369
2370 if (!in_interrupt())
2371 kgsl_pre_hwaccess(device);
2372
2373 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002374 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002375
2376 /*ensure previous writes post before this one,
2377 * i.e. act like normal writel() */
2378 wmb();
2379 __raw_writel(value, reg);
2380}
2381
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002382static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2383{
2384 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002385 if (k_ctxt != NULL) {
2386 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002387 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2388 context_id = KGSL_CONTEXT_INVALID;
2389 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2390 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002391 }
2392
2393 return context_id;
2394}
2395
Jordan Crouse313faf62012-11-20 15:12:28 -07002396static void adreno_next_event(struct kgsl_device *device,
2397 struct kgsl_event *event)
2398{
2399 int status;
2400 unsigned int ref_ts, enableflag;
2401 unsigned int context_id = _get_context_id(event->context);
2402 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2403
2404 status = kgsl_check_timestamp(device, event->context, event->timestamp);
2405 if (!status) {
2406 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2407 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2408 /*
2409 * Barrier is needed here to make sure the read from memstore
2410 * has posted
2411 */
2412
2413 mb();
2414
2415 if (enableflag) {
2416 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
2417 KGSL_MEMSTORE_OFFSET(context_id,
2418 ref_wait_ts));
2419
2420 /* Make sure the memstore read has posted */
2421 mb();
2422 if (timestamp_cmp(ref_ts, event->timestamp) >= 0) {
2423 kgsl_sharedmem_writel(&device->memstore,
2424 KGSL_MEMSTORE_OFFSET(context_id,
2425 ref_wait_ts), event->timestamp);
2426 /* Make sure the memstore write is posted */
2427 wmb();
2428 }
2429 } else {
2430 unsigned int cmds[2];
2431 kgsl_sharedmem_writel(&device->memstore,
2432 KGSL_MEMSTORE_OFFSET(context_id,
2433 ref_wait_ts), event->timestamp);
2434 enableflag = 1;
2435 kgsl_sharedmem_writel(&device->memstore,
2436 KGSL_MEMSTORE_OFFSET(context_id,
2437 ts_cmp_enable), enableflag);
2438
2439 /* Make sure the memstore write gets posted */
2440 wmb();
2441
2442 /*
2443 * submit a dummy packet so that even if all
2444 * commands upto timestamp get executed we will still
2445 * get an interrupt
2446 */
2447 cmds[0] = cp_type3_packet(CP_NOP, 1);
2448 cmds[1] = 0;
2449
2450 if (adreno_dev->drawctxt_active)
2451 adreno_ringbuffer_issuecmds_intr(device,
2452 event->context, &cmds[0], 2);
2453 }
2454 }
2455}
2456
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002457static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002458 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002459{
2460 int status;
2461 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002462 unsigned int context_id;
2463
2464 mutex_lock(&device->mutex);
2465 context_id = _get_context_id(context);
2466 /*
2467 * If the context ID is invalid, we are in a race with
2468 * the context being destroyed by userspace so bail.
2469 */
2470 if (context_id == KGSL_CONTEXT_INVALID) {
2471 KGSL_DRV_WARN(device, "context was detached");
2472 status = -EINVAL;
2473 goto unlock;
2474 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002475
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002476 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002477 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002478 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002479 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002480 mb();
2481
2482 if (enableflag) {
2483 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002484 KGSL_MEMSTORE_OFFSET(context_id,
2485 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002486 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002487 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002488 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002489 KGSL_MEMSTORE_OFFSET(context_id,
2490 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002491 wmb();
2492 }
2493 } else {
2494 unsigned int cmds[2];
2495 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002496 KGSL_MEMSTORE_OFFSET(context_id,
2497 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002498 enableflag = 1;
2499 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002500 KGSL_MEMSTORE_OFFSET(context_id,
2501 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002502 wmb();
2503 /* submit a dummy packet so that even if all
2504 * commands upto timestamp get executed we will still
2505 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002506 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002507 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002508
Vladimir Razgulin38345302013-01-22 18:41:59 -07002509 if (context && device->state != KGSL_STATE_SLUMBER)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002510 adreno_ringbuffer_issuecmds_intr(device,
2511 context, &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002512 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002513 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002514unlock:
2515 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002516
2517 return status;
2518}
2519
2520/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002521 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002522 placing a process in wait q. For conditional interrupts we expect the
2523 process to already be in its wait q when its exit condition checking
2524 function is called.
2525*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002526#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002527({ \
2528 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002529 if (io) \
2530 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2531 else \
2532 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002533 __ret; \
2534})
2535
Tarun Karra3335f142012-06-19 14:11:48 -07002536
2537
2538unsigned int adreno_hang_detect(struct kgsl_device *device,
2539 unsigned int *prev_reg_val)
2540{
2541 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2542 unsigned int curr_reg_val[hang_detect_regs_count];
2543 unsigned int hang_detected = 1;
2544 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002545 static unsigned long next_hang_detect_time;
Tarun Karra3335f142012-06-19 14:11:48 -07002546
2547 if (!adreno_dev->fast_hang_detect)
2548 return 0;
2549
Jordan Crousecca61142012-11-20 10:54:24 -07002550 if (is_adreno_rbbm_status_idle(device)) {
2551
2552 /*
2553 * On A20X if the RPTR != WPTR and the device is idle, then
2554 * the last write to WPTR probably failed to latch so write it
2555 * again
2556 */
2557
2558 if (adreno_is_a2xx(adreno_dev)) {
2559 unsigned int rptr;
2560 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2561 if (rptr != adreno_dev->ringbuffer.wptr)
2562 adreno_regwrite(device, REG_CP_RB_WPTR,
2563 adreno_dev->ringbuffer.wptr);
2564 }
2565
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002566 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002567 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002568
Tarun Karra4648db92013-02-01 17:03:40 -08002569 /*
2570 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2571 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2572 * from the last time do nothing.
2573 */
2574 if ((next_hang_detect_time) &&
2575 (time_before(jiffies, next_hang_detect_time)))
2576 return 0;
2577 else
2578 next_hang_detect_time = (jiffies +
2579 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2580
Tarun Karra3335f142012-06-19 14:11:48 -07002581 for (i = 0; i < hang_detect_regs_count; i++) {
Jordan Crouseb5c80482012-10-03 09:38:41 -06002582
2583 if (hang_detect_regs[i] == 0)
2584 continue;
2585
Tarun Karra3335f142012-06-19 14:11:48 -07002586 adreno_regread(device, hang_detect_regs[i],
2587 &curr_reg_val[i]);
2588 if (curr_reg_val[i] != prev_reg_val[i]) {
2589 prev_reg_val[i] = curr_reg_val[i];
2590 hang_detected = 0;
2591 }
2592 }
2593
2594 return hang_detected;
2595}
2596
Jordan Crouse92446a62012-11-15 11:00:06 -07002597/**
2598 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2599 * @device - pointer to a KGSL device structure
2600 * @context - pointer to the active KGSL context
2601 * @timestamp - the timestamp that the process was waiting for
2602 *
Tarun Karrad20d71a2013-01-25 15:38:57 -08002603 * Process a possible GPU hang and try fault tolerance from it
2604 * cleanly
Jordan Crouse92446a62012-11-15 11:00:06 -07002605 */
2606static int adreno_handle_hang(struct kgsl_device *device,
2607 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002608{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002609 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002610 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002611 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002612 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002613
Jordan Crouse92446a62012-11-15 11:00:06 -07002614 /* Do one last check to see if we somehow made it through */
2615 if (kgsl_check_timestamp(device, context, timestamp))
2616 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002617
2618 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002619
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002620 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2621 mb();
2622
Jeremy Gebben63904832012-02-07 16:10:55 -07002623 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002624 "Device hang detected while waiting for timestamp: "
2625 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002626 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2627 context_id, timestamp, context_id, ts_issued, context_id,
2628 kgsl_readtimestamp(device, context,
2629 KGSL_TIMESTAMP_RETIRED),
2630 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002631
Tarun Karrad20d71a2013-01-25 15:38:57 -08002632 /* Return 0 after a successful fault tolerance */
2633 if (!adreno_dump_and_exec_ft(device))
Jordan Crouse92446a62012-11-15 11:00:06 -07002634 return 0;
2635
2636 return -ETIMEDOUT;
2637}
2638
2639static int _check_pending_timestamp(struct kgsl_device *device,
2640 struct kgsl_context *context, unsigned int timestamp)
2641{
2642 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2643 unsigned int context_id = _get_context_id(context);
2644 unsigned int ts_issued;
2645
2646 if (context_id == KGSL_CONTEXT_INVALID)
2647 return -EINVAL;
2648
2649 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2650
2651 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2652 return 0;
2653
2654 if (context && !context->wait_on_invalid_ts) {
2655 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2656 context_id, timestamp, context_id, ts_issued);
2657
2658 /* Only print this message once */
2659 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002660 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002661
2662 return -EINVAL;
2663}
2664
2665/**
2666 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2667 * @device - pointer to a KGSL device structure
2668 * @context - pointer to the active kgsl context
2669 * @timestamp - GPU timestamp to wait for
2670 * @msecs - amount of time to wait (in milliseconds)
2671 *
2672 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2673 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2674 * one if it happened. Otherwise, spend most of our time in an interruptible
2675 * wait for the timestamp interrupt to be processed. This function must be
2676 * called with the mutex already held.
2677 */
2678static int adreno_waittimestamp(struct kgsl_device *device,
2679 struct kgsl_context *context,
2680 unsigned int timestamp,
2681 unsigned int msecs)
2682{
2683 static unsigned int io_cnt;
2684 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2685 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2686 unsigned int context_id = _get_context_id(context);
2687 unsigned int prev_reg_val[hang_detect_regs_count];
2688 unsigned int time_elapsed = 0;
2689 unsigned int wait;
2690 int ts_compare = 1;
2691 int io, ret = -ETIMEDOUT;
2692
2693 /* Get out early if the context has already been destroyed */
2694
2695 if (context_id == KGSL_CONTEXT_INVALID) {
2696 KGSL_DRV_WARN(device, "context was detached");
2697 return -EINVAL;
2698 }
2699
2700 /*
2701 * Check to see if the requested timestamp is "newer" then the last
2702 * timestamp issued. If it is complain once and return error. Only
2703 * print the message once per context so that badly behaving
2704 * applications don't spam the logs
2705 */
2706
2707 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2708 if (_check_pending_timestamp(device, context, timestamp))
2709 return -EINVAL;
2710
2711 /* Reset the invalid timestamp flag on a valid wait */
2712 context->wait_on_invalid_ts = false;
2713 }
2714
2715
2716 /* Clear the registers used for hang detection */
2717 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2718
2719 /*
2720 * On the first time through the loop only wait 100ms.
2721 * this gives enough time for the engine to start moving and oddly
2722 * provides better hang detection results than just going the full
2723 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2724 * is if msecs happens to be < 100ms then just use the full timeout
2725 */
2726
2727 wait = 100;
2728
2729 do {
2730 long status;
2731
Jordan Crouse92446a62012-11-15 11:00:06 -07002732 /*
2733 * if the timestamp happens while we're not
2734 * waiting, there's a chance that an interrupt
2735 * will not be generated and thus the timestamp
2736 * work needs to be queued.
2737 */
2738
2739 if (kgsl_check_timestamp(device, context, timestamp)) {
2740 queue_work(device->work_queue, &device->ts_expired_ws);
2741 ret = 0;
2742 break;
2743 }
2744
2745 /* Check to see if the GPU is hung */
2746 if (adreno_hang_detect(device, prev_reg_val)) {
2747 ret = adreno_handle_hang(device, context, timestamp);
2748 break;
2749 }
2750
2751 /*
2752 * For proper power accounting sometimes we need to call
2753 * io_wait_interruptible_timeout and sometimes we need to call
2754 * plain old wait_interruptible_timeout. We call the regular
2755 * timeout N times out of 100, where N is a number specified by
2756 * the current power level
2757 */
2758
2759 io_cnt = (io_cnt + 1) % 100;
2760 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2761 ? 0 : 1;
2762
2763 mutex_unlock(&device->mutex);
2764
2765 /* Wait for a timestamp event */
2766 status = kgsl_wait_event_interruptible_timeout(
2767 device->wait_queue,
2768 kgsl_check_interrupt_timestamp(device, context,
2769 timestamp), msecs_to_jiffies(wait), io);
2770
2771 mutex_lock(&device->mutex);
2772
2773 /*
2774 * If status is non zero then either the condition was satisfied
2775 * or there was an error. In either event, this is the end of
2776 * the line for us
2777 */
2778
2779 if (status != 0) {
2780 ret = (status > 0) ? 0 : (int) status;
2781 break;
2782 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002783 time_elapsed += wait;
2784
2785 /* If user specified timestamps are being used, wait at least
2786 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
2787 * issue a IB for a timestamp before checking to see if the
2788 * current timestamp we are waiting for is valid or not
2789 */
2790
2791 if (ts_compare && (adreno_ctx &&
2792 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
2793 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
2794 ret = _check_pending_timestamp(device, context,
2795 timestamp);
2796 if (ret)
2797 break;
2798
2799 /* Don't do this check again */
2800 ts_compare = 0;
2801
2802 /*
2803 * Reset the invalid timestamp flag on a valid
2804 * wait
2805 */
2806 context->wait_on_invalid_ts = false;
2807 }
2808 }
2809
2810 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07002811 * We want to wait the floor of KGSL_TIMEOUT_PART
2812 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07002813 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07002814
2815 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
2816 wait = KGSL_TIMEOUT_PART;
2817 else
2818 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07002819
2820 } while (!msecs || time_elapsed < msecs);
2821
2822 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002823}
2824
2825static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002826 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002827{
2828 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002829 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002830
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002831 /*
2832 * If the context ID is invalid, we are in a race with
2833 * the context being destroyed by userspace so bail.
2834 */
2835 if (context_id == KGSL_CONTEXT_INVALID) {
2836 KGSL_DRV_WARN(device, "context was detached");
2837 return timestamp;
2838 }
Jordan Crousec659f382012-04-16 11:10:41 -06002839 switch (type) {
2840 case KGSL_TIMESTAMP_QUEUED: {
2841 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2842 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2843
2844 timestamp = rb->timestamp[context_id];
2845 break;
2846 }
2847 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002848 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002849 break;
2850 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002851 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002852 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2853 break;
2854 }
2855
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002856 rmb();
2857
2858 return timestamp;
2859}
2860
2861static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2862 unsigned int cmd, void *data)
2863{
2864 int result = 0;
2865 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2866 struct kgsl_context *context;
2867
2868 switch (cmd) {
2869 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2870 binbase = data;
2871
2872 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2873 if (context) {
2874 adreno_drawctxt_set_bin_base_offset(
2875 dev_priv->device, context, binbase->offset);
2876 } else {
2877 result = -EINVAL;
2878 KGSL_DRV_ERR(dev_priv->device,
2879 "invalid drawctxt drawctxt_id %d "
2880 "device_id=%d\n",
2881 binbase->drawctxt_id, dev_priv->device->id);
2882 }
2883 break;
2884
2885 default:
2886 KGSL_DRV_INFO(dev_priv->device,
2887 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002888 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002889 break;
2890 }
2891 return result;
2892
2893}
2894
2895static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2896{
2897 gpu_freq /= 1000000;
2898 return ticks / gpu_freq;
2899}
2900
2901static void adreno_power_stats(struct kgsl_device *device,
2902 struct kgsl_power_stats *stats)
2903{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002904 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002905 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002906 unsigned int cycles;
2907
2908 /* Get the busy cycles counted since the counter was last reset */
2909 /* Calling this function also resets and restarts the counter */
2910
2911 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002912
2913 /* In order to calculate idle you have to have run the algorithm *
2914 * at least once to get a start time. */
2915 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002916 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002917 stats->total_time = tmp - pwr->time;
2918 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002919 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002920 pwrlevels[device->pwrctrl.active_pwrlevel].
2921 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002922 } else {
2923 stats->total_time = 0;
2924 stats->busy_time = 0;
2925 pwr->time = ktime_to_us(ktime_get());
2926 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002927}
2928
2929void adreno_irqctrl(struct kgsl_device *device, int state)
2930{
Jordan Crousea78c9172011-07-11 13:14:09 -06002931 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2932 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002933}
2934
Jordan Croused6535882012-06-20 08:22:16 -06002935static unsigned int adreno_gpuid(struct kgsl_device *device,
2936 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002937{
2938 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2939
Jordan Croused6535882012-06-20 08:22:16 -06002940 /* Some applications need to know the chip ID too, so pass
2941 * that as a parameter */
2942
2943 if (chipid != NULL)
2944 *chipid = adreno_dev->chip_id;
2945
Jordan Crousea0758f22011-12-07 11:19:22 -07002946 /* Standard KGSL gpuid format:
2947 * top word is 0x0002 for 2D or 0x0003 for 3D
2948 * Bottom word is core specific identifer
2949 */
2950
2951 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2952}
2953
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002954static const struct kgsl_functable adreno_functable = {
2955 /* Mandatory functions */
2956 .regread = adreno_regread,
2957 .regwrite = adreno_regwrite,
2958 .idle = adreno_idle,
2959 .isidle = adreno_isidle,
2960 .suspend_context = adreno_suspend_context,
2961 .start = adreno_start,
2962 .stop = adreno_stop,
2963 .getproperty = adreno_getproperty,
2964 .waittimestamp = adreno_waittimestamp,
2965 .readtimestamp = adreno_readtimestamp,
2966 .issueibcmds = adreno_ringbuffer_issueibcmds,
2967 .ioctl = adreno_ioctl,
2968 .setup_pt = adreno_setup_pt,
2969 .cleanup_pt = adreno_cleanup_pt,
2970 .power_stats = adreno_power_stats,
2971 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002972 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002973 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002974 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002975 /* Optional functions */
2976 .setstate = adreno_setstate,
2977 .drawctxt_create = adreno_drawctxt_create,
2978 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002979 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002980 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07002981 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002982};
2983
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002984static struct platform_driver adreno_platform_driver = {
2985 .probe = adreno_probe,
2986 .remove = __devexit_p(adreno_remove),
2987 .suspend = kgsl_suspend_driver,
2988 .resume = kgsl_resume_driver,
2989 .id_table = adreno_id_table,
2990 .driver = {
2991 .owner = THIS_MODULE,
2992 .name = DEVICE_3D_NAME,
2993 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002994 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002995 }
2996};
2997
2998static int __init kgsl_3d_init(void)
2999{
3000 return platform_driver_register(&adreno_platform_driver);
3001}
3002
3003static void __exit kgsl_3d_exit(void)
3004{
3005 platform_driver_unregister(&adreno_platform_driver);
3006}
3007
3008module_init(kgsl_3d_init);
3009module_exit(kgsl_3d_exit);
3010
3011MODULE_DESCRIPTION("3D Graphics driver");
3012MODULE_VERSION("1.2");
3013MODULE_LICENSE("GPL v2");
3014MODULE_ALIAS("platform:kgsl_3d");