blob: 3e6b77c3a4b3a36930916ebb1c3a82036ed8c25b [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) {
1801 KGSL_FT_ERR(device,
1802 "No FT set for this context play good cmds\n");
1803 goto play_good_cmds;
1804 }
1805
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001806 }
1807
Tarun Karra3164fb02013-02-05 15:38:51 -08001808 /*
1809 * Extract valid contents from rb which can still be executed after
1810 * hang
1811 */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001812 adreno_ringbuffer_extract(rb, ft_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001813
Tarun Karra696f89e2013-01-27 21:31:40 -08001814 /* Check if we detected a long running IB,
1815 * if true do not attempt replay of bad cmds */
1816 if (adreno_dev->long_ib) {
1817 if (_adreno_check_long_ib(device)) {
Tarun Karra3164fb02013-02-05 15:38:51 -08001818 ft_data->status = 1;
Tarun Karra696f89e2013-01-27 21:31:40 -08001819 _adreno_debug_ft_info(device, ft_data);
1820 goto play_good_cmds;
1821 } else {
1822 adreno_context->flags &= ~CTXT_FLAGS_GPU_HANG;
1823 return 0;
1824 }
1825 }
1826
Tarun Karradeeecc02013-01-21 23:42:17 -08001827 /* Do not try the bad commands if hang is due to a fault */
Tarun Karra3164fb02013-02-05 15:38:51 -08001828 if (device->mmu.fault) {
1829 KGSL_FT_ERR(device, "MMU fault skipping bad cmds\n");
1830 device->mmu.fault = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001831 goto play_good_cmds;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001832 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001833
Tarun Karra3164fb02013-02-05 15:38:51 -08001834 if (ft_data->ft_policy & KGSL_FT_DISABLE) {
1835 KGSL_FT_ERR(device, "NO FT policy play only good cmds\n");
1836 goto play_good_cmds;
1837 }
1838
1839 if (ft_data->ft_policy & KGSL_FT_REPLAY) {
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001840
Tarun Karrad20d71a2013-01-25 15:38:57 -08001841 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1842 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001843
Tarun Karra3164fb02013-02-05 15:38:51 -08001844 if (ret) {
1845 KGSL_FT_ERR(device, "Replay unsuccessful\n");
1846 ft_data->status = 1;
1847 } else
Tarun Karradeeecc02013-01-21 23:42:17 -08001848 goto play_good_cmds;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001849 }
1850
Tarun Karra3164fb02013-02-05 15:38:51 -08001851 if (ft_data->ft_policy & KGSL_FT_SKIPIB) {
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001852
Tarun Karrad20d71a2013-01-25 15:38:57 -08001853 for (i = 0; i < ft_data->bad_rb_size; i++) {
1854 if ((ft_data->bad_rb_buffer[i] ==
1855 CP_HDR_INDIRECT_BUFFER_PFD) &&
1856 (ft_data->bad_rb_buffer[i+1] == ft_data->ib1)) {
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001857
Tarun Karrad20d71a2013-01-25 15:38:57 -08001858 ft_data->bad_rb_buffer[i] = cp_nop_packet(2);
1859 ft_data->bad_rb_buffer[i+1] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001860 KGSL_NOP_IB_IDENTIFIER;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001861 ft_data->bad_rb_buffer[i+2] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001862 KGSL_NOP_IB_IDENTIFIER;
1863 break;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001864 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001865 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001866
Tarun Karrad20d71a2013-01-25 15:38:57 -08001867 if ((i == (ft_data->bad_rb_size)) || (!ft_data->ib1)) {
1868 KGSL_FT_ERR(device, "Bad IB to NOP not found\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001869 ft_data->status = 1;
Tarun Karradeeecc02013-01-21 23:42:17 -08001870 goto play_good_cmds;
1871 }
1872
Tarun Karrad20d71a2013-01-25 15:38:57 -08001873 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1874 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001875
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001876 if (ret) {
Tarun Karra3164fb02013-02-05 15:38:51 -08001877 KGSL_FT_ERR(device, "NOP faulty IB unsuccessful\n");
1878 ft_data->status = 1;
1879 } else {
1880 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001881 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001882 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001883 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001884
Tarun Karra3164fb02013-02-05 15:38:51 -08001885 if (ft_data->ft_policy & KGSL_FT_SKIPFRAME) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001886
Tarun Karrad20d71a2013-01-25 15:38:57 -08001887 for (i = 0; i < ft_data->bad_rb_size; i++) {
1888 if (ft_data->bad_rb_buffer[i] ==
1889 KGSL_END_OF_FRAME_IDENTIFIER) {
1890 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001891 break;
1892 }
1893 }
1894
1895 /* EOF not found in RB, discard till EOF in
1896 next IB submission */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001897 if (i == ft_data->bad_rb_size) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001898 adreno_context->flags |= CTXT_FLAGS_SKIP_EOF;
Tarun Karra3164fb02013-02-05 15:38:51 -08001899 KGSL_FT_INFO(device,
1900 "EOF not found in RB, skip next issueib till EOF\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08001901 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001902 }
1903
Tarun Karrad20d71a2013-01-25 15:38:57 -08001904 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1905 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001906
1907 if (ret) {
Tarun Karra3164fb02013-02-05 15:38:51 -08001908 KGSL_FT_ERR(device, "Skip EOF unsuccessful\n");
1909 ft_data->status = 1;
1910 } else {
1911 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001912 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001913 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001914 }
1915
1916play_good_cmds:
1917
Tarun Karra3164fb02013-02-05 15:38:51 -08001918 if (ft_data->status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001919 KGSL_FT_ERR(device, "Bad context commands failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001920 else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001921 KGSL_FT_INFO(device, "Bad context commands success\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001922
1923 if (adreno_context) {
1924 adreno_context->flags = (adreno_context->flags &
Tarun Karrad20d71a2013-01-25 15:38:57 -08001925 ~CTXT_FLAGS_GPU_HANG) | CTXT_FLAGS_GPU_HANG_FT;
Tarun Karradeeecc02013-01-21 23:42:17 -08001926 }
1927 adreno_dev->drawctxt_active = last_active_ctx;
1928 }
1929
Tarun Karrad20d71a2013-01-25 15:38:57 -08001930 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1931 ft_data->good_rb_buffer, ft_data->good_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001932
1933 if (ret) {
1934 /* If we fail here we can try to invalidate another
1935 * context and try fault tolerance again */
1936 ret = -EAGAIN;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001937 KGSL_FT_ERR(device, "Playing good commands unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001938 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001939 } else
1940 KGSL_FT_INFO(device, "Playing good commands successful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001941
1942 /* ringbuffer now has data from the last valid context id,
1943 * so restore the active_ctx to the last valid context */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001944 if (ft_data->last_valid_ctx_id) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001945 struct kgsl_context *last_ctx =
1946 idr_find(&device->context_idr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001947 ft_data->last_valid_ctx_id);
Tarun Karradeeecc02013-01-21 23:42:17 -08001948 if (last_ctx)
1949 adreno_dev->drawctxt_active = last_ctx->devctxt;
1950 }
1951
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001952done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001953 /* Turn off iommu clocks */
1954 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1955 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001956 return ret;
1957}
1958
1959static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001960adreno_ft(struct kgsl_device *device,
1961 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001962{
1963 int ret = 0;
1964 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1965 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1966 unsigned int timestamp;
1967
Tarun Karrad20d71a2013-01-25 15:38:57 -08001968 KGSL_FT_INFO(device,
1969 "Start Parameters: IB1: 0x%X, "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001970 "Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001971 ft_data->ib1, ft_data->context_id, ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001972
1973 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
Tarun Karrad20d71a2013-01-25 15:38:57 -08001974 KGSL_FT_INFO(device, "Last issued global timestamp: %x\n", timestamp);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001975
1976 /* We may need to replay commands multiple times based on whether
1977 * multiple contexts hang the GPU */
1978 while (true) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001979
Tarun Karrad20d71a2013-01-25 15:38:57 -08001980 ret = _adreno_ft(device, ft_data);
1981
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001982 if (-EAGAIN == ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001983 /* setup new fault tolerance parameters and retry, this
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001984 * means more than 1 contexts are causing hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001985 adreno_destroy_ft_data(ft_data);
1986 ret = adreno_setup_ft_data(device, ft_data);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001987 if (ret)
1988 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001989 KGSL_FT_INFO(device,
1990 "Retry. Parameters: "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001991 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001992 ft_data->ib1, ft_data->context_id,
1993 ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001994 } else {
1995 break;
1996 }
1997 }
1998
1999 if (ret)
2000 goto done;
2001
Tarun Karrad20d71a2013-01-25 15:38:57 -08002002 /* Restore correct states after fault tolerance */
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002003 if (adreno_dev->drawctxt_active)
2004 device->mmu.hwpagetable =
2005 adreno_dev->drawctxt_active->pagetable;
2006 else
2007 device->mmu.hwpagetable = device->mmu.defaultpagetable;
2008 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
2009 kgsl_sharedmem_writel(&device->memstore,
2010 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2011 eoptimestamp),
2012 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07002013
2014 /* switch to NULL ctxt */
2015 if (adreno_dev->drawctxt_active != NULL)
2016 adreno_drawctxt_switch(adreno_dev, NULL, 0);
2017
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002018done:
2019 adreno_set_max_ts_for_bad_ctxs(device);
2020 adreno_mark_context_status(device, ret);
Tarun Karra3164fb02013-02-05 15:38:51 -08002021 KGSL_FT_ERR(device, "policy 0x%X status 0x%x\n",
2022 ft_data->ft_policy, ret);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002023 return ret;
2024}
2025
2026int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002027adreno_dump_and_exec_ft(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002028{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002029 int result = -ETIMEDOUT;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002030 struct adreno_ft_data ft_data;
Tarun Karra696f89e2013-01-27 21:31:40 -08002031 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karrad2326b82013-01-28 19:09:41 -08002032 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2033 unsigned int curr_pwrlevel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002034
2035 if (device->state == KGSL_STATE_HUNG)
2036 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002037 if (device->state == KGSL_STATE_DUMP_AND_FT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002038 mutex_unlock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002039 wait_for_completion(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002040 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07002041 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002042 result = 0;
2043 } else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002044 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_FT);
2045 INIT_COMPLETION(device->ft_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002046 /* Detected a hang */
2047
Tarun Karrad2326b82013-01-28 19:09:41 -08002048 /* Run fault tolerance at max power level */
2049 curr_pwrlevel = pwr->active_pwrlevel;
2050 kgsl_pwrctrl_pwrlevel_change(device, pwr->max_pwrlevel);
2051
Tarun Karrad20d71a2013-01-25 15:38:57 -08002052 /* Get the fault tolerance data as soon as hang is detected */
2053 result = adreno_setup_ft_data(device, &ft_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002054
2055 /*
Tarun Karra696f89e2013-01-27 21:31:40 -08002056 * If long ib is detected, do not attempt postmortem or
2057 * snapshot, if GPU is still executing commands
2058 * we will get errors
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002059 */
Tarun Karra696f89e2013-01-27 21:31:40 -08002060 if (!adreno_dev->long_ib) {
2061 /*
2062 * Trigger an automatic dump of the state to
2063 * the console
2064 */
2065 kgsl_postmortem_dump(device, 0);
2066
2067 /*
2068 * Make a GPU snapshot. For now, do it after the
2069 * PM dump so we can at least be sure the PM dump
2070 * will work as it always has
2071 */
2072 kgsl_device_snapshot(device, 1);
2073 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002074
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002075 if (!result) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002076 result = adreno_ft(device, &ft_data);
2077 adreno_destroy_ft_data(&ft_data);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002078 }
Tarun Karra696f89e2013-01-27 21:31:40 -08002079
Tarun Karrad2326b82013-01-28 19:09:41 -08002080 /* restore power level */
2081 kgsl_pwrctrl_pwrlevel_change(device, curr_pwrlevel);
2082
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002083 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002084 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002085 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002086 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002087 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
2088 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08002089 complete_all(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002090 }
2091done:
2092 return result;
2093}
Tarun Karrad20d71a2013-01-25 15:38:57 -08002094EXPORT_SYMBOL(adreno_dump_and_exec_ft);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002095
2096static int adreno_getproperty(struct kgsl_device *device,
2097 enum kgsl_property_type type,
2098 void *value,
2099 unsigned int sizebytes)
2100{
2101 int status = -EINVAL;
2102 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2103
2104 switch (type) {
2105 case KGSL_PROP_DEVICE_INFO:
2106 {
2107 struct kgsl_devinfo devinfo;
2108
2109 if (sizebytes != sizeof(devinfo)) {
2110 status = -EINVAL;
2111 break;
2112 }
2113
2114 memset(&devinfo, 0, sizeof(devinfo));
2115 devinfo.device_id = device->id+1;
2116 devinfo.chip_id = adreno_dev->chip_id;
2117 devinfo.mmu_enabled = kgsl_mmu_enabled();
2118 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06002119 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
2120 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002121
2122 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
2123 0) {
2124 status = -EFAULT;
2125 break;
2126 }
2127 status = 0;
2128 }
2129 break;
2130 case KGSL_PROP_DEVICE_SHADOW:
2131 {
2132 struct kgsl_shadowprop shadowprop;
2133
2134 if (sizebytes != sizeof(shadowprop)) {
2135 status = -EINVAL;
2136 break;
2137 }
2138 memset(&shadowprop, 0, sizeof(shadowprop));
2139 if (device->memstore.hostptr) {
2140 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2141 * anything to mmap().
2142 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07002143 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002144 shadowprop.size = device->memstore.size;
2145 /* GSL needs this to be set, even if it
2146 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002147 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2148 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002149 }
2150 if (copy_to_user(value, &shadowprop,
2151 sizeof(shadowprop))) {
2152 status = -EFAULT;
2153 break;
2154 }
2155 status = 0;
2156 }
2157 break;
2158 case KGSL_PROP_MMU_ENABLE:
2159 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002160 int mmu_prop = kgsl_mmu_enabled();
2161
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002162 if (sizebytes != sizeof(int)) {
2163 status = -EINVAL;
2164 break;
2165 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002166 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002167 status = -EFAULT;
2168 break;
2169 }
2170 status = 0;
2171 }
2172 break;
2173 case KGSL_PROP_INTERRUPT_WAITS:
2174 {
2175 int int_waits = 1;
2176 if (sizebytes != sizeof(int)) {
2177 status = -EINVAL;
2178 break;
2179 }
2180 if (copy_to_user(value, &int_waits, sizeof(int))) {
2181 status = -EFAULT;
2182 break;
2183 }
2184 status = 0;
2185 }
2186 break;
2187 default:
2188 status = -EINVAL;
2189 }
2190
2191 return status;
2192}
2193
Jordan Crousef7370f82012-04-18 09:31:07 -06002194static int adreno_setproperty(struct kgsl_device *device,
2195 enum kgsl_property_type type,
2196 void *value,
2197 unsigned int sizebytes)
2198{
2199 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08002200 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06002201
2202 switch (type) {
2203 case KGSL_PROP_PWRCTRL: {
2204 unsigned int enable;
2205 struct kgsl_device_platform_data *pdata =
2206 kgsl_device_get_drvdata(device);
2207
2208 if (sizebytes != sizeof(enable))
2209 break;
2210
2211 if (copy_from_user(&enable, (void __user *) value,
2212 sizeof(enable))) {
2213 status = -EFAULT;
2214 break;
2215 }
2216
2217 if (enable) {
2218 if (pdata->nap_allowed)
2219 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002220 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002221 kgsl_pwrscale_enable(device);
2222 } else {
2223 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002224 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002225 kgsl_pwrscale_disable(device);
2226 }
2227
2228 status = 0;
2229 }
2230 break;
Tarun Karra3164fb02013-02-05 15:38:51 -08002231 case KGSL_PROP_FAULT_TOLERANCE: {
2232 struct kgsl_ft_config ftd;
2233
2234 if (sizebytes != sizeof(ftd))
2235 break;
2236
2237 if (copy_from_user(&ftd, (void __user *) value,
2238 sizeof(ftd))) {
2239 status = -EFAULT;
2240 break;
2241 }
2242
2243 if (ftd.ft_policy)
2244 adreno_dev->ft_policy = ftd.ft_policy;
2245 else
2246 adreno_dev->ft_policy = KGSL_FT_DEFAULT_POLICY;
2247
2248 if (ftd.ft_pm_dump)
2249 device->pm_dump_enable = 1;
2250 else
2251 device->pm_dump_enable = 0;
2252
2253 }
2254 break;
Jordan Crousef7370f82012-04-18 09:31:07 -06002255 default:
2256 break;
2257 }
2258
2259 return status;
2260}
2261
Jordan Crousea29a2e02012-08-14 09:09:23 -06002262static int adreno_ringbuffer_drain(struct kgsl_device *device,
2263 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002264{
2265 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2266 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002267 unsigned long wait;
2268 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2269
2270 if (!(rb->flags & KGSL_FLAGS_STARTED))
2271 return 0;
2272
2273 /*
2274 * The first time into the loop, wait for 100 msecs and kick wptr again
2275 * to ensure that the hardware has updated correctly. After that, kick
2276 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2277 * expires
2278 */
2279
2280 wait = jiffies + msecs_to_jiffies(100);
2281
Jordan Crousea29a2e02012-08-14 09:09:23 -06002282 do {
2283 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002284 /* Check to see if the core is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002285 if (adreno_ft_detect(device, regs))
Jordan Crousea29a2e02012-08-14 09:09:23 -06002286 return -ETIMEDOUT;
2287
2288 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2289 }
2290 GSL_RB_GET_READPTR(rb, &rb->rptr);
2291
2292 if (time_after(jiffies, timeout)) {
2293 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2294 rb->rptr, rb->wptr);
2295 return -ETIMEDOUT;
2296 }
2297 } while (rb->rptr != rb->wptr);
2298
2299 return 0;
2300}
2301
2302/* Caller must hold the device mutex. */
2303int adreno_idle(struct kgsl_device *device)
2304{
2305 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002306 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302307 unsigned long wait_time;
2308 unsigned long wait_time_part;
Tarun Karra696f89e2013-01-27 21:31:40 -08002309 unsigned int prev_reg_val[ft_detect_regs_count];
Tarun Karra3335f142012-06-19 14:11:48 -07002310
2311 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002312
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002313 kgsl_cffdump_regpoll(device->id,
2314 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002315 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002317retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002318 /* First, wait for the ringbuffer to drain */
2319 if (adreno_ringbuffer_drain(device, prev_reg_val))
2320 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002321
2322 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002323 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002324 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2325
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002326 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002327 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2328 &rbbm_status);
2329 if (adreno_is_a2xx(adreno_dev)) {
2330 if (rbbm_status == 0x110)
2331 return 0;
2332 } else {
2333 if (!(rbbm_status & 0x80000000))
2334 return 0;
2335 }
Tarun Karra3335f142012-06-19 14:11:48 -07002336
2337 /* Dont wait for timeout, detect hang faster.
2338 */
2339 if (time_after(jiffies, wait_time_part)) {
2340 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002341 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra696f89e2013-01-27 21:31:40 -08002342 if ((adreno_ft_detect(device, prev_reg_val)))
Tarun Karra3335f142012-06-19 14:11:48 -07002343 goto err;
2344 }
2345
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002346 }
2347
2348err:
2349 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08002350 if (KGSL_STATE_DUMP_AND_FT != device->state &&
2351 !adreno_dump_and_exec_ft(device)) {
2352 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002353 goto retry;
2354 }
2355 return -ETIMEDOUT;
2356}
2357
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002358/**
2359 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2360 * rbbm_status register
2361 * @device - Pointer to the GPU device whose idle status is to be
2362 * checked
2363 * @returns - Returns whether the core is idle (based on rbbm_status)
2364 * false if the core is active, true if the core is idle
2365 */
2366static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2367{
2368 unsigned int reg_rbbm_status;
2369 bool status = false;
2370 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2371
2372 /* Is the core idle? */
2373 adreno_regread(device,
2374 adreno_dev->gpudev->reg_rbbm_status,
2375 &reg_rbbm_status);
2376
2377 if (adreno_is_a2xx(adreno_dev)) {
2378 if (reg_rbbm_status == 0x110)
2379 status = true;
2380 } else {
2381 if (!(reg_rbbm_status & 0x80000000))
2382 status = true;
2383 }
2384 return status;
2385}
2386
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002387static unsigned int adreno_isidle(struct kgsl_device *device)
2388{
2389 int status = false;
2390 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2391 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002392
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002393 WARN_ON(device->state == KGSL_STATE_INIT);
2394 /* If the device isn't active, don't force it on. */
2395 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002396 /* Is the ring buffer is empty? */
2397 GSL_RB_GET_READPTR(rb, &rb->rptr);
2398 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
2399 /* Is the core idle? */
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002400 status = is_adreno_rbbm_status_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002401 }
2402 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002403 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002404 }
2405 return status;
2406}
2407
2408/* Caller must hold the device mutex. */
2409static int adreno_suspend_context(struct kgsl_device *device)
2410{
2411 int status = 0;
2412 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2413
2414 /* switch to NULL ctxt */
2415 if (adreno_dev->drawctxt_active != NULL) {
2416 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002417 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002418 }
2419
2420 return status;
2421}
2422
Jordan Crouse233b2092012-04-18 09:31:09 -06002423/* Find a memory structure attached to an adreno context */
2424
2425struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2426 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2427{
2428 struct kgsl_context *context;
2429 struct adreno_context *adreno_context = NULL;
2430 int next = 0;
2431
2432 while (1) {
2433 context = idr_get_next(&device->context_idr, &next);
2434 if (context == NULL)
2435 break;
2436
2437 adreno_context = (struct adreno_context *)context->devctxt;
2438
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002439 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2440 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002441 struct kgsl_memdesc *desc;
2442
2443 desc = &adreno_context->gpustate;
2444 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2445 return desc;
2446
2447 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2448 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2449 return desc;
2450 }
2451 next = next + 1;
2452 }
2453
2454 return NULL;
2455}
2456
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002457struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002458 unsigned int pt_base,
2459 unsigned int gpuaddr,
2460 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002461{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002462 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002463 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2464 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2465
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002466 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2467 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002468
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002469 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2470 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002471
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002472 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2473 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002474
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002475 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2476 size))
2477 return &device->mmu.setstate_memory;
2478
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002479 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002480
2481 if (entry)
2482 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002483
Jordan Crouse233b2092012-04-18 09:31:09 -06002484 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002485}
2486
2487uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2488 unsigned int gpuaddr, unsigned int size)
2489{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002490 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002491
2492 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2493
2494 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002495}
2496
2497void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2498 unsigned int *value)
2499{
2500 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002501 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2502 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002503
2504 if (!in_interrupt())
2505 kgsl_pre_hwaccess(device);
2506
2507 /*ensure this read finishes before the next one.
2508 * i.e. act like normal readl() */
2509 *value = __raw_readl(reg);
2510 rmb();
2511}
2512
2513void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2514 unsigned int value)
2515{
2516 unsigned int *reg;
2517
Jordan Crouse7501d452012-04-19 08:58:44 -06002518 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002519
2520 if (!in_interrupt())
2521 kgsl_pre_hwaccess(device);
2522
2523 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002524 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002525
2526 /*ensure previous writes post before this one,
2527 * i.e. act like normal writel() */
2528 wmb();
2529 __raw_writel(value, reg);
2530}
2531
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002532static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2533{
2534 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002535 if (k_ctxt != NULL) {
2536 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002537 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2538 context_id = KGSL_CONTEXT_INVALID;
2539 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2540 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002541 }
2542
2543 return context_id;
2544}
2545
Jordan Crouse313faf62012-11-20 15:12:28 -07002546static void adreno_next_event(struct kgsl_device *device,
2547 struct kgsl_event *event)
2548{
2549 int status;
2550 unsigned int ref_ts, enableflag;
2551 unsigned int context_id = _get_context_id(event->context);
2552 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2553
2554 status = kgsl_check_timestamp(device, event->context, event->timestamp);
2555 if (!status) {
2556 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2557 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2558 /*
2559 * Barrier is needed here to make sure the read from memstore
2560 * has posted
2561 */
2562
2563 mb();
2564
2565 if (enableflag) {
2566 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
2567 KGSL_MEMSTORE_OFFSET(context_id,
2568 ref_wait_ts));
2569
2570 /* Make sure the memstore read has posted */
2571 mb();
2572 if (timestamp_cmp(ref_ts, event->timestamp) >= 0) {
2573 kgsl_sharedmem_writel(&device->memstore,
2574 KGSL_MEMSTORE_OFFSET(context_id,
2575 ref_wait_ts), event->timestamp);
2576 /* Make sure the memstore write is posted */
2577 wmb();
2578 }
2579 } else {
2580 unsigned int cmds[2];
2581 kgsl_sharedmem_writel(&device->memstore,
2582 KGSL_MEMSTORE_OFFSET(context_id,
2583 ref_wait_ts), event->timestamp);
2584 enableflag = 1;
2585 kgsl_sharedmem_writel(&device->memstore,
2586 KGSL_MEMSTORE_OFFSET(context_id,
2587 ts_cmp_enable), enableflag);
2588
2589 /* Make sure the memstore write gets posted */
2590 wmb();
2591
2592 /*
2593 * submit a dummy packet so that even if all
2594 * commands upto timestamp get executed we will still
2595 * get an interrupt
2596 */
2597 cmds[0] = cp_type3_packet(CP_NOP, 1);
2598 cmds[1] = 0;
2599
2600 if (adreno_dev->drawctxt_active)
2601 adreno_ringbuffer_issuecmds_intr(device,
2602 event->context, &cmds[0], 2);
2603 }
2604 }
2605}
2606
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002607static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002608 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002609{
2610 int status;
2611 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002612 unsigned int context_id;
2613
2614 mutex_lock(&device->mutex);
2615 context_id = _get_context_id(context);
2616 /*
2617 * If the context ID is invalid, we are in a race with
2618 * the context being destroyed by userspace so bail.
2619 */
2620 if (context_id == KGSL_CONTEXT_INVALID) {
2621 KGSL_DRV_WARN(device, "context was detached");
2622 status = -EINVAL;
2623 goto unlock;
2624 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002625
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002626 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002627 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002628 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002629 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002630 mb();
2631
2632 if (enableflag) {
2633 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002634 KGSL_MEMSTORE_OFFSET(context_id,
2635 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002636 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002637 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002638 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002639 KGSL_MEMSTORE_OFFSET(context_id,
2640 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002641 wmb();
2642 }
2643 } else {
2644 unsigned int cmds[2];
2645 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002646 KGSL_MEMSTORE_OFFSET(context_id,
2647 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002648 enableflag = 1;
2649 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002650 KGSL_MEMSTORE_OFFSET(context_id,
2651 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002652 wmb();
2653 /* submit a dummy packet so that even if all
2654 * commands upto timestamp get executed we will still
2655 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002656 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002657 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002658
Vladimir Razgulin38345302013-01-22 18:41:59 -07002659 if (context && device->state != KGSL_STATE_SLUMBER)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002660 adreno_ringbuffer_issuecmds_intr(device,
2661 context, &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002662 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002663 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002664unlock:
2665 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002666
2667 return status;
2668}
2669
2670/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002671 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002672 placing a process in wait q. For conditional interrupts we expect the
2673 process to already be in its wait q when its exit condition checking
2674 function is called.
2675*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002676#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002677({ \
2678 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002679 if (io) \
2680 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2681 else \
2682 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002683 __ret; \
2684})
2685
Tarun Karra3335f142012-06-19 14:11:48 -07002686
2687
Tarun Karra696f89e2013-01-27 21:31:40 -08002688unsigned int adreno_ft_detect(struct kgsl_device *device,
Tarun Karra3335f142012-06-19 14:11:48 -07002689 unsigned int *prev_reg_val)
2690{
2691 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karra696f89e2013-01-27 21:31:40 -08002692 unsigned int curr_reg_val[ft_detect_regs_count];
2693 unsigned int fast_hang_detected = 1;
2694 unsigned int long_ib_detected = 1;
Tarun Karra3335f142012-06-19 14:11:48 -07002695 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002696 static unsigned long next_hang_detect_time;
Tarun Karra696f89e2013-01-27 21:31:40 -08002697 static unsigned int prev_global_ts;
2698 unsigned int curr_global_ts = 0;
2699 unsigned int curr_context_id = 0;
2700 static struct adreno_context *curr_context;
2701 static struct kgsl_context *context;
Tarun Karra3335f142012-06-19 14:11:48 -07002702
2703 if (!adreno_dev->fast_hang_detect)
Tarun Karra696f89e2013-01-27 21:31:40 -08002704 fast_hang_detected = 0;
2705
2706 if (!adreno_dev->long_ib_detect)
2707 long_ib_detected = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002708
Jordan Crousecca61142012-11-20 10:54:24 -07002709 if (is_adreno_rbbm_status_idle(device)) {
2710
2711 /*
2712 * On A20X if the RPTR != WPTR and the device is idle, then
2713 * the last write to WPTR probably failed to latch so write it
2714 * again
2715 */
2716
2717 if (adreno_is_a2xx(adreno_dev)) {
2718 unsigned int rptr;
2719 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2720 if (rptr != adreno_dev->ringbuffer.wptr)
2721 adreno_regwrite(device, REG_CP_RB_WPTR,
2722 adreno_dev->ringbuffer.wptr);
2723 }
2724
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002725 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002726 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002727
Tarun Karra4648db92013-02-01 17:03:40 -08002728 /*
2729 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2730 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2731 * from the last time do nothing.
2732 */
2733 if ((next_hang_detect_time) &&
2734 (time_before(jiffies, next_hang_detect_time)))
2735 return 0;
2736 else
2737 next_hang_detect_time = (jiffies +
2738 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2739
Tarun Karra696f89e2013-01-27 21:31:40 -08002740 /* Read the current Hang detect reg values here */
2741 for (i = 0; i < ft_detect_regs_count; i++) {
2742 if (ft_detect_regs[i] == 0)
Jordan Crouseb5c80482012-10-03 09:38:41 -06002743 continue;
Tarun Karra696f89e2013-01-27 21:31:40 -08002744 adreno_regread(device, ft_detect_regs[i],
2745 &curr_reg_val[i]);
Tarun Karra3335f142012-06-19 14:11:48 -07002746 }
2747
Tarun Karra696f89e2013-01-27 21:31:40 -08002748 /* Read the current global timestamp here */
2749 kgsl_sharedmem_readl(&device->memstore,
2750 &curr_global_ts,
2751 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2752 eoptimestamp));
2753
2754 mb();
2755
2756 if (curr_global_ts == prev_global_ts) {
2757
2758 /* Get the current context here */
2759 if (context == NULL) {
2760 kgsl_sharedmem_readl(&device->memstore,
2761 &curr_context_id,
2762 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2763 current_context));
2764 context = idr_find(&device->context_idr,
2765 curr_context_id);
2766 if (context != NULL) {
2767 curr_context = context->devctxt;
2768 curr_context->ib_gpu_time_used = 0;
2769 } else {
2770 KGSL_DRV_ERR(device,
2771 "Fault tolerance no context found\n");
2772 }
2773 }
2774
2775 mb();
2776
2777 if (curr_context != NULL) {
2778
2779 curr_context->ib_gpu_time_used += KGSL_TIMEOUT_PART;
2780 KGSL_FT_INFO(device,
2781 "Proc %s used GPU Time %d ms on timestamp 0x%X\n",
2782 curr_context->pid_name, curr_context->ib_gpu_time_used,
2783 curr_global_ts+1);
2784
2785 for (i = 0; i < ft_detect_regs_count; i++) {
2786 if (curr_reg_val[i] != prev_reg_val[i]) {
2787 fast_hang_detected = 0;
2788
2789 /* Check for long IB here */
2790 if ((i >=
2791 LONG_IB_DETECT_REG_INDEX_START)
2792 &&
2793 (i <=
2794 LONG_IB_DETECT_REG_INDEX_END))
2795 long_ib_detected = 0;
2796 }
2797 }
2798
2799 if (fast_hang_detected) {
2800 KGSL_FT_ERR(device,
2801 "Proc %s, ctxt_id %d ts %d triggered fault tolerance"
2802 " on global ts %d\n",
2803 curr_context->pid_name, curr_context->id
2804 , (kgsl_readtimestamp(device, context,
2805 KGSL_TIMESTAMP_RETIRED)+1),
2806 curr_global_ts+1);
2807 return 1;
2808 }
2809
Tarun Karra83297222013-02-05 19:45:49 -08002810 if ((long_ib_detected) &&
2811 (!(curr_context->flags &
2812 CTXT_FLAGS_NO_FAULT_TOLERANCE))) {
Tarun Karra696f89e2013-01-27 21:31:40 -08002813 curr_context->ib_gpu_time_used +=
2814 KGSL_TIMEOUT_PART;
2815 if (curr_context->ib_gpu_time_used >
2816 KGSL_TIMEOUT_LONG_IB_DETECTION) {
2817 if (adreno_dev->long_ib_ts !=
2818 curr_global_ts) {
2819 KGSL_FT_ERR(device,
2820 "Proc %s, ctxt_id %d ts %d"
2821 "used GPU for %d ms long ib "
2822 "detected on global ts %d\n",
2823 curr_context->pid_name,
2824 curr_context->id,
2825 (kgsl_readtimestamp(device,
2826 context,
2827 KGSL_TIMESTAMP_RETIRED)+1),
2828 curr_context->ib_gpu_time_used,
2829 curr_global_ts+1);
2830 adreno_dev->long_ib = 1;
2831 adreno_dev->long_ib_ts =
2832 curr_global_ts;
2833 curr_context->ib_gpu_time_used =
2834 0;
2835 return 1;
2836 }
2837 }
2838 }
2839 } else {
2840 KGSL_FT_ERR(device,
2841 "Last context unknown id:%d\n",
2842 curr_context_id);
2843 }
2844 } else {
2845 /* GPU is moving forward */
2846 prev_global_ts = curr_global_ts;
2847 context = NULL;
2848 curr_context = NULL;
2849 adreno_dev->long_ib = 0;
2850 adreno_dev->long_ib_ts = 0;
2851 }
2852
2853
2854 /* If hangs are not detected copy the current reg values
2855 * to previous values and return no hang */
2856 for (i = 0; i < ft_detect_regs_count; i++)
2857 prev_reg_val[i] = curr_reg_val[i];
2858 return 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002859}
2860
Jordan Crouse92446a62012-11-15 11:00:06 -07002861/**
2862 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2863 * @device - pointer to a KGSL device structure
2864 * @context - pointer to the active KGSL context
2865 * @timestamp - the timestamp that the process was waiting for
2866 *
Tarun Karrad20d71a2013-01-25 15:38:57 -08002867 * Process a possible GPU hang and try fault tolerance from it
2868 * cleanly
Jordan Crouse92446a62012-11-15 11:00:06 -07002869 */
2870static int adreno_handle_hang(struct kgsl_device *device,
2871 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002872{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002873 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002874 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002875 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002876 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002877
Jordan Crouse92446a62012-11-15 11:00:06 -07002878 /* Do one last check to see if we somehow made it through */
2879 if (kgsl_check_timestamp(device, context, timestamp))
2880 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002881
2882 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002883
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002884 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2885 mb();
2886
Tarun Karra696f89e2013-01-27 21:31:40 -08002887 KGSL_DRV_WARN(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002888 "Device hang detected while waiting for timestamp: "
2889 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002890 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2891 context_id, timestamp, context_id, ts_issued, context_id,
2892 kgsl_readtimestamp(device, context,
2893 KGSL_TIMESTAMP_RETIRED),
2894 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002895
Tarun Karrad20d71a2013-01-25 15:38:57 -08002896 /* Return 0 after a successful fault tolerance */
2897 if (!adreno_dump_and_exec_ft(device))
Jordan Crouse92446a62012-11-15 11:00:06 -07002898 return 0;
2899
2900 return -ETIMEDOUT;
2901}
2902
2903static int _check_pending_timestamp(struct kgsl_device *device,
2904 struct kgsl_context *context, unsigned int timestamp)
2905{
2906 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2907 unsigned int context_id = _get_context_id(context);
2908 unsigned int ts_issued;
2909
2910 if (context_id == KGSL_CONTEXT_INVALID)
2911 return -EINVAL;
2912
2913 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2914
2915 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2916 return 0;
2917
2918 if (context && !context->wait_on_invalid_ts) {
2919 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2920 context_id, timestamp, context_id, ts_issued);
2921
2922 /* Only print this message once */
2923 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002924 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002925
2926 return -EINVAL;
2927}
2928
2929/**
2930 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2931 * @device - pointer to a KGSL device structure
2932 * @context - pointer to the active kgsl context
2933 * @timestamp - GPU timestamp to wait for
2934 * @msecs - amount of time to wait (in milliseconds)
2935 *
2936 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2937 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2938 * one if it happened. Otherwise, spend most of our time in an interruptible
2939 * wait for the timestamp interrupt to be processed. This function must be
2940 * called with the mutex already held.
2941 */
2942static int adreno_waittimestamp(struct kgsl_device *device,
2943 struct kgsl_context *context,
2944 unsigned int timestamp,
2945 unsigned int msecs)
2946{
2947 static unsigned int io_cnt;
2948 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2949 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2950 unsigned int context_id = _get_context_id(context);
Tarun Karra696f89e2013-01-27 21:31:40 -08002951 unsigned int prev_reg_val[ft_detect_regs_count];
Jordan Crouse92446a62012-11-15 11:00:06 -07002952 unsigned int time_elapsed = 0;
2953 unsigned int wait;
2954 int ts_compare = 1;
2955 int io, ret = -ETIMEDOUT;
2956
2957 /* Get out early if the context has already been destroyed */
2958
2959 if (context_id == KGSL_CONTEXT_INVALID) {
2960 KGSL_DRV_WARN(device, "context was detached");
2961 return -EINVAL;
2962 }
2963
2964 /*
2965 * Check to see if the requested timestamp is "newer" then the last
2966 * timestamp issued. If it is complain once and return error. Only
2967 * print the message once per context so that badly behaving
2968 * applications don't spam the logs
2969 */
2970
2971 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2972 if (_check_pending_timestamp(device, context, timestamp))
2973 return -EINVAL;
2974
2975 /* Reset the invalid timestamp flag on a valid wait */
2976 context->wait_on_invalid_ts = false;
2977 }
2978
2979
2980 /* Clear the registers used for hang detection */
2981 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2982
2983 /*
2984 * On the first time through the loop only wait 100ms.
2985 * this gives enough time for the engine to start moving and oddly
2986 * provides better hang detection results than just going the full
2987 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2988 * is if msecs happens to be < 100ms then just use the full timeout
2989 */
2990
2991 wait = 100;
2992
2993 do {
2994 long status;
2995
Jordan Crouse92446a62012-11-15 11:00:06 -07002996 /*
2997 * if the timestamp happens while we're not
2998 * waiting, there's a chance that an interrupt
2999 * will not be generated and thus the timestamp
3000 * work needs to be queued.
3001 */
3002
3003 if (kgsl_check_timestamp(device, context, timestamp)) {
3004 queue_work(device->work_queue, &device->ts_expired_ws);
3005 ret = 0;
3006 break;
3007 }
3008
3009 /* Check to see if the GPU is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08003010 if (adreno_ft_detect(device, prev_reg_val)) {
Jordan Crouse92446a62012-11-15 11:00:06 -07003011 ret = adreno_handle_hang(device, context, timestamp);
3012 break;
3013 }
3014
3015 /*
3016 * For proper power accounting sometimes we need to call
3017 * io_wait_interruptible_timeout and sometimes we need to call
3018 * plain old wait_interruptible_timeout. We call the regular
3019 * timeout N times out of 100, where N is a number specified by
3020 * the current power level
3021 */
3022
3023 io_cnt = (io_cnt + 1) % 100;
3024 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
3025 ? 0 : 1;
3026
3027 mutex_unlock(&device->mutex);
3028
3029 /* Wait for a timestamp event */
3030 status = kgsl_wait_event_interruptible_timeout(
3031 device->wait_queue,
3032 kgsl_check_interrupt_timestamp(device, context,
3033 timestamp), msecs_to_jiffies(wait), io);
3034
3035 mutex_lock(&device->mutex);
3036
3037 /*
3038 * If status is non zero then either the condition was satisfied
3039 * or there was an error. In either event, this is the end of
3040 * the line for us
3041 */
3042
3043 if (status != 0) {
3044 ret = (status > 0) ? 0 : (int) status;
3045 break;
3046 }
Jordan Crouse92446a62012-11-15 11:00:06 -07003047 time_elapsed += wait;
3048
3049 /* If user specified timestamps are being used, wait at least
3050 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
3051 * issue a IB for a timestamp before checking to see if the
3052 * current timestamp we are waiting for is valid or not
3053 */
3054
3055 if (ts_compare && (adreno_ctx &&
3056 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
3057 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
3058 ret = _check_pending_timestamp(device, context,
3059 timestamp);
3060 if (ret)
3061 break;
3062
3063 /* Don't do this check again */
3064 ts_compare = 0;
3065
3066 /*
3067 * Reset the invalid timestamp flag on a valid
3068 * wait
3069 */
3070 context->wait_on_invalid_ts = false;
3071 }
3072 }
3073
3074 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003075 * We want to wait the floor of KGSL_TIMEOUT_PART
3076 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07003077 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003078
3079 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
3080 wait = KGSL_TIMEOUT_PART;
3081 else
3082 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07003083
3084 } while (!msecs || time_elapsed < msecs);
3085
3086 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003087}
3088
3089static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003090 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003091{
3092 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003093 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003094
Jeremy Gebben9ad86922012-05-08 15:33:23 -06003095 /*
3096 * If the context ID is invalid, we are in a race with
3097 * the context being destroyed by userspace so bail.
3098 */
3099 if (context_id == KGSL_CONTEXT_INVALID) {
3100 KGSL_DRV_WARN(device, "context was detached");
3101 return timestamp;
3102 }
Jordan Crousec659f382012-04-16 11:10:41 -06003103 switch (type) {
3104 case KGSL_TIMESTAMP_QUEUED: {
3105 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3106 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
3107
3108 timestamp = rb->timestamp[context_id];
3109 break;
3110 }
3111 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003112 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06003113 break;
3114 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003115 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06003116 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
3117 break;
3118 }
3119
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003120 rmb();
3121
3122 return timestamp;
3123}
3124
3125static long adreno_ioctl(struct kgsl_device_private *dev_priv,
3126 unsigned int cmd, void *data)
3127{
3128 int result = 0;
3129 struct kgsl_drawctxt_set_bin_base_offset *binbase;
3130 struct kgsl_context *context;
3131
3132 switch (cmd) {
3133 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
3134 binbase = data;
3135
3136 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
3137 if (context) {
3138 adreno_drawctxt_set_bin_base_offset(
3139 dev_priv->device, context, binbase->offset);
3140 } else {
3141 result = -EINVAL;
3142 KGSL_DRV_ERR(dev_priv->device,
3143 "invalid drawctxt drawctxt_id %d "
3144 "device_id=%d\n",
3145 binbase->drawctxt_id, dev_priv->device->id);
3146 }
3147 break;
3148
3149 default:
3150 KGSL_DRV_INFO(dev_priv->device,
3151 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07003152 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003153 break;
3154 }
3155 return result;
3156
3157}
3158
3159static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
3160{
3161 gpu_freq /= 1000000;
3162 return ticks / gpu_freq;
3163}
3164
3165static void adreno_power_stats(struct kgsl_device *device,
3166 struct kgsl_power_stats *stats)
3167{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003168 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003169 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003170 unsigned int cycles;
3171
3172 /* Get the busy cycles counted since the counter was last reset */
3173 /* Calling this function also resets and restarts the counter */
3174
3175 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003176
3177 /* In order to calculate idle you have to have run the algorithm *
3178 * at least once to get a start time. */
3179 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003180 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003181 stats->total_time = tmp - pwr->time;
3182 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003183 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003184 pwrlevels[device->pwrctrl.active_pwrlevel].
3185 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003186 } else {
3187 stats->total_time = 0;
3188 stats->busy_time = 0;
3189 pwr->time = ktime_to_us(ktime_get());
3190 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003191}
3192
3193void adreno_irqctrl(struct kgsl_device *device, int state)
3194{
Jordan Crousea78c9172011-07-11 13:14:09 -06003195 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3196 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003197}
3198
Jordan Croused6535882012-06-20 08:22:16 -06003199static unsigned int adreno_gpuid(struct kgsl_device *device,
3200 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07003201{
3202 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3203
Jordan Croused6535882012-06-20 08:22:16 -06003204 /* Some applications need to know the chip ID too, so pass
3205 * that as a parameter */
3206
3207 if (chipid != NULL)
3208 *chipid = adreno_dev->chip_id;
3209
Jordan Crousea0758f22011-12-07 11:19:22 -07003210 /* Standard KGSL gpuid format:
3211 * top word is 0x0002 for 2D or 0x0003 for 3D
3212 * Bottom word is core specific identifer
3213 */
3214
3215 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
3216}
3217
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003218static const struct kgsl_functable adreno_functable = {
3219 /* Mandatory functions */
3220 .regread = adreno_regread,
3221 .regwrite = adreno_regwrite,
3222 .idle = adreno_idle,
3223 .isidle = adreno_isidle,
3224 .suspend_context = adreno_suspend_context,
3225 .start = adreno_start,
3226 .stop = adreno_stop,
3227 .getproperty = adreno_getproperty,
3228 .waittimestamp = adreno_waittimestamp,
3229 .readtimestamp = adreno_readtimestamp,
3230 .issueibcmds = adreno_ringbuffer_issueibcmds,
3231 .ioctl = adreno_ioctl,
3232 .setup_pt = adreno_setup_pt,
3233 .cleanup_pt = adreno_cleanup_pt,
3234 .power_stats = adreno_power_stats,
3235 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07003236 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07003237 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06003238 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003239 /* Optional functions */
3240 .setstate = adreno_setstate,
3241 .drawctxt_create = adreno_drawctxt_create,
3242 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06003243 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06003244 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07003245 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003246};
3247
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003248static struct platform_driver adreno_platform_driver = {
3249 .probe = adreno_probe,
3250 .remove = __devexit_p(adreno_remove),
3251 .suspend = kgsl_suspend_driver,
3252 .resume = kgsl_resume_driver,
3253 .id_table = adreno_id_table,
3254 .driver = {
3255 .owner = THIS_MODULE,
3256 .name = DEVICE_3D_NAME,
3257 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06003258 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003259 }
3260};
3261
3262static int __init kgsl_3d_init(void)
3263{
3264 return platform_driver_register(&adreno_platform_driver);
3265}
3266
3267static void __exit kgsl_3d_exit(void)
3268{
3269 platform_driver_unregister(&adreno_platform_driver);
3270}
3271
3272module_init(kgsl_3d_init);
3273module_exit(kgsl_3d_exit);
3274
3275MODULE_DESCRIPTION("3D Graphics driver");
3276MODULE_VERSION("1.2");
3277MODULE_LICENSE("GPL v2");
3278MODULE_ALIAS("platform:kgsl_3d");