blob: 9f4bb88c770cedf8d4d7df503290c2122e32a78e [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
318 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700319 if (context == NULL)
320 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600321 adreno_ctx = context->devctxt;
322
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600323 if (kgsl_mmu_enable_clk(&device->mmu,
324 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700325 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600326
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600327 cmds += __adreno_add_idle_indirect_cmds(cmds,
328 device->mmu.setstate_memory.gpuaddr +
329 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
330
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600331 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600332 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
333 device->mmu.setstate_memory.gpuaddr +
334 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
335 else
336 cmds += adreno_add_bank_change_cmds(cmds,
337 KGSL_IOMMU_CONTEXT_USER,
338 device->mmu.setstate_memory.gpuaddr +
339 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
340
Tarun Karra9c070822012-11-27 16:43:51 -0700341 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
342
343 /* Acquire GPU-CPU sync Lock here */
344 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
345
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700346 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
347 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600348 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600349 /*
350 * We need to perfrom the following operations for all
351 * IOMMU units
352 */
353 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700354 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
355 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600356 /*
357 * Set address of the new pagetable by writng to IOMMU
358 * TTBR0 register
359 */
360 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700361 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
362 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600363 *cmds++ = reg_pt_val;
364 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
365 *cmds++ = 0x00000000;
366
367 /*
368 * Read back the ttbr0 register as a barrier to ensure
369 * above writes have completed
370 */
371 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700372 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
373 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600374 reg_pt_val,
375 device->mmu.setstate_memory.gpuaddr +
376 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600377 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600378 }
379 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
380 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700381 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600382 */
383 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700384 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
385 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700386
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600387 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700388 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
389 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700390 KGSL_IOMMU_CTX_TLBIALL);
391 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600392
393 cmds += __adreno_add_idle_indirect_cmds(cmds,
394 device->mmu.setstate_memory.gpuaddr +
395 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
396
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600397 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700398 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
399 KGSL_IOMMU_CONTEXT_USER,
400 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700401 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600402 device->mmu.setstate_memory.gpuaddr +
403 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
404 }
405 }
406
Tarun Karra9c070822012-11-27 16:43:51 -0700407 /* Release GPU-CPU sync Lock here */
408 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
409
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600410 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600411 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700412 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
413 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600414 device->mmu.setstate_memory.gpuaddr +
415 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
416 else
417 cmds += adreno_add_bank_change_cmds(cmds,
418 KGSL_IOMMU_CONTEXT_PRIV,
419 device->mmu.setstate_memory.gpuaddr +
420 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
421
Tarun Karra9c070822012-11-27 16:43:51 -0700422 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
423
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600424 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600425 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700426 /* invalidate all base pointers */
427 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
428 *cmds++ = 0x7fff;
429 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600430 /* This returns the per context timestamp but we need to
431 * use the global timestamp for iommu clock disablement */
432 adreno_ringbuffer_issuecmds(device, adreno_ctx,
433 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600434 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600435 kgsl_mmu_disable_clk_on_ts(&device->mmu,
436 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600437 }
Tarun Karra9c070822012-11-27 16:43:51 -0700438
439 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
440 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
441 BUG();
442 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600443}
444
445static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600446 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600447 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448{
449 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
450 unsigned int link[32];
451 unsigned int *cmds = &link[0];
452 int sizedwords = 0;
453 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600454 struct kgsl_context *context;
455 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600457 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530458 * Fix target freeze issue by adding TLB flush for each submit
459 * on A20X based targets.
460 */
461 if (adreno_is_a20x(adreno_dev))
462 flags |= KGSL_MMUFLAGS_TLBFLUSH;
463 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600464 * If possible, then set the state via the command stream to avoid
465 * a CPU idle. Otherwise, use the default setstate which uses register
466 * writes For CFF dump we must idle and use the registers so that it is
467 * easier to filter out the mmu accesses from the dump
468 */
469 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600470 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700471 if (context == NULL)
472 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600473 adreno_ctx = context->devctxt;
474
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
476 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600477 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 *cmds++ = 0x00000000;
479
480 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600481 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700482 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600483 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 sizedwords += 4;
485 }
486
487 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
488 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600489 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 1);
491 *cmds++ = 0x00000000;
492 sizedwords += 2;
493 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600494 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 *cmds++ = mh_mmu_invalidate;
496 sizedwords += 2;
497 }
498
499 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600500 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501 /* HW workaround: to resolve MMU page fault interrupts
502 * caused by the VGT.It prevents the CP PFP from filling
503 * the VGT DMA request fifo too early,thereby ensuring
504 * that the VGT will not fetch vertex/bin data until
505 * after the page table base register has been updated.
506 *
507 * Two null DRAW_INDX_BIN packets are inserted right
508 * after the page table base update, followed by a
509 * wait for idle. The null packets will fill up the
510 * VGT DMA request fifo and prevent any further
511 * vertex/bin updates from occurring until the wait
512 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600513 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 *cmds++ = (0x4 << 16) |
515 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
516 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600517 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600518 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600519 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 *cmds++ = 0; /* viz query info */
521 *cmds++ = 0x0003C004; /* draw indicator */
522 *cmds++ = 0; /* bin base */
523 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600524 *cmds++ =
525 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600527 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 *cmds++ = 0; /* viz query info */
529 *cmds++ = 0x0003C004; /* draw indicator */
530 *cmds++ = 0; /* bin base */
531 *cmds++ = 3; /* bin size */
532 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600533 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600535 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536 *cmds++ = 0x00000000;
537 sizedwords += 21;
538 }
539
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600540
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600542 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 *cmds++ = 0x7fff; /* invalidate all base pointers */
544 sizedwords += 2;
545 }
546
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600547 adreno_ringbuffer_issuecmds(device, adreno_ctx,
548 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600550 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600551 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600552 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553}
554
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600555static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600556 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600557 uint32_t flags)
558{
559 /* call the mmu specific handler */
560 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600561 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600562 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600563 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600564}
565
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700567a3xx_getchipid(struct kgsl_device *device)
568{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600569 struct kgsl_device_platform_data *pdata =
570 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700571
Jordan Crouse54154c62012-03-27 16:33:26 -0600572 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600573 * All current A3XX chipids are detected at the SOC level. Leave this
574 * function here to support any future GPUs that have working
575 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600576 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700577
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600578 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700579}
580
581static unsigned int
582a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583{
584 unsigned int chipid = 0;
585 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600586 struct kgsl_device_platform_data *pdata =
587 kgsl_device_get_drvdata(device);
588
589 /* If the chip id is set at the platform level, then just use that */
590
591 if (pdata->chipid != 0)
592 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593
594 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
595 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
596 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
597
598 /*
599 * adreno 22x gpus are indicated by coreid 2,
600 * but REG_RBBM_PERIPHID1 always contains 0 for this field
601 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600602 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 chipid = 2 << 24;
604 else
605 chipid = (coreid & 0xF) << 24;
606
607 chipid |= ((majorid >> 4) & 0xF) << 16;
608
609 minorid = ((revid >> 0) & 0xFF);
610
611 patchid = ((revid >> 16) & 0xFF);
612
613 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530614 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615 if (cpu_is_qsd8x50())
616 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530617 else if (cpu_is_msm8625() && minorid == 0)
618 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619
620 chipid |= (minorid << 8) | patchid;
621
622 return chipid;
623}
624
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700625static unsigned int
626adreno_getchipid(struct kgsl_device *device)
627{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600628 struct kgsl_device_platform_data *pdata =
629 kgsl_device_get_drvdata(device);
630
631 /*
632 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
633 * an A2XX processor
634 */
635
636 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700637 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600638 else
639 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700640}
641
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642static inline bool _rev_match(unsigned int id, unsigned int entry)
643{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600644 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646
647static void
648adreno_identify_gpu(struct adreno_device *adreno_dev)
649{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600650 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651
652 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
653
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600654 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
655 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
656 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
657 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658
Jordan Crouse505df9c2011-07-28 08:37:59 -0600659 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
660 if (core == adreno_gpulist[i].core &&
661 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600662 _rev_match(minor, adreno_gpulist[i].minor) &&
663 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665 }
666
Jordan Crouse505df9c2011-07-28 08:37:59 -0600667 if (i == ARRAY_SIZE(adreno_gpulist)) {
668 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
669 return;
670 }
671
672 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
673 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
674 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
675 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700676 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
677 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700678 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600679 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Tarun Karra9c070822012-11-27 16:43:51 -0700680 adreno_dev->gpulist_index = i;
681
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682}
683
Lokesh Batra805e1e12012-08-03 08:34:06 -0600684static struct platform_device_id adreno_id_table[] = {
685 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
686 {},
687};
688
689MODULE_DEVICE_TABLE(platform, adreno_id_table);
690
691static struct of_device_id adreno_match_table[] = {
692 { .compatible = "qcom,kgsl-3d0", },
693 {}
694};
695
696static inline int adreno_of_read_property(struct device_node *node,
697 const char *prop, unsigned int *ptr)
698{
699 int ret = of_property_read_u32(node, prop, ptr);
700 if (ret)
701 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
702 return ret;
703}
704
705static struct device_node *adreno_of_find_subnode(struct device_node *parent,
706 const char *name)
707{
708 struct device_node *child;
709
710 for_each_child_of_node(parent, child) {
711 if (of_device_is_compatible(child, name))
712 return child;
713 }
714
715 return NULL;
716}
717
718static int adreno_of_get_pwrlevels(struct device_node *parent,
719 struct kgsl_device_platform_data *pdata)
720{
721 struct device_node *node, *child;
722 int ret = -EINVAL;
723
724 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
725
726 if (node == NULL) {
727 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
728 return -EINVAL;
729 }
730
731 pdata->num_levels = 0;
732
733 for_each_child_of_node(node, child) {
734 unsigned int index;
735 struct kgsl_pwrlevel *level;
736
737 if (adreno_of_read_property(child, "reg", &index))
738 goto done;
739
740 if (index >= KGSL_MAX_PWRLEVELS) {
741 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
742 index);
743 continue;
744 }
745
746 if (index >= pdata->num_levels)
747 pdata->num_levels = index + 1;
748
749 level = &pdata->pwrlevel[index];
750
751 if (adreno_of_read_property(child, "qcom,gpu-freq",
752 &level->gpu_freq))
753 goto done;
754
755 if (adreno_of_read_property(child, "qcom,bus-freq",
756 &level->bus_freq))
757 goto done;
758
759 if (adreno_of_read_property(child, "qcom,io-fraction",
760 &level->io_fraction))
761 level->io_fraction = 0;
762 }
763
764 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
765 &pdata->init_level))
766 pdata->init_level = 1;
767
768 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
769 KGSL_CORE_ERR("Initial power level out of range\n");
770 pdata->init_level = 1;
771 }
772
773 ret = 0;
774done:
775 return ret;
776
777}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600778
779static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
780{
781 struct device_node *node, *child;
782 struct msm_dcvs_core_info *info = NULL;
783 int count = 0;
784 int ret = -EINVAL;
785
786 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
787 if (node == NULL)
788 return ERR_PTR(-EINVAL);
789
790 info = kzalloc(sizeof(*info), GFP_KERNEL);
791
792 if (info == NULL) {
793 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
794 ret = -ENOMEM;
795 goto err;
796 }
797
798 for_each_child_of_node(node, child)
799 count++;
800
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700801 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600802
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700803 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600804 sizeof(struct msm_dcvs_freq_entry),
805 GFP_KERNEL);
806
807 if (info->freq_tbl == NULL) {
808 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700809 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600810 sizeof(struct msm_dcvs_freq_entry));
811 ret = -ENOMEM;
812 goto err;
813 }
814
815 for_each_child_of_node(node, child) {
816 unsigned int index;
817
818 if (adreno_of_read_property(child, "reg", &index))
819 goto err;
820
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700821 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600822 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
823 index);
824 continue;
825 }
826
827 if (adreno_of_read_property(child, "qcom,freq",
828 &info->freq_tbl[index].freq))
829 goto err;
830
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700831 if (adreno_of_read_property(child, "qcom,voltage",
832 &info->freq_tbl[index].voltage))
833 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600834
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700835 if (adreno_of_read_property(child, "qcom,is_trans_level",
836 &info->freq_tbl[index].is_trans_level))
837 info->freq_tbl[index].is_trans_level = 0;
838
839 if (adreno_of_read_property(child, "qcom,active-energy-offset",
840 &info->freq_tbl[index].active_energy_offset))
841 info->freq_tbl[index].active_energy_offset = 0;
842
843 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
844 &info->freq_tbl[index].leakage_energy_offset))
845 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600846 }
847
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700848 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
849 goto err;
850
851 info->sensors = kzalloc(info->num_cores *
852 sizeof(int),
853 GFP_KERNEL);
854
855 for (count = 0; count < info->num_cores; count++) {
856 if (adreno_of_read_property(node, "qcom,sensors",
857 &(info->sensors[count])))
858 goto err;
859 }
860
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700861 if (adreno_of_read_property(node, "qcom,core-core-type",
862 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600863 goto err;
864
865 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
866 &info->algo_param.disable_pc_threshold))
867 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700868 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
869 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600870 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700871 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
872 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600873 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600874 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
875 &info->algo_param.em_max_util_pct))
876 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700877 if (adreno_of_read_property(node, "qcom,algo-group-id",
878 &info->algo_param.group_id))
879 goto err;
880 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
881 &info->algo_param.max_freq_chg_time_us))
882 goto err;
883 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
884 &info->algo_param.slack_mode_dynamic))
885 goto err;
886 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
887 &info->algo_param.slack_weight_thresh_pct))
888 goto err;
889 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
890 &info->algo_param.slack_time_min_us))
891 goto err;
892 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
893 &info->algo_param.slack_time_max_us))
894 goto err;
895 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
896 &info->algo_param.ss_win_size_min_us))
897 goto err;
898 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
899 &info->algo_param.ss_win_size_max_us))
900 goto err;
901 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
902 &info->algo_param.ss_util_pct))
903 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800904 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
905 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600906 goto err;
907
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700908 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
909 &info->energy_coeffs.active_coeff_a))
910 goto err;
911 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
912 &info->energy_coeffs.active_coeff_b))
913 goto err;
914 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
915 &info->energy_coeffs.active_coeff_c))
916 goto err;
917 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
918 &info->energy_coeffs.leakage_coeff_a))
919 goto err;
920 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
921 &info->energy_coeffs.leakage_coeff_b))
922 goto err;
923 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
924 &info->energy_coeffs.leakage_coeff_c))
925 goto err;
926 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
927 &info->energy_coeffs.leakage_coeff_d))
928 goto err;
929
930 if (adreno_of_read_property(node, "qcom,power-current-temp",
931 &info->power_param.current_temp))
932 goto err;
933
Lokesh Batra805e1e12012-08-03 08:34:06 -0600934 return info;
935
936err:
937 if (info)
938 kfree(info->freq_tbl);
939
940 kfree(info);
941
942 return ERR_PTR(ret);
943}
944
945static int adreno_of_get_iommu(struct device_node *parent,
946 struct kgsl_device_platform_data *pdata)
947{
948 struct device_node *node, *child;
949 struct kgsl_device_iommu_data *data = NULL;
950 struct kgsl_iommu_ctx *ctxs = NULL;
951 u32 reg_val[2];
952 int ctx_index = 0;
953
954 node = of_parse_phandle(parent, "iommu", 0);
955 if (node == NULL)
956 return -EINVAL;
957
958 data = kzalloc(sizeof(*data), GFP_KERNEL);
959 if (data == NULL) {
960 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
961 goto err;
962 }
963
964 if (of_property_read_u32_array(node, "reg", reg_val, 2))
965 goto err;
966
967 data->physstart = reg_val[0];
968 data->physend = data->physstart + reg_val[1] - 1;
969
970 data->iommu_ctx_count = 0;
971
972 for_each_child_of_node(node, child)
973 data->iommu_ctx_count++;
974
975 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
976 GFP_KERNEL);
977
978 if (ctxs == NULL) {
979 KGSL_CORE_ERR("kzalloc(%d) failed\n",
980 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
981 goto err;
982 }
983
984 for_each_child_of_node(node, child) {
985 int ret = of_property_read_string(child, "label",
986 &ctxs[ctx_index].iommu_ctx_name);
987
988 if (ret) {
989 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
990 goto err;
991 }
992
993 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
994 &ctxs[ctx_index].ctx_id))
995 goto err;
996
997 ctx_index++;
998 }
999
1000 data->iommu_ctxs = ctxs;
1001
1002 pdata->iommu_data = data;
1003 pdata->iommu_count = 1;
1004
1005 return 0;
1006
1007err:
1008 kfree(ctxs);
1009 kfree(data);
1010
1011 return -EINVAL;
1012}
1013
1014static int adreno_of_get_pdata(struct platform_device *pdev)
1015{
1016 struct kgsl_device_platform_data *pdata = NULL;
1017 struct kgsl_device *device;
1018 int ret = -EINVAL;
1019
1020 pdev->id_entry = adreno_id_table;
1021
1022 pdata = pdev->dev.platform_data;
1023 if (pdata)
1024 return 0;
1025
1026 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1027 KGSL_CORE_ERR("Unable to read 'label'\n");
1028 goto err;
1029 }
1030
1031 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1032 goto err;
1033
1034 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1035 if (pdata == NULL) {
1036 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1037 ret = -ENOMEM;
1038 goto err;
1039 }
1040
1041 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1042 &pdata->chipid))
1043 goto err;
1044
1045 /* pwrlevel Data */
1046 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1047 if (ret)
1048 goto err;
1049
1050 /* Default value is 83, if not found in DT */
1051 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1052 &pdata->idle_timeout))
1053 pdata->idle_timeout = 83;
1054
1055 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1056 &pdata->nap_allowed))
1057 pdata->nap_allowed = 1;
1058
1059 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1060 &pdata->clk_map))
1061 goto err;
1062
1063 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1064
1065 if (device->id != KGSL_DEVICE_3D0)
1066 goto err;
1067
1068 /* Bus Scale Data */
1069
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001070 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001071 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1072 ret = PTR_ERR(pdata->bus_scale_table);
1073 goto err;
1074 }
1075
1076 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1077 if (IS_ERR_OR_NULL(pdata->core_info)) {
1078 ret = PTR_ERR(pdata->core_info);
1079 goto err;
1080 }
1081
1082 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1083 if (ret)
1084 goto err;
1085
1086 pdev->dev.platform_data = pdata;
1087 return 0;
1088
1089err:
1090 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001091 if (pdata->core_info)
1092 kfree(pdata->core_info->freq_tbl);
1093 kfree(pdata->core_info);
1094
1095 if (pdata->iommu_data)
1096 kfree(pdata->iommu_data->iommu_ctxs);
1097
1098 kfree(pdata->iommu_data);
1099 }
1100
1101 kfree(pdata);
1102
1103 return ret;
1104}
1105
liu zhong7dfa2a32012-04-27 19:11:01 -07001106#ifdef CONFIG_MSM_OCMEM
1107static int
1108adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1109{
Jordan Crousec0978202012-08-29 14:35:51 -06001110 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001111 return 0;
1112
1113 /* OCMEM is only needed once, do not support consective allocation */
1114 if (adreno_dev->ocmem_hdl != NULL)
1115 return 0;
1116
1117 adreno_dev->ocmem_hdl =
1118 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1119 if (adreno_dev->ocmem_hdl == NULL)
1120 return -ENOMEM;
1121
1122 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001123 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001124
1125 return 0;
1126}
1127
1128static void
1129adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1130{
Jordan Crousec0978202012-08-29 14:35:51 -06001131 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001132 return;
1133
1134 if (adreno_dev->ocmem_hdl == NULL)
1135 return;
1136
1137 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1138 adreno_dev->ocmem_hdl = NULL;
1139}
1140#else
1141static int
1142adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1143{
1144 return 0;
1145}
1146
1147static void
1148adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1149{
1150}
1151#endif
1152
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153static int __devinit
1154adreno_probe(struct platform_device *pdev)
1155{
1156 struct kgsl_device *device;
1157 struct adreno_device *adreno_dev;
1158 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001159 bool is_dt;
1160
1161 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1162
1163 if (is_dt && pdev->dev.of_node) {
1164 status = adreno_of_get_pdata(pdev);
1165 if (status)
1166 goto error_return;
1167 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001168
1169 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1170 adreno_dev = ADRENO_DEVICE(device);
1171 device->parentdev = &pdev->dev;
1172
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173 status = adreno_ringbuffer_init(device);
1174 if (status != 0)
1175 goto error;
1176
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001177 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 if (status)
1179 goto error_close_rb;
1180
1181 adreno_debugfs_init(device);
1182
1183 kgsl_pwrscale_init(device);
1184 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1185
1186 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1187 return 0;
1188
1189error_close_rb:
1190 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1191error:
1192 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001193error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194 return status;
1195}
1196
1197static int __devexit adreno_remove(struct platform_device *pdev)
1198{
1199 struct kgsl_device *device;
1200 struct adreno_device *adreno_dev;
1201
1202 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1203 adreno_dev = ADRENO_DEVICE(device);
1204
1205 kgsl_pwrscale_detach_policy(device);
1206 kgsl_pwrscale_close(device);
1207
1208 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1209 kgsl_device_platform_remove(device);
1210
1211 return 0;
1212}
1213
1214static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1215{
1216 int status = -EINVAL;
1217 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218
Tarun Karrad20d71a2013-01-25 15:38:57 -08001219 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001220 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221
1222 /* Power up the device */
1223 kgsl_pwrctrl_enable(device);
1224
1225 /* Identify the specific GPU */
1226 adreno_identify_gpu(adreno_dev);
1227
Tarun Karra9c070822012-11-27 16:43:51 -07001228 if (adreno_ringbuffer_read_pm4_ucode(device)) {
1229 KGSL_DRV_ERR(device, "Reading pm4 microcode failed %s\n",
1230 adreno_dev->pm4_fwfile);
1231 BUG_ON(1);
1232 }
1233
1234 if (adreno_ringbuffer_read_pfp_ucode(device)) {
1235 KGSL_DRV_ERR(device, "Reading pfp microcode failed %s\n",
1236 adreno_dev->pfp_fwfile);
1237 BUG_ON(1);
1238 }
1239
Jordan Crouse505df9c2011-07-28 08:37:59 -06001240 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1241 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1242 adreno_dev->chip_id);
1243 goto error_clk_off;
1244 }
1245
Tarun Karra9c070822012-11-27 16:43:51 -07001246
1247 /*
1248 * Check if firmware supports the sync lock PM4 packets needed
1249 * for IOMMUv1
1250 */
1251
1252 if ((adreno_dev->pm4_fw_version >=
1253 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pm4_ver) &&
1254 (adreno_dev->pfp_fw_version >=
1255 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pfp_ver))
1256 device->mmu.flags |= KGSL_MMU_FLAGS_IOMMU_SYNC;
1257
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001258 /* Set up the MMU */
1259 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001260 /*
1261 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1262 * on older gpus
1263 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001264 if (adreno_is_a20x(adreno_dev)) {
1265 device->mh.mh_intf_cfg1 = 0;
1266 device->mh.mh_intf_cfg2 = 0;
1267 }
1268
1269 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001270 }
1271
Tarun Karra3335f142012-06-19 14:11:48 -07001272 /* Assign correct RBBM status register to hang detect regs
1273 */
Tarun Karra696f89e2013-01-27 21:31:40 -08001274 ft_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
Tarun Karra3335f142012-06-19 14:11:48 -07001275
Jordan Crouseb5c80482012-10-03 09:38:41 -06001276 /* Add A3XX specific registers for hang detection */
1277 if (adreno_is_a3xx(adreno_dev)) {
Tarun Karra696f89e2013-01-27 21:31:40 -08001278 ft_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1279 ft_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
1280 ft_detect_regs[8] = A3XX_RBBM_PERFCTR_SP_6_LO;
1281 ft_detect_regs[9] = A3XX_RBBM_PERFCTR_SP_6_HI;
1282 ft_detect_regs[10] = A3XX_RBBM_PERFCTR_SP_5_LO;
1283 ft_detect_regs[11] = A3XX_RBBM_PERFCTR_SP_5_HI;
Jordan Crouseb5c80482012-10-03 09:38:41 -06001284 }
1285
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001286 status = kgsl_mmu_start(device);
1287 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288 goto error_clk_off;
1289
liu zhong7dfa2a32012-04-27 19:11:01 -07001290 status = adreno_ocmem_gmem_malloc(adreno_dev);
1291 if (status) {
1292 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1293 goto error_mmu_off;
1294 }
1295
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001296 /* Start the GPU */
1297 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001298
1299 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001300 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301
1302 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouse013cf422013-05-28 17:03:32 -06001303 if (status)
1304 goto error_irq_off;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305
Jordan Crouse013cf422013-05-28 17:03:32 -06001306 /*
1307 * While recovery is on we do not want timer to
1308 * fire and attempt to change any device state
1309 */
1310
1311 if (KGSL_STATE_DUMP_AND_FT != device->state)
1312 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1313
1314 device->reset_counter++;
1315
1316 return 0;
1317
1318error_irq_off:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001319 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001320
1321error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001322 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001323
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001324error_clk_off:
1325 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001326
1327 return status;
1328}
1329
1330static int adreno_stop(struct kgsl_device *device)
1331{
1332 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1333
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001334 adreno_dev->drawctxt_active = NULL;
1335
1336 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1337
Shubhraprakash Das79447952012-04-26 18:12:23 -06001338 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001340 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301341 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001342 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001343
liu zhong7dfa2a32012-04-27 19:11:01 -07001344 adreno_ocmem_gmem_free(adreno_dev);
1345
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001346 /* Power down the device */
1347 kgsl_pwrctrl_disable(device);
1348
1349 return 0;
1350}
1351
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001352static void adreno_mark_context_status(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001353 int ft_status)
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001354{
1355 struct kgsl_context *context;
1356 int next = 0;
1357 /*
1358 * Set the reset status of all contexts to
1359 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
Tarun Karrad20d71a2013-01-25 15:38:57 -08001360 * since thats the guilty party, if fault tolerance failed then
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001361 * mark all as guilty
1362 */
1363 while ((context = idr_get_next(&device->context_idr, &next))) {
1364 struct adreno_context *adreno_context = context->devctxt;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001365 if (ft_status) {
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001366 context->reset_status =
1367 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1368 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1369 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1370 context->reset_status) {
Carter Cooper8179f5a2012-12-17 11:32:27 -07001371 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG |
Tarun Karrad20d71a2013-01-25 15:38:57 -08001372 CTXT_FLAGS_GPU_HANG_FT))
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001373 context->reset_status =
1374 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1375 else
1376 context->reset_status =
1377 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1378 }
1379 next = next + 1;
1380 }
1381}
1382
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001383static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1384{
1385 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1386 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1387 struct kgsl_context *context;
1388 struct adreno_context *temp_adreno_context;
1389 int next = 0;
1390
1391 while ((context = idr_get_next(&device->context_idr, &next))) {
1392 temp_adreno_context = context->devctxt;
1393 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1394 kgsl_sharedmem_writel(&device->memstore,
1395 KGSL_MEMSTORE_OFFSET(context->id,
1396 soptimestamp),
1397 rb->timestamp[context->id]);
1398 kgsl_sharedmem_writel(&device->memstore,
1399 KGSL_MEMSTORE_OFFSET(context->id,
1400 eoptimestamp),
1401 rb->timestamp[context->id]);
1402 }
1403 next = next + 1;
1404 }
1405}
1406
Tarun Karrad20d71a2013-01-25 15:38:57 -08001407static void adreno_destroy_ft_data(struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001408{
Tarun Karrad20d71a2013-01-25 15:38:57 -08001409 vfree(ft_data->rb_buffer);
1410 vfree(ft_data->bad_rb_buffer);
1411 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001412}
1413
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001414static int _find_start_of_cmd_seq(struct adreno_ringbuffer *rb,
1415 unsigned int *ptr,
1416 bool inc)
1417{
1418 int status = -EINVAL;
1419 unsigned int val1;
1420 unsigned int size = rb->buffer_desc.size;
1421 unsigned int start_ptr = *ptr;
1422
1423 while ((start_ptr / sizeof(unsigned int)) != rb->wptr) {
1424 if (inc)
1425 start_ptr = adreno_ringbuffer_inc_wrapped(start_ptr,
1426 size);
1427 else
1428 start_ptr = adreno_ringbuffer_dec_wrapped(start_ptr,
1429 size);
1430 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, start_ptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001431 /* Ensure above read is finished before next read */
1432 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001433 if (KGSL_CMD_IDENTIFIER == val1) {
1434 if ((start_ptr / sizeof(unsigned int)) != rb->wptr)
1435 start_ptr = adreno_ringbuffer_dec_wrapped(
1436 start_ptr, size);
1437 *ptr = start_ptr;
1438 status = 0;
1439 break;
1440 }
1441 }
1442 return status;
1443}
1444
1445static int _find_cmd_seq_after_eop_ts(struct adreno_ringbuffer *rb,
1446 unsigned int *rb_rptr,
1447 unsigned int global_eop,
1448 bool inc)
1449{
1450 int status = -EINVAL;
1451 unsigned int temp_rb_rptr = *rb_rptr;
1452 unsigned int size = rb->buffer_desc.size;
1453 unsigned int val[3];
1454 int i = 0;
1455 bool check = false;
1456
1457 if (inc && temp_rb_rptr / sizeof(unsigned int) != rb->wptr)
1458 return status;
1459
1460 do {
1461 /*
1462 * when decrementing we need to decrement first and
1463 * then read make sure we cover all the data
1464 */
1465 if (!inc)
1466 temp_rb_rptr = adreno_ringbuffer_dec_wrapped(
1467 temp_rb_rptr, size);
1468 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i],
1469 temp_rb_rptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001470 /* Ensure above read is finished before next read */
1471 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001472
1473 if (check && ((inc && val[i] == global_eop) ||
1474 (!inc && (val[i] ==
1475 cp_type3_packet(CP_MEM_WRITE, 2) ||
1476 val[i] == CACHE_FLUSH_TS)))) {
1477 /* decrement i, i.e i = (i - 1 + 3) % 3 if
1478 * we are going forward, else increment i */
1479 i = (i + 2) % 3;
1480 if (val[i] == rb->device->memstore.gpuaddr +
1481 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1482 eoptimestamp)) {
1483 int j = ((i + 2) % 3);
1484 if ((inc && (val[j] == CACHE_FLUSH_TS ||
1485 val[j] == cp_type3_packet(
1486 CP_MEM_WRITE, 2))) ||
1487 (!inc && val[j] == global_eop)) {
1488 /* Found the global eop */
1489 status = 0;
1490 break;
1491 }
1492 }
1493 /* if no match found then increment i again
1494 * since we decremented before matching */
1495 i = (i + 1) % 3;
1496 }
1497 if (inc)
1498 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(
1499 temp_rb_rptr, size);
1500
1501 i = (i + 1) % 3;
1502 if (2 == i)
1503 check = true;
1504 } while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr);
1505 /* temp_rb_rptr points to the command stream after global eop,
1506 * move backward till the start of command sequence */
1507 if (!status) {
1508 status = _find_start_of_cmd_seq(rb, &temp_rb_rptr, false);
1509 if (!status) {
1510 *rb_rptr = temp_rb_rptr;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001511 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001512 "Offset of cmd sequence after eop timestamp: 0x%x\n",
1513 temp_rb_rptr / sizeof(unsigned int));
1514 }
1515 }
1516 if (status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001517 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001518 "Failed to find the command sequence after eop timestamp\n");
1519 return status;
1520}
1521
1522static int _find_hanging_ib_sequence(struct adreno_ringbuffer *rb,
1523 unsigned int *rb_rptr,
1524 unsigned int ib1)
1525{
1526 int status = -EINVAL;
1527 unsigned int temp_rb_rptr = *rb_rptr;
1528 unsigned int size = rb->buffer_desc.size;
1529 unsigned int val[2];
1530 int i = 0;
1531 bool check = false;
1532 bool ctx_switch = false;
1533
1534 while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr) {
1535 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i], temp_rb_rptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001536 /* Ensure above read is finished before next read */
1537 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001538
1539 if (check && val[i] == ib1) {
1540 /* decrement i, i.e i = (i - 1 + 2) % 2 */
1541 i = (i + 1) % 2;
1542 if (adreno_cmd_is_ib(val[i])) {
1543 /* go till start of command sequence */
1544 status = _find_start_of_cmd_seq(rb,
1545 &temp_rb_rptr, false);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001546
1547 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001548 "Found the hanging IB at offset 0x%x\n",
1549 temp_rb_rptr / sizeof(unsigned int));
1550 break;
1551 }
1552 /* if no match the increment i since we decremented
1553 * before checking */
1554 i = (i + 1) % 2;
1555 }
1556 /* Make sure you do not encounter a context switch twice, we can
1557 * encounter it once for the bad context as the start of search
1558 * can point to the context switch */
1559 if (val[i] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
1560 if (ctx_switch) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001561 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001562 "Context switch encountered before bad "
1563 "IB found\n");
1564 break;
1565 }
1566 ctx_switch = true;
1567 }
1568 i = (i + 1) % 2;
1569 if (1 == i)
1570 check = true;
1571 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(temp_rb_rptr,
1572 size);
1573 }
1574 if (!status)
1575 *rb_rptr = temp_rb_rptr;
1576 return status;
1577}
1578
Tarun Karra59e79542013-04-10 10:25:25 -07001579static void adreno_setup_ft_data(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001580 struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001581{
1582 int ret = 0;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001583 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1584 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001585 struct kgsl_context *context;
1586 struct adreno_context *adreno_context;
1587 unsigned int rb_rptr = rb->wptr * sizeof(unsigned int);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001588
Tarun Karrad20d71a2013-01-25 15:38:57 -08001589 memset(ft_data, 0, sizeof(*ft_data));
1590 ft_data->start_of_replay_cmds = 0xFFFFFFFF;
1591 ft_data->replay_for_snapshot = 0xFFFFFFFF;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001592
Tarun Karrad20d71a2013-01-25 15:38:57 -08001593 adreno_regread(device, REG_CP_IB1_BASE, &ft_data->ib1);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001594
Tarun Karrad20d71a2013-01-25 15:38:57 -08001595 kgsl_sharedmem_readl(&device->memstore, &ft_data->context_id,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001596 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1597 current_context));
1598
1599 kgsl_sharedmem_readl(&device->memstore,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001600 &ft_data->global_eop,
Tarun Karradeeecc02013-01-21 23:42:17 -08001601 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1602 eoptimestamp));
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001603
Tarun Karra782734f2013-03-27 20:21:02 -07001604 /* Ensure context id and global eop ts read complete */
1605 rmb();
1606
Tarun Karrad20d71a2013-01-25 15:38:57 -08001607 ft_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1608 if (!ft_data->rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001609 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1610 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001611 return;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001612 }
1613
Tarun Karrad20d71a2013-01-25 15:38:57 -08001614 ft_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1615 if (!ft_data->bad_rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001616 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1617 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001618 return;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001619 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001620
Tarun Karrad20d71a2013-01-25 15:38:57 -08001621 ft_data->good_rb_buffer = vmalloc(rb->buffer_desc.size);
1622 if (!ft_data->good_rb_buffer) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001623 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1624 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001625 return;
Tarun Karradeeecc02013-01-21 23:42:17 -08001626 }
Tarun Karra3164fb02013-02-05 15:38:51 -08001627
Tarun Karra59e79542013-04-10 10:25:25 -07001628 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001629
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001630 /* find the start of bad command sequence in rb */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001631 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001632 /* Look for the command stream that is right after the global eop */
1633
1634 if (!context) {
1635 /*
Tarun Karrad20d71a2013-01-25 15:38:57 -08001636 * If there is no context then fault tolerance does not need to
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001637 * replay anything, just reset GPU and thats it
1638 */
Tarun Karra59e79542013-04-10 10:25:25 -07001639 return;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001640 }
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001641
Tarun Karra59e79542013-04-10 10:25:25 -07001642 ft_data->ft_policy = adreno_dev->ft_policy;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001643
Tarun Karra3164fb02013-02-05 15:38:51 -08001644 if (!adreno_dev->ft_policy)
1645 adreno_dev->ft_policy = KGSL_FT_DEFAULT_POLICY;
1646
Tarun Karra59e79542013-04-10 10:25:25 -07001647 ret = _find_cmd_seq_after_eop_ts(rb, &rb_rptr,
1648 ft_data->global_eop + 1, false);
1649 if (ret) {
1650 ft_data->ft_policy |= KGSL_FT_TEMP_DISABLE;
1651 return;
1652 } else
1653 ft_data->ft_policy &= ~KGSL_FT_TEMP_DISABLE;
Tarun Karra3164fb02013-02-05 15:38:51 -08001654
Tarun Karra59e79542013-04-10 10:25:25 -07001655 ft_data->start_of_replay_cmds = rb_rptr;
Tarun Karra3164fb02013-02-05 15:38:51 -08001656
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001657 adreno_context = context->devctxt;
1658 if (adreno_context->flags & CTXT_FLAGS_PREAMBLE) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001659 if (ft_data->ib1) {
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001660 ret = _find_hanging_ib_sequence(rb,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001661 &rb_rptr, ft_data->ib1);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001662 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001663 KGSL_FT_ERR(device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001664 "Start not found for replay IB sequence\n");
Tarun Karra59e79542013-04-10 10:25:25 -07001665 return;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001666 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001667 ft_data->start_of_replay_cmds = rb_rptr;
1668 ft_data->replay_for_snapshot = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001669 }
1670 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001671}
1672
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001673static int
Tarun Karra696f89e2013-01-27 21:31:40 -08001674_adreno_check_long_ib(struct kgsl_device *device)
1675{
1676 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1677 unsigned int curr_global_ts = 0;
1678
1679 /* check if the global ts is still the same */
1680 kgsl_sharedmem_readl(&device->memstore,
1681 &curr_global_ts,
1682 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1683 eoptimestamp));
Tarun Karra782734f2013-03-27 20:21:02 -07001684 /* Ensure above read is finished before long ib check */
1685 rmb();
Tarun Karra696f89e2013-01-27 21:31:40 -08001686
1687 /* Mark long ib as handled */
1688 adreno_dev->long_ib = 0;
1689
1690 if (curr_global_ts == adreno_dev->long_ib_ts) {
1691 KGSL_FT_ERR(device,
1692 "IB ran too long, invalidate ctxt\n");
1693 return 1;
1694 } else {
1695 /* Do nothing GPU has gone ahead */
1696 KGSL_FT_INFO(device, "false long ib detection return\n");
1697 return 0;
1698 }
1699}
1700
1701static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001702_adreno_ft_restart_device(struct kgsl_device *device,
Tarun Karra59e79542013-04-10 10:25:25 -07001703 struct kgsl_context *context)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001704{
Tarun Karradeeecc02013-01-21 23:42:17 -08001705
1706 struct adreno_context *adreno_context = context->devctxt;
1707
1708 /* restart device */
1709 if (adreno_stop(device)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001710 KGSL_FT_ERR(device, "Device stop failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001711 return 1;
1712 }
1713
1714 if (adreno_start(device, true)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001715 KGSL_FT_ERR(device, "Device start failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001716 return 1;
1717 }
1718
1719 if (context)
1720 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1721 KGSL_MEMSTORE_GLOBAL);
1722
1723 /* If iommu is used then we need to make sure that the iommu clocks
1724 * are on since there could be commands in pipeline that touch iommu */
1725 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1726 if (kgsl_mmu_enable_clk(&device->mmu,
1727 KGSL_IOMMU_CONTEXT_USER))
1728 return 1;
1729 }
1730
1731 return 0;
1732}
1733
Tarun Karra696f89e2013-01-27 21:31:40 -08001734static inline void
1735_adreno_debug_ft_info(struct kgsl_device *device,
1736 struct adreno_ft_data *ft_data)
1737{
1738
1739 /*
1740 * Dumping rb is a very useful tool to debug FT.
1741 * It will tell us if we are extracting the rb correctly
1742 * NOP'ing the right IB, skipping the EOF correctly etc.
1743 */
1744 if (device->ft_log >= 7) {
1745
1746 /* Print fault tolerance data here */
1747 KGSL_FT_INFO(device, "Temp RB buffer size 0x%X\n",
1748 ft_data->rb_size);
1749 adreno_dump_rb(device, ft_data->rb_buffer,
1750 ft_data->rb_size<<2, 0, ft_data->rb_size);
1751
1752 KGSL_FT_INFO(device, "Bad RB buffer size 0x%X\n",
1753 ft_data->bad_rb_size);
1754 adreno_dump_rb(device, ft_data->bad_rb_buffer,
1755 ft_data->bad_rb_size<<2, 0, ft_data->bad_rb_size);
1756
1757 KGSL_FT_INFO(device, "Good RB buffer size 0x%X\n",
1758 ft_data->good_rb_size);
1759 adreno_dump_rb(device, ft_data->good_rb_buffer,
1760 ft_data->good_rb_size<<2, 0, ft_data->good_rb_size);
1761
1762 }
1763}
1764
Tarun Karradeeecc02013-01-21 23:42:17 -08001765static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001766_adreno_ft_resubmit_rb(struct kgsl_device *device,
Tarun Karradeeecc02013-01-21 23:42:17 -08001767 struct adreno_ringbuffer *rb,
1768 struct kgsl_context *context,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001769 struct adreno_ft_data *ft_data,
Tarun Karradeeecc02013-01-21 23:42:17 -08001770 unsigned int *buff, unsigned int size)
1771{
1772 unsigned int ret = 0;
Tarun Karra59e79542013-04-10 10:25:25 -07001773 unsigned int retry_num = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001774
Tarun Karra696f89e2013-01-27 21:31:40 -08001775 _adreno_debug_ft_info(device, ft_data);
1776
Tarun Karra59e79542013-04-10 10:25:25 -07001777 do {
1778 ret = _adreno_ft_restart_device(device, context);
1779 if (ret == 0)
1780 break;
1781 /*
1782 * If device restart fails sleep for 20ms before
1783 * attempting restart. This allows GPU HW to settle
1784 * and improve the chances of next restart to be
1785 * successful.
1786 */
1787 msleep(20);
1788 KGSL_FT_ERR(device, "Retry device restart %d\n", retry_num);
1789 retry_num++;
1790 } while (retry_num < 4);
1791
1792 if (ret) {
1793 KGSL_FT_ERR(device, "Device restart failed\n");
1794 BUG_ON(1);
1795 goto done;
1796 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001797
1798 if (size) {
1799
1800 /* submit commands and wait for them to pass */
1801 adreno_ringbuffer_restore(rb, buff, size);
1802
1803 ret = adreno_idle(device);
1804 }
1805
Tarun Karra59e79542013-04-10 10:25:25 -07001806done:
Tarun Karradeeecc02013-01-21 23:42:17 -08001807 return ret;
1808}
1809
Tarun Karradeeecc02013-01-21 23:42:17 -08001810static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001811_adreno_ft(struct kgsl_device *device,
1812 struct adreno_ft_data *ft_data)
Tarun Karradeeecc02013-01-21 23:42:17 -08001813{
1814 int ret = 0, i;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001815 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1816 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1817 struct kgsl_context *context;
1818 struct adreno_context *adreno_context = NULL;
1819 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
Tarun Karra59e79542013-04-10 10:25:25 -07001820 unsigned int long_ib = 0;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001821
Tarun Karrad20d71a2013-01-25 15:38:57 -08001822 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001823 if (context == NULL) {
Tarun Karra59e79542013-04-10 10:25:25 -07001824 KGSL_FT_ERR(device, "Last context unknown id:%d\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001825 ft_data->context_id);
Tarun Karra59e79542013-04-10 10:25:25 -07001826 goto play_good_cmds;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001827 } else {
1828 adreno_context = context->devctxt;
1829 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001830 /*
1831 * set the invalid ts flag to 0 for this context since we have
1832 * detected a hang for it
1833 */
1834 context->wait_on_invalid_ts = false;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001835
Tarun Karra59e79542013-04-10 10:25:25 -07001836 if (!(adreno_context->flags & CTXT_FLAGS_PER_CONTEXT_TS)) {
1837 ft_data->status = 1;
1838 KGSL_FT_ERR(device, "Fault tolerance not supported\n");
1839 goto play_good_cmds;
1840 }
1841
Tarun Karra83297222013-02-05 19:45:49 -08001842 /*
1843 * This flag will be set by userspace for contexts
1844 * that do not want to be fault tolerant (ex: OPENCL)
1845 */
1846 if (adreno_context->flags & CTXT_FLAGS_NO_FAULT_TOLERANCE) {
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001847 ft_data->status = 1;
Tarun Karra83297222013-02-05 19:45:49 -08001848 KGSL_FT_ERR(device,
1849 "No FT set for this context play good cmds\n");
1850 goto play_good_cmds;
1851 }
1852
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001853 }
1854
Tarun Karra59e79542013-04-10 10:25:25 -07001855 /* Check if we detected a long running IB,
1856 * if true do not attempt replay of bad cmds */
1857 if (adreno_dev->long_ib) {
1858 long_ib = _adreno_check_long_ib(device);
1859 if (!long_ib) {
1860 adreno_context->flags &= ~CTXT_FLAGS_GPU_HANG;
1861 return 0;
1862 }
1863 }
1864
Tarun Karra3164fb02013-02-05 15:38:51 -08001865 /*
1866 * Extract valid contents from rb which can still be executed after
1867 * hang
1868 */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001869 adreno_ringbuffer_extract(rb, ft_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001870
Tarun Karra59e79542013-04-10 10:25:25 -07001871 /* If long IB detected do not attempt replay of bad cmds */
1872 if (long_ib) {
1873 ft_data->status = 1;
1874 _adreno_debug_ft_info(device, ft_data);
1875 goto play_good_cmds;
1876 }
1877
1878 if ((ft_data->ft_policy & KGSL_FT_DISABLE) ||
1879 (ft_data->ft_policy & KGSL_FT_TEMP_DISABLE)) {
1880 KGSL_FT_ERR(device, "NO FT policy play only good cmds\n");
1881 ft_data->status = 1;
1882 goto play_good_cmds;
Tarun Karra696f89e2013-01-27 21:31:40 -08001883 }
1884
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001885 /* Do not try the reply if hang is due to a pagefault */
1886 if (adreno_context->pagefault) {
1887 if ((ft_data->context_id == adreno_context->id) &&
1888 (ft_data->global_eop == adreno_context->pagefault_ts)) {
1889 ft_data->ft_policy &= ~KGSL_FT_REPLAY;
1890 KGSL_FT_ERR(device, "MMU fault skipping replay\n");
1891 }
1892
1893 adreno_context->pagefault = 0;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001894 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001895
Tarun Karra3164fb02013-02-05 15:38:51 -08001896 if (ft_data->ft_policy & KGSL_FT_REPLAY) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001897 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1898 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001899
Tarun Karra3164fb02013-02-05 15:38:51 -08001900 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001901 KGSL_FT_ERR(device, "Replay status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001902 ft_data->status = 1;
1903 } else
Tarun Karradeeecc02013-01-21 23:42:17 -08001904 goto play_good_cmds;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001905 }
1906
Tarun Karra3164fb02013-02-05 15:38:51 -08001907 if (ft_data->ft_policy & KGSL_FT_SKIPIB) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001908 for (i = 0; i < ft_data->bad_rb_size; i++) {
1909 if ((ft_data->bad_rb_buffer[i] ==
1910 CP_HDR_INDIRECT_BUFFER_PFD) &&
1911 (ft_data->bad_rb_buffer[i+1] == ft_data->ib1)) {
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001912
Tarun Karrad20d71a2013-01-25 15:38:57 -08001913 ft_data->bad_rb_buffer[i] = cp_nop_packet(2);
1914 ft_data->bad_rb_buffer[i+1] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001915 KGSL_NOP_IB_IDENTIFIER;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001916 ft_data->bad_rb_buffer[i+2] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001917 KGSL_NOP_IB_IDENTIFIER;
1918 break;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001919 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001920 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001921
Tarun Karrad20d71a2013-01-25 15:38:57 -08001922 if ((i == (ft_data->bad_rb_size)) || (!ft_data->ib1)) {
1923 KGSL_FT_ERR(device, "Bad IB to NOP not found\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001924 ft_data->status = 1;
Tarun Karradeeecc02013-01-21 23:42:17 -08001925 goto play_good_cmds;
1926 }
1927
Tarun Karrad20d71a2013-01-25 15:38:57 -08001928 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1929 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001930
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001931 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001932 KGSL_FT_ERR(device, "NOP faulty IB status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001933 ft_data->status = 1;
1934 } else {
1935 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001936 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001937 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001938 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001939
Tarun Karra3164fb02013-02-05 15:38:51 -08001940 if (ft_data->ft_policy & KGSL_FT_SKIPFRAME) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001941 for (i = 0; i < ft_data->bad_rb_size; i++) {
1942 if (ft_data->bad_rb_buffer[i] ==
1943 KGSL_END_OF_FRAME_IDENTIFIER) {
1944 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001945 break;
1946 }
1947 }
1948
1949 /* EOF not found in RB, discard till EOF in
1950 next IB submission */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001951 if (i == ft_data->bad_rb_size) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001952 adreno_context->flags |= CTXT_FLAGS_SKIP_EOF;
Tarun Karra3164fb02013-02-05 15:38:51 -08001953 KGSL_FT_INFO(device,
1954 "EOF not found in RB, skip next issueib till EOF\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08001955 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001956 }
1957
Tarun Karrad20d71a2013-01-25 15:38:57 -08001958 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1959 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001960
1961 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001962 KGSL_FT_ERR(device, "Skip EOF status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001963 ft_data->status = 1;
1964 } else {
1965 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001966 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001967 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001968 }
1969
1970play_good_cmds:
1971
Tarun Karra3164fb02013-02-05 15:38:51 -08001972 if (ft_data->status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001973 KGSL_FT_ERR(device, "Bad context commands failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001974 else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001975 KGSL_FT_INFO(device, "Bad context commands success\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001976
1977 if (adreno_context) {
1978 adreno_context->flags = (adreno_context->flags &
Tarun Karrad20d71a2013-01-25 15:38:57 -08001979 ~CTXT_FLAGS_GPU_HANG) | CTXT_FLAGS_GPU_HANG_FT;
Tarun Karradeeecc02013-01-21 23:42:17 -08001980 }
1981 adreno_dev->drawctxt_active = last_active_ctx;
1982 }
1983
Tarun Karrad20d71a2013-01-25 15:38:57 -08001984 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1985 ft_data->good_rb_buffer, ft_data->good_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001986
1987 if (ret) {
1988 /* If we fail here we can try to invalidate another
1989 * context and try fault tolerance again */
1990 ret = -EAGAIN;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001991 KGSL_FT_ERR(device, "Playing good commands unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001992 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001993 } else
1994 KGSL_FT_INFO(device, "Playing good commands successful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001995
1996 /* ringbuffer now has data from the last valid context id,
1997 * so restore the active_ctx to the last valid context */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001998 if (ft_data->last_valid_ctx_id) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001999 struct kgsl_context *last_ctx =
2000 idr_find(&device->context_idr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08002001 ft_data->last_valid_ctx_id);
Tarun Karradeeecc02013-01-21 23:42:17 -08002002 if (last_ctx)
2003 adreno_dev->drawctxt_active = last_ctx->devctxt;
2004 }
2005
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002006done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06002007 /* Turn off iommu clocks */
2008 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
2009 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002010 return ret;
2011}
2012
2013static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002014adreno_ft(struct kgsl_device *device,
2015 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002016{
2017 int ret = 0;
2018 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2019 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2020 unsigned int timestamp;
2021
Tarun Karrad20d71a2013-01-25 15:38:57 -08002022 KGSL_FT_INFO(device,
2023 "Start Parameters: IB1: 0x%X, "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002024 "Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08002025 ft_data->ib1, ft_data->context_id, ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002026
2027 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
Tarun Karrad20d71a2013-01-25 15:38:57 -08002028 KGSL_FT_INFO(device, "Last issued global timestamp: %x\n", timestamp);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002029
2030 /* We may need to replay commands multiple times based on whether
2031 * multiple contexts hang the GPU */
2032 while (true) {
Tarun Karradeeecc02013-01-21 23:42:17 -08002033
Tarun Karrad20d71a2013-01-25 15:38:57 -08002034 ret = _adreno_ft(device, ft_data);
2035
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002036 if (-EAGAIN == ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002037 /* setup new fault tolerance parameters and retry, this
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002038 * means more than 1 contexts are causing hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08002039 adreno_destroy_ft_data(ft_data);
Tarun Karra59e79542013-04-10 10:25:25 -07002040 adreno_setup_ft_data(device, ft_data);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002041 KGSL_FT_INFO(device,
2042 "Retry. Parameters: "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002043 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08002044 ft_data->ib1, ft_data->context_id,
2045 ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002046 } else {
2047 break;
2048 }
2049 }
2050
2051 if (ret)
2052 goto done;
2053
Tarun Karrad20d71a2013-01-25 15:38:57 -08002054 /* Restore correct states after fault tolerance */
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002055 if (adreno_dev->drawctxt_active)
2056 device->mmu.hwpagetable =
2057 adreno_dev->drawctxt_active->pagetable;
2058 else
2059 device->mmu.hwpagetable = device->mmu.defaultpagetable;
2060 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
2061 kgsl_sharedmem_writel(&device->memstore,
2062 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2063 eoptimestamp),
2064 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07002065
2066 /* switch to NULL ctxt */
2067 if (adreno_dev->drawctxt_active != NULL)
2068 adreno_drawctxt_switch(adreno_dev, NULL, 0);
2069
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002070done:
2071 adreno_set_max_ts_for_bad_ctxs(device);
2072 adreno_mark_context_status(device, ret);
Tarun Karra3164fb02013-02-05 15:38:51 -08002073 KGSL_FT_ERR(device, "policy 0x%X status 0x%x\n",
2074 ft_data->ft_policy, ret);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002075 return ret;
2076}
2077
2078int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002079adreno_dump_and_exec_ft(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002080{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002081 int result = -ETIMEDOUT;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002082 struct adreno_ft_data ft_data;
Tarun Karra696f89e2013-01-27 21:31:40 -08002083 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karrad2326b82013-01-28 19:09:41 -08002084 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2085 unsigned int curr_pwrlevel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002086
2087 if (device->state == KGSL_STATE_HUNG)
2088 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002089 if (device->state == KGSL_STATE_DUMP_AND_FT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002090 mutex_unlock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002091 wait_for_completion(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002092 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07002093 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002094 result = 0;
2095 } else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002096 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_FT);
2097 INIT_COMPLETION(device->ft_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002098 /* Detected a hang */
2099
Tarun Karrad2326b82013-01-28 19:09:41 -08002100 /* Run fault tolerance at max power level */
2101 curr_pwrlevel = pwr->active_pwrlevel;
2102 kgsl_pwrctrl_pwrlevel_change(device, pwr->max_pwrlevel);
2103
Tarun Karrad20d71a2013-01-25 15:38:57 -08002104 /* Get the fault tolerance data as soon as hang is detected */
Tarun Karra59e79542013-04-10 10:25:25 -07002105 adreno_setup_ft_data(device, &ft_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002106
2107 /*
Tarun Karra696f89e2013-01-27 21:31:40 -08002108 * If long ib is detected, do not attempt postmortem or
2109 * snapshot, if GPU is still executing commands
2110 * we will get errors
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002111 */
Tarun Karra696f89e2013-01-27 21:31:40 -08002112 if (!adreno_dev->long_ib) {
2113 /*
2114 * Trigger an automatic dump of the state to
2115 * the console
2116 */
2117 kgsl_postmortem_dump(device, 0);
2118
2119 /*
2120 * Make a GPU snapshot. For now, do it after the
2121 * PM dump so we can at least be sure the PM dump
2122 * will work as it always has
2123 */
2124 kgsl_device_snapshot(device, 1);
2125 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002126
Tarun Karra59e79542013-04-10 10:25:25 -07002127 result = adreno_ft(device, &ft_data);
2128 adreno_destroy_ft_data(&ft_data);
Tarun Karra696f89e2013-01-27 21:31:40 -08002129
Tarun Karrad2326b82013-01-28 19:09:41 -08002130 /* restore power level */
2131 kgsl_pwrctrl_pwrlevel_change(device, curr_pwrlevel);
2132
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002133 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002134 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002135 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002136 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002137 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
2138 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08002139 complete_all(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002140 }
2141done:
2142 return result;
2143}
Tarun Karrad20d71a2013-01-25 15:38:57 -08002144EXPORT_SYMBOL(adreno_dump_and_exec_ft);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002145
2146static int adreno_getproperty(struct kgsl_device *device,
2147 enum kgsl_property_type type,
2148 void *value,
2149 unsigned int sizebytes)
2150{
2151 int status = -EINVAL;
2152 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2153
2154 switch (type) {
2155 case KGSL_PROP_DEVICE_INFO:
2156 {
2157 struct kgsl_devinfo devinfo;
2158
2159 if (sizebytes != sizeof(devinfo)) {
2160 status = -EINVAL;
2161 break;
2162 }
2163
2164 memset(&devinfo, 0, sizeof(devinfo));
2165 devinfo.device_id = device->id+1;
2166 devinfo.chip_id = adreno_dev->chip_id;
2167 devinfo.mmu_enabled = kgsl_mmu_enabled();
2168 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06002169 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
2170 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002171
2172 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
2173 0) {
2174 status = -EFAULT;
2175 break;
2176 }
2177 status = 0;
2178 }
2179 break;
2180 case KGSL_PROP_DEVICE_SHADOW:
2181 {
2182 struct kgsl_shadowprop shadowprop;
2183
2184 if (sizebytes != sizeof(shadowprop)) {
2185 status = -EINVAL;
2186 break;
2187 }
2188 memset(&shadowprop, 0, sizeof(shadowprop));
2189 if (device->memstore.hostptr) {
2190 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2191 * anything to mmap().
2192 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07002193 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002194 shadowprop.size = device->memstore.size;
2195 /* GSL needs this to be set, even if it
2196 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002197 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2198 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002199 }
2200 if (copy_to_user(value, &shadowprop,
2201 sizeof(shadowprop))) {
2202 status = -EFAULT;
2203 break;
2204 }
2205 status = 0;
2206 }
2207 break;
2208 case KGSL_PROP_MMU_ENABLE:
2209 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002210 int mmu_prop = kgsl_mmu_enabled();
2211
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002212 if (sizebytes != sizeof(int)) {
2213 status = -EINVAL;
2214 break;
2215 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002216 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002217 status = -EFAULT;
2218 break;
2219 }
2220 status = 0;
2221 }
2222 break;
2223 case KGSL_PROP_INTERRUPT_WAITS:
2224 {
2225 int int_waits = 1;
2226 if (sizebytes != sizeof(int)) {
2227 status = -EINVAL;
2228 break;
2229 }
2230 if (copy_to_user(value, &int_waits, sizeof(int))) {
2231 status = -EFAULT;
2232 break;
2233 }
2234 status = 0;
2235 }
2236 break;
2237 default:
2238 status = -EINVAL;
2239 }
2240
2241 return status;
2242}
2243
Jordan Crousef7370f82012-04-18 09:31:07 -06002244static int adreno_setproperty(struct kgsl_device *device,
2245 enum kgsl_property_type type,
2246 void *value,
2247 unsigned int sizebytes)
2248{
2249 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08002250 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06002251
2252 switch (type) {
2253 case KGSL_PROP_PWRCTRL: {
2254 unsigned int enable;
2255 struct kgsl_device_platform_data *pdata =
2256 kgsl_device_get_drvdata(device);
2257
2258 if (sizebytes != sizeof(enable))
2259 break;
2260
2261 if (copy_from_user(&enable, (void __user *) value,
2262 sizeof(enable))) {
2263 status = -EFAULT;
2264 break;
2265 }
2266
2267 if (enable) {
2268 if (pdata->nap_allowed)
2269 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002270 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002271 kgsl_pwrscale_enable(device);
2272 } else {
2273 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002274 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002275 kgsl_pwrscale_disable(device);
2276 }
2277
2278 status = 0;
2279 }
2280 break;
2281 default:
2282 break;
2283 }
2284
2285 return status;
2286}
2287
Jordan Crousea29a2e02012-08-14 09:09:23 -06002288static int adreno_ringbuffer_drain(struct kgsl_device *device,
2289 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002290{
2291 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2292 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002293 unsigned long wait;
2294 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2295
2296 if (!(rb->flags & KGSL_FLAGS_STARTED))
2297 return 0;
2298
2299 /*
2300 * The first time into the loop, wait for 100 msecs and kick wptr again
2301 * to ensure that the hardware has updated correctly. After that, kick
2302 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2303 * expires
2304 */
2305
2306 wait = jiffies + msecs_to_jiffies(100);
2307
Jordan Crousea29a2e02012-08-14 09:09:23 -06002308 do {
2309 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002310 /* Check to see if the core is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002311 if (adreno_ft_detect(device, regs))
Jordan Crousea29a2e02012-08-14 09:09:23 -06002312 return -ETIMEDOUT;
2313
2314 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2315 }
2316 GSL_RB_GET_READPTR(rb, &rb->rptr);
2317
2318 if (time_after(jiffies, timeout)) {
2319 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2320 rb->rptr, rb->wptr);
2321 return -ETIMEDOUT;
2322 }
2323 } while (rb->rptr != rb->wptr);
2324
2325 return 0;
2326}
2327
2328/* Caller must hold the device mutex. */
2329int adreno_idle(struct kgsl_device *device)
2330{
2331 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002332 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302333 unsigned long wait_time;
2334 unsigned long wait_time_part;
Tarun Karra696f89e2013-01-27 21:31:40 -08002335 unsigned int prev_reg_val[ft_detect_regs_count];
Tarun Karra3335f142012-06-19 14:11:48 -07002336
2337 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002338
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002339 kgsl_cffdump_regpoll(device->id,
2340 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002341 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002342
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002343retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002344 /* First, wait for the ringbuffer to drain */
2345 if (adreno_ringbuffer_drain(device, prev_reg_val))
2346 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002347
2348 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002349 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002350 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2351
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002352 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002353 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2354 &rbbm_status);
2355 if (adreno_is_a2xx(adreno_dev)) {
2356 if (rbbm_status == 0x110)
2357 return 0;
2358 } else {
2359 if (!(rbbm_status & 0x80000000))
2360 return 0;
2361 }
Tarun Karra3335f142012-06-19 14:11:48 -07002362
2363 /* Dont wait for timeout, detect hang faster.
2364 */
2365 if (time_after(jiffies, wait_time_part)) {
2366 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002367 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra696f89e2013-01-27 21:31:40 -08002368 if ((adreno_ft_detect(device, prev_reg_val)))
Tarun Karra3335f142012-06-19 14:11:48 -07002369 goto err;
2370 }
2371
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002372 }
2373
2374err:
2375 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08002376 if (KGSL_STATE_DUMP_AND_FT != device->state &&
2377 !adreno_dump_and_exec_ft(device)) {
2378 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002379 goto retry;
2380 }
2381 return -ETIMEDOUT;
2382}
2383
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002384/**
2385 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2386 * rbbm_status register
2387 * @device - Pointer to the GPU device whose idle status is to be
2388 * checked
2389 * @returns - Returns whether the core is idle (based on rbbm_status)
2390 * false if the core is active, true if the core is idle
2391 */
2392static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2393{
2394 unsigned int reg_rbbm_status;
2395 bool status = false;
2396 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2397
2398 /* Is the core idle? */
2399 adreno_regread(device,
2400 adreno_dev->gpudev->reg_rbbm_status,
2401 &reg_rbbm_status);
2402
2403 if (adreno_is_a2xx(adreno_dev)) {
2404 if (reg_rbbm_status == 0x110)
2405 status = true;
2406 } else {
2407 if (!(reg_rbbm_status & 0x80000000))
2408 status = true;
2409 }
2410 return status;
2411}
2412
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002413static unsigned int adreno_isidle(struct kgsl_device *device)
2414{
2415 int status = false;
2416 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2417 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002418
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002419 WARN_ON(device->state == KGSL_STATE_INIT);
2420 /* If the device isn't active, don't force it on. */
2421 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002422 /* Is the ring buffer is empty? */
2423 GSL_RB_GET_READPTR(rb, &rb->rptr);
2424 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
Jordan Crouseab601992013-03-05 11:18:20 -07002425 /*
2426 * Are there interrupts pending? If so then pretend we
2427 * are not idle - this avoids the possiblity that we go
2428 * to a lower power state without handling interrupts
2429 * first.
2430 */
2431
2432 if (!adreno_dev->gpudev->irq_pending(adreno_dev)) {
2433 /* Is the core idle? */
2434 status = is_adreno_rbbm_status_idle(device);
2435 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002436 }
2437 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002438 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002439 }
2440 return status;
2441}
2442
2443/* Caller must hold the device mutex. */
2444static int adreno_suspend_context(struct kgsl_device *device)
2445{
2446 int status = 0;
2447 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2448
2449 /* switch to NULL ctxt */
2450 if (adreno_dev->drawctxt_active != NULL) {
2451 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002452 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002453 }
2454
2455 return status;
2456}
2457
Jordan Crouse233b2092012-04-18 09:31:09 -06002458/* Find a memory structure attached to an adreno context */
2459
2460struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2461 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2462{
2463 struct kgsl_context *context;
2464 struct adreno_context *adreno_context = NULL;
2465 int next = 0;
2466
2467 while (1) {
2468 context = idr_get_next(&device->context_idr, &next);
2469 if (context == NULL)
2470 break;
2471
2472 adreno_context = (struct adreno_context *)context->devctxt;
2473
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002474 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2475 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002476 struct kgsl_memdesc *desc;
2477
2478 desc = &adreno_context->gpustate;
2479 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2480 return desc;
2481
2482 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2483 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2484 return desc;
2485 }
2486 next = next + 1;
2487 }
2488
2489 return NULL;
2490}
2491
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002492struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002493 unsigned int pt_base,
2494 unsigned int gpuaddr,
2495 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002496{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002497 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002498 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2499 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2500
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002501 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2502 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002503
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002504 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2505 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002506
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002507 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2508 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002509
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002510 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2511 size))
2512 return &device->mmu.setstate_memory;
2513
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002514 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002515
2516 if (entry)
2517 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002518
Jordan Crouse233b2092012-04-18 09:31:09 -06002519 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002520}
2521
2522uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2523 unsigned int gpuaddr, unsigned int size)
2524{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002525 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002526
2527 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2528
2529 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002530}
2531
2532void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2533 unsigned int *value)
2534{
2535 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002536 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2537 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002538
2539 if (!in_interrupt())
2540 kgsl_pre_hwaccess(device);
2541
2542 /*ensure this read finishes before the next one.
2543 * i.e. act like normal readl() */
2544 *value = __raw_readl(reg);
2545 rmb();
2546}
2547
2548void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2549 unsigned int value)
2550{
2551 unsigned int *reg;
2552
Jordan Crouse7501d452012-04-19 08:58:44 -06002553 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002554
2555 if (!in_interrupt())
2556 kgsl_pre_hwaccess(device);
2557
Jordan Crousef7b81e82013-05-28 17:03:00 -06002558 trace_kgsl_regwrite(device, offsetwords, value);
2559
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002560 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002561 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002562
2563 /*ensure previous writes post before this one,
2564 * i.e. act like normal writel() */
2565 wmb();
2566 __raw_writel(value, reg);
2567}
2568
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002569static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2570{
2571 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002572 if (k_ctxt != NULL) {
2573 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002574 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2575 context_id = KGSL_CONTEXT_INVALID;
2576 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2577 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002578 }
2579
2580 return context_id;
2581}
2582
Carter Cooper740f6742013-01-03 16:19:23 -07002583static unsigned int adreno_check_hw_ts(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002584 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002585{
Carter Cooper740f6742013-01-03 16:19:23 -07002586 int status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002587 unsigned int ref_ts, enableflag;
Carter Cooper740f6742013-01-03 16:19:23 -07002588 unsigned int context_id = _get_context_id(context);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002589
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002590 /*
2591 * If the context ID is invalid, we are in a race with
2592 * the context being destroyed by userspace so bail.
2593 */
2594 if (context_id == KGSL_CONTEXT_INVALID) {
2595 KGSL_DRV_WARN(device, "context was detached");
Carter Cooper740f6742013-01-03 16:19:23 -07002596 return -EINVAL;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002597 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002598
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002599 status = kgsl_check_timestamp(device, context, timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002600 if (status)
2601 return status;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002602
Carter Cooper740f6742013-01-03 16:19:23 -07002603 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2604 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2605 /*
2606 * Barrier is needed here to make sure the read from memstore
2607 * has posted
2608 */
2609
2610 mb();
2611
2612 if (enableflag) {
2613 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002614 KGSL_MEMSTORE_OFFSET(context_id,
2615 ref_wait_ts));
Carter Cooper740f6742013-01-03 16:19:23 -07002616
2617 /* Make sure the memstore read has posted */
2618 mb();
2619 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
2620 kgsl_sharedmem_writel(&device->memstore,
2621 KGSL_MEMSTORE_OFFSET(context_id,
2622 ref_wait_ts), timestamp);
2623 /* Make sure the memstore write is posted */
2624 wmb();
2625 }
2626 } else {
2627 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002628 KGSL_MEMSTORE_OFFSET(context_id,
2629 ref_wait_ts), timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002630 enableflag = 1;
2631 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002632 KGSL_MEMSTORE_OFFSET(context_id,
2633 ts_cmp_enable), enableflag);
Carter Cooper740f6742013-01-03 16:19:23 -07002634 /* Make sure the memstore write gets posted */
2635 wmb();
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002636
Carter Cooper740f6742013-01-03 16:19:23 -07002637 /*
2638 * submit a dummy packet so that even if all
2639 * commands upto timestamp get executed we will still
2640 * get an interrupt
2641 */
2642
2643 if (context && device->state != KGSL_STATE_SLUMBER)
2644 adreno_ringbuffer_issuecmds(device, context->devctxt,
2645 KGSL_CMD_FLAGS_NONE, NULL, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002646 }
Carter Cooper740f6742013-01-03 16:19:23 -07002647
2648 return 0;
2649}
2650
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002651/* Return 1 if the event timestmp has already passed, 0 if it was marked */
2652static int adreno_next_event(struct kgsl_device *device,
Carter Cooper740f6742013-01-03 16:19:23 -07002653 struct kgsl_event *event)
2654{
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002655 return adreno_check_hw_ts(device, event->context, event->timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002656}
2657
2658static int adreno_check_interrupt_timestamp(struct kgsl_device *device,
2659 struct kgsl_context *context, unsigned int timestamp)
2660{
2661 int status;
2662
2663 mutex_lock(&device->mutex);
2664 status = adreno_check_hw_ts(device, context, timestamp);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002665 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002666
2667 return status;
2668}
2669
2670/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002671 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002672 placing a process in wait q. For conditional interrupts we expect the
2673 process to already be in its wait q when its exit condition checking
2674 function is called.
2675*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002676#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002677({ \
2678 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002679 if (io) \
2680 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2681 else \
2682 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002683 __ret; \
2684})
2685
Tarun Karra3335f142012-06-19 14:11:48 -07002686
2687
Tarun Karra696f89e2013-01-27 21:31:40 -08002688unsigned int adreno_ft_detect(struct kgsl_device *device,
Tarun Karra3335f142012-06-19 14:11:48 -07002689 unsigned int *prev_reg_val)
2690{
2691 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karra696f89e2013-01-27 21:31:40 -08002692 unsigned int curr_reg_val[ft_detect_regs_count];
2693 unsigned int fast_hang_detected = 1;
2694 unsigned int long_ib_detected = 1;
Tarun Karra3335f142012-06-19 14:11:48 -07002695 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002696 static unsigned long next_hang_detect_time;
Tarun Karra696f89e2013-01-27 21:31:40 -08002697 static unsigned int prev_global_ts;
2698 unsigned int curr_global_ts = 0;
2699 unsigned int curr_context_id = 0;
2700 static struct adreno_context *curr_context;
2701 static struct kgsl_context *context;
Tarun Karra3335f142012-06-19 14:11:48 -07002702
2703 if (!adreno_dev->fast_hang_detect)
Tarun Karra696f89e2013-01-27 21:31:40 -08002704 fast_hang_detected = 0;
2705
2706 if (!adreno_dev->long_ib_detect)
2707 long_ib_detected = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002708
Jordan Crousecca61142012-11-20 10:54:24 -07002709 if (is_adreno_rbbm_status_idle(device)) {
2710
2711 /*
2712 * On A20X if the RPTR != WPTR and the device is idle, then
2713 * the last write to WPTR probably failed to latch so write it
2714 * again
2715 */
2716
2717 if (adreno_is_a2xx(adreno_dev)) {
2718 unsigned int rptr;
2719 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2720 if (rptr != adreno_dev->ringbuffer.wptr)
2721 adreno_regwrite(device, REG_CP_RB_WPTR,
2722 adreno_dev->ringbuffer.wptr);
2723 }
2724
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002725 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002726 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002727
Tarun Karra4648db92013-02-01 17:03:40 -08002728 /*
2729 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2730 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2731 * from the last time do nothing.
2732 */
2733 if ((next_hang_detect_time) &&
2734 (time_before(jiffies, next_hang_detect_time)))
2735 return 0;
2736 else
2737 next_hang_detect_time = (jiffies +
2738 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2739
Tarun Karra696f89e2013-01-27 21:31:40 -08002740 /* Read the current Hang detect reg values here */
2741 for (i = 0; i < ft_detect_regs_count; i++) {
2742 if (ft_detect_regs[i] == 0)
Jordan Crouseb5c80482012-10-03 09:38:41 -06002743 continue;
Tarun Karra696f89e2013-01-27 21:31:40 -08002744 adreno_regread(device, ft_detect_regs[i],
2745 &curr_reg_val[i]);
Tarun Karra3335f142012-06-19 14:11:48 -07002746 }
2747
Tarun Karra696f89e2013-01-27 21:31:40 -08002748 /* Read the current global timestamp here */
2749 kgsl_sharedmem_readl(&device->memstore,
2750 &curr_global_ts,
2751 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2752 eoptimestamp));
2753
2754 mb();
2755
2756 if (curr_global_ts == prev_global_ts) {
2757
2758 /* Get the current context here */
2759 if (context == NULL) {
2760 kgsl_sharedmem_readl(&device->memstore,
2761 &curr_context_id,
2762 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2763 current_context));
2764 context = idr_find(&device->context_idr,
2765 curr_context_id);
2766 if (context != NULL) {
2767 curr_context = context->devctxt;
2768 curr_context->ib_gpu_time_used = 0;
2769 } else {
2770 KGSL_DRV_ERR(device,
2771 "Fault tolerance no context found\n");
2772 }
2773 }
2774
2775 mb();
2776
2777 if (curr_context != NULL) {
2778
2779 curr_context->ib_gpu_time_used += KGSL_TIMEOUT_PART;
2780 KGSL_FT_INFO(device,
2781 "Proc %s used GPU Time %d ms on timestamp 0x%X\n",
2782 curr_context->pid_name, curr_context->ib_gpu_time_used,
2783 curr_global_ts+1);
2784
2785 for (i = 0; i < ft_detect_regs_count; i++) {
2786 if (curr_reg_val[i] != prev_reg_val[i]) {
2787 fast_hang_detected = 0;
2788
2789 /* Check for long IB here */
2790 if ((i >=
2791 LONG_IB_DETECT_REG_INDEX_START)
2792 &&
2793 (i <=
2794 LONG_IB_DETECT_REG_INDEX_END))
2795 long_ib_detected = 0;
2796 }
2797 }
2798
2799 if (fast_hang_detected) {
2800 KGSL_FT_ERR(device,
2801 "Proc %s, ctxt_id %d ts %d triggered fault tolerance"
2802 " on global ts %d\n",
2803 curr_context->pid_name, curr_context->id
2804 , (kgsl_readtimestamp(device, context,
2805 KGSL_TIMESTAMP_RETIRED)+1),
2806 curr_global_ts+1);
2807 return 1;
2808 }
2809
Tarun Karra83297222013-02-05 19:45:49 -08002810 if ((long_ib_detected) &&
2811 (!(curr_context->flags &
2812 CTXT_FLAGS_NO_FAULT_TOLERANCE))) {
Tarun Karra696f89e2013-01-27 21:31:40 -08002813 curr_context->ib_gpu_time_used +=
2814 KGSL_TIMEOUT_PART;
2815 if (curr_context->ib_gpu_time_used >
2816 KGSL_TIMEOUT_LONG_IB_DETECTION) {
2817 if (adreno_dev->long_ib_ts !=
2818 curr_global_ts) {
2819 KGSL_FT_ERR(device,
2820 "Proc %s, ctxt_id %d ts %d"
2821 "used GPU for %d ms long ib "
2822 "detected on global ts %d\n",
2823 curr_context->pid_name,
2824 curr_context->id,
2825 (kgsl_readtimestamp(device,
2826 context,
2827 KGSL_TIMESTAMP_RETIRED)+1),
2828 curr_context->ib_gpu_time_used,
2829 curr_global_ts+1);
2830 adreno_dev->long_ib = 1;
2831 adreno_dev->long_ib_ts =
2832 curr_global_ts;
2833 curr_context->ib_gpu_time_used =
2834 0;
2835 return 1;
2836 }
2837 }
2838 }
2839 } else {
2840 KGSL_FT_ERR(device,
2841 "Last context unknown id:%d\n",
2842 curr_context_id);
2843 }
2844 } else {
2845 /* GPU is moving forward */
2846 prev_global_ts = curr_global_ts;
2847 context = NULL;
2848 curr_context = NULL;
2849 adreno_dev->long_ib = 0;
2850 adreno_dev->long_ib_ts = 0;
2851 }
2852
2853
2854 /* If hangs are not detected copy the current reg values
2855 * to previous values and return no hang */
2856 for (i = 0; i < ft_detect_regs_count; i++)
2857 prev_reg_val[i] = curr_reg_val[i];
2858 return 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002859}
2860
Jordan Crouse92446a62012-11-15 11:00:06 -07002861/**
2862 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2863 * @device - pointer to a KGSL device structure
2864 * @context - pointer to the active KGSL context
2865 * @timestamp - the timestamp that the process was waiting for
2866 *
Tarun Karrad20d71a2013-01-25 15:38:57 -08002867 * Process a possible GPU hang and try fault tolerance from it
2868 * cleanly
Jordan Crouse92446a62012-11-15 11:00:06 -07002869 */
2870static int adreno_handle_hang(struct kgsl_device *device,
2871 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002872{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002873 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002874 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002875 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002876 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002877
Jordan Crouse92446a62012-11-15 11:00:06 -07002878 /* Do one last check to see if we somehow made it through */
2879 if (kgsl_check_timestamp(device, context, timestamp))
2880 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002881
2882 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002883
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002884 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2885 mb();
2886
Tarun Karra696f89e2013-01-27 21:31:40 -08002887 KGSL_DRV_WARN(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002888 "Device hang detected while waiting for timestamp: "
2889 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002890 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2891 context_id, timestamp, context_id, ts_issued, context_id,
2892 kgsl_readtimestamp(device, context,
2893 KGSL_TIMESTAMP_RETIRED),
2894 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002895
Tarun Karrad20d71a2013-01-25 15:38:57 -08002896 /* Return 0 after a successful fault tolerance */
2897 if (!adreno_dump_and_exec_ft(device))
Jordan Crouse92446a62012-11-15 11:00:06 -07002898 return 0;
2899
2900 return -ETIMEDOUT;
2901}
2902
2903static int _check_pending_timestamp(struct kgsl_device *device,
2904 struct kgsl_context *context, unsigned int timestamp)
2905{
2906 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2907 unsigned int context_id = _get_context_id(context);
2908 unsigned int ts_issued;
2909
2910 if (context_id == KGSL_CONTEXT_INVALID)
2911 return -EINVAL;
2912
2913 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2914
2915 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2916 return 0;
2917
2918 if (context && !context->wait_on_invalid_ts) {
2919 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2920 context_id, timestamp, context_id, ts_issued);
2921
2922 /* Only print this message once */
2923 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002924 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002925
2926 return -EINVAL;
2927}
2928
2929/**
2930 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2931 * @device - pointer to a KGSL device structure
2932 * @context - pointer to the active kgsl context
2933 * @timestamp - GPU timestamp to wait for
2934 * @msecs - amount of time to wait (in milliseconds)
2935 *
2936 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2937 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2938 * one if it happened. Otherwise, spend most of our time in an interruptible
2939 * wait for the timestamp interrupt to be processed. This function must be
2940 * called with the mutex already held.
2941 */
2942static int adreno_waittimestamp(struct kgsl_device *device,
2943 struct kgsl_context *context,
2944 unsigned int timestamp,
2945 unsigned int msecs)
2946{
2947 static unsigned int io_cnt;
2948 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2949 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2950 unsigned int context_id = _get_context_id(context);
Tarun Karra696f89e2013-01-27 21:31:40 -08002951 unsigned int prev_reg_val[ft_detect_regs_count];
Jordan Crouse92446a62012-11-15 11:00:06 -07002952 unsigned int time_elapsed = 0;
2953 unsigned int wait;
2954 int ts_compare = 1;
2955 int io, ret = -ETIMEDOUT;
2956
2957 /* Get out early if the context has already been destroyed */
2958
2959 if (context_id == KGSL_CONTEXT_INVALID) {
2960 KGSL_DRV_WARN(device, "context was detached");
2961 return -EINVAL;
2962 }
2963
2964 /*
2965 * Check to see if the requested timestamp is "newer" then the last
2966 * timestamp issued. If it is complain once and return error. Only
2967 * print the message once per context so that badly behaving
2968 * applications don't spam the logs
2969 */
2970
2971 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2972 if (_check_pending_timestamp(device, context, timestamp))
2973 return -EINVAL;
2974
2975 /* Reset the invalid timestamp flag on a valid wait */
2976 context->wait_on_invalid_ts = false;
2977 }
2978
2979
2980 /* Clear the registers used for hang detection */
2981 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2982
2983 /*
2984 * On the first time through the loop only wait 100ms.
2985 * this gives enough time for the engine to start moving and oddly
2986 * provides better hang detection results than just going the full
2987 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2988 * is if msecs happens to be < 100ms then just use the full timeout
2989 */
2990
2991 wait = 100;
2992
2993 do {
2994 long status;
2995
Jordan Crouse92446a62012-11-15 11:00:06 -07002996 /*
2997 * if the timestamp happens while we're not
2998 * waiting, there's a chance that an interrupt
2999 * will not be generated and thus the timestamp
3000 * work needs to be queued.
3001 */
3002
3003 if (kgsl_check_timestamp(device, context, timestamp)) {
3004 queue_work(device->work_queue, &device->ts_expired_ws);
3005 ret = 0;
3006 break;
3007 }
3008
3009 /* Check to see if the GPU is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08003010 if (adreno_ft_detect(device, prev_reg_val)) {
Jordan Crouse92446a62012-11-15 11:00:06 -07003011 ret = adreno_handle_hang(device, context, timestamp);
3012 break;
3013 }
3014
3015 /*
3016 * For proper power accounting sometimes we need to call
3017 * io_wait_interruptible_timeout and sometimes we need to call
3018 * plain old wait_interruptible_timeout. We call the regular
3019 * timeout N times out of 100, where N is a number specified by
3020 * the current power level
3021 */
3022
3023 io_cnt = (io_cnt + 1) % 100;
3024 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
3025 ? 0 : 1;
3026
3027 mutex_unlock(&device->mutex);
3028
3029 /* Wait for a timestamp event */
3030 status = kgsl_wait_event_interruptible_timeout(
3031 device->wait_queue,
Carter Cooper740f6742013-01-03 16:19:23 -07003032 adreno_check_interrupt_timestamp(device, context,
Jordan Crouse92446a62012-11-15 11:00:06 -07003033 timestamp), msecs_to_jiffies(wait), io);
3034
3035 mutex_lock(&device->mutex);
3036
3037 /*
3038 * If status is non zero then either the condition was satisfied
3039 * or there was an error. In either event, this is the end of
3040 * the line for us
3041 */
3042
3043 if (status != 0) {
3044 ret = (status > 0) ? 0 : (int) status;
3045 break;
3046 }
Jordan Crouse92446a62012-11-15 11:00:06 -07003047 time_elapsed += wait;
3048
3049 /* If user specified timestamps are being used, wait at least
3050 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
3051 * issue a IB for a timestamp before checking to see if the
3052 * current timestamp we are waiting for is valid or not
3053 */
3054
3055 if (ts_compare && (adreno_ctx &&
3056 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
3057 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
3058 ret = _check_pending_timestamp(device, context,
3059 timestamp);
3060 if (ret)
3061 break;
3062
3063 /* Don't do this check again */
3064 ts_compare = 0;
3065
3066 /*
3067 * Reset the invalid timestamp flag on a valid
3068 * wait
3069 */
3070 context->wait_on_invalid_ts = false;
3071 }
3072 }
3073
3074 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003075 * We want to wait the floor of KGSL_TIMEOUT_PART
3076 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07003077 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003078
3079 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
3080 wait = KGSL_TIMEOUT_PART;
3081 else
3082 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07003083
3084 } while (!msecs || time_elapsed < msecs);
3085
3086 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003087}
3088
3089static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003090 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003091{
3092 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003093 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003094
Jeremy Gebben9ad86922012-05-08 15:33:23 -06003095 /*
3096 * If the context ID is invalid, we are in a race with
3097 * the context being destroyed by userspace so bail.
3098 */
3099 if (context_id == KGSL_CONTEXT_INVALID) {
3100 KGSL_DRV_WARN(device, "context was detached");
3101 return timestamp;
3102 }
Jordan Crousec659f382012-04-16 11:10:41 -06003103 switch (type) {
3104 case KGSL_TIMESTAMP_QUEUED: {
3105 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3106 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
3107
3108 timestamp = rb->timestamp[context_id];
3109 break;
3110 }
3111 case KGSL_TIMESTAMP_CONSUMED:
Carter Cooper78d8d362013-05-28 17:01:59 -06003112 kgsl_sharedmem_readl(&device->memstore, &timestamp,
3113 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp));
Jordan Crousec659f382012-04-16 11:10:41 -06003114 break;
3115 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003116 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06003117 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
3118 break;
3119 }
3120
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003121 rmb();
3122
3123 return timestamp;
3124}
3125
3126static long adreno_ioctl(struct kgsl_device_private *dev_priv,
3127 unsigned int cmd, void *data)
3128{
3129 int result = 0;
3130 struct kgsl_drawctxt_set_bin_base_offset *binbase;
3131 struct kgsl_context *context;
3132
3133 switch (cmd) {
3134 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
3135 binbase = data;
3136
3137 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
3138 if (context) {
3139 adreno_drawctxt_set_bin_base_offset(
3140 dev_priv->device, context, binbase->offset);
3141 } else {
3142 result = -EINVAL;
3143 KGSL_DRV_ERR(dev_priv->device,
3144 "invalid drawctxt drawctxt_id %d "
3145 "device_id=%d\n",
3146 binbase->drawctxt_id, dev_priv->device->id);
3147 }
3148 break;
3149
3150 default:
3151 KGSL_DRV_INFO(dev_priv->device,
3152 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07003153 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003154 break;
3155 }
3156 return result;
3157
3158}
3159
3160static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
3161{
3162 gpu_freq /= 1000000;
3163 return ticks / gpu_freq;
3164}
3165
3166static void adreno_power_stats(struct kgsl_device *device,
3167 struct kgsl_power_stats *stats)
3168{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003169 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003170 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003171 unsigned int cycles;
3172
3173 /* Get the busy cycles counted since the counter was last reset */
3174 /* Calling this function also resets and restarts the counter */
3175
3176 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003177
3178 /* In order to calculate idle you have to have run the algorithm *
3179 * at least once to get a start time. */
3180 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003181 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003182 stats->total_time = tmp - pwr->time;
3183 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003184 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003185 pwrlevels[device->pwrctrl.active_pwrlevel].
3186 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003187 } else {
3188 stats->total_time = 0;
3189 stats->busy_time = 0;
3190 pwr->time = ktime_to_us(ktime_get());
3191 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003192}
3193
3194void adreno_irqctrl(struct kgsl_device *device, int state)
3195{
Jordan Crousea78c9172011-07-11 13:14:09 -06003196 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3197 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003198}
3199
Jordan Croused6535882012-06-20 08:22:16 -06003200static unsigned int adreno_gpuid(struct kgsl_device *device,
3201 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07003202{
3203 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3204
Jordan Croused6535882012-06-20 08:22:16 -06003205 /* Some applications need to know the chip ID too, so pass
3206 * that as a parameter */
3207
3208 if (chipid != NULL)
3209 *chipid = adreno_dev->chip_id;
3210
Jordan Crousea0758f22011-12-07 11:19:22 -07003211 /* Standard KGSL gpuid format:
3212 * top word is 0x0002 for 2D or 0x0003 for 3D
3213 * Bottom word is core specific identifer
3214 */
3215
3216 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
3217}
3218
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003219static const struct kgsl_functable adreno_functable = {
3220 /* Mandatory functions */
3221 .regread = adreno_regread,
3222 .regwrite = adreno_regwrite,
3223 .idle = adreno_idle,
3224 .isidle = adreno_isidle,
3225 .suspend_context = adreno_suspend_context,
3226 .start = adreno_start,
3227 .stop = adreno_stop,
3228 .getproperty = adreno_getproperty,
3229 .waittimestamp = adreno_waittimestamp,
3230 .readtimestamp = adreno_readtimestamp,
3231 .issueibcmds = adreno_ringbuffer_issueibcmds,
3232 .ioctl = adreno_ioctl,
3233 .setup_pt = adreno_setup_pt,
3234 .cleanup_pt = adreno_cleanup_pt,
3235 .power_stats = adreno_power_stats,
3236 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07003237 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07003238 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06003239 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003240 /* Optional functions */
3241 .setstate = adreno_setstate,
3242 .drawctxt_create = adreno_drawctxt_create,
3243 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06003244 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06003245 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07003246 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003247};
3248
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003249static struct platform_driver adreno_platform_driver = {
3250 .probe = adreno_probe,
3251 .remove = __devexit_p(adreno_remove),
3252 .suspend = kgsl_suspend_driver,
3253 .resume = kgsl_resume_driver,
3254 .id_table = adreno_id_table,
3255 .driver = {
3256 .owner = THIS_MODULE,
3257 .name = DEVICE_3D_NAME,
3258 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06003259 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003260 }
3261};
3262
3263static int __init kgsl_3d_init(void)
3264{
3265 return platform_driver_register(&adreno_platform_driver);
3266}
3267
3268static void __exit kgsl_3d_exit(void)
3269{
3270 platform_driver_unregister(&adreno_platform_driver);
3271}
3272
3273module_init(kgsl_3d_init);
3274module_exit(kgsl_3d_exit);
3275
3276MODULE_DESCRIPTION("3D Graphics driver");
3277MODULE_VERSION("1.2");
3278MODULE_LICENSE("GPL v2");
3279MODULE_ALIAS("platform:kgsl_3d");