blob: b49261ce14b60128dde02d51a16c9429d79b538b [file] [log] [blame]
Carter Cooper8179f5a2012-12-17 11:32:27 -07001/* Copyright (c) 2002,2007-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
Steve Mucklef132c6c2012-06-06 18:30:57 -070013#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070014#include <linux/uaccess.h>
15#include <linux/vmalloc.h>
16#include <linux/ioctl.h>
17#include <linux/sched.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060018#include <linux/of.h>
19#include <linux/of_device.h>
Tarun Karra3164fb02013-02-05 15:38:51 -080020#include <linux/msm_kgsl.h>
Tarun Karra59e79542013-04-10 10:25:25 -070021#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022
23#include <mach/socinfo.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060024#include <mach/msm_bus_board.h>
25#include <mach/msm_bus.h>
26#include <mach/msm_dcvs.h>
27#include <mach/msm_dcvs_scm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29#include "kgsl.h"
30#include "kgsl_pwrscale.h"
31#include "kgsl_cffdump.h"
32#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060033#include "kgsl_iommu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034
35#include "adreno.h"
36#include "adreno_pm4types.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070038#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070039#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040
41#define DRIVER_VERSION_MAJOR 3
42#define DRIVER_VERSION_MINOR 1
43
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044/* Adreno MH arbiter config*/
45#define ADRENO_CFG_MHARB \
46 (0x10 \
47 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
48 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
50 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
53 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
54 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
55 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
56 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
57 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
58 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
59 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
60 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
61
62#define ADRENO_MMU_CONFIG \
63 (0x01 \
64 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
69 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
70 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
71 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
72 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
73 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
74 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
75
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076static const struct kgsl_functable adreno_functable;
77
78static struct adreno_device device_3d0 = {
79 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070080 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081 .name = DEVICE_3D0_NAME,
82 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060083 .mh = {
84 .mharb = ADRENO_CFG_MHARB,
85 /* Remove 1k boundary check in z470 to avoid a GPU
86 * hang. Notice that this solution won't work if
87 * both EBI and SMI are used
88 */
89 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090 /* turn off memory protection unit by setting
91 acceptable physical address range to include
92 all pages. */
93 .mpu_base = 0x00000000,
94 .mpu_range = 0xFFFFF000,
95 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060096 .mmu = {
97 .config = ADRENO_MMU_CONFIG,
98 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102 .iomemname = KGSL_3D0_REG_MEMORY,
103 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600105 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
107 .suspend = kgsl_early_suspend_driver,
108 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600110#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600112 .gmem_base = 0,
113 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114 .pfp_fw = NULL,
115 .pm4_fw = NULL,
Jordan Crouse21f75a02012-08-09 15:08:59 -0600116 .wait_timeout = 0, /* in milliseconds, 0 means disabled */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600117 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118};
119
Tarun Karra3335f142012-06-19 14:11:48 -0700120/* This set of registers are used for Hang detection
121 * If the values of these registers are same after
122 * KGSL_TIMEOUT_PART time, GPU hang is reported in
123 * kernel log.
Tarun Karra696f89e2013-01-27 21:31:40 -0800124 * *****ALERT******ALERT********ALERT*************
125 * Order of registers below is important, registers
126 * from LONG_IB_DETECT_REG_INDEX_START to
127 * LONG_IB_DETECT_REG_INDEX_END are used in long ib detection.
Tarun Karra3335f142012-06-19 14:11:48 -0700128 */
Tarun Karra696f89e2013-01-27 21:31:40 -0800129#define LONG_IB_DETECT_REG_INDEX_START 1
130#define LONG_IB_DETECT_REG_INDEX_END 5
131
132unsigned int ft_detect_regs[] = {
Tarun Karra3335f142012-06-19 14:11:48 -0700133 A3XX_RBBM_STATUS,
Tarun Karra696f89e2013-01-27 21:31:40 -0800134 REG_CP_RB_RPTR, /* LONG_IB_DETECT_REG_INDEX_START */
Tarun Karra3335f142012-06-19 14:11:48 -0700135 REG_CP_IB1_BASE,
136 REG_CP_IB1_BUFSZ,
137 REG_CP_IB2_BASE,
Tarun Karra696f89e2013-01-27 21:31:40 -0800138 REG_CP_IB2_BUFSZ, /* LONG_IB_DETECT_REG_INDEX_END */
Jordan Crouseb5c80482012-10-03 09:38:41 -0600139 0,
Tarun Karra6e750d72013-01-04 10:28:40 -0800140 0,
141 0,
142 0,
143 0,
Jordan Crouseb5c80482012-10-03 09:38:41 -0600144 0
Tarun Karra3335f142012-06-19 14:11:48 -0700145};
146
Tarun Karra696f89e2013-01-27 21:31:40 -0800147const unsigned int ft_detect_regs_count = ARRAY_SIZE(ft_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700148
Jordan Crouse505df9c2011-07-28 08:37:59 -0600149/*
150 * This is the master list of all GPU cores that are supported by this
151 * driver.
152 */
153
154#define ANY_ID (~0)
Tarun Karra9c070822012-11-27 16:43:51 -0700155#define NO_VER (~0)
Jordan Crouse505df9c2011-07-28 08:37:59 -0600156
157static const struct {
158 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600159 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600160 const char *pm4fw;
161 const char *pfpfw;
162 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700163 unsigned int istore_size;
164 unsigned int pix_shader_start;
Tarun Karra9c070822012-11-27 16:43:51 -0700165 /* Size of an instruction in dwords */
166 unsigned int instruction_size;
167 /* size of gmem for gpu*/
168 unsigned int gmem_size;
169 /* version of pm4 microcode that supports sync_lock
170 between CPU and GPU for SMMU-v1 programming */
171 unsigned int sync_lock_pm4_ver;
172 /* version of pfp microcode that supports sync_lock
173 between CPU and GPU for SMMU-v1 programming */
174 unsigned int sync_lock_pfp_ver;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600175} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600176 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700177 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700178 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530179 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
180 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700181 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600182 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700183 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700184 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600185 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700186 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700187 512, 384, 3, SZ_512K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600188 /*
189 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
190 * a hardware problem.
191 */
192 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700193 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700194 1536, 768, 3, SZ_512K, NO_VER, NO_VER },
Carter Cooperf27ec722011-11-17 15:20:38 -0700195 { ADRENO_REV_A225, 2, 2, 0, 6,
196 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700197 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600198 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700199 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700200 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530201 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530202 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530203 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700204 512, 0, 2, SZ_256K, 0x3FF037, 0x3FF016 },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700205 /* A3XX doesn't use the pix_shader_start */
Carter Cooper95f7f792012-08-19 13:40:34 -0600206 { ADRENO_REV_A320, 3, 2, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700207 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700208 512, 0, 2, SZ_512K, 0x3FF037, 0x3FF016 },
liu zhongfd42e622012-05-01 19:18:30 -0700209 { ADRENO_REV_A330, 3, 3, 0, 0,
210 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700211 512, 0, 2, SZ_1M, NO_VER, NO_VER },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600212};
213
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600214static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215{
Jordan Crousea78c9172011-07-11 13:14:09 -0600216 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600217 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218
Jordan Crousea78c9172011-07-11 13:14:09 -0600219 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220
221 if (device->requested_state == KGSL_STATE_NONE) {
222 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700223 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 queue_work(device->work_queue, &device->idle_check_ws);
225 } else if (device->pwrscale.policy != NULL) {
226 queue_work(device->work_queue, &device->idle_check_ws);
227 }
228 }
229
230 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800231 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232 jiffies + device->pwrctrl.interval_timeout);
233 return result;
234}
235
Jordan Crouse9f739212011-07-28 08:37:57 -0600236static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237 struct kgsl_pagetable *pagetable)
238{
239 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
240 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
241
242 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
243
244 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
245
246 kgsl_mmu_unmap(pagetable, &device->memstore);
247
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600248 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249}
250
251static int adreno_setup_pt(struct kgsl_device *device,
252 struct kgsl_pagetable *pagetable)
253{
254 int result = 0;
255 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
256 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
257
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
259 GSL_PT_PAGE_RV);
260 if (result)
261 goto error;
262
263 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
264 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
265 if (result)
266 goto unmap_buffer_desc;
267
268 result = kgsl_mmu_map_global(pagetable, &device->memstore,
269 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
270 if (result)
271 goto unmap_memptrs_desc;
272
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600273 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
275 if (result)
276 goto unmap_memstore_desc;
277
278 return result;
279
280unmap_memstore_desc:
281 kgsl_mmu_unmap(pagetable, &device->memstore);
282
283unmap_memptrs_desc:
284 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
285
286unmap_buffer_desc:
287 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
288
289error:
290 return result;
291}
292
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600293static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600294 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600295 uint32_t flags)
296{
297 unsigned int pt_val, reg_pt_val;
Tarun Karra9c070822012-11-27 16:43:51 -0700298 unsigned int link[250];
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600299 unsigned int *cmds = &link[0];
300 int sizedwords = 0;
301 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600302 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600303 struct kgsl_context *context;
304 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600305
306 if (!adreno_dev->drawctxt_active)
307 return kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700308 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600309
310 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700311 if (context == NULL)
312 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600313 adreno_ctx = context->devctxt;
314
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600315 if (kgsl_mmu_enable_clk(&device->mmu,
316 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700317 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600318
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600319 cmds += __adreno_add_idle_indirect_cmds(cmds,
320 device->mmu.setstate_memory.gpuaddr +
321 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
322
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600323 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600324 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
325 device->mmu.setstate_memory.gpuaddr +
326 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
327 else
328 cmds += adreno_add_bank_change_cmds(cmds,
329 KGSL_IOMMU_CONTEXT_USER,
330 device->mmu.setstate_memory.gpuaddr +
331 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
332
Tarun Karra9c070822012-11-27 16:43:51 -0700333 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
334
335 /* Acquire GPU-CPU sync Lock here */
336 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
337
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700338 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
339 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600340 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600341 /*
342 * We need to perfrom the following operations for all
343 * IOMMU units
344 */
345 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700346 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
347 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600348 /*
349 * Set address of the new pagetable by writng to IOMMU
350 * TTBR0 register
351 */
352 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700353 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
354 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600355 *cmds++ = reg_pt_val;
356 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
357 *cmds++ = 0x00000000;
358
359 /*
360 * Read back the ttbr0 register as a barrier to ensure
361 * above writes have completed
362 */
363 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700364 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
365 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600366 reg_pt_val,
367 device->mmu.setstate_memory.gpuaddr +
368 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600369 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600370 }
371 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
372 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700373 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600374 */
375 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700376 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
377 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700378
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600379 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700380 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
381 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700382 KGSL_IOMMU_CTX_TLBIALL);
383 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600384
385 cmds += __adreno_add_idle_indirect_cmds(cmds,
386 device->mmu.setstate_memory.gpuaddr +
387 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
388
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600389 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700390 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
391 KGSL_IOMMU_CONTEXT_USER,
392 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700393 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600394 device->mmu.setstate_memory.gpuaddr +
395 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
396 }
397 }
398
Tarun Karra9c070822012-11-27 16:43:51 -0700399 /* Release GPU-CPU sync Lock here */
400 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
401
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600402 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600403 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700404 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
405 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600406 device->mmu.setstate_memory.gpuaddr +
407 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
408 else
409 cmds += adreno_add_bank_change_cmds(cmds,
410 KGSL_IOMMU_CONTEXT_PRIV,
411 device->mmu.setstate_memory.gpuaddr +
412 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
413
Tarun Karra9c070822012-11-27 16:43:51 -0700414 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
415
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600416 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600417 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700418 /* invalidate all base pointers */
419 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
420 *cmds++ = 0x7fff;
421 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600422 /* This returns the per context timestamp but we need to
423 * use the global timestamp for iommu clock disablement */
424 adreno_ringbuffer_issuecmds(device, adreno_ctx,
425 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600426 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600427 kgsl_mmu_disable_clk_on_ts(&device->mmu,
428 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600429 }
Tarun Karra9c070822012-11-27 16:43:51 -0700430
431 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
432 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
433 BUG();
434 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600435}
436
437static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600438 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600439 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440{
441 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
442 unsigned int link[32];
443 unsigned int *cmds = &link[0];
444 int sizedwords = 0;
445 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600446 struct kgsl_context *context;
447 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600449 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530450 * Fix target freeze issue by adding TLB flush for each submit
451 * on A20X based targets.
452 */
453 if (adreno_is_a20x(adreno_dev))
454 flags |= KGSL_MMUFLAGS_TLBFLUSH;
455 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600456 * If possible, then set the state via the command stream to avoid
457 * a CPU idle. Otherwise, use the default setstate which uses register
458 * writes For CFF dump we must idle and use the registers so that it is
459 * easier to filter out the mmu accesses from the dump
460 */
461 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600462 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700463 if (context == NULL)
464 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600465 adreno_ctx = context->devctxt;
466
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
468 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600469 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470 *cmds++ = 0x00000000;
471
472 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600473 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700474 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600475 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476 sizedwords += 4;
477 }
478
479 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
480 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600481 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 1);
483 *cmds++ = 0x00000000;
484 sizedwords += 2;
485 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600486 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 *cmds++ = mh_mmu_invalidate;
488 sizedwords += 2;
489 }
490
491 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600492 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 /* HW workaround: to resolve MMU page fault interrupts
494 * caused by the VGT.It prevents the CP PFP from filling
495 * the VGT DMA request fifo too early,thereby ensuring
496 * that the VGT will not fetch vertex/bin data until
497 * after the page table base register has been updated.
498 *
499 * Two null DRAW_INDX_BIN packets are inserted right
500 * after the page table base update, followed by a
501 * wait for idle. The null packets will fill up the
502 * VGT DMA request fifo and prevent any further
503 * vertex/bin updates from occurring until the wait
504 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600505 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 *cmds++ = (0x4 << 16) |
507 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
508 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600509 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600510 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600511 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512 *cmds++ = 0; /* viz query info */
513 *cmds++ = 0x0003C004; /* draw indicator */
514 *cmds++ = 0; /* bin base */
515 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600516 *cmds++ =
517 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600519 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 *cmds++ = 0; /* viz query info */
521 *cmds++ = 0x0003C004; /* draw indicator */
522 *cmds++ = 0; /* bin base */
523 *cmds++ = 3; /* bin size */
524 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600525 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600527 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 *cmds++ = 0x00000000;
529 sizedwords += 21;
530 }
531
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600532
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600534 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 *cmds++ = 0x7fff; /* invalidate all base pointers */
536 sizedwords += 2;
537 }
538
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600539 adreno_ringbuffer_issuecmds(device, adreno_ctx,
540 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600542 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600543 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600544 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545}
546
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600547static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600548 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600549 uint32_t flags)
550{
551 /* call the mmu specific handler */
552 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600553 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600554 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600555 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600556}
557
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700559a3xx_getchipid(struct kgsl_device *device)
560{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600561 struct kgsl_device_platform_data *pdata =
562 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700563
Jordan Crouse54154c62012-03-27 16:33:26 -0600564 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600565 * All current A3XX chipids are detected at the SOC level. Leave this
566 * function here to support any future GPUs that have working
567 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600568 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700569
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600570 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700571}
572
573static unsigned int
574a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575{
576 unsigned int chipid = 0;
577 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600578 struct kgsl_device_platform_data *pdata =
579 kgsl_device_get_drvdata(device);
580
581 /* If the chip id is set at the platform level, then just use that */
582
583 if (pdata->chipid != 0)
584 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585
586 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
587 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
588 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
589
590 /*
591 * adreno 22x gpus are indicated by coreid 2,
592 * but REG_RBBM_PERIPHID1 always contains 0 for this field
593 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600594 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 chipid = 2 << 24;
596 else
597 chipid = (coreid & 0xF) << 24;
598
599 chipid |= ((majorid >> 4) & 0xF) << 16;
600
601 minorid = ((revid >> 0) & 0xFF);
602
603 patchid = ((revid >> 16) & 0xFF);
604
605 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530606 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 if (cpu_is_qsd8x50())
608 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530609 else if (cpu_is_msm8625() && minorid == 0)
610 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611
612 chipid |= (minorid << 8) | patchid;
613
614 return chipid;
615}
616
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700617static unsigned int
618adreno_getchipid(struct kgsl_device *device)
619{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600620 struct kgsl_device_platform_data *pdata =
621 kgsl_device_get_drvdata(device);
622
623 /*
624 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
625 * an A2XX processor
626 */
627
628 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700629 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600630 else
631 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700632}
633
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634static inline bool _rev_match(unsigned int id, unsigned int entry)
635{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600636 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638
639static void
640adreno_identify_gpu(struct adreno_device *adreno_dev)
641{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600642 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643
644 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
645
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600646 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
647 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
648 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
649 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650
Jordan Crouse505df9c2011-07-28 08:37:59 -0600651 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
652 if (core == adreno_gpulist[i].core &&
653 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600654 _rev_match(minor, adreno_gpulist[i].minor) &&
655 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 }
658
Jordan Crouse505df9c2011-07-28 08:37:59 -0600659 if (i == ARRAY_SIZE(adreno_gpulist)) {
660 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
661 return;
662 }
663
664 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
665 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
666 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
667 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700668 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
669 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700670 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600671 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Tarun Karra9c070822012-11-27 16:43:51 -0700672 adreno_dev->gpulist_index = i;
673
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674}
675
Lokesh Batra805e1e12012-08-03 08:34:06 -0600676static struct platform_device_id adreno_id_table[] = {
677 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
678 {},
679};
680
681MODULE_DEVICE_TABLE(platform, adreno_id_table);
682
683static struct of_device_id adreno_match_table[] = {
684 { .compatible = "qcom,kgsl-3d0", },
685 {}
686};
687
688static inline int adreno_of_read_property(struct device_node *node,
689 const char *prop, unsigned int *ptr)
690{
691 int ret = of_property_read_u32(node, prop, ptr);
692 if (ret)
693 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
694 return ret;
695}
696
697static struct device_node *adreno_of_find_subnode(struct device_node *parent,
698 const char *name)
699{
700 struct device_node *child;
701
702 for_each_child_of_node(parent, child) {
703 if (of_device_is_compatible(child, name))
704 return child;
705 }
706
707 return NULL;
708}
709
710static int adreno_of_get_pwrlevels(struct device_node *parent,
711 struct kgsl_device_platform_data *pdata)
712{
713 struct device_node *node, *child;
714 int ret = -EINVAL;
715
716 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
717
718 if (node == NULL) {
719 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
720 return -EINVAL;
721 }
722
723 pdata->num_levels = 0;
724
725 for_each_child_of_node(node, child) {
726 unsigned int index;
727 struct kgsl_pwrlevel *level;
728
729 if (adreno_of_read_property(child, "reg", &index))
730 goto done;
731
732 if (index >= KGSL_MAX_PWRLEVELS) {
733 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
734 index);
735 continue;
736 }
737
738 if (index >= pdata->num_levels)
739 pdata->num_levels = index + 1;
740
741 level = &pdata->pwrlevel[index];
742
743 if (adreno_of_read_property(child, "qcom,gpu-freq",
744 &level->gpu_freq))
745 goto done;
746
747 if (adreno_of_read_property(child, "qcom,bus-freq",
748 &level->bus_freq))
749 goto done;
750
751 if (adreno_of_read_property(child, "qcom,io-fraction",
752 &level->io_fraction))
753 level->io_fraction = 0;
754 }
755
756 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
757 &pdata->init_level))
758 pdata->init_level = 1;
759
760 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
761 KGSL_CORE_ERR("Initial power level out of range\n");
762 pdata->init_level = 1;
763 }
764
765 ret = 0;
766done:
767 return ret;
768
769}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600770
771static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
772{
773 struct device_node *node, *child;
774 struct msm_dcvs_core_info *info = NULL;
775 int count = 0;
776 int ret = -EINVAL;
777
778 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
779 if (node == NULL)
780 return ERR_PTR(-EINVAL);
781
782 info = kzalloc(sizeof(*info), GFP_KERNEL);
783
784 if (info == NULL) {
785 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
786 ret = -ENOMEM;
787 goto err;
788 }
789
790 for_each_child_of_node(node, child)
791 count++;
792
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700793 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600794
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700795 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600796 sizeof(struct msm_dcvs_freq_entry),
797 GFP_KERNEL);
798
799 if (info->freq_tbl == NULL) {
800 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700801 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600802 sizeof(struct msm_dcvs_freq_entry));
803 ret = -ENOMEM;
804 goto err;
805 }
806
807 for_each_child_of_node(node, child) {
808 unsigned int index;
809
810 if (adreno_of_read_property(child, "reg", &index))
811 goto err;
812
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700813 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600814 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
815 index);
816 continue;
817 }
818
819 if (adreno_of_read_property(child, "qcom,freq",
820 &info->freq_tbl[index].freq))
821 goto err;
822
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700823 if (adreno_of_read_property(child, "qcom,voltage",
824 &info->freq_tbl[index].voltage))
825 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600826
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700827 if (adreno_of_read_property(child, "qcom,is_trans_level",
828 &info->freq_tbl[index].is_trans_level))
829 info->freq_tbl[index].is_trans_level = 0;
830
831 if (adreno_of_read_property(child, "qcom,active-energy-offset",
832 &info->freq_tbl[index].active_energy_offset))
833 info->freq_tbl[index].active_energy_offset = 0;
834
835 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
836 &info->freq_tbl[index].leakage_energy_offset))
837 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600838 }
839
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700840 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
841 goto err;
842
843 info->sensors = kzalloc(info->num_cores *
844 sizeof(int),
845 GFP_KERNEL);
846
847 for (count = 0; count < info->num_cores; count++) {
848 if (adreno_of_read_property(node, "qcom,sensors",
849 &(info->sensors[count])))
850 goto err;
851 }
852
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700853 if (adreno_of_read_property(node, "qcom,core-core-type",
854 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600855 goto err;
856
857 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
858 &info->algo_param.disable_pc_threshold))
859 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700860 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
861 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600862 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700863 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
864 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600865 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600866 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
867 &info->algo_param.em_max_util_pct))
868 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700869 if (adreno_of_read_property(node, "qcom,algo-group-id",
870 &info->algo_param.group_id))
871 goto err;
872 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
873 &info->algo_param.max_freq_chg_time_us))
874 goto err;
875 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
876 &info->algo_param.slack_mode_dynamic))
877 goto err;
878 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
879 &info->algo_param.slack_weight_thresh_pct))
880 goto err;
881 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
882 &info->algo_param.slack_time_min_us))
883 goto err;
884 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
885 &info->algo_param.slack_time_max_us))
886 goto err;
887 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
888 &info->algo_param.ss_win_size_min_us))
889 goto err;
890 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
891 &info->algo_param.ss_win_size_max_us))
892 goto err;
893 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
894 &info->algo_param.ss_util_pct))
895 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800896 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
897 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600898 goto err;
899
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700900 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
901 &info->energy_coeffs.active_coeff_a))
902 goto err;
903 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
904 &info->energy_coeffs.active_coeff_b))
905 goto err;
906 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
907 &info->energy_coeffs.active_coeff_c))
908 goto err;
909 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
910 &info->energy_coeffs.leakage_coeff_a))
911 goto err;
912 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
913 &info->energy_coeffs.leakage_coeff_b))
914 goto err;
915 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
916 &info->energy_coeffs.leakage_coeff_c))
917 goto err;
918 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
919 &info->energy_coeffs.leakage_coeff_d))
920 goto err;
921
922 if (adreno_of_read_property(node, "qcom,power-current-temp",
923 &info->power_param.current_temp))
924 goto err;
925
Lokesh Batra805e1e12012-08-03 08:34:06 -0600926 return info;
927
928err:
929 if (info)
930 kfree(info->freq_tbl);
931
932 kfree(info);
933
934 return ERR_PTR(ret);
935}
936
937static int adreno_of_get_iommu(struct device_node *parent,
938 struct kgsl_device_platform_data *pdata)
939{
940 struct device_node *node, *child;
941 struct kgsl_device_iommu_data *data = NULL;
942 struct kgsl_iommu_ctx *ctxs = NULL;
943 u32 reg_val[2];
944 int ctx_index = 0;
945
946 node = of_parse_phandle(parent, "iommu", 0);
947 if (node == NULL)
948 return -EINVAL;
949
950 data = kzalloc(sizeof(*data), GFP_KERNEL);
951 if (data == NULL) {
952 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
953 goto err;
954 }
955
956 if (of_property_read_u32_array(node, "reg", reg_val, 2))
957 goto err;
958
959 data->physstart = reg_val[0];
960 data->physend = data->physstart + reg_val[1] - 1;
961
962 data->iommu_ctx_count = 0;
963
964 for_each_child_of_node(node, child)
965 data->iommu_ctx_count++;
966
967 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
968 GFP_KERNEL);
969
970 if (ctxs == NULL) {
971 KGSL_CORE_ERR("kzalloc(%d) failed\n",
972 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
973 goto err;
974 }
975
976 for_each_child_of_node(node, child) {
977 int ret = of_property_read_string(child, "label",
978 &ctxs[ctx_index].iommu_ctx_name);
979
980 if (ret) {
981 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
982 goto err;
983 }
984
985 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
986 &ctxs[ctx_index].ctx_id))
987 goto err;
988
989 ctx_index++;
990 }
991
992 data->iommu_ctxs = ctxs;
993
994 pdata->iommu_data = data;
995 pdata->iommu_count = 1;
996
997 return 0;
998
999err:
1000 kfree(ctxs);
1001 kfree(data);
1002
1003 return -EINVAL;
1004}
1005
1006static int adreno_of_get_pdata(struct platform_device *pdev)
1007{
1008 struct kgsl_device_platform_data *pdata = NULL;
1009 struct kgsl_device *device;
1010 int ret = -EINVAL;
1011
1012 pdev->id_entry = adreno_id_table;
1013
1014 pdata = pdev->dev.platform_data;
1015 if (pdata)
1016 return 0;
1017
1018 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1019 KGSL_CORE_ERR("Unable to read 'label'\n");
1020 goto err;
1021 }
1022
1023 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1024 goto err;
1025
1026 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1027 if (pdata == NULL) {
1028 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1029 ret = -ENOMEM;
1030 goto err;
1031 }
1032
1033 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1034 &pdata->chipid))
1035 goto err;
1036
1037 /* pwrlevel Data */
1038 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1039 if (ret)
1040 goto err;
1041
1042 /* Default value is 83, if not found in DT */
1043 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1044 &pdata->idle_timeout))
1045 pdata->idle_timeout = 83;
1046
1047 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1048 &pdata->nap_allowed))
1049 pdata->nap_allowed = 1;
1050
1051 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1052 &pdata->clk_map))
1053 goto err;
1054
1055 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1056
1057 if (device->id != KGSL_DEVICE_3D0)
1058 goto err;
1059
1060 /* Bus Scale Data */
1061
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001062 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001063 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1064 ret = PTR_ERR(pdata->bus_scale_table);
1065 goto err;
1066 }
1067
1068 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1069 if (IS_ERR_OR_NULL(pdata->core_info)) {
1070 ret = PTR_ERR(pdata->core_info);
1071 goto err;
1072 }
1073
1074 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1075 if (ret)
1076 goto err;
1077
1078 pdev->dev.platform_data = pdata;
1079 return 0;
1080
1081err:
1082 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001083 if (pdata->core_info)
1084 kfree(pdata->core_info->freq_tbl);
1085 kfree(pdata->core_info);
1086
1087 if (pdata->iommu_data)
1088 kfree(pdata->iommu_data->iommu_ctxs);
1089
1090 kfree(pdata->iommu_data);
1091 }
1092
1093 kfree(pdata);
1094
1095 return ret;
1096}
1097
liu zhong7dfa2a32012-04-27 19:11:01 -07001098#ifdef CONFIG_MSM_OCMEM
1099static int
1100adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1101{
Jordan Crousec0978202012-08-29 14:35:51 -06001102 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001103 return 0;
1104
1105 /* OCMEM is only needed once, do not support consective allocation */
1106 if (adreno_dev->ocmem_hdl != NULL)
1107 return 0;
1108
1109 adreno_dev->ocmem_hdl =
1110 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1111 if (adreno_dev->ocmem_hdl == NULL)
1112 return -ENOMEM;
1113
1114 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001115 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001116
1117 return 0;
1118}
1119
1120static void
1121adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1122{
Jordan Crousec0978202012-08-29 14:35:51 -06001123 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001124 return;
1125
1126 if (adreno_dev->ocmem_hdl == NULL)
1127 return;
1128
1129 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1130 adreno_dev->ocmem_hdl = NULL;
1131}
1132#else
1133static int
1134adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1135{
1136 return 0;
1137}
1138
1139static void
1140adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1141{
1142}
1143#endif
1144
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001145static int __devinit
1146adreno_probe(struct platform_device *pdev)
1147{
1148 struct kgsl_device *device;
1149 struct adreno_device *adreno_dev;
1150 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001151 bool is_dt;
1152
1153 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1154
1155 if (is_dt && pdev->dev.of_node) {
1156 status = adreno_of_get_pdata(pdev);
1157 if (status)
1158 goto error_return;
1159 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001160
1161 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1162 adreno_dev = ADRENO_DEVICE(device);
1163 device->parentdev = &pdev->dev;
1164
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001165 status = adreno_ringbuffer_init(device);
1166 if (status != 0)
1167 goto error;
1168
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001169 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001170 if (status)
1171 goto error_close_rb;
1172
1173 adreno_debugfs_init(device);
1174
1175 kgsl_pwrscale_init(device);
1176 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1177
1178 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1179 return 0;
1180
1181error_close_rb:
1182 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1183error:
1184 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001185error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 return status;
1187}
1188
1189static int __devexit adreno_remove(struct platform_device *pdev)
1190{
1191 struct kgsl_device *device;
1192 struct adreno_device *adreno_dev;
1193
1194 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1195 adreno_dev = ADRENO_DEVICE(device);
1196
1197 kgsl_pwrscale_detach_policy(device);
1198 kgsl_pwrscale_close(device);
1199
1200 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1201 kgsl_device_platform_remove(device);
1202
1203 return 0;
1204}
1205
1206static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1207{
1208 int status = -EINVAL;
1209 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210
Tarun Karrad20d71a2013-01-25 15:38:57 -08001211 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001212 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213
1214 /* Power up the device */
1215 kgsl_pwrctrl_enable(device);
1216
1217 /* Identify the specific GPU */
1218 adreno_identify_gpu(adreno_dev);
1219
Tarun Karra9c070822012-11-27 16:43:51 -07001220 if (adreno_ringbuffer_read_pm4_ucode(device)) {
1221 KGSL_DRV_ERR(device, "Reading pm4 microcode failed %s\n",
1222 adreno_dev->pm4_fwfile);
1223 BUG_ON(1);
1224 }
1225
1226 if (adreno_ringbuffer_read_pfp_ucode(device)) {
1227 KGSL_DRV_ERR(device, "Reading pfp microcode failed %s\n",
1228 adreno_dev->pfp_fwfile);
1229 BUG_ON(1);
1230 }
1231
Jordan Crouse505df9c2011-07-28 08:37:59 -06001232 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1233 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1234 adreno_dev->chip_id);
1235 goto error_clk_off;
1236 }
1237
Tarun Karra9c070822012-11-27 16:43:51 -07001238
1239 /*
1240 * Check if firmware supports the sync lock PM4 packets needed
1241 * for IOMMUv1
1242 */
1243
1244 if ((adreno_dev->pm4_fw_version >=
1245 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pm4_ver) &&
1246 (adreno_dev->pfp_fw_version >=
1247 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pfp_ver))
1248 device->mmu.flags |= KGSL_MMU_FLAGS_IOMMU_SYNC;
1249
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001250 /* Set up the MMU */
1251 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001252 /*
1253 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1254 * on older gpus
1255 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001256 if (adreno_is_a20x(adreno_dev)) {
1257 device->mh.mh_intf_cfg1 = 0;
1258 device->mh.mh_intf_cfg2 = 0;
1259 }
1260
1261 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001262 }
1263
Tarun Karra3335f142012-06-19 14:11:48 -07001264 /* Assign correct RBBM status register to hang detect regs
1265 */
Tarun Karra696f89e2013-01-27 21:31:40 -08001266 ft_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
Tarun Karra3335f142012-06-19 14:11:48 -07001267
Jordan Crouseb5c80482012-10-03 09:38:41 -06001268 /* Add A3XX specific registers for hang detection */
1269 if (adreno_is_a3xx(adreno_dev)) {
Tarun Karra696f89e2013-01-27 21:31:40 -08001270 ft_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1271 ft_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
1272 ft_detect_regs[8] = A3XX_RBBM_PERFCTR_SP_6_LO;
1273 ft_detect_regs[9] = A3XX_RBBM_PERFCTR_SP_6_HI;
1274 ft_detect_regs[10] = A3XX_RBBM_PERFCTR_SP_5_LO;
1275 ft_detect_regs[11] = A3XX_RBBM_PERFCTR_SP_5_HI;
Jordan Crouseb5c80482012-10-03 09:38:41 -06001276 }
1277
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001278 status = kgsl_mmu_start(device);
1279 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001280 goto error_clk_off;
1281
liu zhong7dfa2a32012-04-27 19:11:01 -07001282 status = adreno_ocmem_gmem_malloc(adreno_dev);
1283 if (status) {
1284 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1285 goto error_mmu_off;
1286 }
1287
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001288 /* Start the GPU */
1289 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290
1291 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001292 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293
1294 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001295 if (status == 0) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001296 /* While fault tolerance is on we do not want timer to
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001297 * fire and attempt to change any device state */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001298 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001299 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001300 return 0;
1301 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001303 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001304
1305error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001306 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001307
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001308error_clk_off:
1309 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310
1311 return status;
1312}
1313
1314static int adreno_stop(struct kgsl_device *device)
1315{
1316 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1317
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318 adreno_dev->drawctxt_active = NULL;
1319
1320 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1321
Shubhraprakash Das79447952012-04-26 18:12:23 -06001322 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001323
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001324 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301325 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001326 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001327
liu zhong7dfa2a32012-04-27 19:11:01 -07001328 adreno_ocmem_gmem_free(adreno_dev);
1329
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330 /* Power down the device */
1331 kgsl_pwrctrl_disable(device);
1332
1333 return 0;
1334}
1335
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001336static void adreno_mark_context_status(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001337 int ft_status)
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001338{
1339 struct kgsl_context *context;
1340 int next = 0;
1341 /*
1342 * Set the reset status of all contexts to
1343 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
Tarun Karrad20d71a2013-01-25 15:38:57 -08001344 * since thats the guilty party, if fault tolerance failed then
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001345 * mark all as guilty
1346 */
1347 while ((context = idr_get_next(&device->context_idr, &next))) {
1348 struct adreno_context *adreno_context = context->devctxt;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001349 if (ft_status) {
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001350 context->reset_status =
1351 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1352 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1353 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1354 context->reset_status) {
Carter Cooper8179f5a2012-12-17 11:32:27 -07001355 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG |
Tarun Karrad20d71a2013-01-25 15:38:57 -08001356 CTXT_FLAGS_GPU_HANG_FT))
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001357 context->reset_status =
1358 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1359 else
1360 context->reset_status =
1361 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1362 }
1363 next = next + 1;
1364 }
1365}
1366
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001367static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1368{
1369 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1370 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1371 struct kgsl_context *context;
1372 struct adreno_context *temp_adreno_context;
1373 int next = 0;
1374
1375 while ((context = idr_get_next(&device->context_idr, &next))) {
1376 temp_adreno_context = context->devctxt;
1377 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1378 kgsl_sharedmem_writel(&device->memstore,
1379 KGSL_MEMSTORE_OFFSET(context->id,
1380 soptimestamp),
1381 rb->timestamp[context->id]);
1382 kgsl_sharedmem_writel(&device->memstore,
1383 KGSL_MEMSTORE_OFFSET(context->id,
1384 eoptimestamp),
1385 rb->timestamp[context->id]);
1386 }
1387 next = next + 1;
1388 }
1389}
1390
Tarun Karrad20d71a2013-01-25 15:38:57 -08001391static void adreno_destroy_ft_data(struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001392{
Tarun Karrad20d71a2013-01-25 15:38:57 -08001393 vfree(ft_data->rb_buffer);
1394 vfree(ft_data->bad_rb_buffer);
1395 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001396}
1397
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001398static int _find_start_of_cmd_seq(struct adreno_ringbuffer *rb,
1399 unsigned int *ptr,
1400 bool inc)
1401{
1402 int status = -EINVAL;
1403 unsigned int val1;
1404 unsigned int size = rb->buffer_desc.size;
1405 unsigned int start_ptr = *ptr;
1406
1407 while ((start_ptr / sizeof(unsigned int)) != rb->wptr) {
1408 if (inc)
1409 start_ptr = adreno_ringbuffer_inc_wrapped(start_ptr,
1410 size);
1411 else
1412 start_ptr = adreno_ringbuffer_dec_wrapped(start_ptr,
1413 size);
1414 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, start_ptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001415 /* Ensure above read is finished before next read */
1416 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001417 if (KGSL_CMD_IDENTIFIER == val1) {
1418 if ((start_ptr / sizeof(unsigned int)) != rb->wptr)
1419 start_ptr = adreno_ringbuffer_dec_wrapped(
1420 start_ptr, size);
1421 *ptr = start_ptr;
1422 status = 0;
1423 break;
1424 }
1425 }
1426 return status;
1427}
1428
1429static int _find_cmd_seq_after_eop_ts(struct adreno_ringbuffer *rb,
1430 unsigned int *rb_rptr,
1431 unsigned int global_eop,
1432 bool inc)
1433{
1434 int status = -EINVAL;
1435 unsigned int temp_rb_rptr = *rb_rptr;
1436 unsigned int size = rb->buffer_desc.size;
1437 unsigned int val[3];
1438 int i = 0;
1439 bool check = false;
1440
1441 if (inc && temp_rb_rptr / sizeof(unsigned int) != rb->wptr)
1442 return status;
1443
1444 do {
1445 /*
1446 * when decrementing we need to decrement first and
1447 * then read make sure we cover all the data
1448 */
1449 if (!inc)
1450 temp_rb_rptr = adreno_ringbuffer_dec_wrapped(
1451 temp_rb_rptr, size);
1452 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i],
1453 temp_rb_rptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001454 /* Ensure above read is finished before next read */
1455 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001456
1457 if (check && ((inc && val[i] == global_eop) ||
1458 (!inc && (val[i] ==
1459 cp_type3_packet(CP_MEM_WRITE, 2) ||
1460 val[i] == CACHE_FLUSH_TS)))) {
1461 /* decrement i, i.e i = (i - 1 + 3) % 3 if
1462 * we are going forward, else increment i */
1463 i = (i + 2) % 3;
1464 if (val[i] == rb->device->memstore.gpuaddr +
1465 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1466 eoptimestamp)) {
1467 int j = ((i + 2) % 3);
1468 if ((inc && (val[j] == CACHE_FLUSH_TS ||
1469 val[j] == cp_type3_packet(
1470 CP_MEM_WRITE, 2))) ||
1471 (!inc && val[j] == global_eop)) {
1472 /* Found the global eop */
1473 status = 0;
1474 break;
1475 }
1476 }
1477 /* if no match found then increment i again
1478 * since we decremented before matching */
1479 i = (i + 1) % 3;
1480 }
1481 if (inc)
1482 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(
1483 temp_rb_rptr, size);
1484
1485 i = (i + 1) % 3;
1486 if (2 == i)
1487 check = true;
1488 } while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr);
1489 /* temp_rb_rptr points to the command stream after global eop,
1490 * move backward till the start of command sequence */
1491 if (!status) {
1492 status = _find_start_of_cmd_seq(rb, &temp_rb_rptr, false);
1493 if (!status) {
1494 *rb_rptr = temp_rb_rptr;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001495 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001496 "Offset of cmd sequence after eop timestamp: 0x%x\n",
1497 temp_rb_rptr / sizeof(unsigned int));
1498 }
1499 }
1500 if (status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001501 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001502 "Failed to find the command sequence after eop timestamp\n");
1503 return status;
1504}
1505
1506static int _find_hanging_ib_sequence(struct adreno_ringbuffer *rb,
1507 unsigned int *rb_rptr,
1508 unsigned int ib1)
1509{
1510 int status = -EINVAL;
1511 unsigned int temp_rb_rptr = *rb_rptr;
1512 unsigned int size = rb->buffer_desc.size;
1513 unsigned int val[2];
1514 int i = 0;
1515 bool check = false;
1516 bool ctx_switch = false;
1517
1518 while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr) {
1519 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i], temp_rb_rptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001520 /* Ensure above read is finished before next read */
1521 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001522
1523 if (check && val[i] == ib1) {
1524 /* decrement i, i.e i = (i - 1 + 2) % 2 */
1525 i = (i + 1) % 2;
1526 if (adreno_cmd_is_ib(val[i])) {
1527 /* go till start of command sequence */
1528 status = _find_start_of_cmd_seq(rb,
1529 &temp_rb_rptr, false);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001530
1531 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001532 "Found the hanging IB at offset 0x%x\n",
1533 temp_rb_rptr / sizeof(unsigned int));
1534 break;
1535 }
1536 /* if no match the increment i since we decremented
1537 * before checking */
1538 i = (i + 1) % 2;
1539 }
1540 /* Make sure you do not encounter a context switch twice, we can
1541 * encounter it once for the bad context as the start of search
1542 * can point to the context switch */
1543 if (val[i] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
1544 if (ctx_switch) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001545 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001546 "Context switch encountered before bad "
1547 "IB found\n");
1548 break;
1549 }
1550 ctx_switch = true;
1551 }
1552 i = (i + 1) % 2;
1553 if (1 == i)
1554 check = true;
1555 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(temp_rb_rptr,
1556 size);
1557 }
1558 if (!status)
1559 *rb_rptr = temp_rb_rptr;
1560 return status;
1561}
1562
Tarun Karra59e79542013-04-10 10:25:25 -07001563static void adreno_setup_ft_data(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001564 struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001565{
1566 int ret = 0;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001567 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1568 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001569 struct kgsl_context *context;
1570 struct adreno_context *adreno_context;
1571 unsigned int rb_rptr = rb->wptr * sizeof(unsigned int);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001572
Tarun Karrad20d71a2013-01-25 15:38:57 -08001573 memset(ft_data, 0, sizeof(*ft_data));
1574 ft_data->start_of_replay_cmds = 0xFFFFFFFF;
1575 ft_data->replay_for_snapshot = 0xFFFFFFFF;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001576
Tarun Karrad20d71a2013-01-25 15:38:57 -08001577 adreno_regread(device, REG_CP_IB1_BASE, &ft_data->ib1);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001578
Tarun Karrad20d71a2013-01-25 15:38:57 -08001579 kgsl_sharedmem_readl(&device->memstore, &ft_data->context_id,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001580 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1581 current_context));
1582
1583 kgsl_sharedmem_readl(&device->memstore,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001584 &ft_data->global_eop,
Tarun Karradeeecc02013-01-21 23:42:17 -08001585 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1586 eoptimestamp));
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001587
Tarun Karra782734f2013-03-27 20:21:02 -07001588 /* Ensure context id and global eop ts read complete */
1589 rmb();
1590
Tarun Karrad20d71a2013-01-25 15:38:57 -08001591 ft_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1592 if (!ft_data->rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001593 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1594 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001595 return;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001596 }
1597
Tarun Karrad20d71a2013-01-25 15:38:57 -08001598 ft_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1599 if (!ft_data->bad_rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001600 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1601 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001602 return;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001603 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001604
Tarun Karrad20d71a2013-01-25 15:38:57 -08001605 ft_data->good_rb_buffer = vmalloc(rb->buffer_desc.size);
1606 if (!ft_data->good_rb_buffer) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001607 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1608 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001609 return;
Tarun Karradeeecc02013-01-21 23:42:17 -08001610 }
Tarun Karra3164fb02013-02-05 15:38:51 -08001611
Tarun Karra59e79542013-04-10 10:25:25 -07001612 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001613
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001614 /* find the start of bad command sequence in rb */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001615 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001616 /* Look for the command stream that is right after the global eop */
1617
1618 if (!context) {
1619 /*
Tarun Karrad20d71a2013-01-25 15:38:57 -08001620 * If there is no context then fault tolerance does not need to
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001621 * replay anything, just reset GPU and thats it
1622 */
Tarun Karra59e79542013-04-10 10:25:25 -07001623 return;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001624 }
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001625
Tarun Karra59e79542013-04-10 10:25:25 -07001626 ft_data->ft_policy = adreno_dev->ft_policy;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001627
Tarun Karra3164fb02013-02-05 15:38:51 -08001628 if (!adreno_dev->ft_policy)
1629 adreno_dev->ft_policy = KGSL_FT_DEFAULT_POLICY;
1630
Tarun Karra59e79542013-04-10 10:25:25 -07001631 ret = _find_cmd_seq_after_eop_ts(rb, &rb_rptr,
1632 ft_data->global_eop + 1, false);
1633 if (ret) {
1634 ft_data->ft_policy |= KGSL_FT_TEMP_DISABLE;
1635 return;
1636 } else
1637 ft_data->ft_policy &= ~KGSL_FT_TEMP_DISABLE;
Tarun Karra3164fb02013-02-05 15:38:51 -08001638
Tarun Karra59e79542013-04-10 10:25:25 -07001639 ft_data->start_of_replay_cmds = rb_rptr;
Tarun Karra3164fb02013-02-05 15:38:51 -08001640
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001641 adreno_context = context->devctxt;
1642 if (adreno_context->flags & CTXT_FLAGS_PREAMBLE) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001643 if (ft_data->ib1) {
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001644 ret = _find_hanging_ib_sequence(rb,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001645 &rb_rptr, ft_data->ib1);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001646 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001647 KGSL_FT_ERR(device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001648 "Start not found for replay IB sequence\n");
Tarun Karra59e79542013-04-10 10:25:25 -07001649 return;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001650 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001651 ft_data->start_of_replay_cmds = rb_rptr;
1652 ft_data->replay_for_snapshot = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001653 }
1654 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001655}
1656
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001657static int
Tarun Karra696f89e2013-01-27 21:31:40 -08001658_adreno_check_long_ib(struct kgsl_device *device)
1659{
1660 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1661 unsigned int curr_global_ts = 0;
1662
1663 /* check if the global ts is still the same */
1664 kgsl_sharedmem_readl(&device->memstore,
1665 &curr_global_ts,
1666 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1667 eoptimestamp));
Tarun Karra782734f2013-03-27 20:21:02 -07001668 /* Ensure above read is finished before long ib check */
1669 rmb();
Tarun Karra696f89e2013-01-27 21:31:40 -08001670
1671 /* Mark long ib as handled */
1672 adreno_dev->long_ib = 0;
1673
1674 if (curr_global_ts == adreno_dev->long_ib_ts) {
1675 KGSL_FT_ERR(device,
1676 "IB ran too long, invalidate ctxt\n");
1677 return 1;
1678 } else {
1679 /* Do nothing GPU has gone ahead */
1680 KGSL_FT_INFO(device, "false long ib detection return\n");
1681 return 0;
1682 }
1683}
1684
1685static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001686_adreno_ft_restart_device(struct kgsl_device *device,
Tarun Karra59e79542013-04-10 10:25:25 -07001687 struct kgsl_context *context)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001688{
Tarun Karradeeecc02013-01-21 23:42:17 -08001689
1690 struct adreno_context *adreno_context = context->devctxt;
1691
1692 /* restart device */
1693 if (adreno_stop(device)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001694 KGSL_FT_ERR(device, "Device stop failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001695 return 1;
1696 }
1697
1698 if (adreno_start(device, true)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001699 KGSL_FT_ERR(device, "Device start failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001700 return 1;
1701 }
1702
1703 if (context)
1704 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1705 KGSL_MEMSTORE_GLOBAL);
1706
1707 /* If iommu is used then we need to make sure that the iommu clocks
1708 * are on since there could be commands in pipeline that touch iommu */
1709 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1710 if (kgsl_mmu_enable_clk(&device->mmu,
1711 KGSL_IOMMU_CONTEXT_USER))
1712 return 1;
1713 }
1714
1715 return 0;
1716}
1717
Tarun Karra696f89e2013-01-27 21:31:40 -08001718static inline void
1719_adreno_debug_ft_info(struct kgsl_device *device,
1720 struct adreno_ft_data *ft_data)
1721{
1722
1723 /*
1724 * Dumping rb is a very useful tool to debug FT.
1725 * It will tell us if we are extracting the rb correctly
1726 * NOP'ing the right IB, skipping the EOF correctly etc.
1727 */
1728 if (device->ft_log >= 7) {
1729
1730 /* Print fault tolerance data here */
1731 KGSL_FT_INFO(device, "Temp RB buffer size 0x%X\n",
1732 ft_data->rb_size);
1733 adreno_dump_rb(device, ft_data->rb_buffer,
1734 ft_data->rb_size<<2, 0, ft_data->rb_size);
1735
1736 KGSL_FT_INFO(device, "Bad RB buffer size 0x%X\n",
1737 ft_data->bad_rb_size);
1738 adreno_dump_rb(device, ft_data->bad_rb_buffer,
1739 ft_data->bad_rb_size<<2, 0, ft_data->bad_rb_size);
1740
1741 KGSL_FT_INFO(device, "Good RB buffer size 0x%X\n",
1742 ft_data->good_rb_size);
1743 adreno_dump_rb(device, ft_data->good_rb_buffer,
1744 ft_data->good_rb_size<<2, 0, ft_data->good_rb_size);
1745
1746 }
1747}
1748
Tarun Karradeeecc02013-01-21 23:42:17 -08001749static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001750_adreno_ft_resubmit_rb(struct kgsl_device *device,
Tarun Karradeeecc02013-01-21 23:42:17 -08001751 struct adreno_ringbuffer *rb,
1752 struct kgsl_context *context,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001753 struct adreno_ft_data *ft_data,
Tarun Karradeeecc02013-01-21 23:42:17 -08001754 unsigned int *buff, unsigned int size)
1755{
1756 unsigned int ret = 0;
Tarun Karra59e79542013-04-10 10:25:25 -07001757 unsigned int retry_num = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001758
Tarun Karra696f89e2013-01-27 21:31:40 -08001759 _adreno_debug_ft_info(device, ft_data);
1760
Tarun Karra59e79542013-04-10 10:25:25 -07001761 do {
1762 ret = _adreno_ft_restart_device(device, context);
1763 if (ret == 0)
1764 break;
1765 /*
1766 * If device restart fails sleep for 20ms before
1767 * attempting restart. This allows GPU HW to settle
1768 * and improve the chances of next restart to be
1769 * successful.
1770 */
1771 msleep(20);
1772 KGSL_FT_ERR(device, "Retry device restart %d\n", retry_num);
1773 retry_num++;
1774 } while (retry_num < 4);
1775
1776 if (ret) {
1777 KGSL_FT_ERR(device, "Device restart failed\n");
1778 BUG_ON(1);
1779 goto done;
1780 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001781
1782 if (size) {
1783
1784 /* submit commands and wait for them to pass */
1785 adreno_ringbuffer_restore(rb, buff, size);
1786
1787 ret = adreno_idle(device);
1788 }
1789
Tarun Karra59e79542013-04-10 10:25:25 -07001790done:
Tarun Karradeeecc02013-01-21 23:42:17 -08001791 return ret;
1792}
1793
Tarun Karradeeecc02013-01-21 23:42:17 -08001794static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001795_adreno_ft(struct kgsl_device *device,
1796 struct adreno_ft_data *ft_data)
Tarun Karradeeecc02013-01-21 23:42:17 -08001797{
1798 int ret = 0, i;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001799 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1800 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1801 struct kgsl_context *context;
1802 struct adreno_context *adreno_context = NULL;
1803 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
Tarun Karra59e79542013-04-10 10:25:25 -07001804 unsigned int long_ib = 0;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001805
Tarun Karrad20d71a2013-01-25 15:38:57 -08001806 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001807 if (context == NULL) {
Tarun Karra59e79542013-04-10 10:25:25 -07001808 KGSL_FT_ERR(device, "Last context unknown id:%d\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001809 ft_data->context_id);
Tarun Karra59e79542013-04-10 10:25:25 -07001810 goto play_good_cmds;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001811 } else {
1812 adreno_context = context->devctxt;
1813 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001814 /*
1815 * set the invalid ts flag to 0 for this context since we have
1816 * detected a hang for it
1817 */
1818 context->wait_on_invalid_ts = false;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001819
Tarun Karra59e79542013-04-10 10:25:25 -07001820 if (!(adreno_context->flags & CTXT_FLAGS_PER_CONTEXT_TS)) {
1821 ft_data->status = 1;
1822 KGSL_FT_ERR(device, "Fault tolerance not supported\n");
1823 goto play_good_cmds;
1824 }
1825
Tarun Karra83297222013-02-05 19:45:49 -08001826 /*
1827 * This flag will be set by userspace for contexts
1828 * that do not want to be fault tolerant (ex: OPENCL)
1829 */
1830 if (adreno_context->flags & CTXT_FLAGS_NO_FAULT_TOLERANCE) {
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001831 ft_data->status = 1;
Tarun Karra83297222013-02-05 19:45:49 -08001832 KGSL_FT_ERR(device,
1833 "No FT set for this context play good cmds\n");
1834 goto play_good_cmds;
1835 }
1836
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001837 }
1838
Tarun Karra59e79542013-04-10 10:25:25 -07001839 /* Check if we detected a long running IB,
1840 * if true do not attempt replay of bad cmds */
1841 if (adreno_dev->long_ib) {
1842 long_ib = _adreno_check_long_ib(device);
1843 if (!long_ib) {
1844 adreno_context->flags &= ~CTXT_FLAGS_GPU_HANG;
1845 return 0;
1846 }
1847 }
1848
Tarun Karra3164fb02013-02-05 15:38:51 -08001849 /*
1850 * Extract valid contents from rb which can still be executed after
1851 * hang
1852 */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001853 adreno_ringbuffer_extract(rb, ft_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001854
Tarun Karra59e79542013-04-10 10:25:25 -07001855 /* If long IB detected do not attempt replay of bad cmds */
1856 if (long_ib) {
1857 ft_data->status = 1;
1858 _adreno_debug_ft_info(device, ft_data);
1859 goto play_good_cmds;
1860 }
1861
1862 if ((ft_data->ft_policy & KGSL_FT_DISABLE) ||
1863 (ft_data->ft_policy & KGSL_FT_TEMP_DISABLE)) {
1864 KGSL_FT_ERR(device, "NO FT policy play only good cmds\n");
1865 ft_data->status = 1;
1866 goto play_good_cmds;
Tarun Karra696f89e2013-01-27 21:31:40 -08001867 }
1868
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001869 /* Do not try the reply if hang is due to a pagefault */
1870 if (adreno_context->pagefault) {
1871 if ((ft_data->context_id == adreno_context->id) &&
1872 (ft_data->global_eop == adreno_context->pagefault_ts)) {
1873 ft_data->ft_policy &= ~KGSL_FT_REPLAY;
1874 KGSL_FT_ERR(device, "MMU fault skipping replay\n");
1875 }
1876
1877 adreno_context->pagefault = 0;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001878 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001879
Tarun Karra3164fb02013-02-05 15:38:51 -08001880 if (ft_data->ft_policy & KGSL_FT_REPLAY) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001881 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1882 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001883
Tarun Karra3164fb02013-02-05 15:38:51 -08001884 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001885 KGSL_FT_ERR(device, "Replay status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001886 ft_data->status = 1;
1887 } else
Tarun Karradeeecc02013-01-21 23:42:17 -08001888 goto play_good_cmds;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001889 }
1890
Tarun Karra3164fb02013-02-05 15:38:51 -08001891 if (ft_data->ft_policy & KGSL_FT_SKIPIB) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001892 for (i = 0; i < ft_data->bad_rb_size; i++) {
1893 if ((ft_data->bad_rb_buffer[i] ==
1894 CP_HDR_INDIRECT_BUFFER_PFD) &&
1895 (ft_data->bad_rb_buffer[i+1] == ft_data->ib1)) {
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001896
Tarun Karrad20d71a2013-01-25 15:38:57 -08001897 ft_data->bad_rb_buffer[i] = cp_nop_packet(2);
1898 ft_data->bad_rb_buffer[i+1] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001899 KGSL_NOP_IB_IDENTIFIER;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001900 ft_data->bad_rb_buffer[i+2] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001901 KGSL_NOP_IB_IDENTIFIER;
1902 break;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001903 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001904 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001905
Tarun Karrad20d71a2013-01-25 15:38:57 -08001906 if ((i == (ft_data->bad_rb_size)) || (!ft_data->ib1)) {
1907 KGSL_FT_ERR(device, "Bad IB to NOP not found\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001908 ft_data->status = 1;
Tarun Karradeeecc02013-01-21 23:42:17 -08001909 goto play_good_cmds;
1910 }
1911
Tarun Karrad20d71a2013-01-25 15:38:57 -08001912 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1913 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001914
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001915 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001916 KGSL_FT_ERR(device, "NOP faulty IB status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001917 ft_data->status = 1;
1918 } else {
1919 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001920 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001921 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001922 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001923
Tarun Karra3164fb02013-02-05 15:38:51 -08001924 if (ft_data->ft_policy & KGSL_FT_SKIPFRAME) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001925 for (i = 0; i < ft_data->bad_rb_size; i++) {
1926 if (ft_data->bad_rb_buffer[i] ==
1927 KGSL_END_OF_FRAME_IDENTIFIER) {
1928 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001929 break;
1930 }
1931 }
1932
1933 /* EOF not found in RB, discard till EOF in
1934 next IB submission */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001935 if (i == ft_data->bad_rb_size) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001936 adreno_context->flags |= CTXT_FLAGS_SKIP_EOF;
Tarun Karra3164fb02013-02-05 15:38:51 -08001937 KGSL_FT_INFO(device,
1938 "EOF not found in RB, skip next issueib till EOF\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08001939 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001940 }
1941
Tarun Karrad20d71a2013-01-25 15:38:57 -08001942 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1943 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001944
1945 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001946 KGSL_FT_ERR(device, "Skip EOF status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001947 ft_data->status = 1;
1948 } else {
1949 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001950 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001951 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001952 }
1953
1954play_good_cmds:
1955
Tarun Karra3164fb02013-02-05 15:38:51 -08001956 if (ft_data->status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001957 KGSL_FT_ERR(device, "Bad context commands failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001958 else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001959 KGSL_FT_INFO(device, "Bad context commands success\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001960
1961 if (adreno_context) {
1962 adreno_context->flags = (adreno_context->flags &
Tarun Karrad20d71a2013-01-25 15:38:57 -08001963 ~CTXT_FLAGS_GPU_HANG) | CTXT_FLAGS_GPU_HANG_FT;
Tarun Karradeeecc02013-01-21 23:42:17 -08001964 }
1965 adreno_dev->drawctxt_active = last_active_ctx;
1966 }
1967
Tarun Karrad20d71a2013-01-25 15:38:57 -08001968 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1969 ft_data->good_rb_buffer, ft_data->good_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001970
1971 if (ret) {
1972 /* If we fail here we can try to invalidate another
1973 * context and try fault tolerance again */
1974 ret = -EAGAIN;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001975 KGSL_FT_ERR(device, "Playing good commands unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001976 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001977 } else
1978 KGSL_FT_INFO(device, "Playing good commands successful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001979
1980 /* ringbuffer now has data from the last valid context id,
1981 * so restore the active_ctx to the last valid context */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001982 if (ft_data->last_valid_ctx_id) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001983 struct kgsl_context *last_ctx =
1984 idr_find(&device->context_idr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001985 ft_data->last_valid_ctx_id);
Tarun Karradeeecc02013-01-21 23:42:17 -08001986 if (last_ctx)
1987 adreno_dev->drawctxt_active = last_ctx->devctxt;
1988 }
1989
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001990done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001991 /* Turn off iommu clocks */
1992 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1993 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001994 return ret;
1995}
1996
1997static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001998adreno_ft(struct kgsl_device *device,
1999 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002000{
2001 int ret = 0;
2002 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2003 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2004 unsigned int timestamp;
2005
Tarun Karrad20d71a2013-01-25 15:38:57 -08002006 KGSL_FT_INFO(device,
2007 "Start Parameters: IB1: 0x%X, "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002008 "Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08002009 ft_data->ib1, ft_data->context_id, ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002010
2011 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
Tarun Karrad20d71a2013-01-25 15:38:57 -08002012 KGSL_FT_INFO(device, "Last issued global timestamp: %x\n", timestamp);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002013
2014 /* We may need to replay commands multiple times based on whether
2015 * multiple contexts hang the GPU */
2016 while (true) {
Tarun Karradeeecc02013-01-21 23:42:17 -08002017
Tarun Karrad20d71a2013-01-25 15:38:57 -08002018 ret = _adreno_ft(device, ft_data);
2019
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002020 if (-EAGAIN == ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002021 /* setup new fault tolerance parameters and retry, this
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002022 * means more than 1 contexts are causing hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08002023 adreno_destroy_ft_data(ft_data);
Tarun Karra59e79542013-04-10 10:25:25 -07002024 adreno_setup_ft_data(device, ft_data);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002025 KGSL_FT_INFO(device,
2026 "Retry. Parameters: "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002027 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08002028 ft_data->ib1, ft_data->context_id,
2029 ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002030 } else {
2031 break;
2032 }
2033 }
2034
2035 if (ret)
2036 goto done;
2037
Tarun Karrad20d71a2013-01-25 15:38:57 -08002038 /* Restore correct states after fault tolerance */
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002039 if (adreno_dev->drawctxt_active)
2040 device->mmu.hwpagetable =
2041 adreno_dev->drawctxt_active->pagetable;
2042 else
2043 device->mmu.hwpagetable = device->mmu.defaultpagetable;
2044 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
2045 kgsl_sharedmem_writel(&device->memstore,
2046 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2047 eoptimestamp),
2048 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07002049
2050 /* switch to NULL ctxt */
2051 if (adreno_dev->drawctxt_active != NULL)
2052 adreno_drawctxt_switch(adreno_dev, NULL, 0);
2053
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002054done:
2055 adreno_set_max_ts_for_bad_ctxs(device);
2056 adreno_mark_context_status(device, ret);
Tarun Karra3164fb02013-02-05 15:38:51 -08002057 KGSL_FT_ERR(device, "policy 0x%X status 0x%x\n",
2058 ft_data->ft_policy, ret);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002059 return ret;
2060}
2061
2062int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002063adreno_dump_and_exec_ft(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002064{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002065 int result = -ETIMEDOUT;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002066 struct adreno_ft_data ft_data;
Tarun Karra696f89e2013-01-27 21:31:40 -08002067 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karrad2326b82013-01-28 19:09:41 -08002068 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2069 unsigned int curr_pwrlevel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002070
2071 if (device->state == KGSL_STATE_HUNG)
2072 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002073 if (device->state == KGSL_STATE_DUMP_AND_FT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002074 mutex_unlock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002075 wait_for_completion(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002076 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07002077 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002078 result = 0;
2079 } else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002080 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_FT);
2081 INIT_COMPLETION(device->ft_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002082 /* Detected a hang */
2083
Tarun Karrad2326b82013-01-28 19:09:41 -08002084 /* Run fault tolerance at max power level */
2085 curr_pwrlevel = pwr->active_pwrlevel;
2086 kgsl_pwrctrl_pwrlevel_change(device, pwr->max_pwrlevel);
2087
Tarun Karrad20d71a2013-01-25 15:38:57 -08002088 /* Get the fault tolerance data as soon as hang is detected */
Tarun Karra59e79542013-04-10 10:25:25 -07002089 adreno_setup_ft_data(device, &ft_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002090
2091 /*
Tarun Karra696f89e2013-01-27 21:31:40 -08002092 * If long ib is detected, do not attempt postmortem or
2093 * snapshot, if GPU is still executing commands
2094 * we will get errors
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002095 */
Tarun Karra696f89e2013-01-27 21:31:40 -08002096 if (!adreno_dev->long_ib) {
2097 /*
2098 * Trigger an automatic dump of the state to
2099 * the console
2100 */
2101 kgsl_postmortem_dump(device, 0);
2102
2103 /*
2104 * Make a GPU snapshot. For now, do it after the
2105 * PM dump so we can at least be sure the PM dump
2106 * will work as it always has
2107 */
2108 kgsl_device_snapshot(device, 1);
2109 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002110
Tarun Karra59e79542013-04-10 10:25:25 -07002111 result = adreno_ft(device, &ft_data);
2112 adreno_destroy_ft_data(&ft_data);
Tarun Karra696f89e2013-01-27 21:31:40 -08002113
Tarun Karrad2326b82013-01-28 19:09:41 -08002114 /* restore power level */
2115 kgsl_pwrctrl_pwrlevel_change(device, curr_pwrlevel);
2116
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002117 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002118 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002119 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002120 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002121 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
2122 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08002123 complete_all(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002124 }
2125done:
2126 return result;
2127}
Tarun Karrad20d71a2013-01-25 15:38:57 -08002128EXPORT_SYMBOL(adreno_dump_and_exec_ft);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002129
2130static int adreno_getproperty(struct kgsl_device *device,
2131 enum kgsl_property_type type,
2132 void *value,
2133 unsigned int sizebytes)
2134{
2135 int status = -EINVAL;
2136 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2137
2138 switch (type) {
2139 case KGSL_PROP_DEVICE_INFO:
2140 {
2141 struct kgsl_devinfo devinfo;
2142
2143 if (sizebytes != sizeof(devinfo)) {
2144 status = -EINVAL;
2145 break;
2146 }
2147
2148 memset(&devinfo, 0, sizeof(devinfo));
2149 devinfo.device_id = device->id+1;
2150 devinfo.chip_id = adreno_dev->chip_id;
2151 devinfo.mmu_enabled = kgsl_mmu_enabled();
2152 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06002153 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
2154 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002155
2156 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
2157 0) {
2158 status = -EFAULT;
2159 break;
2160 }
2161 status = 0;
2162 }
2163 break;
2164 case KGSL_PROP_DEVICE_SHADOW:
2165 {
2166 struct kgsl_shadowprop shadowprop;
2167
2168 if (sizebytes != sizeof(shadowprop)) {
2169 status = -EINVAL;
2170 break;
2171 }
2172 memset(&shadowprop, 0, sizeof(shadowprop));
2173 if (device->memstore.hostptr) {
2174 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2175 * anything to mmap().
2176 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07002177 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178 shadowprop.size = device->memstore.size;
2179 /* GSL needs this to be set, even if it
2180 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002181 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2182 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002183 }
2184 if (copy_to_user(value, &shadowprop,
2185 sizeof(shadowprop))) {
2186 status = -EFAULT;
2187 break;
2188 }
2189 status = 0;
2190 }
2191 break;
2192 case KGSL_PROP_MMU_ENABLE:
2193 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002194 int mmu_prop = kgsl_mmu_enabled();
2195
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002196 if (sizebytes != sizeof(int)) {
2197 status = -EINVAL;
2198 break;
2199 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002200 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002201 status = -EFAULT;
2202 break;
2203 }
2204 status = 0;
2205 }
2206 break;
2207 case KGSL_PROP_INTERRUPT_WAITS:
2208 {
2209 int int_waits = 1;
2210 if (sizebytes != sizeof(int)) {
2211 status = -EINVAL;
2212 break;
2213 }
2214 if (copy_to_user(value, &int_waits, sizeof(int))) {
2215 status = -EFAULT;
2216 break;
2217 }
2218 status = 0;
2219 }
2220 break;
2221 default:
2222 status = -EINVAL;
2223 }
2224
2225 return status;
2226}
2227
Jordan Crousef7370f82012-04-18 09:31:07 -06002228static int adreno_setproperty(struct kgsl_device *device,
2229 enum kgsl_property_type type,
2230 void *value,
2231 unsigned int sizebytes)
2232{
2233 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08002234 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06002235
2236 switch (type) {
2237 case KGSL_PROP_PWRCTRL: {
2238 unsigned int enable;
2239 struct kgsl_device_platform_data *pdata =
2240 kgsl_device_get_drvdata(device);
2241
2242 if (sizebytes != sizeof(enable))
2243 break;
2244
2245 if (copy_from_user(&enable, (void __user *) value,
2246 sizeof(enable))) {
2247 status = -EFAULT;
2248 break;
2249 }
2250
2251 if (enable) {
2252 if (pdata->nap_allowed)
2253 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002254 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002255 kgsl_pwrscale_enable(device);
2256 } else {
2257 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002258 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002259 kgsl_pwrscale_disable(device);
2260 }
2261
2262 status = 0;
2263 }
2264 break;
2265 default:
2266 break;
2267 }
2268
2269 return status;
2270}
2271
Jordan Crousea29a2e02012-08-14 09:09:23 -06002272static int adreno_ringbuffer_drain(struct kgsl_device *device,
2273 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002274{
2275 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2276 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002277 unsigned long wait;
2278 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2279
2280 if (!(rb->flags & KGSL_FLAGS_STARTED))
2281 return 0;
2282
2283 /*
2284 * The first time into the loop, wait for 100 msecs and kick wptr again
2285 * to ensure that the hardware has updated correctly. After that, kick
2286 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2287 * expires
2288 */
2289
2290 wait = jiffies + msecs_to_jiffies(100);
2291
Jordan Crousea29a2e02012-08-14 09:09:23 -06002292 do {
2293 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002294 /* Check to see if the core is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002295 if (adreno_ft_detect(device, regs))
Jordan Crousea29a2e02012-08-14 09:09:23 -06002296 return -ETIMEDOUT;
2297
2298 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2299 }
2300 GSL_RB_GET_READPTR(rb, &rb->rptr);
2301
2302 if (time_after(jiffies, timeout)) {
2303 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2304 rb->rptr, rb->wptr);
2305 return -ETIMEDOUT;
2306 }
2307 } while (rb->rptr != rb->wptr);
2308
2309 return 0;
2310}
2311
2312/* Caller must hold the device mutex. */
2313int adreno_idle(struct kgsl_device *device)
2314{
2315 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002316 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302317 unsigned long wait_time;
2318 unsigned long wait_time_part;
Tarun Karra696f89e2013-01-27 21:31:40 -08002319 unsigned int prev_reg_val[ft_detect_regs_count];
Tarun Karra3335f142012-06-19 14:11:48 -07002320
2321 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002322
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002323 kgsl_cffdump_regpoll(device->id,
2324 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002325 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002326
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002327retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002328 /* First, wait for the ringbuffer to drain */
2329 if (adreno_ringbuffer_drain(device, prev_reg_val))
2330 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002331
2332 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002333 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002334 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2335
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002336 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002337 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2338 &rbbm_status);
2339 if (adreno_is_a2xx(adreno_dev)) {
2340 if (rbbm_status == 0x110)
2341 return 0;
2342 } else {
2343 if (!(rbbm_status & 0x80000000))
2344 return 0;
2345 }
Tarun Karra3335f142012-06-19 14:11:48 -07002346
2347 /* Dont wait for timeout, detect hang faster.
2348 */
2349 if (time_after(jiffies, wait_time_part)) {
2350 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002351 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra696f89e2013-01-27 21:31:40 -08002352 if ((adreno_ft_detect(device, prev_reg_val)))
Tarun Karra3335f142012-06-19 14:11:48 -07002353 goto err;
2354 }
2355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002356 }
2357
2358err:
2359 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08002360 if (KGSL_STATE_DUMP_AND_FT != device->state &&
2361 !adreno_dump_and_exec_ft(device)) {
2362 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002363 goto retry;
2364 }
2365 return -ETIMEDOUT;
2366}
2367
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002368/**
2369 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2370 * rbbm_status register
2371 * @device - Pointer to the GPU device whose idle status is to be
2372 * checked
2373 * @returns - Returns whether the core is idle (based on rbbm_status)
2374 * false if the core is active, true if the core is idle
2375 */
2376static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2377{
2378 unsigned int reg_rbbm_status;
2379 bool status = false;
2380 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2381
2382 /* Is the core idle? */
2383 adreno_regread(device,
2384 adreno_dev->gpudev->reg_rbbm_status,
2385 &reg_rbbm_status);
2386
2387 if (adreno_is_a2xx(adreno_dev)) {
2388 if (reg_rbbm_status == 0x110)
2389 status = true;
2390 } else {
2391 if (!(reg_rbbm_status & 0x80000000))
2392 status = true;
2393 }
2394 return status;
2395}
2396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002397static unsigned int adreno_isidle(struct kgsl_device *device)
2398{
2399 int status = false;
2400 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2401 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002402
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002403 WARN_ON(device->state == KGSL_STATE_INIT);
2404 /* If the device isn't active, don't force it on. */
2405 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002406 /* Is the ring buffer is empty? */
2407 GSL_RB_GET_READPTR(rb, &rb->rptr);
2408 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
Jordan Crouseab601992013-03-05 11:18:20 -07002409 /*
2410 * Are there interrupts pending? If so then pretend we
2411 * are not idle - this avoids the possiblity that we go
2412 * to a lower power state without handling interrupts
2413 * first.
2414 */
2415
2416 if (!adreno_dev->gpudev->irq_pending(adreno_dev)) {
2417 /* Is the core idle? */
2418 status = is_adreno_rbbm_status_idle(device);
2419 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002420 }
2421 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002422 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002423 }
2424 return status;
2425}
2426
2427/* Caller must hold the device mutex. */
2428static int adreno_suspend_context(struct kgsl_device *device)
2429{
2430 int status = 0;
2431 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2432
2433 /* switch to NULL ctxt */
2434 if (adreno_dev->drawctxt_active != NULL) {
2435 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002436 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002437 }
2438
2439 return status;
2440}
2441
Jordan Crouse233b2092012-04-18 09:31:09 -06002442/* Find a memory structure attached to an adreno context */
2443
2444struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2445 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2446{
2447 struct kgsl_context *context;
2448 struct adreno_context *adreno_context = NULL;
2449 int next = 0;
2450
2451 while (1) {
2452 context = idr_get_next(&device->context_idr, &next);
2453 if (context == NULL)
2454 break;
2455
2456 adreno_context = (struct adreno_context *)context->devctxt;
2457
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002458 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2459 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002460 struct kgsl_memdesc *desc;
2461
2462 desc = &adreno_context->gpustate;
2463 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2464 return desc;
2465
2466 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2467 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2468 return desc;
2469 }
2470 next = next + 1;
2471 }
2472
2473 return NULL;
2474}
2475
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002476struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002477 unsigned int pt_base,
2478 unsigned int gpuaddr,
2479 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002480{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002482 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2483 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2484
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002485 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2486 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002487
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002488 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2489 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002490
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002491 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2492 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002493
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002494 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2495 size))
2496 return &device->mmu.setstate_memory;
2497
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002498 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002499
2500 if (entry)
2501 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002502
Jordan Crouse233b2092012-04-18 09:31:09 -06002503 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002504}
2505
2506uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2507 unsigned int gpuaddr, unsigned int size)
2508{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002509 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002510
2511 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2512
2513 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002514}
2515
2516void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2517 unsigned int *value)
2518{
2519 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002520 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2521 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002522
2523 if (!in_interrupt())
2524 kgsl_pre_hwaccess(device);
2525
2526 /*ensure this read finishes before the next one.
2527 * i.e. act like normal readl() */
2528 *value = __raw_readl(reg);
2529 rmb();
2530}
2531
2532void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2533 unsigned int value)
2534{
2535 unsigned int *reg;
2536
Jordan Crouse7501d452012-04-19 08:58:44 -06002537 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002538
2539 if (!in_interrupt())
2540 kgsl_pre_hwaccess(device);
2541
2542 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002543 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002544
2545 /*ensure previous writes post before this one,
2546 * i.e. act like normal writel() */
2547 wmb();
2548 __raw_writel(value, reg);
2549}
2550
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002551static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2552{
2553 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002554 if (k_ctxt != NULL) {
2555 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002556 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2557 context_id = KGSL_CONTEXT_INVALID;
2558 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2559 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002560 }
2561
2562 return context_id;
2563}
2564
Carter Cooper740f6742013-01-03 16:19:23 -07002565static unsigned int adreno_check_hw_ts(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002566 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002567{
Carter Cooper740f6742013-01-03 16:19:23 -07002568 int status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002569 unsigned int ref_ts, enableflag;
Carter Cooper740f6742013-01-03 16:19:23 -07002570 unsigned int context_id = _get_context_id(context);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002571
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002572 /*
2573 * If the context ID is invalid, we are in a race with
2574 * the context being destroyed by userspace so bail.
2575 */
2576 if (context_id == KGSL_CONTEXT_INVALID) {
2577 KGSL_DRV_WARN(device, "context was detached");
Carter Cooper740f6742013-01-03 16:19:23 -07002578 return -EINVAL;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002579 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002580
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002581 status = kgsl_check_timestamp(device, context, timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002582 if (status)
2583 return status;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002584
Carter Cooper740f6742013-01-03 16:19:23 -07002585 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2586 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2587 /*
2588 * Barrier is needed here to make sure the read from memstore
2589 * has posted
2590 */
2591
2592 mb();
2593
2594 if (enableflag) {
2595 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002596 KGSL_MEMSTORE_OFFSET(context_id,
2597 ref_wait_ts));
Carter Cooper740f6742013-01-03 16:19:23 -07002598
2599 /* Make sure the memstore read has posted */
2600 mb();
2601 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
2602 kgsl_sharedmem_writel(&device->memstore,
2603 KGSL_MEMSTORE_OFFSET(context_id,
2604 ref_wait_ts), timestamp);
2605 /* Make sure the memstore write is posted */
2606 wmb();
2607 }
2608 } else {
2609 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002610 KGSL_MEMSTORE_OFFSET(context_id,
2611 ref_wait_ts), timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002612 enableflag = 1;
2613 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002614 KGSL_MEMSTORE_OFFSET(context_id,
2615 ts_cmp_enable), enableflag);
Carter Cooper740f6742013-01-03 16:19:23 -07002616 /* Make sure the memstore write gets posted */
2617 wmb();
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002618
Carter Cooper740f6742013-01-03 16:19:23 -07002619 /*
2620 * submit a dummy packet so that even if all
2621 * commands upto timestamp get executed we will still
2622 * get an interrupt
2623 */
2624
2625 if (context && device->state != KGSL_STATE_SLUMBER)
2626 adreno_ringbuffer_issuecmds(device, context->devctxt,
2627 KGSL_CMD_FLAGS_NONE, NULL, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002628 }
Carter Cooper740f6742013-01-03 16:19:23 -07002629
2630 return 0;
2631}
2632
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002633/* Return 1 if the event timestmp has already passed, 0 if it was marked */
2634static int adreno_next_event(struct kgsl_device *device,
Carter Cooper740f6742013-01-03 16:19:23 -07002635 struct kgsl_event *event)
2636{
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002637 return adreno_check_hw_ts(device, event->context, event->timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002638}
2639
2640static int adreno_check_interrupt_timestamp(struct kgsl_device *device,
2641 struct kgsl_context *context, unsigned int timestamp)
2642{
2643 int status;
2644
2645 mutex_lock(&device->mutex);
2646 status = adreno_check_hw_ts(device, context, timestamp);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002647 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002648
2649 return status;
2650}
2651
2652/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002653 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002654 placing a process in wait q. For conditional interrupts we expect the
2655 process to already be in its wait q when its exit condition checking
2656 function is called.
2657*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002658#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002659({ \
2660 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002661 if (io) \
2662 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2663 else \
2664 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002665 __ret; \
2666})
2667
Tarun Karra3335f142012-06-19 14:11:48 -07002668
2669
Tarun Karra696f89e2013-01-27 21:31:40 -08002670unsigned int adreno_ft_detect(struct kgsl_device *device,
Tarun Karra3335f142012-06-19 14:11:48 -07002671 unsigned int *prev_reg_val)
2672{
2673 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karra696f89e2013-01-27 21:31:40 -08002674 unsigned int curr_reg_val[ft_detect_regs_count];
2675 unsigned int fast_hang_detected = 1;
2676 unsigned int long_ib_detected = 1;
Tarun Karra3335f142012-06-19 14:11:48 -07002677 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002678 static unsigned long next_hang_detect_time;
Tarun Karra696f89e2013-01-27 21:31:40 -08002679 static unsigned int prev_global_ts;
2680 unsigned int curr_global_ts = 0;
2681 unsigned int curr_context_id = 0;
2682 static struct adreno_context *curr_context;
2683 static struct kgsl_context *context;
Tarun Karra3335f142012-06-19 14:11:48 -07002684
2685 if (!adreno_dev->fast_hang_detect)
Tarun Karra696f89e2013-01-27 21:31:40 -08002686 fast_hang_detected = 0;
2687
2688 if (!adreno_dev->long_ib_detect)
2689 long_ib_detected = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002690
Jordan Crousecca61142012-11-20 10:54:24 -07002691 if (is_adreno_rbbm_status_idle(device)) {
2692
2693 /*
2694 * On A20X if the RPTR != WPTR and the device is idle, then
2695 * the last write to WPTR probably failed to latch so write it
2696 * again
2697 */
2698
2699 if (adreno_is_a2xx(adreno_dev)) {
2700 unsigned int rptr;
2701 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2702 if (rptr != adreno_dev->ringbuffer.wptr)
2703 adreno_regwrite(device, REG_CP_RB_WPTR,
2704 adreno_dev->ringbuffer.wptr);
2705 }
2706
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002707 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002708 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002709
Tarun Karra4648db92013-02-01 17:03:40 -08002710 /*
2711 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2712 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2713 * from the last time do nothing.
2714 */
2715 if ((next_hang_detect_time) &&
2716 (time_before(jiffies, next_hang_detect_time)))
2717 return 0;
2718 else
2719 next_hang_detect_time = (jiffies +
2720 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2721
Tarun Karra696f89e2013-01-27 21:31:40 -08002722 /* Read the current Hang detect reg values here */
2723 for (i = 0; i < ft_detect_regs_count; i++) {
2724 if (ft_detect_regs[i] == 0)
Jordan Crouseb5c80482012-10-03 09:38:41 -06002725 continue;
Tarun Karra696f89e2013-01-27 21:31:40 -08002726 adreno_regread(device, ft_detect_regs[i],
2727 &curr_reg_val[i]);
Tarun Karra3335f142012-06-19 14:11:48 -07002728 }
2729
Tarun Karra696f89e2013-01-27 21:31:40 -08002730 /* Read the current global timestamp here */
2731 kgsl_sharedmem_readl(&device->memstore,
2732 &curr_global_ts,
2733 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2734 eoptimestamp));
2735
2736 mb();
2737
2738 if (curr_global_ts == prev_global_ts) {
2739
2740 /* Get the current context here */
2741 if (context == NULL) {
2742 kgsl_sharedmem_readl(&device->memstore,
2743 &curr_context_id,
2744 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2745 current_context));
2746 context = idr_find(&device->context_idr,
2747 curr_context_id);
2748 if (context != NULL) {
2749 curr_context = context->devctxt;
2750 curr_context->ib_gpu_time_used = 0;
2751 } else {
2752 KGSL_DRV_ERR(device,
2753 "Fault tolerance no context found\n");
2754 }
2755 }
2756
2757 mb();
2758
2759 if (curr_context != NULL) {
2760
2761 curr_context->ib_gpu_time_used += KGSL_TIMEOUT_PART;
2762 KGSL_FT_INFO(device,
2763 "Proc %s used GPU Time %d ms on timestamp 0x%X\n",
2764 curr_context->pid_name, curr_context->ib_gpu_time_used,
2765 curr_global_ts+1);
2766
2767 for (i = 0; i < ft_detect_regs_count; i++) {
2768 if (curr_reg_val[i] != prev_reg_val[i]) {
2769 fast_hang_detected = 0;
2770
2771 /* Check for long IB here */
2772 if ((i >=
2773 LONG_IB_DETECT_REG_INDEX_START)
2774 &&
2775 (i <=
2776 LONG_IB_DETECT_REG_INDEX_END))
2777 long_ib_detected = 0;
2778 }
2779 }
2780
2781 if (fast_hang_detected) {
2782 KGSL_FT_ERR(device,
2783 "Proc %s, ctxt_id %d ts %d triggered fault tolerance"
2784 " on global ts %d\n",
2785 curr_context->pid_name, curr_context->id
2786 , (kgsl_readtimestamp(device, context,
2787 KGSL_TIMESTAMP_RETIRED)+1),
2788 curr_global_ts+1);
2789 return 1;
2790 }
2791
Tarun Karra83297222013-02-05 19:45:49 -08002792 if ((long_ib_detected) &&
2793 (!(curr_context->flags &
2794 CTXT_FLAGS_NO_FAULT_TOLERANCE))) {
Tarun Karra696f89e2013-01-27 21:31:40 -08002795 curr_context->ib_gpu_time_used +=
2796 KGSL_TIMEOUT_PART;
2797 if (curr_context->ib_gpu_time_used >
2798 KGSL_TIMEOUT_LONG_IB_DETECTION) {
2799 if (adreno_dev->long_ib_ts !=
2800 curr_global_ts) {
2801 KGSL_FT_ERR(device,
2802 "Proc %s, ctxt_id %d ts %d"
2803 "used GPU for %d ms long ib "
2804 "detected on global ts %d\n",
2805 curr_context->pid_name,
2806 curr_context->id,
2807 (kgsl_readtimestamp(device,
2808 context,
2809 KGSL_TIMESTAMP_RETIRED)+1),
2810 curr_context->ib_gpu_time_used,
2811 curr_global_ts+1);
2812 adreno_dev->long_ib = 1;
2813 adreno_dev->long_ib_ts =
2814 curr_global_ts;
2815 curr_context->ib_gpu_time_used =
2816 0;
2817 return 1;
2818 }
2819 }
2820 }
2821 } else {
2822 KGSL_FT_ERR(device,
2823 "Last context unknown id:%d\n",
2824 curr_context_id);
2825 }
2826 } else {
2827 /* GPU is moving forward */
2828 prev_global_ts = curr_global_ts;
2829 context = NULL;
2830 curr_context = NULL;
2831 adreno_dev->long_ib = 0;
2832 adreno_dev->long_ib_ts = 0;
2833 }
2834
2835
2836 /* If hangs are not detected copy the current reg values
2837 * to previous values and return no hang */
2838 for (i = 0; i < ft_detect_regs_count; i++)
2839 prev_reg_val[i] = curr_reg_val[i];
2840 return 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002841}
2842
Jordan Crouse92446a62012-11-15 11:00:06 -07002843/**
2844 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2845 * @device - pointer to a KGSL device structure
2846 * @context - pointer to the active KGSL context
2847 * @timestamp - the timestamp that the process was waiting for
2848 *
Tarun Karrad20d71a2013-01-25 15:38:57 -08002849 * Process a possible GPU hang and try fault tolerance from it
2850 * cleanly
Jordan Crouse92446a62012-11-15 11:00:06 -07002851 */
2852static int adreno_handle_hang(struct kgsl_device *device,
2853 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002854{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002855 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002856 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002857 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002858 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002859
Jordan Crouse92446a62012-11-15 11:00:06 -07002860 /* Do one last check to see if we somehow made it through */
2861 if (kgsl_check_timestamp(device, context, timestamp))
2862 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002863
2864 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002865
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002866 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2867 mb();
2868
Tarun Karra696f89e2013-01-27 21:31:40 -08002869 KGSL_DRV_WARN(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002870 "Device hang detected while waiting for timestamp: "
2871 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002872 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2873 context_id, timestamp, context_id, ts_issued, context_id,
2874 kgsl_readtimestamp(device, context,
2875 KGSL_TIMESTAMP_RETIRED),
2876 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002877
Tarun Karrad20d71a2013-01-25 15:38:57 -08002878 /* Return 0 after a successful fault tolerance */
2879 if (!adreno_dump_and_exec_ft(device))
Jordan Crouse92446a62012-11-15 11:00:06 -07002880 return 0;
2881
2882 return -ETIMEDOUT;
2883}
2884
2885static int _check_pending_timestamp(struct kgsl_device *device,
2886 struct kgsl_context *context, unsigned int timestamp)
2887{
2888 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2889 unsigned int context_id = _get_context_id(context);
2890 unsigned int ts_issued;
2891
2892 if (context_id == KGSL_CONTEXT_INVALID)
2893 return -EINVAL;
2894
2895 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2896
2897 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2898 return 0;
2899
2900 if (context && !context->wait_on_invalid_ts) {
2901 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2902 context_id, timestamp, context_id, ts_issued);
2903
2904 /* Only print this message once */
2905 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002906 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002907
2908 return -EINVAL;
2909}
2910
2911/**
2912 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2913 * @device - pointer to a KGSL device structure
2914 * @context - pointer to the active kgsl context
2915 * @timestamp - GPU timestamp to wait for
2916 * @msecs - amount of time to wait (in milliseconds)
2917 *
2918 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2919 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2920 * one if it happened. Otherwise, spend most of our time in an interruptible
2921 * wait for the timestamp interrupt to be processed. This function must be
2922 * called with the mutex already held.
2923 */
2924static int adreno_waittimestamp(struct kgsl_device *device,
2925 struct kgsl_context *context,
2926 unsigned int timestamp,
2927 unsigned int msecs)
2928{
2929 static unsigned int io_cnt;
2930 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2931 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2932 unsigned int context_id = _get_context_id(context);
Tarun Karra696f89e2013-01-27 21:31:40 -08002933 unsigned int prev_reg_val[ft_detect_regs_count];
Jordan Crouse92446a62012-11-15 11:00:06 -07002934 unsigned int time_elapsed = 0;
2935 unsigned int wait;
2936 int ts_compare = 1;
2937 int io, ret = -ETIMEDOUT;
2938
2939 /* Get out early if the context has already been destroyed */
2940
2941 if (context_id == KGSL_CONTEXT_INVALID) {
2942 KGSL_DRV_WARN(device, "context was detached");
2943 return -EINVAL;
2944 }
2945
2946 /*
2947 * Check to see if the requested timestamp is "newer" then the last
2948 * timestamp issued. If it is complain once and return error. Only
2949 * print the message once per context so that badly behaving
2950 * applications don't spam the logs
2951 */
2952
2953 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2954 if (_check_pending_timestamp(device, context, timestamp))
2955 return -EINVAL;
2956
2957 /* Reset the invalid timestamp flag on a valid wait */
2958 context->wait_on_invalid_ts = false;
2959 }
2960
2961
2962 /* Clear the registers used for hang detection */
2963 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2964
2965 /*
2966 * On the first time through the loop only wait 100ms.
2967 * this gives enough time for the engine to start moving and oddly
2968 * provides better hang detection results than just going the full
2969 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2970 * is if msecs happens to be < 100ms then just use the full timeout
2971 */
2972
2973 wait = 100;
2974
2975 do {
2976 long status;
2977
Jordan Crouse92446a62012-11-15 11:00:06 -07002978 /*
2979 * if the timestamp happens while we're not
2980 * waiting, there's a chance that an interrupt
2981 * will not be generated and thus the timestamp
2982 * work needs to be queued.
2983 */
2984
2985 if (kgsl_check_timestamp(device, context, timestamp)) {
2986 queue_work(device->work_queue, &device->ts_expired_ws);
2987 ret = 0;
2988 break;
2989 }
2990
2991 /* Check to see if the GPU is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002992 if (adreno_ft_detect(device, prev_reg_val)) {
Jordan Crouse92446a62012-11-15 11:00:06 -07002993 ret = adreno_handle_hang(device, context, timestamp);
2994 break;
2995 }
2996
2997 /*
2998 * For proper power accounting sometimes we need to call
2999 * io_wait_interruptible_timeout and sometimes we need to call
3000 * plain old wait_interruptible_timeout. We call the regular
3001 * timeout N times out of 100, where N is a number specified by
3002 * the current power level
3003 */
3004
3005 io_cnt = (io_cnt + 1) % 100;
3006 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
3007 ? 0 : 1;
3008
3009 mutex_unlock(&device->mutex);
3010
3011 /* Wait for a timestamp event */
3012 status = kgsl_wait_event_interruptible_timeout(
3013 device->wait_queue,
Carter Cooper740f6742013-01-03 16:19:23 -07003014 adreno_check_interrupt_timestamp(device, context,
Jordan Crouse92446a62012-11-15 11:00:06 -07003015 timestamp), msecs_to_jiffies(wait), io);
3016
3017 mutex_lock(&device->mutex);
3018
3019 /*
3020 * If status is non zero then either the condition was satisfied
3021 * or there was an error. In either event, this is the end of
3022 * the line for us
3023 */
3024
3025 if (status != 0) {
3026 ret = (status > 0) ? 0 : (int) status;
3027 break;
3028 }
Jordan Crouse92446a62012-11-15 11:00:06 -07003029 time_elapsed += wait;
3030
3031 /* If user specified timestamps are being used, wait at least
3032 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
3033 * issue a IB for a timestamp before checking to see if the
3034 * current timestamp we are waiting for is valid or not
3035 */
3036
3037 if (ts_compare && (adreno_ctx &&
3038 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
3039 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
3040 ret = _check_pending_timestamp(device, context,
3041 timestamp);
3042 if (ret)
3043 break;
3044
3045 /* Don't do this check again */
3046 ts_compare = 0;
3047
3048 /*
3049 * Reset the invalid timestamp flag on a valid
3050 * wait
3051 */
3052 context->wait_on_invalid_ts = false;
3053 }
3054 }
3055
3056 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003057 * We want to wait the floor of KGSL_TIMEOUT_PART
3058 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07003059 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003060
3061 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
3062 wait = KGSL_TIMEOUT_PART;
3063 else
3064 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07003065
3066 } while (!msecs || time_elapsed < msecs);
3067
3068 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003069}
3070
3071static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003072 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003073{
3074 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003075 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003076
Jeremy Gebben9ad86922012-05-08 15:33:23 -06003077 /*
3078 * If the context ID is invalid, we are in a race with
3079 * the context being destroyed by userspace so bail.
3080 */
3081 if (context_id == KGSL_CONTEXT_INVALID) {
3082 KGSL_DRV_WARN(device, "context was detached");
3083 return timestamp;
3084 }
Jordan Crousec659f382012-04-16 11:10:41 -06003085 switch (type) {
3086 case KGSL_TIMESTAMP_QUEUED: {
3087 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3088 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
3089
3090 timestamp = rb->timestamp[context_id];
3091 break;
3092 }
3093 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003094 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06003095 break;
3096 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003097 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06003098 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
3099 break;
3100 }
3101
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003102 rmb();
3103
3104 return timestamp;
3105}
3106
3107static long adreno_ioctl(struct kgsl_device_private *dev_priv,
3108 unsigned int cmd, void *data)
3109{
3110 int result = 0;
3111 struct kgsl_drawctxt_set_bin_base_offset *binbase;
3112 struct kgsl_context *context;
3113
3114 switch (cmd) {
3115 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
3116 binbase = data;
3117
3118 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
3119 if (context) {
3120 adreno_drawctxt_set_bin_base_offset(
3121 dev_priv->device, context, binbase->offset);
3122 } else {
3123 result = -EINVAL;
3124 KGSL_DRV_ERR(dev_priv->device,
3125 "invalid drawctxt drawctxt_id %d "
3126 "device_id=%d\n",
3127 binbase->drawctxt_id, dev_priv->device->id);
3128 }
3129 break;
3130
3131 default:
3132 KGSL_DRV_INFO(dev_priv->device,
3133 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07003134 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003135 break;
3136 }
3137 return result;
3138
3139}
3140
3141static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
3142{
3143 gpu_freq /= 1000000;
3144 return ticks / gpu_freq;
3145}
3146
3147static void adreno_power_stats(struct kgsl_device *device,
3148 struct kgsl_power_stats *stats)
3149{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003150 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003151 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003152 unsigned int cycles;
3153
3154 /* Get the busy cycles counted since the counter was last reset */
3155 /* Calling this function also resets and restarts the counter */
3156
3157 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003158
3159 /* In order to calculate idle you have to have run the algorithm *
3160 * at least once to get a start time. */
3161 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003162 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003163 stats->total_time = tmp - pwr->time;
3164 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003165 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003166 pwrlevels[device->pwrctrl.active_pwrlevel].
3167 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003168 } else {
3169 stats->total_time = 0;
3170 stats->busy_time = 0;
3171 pwr->time = ktime_to_us(ktime_get());
3172 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003173}
3174
3175void adreno_irqctrl(struct kgsl_device *device, int state)
3176{
Jordan Crousea78c9172011-07-11 13:14:09 -06003177 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3178 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003179}
3180
Jordan Croused6535882012-06-20 08:22:16 -06003181static unsigned int adreno_gpuid(struct kgsl_device *device,
3182 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07003183{
3184 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3185
Jordan Croused6535882012-06-20 08:22:16 -06003186 /* Some applications need to know the chip ID too, so pass
3187 * that as a parameter */
3188
3189 if (chipid != NULL)
3190 *chipid = adreno_dev->chip_id;
3191
Jordan Crousea0758f22011-12-07 11:19:22 -07003192 /* Standard KGSL gpuid format:
3193 * top word is 0x0002 for 2D or 0x0003 for 3D
3194 * Bottom word is core specific identifer
3195 */
3196
3197 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
3198}
3199
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003200static const struct kgsl_functable adreno_functable = {
3201 /* Mandatory functions */
3202 .regread = adreno_regread,
3203 .regwrite = adreno_regwrite,
3204 .idle = adreno_idle,
3205 .isidle = adreno_isidle,
3206 .suspend_context = adreno_suspend_context,
3207 .start = adreno_start,
3208 .stop = adreno_stop,
3209 .getproperty = adreno_getproperty,
3210 .waittimestamp = adreno_waittimestamp,
3211 .readtimestamp = adreno_readtimestamp,
3212 .issueibcmds = adreno_ringbuffer_issueibcmds,
3213 .ioctl = adreno_ioctl,
3214 .setup_pt = adreno_setup_pt,
3215 .cleanup_pt = adreno_cleanup_pt,
3216 .power_stats = adreno_power_stats,
3217 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07003218 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07003219 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06003220 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003221 /* Optional functions */
3222 .setstate = adreno_setstate,
3223 .drawctxt_create = adreno_drawctxt_create,
3224 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06003225 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06003226 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07003227 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003228};
3229
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003230static struct platform_driver adreno_platform_driver = {
3231 .probe = adreno_probe,
3232 .remove = __devexit_p(adreno_remove),
3233 .suspend = kgsl_suspend_driver,
3234 .resume = kgsl_resume_driver,
3235 .id_table = adreno_id_table,
3236 .driver = {
3237 .owner = THIS_MODULE,
3238 .name = DEVICE_3D_NAME,
3239 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06003240 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003241 }
3242};
3243
3244static int __init kgsl_3d_init(void)
3245{
3246 return platform_driver_register(&adreno_platform_driver);
3247}
3248
3249static void __exit kgsl_3d_exit(void)
3250{
3251 platform_driver_unregister(&adreno_platform_driver);
3252}
3253
3254module_init(kgsl_3d_init);
3255module_exit(kgsl_3d_exit);
3256
3257MODULE_DESCRIPTION("3D Graphics driver");
3258MODULE_VERSION("1.2");
3259MODULE_LICENSE("GPL v2");
3260MODULE_ALIAS("platform:kgsl_3d");