blob: 16a10ac6f4537294b00d3a5a2ce80fbec20ab02a [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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021
22#include <mach/socinfo.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060023#include <mach/msm_bus_board.h>
24#include <mach/msm_bus.h>
25#include <mach/msm_dcvs.h>
26#include <mach/msm_dcvs_scm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027
28#include "kgsl.h"
29#include "kgsl_pwrscale.h"
30#include "kgsl_cffdump.h"
31#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060032#include "kgsl_iommu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34#include "adreno.h"
35#include "adreno_pm4types.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070037#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070038#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40#define DRIVER_VERSION_MAJOR 3
41#define DRIVER_VERSION_MINOR 1
42
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043/* Adreno MH arbiter config*/
44#define ADRENO_CFG_MHARB \
45 (0x10 \
46 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
47 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
48 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
49 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
53 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
54 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
55 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
56 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
57 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
58 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
59 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
60
61#define ADRENO_MMU_CONFIG \
62 (0x01 \
63 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
69 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
70 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
71 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
72 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
73 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
74
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075static const struct kgsl_functable adreno_functable;
76
77static struct adreno_device device_3d0 = {
78 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070079 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080 .name = DEVICE_3D0_NAME,
81 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060082 .mh = {
83 .mharb = ADRENO_CFG_MHARB,
84 /* Remove 1k boundary check in z470 to avoid a GPU
85 * hang. Notice that this solution won't work if
86 * both EBI and SMI are used
87 */
88 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089 /* turn off memory protection unit by setting
90 acceptable physical address range to include
91 all pages. */
92 .mpu_base = 0x00000000,
93 .mpu_range = 0xFFFFF000,
94 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060095 .mmu = {
96 .config = ADRENO_MMU_CONFIG,
97 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101 .iomemname = KGSL_3D0_REG_MEMORY,
102 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600104 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
106 .suspend = kgsl_early_suspend_driver,
107 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600109#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600111 .gmem_base = 0,
112 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 .pfp_fw = NULL,
114 .pm4_fw = NULL,
Jordan Crouse21f75a02012-08-09 15:08:59 -0600115 .wait_timeout = 0, /* in milliseconds, 0 means disabled */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600116 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117};
118
Tarun Karra3335f142012-06-19 14:11:48 -0700119/* This set of registers are used for Hang detection
120 * If the values of these registers are same after
121 * KGSL_TIMEOUT_PART time, GPU hang is reported in
122 * kernel log.
Tarun Karra696f89e2013-01-27 21:31:40 -0800123 * *****ALERT******ALERT********ALERT*************
124 * Order of registers below is important, registers
125 * from LONG_IB_DETECT_REG_INDEX_START to
126 * LONG_IB_DETECT_REG_INDEX_END are used in long ib detection.
Tarun Karra3335f142012-06-19 14:11:48 -0700127 */
Tarun Karra696f89e2013-01-27 21:31:40 -0800128#define LONG_IB_DETECT_REG_INDEX_START 1
129#define LONG_IB_DETECT_REG_INDEX_END 5
130
131unsigned int ft_detect_regs[] = {
Tarun Karra3335f142012-06-19 14:11:48 -0700132 A3XX_RBBM_STATUS,
Tarun Karra696f89e2013-01-27 21:31:40 -0800133 REG_CP_RB_RPTR, /* LONG_IB_DETECT_REG_INDEX_START */
Tarun Karra3335f142012-06-19 14:11:48 -0700134 REG_CP_IB1_BASE,
135 REG_CP_IB1_BUFSZ,
136 REG_CP_IB2_BASE,
Tarun Karra696f89e2013-01-27 21:31:40 -0800137 REG_CP_IB2_BUFSZ, /* LONG_IB_DETECT_REG_INDEX_END */
Jordan Crouseb5c80482012-10-03 09:38:41 -0600138 0,
Tarun Karra6e750d72013-01-04 10:28:40 -0800139 0,
140 0,
141 0,
142 0,
Jordan Crouseb5c80482012-10-03 09:38:41 -0600143 0
Tarun Karra3335f142012-06-19 14:11:48 -0700144};
145
Tarun Karra696f89e2013-01-27 21:31:40 -0800146const unsigned int ft_detect_regs_count = ARRAY_SIZE(ft_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700147
Jordan Crouse505df9c2011-07-28 08:37:59 -0600148/*
149 * This is the master list of all GPU cores that are supported by this
150 * driver.
151 */
152
153#define ANY_ID (~0)
Tarun Karra9c070822012-11-27 16:43:51 -0700154#define NO_VER (~0)
Jordan Crouse505df9c2011-07-28 08:37:59 -0600155
156static const struct {
157 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600158 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600159 const char *pm4fw;
160 const char *pfpfw;
161 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700162 unsigned int istore_size;
163 unsigned int pix_shader_start;
Tarun Karra9c070822012-11-27 16:43:51 -0700164 /* Size of an instruction in dwords */
165 unsigned int instruction_size;
166 /* size of gmem for gpu*/
167 unsigned int gmem_size;
168 /* version of pm4 microcode that supports sync_lock
169 between CPU and GPU for SMMU-v1 programming */
170 unsigned int sync_lock_pm4_ver;
171 /* version of pfp microcode that supports sync_lock
172 between CPU and GPU for SMMU-v1 programming */
173 unsigned int sync_lock_pfp_ver;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600174} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600175 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700176 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700177 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530178 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
179 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700180 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600181 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700182 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700183 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600184 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700185 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700186 512, 384, 3, SZ_512K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600187 /*
188 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
189 * a hardware problem.
190 */
191 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700192 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700193 1536, 768, 3, SZ_512K, NO_VER, NO_VER },
Carter Cooperf27ec722011-11-17 15:20:38 -0700194 { ADRENO_REV_A225, 2, 2, 0, 6,
195 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700196 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600197 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700198 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700199 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530200 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530201 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530202 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700203 512, 0, 2, SZ_256K, 0x3FF037, 0x3FF016 },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700204 /* A3XX doesn't use the pix_shader_start */
Carter Cooper95f7f792012-08-19 13:40:34 -0600205 { ADRENO_REV_A320, 3, 2, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700206 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700207 512, 0, 2, SZ_512K, 0x3FF037, 0x3FF016 },
liu zhongfd42e622012-05-01 19:18:30 -0700208 { ADRENO_REV_A330, 3, 3, 0, 0,
209 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700210 512, 0, 2, SZ_1M, NO_VER, NO_VER },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600211};
212
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600213static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214{
Jordan Crousea78c9172011-07-11 13:14:09 -0600215 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600216 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217
Jordan Crousea78c9172011-07-11 13:14:09 -0600218 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219
220 if (device->requested_state == KGSL_STATE_NONE) {
221 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700222 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 queue_work(device->work_queue, &device->idle_check_ws);
224 } else if (device->pwrscale.policy != NULL) {
225 queue_work(device->work_queue, &device->idle_check_ws);
226 }
227 }
228
229 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800230 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231 jiffies + device->pwrctrl.interval_timeout);
232 return result;
233}
234
Jordan Crouse9f739212011-07-28 08:37:57 -0600235static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 struct kgsl_pagetable *pagetable)
237{
238 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
239 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
240
241 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
242
243 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
244
245 kgsl_mmu_unmap(pagetable, &device->memstore);
246
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600247 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248}
249
250static int adreno_setup_pt(struct kgsl_device *device,
251 struct kgsl_pagetable *pagetable)
252{
253 int result = 0;
254 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
255 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
256
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
258 GSL_PT_PAGE_RV);
259 if (result)
260 goto error;
261
262 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
263 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
264 if (result)
265 goto unmap_buffer_desc;
266
267 result = kgsl_mmu_map_global(pagetable, &device->memstore,
268 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
269 if (result)
270 goto unmap_memptrs_desc;
271
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600272 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700273 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
274 if (result)
275 goto unmap_memstore_desc;
276
277 return result;
278
279unmap_memstore_desc:
280 kgsl_mmu_unmap(pagetable, &device->memstore);
281
282unmap_memptrs_desc:
283 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
284
285unmap_buffer_desc:
286 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
287
288error:
289 return result;
290}
291
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600292static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600293 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600294 uint32_t flags)
295{
296 unsigned int pt_val, reg_pt_val;
Tarun Karra9c070822012-11-27 16:43:51 -0700297 unsigned int link[250];
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600298 unsigned int *cmds = &link[0];
299 int sizedwords = 0;
300 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600301 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600302 struct kgsl_context *context;
303 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600304
305 if (!adreno_dev->drawctxt_active)
306 return kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700307 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600308
309 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700310 if (context == NULL)
311 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600312 adreno_ctx = context->devctxt;
313
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600314 if (kgsl_mmu_enable_clk(&device->mmu,
315 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700316 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600317
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600318 cmds += __adreno_add_idle_indirect_cmds(cmds,
319 device->mmu.setstate_memory.gpuaddr +
320 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
321
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600322 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600323 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
324 device->mmu.setstate_memory.gpuaddr +
325 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
326 else
327 cmds += adreno_add_bank_change_cmds(cmds,
328 KGSL_IOMMU_CONTEXT_USER,
329 device->mmu.setstate_memory.gpuaddr +
330 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
331
Tarun Karra9c070822012-11-27 16:43:51 -0700332 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
333
334 /* Acquire GPU-CPU sync Lock here */
335 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
336
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700337 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
338 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600339 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600340 /*
341 * We need to perfrom the following operations for all
342 * IOMMU units
343 */
344 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700345 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
346 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600347 /*
348 * Set address of the new pagetable by writng to IOMMU
349 * TTBR0 register
350 */
351 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700352 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
353 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600354 *cmds++ = reg_pt_val;
355 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
356 *cmds++ = 0x00000000;
357
358 /*
359 * Read back the ttbr0 register as a barrier to ensure
360 * above writes have completed
361 */
362 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700363 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
364 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600365 reg_pt_val,
366 device->mmu.setstate_memory.gpuaddr +
367 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600368 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600369 }
370 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
371 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700372 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600373 */
374 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700375 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
376 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700377
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600378 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700379 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
380 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700381 KGSL_IOMMU_CTX_TLBIALL);
382 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600383
384 cmds += __adreno_add_idle_indirect_cmds(cmds,
385 device->mmu.setstate_memory.gpuaddr +
386 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
387
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600388 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700389 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
390 KGSL_IOMMU_CONTEXT_USER,
391 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700392 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600393 device->mmu.setstate_memory.gpuaddr +
394 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
395 }
396 }
397
Tarun Karra9c070822012-11-27 16:43:51 -0700398 /* Release GPU-CPU sync Lock here */
399 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
400
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600401 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600402 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700403 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
404 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600405 device->mmu.setstate_memory.gpuaddr +
406 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
407 else
408 cmds += adreno_add_bank_change_cmds(cmds,
409 KGSL_IOMMU_CONTEXT_PRIV,
410 device->mmu.setstate_memory.gpuaddr +
411 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
412
Tarun Karra9c070822012-11-27 16:43:51 -0700413 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
414
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600415 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600416 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700417 /* invalidate all base pointers */
418 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
419 *cmds++ = 0x7fff;
420 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600421 /* This returns the per context timestamp but we need to
422 * use the global timestamp for iommu clock disablement */
423 adreno_ringbuffer_issuecmds(device, adreno_ctx,
424 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600425 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600426 kgsl_mmu_disable_clk_on_ts(&device->mmu,
427 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600428 }
Tarun Karra9c070822012-11-27 16:43:51 -0700429
430 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
431 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
432 BUG();
433 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600434}
435
436static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600437 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600438 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439{
440 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
441 unsigned int link[32];
442 unsigned int *cmds = &link[0];
443 int sizedwords = 0;
444 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600445 struct kgsl_context *context;
446 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600448 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530449 * Fix target freeze issue by adding TLB flush for each submit
450 * on A20X based targets.
451 */
452 if (adreno_is_a20x(adreno_dev))
453 flags |= KGSL_MMUFLAGS_TLBFLUSH;
454 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600455 * If possible, then set the state via the command stream to avoid
456 * a CPU idle. Otherwise, use the default setstate which uses register
457 * writes For CFF dump we must idle and use the registers so that it is
458 * easier to filter out the mmu accesses from the dump
459 */
460 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600461 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700462 if (context == NULL)
463 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600464 adreno_ctx = context->devctxt;
465
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
467 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600468 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 *cmds++ = 0x00000000;
470
471 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600472 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700473 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600474 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 sizedwords += 4;
476 }
477
478 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
479 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600480 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 1);
482 *cmds++ = 0x00000000;
483 sizedwords += 2;
484 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600485 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 *cmds++ = mh_mmu_invalidate;
487 sizedwords += 2;
488 }
489
490 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600491 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 /* HW workaround: to resolve MMU page fault interrupts
493 * caused by the VGT.It prevents the CP PFP from filling
494 * the VGT DMA request fifo too early,thereby ensuring
495 * that the VGT will not fetch vertex/bin data until
496 * after the page table base register has been updated.
497 *
498 * Two null DRAW_INDX_BIN packets are inserted right
499 * after the page table base update, followed by a
500 * wait for idle. The null packets will fill up the
501 * VGT DMA request fifo and prevent any further
502 * vertex/bin updates from occurring until the wait
503 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600504 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 *cmds++ = (0x4 << 16) |
506 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
507 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600508 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600509 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600510 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 *cmds++ = 0; /* viz query info */
512 *cmds++ = 0x0003C004; /* draw indicator */
513 *cmds++ = 0; /* bin base */
514 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600515 *cmds++ =
516 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600518 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 *cmds++ = 0; /* viz query info */
520 *cmds++ = 0x0003C004; /* draw indicator */
521 *cmds++ = 0; /* bin base */
522 *cmds++ = 3; /* bin size */
523 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600524 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600526 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 *cmds++ = 0x00000000;
528 sizedwords += 21;
529 }
530
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600531
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600533 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534 *cmds++ = 0x7fff; /* invalidate all base pointers */
535 sizedwords += 2;
536 }
537
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600538 adreno_ringbuffer_issuecmds(device, adreno_ctx,
539 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600541 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600542 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600543 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544}
545
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600546static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600547 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600548 uint32_t flags)
549{
550 /* call the mmu specific handler */
551 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600552 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600553 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600554 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600555}
556
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700558a3xx_getchipid(struct kgsl_device *device)
559{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600560 struct kgsl_device_platform_data *pdata =
561 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700562
Jordan Crouse54154c62012-03-27 16:33:26 -0600563 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600564 * All current A3XX chipids are detected at the SOC level. Leave this
565 * function here to support any future GPUs that have working
566 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600567 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700568
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600569 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700570}
571
572static unsigned int
573a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574{
575 unsigned int chipid = 0;
576 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600577 struct kgsl_device_platform_data *pdata =
578 kgsl_device_get_drvdata(device);
579
580 /* If the chip id is set at the platform level, then just use that */
581
582 if (pdata->chipid != 0)
583 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584
585 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
586 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
587 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
588
589 /*
590 * adreno 22x gpus are indicated by coreid 2,
591 * but REG_RBBM_PERIPHID1 always contains 0 for this field
592 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600593 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 chipid = 2 << 24;
595 else
596 chipid = (coreid & 0xF) << 24;
597
598 chipid |= ((majorid >> 4) & 0xF) << 16;
599
600 minorid = ((revid >> 0) & 0xFF);
601
602 patchid = ((revid >> 16) & 0xFF);
603
604 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530605 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606 if (cpu_is_qsd8x50())
607 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530608 else if (cpu_is_msm8625() && minorid == 0)
609 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610
611 chipid |= (minorid << 8) | patchid;
612
613 return chipid;
614}
615
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700616static unsigned int
617adreno_getchipid(struct kgsl_device *device)
618{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600619 struct kgsl_device_platform_data *pdata =
620 kgsl_device_get_drvdata(device);
621
622 /*
623 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
624 * an A2XX processor
625 */
626
627 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700628 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600629 else
630 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700631}
632
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633static inline bool _rev_match(unsigned int id, unsigned int entry)
634{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600635 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637
638static void
639adreno_identify_gpu(struct adreno_device *adreno_dev)
640{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600641 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642
643 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
644
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600645 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
646 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
647 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
648 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649
Jordan Crouse505df9c2011-07-28 08:37:59 -0600650 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
651 if (core == adreno_gpulist[i].core &&
652 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600653 _rev_match(minor, adreno_gpulist[i].minor) &&
654 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 }
657
Jordan Crouse505df9c2011-07-28 08:37:59 -0600658 if (i == ARRAY_SIZE(adreno_gpulist)) {
659 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
660 return;
661 }
662
663 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
664 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
665 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
666 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700667 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
668 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700669 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600670 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Tarun Karra9c070822012-11-27 16:43:51 -0700671 adreno_dev->gpulist_index = i;
672
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673}
674
Lokesh Batra805e1e12012-08-03 08:34:06 -0600675static struct platform_device_id adreno_id_table[] = {
676 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
677 {},
678};
679
680MODULE_DEVICE_TABLE(platform, adreno_id_table);
681
682static struct of_device_id adreno_match_table[] = {
683 { .compatible = "qcom,kgsl-3d0", },
684 {}
685};
686
687static inline int adreno_of_read_property(struct device_node *node,
688 const char *prop, unsigned int *ptr)
689{
690 int ret = of_property_read_u32(node, prop, ptr);
691 if (ret)
692 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
693 return ret;
694}
695
696static struct device_node *adreno_of_find_subnode(struct device_node *parent,
697 const char *name)
698{
699 struct device_node *child;
700
701 for_each_child_of_node(parent, child) {
702 if (of_device_is_compatible(child, name))
703 return child;
704 }
705
706 return NULL;
707}
708
709static int adreno_of_get_pwrlevels(struct device_node *parent,
710 struct kgsl_device_platform_data *pdata)
711{
712 struct device_node *node, *child;
713 int ret = -EINVAL;
714
715 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
716
717 if (node == NULL) {
718 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
719 return -EINVAL;
720 }
721
722 pdata->num_levels = 0;
723
724 for_each_child_of_node(node, child) {
725 unsigned int index;
726 struct kgsl_pwrlevel *level;
727
728 if (adreno_of_read_property(child, "reg", &index))
729 goto done;
730
731 if (index >= KGSL_MAX_PWRLEVELS) {
732 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
733 index);
734 continue;
735 }
736
737 if (index >= pdata->num_levels)
738 pdata->num_levels = index + 1;
739
740 level = &pdata->pwrlevel[index];
741
742 if (adreno_of_read_property(child, "qcom,gpu-freq",
743 &level->gpu_freq))
744 goto done;
745
746 if (adreno_of_read_property(child, "qcom,bus-freq",
747 &level->bus_freq))
748 goto done;
749
750 if (adreno_of_read_property(child, "qcom,io-fraction",
751 &level->io_fraction))
752 level->io_fraction = 0;
753 }
754
755 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
756 &pdata->init_level))
757 pdata->init_level = 1;
758
759 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
760 KGSL_CORE_ERR("Initial power level out of range\n");
761 pdata->init_level = 1;
762 }
763
764 ret = 0;
765done:
766 return ret;
767
768}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600769
770static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
771{
772 struct device_node *node, *child;
773 struct msm_dcvs_core_info *info = NULL;
774 int count = 0;
775 int ret = -EINVAL;
776
777 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
778 if (node == NULL)
779 return ERR_PTR(-EINVAL);
780
781 info = kzalloc(sizeof(*info), GFP_KERNEL);
782
783 if (info == NULL) {
784 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
785 ret = -ENOMEM;
786 goto err;
787 }
788
789 for_each_child_of_node(node, child)
790 count++;
791
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700792 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600793
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700794 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600795 sizeof(struct msm_dcvs_freq_entry),
796 GFP_KERNEL);
797
798 if (info->freq_tbl == NULL) {
799 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700800 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600801 sizeof(struct msm_dcvs_freq_entry));
802 ret = -ENOMEM;
803 goto err;
804 }
805
806 for_each_child_of_node(node, child) {
807 unsigned int index;
808
809 if (adreno_of_read_property(child, "reg", &index))
810 goto err;
811
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700812 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600813 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
814 index);
815 continue;
816 }
817
818 if (adreno_of_read_property(child, "qcom,freq",
819 &info->freq_tbl[index].freq))
820 goto err;
821
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700822 if (adreno_of_read_property(child, "qcom,voltage",
823 &info->freq_tbl[index].voltage))
824 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600825
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700826 if (adreno_of_read_property(child, "qcom,is_trans_level",
827 &info->freq_tbl[index].is_trans_level))
828 info->freq_tbl[index].is_trans_level = 0;
829
830 if (adreno_of_read_property(child, "qcom,active-energy-offset",
831 &info->freq_tbl[index].active_energy_offset))
832 info->freq_tbl[index].active_energy_offset = 0;
833
834 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
835 &info->freq_tbl[index].leakage_energy_offset))
836 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600837 }
838
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700839 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
840 goto err;
841
842 info->sensors = kzalloc(info->num_cores *
843 sizeof(int),
844 GFP_KERNEL);
845
846 for (count = 0; count < info->num_cores; count++) {
847 if (adreno_of_read_property(node, "qcom,sensors",
848 &(info->sensors[count])))
849 goto err;
850 }
851
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700852 if (adreno_of_read_property(node, "qcom,core-core-type",
853 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600854 goto err;
855
856 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
857 &info->algo_param.disable_pc_threshold))
858 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700859 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
860 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600861 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700862 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
863 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600864 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600865 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
866 &info->algo_param.em_max_util_pct))
867 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700868 if (adreno_of_read_property(node, "qcom,algo-group-id",
869 &info->algo_param.group_id))
870 goto err;
871 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
872 &info->algo_param.max_freq_chg_time_us))
873 goto err;
874 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
875 &info->algo_param.slack_mode_dynamic))
876 goto err;
877 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
878 &info->algo_param.slack_weight_thresh_pct))
879 goto err;
880 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
881 &info->algo_param.slack_time_min_us))
882 goto err;
883 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
884 &info->algo_param.slack_time_max_us))
885 goto err;
886 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
887 &info->algo_param.ss_win_size_min_us))
888 goto err;
889 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
890 &info->algo_param.ss_win_size_max_us))
891 goto err;
892 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
893 &info->algo_param.ss_util_pct))
894 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800895 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
896 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600897 goto err;
898
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700899 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
900 &info->energy_coeffs.active_coeff_a))
901 goto err;
902 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
903 &info->energy_coeffs.active_coeff_b))
904 goto err;
905 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
906 &info->energy_coeffs.active_coeff_c))
907 goto err;
908 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
909 &info->energy_coeffs.leakage_coeff_a))
910 goto err;
911 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
912 &info->energy_coeffs.leakage_coeff_b))
913 goto err;
914 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
915 &info->energy_coeffs.leakage_coeff_c))
916 goto err;
917 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
918 &info->energy_coeffs.leakage_coeff_d))
919 goto err;
920
921 if (adreno_of_read_property(node, "qcom,power-current-temp",
922 &info->power_param.current_temp))
923 goto err;
924
Lokesh Batra805e1e12012-08-03 08:34:06 -0600925 return info;
926
927err:
928 if (info)
929 kfree(info->freq_tbl);
930
931 kfree(info);
932
933 return ERR_PTR(ret);
934}
935
936static int adreno_of_get_iommu(struct device_node *parent,
937 struct kgsl_device_platform_data *pdata)
938{
939 struct device_node *node, *child;
940 struct kgsl_device_iommu_data *data = NULL;
941 struct kgsl_iommu_ctx *ctxs = NULL;
942 u32 reg_val[2];
943 int ctx_index = 0;
944
945 node = of_parse_phandle(parent, "iommu", 0);
946 if (node == NULL)
947 return -EINVAL;
948
949 data = kzalloc(sizeof(*data), GFP_KERNEL);
950 if (data == NULL) {
951 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
952 goto err;
953 }
954
955 if (of_property_read_u32_array(node, "reg", reg_val, 2))
956 goto err;
957
958 data->physstart = reg_val[0];
959 data->physend = data->physstart + reg_val[1] - 1;
960
961 data->iommu_ctx_count = 0;
962
963 for_each_child_of_node(node, child)
964 data->iommu_ctx_count++;
965
966 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
967 GFP_KERNEL);
968
969 if (ctxs == NULL) {
970 KGSL_CORE_ERR("kzalloc(%d) failed\n",
971 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
972 goto err;
973 }
974
975 for_each_child_of_node(node, child) {
976 int ret = of_property_read_string(child, "label",
977 &ctxs[ctx_index].iommu_ctx_name);
978
979 if (ret) {
980 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
981 goto err;
982 }
983
984 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
985 &ctxs[ctx_index].ctx_id))
986 goto err;
987
988 ctx_index++;
989 }
990
991 data->iommu_ctxs = ctxs;
992
993 pdata->iommu_data = data;
994 pdata->iommu_count = 1;
995
996 return 0;
997
998err:
999 kfree(ctxs);
1000 kfree(data);
1001
1002 return -EINVAL;
1003}
1004
1005static int adreno_of_get_pdata(struct platform_device *pdev)
1006{
1007 struct kgsl_device_platform_data *pdata = NULL;
1008 struct kgsl_device *device;
1009 int ret = -EINVAL;
1010
1011 pdev->id_entry = adreno_id_table;
1012
1013 pdata = pdev->dev.platform_data;
1014 if (pdata)
1015 return 0;
1016
1017 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1018 KGSL_CORE_ERR("Unable to read 'label'\n");
1019 goto err;
1020 }
1021
1022 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1023 goto err;
1024
1025 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1026 if (pdata == NULL) {
1027 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1028 ret = -ENOMEM;
1029 goto err;
1030 }
1031
1032 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1033 &pdata->chipid))
1034 goto err;
1035
1036 /* pwrlevel Data */
1037 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1038 if (ret)
1039 goto err;
1040
1041 /* Default value is 83, if not found in DT */
1042 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1043 &pdata->idle_timeout))
1044 pdata->idle_timeout = 83;
1045
1046 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1047 &pdata->nap_allowed))
1048 pdata->nap_allowed = 1;
1049
1050 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1051 &pdata->clk_map))
1052 goto err;
1053
1054 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1055
1056 if (device->id != KGSL_DEVICE_3D0)
1057 goto err;
1058
1059 /* Bus Scale Data */
1060
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001061 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001062 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1063 ret = PTR_ERR(pdata->bus_scale_table);
1064 goto err;
1065 }
1066
1067 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1068 if (IS_ERR_OR_NULL(pdata->core_info)) {
1069 ret = PTR_ERR(pdata->core_info);
1070 goto err;
1071 }
1072
1073 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1074 if (ret)
1075 goto err;
1076
1077 pdev->dev.platform_data = pdata;
1078 return 0;
1079
1080err:
1081 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001082 if (pdata->core_info)
1083 kfree(pdata->core_info->freq_tbl);
1084 kfree(pdata->core_info);
1085
1086 if (pdata->iommu_data)
1087 kfree(pdata->iommu_data->iommu_ctxs);
1088
1089 kfree(pdata->iommu_data);
1090 }
1091
1092 kfree(pdata);
1093
1094 return ret;
1095}
1096
liu zhong7dfa2a32012-04-27 19:11:01 -07001097#ifdef CONFIG_MSM_OCMEM
1098static int
1099adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1100{
Jordan Crousec0978202012-08-29 14:35:51 -06001101 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001102 return 0;
1103
1104 /* OCMEM is only needed once, do not support consective allocation */
1105 if (adreno_dev->ocmem_hdl != NULL)
1106 return 0;
1107
1108 adreno_dev->ocmem_hdl =
1109 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1110 if (adreno_dev->ocmem_hdl == NULL)
1111 return -ENOMEM;
1112
1113 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001114 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001115
1116 return 0;
1117}
1118
1119static void
1120adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1121{
Jordan Crousec0978202012-08-29 14:35:51 -06001122 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001123 return;
1124
1125 if (adreno_dev->ocmem_hdl == NULL)
1126 return;
1127
1128 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1129 adreno_dev->ocmem_hdl = NULL;
1130}
1131#else
1132static int
1133adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1134{
1135 return 0;
1136}
1137
1138static void
1139adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1140{
1141}
1142#endif
1143
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144static int __devinit
1145adreno_probe(struct platform_device *pdev)
1146{
1147 struct kgsl_device *device;
1148 struct adreno_device *adreno_dev;
1149 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001150 bool is_dt;
1151
1152 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1153
1154 if (is_dt && pdev->dev.of_node) {
1155 status = adreno_of_get_pdata(pdev);
1156 if (status)
1157 goto error_return;
1158 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159
1160 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1161 adreno_dev = ADRENO_DEVICE(device);
1162 device->parentdev = &pdev->dev;
1163
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 status = adreno_ringbuffer_init(device);
1165 if (status != 0)
1166 goto error;
1167
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001168 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169 if (status)
1170 goto error_close_rb;
1171
1172 adreno_debugfs_init(device);
1173
1174 kgsl_pwrscale_init(device);
1175 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1176
1177 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1178 return 0;
1179
1180error_close_rb:
1181 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1182error:
1183 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001184error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001185 return status;
1186}
1187
1188static int __devexit adreno_remove(struct platform_device *pdev)
1189{
1190 struct kgsl_device *device;
1191 struct adreno_device *adreno_dev;
1192
1193 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1194 adreno_dev = ADRENO_DEVICE(device);
1195
1196 kgsl_pwrscale_detach_policy(device);
1197 kgsl_pwrscale_close(device);
1198
1199 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1200 kgsl_device_platform_remove(device);
1201
1202 return 0;
1203}
1204
1205static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1206{
1207 int status = -EINVAL;
1208 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209
Tarun Karrad20d71a2013-01-25 15:38:57 -08001210 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001211 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001212
1213 /* Power up the device */
1214 kgsl_pwrctrl_enable(device);
1215
1216 /* Identify the specific GPU */
1217 adreno_identify_gpu(adreno_dev);
1218
Tarun Karra9c070822012-11-27 16:43:51 -07001219 if (adreno_ringbuffer_read_pm4_ucode(device)) {
1220 KGSL_DRV_ERR(device, "Reading pm4 microcode failed %s\n",
1221 adreno_dev->pm4_fwfile);
1222 BUG_ON(1);
1223 }
1224
1225 if (adreno_ringbuffer_read_pfp_ucode(device)) {
1226 KGSL_DRV_ERR(device, "Reading pfp microcode failed %s\n",
1227 adreno_dev->pfp_fwfile);
1228 BUG_ON(1);
1229 }
1230
Jordan Crouse505df9c2011-07-28 08:37:59 -06001231 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1232 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1233 adreno_dev->chip_id);
1234 goto error_clk_off;
1235 }
1236
Tarun Karra9c070822012-11-27 16:43:51 -07001237
1238 /*
1239 * Check if firmware supports the sync lock PM4 packets needed
1240 * for IOMMUv1
1241 */
1242
1243 if ((adreno_dev->pm4_fw_version >=
1244 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pm4_ver) &&
1245 (adreno_dev->pfp_fw_version >=
1246 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pfp_ver))
1247 device->mmu.flags |= KGSL_MMU_FLAGS_IOMMU_SYNC;
1248
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001249 /* Set up the MMU */
1250 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001251 /*
1252 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1253 * on older gpus
1254 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001255 if (adreno_is_a20x(adreno_dev)) {
1256 device->mh.mh_intf_cfg1 = 0;
1257 device->mh.mh_intf_cfg2 = 0;
1258 }
1259
1260 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001261 }
1262
Tarun Karra3335f142012-06-19 14:11:48 -07001263 /* Assign correct RBBM status register to hang detect regs
1264 */
Tarun Karra696f89e2013-01-27 21:31:40 -08001265 ft_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
Tarun Karra3335f142012-06-19 14:11:48 -07001266
Jordan Crouseb5c80482012-10-03 09:38:41 -06001267 /* Add A3XX specific registers for hang detection */
1268 if (adreno_is_a3xx(adreno_dev)) {
Tarun Karra696f89e2013-01-27 21:31:40 -08001269 ft_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1270 ft_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
1271 ft_detect_regs[8] = A3XX_RBBM_PERFCTR_SP_6_LO;
1272 ft_detect_regs[9] = A3XX_RBBM_PERFCTR_SP_6_HI;
1273 ft_detect_regs[10] = A3XX_RBBM_PERFCTR_SP_5_LO;
1274 ft_detect_regs[11] = A3XX_RBBM_PERFCTR_SP_5_HI;
Jordan Crouseb5c80482012-10-03 09:38:41 -06001275 }
1276
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001277 status = kgsl_mmu_start(device);
1278 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001279 goto error_clk_off;
1280
liu zhong7dfa2a32012-04-27 19:11:01 -07001281 status = adreno_ocmem_gmem_malloc(adreno_dev);
1282 if (status) {
1283 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1284 goto error_mmu_off;
1285 }
1286
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001287 /* Start the GPU */
1288 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289
1290 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001291 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292
1293 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001294 if (status == 0) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001295 /* While fault tolerance is on we do not want timer to
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001296 * fire and attempt to change any device state */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001297 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001298 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001299 return 0;
1300 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001303
1304error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001305 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001306
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307error_clk_off:
1308 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309
1310 return status;
1311}
1312
1313static int adreno_stop(struct kgsl_device *device)
1314{
1315 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317 adreno_dev->drawctxt_active = NULL;
1318
1319 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1320
Shubhraprakash Das79447952012-04-26 18:12:23 -06001321 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001323 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301324 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001325 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001326
liu zhong7dfa2a32012-04-27 19:11:01 -07001327 adreno_ocmem_gmem_free(adreno_dev);
1328
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 /* Power down the device */
1330 kgsl_pwrctrl_disable(device);
1331
1332 return 0;
1333}
1334
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001335static void adreno_mark_context_status(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001336 int ft_status)
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001337{
1338 struct kgsl_context *context;
1339 int next = 0;
1340 /*
1341 * Set the reset status of all contexts to
1342 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
Tarun Karrad20d71a2013-01-25 15:38:57 -08001343 * since thats the guilty party, if fault tolerance failed then
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001344 * mark all as guilty
1345 */
1346 while ((context = idr_get_next(&device->context_idr, &next))) {
1347 struct adreno_context *adreno_context = context->devctxt;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001348 if (ft_status) {
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001349 context->reset_status =
1350 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1351 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1352 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1353 context->reset_status) {
Carter Cooper8179f5a2012-12-17 11:32:27 -07001354 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG |
Tarun Karrad20d71a2013-01-25 15:38:57 -08001355 CTXT_FLAGS_GPU_HANG_FT))
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001356 context->reset_status =
1357 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1358 else
1359 context->reset_status =
1360 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1361 }
1362 next = next + 1;
1363 }
1364}
1365
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001366static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1367{
1368 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1369 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1370 struct kgsl_context *context;
1371 struct adreno_context *temp_adreno_context;
1372 int next = 0;
1373
1374 while ((context = idr_get_next(&device->context_idr, &next))) {
1375 temp_adreno_context = context->devctxt;
1376 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1377 kgsl_sharedmem_writel(&device->memstore,
1378 KGSL_MEMSTORE_OFFSET(context->id,
1379 soptimestamp),
1380 rb->timestamp[context->id]);
1381 kgsl_sharedmem_writel(&device->memstore,
1382 KGSL_MEMSTORE_OFFSET(context->id,
1383 eoptimestamp),
1384 rb->timestamp[context->id]);
1385 }
1386 next = next + 1;
1387 }
1388}
1389
Tarun Karrad20d71a2013-01-25 15:38:57 -08001390static void adreno_destroy_ft_data(struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001391{
Tarun Karrad20d71a2013-01-25 15:38:57 -08001392 vfree(ft_data->rb_buffer);
1393 vfree(ft_data->bad_rb_buffer);
1394 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001395}
1396
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001397static int _find_start_of_cmd_seq(struct adreno_ringbuffer *rb,
1398 unsigned int *ptr,
1399 bool inc)
1400{
1401 int status = -EINVAL;
1402 unsigned int val1;
1403 unsigned int size = rb->buffer_desc.size;
1404 unsigned int start_ptr = *ptr;
1405
1406 while ((start_ptr / sizeof(unsigned int)) != rb->wptr) {
1407 if (inc)
1408 start_ptr = adreno_ringbuffer_inc_wrapped(start_ptr,
1409 size);
1410 else
1411 start_ptr = adreno_ringbuffer_dec_wrapped(start_ptr,
1412 size);
1413 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, start_ptr);
1414 if (KGSL_CMD_IDENTIFIER == val1) {
1415 if ((start_ptr / sizeof(unsigned int)) != rb->wptr)
1416 start_ptr = adreno_ringbuffer_dec_wrapped(
1417 start_ptr, size);
1418 *ptr = start_ptr;
1419 status = 0;
1420 break;
1421 }
1422 }
1423 return status;
1424}
1425
1426static int _find_cmd_seq_after_eop_ts(struct adreno_ringbuffer *rb,
1427 unsigned int *rb_rptr,
1428 unsigned int global_eop,
1429 bool inc)
1430{
1431 int status = -EINVAL;
1432 unsigned int temp_rb_rptr = *rb_rptr;
1433 unsigned int size = rb->buffer_desc.size;
1434 unsigned int val[3];
1435 int i = 0;
1436 bool check = false;
1437
1438 if (inc && temp_rb_rptr / sizeof(unsigned int) != rb->wptr)
1439 return status;
1440
1441 do {
1442 /*
1443 * when decrementing we need to decrement first and
1444 * then read make sure we cover all the data
1445 */
1446 if (!inc)
1447 temp_rb_rptr = adreno_ringbuffer_dec_wrapped(
1448 temp_rb_rptr, size);
1449 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i],
1450 temp_rb_rptr);
1451
1452 if (check && ((inc && val[i] == global_eop) ||
1453 (!inc && (val[i] ==
1454 cp_type3_packet(CP_MEM_WRITE, 2) ||
1455 val[i] == CACHE_FLUSH_TS)))) {
1456 /* decrement i, i.e i = (i - 1 + 3) % 3 if
1457 * we are going forward, else increment i */
1458 i = (i + 2) % 3;
1459 if (val[i] == rb->device->memstore.gpuaddr +
1460 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1461 eoptimestamp)) {
1462 int j = ((i + 2) % 3);
1463 if ((inc && (val[j] == CACHE_FLUSH_TS ||
1464 val[j] == cp_type3_packet(
1465 CP_MEM_WRITE, 2))) ||
1466 (!inc && val[j] == global_eop)) {
1467 /* Found the global eop */
1468 status = 0;
1469 break;
1470 }
1471 }
1472 /* if no match found then increment i again
1473 * since we decremented before matching */
1474 i = (i + 1) % 3;
1475 }
1476 if (inc)
1477 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(
1478 temp_rb_rptr, size);
1479
1480 i = (i + 1) % 3;
1481 if (2 == i)
1482 check = true;
1483 } while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr);
1484 /* temp_rb_rptr points to the command stream after global eop,
1485 * move backward till the start of command sequence */
1486 if (!status) {
1487 status = _find_start_of_cmd_seq(rb, &temp_rb_rptr, false);
1488 if (!status) {
1489 *rb_rptr = temp_rb_rptr;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001490 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001491 "Offset of cmd sequence after eop timestamp: 0x%x\n",
1492 temp_rb_rptr / sizeof(unsigned int));
1493 }
1494 }
1495 if (status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001496 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001497 "Failed to find the command sequence after eop timestamp\n");
1498 return status;
1499}
1500
1501static int _find_hanging_ib_sequence(struct adreno_ringbuffer *rb,
1502 unsigned int *rb_rptr,
1503 unsigned int ib1)
1504{
1505 int status = -EINVAL;
1506 unsigned int temp_rb_rptr = *rb_rptr;
1507 unsigned int size = rb->buffer_desc.size;
1508 unsigned int val[2];
1509 int i = 0;
1510 bool check = false;
1511 bool ctx_switch = false;
1512
1513 while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr) {
1514 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i], temp_rb_rptr);
1515
1516 if (check && val[i] == ib1) {
1517 /* decrement i, i.e i = (i - 1 + 2) % 2 */
1518 i = (i + 1) % 2;
1519 if (adreno_cmd_is_ib(val[i])) {
1520 /* go till start of command sequence */
1521 status = _find_start_of_cmd_seq(rb,
1522 &temp_rb_rptr, false);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001523
1524 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001525 "Found the hanging IB at offset 0x%x\n",
1526 temp_rb_rptr / sizeof(unsigned int));
1527 break;
1528 }
1529 /* if no match the increment i since we decremented
1530 * before checking */
1531 i = (i + 1) % 2;
1532 }
1533 /* Make sure you do not encounter a context switch twice, we can
1534 * encounter it once for the bad context as the start of search
1535 * can point to the context switch */
1536 if (val[i] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
1537 if (ctx_switch) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001538 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001539 "Context switch encountered before bad "
1540 "IB found\n");
1541 break;
1542 }
1543 ctx_switch = true;
1544 }
1545 i = (i + 1) % 2;
1546 if (1 == i)
1547 check = true;
1548 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(temp_rb_rptr,
1549 size);
1550 }
1551 if (!status)
1552 *rb_rptr = temp_rb_rptr;
1553 return status;
1554}
1555
Tarun Karrad20d71a2013-01-25 15:38:57 -08001556static int adreno_setup_ft_data(struct kgsl_device *device,
1557 struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001558{
1559 int ret = 0;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001560 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1561 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001562 struct kgsl_context *context;
1563 struct adreno_context *adreno_context;
1564 unsigned int rb_rptr = rb->wptr * sizeof(unsigned int);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001565
Tarun Karrad20d71a2013-01-25 15:38:57 -08001566 memset(ft_data, 0, sizeof(*ft_data));
1567 ft_data->start_of_replay_cmds = 0xFFFFFFFF;
1568 ft_data->replay_for_snapshot = 0xFFFFFFFF;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001569
Tarun Karrad20d71a2013-01-25 15:38:57 -08001570 adreno_regread(device, REG_CP_IB1_BASE, &ft_data->ib1);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001571
Tarun Karrad20d71a2013-01-25 15:38:57 -08001572 kgsl_sharedmem_readl(&device->memstore, &ft_data->context_id,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001573 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1574 current_context));
1575
1576 kgsl_sharedmem_readl(&device->memstore,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001577 &ft_data->global_eop,
Tarun Karradeeecc02013-01-21 23:42:17 -08001578 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1579 eoptimestamp));
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001580
Tarun Karrad20d71a2013-01-25 15:38:57 -08001581 ft_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1582 if (!ft_data->rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001583 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1584 rb->buffer_desc.size);
1585 return -ENOMEM;
1586 }
1587
Tarun Karrad20d71a2013-01-25 15:38:57 -08001588 ft_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1589 if (!ft_data->bad_rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001590 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1591 rb->buffer_desc.size);
1592 ret = -ENOMEM;
1593 goto done;
1594 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001595
Tarun Karrad20d71a2013-01-25 15:38:57 -08001596 ft_data->good_rb_buffer = vmalloc(rb->buffer_desc.size);
1597 if (!ft_data->good_rb_buffer) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001598 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1599 rb->buffer_desc.size);
1600 ret = -ENOMEM;
1601 goto done;
1602 }
Tarun Karra3164fb02013-02-05 15:38:51 -08001603
1604 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001605
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001606 /* find the start of bad command sequence in rb */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001607 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001608 /* Look for the command stream that is right after the global eop */
1609
1610 if (!context) {
1611 /*
Tarun Karrad20d71a2013-01-25 15:38:57 -08001612 * If there is no context then fault tolerance does not need to
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001613 * replay anything, just reset GPU and thats it
1614 */
1615 goto done;
1616 }
1617 ret = _find_cmd_seq_after_eop_ts(rb, &rb_rptr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001618 ft_data->global_eop + 1, false);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001619 if (ret)
1620 goto done;
1621
Tarun Karrad20d71a2013-01-25 15:38:57 -08001622 ft_data->start_of_replay_cmds = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001623
Tarun Karra3164fb02013-02-05 15:38:51 -08001624 if (!adreno_dev->ft_policy)
1625 adreno_dev->ft_policy = KGSL_FT_DEFAULT_POLICY;
1626
1627 ft_data->ft_policy = adreno_dev->ft_policy;
1628
1629
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001630 adreno_context = context->devctxt;
1631 if (adreno_context->flags & CTXT_FLAGS_PREAMBLE) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001632 if (ft_data->ib1) {
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001633 ret = _find_hanging_ib_sequence(rb,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001634 &rb_rptr, ft_data->ib1);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001635 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001636 KGSL_FT_ERR(device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001637 "Start not found for replay IB sequence\n");
1638 ret = 0;
1639 goto done;
1640 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001641 ft_data->start_of_replay_cmds = rb_rptr;
1642 ft_data->replay_for_snapshot = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001643 }
1644 }
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001645
1646done:
1647 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001648 vfree(ft_data->rb_buffer);
1649 vfree(ft_data->bad_rb_buffer);
1650 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001651 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001652 return ret;
1653}
1654
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001655static int
Tarun Karra696f89e2013-01-27 21:31:40 -08001656_adreno_check_long_ib(struct kgsl_device *device)
1657{
1658 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1659 unsigned int curr_global_ts = 0;
1660
1661 /* check if the global ts is still the same */
1662 kgsl_sharedmem_readl(&device->memstore,
1663 &curr_global_ts,
1664 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1665 eoptimestamp));
1666
1667 /* Mark long ib as handled */
1668 adreno_dev->long_ib = 0;
1669
1670 if (curr_global_ts == adreno_dev->long_ib_ts) {
1671 KGSL_FT_ERR(device,
1672 "IB ran too long, invalidate ctxt\n");
1673 return 1;
1674 } else {
1675 /* Do nothing GPU has gone ahead */
1676 KGSL_FT_INFO(device, "false long ib detection return\n");
1677 return 0;
1678 }
1679}
1680
1681static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001682_adreno_ft_restart_device(struct kgsl_device *device,
Tarun Karra696f89e2013-01-27 21:31:40 -08001683 struct kgsl_context *context,
1684 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001685{
Tarun Karradeeecc02013-01-21 23:42:17 -08001686
1687 struct adreno_context *adreno_context = context->devctxt;
1688
1689 /* restart device */
1690 if (adreno_stop(device)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001691 KGSL_FT_ERR(device, "Device stop failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001692 return 1;
1693 }
1694
1695 if (adreno_start(device, true)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001696 KGSL_FT_ERR(device, "Device start failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001697 return 1;
1698 }
1699
1700 if (context)
1701 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1702 KGSL_MEMSTORE_GLOBAL);
1703
1704 /* If iommu is used then we need to make sure that the iommu clocks
1705 * are on since there could be commands in pipeline that touch iommu */
1706 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1707 if (kgsl_mmu_enable_clk(&device->mmu,
1708 KGSL_IOMMU_CONTEXT_USER))
1709 return 1;
1710 }
1711
1712 return 0;
1713}
1714
Tarun Karra696f89e2013-01-27 21:31:40 -08001715static inline void
1716_adreno_debug_ft_info(struct kgsl_device *device,
1717 struct adreno_ft_data *ft_data)
1718{
1719
1720 /*
1721 * Dumping rb is a very useful tool to debug FT.
1722 * It will tell us if we are extracting the rb correctly
1723 * NOP'ing the right IB, skipping the EOF correctly etc.
1724 */
1725 if (device->ft_log >= 7) {
1726
1727 /* Print fault tolerance data here */
1728 KGSL_FT_INFO(device, "Temp RB buffer size 0x%X\n",
1729 ft_data->rb_size);
1730 adreno_dump_rb(device, ft_data->rb_buffer,
1731 ft_data->rb_size<<2, 0, ft_data->rb_size);
1732
1733 KGSL_FT_INFO(device, "Bad RB buffer size 0x%X\n",
1734 ft_data->bad_rb_size);
1735 adreno_dump_rb(device, ft_data->bad_rb_buffer,
1736 ft_data->bad_rb_size<<2, 0, ft_data->bad_rb_size);
1737
1738 KGSL_FT_INFO(device, "Good RB buffer size 0x%X\n",
1739 ft_data->good_rb_size);
1740 adreno_dump_rb(device, ft_data->good_rb_buffer,
1741 ft_data->good_rb_size<<2, 0, ft_data->good_rb_size);
1742
1743 }
1744}
1745
Tarun Karradeeecc02013-01-21 23:42:17 -08001746static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001747_adreno_ft_resubmit_rb(struct kgsl_device *device,
Tarun Karradeeecc02013-01-21 23:42:17 -08001748 struct adreno_ringbuffer *rb,
1749 struct kgsl_context *context,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001750 struct adreno_ft_data *ft_data,
Tarun Karradeeecc02013-01-21 23:42:17 -08001751 unsigned int *buff, unsigned int size)
1752{
1753 unsigned int ret = 0;
1754
Tarun Karra696f89e2013-01-27 21:31:40 -08001755 _adreno_debug_ft_info(device, ft_data);
1756
Tarun Karrad20d71a2013-01-25 15:38:57 -08001757 if (_adreno_ft_restart_device(device, context, ft_data))
Tarun Karradeeecc02013-01-21 23:42:17 -08001758 return 1;
1759
1760 if (size) {
1761
1762 /* submit commands and wait for them to pass */
1763 adreno_ringbuffer_restore(rb, buff, size);
1764
1765 ret = adreno_idle(device);
1766 }
1767
1768 return ret;
1769}
1770
1771
1772static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001773_adreno_ft(struct kgsl_device *device,
1774 struct adreno_ft_data *ft_data)
Tarun Karradeeecc02013-01-21 23:42:17 -08001775{
1776 int ret = 0, i;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001777 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1778 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1779 struct kgsl_context *context;
1780 struct adreno_context *adreno_context = NULL;
1781 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1782
Tarun Karrad20d71a2013-01-25 15:38:57 -08001783 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001784 if (context == NULL) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001785 KGSL_FT_CRIT(device, "Last context unknown id:%d\n",
1786 ft_data->context_id);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001787 } else {
1788 adreno_context = context->devctxt;
1789 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001790 /*
1791 * set the invalid ts flag to 0 for this context since we have
1792 * detected a hang for it
1793 */
1794 context->wait_on_invalid_ts = false;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001795
Tarun Karra83297222013-02-05 19:45:49 -08001796 /*
1797 * This flag will be set by userspace for contexts
1798 * that do not want to be fault tolerant (ex: OPENCL)
1799 */
1800 if (adreno_context->flags & CTXT_FLAGS_NO_FAULT_TOLERANCE) {
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001801 ft_data->status = 1;
Tarun Karra83297222013-02-05 19:45:49 -08001802 KGSL_FT_ERR(device,
1803 "No FT set for this context play good cmds\n");
1804 goto play_good_cmds;
1805 }
1806
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001807 }
1808
Tarun Karra3164fb02013-02-05 15:38:51 -08001809 /*
1810 * Extract valid contents from rb which can still be executed after
1811 * hang
1812 */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001813 adreno_ringbuffer_extract(rb, ft_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001814
Tarun Karra696f89e2013-01-27 21:31:40 -08001815 /* Check if we detected a long running IB,
1816 * if true do not attempt replay of bad cmds */
1817 if (adreno_dev->long_ib) {
1818 if (_adreno_check_long_ib(device)) {
Tarun Karra3164fb02013-02-05 15:38:51 -08001819 ft_data->status = 1;
Tarun Karra696f89e2013-01-27 21:31:40 -08001820 _adreno_debug_ft_info(device, ft_data);
1821 goto play_good_cmds;
1822 } else {
1823 adreno_context->flags &= ~CTXT_FLAGS_GPU_HANG;
1824 return 0;
1825 }
1826 }
1827
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001828 /* Do not try the reply if hang is due to a pagefault */
1829 if (adreno_context->pagefault) {
1830 if ((ft_data->context_id == adreno_context->id) &&
1831 (ft_data->global_eop == adreno_context->pagefault_ts)) {
1832 ft_data->ft_policy &= ~KGSL_FT_REPLAY;
1833 KGSL_FT_ERR(device, "MMU fault skipping replay\n");
1834 }
1835
1836 adreno_context->pagefault = 0;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001837 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001838
Tarun Karra3164fb02013-02-05 15:38:51 -08001839 if (ft_data->ft_policy & KGSL_FT_DISABLE) {
1840 KGSL_FT_ERR(device, "NO FT policy play only good cmds\n");
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001841 ft_data->status = 1;
Tarun Karra3164fb02013-02-05 15:38:51 -08001842 goto play_good_cmds;
1843 }
1844
1845 if (ft_data->ft_policy & KGSL_FT_REPLAY) {
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001846
Tarun Karrad20d71a2013-01-25 15:38:57 -08001847 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1848 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001849
Tarun Karra3164fb02013-02-05 15:38:51 -08001850 if (ret) {
1851 KGSL_FT_ERR(device, "Replay unsuccessful\n");
1852 ft_data->status = 1;
1853 } else
Tarun Karradeeecc02013-01-21 23:42:17 -08001854 goto play_good_cmds;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001855 }
1856
Tarun Karra3164fb02013-02-05 15:38:51 -08001857 if (ft_data->ft_policy & KGSL_FT_SKIPIB) {
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001858
Tarun Karrad20d71a2013-01-25 15:38:57 -08001859 for (i = 0; i < ft_data->bad_rb_size; i++) {
1860 if ((ft_data->bad_rb_buffer[i] ==
1861 CP_HDR_INDIRECT_BUFFER_PFD) &&
1862 (ft_data->bad_rb_buffer[i+1] == ft_data->ib1)) {
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001863
Tarun Karrad20d71a2013-01-25 15:38:57 -08001864 ft_data->bad_rb_buffer[i] = cp_nop_packet(2);
1865 ft_data->bad_rb_buffer[i+1] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001866 KGSL_NOP_IB_IDENTIFIER;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001867 ft_data->bad_rb_buffer[i+2] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001868 KGSL_NOP_IB_IDENTIFIER;
1869 break;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001870 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001871 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001872
Tarun Karrad20d71a2013-01-25 15:38:57 -08001873 if ((i == (ft_data->bad_rb_size)) || (!ft_data->ib1)) {
1874 KGSL_FT_ERR(device, "Bad IB to NOP not found\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001875 ft_data->status = 1;
Tarun Karradeeecc02013-01-21 23:42:17 -08001876 goto play_good_cmds;
1877 }
1878
Tarun Karrad20d71a2013-01-25 15:38:57 -08001879 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1880 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001881
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001882 if (ret) {
Tarun Karra3164fb02013-02-05 15:38:51 -08001883 KGSL_FT_ERR(device, "NOP faulty IB unsuccessful\n");
1884 ft_data->status = 1;
1885 } else {
1886 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001887 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001888 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001889 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001890
Tarun Karra3164fb02013-02-05 15:38:51 -08001891 if (ft_data->ft_policy & KGSL_FT_SKIPFRAME) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001892
Tarun Karrad20d71a2013-01-25 15:38:57 -08001893 for (i = 0; i < ft_data->bad_rb_size; i++) {
1894 if (ft_data->bad_rb_buffer[i] ==
1895 KGSL_END_OF_FRAME_IDENTIFIER) {
1896 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001897 break;
1898 }
1899 }
1900
1901 /* EOF not found in RB, discard till EOF in
1902 next IB submission */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001903 if (i == ft_data->bad_rb_size) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001904 adreno_context->flags |= CTXT_FLAGS_SKIP_EOF;
Tarun Karra3164fb02013-02-05 15:38:51 -08001905 KGSL_FT_INFO(device,
1906 "EOF not found in RB, skip next issueib till EOF\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08001907 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001908 }
1909
Tarun Karrad20d71a2013-01-25 15:38:57 -08001910 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1911 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001912
1913 if (ret) {
Tarun Karra3164fb02013-02-05 15:38:51 -08001914 KGSL_FT_ERR(device, "Skip EOF unsuccessful\n");
1915 ft_data->status = 1;
1916 } else {
1917 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001918 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001919 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001920 }
1921
1922play_good_cmds:
1923
Tarun Karra3164fb02013-02-05 15:38:51 -08001924 if (ft_data->status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001925 KGSL_FT_ERR(device, "Bad context commands failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001926 else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001927 KGSL_FT_INFO(device, "Bad context commands success\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001928
1929 if (adreno_context) {
1930 adreno_context->flags = (adreno_context->flags &
Tarun Karrad20d71a2013-01-25 15:38:57 -08001931 ~CTXT_FLAGS_GPU_HANG) | CTXT_FLAGS_GPU_HANG_FT;
Tarun Karradeeecc02013-01-21 23:42:17 -08001932 }
1933 adreno_dev->drawctxt_active = last_active_ctx;
1934 }
1935
Tarun Karrad20d71a2013-01-25 15:38:57 -08001936 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1937 ft_data->good_rb_buffer, ft_data->good_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001938
1939 if (ret) {
1940 /* If we fail here we can try to invalidate another
1941 * context and try fault tolerance again */
1942 ret = -EAGAIN;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001943 KGSL_FT_ERR(device, "Playing good commands unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001944 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001945 } else
1946 KGSL_FT_INFO(device, "Playing good commands successful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001947
1948 /* ringbuffer now has data from the last valid context id,
1949 * so restore the active_ctx to the last valid context */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001950 if (ft_data->last_valid_ctx_id) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001951 struct kgsl_context *last_ctx =
1952 idr_find(&device->context_idr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001953 ft_data->last_valid_ctx_id);
Tarun Karradeeecc02013-01-21 23:42:17 -08001954 if (last_ctx)
1955 adreno_dev->drawctxt_active = last_ctx->devctxt;
1956 }
1957
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001958done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001959 /* Turn off iommu clocks */
1960 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1961 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001962 return ret;
1963}
1964
1965static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001966adreno_ft(struct kgsl_device *device,
1967 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001968{
1969 int ret = 0;
1970 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1971 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1972 unsigned int timestamp;
1973
Tarun Karrad20d71a2013-01-25 15:38:57 -08001974 KGSL_FT_INFO(device,
1975 "Start Parameters: IB1: 0x%X, "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001976 "Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001977 ft_data->ib1, ft_data->context_id, ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001978
1979 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
Tarun Karrad20d71a2013-01-25 15:38:57 -08001980 KGSL_FT_INFO(device, "Last issued global timestamp: %x\n", timestamp);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001981
1982 /* We may need to replay commands multiple times based on whether
1983 * multiple contexts hang the GPU */
1984 while (true) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001985
Tarun Karrad20d71a2013-01-25 15:38:57 -08001986 ret = _adreno_ft(device, ft_data);
1987
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001988 if (-EAGAIN == ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001989 /* setup new fault tolerance parameters and retry, this
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001990 * means more than 1 contexts are causing hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001991 adreno_destroy_ft_data(ft_data);
1992 ret = adreno_setup_ft_data(device, ft_data);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001993 if (ret)
1994 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001995 KGSL_FT_INFO(device,
1996 "Retry. Parameters: "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001997 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001998 ft_data->ib1, ft_data->context_id,
1999 ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002000 } else {
2001 break;
2002 }
2003 }
2004
2005 if (ret)
2006 goto done;
2007
Tarun Karrad20d71a2013-01-25 15:38:57 -08002008 /* Restore correct states after fault tolerance */
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002009 if (adreno_dev->drawctxt_active)
2010 device->mmu.hwpagetable =
2011 adreno_dev->drawctxt_active->pagetable;
2012 else
2013 device->mmu.hwpagetable = device->mmu.defaultpagetable;
2014 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
2015 kgsl_sharedmem_writel(&device->memstore,
2016 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2017 eoptimestamp),
2018 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07002019
2020 /* switch to NULL ctxt */
2021 if (adreno_dev->drawctxt_active != NULL)
2022 adreno_drawctxt_switch(adreno_dev, NULL, 0);
2023
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002024done:
2025 adreno_set_max_ts_for_bad_ctxs(device);
2026 adreno_mark_context_status(device, ret);
Tarun Karra3164fb02013-02-05 15:38:51 -08002027 KGSL_FT_ERR(device, "policy 0x%X status 0x%x\n",
2028 ft_data->ft_policy, ret);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002029 return ret;
2030}
2031
2032int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002033adreno_dump_and_exec_ft(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002034{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002035 int result = -ETIMEDOUT;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002036 struct adreno_ft_data ft_data;
Tarun Karra696f89e2013-01-27 21:31:40 -08002037 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karrad2326b82013-01-28 19:09:41 -08002038 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2039 unsigned int curr_pwrlevel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002040
2041 if (device->state == KGSL_STATE_HUNG)
2042 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002043 if (device->state == KGSL_STATE_DUMP_AND_FT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002044 mutex_unlock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002045 wait_for_completion(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002046 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07002047 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002048 result = 0;
2049 } else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002050 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_FT);
2051 INIT_COMPLETION(device->ft_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002052 /* Detected a hang */
2053
Tarun Karrad2326b82013-01-28 19:09:41 -08002054 /* Run fault tolerance at max power level */
2055 curr_pwrlevel = pwr->active_pwrlevel;
2056 kgsl_pwrctrl_pwrlevel_change(device, pwr->max_pwrlevel);
2057
Tarun Karrad20d71a2013-01-25 15:38:57 -08002058 /* Get the fault tolerance data as soon as hang is detected */
2059 result = adreno_setup_ft_data(device, &ft_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002060
2061 /*
Tarun Karra696f89e2013-01-27 21:31:40 -08002062 * If long ib is detected, do not attempt postmortem or
2063 * snapshot, if GPU is still executing commands
2064 * we will get errors
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002065 */
Tarun Karra696f89e2013-01-27 21:31:40 -08002066 if (!adreno_dev->long_ib) {
2067 /*
2068 * Trigger an automatic dump of the state to
2069 * the console
2070 */
2071 kgsl_postmortem_dump(device, 0);
2072
2073 /*
2074 * Make a GPU snapshot. For now, do it after the
2075 * PM dump so we can at least be sure the PM dump
2076 * will work as it always has
2077 */
2078 kgsl_device_snapshot(device, 1);
2079 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002080
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002081 if (!result) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002082 result = adreno_ft(device, &ft_data);
2083 adreno_destroy_ft_data(&ft_data);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002084 }
Tarun Karra696f89e2013-01-27 21:31:40 -08002085
Tarun Karrad2326b82013-01-28 19:09:41 -08002086 /* restore power level */
2087 kgsl_pwrctrl_pwrlevel_change(device, curr_pwrlevel);
2088
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002089 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002090 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002091 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002092 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002093 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
2094 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08002095 complete_all(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002096 }
2097done:
2098 return result;
2099}
Tarun Karrad20d71a2013-01-25 15:38:57 -08002100EXPORT_SYMBOL(adreno_dump_and_exec_ft);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002101
2102static int adreno_getproperty(struct kgsl_device *device,
2103 enum kgsl_property_type type,
2104 void *value,
2105 unsigned int sizebytes)
2106{
2107 int status = -EINVAL;
2108 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2109
2110 switch (type) {
2111 case KGSL_PROP_DEVICE_INFO:
2112 {
2113 struct kgsl_devinfo devinfo;
2114
2115 if (sizebytes != sizeof(devinfo)) {
2116 status = -EINVAL;
2117 break;
2118 }
2119
2120 memset(&devinfo, 0, sizeof(devinfo));
2121 devinfo.device_id = device->id+1;
2122 devinfo.chip_id = adreno_dev->chip_id;
2123 devinfo.mmu_enabled = kgsl_mmu_enabled();
2124 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06002125 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
2126 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002127
2128 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
2129 0) {
2130 status = -EFAULT;
2131 break;
2132 }
2133 status = 0;
2134 }
2135 break;
2136 case KGSL_PROP_DEVICE_SHADOW:
2137 {
2138 struct kgsl_shadowprop shadowprop;
2139
2140 if (sizebytes != sizeof(shadowprop)) {
2141 status = -EINVAL;
2142 break;
2143 }
2144 memset(&shadowprop, 0, sizeof(shadowprop));
2145 if (device->memstore.hostptr) {
2146 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2147 * anything to mmap().
2148 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07002149 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002150 shadowprop.size = device->memstore.size;
2151 /* GSL needs this to be set, even if it
2152 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002153 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2154 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002155 }
2156 if (copy_to_user(value, &shadowprop,
2157 sizeof(shadowprop))) {
2158 status = -EFAULT;
2159 break;
2160 }
2161 status = 0;
2162 }
2163 break;
2164 case KGSL_PROP_MMU_ENABLE:
2165 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002166 int mmu_prop = kgsl_mmu_enabled();
2167
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002168 if (sizebytes != sizeof(int)) {
2169 status = -EINVAL;
2170 break;
2171 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002172 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002173 status = -EFAULT;
2174 break;
2175 }
2176 status = 0;
2177 }
2178 break;
2179 case KGSL_PROP_INTERRUPT_WAITS:
2180 {
2181 int int_waits = 1;
2182 if (sizebytes != sizeof(int)) {
2183 status = -EINVAL;
2184 break;
2185 }
2186 if (copy_to_user(value, &int_waits, sizeof(int))) {
2187 status = -EFAULT;
2188 break;
2189 }
2190 status = 0;
2191 }
2192 break;
2193 default:
2194 status = -EINVAL;
2195 }
2196
2197 return status;
2198}
2199
Jordan Crousef7370f82012-04-18 09:31:07 -06002200static int adreno_setproperty(struct kgsl_device *device,
2201 enum kgsl_property_type type,
2202 void *value,
2203 unsigned int sizebytes)
2204{
2205 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08002206 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06002207
2208 switch (type) {
2209 case KGSL_PROP_PWRCTRL: {
2210 unsigned int enable;
2211 struct kgsl_device_platform_data *pdata =
2212 kgsl_device_get_drvdata(device);
2213
2214 if (sizebytes != sizeof(enable))
2215 break;
2216
2217 if (copy_from_user(&enable, (void __user *) value,
2218 sizeof(enable))) {
2219 status = -EFAULT;
2220 break;
2221 }
2222
2223 if (enable) {
2224 if (pdata->nap_allowed)
2225 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002226 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002227 kgsl_pwrscale_enable(device);
2228 } else {
2229 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002230 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002231 kgsl_pwrscale_disable(device);
2232 }
2233
2234 status = 0;
2235 }
2236 break;
Tarun Karra3164fb02013-02-05 15:38:51 -08002237 case KGSL_PROP_FAULT_TOLERANCE: {
2238 struct kgsl_ft_config ftd;
2239
Tarun Karra987e2f72013-02-14 12:12:22 -08002240 if (adreno_dev->ft_user_control == 0)
2241 break;
2242
Tarun Karra3164fb02013-02-05 15:38:51 -08002243 if (sizebytes != sizeof(ftd))
2244 break;
2245
2246 if (copy_from_user(&ftd, (void __user *) value,
2247 sizeof(ftd))) {
2248 status = -EFAULT;
2249 break;
2250 }
2251
2252 if (ftd.ft_policy)
2253 adreno_dev->ft_policy = ftd.ft_policy;
2254 else
2255 adreno_dev->ft_policy = KGSL_FT_DEFAULT_POLICY;
2256
Tarun Karra99678f82013-02-13 13:57:25 -08002257 if (ftd.ft_pf_policy)
2258 adreno_dev->ft_pf_policy = ftd.ft_policy;
2259 else
2260 adreno_dev->ft_pf_policy =
2261 KGSL_FT_PAGEFAULT_DEFAULT_POLICY;
2262
Tarun Karra3164fb02013-02-05 15:38:51 -08002263 if (ftd.ft_pm_dump)
2264 device->pm_dump_enable = 1;
2265 else
2266 device->pm_dump_enable = 0;
2267
2268 }
2269 break;
Jordan Crousef7370f82012-04-18 09:31:07 -06002270 default:
2271 break;
2272 }
2273
2274 return status;
2275}
2276
Jordan Crousea29a2e02012-08-14 09:09:23 -06002277static int adreno_ringbuffer_drain(struct kgsl_device *device,
2278 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002279{
2280 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2281 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002282 unsigned long wait;
2283 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2284
2285 if (!(rb->flags & KGSL_FLAGS_STARTED))
2286 return 0;
2287
2288 /*
2289 * The first time into the loop, wait for 100 msecs and kick wptr again
2290 * to ensure that the hardware has updated correctly. After that, kick
2291 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2292 * expires
2293 */
2294
2295 wait = jiffies + msecs_to_jiffies(100);
2296
Jordan Crousea29a2e02012-08-14 09:09:23 -06002297 do {
2298 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002299 /* Check to see if the core is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002300 if (adreno_ft_detect(device, regs))
Jordan Crousea29a2e02012-08-14 09:09:23 -06002301 return -ETIMEDOUT;
2302
2303 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2304 }
2305 GSL_RB_GET_READPTR(rb, &rb->rptr);
2306
2307 if (time_after(jiffies, timeout)) {
2308 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2309 rb->rptr, rb->wptr);
2310 return -ETIMEDOUT;
2311 }
2312 } while (rb->rptr != rb->wptr);
2313
2314 return 0;
2315}
2316
2317/* Caller must hold the device mutex. */
2318int adreno_idle(struct kgsl_device *device)
2319{
2320 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002321 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302322 unsigned long wait_time;
2323 unsigned long wait_time_part;
Tarun Karra696f89e2013-01-27 21:31:40 -08002324 unsigned int prev_reg_val[ft_detect_regs_count];
Tarun Karra3335f142012-06-19 14:11:48 -07002325
2326 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002327
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002328 kgsl_cffdump_regpoll(device->id,
2329 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002330 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002331
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002332retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002333 /* First, wait for the ringbuffer to drain */
2334 if (adreno_ringbuffer_drain(device, prev_reg_val))
2335 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002336
2337 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002338 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002339 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2340
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002341 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002342 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2343 &rbbm_status);
2344 if (adreno_is_a2xx(adreno_dev)) {
2345 if (rbbm_status == 0x110)
2346 return 0;
2347 } else {
2348 if (!(rbbm_status & 0x80000000))
2349 return 0;
2350 }
Tarun Karra3335f142012-06-19 14:11:48 -07002351
2352 /* Dont wait for timeout, detect hang faster.
2353 */
2354 if (time_after(jiffies, wait_time_part)) {
2355 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002356 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra696f89e2013-01-27 21:31:40 -08002357 if ((adreno_ft_detect(device, prev_reg_val)))
Tarun Karra3335f142012-06-19 14:11:48 -07002358 goto err;
2359 }
2360
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002361 }
2362
2363err:
2364 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08002365 if (KGSL_STATE_DUMP_AND_FT != device->state &&
2366 !adreno_dump_and_exec_ft(device)) {
2367 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002368 goto retry;
2369 }
2370 return -ETIMEDOUT;
2371}
2372
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002373/**
2374 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2375 * rbbm_status register
2376 * @device - Pointer to the GPU device whose idle status is to be
2377 * checked
2378 * @returns - Returns whether the core is idle (based on rbbm_status)
2379 * false if the core is active, true if the core is idle
2380 */
2381static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2382{
2383 unsigned int reg_rbbm_status;
2384 bool status = false;
2385 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2386
2387 /* Is the core idle? */
2388 adreno_regread(device,
2389 adreno_dev->gpudev->reg_rbbm_status,
2390 &reg_rbbm_status);
2391
2392 if (adreno_is_a2xx(adreno_dev)) {
2393 if (reg_rbbm_status == 0x110)
2394 status = true;
2395 } else {
2396 if (!(reg_rbbm_status & 0x80000000))
2397 status = true;
2398 }
2399 return status;
2400}
2401
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002402static unsigned int adreno_isidle(struct kgsl_device *device)
2403{
2404 int status = false;
2405 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2406 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002407
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002408 WARN_ON(device->state == KGSL_STATE_INIT);
2409 /* If the device isn't active, don't force it on. */
2410 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002411 /* Is the ring buffer is empty? */
2412 GSL_RB_GET_READPTR(rb, &rb->rptr);
2413 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
Jordan Crouseab601992013-03-05 11:18:20 -07002414 /*
2415 * Are there interrupts pending? If so then pretend we
2416 * are not idle - this avoids the possiblity that we go
2417 * to a lower power state without handling interrupts
2418 * first.
2419 */
2420
2421 if (!adreno_dev->gpudev->irq_pending(adreno_dev)) {
2422 /* Is the core idle? */
2423 status = is_adreno_rbbm_status_idle(device);
2424 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002425 }
2426 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002427 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002428 }
2429 return status;
2430}
2431
2432/* Caller must hold the device mutex. */
2433static int adreno_suspend_context(struct kgsl_device *device)
2434{
2435 int status = 0;
2436 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2437
2438 /* switch to NULL ctxt */
2439 if (adreno_dev->drawctxt_active != NULL) {
2440 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002441 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002442 }
2443
2444 return status;
2445}
2446
Jordan Crouse233b2092012-04-18 09:31:09 -06002447/* Find a memory structure attached to an adreno context */
2448
2449struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2450 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2451{
2452 struct kgsl_context *context;
2453 struct adreno_context *adreno_context = NULL;
2454 int next = 0;
2455
2456 while (1) {
2457 context = idr_get_next(&device->context_idr, &next);
2458 if (context == NULL)
2459 break;
2460
2461 adreno_context = (struct adreno_context *)context->devctxt;
2462
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002463 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2464 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002465 struct kgsl_memdesc *desc;
2466
2467 desc = &adreno_context->gpustate;
2468 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2469 return desc;
2470
2471 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2472 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2473 return desc;
2474 }
2475 next = next + 1;
2476 }
2477
2478 return NULL;
2479}
2480
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002481struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002482 unsigned int pt_base,
2483 unsigned int gpuaddr,
2484 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002485{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002486 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002487 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2488 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2489
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002490 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2491 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002492
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002493 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2494 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002495
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002496 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2497 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002498
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002499 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2500 size))
2501 return &device->mmu.setstate_memory;
2502
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002503 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002504
2505 if (entry)
2506 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002507
Jordan Crouse233b2092012-04-18 09:31:09 -06002508 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002509}
2510
2511uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2512 unsigned int gpuaddr, unsigned int size)
2513{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002514 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002515
2516 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2517
2518 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002519}
2520
2521void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2522 unsigned int *value)
2523{
2524 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002525 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2526 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002527
2528 if (!in_interrupt())
2529 kgsl_pre_hwaccess(device);
2530
2531 /*ensure this read finishes before the next one.
2532 * i.e. act like normal readl() */
2533 *value = __raw_readl(reg);
2534 rmb();
2535}
2536
2537void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2538 unsigned int value)
2539{
2540 unsigned int *reg;
2541
Jordan Crouse7501d452012-04-19 08:58:44 -06002542 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002543
2544 if (!in_interrupt())
2545 kgsl_pre_hwaccess(device);
2546
2547 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002548 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002549
2550 /*ensure previous writes post before this one,
2551 * i.e. act like normal writel() */
2552 wmb();
2553 __raw_writel(value, reg);
2554}
2555
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002556static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2557{
2558 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002559 if (k_ctxt != NULL) {
2560 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002561 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2562 context_id = KGSL_CONTEXT_INVALID;
2563 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2564 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002565 }
2566
2567 return context_id;
2568}
2569
Carter Cooper740f6742013-01-03 16:19:23 -07002570static unsigned int adreno_check_hw_ts(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002571 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002572{
Carter Cooper740f6742013-01-03 16:19:23 -07002573 int status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002574 unsigned int ref_ts, enableflag;
Carter Cooper740f6742013-01-03 16:19:23 -07002575 unsigned int context_id = _get_context_id(context);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002576
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002577 /*
2578 * If the context ID is invalid, we are in a race with
2579 * the context being destroyed by userspace so bail.
2580 */
2581 if (context_id == KGSL_CONTEXT_INVALID) {
2582 KGSL_DRV_WARN(device, "context was detached");
Carter Cooper740f6742013-01-03 16:19:23 -07002583 return -EINVAL;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002584 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002585
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002586 status = kgsl_check_timestamp(device, context, timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002587 if (status)
2588 return status;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002589
Carter Cooper740f6742013-01-03 16:19:23 -07002590 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2591 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2592 /*
2593 * Barrier is needed here to make sure the read from memstore
2594 * has posted
2595 */
2596
2597 mb();
2598
2599 if (enableflag) {
2600 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002601 KGSL_MEMSTORE_OFFSET(context_id,
2602 ref_wait_ts));
Carter Cooper740f6742013-01-03 16:19:23 -07002603
2604 /* Make sure the memstore read has posted */
2605 mb();
2606 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
2607 kgsl_sharedmem_writel(&device->memstore,
2608 KGSL_MEMSTORE_OFFSET(context_id,
2609 ref_wait_ts), timestamp);
2610 /* Make sure the memstore write is posted */
2611 wmb();
2612 }
2613 } else {
2614 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002615 KGSL_MEMSTORE_OFFSET(context_id,
2616 ref_wait_ts), timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002617 enableflag = 1;
2618 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002619 KGSL_MEMSTORE_OFFSET(context_id,
2620 ts_cmp_enable), enableflag);
Carter Cooper740f6742013-01-03 16:19:23 -07002621 /* Make sure the memstore write gets posted */
2622 wmb();
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002623
Carter Cooper740f6742013-01-03 16:19:23 -07002624 /*
2625 * submit a dummy packet so that even if all
2626 * commands upto timestamp get executed we will still
2627 * get an interrupt
2628 */
2629
2630 if (context && device->state != KGSL_STATE_SLUMBER)
2631 adreno_ringbuffer_issuecmds(device, context->devctxt,
2632 KGSL_CMD_FLAGS_NONE, NULL, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002633 }
Carter Cooper740f6742013-01-03 16:19:23 -07002634
2635 return 0;
2636}
2637
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002638/* Return 1 if the event timestmp has already passed, 0 if it was marked */
2639static int adreno_next_event(struct kgsl_device *device,
Carter Cooper740f6742013-01-03 16:19:23 -07002640 struct kgsl_event *event)
2641{
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002642 return adreno_check_hw_ts(device, event->context, event->timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002643}
2644
2645static int adreno_check_interrupt_timestamp(struct kgsl_device *device,
2646 struct kgsl_context *context, unsigned int timestamp)
2647{
2648 int status;
2649
2650 mutex_lock(&device->mutex);
2651 status = adreno_check_hw_ts(device, context, timestamp);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002652 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002653
2654 return status;
2655}
2656
2657/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002658 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002659 placing a process in wait q. For conditional interrupts we expect the
2660 process to already be in its wait q when its exit condition checking
2661 function is called.
2662*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002663#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002664({ \
2665 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002666 if (io) \
2667 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2668 else \
2669 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002670 __ret; \
2671})
2672
Tarun Karra3335f142012-06-19 14:11:48 -07002673
2674
Tarun Karra696f89e2013-01-27 21:31:40 -08002675unsigned int adreno_ft_detect(struct kgsl_device *device,
Tarun Karra3335f142012-06-19 14:11:48 -07002676 unsigned int *prev_reg_val)
2677{
2678 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karra696f89e2013-01-27 21:31:40 -08002679 unsigned int curr_reg_val[ft_detect_regs_count];
2680 unsigned int fast_hang_detected = 1;
2681 unsigned int long_ib_detected = 1;
Tarun Karra3335f142012-06-19 14:11:48 -07002682 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002683 static unsigned long next_hang_detect_time;
Tarun Karra696f89e2013-01-27 21:31:40 -08002684 static unsigned int prev_global_ts;
2685 unsigned int curr_global_ts = 0;
2686 unsigned int curr_context_id = 0;
2687 static struct adreno_context *curr_context;
2688 static struct kgsl_context *context;
Tarun Karra3335f142012-06-19 14:11:48 -07002689
2690 if (!adreno_dev->fast_hang_detect)
Tarun Karra696f89e2013-01-27 21:31:40 -08002691 fast_hang_detected = 0;
2692
2693 if (!adreno_dev->long_ib_detect)
2694 long_ib_detected = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002695
Jordan Crousecca61142012-11-20 10:54:24 -07002696 if (is_adreno_rbbm_status_idle(device)) {
2697
2698 /*
2699 * On A20X if the RPTR != WPTR and the device is idle, then
2700 * the last write to WPTR probably failed to latch so write it
2701 * again
2702 */
2703
2704 if (adreno_is_a2xx(adreno_dev)) {
2705 unsigned int rptr;
2706 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2707 if (rptr != adreno_dev->ringbuffer.wptr)
2708 adreno_regwrite(device, REG_CP_RB_WPTR,
2709 adreno_dev->ringbuffer.wptr);
2710 }
2711
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002712 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002713 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002714
Tarun Karra4648db92013-02-01 17:03:40 -08002715 /*
2716 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2717 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2718 * from the last time do nothing.
2719 */
2720 if ((next_hang_detect_time) &&
2721 (time_before(jiffies, next_hang_detect_time)))
2722 return 0;
2723 else
2724 next_hang_detect_time = (jiffies +
2725 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2726
Tarun Karra696f89e2013-01-27 21:31:40 -08002727 /* Read the current Hang detect reg values here */
2728 for (i = 0; i < ft_detect_regs_count; i++) {
2729 if (ft_detect_regs[i] == 0)
Jordan Crouseb5c80482012-10-03 09:38:41 -06002730 continue;
Tarun Karra696f89e2013-01-27 21:31:40 -08002731 adreno_regread(device, ft_detect_regs[i],
2732 &curr_reg_val[i]);
Tarun Karra3335f142012-06-19 14:11:48 -07002733 }
2734
Tarun Karra696f89e2013-01-27 21:31:40 -08002735 /* Read the current global timestamp here */
2736 kgsl_sharedmem_readl(&device->memstore,
2737 &curr_global_ts,
2738 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2739 eoptimestamp));
2740
2741 mb();
2742
2743 if (curr_global_ts == prev_global_ts) {
2744
2745 /* Get the current context here */
2746 if (context == NULL) {
2747 kgsl_sharedmem_readl(&device->memstore,
2748 &curr_context_id,
2749 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2750 current_context));
2751 context = idr_find(&device->context_idr,
2752 curr_context_id);
2753 if (context != NULL) {
2754 curr_context = context->devctxt;
2755 curr_context->ib_gpu_time_used = 0;
2756 } else {
2757 KGSL_DRV_ERR(device,
2758 "Fault tolerance no context found\n");
2759 }
2760 }
2761
2762 mb();
2763
2764 if (curr_context != NULL) {
2765
2766 curr_context->ib_gpu_time_used += KGSL_TIMEOUT_PART;
2767 KGSL_FT_INFO(device,
2768 "Proc %s used GPU Time %d ms on timestamp 0x%X\n",
2769 curr_context->pid_name, curr_context->ib_gpu_time_used,
2770 curr_global_ts+1);
2771
2772 for (i = 0; i < ft_detect_regs_count; i++) {
2773 if (curr_reg_val[i] != prev_reg_val[i]) {
2774 fast_hang_detected = 0;
2775
2776 /* Check for long IB here */
2777 if ((i >=
2778 LONG_IB_DETECT_REG_INDEX_START)
2779 &&
2780 (i <=
2781 LONG_IB_DETECT_REG_INDEX_END))
2782 long_ib_detected = 0;
2783 }
2784 }
2785
2786 if (fast_hang_detected) {
2787 KGSL_FT_ERR(device,
2788 "Proc %s, ctxt_id %d ts %d triggered fault tolerance"
2789 " on global ts %d\n",
2790 curr_context->pid_name, curr_context->id
2791 , (kgsl_readtimestamp(device, context,
2792 KGSL_TIMESTAMP_RETIRED)+1),
2793 curr_global_ts+1);
2794 return 1;
2795 }
2796
Tarun Karra83297222013-02-05 19:45:49 -08002797 if ((long_ib_detected) &&
2798 (!(curr_context->flags &
2799 CTXT_FLAGS_NO_FAULT_TOLERANCE))) {
Tarun Karra696f89e2013-01-27 21:31:40 -08002800 curr_context->ib_gpu_time_used +=
2801 KGSL_TIMEOUT_PART;
2802 if (curr_context->ib_gpu_time_used >
2803 KGSL_TIMEOUT_LONG_IB_DETECTION) {
2804 if (adreno_dev->long_ib_ts !=
2805 curr_global_ts) {
2806 KGSL_FT_ERR(device,
2807 "Proc %s, ctxt_id %d ts %d"
2808 "used GPU for %d ms long ib "
2809 "detected on global ts %d\n",
2810 curr_context->pid_name,
2811 curr_context->id,
2812 (kgsl_readtimestamp(device,
2813 context,
2814 KGSL_TIMESTAMP_RETIRED)+1),
2815 curr_context->ib_gpu_time_used,
2816 curr_global_ts+1);
2817 adreno_dev->long_ib = 1;
2818 adreno_dev->long_ib_ts =
2819 curr_global_ts;
2820 curr_context->ib_gpu_time_used =
2821 0;
2822 return 1;
2823 }
2824 }
2825 }
2826 } else {
2827 KGSL_FT_ERR(device,
2828 "Last context unknown id:%d\n",
2829 curr_context_id);
2830 }
2831 } else {
2832 /* GPU is moving forward */
2833 prev_global_ts = curr_global_ts;
2834 context = NULL;
2835 curr_context = NULL;
2836 adreno_dev->long_ib = 0;
2837 adreno_dev->long_ib_ts = 0;
2838 }
2839
2840
2841 /* If hangs are not detected copy the current reg values
2842 * to previous values and return no hang */
2843 for (i = 0; i < ft_detect_regs_count; i++)
2844 prev_reg_val[i] = curr_reg_val[i];
2845 return 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002846}
2847
Jordan Crouse92446a62012-11-15 11:00:06 -07002848/**
2849 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2850 * @device - pointer to a KGSL device structure
2851 * @context - pointer to the active KGSL context
2852 * @timestamp - the timestamp that the process was waiting for
2853 *
Tarun Karrad20d71a2013-01-25 15:38:57 -08002854 * Process a possible GPU hang and try fault tolerance from it
2855 * cleanly
Jordan Crouse92446a62012-11-15 11:00:06 -07002856 */
2857static int adreno_handle_hang(struct kgsl_device *device,
2858 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002859{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002860 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002861 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002862 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002863 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002864
Jordan Crouse92446a62012-11-15 11:00:06 -07002865 /* Do one last check to see if we somehow made it through */
2866 if (kgsl_check_timestamp(device, context, timestamp))
2867 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002868
2869 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002870
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002871 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2872 mb();
2873
Tarun Karra696f89e2013-01-27 21:31:40 -08002874 KGSL_DRV_WARN(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002875 "Device hang detected while waiting for timestamp: "
2876 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002877 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2878 context_id, timestamp, context_id, ts_issued, context_id,
2879 kgsl_readtimestamp(device, context,
2880 KGSL_TIMESTAMP_RETIRED),
2881 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002882
Tarun Karrad20d71a2013-01-25 15:38:57 -08002883 /* Return 0 after a successful fault tolerance */
2884 if (!adreno_dump_and_exec_ft(device))
Jordan Crouse92446a62012-11-15 11:00:06 -07002885 return 0;
2886
2887 return -ETIMEDOUT;
2888}
2889
2890static int _check_pending_timestamp(struct kgsl_device *device,
2891 struct kgsl_context *context, unsigned int timestamp)
2892{
2893 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2894 unsigned int context_id = _get_context_id(context);
2895 unsigned int ts_issued;
2896
2897 if (context_id == KGSL_CONTEXT_INVALID)
2898 return -EINVAL;
2899
2900 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2901
2902 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2903 return 0;
2904
2905 if (context && !context->wait_on_invalid_ts) {
2906 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2907 context_id, timestamp, context_id, ts_issued);
2908
2909 /* Only print this message once */
2910 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002911 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002912
2913 return -EINVAL;
2914}
2915
2916/**
2917 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2918 * @device - pointer to a KGSL device structure
2919 * @context - pointer to the active kgsl context
2920 * @timestamp - GPU timestamp to wait for
2921 * @msecs - amount of time to wait (in milliseconds)
2922 *
2923 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2924 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2925 * one if it happened. Otherwise, spend most of our time in an interruptible
2926 * wait for the timestamp interrupt to be processed. This function must be
2927 * called with the mutex already held.
2928 */
2929static int adreno_waittimestamp(struct kgsl_device *device,
2930 struct kgsl_context *context,
2931 unsigned int timestamp,
2932 unsigned int msecs)
2933{
2934 static unsigned int io_cnt;
2935 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2936 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2937 unsigned int context_id = _get_context_id(context);
Tarun Karra696f89e2013-01-27 21:31:40 -08002938 unsigned int prev_reg_val[ft_detect_regs_count];
Jordan Crouse92446a62012-11-15 11:00:06 -07002939 unsigned int time_elapsed = 0;
2940 unsigned int wait;
2941 int ts_compare = 1;
2942 int io, ret = -ETIMEDOUT;
2943
2944 /* Get out early if the context has already been destroyed */
2945
2946 if (context_id == KGSL_CONTEXT_INVALID) {
2947 KGSL_DRV_WARN(device, "context was detached");
2948 return -EINVAL;
2949 }
2950
2951 /*
2952 * Check to see if the requested timestamp is "newer" then the last
2953 * timestamp issued. If it is complain once and return error. Only
2954 * print the message once per context so that badly behaving
2955 * applications don't spam the logs
2956 */
2957
2958 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2959 if (_check_pending_timestamp(device, context, timestamp))
2960 return -EINVAL;
2961
2962 /* Reset the invalid timestamp flag on a valid wait */
2963 context->wait_on_invalid_ts = false;
2964 }
2965
2966
2967 /* Clear the registers used for hang detection */
2968 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2969
2970 /*
2971 * On the first time through the loop only wait 100ms.
2972 * this gives enough time for the engine to start moving and oddly
2973 * provides better hang detection results than just going the full
2974 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2975 * is if msecs happens to be < 100ms then just use the full timeout
2976 */
2977
2978 wait = 100;
2979
2980 do {
2981 long status;
2982
Jordan Crouse92446a62012-11-15 11:00:06 -07002983 /*
2984 * if the timestamp happens while we're not
2985 * waiting, there's a chance that an interrupt
2986 * will not be generated and thus the timestamp
2987 * work needs to be queued.
2988 */
2989
2990 if (kgsl_check_timestamp(device, context, timestamp)) {
2991 queue_work(device->work_queue, &device->ts_expired_ws);
2992 ret = 0;
2993 break;
2994 }
2995
2996 /* Check to see if the GPU is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002997 if (adreno_ft_detect(device, prev_reg_val)) {
Jordan Crouse92446a62012-11-15 11:00:06 -07002998 ret = adreno_handle_hang(device, context, timestamp);
2999 break;
3000 }
3001
3002 /*
3003 * For proper power accounting sometimes we need to call
3004 * io_wait_interruptible_timeout and sometimes we need to call
3005 * plain old wait_interruptible_timeout. We call the regular
3006 * timeout N times out of 100, where N is a number specified by
3007 * the current power level
3008 */
3009
3010 io_cnt = (io_cnt + 1) % 100;
3011 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
3012 ? 0 : 1;
3013
3014 mutex_unlock(&device->mutex);
3015
3016 /* Wait for a timestamp event */
3017 status = kgsl_wait_event_interruptible_timeout(
3018 device->wait_queue,
Carter Cooper740f6742013-01-03 16:19:23 -07003019 adreno_check_interrupt_timestamp(device, context,
Jordan Crouse92446a62012-11-15 11:00:06 -07003020 timestamp), msecs_to_jiffies(wait), io);
3021
3022 mutex_lock(&device->mutex);
3023
3024 /*
3025 * If status is non zero then either the condition was satisfied
3026 * or there was an error. In either event, this is the end of
3027 * the line for us
3028 */
3029
3030 if (status != 0) {
3031 ret = (status > 0) ? 0 : (int) status;
3032 break;
3033 }
Jordan Crouse92446a62012-11-15 11:00:06 -07003034 time_elapsed += wait;
3035
3036 /* If user specified timestamps are being used, wait at least
3037 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
3038 * issue a IB for a timestamp before checking to see if the
3039 * current timestamp we are waiting for is valid or not
3040 */
3041
3042 if (ts_compare && (adreno_ctx &&
3043 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
3044 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
3045 ret = _check_pending_timestamp(device, context,
3046 timestamp);
3047 if (ret)
3048 break;
3049
3050 /* Don't do this check again */
3051 ts_compare = 0;
3052
3053 /*
3054 * Reset the invalid timestamp flag on a valid
3055 * wait
3056 */
3057 context->wait_on_invalid_ts = false;
3058 }
3059 }
3060
3061 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003062 * We want to wait the floor of KGSL_TIMEOUT_PART
3063 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07003064 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003065
3066 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
3067 wait = KGSL_TIMEOUT_PART;
3068 else
3069 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07003070
3071 } while (!msecs || time_elapsed < msecs);
3072
3073 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003074}
3075
3076static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003077 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003078{
3079 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003080 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003081
Jeremy Gebben9ad86922012-05-08 15:33:23 -06003082 /*
3083 * If the context ID is invalid, we are in a race with
3084 * the context being destroyed by userspace so bail.
3085 */
3086 if (context_id == KGSL_CONTEXT_INVALID) {
3087 KGSL_DRV_WARN(device, "context was detached");
3088 return timestamp;
3089 }
Jordan Crousec659f382012-04-16 11:10:41 -06003090 switch (type) {
3091 case KGSL_TIMESTAMP_QUEUED: {
3092 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3093 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
3094
3095 timestamp = rb->timestamp[context_id];
3096 break;
3097 }
3098 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003099 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06003100 break;
3101 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003102 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06003103 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
3104 break;
3105 }
3106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003107 rmb();
3108
3109 return timestamp;
3110}
3111
3112static long adreno_ioctl(struct kgsl_device_private *dev_priv,
3113 unsigned int cmd, void *data)
3114{
3115 int result = 0;
3116 struct kgsl_drawctxt_set_bin_base_offset *binbase;
3117 struct kgsl_context *context;
3118
3119 switch (cmd) {
3120 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
3121 binbase = data;
3122
3123 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
3124 if (context) {
3125 adreno_drawctxt_set_bin_base_offset(
3126 dev_priv->device, context, binbase->offset);
3127 } else {
3128 result = -EINVAL;
3129 KGSL_DRV_ERR(dev_priv->device,
3130 "invalid drawctxt drawctxt_id %d "
3131 "device_id=%d\n",
3132 binbase->drawctxt_id, dev_priv->device->id);
3133 }
3134 break;
3135
3136 default:
3137 KGSL_DRV_INFO(dev_priv->device,
3138 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07003139 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003140 break;
3141 }
3142 return result;
3143
3144}
3145
3146static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
3147{
3148 gpu_freq /= 1000000;
3149 return ticks / gpu_freq;
3150}
3151
3152static void adreno_power_stats(struct kgsl_device *device,
3153 struct kgsl_power_stats *stats)
3154{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003155 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003156 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003157 unsigned int cycles;
3158
3159 /* Get the busy cycles counted since the counter was last reset */
3160 /* Calling this function also resets and restarts the counter */
3161
3162 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003163
3164 /* In order to calculate idle you have to have run the algorithm *
3165 * at least once to get a start time. */
3166 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003167 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003168 stats->total_time = tmp - pwr->time;
3169 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003170 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003171 pwrlevels[device->pwrctrl.active_pwrlevel].
3172 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003173 } else {
3174 stats->total_time = 0;
3175 stats->busy_time = 0;
3176 pwr->time = ktime_to_us(ktime_get());
3177 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003178}
3179
3180void adreno_irqctrl(struct kgsl_device *device, int state)
3181{
Jordan Crousea78c9172011-07-11 13:14:09 -06003182 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3183 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003184}
3185
Jordan Croused6535882012-06-20 08:22:16 -06003186static unsigned int adreno_gpuid(struct kgsl_device *device,
3187 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07003188{
3189 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3190
Jordan Croused6535882012-06-20 08:22:16 -06003191 /* Some applications need to know the chip ID too, so pass
3192 * that as a parameter */
3193
3194 if (chipid != NULL)
3195 *chipid = adreno_dev->chip_id;
3196
Jordan Crousea0758f22011-12-07 11:19:22 -07003197 /* Standard KGSL gpuid format:
3198 * top word is 0x0002 for 2D or 0x0003 for 3D
3199 * Bottom word is core specific identifer
3200 */
3201
3202 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
3203}
3204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003205static const struct kgsl_functable adreno_functable = {
3206 /* Mandatory functions */
3207 .regread = adreno_regread,
3208 .regwrite = adreno_regwrite,
3209 .idle = adreno_idle,
3210 .isidle = adreno_isidle,
3211 .suspend_context = adreno_suspend_context,
3212 .start = adreno_start,
3213 .stop = adreno_stop,
3214 .getproperty = adreno_getproperty,
3215 .waittimestamp = adreno_waittimestamp,
3216 .readtimestamp = adreno_readtimestamp,
3217 .issueibcmds = adreno_ringbuffer_issueibcmds,
3218 .ioctl = adreno_ioctl,
3219 .setup_pt = adreno_setup_pt,
3220 .cleanup_pt = adreno_cleanup_pt,
3221 .power_stats = adreno_power_stats,
3222 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07003223 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07003224 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06003225 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003226 /* Optional functions */
3227 .setstate = adreno_setstate,
3228 .drawctxt_create = adreno_drawctxt_create,
3229 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06003230 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06003231 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07003232 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003233};
3234
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003235static struct platform_driver adreno_platform_driver = {
3236 .probe = adreno_probe,
3237 .remove = __devexit_p(adreno_remove),
3238 .suspend = kgsl_suspend_driver,
3239 .resume = kgsl_resume_driver,
3240 .id_table = adreno_id_table,
3241 .driver = {
3242 .owner = THIS_MODULE,
3243 .name = DEVICE_3D_NAME,
3244 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06003245 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003246 }
3247};
3248
3249static int __init kgsl_3d_init(void)
3250{
3251 return platform_driver_register(&adreno_platform_driver);
3252}
3253
3254static void __exit kgsl_3d_exit(void)
3255{
3256 platform_driver_unregister(&adreno_platform_driver);
3257}
3258
3259module_init(kgsl_3d_init);
3260module_exit(kgsl_3d_exit);
3261
3262MODULE_DESCRIPTION("3D Graphics driver");
3263MODULE_VERSION("1.2");
3264MODULE_LICENSE("GPL v2");
3265MODULE_ALIAS("platform:kgsl_3d");