blob: f0ce8f9a9c073085d4b1501d4b0dc874b8c6f371 [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>
Tarun Karra3164fb02013-02-05 15:38:51 -080020#include <linux/msm_kgsl.h>
Tarun Karra59e79542013-04-10 10:25:25 -070021#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022
23#include <mach/socinfo.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060024#include <mach/msm_bus_board.h>
25#include <mach/msm_bus.h>
26#include <mach/msm_dcvs.h>
27#include <mach/msm_dcvs_scm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29#include "kgsl.h"
30#include "kgsl_pwrscale.h"
31#include "kgsl_cffdump.h"
32#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060033#include "kgsl_iommu.h"
Jordan Crousef7b81e82013-05-28 17:03:00 -060034#include "kgsl_trace.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
36#include "adreno.h"
37#include "adreno_pm4types.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070039#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070040#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041
42#define DRIVER_VERSION_MAJOR 3
43#define DRIVER_VERSION_MINOR 1
44
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045/* Adreno MH arbiter config*/
46#define ADRENO_CFG_MHARB \
47 (0x10 \
48 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
51 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
53 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
54 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
55 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
56 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
57 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
58 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
59 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
60 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
61 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
62
63#define ADRENO_MMU_CONFIG \
64 (0x01 \
65 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
69 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
70 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
71 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
72 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
73 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
74 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
75 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
76
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077static const struct kgsl_functable adreno_functable;
78
79static struct adreno_device device_3d0 = {
80 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070081 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082 .name = DEVICE_3D0_NAME,
83 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060084 .mh = {
85 .mharb = ADRENO_CFG_MHARB,
86 /* Remove 1k boundary check in z470 to avoid a GPU
87 * hang. Notice that this solution won't work if
88 * both EBI and SMI are used
89 */
90 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091 /* turn off memory protection unit by setting
92 acceptable physical address range to include
93 all pages. */
94 .mpu_base = 0x00000000,
95 .mpu_range = 0xFFFFF000,
96 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060097 .mmu = {
98 .config = ADRENO_MMU_CONFIG,
99 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103 .iomemname = KGSL_3D0_REG_MEMORY,
104 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600106 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
108 .suspend = kgsl_early_suspend_driver,
109 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600111#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600113 .gmem_base = 0,
114 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115 .pfp_fw = NULL,
116 .pm4_fw = NULL,
Jordan Crouse21f75a02012-08-09 15:08:59 -0600117 .wait_timeout = 0, /* in milliseconds, 0 means disabled */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600118 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119};
120
Tarun Karra3335f142012-06-19 14:11:48 -0700121/* This set of registers are used for Hang detection
122 * If the values of these registers are same after
123 * KGSL_TIMEOUT_PART time, GPU hang is reported in
124 * kernel log.
Tarun Karra696f89e2013-01-27 21:31:40 -0800125 * *****ALERT******ALERT********ALERT*************
126 * Order of registers below is important, registers
127 * from LONG_IB_DETECT_REG_INDEX_START to
128 * LONG_IB_DETECT_REG_INDEX_END are used in long ib detection.
Tarun Karra3335f142012-06-19 14:11:48 -0700129 */
Tarun Karra696f89e2013-01-27 21:31:40 -0800130#define LONG_IB_DETECT_REG_INDEX_START 1
131#define LONG_IB_DETECT_REG_INDEX_END 5
132
133unsigned int ft_detect_regs[] = {
Tarun Karra3335f142012-06-19 14:11:48 -0700134 A3XX_RBBM_STATUS,
Tarun Karra696f89e2013-01-27 21:31:40 -0800135 REG_CP_RB_RPTR, /* LONG_IB_DETECT_REG_INDEX_START */
Tarun Karra3335f142012-06-19 14:11:48 -0700136 REG_CP_IB1_BASE,
137 REG_CP_IB1_BUFSZ,
138 REG_CP_IB2_BASE,
Tarun Karra696f89e2013-01-27 21:31:40 -0800139 REG_CP_IB2_BUFSZ, /* LONG_IB_DETECT_REG_INDEX_END */
Jordan Crouseb5c80482012-10-03 09:38:41 -0600140 0,
Tarun Karra6e750d72013-01-04 10:28:40 -0800141 0,
142 0,
143 0,
144 0,
Jordan Crouseb5c80482012-10-03 09:38:41 -0600145 0
Tarun Karra3335f142012-06-19 14:11:48 -0700146};
147
Tarun Karra696f89e2013-01-27 21:31:40 -0800148const unsigned int ft_detect_regs_count = ARRAY_SIZE(ft_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700149
Jordan Crouse505df9c2011-07-28 08:37:59 -0600150/*
151 * This is the master list of all GPU cores that are supported by this
152 * driver.
153 */
154
155#define ANY_ID (~0)
Tarun Karra9c070822012-11-27 16:43:51 -0700156#define NO_VER (~0)
Jordan Crouse505df9c2011-07-28 08:37:59 -0600157
158static const struct {
159 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600160 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600161 const char *pm4fw;
162 const char *pfpfw;
163 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700164 unsigned int istore_size;
165 unsigned int pix_shader_start;
Tarun Karra9c070822012-11-27 16:43:51 -0700166 /* Size of an instruction in dwords */
167 unsigned int instruction_size;
168 /* size of gmem for gpu*/
169 unsigned int gmem_size;
170 /* version of pm4 microcode that supports sync_lock
171 between CPU and GPU for SMMU-v1 programming */
172 unsigned int sync_lock_pm4_ver;
173 /* version of pfp microcode that supports sync_lock
174 between CPU and GPU for SMMU-v1 programming */
175 unsigned int sync_lock_pfp_ver;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600176} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600177 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700178 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700179 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530180 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
181 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700182 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600183 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700184 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700185 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600186 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700187 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700188 512, 384, 3, SZ_512K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600189 /*
190 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
191 * a hardware problem.
192 */
193 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700194 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700195 1536, 768, 3, SZ_512K, NO_VER, NO_VER },
Carter Cooperf27ec722011-11-17 15:20:38 -0700196 { ADRENO_REV_A225, 2, 2, 0, 6,
197 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700198 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600199 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700200 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700201 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530202 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530203 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530204 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700205 512, 0, 2, SZ_256K, 0x3FF037, 0x3FF016 },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700206 /* A3XX doesn't use the pix_shader_start */
Carter Cooper95f7f792012-08-19 13:40:34 -0600207 { ADRENO_REV_A320, 3, 2, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700208 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700209 512, 0, 2, SZ_512K, 0x3FF037, 0x3FF016 },
liu zhongfd42e622012-05-01 19:18:30 -0700210 { ADRENO_REV_A330, 3, 3, 0, 0,
211 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700212 512, 0, 2, SZ_1M, NO_VER, NO_VER },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600213};
214
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600215static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216{
Jordan Crousea78c9172011-07-11 13:14:09 -0600217 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600218 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219
Jordan Crousea78c9172011-07-11 13:14:09 -0600220 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221
222 if (device->requested_state == KGSL_STATE_NONE) {
223 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700224 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 queue_work(device->work_queue, &device->idle_check_ws);
226 } else if (device->pwrscale.policy != NULL) {
227 queue_work(device->work_queue, &device->idle_check_ws);
228 }
229 }
230
231 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800232 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 jiffies + device->pwrctrl.interval_timeout);
234 return result;
235}
236
Jordan Crouse9f739212011-07-28 08:37:57 -0600237static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 struct kgsl_pagetable *pagetable)
239{
240 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
241 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
242
243 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
244
245 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
246
247 kgsl_mmu_unmap(pagetable, &device->memstore);
248
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600249 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250}
251
252static int adreno_setup_pt(struct kgsl_device *device,
253 struct kgsl_pagetable *pagetable)
254{
255 int result = 0;
256 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
257 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
258
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
260 GSL_PT_PAGE_RV);
261 if (result)
262 goto error;
263
264 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
265 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
266 if (result)
267 goto unmap_buffer_desc;
268
269 result = kgsl_mmu_map_global(pagetable, &device->memstore,
270 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
271 if (result)
272 goto unmap_memptrs_desc;
273
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600274 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
276 if (result)
277 goto unmap_memstore_desc;
278
Jeremy Gebben2aba0f32013-05-28 16:54:00 -0600279 /*
280 * Set the mpu end to the last "normal" global memory we use.
281 * For the IOMMU, this will be used to restrict access to the
282 * mapped registers.
283 */
284 device->mh.mpu_range = device->mmu.setstate_memory.gpuaddr +
285 device->mmu.setstate_memory.size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 return result;
287
288unmap_memstore_desc:
289 kgsl_mmu_unmap(pagetable, &device->memstore);
290
291unmap_memptrs_desc:
292 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
293
294unmap_buffer_desc:
295 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
296
297error:
298 return result;
299}
300
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600301static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600302 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600303 uint32_t flags)
304{
305 unsigned int pt_val, reg_pt_val;
Tarun Karra9c070822012-11-27 16:43:51 -0700306 unsigned int link[250];
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600307 unsigned int *cmds = &link[0];
308 int sizedwords = 0;
309 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600310 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600311 struct kgsl_context *context;
312 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600313
314 if (!adreno_dev->drawctxt_active)
315 return kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700316 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600317
Jordan Crouse67db48d2013-05-28 17:04:17 -0600318 context = kgsl_context_get(device, context_id);
319
Jordan Crouseeef8a132013-01-11 11:17:16 -0700320 if (context == NULL)
321 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600322 adreno_ctx = context->devctxt;
323
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600324 if (kgsl_mmu_enable_clk(&device->mmu,
325 KGSL_IOMMU_CONTEXT_USER))
Jordan Crouse67db48d2013-05-28 17:04:17 -0600326 goto done;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600327
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600328 cmds += __adreno_add_idle_indirect_cmds(cmds,
329 device->mmu.setstate_memory.gpuaddr +
330 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
331
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600332 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600333 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
334 device->mmu.setstate_memory.gpuaddr +
335 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
336 else
337 cmds += adreno_add_bank_change_cmds(cmds,
338 KGSL_IOMMU_CONTEXT_USER,
339 device->mmu.setstate_memory.gpuaddr +
340 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
341
Tarun Karra9c070822012-11-27 16:43:51 -0700342 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
343
344 /* Acquire GPU-CPU sync Lock here */
345 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
346
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700347 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
348 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600349 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600350 /*
351 * We need to perfrom the following operations for all
352 * IOMMU units
353 */
354 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700355 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
356 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600357 /*
358 * Set address of the new pagetable by writng to IOMMU
359 * TTBR0 register
360 */
361 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700362 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
363 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600364 *cmds++ = reg_pt_val;
365 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
366 *cmds++ = 0x00000000;
367
368 /*
369 * Read back the ttbr0 register as a barrier to ensure
370 * above writes have completed
371 */
372 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700373 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
374 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600375 reg_pt_val,
376 device->mmu.setstate_memory.gpuaddr +
377 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600378 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600379 }
380 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
381 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700382 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600383 */
384 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700385 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
386 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700387
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600388 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700389 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
390 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700391 KGSL_IOMMU_CTX_TLBIALL);
392 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600393
394 cmds += __adreno_add_idle_indirect_cmds(cmds,
395 device->mmu.setstate_memory.gpuaddr +
396 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
397
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600398 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700399 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
400 KGSL_IOMMU_CONTEXT_USER,
401 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700402 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600403 device->mmu.setstate_memory.gpuaddr +
404 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
405 }
406 }
407
Tarun Karra9c070822012-11-27 16:43:51 -0700408 /* Release GPU-CPU sync Lock here */
409 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
410
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600411 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600412 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700413 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
414 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600415 device->mmu.setstate_memory.gpuaddr +
416 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
417 else
418 cmds += adreno_add_bank_change_cmds(cmds,
419 KGSL_IOMMU_CONTEXT_PRIV,
420 device->mmu.setstate_memory.gpuaddr +
421 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
422
Tarun Karra9c070822012-11-27 16:43:51 -0700423 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
424
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600425 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600426 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700427 /* invalidate all base pointers */
428 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
429 *cmds++ = 0x7fff;
430 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600431 /* This returns the per context timestamp but we need to
432 * use the global timestamp for iommu clock disablement */
433 adreno_ringbuffer_issuecmds(device, adreno_ctx,
434 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600435 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600436 kgsl_mmu_disable_clk_on_ts(&device->mmu,
437 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600438 }
Tarun Karra9c070822012-11-27 16:43:51 -0700439
440 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
441 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
442 BUG();
443 }
Jordan Crouse67db48d2013-05-28 17:04:17 -0600444done:
445 kgsl_context_put(context);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600446}
447
448static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600449 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600450 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451{
452 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
453 unsigned int link[32];
454 unsigned int *cmds = &link[0];
455 int sizedwords = 0;
456 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600457 struct kgsl_context *context;
458 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600460 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530461 * Fix target freeze issue by adding TLB flush for each submit
462 * on A20X based targets.
463 */
464 if (adreno_is_a20x(adreno_dev))
465 flags |= KGSL_MMUFLAGS_TLBFLUSH;
466 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600467 * If possible, then set the state via the command stream to avoid
468 * a CPU idle. Otherwise, use the default setstate which uses register
469 * writes For CFF dump we must idle and use the registers so that it is
470 * easier to filter out the mmu accesses from the dump
471 */
472 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Jordan Crouse67db48d2013-05-28 17:04:17 -0600473 context = kgsl_context_get(device, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700474 if (context == NULL)
475 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600476 adreno_ctx = context->devctxt;
477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
479 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600480 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 *cmds++ = 0x00000000;
482
483 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600484 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700485 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600486 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 sizedwords += 4;
488 }
489
490 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
491 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600492 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 1);
494 *cmds++ = 0x00000000;
495 sizedwords += 2;
496 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600497 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 *cmds++ = mh_mmu_invalidate;
499 sizedwords += 2;
500 }
501
502 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600503 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 /* HW workaround: to resolve MMU page fault interrupts
505 * caused by the VGT.It prevents the CP PFP from filling
506 * the VGT DMA request fifo too early,thereby ensuring
507 * that the VGT will not fetch vertex/bin data until
508 * after the page table base register has been updated.
509 *
510 * Two null DRAW_INDX_BIN packets are inserted right
511 * after the page table base update, followed by a
512 * wait for idle. The null packets will fill up the
513 * VGT DMA request fifo and prevent any further
514 * vertex/bin updates from occurring until the wait
515 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600516 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 *cmds++ = (0x4 << 16) |
518 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
519 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600520 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600521 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600522 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 *cmds++ = 0; /* viz query info */
524 *cmds++ = 0x0003C004; /* draw indicator */
525 *cmds++ = 0; /* bin base */
526 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600527 *cmds++ =
528 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600530 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531 *cmds++ = 0; /* viz query info */
532 *cmds++ = 0x0003C004; /* draw indicator */
533 *cmds++ = 0; /* bin base */
534 *cmds++ = 3; /* bin size */
535 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600536 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600538 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 *cmds++ = 0x00000000;
540 sizedwords += 21;
541 }
542
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600543
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600545 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546 *cmds++ = 0x7fff; /* invalidate all base pointers */
547 sizedwords += 2;
548 }
549
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600550 adreno_ringbuffer_issuecmds(device, adreno_ctx,
551 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552 &link[0], sizedwords);
Jordan Crouse67db48d2013-05-28 17:04:17 -0600553
554 kgsl_context_put(context);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600555 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600556 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600557 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558}
559
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600560static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600561 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600562 uint32_t flags)
563{
564 /* call the mmu specific handler */
565 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600566 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600567 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600568 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600569}
570
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700572a3xx_getchipid(struct kgsl_device *device)
573{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600574 struct kgsl_device_platform_data *pdata =
575 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700576
Jordan Crouse54154c62012-03-27 16:33:26 -0600577 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600578 * All current A3XX chipids are detected at the SOC level. Leave this
579 * function here to support any future GPUs that have working
580 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600581 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700582
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600583 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700584}
585
586static unsigned int
587a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588{
589 unsigned int chipid = 0;
590 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600591 struct kgsl_device_platform_data *pdata =
592 kgsl_device_get_drvdata(device);
593
594 /* If the chip id is set at the platform level, then just use that */
595
596 if (pdata->chipid != 0)
597 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598
599 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
600 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
601 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
602
603 /*
604 * adreno 22x gpus are indicated by coreid 2,
605 * but REG_RBBM_PERIPHID1 always contains 0 for this field
606 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600607 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 chipid = 2 << 24;
609 else
610 chipid = (coreid & 0xF) << 24;
611
612 chipid |= ((majorid >> 4) & 0xF) << 16;
613
614 minorid = ((revid >> 0) & 0xFF);
615
616 patchid = ((revid >> 16) & 0xFF);
617
618 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530619 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620 if (cpu_is_qsd8x50())
621 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530622 else if (cpu_is_msm8625() && minorid == 0)
623 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624
625 chipid |= (minorid << 8) | patchid;
626
627 return chipid;
628}
629
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700630static unsigned int
631adreno_getchipid(struct kgsl_device *device)
632{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600633 struct kgsl_device_platform_data *pdata =
634 kgsl_device_get_drvdata(device);
635
636 /*
637 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
638 * an A2XX processor
639 */
640
641 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700642 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600643 else
644 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700645}
646
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647static inline bool _rev_match(unsigned int id, unsigned int entry)
648{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600649 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651
652static void
653adreno_identify_gpu(struct adreno_device *adreno_dev)
654{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600655 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656
657 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
658
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600659 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
660 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
661 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
662 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663
Jordan Crouse505df9c2011-07-28 08:37:59 -0600664 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
665 if (core == adreno_gpulist[i].core &&
666 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600667 _rev_match(minor, adreno_gpulist[i].minor) &&
668 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 }
671
Jordan Crouse505df9c2011-07-28 08:37:59 -0600672 if (i == ARRAY_SIZE(adreno_gpulist)) {
673 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
674 return;
675 }
676
677 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
678 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
679 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
680 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700681 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
682 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700683 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600684 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Tarun Karra9c070822012-11-27 16:43:51 -0700685 adreno_dev->gpulist_index = i;
686
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687}
688
Lokesh Batra805e1e12012-08-03 08:34:06 -0600689static struct platform_device_id adreno_id_table[] = {
690 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
691 {},
692};
693
694MODULE_DEVICE_TABLE(platform, adreno_id_table);
695
696static struct of_device_id adreno_match_table[] = {
697 { .compatible = "qcom,kgsl-3d0", },
698 {}
699};
700
701static inline int adreno_of_read_property(struct device_node *node,
702 const char *prop, unsigned int *ptr)
703{
704 int ret = of_property_read_u32(node, prop, ptr);
705 if (ret)
706 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
707 return ret;
708}
709
710static struct device_node *adreno_of_find_subnode(struct device_node *parent,
711 const char *name)
712{
713 struct device_node *child;
714
715 for_each_child_of_node(parent, child) {
716 if (of_device_is_compatible(child, name))
717 return child;
718 }
719
720 return NULL;
721}
722
723static int adreno_of_get_pwrlevels(struct device_node *parent,
724 struct kgsl_device_platform_data *pdata)
725{
726 struct device_node *node, *child;
727 int ret = -EINVAL;
728
729 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
730
731 if (node == NULL) {
732 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
733 return -EINVAL;
734 }
735
736 pdata->num_levels = 0;
737
738 for_each_child_of_node(node, child) {
739 unsigned int index;
740 struct kgsl_pwrlevel *level;
741
742 if (adreno_of_read_property(child, "reg", &index))
743 goto done;
744
745 if (index >= KGSL_MAX_PWRLEVELS) {
746 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
747 index);
748 continue;
749 }
750
751 if (index >= pdata->num_levels)
752 pdata->num_levels = index + 1;
753
754 level = &pdata->pwrlevel[index];
755
756 if (adreno_of_read_property(child, "qcom,gpu-freq",
757 &level->gpu_freq))
758 goto done;
759
760 if (adreno_of_read_property(child, "qcom,bus-freq",
761 &level->bus_freq))
762 goto done;
763
764 if (adreno_of_read_property(child, "qcom,io-fraction",
765 &level->io_fraction))
766 level->io_fraction = 0;
767 }
768
769 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
770 &pdata->init_level))
771 pdata->init_level = 1;
772
773 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
774 KGSL_CORE_ERR("Initial power level out of range\n");
775 pdata->init_level = 1;
776 }
777
778 ret = 0;
779done:
780 return ret;
781
782}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600783
784static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
785{
786 struct device_node *node, *child;
787 struct msm_dcvs_core_info *info = NULL;
788 int count = 0;
789 int ret = -EINVAL;
790
791 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
792 if (node == NULL)
793 return ERR_PTR(-EINVAL);
794
795 info = kzalloc(sizeof(*info), GFP_KERNEL);
796
797 if (info == NULL) {
798 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
799 ret = -ENOMEM;
800 goto err;
801 }
802
803 for_each_child_of_node(node, child)
804 count++;
805
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700806 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600807
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700808 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600809 sizeof(struct msm_dcvs_freq_entry),
810 GFP_KERNEL);
811
812 if (info->freq_tbl == NULL) {
813 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700814 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600815 sizeof(struct msm_dcvs_freq_entry));
816 ret = -ENOMEM;
817 goto err;
818 }
819
820 for_each_child_of_node(node, child) {
821 unsigned int index;
822
823 if (adreno_of_read_property(child, "reg", &index))
824 goto err;
825
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700826 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600827 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
828 index);
829 continue;
830 }
831
832 if (adreno_of_read_property(child, "qcom,freq",
833 &info->freq_tbl[index].freq))
834 goto err;
835
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700836 if (adreno_of_read_property(child, "qcom,voltage",
837 &info->freq_tbl[index].voltage))
838 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600839
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700840 if (adreno_of_read_property(child, "qcom,is_trans_level",
841 &info->freq_tbl[index].is_trans_level))
842 info->freq_tbl[index].is_trans_level = 0;
843
844 if (adreno_of_read_property(child, "qcom,active-energy-offset",
845 &info->freq_tbl[index].active_energy_offset))
846 info->freq_tbl[index].active_energy_offset = 0;
847
848 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
849 &info->freq_tbl[index].leakage_energy_offset))
850 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600851 }
852
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700853 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
854 goto err;
855
856 info->sensors = kzalloc(info->num_cores *
857 sizeof(int),
858 GFP_KERNEL);
859
860 for (count = 0; count < info->num_cores; count++) {
861 if (adreno_of_read_property(node, "qcom,sensors",
862 &(info->sensors[count])))
863 goto err;
864 }
865
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700866 if (adreno_of_read_property(node, "qcom,core-core-type",
867 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600868 goto err;
869
870 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
871 &info->algo_param.disable_pc_threshold))
872 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700873 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
874 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600875 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700876 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
877 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600878 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600879 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
880 &info->algo_param.em_max_util_pct))
881 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700882 if (adreno_of_read_property(node, "qcom,algo-group-id",
883 &info->algo_param.group_id))
884 goto err;
885 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
886 &info->algo_param.max_freq_chg_time_us))
887 goto err;
888 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
889 &info->algo_param.slack_mode_dynamic))
890 goto err;
891 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
892 &info->algo_param.slack_weight_thresh_pct))
893 goto err;
894 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
895 &info->algo_param.slack_time_min_us))
896 goto err;
897 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
898 &info->algo_param.slack_time_max_us))
899 goto err;
900 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
901 &info->algo_param.ss_win_size_min_us))
902 goto err;
903 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
904 &info->algo_param.ss_win_size_max_us))
905 goto err;
906 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
907 &info->algo_param.ss_util_pct))
908 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800909 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
910 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600911 goto err;
912
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700913 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
914 &info->energy_coeffs.active_coeff_a))
915 goto err;
916 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
917 &info->energy_coeffs.active_coeff_b))
918 goto err;
919 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
920 &info->energy_coeffs.active_coeff_c))
921 goto err;
922 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
923 &info->energy_coeffs.leakage_coeff_a))
924 goto err;
925 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
926 &info->energy_coeffs.leakage_coeff_b))
927 goto err;
928 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
929 &info->energy_coeffs.leakage_coeff_c))
930 goto err;
931 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
932 &info->energy_coeffs.leakage_coeff_d))
933 goto err;
934
935 if (adreno_of_read_property(node, "qcom,power-current-temp",
936 &info->power_param.current_temp))
937 goto err;
938
Lokesh Batra805e1e12012-08-03 08:34:06 -0600939 return info;
940
941err:
942 if (info)
943 kfree(info->freq_tbl);
944
945 kfree(info);
946
947 return ERR_PTR(ret);
948}
949
950static int adreno_of_get_iommu(struct device_node *parent,
951 struct kgsl_device_platform_data *pdata)
952{
953 struct device_node *node, *child;
954 struct kgsl_device_iommu_data *data = NULL;
955 struct kgsl_iommu_ctx *ctxs = NULL;
956 u32 reg_val[2];
957 int ctx_index = 0;
958
959 node = of_parse_phandle(parent, "iommu", 0);
960 if (node == NULL)
961 return -EINVAL;
962
963 data = kzalloc(sizeof(*data), GFP_KERNEL);
964 if (data == NULL) {
965 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
966 goto err;
967 }
968
969 if (of_property_read_u32_array(node, "reg", reg_val, 2))
970 goto err;
971
972 data->physstart = reg_val[0];
973 data->physend = data->physstart + reg_val[1] - 1;
974
975 data->iommu_ctx_count = 0;
976
977 for_each_child_of_node(node, child)
978 data->iommu_ctx_count++;
979
980 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
981 GFP_KERNEL);
982
983 if (ctxs == NULL) {
984 KGSL_CORE_ERR("kzalloc(%d) failed\n",
985 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
986 goto err;
987 }
988
989 for_each_child_of_node(node, child) {
990 int ret = of_property_read_string(child, "label",
991 &ctxs[ctx_index].iommu_ctx_name);
992
993 if (ret) {
994 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
995 goto err;
996 }
997
998 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
999 &ctxs[ctx_index].ctx_id))
1000 goto err;
1001
1002 ctx_index++;
1003 }
1004
1005 data->iommu_ctxs = ctxs;
1006
1007 pdata->iommu_data = data;
1008 pdata->iommu_count = 1;
1009
1010 return 0;
1011
1012err:
1013 kfree(ctxs);
1014 kfree(data);
1015
1016 return -EINVAL;
1017}
1018
1019static int adreno_of_get_pdata(struct platform_device *pdev)
1020{
1021 struct kgsl_device_platform_data *pdata = NULL;
1022 struct kgsl_device *device;
1023 int ret = -EINVAL;
1024
1025 pdev->id_entry = adreno_id_table;
1026
1027 pdata = pdev->dev.platform_data;
1028 if (pdata)
1029 return 0;
1030
1031 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1032 KGSL_CORE_ERR("Unable to read 'label'\n");
1033 goto err;
1034 }
1035
1036 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1037 goto err;
1038
1039 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1040 if (pdata == NULL) {
1041 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1042 ret = -ENOMEM;
1043 goto err;
1044 }
1045
1046 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1047 &pdata->chipid))
1048 goto err;
1049
1050 /* pwrlevel Data */
1051 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1052 if (ret)
1053 goto err;
1054
1055 /* Default value is 83, if not found in DT */
1056 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1057 &pdata->idle_timeout))
1058 pdata->idle_timeout = 83;
1059
1060 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1061 &pdata->nap_allowed))
1062 pdata->nap_allowed = 1;
1063
1064 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1065 &pdata->clk_map))
1066 goto err;
1067
1068 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1069
1070 if (device->id != KGSL_DEVICE_3D0)
1071 goto err;
1072
1073 /* Bus Scale Data */
1074
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001075 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001076 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1077 ret = PTR_ERR(pdata->bus_scale_table);
1078 goto err;
1079 }
1080
1081 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1082 if (IS_ERR_OR_NULL(pdata->core_info)) {
1083 ret = PTR_ERR(pdata->core_info);
1084 goto err;
1085 }
1086
1087 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1088 if (ret)
1089 goto err;
1090
1091 pdev->dev.platform_data = pdata;
1092 return 0;
1093
1094err:
1095 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001096 if (pdata->core_info)
1097 kfree(pdata->core_info->freq_tbl);
1098 kfree(pdata->core_info);
1099
1100 if (pdata->iommu_data)
1101 kfree(pdata->iommu_data->iommu_ctxs);
1102
1103 kfree(pdata->iommu_data);
1104 }
1105
1106 kfree(pdata);
1107
1108 return ret;
1109}
1110
liu zhong7dfa2a32012-04-27 19:11:01 -07001111#ifdef CONFIG_MSM_OCMEM
1112static int
1113adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1114{
Jordan Crousec0978202012-08-29 14:35:51 -06001115 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001116 return 0;
1117
1118 /* OCMEM is only needed once, do not support consective allocation */
1119 if (adreno_dev->ocmem_hdl != NULL)
1120 return 0;
1121
1122 adreno_dev->ocmem_hdl =
1123 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1124 if (adreno_dev->ocmem_hdl == NULL)
1125 return -ENOMEM;
1126
1127 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001128 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001129
1130 return 0;
1131}
1132
1133static void
1134adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1135{
Jordan Crousec0978202012-08-29 14:35:51 -06001136 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001137 return;
1138
1139 if (adreno_dev->ocmem_hdl == NULL)
1140 return;
1141
1142 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1143 adreno_dev->ocmem_hdl = NULL;
1144}
1145#else
1146static int
1147adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1148{
1149 return 0;
1150}
1151
1152static void
1153adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1154{
1155}
1156#endif
1157
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158static int __devinit
1159adreno_probe(struct platform_device *pdev)
1160{
1161 struct kgsl_device *device;
1162 struct adreno_device *adreno_dev;
1163 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001164 bool is_dt;
1165
1166 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1167
1168 if (is_dt && pdev->dev.of_node) {
1169 status = adreno_of_get_pdata(pdev);
1170 if (status)
1171 goto error_return;
1172 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173
1174 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1175 adreno_dev = ADRENO_DEVICE(device);
1176 device->parentdev = &pdev->dev;
1177
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 status = adreno_ringbuffer_init(device);
1179 if (status != 0)
1180 goto error;
1181
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001182 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183 if (status)
1184 goto error_close_rb;
1185
1186 adreno_debugfs_init(device);
1187
1188 kgsl_pwrscale_init(device);
1189 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1190
1191 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1192 return 0;
1193
1194error_close_rb:
1195 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1196error:
1197 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001198error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001199 return status;
1200}
1201
1202static int __devexit adreno_remove(struct platform_device *pdev)
1203{
1204 struct kgsl_device *device;
1205 struct adreno_device *adreno_dev;
1206
1207 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1208 adreno_dev = ADRENO_DEVICE(device);
1209
1210 kgsl_pwrscale_detach_policy(device);
1211 kgsl_pwrscale_close(device);
1212
1213 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1214 kgsl_device_platform_remove(device);
1215
1216 return 0;
1217}
1218
1219static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1220{
1221 int status = -EINVAL;
1222 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223
Tarun Karrad20d71a2013-01-25 15:38:57 -08001224 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001225 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226
1227 /* Power up the device */
1228 kgsl_pwrctrl_enable(device);
1229
1230 /* Identify the specific GPU */
1231 adreno_identify_gpu(adreno_dev);
1232
Tarun Karra9c070822012-11-27 16:43:51 -07001233 if (adreno_ringbuffer_read_pm4_ucode(device)) {
1234 KGSL_DRV_ERR(device, "Reading pm4 microcode failed %s\n",
1235 adreno_dev->pm4_fwfile);
1236 BUG_ON(1);
1237 }
1238
1239 if (adreno_ringbuffer_read_pfp_ucode(device)) {
1240 KGSL_DRV_ERR(device, "Reading pfp microcode failed %s\n",
1241 adreno_dev->pfp_fwfile);
1242 BUG_ON(1);
1243 }
1244
Jordan Crouse505df9c2011-07-28 08:37:59 -06001245 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1246 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1247 adreno_dev->chip_id);
1248 goto error_clk_off;
1249 }
1250
Tarun Karra9c070822012-11-27 16:43:51 -07001251
1252 /*
1253 * Check if firmware supports the sync lock PM4 packets needed
1254 * for IOMMUv1
1255 */
1256
1257 if ((adreno_dev->pm4_fw_version >=
1258 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pm4_ver) &&
1259 (adreno_dev->pfp_fw_version >=
1260 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pfp_ver))
1261 device->mmu.flags |= KGSL_MMU_FLAGS_IOMMU_SYNC;
1262
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001263 /* Set up the MMU */
1264 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001265 /*
1266 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1267 * on older gpus
1268 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001269 if (adreno_is_a20x(adreno_dev)) {
1270 device->mh.mh_intf_cfg1 = 0;
1271 device->mh.mh_intf_cfg2 = 0;
1272 }
1273
1274 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001275 }
1276
Tarun Karra3335f142012-06-19 14:11:48 -07001277 /* Assign correct RBBM status register to hang detect regs
1278 */
Tarun Karra696f89e2013-01-27 21:31:40 -08001279 ft_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
Tarun Karra3335f142012-06-19 14:11:48 -07001280
Jordan Crouseb5c80482012-10-03 09:38:41 -06001281 /* Add A3XX specific registers for hang detection */
1282 if (adreno_is_a3xx(adreno_dev)) {
Tarun Karra696f89e2013-01-27 21:31:40 -08001283 ft_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1284 ft_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
1285 ft_detect_regs[8] = A3XX_RBBM_PERFCTR_SP_6_LO;
1286 ft_detect_regs[9] = A3XX_RBBM_PERFCTR_SP_6_HI;
1287 ft_detect_regs[10] = A3XX_RBBM_PERFCTR_SP_5_LO;
1288 ft_detect_regs[11] = A3XX_RBBM_PERFCTR_SP_5_HI;
Jordan Crouseb5c80482012-10-03 09:38:41 -06001289 }
1290
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001291 status = kgsl_mmu_start(device);
1292 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 goto error_clk_off;
1294
liu zhong7dfa2a32012-04-27 19:11:01 -07001295 status = adreno_ocmem_gmem_malloc(adreno_dev);
1296 if (status) {
1297 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1298 goto error_mmu_off;
1299 }
1300
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001301 /* Start the GPU */
1302 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001303
1304 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001305 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306
1307 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouse013cf422013-05-28 17:03:32 -06001308 if (status)
1309 goto error_irq_off;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310
Jordan Crouse013cf422013-05-28 17:03:32 -06001311 /*
1312 * While recovery is on we do not want timer to
1313 * fire and attempt to change any device state
1314 */
1315
1316 if (KGSL_STATE_DUMP_AND_FT != device->state)
1317 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1318
1319 device->reset_counter++;
1320
1321 return 0;
1322
1323error_irq_off:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001324 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001325
1326error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001327 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001328
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329error_clk_off:
1330 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331
1332 return status;
1333}
1334
1335static int adreno_stop(struct kgsl_device *device)
1336{
1337 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339 adreno_dev->drawctxt_active = NULL;
1340
1341 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1342
Shubhraprakash Das79447952012-04-26 18:12:23 -06001343 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001344
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001345 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301346 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001347 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001348
liu zhong7dfa2a32012-04-27 19:11:01 -07001349 adreno_ocmem_gmem_free(adreno_dev);
1350
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001351 /* Power down the device */
1352 kgsl_pwrctrl_disable(device);
1353
1354 return 0;
1355}
1356
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001357static void adreno_mark_context_status(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001358 int ft_status)
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001359{
1360 struct kgsl_context *context;
1361 int next = 0;
1362 /*
1363 * Set the reset status of all contexts to
1364 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
Tarun Karrad20d71a2013-01-25 15:38:57 -08001365 * since thats the guilty party, if fault tolerance failed then
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001366 * mark all as guilty
1367 */
Jordan Crouse67db48d2013-05-28 17:04:17 -06001368
1369 rcu_read_lock();
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001370 while ((context = idr_get_next(&device->context_idr, &next))) {
1371 struct adreno_context *adreno_context = context->devctxt;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001372 if (ft_status) {
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001373 context->reset_status =
1374 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1375 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1376 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1377 context->reset_status) {
Carter Cooper8179f5a2012-12-17 11:32:27 -07001378 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG |
Tarun Karrad20d71a2013-01-25 15:38:57 -08001379 CTXT_FLAGS_GPU_HANG_FT))
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001380 context->reset_status =
1381 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1382 else
1383 context->reset_status =
1384 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1385 }
1386 next = next + 1;
1387 }
Jordan Crouse67db48d2013-05-28 17:04:17 -06001388 rcu_read_unlock();
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001389}
1390
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001391static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1392{
1393 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1394 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1395 struct kgsl_context *context;
1396 struct adreno_context *temp_adreno_context;
1397 int next = 0;
1398
Jordan Crouse67db48d2013-05-28 17:04:17 -06001399 rcu_read_lock();
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001400 while ((context = idr_get_next(&device->context_idr, &next))) {
1401 temp_adreno_context = context->devctxt;
1402 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1403 kgsl_sharedmem_writel(&device->memstore,
1404 KGSL_MEMSTORE_OFFSET(context->id,
1405 soptimestamp),
1406 rb->timestamp[context->id]);
1407 kgsl_sharedmem_writel(&device->memstore,
1408 KGSL_MEMSTORE_OFFSET(context->id,
1409 eoptimestamp),
1410 rb->timestamp[context->id]);
1411 }
1412 next = next + 1;
1413 }
Jordan Crouse67db48d2013-05-28 17:04:17 -06001414 rcu_read_unlock();
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001415}
1416
Tarun Karrad20d71a2013-01-25 15:38:57 -08001417static void adreno_destroy_ft_data(struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001418{
Tarun Karrad20d71a2013-01-25 15:38:57 -08001419 vfree(ft_data->rb_buffer);
1420 vfree(ft_data->bad_rb_buffer);
1421 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001422}
1423
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001424static int _find_start_of_cmd_seq(struct adreno_ringbuffer *rb,
1425 unsigned int *ptr,
1426 bool inc)
1427{
1428 int status = -EINVAL;
1429 unsigned int val1;
1430 unsigned int size = rb->buffer_desc.size;
1431 unsigned int start_ptr = *ptr;
1432
1433 while ((start_ptr / sizeof(unsigned int)) != rb->wptr) {
1434 if (inc)
1435 start_ptr = adreno_ringbuffer_inc_wrapped(start_ptr,
1436 size);
1437 else
1438 start_ptr = adreno_ringbuffer_dec_wrapped(start_ptr,
1439 size);
1440 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, start_ptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001441 /* Ensure above read is finished before next read */
1442 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001443 if (KGSL_CMD_IDENTIFIER == val1) {
1444 if ((start_ptr / sizeof(unsigned int)) != rb->wptr)
1445 start_ptr = adreno_ringbuffer_dec_wrapped(
1446 start_ptr, size);
1447 *ptr = start_ptr;
1448 status = 0;
1449 break;
1450 }
1451 }
1452 return status;
1453}
1454
1455static int _find_cmd_seq_after_eop_ts(struct adreno_ringbuffer *rb,
1456 unsigned int *rb_rptr,
1457 unsigned int global_eop,
1458 bool inc)
1459{
1460 int status = -EINVAL;
1461 unsigned int temp_rb_rptr = *rb_rptr;
1462 unsigned int size = rb->buffer_desc.size;
1463 unsigned int val[3];
1464 int i = 0;
1465 bool check = false;
1466
1467 if (inc && temp_rb_rptr / sizeof(unsigned int) != rb->wptr)
1468 return status;
1469
1470 do {
1471 /*
1472 * when decrementing we need to decrement first and
1473 * then read make sure we cover all the data
1474 */
1475 if (!inc)
1476 temp_rb_rptr = adreno_ringbuffer_dec_wrapped(
1477 temp_rb_rptr, size);
1478 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i],
1479 temp_rb_rptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001480 /* Ensure above read is finished before next read */
1481 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001482
1483 if (check && ((inc && val[i] == global_eop) ||
1484 (!inc && (val[i] ==
1485 cp_type3_packet(CP_MEM_WRITE, 2) ||
1486 val[i] == CACHE_FLUSH_TS)))) {
1487 /* decrement i, i.e i = (i - 1 + 3) % 3 if
1488 * we are going forward, else increment i */
1489 i = (i + 2) % 3;
1490 if (val[i] == rb->device->memstore.gpuaddr +
1491 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1492 eoptimestamp)) {
1493 int j = ((i + 2) % 3);
1494 if ((inc && (val[j] == CACHE_FLUSH_TS ||
1495 val[j] == cp_type3_packet(
1496 CP_MEM_WRITE, 2))) ||
1497 (!inc && val[j] == global_eop)) {
1498 /* Found the global eop */
1499 status = 0;
1500 break;
1501 }
1502 }
1503 /* if no match found then increment i again
1504 * since we decremented before matching */
1505 i = (i + 1) % 3;
1506 }
1507 if (inc)
1508 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(
1509 temp_rb_rptr, size);
1510
1511 i = (i + 1) % 3;
1512 if (2 == i)
1513 check = true;
1514 } while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr);
1515 /* temp_rb_rptr points to the command stream after global eop,
1516 * move backward till the start of command sequence */
1517 if (!status) {
1518 status = _find_start_of_cmd_seq(rb, &temp_rb_rptr, false);
1519 if (!status) {
1520 *rb_rptr = temp_rb_rptr;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001521 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001522 "Offset of cmd sequence after eop timestamp: 0x%x\n",
1523 temp_rb_rptr / sizeof(unsigned int));
1524 }
1525 }
1526 if (status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001527 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001528 "Failed to find the command sequence after eop timestamp\n");
1529 return status;
1530}
1531
1532static int _find_hanging_ib_sequence(struct adreno_ringbuffer *rb,
1533 unsigned int *rb_rptr,
1534 unsigned int ib1)
1535{
1536 int status = -EINVAL;
1537 unsigned int temp_rb_rptr = *rb_rptr;
1538 unsigned int size = rb->buffer_desc.size;
1539 unsigned int val[2];
1540 int i = 0;
1541 bool check = false;
1542 bool ctx_switch = false;
1543
1544 while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr) {
1545 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i], temp_rb_rptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001546 /* Ensure above read is finished before next read */
1547 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001548
1549 if (check && val[i] == ib1) {
1550 /* decrement i, i.e i = (i - 1 + 2) % 2 */
1551 i = (i + 1) % 2;
1552 if (adreno_cmd_is_ib(val[i])) {
1553 /* go till start of command sequence */
1554 status = _find_start_of_cmd_seq(rb,
1555 &temp_rb_rptr, false);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001556
1557 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001558 "Found the hanging IB at offset 0x%x\n",
1559 temp_rb_rptr / sizeof(unsigned int));
1560 break;
1561 }
1562 /* if no match the increment i since we decremented
1563 * before checking */
1564 i = (i + 1) % 2;
1565 }
1566 /* Make sure you do not encounter a context switch twice, we can
1567 * encounter it once for the bad context as the start of search
1568 * can point to the context switch */
1569 if (val[i] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
1570 if (ctx_switch) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001571 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001572 "Context switch encountered before bad "
1573 "IB found\n");
1574 break;
1575 }
1576 ctx_switch = true;
1577 }
1578 i = (i + 1) % 2;
1579 if (1 == i)
1580 check = true;
1581 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(temp_rb_rptr,
1582 size);
1583 }
1584 if (!status)
1585 *rb_rptr = temp_rb_rptr;
1586 return status;
1587}
1588
Tarun Karra59e79542013-04-10 10:25:25 -07001589static void adreno_setup_ft_data(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001590 struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001591{
1592 int ret = 0;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001593 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1594 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001595 struct kgsl_context *context;
1596 struct adreno_context *adreno_context;
1597 unsigned int rb_rptr = rb->wptr * sizeof(unsigned int);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001598
Tarun Karrad20d71a2013-01-25 15:38:57 -08001599 memset(ft_data, 0, sizeof(*ft_data));
1600 ft_data->start_of_replay_cmds = 0xFFFFFFFF;
1601 ft_data->replay_for_snapshot = 0xFFFFFFFF;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001602
Tarun Karrad20d71a2013-01-25 15:38:57 -08001603 adreno_regread(device, REG_CP_IB1_BASE, &ft_data->ib1);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001604
Tarun Karrad20d71a2013-01-25 15:38:57 -08001605 kgsl_sharedmem_readl(&device->memstore, &ft_data->context_id,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001606 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1607 current_context));
1608
1609 kgsl_sharedmem_readl(&device->memstore,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001610 &ft_data->global_eop,
Tarun Karradeeecc02013-01-21 23:42:17 -08001611 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1612 eoptimestamp));
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001613
Tarun Karra782734f2013-03-27 20:21:02 -07001614 /* Ensure context id and global eop ts read complete */
1615 rmb();
1616
Tarun Karrad20d71a2013-01-25 15:38:57 -08001617 ft_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1618 if (!ft_data->rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001619 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1620 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001621 return;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001622 }
1623
Tarun Karrad20d71a2013-01-25 15:38:57 -08001624 ft_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1625 if (!ft_data->bad_rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001626 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1627 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001628 return;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001629 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001630
Tarun Karrad20d71a2013-01-25 15:38:57 -08001631 ft_data->good_rb_buffer = vmalloc(rb->buffer_desc.size);
1632 if (!ft_data->good_rb_buffer) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001633 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1634 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001635 return;
Tarun Karradeeecc02013-01-21 23:42:17 -08001636 }
Tarun Karra3164fb02013-02-05 15:38:51 -08001637
Tarun Karra59e79542013-04-10 10:25:25 -07001638 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001639
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001640 /* find the start of bad command sequence in rb */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001641 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001642 /* Look for the command stream that is right after the global eop */
1643
1644 if (!context) {
1645 /*
Tarun Karrad20d71a2013-01-25 15:38:57 -08001646 * If there is no context then fault tolerance does not need to
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001647 * replay anything, just reset GPU and thats it
1648 */
Tarun Karra59e79542013-04-10 10:25:25 -07001649 return;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001650 }
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001651
Tarun Karra59e79542013-04-10 10:25:25 -07001652 ft_data->ft_policy = adreno_dev->ft_policy;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001653
Tarun Karra3164fb02013-02-05 15:38:51 -08001654 if (!adreno_dev->ft_policy)
1655 adreno_dev->ft_policy = KGSL_FT_DEFAULT_POLICY;
1656
Tarun Karra59e79542013-04-10 10:25:25 -07001657 ret = _find_cmd_seq_after_eop_ts(rb, &rb_rptr,
1658 ft_data->global_eop + 1, false);
1659 if (ret) {
1660 ft_data->ft_policy |= KGSL_FT_TEMP_DISABLE;
1661 return;
1662 } else
1663 ft_data->ft_policy &= ~KGSL_FT_TEMP_DISABLE;
Tarun Karra3164fb02013-02-05 15:38:51 -08001664
Tarun Karra59e79542013-04-10 10:25:25 -07001665 ft_data->start_of_replay_cmds = rb_rptr;
Tarun Karra3164fb02013-02-05 15:38:51 -08001666
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001667 adreno_context = context->devctxt;
1668 if (adreno_context->flags & CTXT_FLAGS_PREAMBLE) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001669 if (ft_data->ib1) {
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001670 ret = _find_hanging_ib_sequence(rb,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001671 &rb_rptr, ft_data->ib1);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001672 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001673 KGSL_FT_ERR(device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001674 "Start not found for replay IB sequence\n");
Tarun Karra59e79542013-04-10 10:25:25 -07001675 return;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001676 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001677 ft_data->start_of_replay_cmds = rb_rptr;
1678 ft_data->replay_for_snapshot = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001679 }
1680 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001681}
1682
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001683static int
Tarun Karra696f89e2013-01-27 21:31:40 -08001684_adreno_check_long_ib(struct kgsl_device *device)
1685{
1686 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1687 unsigned int curr_global_ts = 0;
1688
1689 /* check if the global ts is still the same */
1690 kgsl_sharedmem_readl(&device->memstore,
1691 &curr_global_ts,
1692 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1693 eoptimestamp));
Tarun Karra782734f2013-03-27 20:21:02 -07001694 /* Ensure above read is finished before long ib check */
1695 rmb();
Tarun Karra696f89e2013-01-27 21:31:40 -08001696
1697 /* Mark long ib as handled */
1698 adreno_dev->long_ib = 0;
1699
1700 if (curr_global_ts == adreno_dev->long_ib_ts) {
1701 KGSL_FT_ERR(device,
1702 "IB ran too long, invalidate ctxt\n");
1703 return 1;
1704 } else {
1705 /* Do nothing GPU has gone ahead */
1706 KGSL_FT_INFO(device, "false long ib detection return\n");
1707 return 0;
1708 }
1709}
1710
1711static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001712_adreno_ft_restart_device(struct kgsl_device *device,
Tarun Karra59e79542013-04-10 10:25:25 -07001713 struct kgsl_context *context)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001714{
Tarun Karradeeecc02013-01-21 23:42:17 -08001715
1716 struct adreno_context *adreno_context = context->devctxt;
1717
1718 /* restart device */
1719 if (adreno_stop(device)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001720 KGSL_FT_ERR(device, "Device stop failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001721 return 1;
1722 }
1723
1724 if (adreno_start(device, true)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001725 KGSL_FT_ERR(device, "Device start failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001726 return 1;
1727 }
1728
1729 if (context)
1730 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1731 KGSL_MEMSTORE_GLOBAL);
1732
1733 /* If iommu is used then we need to make sure that the iommu clocks
1734 * are on since there could be commands in pipeline that touch iommu */
1735 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1736 if (kgsl_mmu_enable_clk(&device->mmu,
1737 KGSL_IOMMU_CONTEXT_USER))
1738 return 1;
1739 }
1740
1741 return 0;
1742}
1743
Tarun Karra696f89e2013-01-27 21:31:40 -08001744static inline void
1745_adreno_debug_ft_info(struct kgsl_device *device,
1746 struct adreno_ft_data *ft_data)
1747{
1748
1749 /*
1750 * Dumping rb is a very useful tool to debug FT.
1751 * It will tell us if we are extracting the rb correctly
1752 * NOP'ing the right IB, skipping the EOF correctly etc.
1753 */
1754 if (device->ft_log >= 7) {
1755
1756 /* Print fault tolerance data here */
1757 KGSL_FT_INFO(device, "Temp RB buffer size 0x%X\n",
1758 ft_data->rb_size);
1759 adreno_dump_rb(device, ft_data->rb_buffer,
1760 ft_data->rb_size<<2, 0, ft_data->rb_size);
1761
1762 KGSL_FT_INFO(device, "Bad RB buffer size 0x%X\n",
1763 ft_data->bad_rb_size);
1764 adreno_dump_rb(device, ft_data->bad_rb_buffer,
1765 ft_data->bad_rb_size<<2, 0, ft_data->bad_rb_size);
1766
1767 KGSL_FT_INFO(device, "Good RB buffer size 0x%X\n",
1768 ft_data->good_rb_size);
1769 adreno_dump_rb(device, ft_data->good_rb_buffer,
1770 ft_data->good_rb_size<<2, 0, ft_data->good_rb_size);
1771
1772 }
1773}
1774
Tarun Karradeeecc02013-01-21 23:42:17 -08001775static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001776_adreno_ft_resubmit_rb(struct kgsl_device *device,
Tarun Karradeeecc02013-01-21 23:42:17 -08001777 struct adreno_ringbuffer *rb,
1778 struct kgsl_context *context,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001779 struct adreno_ft_data *ft_data,
Tarun Karradeeecc02013-01-21 23:42:17 -08001780 unsigned int *buff, unsigned int size)
1781{
1782 unsigned int ret = 0;
Tarun Karra59e79542013-04-10 10:25:25 -07001783 unsigned int retry_num = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001784
Tarun Karra696f89e2013-01-27 21:31:40 -08001785 _adreno_debug_ft_info(device, ft_data);
1786
Tarun Karra59e79542013-04-10 10:25:25 -07001787 do {
1788 ret = _adreno_ft_restart_device(device, context);
1789 if (ret == 0)
1790 break;
1791 /*
1792 * If device restart fails sleep for 20ms before
1793 * attempting restart. This allows GPU HW to settle
1794 * and improve the chances of next restart to be
1795 * successful.
1796 */
1797 msleep(20);
1798 KGSL_FT_ERR(device, "Retry device restart %d\n", retry_num);
1799 retry_num++;
1800 } while (retry_num < 4);
1801
1802 if (ret) {
1803 KGSL_FT_ERR(device, "Device restart failed\n");
1804 BUG_ON(1);
1805 goto done;
1806 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001807
1808 if (size) {
1809
1810 /* submit commands and wait for them to pass */
1811 adreno_ringbuffer_restore(rb, buff, size);
1812
1813 ret = adreno_idle(device);
1814 }
1815
Tarun Karra59e79542013-04-10 10:25:25 -07001816done:
Tarun Karradeeecc02013-01-21 23:42:17 -08001817 return ret;
1818}
1819
Tarun Karradeeecc02013-01-21 23:42:17 -08001820static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001821_adreno_ft(struct kgsl_device *device,
1822 struct adreno_ft_data *ft_data)
Tarun Karradeeecc02013-01-21 23:42:17 -08001823{
1824 int ret = 0, i;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001825 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1826 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1827 struct kgsl_context *context;
1828 struct adreno_context *adreno_context = NULL;
1829 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
Tarun Karra59e79542013-04-10 10:25:25 -07001830 unsigned int long_ib = 0;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001831
Jordan Crouse67db48d2013-05-28 17:04:17 -06001832 context = kgsl_context_get(device, ft_data->context_id);
1833
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001834 if (context == NULL) {
Tarun Karra59e79542013-04-10 10:25:25 -07001835 KGSL_FT_ERR(device, "Last context unknown id:%d\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001836 ft_data->context_id);
Tarun Karra59e79542013-04-10 10:25:25 -07001837 goto play_good_cmds;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001838 } else {
1839 adreno_context = context->devctxt;
1840 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001841 /*
1842 * set the invalid ts flag to 0 for this context since we have
1843 * detected a hang for it
1844 */
1845 context->wait_on_invalid_ts = false;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001846
Tarun Karra59e79542013-04-10 10:25:25 -07001847 if (!(adreno_context->flags & CTXT_FLAGS_PER_CONTEXT_TS)) {
1848 ft_data->status = 1;
1849 KGSL_FT_ERR(device, "Fault tolerance not supported\n");
1850 goto play_good_cmds;
1851 }
1852
Tarun Karra83297222013-02-05 19:45:49 -08001853 /*
1854 * This flag will be set by userspace for contexts
1855 * that do not want to be fault tolerant (ex: OPENCL)
1856 */
1857 if (adreno_context->flags & CTXT_FLAGS_NO_FAULT_TOLERANCE) {
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001858 ft_data->status = 1;
Tarun Karra83297222013-02-05 19:45:49 -08001859 KGSL_FT_ERR(device,
1860 "No FT set for this context play good cmds\n");
1861 goto play_good_cmds;
1862 }
1863
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001864 }
1865
Tarun Karra59e79542013-04-10 10:25:25 -07001866 /* Check if we detected a long running IB,
1867 * if true do not attempt replay of bad cmds */
1868 if (adreno_dev->long_ib) {
1869 long_ib = _adreno_check_long_ib(device);
1870 if (!long_ib) {
1871 adreno_context->flags &= ~CTXT_FLAGS_GPU_HANG;
1872 return 0;
1873 }
1874 }
1875
Tarun Karra3164fb02013-02-05 15:38:51 -08001876 /*
1877 * Extract valid contents from rb which can still be executed after
1878 * hang
1879 */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001880 adreno_ringbuffer_extract(rb, ft_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001881
Tarun Karra59e79542013-04-10 10:25:25 -07001882 /* If long IB detected do not attempt replay of bad cmds */
1883 if (long_ib) {
1884 ft_data->status = 1;
1885 _adreno_debug_ft_info(device, ft_data);
1886 goto play_good_cmds;
1887 }
1888
1889 if ((ft_data->ft_policy & KGSL_FT_DISABLE) ||
1890 (ft_data->ft_policy & KGSL_FT_TEMP_DISABLE)) {
1891 KGSL_FT_ERR(device, "NO FT policy play only good cmds\n");
1892 ft_data->status = 1;
1893 goto play_good_cmds;
Tarun Karra696f89e2013-01-27 21:31:40 -08001894 }
1895
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001896 /* Do not try the reply if hang is due to a pagefault */
1897 if (adreno_context->pagefault) {
1898 if ((ft_data->context_id == adreno_context->id) &&
1899 (ft_data->global_eop == adreno_context->pagefault_ts)) {
1900 ft_data->ft_policy &= ~KGSL_FT_REPLAY;
1901 KGSL_FT_ERR(device, "MMU fault skipping replay\n");
1902 }
1903
1904 adreno_context->pagefault = 0;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001905 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001906
Tarun Karra3164fb02013-02-05 15:38:51 -08001907 if (ft_data->ft_policy & KGSL_FT_REPLAY) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001908 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1909 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001910
Tarun Karra3164fb02013-02-05 15:38:51 -08001911 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001912 KGSL_FT_ERR(device, "Replay status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001913 ft_data->status = 1;
1914 } else
Tarun Karradeeecc02013-01-21 23:42:17 -08001915 goto play_good_cmds;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001916 }
1917
Tarun Karra3164fb02013-02-05 15:38:51 -08001918 if (ft_data->ft_policy & KGSL_FT_SKIPIB) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001919 for (i = 0; i < ft_data->bad_rb_size; i++) {
1920 if ((ft_data->bad_rb_buffer[i] ==
1921 CP_HDR_INDIRECT_BUFFER_PFD) &&
1922 (ft_data->bad_rb_buffer[i+1] == ft_data->ib1)) {
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001923
Tarun Karrad20d71a2013-01-25 15:38:57 -08001924 ft_data->bad_rb_buffer[i] = cp_nop_packet(2);
1925 ft_data->bad_rb_buffer[i+1] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001926 KGSL_NOP_IB_IDENTIFIER;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001927 ft_data->bad_rb_buffer[i+2] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001928 KGSL_NOP_IB_IDENTIFIER;
1929 break;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001930 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001931 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001932
Tarun Karrad20d71a2013-01-25 15:38:57 -08001933 if ((i == (ft_data->bad_rb_size)) || (!ft_data->ib1)) {
1934 KGSL_FT_ERR(device, "Bad IB to NOP not found\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001935 ft_data->status = 1;
Tarun Karradeeecc02013-01-21 23:42:17 -08001936 goto play_good_cmds;
1937 }
1938
Tarun Karrad20d71a2013-01-25 15:38:57 -08001939 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1940 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001941
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001942 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001943 KGSL_FT_ERR(device, "NOP faulty IB status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001944 ft_data->status = 1;
1945 } else {
1946 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001947 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001948 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001949 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001950
Tarun Karra3164fb02013-02-05 15:38:51 -08001951 if (ft_data->ft_policy & KGSL_FT_SKIPFRAME) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001952 for (i = 0; i < ft_data->bad_rb_size; i++) {
1953 if (ft_data->bad_rb_buffer[i] ==
1954 KGSL_END_OF_FRAME_IDENTIFIER) {
1955 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001956 break;
1957 }
1958 }
1959
1960 /* EOF not found in RB, discard till EOF in
1961 next IB submission */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001962 if (i == ft_data->bad_rb_size) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001963 adreno_context->flags |= CTXT_FLAGS_SKIP_EOF;
Tarun Karra3164fb02013-02-05 15:38:51 -08001964 KGSL_FT_INFO(device,
1965 "EOF not found in RB, skip next issueib till EOF\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08001966 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001967 }
1968
Tarun Karrad20d71a2013-01-25 15:38:57 -08001969 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1970 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001971
1972 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001973 KGSL_FT_ERR(device, "Skip EOF status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001974 ft_data->status = 1;
1975 } else {
1976 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001977 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001978 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001979 }
1980
1981play_good_cmds:
1982
Tarun Karra3164fb02013-02-05 15:38:51 -08001983 if (ft_data->status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001984 KGSL_FT_ERR(device, "Bad context commands failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001985 else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001986 KGSL_FT_INFO(device, "Bad context commands success\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001987
1988 if (adreno_context) {
1989 adreno_context->flags = (adreno_context->flags &
Tarun Karrad20d71a2013-01-25 15:38:57 -08001990 ~CTXT_FLAGS_GPU_HANG) | CTXT_FLAGS_GPU_HANG_FT;
Tarun Karradeeecc02013-01-21 23:42:17 -08001991 }
1992 adreno_dev->drawctxt_active = last_active_ctx;
1993 }
1994
Tarun Karrad20d71a2013-01-25 15:38:57 -08001995 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1996 ft_data->good_rb_buffer, ft_data->good_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001997
1998 if (ret) {
1999 /* If we fail here we can try to invalidate another
2000 * context and try fault tolerance again */
2001 ret = -EAGAIN;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002002 KGSL_FT_ERR(device, "Playing good commands unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08002003 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002004 } else
2005 KGSL_FT_INFO(device, "Playing good commands successful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08002006
2007 /* ringbuffer now has data from the last valid context id,
2008 * so restore the active_ctx to the last valid context */
Tarun Karrad20d71a2013-01-25 15:38:57 -08002009 if (ft_data->last_valid_ctx_id) {
Jordan Crouse67db48d2013-05-28 17:04:17 -06002010 struct kgsl_context *last_ctx = kgsl_context_get(device,
2011 ft_data->last_valid_ctx_id);
2012
Tarun Karradeeecc02013-01-21 23:42:17 -08002013 if (last_ctx)
2014 adreno_dev->drawctxt_active = last_ctx->devctxt;
Jordan Crouse67db48d2013-05-28 17:04:17 -06002015
2016 kgsl_context_put(last_ctx);
Tarun Karradeeecc02013-01-21 23:42:17 -08002017 }
2018
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002019done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06002020 /* Turn off iommu clocks */
2021 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
2022 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Jordan Crouse67db48d2013-05-28 17:04:17 -06002023
2024 kgsl_context_put(context);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002025 return ret;
2026}
2027
2028static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002029adreno_ft(struct kgsl_device *device,
2030 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002031{
2032 int ret = 0;
2033 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2034 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2035 unsigned int timestamp;
2036
Tarun Karrad20d71a2013-01-25 15:38:57 -08002037 KGSL_FT_INFO(device,
2038 "Start Parameters: IB1: 0x%X, "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002039 "Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08002040 ft_data->ib1, ft_data->context_id, ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002041
2042 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
Tarun Karrad20d71a2013-01-25 15:38:57 -08002043 KGSL_FT_INFO(device, "Last issued global timestamp: %x\n", timestamp);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002044
2045 /* We may need to replay commands multiple times based on whether
2046 * multiple contexts hang the GPU */
2047 while (true) {
Tarun Karradeeecc02013-01-21 23:42:17 -08002048
Tarun Karrad20d71a2013-01-25 15:38:57 -08002049 ret = _adreno_ft(device, ft_data);
2050
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002051 if (-EAGAIN == ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002052 /* setup new fault tolerance parameters and retry, this
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002053 * means more than 1 contexts are causing hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08002054 adreno_destroy_ft_data(ft_data);
Tarun Karra59e79542013-04-10 10:25:25 -07002055 adreno_setup_ft_data(device, ft_data);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002056 KGSL_FT_INFO(device,
2057 "Retry. Parameters: "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002058 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08002059 ft_data->ib1, ft_data->context_id,
2060 ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002061 } else {
2062 break;
2063 }
2064 }
2065
2066 if (ret)
2067 goto done;
2068
Tarun Karrad20d71a2013-01-25 15:38:57 -08002069 /* Restore correct states after fault tolerance */
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002070 if (adreno_dev->drawctxt_active)
2071 device->mmu.hwpagetable =
2072 adreno_dev->drawctxt_active->pagetable;
2073 else
2074 device->mmu.hwpagetable = device->mmu.defaultpagetable;
2075 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
2076 kgsl_sharedmem_writel(&device->memstore,
2077 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2078 eoptimestamp),
2079 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07002080
2081 /* switch to NULL ctxt */
2082 if (adreno_dev->drawctxt_active != NULL)
2083 adreno_drawctxt_switch(adreno_dev, NULL, 0);
2084
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002085done:
2086 adreno_set_max_ts_for_bad_ctxs(device);
2087 adreno_mark_context_status(device, ret);
Tarun Karra3164fb02013-02-05 15:38:51 -08002088 KGSL_FT_ERR(device, "policy 0x%X status 0x%x\n",
2089 ft_data->ft_policy, ret);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002090 return ret;
2091}
2092
2093int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002094adreno_dump_and_exec_ft(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002095{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002096 int result = -ETIMEDOUT;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002097 struct adreno_ft_data ft_data;
Tarun Karra696f89e2013-01-27 21:31:40 -08002098 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karrad2326b82013-01-28 19:09:41 -08002099 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2100 unsigned int curr_pwrlevel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002101
2102 if (device->state == KGSL_STATE_HUNG)
2103 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002104 if (device->state == KGSL_STATE_DUMP_AND_FT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002105 mutex_unlock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002106 wait_for_completion(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002107 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07002108 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002109 result = 0;
2110 } else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002111 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_FT);
2112 INIT_COMPLETION(device->ft_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002113 /* Detected a hang */
2114
Tarun Karrad2326b82013-01-28 19:09:41 -08002115 /* Run fault tolerance at max power level */
2116 curr_pwrlevel = pwr->active_pwrlevel;
2117 kgsl_pwrctrl_pwrlevel_change(device, pwr->max_pwrlevel);
2118
Tarun Karrad20d71a2013-01-25 15:38:57 -08002119 /* Get the fault tolerance data as soon as hang is detected */
Tarun Karra59e79542013-04-10 10:25:25 -07002120 adreno_setup_ft_data(device, &ft_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002121
2122 /*
Tarun Karra696f89e2013-01-27 21:31:40 -08002123 * If long ib is detected, do not attempt postmortem or
2124 * snapshot, if GPU is still executing commands
2125 * we will get errors
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002126 */
Tarun Karra696f89e2013-01-27 21:31:40 -08002127 if (!adreno_dev->long_ib) {
2128 /*
2129 * Trigger an automatic dump of the state to
2130 * the console
2131 */
2132 kgsl_postmortem_dump(device, 0);
2133
2134 /*
2135 * Make a GPU snapshot. For now, do it after the
2136 * PM dump so we can at least be sure the PM dump
2137 * will work as it always has
2138 */
2139 kgsl_device_snapshot(device, 1);
2140 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002141
Tarun Karra59e79542013-04-10 10:25:25 -07002142 result = adreno_ft(device, &ft_data);
2143 adreno_destroy_ft_data(&ft_data);
Tarun Karra696f89e2013-01-27 21:31:40 -08002144
Tarun Karrad2326b82013-01-28 19:09:41 -08002145 /* restore power level */
2146 kgsl_pwrctrl_pwrlevel_change(device, curr_pwrlevel);
2147
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002148 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002149 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002150 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002151 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002152 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
2153 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08002154 complete_all(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002155 }
2156done:
2157 return result;
2158}
Tarun Karrad20d71a2013-01-25 15:38:57 -08002159EXPORT_SYMBOL(adreno_dump_and_exec_ft);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002160
2161static int adreno_getproperty(struct kgsl_device *device,
2162 enum kgsl_property_type type,
2163 void *value,
2164 unsigned int sizebytes)
2165{
2166 int status = -EINVAL;
2167 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2168
2169 switch (type) {
2170 case KGSL_PROP_DEVICE_INFO:
2171 {
2172 struct kgsl_devinfo devinfo;
2173
2174 if (sizebytes != sizeof(devinfo)) {
2175 status = -EINVAL;
2176 break;
2177 }
2178
2179 memset(&devinfo, 0, sizeof(devinfo));
2180 devinfo.device_id = device->id+1;
2181 devinfo.chip_id = adreno_dev->chip_id;
2182 devinfo.mmu_enabled = kgsl_mmu_enabled();
2183 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06002184 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
2185 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186
2187 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
2188 0) {
2189 status = -EFAULT;
2190 break;
2191 }
2192 status = 0;
2193 }
2194 break;
2195 case KGSL_PROP_DEVICE_SHADOW:
2196 {
2197 struct kgsl_shadowprop shadowprop;
2198
2199 if (sizebytes != sizeof(shadowprop)) {
2200 status = -EINVAL;
2201 break;
2202 }
2203 memset(&shadowprop, 0, sizeof(shadowprop));
2204 if (device->memstore.hostptr) {
2205 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2206 * anything to mmap().
2207 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07002208 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002209 shadowprop.size = device->memstore.size;
2210 /* GSL needs this to be set, even if it
2211 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002212 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2213 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002214 }
2215 if (copy_to_user(value, &shadowprop,
2216 sizeof(shadowprop))) {
2217 status = -EFAULT;
2218 break;
2219 }
2220 status = 0;
2221 }
2222 break;
2223 case KGSL_PROP_MMU_ENABLE:
2224 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002225 int mmu_prop = kgsl_mmu_enabled();
2226
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002227 if (sizebytes != sizeof(int)) {
2228 status = -EINVAL;
2229 break;
2230 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002231 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002232 status = -EFAULT;
2233 break;
2234 }
2235 status = 0;
2236 }
2237 break;
2238 case KGSL_PROP_INTERRUPT_WAITS:
2239 {
2240 int int_waits = 1;
2241 if (sizebytes != sizeof(int)) {
2242 status = -EINVAL;
2243 break;
2244 }
2245 if (copy_to_user(value, &int_waits, sizeof(int))) {
2246 status = -EFAULT;
2247 break;
2248 }
2249 status = 0;
2250 }
2251 break;
2252 default:
2253 status = -EINVAL;
2254 }
2255
2256 return status;
2257}
2258
Jordan Crousef7370f82012-04-18 09:31:07 -06002259static int adreno_setproperty(struct kgsl_device *device,
2260 enum kgsl_property_type type,
2261 void *value,
2262 unsigned int sizebytes)
2263{
2264 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08002265 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06002266
2267 switch (type) {
2268 case KGSL_PROP_PWRCTRL: {
2269 unsigned int enable;
2270 struct kgsl_device_platform_data *pdata =
2271 kgsl_device_get_drvdata(device);
2272
2273 if (sizebytes != sizeof(enable))
2274 break;
2275
2276 if (copy_from_user(&enable, (void __user *) value,
2277 sizeof(enable))) {
2278 status = -EFAULT;
2279 break;
2280 }
2281
2282 if (enable) {
2283 if (pdata->nap_allowed)
2284 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002285 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002286 kgsl_pwrscale_enable(device);
2287 } else {
2288 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002289 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002290 kgsl_pwrscale_disable(device);
2291 }
2292
2293 status = 0;
2294 }
2295 break;
2296 default:
2297 break;
2298 }
2299
2300 return status;
2301}
2302
Jordan Crousea29a2e02012-08-14 09:09:23 -06002303static int adreno_ringbuffer_drain(struct kgsl_device *device,
2304 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002305{
2306 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2307 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002308 unsigned long wait;
2309 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2310
2311 if (!(rb->flags & KGSL_FLAGS_STARTED))
2312 return 0;
2313
2314 /*
2315 * The first time into the loop, wait for 100 msecs and kick wptr again
2316 * to ensure that the hardware has updated correctly. After that, kick
2317 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2318 * expires
2319 */
2320
2321 wait = jiffies + msecs_to_jiffies(100);
2322
Jordan Crousea29a2e02012-08-14 09:09:23 -06002323 do {
2324 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002325 /* Check to see if the core is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002326 if (adreno_ft_detect(device, regs))
Jordan Crousea29a2e02012-08-14 09:09:23 -06002327 return -ETIMEDOUT;
2328
2329 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2330 }
2331 GSL_RB_GET_READPTR(rb, &rb->rptr);
2332
2333 if (time_after(jiffies, timeout)) {
2334 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2335 rb->rptr, rb->wptr);
2336 return -ETIMEDOUT;
2337 }
2338 } while (rb->rptr != rb->wptr);
2339
2340 return 0;
2341}
2342
2343/* Caller must hold the device mutex. */
2344int adreno_idle(struct kgsl_device *device)
2345{
2346 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002347 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302348 unsigned long wait_time;
2349 unsigned long wait_time_part;
Tarun Karra696f89e2013-01-27 21:31:40 -08002350 unsigned int prev_reg_val[ft_detect_regs_count];
Tarun Karra3335f142012-06-19 14:11:48 -07002351
2352 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002353
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002354 kgsl_cffdump_regpoll(device->id,
2355 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002356 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002357
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002358retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002359 /* First, wait for the ringbuffer to drain */
2360 if (adreno_ringbuffer_drain(device, prev_reg_val))
2361 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002362
2363 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002364 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002365 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2366
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002367 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002368 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2369 &rbbm_status);
2370 if (adreno_is_a2xx(adreno_dev)) {
2371 if (rbbm_status == 0x110)
2372 return 0;
2373 } else {
2374 if (!(rbbm_status & 0x80000000))
2375 return 0;
2376 }
Tarun Karra3335f142012-06-19 14:11:48 -07002377
2378 /* Dont wait for timeout, detect hang faster.
2379 */
2380 if (time_after(jiffies, wait_time_part)) {
2381 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002382 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra696f89e2013-01-27 21:31:40 -08002383 if ((adreno_ft_detect(device, prev_reg_val)))
Tarun Karra3335f142012-06-19 14:11:48 -07002384 goto err;
2385 }
2386
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002387 }
2388
2389err:
2390 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08002391 if (KGSL_STATE_DUMP_AND_FT != device->state &&
2392 !adreno_dump_and_exec_ft(device)) {
2393 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002394 goto retry;
2395 }
2396 return -ETIMEDOUT;
2397}
2398
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002399/**
2400 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2401 * rbbm_status register
2402 * @device - Pointer to the GPU device whose idle status is to be
2403 * checked
2404 * @returns - Returns whether the core is idle (based on rbbm_status)
2405 * false if the core is active, true if the core is idle
2406 */
2407static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2408{
2409 unsigned int reg_rbbm_status;
2410 bool status = false;
2411 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2412
2413 /* Is the core idle? */
2414 adreno_regread(device,
2415 adreno_dev->gpudev->reg_rbbm_status,
2416 &reg_rbbm_status);
2417
2418 if (adreno_is_a2xx(adreno_dev)) {
2419 if (reg_rbbm_status == 0x110)
2420 status = true;
2421 } else {
2422 if (!(reg_rbbm_status & 0x80000000))
2423 status = true;
2424 }
2425 return status;
2426}
2427
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002428static unsigned int adreno_isidle(struct kgsl_device *device)
2429{
2430 int status = false;
2431 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2432 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002433
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002434 WARN_ON(device->state == KGSL_STATE_INIT);
2435 /* If the device isn't active, don't force it on. */
2436 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002437 /* Is the ring buffer is empty? */
2438 GSL_RB_GET_READPTR(rb, &rb->rptr);
2439 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
Jordan Crouseab601992013-03-05 11:18:20 -07002440 /*
2441 * Are there interrupts pending? If so then pretend we
2442 * are not idle - this avoids the possiblity that we go
2443 * to a lower power state without handling interrupts
2444 * first.
2445 */
2446
2447 if (!adreno_dev->gpudev->irq_pending(adreno_dev)) {
2448 /* Is the core idle? */
2449 status = is_adreno_rbbm_status_idle(device);
2450 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002451 }
2452 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002453 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002454 }
2455 return status;
2456}
2457
2458/* Caller must hold the device mutex. */
2459static int adreno_suspend_context(struct kgsl_device *device)
2460{
2461 int status = 0;
2462 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2463
2464 /* switch to NULL ctxt */
2465 if (adreno_dev->drawctxt_active != NULL) {
2466 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002467 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002468 }
2469
2470 return status;
2471}
2472
Jordan Crouse233b2092012-04-18 09:31:09 -06002473/* Find a memory structure attached to an adreno context */
2474
2475struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2476 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2477{
2478 struct kgsl_context *context;
2479 struct adreno_context *adreno_context = NULL;
2480 int next = 0;
Jordan Crouse67db48d2013-05-28 17:04:17 -06002481 struct kgsl_memdesc *desc = NULL;
Jordan Crouse233b2092012-04-18 09:31:09 -06002482
Jordan Crouse67db48d2013-05-28 17:04:17 -06002483 rcu_read_lock();
Jordan Crouse233b2092012-04-18 09:31:09 -06002484 while (1) {
2485 context = idr_get_next(&device->context_idr, &next);
2486 if (context == NULL)
2487 break;
2488
2489 adreno_context = (struct adreno_context *)context->devctxt;
2490
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002491 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2492 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002493 desc = &adreno_context->gpustate;
2494 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
Jordan Crouse67db48d2013-05-28 17:04:17 -06002495 break;
Jordan Crouse233b2092012-04-18 09:31:09 -06002496
2497 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2498 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
Jordan Crouse67db48d2013-05-28 17:04:17 -06002499 break;
Jordan Crouse233b2092012-04-18 09:31:09 -06002500 }
2501 next = next + 1;
Jordan Crouse67db48d2013-05-28 17:04:17 -06002502 desc = NULL;
Jordan Crouse233b2092012-04-18 09:31:09 -06002503 }
Jordan Crouse67db48d2013-05-28 17:04:17 -06002504 rcu_read_unlock();
2505 return desc;
Jordan Crouse233b2092012-04-18 09:31:09 -06002506}
2507
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002508struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002509 unsigned int pt_base,
2510 unsigned int gpuaddr,
2511 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002512{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002513 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002514 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2515 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2516
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002517 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2518 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002519
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002520 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2521 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002522
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002523 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2524 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002525
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002526 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2527 size))
2528 return &device->mmu.setstate_memory;
2529
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002530 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002531
2532 if (entry)
2533 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002534
Jordan Crouse233b2092012-04-18 09:31:09 -06002535 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002536}
2537
2538uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2539 unsigned int gpuaddr, unsigned int size)
2540{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002541 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002542
2543 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2544
2545 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002546}
2547
2548void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2549 unsigned int *value)
2550{
2551 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002552 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2553 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002554
2555 if (!in_interrupt())
2556 kgsl_pre_hwaccess(device);
2557
2558 /*ensure this read finishes before the next one.
2559 * i.e. act like normal readl() */
2560 *value = __raw_readl(reg);
2561 rmb();
2562}
2563
2564void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2565 unsigned int value)
2566{
2567 unsigned int *reg;
2568
Jordan Crouse7501d452012-04-19 08:58:44 -06002569 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002570
2571 if (!in_interrupt())
2572 kgsl_pre_hwaccess(device);
2573
Jordan Crousef7b81e82013-05-28 17:03:00 -06002574 trace_kgsl_regwrite(device, offsetwords, value);
2575
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002576 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002577 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002578
2579 /*ensure previous writes post before this one,
2580 * i.e. act like normal writel() */
2581 wmb();
2582 __raw_writel(value, reg);
2583}
2584
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002585static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2586{
2587 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002588 if (k_ctxt != NULL) {
2589 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002590 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2591 context_id = KGSL_CONTEXT_INVALID;
2592 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2593 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002594 }
2595
2596 return context_id;
2597}
2598
Carter Cooper740f6742013-01-03 16:19:23 -07002599static unsigned int adreno_check_hw_ts(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002600 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002601{
Carter Cooper740f6742013-01-03 16:19:23 -07002602 int status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002603 unsigned int ref_ts, enableflag;
Carter Cooper740f6742013-01-03 16:19:23 -07002604 unsigned int context_id = _get_context_id(context);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002605
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002606 /*
2607 * If the context ID is invalid, we are in a race with
2608 * the context being destroyed by userspace so bail.
2609 */
2610 if (context_id == KGSL_CONTEXT_INVALID) {
2611 KGSL_DRV_WARN(device, "context was detached");
Carter Cooper740f6742013-01-03 16:19:23 -07002612 return -EINVAL;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002613 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002614
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002615 status = kgsl_check_timestamp(device, context, timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002616 if (status)
2617 return status;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002618
Carter Cooper740f6742013-01-03 16:19:23 -07002619 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2620 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2621 /*
2622 * Barrier is needed here to make sure the read from memstore
2623 * has posted
2624 */
2625
2626 mb();
2627
2628 if (enableflag) {
2629 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002630 KGSL_MEMSTORE_OFFSET(context_id,
2631 ref_wait_ts));
Carter Cooper740f6742013-01-03 16:19:23 -07002632
2633 /* Make sure the memstore read has posted */
2634 mb();
2635 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
2636 kgsl_sharedmem_writel(&device->memstore,
2637 KGSL_MEMSTORE_OFFSET(context_id,
2638 ref_wait_ts), timestamp);
2639 /* Make sure the memstore write is posted */
2640 wmb();
2641 }
2642 } else {
2643 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002644 KGSL_MEMSTORE_OFFSET(context_id,
2645 ref_wait_ts), timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002646 enableflag = 1;
2647 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002648 KGSL_MEMSTORE_OFFSET(context_id,
2649 ts_cmp_enable), enableflag);
Carter Cooper740f6742013-01-03 16:19:23 -07002650 /* Make sure the memstore write gets posted */
2651 wmb();
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002652
Carter Cooper740f6742013-01-03 16:19:23 -07002653 /*
2654 * submit a dummy packet so that even if all
2655 * commands upto timestamp get executed we will still
2656 * get an interrupt
2657 */
2658
2659 if (context && device->state != KGSL_STATE_SLUMBER)
2660 adreno_ringbuffer_issuecmds(device, context->devctxt,
2661 KGSL_CMD_FLAGS_NONE, NULL, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002662 }
Carter Cooper740f6742013-01-03 16:19:23 -07002663
2664 return 0;
2665}
2666
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002667/* Return 1 if the event timestmp has already passed, 0 if it was marked */
2668static int adreno_next_event(struct kgsl_device *device,
Carter Cooper740f6742013-01-03 16:19:23 -07002669 struct kgsl_event *event)
2670{
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002671 return adreno_check_hw_ts(device, event->context, event->timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002672}
2673
2674static int adreno_check_interrupt_timestamp(struct kgsl_device *device,
2675 struct kgsl_context *context, unsigned int timestamp)
2676{
2677 int status;
2678
2679 mutex_lock(&device->mutex);
2680 status = adreno_check_hw_ts(device, context, timestamp);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002681 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002682
2683 return status;
2684}
2685
2686/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002687 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002688 placing a process in wait q. For conditional interrupts we expect the
2689 process to already be in its wait q when its exit condition checking
2690 function is called.
2691*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002692#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002693({ \
2694 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002695 if (io) \
2696 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2697 else \
2698 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002699 __ret; \
2700})
2701
Tarun Karra3335f142012-06-19 14:11:48 -07002702
2703
Tarun Karra696f89e2013-01-27 21:31:40 -08002704unsigned int adreno_ft_detect(struct kgsl_device *device,
Tarun Karra3335f142012-06-19 14:11:48 -07002705 unsigned int *prev_reg_val)
2706{
2707 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karra696f89e2013-01-27 21:31:40 -08002708 unsigned int curr_reg_val[ft_detect_regs_count];
2709 unsigned int fast_hang_detected = 1;
2710 unsigned int long_ib_detected = 1;
Tarun Karra3335f142012-06-19 14:11:48 -07002711 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002712 static unsigned long next_hang_detect_time;
Tarun Karra696f89e2013-01-27 21:31:40 -08002713 static unsigned int prev_global_ts;
2714 unsigned int curr_global_ts = 0;
2715 unsigned int curr_context_id = 0;
2716 static struct adreno_context *curr_context;
2717 static struct kgsl_context *context;
Tarun Karra3335f142012-06-19 14:11:48 -07002718
2719 if (!adreno_dev->fast_hang_detect)
Tarun Karra696f89e2013-01-27 21:31:40 -08002720 fast_hang_detected = 0;
2721
2722 if (!adreno_dev->long_ib_detect)
2723 long_ib_detected = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002724
Jordan Crousecca61142012-11-20 10:54:24 -07002725 if (is_adreno_rbbm_status_idle(device)) {
2726
2727 /*
2728 * On A20X if the RPTR != WPTR and the device is idle, then
2729 * the last write to WPTR probably failed to latch so write it
2730 * again
2731 */
2732
2733 if (adreno_is_a2xx(adreno_dev)) {
2734 unsigned int rptr;
2735 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2736 if (rptr != adreno_dev->ringbuffer.wptr)
2737 adreno_regwrite(device, REG_CP_RB_WPTR,
2738 adreno_dev->ringbuffer.wptr);
2739 }
2740
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002741 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002742 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002743
Tarun Karra4648db92013-02-01 17:03:40 -08002744 /*
2745 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2746 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2747 * from the last time do nothing.
2748 */
2749 if ((next_hang_detect_time) &&
2750 (time_before(jiffies, next_hang_detect_time)))
2751 return 0;
2752 else
2753 next_hang_detect_time = (jiffies +
2754 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2755
Tarun Karra696f89e2013-01-27 21:31:40 -08002756 /* Read the current Hang detect reg values here */
2757 for (i = 0; i < ft_detect_regs_count; i++) {
2758 if (ft_detect_regs[i] == 0)
Jordan Crouseb5c80482012-10-03 09:38:41 -06002759 continue;
Tarun Karra696f89e2013-01-27 21:31:40 -08002760 adreno_regread(device, ft_detect_regs[i],
2761 &curr_reg_val[i]);
Tarun Karra3335f142012-06-19 14:11:48 -07002762 }
2763
Tarun Karra696f89e2013-01-27 21:31:40 -08002764 /* Read the current global timestamp here */
2765 kgsl_sharedmem_readl(&device->memstore,
2766 &curr_global_ts,
2767 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2768 eoptimestamp));
2769
2770 mb();
2771
2772 if (curr_global_ts == prev_global_ts) {
2773
2774 /* Get the current context here */
2775 if (context == NULL) {
2776 kgsl_sharedmem_readl(&device->memstore,
2777 &curr_context_id,
2778 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2779 current_context));
2780 context = idr_find(&device->context_idr,
2781 curr_context_id);
2782 if (context != NULL) {
2783 curr_context = context->devctxt;
2784 curr_context->ib_gpu_time_used = 0;
2785 } else {
2786 KGSL_DRV_ERR(device,
2787 "Fault tolerance no context found\n");
2788 }
2789 }
2790
2791 mb();
2792
2793 if (curr_context != NULL) {
2794
2795 curr_context->ib_gpu_time_used += KGSL_TIMEOUT_PART;
2796 KGSL_FT_INFO(device,
2797 "Proc %s used GPU Time %d ms on timestamp 0x%X\n",
2798 curr_context->pid_name, curr_context->ib_gpu_time_used,
2799 curr_global_ts+1);
2800
2801 for (i = 0; i < ft_detect_regs_count; i++) {
2802 if (curr_reg_val[i] != prev_reg_val[i]) {
2803 fast_hang_detected = 0;
2804
2805 /* Check for long IB here */
2806 if ((i >=
2807 LONG_IB_DETECT_REG_INDEX_START)
2808 &&
2809 (i <=
2810 LONG_IB_DETECT_REG_INDEX_END))
2811 long_ib_detected = 0;
2812 }
2813 }
2814
2815 if (fast_hang_detected) {
2816 KGSL_FT_ERR(device,
2817 "Proc %s, ctxt_id %d ts %d triggered fault tolerance"
2818 " on global ts %d\n",
2819 curr_context->pid_name, curr_context->id
2820 , (kgsl_readtimestamp(device, context,
2821 KGSL_TIMESTAMP_RETIRED)+1),
2822 curr_global_ts+1);
2823 return 1;
2824 }
2825
Tarun Karra83297222013-02-05 19:45:49 -08002826 if ((long_ib_detected) &&
2827 (!(curr_context->flags &
2828 CTXT_FLAGS_NO_FAULT_TOLERANCE))) {
Tarun Karra696f89e2013-01-27 21:31:40 -08002829 curr_context->ib_gpu_time_used +=
2830 KGSL_TIMEOUT_PART;
2831 if (curr_context->ib_gpu_time_used >
2832 KGSL_TIMEOUT_LONG_IB_DETECTION) {
2833 if (adreno_dev->long_ib_ts !=
2834 curr_global_ts) {
2835 KGSL_FT_ERR(device,
2836 "Proc %s, ctxt_id %d ts %d"
2837 "used GPU for %d ms long ib "
2838 "detected on global ts %d\n",
2839 curr_context->pid_name,
2840 curr_context->id,
2841 (kgsl_readtimestamp(device,
2842 context,
2843 KGSL_TIMESTAMP_RETIRED)+1),
2844 curr_context->ib_gpu_time_used,
2845 curr_global_ts+1);
2846 adreno_dev->long_ib = 1;
2847 adreno_dev->long_ib_ts =
2848 curr_global_ts;
2849 curr_context->ib_gpu_time_used =
2850 0;
2851 return 1;
2852 }
2853 }
2854 }
2855 } else {
2856 KGSL_FT_ERR(device,
2857 "Last context unknown id:%d\n",
2858 curr_context_id);
2859 }
2860 } else {
2861 /* GPU is moving forward */
2862 prev_global_ts = curr_global_ts;
2863 context = NULL;
2864 curr_context = NULL;
2865 adreno_dev->long_ib = 0;
2866 adreno_dev->long_ib_ts = 0;
2867 }
2868
2869
2870 /* If hangs are not detected copy the current reg values
2871 * to previous values and return no hang */
2872 for (i = 0; i < ft_detect_regs_count; i++)
2873 prev_reg_val[i] = curr_reg_val[i];
2874 return 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002875}
2876
Jordan Crouse92446a62012-11-15 11:00:06 -07002877/**
2878 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2879 * @device - pointer to a KGSL device structure
2880 * @context - pointer to the active KGSL context
2881 * @timestamp - the timestamp that the process was waiting for
2882 *
Tarun Karrad20d71a2013-01-25 15:38:57 -08002883 * Process a possible GPU hang and try fault tolerance from it
2884 * cleanly
Jordan Crouse92446a62012-11-15 11:00:06 -07002885 */
2886static int adreno_handle_hang(struct kgsl_device *device,
2887 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002888{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002889 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002890 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002891 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002892 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002893
Jordan Crouse92446a62012-11-15 11:00:06 -07002894 /* Do one last check to see if we somehow made it through */
2895 if (kgsl_check_timestamp(device, context, timestamp))
2896 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002897
2898 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002899
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002900 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2901 mb();
2902
Tarun Karra696f89e2013-01-27 21:31:40 -08002903 KGSL_DRV_WARN(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002904 "Device hang detected while waiting for timestamp: "
2905 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002906 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2907 context_id, timestamp, context_id, ts_issued, context_id,
2908 kgsl_readtimestamp(device, context,
2909 KGSL_TIMESTAMP_RETIRED),
2910 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002911
Tarun Karrad20d71a2013-01-25 15:38:57 -08002912 /* Return 0 after a successful fault tolerance */
2913 if (!adreno_dump_and_exec_ft(device))
Jordan Crouse92446a62012-11-15 11:00:06 -07002914 return 0;
2915
2916 return -ETIMEDOUT;
2917}
2918
2919static int _check_pending_timestamp(struct kgsl_device *device,
2920 struct kgsl_context *context, unsigned int timestamp)
2921{
2922 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2923 unsigned int context_id = _get_context_id(context);
2924 unsigned int ts_issued;
2925
2926 if (context_id == KGSL_CONTEXT_INVALID)
2927 return -EINVAL;
2928
2929 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2930
2931 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2932 return 0;
2933
2934 if (context && !context->wait_on_invalid_ts) {
2935 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2936 context_id, timestamp, context_id, ts_issued);
2937
2938 /* Only print this message once */
2939 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002940 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002941
2942 return -EINVAL;
2943}
2944
2945/**
2946 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2947 * @device - pointer to a KGSL device structure
2948 * @context - pointer to the active kgsl context
2949 * @timestamp - GPU timestamp to wait for
2950 * @msecs - amount of time to wait (in milliseconds)
2951 *
2952 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2953 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2954 * one if it happened. Otherwise, spend most of our time in an interruptible
2955 * wait for the timestamp interrupt to be processed. This function must be
2956 * called with the mutex already held.
2957 */
2958static int adreno_waittimestamp(struct kgsl_device *device,
2959 struct kgsl_context *context,
2960 unsigned int timestamp,
2961 unsigned int msecs)
2962{
2963 static unsigned int io_cnt;
2964 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2965 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2966 unsigned int context_id = _get_context_id(context);
Tarun Karra696f89e2013-01-27 21:31:40 -08002967 unsigned int prev_reg_val[ft_detect_regs_count];
Jordan Crouse92446a62012-11-15 11:00:06 -07002968 unsigned int time_elapsed = 0;
2969 unsigned int wait;
2970 int ts_compare = 1;
2971 int io, ret = -ETIMEDOUT;
2972
2973 /* Get out early if the context has already been destroyed */
2974
2975 if (context_id == KGSL_CONTEXT_INVALID) {
2976 KGSL_DRV_WARN(device, "context was detached");
2977 return -EINVAL;
2978 }
2979
2980 /*
2981 * Check to see if the requested timestamp is "newer" then the last
2982 * timestamp issued. If it is complain once and return error. Only
2983 * print the message once per context so that badly behaving
2984 * applications don't spam the logs
2985 */
2986
2987 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2988 if (_check_pending_timestamp(device, context, timestamp))
2989 return -EINVAL;
2990
2991 /* Reset the invalid timestamp flag on a valid wait */
2992 context->wait_on_invalid_ts = false;
2993 }
2994
2995
2996 /* Clear the registers used for hang detection */
2997 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2998
2999 /*
3000 * On the first time through the loop only wait 100ms.
3001 * this gives enough time for the engine to start moving and oddly
3002 * provides better hang detection results than just going the full
3003 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
3004 * is if msecs happens to be < 100ms then just use the full timeout
3005 */
3006
3007 wait = 100;
3008
3009 do {
3010 long status;
3011
Jordan Crouse92446a62012-11-15 11:00:06 -07003012 /*
3013 * if the timestamp happens while we're not
3014 * waiting, there's a chance that an interrupt
3015 * will not be generated and thus the timestamp
3016 * work needs to be queued.
3017 */
3018
3019 if (kgsl_check_timestamp(device, context, timestamp)) {
3020 queue_work(device->work_queue, &device->ts_expired_ws);
3021 ret = 0;
3022 break;
3023 }
3024
3025 /* Check to see if the GPU is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08003026 if (adreno_ft_detect(device, prev_reg_val)) {
Jordan Crouse92446a62012-11-15 11:00:06 -07003027 ret = adreno_handle_hang(device, context, timestamp);
3028 break;
3029 }
3030
3031 /*
3032 * For proper power accounting sometimes we need to call
3033 * io_wait_interruptible_timeout and sometimes we need to call
3034 * plain old wait_interruptible_timeout. We call the regular
3035 * timeout N times out of 100, where N is a number specified by
3036 * the current power level
3037 */
3038
3039 io_cnt = (io_cnt + 1) % 100;
3040 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
3041 ? 0 : 1;
3042
3043 mutex_unlock(&device->mutex);
3044
3045 /* Wait for a timestamp event */
3046 status = kgsl_wait_event_interruptible_timeout(
3047 device->wait_queue,
Carter Cooper740f6742013-01-03 16:19:23 -07003048 adreno_check_interrupt_timestamp(device, context,
Jordan Crouse92446a62012-11-15 11:00:06 -07003049 timestamp), msecs_to_jiffies(wait), io);
3050
3051 mutex_lock(&device->mutex);
3052
3053 /*
3054 * If status is non zero then either the condition was satisfied
3055 * or there was an error. In either event, this is the end of
3056 * the line for us
3057 */
3058
3059 if (status != 0) {
3060 ret = (status > 0) ? 0 : (int) status;
3061 break;
3062 }
Jordan Crouse92446a62012-11-15 11:00:06 -07003063 time_elapsed += wait;
3064
3065 /* If user specified timestamps are being used, wait at least
3066 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
3067 * issue a IB for a timestamp before checking to see if the
3068 * current timestamp we are waiting for is valid or not
3069 */
3070
3071 if (ts_compare && (adreno_ctx &&
3072 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
3073 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
3074 ret = _check_pending_timestamp(device, context,
3075 timestamp);
3076 if (ret)
3077 break;
3078
3079 /* Don't do this check again */
3080 ts_compare = 0;
3081
3082 /*
3083 * Reset the invalid timestamp flag on a valid
3084 * wait
3085 */
3086 context->wait_on_invalid_ts = false;
3087 }
3088 }
3089
3090 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003091 * We want to wait the floor of KGSL_TIMEOUT_PART
3092 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07003093 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003094
3095 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
3096 wait = KGSL_TIMEOUT_PART;
3097 else
3098 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07003099
3100 } while (!msecs || time_elapsed < msecs);
3101
3102 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003103}
3104
3105static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003106 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003107{
3108 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003109 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003110
Jeremy Gebben9ad86922012-05-08 15:33:23 -06003111 /*
3112 * If the context ID is invalid, we are in a race with
3113 * the context being destroyed by userspace so bail.
3114 */
3115 if (context_id == KGSL_CONTEXT_INVALID) {
3116 KGSL_DRV_WARN(device, "context was detached");
3117 return timestamp;
3118 }
Jordan Crousec659f382012-04-16 11:10:41 -06003119 switch (type) {
3120 case KGSL_TIMESTAMP_QUEUED: {
3121 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3122 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
3123
3124 timestamp = rb->timestamp[context_id];
3125 break;
3126 }
3127 case KGSL_TIMESTAMP_CONSUMED:
Carter Cooper78d8d362013-05-28 17:01:59 -06003128 kgsl_sharedmem_readl(&device->memstore, &timestamp,
3129 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp));
Jordan Crousec659f382012-04-16 11:10:41 -06003130 break;
3131 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003132 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06003133 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
3134 break;
3135 }
3136
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003137 rmb();
3138
3139 return timestamp;
3140}
3141
3142static long adreno_ioctl(struct kgsl_device_private *dev_priv,
3143 unsigned int cmd, void *data)
3144{
3145 int result = 0;
3146 struct kgsl_drawctxt_set_bin_base_offset *binbase;
3147 struct kgsl_context *context;
3148
3149 switch (cmd) {
3150 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
3151 binbase = data;
3152
Jordan Crouse67db48d2013-05-28 17:04:17 -06003153 context = kgsl_context_get_owner(dev_priv,
3154 binbase->drawctxt_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003155 if (context) {
3156 adreno_drawctxt_set_bin_base_offset(
3157 dev_priv->device, context, binbase->offset);
3158 } else {
3159 result = -EINVAL;
3160 KGSL_DRV_ERR(dev_priv->device,
3161 "invalid drawctxt drawctxt_id %d "
3162 "device_id=%d\n",
3163 binbase->drawctxt_id, dev_priv->device->id);
3164 }
Jordan Crouse67db48d2013-05-28 17:04:17 -06003165
3166 kgsl_context_put(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003167 break;
3168
3169 default:
3170 KGSL_DRV_INFO(dev_priv->device,
3171 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07003172 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003173 break;
3174 }
3175 return result;
3176
3177}
3178
3179static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
3180{
3181 gpu_freq /= 1000000;
3182 return ticks / gpu_freq;
3183}
3184
3185static void adreno_power_stats(struct kgsl_device *device,
3186 struct kgsl_power_stats *stats)
3187{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003188 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003189 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003190 unsigned int cycles;
3191
3192 /* Get the busy cycles counted since the counter was last reset */
3193 /* Calling this function also resets and restarts the counter */
3194
3195 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003196
3197 /* In order to calculate idle you have to have run the algorithm *
3198 * at least once to get a start time. */
3199 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003200 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003201 stats->total_time = tmp - pwr->time;
3202 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003203 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003204 pwrlevels[device->pwrctrl.active_pwrlevel].
3205 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003206 } else {
3207 stats->total_time = 0;
3208 stats->busy_time = 0;
3209 pwr->time = ktime_to_us(ktime_get());
3210 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003211}
3212
3213void adreno_irqctrl(struct kgsl_device *device, int state)
3214{
Jordan Crousea78c9172011-07-11 13:14:09 -06003215 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3216 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003217}
3218
Jordan Croused6535882012-06-20 08:22:16 -06003219static unsigned int adreno_gpuid(struct kgsl_device *device,
3220 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07003221{
3222 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3223
Jordan Croused6535882012-06-20 08:22:16 -06003224 /* Some applications need to know the chip ID too, so pass
3225 * that as a parameter */
3226
3227 if (chipid != NULL)
3228 *chipid = adreno_dev->chip_id;
3229
Jordan Crousea0758f22011-12-07 11:19:22 -07003230 /* Standard KGSL gpuid format:
3231 * top word is 0x0002 for 2D or 0x0003 for 3D
3232 * Bottom word is core specific identifer
3233 */
3234
3235 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
3236}
3237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003238static const struct kgsl_functable adreno_functable = {
3239 /* Mandatory functions */
3240 .regread = adreno_regread,
3241 .regwrite = adreno_regwrite,
3242 .idle = adreno_idle,
3243 .isidle = adreno_isidle,
3244 .suspend_context = adreno_suspend_context,
3245 .start = adreno_start,
3246 .stop = adreno_stop,
3247 .getproperty = adreno_getproperty,
3248 .waittimestamp = adreno_waittimestamp,
3249 .readtimestamp = adreno_readtimestamp,
3250 .issueibcmds = adreno_ringbuffer_issueibcmds,
3251 .ioctl = adreno_ioctl,
3252 .setup_pt = adreno_setup_pt,
3253 .cleanup_pt = adreno_cleanup_pt,
3254 .power_stats = adreno_power_stats,
3255 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07003256 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07003257 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06003258 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003259 /* Optional functions */
3260 .setstate = adreno_setstate,
3261 .drawctxt_create = adreno_drawctxt_create,
3262 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06003263 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06003264 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07003265 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003266};
3267
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003268static struct platform_driver adreno_platform_driver = {
3269 .probe = adreno_probe,
3270 .remove = __devexit_p(adreno_remove),
3271 .suspend = kgsl_suspend_driver,
3272 .resume = kgsl_resume_driver,
3273 .id_table = adreno_id_table,
3274 .driver = {
3275 .owner = THIS_MODULE,
3276 .name = DEVICE_3D_NAME,
3277 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06003278 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003279 }
3280};
3281
3282static int __init kgsl_3d_init(void)
3283{
3284 return platform_driver_register(&adreno_platform_driver);
3285}
3286
3287static void __exit kgsl_3d_exit(void)
3288{
3289 platform_driver_unregister(&adreno_platform_driver);
3290}
3291
3292module_init(kgsl_3d_init);
3293module_exit(kgsl_3d_exit);
3294
3295MODULE_DESCRIPTION("3D Graphics driver");
3296MODULE_VERSION("1.2");
3297MODULE_LICENSE("GPL v2");
3298MODULE_ALIAS("platform:kgsl_3d");