blob: e85bead36a1b9946a369c1df6ef5b10752440c21 [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 Crouseb4d31bd2012-02-01 22:11:12 -07001303 if (status == 0) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001304 /* While fault tolerance is on we do not want timer to
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001305 * fire and attempt to change any device state */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001306 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001307 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001308 return 0;
1309 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001312
1313error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001314 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001315
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001316error_clk_off:
1317 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318
1319 return status;
1320}
1321
1322static int adreno_stop(struct kgsl_device *device)
1323{
1324 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1325
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001326 adreno_dev->drawctxt_active = NULL;
1327
1328 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1329
Shubhraprakash Das79447952012-04-26 18:12:23 -06001330 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001332 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301333 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001334 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001335
liu zhong7dfa2a32012-04-27 19:11:01 -07001336 adreno_ocmem_gmem_free(adreno_dev);
1337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001338 /* Power down the device */
1339 kgsl_pwrctrl_disable(device);
1340
1341 return 0;
1342}
1343
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001344static void adreno_mark_context_status(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001345 int ft_status)
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001346{
1347 struct kgsl_context *context;
1348 int next = 0;
1349 /*
1350 * Set the reset status of all contexts to
1351 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
Tarun Karrad20d71a2013-01-25 15:38:57 -08001352 * since thats the guilty party, if fault tolerance failed then
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001353 * mark all as guilty
1354 */
1355 while ((context = idr_get_next(&device->context_idr, &next))) {
1356 struct adreno_context *adreno_context = context->devctxt;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001357 if (ft_status) {
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001358 context->reset_status =
1359 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1360 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1361 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1362 context->reset_status) {
Carter Cooper8179f5a2012-12-17 11:32:27 -07001363 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG |
Tarun Karrad20d71a2013-01-25 15:38:57 -08001364 CTXT_FLAGS_GPU_HANG_FT))
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001365 context->reset_status =
1366 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1367 else
1368 context->reset_status =
1369 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1370 }
1371 next = next + 1;
1372 }
1373}
1374
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001375static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1376{
1377 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1378 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1379 struct kgsl_context *context;
1380 struct adreno_context *temp_adreno_context;
1381 int next = 0;
1382
1383 while ((context = idr_get_next(&device->context_idr, &next))) {
1384 temp_adreno_context = context->devctxt;
1385 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1386 kgsl_sharedmem_writel(&device->memstore,
1387 KGSL_MEMSTORE_OFFSET(context->id,
1388 soptimestamp),
1389 rb->timestamp[context->id]);
1390 kgsl_sharedmem_writel(&device->memstore,
1391 KGSL_MEMSTORE_OFFSET(context->id,
1392 eoptimestamp),
1393 rb->timestamp[context->id]);
1394 }
1395 next = next + 1;
1396 }
1397}
1398
Tarun Karrad20d71a2013-01-25 15:38:57 -08001399static void adreno_destroy_ft_data(struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001400{
Tarun Karrad20d71a2013-01-25 15:38:57 -08001401 vfree(ft_data->rb_buffer);
1402 vfree(ft_data->bad_rb_buffer);
1403 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001404}
1405
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001406static int _find_start_of_cmd_seq(struct adreno_ringbuffer *rb,
1407 unsigned int *ptr,
1408 bool inc)
1409{
1410 int status = -EINVAL;
1411 unsigned int val1;
1412 unsigned int size = rb->buffer_desc.size;
1413 unsigned int start_ptr = *ptr;
1414
1415 while ((start_ptr / sizeof(unsigned int)) != rb->wptr) {
1416 if (inc)
1417 start_ptr = adreno_ringbuffer_inc_wrapped(start_ptr,
1418 size);
1419 else
1420 start_ptr = adreno_ringbuffer_dec_wrapped(start_ptr,
1421 size);
1422 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, start_ptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001423 /* Ensure above read is finished before next read */
1424 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001425 if (KGSL_CMD_IDENTIFIER == val1) {
1426 if ((start_ptr / sizeof(unsigned int)) != rb->wptr)
1427 start_ptr = adreno_ringbuffer_dec_wrapped(
1428 start_ptr, size);
1429 *ptr = start_ptr;
1430 status = 0;
1431 break;
1432 }
1433 }
1434 return status;
1435}
1436
1437static int _find_cmd_seq_after_eop_ts(struct adreno_ringbuffer *rb,
1438 unsigned int *rb_rptr,
1439 unsigned int global_eop,
1440 bool inc)
1441{
1442 int status = -EINVAL;
1443 unsigned int temp_rb_rptr = *rb_rptr;
1444 unsigned int size = rb->buffer_desc.size;
1445 unsigned int val[3];
1446 int i = 0;
1447 bool check = false;
1448
1449 if (inc && temp_rb_rptr / sizeof(unsigned int) != rb->wptr)
1450 return status;
1451
1452 do {
1453 /*
1454 * when decrementing we need to decrement first and
1455 * then read make sure we cover all the data
1456 */
1457 if (!inc)
1458 temp_rb_rptr = adreno_ringbuffer_dec_wrapped(
1459 temp_rb_rptr, size);
1460 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i],
1461 temp_rb_rptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001462 /* Ensure above read is finished before next read */
1463 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001464
1465 if (check && ((inc && val[i] == global_eop) ||
1466 (!inc && (val[i] ==
1467 cp_type3_packet(CP_MEM_WRITE, 2) ||
1468 val[i] == CACHE_FLUSH_TS)))) {
1469 /* decrement i, i.e i = (i - 1 + 3) % 3 if
1470 * we are going forward, else increment i */
1471 i = (i + 2) % 3;
1472 if (val[i] == rb->device->memstore.gpuaddr +
1473 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1474 eoptimestamp)) {
1475 int j = ((i + 2) % 3);
1476 if ((inc && (val[j] == CACHE_FLUSH_TS ||
1477 val[j] == cp_type3_packet(
1478 CP_MEM_WRITE, 2))) ||
1479 (!inc && val[j] == global_eop)) {
1480 /* Found the global eop */
1481 status = 0;
1482 break;
1483 }
1484 }
1485 /* if no match found then increment i again
1486 * since we decremented before matching */
1487 i = (i + 1) % 3;
1488 }
1489 if (inc)
1490 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(
1491 temp_rb_rptr, size);
1492
1493 i = (i + 1) % 3;
1494 if (2 == i)
1495 check = true;
1496 } while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr);
1497 /* temp_rb_rptr points to the command stream after global eop,
1498 * move backward till the start of command sequence */
1499 if (!status) {
1500 status = _find_start_of_cmd_seq(rb, &temp_rb_rptr, false);
1501 if (!status) {
1502 *rb_rptr = temp_rb_rptr;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001503 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001504 "Offset of cmd sequence after eop timestamp: 0x%x\n",
1505 temp_rb_rptr / sizeof(unsigned int));
1506 }
1507 }
1508 if (status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001509 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001510 "Failed to find the command sequence after eop timestamp\n");
1511 return status;
1512}
1513
1514static int _find_hanging_ib_sequence(struct adreno_ringbuffer *rb,
1515 unsigned int *rb_rptr,
1516 unsigned int ib1)
1517{
1518 int status = -EINVAL;
1519 unsigned int temp_rb_rptr = *rb_rptr;
1520 unsigned int size = rb->buffer_desc.size;
1521 unsigned int val[2];
1522 int i = 0;
1523 bool check = false;
1524 bool ctx_switch = false;
1525
1526 while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr) {
1527 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i], temp_rb_rptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001528 /* Ensure above read is finished before next read */
1529 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001530
1531 if (check && val[i] == ib1) {
1532 /* decrement i, i.e i = (i - 1 + 2) % 2 */
1533 i = (i + 1) % 2;
1534 if (adreno_cmd_is_ib(val[i])) {
1535 /* go till start of command sequence */
1536 status = _find_start_of_cmd_seq(rb,
1537 &temp_rb_rptr, false);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001538
1539 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001540 "Found the hanging IB at offset 0x%x\n",
1541 temp_rb_rptr / sizeof(unsigned int));
1542 break;
1543 }
1544 /* if no match the increment i since we decremented
1545 * before checking */
1546 i = (i + 1) % 2;
1547 }
1548 /* Make sure you do not encounter a context switch twice, we can
1549 * encounter it once for the bad context as the start of search
1550 * can point to the context switch */
1551 if (val[i] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
1552 if (ctx_switch) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001553 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001554 "Context switch encountered before bad "
1555 "IB found\n");
1556 break;
1557 }
1558 ctx_switch = true;
1559 }
1560 i = (i + 1) % 2;
1561 if (1 == i)
1562 check = true;
1563 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(temp_rb_rptr,
1564 size);
1565 }
1566 if (!status)
1567 *rb_rptr = temp_rb_rptr;
1568 return status;
1569}
1570
Tarun Karra59e79542013-04-10 10:25:25 -07001571static void adreno_setup_ft_data(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001572 struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001573{
1574 int ret = 0;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001575 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1576 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001577 struct kgsl_context *context;
1578 struct adreno_context *adreno_context;
1579 unsigned int rb_rptr = rb->wptr * sizeof(unsigned int);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001580
Tarun Karrad20d71a2013-01-25 15:38:57 -08001581 memset(ft_data, 0, sizeof(*ft_data));
1582 ft_data->start_of_replay_cmds = 0xFFFFFFFF;
1583 ft_data->replay_for_snapshot = 0xFFFFFFFF;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001584
Tarun Karrad20d71a2013-01-25 15:38:57 -08001585 adreno_regread(device, REG_CP_IB1_BASE, &ft_data->ib1);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001586
Tarun Karrad20d71a2013-01-25 15:38:57 -08001587 kgsl_sharedmem_readl(&device->memstore, &ft_data->context_id,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001588 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1589 current_context));
1590
1591 kgsl_sharedmem_readl(&device->memstore,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001592 &ft_data->global_eop,
Tarun Karradeeecc02013-01-21 23:42:17 -08001593 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1594 eoptimestamp));
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001595
Tarun Karra782734f2013-03-27 20:21:02 -07001596 /* Ensure context id and global eop ts read complete */
1597 rmb();
1598
Tarun Karrad20d71a2013-01-25 15:38:57 -08001599 ft_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1600 if (!ft_data->rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001601 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1602 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001603 return;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001604 }
1605
Tarun Karrad20d71a2013-01-25 15:38:57 -08001606 ft_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1607 if (!ft_data->bad_rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001608 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1609 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001610 return;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001611 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001612
Tarun Karrad20d71a2013-01-25 15:38:57 -08001613 ft_data->good_rb_buffer = vmalloc(rb->buffer_desc.size);
1614 if (!ft_data->good_rb_buffer) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001615 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1616 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001617 return;
Tarun Karradeeecc02013-01-21 23:42:17 -08001618 }
Tarun Karra3164fb02013-02-05 15:38:51 -08001619
Tarun Karra59e79542013-04-10 10:25:25 -07001620 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001621
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001622 /* find the start of bad command sequence in rb */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001623 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001624 /* Look for the command stream that is right after the global eop */
1625
1626 if (!context) {
1627 /*
Tarun Karrad20d71a2013-01-25 15:38:57 -08001628 * If there is no context then fault tolerance does not need to
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001629 * replay anything, just reset GPU and thats it
1630 */
Tarun Karra59e79542013-04-10 10:25:25 -07001631 return;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001632 }
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001633
Tarun Karra59e79542013-04-10 10:25:25 -07001634 ft_data->ft_policy = adreno_dev->ft_policy;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001635
Tarun Karra3164fb02013-02-05 15:38:51 -08001636 if (!adreno_dev->ft_policy)
1637 adreno_dev->ft_policy = KGSL_FT_DEFAULT_POLICY;
1638
Tarun Karra59e79542013-04-10 10:25:25 -07001639 ret = _find_cmd_seq_after_eop_ts(rb, &rb_rptr,
1640 ft_data->global_eop + 1, false);
1641 if (ret) {
1642 ft_data->ft_policy |= KGSL_FT_TEMP_DISABLE;
1643 return;
1644 } else
1645 ft_data->ft_policy &= ~KGSL_FT_TEMP_DISABLE;
Tarun Karra3164fb02013-02-05 15:38:51 -08001646
Tarun Karra59e79542013-04-10 10:25:25 -07001647 ft_data->start_of_replay_cmds = rb_rptr;
Tarun Karra3164fb02013-02-05 15:38:51 -08001648
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001649 adreno_context = context->devctxt;
1650 if (adreno_context->flags & CTXT_FLAGS_PREAMBLE) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001651 if (ft_data->ib1) {
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001652 ret = _find_hanging_ib_sequence(rb,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001653 &rb_rptr, ft_data->ib1);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001654 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001655 KGSL_FT_ERR(device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001656 "Start not found for replay IB sequence\n");
Tarun Karra59e79542013-04-10 10:25:25 -07001657 return;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001658 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001659 ft_data->start_of_replay_cmds = rb_rptr;
1660 ft_data->replay_for_snapshot = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001661 }
1662 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001663}
1664
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001665static int
Tarun Karra696f89e2013-01-27 21:31:40 -08001666_adreno_check_long_ib(struct kgsl_device *device)
1667{
1668 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1669 unsigned int curr_global_ts = 0;
1670
1671 /* check if the global ts is still the same */
1672 kgsl_sharedmem_readl(&device->memstore,
1673 &curr_global_ts,
1674 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1675 eoptimestamp));
Tarun Karra782734f2013-03-27 20:21:02 -07001676 /* Ensure above read is finished before long ib check */
1677 rmb();
Tarun Karra696f89e2013-01-27 21:31:40 -08001678
1679 /* Mark long ib as handled */
1680 adreno_dev->long_ib = 0;
1681
1682 if (curr_global_ts == adreno_dev->long_ib_ts) {
1683 KGSL_FT_ERR(device,
1684 "IB ran too long, invalidate ctxt\n");
1685 return 1;
1686 } else {
1687 /* Do nothing GPU has gone ahead */
1688 KGSL_FT_INFO(device, "false long ib detection return\n");
1689 return 0;
1690 }
1691}
1692
1693static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001694_adreno_ft_restart_device(struct kgsl_device *device,
Tarun Karra59e79542013-04-10 10:25:25 -07001695 struct kgsl_context *context)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001696{
Tarun Karradeeecc02013-01-21 23:42:17 -08001697
1698 struct adreno_context *adreno_context = context->devctxt;
1699
1700 /* restart device */
1701 if (adreno_stop(device)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001702 KGSL_FT_ERR(device, "Device stop failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001703 return 1;
1704 }
1705
1706 if (adreno_start(device, true)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001707 KGSL_FT_ERR(device, "Device start failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001708 return 1;
1709 }
1710
1711 if (context)
1712 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1713 KGSL_MEMSTORE_GLOBAL);
1714
1715 /* If iommu is used then we need to make sure that the iommu clocks
1716 * are on since there could be commands in pipeline that touch iommu */
1717 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1718 if (kgsl_mmu_enable_clk(&device->mmu,
1719 KGSL_IOMMU_CONTEXT_USER))
1720 return 1;
1721 }
1722
1723 return 0;
1724}
1725
Tarun Karra696f89e2013-01-27 21:31:40 -08001726static inline void
1727_adreno_debug_ft_info(struct kgsl_device *device,
1728 struct adreno_ft_data *ft_data)
1729{
1730
1731 /*
1732 * Dumping rb is a very useful tool to debug FT.
1733 * It will tell us if we are extracting the rb correctly
1734 * NOP'ing the right IB, skipping the EOF correctly etc.
1735 */
1736 if (device->ft_log >= 7) {
1737
1738 /* Print fault tolerance data here */
1739 KGSL_FT_INFO(device, "Temp RB buffer size 0x%X\n",
1740 ft_data->rb_size);
1741 adreno_dump_rb(device, ft_data->rb_buffer,
1742 ft_data->rb_size<<2, 0, ft_data->rb_size);
1743
1744 KGSL_FT_INFO(device, "Bad RB buffer size 0x%X\n",
1745 ft_data->bad_rb_size);
1746 adreno_dump_rb(device, ft_data->bad_rb_buffer,
1747 ft_data->bad_rb_size<<2, 0, ft_data->bad_rb_size);
1748
1749 KGSL_FT_INFO(device, "Good RB buffer size 0x%X\n",
1750 ft_data->good_rb_size);
1751 adreno_dump_rb(device, ft_data->good_rb_buffer,
1752 ft_data->good_rb_size<<2, 0, ft_data->good_rb_size);
1753
1754 }
1755}
1756
Tarun Karradeeecc02013-01-21 23:42:17 -08001757static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001758_adreno_ft_resubmit_rb(struct kgsl_device *device,
Tarun Karradeeecc02013-01-21 23:42:17 -08001759 struct adreno_ringbuffer *rb,
1760 struct kgsl_context *context,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001761 struct adreno_ft_data *ft_data,
Tarun Karradeeecc02013-01-21 23:42:17 -08001762 unsigned int *buff, unsigned int size)
1763{
1764 unsigned int ret = 0;
Tarun Karra59e79542013-04-10 10:25:25 -07001765 unsigned int retry_num = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001766
Tarun Karra696f89e2013-01-27 21:31:40 -08001767 _adreno_debug_ft_info(device, ft_data);
1768
Tarun Karra59e79542013-04-10 10:25:25 -07001769 do {
1770 ret = _adreno_ft_restart_device(device, context);
1771 if (ret == 0)
1772 break;
1773 /*
1774 * If device restart fails sleep for 20ms before
1775 * attempting restart. This allows GPU HW to settle
1776 * and improve the chances of next restart to be
1777 * successful.
1778 */
1779 msleep(20);
1780 KGSL_FT_ERR(device, "Retry device restart %d\n", retry_num);
1781 retry_num++;
1782 } while (retry_num < 4);
1783
1784 if (ret) {
1785 KGSL_FT_ERR(device, "Device restart failed\n");
1786 BUG_ON(1);
1787 goto done;
1788 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001789
1790 if (size) {
1791
1792 /* submit commands and wait for them to pass */
1793 adreno_ringbuffer_restore(rb, buff, size);
1794
1795 ret = adreno_idle(device);
1796 }
1797
Tarun Karra59e79542013-04-10 10:25:25 -07001798done:
Tarun Karradeeecc02013-01-21 23:42:17 -08001799 return ret;
1800}
1801
Tarun Karradeeecc02013-01-21 23:42:17 -08001802static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001803_adreno_ft(struct kgsl_device *device,
1804 struct adreno_ft_data *ft_data)
Tarun Karradeeecc02013-01-21 23:42:17 -08001805{
1806 int ret = 0, i;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001807 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1808 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1809 struct kgsl_context *context;
1810 struct adreno_context *adreno_context = NULL;
1811 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
Tarun Karra59e79542013-04-10 10:25:25 -07001812 unsigned int long_ib = 0;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001813
Tarun Karrad20d71a2013-01-25 15:38:57 -08001814 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001815 if (context == NULL) {
Tarun Karra59e79542013-04-10 10:25:25 -07001816 KGSL_FT_ERR(device, "Last context unknown id:%d\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001817 ft_data->context_id);
Tarun Karra59e79542013-04-10 10:25:25 -07001818 goto play_good_cmds;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001819 } else {
1820 adreno_context = context->devctxt;
1821 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001822 /*
1823 * set the invalid ts flag to 0 for this context since we have
1824 * detected a hang for it
1825 */
1826 context->wait_on_invalid_ts = false;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001827
Tarun Karra59e79542013-04-10 10:25:25 -07001828 if (!(adreno_context->flags & CTXT_FLAGS_PER_CONTEXT_TS)) {
1829 ft_data->status = 1;
1830 KGSL_FT_ERR(device, "Fault tolerance not supported\n");
1831 goto play_good_cmds;
1832 }
1833
Tarun Karra83297222013-02-05 19:45:49 -08001834 /*
1835 * This flag will be set by userspace for contexts
1836 * that do not want to be fault tolerant (ex: OPENCL)
1837 */
1838 if (adreno_context->flags & CTXT_FLAGS_NO_FAULT_TOLERANCE) {
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001839 ft_data->status = 1;
Tarun Karra83297222013-02-05 19:45:49 -08001840 KGSL_FT_ERR(device,
1841 "No FT set for this context play good cmds\n");
1842 goto play_good_cmds;
1843 }
1844
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001845 }
1846
Tarun Karra59e79542013-04-10 10:25:25 -07001847 /* Check if we detected a long running IB,
1848 * if true do not attempt replay of bad cmds */
1849 if (adreno_dev->long_ib) {
1850 long_ib = _adreno_check_long_ib(device);
1851 if (!long_ib) {
1852 adreno_context->flags &= ~CTXT_FLAGS_GPU_HANG;
1853 return 0;
1854 }
1855 }
1856
Tarun Karra3164fb02013-02-05 15:38:51 -08001857 /*
1858 * Extract valid contents from rb which can still be executed after
1859 * hang
1860 */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001861 adreno_ringbuffer_extract(rb, ft_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001862
Tarun Karra59e79542013-04-10 10:25:25 -07001863 /* If long IB detected do not attempt replay of bad cmds */
1864 if (long_ib) {
1865 ft_data->status = 1;
1866 _adreno_debug_ft_info(device, ft_data);
1867 goto play_good_cmds;
1868 }
1869
1870 if ((ft_data->ft_policy & KGSL_FT_DISABLE) ||
1871 (ft_data->ft_policy & KGSL_FT_TEMP_DISABLE)) {
1872 KGSL_FT_ERR(device, "NO FT policy play only good cmds\n");
1873 ft_data->status = 1;
1874 goto play_good_cmds;
Tarun Karra696f89e2013-01-27 21:31:40 -08001875 }
1876
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001877 /* Do not try the reply if hang is due to a pagefault */
1878 if (adreno_context->pagefault) {
1879 if ((ft_data->context_id == adreno_context->id) &&
1880 (ft_data->global_eop == adreno_context->pagefault_ts)) {
1881 ft_data->ft_policy &= ~KGSL_FT_REPLAY;
1882 KGSL_FT_ERR(device, "MMU fault skipping replay\n");
1883 }
1884
1885 adreno_context->pagefault = 0;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001886 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001887
Tarun Karra3164fb02013-02-05 15:38:51 -08001888 if (ft_data->ft_policy & KGSL_FT_REPLAY) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001889 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1890 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001891
Tarun Karra3164fb02013-02-05 15:38:51 -08001892 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001893 KGSL_FT_ERR(device, "Replay status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001894 ft_data->status = 1;
1895 } else
Tarun Karradeeecc02013-01-21 23:42:17 -08001896 goto play_good_cmds;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001897 }
1898
Tarun Karra3164fb02013-02-05 15:38:51 -08001899 if (ft_data->ft_policy & KGSL_FT_SKIPIB) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001900 for (i = 0; i < ft_data->bad_rb_size; i++) {
1901 if ((ft_data->bad_rb_buffer[i] ==
1902 CP_HDR_INDIRECT_BUFFER_PFD) &&
1903 (ft_data->bad_rb_buffer[i+1] == ft_data->ib1)) {
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001904
Tarun Karrad20d71a2013-01-25 15:38:57 -08001905 ft_data->bad_rb_buffer[i] = cp_nop_packet(2);
1906 ft_data->bad_rb_buffer[i+1] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001907 KGSL_NOP_IB_IDENTIFIER;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001908 ft_data->bad_rb_buffer[i+2] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001909 KGSL_NOP_IB_IDENTIFIER;
1910 break;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001911 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001912 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001913
Tarun Karrad20d71a2013-01-25 15:38:57 -08001914 if ((i == (ft_data->bad_rb_size)) || (!ft_data->ib1)) {
1915 KGSL_FT_ERR(device, "Bad IB to NOP not found\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001916 ft_data->status = 1;
Tarun Karradeeecc02013-01-21 23:42:17 -08001917 goto play_good_cmds;
1918 }
1919
Tarun Karrad20d71a2013-01-25 15:38:57 -08001920 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1921 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001922
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001923 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001924 KGSL_FT_ERR(device, "NOP faulty IB status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001925 ft_data->status = 1;
1926 } else {
1927 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001928 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001929 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001930 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001931
Tarun Karra3164fb02013-02-05 15:38:51 -08001932 if (ft_data->ft_policy & KGSL_FT_SKIPFRAME) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001933 for (i = 0; i < ft_data->bad_rb_size; i++) {
1934 if (ft_data->bad_rb_buffer[i] ==
1935 KGSL_END_OF_FRAME_IDENTIFIER) {
1936 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001937 break;
1938 }
1939 }
1940
1941 /* EOF not found in RB, discard till EOF in
1942 next IB submission */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001943 if (i == ft_data->bad_rb_size) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001944 adreno_context->flags |= CTXT_FLAGS_SKIP_EOF;
Tarun Karra3164fb02013-02-05 15:38:51 -08001945 KGSL_FT_INFO(device,
1946 "EOF not found in RB, skip next issueib till EOF\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08001947 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001948 }
1949
Tarun Karrad20d71a2013-01-25 15:38:57 -08001950 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1951 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001952
1953 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001954 KGSL_FT_ERR(device, "Skip EOF status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001955 ft_data->status = 1;
1956 } else {
1957 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001958 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001959 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001960 }
1961
1962play_good_cmds:
1963
Tarun Karra3164fb02013-02-05 15:38:51 -08001964 if (ft_data->status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001965 KGSL_FT_ERR(device, "Bad context commands failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001966 else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001967 KGSL_FT_INFO(device, "Bad context commands success\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001968
1969 if (adreno_context) {
1970 adreno_context->flags = (adreno_context->flags &
Tarun Karrad20d71a2013-01-25 15:38:57 -08001971 ~CTXT_FLAGS_GPU_HANG) | CTXT_FLAGS_GPU_HANG_FT;
Tarun Karradeeecc02013-01-21 23:42:17 -08001972 }
1973 adreno_dev->drawctxt_active = last_active_ctx;
1974 }
1975
Tarun Karrad20d71a2013-01-25 15:38:57 -08001976 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1977 ft_data->good_rb_buffer, ft_data->good_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001978
1979 if (ret) {
1980 /* If we fail here we can try to invalidate another
1981 * context and try fault tolerance again */
1982 ret = -EAGAIN;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001983 KGSL_FT_ERR(device, "Playing good commands unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001984 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001985 } else
1986 KGSL_FT_INFO(device, "Playing good commands successful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001987
1988 /* ringbuffer now has data from the last valid context id,
1989 * so restore the active_ctx to the last valid context */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001990 if (ft_data->last_valid_ctx_id) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001991 struct kgsl_context *last_ctx =
1992 idr_find(&device->context_idr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001993 ft_data->last_valid_ctx_id);
Tarun Karradeeecc02013-01-21 23:42:17 -08001994 if (last_ctx)
1995 adreno_dev->drawctxt_active = last_ctx->devctxt;
1996 }
1997
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001998done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001999 /* Turn off iommu clocks */
2000 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
2001 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002002 return ret;
2003}
2004
2005static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002006adreno_ft(struct kgsl_device *device,
2007 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002008{
2009 int ret = 0;
2010 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2011 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2012 unsigned int timestamp;
2013
Tarun Karrad20d71a2013-01-25 15:38:57 -08002014 KGSL_FT_INFO(device,
2015 "Start Parameters: IB1: 0x%X, "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002016 "Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08002017 ft_data->ib1, ft_data->context_id, ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002018
2019 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
Tarun Karrad20d71a2013-01-25 15:38:57 -08002020 KGSL_FT_INFO(device, "Last issued global timestamp: %x\n", timestamp);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002021
2022 /* We may need to replay commands multiple times based on whether
2023 * multiple contexts hang the GPU */
2024 while (true) {
Tarun Karradeeecc02013-01-21 23:42:17 -08002025
Tarun Karrad20d71a2013-01-25 15:38:57 -08002026 ret = _adreno_ft(device, ft_data);
2027
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002028 if (-EAGAIN == ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002029 /* setup new fault tolerance parameters and retry, this
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002030 * means more than 1 contexts are causing hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08002031 adreno_destroy_ft_data(ft_data);
Tarun Karra59e79542013-04-10 10:25:25 -07002032 adreno_setup_ft_data(device, ft_data);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002033 KGSL_FT_INFO(device,
2034 "Retry. Parameters: "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002035 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08002036 ft_data->ib1, ft_data->context_id,
2037 ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002038 } else {
2039 break;
2040 }
2041 }
2042
2043 if (ret)
2044 goto done;
2045
Tarun Karrad20d71a2013-01-25 15:38:57 -08002046 /* Restore correct states after fault tolerance */
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002047 if (adreno_dev->drawctxt_active)
2048 device->mmu.hwpagetable =
2049 adreno_dev->drawctxt_active->pagetable;
2050 else
2051 device->mmu.hwpagetable = device->mmu.defaultpagetable;
2052 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
2053 kgsl_sharedmem_writel(&device->memstore,
2054 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2055 eoptimestamp),
2056 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07002057
2058 /* switch to NULL ctxt */
2059 if (adreno_dev->drawctxt_active != NULL)
2060 adreno_drawctxt_switch(adreno_dev, NULL, 0);
2061
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002062done:
2063 adreno_set_max_ts_for_bad_ctxs(device);
2064 adreno_mark_context_status(device, ret);
Tarun Karra3164fb02013-02-05 15:38:51 -08002065 KGSL_FT_ERR(device, "policy 0x%X status 0x%x\n",
2066 ft_data->ft_policy, ret);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002067 return ret;
2068}
2069
2070int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002071adreno_dump_and_exec_ft(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002072{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002073 int result = -ETIMEDOUT;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002074 struct adreno_ft_data ft_data;
Tarun Karra696f89e2013-01-27 21:31:40 -08002075 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karrad2326b82013-01-28 19:09:41 -08002076 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2077 unsigned int curr_pwrlevel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002078
2079 if (device->state == KGSL_STATE_HUNG)
2080 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002081 if (device->state == KGSL_STATE_DUMP_AND_FT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002082 mutex_unlock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002083 wait_for_completion(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002084 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07002085 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002086 result = 0;
2087 } else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002088 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_FT);
2089 INIT_COMPLETION(device->ft_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002090 /* Detected a hang */
2091
Tarun Karrad2326b82013-01-28 19:09:41 -08002092 /* Run fault tolerance at max power level */
2093 curr_pwrlevel = pwr->active_pwrlevel;
2094 kgsl_pwrctrl_pwrlevel_change(device, pwr->max_pwrlevel);
2095
Tarun Karrad20d71a2013-01-25 15:38:57 -08002096 /* Get the fault tolerance data as soon as hang is detected */
Tarun Karra59e79542013-04-10 10:25:25 -07002097 adreno_setup_ft_data(device, &ft_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002098
2099 /*
Tarun Karra696f89e2013-01-27 21:31:40 -08002100 * If long ib is detected, do not attempt postmortem or
2101 * snapshot, if GPU is still executing commands
2102 * we will get errors
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002103 */
Tarun Karra696f89e2013-01-27 21:31:40 -08002104 if (!adreno_dev->long_ib) {
2105 /*
2106 * Trigger an automatic dump of the state to
2107 * the console
2108 */
2109 kgsl_postmortem_dump(device, 0);
2110
2111 /*
2112 * Make a GPU snapshot. For now, do it after the
2113 * PM dump so we can at least be sure the PM dump
2114 * will work as it always has
2115 */
2116 kgsl_device_snapshot(device, 1);
2117 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002118
Tarun Karra59e79542013-04-10 10:25:25 -07002119 result = adreno_ft(device, &ft_data);
2120 adreno_destroy_ft_data(&ft_data);
Tarun Karra696f89e2013-01-27 21:31:40 -08002121
Tarun Karrad2326b82013-01-28 19:09:41 -08002122 /* restore power level */
2123 kgsl_pwrctrl_pwrlevel_change(device, curr_pwrlevel);
2124
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002125 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002126 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002127 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002128 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002129 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
2130 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08002131 complete_all(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002132 }
2133done:
2134 return result;
2135}
Tarun Karrad20d71a2013-01-25 15:38:57 -08002136EXPORT_SYMBOL(adreno_dump_and_exec_ft);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002137
2138static int adreno_getproperty(struct kgsl_device *device,
2139 enum kgsl_property_type type,
2140 void *value,
2141 unsigned int sizebytes)
2142{
2143 int status = -EINVAL;
2144 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2145
2146 switch (type) {
2147 case KGSL_PROP_DEVICE_INFO:
2148 {
2149 struct kgsl_devinfo devinfo;
2150
2151 if (sizebytes != sizeof(devinfo)) {
2152 status = -EINVAL;
2153 break;
2154 }
2155
2156 memset(&devinfo, 0, sizeof(devinfo));
2157 devinfo.device_id = device->id+1;
2158 devinfo.chip_id = adreno_dev->chip_id;
2159 devinfo.mmu_enabled = kgsl_mmu_enabled();
2160 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06002161 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
2162 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002163
2164 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
2165 0) {
2166 status = -EFAULT;
2167 break;
2168 }
2169 status = 0;
2170 }
2171 break;
2172 case KGSL_PROP_DEVICE_SHADOW:
2173 {
2174 struct kgsl_shadowprop shadowprop;
2175
2176 if (sizebytes != sizeof(shadowprop)) {
2177 status = -EINVAL;
2178 break;
2179 }
2180 memset(&shadowprop, 0, sizeof(shadowprop));
2181 if (device->memstore.hostptr) {
2182 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2183 * anything to mmap().
2184 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07002185 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186 shadowprop.size = device->memstore.size;
2187 /* GSL needs this to be set, even if it
2188 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002189 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2190 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002191 }
2192 if (copy_to_user(value, &shadowprop,
2193 sizeof(shadowprop))) {
2194 status = -EFAULT;
2195 break;
2196 }
2197 status = 0;
2198 }
2199 break;
2200 case KGSL_PROP_MMU_ENABLE:
2201 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002202 int mmu_prop = kgsl_mmu_enabled();
2203
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002204 if (sizebytes != sizeof(int)) {
2205 status = -EINVAL;
2206 break;
2207 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002208 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002209 status = -EFAULT;
2210 break;
2211 }
2212 status = 0;
2213 }
2214 break;
2215 case KGSL_PROP_INTERRUPT_WAITS:
2216 {
2217 int int_waits = 1;
2218 if (sizebytes != sizeof(int)) {
2219 status = -EINVAL;
2220 break;
2221 }
2222 if (copy_to_user(value, &int_waits, sizeof(int))) {
2223 status = -EFAULT;
2224 break;
2225 }
2226 status = 0;
2227 }
2228 break;
2229 default:
2230 status = -EINVAL;
2231 }
2232
2233 return status;
2234}
2235
Jordan Crousef7370f82012-04-18 09:31:07 -06002236static int adreno_setproperty(struct kgsl_device *device,
2237 enum kgsl_property_type type,
2238 void *value,
2239 unsigned int sizebytes)
2240{
2241 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08002242 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06002243
2244 switch (type) {
2245 case KGSL_PROP_PWRCTRL: {
2246 unsigned int enable;
2247 struct kgsl_device_platform_data *pdata =
2248 kgsl_device_get_drvdata(device);
2249
2250 if (sizebytes != sizeof(enable))
2251 break;
2252
2253 if (copy_from_user(&enable, (void __user *) value,
2254 sizeof(enable))) {
2255 status = -EFAULT;
2256 break;
2257 }
2258
2259 if (enable) {
2260 if (pdata->nap_allowed)
2261 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002262 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002263 kgsl_pwrscale_enable(device);
2264 } else {
2265 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002266 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002267 kgsl_pwrscale_disable(device);
2268 }
2269
2270 status = 0;
2271 }
2272 break;
2273 default:
2274 break;
2275 }
2276
2277 return status;
2278}
2279
Jordan Crousea29a2e02012-08-14 09:09:23 -06002280static int adreno_ringbuffer_drain(struct kgsl_device *device,
2281 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002282{
2283 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2284 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002285 unsigned long wait;
2286 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2287
2288 if (!(rb->flags & KGSL_FLAGS_STARTED))
2289 return 0;
2290
2291 /*
2292 * The first time into the loop, wait for 100 msecs and kick wptr again
2293 * to ensure that the hardware has updated correctly. After that, kick
2294 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2295 * expires
2296 */
2297
2298 wait = jiffies + msecs_to_jiffies(100);
2299
Jordan Crousea29a2e02012-08-14 09:09:23 -06002300 do {
2301 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002302 /* Check to see if the core is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002303 if (adreno_ft_detect(device, regs))
Jordan Crousea29a2e02012-08-14 09:09:23 -06002304 return -ETIMEDOUT;
2305
2306 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2307 }
2308 GSL_RB_GET_READPTR(rb, &rb->rptr);
2309
2310 if (time_after(jiffies, timeout)) {
2311 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2312 rb->rptr, rb->wptr);
2313 return -ETIMEDOUT;
2314 }
2315 } while (rb->rptr != rb->wptr);
2316
2317 return 0;
2318}
2319
2320/* Caller must hold the device mutex. */
2321int adreno_idle(struct kgsl_device *device)
2322{
2323 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002324 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302325 unsigned long wait_time;
2326 unsigned long wait_time_part;
Tarun Karra696f89e2013-01-27 21:31:40 -08002327 unsigned int prev_reg_val[ft_detect_regs_count];
Tarun Karra3335f142012-06-19 14:11:48 -07002328
2329 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002330
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002331 kgsl_cffdump_regpoll(device->id,
2332 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002333 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002334
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002335retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002336 /* First, wait for the ringbuffer to drain */
2337 if (adreno_ringbuffer_drain(device, prev_reg_val))
2338 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002339
2340 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002341 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002342 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2343
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002344 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002345 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2346 &rbbm_status);
2347 if (adreno_is_a2xx(adreno_dev)) {
2348 if (rbbm_status == 0x110)
2349 return 0;
2350 } else {
2351 if (!(rbbm_status & 0x80000000))
2352 return 0;
2353 }
Tarun Karra3335f142012-06-19 14:11:48 -07002354
2355 /* Dont wait for timeout, detect hang faster.
2356 */
2357 if (time_after(jiffies, wait_time_part)) {
2358 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002359 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra696f89e2013-01-27 21:31:40 -08002360 if ((adreno_ft_detect(device, prev_reg_val)))
Tarun Karra3335f142012-06-19 14:11:48 -07002361 goto err;
2362 }
2363
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002364 }
2365
2366err:
2367 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08002368 if (KGSL_STATE_DUMP_AND_FT != device->state &&
2369 !adreno_dump_and_exec_ft(device)) {
2370 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002371 goto retry;
2372 }
2373 return -ETIMEDOUT;
2374}
2375
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002376/**
2377 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2378 * rbbm_status register
2379 * @device - Pointer to the GPU device whose idle status is to be
2380 * checked
2381 * @returns - Returns whether the core is idle (based on rbbm_status)
2382 * false if the core is active, true if the core is idle
2383 */
2384static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2385{
2386 unsigned int reg_rbbm_status;
2387 bool status = false;
2388 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2389
2390 /* Is the core idle? */
2391 adreno_regread(device,
2392 adreno_dev->gpudev->reg_rbbm_status,
2393 &reg_rbbm_status);
2394
2395 if (adreno_is_a2xx(adreno_dev)) {
2396 if (reg_rbbm_status == 0x110)
2397 status = true;
2398 } else {
2399 if (!(reg_rbbm_status & 0x80000000))
2400 status = true;
2401 }
2402 return status;
2403}
2404
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002405static unsigned int adreno_isidle(struct kgsl_device *device)
2406{
2407 int status = false;
2408 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2409 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002410
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002411 WARN_ON(device->state == KGSL_STATE_INIT);
2412 /* If the device isn't active, don't force it on. */
2413 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002414 /* Is the ring buffer is empty? */
2415 GSL_RB_GET_READPTR(rb, &rb->rptr);
2416 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
Jordan Crouseab601992013-03-05 11:18:20 -07002417 /*
2418 * Are there interrupts pending? If so then pretend we
2419 * are not idle - this avoids the possiblity that we go
2420 * to a lower power state without handling interrupts
2421 * first.
2422 */
2423
2424 if (!adreno_dev->gpudev->irq_pending(adreno_dev)) {
2425 /* Is the core idle? */
2426 status = is_adreno_rbbm_status_idle(device);
2427 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002428 }
2429 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002430 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002431 }
2432 return status;
2433}
2434
2435/* Caller must hold the device mutex. */
2436static int adreno_suspend_context(struct kgsl_device *device)
2437{
2438 int status = 0;
2439 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2440
2441 /* switch to NULL ctxt */
2442 if (adreno_dev->drawctxt_active != NULL) {
2443 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002444 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002445 }
2446
2447 return status;
2448}
2449
Jordan Crouse233b2092012-04-18 09:31:09 -06002450/* Find a memory structure attached to an adreno context */
2451
2452struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2453 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2454{
2455 struct kgsl_context *context;
2456 struct adreno_context *adreno_context = NULL;
2457 int next = 0;
2458
2459 while (1) {
2460 context = idr_get_next(&device->context_idr, &next);
2461 if (context == NULL)
2462 break;
2463
2464 adreno_context = (struct adreno_context *)context->devctxt;
2465
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002466 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2467 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002468 struct kgsl_memdesc *desc;
2469
2470 desc = &adreno_context->gpustate;
2471 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2472 return desc;
2473
2474 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2475 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2476 return desc;
2477 }
2478 next = next + 1;
2479 }
2480
2481 return NULL;
2482}
2483
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002484struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002485 unsigned int pt_base,
2486 unsigned int gpuaddr,
2487 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002488{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002489 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002490 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2491 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2492
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002493 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2494 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002495
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002496 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2497 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002498
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002499 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2500 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002501
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002502 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2503 size))
2504 return &device->mmu.setstate_memory;
2505
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002506 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002507
2508 if (entry)
2509 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002510
Jordan Crouse233b2092012-04-18 09:31:09 -06002511 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002512}
2513
2514uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2515 unsigned int gpuaddr, unsigned int size)
2516{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002517 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002518
2519 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2520
2521 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002522}
2523
2524void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2525 unsigned int *value)
2526{
2527 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002528 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2529 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002530
2531 if (!in_interrupt())
2532 kgsl_pre_hwaccess(device);
2533
2534 /*ensure this read finishes before the next one.
2535 * i.e. act like normal readl() */
2536 *value = __raw_readl(reg);
2537 rmb();
2538}
2539
2540void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2541 unsigned int value)
2542{
2543 unsigned int *reg;
2544
Jordan Crouse7501d452012-04-19 08:58:44 -06002545 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002546
2547 if (!in_interrupt())
2548 kgsl_pre_hwaccess(device);
2549
Jordan Crousef7b81e82013-05-28 17:03:00 -06002550 trace_kgsl_regwrite(device, offsetwords, value);
2551
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002552 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002553 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002554
2555 /*ensure previous writes post before this one,
2556 * i.e. act like normal writel() */
2557 wmb();
2558 __raw_writel(value, reg);
2559}
2560
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002561static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2562{
2563 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002564 if (k_ctxt != NULL) {
2565 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002566 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2567 context_id = KGSL_CONTEXT_INVALID;
2568 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2569 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002570 }
2571
2572 return context_id;
2573}
2574
Carter Cooper740f6742013-01-03 16:19:23 -07002575static unsigned int adreno_check_hw_ts(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002576 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002577{
Carter Cooper740f6742013-01-03 16:19:23 -07002578 int status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002579 unsigned int ref_ts, enableflag;
Carter Cooper740f6742013-01-03 16:19:23 -07002580 unsigned int context_id = _get_context_id(context);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002581
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002582 /*
2583 * If the context ID is invalid, we are in a race with
2584 * the context being destroyed by userspace so bail.
2585 */
2586 if (context_id == KGSL_CONTEXT_INVALID) {
2587 KGSL_DRV_WARN(device, "context was detached");
Carter Cooper740f6742013-01-03 16:19:23 -07002588 return -EINVAL;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002589 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002590
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002591 status = kgsl_check_timestamp(device, context, timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002592 if (status)
2593 return status;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002594
Carter Cooper740f6742013-01-03 16:19:23 -07002595 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2596 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2597 /*
2598 * Barrier is needed here to make sure the read from memstore
2599 * has posted
2600 */
2601
2602 mb();
2603
2604 if (enableflag) {
2605 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002606 KGSL_MEMSTORE_OFFSET(context_id,
2607 ref_wait_ts));
Carter Cooper740f6742013-01-03 16:19:23 -07002608
2609 /* Make sure the memstore read has posted */
2610 mb();
2611 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
2612 kgsl_sharedmem_writel(&device->memstore,
2613 KGSL_MEMSTORE_OFFSET(context_id,
2614 ref_wait_ts), timestamp);
2615 /* Make sure the memstore write is posted */
2616 wmb();
2617 }
2618 } else {
2619 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002620 KGSL_MEMSTORE_OFFSET(context_id,
2621 ref_wait_ts), timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002622 enableflag = 1;
2623 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002624 KGSL_MEMSTORE_OFFSET(context_id,
2625 ts_cmp_enable), enableflag);
Carter Cooper740f6742013-01-03 16:19:23 -07002626 /* Make sure the memstore write gets posted */
2627 wmb();
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002628
Carter Cooper740f6742013-01-03 16:19:23 -07002629 /*
2630 * submit a dummy packet so that even if all
2631 * commands upto timestamp get executed we will still
2632 * get an interrupt
2633 */
2634
2635 if (context && device->state != KGSL_STATE_SLUMBER)
2636 adreno_ringbuffer_issuecmds(device, context->devctxt,
2637 KGSL_CMD_FLAGS_NONE, NULL, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002638 }
Carter Cooper740f6742013-01-03 16:19:23 -07002639
2640 return 0;
2641}
2642
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002643/* Return 1 if the event timestmp has already passed, 0 if it was marked */
2644static int adreno_next_event(struct kgsl_device *device,
Carter Cooper740f6742013-01-03 16:19:23 -07002645 struct kgsl_event *event)
2646{
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002647 return adreno_check_hw_ts(device, event->context, event->timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002648}
2649
2650static int adreno_check_interrupt_timestamp(struct kgsl_device *device,
2651 struct kgsl_context *context, unsigned int timestamp)
2652{
2653 int status;
2654
2655 mutex_lock(&device->mutex);
2656 status = adreno_check_hw_ts(device, context, timestamp);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002657 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002658
2659 return status;
2660}
2661
2662/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002663 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002664 placing a process in wait q. For conditional interrupts we expect the
2665 process to already be in its wait q when its exit condition checking
2666 function is called.
2667*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002668#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002669({ \
2670 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002671 if (io) \
2672 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2673 else \
2674 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002675 __ret; \
2676})
2677
Tarun Karra3335f142012-06-19 14:11:48 -07002678
2679
Tarun Karra696f89e2013-01-27 21:31:40 -08002680unsigned int adreno_ft_detect(struct kgsl_device *device,
Tarun Karra3335f142012-06-19 14:11:48 -07002681 unsigned int *prev_reg_val)
2682{
2683 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karra696f89e2013-01-27 21:31:40 -08002684 unsigned int curr_reg_val[ft_detect_regs_count];
2685 unsigned int fast_hang_detected = 1;
2686 unsigned int long_ib_detected = 1;
Tarun Karra3335f142012-06-19 14:11:48 -07002687 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002688 static unsigned long next_hang_detect_time;
Tarun Karra696f89e2013-01-27 21:31:40 -08002689 static unsigned int prev_global_ts;
2690 unsigned int curr_global_ts = 0;
2691 unsigned int curr_context_id = 0;
2692 static struct adreno_context *curr_context;
2693 static struct kgsl_context *context;
Tarun Karra3335f142012-06-19 14:11:48 -07002694
2695 if (!adreno_dev->fast_hang_detect)
Tarun Karra696f89e2013-01-27 21:31:40 -08002696 fast_hang_detected = 0;
2697
2698 if (!adreno_dev->long_ib_detect)
2699 long_ib_detected = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002700
Jordan Crousecca61142012-11-20 10:54:24 -07002701 if (is_adreno_rbbm_status_idle(device)) {
2702
2703 /*
2704 * On A20X if the RPTR != WPTR and the device is idle, then
2705 * the last write to WPTR probably failed to latch so write it
2706 * again
2707 */
2708
2709 if (adreno_is_a2xx(adreno_dev)) {
2710 unsigned int rptr;
2711 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2712 if (rptr != adreno_dev->ringbuffer.wptr)
2713 adreno_regwrite(device, REG_CP_RB_WPTR,
2714 adreno_dev->ringbuffer.wptr);
2715 }
2716
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002717 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002718 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002719
Tarun Karra4648db92013-02-01 17:03:40 -08002720 /*
2721 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2722 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2723 * from the last time do nothing.
2724 */
2725 if ((next_hang_detect_time) &&
2726 (time_before(jiffies, next_hang_detect_time)))
2727 return 0;
2728 else
2729 next_hang_detect_time = (jiffies +
2730 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2731
Tarun Karra696f89e2013-01-27 21:31:40 -08002732 /* Read the current Hang detect reg values here */
2733 for (i = 0; i < ft_detect_regs_count; i++) {
2734 if (ft_detect_regs[i] == 0)
Jordan Crouseb5c80482012-10-03 09:38:41 -06002735 continue;
Tarun Karra696f89e2013-01-27 21:31:40 -08002736 adreno_regread(device, ft_detect_regs[i],
2737 &curr_reg_val[i]);
Tarun Karra3335f142012-06-19 14:11:48 -07002738 }
2739
Tarun Karra696f89e2013-01-27 21:31:40 -08002740 /* Read the current global timestamp here */
2741 kgsl_sharedmem_readl(&device->memstore,
2742 &curr_global_ts,
2743 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2744 eoptimestamp));
2745
2746 mb();
2747
2748 if (curr_global_ts == prev_global_ts) {
2749
2750 /* Get the current context here */
2751 if (context == NULL) {
2752 kgsl_sharedmem_readl(&device->memstore,
2753 &curr_context_id,
2754 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2755 current_context));
2756 context = idr_find(&device->context_idr,
2757 curr_context_id);
2758 if (context != NULL) {
2759 curr_context = context->devctxt;
2760 curr_context->ib_gpu_time_used = 0;
2761 } else {
2762 KGSL_DRV_ERR(device,
2763 "Fault tolerance no context found\n");
2764 }
2765 }
2766
2767 mb();
2768
2769 if (curr_context != NULL) {
2770
2771 curr_context->ib_gpu_time_used += KGSL_TIMEOUT_PART;
2772 KGSL_FT_INFO(device,
2773 "Proc %s used GPU Time %d ms on timestamp 0x%X\n",
2774 curr_context->pid_name, curr_context->ib_gpu_time_used,
2775 curr_global_ts+1);
2776
2777 for (i = 0; i < ft_detect_regs_count; i++) {
2778 if (curr_reg_val[i] != prev_reg_val[i]) {
2779 fast_hang_detected = 0;
2780
2781 /* Check for long IB here */
2782 if ((i >=
2783 LONG_IB_DETECT_REG_INDEX_START)
2784 &&
2785 (i <=
2786 LONG_IB_DETECT_REG_INDEX_END))
2787 long_ib_detected = 0;
2788 }
2789 }
2790
2791 if (fast_hang_detected) {
2792 KGSL_FT_ERR(device,
2793 "Proc %s, ctxt_id %d ts %d triggered fault tolerance"
2794 " on global ts %d\n",
2795 curr_context->pid_name, curr_context->id
2796 , (kgsl_readtimestamp(device, context,
2797 KGSL_TIMESTAMP_RETIRED)+1),
2798 curr_global_ts+1);
2799 return 1;
2800 }
2801
Tarun Karra83297222013-02-05 19:45:49 -08002802 if ((long_ib_detected) &&
2803 (!(curr_context->flags &
2804 CTXT_FLAGS_NO_FAULT_TOLERANCE))) {
Tarun Karra696f89e2013-01-27 21:31:40 -08002805 curr_context->ib_gpu_time_used +=
2806 KGSL_TIMEOUT_PART;
2807 if (curr_context->ib_gpu_time_used >
2808 KGSL_TIMEOUT_LONG_IB_DETECTION) {
2809 if (adreno_dev->long_ib_ts !=
2810 curr_global_ts) {
2811 KGSL_FT_ERR(device,
2812 "Proc %s, ctxt_id %d ts %d"
2813 "used GPU for %d ms long ib "
2814 "detected on global ts %d\n",
2815 curr_context->pid_name,
2816 curr_context->id,
2817 (kgsl_readtimestamp(device,
2818 context,
2819 KGSL_TIMESTAMP_RETIRED)+1),
2820 curr_context->ib_gpu_time_used,
2821 curr_global_ts+1);
2822 adreno_dev->long_ib = 1;
2823 adreno_dev->long_ib_ts =
2824 curr_global_ts;
2825 curr_context->ib_gpu_time_used =
2826 0;
2827 return 1;
2828 }
2829 }
2830 }
2831 } else {
2832 KGSL_FT_ERR(device,
2833 "Last context unknown id:%d\n",
2834 curr_context_id);
2835 }
2836 } else {
2837 /* GPU is moving forward */
2838 prev_global_ts = curr_global_ts;
2839 context = NULL;
2840 curr_context = NULL;
2841 adreno_dev->long_ib = 0;
2842 adreno_dev->long_ib_ts = 0;
2843 }
2844
2845
2846 /* If hangs are not detected copy the current reg values
2847 * to previous values and return no hang */
2848 for (i = 0; i < ft_detect_regs_count; i++)
2849 prev_reg_val[i] = curr_reg_val[i];
2850 return 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002851}
2852
Jordan Crouse92446a62012-11-15 11:00:06 -07002853/**
2854 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2855 * @device - pointer to a KGSL device structure
2856 * @context - pointer to the active KGSL context
2857 * @timestamp - the timestamp that the process was waiting for
2858 *
Tarun Karrad20d71a2013-01-25 15:38:57 -08002859 * Process a possible GPU hang and try fault tolerance from it
2860 * cleanly
Jordan Crouse92446a62012-11-15 11:00:06 -07002861 */
2862static int adreno_handle_hang(struct kgsl_device *device,
2863 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002864{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002865 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002866 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002867 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002868 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002869
Jordan Crouse92446a62012-11-15 11:00:06 -07002870 /* Do one last check to see if we somehow made it through */
2871 if (kgsl_check_timestamp(device, context, timestamp))
2872 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002873
2874 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002875
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002876 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2877 mb();
2878
Tarun Karra696f89e2013-01-27 21:31:40 -08002879 KGSL_DRV_WARN(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002880 "Device hang detected while waiting for timestamp: "
2881 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002882 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2883 context_id, timestamp, context_id, ts_issued, context_id,
2884 kgsl_readtimestamp(device, context,
2885 KGSL_TIMESTAMP_RETIRED),
2886 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002887
Tarun Karrad20d71a2013-01-25 15:38:57 -08002888 /* Return 0 after a successful fault tolerance */
2889 if (!adreno_dump_and_exec_ft(device))
Jordan Crouse92446a62012-11-15 11:00:06 -07002890 return 0;
2891
2892 return -ETIMEDOUT;
2893}
2894
2895static int _check_pending_timestamp(struct kgsl_device *device,
2896 struct kgsl_context *context, unsigned int timestamp)
2897{
2898 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2899 unsigned int context_id = _get_context_id(context);
2900 unsigned int ts_issued;
2901
2902 if (context_id == KGSL_CONTEXT_INVALID)
2903 return -EINVAL;
2904
2905 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2906
2907 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2908 return 0;
2909
2910 if (context && !context->wait_on_invalid_ts) {
2911 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2912 context_id, timestamp, context_id, ts_issued);
2913
2914 /* Only print this message once */
2915 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002916 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002917
2918 return -EINVAL;
2919}
2920
2921/**
2922 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2923 * @device - pointer to a KGSL device structure
2924 * @context - pointer to the active kgsl context
2925 * @timestamp - GPU timestamp to wait for
2926 * @msecs - amount of time to wait (in milliseconds)
2927 *
2928 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2929 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2930 * one if it happened. Otherwise, spend most of our time in an interruptible
2931 * wait for the timestamp interrupt to be processed. This function must be
2932 * called with the mutex already held.
2933 */
2934static int adreno_waittimestamp(struct kgsl_device *device,
2935 struct kgsl_context *context,
2936 unsigned int timestamp,
2937 unsigned int msecs)
2938{
2939 static unsigned int io_cnt;
2940 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2941 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2942 unsigned int context_id = _get_context_id(context);
Tarun Karra696f89e2013-01-27 21:31:40 -08002943 unsigned int prev_reg_val[ft_detect_regs_count];
Jordan Crouse92446a62012-11-15 11:00:06 -07002944 unsigned int time_elapsed = 0;
2945 unsigned int wait;
2946 int ts_compare = 1;
2947 int io, ret = -ETIMEDOUT;
2948
2949 /* Get out early if the context has already been destroyed */
2950
2951 if (context_id == KGSL_CONTEXT_INVALID) {
2952 KGSL_DRV_WARN(device, "context was detached");
2953 return -EINVAL;
2954 }
2955
2956 /*
2957 * Check to see if the requested timestamp is "newer" then the last
2958 * timestamp issued. If it is complain once and return error. Only
2959 * print the message once per context so that badly behaving
2960 * applications don't spam the logs
2961 */
2962
2963 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2964 if (_check_pending_timestamp(device, context, timestamp))
2965 return -EINVAL;
2966
2967 /* Reset the invalid timestamp flag on a valid wait */
2968 context->wait_on_invalid_ts = false;
2969 }
2970
2971
2972 /* Clear the registers used for hang detection */
2973 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2974
2975 /*
2976 * On the first time through the loop only wait 100ms.
2977 * this gives enough time for the engine to start moving and oddly
2978 * provides better hang detection results than just going the full
2979 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2980 * is if msecs happens to be < 100ms then just use the full timeout
2981 */
2982
2983 wait = 100;
2984
2985 do {
2986 long status;
2987
Jordan Crouse92446a62012-11-15 11:00:06 -07002988 /*
2989 * if the timestamp happens while we're not
2990 * waiting, there's a chance that an interrupt
2991 * will not be generated and thus the timestamp
2992 * work needs to be queued.
2993 */
2994
2995 if (kgsl_check_timestamp(device, context, timestamp)) {
2996 queue_work(device->work_queue, &device->ts_expired_ws);
2997 ret = 0;
2998 break;
2999 }
3000
3001 /* Check to see if the GPU is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08003002 if (adreno_ft_detect(device, prev_reg_val)) {
Jordan Crouse92446a62012-11-15 11:00:06 -07003003 ret = adreno_handle_hang(device, context, timestamp);
3004 break;
3005 }
3006
3007 /*
3008 * For proper power accounting sometimes we need to call
3009 * io_wait_interruptible_timeout and sometimes we need to call
3010 * plain old wait_interruptible_timeout. We call the regular
3011 * timeout N times out of 100, where N is a number specified by
3012 * the current power level
3013 */
3014
3015 io_cnt = (io_cnt + 1) % 100;
3016 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
3017 ? 0 : 1;
3018
3019 mutex_unlock(&device->mutex);
3020
3021 /* Wait for a timestamp event */
3022 status = kgsl_wait_event_interruptible_timeout(
3023 device->wait_queue,
Carter Cooper740f6742013-01-03 16:19:23 -07003024 adreno_check_interrupt_timestamp(device, context,
Jordan Crouse92446a62012-11-15 11:00:06 -07003025 timestamp), msecs_to_jiffies(wait), io);
3026
3027 mutex_lock(&device->mutex);
3028
3029 /*
3030 * If status is non zero then either the condition was satisfied
3031 * or there was an error. In either event, this is the end of
3032 * the line for us
3033 */
3034
3035 if (status != 0) {
3036 ret = (status > 0) ? 0 : (int) status;
3037 break;
3038 }
Jordan Crouse92446a62012-11-15 11:00:06 -07003039 time_elapsed += wait;
3040
3041 /* If user specified timestamps are being used, wait at least
3042 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
3043 * issue a IB for a timestamp before checking to see if the
3044 * current timestamp we are waiting for is valid or not
3045 */
3046
3047 if (ts_compare && (adreno_ctx &&
3048 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
3049 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
3050 ret = _check_pending_timestamp(device, context,
3051 timestamp);
3052 if (ret)
3053 break;
3054
3055 /* Don't do this check again */
3056 ts_compare = 0;
3057
3058 /*
3059 * Reset the invalid timestamp flag on a valid
3060 * wait
3061 */
3062 context->wait_on_invalid_ts = false;
3063 }
3064 }
3065
3066 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003067 * We want to wait the floor of KGSL_TIMEOUT_PART
3068 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07003069 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003070
3071 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
3072 wait = KGSL_TIMEOUT_PART;
3073 else
3074 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07003075
3076 } while (!msecs || time_elapsed < msecs);
3077
3078 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003079}
3080
3081static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003082 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003083{
3084 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003085 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003086
Jeremy Gebben9ad86922012-05-08 15:33:23 -06003087 /*
3088 * If the context ID is invalid, we are in a race with
3089 * the context being destroyed by userspace so bail.
3090 */
3091 if (context_id == KGSL_CONTEXT_INVALID) {
3092 KGSL_DRV_WARN(device, "context was detached");
3093 return timestamp;
3094 }
Jordan Crousec659f382012-04-16 11:10:41 -06003095 switch (type) {
3096 case KGSL_TIMESTAMP_QUEUED: {
3097 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3098 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
3099
3100 timestamp = rb->timestamp[context_id];
3101 break;
3102 }
3103 case KGSL_TIMESTAMP_CONSUMED:
Carter Cooper78d8d362013-05-28 17:01:59 -06003104 kgsl_sharedmem_readl(&device->memstore, &timestamp,
3105 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp));
Jordan Crousec659f382012-04-16 11:10:41 -06003106 break;
3107 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003108 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06003109 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
3110 break;
3111 }
3112
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003113 rmb();
3114
3115 return timestamp;
3116}
3117
3118static long adreno_ioctl(struct kgsl_device_private *dev_priv,
3119 unsigned int cmd, void *data)
3120{
3121 int result = 0;
3122 struct kgsl_drawctxt_set_bin_base_offset *binbase;
3123 struct kgsl_context *context;
3124
3125 switch (cmd) {
3126 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
3127 binbase = data;
3128
3129 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
3130 if (context) {
3131 adreno_drawctxt_set_bin_base_offset(
3132 dev_priv->device, context, binbase->offset);
3133 } else {
3134 result = -EINVAL;
3135 KGSL_DRV_ERR(dev_priv->device,
3136 "invalid drawctxt drawctxt_id %d "
3137 "device_id=%d\n",
3138 binbase->drawctxt_id, dev_priv->device->id);
3139 }
3140 break;
3141
3142 default:
3143 KGSL_DRV_INFO(dev_priv->device,
3144 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07003145 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003146 break;
3147 }
3148 return result;
3149
3150}
3151
3152static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
3153{
3154 gpu_freq /= 1000000;
3155 return ticks / gpu_freq;
3156}
3157
3158static void adreno_power_stats(struct kgsl_device *device,
3159 struct kgsl_power_stats *stats)
3160{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003161 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003162 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003163 unsigned int cycles;
3164
3165 /* Get the busy cycles counted since the counter was last reset */
3166 /* Calling this function also resets and restarts the counter */
3167
3168 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003169
3170 /* In order to calculate idle you have to have run the algorithm *
3171 * at least once to get a start time. */
3172 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003173 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003174 stats->total_time = tmp - pwr->time;
3175 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003176 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003177 pwrlevels[device->pwrctrl.active_pwrlevel].
3178 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003179 } else {
3180 stats->total_time = 0;
3181 stats->busy_time = 0;
3182 pwr->time = ktime_to_us(ktime_get());
3183 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003184}
3185
3186void adreno_irqctrl(struct kgsl_device *device, int state)
3187{
Jordan Crousea78c9172011-07-11 13:14:09 -06003188 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3189 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003190}
3191
Jordan Croused6535882012-06-20 08:22:16 -06003192static unsigned int adreno_gpuid(struct kgsl_device *device,
3193 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07003194{
3195 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3196
Jordan Croused6535882012-06-20 08:22:16 -06003197 /* Some applications need to know the chip ID too, so pass
3198 * that as a parameter */
3199
3200 if (chipid != NULL)
3201 *chipid = adreno_dev->chip_id;
3202
Jordan Crousea0758f22011-12-07 11:19:22 -07003203 /* Standard KGSL gpuid format:
3204 * top word is 0x0002 for 2D or 0x0003 for 3D
3205 * Bottom word is core specific identifer
3206 */
3207
3208 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
3209}
3210
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003211static const struct kgsl_functable adreno_functable = {
3212 /* Mandatory functions */
3213 .regread = adreno_regread,
3214 .regwrite = adreno_regwrite,
3215 .idle = adreno_idle,
3216 .isidle = adreno_isidle,
3217 .suspend_context = adreno_suspend_context,
3218 .start = adreno_start,
3219 .stop = adreno_stop,
3220 .getproperty = adreno_getproperty,
3221 .waittimestamp = adreno_waittimestamp,
3222 .readtimestamp = adreno_readtimestamp,
3223 .issueibcmds = adreno_ringbuffer_issueibcmds,
3224 .ioctl = adreno_ioctl,
3225 .setup_pt = adreno_setup_pt,
3226 .cleanup_pt = adreno_cleanup_pt,
3227 .power_stats = adreno_power_stats,
3228 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07003229 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07003230 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06003231 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003232 /* Optional functions */
3233 .setstate = adreno_setstate,
3234 .drawctxt_create = adreno_drawctxt_create,
3235 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06003236 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06003237 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07003238 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003239};
3240
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003241static struct platform_driver adreno_platform_driver = {
3242 .probe = adreno_probe,
3243 .remove = __devexit_p(adreno_remove),
3244 .suspend = kgsl_suspend_driver,
3245 .resume = kgsl_resume_driver,
3246 .id_table = adreno_id_table,
3247 .driver = {
3248 .owner = THIS_MODULE,
3249 .name = DEVICE_3D_NAME,
3250 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06003251 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003252 }
3253};
3254
3255static int __init kgsl_3d_init(void)
3256{
3257 return platform_driver_register(&adreno_platform_driver);
3258}
3259
3260static void __exit kgsl_3d_exit(void)
3261{
3262 platform_driver_unregister(&adreno_platform_driver);
3263}
3264
3265module_init(kgsl_3d_init);
3266module_exit(kgsl_3d_exit);
3267
3268MODULE_DESCRIPTION("3D Graphics driver");
3269MODULE_VERSION("1.2");
3270MODULE_LICENSE("GPL v2");
3271MODULE_ALIAS("platform:kgsl_3d");