blob: f3944220514f2e178d1bfa1202119346eceaf9ff [file] [log] [blame]
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -07001/* Copyright (c) 2002,2007-2012, 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,
131 0
Tarun Karra3335f142012-06-19 14:11:48 -0700132};
133
134const unsigned int hang_detect_regs_count = ARRAY_SIZE(hang_detect_regs);
Jordan Crouse95b33272011-11-11 14:50:12 -0700135
Jordan Crouse505df9c2011-07-28 08:37:59 -0600136/*
137 * This is the master list of all GPU cores that are supported by this
138 * driver.
139 */
140
141#define ANY_ID (~0)
142
143static const struct {
144 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600145 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600146 const char *pm4fw;
147 const char *pfpfw;
148 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700149 unsigned int istore_size;
150 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700151 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530152 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600153} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600154 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700155 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530156 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530157 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
158 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530159 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600160 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700161 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530162 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600163 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700164 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530165 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600166 /*
167 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
168 * a hardware problem.
169 */
170 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700171 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530172 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700173 { ADRENO_REV_A225, 2, 2, 0, 6,
174 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530175 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600176 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700177 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530178 1536, 768, 3, SZ_512K },
179 /* A3XX doesn't use the pix_shader_start */
Sudhakara Rao Tentue13766d2012-06-12 06:00:26 +0530180 { ADRENO_REV_A305, 3, 0, 5, ANY_ID,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530181 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
182 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700183 /* A3XX doesn't use the pix_shader_start */
Carter Cooper95f7f792012-08-19 13:40:34 -0600184 { ADRENO_REV_A320, 3, 2, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700185 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530186 512, 0, 2, SZ_512K },
liu zhongfd42e622012-05-01 19:18:30 -0700187 { ADRENO_REV_A330, 3, 3, 0, 0,
188 "a330_pm4.fw", "a330_pfp.fw", &adreno_a3xx_gpudev,
189 512, 0, 2, SZ_1M },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600190};
191
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600192static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193{
Jordan Crousea78c9172011-07-11 13:14:09 -0600194 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600195 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196
Jordan Crousea78c9172011-07-11 13:14:09 -0600197 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198
199 if (device->requested_state == KGSL_STATE_NONE) {
200 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700201 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 queue_work(device->work_queue, &device->idle_check_ws);
203 } else if (device->pwrscale.policy != NULL) {
204 queue_work(device->work_queue, &device->idle_check_ws);
205 }
206 }
207
208 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800209 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 jiffies + device->pwrctrl.interval_timeout);
211 return result;
212}
213
Jordan Crouse9f739212011-07-28 08:37:57 -0600214static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 struct kgsl_pagetable *pagetable)
216{
217 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
218 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
219
220 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
221
222 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
223
224 kgsl_mmu_unmap(pagetable, &device->memstore);
225
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600226 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227}
228
229static int adreno_setup_pt(struct kgsl_device *device,
230 struct kgsl_pagetable *pagetable)
231{
232 int result = 0;
233 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
234 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
235
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
237 GSL_PT_PAGE_RV);
238 if (result)
239 goto error;
240
241 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
242 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
243 if (result)
244 goto unmap_buffer_desc;
245
246 result = kgsl_mmu_map_global(pagetable, &device->memstore,
247 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
248 if (result)
249 goto unmap_memptrs_desc;
250
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600251 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
253 if (result)
254 goto unmap_memstore_desc;
255
256 return result;
257
258unmap_memstore_desc:
259 kgsl_mmu_unmap(pagetable, &device->memstore);
260
261unmap_memptrs_desc:
262 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
263
264unmap_buffer_desc:
265 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
266
267error:
268 return result;
269}
270
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600271static void adreno_iommu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600272 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600273 uint32_t flags)
274{
275 unsigned int pt_val, reg_pt_val;
276 unsigned int link[200];
277 unsigned int *cmds = &link[0];
278 int sizedwords = 0;
279 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600280 int num_iommu_units, i;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600281 struct kgsl_context *context;
282 struct adreno_context *adreno_ctx = NULL;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600283
284 if (!adreno_dev->drawctxt_active)
285 return kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700286 num_iommu_units = kgsl_mmu_get_num_iommu_units(&device->mmu);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600287
288 context = idr_find(&device->context_idr, context_id);
289 adreno_ctx = context->devctxt;
290
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600291 if (kgsl_mmu_enable_clk(&device->mmu,
292 KGSL_IOMMU_CONTEXT_USER))
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700293 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600294
Shubhraprakash Das939c0d42012-06-15 11:40:48 -0600295 cmds += __adreno_add_idle_indirect_cmds(cmds,
296 device->mmu.setstate_memory.gpuaddr +
297 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
298
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600299 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600300 cmds += adreno_add_change_mh_phys_limit_cmds(cmds, 0xFFFFF000,
301 device->mmu.setstate_memory.gpuaddr +
302 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
303 else
304 cmds += adreno_add_bank_change_cmds(cmds,
305 KGSL_IOMMU_CONTEXT_USER,
306 device->mmu.setstate_memory.gpuaddr +
307 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
308
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700309 pt_val = kgsl_mmu_get_pt_base_addr(&device->mmu,
310 device->mmu.hwpagetable);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600311 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600312 /*
313 * We need to perfrom the following operations for all
314 * IOMMU units
315 */
316 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700317 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
318 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600319 /*
320 * Set address of the new pagetable by writng to IOMMU
321 * TTBR0 register
322 */
323 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700324 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
325 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600326 *cmds++ = reg_pt_val;
327 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
328 *cmds++ = 0x00000000;
329
330 /*
331 * Read back the ttbr0 register as a barrier to ensure
332 * above writes have completed
333 */
334 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700335 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
336 KGSL_IOMMU_CONTEXT_USER, KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600337 reg_pt_val,
338 device->mmu.setstate_memory.gpuaddr +
339 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600340 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600341 }
342 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
343 /*
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700344 * tlb flush
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600345 */
346 for (i = 0; i < num_iommu_units; i++) {
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700347 reg_pt_val = (pt_val + kgsl_mmu_get_pt_lsb(&device->mmu,
348 i, KGSL_IOMMU_CONTEXT_USER));
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700349
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600350 *cmds++ = cp_type3_packet(CP_MEM_WRITE, 2);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700351 *cmds++ = kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
352 KGSL_IOMMU_CONTEXT_USER,
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700353 KGSL_IOMMU_CTX_TLBIALL);
354 *cmds++ = 1;
Shubhraprakash Dasbe397282012-07-09 10:25:01 -0600355
356 cmds += __adreno_add_idle_indirect_cmds(cmds,
357 device->mmu.setstate_memory.gpuaddr +
358 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
359
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600360 cmds += adreno_add_read_cmds(device, cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700361 kgsl_mmu_get_reg_gpuaddr(&device->mmu, i,
362 KGSL_IOMMU_CONTEXT_USER,
363 KGSL_IOMMU_CTX_TTBR0),
Shubhraprakash Das8649fa52012-07-26 15:49:46 -0700364 reg_pt_val,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600365 device->mmu.setstate_memory.gpuaddr +
366 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
367 }
368 }
369
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600370 if (cpu_is_msm8960())
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600371 cmds += adreno_add_change_mh_phys_limit_cmds(cmds,
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700372 kgsl_mmu_get_reg_gpuaddr(&device->mmu, 0,
373 0, KGSL_IOMMU_GLOBAL_BASE),
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600374 device->mmu.setstate_memory.gpuaddr +
375 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
376 else
377 cmds += adreno_add_bank_change_cmds(cmds,
378 KGSL_IOMMU_CONTEXT_PRIV,
379 device->mmu.setstate_memory.gpuaddr +
380 KGSL_IOMMU_SETSTATE_NOP_OFFSET);
381
382 sizedwords += (cmds - &link[0]);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600383 if (sizedwords) {
Shubhraprakash Dasaef19842012-09-10 16:01:43 -0700384 /* invalidate all base pointers */
385 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
386 *cmds++ = 0x7fff;
387 sizedwords += 2;
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600388 /*
389 * add an interrupt at the end of commands so that the smmu
390 * disable clock off function will get called
391 */
392 *cmds++ = cp_type3_packet(CP_INTERRUPT, 1);
393 *cmds++ = CP_INT_CNTL__RB_INT_MASK;
394 sizedwords += 2;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600395 /* This returns the per context timestamp but we need to
396 * use the global timestamp for iommu clock disablement */
397 adreno_ringbuffer_issuecmds(device, adreno_ctx,
398 KGSL_CMD_FLAGS_PMODE,
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600399 &link[0], sizedwords);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600400 kgsl_mmu_disable_clk_on_ts(&device->mmu,
401 adreno_dev->ringbuffer.timestamp[KGSL_MEMSTORE_GLOBAL], true);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600402 }
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600403}
404
405static void adreno_gpummu_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600406 unsigned int context_id,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600407 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408{
409 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
410 unsigned int link[32];
411 unsigned int *cmds = &link[0];
412 int sizedwords = 0;
413 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600414 struct kgsl_context *context;
415 struct adreno_context *adreno_ctx = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600417 /*
Rajesh Kemisetti22a06d12012-06-29 20:21:31 +0530418 * Fix target freeze issue by adding TLB flush for each submit
419 * on A20X based targets.
420 */
421 if (adreno_is_a20x(adreno_dev))
422 flags |= KGSL_MMUFLAGS_TLBFLUSH;
423 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600424 * If possible, then set the state via the command stream to avoid
425 * a CPU idle. Otherwise, use the default setstate which uses register
426 * writes For CFF dump we must idle and use the registers so that it is
427 * easier to filter out the mmu accesses from the dump
428 */
429 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600430 context = idr_find(&device->context_idr, context_id);
431 adreno_ctx = context->devctxt;
432
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
434 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600435 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436 *cmds++ = 0x00000000;
437
438 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600439 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700440 *cmds++ = kgsl_mmu_get_pt_base_addr(&device->mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600441 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442 sizedwords += 4;
443 }
444
445 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
446 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600447 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448 1);
449 *cmds++ = 0x00000000;
450 sizedwords += 2;
451 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600452 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 *cmds++ = mh_mmu_invalidate;
454 sizedwords += 2;
455 }
456
457 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600458 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 /* HW workaround: to resolve MMU page fault interrupts
460 * caused by the VGT.It prevents the CP PFP from filling
461 * the VGT DMA request fifo too early,thereby ensuring
462 * that the VGT will not fetch vertex/bin data until
463 * after the page table base register has been updated.
464 *
465 * Two null DRAW_INDX_BIN packets are inserted right
466 * after the page table base update, followed by a
467 * wait for idle. The null packets will fill up the
468 * VGT DMA request fifo and prevent any further
469 * vertex/bin updates from occurring until the wait
470 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600471 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472 *cmds++ = (0x4 << 16) |
473 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
474 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600475 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600476 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600477 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 *cmds++ = 0; /* viz query info */
479 *cmds++ = 0x0003C004; /* draw indicator */
480 *cmds++ = 0; /* bin base */
481 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600482 *cmds++ =
483 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600485 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 *cmds++ = 0; /* viz query info */
487 *cmds++ = 0x0003C004; /* draw indicator */
488 *cmds++ = 0; /* bin base */
489 *cmds++ = 3; /* bin size */
490 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600491 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600493 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494 *cmds++ = 0x00000000;
495 sizedwords += 21;
496 }
497
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600498
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600500 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501 *cmds++ = 0x7fff; /* invalidate all base pointers */
502 sizedwords += 2;
503 }
504
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600505 adreno_ringbuffer_issuecmds(device, adreno_ctx,
506 KGSL_CMD_FLAGS_PMODE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600508 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600509 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600510 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511}
512
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600513static void adreno_setstate(struct kgsl_device *device,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600514 unsigned int context_id,
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600515 uint32_t flags)
516{
517 /* call the mmu specific handler */
518 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600519 return adreno_gpummu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600520 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600521 return adreno_iommu_setstate(device, context_id, flags);
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600522}
523
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700525a3xx_getchipid(struct kgsl_device *device)
526{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600527 struct kgsl_device_platform_data *pdata =
528 kgsl_device_get_drvdata(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700529
Jordan Crouse54154c62012-03-27 16:33:26 -0600530 /*
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600531 * All current A3XX chipids are detected at the SOC level. Leave this
532 * function here to support any future GPUs that have working
533 * chip ID registers
Jordan Crouse54154c62012-03-27 16:33:26 -0600534 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700535
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600536 return pdata->chipid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700537}
538
539static unsigned int
540a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541{
542 unsigned int chipid = 0;
543 unsigned int coreid, majorid, minorid, patchid, revid;
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600544 struct kgsl_device_platform_data *pdata =
545 kgsl_device_get_drvdata(device);
546
547 /* If the chip id is set at the platform level, then just use that */
548
549 if (pdata->chipid != 0)
550 return pdata->chipid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551
552 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
553 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
554 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
555
556 /*
557 * adreno 22x gpus are indicated by coreid 2,
558 * but REG_RBBM_PERIPHID1 always contains 0 for this field
559 */
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600560 if (cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 chipid = 2 << 24;
562 else
563 chipid = (coreid & 0xF) << 24;
564
565 chipid |= ((majorid >> 4) & 0xF) << 16;
566
567 minorid = ((revid >> 0) & 0xFF);
568
569 patchid = ((revid >> 16) & 0xFF);
570
571 /* 8x50 returns 0 for patch release, but it should be 1 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530572 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 if (cpu_is_qsd8x50())
574 patchid = 1;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530575 else if (cpu_is_msm8625() && minorid == 0)
576 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577
578 chipid |= (minorid << 8) | patchid;
579
580 return chipid;
581}
582
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700583static unsigned int
584adreno_getchipid(struct kgsl_device *device)
585{
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600586 struct kgsl_device_platform_data *pdata =
587 kgsl_device_get_drvdata(device);
588
589 /*
590 * All A3XX chipsets will have pdata set, so assume !pdata->chipid is
591 * an A2XX processor
592 */
593
594 if (pdata->chipid == 0 || ADRENO_CHIPID_MAJOR(pdata->chipid) == 2)
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700595 return a2xx_getchipid(device);
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600596 else
597 return a3xx_getchipid(device);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700598}
599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600static inline bool _rev_match(unsigned int id, unsigned int entry)
601{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600602 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604
605static void
606adreno_identify_gpu(struct adreno_device *adreno_dev)
607{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600608 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609
610 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
611
Jordan Crouse4815e9f2012-07-09 15:36:37 -0600612 core = ADRENO_CHIPID_CORE(adreno_dev->chip_id);
613 major = ADRENO_CHIPID_MAJOR(adreno_dev->chip_id);
614 minor = ADRENO_CHIPID_MINOR(adreno_dev->chip_id);
615 patchid = ADRENO_CHIPID_PATCH(adreno_dev->chip_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616
Jordan Crouse505df9c2011-07-28 08:37:59 -0600617 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
618 if (core == adreno_gpulist[i].core &&
619 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600620 _rev_match(minor, adreno_gpulist[i].minor) &&
621 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 }
624
Jordan Crouse505df9c2011-07-28 08:37:59 -0600625 if (i == ARRAY_SIZE(adreno_gpulist)) {
626 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
627 return;
628 }
629
630 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
631 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
632 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
633 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700634 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
635 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700636 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600637 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638}
639
Lokesh Batra805e1e12012-08-03 08:34:06 -0600640static struct platform_device_id adreno_id_table[] = {
641 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
642 {},
643};
644
645MODULE_DEVICE_TABLE(platform, adreno_id_table);
646
647static struct of_device_id adreno_match_table[] = {
648 { .compatible = "qcom,kgsl-3d0", },
649 {}
650};
651
652static inline int adreno_of_read_property(struct device_node *node,
653 const char *prop, unsigned int *ptr)
654{
655 int ret = of_property_read_u32(node, prop, ptr);
656 if (ret)
657 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
658 return ret;
659}
660
661static struct device_node *adreno_of_find_subnode(struct device_node *parent,
662 const char *name)
663{
664 struct device_node *child;
665
666 for_each_child_of_node(parent, child) {
667 if (of_device_is_compatible(child, name))
668 return child;
669 }
670
671 return NULL;
672}
673
674static int adreno_of_get_pwrlevels(struct device_node *parent,
675 struct kgsl_device_platform_data *pdata)
676{
677 struct device_node *node, *child;
678 int ret = -EINVAL;
679
680 node = adreno_of_find_subnode(parent, "qcom,gpu-pwrlevels");
681
682 if (node == NULL) {
683 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
684 return -EINVAL;
685 }
686
687 pdata->num_levels = 0;
688
689 for_each_child_of_node(node, child) {
690 unsigned int index;
691 struct kgsl_pwrlevel *level;
692
693 if (adreno_of_read_property(child, "reg", &index))
694 goto done;
695
696 if (index >= KGSL_MAX_PWRLEVELS) {
697 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
698 index);
699 continue;
700 }
701
702 if (index >= pdata->num_levels)
703 pdata->num_levels = index + 1;
704
705 level = &pdata->pwrlevel[index];
706
707 if (adreno_of_read_property(child, "qcom,gpu-freq",
708 &level->gpu_freq))
709 goto done;
710
711 if (adreno_of_read_property(child, "qcom,bus-freq",
712 &level->bus_freq))
713 goto done;
714
715 if (adreno_of_read_property(child, "qcom,io-fraction",
716 &level->io_fraction))
717 level->io_fraction = 0;
718 }
719
720 if (adreno_of_read_property(parent, "qcom,initial-pwrlevel",
721 &pdata->init_level))
722 pdata->init_level = 1;
723
724 if (pdata->init_level < 0 || pdata->init_level > pdata->num_levels) {
725 KGSL_CORE_ERR("Initial power level out of range\n");
726 pdata->init_level = 1;
727 }
728
729 ret = 0;
730done:
731 return ret;
732
733}
734static void adreno_of_free_bus_scale_info(struct msm_bus_scale_pdata *pdata)
735{
736 int i;
737
738 if (pdata == NULL)
739 return;
740
741 for (i = 0; pdata->usecase && i < pdata->num_usecases; i++)
742 kfree(pdata->usecase[i].vectors);
743
744 kfree(pdata->usecase);
745 kfree(pdata);
746}
747
748struct msm_bus_scale_pdata *adreno_of_get_bus_scale(struct device_node *node)
749{
750 static int bus_vectors_src[3] = {MSM_BUS_MASTER_GRAPHICS_3D,
751 MSM_BUS_MASTER_GRAPHICS_3D_PORT1, MSM_BUS_MASTER_V_OCMEM_GFX3D};
752 static int bus_vectors_dst[2] = {MSM_BUS_SLAVE_EBI_CH0,
753 MSM_BUS_SLAVE_OCMEM};
754 const unsigned int *vectors;
755 struct msm_bus_scale_pdata *pdata;
756 int i, j, len, num_paths;
757 int ret = -EINVAL;
758
759 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
760
761 if (!pdata) {
762 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
763 return ERR_PTR(-ENOMEM);
764 }
765
766 if (adreno_of_read_property(node, "qcom,grp3d-num-bus-scale-usecases",
767 &pdata->num_usecases)) {
768 pdata->num_usecases = 0;
769 goto err;
770 }
771
772 pdata->usecase = kzalloc(pdata->num_usecases *
773 sizeof(struct msm_bus_paths), GFP_KERNEL);
774
775 if (pdata->usecase == NULL) {
776 KGSL_CORE_ERR("kzalloc (%d) failed\n",
777 pdata->num_usecases * sizeof(struct msm_bus_paths));
778 ret = -ENOMEM;
779 goto err;
780 }
781
782 if (adreno_of_read_property(node, "qcom,grp3d-num-vectors-per-usecase",
783 &num_paths))
784 goto err;
785
786 vectors = of_get_property(node, "qcom,grp3d-vectors", &len);
787
788 if (len != pdata->num_usecases * num_paths *
789 sizeof(struct msm_bus_vectors)) {
790 KGSL_CORE_ERR("Invalid size for the bus scale vectors\n");
791 goto err;
792 }
793
794 for (i = 0; i < pdata->num_usecases; i++) {
795 pdata->usecase[i].num_paths = num_paths;
796 pdata->usecase[i].vectors = kzalloc(num_paths *
797 sizeof(struct msm_bus_vectors),
798 GFP_KERNEL);
799 if (!pdata->usecase[i].vectors) {
800 KGSL_CORE_ERR("kzalloc(%d) failed\n",
801 num_paths * sizeof(struct msm_bus_vectors));
802 ret = -ENOMEM;
803 goto err;
804 }
805 for (j = 0; j < num_paths; j++) {
806 int index = (i * num_paths + j) * 4;
807 pdata->usecase[i].vectors[j].src =
808 bus_vectors_src[be32_to_cpu(vectors[index])];
809 pdata->usecase[i].vectors[j].dst =
810 bus_vectors_dst[
811 be32_to_cpu(vectors[index + 1])];
812 pdata->usecase[i].vectors[j].ab =
813 be32_to_cpu(vectors[index + 2]);
814 pdata->usecase[i].vectors[j].ib =
815 KGSL_CONVERT_TO_MBPS(
816 be32_to_cpu(vectors[index + 3]));
817 }
818 }
819
820 pdata->name = "grp3d";
821
822 return pdata;
823
824err:
825 adreno_of_free_bus_scale_info(pdata);
826
827 return ERR_PTR(ret);
828}
829
830static struct msm_dcvs_core_info *adreno_of_get_dcvs(struct device_node *parent)
831{
832 struct device_node *node, *child;
833 struct msm_dcvs_core_info *info = NULL;
834 int count = 0;
835 int ret = -EINVAL;
836
837 node = adreno_of_find_subnode(parent, "qcom,dcvs-core-info");
838 if (node == NULL)
839 return ERR_PTR(-EINVAL);
840
841 info = kzalloc(sizeof(*info), GFP_KERNEL);
842
843 if (info == NULL) {
844 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*info));
845 ret = -ENOMEM;
846 goto err;
847 }
848
849 for_each_child_of_node(node, child)
850 count++;
851
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700852 info->power_param.num_freq = count;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600853
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700854 info->freq_tbl = kzalloc(info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600855 sizeof(struct msm_dcvs_freq_entry),
856 GFP_KERNEL);
857
858 if (info->freq_tbl == NULL) {
859 KGSL_CORE_ERR("kzalloc(%d) failed\n",
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700860 info->power_param.num_freq *
Lokesh Batra805e1e12012-08-03 08:34:06 -0600861 sizeof(struct msm_dcvs_freq_entry));
862 ret = -ENOMEM;
863 goto err;
864 }
865
866 for_each_child_of_node(node, child) {
867 unsigned int index;
868
869 if (adreno_of_read_property(child, "reg", &index))
870 goto err;
871
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700872 if (index >= info->power_param.num_freq) {
Lokesh Batra805e1e12012-08-03 08:34:06 -0600873 KGSL_CORE_ERR("DCVS freq entry %d is out of range\n",
874 index);
875 continue;
876 }
877
878 if (adreno_of_read_property(child, "qcom,freq",
879 &info->freq_tbl[index].freq))
880 goto err;
881
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700882 if (adreno_of_read_property(child, "qcom,voltage",
883 &info->freq_tbl[index].voltage))
884 info->freq_tbl[index].voltage = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600885
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700886 if (adreno_of_read_property(child, "qcom,is_trans_level",
887 &info->freq_tbl[index].is_trans_level))
888 info->freq_tbl[index].is_trans_level = 0;
889
890 if (adreno_of_read_property(child, "qcom,active-energy-offset",
891 &info->freq_tbl[index].active_energy_offset))
892 info->freq_tbl[index].active_energy_offset = 0;
893
894 if (adreno_of_read_property(child, "qcom,leakage-energy-offset",
895 &info->freq_tbl[index].leakage_energy_offset))
896 info->freq_tbl[index].leakage_energy_offset = 0;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600897 }
898
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700899 if (adreno_of_read_property(node, "qcom,num-cores", &info->num_cores))
900 goto err;
901
902 info->sensors = kzalloc(info->num_cores *
903 sizeof(int),
904 GFP_KERNEL);
905
906 for (count = 0; count < info->num_cores; count++) {
907 if (adreno_of_read_property(node, "qcom,sensors",
908 &(info->sensors[count])))
909 goto err;
910 }
911
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700912 if (adreno_of_read_property(node, "qcom,core-core-type",
913 &info->core_param.core_type))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600914 goto err;
915
916 if (adreno_of_read_property(node, "qcom,algo-disable-pc-threshold",
917 &info->algo_param.disable_pc_threshold))
918 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700919 if (adreno_of_read_property(node, "qcom,algo-em-win-size-min-us",
920 &info->algo_param.em_win_size_min_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600921 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700922 if (adreno_of_read_property(node, "qcom,algo-em-win-size-max-us",
923 &info->algo_param.em_win_size_max_us))
Lokesh Batra805e1e12012-08-03 08:34:06 -0600924 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600925 if (adreno_of_read_property(node, "qcom,algo-em-max-util-pct",
926 &info->algo_param.em_max_util_pct))
927 goto err;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700928 if (adreno_of_read_property(node, "qcom,algo-group-id",
929 &info->algo_param.group_id))
930 goto err;
931 if (adreno_of_read_property(node, "qcom,algo-max-freq-chg-time-us",
932 &info->algo_param.max_freq_chg_time_us))
933 goto err;
934 if (adreno_of_read_property(node, "qcom,algo-slack-mode-dynamic",
935 &info->algo_param.slack_mode_dynamic))
936 goto err;
937 if (adreno_of_read_property(node, "qcom,algo-slack-weight-thresh-pct",
938 &info->algo_param.slack_weight_thresh_pct))
939 goto err;
940 if (adreno_of_read_property(node, "qcom,algo-slack-time-min-us",
941 &info->algo_param.slack_time_min_us))
942 goto err;
943 if (adreno_of_read_property(node, "qcom,algo-slack-time-max-us",
944 &info->algo_param.slack_time_max_us))
945 goto err;
946 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-min-us",
947 &info->algo_param.ss_win_size_min_us))
948 goto err;
949 if (adreno_of_read_property(node, "qcom,algo-ss-win-size-max-us",
950 &info->algo_param.ss_win_size_max_us))
951 goto err;
952 if (adreno_of_read_property(node, "qcom,algo-ss-util-pct",
953 &info->algo_param.ss_util_pct))
954 goto err;
Lokesh Batra805e1e12012-08-03 08:34:06 -0600955 if (adreno_of_read_property(node, "qcom,algo-ss-iobusy-conv",
956 &info->algo_param.ss_iobusy_conv))
957 goto err;
958
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700959 if (adreno_of_read_property(node, "qcom,energy-active-coeff-a",
960 &info->energy_coeffs.active_coeff_a))
961 goto err;
962 if (adreno_of_read_property(node, "qcom,energy-active-coeff-b",
963 &info->energy_coeffs.active_coeff_b))
964 goto err;
965 if (adreno_of_read_property(node, "qcom,energy-active-coeff-c",
966 &info->energy_coeffs.active_coeff_c))
967 goto err;
968 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-a",
969 &info->energy_coeffs.leakage_coeff_a))
970 goto err;
971 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-b",
972 &info->energy_coeffs.leakage_coeff_b))
973 goto err;
974 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-c",
975 &info->energy_coeffs.leakage_coeff_c))
976 goto err;
977 if (adreno_of_read_property(node, "qcom,energy-leakage-coeff-d",
978 &info->energy_coeffs.leakage_coeff_d))
979 goto err;
980
981 if (adreno_of_read_property(node, "qcom,power-current-temp",
982 &info->power_param.current_temp))
983 goto err;
984
Lokesh Batra805e1e12012-08-03 08:34:06 -0600985 return info;
986
987err:
988 if (info)
989 kfree(info->freq_tbl);
990
991 kfree(info);
992
993 return ERR_PTR(ret);
994}
995
996static int adreno_of_get_iommu(struct device_node *parent,
997 struct kgsl_device_platform_data *pdata)
998{
999 struct device_node *node, *child;
1000 struct kgsl_device_iommu_data *data = NULL;
1001 struct kgsl_iommu_ctx *ctxs = NULL;
1002 u32 reg_val[2];
1003 int ctx_index = 0;
1004
1005 node = of_parse_phandle(parent, "iommu", 0);
1006 if (node == NULL)
1007 return -EINVAL;
1008
1009 data = kzalloc(sizeof(*data), GFP_KERNEL);
1010 if (data == NULL) {
1011 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*data));
1012 goto err;
1013 }
1014
1015 if (of_property_read_u32_array(node, "reg", reg_val, 2))
1016 goto err;
1017
1018 data->physstart = reg_val[0];
1019 data->physend = data->physstart + reg_val[1] - 1;
1020
1021 data->iommu_ctx_count = 0;
1022
1023 for_each_child_of_node(node, child)
1024 data->iommu_ctx_count++;
1025
1026 ctxs = kzalloc(data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx),
1027 GFP_KERNEL);
1028
1029 if (ctxs == NULL) {
1030 KGSL_CORE_ERR("kzalloc(%d) failed\n",
1031 data->iommu_ctx_count * sizeof(struct kgsl_iommu_ctx));
1032 goto err;
1033 }
1034
1035 for_each_child_of_node(node, child) {
1036 int ret = of_property_read_string(child, "label",
1037 &ctxs[ctx_index].iommu_ctx_name);
1038
1039 if (ret) {
1040 KGSL_CORE_ERR("Unable to read KGSL IOMMU 'label'\n");
1041 goto err;
1042 }
1043
1044 if (adreno_of_read_property(child, "qcom,iommu-ctx-sids",
1045 &ctxs[ctx_index].ctx_id))
1046 goto err;
1047
1048 ctx_index++;
1049 }
1050
1051 data->iommu_ctxs = ctxs;
1052
1053 pdata->iommu_data = data;
1054 pdata->iommu_count = 1;
1055
1056 return 0;
1057
1058err:
1059 kfree(ctxs);
1060 kfree(data);
1061
1062 return -EINVAL;
1063}
1064
1065static int adreno_of_get_pdata(struct platform_device *pdev)
1066{
1067 struct kgsl_device_platform_data *pdata = NULL;
1068 struct kgsl_device *device;
1069 int ret = -EINVAL;
1070
1071 pdev->id_entry = adreno_id_table;
1072
1073 pdata = pdev->dev.platform_data;
1074 if (pdata)
1075 return 0;
1076
1077 if (of_property_read_string(pdev->dev.of_node, "label", &pdev->name)) {
1078 KGSL_CORE_ERR("Unable to read 'label'\n");
1079 goto err;
1080 }
1081
1082 if (adreno_of_read_property(pdev->dev.of_node, "qcom,id", &pdev->id))
1083 goto err;
1084
1085 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1086 if (pdata == NULL) {
1087 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*pdata));
1088 ret = -ENOMEM;
1089 goto err;
1090 }
1091
1092 if (adreno_of_read_property(pdev->dev.of_node, "qcom,chipid",
1093 &pdata->chipid))
1094 goto err;
1095
1096 /* pwrlevel Data */
1097 ret = adreno_of_get_pwrlevels(pdev->dev.of_node, pdata);
1098 if (ret)
1099 goto err;
1100
1101 /* Default value is 83, if not found in DT */
1102 if (adreno_of_read_property(pdev->dev.of_node, "qcom,idle-timeout",
1103 &pdata->idle_timeout))
1104 pdata->idle_timeout = 83;
1105
1106 if (adreno_of_read_property(pdev->dev.of_node, "qcom,nap-allowed",
1107 &pdata->nap_allowed))
1108 pdata->nap_allowed = 1;
1109
1110 if (adreno_of_read_property(pdev->dev.of_node, "qcom,clk-map",
1111 &pdata->clk_map))
1112 goto err;
1113
1114 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1115
1116 if (device->id != KGSL_DEVICE_3D0)
1117 goto err;
1118
1119 /* Bus Scale Data */
1120
1121 pdata->bus_scale_table = adreno_of_get_bus_scale(pdev->dev.of_node);
1122 if (IS_ERR_OR_NULL(pdata->bus_scale_table)) {
1123 ret = PTR_ERR(pdata->bus_scale_table);
1124 goto err;
1125 }
1126
1127 pdata->core_info = adreno_of_get_dcvs(pdev->dev.of_node);
1128 if (IS_ERR_OR_NULL(pdata->core_info)) {
1129 ret = PTR_ERR(pdata->core_info);
1130 goto err;
1131 }
1132
1133 ret = adreno_of_get_iommu(pdev->dev.of_node, pdata);
1134 if (ret)
1135 goto err;
1136
1137 pdev->dev.platform_data = pdata;
1138 return 0;
1139
1140err:
1141 if (pdata) {
1142 adreno_of_free_bus_scale_info(pdata->bus_scale_table);
1143 if (pdata->core_info)
1144 kfree(pdata->core_info->freq_tbl);
1145 kfree(pdata->core_info);
1146
1147 if (pdata->iommu_data)
1148 kfree(pdata->iommu_data->iommu_ctxs);
1149
1150 kfree(pdata->iommu_data);
1151 }
1152
1153 kfree(pdata);
1154
1155 return ret;
1156}
1157
liu zhong7dfa2a32012-04-27 19:11:01 -07001158#ifdef CONFIG_MSM_OCMEM
1159static int
1160adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1161{
Jordan Crousec0978202012-08-29 14:35:51 -06001162 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001163 return 0;
1164
1165 /* OCMEM is only needed once, do not support consective allocation */
1166 if (adreno_dev->ocmem_hdl != NULL)
1167 return 0;
1168
1169 adreno_dev->ocmem_hdl =
1170 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1171 if (adreno_dev->ocmem_hdl == NULL)
1172 return -ENOMEM;
1173
1174 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
liu zhong5af32d92012-08-29 14:36:36 -06001175 adreno_dev->ocmem_base = adreno_dev->ocmem_hdl->addr;
liu zhong7dfa2a32012-04-27 19:11:01 -07001176
1177 return 0;
1178}
1179
1180static void
1181adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1182{
Jordan Crousec0978202012-08-29 14:35:51 -06001183 if (!adreno_is_a330(adreno_dev))
liu zhong7dfa2a32012-04-27 19:11:01 -07001184 return;
1185
1186 if (adreno_dev->ocmem_hdl == NULL)
1187 return;
1188
1189 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1190 adreno_dev->ocmem_hdl = NULL;
1191}
1192#else
1193static int
1194adreno_ocmem_gmem_malloc(struct adreno_device *adreno_dev)
1195{
1196 return 0;
1197}
1198
1199static void
1200adreno_ocmem_gmem_free(struct adreno_device *adreno_dev)
1201{
1202}
1203#endif
1204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001205static int __devinit
1206adreno_probe(struct platform_device *pdev)
1207{
1208 struct kgsl_device *device;
1209 struct adreno_device *adreno_dev;
1210 int status = -EINVAL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001211 bool is_dt;
1212
1213 is_dt = of_match_device(adreno_match_table, &pdev->dev);
1214
1215 if (is_dt && pdev->dev.of_node) {
1216 status = adreno_of_get_pdata(pdev);
1217 if (status)
1218 goto error_return;
1219 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220
1221 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1222 adreno_dev = ADRENO_DEVICE(device);
1223 device->parentdev = &pdev->dev;
1224
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225 status = adreno_ringbuffer_init(device);
1226 if (status != 0)
1227 goto error;
1228
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001229 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230 if (status)
1231 goto error_close_rb;
1232
1233 adreno_debugfs_init(device);
1234
1235 kgsl_pwrscale_init(device);
1236 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
1237
1238 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
1239 return 0;
1240
1241error_close_rb:
1242 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1243error:
1244 device->parentdev = NULL;
Lokesh Batra805e1e12012-08-03 08:34:06 -06001245error_return:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246 return status;
1247}
1248
1249static int __devexit adreno_remove(struct platform_device *pdev)
1250{
1251 struct kgsl_device *device;
1252 struct adreno_device *adreno_dev;
1253
1254 device = (struct kgsl_device *)pdev->id_entry->driver_data;
1255 adreno_dev = ADRENO_DEVICE(device);
1256
1257 kgsl_pwrscale_detach_policy(device);
1258 kgsl_pwrscale_close(device);
1259
1260 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
1261 kgsl_device_platform_remove(device);
1262
1263 return 0;
1264}
1265
1266static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
1267{
1268 int status = -EINVAL;
1269 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001270
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001271 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1272 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001273
1274 /* Power up the device */
1275 kgsl_pwrctrl_enable(device);
1276
1277 /* Identify the specific GPU */
1278 adreno_identify_gpu(adreno_dev);
1279
Jordan Crouse505df9c2011-07-28 08:37:59 -06001280 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
1281 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
1282 adreno_dev->chip_id);
1283 goto error_clk_off;
1284 }
1285
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001286 /* Set up the MMU */
1287 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001288 /*
1289 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
1290 * on older gpus
1291 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001292 if (adreno_is_a20x(adreno_dev)) {
1293 device->mh.mh_intf_cfg1 = 0;
1294 device->mh.mh_intf_cfg2 = 0;
1295 }
1296
1297 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -06001298 }
1299
Tarun Karra3335f142012-06-19 14:11:48 -07001300 /* Assign correct RBBM status register to hang detect regs
1301 */
1302 hang_detect_regs[0] = adreno_dev->gpudev->reg_rbbm_status;
1303
Jordan Crouseb5c80482012-10-03 09:38:41 -06001304 /* Add A3XX specific registers for hang detection */
1305 if (adreno_is_a3xx(adreno_dev)) {
1306 hang_detect_regs[6] = A3XX_RBBM_PERFCTR_SP_7_LO;
1307 hang_detect_regs[7] = A3XX_RBBM_PERFCTR_SP_7_HI;
1308 }
1309
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001310 status = kgsl_mmu_start(device);
1311 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312 goto error_clk_off;
1313
liu zhong7dfa2a32012-04-27 19:11:01 -07001314 status = adreno_ocmem_gmem_malloc(adreno_dev);
1315 if (status) {
1316 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1317 goto error_mmu_off;
1318 }
1319
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001320 /* Start the GPU */
1321 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322
1323 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001324 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325
1326 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001327 if (status == 0) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001328 /* While recovery is on we do not want timer to
1329 * fire and attempt to change any device state */
1330 if (KGSL_STATE_DUMP_AND_RECOVER != device->state)
1331 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001332 return 0;
1333 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001334
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
liu zhong7dfa2a32012-04-27 19:11:01 -07001336
1337error_mmu_off:
Shubhraprakash Das79447952012-04-26 18:12:23 -06001338 kgsl_mmu_stop(&device->mmu);
liu zhong7dfa2a32012-04-27 19:11:01 -07001339
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001340error_clk_off:
1341 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001342
1343 return status;
1344}
1345
1346static int adreno_stop(struct kgsl_device *device)
1347{
1348 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1349
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350 adreno_dev->drawctxt_active = NULL;
1351
1352 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
1353
Shubhraprakash Das79447952012-04-26 18:12:23 -06001354 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001355
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001356 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +05301357 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -08001358 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -06001359
liu zhong7dfa2a32012-04-27 19:11:01 -07001360 adreno_ocmem_gmem_free(adreno_dev);
1361
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001362 /* Power down the device */
1363 kgsl_pwrctrl_disable(device);
1364
1365 return 0;
1366}
1367
Shubhraprakash Das29ed38e2012-06-06 01:43:55 -06001368static void adreno_mark_context_status(struct kgsl_device *device,
1369 int recovery_status)
1370{
1371 struct kgsl_context *context;
1372 int next = 0;
1373 /*
1374 * Set the reset status of all contexts to
1375 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
1376 * since thats the guilty party, if recovery failed then
1377 * mark all as guilty
1378 */
1379 while ((context = idr_get_next(&device->context_idr, &next))) {
1380 struct adreno_context *adreno_context = context->devctxt;
1381 if (recovery_status) {
1382 context->reset_status =
1383 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1384 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1385 } else if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
1386 context->reset_status) {
1387 if (adreno_context->flags & (CTXT_FLAGS_GPU_HANG ||
1388 CTXT_FLAGS_GPU_HANG_RECOVERED))
1389 context->reset_status =
1390 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1391 else
1392 context->reset_status =
1393 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1394 }
1395 next = next + 1;
1396 }
1397}
1398
Shubhraprakash Das5f085f42012-06-06 02:01:24 -06001399static void adreno_set_max_ts_for_bad_ctxs(struct kgsl_device *device)
1400{
1401 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1402 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1403 struct kgsl_context *context;
1404 struct adreno_context *temp_adreno_context;
1405 int next = 0;
1406
1407 while ((context = idr_get_next(&device->context_idr, &next))) {
1408 temp_adreno_context = context->devctxt;
1409 if (temp_adreno_context->flags & CTXT_FLAGS_GPU_HANG) {
1410 kgsl_sharedmem_writel(&device->memstore,
1411 KGSL_MEMSTORE_OFFSET(context->id,
1412 soptimestamp),
1413 rb->timestamp[context->id]);
1414 kgsl_sharedmem_writel(&device->memstore,
1415 KGSL_MEMSTORE_OFFSET(context->id,
1416 eoptimestamp),
1417 rb->timestamp[context->id]);
1418 }
1419 next = next + 1;
1420 }
1421}
1422
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001423static void adreno_destroy_recovery_data(struct adreno_recovery_data *rec_data)
1424{
1425 vfree(rec_data->rb_buffer);
1426 vfree(rec_data->bad_rb_buffer);
1427}
1428
1429static int adreno_setup_recovery_data(struct kgsl_device *device,
1430 struct adreno_recovery_data *rec_data)
1431{
1432 int ret = 0;
1433 unsigned int ib1_sz, ib2_sz;
1434 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1435 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1436
1437 memset(rec_data, 0, sizeof(*rec_data));
1438
1439 adreno_regread(device, REG_CP_IB1_BUFSZ, &ib1_sz);
1440 adreno_regread(device, REG_CP_IB2_BUFSZ, &ib2_sz);
1441 if (ib1_sz || ib2_sz)
1442 adreno_regread(device, REG_CP_IB1_BASE, &rec_data->ib1);
1443
1444 kgsl_sharedmem_readl(&device->memstore, &rec_data->context_id,
1445 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1446 current_context));
1447
1448 kgsl_sharedmem_readl(&device->memstore,
1449 &rec_data->global_eop,
1450 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1451 eoptimestamp));
1452
1453 rec_data->rb_buffer = vmalloc(rb->buffer_desc.size);
1454 if (!rec_data->rb_buffer) {
1455 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1456 rb->buffer_desc.size);
1457 return -ENOMEM;
1458 }
1459
1460 rec_data->bad_rb_buffer = vmalloc(rb->buffer_desc.size);
1461 if (!rec_data->bad_rb_buffer) {
1462 KGSL_MEM_ERR(device, "vmalloc(%d) failed\n",
1463 rb->buffer_desc.size);
1464 ret = -ENOMEM;
1465 goto done;
1466 }
1467
1468done:
1469 if (ret) {
1470 vfree(rec_data->rb_buffer);
1471 vfree(rec_data->bad_rb_buffer);
1472 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001473 return ret;
1474}
1475
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001476static int
1477_adreno_recover_hang(struct kgsl_device *device,
1478 struct adreno_recovery_data *rec_data,
1479 bool try_bad_commands)
1480{
1481 int ret;
1482 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1483 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1484 struct kgsl_context *context;
1485 struct adreno_context *adreno_context = NULL;
1486 struct adreno_context *last_active_ctx = adreno_dev->drawctxt_active;
1487
1488 context = idr_find(&device->context_idr, rec_data->context_id);
1489 if (context == NULL) {
1490 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
1491 rec_data->context_id);
1492 } else {
1493 adreno_context = context->devctxt;
1494 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
1495 }
1496
1497 /* Extract valid contents from rb which can still be executed after
1498 * hang */
1499 ret = adreno_ringbuffer_extract(rb, rec_data);
1500 if (ret)
1501 goto done;
1502
1503 /* restart device */
1504 ret = adreno_stop(device);
1505 if (ret) {
1506 KGSL_DRV_ERR(device, "Device stop failed in recovery\n");
1507 goto done;
1508 }
1509
1510 ret = adreno_start(device, true);
1511 if (ret) {
1512 KGSL_DRV_ERR(device, "Device start failed in recovery\n");
1513 goto done;
1514 }
1515
1516 if (context)
1517 kgsl_mmu_setstate(&device->mmu, adreno_context->pagetable,
1518 KGSL_MEMSTORE_GLOBAL);
1519
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001520 /* If iommu is used then we need to make sure that the iommu clocks
1521 * are on since there could be commands in pipeline that touch iommu */
1522 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1523 ret = kgsl_mmu_enable_clk(&device->mmu,
1524 KGSL_IOMMU_CONTEXT_USER);
1525 if (ret)
1526 goto done;
1527 }
1528
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001529 /* Do not try the bad caommands if recovery has failed bad commands
1530 * once already */
1531 if (!try_bad_commands)
1532 rec_data->bad_rb_size = 0;
1533
1534 if (rec_data->bad_rb_size) {
1535 int idle_ret;
1536 /* submit the bad and good context commands and wait for
1537 * them to pass */
1538 adreno_ringbuffer_restore(rb, rec_data->bad_rb_buffer,
1539 rec_data->bad_rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001540 idle_ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001541 if (idle_ret) {
1542 ret = adreno_stop(device);
1543 if (ret) {
1544 KGSL_DRV_ERR(device,
1545 "Device stop failed in recovery\n");
1546 goto done;
1547 }
1548 ret = adreno_start(device, true);
1549 if (ret) {
1550 KGSL_DRV_ERR(device,
1551 "Device start failed in recovery\n");
1552 goto done;
1553 }
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001554 if (context)
1555 kgsl_mmu_setstate(&device->mmu,
1556 adreno_context->pagetable,
1557 KGSL_MEMSTORE_GLOBAL);
1558
1559 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) {
1560 ret = kgsl_mmu_enable_clk(&device->mmu,
1561 KGSL_IOMMU_CONTEXT_USER);
1562 if (ret)
1563 goto done;
1564 }
1565
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001566 ret = idle_ret;
1567 KGSL_DRV_ERR(device,
1568 "Bad context commands hung in recovery\n");
1569 } else {
1570 KGSL_DRV_ERR(device,
1571 "Bad context commands succeeded in recovery\n");
1572 if (adreno_context)
1573 adreno_context->flags = (adreno_context->flags &
1574 ~CTXT_FLAGS_GPU_HANG) |
1575 CTXT_FLAGS_GPU_HANG_RECOVERED;
1576 adreno_dev->drawctxt_active = last_active_ctx;
1577 }
1578 }
1579 /* If either the bad command sequence failed or we did not play it */
1580 if (ret || !rec_data->bad_rb_size) {
1581 adreno_ringbuffer_restore(rb, rec_data->rb_buffer,
1582 rec_data->rb_size);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001583 ret = adreno_idle(device);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001584 if (ret) {
1585 /* If we fail here we can try to invalidate another
1586 * context and try recovering again */
1587 ret = -EAGAIN;
1588 goto done;
1589 }
1590 /* ringbuffer now has data from the last valid context id,
1591 * so restore the active_ctx to the last valid context */
1592 if (rec_data->last_valid_ctx_id) {
1593 struct kgsl_context *last_ctx =
1594 idr_find(&device->context_idr,
1595 rec_data->last_valid_ctx_id);
1596 if (last_ctx)
1597 adreno_dev->drawctxt_active = last_ctx->devctxt;
1598 }
1599 }
1600done:
Shubhraprakash Dasbd396692012-06-15 14:19:34 -06001601 /* Turn off iommu clocks */
1602 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
1603 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Shubhraprakash Das32240ef2012-06-06 20:27:46 -06001604 return ret;
1605}
1606
1607static int
1608adreno_recover_hang(struct kgsl_device *device,
1609 struct adreno_recovery_data *rec_data)
1610{
1611 int ret = 0;
1612 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1613 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1614 unsigned int timestamp;
1615
1616 KGSL_DRV_ERR(device,
1617 "Starting recovery from 3D GPU hang. Recovery parameters: IB1: 0x%X, "
1618 "Bad context_id: %u, global_eop: 0x%x\n",
1619 rec_data->ib1, rec_data->context_id, rec_data->global_eop);
1620
1621 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
1622 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
1623
1624 /* We may need to replay commands multiple times based on whether
1625 * multiple contexts hang the GPU */
1626 while (true) {
1627 if (!ret)
1628 ret = _adreno_recover_hang(device, rec_data, true);
1629 else
1630 ret = _adreno_recover_hang(device, rec_data, false);
1631
1632 if (-EAGAIN == ret) {
1633 /* setup new recovery parameters and retry, this
1634 * means more than 1 contexts are causing hang */
1635 adreno_destroy_recovery_data(rec_data);
1636 adreno_setup_recovery_data(device, rec_data);
1637 KGSL_DRV_ERR(device,
1638 "Retry recovery from 3D GPU hang. Recovery parameters: "
1639 "IB1: 0x%X, Bad context_id: %u, global_eop: 0x%x\n",
1640 rec_data->ib1, rec_data->context_id,
1641 rec_data->global_eop);
1642 } else {
1643 break;
1644 }
1645 }
1646
1647 if (ret)
1648 goto done;
1649
1650 /* Restore correct states after recovery */
1651 if (adreno_dev->drawctxt_active)
1652 device->mmu.hwpagetable =
1653 adreno_dev->drawctxt_active->pagetable;
1654 else
1655 device->mmu.hwpagetable = device->mmu.defaultpagetable;
1656 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
1657 kgsl_sharedmem_writel(&device->memstore,
1658 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1659 eoptimestamp),
1660 rb->timestamp[KGSL_MEMSTORE_GLOBAL]);
1661done:
1662 adreno_set_max_ts_for_bad_ctxs(device);
1663 adreno_mark_context_status(device, ret);
1664 if (!ret)
1665 KGSL_DRV_ERR(device, "Recovery succeeded\n");
1666 else
1667 KGSL_DRV_ERR(device, "Recovery failed\n");
1668 return ret;
1669}
1670
1671int
1672adreno_dump_and_recover(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001673{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001674 int result = -ETIMEDOUT;
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001675 struct adreno_recovery_data rec_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001676
1677 if (device->state == KGSL_STATE_HUNG)
1678 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -07001679 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001680 mutex_unlock(&device->mutex);
1681 wait_for_completion(&device->recovery_gate);
1682 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001683 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001684 result = 0;
1685 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001686 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001687 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001688 /* Detected a hang */
1689
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001690 /* Get the recovery data as soon as hang is detected */
1691 result = adreno_setup_recovery_data(device, &rec_data);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001692 /*
1693 * Trigger an automatic dump of the state to
1694 * the console
1695 */
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06001696 kgsl_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001697
1698 /*
1699 * Make a GPU snapshot. For now, do it after the PM dump so we
1700 * can at least be sure the PM dump will work as it always has
1701 */
1702 kgsl_device_snapshot(device, 1);
1703
Shubhraprakash Dasba6c70b2012-05-31 02:53:06 -06001704 result = adreno_recover_hang(device, &rec_data);
1705 adreno_destroy_recovery_data(&rec_data);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001706 if (result) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001707 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001708 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001709 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Shubhraprakash Dasdf609302012-06-06 20:02:58 -06001710 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
1711 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001712 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713 }
1714done:
1715 return result;
1716}
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06001717EXPORT_SYMBOL(adreno_dump_and_recover);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001718
1719static int adreno_getproperty(struct kgsl_device *device,
1720 enum kgsl_property_type type,
1721 void *value,
1722 unsigned int sizebytes)
1723{
1724 int status = -EINVAL;
1725 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1726
1727 switch (type) {
1728 case KGSL_PROP_DEVICE_INFO:
1729 {
1730 struct kgsl_devinfo devinfo;
1731
1732 if (sizebytes != sizeof(devinfo)) {
1733 status = -EINVAL;
1734 break;
1735 }
1736
1737 memset(&devinfo, 0, sizeof(devinfo));
1738 devinfo.device_id = device->id+1;
1739 devinfo.chip_id = adreno_dev->chip_id;
1740 devinfo.mmu_enabled = kgsl_mmu_enabled();
1741 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -06001742 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
1743 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001744
1745 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
1746 0) {
1747 status = -EFAULT;
1748 break;
1749 }
1750 status = 0;
1751 }
1752 break;
1753 case KGSL_PROP_DEVICE_SHADOW:
1754 {
1755 struct kgsl_shadowprop shadowprop;
1756
1757 if (sizebytes != sizeof(shadowprop)) {
1758 status = -EINVAL;
1759 break;
1760 }
1761 memset(&shadowprop, 0, sizeof(shadowprop));
1762 if (device->memstore.hostptr) {
1763 /*NOTE: with mmu enabled, gpuaddr doesn't mean
1764 * anything to mmap().
1765 */
Shubhraprakash Das87f68132012-07-30 23:25:13 -07001766 shadowprop.gpuaddr = device->memstore.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001767 shadowprop.size = device->memstore.size;
1768 /* GSL needs this to be set, even if it
1769 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001770 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
1771 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001772 }
1773 if (copy_to_user(value, &shadowprop,
1774 sizeof(shadowprop))) {
1775 status = -EFAULT;
1776 break;
1777 }
1778 status = 0;
1779 }
1780 break;
1781 case KGSL_PROP_MMU_ENABLE:
1782 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001783 int mmu_prop = kgsl_mmu_enabled();
1784
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001785 if (sizebytes != sizeof(int)) {
1786 status = -EINVAL;
1787 break;
1788 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06001789 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790 status = -EFAULT;
1791 break;
1792 }
1793 status = 0;
1794 }
1795 break;
1796 case KGSL_PROP_INTERRUPT_WAITS:
1797 {
1798 int int_waits = 1;
1799 if (sizebytes != sizeof(int)) {
1800 status = -EINVAL;
1801 break;
1802 }
1803 if (copy_to_user(value, &int_waits, sizeof(int))) {
1804 status = -EFAULT;
1805 break;
1806 }
1807 status = 0;
1808 }
1809 break;
1810 default:
1811 status = -EINVAL;
1812 }
1813
1814 return status;
1815}
1816
Jordan Crousef7370f82012-04-18 09:31:07 -06001817static int adreno_setproperty(struct kgsl_device *device,
1818 enum kgsl_property_type type,
1819 void *value,
1820 unsigned int sizebytes)
1821{
1822 int status = -EINVAL;
1823
1824 switch (type) {
1825 case KGSL_PROP_PWRCTRL: {
1826 unsigned int enable;
1827 struct kgsl_device_platform_data *pdata =
1828 kgsl_device_get_drvdata(device);
1829
1830 if (sizebytes != sizeof(enable))
1831 break;
1832
1833 if (copy_from_user(&enable, (void __user *) value,
1834 sizeof(enable))) {
1835 status = -EFAULT;
1836 break;
1837 }
1838
1839 if (enable) {
1840 if (pdata->nap_allowed)
1841 device->pwrctrl.nap_allowed = true;
1842
1843 kgsl_pwrscale_enable(device);
1844 } else {
1845 device->pwrctrl.nap_allowed = false;
1846 kgsl_pwrscale_disable(device);
1847 }
1848
1849 status = 0;
1850 }
1851 break;
1852 default:
1853 break;
1854 }
1855
1856 return status;
1857}
1858
Lynus Vaz06a9a902011-10-04 19:25:33 +05301859static inline void adreno_poke(struct kgsl_device *device)
1860{
1861 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1862 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
1863}
1864
Jordan Crousea29a2e02012-08-14 09:09:23 -06001865static int adreno_ringbuffer_drain(struct kgsl_device *device,
1866 unsigned int *regs)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001867{
1868 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1869 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
Jordan Crousea29a2e02012-08-14 09:09:23 -06001870 unsigned long wait;
1871 unsigned long timeout = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
1872
1873 if (!(rb->flags & KGSL_FLAGS_STARTED))
1874 return 0;
1875
1876 /*
1877 * The first time into the loop, wait for 100 msecs and kick wptr again
1878 * to ensure that the hardware has updated correctly. After that, kick
1879 * it periodically every KGSL_TIMEOUT_PART msecs until the timeout
1880 * expires
1881 */
1882
1883 wait = jiffies + msecs_to_jiffies(100);
1884
1885 adreno_poke(device);
1886
1887 do {
1888 if (time_after(jiffies, wait)) {
1889 adreno_poke(device);
1890
1891 /* Check to see if the core is hung */
1892 if (adreno_hang_detect(device, regs))
1893 return -ETIMEDOUT;
1894
1895 wait = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1896 }
1897 GSL_RB_GET_READPTR(rb, &rb->rptr);
1898
1899 if (time_after(jiffies, timeout)) {
1900 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
1901 rb->rptr, rb->wptr);
1902 return -ETIMEDOUT;
1903 }
1904 } while (rb->rptr != rb->wptr);
1905
1906 return 0;
1907}
1908
1909/* Caller must hold the device mutex. */
1910int adreno_idle(struct kgsl_device *device)
1911{
1912 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001913 unsigned int rbbm_status;
Lynus Vaz284d1042012-01-31 16:32:31 +05301914 unsigned long wait_time;
1915 unsigned long wait_time_part;
Tarun Karra3335f142012-06-19 14:11:48 -07001916 unsigned int prev_reg_val[hang_detect_regs_count];
1917
1918 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001919
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001920 kgsl_cffdump_regpoll(device->id,
1921 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001922 0x00000000, 0x80000000);
Jordan Crousea29a2e02012-08-14 09:09:23 -06001923
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924retry:
Jordan Crousea29a2e02012-08-14 09:09:23 -06001925 /* First, wait for the ringbuffer to drain */
1926 if (adreno_ringbuffer_drain(device, prev_reg_val))
1927 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001928
1929 /* now, wait for the GPU to finish its operations */
Jordan Crousea29a2e02012-08-14 09:09:23 -06001930 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
1931 wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
1932
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001933 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001934 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
1935 &rbbm_status);
1936 if (adreno_is_a2xx(adreno_dev)) {
1937 if (rbbm_status == 0x110)
1938 return 0;
1939 } else {
1940 if (!(rbbm_status & 0x80000000))
1941 return 0;
1942 }
Tarun Karra3335f142012-06-19 14:11:48 -07001943
1944 /* Dont wait for timeout, detect hang faster.
1945 */
1946 if (time_after(jiffies, wait_time_part)) {
1947 wait_time_part = jiffies +
Jordan Crousea29a2e02012-08-14 09:09:23 -06001948 msecs_to_jiffies(KGSL_TIMEOUT_PART);
Tarun Karra3335f142012-06-19 14:11:48 -07001949 if ((adreno_hang_detect(device, prev_reg_val)))
1950 goto err;
1951 }
1952
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001953 }
1954
1955err:
1956 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06001957 if (KGSL_STATE_DUMP_AND_RECOVER != device->state &&
1958 !adreno_dump_and_recover(device)) {
Jordan Crousea29a2e02012-08-14 09:09:23 -06001959 wait_time = jiffies + ADRENO_IDLE_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001960 goto retry;
1961 }
1962 return -ETIMEDOUT;
1963}
1964
1965static unsigned int adreno_isidle(struct kgsl_device *device)
1966{
1967 int status = false;
1968 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1969 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1970 unsigned int rbbm_status;
1971
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001972 WARN_ON(device->state == KGSL_STATE_INIT);
1973 /* If the device isn't active, don't force it on. */
1974 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001975 /* Is the ring buffer is empty? */
1976 GSL_RB_GET_READPTR(rb, &rb->rptr);
1977 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
1978 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001979 adreno_regread(device,
1980 adreno_dev->gpudev->reg_rbbm_status,
1981 &rbbm_status);
1982
1983 if (adreno_is_a2xx(adreno_dev)) {
1984 if (rbbm_status == 0x110)
1985 status = true;
1986 } else {
1987 if (!(rbbm_status & 0x80000000))
1988 status = true;
1989 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001990 }
1991 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001992 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001993 }
1994 return status;
1995}
1996
1997/* Caller must hold the device mutex. */
1998static int adreno_suspend_context(struct kgsl_device *device)
1999{
2000 int status = 0;
2001 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2002
2003 /* switch to NULL ctxt */
2004 if (adreno_dev->drawctxt_active != NULL) {
2005 adreno_drawctxt_switch(adreno_dev, NULL, 0);
Jordan Crousea29a2e02012-08-14 09:09:23 -06002006 status = adreno_idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002007 }
2008
2009 return status;
2010}
2011
Jordan Crouse233b2092012-04-18 09:31:09 -06002012/* Find a memory structure attached to an adreno context */
2013
2014struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
2015 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
2016{
2017 struct kgsl_context *context;
2018 struct adreno_context *adreno_context = NULL;
2019 int next = 0;
2020
2021 while (1) {
2022 context = idr_get_next(&device->context_idr, &next);
2023 if (context == NULL)
2024 break;
2025
2026 adreno_context = (struct adreno_context *)context->devctxt;
2027
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002028 if (kgsl_mmu_pt_equal(&device->mmu, adreno_context->pagetable,
2029 pt_base)) {
Jordan Crouse233b2092012-04-18 09:31:09 -06002030 struct kgsl_memdesc *desc;
2031
2032 desc = &adreno_context->gpustate;
2033 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2034 return desc;
2035
2036 desc = &adreno_context->context_gmem_shadow.gmemshadow;
2037 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
2038 return desc;
2039 }
2040 next = next + 1;
2041 }
2042
2043 return NULL;
2044}
2045
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002046struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002047 unsigned int pt_base,
2048 unsigned int gpuaddr,
2049 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002050{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002051 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002052 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2053 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
2054
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002055 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
2056 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002057
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002058 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
2059 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002060
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002061 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
2062 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002063
Shubhraprakash Das9a140972012-04-12 13:12:42 -06002064 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
2065 size))
2066 return &device->mmu.setstate_memory;
2067
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -07002068 entry = kgsl_get_mem_entry(device, pt_base, gpuaddr, size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06002069
2070 if (entry)
2071 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002072
Jordan Crouse233b2092012-04-18 09:31:09 -06002073 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002074}
2075
2076uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
2077 unsigned int gpuaddr, unsigned int size)
2078{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06002079 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07002080
2081 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
2082
2083 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002084}
2085
2086void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2087 unsigned int *value)
2088{
2089 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06002090 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
2091 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002092
2093 if (!in_interrupt())
2094 kgsl_pre_hwaccess(device);
2095
2096 /*ensure this read finishes before the next one.
2097 * i.e. act like normal readl() */
2098 *value = __raw_readl(reg);
2099 rmb();
2100}
2101
2102void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
2103 unsigned int value)
2104{
2105 unsigned int *reg;
2106
Jordan Crouse7501d452012-04-19 08:58:44 -06002107 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002108
2109 if (!in_interrupt())
2110 kgsl_pre_hwaccess(device);
2111
2112 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06002113 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002114
2115 /*ensure previous writes post before this one,
2116 * i.e. act like normal writel() */
2117 wmb();
2118 __raw_writel(value, reg);
2119}
2120
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002121static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
2122{
2123 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002124 if (k_ctxt != NULL) {
2125 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002126 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
2127 context_id = KGSL_CONTEXT_INVALID;
2128 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
2129 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002130 }
2131
2132 return context_id;
2133}
2134
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002135static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002136 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002137{
2138 int status;
2139 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002140 unsigned int context_id;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002141 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002142
2143 mutex_lock(&device->mutex);
2144 context_id = _get_context_id(context);
2145 /*
2146 * If the context ID is invalid, we are in a race with
2147 * the context being destroyed by userspace so bail.
2148 */
2149 if (context_id == KGSL_CONTEXT_INVALID) {
2150 KGSL_DRV_WARN(device, "context was detached");
2151 status = -EINVAL;
2152 goto unlock;
2153 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002154
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002155 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002156 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002157 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002158 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002159 mb();
2160
2161 if (enableflag) {
2162 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002163 KGSL_MEMSTORE_OFFSET(context_id,
2164 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002165 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07002166 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002167 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002168 KGSL_MEMSTORE_OFFSET(context_id,
2169 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002170 wmb();
2171 }
2172 } else {
2173 unsigned int cmds[2];
2174 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002175 KGSL_MEMSTORE_OFFSET(context_id,
2176 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002177 enableflag = 1;
2178 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002179 KGSL_MEMSTORE_OFFSET(context_id,
2180 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181 wmb();
2182 /* submit a dummy packet so that even if all
2183 * commands upto timestamp get executed we will still
2184 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06002185 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186 cmds[1] = 0;
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002187
2188 if (adreno_dev->drawctxt_active)
Carter Cooper7ffaba62012-05-24 13:59:53 -06002189 adreno_ringbuffer_issuecmds_intr(device,
2190 context, &cmds[0], 2);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -06002191 else
2192 /* We would never call this function if there
2193 * was no active contexts running */
2194 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002195 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002196 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002197unlock:
2198 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002199
2200 return status;
2201}
2202
2203/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06002204 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002205 placing a process in wait q. For conditional interrupts we expect the
2206 process to already be in its wait q when its exit condition checking
2207 function is called.
2208*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06002209#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002210({ \
2211 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06002212 if (io) \
2213 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
2214 else \
2215 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002216 __ret; \
2217})
2218
Tarun Karra3335f142012-06-19 14:11:48 -07002219
2220
2221unsigned int adreno_hang_detect(struct kgsl_device *device,
2222 unsigned int *prev_reg_val)
2223{
2224 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2225 unsigned int curr_reg_val[hang_detect_regs_count];
2226 unsigned int hang_detected = 1;
2227 unsigned int i;
2228
2229 if (!adreno_dev->fast_hang_detect)
2230 return 0;
2231
2232 for (i = 0; i < hang_detect_regs_count; i++) {
Jordan Crouseb5c80482012-10-03 09:38:41 -06002233
2234 if (hang_detect_regs[i] == 0)
2235 continue;
2236
Tarun Karra3335f142012-06-19 14:11:48 -07002237 adreno_regread(device, hang_detect_regs[i],
2238 &curr_reg_val[i]);
2239 if (curr_reg_val[i] != prev_reg_val[i]) {
2240 prev_reg_val[i] = curr_reg_val[i];
2241 hang_detected = 0;
2242 }
2243 }
2244
2245 return hang_detected;
2246}
2247
2248
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002249/* MUST be called with the device mutex held */
2250static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002251 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002252 unsigned int timestamp,
2253 unsigned int msecs)
2254{
2255 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06002256 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06002257 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002258 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06002259 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Tarun Karra3335f142012-06-19 14:11:48 -07002260 int retries = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002261 unsigned int ts_issued;
2262 unsigned int context_id = _get_context_id(context);
Tarun Karra3335f142012-06-19 14:11:48 -07002263 unsigned int time_elapsed = 0;
2264 unsigned int prev_reg_val[hang_detect_regs_count];
Jordan Crouse21f75a02012-08-09 15:08:59 -06002265 unsigned int wait;
Tarun Karra3335f142012-06-19 14:11:48 -07002266
2267 memset(prev_reg_val, 0, sizeof(prev_reg_val));
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002268
2269 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002270
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302271 /* Don't wait forever, set a max value for now */
Tarun Karra3335f142012-06-19 14:11:48 -07002272 if (msecs == KGSL_TIMEOUT_DEFAULT)
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05302273 msecs = adreno_dev->wait_timeout;
2274
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002275 if (timestamp_cmp(timestamp, ts_issued) > 0) {
2276 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
2277 "last issued ts <%d:0x%x>\n",
2278 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002279 status = -EINVAL;
2280 goto done;
2281 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002282
Jordan Crouse21f75a02012-08-09 15:08:59 -06002283 /*
2284 * Make the first timeout interval 100 msecs and then try to kick the
2285 * wptr again. This helps to ensure the wptr is updated properly. If
2286 * the requested timeout is less than 100 msecs, then wait 20msecs which
2287 * is the minimum amount of time we can safely wait at 100HZ
Lynus Vaz06a9a902011-10-04 19:25:33 +05302288 */
Jordan Crouse21f75a02012-08-09 15:08:59 -06002289
2290 if (msecs == 0 || msecs >= 100)
2291 wait = 100;
2292 else
2293 wait = 20;
2294
Tarun Karra3335f142012-06-19 14:11:48 -07002295 do {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002296 /*
2297 * If the context ID is invalid, we are in a race with
2298 * the context being destroyed by userspace so bail.
2299 */
2300 if (context_id == KGSL_CONTEXT_INVALID) {
2301 KGSL_DRV_WARN(device, "context was detached");
2302 status = -EINVAL;
2303 goto done;
2304 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002305 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07002306 /* if the timestamp happens while we're not
2307 * waiting, there's a chance that an interrupt
2308 * will not be generated and thus the timestamp
2309 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05302310 */
Jeremy Gebben63904832012-02-07 16:10:55 -07002311 queue_work(device->work_queue, &device->ts_expired_ws);
2312 status = 0;
2313 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002314 }
Jeremy Gebben63904832012-02-07 16:10:55 -07002315 adreno_poke(device);
2316 io_cnt = (io_cnt + 1) % 100;
2317 if (io_cnt <
2318 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
2319 io = 0;
Tarun Karra3335f142012-06-19 14:11:48 -07002320
2321 if ((retries > 0) &&
2322 (adreno_hang_detect(device, prev_reg_val)))
2323 goto hang_dump;
2324
Jeremy Gebben63904832012-02-07 16:10:55 -07002325 mutex_unlock(&device->mutex);
2326 /* We need to make sure that the process is
2327 * placed in wait-q before its condition is called
2328 */
2329 status = kgsl_wait_event_interruptible_timeout(
2330 device->wait_queue,
2331 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002332 context, timestamp),
Jordan Crouse21f75a02012-08-09 15:08:59 -06002333 msecs_to_jiffies(wait), io);
2334
Jeremy Gebben63904832012-02-07 16:10:55 -07002335 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002336
Jeremy Gebben63904832012-02-07 16:10:55 -07002337 if (status > 0) {
2338 /*completed before the wait finished */
2339 status = 0;
2340 goto done;
2341 } else if (status < 0) {
2342 /*an error occurred*/
2343 goto done;
2344 }
2345 /*this wait timed out*/
Tarun Karra3335f142012-06-19 14:11:48 -07002346
Jordan Crouse21f75a02012-08-09 15:08:59 -06002347 time_elapsed += wait;
2348 wait = KGSL_TIMEOUT_PART;
2349
Tarun Karra3335f142012-06-19 14:11:48 -07002350 retries++;
2351
Jordan Crouse21f75a02012-08-09 15:08:59 -06002352 } while (!msecs || time_elapsed < msecs);
Tarun Karra3335f142012-06-19 14:11:48 -07002353
2354hang_dump:
Shubhraprakash Das54396e52012-03-10 13:24:54 -07002355 /*
2356 * Check if timestamp has retired here because we may have hit
2357 * recovery which can take some time and cause waiting threads
2358 * to timeout
2359 */
2360 if (kgsl_check_timestamp(device, context, timestamp))
2361 goto done;
Jeremy Gebben63904832012-02-07 16:10:55 -07002362 status = -ETIMEDOUT;
2363 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002364 "Device hang detected while waiting for timestamp: "
2365 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
2366 "wptr: 0x%x\n",
2367 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07002368 adreno_dev->ringbuffer.wptr);
2369 if (!adreno_dump_and_recover(device)) {
Shubhraprakash Das1088bdb2012-05-29 18:19:11 -06002370 /* The timestamp that this process wanted
2371 * to wait on may be invalid or expired now
2372 * after successful recovery */
Jeremy Gebben63904832012-02-07 16:10:55 -07002373 status = 0;
2374 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002375done:
2376 return (int)status;
2377}
2378
2379static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002380 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002381{
2382 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07002383 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002384
Jeremy Gebben9ad86922012-05-08 15:33:23 -06002385 /*
2386 * If the context ID is invalid, we are in a race with
2387 * the context being destroyed by userspace so bail.
2388 */
2389 if (context_id == KGSL_CONTEXT_INVALID) {
2390 KGSL_DRV_WARN(device, "context was detached");
2391 return timestamp;
2392 }
Jordan Crousec659f382012-04-16 11:10:41 -06002393 switch (type) {
2394 case KGSL_TIMESTAMP_QUEUED: {
2395 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2396 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
2397
2398 timestamp = rb->timestamp[context_id];
2399 break;
2400 }
2401 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002402 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06002403 break;
2404 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002405 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06002406 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
2407 break;
2408 }
2409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002410 rmb();
2411
2412 return timestamp;
2413}
2414
2415static long adreno_ioctl(struct kgsl_device_private *dev_priv,
2416 unsigned int cmd, void *data)
2417{
2418 int result = 0;
2419 struct kgsl_drawctxt_set_bin_base_offset *binbase;
2420 struct kgsl_context *context;
2421
2422 switch (cmd) {
2423 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
2424 binbase = data;
2425
2426 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
2427 if (context) {
2428 adreno_drawctxt_set_bin_base_offset(
2429 dev_priv->device, context, binbase->offset);
2430 } else {
2431 result = -EINVAL;
2432 KGSL_DRV_ERR(dev_priv->device,
2433 "invalid drawctxt drawctxt_id %d "
2434 "device_id=%d\n",
2435 binbase->drawctxt_id, dev_priv->device->id);
2436 }
2437 break;
2438
2439 default:
2440 KGSL_DRV_INFO(dev_priv->device,
2441 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002442 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002443 break;
2444 }
2445 return result;
2446
2447}
2448
2449static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
2450{
2451 gpu_freq /= 1000000;
2452 return ticks / gpu_freq;
2453}
2454
2455static void adreno_power_stats(struct kgsl_device *device,
2456 struct kgsl_power_stats *stats)
2457{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002458 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002459 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002460 unsigned int cycles;
2461
2462 /* Get the busy cycles counted since the counter was last reset */
2463 /* Calling this function also resets and restarts the counter */
2464
2465 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002466
2467 /* In order to calculate idle you have to have run the algorithm *
2468 * at least once to get a start time. */
2469 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002470 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002471 stats->total_time = tmp - pwr->time;
2472 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07002473 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002474 pwrlevels[device->pwrctrl.active_pwrlevel].
2475 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002476 } else {
2477 stats->total_time = 0;
2478 stats->busy_time = 0;
2479 pwr->time = ktime_to_us(ktime_get());
2480 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481}
2482
2483void adreno_irqctrl(struct kgsl_device *device, int state)
2484{
Jordan Crousea78c9172011-07-11 13:14:09 -06002485 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2486 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002487}
2488
Jordan Croused6535882012-06-20 08:22:16 -06002489static unsigned int adreno_gpuid(struct kgsl_device *device,
2490 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -07002491{
2492 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2493
Jordan Croused6535882012-06-20 08:22:16 -06002494 /* Some applications need to know the chip ID too, so pass
2495 * that as a parameter */
2496
2497 if (chipid != NULL)
2498 *chipid = adreno_dev->chip_id;
2499
Jordan Crousea0758f22011-12-07 11:19:22 -07002500 /* Standard KGSL gpuid format:
2501 * top word is 0x0002 for 2D or 0x0003 for 3D
2502 * Bottom word is core specific identifer
2503 */
2504
2505 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
2506}
2507
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002508static const struct kgsl_functable adreno_functable = {
2509 /* Mandatory functions */
2510 .regread = adreno_regread,
2511 .regwrite = adreno_regwrite,
2512 .idle = adreno_idle,
2513 .isidle = adreno_isidle,
2514 .suspend_context = adreno_suspend_context,
2515 .start = adreno_start,
2516 .stop = adreno_stop,
2517 .getproperty = adreno_getproperty,
2518 .waittimestamp = adreno_waittimestamp,
2519 .readtimestamp = adreno_readtimestamp,
2520 .issueibcmds = adreno_ringbuffer_issueibcmds,
2521 .ioctl = adreno_ioctl,
2522 .setup_pt = adreno_setup_pt,
2523 .cleanup_pt = adreno_cleanup_pt,
2524 .power_stats = adreno_power_stats,
2525 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07002526 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002527 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06002528 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529 /* Optional functions */
2530 .setstate = adreno_setstate,
2531 .drawctxt_create = adreno_drawctxt_create,
2532 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06002533 .setproperty = adreno_setproperty,
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -06002534 .postmortem_dump = adreno_dump,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002535};
2536
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002537static struct platform_driver adreno_platform_driver = {
2538 .probe = adreno_probe,
2539 .remove = __devexit_p(adreno_remove),
2540 .suspend = kgsl_suspend_driver,
2541 .resume = kgsl_resume_driver,
2542 .id_table = adreno_id_table,
2543 .driver = {
2544 .owner = THIS_MODULE,
2545 .name = DEVICE_3D_NAME,
2546 .pm = &kgsl_pm_ops,
Lokesh Batra805e1e12012-08-03 08:34:06 -06002547 .of_match_table = adreno_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002548 }
2549};
2550
2551static int __init kgsl_3d_init(void)
2552{
2553 return platform_driver_register(&adreno_platform_driver);
2554}
2555
2556static void __exit kgsl_3d_exit(void)
2557{
2558 platform_driver_unregister(&adreno_platform_driver);
2559}
2560
2561module_init(kgsl_3d_init);
2562module_exit(kgsl_3d_exit);
2563
2564MODULE_DESCRIPTION("3D Graphics driver");
2565MODULE_VERSION("1.2");
2566MODULE_LICENSE("GPL v2");
2567MODULE_ALIAS("platform:kgsl_3d");