blob: c54c477c77cd0691036764782283eb3c9784161b [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
Jeremy Gebben2aba0f32013-05-28 16:54:00 -0600278 /*
279 * Set the mpu end to the last "normal" global memory we use.
280 * For the IOMMU, this will be used to restrict access to the
281 * mapped registers.
282 */
283 device->mh.mpu_range = device->mmu.setstate_memory.gpuaddr +
284 device->mmu.setstate_memory.size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700285 return result;
286
287unmap_memstore_desc:
288 kgsl_mmu_unmap(pagetable, &device->memstore);
289
290unmap_memptrs_desc:
291 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
292
293unmap_buffer_desc:
294 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
295
296error:
297 return result;
298}
299
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600300static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600301 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600302 uint32_t flags)
303{
304 unsigned int pt_val, reg_pt_val;
Tarun Karra9c070822012-11-27 16:43:51 -0700305 unsigned int link[250];
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600306 unsigned int *cmds = &link[0];
307 int sizedwords = 0;
308 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600309 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600310 struct kgsl_context *context;
311 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600312
313 if (!adreno_dev->drawctxt_active)
314 return kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700315 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600316
317 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700318 if (context == NULL)
319 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600320 adreno_ctx = context->devctxt;
321
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600322 if (kgsl_mmu_enable_clk(&device->mmu,
323 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700324 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600325
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600326 cmds += __adreno_add_idle_indirect_cmds(cmds,
327 device->mmu.setstate_memory.gpuaddr +
328 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
329
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600330 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600331 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
332 device->mmu.setstate_memory.gpuaddr +
333 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
334 else
335 cmds += adreno_add_bank_change_cmds(cmds,
336 KGSL_IOMMU_CONTEXT_USER,
337 device->mmu.setstate_memory.gpuaddr +
338 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
339
Tarun Karra9c070822012-11-27 16:43:51 -0700340 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
341
342 /* Acquire GPU-CPU sync Lock here */
343 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
344
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700345 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
346 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600347 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600348 /*
349 * We need to perfrom the following operations for all
350 * IOMMU units
351 */
352 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700353 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
354 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600355 /*
356 * Set address of the new pagetable by writng to IOMMU
357 * TTBR0 register
358 */
359 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700360 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
361 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600362 *cmds++ = reg_pt_val;
363 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
364 *cmds++ = 0x00000000;
365
366 /*
367 * Read back the ttbr0 register as a barrier to ensure
368 * above writes have completed
369 */
370 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700371 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
372 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600373 reg_pt_val,
374 device->mmu.setstate_memory.gpuaddr +
375 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600376 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600377 }
378 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
379 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700380 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600381 */
382 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700383 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
384 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700385
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600386 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700387 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
388 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700389 KGSL_IOMMU_CTX_TLBIALL);
390 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600391
392 cmds += __adreno_add_idle_indirect_cmds(cmds,
393 device->mmu.setstate_memory.gpuaddr +
394 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
395
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600396 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700397 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
398 KGSL_IOMMU_CONTEXT_USER,
399 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700400 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600401 device->mmu.setstate_memory.gpuaddr +
402 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
403 }
404 }
405
Tarun Karra9c070822012-11-27 16:43:51 -0700406 /* Release GPU-CPU sync Lock here */
407 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
408
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600409 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600410 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700411 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
412 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600413 device->mmu.setstate_memory.gpuaddr +
414 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
415 else
416 cmds += adreno_add_bank_change_cmds(cmds,
417 KGSL_IOMMU_CONTEXT_PRIV,
418 device->mmu.setstate_memory.gpuaddr +
419 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
420
Tarun Karra9c070822012-11-27 16:43:51 -0700421 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
422
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600423 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600424 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700425 /* invalidate all base pointers */
426 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
427 *cmds++ = 0x7fff;
428 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600429 /* This returns the per context timestamp but we need to
430 * use the global timestamp for iommu clock disablement */
431 adreno_ringbuffer_issuecmds(device, adreno_ctx,
432 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600433 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600434 kgsl_mmu_disable_clk_on_ts(&device->mmu,
435 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600436 }
Tarun Karra9c070822012-11-27 16:43:51 -0700437
438 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
439 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
440 BUG();
441 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600442}
443
444static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600445 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600446 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447{
448 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
449 unsigned int link[32];
450 unsigned int *cmds = &link[0];
451 int sizedwords = 0;
452 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600453 struct kgsl_context *context;
454 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600456 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530457 * Fix target freeze issue by adding TLB flush for each submit
458 * on A20X based targets.
459 */
460 if (adreno_is_a20x(adreno_dev))
461 flags |= KGSL_MMUFLAGS_TLBFLUSH;
462 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600463 * If possible, then set the state via the command stream to avoid
464 * a CPU idle. Otherwise, use the default setstate which uses register
465 * writes For CFF dump we must idle and use the registers so that it is
466 * easier to filter out the mmu accesses from the dump
467 */
468 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600469 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700470 if (context == NULL)
471 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600472 adreno_ctx = context->devctxt;
473
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
475 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600476 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477 *cmds++ = 0x00000000;
478
479 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600480 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700481 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600482 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483 sizedwords += 4;
484 }
485
486 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
487 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600488 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489 1);
490 *cmds++ = 0x00000000;
491 sizedwords += 2;
492 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600493 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494 *cmds++ = mh_mmu_invalidate;
495 sizedwords += 2;
496 }
497
498 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600499 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 /* HW workaround: to resolve MMU page fault interrupts
501 * caused by the VGT.It prevents the CP PFP from filling
502 * the VGT DMA request fifo too early,thereby ensuring
503 * that the VGT will not fetch vertex/bin data until
504 * after the page table base register has been updated.
505 *
506 * Two null DRAW_INDX_BIN packets are inserted right
507 * after the page table base update, followed by a
508 * wait for idle. The null packets will fill up the
509 * VGT DMA request fifo and prevent any further
510 * vertex/bin updates from occurring until the wait
511 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600512 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 *cmds++ = (0x4 << 16) |
514 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
515 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600516 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600517 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600518 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 *cmds++ = 0; /* viz query info */
520 *cmds++ = 0x0003C004; /* draw indicator */
521 *cmds++ = 0; /* bin base */
522 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600523 *cmds++ =
524 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600526 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 *cmds++ = 0; /* viz query info */
528 *cmds++ = 0x0003C004; /* draw indicator */
529 *cmds++ = 0; /* bin base */
530 *cmds++ = 3; /* bin size */
531 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600532 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600534 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 *cmds++ = 0x00000000;
536 sizedwords += 21;
537 }
538
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600539
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600541 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542 *cmds++ = 0x7fff; /* invalidate all base pointers */
543 sizedwords += 2;
544 }
545
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600546 adreno_ringbuffer_issuecmds(device, adreno_ctx,
547 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600549 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600550 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600551 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552}
553
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600554static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600555 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600556 uint32_t flags)
557{
558 /* call the mmu specific handler */
559 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600560 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600561 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600562 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600563}
564
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700566a3xx_getchipid(struct kgsl_device *device)
567{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600568 struct kgsl_device_platform_data *pdata =
569 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700570
Jordan Crouse54154c62012-03-27 16:33:26 -0600571 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600572 * All current A3XX chipids are detected at the SOC level. Leave this
573 * function here to support any future GPUs that have working
574 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600575 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700576
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600577 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700578}
579
580static unsigned int
581a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582{
583 unsigned int chipid = 0;
584 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600585 struct kgsl_device_platform_data *pdata =
586 kgsl_device_get_drvdata(device);
587
588 /* If the chip id is set at the platform level, then just use that */
589
590 if (pdata->chipid != 0)
591 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592
593 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
594 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
595 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
596
597 /*
598 * adreno 22x gpus are indicated by coreid 2,
599 * but REG_RBBM_PERIPHID1 always contains 0 for this field
600 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600601 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 chipid = 2 << 24;
603 else
604 chipid = (coreid & 0xF) << 24;
605
606 chipid |= ((majorid >> 4) & 0xF) << 16;
607
608 minorid = ((revid >> 0) & 0xFF);
609
610 patchid = ((revid >> 16) & 0xFF);
611
612 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530613 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 if (cpu_is_qsd8x50())
615 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530616 else if (cpu_is_msm8625() && minorid == 0)
617 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618
619 chipid |= (minorid << 8) | patchid;
620
621 return chipid;
622}
623
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700624static unsigned int
625adreno_getchipid(struct kgsl_device *device)
626{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600627 struct kgsl_device_platform_data *pdata =
628 kgsl_device_get_drvdata(device);
629
630 /*
631 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
632 * an A2XX processor
633 */
634
635 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700636 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600637 else
638 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700639}
640
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641static inline bool _rev_match(unsigned int id, unsigned int entry)
642{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600643 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645
646static void
647adreno_identify_gpu(struct adreno_device *adreno_dev)
648{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600649 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650
651 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
652
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600653 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
654 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
655 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
656 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657
Jordan Crouse505df9c2011-07-28 08:37:59 -0600658 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
659 if (core == adreno_gpulist[i].core &&
660 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600661 _rev_match(minor, adreno_gpulist[i].minor) &&
662 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 }
665
Jordan Crouse505df9c2011-07-28 08:37:59 -0600666 if (i == ARRAY_SIZE(adreno_gpulist)) {
667 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
668 return;
669 }
670
671 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
672 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
673 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
674 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700675 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
676 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700677 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600678 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Tarun Karra9c070822012-11-27 16:43:51 -0700679 adreno_dev->gpulist_index = i;
680
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681}
682
Lokesh Batra805e1e12012-08-03 08:34:06 -0600683static struct platform_device_id adreno_id_table[] = {
684 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
685 {},
686};
687
688MODULE_DEVICE_TABLE(platform, adreno_id_table);
689
690static struct of_device_id adreno_match_table[] = {
691 { .compatible = "qcom,kgsl-3d0", },
692 {}
693};
694
695static inline int adreno_of_read_property(struct device_node *node,
696 const char *prop, unsigned int *ptr)
697{
698 int ret = of_property_read_u32(node, prop, ptr);
699 if (ret)
700 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
701 return ret;
702}
703
704static struct device_node *adreno_of_find_subnode(struct device_node *parent,
705 const char *name)
706{
707 struct device_node *child;
708
709 for_each_child_of_node(parent, child) {
710 if (of_device_is_compatible(child, name))
711 return child;
712 }
713
714 return NULL;
715}
716
717static int adreno_of_get_pwrlevels(struct device_node *parent,
718 struct kgsl_device_platform_data *pdata)
719{
720 struct device_node *node, *child;
721 int ret = -EINVAL;
722
723 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
724
725 if (node == NULL) {
726 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
727 return -EINVAL;
728 }
729
730 pdata->num_levels = 0;
731
732 for_each_child_of_node(node, child) {
733 unsigned int index;
734 struct kgsl_pwrlevel *level;
735
736 if (adreno_of_read_property(child, "reg", &index))
737 goto done;
738
739 if (index >= KGSL_MAX_PWRLEVELS) {
740 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
741 index);
742 continue;
743 }
744
745 if (index >= pdata->num_levels)
746 pdata->num_levels = index + 1;
747
748 level = &pdata->pwrlevel[index];
749
750 if (adreno_of_read_property(child, "qcom,gpu-freq",
751 &level->gpu_freq))
752 goto done;
753
754 if (adreno_of_read_property(child, "qcom,bus-freq",
755 &level->bus_freq))
756 goto done;
757
758 if (adreno_of_read_property(child, "qcom,io-fraction",
759 &level->io_fraction))
760 level->io_fraction = 0;
761 }
762
763 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
764 &pdata->init_level))
765 pdata->init_level = 1;
766
767 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
768 KGSL_CORE_ERR("Initial power level out of range\n");
769 pdata->init_level = 1;
770 }
771
772 ret = 0;
773done:
774 return ret;
775
776}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600777
778static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
779{
780 struct device_node *node, *child;
781 struct msm_dcvs_core_info *info = NULL;
782 int count = 0;
783 int ret = -EINVAL;
784
785 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
786 if (node == NULL)
787 return ERR_PTR(-EINVAL);
788
789 info = kzalloc(sizeof(*info), GFP_KERNEL);
790
791 if (info == NULL) {
792 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
793 ret = -ENOMEM;
794 goto err;
795 }
796
797 for_each_child_of_node(node, child)
798 count++;
799
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700800 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600801
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700802 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600803 sizeof(struct msm_dcvs_freq_entry),
804 GFP_KERNEL);
805
806 if (info->freq_tbl == NULL) {
807 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700808 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600809 sizeof(struct msm_dcvs_freq_entry));
810 ret = -ENOMEM;
811 goto err;
812 }
813
814 for_each_child_of_node(node, child) {
815 unsigned int index;
816
817 if (adreno_of_read_property(child, "reg", &index))
818 goto err;
819
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700820 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600821 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
822 index);
823 continue;
824 }
825
826 if (adreno_of_read_property(child, "qcom,freq",
827 &info->freq_tbl[index].freq))
828 goto err;
829
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700830 if (adreno_of_read_property(child, "qcom,voltage",
831 &info->freq_tbl[index].voltage))
832 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600833
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700834 if (adreno_of_read_property(child, "qcom,is_trans_level",
835 &info->freq_tbl[index].is_trans_level))
836 info->freq_tbl[index].is_trans_level = 0;
837
838 if (adreno_of_read_property(child, "qcom,active-energy-offset",
839 &info->freq_tbl[index].active_energy_offset))
840 info->freq_tbl[index].active_energy_offset = 0;
841
842 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
843 &info->freq_tbl[index].leakage_energy_offset))
844 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600845 }
846
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700847 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
848 goto err;
849
850 info->sensors = kzalloc(info->num_cores *
851 sizeof(int),
852 GFP_KERNEL);
853
854 for (count = 0; count < info->num_cores; count++) {
855 if (adreno_of_read_property(node, "qcom,sensors",
856 &(info->sensors[count])))
857 goto err;
858 }
859
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700860 if (adreno_of_read_property(node, "qcom,core-core-type",
861 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600862 goto err;
863
864 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
865 &info->algo_param.disable_pc_threshold))
866 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700867 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
868 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600869 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700870 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
871 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600872 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600873 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
874 &info->algo_param.em_max_util_pct))
875 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700876 if (adreno_of_read_property(node, "qcom,algo-group-id",
877 &info->algo_param.group_id))
878 goto err;
879 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
880 &info->algo_param.max_freq_chg_time_us))
881 goto err;
882 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
883 &info->algo_param.slack_mode_dynamic))
884 goto err;
885 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
886 &info->algo_param.slack_weight_thresh_pct))
887 goto err;
888 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
889 &info->algo_param.slack_time_min_us))
890 goto err;
891 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
892 &info->algo_param.slack_time_max_us))
893 goto err;
894 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
895 &info->algo_param.ss_win_size_min_us))
896 goto err;
897 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
898 &info->algo_param.ss_win_size_max_us))
899 goto err;
900 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
901 &info->algo_param.ss_util_pct))
902 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800903 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
904 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600905 goto err;
906
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700907 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
908 &info->energy_coeffs.active_coeff_a))
909 goto err;
910 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
911 &info->energy_coeffs.active_coeff_b))
912 goto err;
913 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
914 &info->energy_coeffs.active_coeff_c))
915 goto err;
916 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
917 &info->energy_coeffs.leakage_coeff_a))
918 goto err;
919 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
920 &info->energy_coeffs.leakage_coeff_b))
921 goto err;
922 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
923 &info->energy_coeffs.leakage_coeff_c))
924 goto err;
925 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
926 &info->energy_coeffs.leakage_coeff_d))
927 goto err;
928
929 if (adreno_of_read_property(node, "qcom,power-current-temp",
930 &info->power_param.current_temp))
931 goto err;
932
Lokesh Batra805e1e12012-08-03 08:34:06 -0600933 return info;
934
935err:
936 if (info)
937 kfree(info->freq_tbl);
938
939 kfree(info);
940
941 return ERR_PTR(ret);
942}
943
944static int adreno_of_get_iommu(struct device_node *parent,
945 struct kgsl_device_platform_data *pdata)
946{
947 struct device_node *node, *child;
948 struct kgsl_device_iommu_data *data = NULL;
949 struct kgsl_iommu_ctx *ctxs = NULL;
950 u32 reg_val[2];
951 int ctx_index = 0;
952
953 node = of_parse_phandle(parent, "iommu", 0);
954 if (node == NULL)
955 return -EINVAL;
956
957 data = kzalloc(sizeof(*data), GFP_KERNEL);
958 if (data == NULL) {
959 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
960 goto err;
961 }
962
963 if (of_property_read_u32_array(node, "reg", reg_val, 2))
964 goto err;
965
966 data->physstart = reg_val[0];
967 data->physend = data->physstart + reg_val[1] - 1;
968
969 data->iommu_ctx_count = 0;
970
971 for_each_child_of_node(node, child)
972 data->iommu_ctx_count++;
973
974 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
975 GFP_KERNEL);
976
977 if (ctxs == NULL) {
978 KGSL_CORE_ERR("kzalloc(%d) failed\n",
979 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
980 goto err;
981 }
982
983 for_each_child_of_node(node, child) {
984 int ret = of_property_read_string(child, "label",
985 &ctxs[ctx_index].iommu_ctx_name);
986
987 if (ret) {
988 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
989 goto err;
990 }
991
992 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
993 &ctxs[ctx_index].ctx_id))
994 goto err;
995
996 ctx_index++;
997 }
998
999 data->iommu_ctxs = ctxs;
1000
1001 pdata->iommu_data = data;
1002 pdata->iommu_count = 1;
1003
1004 return 0;
1005
1006err:
1007 kfree(ctxs);
1008 kfree(data);
1009
1010 return -EINVAL;
1011}
1012
1013static int adreno_of_get_pdata(struct platform_device *pdev)
1014{
1015 struct kgsl_device_platform_data *pdata = NULL;
1016 struct kgsl_device *device;
1017 int ret = -EINVAL;
1018
1019 pdev->id_entry = adreno_id_table;
1020
1021 pdata = pdev->dev.platform_data;
1022 if (pdata)
1023 return 0;
1024
1025 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1026 KGSL_CORE_ERR("Unable to read 'label'\n");
1027 goto err;
1028 }
1029
1030 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1031 goto err;
1032
1033 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1034 if (pdata == NULL) {
1035 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1036 ret = -ENOMEM;
1037 goto err;
1038 }
1039
1040 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1041 &pdata->chipid))
1042 goto err;
1043
1044 /* pwrlevel Data */
1045 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1046 if (ret)
1047 goto err;
1048
1049 /* Default value is 83, if not found in DT */
1050 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1051 &pdata->idle_timeout))
1052 pdata->idle_timeout = 83;
1053
1054 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1055 &pdata->nap_allowed))
1056 pdata->nap_allowed = 1;
1057
1058 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1059 &pdata->clk_map))
1060 goto err;
1061
1062 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1063
1064 if (device->id != KGSL_DEVICE_3D0)
1065 goto err;
1066
1067 /* Bus Scale Data */
1068
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001069 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001070 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1071 ret = PTR_ERR(pdata->bus_scale_table);
1072 goto err;
1073 }
1074
1075 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1076 if (IS_ERR_OR_NULL(pdata->core_info)) {
1077 ret = PTR_ERR(pdata->core_info);
1078 goto err;
1079 }
1080
1081 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1082 if (ret)
1083 goto err;
1084
1085 pdev->dev.platform_data = pdata;
1086 return 0;
1087
1088err:
1089 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001090 if (pdata->core_info)
1091 kfree(pdata->core_info->freq_tbl);
1092 kfree(pdata->core_info);
1093
1094 if (pdata->iommu_data)
1095 kfree(pdata->iommu_data->iommu_ctxs);
1096
1097 kfree(pdata->iommu_data);
1098 }
1099
1100 kfree(pdata);
1101
1102 return ret;
1103}
1104
liu zhong7dfa2a32012-04-27 19:11:01 -07001105#ifdef CONFIG_MSM_OCMEM
1106static int
1107adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1108{
Jordan Crousec0978202012-08-29 14:35:51 -06001109 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001110 return 0;
1111
1112 /* OCMEM is only needed once, do not support consective allocation */
1113 if (adreno_dev->ocmem_hdl != NULL)
1114 return 0;
1115
1116 adreno_dev->ocmem_hdl =
1117 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1118 if (adreno_dev->ocmem_hdl == NULL)
1119 return -ENOMEM;
1120
1121 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001122 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001123
1124 return 0;
1125}
1126
1127static void
1128adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1129{
Jordan Crousec0978202012-08-29 14:35:51 -06001130 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001131 return;
1132
1133 if (adreno_dev->ocmem_hdl == NULL)
1134 return;
1135
1136 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1137 adreno_dev->ocmem_hdl = NULL;
1138}
1139#else
1140static int
1141adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1142{
1143 return 0;
1144}
1145
1146static void
1147adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1148{
1149}
1150#endif
1151
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152static int __devinit
1153adreno_probe(struct platform_device *pdev)
1154{
1155 struct kgsl_device *device;
1156 struct adreno_device *adreno_dev;
1157 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001158 bool is_dt;
1159
1160 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1161
1162 if (is_dt && pdev->dev.of_node) {
1163 status = adreno_of_get_pdata(pdev);
1164 if (status)
1165 goto error_return;
1166 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167
1168 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1169 adreno_dev = ADRENO_DEVICE(device);
1170 device->parentdev = &pdev->dev;
1171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172 status = adreno_ringbuffer_init(device);
1173 if (status != 0)
1174 goto error;
1175
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001176 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 if (status)
1178 goto error_close_rb;
1179
1180 adreno_debugfs_init(device);
1181
1182 kgsl_pwrscale_init(device);
1183 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1184
1185 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1186 return 0;
1187
1188error_close_rb:
1189 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1190error:
1191 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001192error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193 return status;
1194}
1195
1196static int __devexit adreno_remove(struct platform_device *pdev)
1197{
1198 struct kgsl_device *device;
1199 struct adreno_device *adreno_dev;
1200
1201 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1202 adreno_dev = ADRENO_DEVICE(device);
1203
1204 kgsl_pwrscale_detach_policy(device);
1205 kgsl_pwrscale_close(device);
1206
1207 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1208 kgsl_device_platform_remove(device);
1209
1210 return 0;
1211}
1212
1213static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1214{
1215 int status = -EINVAL;
1216 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001217
Tarun Karrad20d71a2013-01-25 15:38:57 -08001218 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001219 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220
1221 /* Power up the device */
1222 kgsl_pwrctrl_enable(device);
1223
1224 /* Identify the specific GPU */
1225 adreno_identify_gpu(adreno_dev);
1226
Tarun Karra9c070822012-11-27 16:43:51 -07001227 if (adreno_ringbuffer_read_pm4_ucode(device)) {
1228 KGSL_DRV_ERR(device, "Reading pm4 microcode failed %s\n",
1229 adreno_dev->pm4_fwfile);
1230 BUG_ON(1);
1231 }
1232
1233 if (adreno_ringbuffer_read_pfp_ucode(device)) {
1234 KGSL_DRV_ERR(device, "Reading pfp microcode failed %s\n",
1235 adreno_dev->pfp_fwfile);
1236 BUG_ON(1);
1237 }
1238
Jordan Crouse505df9c2011-07-28 08:37:59 -06001239 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1240 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1241 adreno_dev->chip_id);
1242 goto error_clk_off;
1243 }
1244
Tarun Karra9c070822012-11-27 16:43:51 -07001245
1246 /*
1247 * Check if firmware supports the sync lock PM4 packets needed
1248 * for IOMMUv1
1249 */
1250
1251 if ((adreno_dev->pm4_fw_version >=
1252 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pm4_ver) &&
1253 (adreno_dev->pfp_fw_version >=
1254 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pfp_ver))
1255 device->mmu.flags |= KGSL_MMU_FLAGS_IOMMU_SYNC;
1256
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001257 /* Set up the MMU */
1258 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001259 /*
1260 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1261 * on older gpus
1262 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001263 if (adreno_is_a20x(adreno_dev)) {
1264 device->mh.mh_intf_cfg1 = 0;
1265 device->mh.mh_intf_cfg2 = 0;
1266 }
1267
1268 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001269 }
1270
Tarun Karra3335f142012-06-19 14:11:48 -07001271 /* Assign correct RBBM status register to hang detect regs
1272 */
Tarun Karra696f89e2013-01-27 21:31:40 -08001273 ft_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
Tarun Karra3335f142012-06-19 14:11:48 -07001274
Jordan Crouseb5c80482012-10-03 09:38:41 -06001275 /* Add A3XX specific registers for hang detection */
1276 if (adreno_is_a3xx(adreno_dev)) {
Tarun Karra696f89e2013-01-27 21:31:40 -08001277 ft_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1278 ft_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
1279 ft_detect_regs[8] = A3XX_RBBM_PERFCTR_SP_6_LO;
1280 ft_detect_regs[9] = A3XX_RBBM_PERFCTR_SP_6_HI;
1281 ft_detect_regs[10] = A3XX_RBBM_PERFCTR_SP_5_LO;
1282 ft_detect_regs[11] = A3XX_RBBM_PERFCTR_SP_5_HI;
Jordan Crouseb5c80482012-10-03 09:38:41 -06001283 }
1284
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001285 status = kgsl_mmu_start(device);
1286 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001287 goto error_clk_off;
1288
liu zhong7dfa2a32012-04-27 19:11:01 -07001289 status = adreno_ocmem_gmem_malloc(adreno_dev);
1290 if (status) {
1291 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1292 goto error_mmu_off;
1293 }
1294
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001295 /* Start the GPU */
1296 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297
1298 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001299 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001300
1301 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001302 if (status == 0) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001303 /* While fault tolerance is on we do not want timer to
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001304 * fire and attempt to change any device state */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001305 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001306 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001307 return 0;
1308 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001311
1312error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001313 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001314
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315error_clk_off:
1316 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317
1318 return status;
1319}
1320
1321static int adreno_stop(struct kgsl_device *device)
1322{
1323 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1324
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325 adreno_dev->drawctxt_active = NULL;
1326
1327 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1328
Shubhraprakash Das79447952012-04-26 18:12:23 -06001329 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001331 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301332 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001333 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001334
liu zhong7dfa2a32012-04-27 19:11:01 -07001335 adreno_ocmem_gmem_free(adreno_dev);
1336
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337 /* Power down the device */
1338 kgsl_pwrctrl_disable(device);
1339
1340 return 0;
1341}
1342
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001343static void adreno_mark_context_status(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001344 int ft_status)
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001345{
1346 struct kgsl_context *context;
1347 int next = 0;
1348 /*
1349 * Set the reset status of all contexts to
1350 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
Tarun Karrad20d71a2013-01-25 15:38:57 -08001351 * since thats the guilty party, if fault tolerance failed then
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001352 * mark all as guilty
1353 */
1354 while ((context = idr_get_next(&device->context_idr, &next))) {
1355 struct adreno_context *adreno_context = context->devctxt;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001356 if (ft_status) {
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001357 context->reset_status =
1358 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1359 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1360 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1361 context->reset_status) {
Carter Cooper8179f5a2012-12-17 11:32:27 -07001362 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG |
Tarun Karrad20d71a2013-01-25 15:38:57 -08001363 CTXT_FLAGS_GPU_HANG_FT))
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001364 context->reset_status =
1365 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1366 else
1367 context->reset_status =
1368 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1369 }
1370 next = next + 1;
1371 }
1372}
1373
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001374static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1375{
1376 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1377 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1378 struct kgsl_context *context;
1379 struct adreno_context *temp_adreno_context;
1380 int next = 0;
1381
1382 while ((context = idr_get_next(&device->context_idr, &next))) {
1383 temp_adreno_context = context->devctxt;
1384 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1385 kgsl_sharedmem_writel(&device->memstore,
1386 KGSL_MEMSTORE_OFFSET(context->id,
1387 soptimestamp),
1388 rb->timestamp[context->id]);
1389 kgsl_sharedmem_writel(&device->memstore,
1390 KGSL_MEMSTORE_OFFSET(context->id,
1391 eoptimestamp),
1392 rb->timestamp[context->id]);
1393 }
1394 next = next + 1;
1395 }
1396}
1397
Tarun Karrad20d71a2013-01-25 15:38:57 -08001398static void adreno_destroy_ft_data(struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001399{
Tarun Karrad20d71a2013-01-25 15:38:57 -08001400 vfree(ft_data->rb_buffer);
1401 vfree(ft_data->bad_rb_buffer);
1402 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001403}
1404
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001405static int _find_start_of_cmd_seq(struct adreno_ringbuffer *rb,
1406 unsigned int *ptr,
1407 bool inc)
1408{
1409 int status = -EINVAL;
1410 unsigned int val1;
1411 unsigned int size = rb->buffer_desc.size;
1412 unsigned int start_ptr = *ptr;
1413
1414 while ((start_ptr / sizeof(unsigned int)) != rb->wptr) {
1415 if (inc)
1416 start_ptr = adreno_ringbuffer_inc_wrapped(start_ptr,
1417 size);
1418 else
1419 start_ptr = adreno_ringbuffer_dec_wrapped(start_ptr,
1420 size);
1421 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, start_ptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001422 /* Ensure above read is finished before next read */
1423 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001424 if (KGSL_CMD_IDENTIFIER == val1) {
1425 if ((start_ptr / sizeof(unsigned int)) != rb->wptr)
1426 start_ptr = adreno_ringbuffer_dec_wrapped(
1427 start_ptr, size);
1428 *ptr = start_ptr;
1429 status = 0;
1430 break;
1431 }
1432 }
1433 return status;
1434}
1435
1436static int _find_cmd_seq_after_eop_ts(struct adreno_ringbuffer *rb,
1437 unsigned int *rb_rptr,
1438 unsigned int global_eop,
1439 bool inc)
1440{
1441 int status = -EINVAL;
1442 unsigned int temp_rb_rptr = *rb_rptr;
1443 unsigned int size = rb->buffer_desc.size;
1444 unsigned int val[3];
1445 int i = 0;
1446 bool check = false;
1447
1448 if (inc && temp_rb_rptr / sizeof(unsigned int) != rb->wptr)
1449 return status;
1450
1451 do {
1452 /*
1453 * when decrementing we need to decrement first and
1454 * then read make sure we cover all the data
1455 */
1456 if (!inc)
1457 temp_rb_rptr = adreno_ringbuffer_dec_wrapped(
1458 temp_rb_rptr, size);
1459 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i],
1460 temp_rb_rptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001461 /* Ensure above read is finished before next read */
1462 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001463
1464 if (check && ((inc && val[i] == global_eop) ||
1465 (!inc && (val[i] ==
1466 cp_type3_packet(CP_MEM_WRITE, 2) ||
1467 val[i] == CACHE_FLUSH_TS)))) {
1468 /* decrement i, i.e i = (i - 1 + 3) % 3 if
1469 * we are going forward, else increment i */
1470 i = (i + 2) % 3;
1471 if (val[i] == rb->device->memstore.gpuaddr +
1472 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1473 eoptimestamp)) {
1474 int j = ((i + 2) % 3);
1475 if ((inc && (val[j] == CACHE_FLUSH_TS ||
1476 val[j] == cp_type3_packet(
1477 CP_MEM_WRITE, 2))) ||
1478 (!inc && val[j] == global_eop)) {
1479 /* Found the global eop */
1480 status = 0;
1481 break;
1482 }
1483 }
1484 /* if no match found then increment i again
1485 * since we decremented before matching */
1486 i = (i + 1) % 3;
1487 }
1488 if (inc)
1489 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(
1490 temp_rb_rptr, size);
1491
1492 i = (i + 1) % 3;
1493 if (2 == i)
1494 check = true;
1495 } while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr);
1496 /* temp_rb_rptr points to the command stream after global eop,
1497 * move backward till the start of command sequence */
1498 if (!status) {
1499 status = _find_start_of_cmd_seq(rb, &temp_rb_rptr, false);
1500 if (!status) {
1501 *rb_rptr = temp_rb_rptr;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001502 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001503 "Offset of cmd sequence after eop timestamp: 0x%x\n",
1504 temp_rb_rptr / sizeof(unsigned int));
1505 }
1506 }
1507 if (status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001508 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001509 "Failed to find the command sequence after eop timestamp\n");
1510 return status;
1511}
1512
1513static int _find_hanging_ib_sequence(struct adreno_ringbuffer *rb,
1514 unsigned int *rb_rptr,
1515 unsigned int ib1)
1516{
1517 int status = -EINVAL;
1518 unsigned int temp_rb_rptr = *rb_rptr;
1519 unsigned int size = rb->buffer_desc.size;
1520 unsigned int val[2];
1521 int i = 0;
1522 bool check = false;
1523 bool ctx_switch = false;
1524
1525 while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr) {
1526 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i], temp_rb_rptr);
Tarun Karra782734f2013-03-27 20:21:02 -07001527 /* Ensure above read is finished before next read */
1528 rmb();
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001529
1530 if (check && val[i] == ib1) {
1531 /* decrement i, i.e i = (i - 1 + 2) % 2 */
1532 i = (i + 1) % 2;
1533 if (adreno_cmd_is_ib(val[i])) {
1534 /* go till start of command sequence */
1535 status = _find_start_of_cmd_seq(rb,
1536 &temp_rb_rptr, false);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001537
1538 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001539 "Found the hanging IB at offset 0x%x\n",
1540 temp_rb_rptr / sizeof(unsigned int));
1541 break;
1542 }
1543 /* if no match the increment i since we decremented
1544 * before checking */
1545 i = (i + 1) % 2;
1546 }
1547 /* Make sure you do not encounter a context switch twice, we can
1548 * encounter it once for the bad context as the start of search
1549 * can point to the context switch */
1550 if (val[i] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
1551 if (ctx_switch) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001552 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001553 "Context switch encountered before bad "
1554 "IB found\n");
1555 break;
1556 }
1557 ctx_switch = true;
1558 }
1559 i = (i + 1) % 2;
1560 if (1 == i)
1561 check = true;
1562 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(temp_rb_rptr,
1563 size);
1564 }
1565 if (!status)
1566 *rb_rptr = temp_rb_rptr;
1567 return status;
1568}
1569
Tarun Karra59e79542013-04-10 10:25:25 -07001570static void adreno_setup_ft_data(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001571 struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001572{
1573 int ret = 0;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001574 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1575 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001576 struct kgsl_context *context;
1577 struct adreno_context *adreno_context;
1578 unsigned int rb_rptr = rb->wptr * sizeof(unsigned int);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001579
Tarun Karrad20d71a2013-01-25 15:38:57 -08001580 memset(ft_data, 0, sizeof(*ft_data));
1581 ft_data->start_of_replay_cmds = 0xFFFFFFFF;
1582 ft_data->replay_for_snapshot = 0xFFFFFFFF;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001583
Tarun Karrad20d71a2013-01-25 15:38:57 -08001584 adreno_regread(device, REG_CP_IB1_BASE, &ft_data->ib1);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001585
Tarun Karrad20d71a2013-01-25 15:38:57 -08001586 kgsl_sharedmem_readl(&device->memstore, &ft_data->context_id,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001587 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1588 current_context));
1589
1590 kgsl_sharedmem_readl(&device->memstore,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001591 &ft_data->global_eop,
Tarun Karradeeecc02013-01-21 23:42:17 -08001592 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1593 eoptimestamp));
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001594
Tarun Karra782734f2013-03-27 20:21:02 -07001595 /* Ensure context id and global eop ts read complete */
1596 rmb();
1597
Tarun Karrad20d71a2013-01-25 15:38:57 -08001598 ft_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1599 if (!ft_data->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 }
1604
Tarun Karrad20d71a2013-01-25 15:38:57 -08001605 ft_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1606 if (!ft_data->bad_rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001607 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1608 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001609 return;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001610 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001611
Tarun Karrad20d71a2013-01-25 15:38:57 -08001612 ft_data->good_rb_buffer = vmalloc(rb->buffer_desc.size);
1613 if (!ft_data->good_rb_buffer) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001614 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1615 rb->buffer_desc.size);
Tarun Karra59e79542013-04-10 10:25:25 -07001616 return;
Tarun Karradeeecc02013-01-21 23:42:17 -08001617 }
Tarun Karra3164fb02013-02-05 15:38:51 -08001618
Tarun Karra59e79542013-04-10 10:25:25 -07001619 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001620
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001621 /* find the start of bad command sequence in rb */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001622 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001623 /* Look for the command stream that is right after the global eop */
1624
1625 if (!context) {
1626 /*
Tarun Karrad20d71a2013-01-25 15:38:57 -08001627 * If there is no context then fault tolerance does not need to
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001628 * replay anything, just reset GPU and thats it
1629 */
Tarun Karra59e79542013-04-10 10:25:25 -07001630 return;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001631 }
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001632
Tarun Karra59e79542013-04-10 10:25:25 -07001633 ft_data->ft_policy = adreno_dev->ft_policy;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001634
Tarun Karra3164fb02013-02-05 15:38:51 -08001635 if (!adreno_dev->ft_policy)
1636 adreno_dev->ft_policy = KGSL_FT_DEFAULT_POLICY;
1637
Tarun Karra59e79542013-04-10 10:25:25 -07001638 ret = _find_cmd_seq_after_eop_ts(rb, &rb_rptr,
1639 ft_data->global_eop + 1, false);
1640 if (ret) {
1641 ft_data->ft_policy |= KGSL_FT_TEMP_DISABLE;
1642 return;
1643 } else
1644 ft_data->ft_policy &= ~KGSL_FT_TEMP_DISABLE;
Tarun Karra3164fb02013-02-05 15:38:51 -08001645
Tarun Karra59e79542013-04-10 10:25:25 -07001646 ft_data->start_of_replay_cmds = rb_rptr;
Tarun Karra3164fb02013-02-05 15:38:51 -08001647
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001648 adreno_context = context->devctxt;
1649 if (adreno_context->flags & CTXT_FLAGS_PREAMBLE) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001650 if (ft_data->ib1) {
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001651 ret = _find_hanging_ib_sequence(rb,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001652 &rb_rptr, ft_data->ib1);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001653 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001654 KGSL_FT_ERR(device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001655 "Start not found for replay IB sequence\n");
Tarun Karra59e79542013-04-10 10:25:25 -07001656 return;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001657 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001658 ft_data->start_of_replay_cmds = rb_rptr;
1659 ft_data->replay_for_snapshot = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001660 }
1661 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001662}
1663
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001664static int
Tarun Karra696f89e2013-01-27 21:31:40 -08001665_adreno_check_long_ib(struct kgsl_device *device)
1666{
1667 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1668 unsigned int curr_global_ts = 0;
1669
1670 /* check if the global ts is still the same */
1671 kgsl_sharedmem_readl(&device->memstore,
1672 &curr_global_ts,
1673 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1674 eoptimestamp));
Tarun Karra782734f2013-03-27 20:21:02 -07001675 /* Ensure above read is finished before long ib check */
1676 rmb();
Tarun Karra696f89e2013-01-27 21:31:40 -08001677
1678 /* Mark long ib as handled */
1679 adreno_dev->long_ib = 0;
1680
1681 if (curr_global_ts == adreno_dev->long_ib_ts) {
1682 KGSL_FT_ERR(device,
1683 "IB ran too long, invalidate ctxt\n");
1684 return 1;
1685 } else {
1686 /* Do nothing GPU has gone ahead */
1687 KGSL_FT_INFO(device, "false long ib detection return\n");
1688 return 0;
1689 }
1690}
1691
1692static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001693_adreno_ft_restart_device(struct kgsl_device *device,
Tarun Karra59e79542013-04-10 10:25:25 -07001694 struct kgsl_context *context)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001695{
Tarun Karradeeecc02013-01-21 23:42:17 -08001696
1697 struct adreno_context *adreno_context = context->devctxt;
1698
1699 /* restart device */
1700 if (adreno_stop(device)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001701 KGSL_FT_ERR(device, "Device stop failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001702 return 1;
1703 }
1704
1705 if (adreno_start(device, true)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001706 KGSL_FT_ERR(device, "Device start failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001707 return 1;
1708 }
1709
1710 if (context)
1711 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1712 KGSL_MEMSTORE_GLOBAL);
1713
1714 /* If iommu is used then we need to make sure that the iommu clocks
1715 * are on since there could be commands in pipeline that touch iommu */
1716 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1717 if (kgsl_mmu_enable_clk(&device->mmu,
1718 KGSL_IOMMU_CONTEXT_USER))
1719 return 1;
1720 }
1721
1722 return 0;
1723}
1724
Tarun Karra696f89e2013-01-27 21:31:40 -08001725static inline void
1726_adreno_debug_ft_info(struct kgsl_device *device,
1727 struct adreno_ft_data *ft_data)
1728{
1729
1730 /*
1731 * Dumping rb is a very useful tool to debug FT.
1732 * It will tell us if we are extracting the rb correctly
1733 * NOP'ing the right IB, skipping the EOF correctly etc.
1734 */
1735 if (device->ft_log >= 7) {
1736
1737 /* Print fault tolerance data here */
1738 KGSL_FT_INFO(device, "Temp RB buffer size 0x%X\n",
1739 ft_data->rb_size);
1740 adreno_dump_rb(device, ft_data->rb_buffer,
1741 ft_data->rb_size<<2, 0, ft_data->rb_size);
1742
1743 KGSL_FT_INFO(device, "Bad RB buffer size 0x%X\n",
1744 ft_data->bad_rb_size);
1745 adreno_dump_rb(device, ft_data->bad_rb_buffer,
1746 ft_data->bad_rb_size<<2, 0, ft_data->bad_rb_size);
1747
1748 KGSL_FT_INFO(device, "Good RB buffer size 0x%X\n",
1749 ft_data->good_rb_size);
1750 adreno_dump_rb(device, ft_data->good_rb_buffer,
1751 ft_data->good_rb_size<<2, 0, ft_data->good_rb_size);
1752
1753 }
1754}
1755
Tarun Karradeeecc02013-01-21 23:42:17 -08001756static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001757_adreno_ft_resubmit_rb(struct kgsl_device *device,
Tarun Karradeeecc02013-01-21 23:42:17 -08001758 struct adreno_ringbuffer *rb,
1759 struct kgsl_context *context,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001760 struct adreno_ft_data *ft_data,
Tarun Karradeeecc02013-01-21 23:42:17 -08001761 unsigned int *buff, unsigned int size)
1762{
1763 unsigned int ret = 0;
Tarun Karra59e79542013-04-10 10:25:25 -07001764 unsigned int retry_num = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001765
Tarun Karra696f89e2013-01-27 21:31:40 -08001766 _adreno_debug_ft_info(device, ft_data);
1767
Tarun Karra59e79542013-04-10 10:25:25 -07001768 do {
1769 ret = _adreno_ft_restart_device(device, context);
1770 if (ret == 0)
1771 break;
1772 /*
1773 * If device restart fails sleep for 20ms before
1774 * attempting restart. This allows GPU HW to settle
1775 * and improve the chances of next restart to be
1776 * successful.
1777 */
1778 msleep(20);
1779 KGSL_FT_ERR(device, "Retry device restart %d\n", retry_num);
1780 retry_num++;
1781 } while (retry_num < 4);
1782
1783 if (ret) {
1784 KGSL_FT_ERR(device, "Device restart failed\n");
1785 BUG_ON(1);
1786 goto done;
1787 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001788
1789 if (size) {
1790
1791 /* submit commands and wait for them to pass */
1792 adreno_ringbuffer_restore(rb, buff, size);
1793
1794 ret = adreno_idle(device);
1795 }
1796
Tarun Karra59e79542013-04-10 10:25:25 -07001797done:
Tarun Karradeeecc02013-01-21 23:42:17 -08001798 return ret;
1799}
1800
Tarun Karradeeecc02013-01-21 23:42:17 -08001801static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001802_adreno_ft(struct kgsl_device *device,
1803 struct adreno_ft_data *ft_data)
Tarun Karradeeecc02013-01-21 23:42:17 -08001804{
1805 int ret = 0, i;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001806 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1807 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1808 struct kgsl_context *context;
1809 struct adreno_context *adreno_context = NULL;
1810 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
Tarun Karra59e79542013-04-10 10:25:25 -07001811 unsigned int long_ib = 0;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001812
Tarun Karrad20d71a2013-01-25 15:38:57 -08001813 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001814 if (context == NULL) {
Tarun Karra59e79542013-04-10 10:25:25 -07001815 KGSL_FT_ERR(device, "Last context unknown id:%d\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001816 ft_data->context_id);
Tarun Karra59e79542013-04-10 10:25:25 -07001817 goto play_good_cmds;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001818 } else {
1819 adreno_context = context->devctxt;
1820 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001821 /*
1822 * set the invalid ts flag to 0 for this context since we have
1823 * detected a hang for it
1824 */
1825 context->wait_on_invalid_ts = false;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001826
Tarun Karra59e79542013-04-10 10:25:25 -07001827 if (!(adreno_context->flags & CTXT_FLAGS_PER_CONTEXT_TS)) {
1828 ft_data->status = 1;
1829 KGSL_FT_ERR(device, "Fault tolerance not supported\n");
1830 goto play_good_cmds;
1831 }
1832
Tarun Karra83297222013-02-05 19:45:49 -08001833 /*
1834 * This flag will be set by userspace for contexts
1835 * that do not want to be fault tolerant (ex: OPENCL)
1836 */
1837 if (adreno_context->flags & CTXT_FLAGS_NO_FAULT_TOLERANCE) {
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001838 ft_data->status = 1;
Tarun Karra83297222013-02-05 19:45:49 -08001839 KGSL_FT_ERR(device,
1840 "No FT set for this context play good cmds\n");
1841 goto play_good_cmds;
1842 }
1843
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001844 }
1845
Tarun Karra59e79542013-04-10 10:25:25 -07001846 /* Check if we detected a long running IB,
1847 * if true do not attempt replay of bad cmds */
1848 if (adreno_dev->long_ib) {
1849 long_ib = _adreno_check_long_ib(device);
1850 if (!long_ib) {
1851 adreno_context->flags &= ~CTXT_FLAGS_GPU_HANG;
1852 return 0;
1853 }
1854 }
1855
Tarun Karra3164fb02013-02-05 15:38:51 -08001856 /*
1857 * Extract valid contents from rb which can still be executed after
1858 * hang
1859 */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001860 adreno_ringbuffer_extract(rb, ft_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001861
Tarun Karra59e79542013-04-10 10:25:25 -07001862 /* If long IB detected do not attempt replay of bad cmds */
1863 if (long_ib) {
1864 ft_data->status = 1;
1865 _adreno_debug_ft_info(device, ft_data);
1866 goto play_good_cmds;
1867 }
1868
1869 if ((ft_data->ft_policy & KGSL_FT_DISABLE) ||
1870 (ft_data->ft_policy & KGSL_FT_TEMP_DISABLE)) {
1871 KGSL_FT_ERR(device, "NO FT policy play only good cmds\n");
1872 ft_data->status = 1;
1873 goto play_good_cmds;
Tarun Karra696f89e2013-01-27 21:31:40 -08001874 }
1875
Tarun Karra24e3dfa2013-02-25 21:58:05 -08001876 /* Do not try the reply if hang is due to a pagefault */
1877 if (adreno_context->pagefault) {
1878 if ((ft_data->context_id == adreno_context->id) &&
1879 (ft_data->global_eop == adreno_context->pagefault_ts)) {
1880 ft_data->ft_policy &= ~KGSL_FT_REPLAY;
1881 KGSL_FT_ERR(device, "MMU fault skipping replay\n");
1882 }
1883
1884 adreno_context->pagefault = 0;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001885 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001886
Tarun Karra3164fb02013-02-05 15:38:51 -08001887 if (ft_data->ft_policy & KGSL_FT_REPLAY) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001888 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1889 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001890
Tarun Karra3164fb02013-02-05 15:38:51 -08001891 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001892 KGSL_FT_ERR(device, "Replay status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001893 ft_data->status = 1;
1894 } else
Tarun Karradeeecc02013-01-21 23:42:17 -08001895 goto play_good_cmds;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001896 }
1897
Tarun Karra3164fb02013-02-05 15:38:51 -08001898 if (ft_data->ft_policy & KGSL_FT_SKIPIB) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001899 for (i = 0; i < ft_data->bad_rb_size; i++) {
1900 if ((ft_data->bad_rb_buffer[i] ==
1901 CP_HDR_INDIRECT_BUFFER_PFD) &&
1902 (ft_data->bad_rb_buffer[i+1] == ft_data->ib1)) {
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001903
Tarun Karrad20d71a2013-01-25 15:38:57 -08001904 ft_data->bad_rb_buffer[i] = cp_nop_packet(2);
1905 ft_data->bad_rb_buffer[i+1] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001906 KGSL_NOP_IB_IDENTIFIER;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001907 ft_data->bad_rb_buffer[i+2] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001908 KGSL_NOP_IB_IDENTIFIER;
1909 break;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001910 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001911 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001912
Tarun Karrad20d71a2013-01-25 15:38:57 -08001913 if ((i == (ft_data->bad_rb_size)) || (!ft_data->ib1)) {
1914 KGSL_FT_ERR(device, "Bad IB to NOP not found\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001915 ft_data->status = 1;
Tarun Karradeeecc02013-01-21 23:42:17 -08001916 goto play_good_cmds;
1917 }
1918
Tarun Karrad20d71a2013-01-25 15:38:57 -08001919 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1920 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001921
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001922 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001923 KGSL_FT_ERR(device, "NOP faulty IB status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001924 ft_data->status = 1;
1925 } else {
1926 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001927 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001928 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001929 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001930
Tarun Karra3164fb02013-02-05 15:38:51 -08001931 if (ft_data->ft_policy & KGSL_FT_SKIPFRAME) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001932 for (i = 0; i < ft_data->bad_rb_size; i++) {
1933 if (ft_data->bad_rb_buffer[i] ==
1934 KGSL_END_OF_FRAME_IDENTIFIER) {
1935 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001936 break;
1937 }
1938 }
1939
1940 /* EOF not found in RB, discard till EOF in
1941 next IB submission */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001942 if (i == ft_data->bad_rb_size) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001943 adreno_context->flags |= CTXT_FLAGS_SKIP_EOF;
Tarun Karra3164fb02013-02-05 15:38:51 -08001944 KGSL_FT_INFO(device,
1945 "EOF not found in RB, skip next issueib till EOF\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08001946 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001947 }
1948
Tarun Karrad20d71a2013-01-25 15:38:57 -08001949 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1950 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001951
1952 if (ret) {
Tarun Karra59e79542013-04-10 10:25:25 -07001953 KGSL_FT_ERR(device, "Skip EOF status: 1\n");
Tarun Karra3164fb02013-02-05 15:38:51 -08001954 ft_data->status = 1;
1955 } else {
1956 ft_data->status = 0;
Tarun Karradeeecc02013-01-21 23:42:17 -08001957 goto play_good_cmds;
Tarun Karra3164fb02013-02-05 15:38:51 -08001958 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001959 }
1960
1961play_good_cmds:
1962
Tarun Karra3164fb02013-02-05 15:38:51 -08001963 if (ft_data->status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001964 KGSL_FT_ERR(device, "Bad context commands failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001965 else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001966 KGSL_FT_INFO(device, "Bad context commands success\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001967
1968 if (adreno_context) {
1969 adreno_context->flags = (adreno_context->flags &
Tarun Karrad20d71a2013-01-25 15:38:57 -08001970 ~CTXT_FLAGS_GPU_HANG) | CTXT_FLAGS_GPU_HANG_FT;
Tarun Karradeeecc02013-01-21 23:42:17 -08001971 }
1972 adreno_dev->drawctxt_active = last_active_ctx;
1973 }
1974
Tarun Karrad20d71a2013-01-25 15:38:57 -08001975 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1976 ft_data->good_rb_buffer, ft_data->good_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001977
1978 if (ret) {
1979 /* If we fail here we can try to invalidate another
1980 * context and try fault tolerance again */
1981 ret = -EAGAIN;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001982 KGSL_FT_ERR(device, "Playing good commands unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001983 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001984 } else
1985 KGSL_FT_INFO(device, "Playing good commands successful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001986
1987 /* ringbuffer now has data from the last valid context id,
1988 * so restore the active_ctx to the last valid context */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001989 if (ft_data->last_valid_ctx_id) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001990 struct kgsl_context *last_ctx =
1991 idr_find(&device->context_idr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001992 ft_data->last_valid_ctx_id);
Tarun Karradeeecc02013-01-21 23:42:17 -08001993 if (last_ctx)
1994 adreno_dev->drawctxt_active = last_ctx->devctxt;
1995 }
1996
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001997done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001998 /* Turn off iommu clocks */
1999 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
2000 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002001 return ret;
2002}
2003
2004static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002005adreno_ft(struct kgsl_device *device,
2006 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002007{
2008 int ret = 0;
2009 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2010 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2011 unsigned int timestamp;
2012
Tarun Karrad20d71a2013-01-25 15:38:57 -08002013 KGSL_FT_INFO(device,
2014 "Start Parameters: IB1: 0x%X, "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002015 "Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08002016 ft_data->ib1, ft_data->context_id, ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002017
2018 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
Tarun Karrad20d71a2013-01-25 15:38:57 -08002019 KGSL_FT_INFO(device, "Last issued global timestamp: %x\n", timestamp);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002020
2021 /* We may need to replay commands multiple times based on whether
2022 * multiple contexts hang the GPU */
2023 while (true) {
Tarun Karradeeecc02013-01-21 23:42:17 -08002024
Tarun Karrad20d71a2013-01-25 15:38:57 -08002025 ret = _adreno_ft(device, ft_data);
2026
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002027 if (-EAGAIN == ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002028 /* setup new fault tolerance parameters and retry, this
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002029 * means more than 1 contexts are causing hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08002030 adreno_destroy_ft_data(ft_data);
Tarun Karra59e79542013-04-10 10:25:25 -07002031 adreno_setup_ft_data(device, ft_data);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002032 KGSL_FT_INFO(device,
2033 "Retry. Parameters: "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002034 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08002035 ft_data->ib1, ft_data->context_id,
2036 ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002037 } else {
2038 break;
2039 }
2040 }
2041
2042 if (ret)
2043 goto done;
2044
Tarun Karrad20d71a2013-01-25 15:38:57 -08002045 /* Restore correct states after fault tolerance */
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002046 if (adreno_dev->drawctxt_active)
2047 device->mmu.hwpagetable =
2048 adreno_dev->drawctxt_active->pagetable;
2049 else
2050 device->mmu.hwpagetable = device->mmu.defaultpagetable;
2051 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
2052 kgsl_sharedmem_writel(&device->memstore,
2053 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2054 eoptimestamp),
2055 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07002056
2057 /* switch to NULL ctxt */
2058 if (adreno_dev->drawctxt_active != NULL)
2059 adreno_drawctxt_switch(adreno_dev, NULL, 0);
2060
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002061done:
2062 adreno_set_max_ts_for_bad_ctxs(device);
2063 adreno_mark_context_status(device, ret);
Tarun Karra3164fb02013-02-05 15:38:51 -08002064 KGSL_FT_ERR(device, "policy 0x%X status 0x%x\n",
2065 ft_data->ft_policy, ret);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06002066 return ret;
2067}
2068
2069int
Tarun Karrad20d71a2013-01-25 15:38:57 -08002070adreno_dump_and_exec_ft(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002071{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002072 int result = -ETIMEDOUT;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002073 struct adreno_ft_data ft_data;
Tarun Karra696f89e2013-01-27 21:31:40 -08002074 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karrad2326b82013-01-28 19:09:41 -08002075 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2076 unsigned int curr_pwrlevel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002077
2078 if (device->state == KGSL_STATE_HUNG)
2079 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002080 if (device->state == KGSL_STATE_DUMP_AND_FT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002081 mutex_unlock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002082 wait_for_completion(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002083 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07002084 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002085 result = 0;
2086 } else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002087 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_FT);
2088 INIT_COMPLETION(device->ft_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002089 /* Detected a hang */
2090
Tarun Karrad2326b82013-01-28 19:09:41 -08002091 /* Run fault tolerance at max power level */
2092 curr_pwrlevel = pwr->active_pwrlevel;
2093 kgsl_pwrctrl_pwrlevel_change(device, pwr->max_pwrlevel);
2094
Tarun Karrad20d71a2013-01-25 15:38:57 -08002095 /* Get the fault tolerance data as soon as hang is detected */
Tarun Karra59e79542013-04-10 10:25:25 -07002096 adreno_setup_ft_data(device, &ft_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002097
2098 /*
Tarun Karra696f89e2013-01-27 21:31:40 -08002099 * If long ib is detected, do not attempt postmortem or
2100 * snapshot, if GPU is still executing commands
2101 * we will get errors
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002102 */
Tarun Karra696f89e2013-01-27 21:31:40 -08002103 if (!adreno_dev->long_ib) {
2104 /*
2105 * Trigger an automatic dump of the state to
2106 * the console
2107 */
2108 kgsl_postmortem_dump(device, 0);
2109
2110 /*
2111 * Make a GPU snapshot. For now, do it after the
2112 * PM dump so we can at least be sure the PM dump
2113 * will work as it always has
2114 */
2115 kgsl_device_snapshot(device, 1);
2116 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002117
Tarun Karra59e79542013-04-10 10:25:25 -07002118 result = adreno_ft(device, &ft_data);
2119 adreno_destroy_ft_data(&ft_data);
Tarun Karra696f89e2013-01-27 21:31:40 -08002120
Tarun Karrad2326b82013-01-28 19:09:41 -08002121 /* restore power level */
2122 kgsl_pwrctrl_pwrlevel_change(device, curr_pwrlevel);
2123
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002124 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002125 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002126 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002127 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002128 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
2129 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08002130 complete_all(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002131 }
2132done:
2133 return result;
2134}
Tarun Karrad20d71a2013-01-25 15:38:57 -08002135EXPORT_SYMBOL(adreno_dump_and_exec_ft);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002136
2137static int adreno_getproperty(struct kgsl_device *device,
2138 enum kgsl_property_type type,
2139 void *value,
2140 unsigned int sizebytes)
2141{
2142 int status = -EINVAL;
2143 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2144
2145 switch (type) {
2146 case KGSL_PROP_DEVICE_INFO:
2147 {
2148 struct kgsl_devinfo devinfo;
2149
2150 if (sizebytes != sizeof(devinfo)) {
2151 status = -EINVAL;
2152 break;
2153 }
2154
2155 memset(&devinfo, 0, sizeof(devinfo));
2156 devinfo.device_id = device->id+1;
2157 devinfo.chip_id = adreno_dev->chip_id;
2158 devinfo.mmu_enabled = kgsl_mmu_enabled();
2159 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06002160 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
2161 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002162
2163 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
2164 0) {
2165 status = -EFAULT;
2166 break;
2167 }
2168 status = 0;
2169 }
2170 break;
2171 case KGSL_PROP_DEVICE_SHADOW:
2172 {
2173 struct kgsl_shadowprop shadowprop;
2174
2175 if (sizebytes != sizeof(shadowprop)) {
2176 status = -EINVAL;
2177 break;
2178 }
2179 memset(&shadowprop, 0, sizeof(shadowprop));
2180 if (device->memstore.hostptr) {
2181 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2182 * anything to mmap().
2183 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07002184 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002185 shadowprop.size = device->memstore.size;
2186 /* GSL needs this to be set, even if it
2187 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002188 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2189 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002190 }
2191 if (copy_to_user(value, &shadowprop,
2192 sizeof(shadowprop))) {
2193 status = -EFAULT;
2194 break;
2195 }
2196 status = 0;
2197 }
2198 break;
2199 case KGSL_PROP_MMU_ENABLE:
2200 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002201 int mmu_prop = kgsl_mmu_enabled();
2202
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002203 if (sizebytes != sizeof(int)) {
2204 status = -EINVAL;
2205 break;
2206 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002207 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208 status = -EFAULT;
2209 break;
2210 }
2211 status = 0;
2212 }
2213 break;
2214 case KGSL_PROP_INTERRUPT_WAITS:
2215 {
2216 int int_waits = 1;
2217 if (sizebytes != sizeof(int)) {
2218 status = -EINVAL;
2219 break;
2220 }
2221 if (copy_to_user(value, &int_waits, sizeof(int))) {
2222 status = -EFAULT;
2223 break;
2224 }
2225 status = 0;
2226 }
2227 break;
2228 default:
2229 status = -EINVAL;
2230 }
2231
2232 return status;
2233}
2234
Jordan Crousef7370f82012-04-18 09:31:07 -06002235static int adreno_setproperty(struct kgsl_device *device,
2236 enum kgsl_property_type type,
2237 void *value,
2238 unsigned int sizebytes)
2239{
2240 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08002241 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06002242
2243 switch (type) {
2244 case KGSL_PROP_PWRCTRL: {
2245 unsigned int enable;
2246 struct kgsl_device_platform_data *pdata =
2247 kgsl_device_get_drvdata(device);
2248
2249 if (sizebytes != sizeof(enable))
2250 break;
2251
2252 if (copy_from_user(&enable, (void __user *) value,
2253 sizeof(enable))) {
2254 status = -EFAULT;
2255 break;
2256 }
2257
2258 if (enable) {
2259 if (pdata->nap_allowed)
2260 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002261 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002262 kgsl_pwrscale_enable(device);
2263 } else {
2264 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002265 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002266 kgsl_pwrscale_disable(device);
2267 }
2268
2269 status = 0;
2270 }
2271 break;
2272 default:
2273 break;
2274 }
2275
2276 return status;
2277}
2278
Jordan Crousea29a2e02012-08-14 09:09:23 -06002279static int adreno_ringbuffer_drain(struct kgsl_device *device,
2280 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002281{
2282 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2283 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002284 unsigned long wait;
2285 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2286
2287 if (!(rb->flags & KGSL_FLAGS_STARTED))
2288 return 0;
2289
2290 /*
2291 * The first time into the loop, wait for 100 msecs and kick wptr again
2292 * to ensure that the hardware has updated correctly. After that, kick
2293 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2294 * expires
2295 */
2296
2297 wait = jiffies + msecs_to_jiffies(100);
2298
Jordan Crousea29a2e02012-08-14 09:09:23 -06002299 do {
2300 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002301 /* Check to see if the core is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002302 if (adreno_ft_detect(device, regs))
Jordan Crousea29a2e02012-08-14 09:09:23 -06002303 return -ETIMEDOUT;
2304
2305 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2306 }
2307 GSL_RB_GET_READPTR(rb, &rb->rptr);
2308
2309 if (time_after(jiffies, timeout)) {
2310 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2311 rb->rptr, rb->wptr);
2312 return -ETIMEDOUT;
2313 }
2314 } while (rb->rptr != rb->wptr);
2315
2316 return 0;
2317}
2318
2319/* Caller must hold the device mutex. */
2320int adreno_idle(struct kgsl_device *device)
2321{
2322 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002323 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302324 unsigned long wait_time;
2325 unsigned long wait_time_part;
Tarun Karra696f89e2013-01-27 21:31:40 -08002326 unsigned int prev_reg_val[ft_detect_regs_count];
Tarun Karra3335f142012-06-19 14:11:48 -07002327
2328 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002329
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002330 kgsl_cffdump_regpoll(device->id,
2331 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002332 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002333
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002334retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002335 /* First, wait for the ringbuffer to drain */
2336 if (adreno_ringbuffer_drain(device, prev_reg_val))
2337 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002338
2339 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002340 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002341 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2342
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002343 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002344 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2345 &rbbm_status);
2346 if (adreno_is_a2xx(adreno_dev)) {
2347 if (rbbm_status == 0x110)
2348 return 0;
2349 } else {
2350 if (!(rbbm_status & 0x80000000))
2351 return 0;
2352 }
Tarun Karra3335f142012-06-19 14:11:48 -07002353
2354 /* Dont wait for timeout, detect hang faster.
2355 */
2356 if (time_after(jiffies, wait_time_part)) {
2357 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002358 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra696f89e2013-01-27 21:31:40 -08002359 if ((adreno_ft_detect(device, prev_reg_val)))
Tarun Karra3335f142012-06-19 14:11:48 -07002360 goto err;
2361 }
2362
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002363 }
2364
2365err:
2366 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08002367 if (KGSL_STATE_DUMP_AND_FT != device->state &&
2368 !adreno_dump_and_exec_ft(device)) {
2369 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002370 goto retry;
2371 }
2372 return -ETIMEDOUT;
2373}
2374
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002375/**
2376 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2377 * rbbm_status register
2378 * @device - Pointer to the GPU device whose idle status is to be
2379 * checked
2380 * @returns - Returns whether the core is idle (based on rbbm_status)
2381 * false if the core is active, true if the core is idle
2382 */
2383static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2384{
2385 unsigned int reg_rbbm_status;
2386 bool status = false;
2387 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2388
2389 /* Is the core idle? */
2390 adreno_regread(device,
2391 adreno_dev->gpudev->reg_rbbm_status,
2392 &reg_rbbm_status);
2393
2394 if (adreno_is_a2xx(adreno_dev)) {
2395 if (reg_rbbm_status == 0x110)
2396 status = true;
2397 } else {
2398 if (!(reg_rbbm_status & 0x80000000))
2399 status = true;
2400 }
2401 return status;
2402}
2403
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002404static unsigned int adreno_isidle(struct kgsl_device *device)
2405{
2406 int status = false;
2407 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2408 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002409
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002410 WARN_ON(device->state == KGSL_STATE_INIT);
2411 /* If the device isn't active, don't force it on. */
2412 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002413 /* Is the ring buffer is empty? */
2414 GSL_RB_GET_READPTR(rb, &rb->rptr);
2415 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
Jordan Crouseab601992013-03-05 11:18:20 -07002416 /*
2417 * Are there interrupts pending? If so then pretend we
2418 * are not idle - this avoids the possiblity that we go
2419 * to a lower power state without handling interrupts
2420 * first.
2421 */
2422
2423 if (!adreno_dev->gpudev->irq_pending(adreno_dev)) {
2424 /* Is the core idle? */
2425 status = is_adreno_rbbm_status_idle(device);
2426 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002427 }
2428 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002429 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002430 }
2431 return status;
2432}
2433
2434/* Caller must hold the device mutex. */
2435static int adreno_suspend_context(struct kgsl_device *device)
2436{
2437 int status = 0;
2438 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2439
2440 /* switch to NULL ctxt */
2441 if (adreno_dev->drawctxt_active != NULL) {
2442 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002443 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002444 }
2445
2446 return status;
2447}
2448
Jordan Crouse233b2092012-04-18 09:31:09 -06002449/* Find a memory structure attached to an adreno context */
2450
2451struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2452 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2453{
2454 struct kgsl_context *context;
2455 struct adreno_context *adreno_context = NULL;
2456 int next = 0;
2457
2458 while (1) {
2459 context = idr_get_next(&device->context_idr, &next);
2460 if (context == NULL)
2461 break;
2462
2463 adreno_context = (struct adreno_context *)context->devctxt;
2464
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002465 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2466 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002467 struct kgsl_memdesc *desc;
2468
2469 desc = &adreno_context->gpustate;
2470 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2471 return desc;
2472
2473 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2474 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2475 return desc;
2476 }
2477 next = next + 1;
2478 }
2479
2480 return NULL;
2481}
2482
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002483struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002484 unsigned int pt_base,
2485 unsigned int gpuaddr,
2486 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002487{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002488 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002489 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2490 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2491
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002492 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2493 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002494
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002495 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2496 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002497
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002498 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2499 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002500
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002501 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2502 size))
2503 return &device->mmu.setstate_memory;
2504
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002505 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002506
2507 if (entry)
2508 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002509
Jordan Crouse233b2092012-04-18 09:31:09 -06002510 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002511}
2512
2513uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2514 unsigned int gpuaddr, unsigned int size)
2515{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002516 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002517
2518 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2519
2520 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002521}
2522
2523void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2524 unsigned int *value)
2525{
2526 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002527 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2528 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529
2530 if (!in_interrupt())
2531 kgsl_pre_hwaccess(device);
2532
2533 /*ensure this read finishes before the next one.
2534 * i.e. act like normal readl() */
2535 *value = __raw_readl(reg);
2536 rmb();
2537}
2538
2539void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2540 unsigned int value)
2541{
2542 unsigned int *reg;
2543
Jordan Crouse7501d452012-04-19 08:58:44 -06002544 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002545
2546 if (!in_interrupt())
2547 kgsl_pre_hwaccess(device);
2548
2549 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002550 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002551
2552 /*ensure previous writes post before this one,
2553 * i.e. act like normal writel() */
2554 wmb();
2555 __raw_writel(value, reg);
2556}
2557
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002558static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2559{
2560 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002561 if (k_ctxt != NULL) {
2562 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002563 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2564 context_id = KGSL_CONTEXT_INVALID;
2565 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2566 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002567 }
2568
2569 return context_id;
2570}
2571
Carter Cooper740f6742013-01-03 16:19:23 -07002572static unsigned int adreno_check_hw_ts(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002573 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002574{
Carter Cooper740f6742013-01-03 16:19:23 -07002575 int status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002576 unsigned int ref_ts, enableflag;
Carter Cooper740f6742013-01-03 16:19:23 -07002577 unsigned int context_id = _get_context_id(context);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002578
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002579 /*
2580 * If the context ID is invalid, we are in a race with
2581 * the context being destroyed by userspace so bail.
2582 */
2583 if (context_id == KGSL_CONTEXT_INVALID) {
2584 KGSL_DRV_WARN(device, "context was detached");
Carter Cooper740f6742013-01-03 16:19:23 -07002585 return -EINVAL;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002586 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002587
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002588 status = kgsl_check_timestamp(device, context, timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002589 if (status)
2590 return status;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002591
Carter Cooper740f6742013-01-03 16:19:23 -07002592 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2593 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2594 /*
2595 * Barrier is needed here to make sure the read from memstore
2596 * has posted
2597 */
2598
2599 mb();
2600
2601 if (enableflag) {
2602 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002603 KGSL_MEMSTORE_OFFSET(context_id,
2604 ref_wait_ts));
Carter Cooper740f6742013-01-03 16:19:23 -07002605
2606 /* Make sure the memstore read has posted */
2607 mb();
2608 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
2609 kgsl_sharedmem_writel(&device->memstore,
2610 KGSL_MEMSTORE_OFFSET(context_id,
2611 ref_wait_ts), timestamp);
2612 /* Make sure the memstore write is posted */
2613 wmb();
2614 }
2615 } else {
2616 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002617 KGSL_MEMSTORE_OFFSET(context_id,
2618 ref_wait_ts), timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002619 enableflag = 1;
2620 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002621 KGSL_MEMSTORE_OFFSET(context_id,
2622 ts_cmp_enable), enableflag);
Carter Cooper740f6742013-01-03 16:19:23 -07002623 /* Make sure the memstore write gets posted */
2624 wmb();
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002625
Carter Cooper740f6742013-01-03 16:19:23 -07002626 /*
2627 * submit a dummy packet so that even if all
2628 * commands upto timestamp get executed we will still
2629 * get an interrupt
2630 */
2631
2632 if (context && device->state != KGSL_STATE_SLUMBER)
2633 adreno_ringbuffer_issuecmds(device, context->devctxt,
2634 KGSL_CMD_FLAGS_NONE, NULL, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002635 }
Carter Cooper740f6742013-01-03 16:19:23 -07002636
2637 return 0;
2638}
2639
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002640/* Return 1 if the event timestmp has already passed, 0 if it was marked */
2641static int adreno_next_event(struct kgsl_device *device,
Carter Cooper740f6742013-01-03 16:19:23 -07002642 struct kgsl_event *event)
2643{
Jordan Crouseed8c2dd2013-01-28 16:58:45 -07002644 return adreno_check_hw_ts(device, event->context, event->timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002645}
2646
2647static int adreno_check_interrupt_timestamp(struct kgsl_device *device,
2648 struct kgsl_context *context, unsigned int timestamp)
2649{
2650 int status;
2651
2652 mutex_lock(&device->mutex);
2653 status = adreno_check_hw_ts(device, context, timestamp);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002654 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002655
2656 return status;
2657}
2658
2659/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002660 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002661 placing a process in wait q. For conditional interrupts we expect the
2662 process to already be in its wait q when its exit condition checking
2663 function is called.
2664*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002665#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002666({ \
2667 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002668 if (io) \
2669 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2670 else \
2671 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002672 __ret; \
2673})
2674
Tarun Karra3335f142012-06-19 14:11:48 -07002675
2676
Tarun Karra696f89e2013-01-27 21:31:40 -08002677unsigned int adreno_ft_detect(struct kgsl_device *device,
Tarun Karra3335f142012-06-19 14:11:48 -07002678 unsigned int *prev_reg_val)
2679{
2680 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karra696f89e2013-01-27 21:31:40 -08002681 unsigned int curr_reg_val[ft_detect_regs_count];
2682 unsigned int fast_hang_detected = 1;
2683 unsigned int long_ib_detected = 1;
Tarun Karra3335f142012-06-19 14:11:48 -07002684 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002685 static unsigned long next_hang_detect_time;
Tarun Karra696f89e2013-01-27 21:31:40 -08002686 static unsigned int prev_global_ts;
2687 unsigned int curr_global_ts = 0;
2688 unsigned int curr_context_id = 0;
2689 static struct adreno_context *curr_context;
2690 static struct kgsl_context *context;
Tarun Karra3335f142012-06-19 14:11:48 -07002691
2692 if (!adreno_dev->fast_hang_detect)
Tarun Karra696f89e2013-01-27 21:31:40 -08002693 fast_hang_detected = 0;
2694
2695 if (!adreno_dev->long_ib_detect)
2696 long_ib_detected = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002697
Jordan Crousecca61142012-11-20 10:54:24 -07002698 if (is_adreno_rbbm_status_idle(device)) {
2699
2700 /*
2701 * On A20X if the RPTR != WPTR and the device is idle, then
2702 * the last write to WPTR probably failed to latch so write it
2703 * again
2704 */
2705
2706 if (adreno_is_a2xx(adreno_dev)) {
2707 unsigned int rptr;
2708 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2709 if (rptr != adreno_dev->ringbuffer.wptr)
2710 adreno_regwrite(device, REG_CP_RB_WPTR,
2711 adreno_dev->ringbuffer.wptr);
2712 }
2713
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002714 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002715 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002716
Tarun Karra4648db92013-02-01 17:03:40 -08002717 /*
2718 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2719 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2720 * from the last time do nothing.
2721 */
2722 if ((next_hang_detect_time) &&
2723 (time_before(jiffies, next_hang_detect_time)))
2724 return 0;
2725 else
2726 next_hang_detect_time = (jiffies +
2727 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2728
Tarun Karra696f89e2013-01-27 21:31:40 -08002729 /* Read the current Hang detect reg values here */
2730 for (i = 0; i < ft_detect_regs_count; i++) {
2731 if (ft_detect_regs[i] == 0)
Jordan Crouseb5c80482012-10-03 09:38:41 -06002732 continue;
Tarun Karra696f89e2013-01-27 21:31:40 -08002733 adreno_regread(device, ft_detect_regs[i],
2734 &curr_reg_val[i]);
Tarun Karra3335f142012-06-19 14:11:48 -07002735 }
2736
Tarun Karra696f89e2013-01-27 21:31:40 -08002737 /* Read the current global timestamp here */
2738 kgsl_sharedmem_readl(&device->memstore,
2739 &curr_global_ts,
2740 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2741 eoptimestamp));
2742
2743 mb();
2744
2745 if (curr_global_ts == prev_global_ts) {
2746
2747 /* Get the current context here */
2748 if (context == NULL) {
2749 kgsl_sharedmem_readl(&device->memstore,
2750 &curr_context_id,
2751 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2752 current_context));
2753 context = idr_find(&device->context_idr,
2754 curr_context_id);
2755 if (context != NULL) {
2756 curr_context = context->devctxt;
2757 curr_context->ib_gpu_time_used = 0;
2758 } else {
2759 KGSL_DRV_ERR(device,
2760 "Fault tolerance no context found\n");
2761 }
2762 }
2763
2764 mb();
2765
2766 if (curr_context != NULL) {
2767
2768 curr_context->ib_gpu_time_used += KGSL_TIMEOUT_PART;
2769 KGSL_FT_INFO(device,
2770 "Proc %s used GPU Time %d ms on timestamp 0x%X\n",
2771 curr_context->pid_name, curr_context->ib_gpu_time_used,
2772 curr_global_ts+1);
2773
2774 for (i = 0; i < ft_detect_regs_count; i++) {
2775 if (curr_reg_val[i] != prev_reg_val[i]) {
2776 fast_hang_detected = 0;
2777
2778 /* Check for long IB here */
2779 if ((i >=
2780 LONG_IB_DETECT_REG_INDEX_START)
2781 &&
2782 (i <=
2783 LONG_IB_DETECT_REG_INDEX_END))
2784 long_ib_detected = 0;
2785 }
2786 }
2787
2788 if (fast_hang_detected) {
2789 KGSL_FT_ERR(device,
2790 "Proc %s, ctxt_id %d ts %d triggered fault tolerance"
2791 " on global ts %d\n",
2792 curr_context->pid_name, curr_context->id
2793 , (kgsl_readtimestamp(device, context,
2794 KGSL_TIMESTAMP_RETIRED)+1),
2795 curr_global_ts+1);
2796 return 1;
2797 }
2798
Tarun Karra83297222013-02-05 19:45:49 -08002799 if ((long_ib_detected) &&
2800 (!(curr_context->flags &
2801 CTXT_FLAGS_NO_FAULT_TOLERANCE))) {
Tarun Karra696f89e2013-01-27 21:31:40 -08002802 curr_context->ib_gpu_time_used +=
2803 KGSL_TIMEOUT_PART;
2804 if (curr_context->ib_gpu_time_used >
2805 KGSL_TIMEOUT_LONG_IB_DETECTION) {
2806 if (adreno_dev->long_ib_ts !=
2807 curr_global_ts) {
2808 KGSL_FT_ERR(device,
2809 "Proc %s, ctxt_id %d ts %d"
2810 "used GPU for %d ms long ib "
2811 "detected on global ts %d\n",
2812 curr_context->pid_name,
2813 curr_context->id,
2814 (kgsl_readtimestamp(device,
2815 context,
2816 KGSL_TIMESTAMP_RETIRED)+1),
2817 curr_context->ib_gpu_time_used,
2818 curr_global_ts+1);
2819 adreno_dev->long_ib = 1;
2820 adreno_dev->long_ib_ts =
2821 curr_global_ts;
2822 curr_context->ib_gpu_time_used =
2823 0;
2824 return 1;
2825 }
2826 }
2827 }
2828 } else {
2829 KGSL_FT_ERR(device,
2830 "Last context unknown id:%d\n",
2831 curr_context_id);
2832 }
2833 } else {
2834 /* GPU is moving forward */
2835 prev_global_ts = curr_global_ts;
2836 context = NULL;
2837 curr_context = NULL;
2838 adreno_dev->long_ib = 0;
2839 adreno_dev->long_ib_ts = 0;
2840 }
2841
2842
2843 /* If hangs are not detected copy the current reg values
2844 * to previous values and return no hang */
2845 for (i = 0; i < ft_detect_regs_count; i++)
2846 prev_reg_val[i] = curr_reg_val[i];
2847 return 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002848}
2849
Jordan Crouse92446a62012-11-15 11:00:06 -07002850/**
2851 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2852 * @device - pointer to a KGSL device structure
2853 * @context - pointer to the active KGSL context
2854 * @timestamp - the timestamp that the process was waiting for
2855 *
Tarun Karrad20d71a2013-01-25 15:38:57 -08002856 * Process a possible GPU hang and try fault tolerance from it
2857 * cleanly
Jordan Crouse92446a62012-11-15 11:00:06 -07002858 */
2859static int adreno_handle_hang(struct kgsl_device *device,
2860 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002861{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002862 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002863 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002864 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002865 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002866
Jordan Crouse92446a62012-11-15 11:00:06 -07002867 /* Do one last check to see if we somehow made it through */
2868 if (kgsl_check_timestamp(device, context, timestamp))
2869 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002870
2871 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002872
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002873 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2874 mb();
2875
Tarun Karra696f89e2013-01-27 21:31:40 -08002876 KGSL_DRV_WARN(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002877 "Device hang detected while waiting for timestamp: "
2878 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002879 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2880 context_id, timestamp, context_id, ts_issued, context_id,
2881 kgsl_readtimestamp(device, context,
2882 KGSL_TIMESTAMP_RETIRED),
2883 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002884
Tarun Karrad20d71a2013-01-25 15:38:57 -08002885 /* Return 0 after a successful fault tolerance */
2886 if (!adreno_dump_and_exec_ft(device))
Jordan Crouse92446a62012-11-15 11:00:06 -07002887 return 0;
2888
2889 return -ETIMEDOUT;
2890}
2891
2892static int _check_pending_timestamp(struct kgsl_device *device,
2893 struct kgsl_context *context, unsigned int timestamp)
2894{
2895 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2896 unsigned int context_id = _get_context_id(context);
2897 unsigned int ts_issued;
2898
2899 if (context_id == KGSL_CONTEXT_INVALID)
2900 return -EINVAL;
2901
2902 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2903
2904 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2905 return 0;
2906
2907 if (context && !context->wait_on_invalid_ts) {
2908 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2909 context_id, timestamp, context_id, ts_issued);
2910
2911 /* Only print this message once */
2912 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002913 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002914
2915 return -EINVAL;
2916}
2917
2918/**
2919 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2920 * @device - pointer to a KGSL device structure
2921 * @context - pointer to the active kgsl context
2922 * @timestamp - GPU timestamp to wait for
2923 * @msecs - amount of time to wait (in milliseconds)
2924 *
2925 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2926 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2927 * one if it happened. Otherwise, spend most of our time in an interruptible
2928 * wait for the timestamp interrupt to be processed. This function must be
2929 * called with the mutex already held.
2930 */
2931static int adreno_waittimestamp(struct kgsl_device *device,
2932 struct kgsl_context *context,
2933 unsigned int timestamp,
2934 unsigned int msecs)
2935{
2936 static unsigned int io_cnt;
2937 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2938 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2939 unsigned int context_id = _get_context_id(context);
Tarun Karra696f89e2013-01-27 21:31:40 -08002940 unsigned int prev_reg_val[ft_detect_regs_count];
Jordan Crouse92446a62012-11-15 11:00:06 -07002941 unsigned int time_elapsed = 0;
2942 unsigned int wait;
2943 int ts_compare = 1;
2944 int io, ret = -ETIMEDOUT;
2945
2946 /* Get out early if the context has already been destroyed */
2947
2948 if (context_id == KGSL_CONTEXT_INVALID) {
2949 KGSL_DRV_WARN(device, "context was detached");
2950 return -EINVAL;
2951 }
2952
2953 /*
2954 * Check to see if the requested timestamp is "newer" then the last
2955 * timestamp issued. If it is complain once and return error. Only
2956 * print the message once per context so that badly behaving
2957 * applications don't spam the logs
2958 */
2959
2960 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2961 if (_check_pending_timestamp(device, context, timestamp))
2962 return -EINVAL;
2963
2964 /* Reset the invalid timestamp flag on a valid wait */
2965 context->wait_on_invalid_ts = false;
2966 }
2967
2968
2969 /* Clear the registers used for hang detection */
2970 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2971
2972 /*
2973 * On the first time through the loop only wait 100ms.
2974 * this gives enough time for the engine to start moving and oddly
2975 * provides better hang detection results than just going the full
2976 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2977 * is if msecs happens to be < 100ms then just use the full timeout
2978 */
2979
2980 wait = 100;
2981
2982 do {
2983 long status;
2984
Jordan Crouse92446a62012-11-15 11:00:06 -07002985 /*
2986 * if the timestamp happens while we're not
2987 * waiting, there's a chance that an interrupt
2988 * will not be generated and thus the timestamp
2989 * work needs to be queued.
2990 */
2991
2992 if (kgsl_check_timestamp(device, context, timestamp)) {
2993 queue_work(device->work_queue, &device->ts_expired_ws);
2994 ret = 0;
2995 break;
2996 }
2997
2998 /* Check to see if the GPU is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002999 if (adreno_ft_detect(device, prev_reg_val)) {
Jordan Crouse92446a62012-11-15 11:00:06 -07003000 ret = adreno_handle_hang(device, context, timestamp);
3001 break;
3002 }
3003
3004 /*
3005 * For proper power accounting sometimes we need to call
3006 * io_wait_interruptible_timeout and sometimes we need to call
3007 * plain old wait_interruptible_timeout. We call the regular
3008 * timeout N times out of 100, where N is a number specified by
3009 * the current power level
3010 */
3011
3012 io_cnt = (io_cnt + 1) % 100;
3013 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
3014 ? 0 : 1;
3015
3016 mutex_unlock(&device->mutex);
3017
3018 /* Wait for a timestamp event */
3019 status = kgsl_wait_event_interruptible_timeout(
3020 device->wait_queue,
Carter Cooper740f6742013-01-03 16:19:23 -07003021 adreno_check_interrupt_timestamp(device, context,
Jordan Crouse92446a62012-11-15 11:00:06 -07003022 timestamp), msecs_to_jiffies(wait), io);
3023
3024 mutex_lock(&device->mutex);
3025
3026 /*
3027 * If status is non zero then either the condition was satisfied
3028 * or there was an error. In either event, this is the end of
3029 * the line for us
3030 */
3031
3032 if (status != 0) {
3033 ret = (status > 0) ? 0 : (int) status;
3034 break;
3035 }
Jordan Crouse92446a62012-11-15 11:00:06 -07003036 time_elapsed += wait;
3037
3038 /* If user specified timestamps are being used, wait at least
3039 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
3040 * issue a IB for a timestamp before checking to see if the
3041 * current timestamp we are waiting for is valid or not
3042 */
3043
3044 if (ts_compare && (adreno_ctx &&
3045 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
3046 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
3047 ret = _check_pending_timestamp(device, context,
3048 timestamp);
3049 if (ret)
3050 break;
3051
3052 /* Don't do this check again */
3053 ts_compare = 0;
3054
3055 /*
3056 * Reset the invalid timestamp flag on a valid
3057 * wait
3058 */
3059 context->wait_on_invalid_ts = false;
3060 }
3061 }
3062
3063 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003064 * We want to wait the floor of KGSL_TIMEOUT_PART
3065 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07003066 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003067
3068 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
3069 wait = KGSL_TIMEOUT_PART;
3070 else
3071 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07003072
3073 } while (!msecs || time_elapsed < msecs);
3074
3075 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003076}
3077
3078static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003079 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003080{
3081 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003082 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003083
Jeremy Gebben9ad86922012-05-08 15:33:23 -06003084 /*
3085 * If the context ID is invalid, we are in a race with
3086 * the context being destroyed by userspace so bail.
3087 */
3088 if (context_id == KGSL_CONTEXT_INVALID) {
3089 KGSL_DRV_WARN(device, "context was detached");
3090 return timestamp;
3091 }
Jordan Crousec659f382012-04-16 11:10:41 -06003092 switch (type) {
3093 case KGSL_TIMESTAMP_QUEUED: {
3094 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3095 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
3096
3097 timestamp = rb->timestamp[context_id];
3098 break;
3099 }
3100 case KGSL_TIMESTAMP_CONSUMED:
Carter Cooper78d8d362013-05-28 17:01:59 -06003101 kgsl_sharedmem_readl(&device->memstore, &timestamp,
3102 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp));
Jordan Crousec659f382012-04-16 11:10:41 -06003103 break;
3104 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003105 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06003106 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
3107 break;
3108 }
3109
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003110 rmb();
3111
3112 return timestamp;
3113}
3114
3115static long adreno_ioctl(struct kgsl_device_private *dev_priv,
3116 unsigned int cmd, void *data)
3117{
3118 int result = 0;
3119 struct kgsl_drawctxt_set_bin_base_offset *binbase;
3120 struct kgsl_context *context;
3121
3122 switch (cmd) {
3123 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
3124 binbase = data;
3125
3126 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
3127 if (context) {
3128 adreno_drawctxt_set_bin_base_offset(
3129 dev_priv->device, context, binbase->offset);
3130 } else {
3131 result = -EINVAL;
3132 KGSL_DRV_ERR(dev_priv->device,
3133 "invalid drawctxt drawctxt_id %d "
3134 "device_id=%d\n",
3135 binbase->drawctxt_id, dev_priv->device->id);
3136 }
3137 break;
3138
3139 default:
3140 KGSL_DRV_INFO(dev_priv->device,
3141 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07003142 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003143 break;
3144 }
3145 return result;
3146
3147}
3148
3149static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
3150{
3151 gpu_freq /= 1000000;
3152 return ticks / gpu_freq;
3153}
3154
3155static void adreno_power_stats(struct kgsl_device *device,
3156 struct kgsl_power_stats *stats)
3157{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003158 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003159 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003160 unsigned int cycles;
3161
3162 /* Get the busy cycles counted since the counter was last reset */
3163 /* Calling this function also resets and restarts the counter */
3164
3165 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003166
3167 /* In order to calculate idle you have to have run the algorithm *
3168 * at least once to get a start time. */
3169 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003170 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003171 stats->total_time = tmp - pwr->time;
3172 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003173 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003174 pwrlevels[device->pwrctrl.active_pwrlevel].
3175 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003176 } else {
3177 stats->total_time = 0;
3178 stats->busy_time = 0;
3179 pwr->time = ktime_to_us(ktime_get());
3180 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003181}
3182
3183void adreno_irqctrl(struct kgsl_device *device, int state)
3184{
Jordan Crousea78c9172011-07-11 13:14:09 -06003185 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3186 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003187}
3188
Jordan Croused6535882012-06-20 08:22:16 -06003189static unsigned int adreno_gpuid(struct kgsl_device *device,
3190 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07003191{
3192 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3193
Jordan Croused6535882012-06-20 08:22:16 -06003194 /* Some applications need to know the chip ID too, so pass
3195 * that as a parameter */
3196
3197 if (chipid != NULL)
3198 *chipid = adreno_dev->chip_id;
3199
Jordan Crousea0758f22011-12-07 11:19:22 -07003200 /* Standard KGSL gpuid format:
3201 * top word is 0x0002 for 2D or 0x0003 for 3D
3202 * Bottom word is core specific identifer
3203 */
3204
3205 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
3206}
3207
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003208static const struct kgsl_functable adreno_functable = {
3209 /* Mandatory functions */
3210 .regread = adreno_regread,
3211 .regwrite = adreno_regwrite,
3212 .idle = adreno_idle,
3213 .isidle = adreno_isidle,
3214 .suspend_context = adreno_suspend_context,
3215 .start = adreno_start,
3216 .stop = adreno_stop,
3217 .getproperty = adreno_getproperty,
3218 .waittimestamp = adreno_waittimestamp,
3219 .readtimestamp = adreno_readtimestamp,
3220 .issueibcmds = adreno_ringbuffer_issueibcmds,
3221 .ioctl = adreno_ioctl,
3222 .setup_pt = adreno_setup_pt,
3223 .cleanup_pt = adreno_cleanup_pt,
3224 .power_stats = adreno_power_stats,
3225 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07003226 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07003227 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06003228 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003229 /* Optional functions */
3230 .setstate = adreno_setstate,
3231 .drawctxt_create = adreno_drawctxt_create,
3232 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06003233 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06003234 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07003235 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003236};
3237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003238static struct platform_driver adreno_platform_driver = {
3239 .probe = adreno_probe,
3240 .remove = __devexit_p(adreno_remove),
3241 .suspend = kgsl_suspend_driver,
3242 .resume = kgsl_resume_driver,
3243 .id_table = adreno_id_table,
3244 .driver = {
3245 .owner = THIS_MODULE,
3246 .name = DEVICE_3D_NAME,
3247 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06003248 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003249 }
3250};
3251
3252static int __init kgsl_3d_init(void)
3253{
3254 return platform_driver_register(&adreno_platform_driver);
3255}
3256
3257static void __exit kgsl_3d_exit(void)
3258{
3259 platform_driver_unregister(&adreno_platform_driver);
3260}
3261
3262module_init(kgsl_3d_init);
3263module_exit(kgsl_3d_exit);
3264
3265MODULE_DESCRIPTION("3D Graphics driver");
3266MODULE_VERSION("1.2");
3267MODULE_LICENSE("GPL v2");
3268MODULE_ALIAS("platform:kgsl_3d");