blob: 10f332588c409f9126e8b279b8a80371c8877c10 [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.
122 */
123unsigned int hang_detect_regs[] = {
124 A3XX_RBBM_STATUS,
125 REG_CP_RB_RPTR,
126 REG_CP_IB1_BASE,
127 REG_CP_IB1_BUFSZ,
128 REG_CP_IB2_BASE,
129 REG_CP_IB2_BUFSZ,
Jordan Crouseb5c80482012-10-03 09:38:41 -0600130 0,
Tarun Karra6e750d72013-01-04 10:28:40 -0800131 0,
132 0,
133 0,
134 0,
Jordan Crouseb5c80482012-10-03 09:38:41 -0600135 0
Tarun Karra3335f142012-06-19 14:11:48 -0700136};
137
138const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700139
Jordan Crouse505df9c2011-07-28 08:37:59 -0600140/*
141 * This is the master list of all GPU cores that are supported by this
142 * driver.
143 */
144
145#define ANY_ID (~0)
Tarun Karra9c070822012-11-27 16:43:51 -0700146#define NO_VER (~0)
Jordan Crouse505df9c2011-07-28 08:37:59 -0600147
148static const struct {
149 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600150 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600151 const char *pm4fw;
152 const char *pfpfw;
153 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700154 unsigned int istore_size;
155 unsigned int pix_shader_start;
Tarun Karra9c070822012-11-27 16:43:51 -0700156 /* Size of an instruction in dwords */
157 unsigned int instruction_size;
158 /* size of gmem for gpu*/
159 unsigned int gmem_size;
160 /* version of pm4 microcode that supports sync_lock
161 between CPU and GPU for SMMU-v1 programming */
162 unsigned int sync_lock_pm4_ver;
163 /* version of pfp microcode that supports sync_lock
164 between CPU and GPU for SMMU-v1 programming */
165 unsigned int sync_lock_pfp_ver;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600166} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600167 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700168 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700169 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530170 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
171 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700172 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600173 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700174 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700175 512, 384, 3, SZ_256K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600176 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700177 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700178 512, 384, 3, SZ_512K, NO_VER, NO_VER },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600179 /*
180 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
181 * a hardware problem.
182 */
183 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700184 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700185 1536, 768, 3, SZ_512K, NO_VER, NO_VER },
Carter Cooperf27ec722011-11-17 15:20:38 -0700186 { ADRENO_REV_A225, 2, 2, 0, 6,
187 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700188 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600189 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700190 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700191 1536, 768, 3, SZ_512K, 0x225011, 0x225002 },
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530192 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530193 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530194 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700195 512, 0, 2, SZ_256K, 0x3FF037, 0x3FF016 },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700196 /* A3XX doesn't use the pix_shader_start */
Carter Cooper95f7f792012-08-19 13:40:34 -0600197 { ADRENO_REV_A320, 3, 2, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700198 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700199 512, 0, 2, SZ_512K, 0x3FF037, 0x3FF016 },
liu zhongfd42e622012-05-01 19:18:30 -0700200 { ADRENO_REV_A330, 3, 3, 0, 0,
201 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
Tarun Karra9c070822012-11-27 16:43:51 -0700202 512, 0, 2, SZ_1M, NO_VER, NO_VER },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600203};
204
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600205static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206{
Jordan Crousea78c9172011-07-11 13:14:09 -0600207 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600208 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209
Jordan Crousea78c9172011-07-11 13:14:09 -0600210 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211
212 if (device->requested_state == KGSL_STATE_NONE) {
213 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700214 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 queue_work(device->work_queue, &device->idle_check_ws);
216 } else if (device->pwrscale.policy != NULL) {
217 queue_work(device->work_queue, &device->idle_check_ws);
218 }
219 }
220
221 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800222 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 jiffies + device->pwrctrl.interval_timeout);
224 return result;
225}
226
Jordan Crouse9f739212011-07-28 08:37:57 -0600227static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 struct kgsl_pagetable *pagetable)
229{
230 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
231 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
232
233 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
234
235 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
236
237 kgsl_mmu_unmap(pagetable, &device->memstore);
238
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600239 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240}
241
242static int adreno_setup_pt(struct kgsl_device *device,
243 struct kgsl_pagetable *pagetable)
244{
245 int result = 0;
246 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
247 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
248
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
250 GSL_PT_PAGE_RV);
251 if (result)
252 goto error;
253
254 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
255 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
256 if (result)
257 goto unmap_buffer_desc;
258
259 result = kgsl_mmu_map_global(pagetable, &device->memstore,
260 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
261 if (result)
262 goto unmap_memptrs_desc;
263
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600264 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
266 if (result)
267 goto unmap_memstore_desc;
268
269 return result;
270
271unmap_memstore_desc:
272 kgsl_mmu_unmap(pagetable, &device->memstore);
273
274unmap_memptrs_desc:
275 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
276
277unmap_buffer_desc:
278 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
279
280error:
281 return result;
282}
283
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600284static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600285 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600286 uint32_t flags)
287{
288 unsigned int pt_val, reg_pt_val;
Tarun Karra9c070822012-11-27 16:43:51 -0700289 unsigned int link[250];
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600290 unsigned int *cmds = &link[0];
291 int sizedwords = 0;
292 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600293 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600294 struct kgsl_context *context;
295 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600296
297 if (!adreno_dev->drawctxt_active)
298 return kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700299 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600300
301 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700302 if (context == NULL)
303 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600304 adreno_ctx = context->devctxt;
305
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600306 if (kgsl_mmu_enable_clk(&device->mmu,
307 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700308 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600309
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600310 cmds += __adreno_add_idle_indirect_cmds(cmds,
311 device->mmu.setstate_memory.gpuaddr +
312 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
313
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600314 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600315 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
316 device->mmu.setstate_memory.gpuaddr +
317 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
318 else
319 cmds += adreno_add_bank_change_cmds(cmds,
320 KGSL_IOMMU_CONTEXT_USER,
321 device->mmu.setstate_memory.gpuaddr +
322 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
323
Tarun Karra9c070822012-11-27 16:43:51 -0700324 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
325
326 /* Acquire GPU-CPU sync Lock here */
327 cmds += kgsl_mmu_sync_lock(&device->mmu, cmds);
328
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700329 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
330 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600331 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600332 /*
333 * We need to perfrom the following operations for all
334 * IOMMU units
335 */
336 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700337 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
338 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600339 /*
340 * Set address of the new pagetable by writng to IOMMU
341 * TTBR0 register
342 */
343 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700344 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
345 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600346 *cmds++ = reg_pt_val;
347 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
348 *cmds++ = 0x00000000;
349
350 /*
351 * Read back the ttbr0 register as a barrier to ensure
352 * above writes have completed
353 */
354 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700355 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
356 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600357 reg_pt_val,
358 device->mmu.setstate_memory.gpuaddr +
359 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600360 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600361 }
362 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
363 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700364 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600365 */
366 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700367 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
368 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700369
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600370 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700371 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
372 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700373 KGSL_IOMMU_CTX_TLBIALL);
374 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600375
376 cmds += __adreno_add_idle_indirect_cmds(cmds,
377 device->mmu.setstate_memory.gpuaddr +
378 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
379
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600380 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700381 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
382 KGSL_IOMMU_CONTEXT_USER,
383 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700384 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600385 device->mmu.setstate_memory.gpuaddr +
386 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
387 }
388 }
389
Tarun Karra9c070822012-11-27 16:43:51 -0700390 /* Release GPU-CPU sync Lock here */
391 cmds += kgsl_mmu_sync_unlock(&device->mmu, cmds);
392
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600393 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600394 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700395 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
396 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600397 device->mmu.setstate_memory.gpuaddr +
398 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
399 else
400 cmds += adreno_add_bank_change_cmds(cmds,
401 KGSL_IOMMU_CONTEXT_PRIV,
402 device->mmu.setstate_memory.gpuaddr +
403 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
404
Tarun Karra9c070822012-11-27 16:43:51 -0700405 cmds += adreno_add_idle_cmds(adreno_dev, cmds);
406
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600407 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600408 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700409 /* invalidate all base pointers */
410 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
411 *cmds++ = 0x7fff;
412 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600413 /* This returns the per context timestamp but we need to
414 * use the global timestamp for iommu clock disablement */
415 adreno_ringbuffer_issuecmds(device, adreno_ctx,
416 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600417 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600418 kgsl_mmu_disable_clk_on_ts(&device->mmu,
419 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600420 }
Tarun Karra9c070822012-11-27 16:43:51 -0700421
422 if (sizedwords > (sizeof(link)/sizeof(unsigned int))) {
423 KGSL_DRV_ERR(device, "Temp command buffer overflow\n");
424 BUG();
425 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600426}
427
428static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600429 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600430 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431{
432 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
433 unsigned int link[32];
434 unsigned int *cmds = &link[0];
435 int sizedwords = 0;
436 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600437 struct kgsl_context *context;
438 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600440 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530441 * Fix target freeze issue by adding TLB flush for each submit
442 * on A20X based targets.
443 */
444 if (adreno_is_a20x(adreno_dev))
445 flags |= KGSL_MMUFLAGS_TLBFLUSH;
446 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600447 * If possible, then set the state via the command stream to avoid
448 * a CPU idle. Otherwise, use the default setstate which uses register
449 * writes For CFF dump we must idle and use the registers so that it is
450 * easier to filter out the mmu accesses from the dump
451 */
452 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600453 context = idr_find(&device->context_idr, context_id);
Jordan Crouseeef8a132013-01-11 11:17:16 -0700454 if (context == NULL)
455 return;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600456 adreno_ctx = context->devctxt;
457
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
459 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600460 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461 *cmds++ = 0x00000000;
462
463 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600464 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700465 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600466 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467 sizedwords += 4;
468 }
469
470 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
471 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600472 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 1);
474 *cmds++ = 0x00000000;
475 sizedwords += 2;
476 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600477 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 *cmds++ = mh_mmu_invalidate;
479 sizedwords += 2;
480 }
481
482 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600483 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 /* HW workaround: to resolve MMU page fault interrupts
485 * caused by the VGT.It prevents the CP PFP from filling
486 * the VGT DMA request fifo too early,thereby ensuring
487 * that the VGT will not fetch vertex/bin data until
488 * after the page table base register has been updated.
489 *
490 * Two null DRAW_INDX_BIN packets are inserted right
491 * after the page table base update, followed by a
492 * wait for idle. The null packets will fill up the
493 * VGT DMA request fifo and prevent any further
494 * vertex/bin updates from occurring until the wait
495 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600496 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 *cmds++ = (0x4 << 16) |
498 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
499 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600500 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600501 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600502 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 *cmds++ = 0; /* viz query info */
504 *cmds++ = 0x0003C004; /* draw indicator */
505 *cmds++ = 0; /* bin base */
506 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600507 *cmds++ =
508 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600510 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 *cmds++ = 0; /* viz query info */
512 *cmds++ = 0x0003C004; /* draw indicator */
513 *cmds++ = 0; /* bin base */
514 *cmds++ = 3; /* bin size */
515 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600516 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600518 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 *cmds++ = 0x00000000;
520 sizedwords += 21;
521 }
522
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600523
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600525 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 *cmds++ = 0x7fff; /* invalidate all base pointers */
527 sizedwords += 2;
528 }
529
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600530 adreno_ringbuffer_issuecmds(device, adreno_ctx,
531 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600533 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600534 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600535 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536}
537
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600538static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600539 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600540 uint32_t flags)
541{
542 /* call the mmu specific handler */
543 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600544 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600545 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600546 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600547}
548
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700550a3xx_getchipid(struct kgsl_device *device)
551{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600552 struct kgsl_device_platform_data *pdata =
553 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700554
Jordan Crouse54154c62012-03-27 16:33:26 -0600555 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600556 * All current A3XX chipids are detected at the SOC level. Leave this
557 * function here to support any future GPUs that have working
558 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600559 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700560
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600561 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700562}
563
564static unsigned int
565a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566{
567 unsigned int chipid = 0;
568 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600569 struct kgsl_device_platform_data *pdata =
570 kgsl_device_get_drvdata(device);
571
572 /* If the chip id is set at the platform level, then just use that */
573
574 if (pdata->chipid != 0)
575 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576
577 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
578 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
579 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
580
581 /*
582 * adreno 22x gpus are indicated by coreid 2,
583 * but REG_RBBM_PERIPHID1 always contains 0 for this field
584 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600585 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586 chipid = 2 << 24;
587 else
588 chipid = (coreid & 0xF) << 24;
589
590 chipid |= ((majorid >> 4) & 0xF) << 16;
591
592 minorid = ((revid >> 0) & 0xFF);
593
594 patchid = ((revid >> 16) & 0xFF);
595
596 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530597 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598 if (cpu_is_qsd8x50())
599 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530600 else if (cpu_is_msm8625() && minorid == 0)
601 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 chipid |= (minorid << 8) | patchid;
604
605 return chipid;
606}
607
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700608static unsigned int
609adreno_getchipid(struct kgsl_device *device)
610{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600611 struct kgsl_device_platform_data *pdata =
612 kgsl_device_get_drvdata(device);
613
614 /*
615 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
616 * an A2XX processor
617 */
618
619 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700620 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600621 else
622 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700623}
624
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625static inline bool _rev_match(unsigned int id, unsigned int entry)
626{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600627 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700629
630static void
631adreno_identify_gpu(struct adreno_device *adreno_dev)
632{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600633 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634
635 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
636
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600637 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
638 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
639 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
640 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641
Jordan Crouse505df9c2011-07-28 08:37:59 -0600642 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
643 if (core == adreno_gpulist[i].core &&
644 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600645 _rev_match(minor, adreno_gpulist[i].minor) &&
646 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 }
649
Jordan Crouse505df9c2011-07-28 08:37:59 -0600650 if (i == ARRAY_SIZE(adreno_gpulist)) {
651 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
652 return;
653 }
654
655 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
656 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
657 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
658 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700659 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
660 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700661 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600662 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Tarun Karra9c070822012-11-27 16:43:51 -0700663 adreno_dev->gpulist_index = i;
664
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665}
666
Lokesh Batra805e1e12012-08-03 08:34:06 -0600667static struct platform_device_id adreno_id_table[] = {
668 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
669 {},
670};
671
672MODULE_DEVICE_TABLE(platform, adreno_id_table);
673
674static struct of_device_id adreno_match_table[] = {
675 { .compatible = "qcom,kgsl-3d0", },
676 {}
677};
678
679static inline int adreno_of_read_property(struct device_node *node,
680 const char *prop, unsigned int *ptr)
681{
682 int ret = of_property_read_u32(node, prop, ptr);
683 if (ret)
684 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
685 return ret;
686}
687
688static struct device_node *adreno_of_find_subnode(struct device_node *parent,
689 const char *name)
690{
691 struct device_node *child;
692
693 for_each_child_of_node(parent, child) {
694 if (of_device_is_compatible(child, name))
695 return child;
696 }
697
698 return NULL;
699}
700
701static int adreno_of_get_pwrlevels(struct device_node *parent,
702 struct kgsl_device_platform_data *pdata)
703{
704 struct device_node *node, *child;
705 int ret = -EINVAL;
706
707 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
708
709 if (node == NULL) {
710 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
711 return -EINVAL;
712 }
713
714 pdata->num_levels = 0;
715
716 for_each_child_of_node(node, child) {
717 unsigned int index;
718 struct kgsl_pwrlevel *level;
719
720 if (adreno_of_read_property(child, "reg", &index))
721 goto done;
722
723 if (index >= KGSL_MAX_PWRLEVELS) {
724 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
725 index);
726 continue;
727 }
728
729 if (index >= pdata->num_levels)
730 pdata->num_levels = index + 1;
731
732 level = &pdata->pwrlevel[index];
733
734 if (adreno_of_read_property(child, "qcom,gpu-freq",
735 &level->gpu_freq))
736 goto done;
737
738 if (adreno_of_read_property(child, "qcom,bus-freq",
739 &level->bus_freq))
740 goto done;
741
742 if (adreno_of_read_property(child, "qcom,io-fraction",
743 &level->io_fraction))
744 level->io_fraction = 0;
745 }
746
747 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
748 &pdata->init_level))
749 pdata->init_level = 1;
750
751 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
752 KGSL_CORE_ERR("Initial power level out of range\n");
753 pdata->init_level = 1;
754 }
755
756 ret = 0;
757done:
758 return ret;
759
760}
Lokesh Batra805e1e12012-08-03 08:34:06 -0600761
762static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
763{
764 struct device_node *node, *child;
765 struct msm_dcvs_core_info *info = NULL;
766 int count = 0;
767 int ret = -EINVAL;
768
769 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
770 if (node == NULL)
771 return ERR_PTR(-EINVAL);
772
773 info = kzalloc(sizeof(*info), GFP_KERNEL);
774
775 if (info == NULL) {
776 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
777 ret = -ENOMEM;
778 goto err;
779 }
780
781 for_each_child_of_node(node, child)
782 count++;
783
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700784 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600785
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700786 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600787 sizeof(struct msm_dcvs_freq_entry),
788 GFP_KERNEL);
789
790 if (info->freq_tbl == NULL) {
791 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700792 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600793 sizeof(struct msm_dcvs_freq_entry));
794 ret = -ENOMEM;
795 goto err;
796 }
797
798 for_each_child_of_node(node, child) {
799 unsigned int index;
800
801 if (adreno_of_read_property(child, "reg", &index))
802 goto err;
803
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700804 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600805 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
806 index);
807 continue;
808 }
809
810 if (adreno_of_read_property(child, "qcom,freq",
811 &info->freq_tbl[index].freq))
812 goto err;
813
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700814 if (adreno_of_read_property(child, "qcom,voltage",
815 &info->freq_tbl[index].voltage))
816 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600817
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700818 if (adreno_of_read_property(child, "qcom,is_trans_level",
819 &info->freq_tbl[index].is_trans_level))
820 info->freq_tbl[index].is_trans_level = 0;
821
822 if (adreno_of_read_property(child, "qcom,active-energy-offset",
823 &info->freq_tbl[index].active_energy_offset))
824 info->freq_tbl[index].active_energy_offset = 0;
825
826 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
827 &info->freq_tbl[index].leakage_energy_offset))
828 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600829 }
830
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700831 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
832 goto err;
833
834 info->sensors = kzalloc(info->num_cores *
835 sizeof(int),
836 GFP_KERNEL);
837
838 for (count = 0; count < info->num_cores; count++) {
839 if (adreno_of_read_property(node, "qcom,sensors",
840 &(info->sensors[count])))
841 goto err;
842 }
843
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700844 if (adreno_of_read_property(node, "qcom,core-core-type",
845 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600846 goto err;
847
848 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
849 &info->algo_param.disable_pc_threshold))
850 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700851 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
852 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600853 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700854 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
855 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600856 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600857 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
858 &info->algo_param.em_max_util_pct))
859 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700860 if (adreno_of_read_property(node, "qcom,algo-group-id",
861 &info->algo_param.group_id))
862 goto err;
863 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
864 &info->algo_param.max_freq_chg_time_us))
865 goto err;
866 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
867 &info->algo_param.slack_mode_dynamic))
868 goto err;
869 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
870 &info->algo_param.slack_weight_thresh_pct))
871 goto err;
872 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
873 &info->algo_param.slack_time_min_us))
874 goto err;
875 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
876 &info->algo_param.slack_time_max_us))
877 goto err;
878 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
879 &info->algo_param.ss_win_size_min_us))
880 goto err;
881 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
882 &info->algo_param.ss_win_size_max_us))
883 goto err;
884 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
885 &info->algo_param.ss_util_pct))
886 goto err;
Steve Muckle8d0782e2012-12-06 14:31:00 -0800887 if (adreno_of_read_property(node, "qcom,algo-ss-no-corr-below-freq",
888 &info->algo_param.ss_no_corr_below_freq))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600889 goto err;
890
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700891 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
892 &info->energy_coeffs.active_coeff_a))
893 goto err;
894 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
895 &info->energy_coeffs.active_coeff_b))
896 goto err;
897 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
898 &info->energy_coeffs.active_coeff_c))
899 goto err;
900 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
901 &info->energy_coeffs.leakage_coeff_a))
902 goto err;
903 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
904 &info->energy_coeffs.leakage_coeff_b))
905 goto err;
906 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
907 &info->energy_coeffs.leakage_coeff_c))
908 goto err;
909 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
910 &info->energy_coeffs.leakage_coeff_d))
911 goto err;
912
913 if (adreno_of_read_property(node, "qcom,power-current-temp",
914 &info->power_param.current_temp))
915 goto err;
916
Lokesh Batra805e1e12012-08-03 08:34:06 -0600917 return info;
918
919err:
920 if (info)
921 kfree(info->freq_tbl);
922
923 kfree(info);
924
925 return ERR_PTR(ret);
926}
927
928static int adreno_of_get_iommu(struct device_node *parent,
929 struct kgsl_device_platform_data *pdata)
930{
931 struct device_node *node, *child;
932 struct kgsl_device_iommu_data *data = NULL;
933 struct kgsl_iommu_ctx *ctxs = NULL;
934 u32 reg_val[2];
935 int ctx_index = 0;
936
937 node = of_parse_phandle(parent, "iommu", 0);
938 if (node == NULL)
939 return -EINVAL;
940
941 data = kzalloc(sizeof(*data), GFP_KERNEL);
942 if (data == NULL) {
943 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
944 goto err;
945 }
946
947 if (of_property_read_u32_array(node, "reg", reg_val, 2))
948 goto err;
949
950 data->physstart = reg_val[0];
951 data->physend = data->physstart + reg_val[1] - 1;
952
953 data->iommu_ctx_count = 0;
954
955 for_each_child_of_node(node, child)
956 data->iommu_ctx_count++;
957
958 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
959 GFP_KERNEL);
960
961 if (ctxs == NULL) {
962 KGSL_CORE_ERR("kzalloc(%d) failed\n",
963 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
964 goto err;
965 }
966
967 for_each_child_of_node(node, child) {
968 int ret = of_property_read_string(child, "label",
969 &ctxs[ctx_index].iommu_ctx_name);
970
971 if (ret) {
972 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
973 goto err;
974 }
975
976 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
977 &ctxs[ctx_index].ctx_id))
978 goto err;
979
980 ctx_index++;
981 }
982
983 data->iommu_ctxs = ctxs;
984
985 pdata->iommu_data = data;
986 pdata->iommu_count = 1;
987
988 return 0;
989
990err:
991 kfree(ctxs);
992 kfree(data);
993
994 return -EINVAL;
995}
996
997static int adreno_of_get_pdata(struct platform_device *pdev)
998{
999 struct kgsl_device_platform_data *pdata = NULL;
1000 struct kgsl_device *device;
1001 int ret = -EINVAL;
1002
1003 pdev->id_entry = adreno_id_table;
1004
1005 pdata = pdev->dev.platform_data;
1006 if (pdata)
1007 return 0;
1008
1009 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1010 KGSL_CORE_ERR("Unable to read 'label'\n");
1011 goto err;
1012 }
1013
1014 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1015 goto err;
1016
1017 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1018 if (pdata == NULL) {
1019 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1020 ret = -ENOMEM;
1021 goto err;
1022 }
1023
1024 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1025 &pdata->chipid))
1026 goto err;
1027
1028 /* pwrlevel Data */
1029 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1030 if (ret)
1031 goto err;
1032
1033 /* Default value is 83, if not found in DT */
1034 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1035 &pdata->idle_timeout))
1036 pdata->idle_timeout = 83;
1037
1038 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1039 &pdata->nap_allowed))
1040 pdata->nap_allowed = 1;
1041
1042 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1043 &pdata->clk_map))
1044 goto err;
1045
1046 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1047
1048 if (device->id != KGSL_DEVICE_3D0)
1049 goto err;
1050
1051 /* Bus Scale Data */
1052
Rajeev Kulkarnic9162002012-11-22 00:42:58 -08001053 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
Lokesh Batra805e1e12012-08-03 08:34:06 -06001054 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1055 ret = PTR_ERR(pdata->bus_scale_table);
1056 goto err;
1057 }
1058
1059 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1060 if (IS_ERR_OR_NULL(pdata->core_info)) {
1061 ret = PTR_ERR(pdata->core_info);
1062 goto err;
1063 }
1064
1065 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1066 if (ret)
1067 goto err;
1068
1069 pdev->dev.platform_data = pdata;
1070 return 0;
1071
1072err:
1073 if (pdata) {
Lokesh Batra805e1e12012-08-03 08:34:06 -06001074 if (pdata->core_info)
1075 kfree(pdata->core_info->freq_tbl);
1076 kfree(pdata->core_info);
1077
1078 if (pdata->iommu_data)
1079 kfree(pdata->iommu_data->iommu_ctxs);
1080
1081 kfree(pdata->iommu_data);
1082 }
1083
1084 kfree(pdata);
1085
1086 return ret;
1087}
1088
liu zhong7dfa2a32012-04-27 19:11:01 -07001089#ifdef CONFIG_MSM_OCMEM
1090static int
1091adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1092{
Jordan Crousec0978202012-08-29 14:35:51 -06001093 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001094 return 0;
1095
1096 /* OCMEM is only needed once, do not support consective allocation */
1097 if (adreno_dev->ocmem_hdl != NULL)
1098 return 0;
1099
1100 adreno_dev->ocmem_hdl =
1101 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1102 if (adreno_dev->ocmem_hdl == NULL)
1103 return -ENOMEM;
1104
1105 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001106 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001107
1108 return 0;
1109}
1110
1111static void
1112adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1113{
Jordan Crousec0978202012-08-29 14:35:51 -06001114 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001115 return;
1116
1117 if (adreno_dev->ocmem_hdl == NULL)
1118 return;
1119
1120 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1121 adreno_dev->ocmem_hdl = NULL;
1122}
1123#else
1124static int
1125adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1126{
1127 return 0;
1128}
1129
1130static void
1131adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1132{
1133}
1134#endif
1135
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001136static int __devinit
1137adreno_probe(struct platform_device *pdev)
1138{
1139 struct kgsl_device *device;
1140 struct adreno_device *adreno_dev;
1141 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001142 bool is_dt;
1143
1144 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1145
1146 if (is_dt && pdev->dev.of_node) {
1147 status = adreno_of_get_pdata(pdev);
1148 if (status)
1149 goto error_return;
1150 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151
1152 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1153 adreno_dev = ADRENO_DEVICE(device);
1154 device->parentdev = &pdev->dev;
1155
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001156 status = adreno_ringbuffer_init(device);
1157 if (status != 0)
1158 goto error;
1159
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001160 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161 if (status)
1162 goto error_close_rb;
1163
1164 adreno_debugfs_init(device);
1165
1166 kgsl_pwrscale_init(device);
1167 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1168
1169 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1170 return 0;
1171
1172error_close_rb:
1173 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1174error:
1175 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001176error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 return status;
1178}
1179
1180static int __devexit adreno_remove(struct platform_device *pdev)
1181{
1182 struct kgsl_device *device;
1183 struct adreno_device *adreno_dev;
1184
1185 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1186 adreno_dev = ADRENO_DEVICE(device);
1187
1188 kgsl_pwrscale_detach_policy(device);
1189 kgsl_pwrscale_close(device);
1190
1191 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1192 kgsl_device_platform_remove(device);
1193
1194 return 0;
1195}
1196
1197static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1198{
1199 int status = -EINVAL;
1200 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001202 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1203 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001204
1205 /* Power up the device */
1206 kgsl_pwrctrl_enable(device);
1207
1208 /* Identify the specific GPU */
1209 adreno_identify_gpu(adreno_dev);
1210
Tarun Karra9c070822012-11-27 16:43:51 -07001211 if (adreno_ringbuffer_read_pm4_ucode(device)) {
1212 KGSL_DRV_ERR(device, "Reading pm4 microcode failed %s\n",
1213 adreno_dev->pm4_fwfile);
1214 BUG_ON(1);
1215 }
1216
1217 if (adreno_ringbuffer_read_pfp_ucode(device)) {
1218 KGSL_DRV_ERR(device, "Reading pfp microcode failed %s\n",
1219 adreno_dev->pfp_fwfile);
1220 BUG_ON(1);
1221 }
1222
Jordan Crouse505df9c2011-07-28 08:37:59 -06001223 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1224 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1225 adreno_dev->chip_id);
1226 goto error_clk_off;
1227 }
1228
Tarun Karra9c070822012-11-27 16:43:51 -07001229
1230 /*
1231 * Check if firmware supports the sync lock PM4 packets needed
1232 * for IOMMUv1
1233 */
1234
1235 if ((adreno_dev->pm4_fw_version >=
1236 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pm4_ver) &&
1237 (adreno_dev->pfp_fw_version >=
1238 adreno_gpulist[adreno_dev->gpulist_index].sync_lock_pfp_ver))
1239 device->mmu.flags |= KGSL_MMU_FLAGS_IOMMU_SYNC;
1240
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001241 /* Set up the MMU */
1242 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001243 /*
1244 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1245 * on older gpus
1246 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001247 if (adreno_is_a20x(adreno_dev)) {
1248 device->mh.mh_intf_cfg1 = 0;
1249 device->mh.mh_intf_cfg2 = 0;
1250 }
1251
1252 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001253 }
1254
Tarun Karra3335f142012-06-19 14:11:48 -07001255 /* Assign correct RBBM status register to hang detect regs
1256 */
1257 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1258
Jordan Crouseb5c80482012-10-03 09:38:41 -06001259 /* Add A3XX specific registers for hang detection */
1260 if (adreno_is_a3xx(adreno_dev)) {
1261 hang_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1262 hang_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
Tarun Karra6e750d72013-01-04 10:28:40 -08001263 hang_detect_regs[8] = A3XX_RBBM_PERFCTR_SP_6_LO;
1264 hang_detect_regs[9] = A3XX_RBBM_PERFCTR_SP_6_HI;
1265 hang_detect_regs[10] = A3XX_RBBM_PERFCTR_SP_5_LO;
1266 hang_detect_regs[11] = A3XX_RBBM_PERFCTR_SP_5_HI;
Jordan Crouseb5c80482012-10-03 09:38:41 -06001267 }
1268
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001269 status = kgsl_mmu_start(device);
1270 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271 goto error_clk_off;
1272
liu zhong7dfa2a32012-04-27 19:11:01 -07001273 status = adreno_ocmem_gmem_malloc(adreno_dev);
1274 if (status) {
1275 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1276 goto error_mmu_off;
1277 }
1278
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001279 /* Start the GPU */
1280 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281
1282 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001283 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001284
1285 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001286 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001287 /* While recovery is on we do not want timer to
1288 * fire and attempt to change any device state */
1289 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1290 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001291 return 0;
1292 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001295
1296error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001297 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001298
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001299error_clk_off:
1300 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301
1302 return status;
1303}
1304
1305static int adreno_stop(struct kgsl_device *device)
1306{
1307 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1308
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309 adreno_dev->drawctxt_active = NULL;
1310
1311 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1312
Shubhraprakash Das79447952012-04-26 18:12:23 -06001313 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001315 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301316 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001317 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001318
liu zhong7dfa2a32012-04-27 19:11:01 -07001319 adreno_ocmem_gmem_free(adreno_dev);
1320
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001321 /* Power down the device */
1322 kgsl_pwrctrl_disable(device);
1323
1324 return 0;
1325}
1326
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001327static void adreno_mark_context_status(struct kgsl_device *device,
1328 int recovery_status)
1329{
1330 struct kgsl_context *context;
1331 int next = 0;
1332 /*
1333 * Set the reset status of all contexts to
1334 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1335 * since thats the guilty party, if recovery failed then
1336 * mark all as guilty
1337 */
1338 while ((context = idr_get_next(&device->context_idr, &next))) {
1339 struct adreno_context *adreno_context = context->devctxt;
1340 if (recovery_status) {
1341 context->reset_status =
1342 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1343 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1344 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1345 context->reset_status) {
Carter Cooper8179f5a2012-12-17 11:32:27 -07001346 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG |
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001347 CTXT_FLAGS_GPU_HANG_RECOVERED))
1348 context->reset_status =
1349 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1350 else
1351 context->reset_status =
1352 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1353 }
1354 next = next + 1;
1355 }
1356}
1357
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001358static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1359{
1360 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1361 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1362 struct kgsl_context *context;
1363 struct adreno_context *temp_adreno_context;
1364 int next = 0;
1365
1366 while ((context = idr_get_next(&device->context_idr, &next))) {
1367 temp_adreno_context = context->devctxt;
1368 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1369 kgsl_sharedmem_writel(&device->memstore,
1370 KGSL_MEMSTORE_OFFSET(context->id,
1371 soptimestamp),
1372 rb->timestamp[context->id]);
1373 kgsl_sharedmem_writel(&device->memstore,
1374 KGSL_MEMSTORE_OFFSET(context->id,
1375 eoptimestamp),
1376 rb->timestamp[context->id]);
1377 }
1378 next = next + 1;
1379 }
1380}
1381
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001382static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1383{
1384 vfree(rec_data->rb_buffer);
1385 vfree(rec_data->bad_rb_buffer);
1386}
1387
1388static int adreno_setup_recovery_data(struct kgsl_device *device,
1389 struct adreno_recovery_data *rec_data)
1390{
1391 int ret = 0;
1392 unsigned int ib1_sz, ib2_sz;
1393 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1394 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1395
1396 memset(rec_data, 0, sizeof(*rec_data));
1397
1398 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1399 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1400 if (ib1_sz || ib2_sz)
1401 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1402
1403 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1404 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1405 current_context));
1406
1407 kgsl_sharedmem_readl(&device->memstore,
1408 &rec_data->global_eop,
1409 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1410 eoptimestamp));
1411
1412 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1413 if (!rec_data->rb_buffer) {
1414 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1415 rb->buffer_desc.size);
1416 return -ENOMEM;
1417 }
1418
1419 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1420 if (!rec_data->bad_rb_buffer) {
1421 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1422 rb->buffer_desc.size);
1423 ret = -ENOMEM;
1424 goto done;
1425 }
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001426 rec_data->fault = device->mmu.fault;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001427
1428done:
1429 if (ret) {
1430 vfree(rec_data->rb_buffer);
1431 vfree(rec_data->bad_rb_buffer);
1432 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 return ret;
1434}
1435
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001436static int
1437_adreno_recover_hang(struct kgsl_device *device,
1438 struct adreno_recovery_data *rec_data,
1439 bool try_bad_commands)
1440{
1441 int ret;
1442 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1443 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1444 struct kgsl_context *context;
1445 struct adreno_context *adreno_context = NULL;
1446 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1447
1448 context = idr_find(&device->context_idr, rec_data->context_id);
1449 if (context == NULL) {
1450 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1451 rec_data->context_id);
1452 } else {
1453 adreno_context = context->devctxt;
1454 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Rajeev Kulkarni46ee1092012-12-14 14:47:55 -08001455 /*
1456 * set the invalid ts flag to 0 for this context since we have
1457 * detected a hang for it
1458 */
1459 context->wait_on_invalid_ts = false;
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001460 }
1461
1462 /* Extract valid contents from rb which can still be executed after
1463 * hang */
1464 ret = adreno_ringbuffer_extract(rb, rec_data);
1465 if (ret)
1466 goto done;
1467
1468 /* restart device */
1469 ret = adreno_stop(device);
1470 if (ret) {
1471 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1472 goto done;
1473 }
1474
1475 ret = adreno_start(device, true);
1476 if (ret) {
1477 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1478 goto done;
1479 }
1480
1481 if (context)
1482 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1483 KGSL_MEMSTORE_GLOBAL);
1484
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001485 /* If iommu is used then we need to make sure that the iommu clocks
1486 * are on since there could be commands in pipeline that touch iommu */
1487 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1488 ret = kgsl_mmu_enable_clk(&device->mmu,
1489 KGSL_IOMMU_CONTEXT_USER);
1490 if (ret)
1491 goto done;
1492 }
1493
Shubhraprakash Das2747cf62012-09-27 23:05:43 -07001494 /* Do not try the bad commands if recovery has failed bad commands
1495 * once already or if hang is due to a fault */
1496 if (!try_bad_commands || rec_data->fault)
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001497 rec_data->bad_rb_size = 0;
1498
1499 if (rec_data->bad_rb_size) {
1500 int idle_ret;
1501 /* submit the bad and good context commands and wait for
1502 * them to pass */
1503 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1504 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001505 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001506 if (idle_ret) {
1507 ret = adreno_stop(device);
1508 if (ret) {
1509 KGSL_DRV_ERR(device,
1510 "Device stop failed in recovery\n");
1511 goto done;
1512 }
1513 ret = adreno_start(device, true);
1514 if (ret) {
1515 KGSL_DRV_ERR(device,
1516 "Device start failed in recovery\n");
1517 goto done;
1518 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001519 if (context)
1520 kgsl_mmu_setstate(&device->mmu,
1521 adreno_context->pagetable,
1522 KGSL_MEMSTORE_GLOBAL);
1523
1524 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1525 ret = kgsl_mmu_enable_clk(&device->mmu,
1526 KGSL_IOMMU_CONTEXT_USER);
1527 if (ret)
1528 goto done;
1529 }
1530
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001531 ret = idle_ret;
1532 KGSL_DRV_ERR(device,
1533 "Bad context commands hung in recovery\n");
1534 } else {
1535 KGSL_DRV_ERR(device,
1536 "Bad context commands succeeded in recovery\n");
1537 if (adreno_context)
1538 adreno_context->flags = (adreno_context->flags &
1539 ~CTXT_FLAGS_GPU_HANG) |
1540 CTXT_FLAGS_GPU_HANG_RECOVERED;
1541 adreno_dev->drawctxt_active = last_active_ctx;
1542 }
1543 }
1544 /* If either the bad command sequence failed or we did not play it */
1545 if (ret || !rec_data->bad_rb_size) {
1546 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1547 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001548 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001549 if (ret) {
1550 /* If we fail here we can try to invalidate another
1551 * context and try recovering again */
1552 ret = -EAGAIN;
1553 goto done;
1554 }
1555 /* ringbuffer now has data from the last valid context id,
1556 * so restore the active_ctx to the last valid context */
1557 if (rec_data->last_valid_ctx_id) {
1558 struct kgsl_context *last_ctx =
1559 idr_find(&device->context_idr,
1560 rec_data->last_valid_ctx_id);
1561 if (last_ctx)
1562 adreno_dev->drawctxt_active = last_ctx->devctxt;
1563 }
1564 }
1565done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001566 /* Turn off iommu clocks */
1567 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1568 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001569 return ret;
1570}
1571
1572static int
1573adreno_recover_hang(struct kgsl_device *device,
1574 struct adreno_recovery_data *rec_data)
1575{
1576 int ret = 0;
1577 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1578 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1579 unsigned int timestamp;
1580
1581 KGSL_DRV_ERR(device,
1582 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1583 "Bad context_id: %u, global_eop: 0x%x\n",
1584 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1585
1586 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1587 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1588
1589 /* We may need to replay commands multiple times based on whether
1590 * multiple contexts hang the GPU */
1591 while (true) {
1592 if (!ret)
1593 ret = _adreno_recover_hang(device, rec_data, true);
1594 else
1595 ret = _adreno_recover_hang(device, rec_data, false);
1596
1597 if (-EAGAIN == ret) {
1598 /* setup new recovery parameters and retry, this
1599 * means more than 1 contexts are causing hang */
1600 adreno_destroy_recovery_data(rec_data);
1601 adreno_setup_recovery_data(device, rec_data);
1602 KGSL_DRV_ERR(device,
1603 "Retry recovery from 3D GPU hang. Recovery parameters: "
1604 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1605 rec_data->ib1, rec_data->context_id,
1606 rec_data->global_eop);
1607 } else {
1608 break;
1609 }
1610 }
1611
1612 if (ret)
1613 goto done;
1614
1615 /* Restore correct states after recovery */
1616 if (adreno_dev->drawctxt_active)
1617 device->mmu.hwpagetable =
1618 adreno_dev->drawctxt_active->pagetable;
1619 else
1620 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1621 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1622 kgsl_sharedmem_writel(&device->memstore,
1623 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1624 eoptimestamp),
1625 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
Carter Cooper8179f5a2012-12-17 11:32:27 -07001626
1627 /* switch to NULL ctxt */
1628 if (adreno_dev->drawctxt_active != NULL)
1629 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1630
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001631done:
1632 adreno_set_max_ts_for_bad_ctxs(device);
1633 adreno_mark_context_status(device, ret);
1634 if (!ret)
1635 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1636 else
1637 KGSL_DRV_ERR(device, "Recovery failed\n");
1638 return ret;
1639}
1640
1641int
1642adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001643{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001644 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001645 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001646
1647 if (device->state == KGSL_STATE_HUNG)
1648 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001649 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001650 mutex_unlock(&device->mutex);
1651 wait_for_completion(&device->recovery_gate);
1652 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001653 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001654 result = 0;
1655 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001656 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001657 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001658 /* Detected a hang */
1659
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001660 /* Get the recovery data as soon as hang is detected */
1661 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001662 /*
1663 * Trigger an automatic dump of the state to
1664 * the console
1665 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001666 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001667
1668 /*
1669 * Make a GPU snapshot. For now, do it after the PM dump so we
1670 * can at least be sure the PM dump will work as it always has
1671 */
1672 kgsl_device_snapshot(device, 1);
1673
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001674 result = adreno_recover_hang(device, &rec_data);
1675 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001676 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001677 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001678 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001679 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001680 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1681 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001682 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001683 }
1684done:
1685 return result;
1686}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001687EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001688
1689static int adreno_getproperty(struct kgsl_device *device,
1690 enum kgsl_property_type type,
1691 void *value,
1692 unsigned int sizebytes)
1693{
1694 int status = -EINVAL;
1695 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1696
1697 switch (type) {
1698 case KGSL_PROP_DEVICE_INFO:
1699 {
1700 struct kgsl_devinfo devinfo;
1701
1702 if (sizebytes != sizeof(devinfo)) {
1703 status = -EINVAL;
1704 break;
1705 }
1706
1707 memset(&devinfo, 0, sizeof(devinfo));
1708 devinfo.device_id = device->id+1;
1709 devinfo.chip_id = adreno_dev->chip_id;
1710 devinfo.mmu_enabled = kgsl_mmu_enabled();
1711 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001712 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1713 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001714
1715 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1716 0) {
1717 status = -EFAULT;
1718 break;
1719 }
1720 status = 0;
1721 }
1722 break;
1723 case KGSL_PROP_DEVICE_SHADOW:
1724 {
1725 struct kgsl_shadowprop shadowprop;
1726
1727 if (sizebytes != sizeof(shadowprop)) {
1728 status = -EINVAL;
1729 break;
1730 }
1731 memset(&shadowprop, 0, sizeof(shadowprop));
1732 if (device->memstore.hostptr) {
1733 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1734 * anything to mmap().
1735 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001736 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001737 shadowprop.size = device->memstore.size;
1738 /* GSL needs this to be set, even if it
1739 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001740 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1741 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001742 }
1743 if (copy_to_user(value, &shadowprop,
1744 sizeof(shadowprop))) {
1745 status = -EFAULT;
1746 break;
1747 }
1748 status = 0;
1749 }
1750 break;
1751 case KGSL_PROP_MMU_ENABLE:
1752 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001753 int mmu_prop = kgsl_mmu_enabled();
1754
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001755 if (sizebytes != sizeof(int)) {
1756 status = -EINVAL;
1757 break;
1758 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001759 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001760 status = -EFAULT;
1761 break;
1762 }
1763 status = 0;
1764 }
1765 break;
1766 case KGSL_PROP_INTERRUPT_WAITS:
1767 {
1768 int int_waits = 1;
1769 if (sizebytes != sizeof(int)) {
1770 status = -EINVAL;
1771 break;
1772 }
1773 if (copy_to_user(value, &int_waits, sizeof(int))) {
1774 status = -EFAULT;
1775 break;
1776 }
1777 status = 0;
1778 }
1779 break;
1780 default:
1781 status = -EINVAL;
1782 }
1783
1784 return status;
1785}
1786
Jordan Crousef7370f82012-04-18 09:31:07 -06001787static int adreno_setproperty(struct kgsl_device *device,
1788 enum kgsl_property_type type,
1789 void *value,
1790 unsigned int sizebytes)
1791{
1792 int status = -EINVAL;
Tarun Karra6e750d72013-01-04 10:28:40 -08001793 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jordan Crousef7370f82012-04-18 09:31:07 -06001794
1795 switch (type) {
1796 case KGSL_PROP_PWRCTRL: {
1797 unsigned int enable;
1798 struct kgsl_device_platform_data *pdata =
1799 kgsl_device_get_drvdata(device);
1800
1801 if (sizebytes != sizeof(enable))
1802 break;
1803
1804 if (copy_from_user(&enable, (void __user *) value,
1805 sizeof(enable))) {
1806 status = -EFAULT;
1807 break;
1808 }
1809
1810 if (enable) {
1811 if (pdata->nap_allowed)
1812 device->pwrctrl.nap_allowed = true;
Tarun Karra6e750d72013-01-04 10:28:40 -08001813 adreno_dev->fast_hang_detect = 1;
Jordan Crousef7370f82012-04-18 09:31:07 -06001814 kgsl_pwrscale_enable(device);
1815 } else {
1816 device->pwrctrl.nap_allowed = false;
Tarun Karra6e750d72013-01-04 10:28:40 -08001817 adreno_dev->fast_hang_detect = 0;
Jordan Crousef7370f82012-04-18 09:31:07 -06001818 kgsl_pwrscale_disable(device);
1819 }
1820
1821 status = 0;
1822 }
1823 break;
1824 default:
1825 break;
1826 }
1827
1828 return status;
1829}
1830
Jordan Crousea29a2e02012-08-14 09:09:23 -06001831static int adreno_ringbuffer_drain(struct kgsl_device *device,
1832 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001833{
1834 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1835 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001836 unsigned long wait;
1837 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1838
1839 if (!(rb->flags & KGSL_FLAGS_STARTED))
1840 return 0;
1841
1842 /*
1843 * The first time into the loop, wait for 100 msecs and kick wptr again
1844 * to ensure that the hardware has updated correctly. After that, kick
1845 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1846 * expires
1847 */
1848
1849 wait = jiffies + msecs_to_jiffies(100);
1850
Jordan Crousea29a2e02012-08-14 09:09:23 -06001851 do {
1852 if (time_after(jiffies, wait)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001853 /* Check to see if the core is hung */
1854 if (adreno_hang_detect(device, regs))
1855 return -ETIMEDOUT;
1856
1857 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1858 }
1859 GSL_RB_GET_READPTR(rb, &rb->rptr);
1860
1861 if (time_after(jiffies, timeout)) {
1862 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1863 rb->rptr, rb->wptr);
1864 return -ETIMEDOUT;
1865 }
1866 } while (rb->rptr != rb->wptr);
1867
1868 return 0;
1869}
1870
1871/* Caller must hold the device mutex. */
1872int adreno_idle(struct kgsl_device *device)
1873{
1874 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001875 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301876 unsigned long wait_time;
1877 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001878 unsigned int prev_reg_val[hang_detect_regs_count];
1879
1880 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001881
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001882 kgsl_cffdump_regpoll(device->id,
1883 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001884 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001885
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001887 /* First, wait for the ringbuffer to drain */
1888 if (adreno_ringbuffer_drain(device, prev_reg_val))
1889 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001890
1891 /* now, wait for the GPU to finish its operations */
Jordan Crouse0bcdb732012-10-25 09:37:43 -06001892 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001893 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1894
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001895 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001896 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1897 &rbbm_status);
1898 if (adreno_is_a2xx(adreno_dev)) {
1899 if (rbbm_status == 0x110)
1900 return 0;
1901 } else {
1902 if (!(rbbm_status & 0x80000000))
1903 return 0;
1904 }
Tarun Karra3335f142012-06-19 14:11:48 -07001905
1906 /* Dont wait for timeout, detect hang faster.
1907 */
1908 if (time_after(jiffies, wait_time_part)) {
1909 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001910 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001911 if ((adreno_hang_detect(device, prev_reg_val)))
1912 goto err;
1913 }
1914
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001915 }
1916
1917err:
1918 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001919 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1920 !adreno_dump_and_recover(device)) {
Jordan Crouse0bcdb732012-10-25 09:37:43 -06001921 wait_time = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001922 goto retry;
1923 }
1924 return -ETIMEDOUT;
1925}
1926
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08001927/**
1928 * is_adreno_rbbm_status_idle - Check if GPU core is idle by probing
1929 * rbbm_status register
1930 * @device - Pointer to the GPU device whose idle status is to be
1931 * checked
1932 * @returns - Returns whether the core is idle (based on rbbm_status)
1933 * false if the core is active, true if the core is idle
1934 */
1935static bool is_adreno_rbbm_status_idle(struct kgsl_device *device)
1936{
1937 unsigned int reg_rbbm_status;
1938 bool status = false;
1939 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1940
1941 /* Is the core idle? */
1942 adreno_regread(device,
1943 adreno_dev->gpudev->reg_rbbm_status,
1944 &reg_rbbm_status);
1945
1946 if (adreno_is_a2xx(adreno_dev)) {
1947 if (reg_rbbm_status == 0x110)
1948 status = true;
1949 } else {
1950 if (!(reg_rbbm_status & 0x80000000))
1951 status = true;
1952 }
1953 return status;
1954}
1955
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001956static unsigned int adreno_isidle(struct kgsl_device *device)
1957{
1958 int status = false;
1959 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1960 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001961
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001962 WARN_ON(device->state == KGSL_STATE_INIT);
1963 /* If the device isn't active, don't force it on. */
1964 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001965 /* Is the ring buffer is empty? */
1966 GSL_RB_GET_READPTR(rb, &rb->rptr);
1967 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1968 /* Is the core idle? */
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08001969 status = is_adreno_rbbm_status_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001970 }
1971 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001972 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001973 }
1974 return status;
1975}
1976
1977/* Caller must hold the device mutex. */
1978static int adreno_suspend_context(struct kgsl_device *device)
1979{
1980 int status = 0;
1981 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1982
1983 /* switch to NULL ctxt */
1984 if (adreno_dev->drawctxt_active != NULL) {
1985 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001986 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001987 }
1988
1989 return status;
1990}
1991
Jordan Crouse233b2092012-04-18 09:31:09 -06001992/* Find a memory structure attached to an adreno context */
1993
1994struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1995 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1996{
1997 struct kgsl_context *context;
1998 struct adreno_context *adreno_context = NULL;
1999 int next = 0;
2000
2001 while (1) {
2002 context = idr_get_next(&device->context_idr, &next);
2003 if (context == NULL)
2004 break;
2005
2006 adreno_context = (struct adreno_context *)context->devctxt;
2007
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002008 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2009 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002010 struct kgsl_memdesc *desc;
2011
2012 desc = &adreno_context->gpustate;
2013 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2014 return desc;
2015
2016 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2017 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2018 return desc;
2019 }
2020 next = next + 1;
2021 }
2022
2023 return NULL;
2024}
2025
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002026struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002027 unsigned int pt_base,
2028 unsigned int gpuaddr,
2029 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002030{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002031 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002032 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2033 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2034
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002035 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2036 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002037
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002038 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2039 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002040
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002041 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2042 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002043
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002044 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2045 size))
2046 return &device->mmu.setstate_memory;
2047
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002048 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002049
2050 if (entry)
2051 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002052
Jordan Crouse233b2092012-04-18 09:31:09 -06002053 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002054}
2055
2056uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2057 unsigned int gpuaddr, unsigned int size)
2058{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002059 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002060
2061 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2062
2063 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002064}
2065
2066void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2067 unsigned int *value)
2068{
2069 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002070 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2071 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002072
2073 if (!in_interrupt())
2074 kgsl_pre_hwaccess(device);
2075
2076 /*ensure this read finishes before the next one.
2077 * i.e. act like normal readl() */
2078 *value = __raw_readl(reg);
2079 rmb();
2080}
2081
2082void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2083 unsigned int value)
2084{
2085 unsigned int *reg;
2086
Jordan Crouse7501d452012-04-19 08:58:44 -06002087 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088
2089 if (!in_interrupt())
2090 kgsl_pre_hwaccess(device);
2091
2092 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002093 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002094
2095 /*ensure previous writes post before this one,
2096 * i.e. act like normal writel() */
2097 wmb();
2098 __raw_writel(value, reg);
2099}
2100
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002101static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2102{
2103 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002104 if (k_ctxt != NULL) {
2105 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002106 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2107 context_id = KGSL_CONTEXT_INVALID;
2108 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2109 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002110 }
2111
2112 return context_id;
2113}
2114
Carter Cooper740f6742013-01-03 16:19:23 -07002115static unsigned int adreno_check_hw_ts(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002116 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002117{
Carter Cooper740f6742013-01-03 16:19:23 -07002118 int status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002119 unsigned int ref_ts, enableflag;
Carter Cooper740f6742013-01-03 16:19:23 -07002120 unsigned int context_id = _get_context_id(context);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002121
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002122 /*
2123 * If the context ID is invalid, we are in a race with
2124 * the context being destroyed by userspace so bail.
2125 */
2126 if (context_id == KGSL_CONTEXT_INVALID) {
2127 KGSL_DRV_WARN(device, "context was detached");
Carter Cooper740f6742013-01-03 16:19:23 -07002128 return -EINVAL;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002129 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002130
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002131 status = kgsl_check_timestamp(device, context, timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002132 if (status)
2133 return status;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002134
Carter Cooper740f6742013-01-03 16:19:23 -07002135 kgsl_sharedmem_readl(&device->memstore, &enableflag,
2136 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
2137 /*
2138 * Barrier is needed here to make sure the read from memstore
2139 * has posted
2140 */
2141
2142 mb();
2143
2144 if (enableflag) {
2145 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002146 KGSL_MEMSTORE_OFFSET(context_id,
2147 ref_wait_ts));
Carter Cooper740f6742013-01-03 16:19:23 -07002148
2149 /* Make sure the memstore read has posted */
2150 mb();
2151 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
2152 kgsl_sharedmem_writel(&device->memstore,
2153 KGSL_MEMSTORE_OFFSET(context_id,
2154 ref_wait_ts), timestamp);
2155 /* Make sure the memstore write is posted */
2156 wmb();
2157 }
2158 } else {
2159 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002160 KGSL_MEMSTORE_OFFSET(context_id,
2161 ref_wait_ts), timestamp);
Carter Cooper740f6742013-01-03 16:19:23 -07002162 enableflag = 1;
2163 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002164 KGSL_MEMSTORE_OFFSET(context_id,
2165 ts_cmp_enable), enableflag);
Carter Cooper740f6742013-01-03 16:19:23 -07002166 /* Make sure the memstore write gets posted */
2167 wmb();
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002168
Carter Cooper740f6742013-01-03 16:19:23 -07002169 /*
2170 * submit a dummy packet so that even if all
2171 * commands upto timestamp get executed we will still
2172 * get an interrupt
2173 */
2174
2175 if (context && device->state != KGSL_STATE_SLUMBER)
2176 adreno_ringbuffer_issuecmds(device, context->devctxt,
2177 KGSL_CMD_FLAGS_NONE, NULL, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178 }
Carter Cooper740f6742013-01-03 16:19:23 -07002179
2180 return 0;
2181}
2182
2183static void adreno_next_event(struct kgsl_device *device,
2184 struct kgsl_event *event)
2185{
2186 adreno_check_hw_ts(device, event->context, event->timestamp);
2187}
2188
2189static int adreno_check_interrupt_timestamp(struct kgsl_device *device,
2190 struct kgsl_context *context, unsigned int timestamp)
2191{
2192 int status;
2193
2194 mutex_lock(&device->mutex);
2195 status = adreno_check_hw_ts(device, context, timestamp);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002196 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197
2198 return status;
2199}
2200
2201/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002202 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002203 placing a process in wait q. For conditional interrupts we expect the
2204 process to already be in its wait q when its exit condition checking
2205 function is called.
2206*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002207#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208({ \
2209 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002210 if (io) \
2211 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2212 else \
2213 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002214 __ret; \
2215})
2216
Tarun Karra3335f142012-06-19 14:11:48 -07002217
2218
2219unsigned int adreno_hang_detect(struct kgsl_device *device,
2220 unsigned int *prev_reg_val)
2221{
2222 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2223 unsigned int curr_reg_val[hang_detect_regs_count];
2224 unsigned int hang_detected = 1;
2225 unsigned int i;
2226
2227 if (!adreno_dev->fast_hang_detect)
2228 return 0;
2229
Jordan Crousecca61142012-11-20 10:54:24 -07002230 if (is_adreno_rbbm_status_idle(device)) {
2231
2232 /*
2233 * On A20X if the RPTR != WPTR and the device is idle, then
2234 * the last write to WPTR probably failed to latch so write it
2235 * again
2236 */
2237
2238 if (adreno_is_a2xx(adreno_dev)) {
2239 unsigned int rptr;
2240 adreno_regread(device, REG_CP_RB_RPTR, &rptr);
2241 if (rptr != adreno_dev->ringbuffer.wptr)
2242 adreno_regwrite(device, REG_CP_RB_WPTR,
2243 adreno_dev->ringbuffer.wptr);
2244 }
2245
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002246 return 0;
Jordan Crousecca61142012-11-20 10:54:24 -07002247 }
Rajeev Kulkarni12d7dcd2012-11-22 00:27:35 -08002248
Tarun Karra3335f142012-06-19 14:11:48 -07002249 for (i = 0; i < hang_detect_regs_count; i++) {
Jordan Crouseb5c80482012-10-03 09:38:41 -06002250
2251 if (hang_detect_regs[i] == 0)
2252 continue;
2253
Tarun Karra3335f142012-06-19 14:11:48 -07002254 adreno_regread(device, hang_detect_regs[i],
2255 &curr_reg_val[i]);
2256 if (curr_reg_val[i] != prev_reg_val[i]) {
2257 prev_reg_val[i] = curr_reg_val[i];
2258 hang_detected = 0;
2259 }
2260 }
2261
2262 return hang_detected;
2263}
2264
Jordan Crouse92446a62012-11-15 11:00:06 -07002265/**
2266 * adreno_handle_hang - Process a hang detected in adreno_waittimestamp
2267 * @device - pointer to a KGSL device structure
2268 * @context - pointer to the active KGSL context
2269 * @timestamp - the timestamp that the process was waiting for
2270 *
2271 * Process a possible GPU hang and try to recover from it cleanly
2272 */
2273static int adreno_handle_hang(struct kgsl_device *device,
2274 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002275{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002276 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002277 unsigned int context_id = _get_context_id(context);
Jordan Crouse92446a62012-11-15 11:00:06 -07002278 unsigned int ts_issued;
Tarun Karra3335f142012-06-19 14:11:48 -07002279
Jordan Crouse92446a62012-11-15 11:00:06 -07002280 /* Do one last check to see if we somehow made it through */
2281 if (kgsl_check_timestamp(device, context, timestamp))
2282 return 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002283
2284 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002285
Jeremy Gebben63904832012-02-07 16:10:55 -07002286 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002287 "Device hang detected while waiting for timestamp: "
2288 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2289 "wptr: 0x%x\n",
2290 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002291 adreno_dev->ringbuffer.wptr);
Jordan Crouse92446a62012-11-15 11:00:06 -07002292
2293 /* Return 0 after a successful recovery */
2294 if (!adreno_dump_and_recover(device))
2295 return 0;
2296
2297 return -ETIMEDOUT;
2298}
2299
2300static int _check_pending_timestamp(struct kgsl_device *device,
2301 struct kgsl_context *context, unsigned int timestamp)
2302{
2303 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2304 unsigned int context_id = _get_context_id(context);
2305 unsigned int ts_issued;
2306
2307 if (context_id == KGSL_CONTEXT_INVALID)
2308 return -EINVAL;
2309
2310 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
2311
2312 if (timestamp_cmp(timestamp, ts_issued) <= 0)
2313 return 0;
2314
2315 if (context && !context->wait_on_invalid_ts) {
2316 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, last issued ts <%d:0x%x>\n",
2317 context_id, timestamp, context_id, ts_issued);
2318
2319 /* Only print this message once */
2320 context->wait_on_invalid_ts = true;
Jeremy Gebben63904832012-02-07 16:10:55 -07002321 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002322
2323 return -EINVAL;
2324}
2325
2326/**
2327 * adreno_waittimestamp - sleep while waiting for the specified timestamp
2328 * @device - pointer to a KGSL device structure
2329 * @context - pointer to the active kgsl context
2330 * @timestamp - GPU timestamp to wait for
2331 * @msecs - amount of time to wait (in milliseconds)
2332 *
2333 * Wait 'msecs' milliseconds for the specified timestamp to expire. Wake up
2334 * every KGSL_TIMEOUT_PART milliseconds to check for a device hang and process
2335 * one if it happened. Otherwise, spend most of our time in an interruptible
2336 * wait for the timestamp interrupt to be processed. This function must be
2337 * called with the mutex already held.
2338 */
2339static int adreno_waittimestamp(struct kgsl_device *device,
2340 struct kgsl_context *context,
2341 unsigned int timestamp,
2342 unsigned int msecs)
2343{
2344 static unsigned int io_cnt;
2345 struct adreno_context *adreno_ctx = context ? context->devctxt : NULL;
2346 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
2347 unsigned int context_id = _get_context_id(context);
2348 unsigned int prev_reg_val[hang_detect_regs_count];
2349 unsigned int time_elapsed = 0;
2350 unsigned int wait;
2351 int ts_compare = 1;
2352 int io, ret = -ETIMEDOUT;
2353
2354 /* Get out early if the context has already been destroyed */
2355
2356 if (context_id == KGSL_CONTEXT_INVALID) {
2357 KGSL_DRV_WARN(device, "context was detached");
2358 return -EINVAL;
2359 }
2360
2361 /*
2362 * Check to see if the requested timestamp is "newer" then the last
2363 * timestamp issued. If it is complain once and return error. Only
2364 * print the message once per context so that badly behaving
2365 * applications don't spam the logs
2366 */
2367
2368 if (adreno_ctx && !(adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS)) {
2369 if (_check_pending_timestamp(device, context, timestamp))
2370 return -EINVAL;
2371
2372 /* Reset the invalid timestamp flag on a valid wait */
2373 context->wait_on_invalid_ts = false;
2374 }
2375
2376
2377 /* Clear the registers used for hang detection */
2378 memset(prev_reg_val, 0, sizeof(prev_reg_val));
2379
2380 /*
2381 * On the first time through the loop only wait 100ms.
2382 * this gives enough time for the engine to start moving and oddly
2383 * provides better hang detection results than just going the full
2384 * KGSL_TIMEOUT_PART right off the bat. The exception to this rule
2385 * is if msecs happens to be < 100ms then just use the full timeout
2386 */
2387
2388 wait = 100;
2389
2390 do {
2391 long status;
2392
Jordan Crouse92446a62012-11-15 11:00:06 -07002393 /*
2394 * if the timestamp happens while we're not
2395 * waiting, there's a chance that an interrupt
2396 * will not be generated and thus the timestamp
2397 * work needs to be queued.
2398 */
2399
2400 if (kgsl_check_timestamp(device, context, timestamp)) {
2401 queue_work(device->work_queue, &device->ts_expired_ws);
2402 ret = 0;
2403 break;
2404 }
2405
2406 /* Check to see if the GPU is hung */
2407 if (adreno_hang_detect(device, prev_reg_val)) {
2408 ret = adreno_handle_hang(device, context, timestamp);
2409 break;
2410 }
2411
2412 /*
2413 * For proper power accounting sometimes we need to call
2414 * io_wait_interruptible_timeout and sometimes we need to call
2415 * plain old wait_interruptible_timeout. We call the regular
2416 * timeout N times out of 100, where N is a number specified by
2417 * the current power level
2418 */
2419
2420 io_cnt = (io_cnt + 1) % 100;
2421 io = (io_cnt < pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2422 ? 0 : 1;
2423
2424 mutex_unlock(&device->mutex);
2425
2426 /* Wait for a timestamp event */
2427 status = kgsl_wait_event_interruptible_timeout(
2428 device->wait_queue,
Carter Cooper740f6742013-01-03 16:19:23 -07002429 adreno_check_interrupt_timestamp(device, context,
Jordan Crouse92446a62012-11-15 11:00:06 -07002430 timestamp), msecs_to_jiffies(wait), io);
2431
2432 mutex_lock(&device->mutex);
2433
2434 /*
2435 * If status is non zero then either the condition was satisfied
2436 * or there was an error. In either event, this is the end of
2437 * the line for us
2438 */
2439
2440 if (status != 0) {
2441 ret = (status > 0) ? 0 : (int) status;
2442 break;
2443 }
Jordan Crouse92446a62012-11-15 11:00:06 -07002444 time_elapsed += wait;
2445
2446 /* If user specified timestamps are being used, wait at least
2447 * KGSL_SYNCOBJ_SERVER_TIMEOUT msecs for the user driver to
2448 * issue a IB for a timestamp before checking to see if the
2449 * current timestamp we are waiting for is valid or not
2450 */
2451
2452 if (ts_compare && (adreno_ctx &&
2453 (adreno_ctx->flags & CTXT_FLAGS_USER_GENERATED_TS))) {
2454 if (time_elapsed > KGSL_SYNCOBJ_SERVER_TIMEOUT) {
2455 ret = _check_pending_timestamp(device, context,
2456 timestamp);
2457 if (ret)
2458 break;
2459
2460 /* Don't do this check again */
2461 ts_compare = 0;
2462
2463 /*
2464 * Reset the invalid timestamp flag on a valid
2465 * wait
2466 */
2467 context->wait_on_invalid_ts = false;
2468 }
2469 }
2470
2471 /*
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07002472 * We want to wait the floor of KGSL_TIMEOUT_PART
2473 * and (msecs - time_elapsed).
Jordan Crouse92446a62012-11-15 11:00:06 -07002474 */
Harsh Vardhan Dwivedi5e57bd32013-01-27 22:54:23 -07002475
2476 if (KGSL_TIMEOUT_PART < (msecs - time_elapsed))
2477 wait = KGSL_TIMEOUT_PART;
2478 else
2479 wait = (msecs - time_elapsed);
Jordan Crouse92446a62012-11-15 11:00:06 -07002480
2481 } while (!msecs || time_elapsed < msecs);
2482
2483 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002484}
2485
2486static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002487 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002488{
2489 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002490 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002491
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002492 /*
2493 * If the context ID is invalid, we are in a race with
2494 * the context being destroyed by userspace so bail.
2495 */
2496 if (context_id == KGSL_CONTEXT_INVALID) {
2497 KGSL_DRV_WARN(device, "context was detached");
2498 return timestamp;
2499 }
Jordan Crousec659f382012-04-16 11:10:41 -06002500 switch (type) {
2501 case KGSL_TIMESTAMP_QUEUED: {
2502 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2503 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2504
2505 timestamp = rb->timestamp[context_id];
2506 break;
2507 }
2508 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002509 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002510 break;
2511 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002512 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002513 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2514 break;
2515 }
2516
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002517 rmb();
2518
2519 return timestamp;
2520}
2521
2522static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2523 unsigned int cmd, void *data)
2524{
2525 int result = 0;
2526 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2527 struct kgsl_context *context;
2528
2529 switch (cmd) {
2530 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2531 binbase = data;
2532
2533 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2534 if (context) {
2535 adreno_drawctxt_set_bin_base_offset(
2536 dev_priv->device, context, binbase->offset);
2537 } else {
2538 result = -EINVAL;
2539 KGSL_DRV_ERR(dev_priv->device,
2540 "invalid drawctxt drawctxt_id %d "
2541 "device_id=%d\n",
2542 binbase->drawctxt_id, dev_priv->device->id);
2543 }
2544 break;
2545
2546 default:
2547 KGSL_DRV_INFO(dev_priv->device,
2548 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002549 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002550 break;
2551 }
2552 return result;
2553
2554}
2555
2556static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2557{
2558 gpu_freq /= 1000000;
2559 return ticks / gpu_freq;
2560}
2561
2562static void adreno_power_stats(struct kgsl_device *device,
2563 struct kgsl_power_stats *stats)
2564{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002565 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002566 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002567 unsigned int cycles;
2568
2569 /* Get the busy cycles counted since the counter was last reset */
2570 /* Calling this function also resets and restarts the counter */
2571
2572 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002573
2574 /* In order to calculate idle you have to have run the algorithm *
2575 * at least once to get a start time. */
2576 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002577 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002578 stats->total_time = tmp - pwr->time;
2579 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002580 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002581 pwrlevels[device->pwrctrl.active_pwrlevel].
2582 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002583 } else {
2584 stats->total_time = 0;
2585 stats->busy_time = 0;
2586 pwr->time = ktime_to_us(ktime_get());
2587 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002588}
2589
2590void adreno_irqctrl(struct kgsl_device *device, int state)
2591{
Jordan Crousea78c9172011-07-11 13:14:09 -06002592 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2593 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002594}
2595
Jordan Croused6535882012-06-20 08:22:16 -06002596static unsigned int adreno_gpuid(struct kgsl_device *device,
2597 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002598{
2599 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2600
Jordan Croused6535882012-06-20 08:22:16 -06002601 /* Some applications need to know the chip ID too, so pass
2602 * that as a parameter */
2603
2604 if (chipid != NULL)
2605 *chipid = adreno_dev->chip_id;
2606
Jordan Crousea0758f22011-12-07 11:19:22 -07002607 /* Standard KGSL gpuid format:
2608 * top word is 0x0002 for 2D or 0x0003 for 3D
2609 * Bottom word is core specific identifer
2610 */
2611
2612 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2613}
2614
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002615static const struct kgsl_functable adreno_functable = {
2616 /* Mandatory functions */
2617 .regread = adreno_regread,
2618 .regwrite = adreno_regwrite,
2619 .idle = adreno_idle,
2620 .isidle = adreno_isidle,
2621 .suspend_context = adreno_suspend_context,
2622 .start = adreno_start,
2623 .stop = adreno_stop,
2624 .getproperty = adreno_getproperty,
2625 .waittimestamp = adreno_waittimestamp,
2626 .readtimestamp = adreno_readtimestamp,
2627 .issueibcmds = adreno_ringbuffer_issueibcmds,
2628 .ioctl = adreno_ioctl,
2629 .setup_pt = adreno_setup_pt,
2630 .cleanup_pt = adreno_cleanup_pt,
2631 .power_stats = adreno_power_stats,
2632 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002633 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002634 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002635 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002636 /* Optional functions */
2637 .setstate = adreno_setstate,
2638 .drawctxt_create = adreno_drawctxt_create,
2639 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002640 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002641 .postmortem_dump = adreno_dump,
Jordan Crouse313faf62012-11-20 15:12:28 -07002642 .next_event = adreno_next_event,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002643};
2644
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002645static struct platform_driver adreno_platform_driver = {
2646 .probe = adreno_probe,
2647 .remove = __devexit_p(adreno_remove),
2648 .suspend = kgsl_suspend_driver,
2649 .resume = kgsl_resume_driver,
2650 .id_table = adreno_id_table,
2651 .driver = {
2652 .owner = THIS_MODULE,
2653 .name = DEVICE_3D_NAME,
2654 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002655 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002656 }
2657};
2658
2659static int __init kgsl_3d_init(void)
2660{
2661 return platform_driver_register(&adreno_platform_driver);
2662}
2663
2664static void __exit kgsl_3d_exit(void)
2665{
2666 platform_driver_unregister(&adreno_platform_driver);
2667}
2668
2669module_init(kgsl_3d_init);
2670module_exit(kgsl_3d_exit);
2671
2672MODULE_DESCRIPTION("3D Graphics driver");
2673MODULE_VERSION("1.2");
2674MODULE_LICENSE("GPL v2");
2675MODULE_ALIAS("platform:kgsl_3d");