blob: 76b577d020700f89a735678ea6d2c4b8d6d20973 [file] [log] [blame]
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001/* Copyright (c) 2002,2007-2012, Code Aurora Forum. 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 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070013#include <linux/uaccess.h>
14#include <linux/vmalloc.h>
15#include <linux/ioctl.h>
16#include <linux/sched.h>
17
18#include <mach/socinfo.h>
19
20#include "kgsl.h"
21#include "kgsl_pwrscale.h"
22#include "kgsl_cffdump.h"
23#include "kgsl_sharedmem.h"
24
25#include "adreno.h"
26#include "adreno_pm4types.h"
27#include "adreno_debugfs.h"
28#include "adreno_postmortem.h"
29
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070030#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070031#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
33#define DRIVER_VERSION_MAJOR 3
34#define DRIVER_VERSION_MINOR 1
35
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036/* Adreno MH arbiter config*/
37#define ADRENO_CFG_MHARB \
38 (0x10 \
39 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
40 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
41 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
42 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
43 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
44 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
45 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
46 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
47 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
48 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
53
54#define ADRENO_MMU_CONFIG \
55 (0x01 \
56 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
57 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
58 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
59 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
60 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
61 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
62 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
67
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068static const struct kgsl_functable adreno_functable;
69
70static struct adreno_device device_3d0 = {
71 .dev = {
72 .name = DEVICE_3D0_NAME,
73 .id = KGSL_DEVICE_3D0,
74 .ver_major = DRIVER_VERSION_MAJOR,
75 .ver_minor = DRIVER_VERSION_MINOR,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060076 .mh = {
77 .mharb = ADRENO_CFG_MHARB,
78 /* Remove 1k boundary check in z470 to avoid a GPU
79 * hang. Notice that this solution won't work if
80 * both EBI and SMI are used
81 */
82 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083 /* turn off memory protection unit by setting
84 acceptable physical address range to include
85 all pages. */
86 .mpu_base = 0x00000000,
87 .mpu_range = 0xFFFFF000,
88 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060089 .mmu = {
90 .config = ADRENO_MMU_CONFIG,
91 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092 .pwrctrl = {
93 .regulator_name = "fs_gfx3d",
94 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095 },
96 .mutex = __MUTEX_INITIALIZER(device_3d0.dev.mutex),
97 .state = KGSL_STATE_INIT,
98 .active_cnt = 0,
99 .iomemname = KGSL_3D0_REG_MEMORY,
100 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600102 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
104 .suspend = kgsl_early_suspend_driver,
105 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600107#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600109 .gmem_base = 0,
110 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111 .pfp_fw = NULL,
112 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700113 .wait_timeout = 10000, /* in milliseconds */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600114 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115};
116
Jordan Crouse95b33272011-11-11 14:50:12 -0700117
Jordan Crouse505df9c2011-07-28 08:37:59 -0600118/*
119 * This is the master list of all GPU cores that are supported by this
120 * driver.
121 */
122
123#define ANY_ID (~0)
124
125static const struct {
126 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600127 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600128 const char *pm4fw;
129 const char *pfpfw;
130 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700131 unsigned int istore_size;
132 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700133 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530134 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600135} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600136 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700137 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530138 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530139 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
140 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530141 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600142 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700143 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530144 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600145 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700146 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530147 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600148 /*
149 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
150 * a hardware problem.
151 */
152 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700153 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530154 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700155 { ADRENO_REV_A225, 2, 2, 0, 6,
156 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530157 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600158 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700159 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530160 1536, 768, 3, SZ_512K },
161 /* A3XX doesn't use the pix_shader_start */
Jordan Crouse54154c62012-03-27 16:33:26 -0600162 { ADRENO_REV_A305, 3, 0, 5, 0,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530163 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
164 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700165 /* A3XX doesn't use the pix_shader_start */
Jordan Crouse54154c62012-03-27 16:33:26 -0600166 { ADRENO_REV_A320, 3, 2, 0, 0,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700167 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530168 512, 0, 2, SZ_512K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700169
Jordan Crouse505df9c2011-07-28 08:37:59 -0600170};
171
Jordan Crouse9f739212011-07-28 08:37:57 -0600172static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173{
Jordan Crousea78c9172011-07-11 13:14:09 -0600174 irqreturn_t result;
175 struct kgsl_device *device = data;
176 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177
Jordan Crousea78c9172011-07-11 13:14:09 -0600178 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179
180 if (device->requested_state == KGSL_STATE_NONE) {
181 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700182 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183 queue_work(device->work_queue, &device->idle_check_ws);
184 } else if (device->pwrscale.policy != NULL) {
185 queue_work(device->work_queue, &device->idle_check_ws);
186 }
187 }
188
189 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800190 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 jiffies + device->pwrctrl.interval_timeout);
192 return result;
193}
194
Jordan Crouse9f739212011-07-28 08:37:57 -0600195static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196 struct kgsl_pagetable *pagetable)
197{
198 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
199 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
200
201 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
202
203 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
204
205 kgsl_mmu_unmap(pagetable, &device->memstore);
206
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600207 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208}
209
210static int adreno_setup_pt(struct kgsl_device *device,
211 struct kgsl_pagetable *pagetable)
212{
213 int result = 0;
214 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
215 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
216
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
218 GSL_PT_PAGE_RV);
219 if (result)
220 goto error;
221
222 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
223 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
224 if (result)
225 goto unmap_buffer_desc;
226
227 result = kgsl_mmu_map_global(pagetable, &device->memstore,
228 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
229 if (result)
230 goto unmap_memptrs_desc;
231
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600232 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
234 if (result)
235 goto unmap_memstore_desc;
236
237 return result;
238
239unmap_memstore_desc:
240 kgsl_mmu_unmap(pagetable, &device->memstore);
241
242unmap_memptrs_desc:
243 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
244
245unmap_buffer_desc:
246 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
247
248error:
249 return result;
250}
251
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600252static void adreno_setstate(struct kgsl_device *device,
253 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254{
255 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
256 unsigned int link[32];
257 unsigned int *cmds = &link[0];
258 int sizedwords = 0;
259 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
260
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600261 /*
Jordan Crousee3f80ea2012-02-04 14:22:36 -0700262 * A3XX doesn't support the fast path (the registers don't even exist)
263 * so just bail out early
264 */
265
266 if (adreno_is_a3xx(adreno_dev)) {
267 kgsl_mmu_device_setstate(device, flags);
268 return;
269 }
270
271 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600272 * If possible, then set the state via the command stream to avoid
273 * a CPU idle. Otherwise, use the default setstate which uses register
274 * writes For CFF dump we must idle and use the registers so that it is
275 * easier to filter out the mmu accesses from the dump
276 */
277 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
279 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600280 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 *cmds++ = 0x00000000;
282
283 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600284 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600285 *cmds++ = kgsl_pt_get_base_addr(
286 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 sizedwords += 4;
288 }
289
290 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
291 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600292 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 1);
294 *cmds++ = 0x00000000;
295 sizedwords += 2;
296 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600297 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 *cmds++ = mh_mmu_invalidate;
299 sizedwords += 2;
300 }
301
302 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600303 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304 /* HW workaround: to resolve MMU page fault interrupts
305 * caused by the VGT.It prevents the CP PFP from filling
306 * the VGT DMA request fifo too early,thereby ensuring
307 * that the VGT will not fetch vertex/bin data until
308 * after the page table base register has been updated.
309 *
310 * Two null DRAW_INDX_BIN packets are inserted right
311 * after the page table base update, followed by a
312 * wait for idle. The null packets will fill up the
313 * VGT DMA request fifo and prevent any further
314 * vertex/bin updates from occurring until the wait
315 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600316 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 *cmds++ = (0x4 << 16) |
318 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
319 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600320 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600321 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600322 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 *cmds++ = 0; /* viz query info */
324 *cmds++ = 0x0003C004; /* draw indicator */
325 *cmds++ = 0; /* bin base */
326 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600327 *cmds++ =
328 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600330 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 *cmds++ = 0; /* viz query info */
332 *cmds++ = 0x0003C004; /* draw indicator */
333 *cmds++ = 0; /* bin base */
334 *cmds++ = 3; /* bin size */
335 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600336 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600338 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 *cmds++ = 0x00000000;
340 sizedwords += 21;
341 }
342
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600343
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600345 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346 *cmds++ = 0x7fff; /* invalidate all base pointers */
347 sizedwords += 2;
348 }
349
350 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
351 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600352 } else {
353 kgsl_mmu_device_setstate(device, flags);
354 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355}
356
357static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700358a3xx_getchipid(struct kgsl_device *device)
359{
Jordan Crouse54154c62012-03-27 16:33:26 -0600360 unsigned int majorid, minorid, patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700361
Jordan Crouse54154c62012-03-27 16:33:26 -0600362 /*
363 * We could detect the chipID from the hardware but it takes multiple
364 * registers to find the right combination. Since we traffic exclusively
365 * in system on chips, we can be (mostly) confident that a SOC version
366 * will match a GPU (at this juncture at least). So do the lazy/quick
367 * thing and set the chip_id based on the SoC
368 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700369
Jordan Crouse54154c62012-03-27 16:33:26 -0600370 if (cpu_is_apq8064()) {
371 /* A320 */
372 majorid = 2;
373 minorid = 0;
374 patchid = 0;
375 } else if (cpu_is_msm8930()) {
376 /* A305 */
377 majorid = 0;
378 minorid = 5;
379 patchid = 0;
380 }
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700381
Jordan Crouse54154c62012-03-27 16:33:26 -0600382 return (0x03 << 24) | (majorid << 16) | (minorid << 8) | patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700383}
384
385static unsigned int
386a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387{
388 unsigned int chipid = 0;
389 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700390 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391
392 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
393 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
394 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
395
396 /*
397 * adreno 22x gpus are indicated by coreid 2,
398 * but REG_RBBM_PERIPHID1 always contains 0 for this field
399 */
Sudhakara Rao Tentudaebac22012-04-02 14:51:29 -0700400 if (cpu_is_msm8960() || cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401 chipid = 2 << 24;
402 else
403 chipid = (coreid & 0xF) << 24;
404
405 chipid |= ((majorid >> 4) & 0xF) << 16;
406
407 minorid = ((revid >> 0) & 0xFF);
408
409 patchid = ((revid >> 16) & 0xFF);
410
411 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700412 /* 8960v3 returns 5 for patch release, but it should be 6 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530413 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414 if (cpu_is_qsd8x50())
415 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700416 else if (cpu_is_msm8960() &&
417 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
418 patchid = 6;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530419 else if (cpu_is_msm8625() && minorid == 0)
420 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421
422 chipid |= (minorid << 8) | patchid;
423
424 return chipid;
425}
426
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700427static unsigned int
428adreno_getchipid(struct kgsl_device *device)
429{
Sudhakara Rao Tentu8ebb2282012-03-06 14:52:58 +0530430 if (cpu_is_apq8064() || cpu_is_msm8930())
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700431 return a3xx_getchipid(device);
432 else
433 return a2xx_getchipid(device);
434}
435
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436static inline bool _rev_match(unsigned int id, unsigned int entry)
437{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600438 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440
441static void
442adreno_identify_gpu(struct adreno_device *adreno_dev)
443{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600444 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445
446 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
447
448 core = (adreno_dev->chip_id >> 24) & 0xff;
449 major = (adreno_dev->chip_id >> 16) & 0xff;
450 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600451 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452
Jordan Crouse505df9c2011-07-28 08:37:59 -0600453 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
454 if (core == adreno_gpulist[i].core &&
455 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600456 _rev_match(minor, adreno_gpulist[i].minor) &&
457 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 }
460
Jordan Crouse505df9c2011-07-28 08:37:59 -0600461 if (i == ARRAY_SIZE(adreno_gpulist)) {
462 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
463 return;
464 }
465
466 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
467 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
468 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
469 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700470 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
471 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700472 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600473 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474}
475
476static int __devinit
477adreno_probe(struct platform_device *pdev)
478{
479 struct kgsl_device *device;
480 struct adreno_device *adreno_dev;
481 int status = -EINVAL;
482
483 device = (struct kgsl_device *)pdev->id_entry->driver_data;
484 adreno_dev = ADRENO_DEVICE(device);
485 device->parentdev = &pdev->dev;
486
487 init_completion(&device->recovery_gate);
488
489 status = adreno_ringbuffer_init(device);
490 if (status != 0)
491 goto error;
492
493 status = kgsl_device_platform_probe(device, adreno_isr);
494 if (status)
495 goto error_close_rb;
496
497 adreno_debugfs_init(device);
498
499 kgsl_pwrscale_init(device);
500 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
501
502 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
503 return 0;
504
505error_close_rb:
506 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
507error:
508 device->parentdev = NULL;
509 return status;
510}
511
512static int __devexit adreno_remove(struct platform_device *pdev)
513{
514 struct kgsl_device *device;
515 struct adreno_device *adreno_dev;
516
517 device = (struct kgsl_device *)pdev->id_entry->driver_data;
518 adreno_dev = ADRENO_DEVICE(device);
519
520 kgsl_pwrscale_detach_policy(device);
521 kgsl_pwrscale_close(device);
522
523 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
524 kgsl_device_platform_remove(device);
525
526 return 0;
527}
528
529static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
530{
531 int status = -EINVAL;
532 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533
Jeremy Gebben388c2972011-12-16 09:05:07 -0700534 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535
536 /* Power up the device */
537 kgsl_pwrctrl_enable(device);
538
539 /* Identify the specific GPU */
540 adreno_identify_gpu(adreno_dev);
541
Jordan Crouse505df9c2011-07-28 08:37:59 -0600542 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
543 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
544 adreno_dev->chip_id);
545 goto error_clk_off;
546 }
547
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700548 /* Set up the MMU */
549 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600550 /*
551 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
552 * on older gpus
553 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700554 if (adreno_is_a20x(adreno_dev)) {
555 device->mh.mh_intf_cfg1 = 0;
556 device->mh.mh_intf_cfg2 = 0;
557 }
558
559 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600560 }
561
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700562 status = kgsl_mmu_start(device);
563 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 goto error_clk_off;
565
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700566 /* Start the GPU */
567 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568
569 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700570 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571
572 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700573 if (status == 0) {
574 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
575 return 0;
576 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600579 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580error_clk_off:
581 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582
583 return status;
584}
585
586static int adreno_stop(struct kgsl_device *device)
587{
588 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
589
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590 adreno_dev->drawctxt_active = NULL;
591
592 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
593
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 kgsl_mmu_stop(device);
595
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700596 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530597 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800598 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 /* Power down the device */
601 kgsl_pwrctrl_disable(device);
602
603 return 0;
604}
605
606static int
607adreno_recover_hang(struct kgsl_device *device)
608{
609 int ret;
610 unsigned int *rb_buffer;
611 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
612 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
613 unsigned int timestamp;
614 unsigned int num_rb_contents;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615 unsigned int reftimestamp;
616 unsigned int enable_ts;
617 unsigned int soptimestamp;
618 unsigned int eoptimestamp;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700619 unsigned int context_id;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700620 struct kgsl_context *context;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700621 struct adreno_context *adreno_context;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700622 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623
624 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
625 rb_buffer = vmalloc(rb->buffer_desc.size);
626 if (!rb_buffer) {
627 KGSL_MEM_ERR(device,
628 "Failed to allocate memory for recovery: %x\n",
629 rb->buffer_desc.size);
630 return -ENOMEM;
631 }
632 /* Extract valid contents from rb which can stil be executed after
633 * hang */
634 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
635 if (ret)
636 goto done;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700637 kgsl_sharedmem_readl(&device->memstore, &context_id,
638 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
639 current_context));
640 context = idr_find(&device->context_idr, context_id);
641 if (context == NULL) {
642 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
643 context_id);
644 context_id = KGSL_MEMSTORE_GLOBAL;
645 }
646
647 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
648 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700651 KGSL_MEMSTORE_OFFSET(context_id,
652 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700654 KGSL_MEMSTORE_OFFSET(context_id,
655 ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700657 KGSL_MEMSTORE_OFFSET(context_id,
658 soptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700660 KGSL_MEMSTORE_OFFSET(context_id,
661 eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662 /* Make sure memory is synchronized before restarting the GPU */
663 mb();
664 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700665 "Context id that caused a GPU hang: %d\n", context_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666 /* restart device */
667 ret = adreno_stop(device);
668 if (ret)
669 goto done;
670 ret = adreno_start(device, true);
671 if (ret)
672 goto done;
673 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
674 /* Restore timestamp states */
675 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700676 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 soptimestamp);
678 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700679 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680 eoptimestamp);
Carter Cooperae4c7bc2012-04-10 09:40:49 -0600681
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682 if (num_rb_contents) {
683 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700684 KGSL_MEMSTORE_OFFSET(context_id, ref_wait_ts),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685 reftimestamp);
686 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700687 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688 enable_ts);
689 }
690 /* Make sure all writes are posted before the GPU reads them */
691 wmb();
692 /* Mark the invalid context so no more commands are accepted from
693 * that context */
694
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700695 adreno_context = context->devctxt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696
697 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700698 "Context that caused a GPU hang: %d\n", adreno_context->id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700700 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700702 /*
703 * Set the reset status of all contexts to
704 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
705 * since thats the guilty party
706 */
707 while ((context = idr_get_next(&device->context_idr, &next))) {
708 if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
709 context->reset_status) {
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700710 if (context->id != context_id)
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700711 context->reset_status =
712 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
713 else
714 context->reset_status =
715 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
716 }
717 next = next + 1;
718 }
719
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 /* Restore valid commands in ringbuffer */
721 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700722 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723done:
724 vfree(rb_buffer);
725 return ret;
726}
727
728static int
729adreno_dump_and_recover(struct kgsl_device *device)
730{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731 int result = -ETIMEDOUT;
732
733 if (device->state == KGSL_STATE_HUNG)
734 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700735 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736 mutex_unlock(&device->mutex);
737 wait_for_completion(&device->recovery_gate);
738 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700739 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 result = 0;
741 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700742 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700744 /* Detected a hang */
745
746
747 /*
748 * Trigger an automatic dump of the state to
749 * the console
750 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700752
753 /*
754 * Make a GPU snapshot. For now, do it after the PM dump so we
755 * can at least be sure the PM dump will work as it always has
756 */
757 kgsl_device_snapshot(device, 1);
758
Jeremy Gebben388c2972011-12-16 09:05:07 -0700759 result = adreno_recover_hang(device);
760 if (result)
761 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
762 else
763 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
764 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 }
766done:
767 return result;
768}
769
770static int adreno_getproperty(struct kgsl_device *device,
771 enum kgsl_property_type type,
772 void *value,
773 unsigned int sizebytes)
774{
775 int status = -EINVAL;
776 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
777
778 switch (type) {
779 case KGSL_PROP_DEVICE_INFO:
780 {
781 struct kgsl_devinfo devinfo;
782
783 if (sizebytes != sizeof(devinfo)) {
784 status = -EINVAL;
785 break;
786 }
787
788 memset(&devinfo, 0, sizeof(devinfo));
789 devinfo.device_id = device->id+1;
790 devinfo.chip_id = adreno_dev->chip_id;
791 devinfo.mmu_enabled = kgsl_mmu_enabled();
792 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -0600793 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
794 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795
796 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
797 0) {
798 status = -EFAULT;
799 break;
800 }
801 status = 0;
802 }
803 break;
804 case KGSL_PROP_DEVICE_SHADOW:
805 {
806 struct kgsl_shadowprop shadowprop;
807
808 if (sizebytes != sizeof(shadowprop)) {
809 status = -EINVAL;
810 break;
811 }
812 memset(&shadowprop, 0, sizeof(shadowprop));
813 if (device->memstore.hostptr) {
814 /*NOTE: with mmu enabled, gpuaddr doesn't mean
815 * anything to mmap().
816 */
817 shadowprop.gpuaddr = device->memstore.physaddr;
818 shadowprop.size = device->memstore.size;
819 /* GSL needs this to be set, even if it
820 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700821 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
822 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 }
824 if (copy_to_user(value, &shadowprop,
825 sizeof(shadowprop))) {
826 status = -EFAULT;
827 break;
828 }
829 status = 0;
830 }
831 break;
832 case KGSL_PROP_MMU_ENABLE:
833 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600834 int mmu_prop = kgsl_mmu_enabled();
835
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 if (sizebytes != sizeof(int)) {
837 status = -EINVAL;
838 break;
839 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600840 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 status = -EFAULT;
842 break;
843 }
844 status = 0;
845 }
846 break;
847 case KGSL_PROP_INTERRUPT_WAITS:
848 {
849 int int_waits = 1;
850 if (sizebytes != sizeof(int)) {
851 status = -EINVAL;
852 break;
853 }
854 if (copy_to_user(value, &int_waits, sizeof(int))) {
855 status = -EFAULT;
856 break;
857 }
858 status = 0;
859 }
860 break;
861 default:
862 status = -EINVAL;
863 }
864
865 return status;
866}
867
Lynus Vaz06a9a902011-10-04 19:25:33 +0530868static inline void adreno_poke(struct kgsl_device *device)
869{
870 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
871 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
872}
873
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874/* Caller must hold the device mutex. */
875int adreno_idle(struct kgsl_device *device, unsigned int timeout)
876{
877 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
878 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
879 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530880 unsigned long wait_timeout =
881 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +0530882 unsigned long wait_time;
883 unsigned long wait_time_part;
884 unsigned int msecs;
885 unsigned int msecs_first;
886 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700888 kgsl_cffdump_regpoll(device->id,
889 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 0x00000000, 0x80000000);
891 /* first, wait until the CP has consumed all the commands in
892 * the ring buffer
893 */
894retry:
895 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +0530896 msecs = adreno_dev->wait_timeout;
897 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
898 msecs_part = (msecs - msecs_first + 3) / 4;
899 wait_time = jiffies + wait_timeout;
900 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700901 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 do {
Lynus Vaz284d1042012-01-31 16:32:31 +0530903 if (time_after(jiffies, wait_time_part)) {
904 adreno_poke(device);
905 wait_time_part = jiffies +
906 msecs_to_jiffies(msecs_part);
907 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 GSL_RB_GET_READPTR(rb, &rb->rptr);
909 if (time_after(jiffies, wait_time)) {
910 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
911 rb->rptr, rb->wptr);
912 goto err;
913 }
914 } while (rb->rptr != rb->wptr);
915 }
916
917 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530918 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700920 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
921 &rbbm_status);
922 if (adreno_is_a2xx(adreno_dev)) {
923 if (rbbm_status == 0x110)
924 return 0;
925 } else {
926 if (!(rbbm_status & 0x80000000))
927 return 0;
928 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929 }
930
931err:
932 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
933 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530934 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935 goto retry;
936 }
937 return -ETIMEDOUT;
938}
939
940static unsigned int adreno_isidle(struct kgsl_device *device)
941{
942 int status = false;
943 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
944 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
945 unsigned int rbbm_status;
946
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700947 WARN_ON(device->state == KGSL_STATE_INIT);
948 /* If the device isn't active, don't force it on. */
949 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950 /* Is the ring buffer is empty? */
951 GSL_RB_GET_READPTR(rb, &rb->rptr);
952 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
953 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700954 adreno_regread(device,
955 adreno_dev->gpudev->reg_rbbm_status,
956 &rbbm_status);
957
958 if (adreno_is_a2xx(adreno_dev)) {
959 if (rbbm_status == 0x110)
960 status = true;
961 } else {
962 if (!(rbbm_status & 0x80000000))
963 status = true;
964 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 }
966 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -0700967 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968 }
969 return status;
970}
971
972/* Caller must hold the device mutex. */
973static int adreno_suspend_context(struct kgsl_device *device)
974{
975 int status = 0;
976 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
977
978 /* switch to NULL ctxt */
979 if (adreno_dev->drawctxt_active != NULL) {
980 adreno_drawctxt_switch(adreno_dev, NULL, 0);
981 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
982 }
983
984 return status;
985}
986
Jordan Crouse233b2092012-04-18 09:31:09 -0600987/* Find a memory structure attached to an adreno context */
988
989struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
990 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
991{
992 struct kgsl_context *context;
993 struct adreno_context *adreno_context = NULL;
994 int next = 0;
995
996 while (1) {
997 context = idr_get_next(&device->context_idr, &next);
998 if (context == NULL)
999 break;
1000
1001 adreno_context = (struct adreno_context *)context->devctxt;
1002
1003 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1004 struct kgsl_memdesc *desc;
1005
1006 desc = &adreno_context->gpustate;
1007 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1008 return desc;
1009
1010 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1011 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1012 return desc;
1013 }
1014 next = next + 1;
1015 }
1016
1017 return NULL;
1018}
1019
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001020struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001021 unsigned int pt_base,
1022 unsigned int gpuaddr,
1023 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1027 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1028
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001029 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1030 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001032 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1033 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001035 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1036 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001038 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1039 size))
1040 return &device->mmu.setstate_memory;
1041
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001042 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1043
1044 if (entry)
1045 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046
Jordan Crouse233b2092012-04-18 09:31:09 -06001047 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001048}
1049
1050uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1051 unsigned int gpuaddr, unsigned int size)
1052{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001053 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001054
1055 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1056
1057 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058}
1059
1060void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1061 unsigned int *value)
1062{
1063 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06001064 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
1065 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066
1067 if (!in_interrupt())
1068 kgsl_pre_hwaccess(device);
1069
1070 /*ensure this read finishes before the next one.
1071 * i.e. act like normal readl() */
1072 *value = __raw_readl(reg);
1073 rmb();
1074}
1075
1076void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1077 unsigned int value)
1078{
1079 unsigned int *reg;
1080
Jordan Crouse7501d452012-04-19 08:58:44 -06001081 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082
1083 if (!in_interrupt())
1084 kgsl_pre_hwaccess(device);
1085
1086 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06001087 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088
1089 /*ensure previous writes post before this one,
1090 * i.e. act like normal writel() */
1091 wmb();
1092 __raw_writel(value, reg);
1093}
1094
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001095static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1096{
1097 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
1098
1099 if (k_ctxt != NULL) {
1100 struct adreno_context *a_ctxt = k_ctxt->devctxt;
1101 /*
1102 * if the context was not created with per context timestamp
1103 * support, we must use the global timestamp since issueibcmds
1104 * will be returning that one.
1105 */
1106 if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
1107 context_id = a_ctxt->id;
1108 }
1109
1110 return context_id;
1111}
1112
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001114 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001115{
1116 int status;
1117 unsigned int ref_ts, enableflag;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001118 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001120 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 if (!status) {
1122 mutex_lock(&device->mutex);
1123 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001124 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125 mb();
1126
1127 if (enableflag) {
1128 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001129 KGSL_MEMSTORE_OFFSET(context_id,
1130 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001131 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001132 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001133 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001134 KGSL_MEMSTORE_OFFSET(context_id,
1135 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001136 wmb();
1137 }
1138 } else {
1139 unsigned int cmds[2];
1140 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001141 KGSL_MEMSTORE_OFFSET(context_id,
1142 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143 enableflag = 1;
1144 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001145 KGSL_MEMSTORE_OFFSET(context_id,
1146 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001147 wmb();
1148 /* submit a dummy packet so that even if all
1149 * commands upto timestamp get executed we will still
1150 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001151 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001153 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1154 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155 }
1156 mutex_unlock(&device->mutex);
1157 }
1158
1159 return status;
1160}
1161
1162/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001163 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 placing a process in wait q. For conditional interrupts we expect the
1165 process to already be in its wait q when its exit condition checking
1166 function is called.
1167*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001168#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169({ \
1170 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001171 if (io) \
1172 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1173 else \
1174 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175 __ret; \
1176})
1177
1178/* MUST be called with the device mutex held */
1179static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001180 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001181 unsigned int timestamp,
1182 unsigned int msecs)
1183{
1184 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001185 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001186 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001187 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001188 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301189 int retries;
1190 unsigned int msecs_first;
1191 unsigned int msecs_part;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001192 unsigned int ts_issued;
1193 unsigned int context_id = _get_context_id(context);
1194
1195 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301197 /* Don't wait forever, set a max value for now */
1198 if (msecs == -1)
1199 msecs = adreno_dev->wait_timeout;
1200
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001201 if (timestamp_cmp(timestamp, ts_issued) > 0) {
1202 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
1203 "last issued ts <%d:0x%x>\n",
1204 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001205 status = -EINVAL;
1206 goto done;
1207 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208
Lynus Vaz06a9a902011-10-04 19:25:33 +05301209 /* Keep the first timeout as 100msecs before rewriting
1210 * the WPTR. Less visible impact if the WPTR has not
1211 * been updated properly.
1212 */
1213 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1214 msecs_part = (msecs - msecs_first + 3) / 4;
1215 for (retries = 0; retries < 5; retries++) {
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001216 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001217 /* if the timestamp happens while we're not
1218 * waiting, there's a chance that an interrupt
1219 * will not be generated and thus the timestamp
1220 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301221 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001222 queue_work(device->work_queue, &device->ts_expired_ws);
1223 status = 0;
1224 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001226 adreno_poke(device);
1227 io_cnt = (io_cnt + 1) % 100;
1228 if (io_cnt <
1229 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1230 io = 0;
1231 mutex_unlock(&device->mutex);
1232 /* We need to make sure that the process is
1233 * placed in wait-q before its condition is called
1234 */
1235 status = kgsl_wait_event_interruptible_timeout(
1236 device->wait_queue,
1237 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001238 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001239 msecs_to_jiffies(retries ?
1240 msecs_part : msecs_first), io);
1241 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242
Jeremy Gebben63904832012-02-07 16:10:55 -07001243 if (status > 0) {
1244 /*completed before the wait finished */
1245 status = 0;
1246 goto done;
1247 } else if (status < 0) {
1248 /*an error occurred*/
1249 goto done;
1250 }
1251 /*this wait timed out*/
1252 }
1253 status = -ETIMEDOUT;
1254 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001255 "Device hang detected while waiting for timestamp: "
1256 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
1257 "wptr: 0x%x\n",
1258 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07001259 adreno_dev->ringbuffer.wptr);
1260 if (!adreno_dump_and_recover(device)) {
1261 /* wait for idle after recovery as the
1262 * timestamp that this process wanted
1263 * to wait on may be invalid */
1264 if (!adreno_idle(device, KGSL_TIMEOUT_DEFAULT))
1265 status = 0;
1266 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001267done:
1268 return (int)status;
1269}
1270
1271static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001272 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001273{
1274 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001275 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001276
Jordan Crousec659f382012-04-16 11:10:41 -06001277 switch (type) {
1278 case KGSL_TIMESTAMP_QUEUED: {
1279 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1280 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1281
1282 timestamp = rb->timestamp[context_id];
1283 break;
1284 }
1285 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06001287 break;
1288 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06001290 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
1291 break;
1292 }
1293
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294 rmb();
1295
1296 return timestamp;
1297}
1298
1299static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1300 unsigned int cmd, void *data)
1301{
1302 int result = 0;
1303 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1304 struct kgsl_context *context;
1305
1306 switch (cmd) {
1307 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1308 binbase = data;
1309
1310 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1311 if (context) {
1312 adreno_drawctxt_set_bin_base_offset(
1313 dev_priv->device, context, binbase->offset);
1314 } else {
1315 result = -EINVAL;
1316 KGSL_DRV_ERR(dev_priv->device,
1317 "invalid drawctxt drawctxt_id %d "
1318 "device_id=%d\n",
1319 binbase->drawctxt_id, dev_priv->device->id);
1320 }
1321 break;
1322
1323 default:
1324 KGSL_DRV_INFO(dev_priv->device,
1325 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001326 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001327 break;
1328 }
1329 return result;
1330
1331}
1332
1333static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1334{
1335 gpu_freq /= 1000000;
1336 return ticks / gpu_freq;
1337}
1338
1339static void adreno_power_stats(struct kgsl_device *device,
1340 struct kgsl_power_stats *stats)
1341{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001342 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001343 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001344 unsigned int cycles;
1345
1346 /* Get the busy cycles counted since the counter was last reset */
1347 /* Calling this function also resets and restarts the counter */
1348
1349 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350
1351 /* In order to calculate idle you have to have run the algorithm *
1352 * at least once to get a start time. */
1353 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001354 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001355 stats->total_time = tmp - pwr->time;
1356 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001357 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001358 pwrlevels[device->pwrctrl.active_pwrlevel].
1359 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360 } else {
1361 stats->total_time = 0;
1362 stats->busy_time = 0;
1363 pwr->time = ktime_to_us(ktime_get());
1364 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365}
1366
1367void adreno_irqctrl(struct kgsl_device *device, int state)
1368{
Jordan Crousea78c9172011-07-11 13:14:09 -06001369 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1370 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001371}
1372
Jordan Crousea0758f22011-12-07 11:19:22 -07001373static unsigned int adreno_gpuid(struct kgsl_device *device)
1374{
1375 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1376
1377 /* Standard KGSL gpuid format:
1378 * top word is 0x0002 for 2D or 0x0003 for 3D
1379 * Bottom word is core specific identifer
1380 */
1381
1382 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1383}
1384
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385static const struct kgsl_functable adreno_functable = {
1386 /* Mandatory functions */
1387 .regread = adreno_regread,
1388 .regwrite = adreno_regwrite,
1389 .idle = adreno_idle,
1390 .isidle = adreno_isidle,
1391 .suspend_context = adreno_suspend_context,
1392 .start = adreno_start,
1393 .stop = adreno_stop,
1394 .getproperty = adreno_getproperty,
1395 .waittimestamp = adreno_waittimestamp,
1396 .readtimestamp = adreno_readtimestamp,
1397 .issueibcmds = adreno_ringbuffer_issueibcmds,
1398 .ioctl = adreno_ioctl,
1399 .setup_pt = adreno_setup_pt,
1400 .cleanup_pt = adreno_cleanup_pt,
1401 .power_stats = adreno_power_stats,
1402 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001403 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001404 .snapshot = adreno_snapshot,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001405 /* Optional functions */
1406 .setstate = adreno_setstate,
1407 .drawctxt_create = adreno_drawctxt_create,
1408 .drawctxt_destroy = adreno_drawctxt_destroy,
1409};
1410
1411static struct platform_device_id adreno_id_table[] = {
1412 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1413 { },
1414};
1415MODULE_DEVICE_TABLE(platform, adreno_id_table);
1416
1417static struct platform_driver adreno_platform_driver = {
1418 .probe = adreno_probe,
1419 .remove = __devexit_p(adreno_remove),
1420 .suspend = kgsl_suspend_driver,
1421 .resume = kgsl_resume_driver,
1422 .id_table = adreno_id_table,
1423 .driver = {
1424 .owner = THIS_MODULE,
1425 .name = DEVICE_3D_NAME,
1426 .pm = &kgsl_pm_ops,
1427 }
1428};
1429
1430static int __init kgsl_3d_init(void)
1431{
1432 return platform_driver_register(&adreno_platform_driver);
1433}
1434
1435static void __exit kgsl_3d_exit(void)
1436{
1437 platform_driver_unregister(&adreno_platform_driver);
1438}
1439
1440module_init(kgsl_3d_init);
1441module_exit(kgsl_3d_exit);
1442
1443MODULE_DESCRIPTION("3D Graphics driver");
1444MODULE_VERSION("1.2");
1445MODULE_LICENSE("GPL v2");
1446MODULE_ALIAS("platform:kgsl_3d");