blob: 678a1b3e44b2e3df08d2cc27a7484d71bfe253ba [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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020
21#include <mach/socinfo.h>
Lokesh Batra805e1e12012-08-03 08:34:06 -060022#include <mach/msm_bus_board.h>
23#include <mach/msm_bus.h>
24#include <mach/msm_dcvs.h>
25#include <mach/msm_dcvs_scm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27#include "kgsl.h"
28#include "kgsl_pwrscale.h"
29#include "kgsl_cffdump.h"
30#include "kgsl_sharedmem.h"
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -060031#include "kgsl_iommu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
33#include "adreno.h"
34#include "adreno_pm4types.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070036#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070037#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
39#define DRIVER_VERSION_MAJOR 3
40#define DRIVER_VERSION_MINOR 1
41
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042/* Adreno MH arbiter config*/
43#define ADRENO_CFG_MHARB \
44 (0x10 \
45 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
46 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
47 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
48 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
52 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
53 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
54 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
55 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
56 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
57 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
58 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
59
60#define ADRENO_MMU_CONFIG \
61 (0x01 \
62 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
69 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
70 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
71 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
72 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
73
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074static const struct kgsl_functable adreno_functable;
75
76static struct adreno_device device_3d0 = {
77 .dev = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070078 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079 .name = DEVICE_3D0_NAME,
80 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060081 .mh = {
82 .mharb = ADRENO_CFG_MHARB,
83 /* Remove 1k boundary check in z470 to avoid a GPU
84 * hang. Notice that this solution won't work if
85 * both EBI and SMI are used
86 */
87 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088 /* turn off memory protection unit by setting
89 acceptable physical address range to include
90 all pages. */
91 .mpu_base = 0x00000000,
92 .mpu_range = 0xFFFFF000,
93 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060094 .mmu = {
95 .config = ADRENO_MMU_CONFIG,
96 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 .iomemname = KGSL_3D0_REG_MEMORY,
101 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600103 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
105 .suspend = kgsl_early_suspend_driver,
106 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600108#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600110 .gmem_base = 0,
111 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 .pfp_fw = NULL,
113 .pm4_fw = NULL,
Jordan Crouse21f75a02012-08-09 15:08:59 -0600114 .wait_timeout = 0, /* in milliseconds, 0 means disabled */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600115 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116};
117
Tarun Karra3335f142012-06-19 14:11:48 -0700118/* This set of registers are used for Hang detection
119 * If the values of these registers are same after
120 * KGSL_TIMEOUT_PART time, GPU hang is reported in
121 * kernel log.
Tarun Karra696f89e2013-01-27 21:31:40 -0800122 * *****ALERT******ALERT********ALERT*************
123 * Order of registers below is important, registers
124 * from LONG_IB_DETECT_REG_INDEX_START to
125 * LONG_IB_DETECT_REG_INDEX_END are used in long ib detection.
Tarun Karra3335f142012-06-19 14:11:48 -0700126 */
Tarun Karra696f89e2013-01-27 21:31:40 -0800127#define LONG_IB_DETECT_REG_INDEX_START 1
128#define LONG_IB_DETECT_REG_INDEX_END 5
129
130unsigned int ft_detect_regs[] = {
Tarun Karra3335f142012-06-19 14:11:48 -0700131 A3XX_RBBM_STATUS,
Tarun Karra696f89e2013-01-27 21:31:40 -0800132 REG_CP_RB_RPTR, /* LONG_IB_DETECT_REG_INDEX_START */
Tarun Karra3335f142012-06-19 14:11:48 -0700133 REG_CP_IB1_BASE,
134 REG_CP_IB1_BUFSZ,
135 REG_CP_IB2_BASE,
Tarun Karra696f89e2013-01-27 21:31:40 -0800136 REG_CP_IB2_BUFSZ, /* LONG_IB_DETECT_REG_INDEX_END */
Jordan Crouseb5c80482012-10-03 09:38:41 -0600137 0,
Tarun Karra6e750d72013-01-04 10:28:40 -0800138 0,
139 0,
140 0,
141 0,
Jordan Crouseb5c80482012-10-03 09:38:41 -0600142 0
Tarun Karra3335f142012-06-19 14:11:48 -0700143};
144
Tarun Karra696f89e2013-01-27 21:31:40 -0800145const unsigned int ft_detect_regs_count = ARRAY_SIZE(ft_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700146
Jordan Crouse505df9c2011-07-28 08:37:59 -0600147/*
148 * This is the master list of all GPU cores that are supported by this
149 * driver.
150 */
151
152#define ANY_ID (~0)
Tarun Karra9c070822012-11-27 16:43:51 -0700153#define NO_VER (~0)
Jordan Crouse505df9c2011-07-28 08:37:59 -0600154
155static const struct {
156 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600157 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600158 const char *pm4fw;
159 const char *pfpfw;
160 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700161 unsigned int istore_size;
162 unsigned int pix_shader_start;
Tarun Karra9c070822012-11-27 16:43:51 -0700163 /* Size of an instruction in dwords */
164 unsigned int instruction_size;
165 /* size of gmem for gpu*/
166 unsigned int gmem_size;
167 /* version of pm4 microcode that supports sync_lock
168 between CPU and GPU for SMMU-v1 programming */
169 unsigned int sync_lock_pm4_ver;
170 /* version of pfp microcode that supports sync_lock
171 between CPU and GPU for SMMU-v1 programming */
172 unsigned int sync_lock_pfp_ver;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600173} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600174 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700175 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700176 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530177 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
178 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700179 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600180 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700181 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700182 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600183 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700184 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700185 512, 384, 3, SZ_512K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600186 /*
187 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
188 * a hardware problem.
189 */
190 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700191 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700192 1536, 768, 3, SZ_512K, NO_VER, NO_VER },
Carter Cooperf27ec722011-11-17 15:20:38 -0700193 { ADRENO_REV_A225, 2, 2, 0, 6,
194 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700195 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600196 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700197 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700198 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530199 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530200 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530201 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700202 512, 0, 2, SZ_256K, 0x3FF037, 0x3FF016 },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700203 /* A3XX doesn't use the pix_shader_start */
Carter Cooper95f7f792012-08-19 13:40:34 -0600204 { ADRENO_REV_A320, 3, 2, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700205 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700206 512, 0, 2, SZ_512K, 0x3FF037, 0x3FF016 },
liu zhongfd42e622012-05-01 19:18:30 -0700207 { ADRENO_REV_A330, 3, 3, 0, 0,
208 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700209 512, 0, 2, SZ_1M, NO_VER, NO_VER },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600210};
211
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600212static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213{
Jordan Crousea78c9172011-07-11 13:14:09 -0600214 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600215 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216
Jordan Crousea78c9172011-07-11 13:14:09 -0600217 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218
219 if (device->requested_state == KGSL_STATE_NONE) {
220 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700221 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 queue_work(device->work_queue, &device->idle_check_ws);
223 } else if (device->pwrscale.policy != NULL) {
224 queue_work(device->work_queue, &device->idle_check_ws);
225 }
226 }
227
228 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800229 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230 jiffies + device->pwrctrl.interval_timeout);
231 return result;
232}
233
Jordan Crouse9f739212011-07-28 08:37:57 -0600234static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235 struct kgsl_pagetable *pagetable)
236{
237 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
238 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
239
240 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
241
242 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
243
244 kgsl_mmu_unmap(pagetable, &device->memstore);
245
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600246 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247}
248
249static int adreno_setup_pt(struct kgsl_device *device,
250 struct kgsl_pagetable *pagetable)
251{
252 int result = 0;
253 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
254 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
255
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
257 GSL_PT_PAGE_RV);
258 if (result)
259 goto error;
260
261 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
262 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
263 if (result)
264 goto unmap_buffer_desc;
265
266 result = kgsl_mmu_map_global(pagetable, &device->memstore,
267 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
268 if (result)
269 goto unmap_memptrs_desc;
270
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600271 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
273 if (result)
274 goto unmap_memstore_desc;
275
276 return result;
277
278unmap_memstore_desc:
279 kgsl_mmu_unmap(pagetable, &device->memstore);
280
281unmap_memptrs_desc:
282 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
283
284unmap_buffer_desc:
285 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
286
287error:
288 return result;
289}
290
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600291static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600292 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600293 uint32_t flags)
294{
295 unsigned int pt_val, reg_pt_val;
Tarun Karra9c070822012-11-27 16:43:51 -0700296 unsigned int link[250];
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600297 unsigned int *cmds = &link[0];
298 int sizedwords = 0;
299 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600300 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600301 struct kgsl_context *context;
302 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600303
304 if (!adreno_dev->drawctxt_active)
305 return kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700306 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600307
308 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700309 if (context == NULL)
310 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600311 adreno_ctx = context->devctxt;
312
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600313 if (kgsl_mmu_enable_clk(&device->mmu,
314 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700315 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600316
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600317 cmds += __adreno_add_idle_indirect_cmds(cmds,
318 device->mmu.setstate_memory.gpuaddr +
319 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
320
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600321 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600322 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
323 device->mmu.setstate_memory.gpuaddr +
324 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
325 else
326 cmds += adreno_add_bank_change_cmds(cmds,
327 KGSL_IOMMU_CONTEXT_USER,
328 device->mmu.setstate_memory.gpuaddr +
329 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
330
Tarun Karra9c070822012-11-27 16:43:51 -0700331 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
332
333 /* Acquire GPU-CPU sync Lock here */
334 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
335
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700336 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
337 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600338 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600339 /*
340 * We need to perfrom the following operations for all
341 * IOMMU units
342 */
343 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700344 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
345 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600346 /*
347 * Set address of the new pagetable by writng to IOMMU
348 * TTBR0 register
349 */
350 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700351 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
352 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600353 *cmds++ = reg_pt_val;
354 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
355 *cmds++ = 0x00000000;
356
357 /*
358 * Read back the ttbr0 register as a barrier to ensure
359 * above writes have completed
360 */
361 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700362 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
363 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600364 reg_pt_val,
365 device->mmu.setstate_memory.gpuaddr +
366 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600367 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600368 }
369 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
370 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700371 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600372 */
373 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700374 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
375 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700376
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600377 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700378 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
379 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700380 KGSL_IOMMU_CTX_TLBIALL);
381 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600382
383 cmds += __adreno_add_idle_indirect_cmds(cmds,
384 device->mmu.setstate_memory.gpuaddr +
385 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
386
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600387 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700388 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
389 KGSL_IOMMU_CONTEXT_USER,
390 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700391 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600392 device->mmu.setstate_memory.gpuaddr +
393 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
394 }
395 }
396
Tarun Karra9c070822012-11-27 16:43:51 -0700397 /* Release GPU-CPU sync Lock here */
398 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
399
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600400 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600401 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700402 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
403 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600404 device->mmu.setstate_memory.gpuaddr +
405 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
406 else
407 cmds += adreno_add_bank_change_cmds(cmds,
408 KGSL_IOMMU_CONTEXT_PRIV,
409 device->mmu.setstate_memory.gpuaddr +
410 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
411
Tarun Karra9c070822012-11-27 16:43:51 -0700412 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
413
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600414 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600415 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700416 /* invalidate all base pointers */
417 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
418 *cmds++ = 0x7fff;
419 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600420 /* This returns the per context timestamp but we need to
421 * use the global timestamp for iommu clock disablement */
422 adreno_ringbuffer_issuecmds(device, adreno_ctx,
423 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600424 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600425 kgsl_mmu_disable_clk_on_ts(&device->mmu,
426 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600427 }
Tarun Karra9c070822012-11-27 16:43:51 -0700428
429 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
430 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
431 BUG();
432 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600433}
434
435static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600436 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600437 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438{
439 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
440 unsigned int link[32];
441 unsigned int *cmds = &link[0];
442 int sizedwords = 0;
443 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600444 struct kgsl_context *context;
445 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600447 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530448 * Fix target freeze issue by adding TLB flush for each submit
449 * on A20X based targets.
450 */
451 if (adreno_is_a20x(adreno_dev))
452 flags |= KGSL_MMUFLAGS_TLBFLUSH;
453 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600454 * If possible, then set the state via the command stream to avoid
455 * a CPU idle. Otherwise, use the default setstate which uses register
456 * writes For CFF dump we must idle and use the registers so that it is
457 * easier to filter out the mmu accesses from the dump
458 */
459 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600460 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700461 if (context == NULL)
462 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600463 adreno_ctx = context->devctxt;
464
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
466 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600467 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468 *cmds++ = 0x00000000;
469
470 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600471 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700472 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600473 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474 sizedwords += 4;
475 }
476
477 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
478 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600479 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 1);
481 *cmds++ = 0x00000000;
482 sizedwords += 2;
483 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600484 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485 *cmds++ = mh_mmu_invalidate;
486 sizedwords += 2;
487 }
488
489 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600490 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 /* HW workaround: to resolve MMU page fault interrupts
492 * caused by the VGT.It prevents the CP PFP from filling
493 * the VGT DMA request fifo too early,thereby ensuring
494 * that the VGT will not fetch vertex/bin data until
495 * after the page table base register has been updated.
496 *
497 * Two null DRAW_INDX_BIN packets are inserted right
498 * after the page table base update, followed by a
499 * wait for idle. The null packets will fill up the
500 * VGT DMA request fifo and prevent any further
501 * vertex/bin updates from occurring until the wait
502 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600503 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 *cmds++ = (0x4 << 16) |
505 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
506 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600507 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600508 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600509 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 *cmds++ = 0; /* viz query info */
511 *cmds++ = 0x0003C004; /* draw indicator */
512 *cmds++ = 0; /* bin base */
513 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600514 *cmds++ =
515 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600517 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518 *cmds++ = 0; /* viz query info */
519 *cmds++ = 0x0003C004; /* draw indicator */
520 *cmds++ = 0; /* bin base */
521 *cmds++ = 3; /* bin size */
522 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600523 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600525 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 *cmds++ = 0x00000000;
527 sizedwords += 21;
528 }
529
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600530
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600532 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 *cmds++ = 0x7fff; /* invalidate all base pointers */
534 sizedwords += 2;
535 }
536
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600537 adreno_ringbuffer_issuecmds(device, adreno_ctx,
538 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600540 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600541 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600542 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543}
544
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600545static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600546 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600547 uint32_t flags)
548{
549 /* call the mmu specific handler */
550 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600551 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600552 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600553 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600554}
555
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700557a3xx_getchipid(struct kgsl_device *device)
558{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600559 struct kgsl_device_platform_data *pdata =
560 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700561
Jordan Crouse54154c62012-03-27 16:33:26 -0600562 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600563 * All current A3XX chipids are detected at the SOC level. Leave this
564 * function here to support any future GPUs that have working
565 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600566 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700567
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600568 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700569}
570
571static unsigned int
572a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573{
574 unsigned int chipid = 0;
575 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600576 struct kgsl_device_platform_data *pdata =
577 kgsl_device_get_drvdata(device);
578
579 /* If the chip id is set at the platform level, then just use that */
580
581 if (pdata->chipid != 0)
582 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583
584 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
585 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
586 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
587
588 /*
589 * adreno 22x gpus are indicated by coreid 2,
590 * but REG_RBBM_PERIPHID1 always contains 0 for this field
591 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600592 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 chipid = 2 << 24;
594 else
595 chipid = (coreid & 0xF) << 24;
596
597 chipid |= ((majorid >> 4) & 0xF) << 16;
598
599 minorid = ((revid >> 0) & 0xFF);
600
601 patchid = ((revid >> 16) & 0xFF);
602
603 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530604 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605 if (cpu_is_qsd8x50())
606 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530607 else if (cpu_is_msm8625() && minorid == 0)
608 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609
610 chipid |= (minorid << 8) | patchid;
611
612 return chipid;
613}
614
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700615static unsigned int
616adreno_getchipid(struct kgsl_device *device)
617{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600618 struct kgsl_device_platform_data *pdata =
619 kgsl_device_get_drvdata(device);
620
621 /*
622 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
623 * an A2XX processor
624 */
625
626 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700627 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600628 else
629 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700630}
631
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632static inline bool _rev_match(unsigned int id, unsigned int entry)
633{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600634 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636
637static void
638adreno_identify_gpu(struct adreno_device *adreno_dev)
639{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600640 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641
642 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
643
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600644 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
645 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
646 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
647 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648
Jordan Crouse505df9c2011-07-28 08:37:59 -0600649 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
650 if (core == adreno_gpulist[i].core &&
651 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600652 _rev_match(minor, adreno_gpulist[i].minor) &&
653 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 }
656
Jordan Crouse505df9c2011-07-28 08:37:59 -0600657 if (i == ARRAY_SIZE(adreno_gpulist)) {
658 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
659 return;
660 }
661
662 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
663 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
664 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
665 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700666 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
667 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700668 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600669 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Tarun Karra9c070822012-11-27 16:43:51 -0700670 adreno_dev->gpulist_index = i;
671
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672}
673
Lokesh Batra805e1e12012-08-03 08:34:06 -0600674static struct platform_device_id adreno_id_table[] = {
675 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
676 {},
677};
678
679MODULE_DEVICE_TABLE(platform, adreno_id_table);
680
681static struct of_device_id adreno_match_table[] = {
682 { .compatible = "qcom,kgsl-3d0", },
683 {}
684};
685
686static inline int adreno_of_read_property(struct device_node *node,
687 const char *prop, unsigned int *ptr)
688{
689 int ret = of_property_read_u32(node, prop, ptr);
690 if (ret)
691 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
692 return ret;
693}
694
695static struct device_node *adreno_of_find_subnode(struct device_node *parent,
696 const char *name)
697{
698 struct device_node *child;
699
700 for_each_child_of_node(parent, child) {
701 if (of_device_is_compatible(child, name))
702 return child;
703 }
704
705 return NULL;
706}
707
708static int adreno_of_get_pwrlevels(struct device_node *parent,
709 struct kgsl_device_platform_data *pdata)
710{
711 struct device_node *node, *child;
712 int ret = -EINVAL;
713
714 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
715
716 if (node == NULL) {
717 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
718 return -EINVAL;
719 }
720
721 pdata->num_levels = 0;
722
723 for_each_child_of_node(node, child) {
724 unsigned int index;
725 struct kgsl_pwrlevel *level;
726
727 if (adreno_of_read_property(child, "reg", &index))
728 goto done;
729
730 if (index >= KGSL_MAX_PWRLEVELS) {
731 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
732 index);
733 continue;
734 }
735
736 if (index >= pdata->num_levels)
737 pdata->num_levels = index + 1;
738
739 level = &pdata->pwrlevel[index];
740
741 if (adreno_of_read_property(child, "qcom,gpu-freq",
742 &level->gpu_freq))
743 goto done;
744
745 if (adreno_of_read_property(child, "qcom,bus-freq",
746 &level->bus_freq))
747 goto done;
748
749 if (adreno_of_read_property(child, "qcom,io-fraction",
750 &level->io_fraction))
751 level->io_fraction = 0;
752 }
753
754 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
755 &pdata->init_level))
756 pdata->init_level = 1;
757
758 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
759 KGSL_CORE_ERR("Initial power level out of range\n");
760 pdata->init_level = 1;
761 }
762
763 ret = 0;
764done:
765 return ret;
766
767}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600768
769static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
770{
771 struct device_node *node, *child;
772 struct msm_dcvs_core_info *info = NULL;
773 int count = 0;
774 int ret = -EINVAL;
775
776 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
777 if (node == NULL)
778 return ERR_PTR(-EINVAL);
779
780 info = kzalloc(sizeof(*info), GFP_KERNEL);
781
782 if (info == NULL) {
783 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
784 ret = -ENOMEM;
785 goto err;
786 }
787
788 for_each_child_of_node(node, child)
789 count++;
790
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700791 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600792
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700793 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600794 sizeof(struct msm_dcvs_freq_entry),
795 GFP_KERNEL);
796
797 if (info->freq_tbl == NULL) {
798 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700799 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600800 sizeof(struct msm_dcvs_freq_entry));
801 ret = -ENOMEM;
802 goto err;
803 }
804
805 for_each_child_of_node(node, child) {
806 unsigned int index;
807
808 if (adreno_of_read_property(child, "reg", &index))
809 goto err;
810
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700811 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600812 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
813 index);
814 continue;
815 }
816
817 if (adreno_of_read_property(child, "qcom,freq",
818 &info->freq_tbl[index].freq))
819 goto err;
820
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700821 if (adreno_of_read_property(child, "qcom,voltage",
822 &info->freq_tbl[index].voltage))
823 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600824
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700825 if (adreno_of_read_property(child, "qcom,is_trans_level",
826 &info->freq_tbl[index].is_trans_level))
827 info->freq_tbl[index].is_trans_level = 0;
828
829 if (adreno_of_read_property(child, "qcom,active-energy-offset",
830 &info->freq_tbl[index].active_energy_offset))
831 info->freq_tbl[index].active_energy_offset = 0;
832
833 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
834 &info->freq_tbl[index].leakage_energy_offset))
835 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600836 }
837
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700838 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
839 goto err;
840
841 info->sensors = kzalloc(info->num_cores *
842 sizeof(int),
843 GFP_KERNEL);
844
845 for (count = 0; count < info->num_cores; count++) {
846 if (adreno_of_read_property(node, "qcom,sensors",
847 &(info->sensors[count])))
848 goto err;
849 }
850
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700851 if (adreno_of_read_property(node, "qcom,core-core-type",
852 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600853 goto err;
854
855 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
856 &info->algo_param.disable_pc_threshold))
857 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700858 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
859 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600860 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700861 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
862 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600863 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600864 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
865 &info->algo_param.em_max_util_pct))
866 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700867 if (adreno_of_read_property(node, "qcom,algo-group-id",
868 &info->algo_param.group_id))
869 goto err;
870 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
871 &info->algo_param.max_freq_chg_time_us))
872 goto err;
873 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
874 &info->algo_param.slack_mode_dynamic))
875 goto err;
876 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
877 &info->algo_param.slack_weight_thresh_pct))
878 goto err;
879 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
880 &info->algo_param.slack_time_min_us))
881 goto err;
882 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
883 &info->algo_param.slack_time_max_us))
884 goto err;
885 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
886 &info->algo_param.ss_win_size_min_us))
887 goto err;
888 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
889 &info->algo_param.ss_win_size_max_us))
890 goto err;
891 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
892 &info->algo_param.ss_util_pct))
893 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800894 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
895 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600896 goto err;
897
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700898 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
899 &info->energy_coeffs.active_coeff_a))
900 goto err;
901 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
902 &info->energy_coeffs.active_coeff_b))
903 goto err;
904 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
905 &info->energy_coeffs.active_coeff_c))
906 goto err;
907 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
908 &info->energy_coeffs.leakage_coeff_a))
909 goto err;
910 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
911 &info->energy_coeffs.leakage_coeff_b))
912 goto err;
913 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
914 &info->energy_coeffs.leakage_coeff_c))
915 goto err;
916 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
917 &info->energy_coeffs.leakage_coeff_d))
918 goto err;
919
920 if (adreno_of_read_property(node, "qcom,power-current-temp",
921 &info->power_param.current_temp))
922 goto err;
923
Lokesh Batra805e1e12012-08-03 08:34:06 -0600924 return info;
925
926err:
927 if (info)
928 kfree(info->freq_tbl);
929
930 kfree(info);
931
932 return ERR_PTR(ret);
933}
934
935static int adreno_of_get_iommu(struct device_node *parent,
936 struct kgsl_device_platform_data *pdata)
937{
938 struct device_node *node, *child;
939 struct kgsl_device_iommu_data *data = NULL;
940 struct kgsl_iommu_ctx *ctxs = NULL;
941 u32 reg_val[2];
942 int ctx_index = 0;
943
944 node = of_parse_phandle(parent, "iommu", 0);
945 if (node == NULL)
946 return -EINVAL;
947
948 data = kzalloc(sizeof(*data), GFP_KERNEL);
949 if (data == NULL) {
950 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
951 goto err;
952 }
953
954 if (of_property_read_u32_array(node, "reg", reg_val, 2))
955 goto err;
956
957 data->physstart = reg_val[0];
958 data->physend = data->physstart + reg_val[1] - 1;
959
960 data->iommu_ctx_count = 0;
961
962 for_each_child_of_node(node, child)
963 data->iommu_ctx_count++;
964
965 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
966 GFP_KERNEL);
967
968 if (ctxs == NULL) {
969 KGSL_CORE_ERR("kzalloc(%d) failed\n",
970 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
971 goto err;
972 }
973
974 for_each_child_of_node(node, child) {
975 int ret = of_property_read_string(child, "label",
976 &ctxs[ctx_index].iommu_ctx_name);
977
978 if (ret) {
979 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
980 goto err;
981 }
982
983 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
984 &ctxs[ctx_index].ctx_id))
985 goto err;
986
987 ctx_index++;
988 }
989
990 data->iommu_ctxs = ctxs;
991
992 pdata->iommu_data = data;
993 pdata->iommu_count = 1;
994
995 return 0;
996
997err:
998 kfree(ctxs);
999 kfree(data);
1000
1001 return -EINVAL;
1002}
1003
1004static int adreno_of_get_pdata(struct platform_device *pdev)
1005{
1006 struct kgsl_device_platform_data *pdata = NULL;
1007 struct kgsl_device *device;
1008 int ret = -EINVAL;
1009
1010 pdev->id_entry = adreno_id_table;
1011
1012 pdata = pdev->dev.platform_data;
1013 if (pdata)
1014 return 0;
1015
1016 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1017 KGSL_CORE_ERR("Unable to read 'label'\n");
1018 goto err;
1019 }
1020
1021 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1022 goto err;
1023
1024 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1025 if (pdata == NULL) {
1026 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1027 ret = -ENOMEM;
1028 goto err;
1029 }
1030
1031 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1032 &pdata->chipid))
1033 goto err;
1034
1035 /* pwrlevel Data */
1036 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1037 if (ret)
1038 goto err;
1039
1040 /* Default value is 83, if not found in DT */
1041 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1042 &pdata->idle_timeout))
1043 pdata->idle_timeout = 83;
1044
1045 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1046 &pdata->nap_allowed))
1047 pdata->nap_allowed = 1;
1048
1049 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1050 &pdata->clk_map))
1051 goto err;
1052
1053 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1054
1055 if (device->id != KGSL_DEVICE_3D0)
1056 goto err;
1057
1058 /* Bus Scale Data */
1059
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001060 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001061 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1062 ret = PTR_ERR(pdata->bus_scale_table);
1063 goto err;
1064 }
1065
1066 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1067 if (IS_ERR_OR_NULL(pdata->core_info)) {
1068 ret = PTR_ERR(pdata->core_info);
1069 goto err;
1070 }
1071
1072 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1073 if (ret)
1074 goto err;
1075
1076 pdev->dev.platform_data = pdata;
1077 return 0;
1078
1079err:
1080 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001081 if (pdata->core_info)
1082 kfree(pdata->core_info->freq_tbl);
1083 kfree(pdata->core_info);
1084
1085 if (pdata->iommu_data)
1086 kfree(pdata->iommu_data->iommu_ctxs);
1087
1088 kfree(pdata->iommu_data);
1089 }
1090
1091 kfree(pdata);
1092
1093 return ret;
1094}
1095
liu zhong7dfa2a32012-04-27 19:11:01 -07001096#ifdef CONFIG_MSM_OCMEM
1097static int
1098adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1099{
Jordan Crousec0978202012-08-29 14:35:51 -06001100 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001101 return 0;
1102
1103 /* OCMEM is only needed once, do not support consective allocation */
1104 if (adreno_dev->ocmem_hdl != NULL)
1105 return 0;
1106
1107 adreno_dev->ocmem_hdl =
1108 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1109 if (adreno_dev->ocmem_hdl == NULL)
1110 return -ENOMEM;
1111
1112 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001113 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001114
1115 return 0;
1116}
1117
1118static void
1119adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1120{
Jordan Crousec0978202012-08-29 14:35:51 -06001121 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001122 return;
1123
1124 if (adreno_dev->ocmem_hdl == NULL)
1125 return;
1126
1127 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1128 adreno_dev->ocmem_hdl = NULL;
1129}
1130#else
1131static int
1132adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1133{
1134 return 0;
1135}
1136
1137static void
1138adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1139{
1140}
1141#endif
1142
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143static int __devinit
1144adreno_probe(struct platform_device *pdev)
1145{
1146 struct kgsl_device *device;
1147 struct adreno_device *adreno_dev;
1148 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001149 bool is_dt;
1150
1151 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1152
1153 if (is_dt && pdev->dev.of_node) {
1154 status = adreno_of_get_pdata(pdev);
1155 if (status)
1156 goto error_return;
1157 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158
1159 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1160 adreno_dev = ADRENO_DEVICE(device);
1161 device->parentdev = &pdev->dev;
1162
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163 status = adreno_ringbuffer_init(device);
1164 if (status != 0)
1165 goto error;
1166
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001167 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001168 if (status)
1169 goto error_close_rb;
1170
1171 adreno_debugfs_init(device);
1172
1173 kgsl_pwrscale_init(device);
1174 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1175
1176 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1177 return 0;
1178
1179error_close_rb:
1180 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1181error:
1182 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001183error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001184 return status;
1185}
1186
1187static int __devexit adreno_remove(struct platform_device *pdev)
1188{
1189 struct kgsl_device *device;
1190 struct adreno_device *adreno_dev;
1191
1192 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1193 adreno_dev = ADRENO_DEVICE(device);
1194
1195 kgsl_pwrscale_detach_policy(device);
1196 kgsl_pwrscale_close(device);
1197
1198 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1199 kgsl_device_platform_remove(device);
1200
1201 return 0;
1202}
1203
1204static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1205{
1206 int status = -EINVAL;
1207 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208
Tarun Karrad20d71a2013-01-25 15:38:57 -08001209 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001210 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211
1212 /* Power up the device */
1213 kgsl_pwrctrl_enable(device);
1214
1215 /* Identify the specific GPU */
1216 adreno_identify_gpu(adreno_dev);
1217
Tarun Karra9c070822012-11-27 16:43:51 -07001218 if (adreno_ringbuffer_read_pm4_ucode(device)) {
1219 KGSL_DRV_ERR(device, "Reading pm4 microcode failed %s\n",
1220 adreno_dev->pm4_fwfile);
1221 BUG_ON(1);
1222 }
1223
1224 if (adreno_ringbuffer_read_pfp_ucode(device)) {
1225 KGSL_DRV_ERR(device, "Reading pfp microcode failed %s\n",
1226 adreno_dev->pfp_fwfile);
1227 BUG_ON(1);
1228 }
1229
Jordan Crouse505df9c2011-07-28 08:37:59 -06001230 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1231 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1232 adreno_dev->chip_id);
1233 goto error_clk_off;
1234 }
1235
Tarun Karra9c070822012-11-27 16:43:51 -07001236
1237 /*
1238 * Check if firmware supports the sync lock PM4 packets needed
1239 * for IOMMUv1
1240 */
1241
1242 if ((adreno_dev->pm4_fw_version >=
1243 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pm4_ver) &&
1244 (adreno_dev->pfp_fw_version >=
1245 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pfp_ver))
1246 device->mmu.flags |= KGSL_MMU_FLAGS_IOMMU_SYNC;
1247
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001248 /* Set up the MMU */
1249 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001250 /*
1251 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1252 * on older gpus
1253 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001254 if (adreno_is_a20x(adreno_dev)) {
1255 device->mh.mh_intf_cfg1 = 0;
1256 device->mh.mh_intf_cfg2 = 0;
1257 }
1258
1259 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001260 }
1261
Tarun Karra3335f142012-06-19 14:11:48 -07001262 /* Assign correct RBBM status register to hang detect regs
1263 */
Tarun Karra696f89e2013-01-27 21:31:40 -08001264 ft_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
Tarun Karra3335f142012-06-19 14:11:48 -07001265
Jordan Crouseb5c80482012-10-03 09:38:41 -06001266 /* Add A3XX specific registers for hang detection */
1267 if (adreno_is_a3xx(adreno_dev)) {
Tarun Karra696f89e2013-01-27 21:31:40 -08001268 ft_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1269 ft_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
1270 ft_detect_regs[8] = A3XX_RBBM_PERFCTR_SP_6_LO;
1271 ft_detect_regs[9] = A3XX_RBBM_PERFCTR_SP_6_HI;
1272 ft_detect_regs[10] = A3XX_RBBM_PERFCTR_SP_5_LO;
1273 ft_detect_regs[11] = A3XX_RBBM_PERFCTR_SP_5_HI;
Jordan Crouseb5c80482012-10-03 09:38:41 -06001274 }
1275
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001276 status = kgsl_mmu_start(device);
1277 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278 goto error_clk_off;
1279
liu zhong7dfa2a32012-04-27 19:11:01 -07001280 status = adreno_ocmem_gmem_malloc(adreno_dev);
1281 if (status) {
1282 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1283 goto error_mmu_off;
1284 }
1285
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001286 /* Start the GPU */
1287 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288
1289 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001290 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001291
1292 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001293 if (status == 0) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001294 /* While fault tolerance is on we do not want timer to
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001295 * fire and attempt to change any device state */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001296 if (KGSL_STATE_DUMP_AND_FT != device->state)
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001297 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001298 return 0;
1299 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001302
1303error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001304 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001305
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306error_clk_off:
1307 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001308
1309 return status;
1310}
1311
1312static int adreno_stop(struct kgsl_device *device)
1313{
1314 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1315
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001316 adreno_dev->drawctxt_active = NULL;
1317
1318 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1319
Shubhraprakash Das79447952012-04-26 18:12:23 -06001320 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001321
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001322 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301323 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001324 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001325
liu zhong7dfa2a32012-04-27 19:11:01 -07001326 adreno_ocmem_gmem_free(adreno_dev);
1327
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001328 /* Power down the device */
1329 kgsl_pwrctrl_disable(device);
1330
1331 return 0;
1332}
1333
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001334static void adreno_mark_context_status(struct kgsl_device *device,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001335 int ft_status)
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001336{
1337 struct kgsl_context *context;
1338 int next = 0;
1339 /*
1340 * Set the reset status of all contexts to
1341 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
Tarun Karrad20d71a2013-01-25 15:38:57 -08001342 * since thats the guilty party, if fault tolerance failed then
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001343 * mark all as guilty
1344 */
1345 while ((context = idr_get_next(&device->context_idr, &next))) {
1346 struct adreno_context *adreno_context = context->devctxt;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001347 if (ft_status) {
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001348 context->reset_status =
1349 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1350 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1351 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1352 context->reset_status) {
Carter Cooper8179f5a2012-12-17 11:32:27 -07001353 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG |
Tarun Karrad20d71a2013-01-25 15:38:57 -08001354 CTXT_FLAGS_GPU_HANG_FT))
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001355 context->reset_status =
1356 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1357 else
1358 context->reset_status =
1359 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1360 }
1361 next = next + 1;
1362 }
1363}
1364
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001365static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1366{
1367 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1368 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1369 struct kgsl_context *context;
1370 struct adreno_context *temp_adreno_context;
1371 int next = 0;
1372
1373 while ((context = idr_get_next(&device->context_idr, &next))) {
1374 temp_adreno_context = context->devctxt;
1375 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1376 kgsl_sharedmem_writel(&device->memstore,
1377 KGSL_MEMSTORE_OFFSET(context->id,
1378 soptimestamp),
1379 rb->timestamp[context->id]);
1380 kgsl_sharedmem_writel(&device->memstore,
1381 KGSL_MEMSTORE_OFFSET(context->id,
1382 eoptimestamp),
1383 rb->timestamp[context->id]);
1384 }
1385 next = next + 1;
1386 }
1387}
1388
Tarun Karrad20d71a2013-01-25 15:38:57 -08001389static void adreno_destroy_ft_data(struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001390{
Tarun Karrad20d71a2013-01-25 15:38:57 -08001391 vfree(ft_data->rb_buffer);
1392 vfree(ft_data->bad_rb_buffer);
1393 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001394}
1395
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001396static int _find_start_of_cmd_seq(struct adreno_ringbuffer *rb,
1397 unsigned int *ptr,
1398 bool inc)
1399{
1400 int status = -EINVAL;
1401 unsigned int val1;
1402 unsigned int size = rb->buffer_desc.size;
1403 unsigned int start_ptr = *ptr;
1404
1405 while ((start_ptr / sizeof(unsigned int)) != rb->wptr) {
1406 if (inc)
1407 start_ptr = adreno_ringbuffer_inc_wrapped(start_ptr,
1408 size);
1409 else
1410 start_ptr = adreno_ringbuffer_dec_wrapped(start_ptr,
1411 size);
1412 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, start_ptr);
1413 if (KGSL_CMD_IDENTIFIER == val1) {
1414 if ((start_ptr / sizeof(unsigned int)) != rb->wptr)
1415 start_ptr = adreno_ringbuffer_dec_wrapped(
1416 start_ptr, size);
1417 *ptr = start_ptr;
1418 status = 0;
1419 break;
1420 }
1421 }
1422 return status;
1423}
1424
1425static int _find_cmd_seq_after_eop_ts(struct adreno_ringbuffer *rb,
1426 unsigned int *rb_rptr,
1427 unsigned int global_eop,
1428 bool inc)
1429{
1430 int status = -EINVAL;
1431 unsigned int temp_rb_rptr = *rb_rptr;
1432 unsigned int size = rb->buffer_desc.size;
1433 unsigned int val[3];
1434 int i = 0;
1435 bool check = false;
1436
1437 if (inc && temp_rb_rptr / sizeof(unsigned int) != rb->wptr)
1438 return status;
1439
1440 do {
1441 /*
1442 * when decrementing we need to decrement first and
1443 * then read make sure we cover all the data
1444 */
1445 if (!inc)
1446 temp_rb_rptr = adreno_ringbuffer_dec_wrapped(
1447 temp_rb_rptr, size);
1448 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i],
1449 temp_rb_rptr);
1450
1451 if (check && ((inc && val[i] == global_eop) ||
1452 (!inc && (val[i] ==
1453 cp_type3_packet(CP_MEM_WRITE, 2) ||
1454 val[i] == CACHE_FLUSH_TS)))) {
1455 /* decrement i, i.e i = (i - 1 + 3) % 3 if
1456 * we are going forward, else increment i */
1457 i = (i + 2) % 3;
1458 if (val[i] == rb->device->memstore.gpuaddr +
1459 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1460 eoptimestamp)) {
1461 int j = ((i + 2) % 3);
1462 if ((inc && (val[j] == CACHE_FLUSH_TS ||
1463 val[j] == cp_type3_packet(
1464 CP_MEM_WRITE, 2))) ||
1465 (!inc && val[j] == global_eop)) {
1466 /* Found the global eop */
1467 status = 0;
1468 break;
1469 }
1470 }
1471 /* if no match found then increment i again
1472 * since we decremented before matching */
1473 i = (i + 1) % 3;
1474 }
1475 if (inc)
1476 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(
1477 temp_rb_rptr, size);
1478
1479 i = (i + 1) % 3;
1480 if (2 == i)
1481 check = true;
1482 } while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr);
1483 /* temp_rb_rptr points to the command stream after global eop,
1484 * move backward till the start of command sequence */
1485 if (!status) {
1486 status = _find_start_of_cmd_seq(rb, &temp_rb_rptr, false);
1487 if (!status) {
1488 *rb_rptr = temp_rb_rptr;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001489 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001490 "Offset of cmd sequence after eop timestamp: 0x%x\n",
1491 temp_rb_rptr / sizeof(unsigned int));
1492 }
1493 }
1494 if (status)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001495 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001496 "Failed to find the command sequence after eop timestamp\n");
1497 return status;
1498}
1499
1500static int _find_hanging_ib_sequence(struct adreno_ringbuffer *rb,
1501 unsigned int *rb_rptr,
1502 unsigned int ib1)
1503{
1504 int status = -EINVAL;
1505 unsigned int temp_rb_rptr = *rb_rptr;
1506 unsigned int size = rb->buffer_desc.size;
1507 unsigned int val[2];
1508 int i = 0;
1509 bool check = false;
1510 bool ctx_switch = false;
1511
1512 while (temp_rb_rptr / sizeof(unsigned int) != rb->wptr) {
1513 kgsl_sharedmem_readl(&rb->buffer_desc, &val[i], temp_rb_rptr);
1514
1515 if (check && val[i] == ib1) {
1516 /* decrement i, i.e i = (i - 1 + 2) % 2 */
1517 i = (i + 1) % 2;
1518 if (adreno_cmd_is_ib(val[i])) {
1519 /* go till start of command sequence */
1520 status = _find_start_of_cmd_seq(rb,
1521 &temp_rb_rptr, false);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001522
1523 KGSL_FT_INFO(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001524 "Found the hanging IB at offset 0x%x\n",
1525 temp_rb_rptr / sizeof(unsigned int));
1526 break;
1527 }
1528 /* if no match the increment i since we decremented
1529 * before checking */
1530 i = (i + 1) % 2;
1531 }
1532 /* Make sure you do not encounter a context switch twice, we can
1533 * encounter it once for the bad context as the start of search
1534 * can point to the context switch */
1535 if (val[i] == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
1536 if (ctx_switch) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001537 KGSL_FT_ERR(rb->device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001538 "Context switch encountered before bad "
1539 "IB found\n");
1540 break;
1541 }
1542 ctx_switch = true;
1543 }
1544 i = (i + 1) % 2;
1545 if (1 == i)
1546 check = true;
1547 temp_rb_rptr = adreno_ringbuffer_inc_wrapped(temp_rb_rptr,
1548 size);
1549 }
1550 if (!status)
1551 *rb_rptr = temp_rb_rptr;
1552 return status;
1553}
1554
Tarun Karrad20d71a2013-01-25 15:38:57 -08001555static int adreno_setup_ft_data(struct kgsl_device *device,
1556 struct adreno_ft_data *ft_data)
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001557{
1558 int ret = 0;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001559 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1560 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001561 struct kgsl_context *context;
1562 struct adreno_context *adreno_context;
1563 unsigned int rb_rptr = rb->wptr * sizeof(unsigned int);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001564
Tarun Karrad20d71a2013-01-25 15:38:57 -08001565 memset(ft_data, 0, sizeof(*ft_data));
1566 ft_data->start_of_replay_cmds = 0xFFFFFFFF;
1567 ft_data->replay_for_snapshot = 0xFFFFFFFF;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001568
Tarun Karrad20d71a2013-01-25 15:38:57 -08001569 adreno_regread(device, REG_CP_IB1_BASE, &ft_data->ib1);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001570
Tarun Karrad20d71a2013-01-25 15:38:57 -08001571 kgsl_sharedmem_readl(&device->memstore, &ft_data->context_id,
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001572 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1573 current_context));
1574
1575 kgsl_sharedmem_readl(&device->memstore,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001576 &ft_data->global_eop,
Tarun Karradeeecc02013-01-21 23:42:17 -08001577 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1578 eoptimestamp));
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001579
Tarun Karrad20d71a2013-01-25 15:38:57 -08001580 ft_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1581 if (!ft_data->rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001582 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1583 rb->buffer_desc.size);
1584 return -ENOMEM;
1585 }
1586
Tarun Karrad20d71a2013-01-25 15:38:57 -08001587 ft_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1588 if (!ft_data->bad_rb_buffer) {
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001589 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1590 rb->buffer_desc.size);
1591 ret = -ENOMEM;
1592 goto done;
1593 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001594
Tarun Karrad20d71a2013-01-25 15:38:57 -08001595 ft_data->good_rb_buffer = vmalloc(rb->buffer_desc.size);
1596 if (!ft_data->good_rb_buffer) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001597 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1598 rb->buffer_desc.size);
1599 ret = -ENOMEM;
1600 goto done;
1601 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001602 ft_data->fault = device->mmu.fault;
1603 ft_data->step = adreno_dev->ft_policy;
Tarun Karradeeecc02013-01-21 23:42:17 -08001604
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001605 /* find the start of bad command sequence in rb */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001606 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001607 /* Look for the command stream that is right after the global eop */
1608
1609 if (!context) {
1610 /*
Tarun Karrad20d71a2013-01-25 15:38:57 -08001611 * If there is no context then fault tolerance does not need to
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001612 * replay anything, just reset GPU and thats it
1613 */
1614 goto done;
1615 }
1616 ret = _find_cmd_seq_after_eop_ts(rb, &rb_rptr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001617 ft_data->global_eop + 1, false);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001618 if (ret)
1619 goto done;
1620
Tarun Karrad20d71a2013-01-25 15:38:57 -08001621 ft_data->start_of_replay_cmds = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001622
1623 adreno_context = context->devctxt;
1624 if (adreno_context->flags & CTXT_FLAGS_PREAMBLE) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001625 if (ft_data->ib1) {
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001626 ret = _find_hanging_ib_sequence(rb,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001627 &rb_rptr, ft_data->ib1);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001628 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001629 KGSL_FT_ERR(device,
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001630 "Start not found for replay IB sequence\n");
1631 ret = 0;
1632 goto done;
1633 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08001634 ft_data->start_of_replay_cmds = rb_rptr;
1635 ft_data->replay_for_snapshot = rb_rptr;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001636 }
1637 }
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001638
1639done:
1640 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001641 vfree(ft_data->rb_buffer);
1642 vfree(ft_data->bad_rb_buffer);
1643 vfree(ft_data->good_rb_buffer);
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001644 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001645 return ret;
1646}
1647
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001648static int
Tarun Karra696f89e2013-01-27 21:31:40 -08001649_adreno_check_long_ib(struct kgsl_device *device)
1650{
1651 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1652 unsigned int curr_global_ts = 0;
1653
1654 /* check if the global ts is still the same */
1655 kgsl_sharedmem_readl(&device->memstore,
1656 &curr_global_ts,
1657 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1658 eoptimestamp));
1659
1660 /* Mark long ib as handled */
1661 adreno_dev->long_ib = 0;
1662
1663 if (curr_global_ts == adreno_dev->long_ib_ts) {
1664 KGSL_FT_ERR(device,
1665 "IB ran too long, invalidate ctxt\n");
1666 return 1;
1667 } else {
1668 /* Do nothing GPU has gone ahead */
1669 KGSL_FT_INFO(device, "false long ib detection return\n");
1670 return 0;
1671 }
1672}
1673
1674static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001675_adreno_ft_restart_device(struct kgsl_device *device,
Tarun Karra696f89e2013-01-27 21:31:40 -08001676 struct kgsl_context *context,
1677 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001678{
Tarun Karradeeecc02013-01-21 23:42:17 -08001679
1680 struct adreno_context *adreno_context = context->devctxt;
1681
1682 /* restart device */
1683 if (adreno_stop(device)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001684 KGSL_FT_ERR(device, "Device stop failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001685 return 1;
1686 }
1687
1688 if (adreno_start(device, true)) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001689 KGSL_FT_ERR(device, "Device start failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001690 return 1;
1691 }
1692
1693 if (context)
1694 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1695 KGSL_MEMSTORE_GLOBAL);
1696
1697 /* If iommu is used then we need to make sure that the iommu clocks
1698 * are on since there could be commands in pipeline that touch iommu */
1699 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1700 if (kgsl_mmu_enable_clk(&device->mmu,
1701 KGSL_IOMMU_CONTEXT_USER))
1702 return 1;
1703 }
1704
1705 return 0;
1706}
1707
Tarun Karra696f89e2013-01-27 21:31:40 -08001708static inline void
1709_adreno_debug_ft_info(struct kgsl_device *device,
1710 struct adreno_ft_data *ft_data)
1711{
1712
1713 /*
1714 * Dumping rb is a very useful tool to debug FT.
1715 * It will tell us if we are extracting the rb correctly
1716 * NOP'ing the right IB, skipping the EOF correctly etc.
1717 */
1718 if (device->ft_log >= 7) {
1719
1720 /* Print fault tolerance data here */
1721 KGSL_FT_INFO(device, "Temp RB buffer size 0x%X\n",
1722 ft_data->rb_size);
1723 adreno_dump_rb(device, ft_data->rb_buffer,
1724 ft_data->rb_size<<2, 0, ft_data->rb_size);
1725
1726 KGSL_FT_INFO(device, "Bad RB buffer size 0x%X\n",
1727 ft_data->bad_rb_size);
1728 adreno_dump_rb(device, ft_data->bad_rb_buffer,
1729 ft_data->bad_rb_size<<2, 0, ft_data->bad_rb_size);
1730
1731 KGSL_FT_INFO(device, "Good RB buffer size 0x%X\n",
1732 ft_data->good_rb_size);
1733 adreno_dump_rb(device, ft_data->good_rb_buffer,
1734 ft_data->good_rb_size<<2, 0, ft_data->good_rb_size);
1735
1736 }
1737}
1738
Tarun Karradeeecc02013-01-21 23:42:17 -08001739static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001740_adreno_ft_resubmit_rb(struct kgsl_device *device,
Tarun Karradeeecc02013-01-21 23:42:17 -08001741 struct adreno_ringbuffer *rb,
1742 struct kgsl_context *context,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001743 struct adreno_ft_data *ft_data,
Tarun Karradeeecc02013-01-21 23:42:17 -08001744 unsigned int *buff, unsigned int size)
1745{
1746 unsigned int ret = 0;
1747
Tarun Karra696f89e2013-01-27 21:31:40 -08001748 _adreno_debug_ft_info(device, ft_data);
1749
Tarun Karrad20d71a2013-01-25 15:38:57 -08001750 if (_adreno_ft_restart_device(device, context, ft_data))
Tarun Karradeeecc02013-01-21 23:42:17 -08001751 return 1;
1752
1753 if (size) {
1754
1755 /* submit commands and wait for them to pass */
1756 adreno_ringbuffer_restore(rb, buff, size);
1757
1758 ret = adreno_idle(device);
1759 }
1760
1761 return ret;
1762}
1763
1764
1765static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001766_adreno_ft(struct kgsl_device *device,
1767 struct adreno_ft_data *ft_data)
Tarun Karradeeecc02013-01-21 23:42:17 -08001768{
1769 int ret = 0, i;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001770 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1771 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1772 struct kgsl_context *context;
1773 struct adreno_context *adreno_context = NULL;
1774 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1775
Tarun Karrad20d71a2013-01-25 15:38:57 -08001776 context = idr_find(&device->context_idr, ft_data->context_id);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001777 if (context == NULL) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001778 KGSL_FT_CRIT(device, "Last context unknown id:%d\n",
1779 ft_data->context_id);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001780 } else {
1781 adreno_context = context->devctxt;
1782 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001783 /*
1784 * set the invalid ts flag to 0 for this context since we have
1785 * detected a hang for it
1786 */
1787 context->wait_on_invalid_ts = false;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001788
1789 KGSL_FT_INFO(device, "Context found\n");
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001790 }
1791
1792 /* Extract valid contents from rb which can still be executed after
1793 * hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001794 adreno_ringbuffer_extract(rb, ft_data);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001795
Tarun Karra696f89e2013-01-27 21:31:40 -08001796 /* Check if we detected a long running IB,
1797 * if true do not attempt replay of bad cmds */
1798 if (adreno_dev->long_ib) {
1799 if (_adreno_check_long_ib(device)) {
1800 _adreno_debug_ft_info(device, ft_data);
1801 goto play_good_cmds;
1802 } else {
1803 adreno_context->flags &= ~CTXT_FLAGS_GPU_HANG;
1804 return 0;
1805 }
1806 }
1807
Tarun Karradeeecc02013-01-21 23:42:17 -08001808 /* Do not try the bad commands if hang is due to a fault */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001809 if (ft_data->fault) {
1810 KGSL_FT_ERR(device, "Page fault no FT for bad context\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001811 goto play_good_cmds;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001812 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001813
Tarun Karrad20d71a2013-01-25 15:38:57 -08001814 if (ft_data->step == FT_REPLAY_BAD_CTXT_CMDS) {
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001815
Tarun Karrad20d71a2013-01-25 15:38:57 -08001816 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1817 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001818
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001819 if (ret)
Tarun Karrad20d71a2013-01-25 15:38:57 -08001820 KGSL_FT_INFO(device, "Replay unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001821 else
1822 goto play_good_cmds;
1823
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001824 }
1825
Tarun Karrad20d71a2013-01-25 15:38:57 -08001826 if (ft_data->step == FT_NOP_IB_BAD_CTXT_CMDS) {
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001827
Tarun Karrad20d71a2013-01-25 15:38:57 -08001828 for (i = 0; i < ft_data->bad_rb_size; i++) {
1829 if ((ft_data->bad_rb_buffer[i] ==
1830 CP_HDR_INDIRECT_BUFFER_PFD) &&
1831 (ft_data->bad_rb_buffer[i+1] == ft_data->ib1)) {
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001832
Tarun Karrad20d71a2013-01-25 15:38:57 -08001833 ft_data->bad_rb_buffer[i] = cp_nop_packet(2);
1834 ft_data->bad_rb_buffer[i+1] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001835 KGSL_NOP_IB_IDENTIFIER;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001836 ft_data->bad_rb_buffer[i+2] =
Tarun Karradeeecc02013-01-21 23:42:17 -08001837 KGSL_NOP_IB_IDENTIFIER;
1838 break;
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001839 }
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001840 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001841
Tarun Karrad20d71a2013-01-25 15:38:57 -08001842 if ((i == (ft_data->bad_rb_size)) || (!ft_data->ib1)) {
1843 KGSL_FT_ERR(device, "Bad IB to NOP not found\n");
1844 ft_data->step = FT_FAIL_BAD_CTXT_CMDS;
Tarun Karradeeecc02013-01-21 23:42:17 -08001845 goto play_good_cmds;
1846 }
1847
Tarun Karrad20d71a2013-01-25 15:38:57 -08001848 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1849 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001850
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001851 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001852 KGSL_FT_INFO(device, "NOP faulty IB unsuccessful\n");
1853 ft_data->step = FT_SKIP_EOF_BAD_CTXT_CMDS;
Tarun Karradeeecc02013-01-21 23:42:17 -08001854 } else
1855 goto play_good_cmds;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001856 }
Tarun Karradeeecc02013-01-21 23:42:17 -08001857
Tarun Karrad20d71a2013-01-25 15:38:57 -08001858 if (ft_data->step == FT_SKIP_EOF_BAD_CTXT_CMDS) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001859
Tarun Karrad20d71a2013-01-25 15:38:57 -08001860 for (i = 0; i < ft_data->bad_rb_size; i++) {
1861 if (ft_data->bad_rb_buffer[i] ==
1862 KGSL_END_OF_FRAME_IDENTIFIER) {
1863 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001864 break;
1865 }
1866 }
1867
1868 /* EOF not found in RB, discard till EOF in
1869 next IB submission */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001870 if (i == ft_data->bad_rb_size) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001871 adreno_context->flags |= CTXT_FLAGS_SKIP_EOF;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001872 ft_data->bad_rb_buffer[0] = cp_nop_packet(i);
Tarun Karradeeecc02013-01-21 23:42:17 -08001873 }
1874
Tarun Karrad20d71a2013-01-25 15:38:57 -08001875 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1876 ft_data->bad_rb_buffer, ft_data->bad_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001877
1878 if (ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001879 KGSL_FT_INFO(device, "Skip EOF unsuccessful\n");
1880 ft_data->step = FT_FAIL_BAD_CTXT_CMDS;
Tarun Karradeeecc02013-01-21 23:42:17 -08001881 } else
1882 goto play_good_cmds;
1883 }
1884
1885play_good_cmds:
1886
Tarun Karrad20d71a2013-01-25 15:38:57 -08001887 if (ft_data->step == FT_FAIL_BAD_CTXT_CMDS)
1888 KGSL_FT_ERR(device, "Bad context commands failed\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001889 else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001890 KGSL_FT_INFO(device, "Bad context commands success\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001891
1892 if (adreno_context) {
1893 adreno_context->flags = (adreno_context->flags &
Tarun Karrad20d71a2013-01-25 15:38:57 -08001894 ~CTXT_FLAGS_GPU_HANG) | CTXT_FLAGS_GPU_HANG_FT;
Tarun Karradeeecc02013-01-21 23:42:17 -08001895 }
1896 adreno_dev->drawctxt_active = last_active_ctx;
1897 }
1898
Tarun Karrad20d71a2013-01-25 15:38:57 -08001899 ret = _adreno_ft_resubmit_rb(device, rb, context, ft_data,
1900 ft_data->good_rb_buffer, ft_data->good_rb_size);
Tarun Karradeeecc02013-01-21 23:42:17 -08001901
1902 if (ret) {
1903 /* If we fail here we can try to invalidate another
1904 * context and try fault tolerance again */
1905 ret = -EAGAIN;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001906 KGSL_FT_ERR(device, "Playing good commands unsuccessful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001907 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001908 } else
1909 KGSL_FT_INFO(device, "Playing good commands successful\n");
Tarun Karradeeecc02013-01-21 23:42:17 -08001910
1911 /* ringbuffer now has data from the last valid context id,
1912 * so restore the active_ctx to the last valid context */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001913 if (ft_data->last_valid_ctx_id) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001914 struct kgsl_context *last_ctx =
1915 idr_find(&device->context_idr,
Tarun Karrad20d71a2013-01-25 15:38:57 -08001916 ft_data->last_valid_ctx_id);
Tarun Karradeeecc02013-01-21 23:42:17 -08001917 if (last_ctx)
1918 adreno_dev->drawctxt_active = last_ctx->devctxt;
1919 }
1920
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001921done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001922 /* Turn off iommu clocks */
1923 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1924 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001925 return ret;
1926}
1927
1928static int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001929adreno_ft(struct kgsl_device *device,
1930 struct adreno_ft_data *ft_data)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001931{
1932 int ret = 0;
1933 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1934 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1935 unsigned int timestamp;
1936
Tarun Karrad20d71a2013-01-25 15:38:57 -08001937 KGSL_FT_INFO(device,
1938 "Start Parameters: IB1: 0x%X, "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001939 "Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001940 ft_data->ib1, ft_data->context_id, ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001941
1942 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
Tarun Karrad20d71a2013-01-25 15:38:57 -08001943 KGSL_FT_INFO(device, "Last issued global timestamp: %x\n", timestamp);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001944
1945 /* We may need to replay commands multiple times based on whether
1946 * multiple contexts hang the GPU */
1947 while (true) {
Tarun Karradeeecc02013-01-21 23:42:17 -08001948
Tarun Karrad20d71a2013-01-25 15:38:57 -08001949 ret = _adreno_ft(device, ft_data);
1950
1951 KGSL_FT_CRIT(device, "POLICY: 0x%X\n", ft_data->step);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001952
1953 if (-EAGAIN == ret) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08001954 /* setup new fault tolerance parameters and retry, this
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001955 * means more than 1 contexts are causing hang */
Tarun Karrad20d71a2013-01-25 15:38:57 -08001956 adreno_destroy_ft_data(ft_data);
1957 ret = adreno_setup_ft_data(device, ft_data);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08001958 if (ret)
1959 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08001960 KGSL_FT_INFO(device,
1961 "Retry. Parameters: "
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001962 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
Tarun Karrad20d71a2013-01-25 15:38:57 -08001963 ft_data->ib1, ft_data->context_id,
1964 ft_data->global_eop);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001965 } else {
1966 break;
1967 }
1968 }
1969
1970 if (ret)
1971 goto done;
1972
Tarun Karrad20d71a2013-01-25 15:38:57 -08001973 /* Restore correct states after fault tolerance */
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001974 if (adreno_dev->drawctxt_active)
1975 device->mmu.hwpagetable =
1976 adreno_dev->drawctxt_active->pagetable;
1977 else
1978 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1979 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1980 kgsl_sharedmem_writel(&device->memstore,
1981 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1982 eoptimestamp),
1983 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07001984
1985 /* switch to NULL ctxt */
1986 if (adreno_dev->drawctxt_active != NULL)
1987 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1988
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001989done:
1990 adreno_set_max_ts_for_bad_ctxs(device);
1991 adreno_mark_context_status(device, ret);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001992 if (ret)
1993 KGSL_FT_ERR(device, "Fault Tolerance failed\n");
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001994 return ret;
1995}
1996
1997int
Tarun Karrad20d71a2013-01-25 15:38:57 -08001998adreno_dump_and_exec_ft(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001999{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002000 int result = -ETIMEDOUT;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002001 struct adreno_ft_data ft_data;
Tarun Karra696f89e2013-01-27 21:31:40 -08002002 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002003
2004 if (device->state == KGSL_STATE_HUNG)
2005 goto done;
Tarun Karrad20d71a2013-01-25 15:38:57 -08002006 if (device->state == KGSL_STATE_DUMP_AND_FT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002007 mutex_unlock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08002008 wait_for_completion(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07002010 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002011 result = 0;
2012 } else {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002013 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_FT);
2014 INIT_COMPLETION(device->ft_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002015 /* Detected a hang */
2016
Tarun Karrad20d71a2013-01-25 15:38:57 -08002017 /* Get the fault tolerance data as soon as hang is detected */
2018 result = adreno_setup_ft_data(device, &ft_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002019
2020 /*
Tarun Karra696f89e2013-01-27 21:31:40 -08002021 * If long ib is detected, do not attempt postmortem or
2022 * snapshot, if GPU is still executing commands
2023 * we will get errors
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002024 */
Tarun Karra696f89e2013-01-27 21:31:40 -08002025 if (!adreno_dev->long_ib) {
2026 /*
2027 * Trigger an automatic dump of the state to
2028 * the console
2029 */
2030 kgsl_postmortem_dump(device, 0);
2031
2032 /*
2033 * Make a GPU snapshot. For now, do it after the
2034 * PM dump so we can at least be sure the PM dump
2035 * will work as it always has
2036 */
2037 kgsl_device_snapshot(device, 1);
2038 }
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002039
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002040 if (!result) {
Tarun Karrad20d71a2013-01-25 15:38:57 -08002041 result = adreno_ft(device, &ft_data);
2042 adreno_destroy_ft_data(&ft_data);
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002043 }
Tarun Karra696f89e2013-01-27 21:31:40 -08002044
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002045 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002046 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002047 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07002048 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06002049 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
2050 }
Tarun Karrad20d71a2013-01-25 15:38:57 -08002051 complete_all(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002052 }
2053done:
2054 return result;
2055}
Tarun Karrad20d71a2013-01-25 15:38:57 -08002056EXPORT_SYMBOL(adreno_dump_and_exec_ft);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002057
2058static int adreno_getproperty(struct kgsl_device *device,
2059 enum kgsl_property_type type,
2060 void *value,
2061 unsigned int sizebytes)
2062{
2063 int status = -EINVAL;
2064 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2065
2066 switch (type) {
2067 case KGSL_PROP_DEVICE_INFO:
2068 {
2069 struct kgsl_devinfo devinfo;
2070
2071 if (sizebytes != sizeof(devinfo)) {
2072 status = -EINVAL;
2073 break;
2074 }
2075
2076 memset(&devinfo, 0, sizeof(devinfo));
2077 devinfo.device_id = device->id+1;
2078 devinfo.chip_id = adreno_dev->chip_id;
2079 devinfo.mmu_enabled = kgsl_mmu_enabled();
2080 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06002081 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
2082 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002083
2084 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
2085 0) {
2086 status = -EFAULT;
2087 break;
2088 }
2089 status = 0;
2090 }
2091 break;
2092 case KGSL_PROP_DEVICE_SHADOW:
2093 {
2094 struct kgsl_shadowprop shadowprop;
2095
2096 if (sizebytes != sizeof(shadowprop)) {
2097 status = -EINVAL;
2098 break;
2099 }
2100 memset(&shadowprop, 0, sizeof(shadowprop));
2101 if (device->memstore.hostptr) {
2102 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2103 * anything to mmap().
2104 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07002105 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002106 shadowprop.size = device->memstore.size;
2107 /* GSL needs this to be set, even if it
2108 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002109 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2110 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002111 }
2112 if (copy_to_user(value, &shadowprop,
2113 sizeof(shadowprop))) {
2114 status = -EFAULT;
2115 break;
2116 }
2117 status = 0;
2118 }
2119 break;
2120 case KGSL_PROP_MMU_ENABLE:
2121 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002122 int mmu_prop = kgsl_mmu_enabled();
2123
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002124 if (sizebytes != sizeof(int)) {
2125 status = -EINVAL;
2126 break;
2127 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002128 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002129 status = -EFAULT;
2130 break;
2131 }
2132 status = 0;
2133 }
2134 break;
2135 case KGSL_PROP_INTERRUPT_WAITS:
2136 {
2137 int int_waits = 1;
2138 if (sizebytes != sizeof(int)) {
2139 status = -EINVAL;
2140 break;
2141 }
2142 if (copy_to_user(value, &int_waits, sizeof(int))) {
2143 status = -EFAULT;
2144 break;
2145 }
2146 status = 0;
2147 }
2148 break;
2149 default:
2150 status = -EINVAL;
2151 }
2152
2153 return status;
2154}
2155
Jordan Crousef7370f82012-04-18 09:31:07 -06002156static int adreno_setproperty(struct kgsl_device *device,
2157 enum kgsl_property_type type,
2158 void *value,
2159 unsigned int sizebytes)
2160{
2161 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08002162 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06002163
2164 switch (type) {
2165 case KGSL_PROP_PWRCTRL: {
2166 unsigned int enable;
2167 struct kgsl_device_platform_data *pdata =
2168 kgsl_device_get_drvdata(device);
2169
2170 if (sizebytes != sizeof(enable))
2171 break;
2172
2173 if (copy_from_user(&enable, (void __user *) value,
2174 sizeof(enable))) {
2175 status = -EFAULT;
2176 break;
2177 }
2178
2179 if (enable) {
2180 if (pdata->nap_allowed)
2181 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08002182 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06002183 kgsl_pwrscale_enable(device);
2184 } else {
2185 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08002186 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06002187 kgsl_pwrscale_disable(device);
2188 }
2189
2190 status = 0;
2191 }
2192 break;
2193 default:
2194 break;
2195 }
2196
2197 return status;
2198}
2199
Jordan Crousea29a2e02012-08-14 09:09:23 -06002200static int adreno_ringbuffer_drain(struct kgsl_device *device,
2201 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002202{
2203 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2204 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06002205 unsigned long wait;
2206 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
2207
2208 if (!(rb->flags & KGSL_FLAGS_STARTED))
2209 return 0;
2210
2211 /*
2212 * The first time into the loop, wait for 100 msecs and kick wptr again
2213 * to ensure that the hardware has updated correctly. After that, kick
2214 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
2215 * expires
2216 */
2217
2218 wait = jiffies + msecs_to_jiffies(100);
2219
Jordan Crousea29a2e02012-08-14 09:09:23 -06002220 do {
2221 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06002222 /* Check to see if the core is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002223 if (adreno_ft_detect(device, regs))
Jordan Crousea29a2e02012-08-14 09:09:23 -06002224 return -ETIMEDOUT;
2225
2226 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2227 }
2228 GSL_RB_GET_READPTR(rb, &rb->rptr);
2229
2230 if (time_after(jiffies, timeout)) {
2231 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
2232 rb->rptr, rb->wptr);
2233 return -ETIMEDOUT;
2234 }
2235 } while (rb->rptr != rb->wptr);
2236
2237 return 0;
2238}
2239
2240/* Caller must hold the device mutex. */
2241int adreno_idle(struct kgsl_device *device)
2242{
2243 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002244 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05302245 unsigned long wait_time;
2246 unsigned long wait_time_part;
Tarun Karra696f89e2013-01-27 21:31:40 -08002247 unsigned int prev_reg_val[ft_detect_regs_count];
Tarun Karra3335f142012-06-19 14:11:48 -07002248
2249 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002250
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002251 kgsl_cffdump_regpoll(device->id,
2252 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002253 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002254
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002255retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06002256 /* First, wait for the ringbuffer to drain */
2257 if (adreno_ringbuffer_drain(device, prev_reg_val))
2258 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002259
2260 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06002261 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002262 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
2263
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002264 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002265 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
2266 &rbbm_status);
2267 if (adreno_is_a2xx(adreno_dev)) {
2268 if (rbbm_status == 0x110)
2269 return 0;
2270 } else {
2271 if (!(rbbm_status & 0x80000000))
2272 return 0;
2273 }
Tarun Karra3335f142012-06-19 14:11:48 -07002274
2275 /* Dont wait for timeout, detect hang faster.
2276 */
2277 if (time_after(jiffies, wait_time_part)) {
2278 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06002279 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra696f89e2013-01-27 21:31:40 -08002280 if ((adreno_ft_detect(device, prev_reg_val)))
Tarun Karra3335f142012-06-19 14:11:48 -07002281 goto err;
2282 }
2283
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002284 }
2285
2286err:
2287 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Tarun Karrad20d71a2013-01-25 15:38:57 -08002288 if (KGSL_STATE_DUMP_AND_FT != device->state &&
2289 !adreno_dump_and_exec_ft(device)) {
2290 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002291 goto retry;
2292 }
2293 return -ETIMEDOUT;
2294}
2295
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002296/**
2297 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
2298 * rbbm_status register
2299 * @device - Pointer to the GPU device whose idle status is to be
2300 * checked
2301 * @returns - Returns whether the core is idle (based on rbbm_status)
2302 * false if the core is active, true if the core is idle
2303 */
2304static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
2305{
2306 unsigned int reg_rbbm_status;
2307 bool status = false;
2308 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2309
2310 /* Is the core idle? */
2311 adreno_regread(device,
2312 adreno_dev->gpudev->reg_rbbm_status,
2313 &reg_rbbm_status);
2314
2315 if (adreno_is_a2xx(adreno_dev)) {
2316 if (reg_rbbm_status == 0x110)
2317 status = true;
2318 } else {
2319 if (!(reg_rbbm_status & 0x80000000))
2320 status = true;
2321 }
2322 return status;
2323}
2324
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002325static unsigned int adreno_isidle(struct kgsl_device *device)
2326{
2327 int status = false;
2328 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2329 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002330
Lucille Sylvester51b764d2011-12-15 16:51:52 -07002331 WARN_ON(device->state == KGSL_STATE_INIT);
2332 /* If the device isn't active, don't force it on. */
2333 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002334 /* Is the ring buffer is empty? */
2335 GSL_RB_GET_READPTR(rb, &rb->rptr);
2336 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
2337 /* Is the core idle? */
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002338 status = is_adreno_rbbm_status_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002339 }
2340 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07002341 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002342 }
2343 return status;
2344}
2345
2346/* Caller must hold the device mutex. */
2347static int adreno_suspend_context(struct kgsl_device *device)
2348{
2349 int status = 0;
2350 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2351
2352 /* switch to NULL ctxt */
2353 if (adreno_dev->drawctxt_active != NULL) {
2354 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002355 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002356 }
2357
2358 return status;
2359}
2360
Jordan Crouse233b2092012-04-18 09:31:09 -06002361/* Find a memory structure attached to an adreno context */
2362
2363struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2364 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2365{
2366 struct kgsl_context *context;
2367 struct adreno_context *adreno_context = NULL;
2368 int next = 0;
2369
2370 while (1) {
2371 context = idr_get_next(&device->context_idr, &next);
2372 if (context == NULL)
2373 break;
2374
2375 adreno_context = (struct adreno_context *)context->devctxt;
2376
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002377 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2378 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002379 struct kgsl_memdesc *desc;
2380
2381 desc = &adreno_context->gpustate;
2382 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2383 return desc;
2384
2385 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2386 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2387 return desc;
2388 }
2389 next = next + 1;
2390 }
2391
2392 return NULL;
2393}
2394
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002395struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002396 unsigned int pt_base,
2397 unsigned int gpuaddr,
2398 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002399{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002400 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002401 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2402 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2403
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002404 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2405 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002406
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002407 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2408 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002409
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002410 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2411 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002412
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002413 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2414 size))
2415 return &device->mmu.setstate_memory;
2416
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002417 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002418
2419 if (entry)
2420 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002421
Jordan Crouse233b2092012-04-18 09:31:09 -06002422 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002423}
2424
2425uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2426 unsigned int gpuaddr, unsigned int size)
2427{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002428 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002429
2430 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2431
2432 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002433}
2434
2435void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2436 unsigned int *value)
2437{
2438 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002439 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2440 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002441
2442 if (!in_interrupt())
2443 kgsl_pre_hwaccess(device);
2444
2445 /*ensure this read finishes before the next one.
2446 * i.e. act like normal readl() */
2447 *value = __raw_readl(reg);
2448 rmb();
2449}
2450
2451void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2452 unsigned int value)
2453{
2454 unsigned int *reg;
2455
Jordan Crouse7501d452012-04-19 08:58:44 -06002456 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002457
2458 if (!in_interrupt())
2459 kgsl_pre_hwaccess(device);
2460
2461 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002462 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002463
2464 /*ensure previous writes post before this one,
2465 * i.e. act like normal writel() */
2466 wmb();
2467 __raw_writel(value, reg);
2468}
2469
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002470static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2471{
2472 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002473 if (k_ctxt != NULL) {
2474 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002475 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2476 context_id = KGSL_CONTEXT_INVALID;
2477 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2478 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002479 }
2480
2481 return context_id;
2482}
2483
Jordan Crouse313faf62012-11-20 15:12:28 -07002484static void adreno_next_event(struct kgsl_device *device,
2485 struct kgsl_event *event)
2486{
2487 int status;
2488 unsigned int ref_ts, enableflag;
2489 unsigned int context_id = _get_context_id(event->context);
2490 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2491
2492 status = kgsl_check_timestamp(device, event->context, event->timestamp);
2493 if (!status) {
2494 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2495 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2496 /*
2497 * Barrier is needed here to make sure the read from memstore
2498 * has posted
2499 */
2500
2501 mb();
2502
2503 if (enableflag) {
2504 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
2505 KGSL_MEMSTORE_OFFSET(context_id,
2506 ref_wait_ts));
2507
2508 /* Make sure the memstore read has posted */
2509 mb();
2510 if (timestamp_cmp(ref_ts, event->timestamp) >= 0) {
2511 kgsl_sharedmem_writel(&device->memstore,
2512 KGSL_MEMSTORE_OFFSET(context_id,
2513 ref_wait_ts), event->timestamp);
2514 /* Make sure the memstore write is posted */
2515 wmb();
2516 }
2517 } else {
2518 unsigned int cmds[2];
2519 kgsl_sharedmem_writel(&device->memstore,
2520 KGSL_MEMSTORE_OFFSET(context_id,
2521 ref_wait_ts), event->timestamp);
2522 enableflag = 1;
2523 kgsl_sharedmem_writel(&device->memstore,
2524 KGSL_MEMSTORE_OFFSET(context_id,
2525 ts_cmp_enable), enableflag);
2526
2527 /* Make sure the memstore write gets posted */
2528 wmb();
2529
2530 /*
2531 * submit a dummy packet so that even if all
2532 * commands upto timestamp get executed we will still
2533 * get an interrupt
2534 */
2535 cmds[0] = cp_type3_packet(CP_NOP, 1);
2536 cmds[1] = 0;
2537
2538 if (adreno_dev->drawctxt_active)
2539 adreno_ringbuffer_issuecmds_intr(device,
2540 event->context, &cmds[0], 2);
2541 }
2542 }
2543}
2544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002545static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002546 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002547{
2548 int status;
2549 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002550 unsigned int context_id;
2551
2552 mutex_lock(&device->mutex);
2553 context_id = _get_context_id(context);
2554 /*
2555 * If the context ID is invalid, we are in a race with
2556 * the context being destroyed by userspace so bail.
2557 */
2558 if (context_id == KGSL_CONTEXT_INVALID) {
2559 KGSL_DRV_WARN(device, "context was detached");
2560 status = -EINVAL;
2561 goto unlock;
2562 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002563
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002564 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002565 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002566 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002567 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002568 mb();
2569
2570 if (enableflag) {
2571 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002572 KGSL_MEMSTORE_OFFSET(context_id,
2573 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002574 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002575 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002576 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002577 KGSL_MEMSTORE_OFFSET(context_id,
2578 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002579 wmb();
2580 }
2581 } else {
2582 unsigned int cmds[2];
2583 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002584 KGSL_MEMSTORE_OFFSET(context_id,
2585 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002586 enableflag = 1;
2587 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002588 KGSL_MEMSTORE_OFFSET(context_id,
2589 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002590 wmb();
2591 /* submit a dummy packet so that even if all
2592 * commands upto timestamp get executed we will still
2593 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002594 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002595 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002596
Vladimir Razgulin38345302013-01-22 18:41:59 -07002597 if (context && device->state != KGSL_STATE_SLUMBER)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002598 adreno_ringbuffer_issuecmds_intr(device,
2599 context, &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002601 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002602unlock:
2603 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002604
2605 return status;
2606}
2607
2608/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002609 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002610 placing a process in wait q. For conditional interrupts we expect the
2611 process to already be in its wait q when its exit condition checking
2612 function is called.
2613*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002614#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002615({ \
2616 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002617 if (io) \
2618 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2619 else \
2620 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002621 __ret; \
2622})
2623
Tarun Karra3335f142012-06-19 14:11:48 -07002624
2625
Tarun Karra696f89e2013-01-27 21:31:40 -08002626unsigned int adreno_ft_detect(struct kgsl_device *device,
Tarun Karra3335f142012-06-19 14:11:48 -07002627 unsigned int *prev_reg_val)
2628{
2629 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Tarun Karra696f89e2013-01-27 21:31:40 -08002630 unsigned int curr_reg_val[ft_detect_regs_count];
2631 unsigned int fast_hang_detected = 1;
2632 unsigned int long_ib_detected = 1;
Tarun Karra3335f142012-06-19 14:11:48 -07002633 unsigned int i;
Tarun Karra4648db92013-02-01 17:03:40 -08002634 static unsigned long next_hang_detect_time;
Tarun Karra696f89e2013-01-27 21:31:40 -08002635 static unsigned int prev_global_ts;
2636 unsigned int curr_global_ts = 0;
2637 unsigned int curr_context_id = 0;
2638 static struct adreno_context *curr_context;
2639 static struct kgsl_context *context;
Tarun Karra3335f142012-06-19 14:11:48 -07002640
2641 if (!adreno_dev->fast_hang_detect)
Tarun Karra696f89e2013-01-27 21:31:40 -08002642 fast_hang_detected = 0;
2643
2644 if (!adreno_dev->long_ib_detect)
2645 long_ib_detected = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002646
Jordan Crousecca61142012-11-20 10:54:24 -07002647 if (is_adreno_rbbm_status_idle(device)) {
2648
2649 /*
2650 * On A20X if the RPTR != WPTR and the device is idle, then
2651 * the last write to WPTR probably failed to latch so write it
2652 * again
2653 */
2654
2655 if (adreno_is_a2xx(adreno_dev)) {
2656 unsigned int rptr;
2657 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2658 if (rptr != adreno_dev->ringbuffer.wptr)
2659 adreno_regwrite(device, REG_CP_RB_WPTR,
2660 adreno_dev->ringbuffer.wptr);
2661 }
2662
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002663 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002664 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002665
Tarun Karra4648db92013-02-01 17:03:40 -08002666 /*
2667 * Time interval between hang detection should be KGSL_TIMEOUT_PART
2668 * or more, if next hang detection is requested < KGSL_TIMEOUT_PART
2669 * from the last time do nothing.
2670 */
2671 if ((next_hang_detect_time) &&
2672 (time_before(jiffies, next_hang_detect_time)))
2673 return 0;
2674 else
2675 next_hang_detect_time = (jiffies +
2676 msecs_to_jiffies(KGSL_TIMEOUT_PART-1));
2677
Tarun Karra696f89e2013-01-27 21:31:40 -08002678 /* Read the current Hang detect reg values here */
2679 for (i = 0; i < ft_detect_regs_count; i++) {
2680 if (ft_detect_regs[i] == 0)
Jordan Crouseb5c80482012-10-03 09:38:41 -06002681 continue;
Tarun Karra696f89e2013-01-27 21:31:40 -08002682 adreno_regread(device, ft_detect_regs[i],
2683 &curr_reg_val[i]);
Tarun Karra3335f142012-06-19 14:11:48 -07002684 }
2685
Tarun Karra696f89e2013-01-27 21:31:40 -08002686 /* Read the current global timestamp here */
2687 kgsl_sharedmem_readl(&device->memstore,
2688 &curr_global_ts,
2689 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2690 eoptimestamp));
2691
2692 mb();
2693
2694 if (curr_global_ts == prev_global_ts) {
2695
2696 /* Get the current context here */
2697 if (context == NULL) {
2698 kgsl_sharedmem_readl(&device->memstore,
2699 &curr_context_id,
2700 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
2701 current_context));
2702 context = idr_find(&device->context_idr,
2703 curr_context_id);
2704 if (context != NULL) {
2705 curr_context = context->devctxt;
2706 curr_context->ib_gpu_time_used = 0;
2707 } else {
2708 KGSL_DRV_ERR(device,
2709 "Fault tolerance no context found\n");
2710 }
2711 }
2712
2713 mb();
2714
2715 if (curr_context != NULL) {
2716
2717 curr_context->ib_gpu_time_used += KGSL_TIMEOUT_PART;
2718 KGSL_FT_INFO(device,
2719 "Proc %s used GPU Time %d ms on timestamp 0x%X\n",
2720 curr_context->pid_name, curr_context->ib_gpu_time_used,
2721 curr_global_ts+1);
2722
2723 for (i = 0; i < ft_detect_regs_count; i++) {
2724 if (curr_reg_val[i] != prev_reg_val[i]) {
2725 fast_hang_detected = 0;
2726
2727 /* Check for long IB here */
2728 if ((i >=
2729 LONG_IB_DETECT_REG_INDEX_START)
2730 &&
2731 (i <=
2732 LONG_IB_DETECT_REG_INDEX_END))
2733 long_ib_detected = 0;
2734 }
2735 }
2736
2737 if (fast_hang_detected) {
2738 KGSL_FT_ERR(device,
2739 "Proc %s, ctxt_id %d ts %d triggered fault tolerance"
2740 " on global ts %d\n",
2741 curr_context->pid_name, curr_context->id
2742 , (kgsl_readtimestamp(device, context,
2743 KGSL_TIMESTAMP_RETIRED)+1),
2744 curr_global_ts+1);
2745 return 1;
2746 }
2747
2748 if (long_ib_detected) {
2749 curr_context->ib_gpu_time_used +=
2750 KGSL_TIMEOUT_PART;
2751 if (curr_context->ib_gpu_time_used >
2752 KGSL_TIMEOUT_LONG_IB_DETECTION) {
2753 if (adreno_dev->long_ib_ts !=
2754 curr_global_ts) {
2755 KGSL_FT_ERR(device,
2756 "Proc %s, ctxt_id %d ts %d"
2757 "used GPU for %d ms long ib "
2758 "detected on global ts %d\n",
2759 curr_context->pid_name,
2760 curr_context->id,
2761 (kgsl_readtimestamp(device,
2762 context,
2763 KGSL_TIMESTAMP_RETIRED)+1),
2764 curr_context->ib_gpu_time_used,
2765 curr_global_ts+1);
2766 adreno_dev->long_ib = 1;
2767 adreno_dev->long_ib_ts =
2768 curr_global_ts;
2769 curr_context->ib_gpu_time_used =
2770 0;
2771 return 1;
2772 }
2773 }
2774 }
2775 } else {
2776 KGSL_FT_ERR(device,
2777 "Last context unknown id:%d\n",
2778 curr_context_id);
2779 }
2780 } else {
2781 /* GPU is moving forward */
2782 prev_global_ts = curr_global_ts;
2783 context = NULL;
2784 curr_context = NULL;
2785 adreno_dev->long_ib = 0;
2786 adreno_dev->long_ib_ts = 0;
2787 }
2788
2789
2790 /* If hangs are not detected copy the current reg values
2791 * to previous values and return no hang */
2792 for (i = 0; i < ft_detect_regs_count; i++)
2793 prev_reg_val[i] = curr_reg_val[i];
2794 return 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002795}
2796
Jordan Crouse92446a62012-11-15 11:00:06 -07002797/**
2798 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2799 * @device - pointer to a KGSL device structure
2800 * @context - pointer to the active KGSL context
2801 * @timestamp - the timestamp that the process was waiting for
2802 *
Tarun Karrad20d71a2013-01-25 15:38:57 -08002803 * Process a possible GPU hang and try fault tolerance from it
2804 * cleanly
Jordan Crouse92446a62012-11-15 11:00:06 -07002805 */
2806static int adreno_handle_hang(struct kgsl_device *device,
2807 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002808{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002809 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002810 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002811 unsigned int ts_issued;
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002812 unsigned int rptr;
Tarun Karra3335f142012-06-19 14:11:48 -07002813
Jordan Crouse92446a62012-11-15 11:00:06 -07002814 /* Do one last check to see if we somehow made it through */
2815 if (kgsl_check_timestamp(device, context, timestamp))
2816 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002817
2818 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002819
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002820 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2821 mb();
2822
Tarun Karra696f89e2013-01-27 21:31:40 -08002823 KGSL_DRV_WARN(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002824 "Device hang detected while waiting for timestamp: "
2825 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
Shubhraprakash Das460cc762013-01-16 16:57:46 -08002826 "retired timestamp: <%d:0x%x>, wptr: 0x%x, rptr: 0x%x\n",
2827 context_id, timestamp, context_id, ts_issued, context_id,
2828 kgsl_readtimestamp(device, context,
2829 KGSL_TIMESTAMP_RETIRED),
2830 adreno_dev->ringbuffer.wptr, rptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002831
Tarun Karrad20d71a2013-01-25 15:38:57 -08002832 /* Return 0 after a successful fault tolerance */
2833 if (!adreno_dump_and_exec_ft(device))
Jordan Crouse92446a62012-11-15 11:00:06 -07002834 return 0;
2835
2836 return -ETIMEDOUT;
2837}
2838
2839static int _check_pending_timestamp(struct kgsl_device *device,
2840 struct kgsl_context *context, unsigned int timestamp)
2841{
2842 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2843 unsigned int context_id = _get_context_id(context);
2844 unsigned int ts_issued;
2845
2846 if (context_id == KGSL_CONTEXT_INVALID)
2847 return -EINVAL;
2848
2849 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2850
2851 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2852 return 0;
2853
2854 if (context && !context->wait_on_invalid_ts) {
2855 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2856 context_id, timestamp, context_id, ts_issued);
2857
2858 /* Only print this message once */
2859 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002860 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002861
2862 return -EINVAL;
2863}
2864
2865/**
2866 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2867 * @device - pointer to a KGSL device structure
2868 * @context - pointer to the active kgsl context
2869 * @timestamp - GPU timestamp to wait for
2870 * @msecs - amount of time to wait (in milliseconds)
2871 *
2872 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2873 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2874 * one if it happened. Otherwise, spend most of our time in an interruptible
2875 * wait for the timestamp interrupt to be processed. This function must be
2876 * called with the mutex already held.
2877 */
2878static int adreno_waittimestamp(struct kgsl_device *device,
2879 struct kgsl_context *context,
2880 unsigned int timestamp,
2881 unsigned int msecs)
2882{
2883 static unsigned int io_cnt;
2884 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2885 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2886 unsigned int context_id = _get_context_id(context);
Tarun Karra696f89e2013-01-27 21:31:40 -08002887 unsigned int prev_reg_val[ft_detect_regs_count];
Jordan Crouse92446a62012-11-15 11:00:06 -07002888 unsigned int time_elapsed = 0;
2889 unsigned int wait;
2890 int ts_compare = 1;
2891 int io, ret = -ETIMEDOUT;
2892
2893 /* Get out early if the context has already been destroyed */
2894
2895 if (context_id == KGSL_CONTEXT_INVALID) {
2896 KGSL_DRV_WARN(device, "context was detached");
2897 return -EINVAL;
2898 }
2899
2900 /*
2901 * Check to see if the requested timestamp is "newer" then the last
2902 * timestamp issued. If it is complain once and return error. Only
2903 * print the message once per context so that badly behaving
2904 * applications don't spam the logs
2905 */
2906
2907 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2908 if (_check_pending_timestamp(device, context, timestamp))
2909 return -EINVAL;
2910
2911 /* Reset the invalid timestamp flag on a valid wait */
2912 context->wait_on_invalid_ts = false;
2913 }
2914
2915
2916 /* Clear the registers used for hang detection */
2917 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2918
2919 /*
2920 * On the first time through the loop only wait 100ms.
2921 * this gives enough time for the engine to start moving and oddly
2922 * provides better hang detection results than just going the full
2923 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2924 * is if msecs happens to be < 100ms then just use the full timeout
2925 */
2926
2927 wait = 100;
2928
2929 do {
2930 long status;
2931
Jordan Crouse92446a62012-11-15 11:00:06 -07002932 /*
2933 * if the timestamp happens while we're not
2934 * waiting, there's a chance that an interrupt
2935 * will not be generated and thus the timestamp
2936 * work needs to be queued.
2937 */
2938
2939 if (kgsl_check_timestamp(device, context, timestamp)) {
2940 queue_work(device->work_queue, &device->ts_expired_ws);
2941 ret = 0;
2942 break;
2943 }
2944
2945 /* Check to see if the GPU is hung */
Tarun Karra696f89e2013-01-27 21:31:40 -08002946 if (adreno_ft_detect(device, prev_reg_val)) {
Jordan Crouse92446a62012-11-15 11:00:06 -07002947 ret = adreno_handle_hang(device, context, timestamp);
2948 break;
2949 }
2950
2951 /*
2952 * For proper power accounting sometimes we need to call
2953 * io_wait_interruptible_timeout and sometimes we need to call
2954 * plain old wait_interruptible_timeout. We call the regular
2955 * timeout N times out of 100, where N is a number specified by
2956 * the current power level
2957 */
2958
2959 io_cnt = (io_cnt + 1) % 100;
2960 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2961 ? 0 : 1;
2962
2963 mutex_unlock(&device->mutex);
2964
2965 /* Wait for a timestamp event */
2966 status = kgsl_wait_event_interruptible_timeout(
2967 device->wait_queue,
2968 kgsl_check_interrupt_timestamp(device, context,
2969 timestamp), msecs_to_jiffies(wait), io);
2970
2971 mutex_lock(&device->mutex);
2972
2973 /*
2974 * If status is non zero then either the condition was satisfied
2975 * or there was an error. In either event, this is the end of
2976 * the line for us
2977 */
2978
2979 if (status != 0) {
2980 ret = (status > 0) ? 0 : (int) status;
2981 break;
2982 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002983 time_elapsed += wait;
2984
2985 /* If user specified timestamps are being used, wait at least
2986 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
2987 * issue a IB for a timestamp before checking to see if the
2988 * current timestamp we are waiting for is valid or not
2989 */
2990
2991 if (ts_compare && (adreno_ctx &&
2992 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
2993 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
2994 ret = _check_pending_timestamp(device, context,
2995 timestamp);
2996 if (ret)
2997 break;
2998
2999 /* Don't do this check again */
3000 ts_compare = 0;
3001
3002 /*
3003 * Reset the invalid timestamp flag on a valid
3004 * wait
3005 */
3006 context->wait_on_invalid_ts = false;
3007 }
3008 }
3009
3010 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003011 * We want to wait the floor of KGSL_TIMEOUT_PART
3012 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07003013 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07003014
3015 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
3016 wait = KGSL_TIMEOUT_PART;
3017 else
3018 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07003019
3020 } while (!msecs || time_elapsed < msecs);
3021
3022 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003023}
3024
3025static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003026 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003027{
3028 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07003029 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003030
Jeremy Gebben9ad86922012-05-08 15:33:23 -06003031 /*
3032 * If the context ID is invalid, we are in a race with
3033 * the context being destroyed by userspace so bail.
3034 */
3035 if (context_id == KGSL_CONTEXT_INVALID) {
3036 KGSL_DRV_WARN(device, "context was detached");
3037 return timestamp;
3038 }
Jordan Crousec659f382012-04-16 11:10:41 -06003039 switch (type) {
3040 case KGSL_TIMESTAMP_QUEUED: {
3041 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3042 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
3043
3044 timestamp = rb->timestamp[context_id];
3045 break;
3046 }
3047 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003048 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06003049 break;
3050 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003051 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06003052 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
3053 break;
3054 }
3055
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003056 rmb();
3057
3058 return timestamp;
3059}
3060
3061static long adreno_ioctl(struct kgsl_device_private *dev_priv,
3062 unsigned int cmd, void *data)
3063{
3064 int result = 0;
3065 struct kgsl_drawctxt_set_bin_base_offset *binbase;
3066 struct kgsl_context *context;
3067
3068 switch (cmd) {
3069 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
3070 binbase = data;
3071
3072 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
3073 if (context) {
3074 adreno_drawctxt_set_bin_base_offset(
3075 dev_priv->device, context, binbase->offset);
3076 } else {
3077 result = -EINVAL;
3078 KGSL_DRV_ERR(dev_priv->device,
3079 "invalid drawctxt drawctxt_id %d "
3080 "device_id=%d\n",
3081 binbase->drawctxt_id, dev_priv->device->id);
3082 }
3083 break;
3084
3085 default:
3086 KGSL_DRV_INFO(dev_priv->device,
3087 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07003088 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003089 break;
3090 }
3091 return result;
3092
3093}
3094
3095static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
3096{
3097 gpu_freq /= 1000000;
3098 return ticks / gpu_freq;
3099}
3100
3101static void adreno_power_stats(struct kgsl_device *device,
3102 struct kgsl_power_stats *stats)
3103{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003104 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003105 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003106 unsigned int cycles;
3107
3108 /* Get the busy cycles counted since the counter was last reset */
3109 /* Calling this function also resets and restarts the counter */
3110
3111 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003112
3113 /* In order to calculate idle you have to have run the algorithm *
3114 * at least once to get a start time. */
3115 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003116 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003117 stats->total_time = tmp - pwr->time;
3118 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07003119 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003120 pwrlevels[device->pwrctrl.active_pwrlevel].
3121 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003122 } else {
3123 stats->total_time = 0;
3124 stats->busy_time = 0;
3125 pwr->time = ktime_to_us(ktime_get());
3126 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003127}
3128
3129void adreno_irqctrl(struct kgsl_device *device, int state)
3130{
Jordan Crousea78c9172011-07-11 13:14:09 -06003131 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3132 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003133}
3134
Jordan Croused6535882012-06-20 08:22:16 -06003135static unsigned int adreno_gpuid(struct kgsl_device *device,
3136 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07003137{
3138 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3139
Jordan Croused6535882012-06-20 08:22:16 -06003140 /* Some applications need to know the chip ID too, so pass
3141 * that as a parameter */
3142
3143 if (chipid != NULL)
3144 *chipid = adreno_dev->chip_id;
3145
Jordan Crousea0758f22011-12-07 11:19:22 -07003146 /* Standard KGSL gpuid format:
3147 * top word is 0x0002 for 2D or 0x0003 for 3D
3148 * Bottom word is core specific identifer
3149 */
3150
3151 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
3152}
3153
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003154static const struct kgsl_functable adreno_functable = {
3155 /* Mandatory functions */
3156 .regread = adreno_regread,
3157 .regwrite = adreno_regwrite,
3158 .idle = adreno_idle,
3159 .isidle = adreno_isidle,
3160 .suspend_context = adreno_suspend_context,
3161 .start = adreno_start,
3162 .stop = adreno_stop,
3163 .getproperty = adreno_getproperty,
3164 .waittimestamp = adreno_waittimestamp,
3165 .readtimestamp = adreno_readtimestamp,
3166 .issueibcmds = adreno_ringbuffer_issueibcmds,
3167 .ioctl = adreno_ioctl,
3168 .setup_pt = adreno_setup_pt,
3169 .cleanup_pt = adreno_cleanup_pt,
3170 .power_stats = adreno_power_stats,
3171 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07003172 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07003173 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06003174 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003175 /* Optional functions */
3176 .setstate = adreno_setstate,
3177 .drawctxt_create = adreno_drawctxt_create,
3178 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06003179 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06003180 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07003181 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003182};
3183
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003184static struct platform_driver adreno_platform_driver = {
3185 .probe = adreno_probe,
3186 .remove = __devexit_p(adreno_remove),
3187 .suspend = kgsl_suspend_driver,
3188 .resume = kgsl_resume_driver,
3189 .id_table = adreno_id_table,
3190 .driver = {
3191 .owner = THIS_MODULE,
3192 .name = DEVICE_3D_NAME,
3193 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06003194 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003195 }
3196};
3197
3198static int __init kgsl_3d_init(void)
3199{
3200 return platform_driver_register(&adreno_platform_driver);
3201}
3202
3203static void __exit kgsl_3d_exit(void)
3204{
3205 platform_driver_unregister(&adreno_platform_driver);
3206}
3207
3208module_init(kgsl_3d_init);
3209module_exit(kgsl_3d_exit);
3210
3211MODULE_DESCRIPTION("3D Graphics driver");
3212MODULE_VERSION("1.2");
3213MODULE_LICENSE("GPL v2");
3214MODULE_ALIAS("platform:kgsl_3d");