blob: 21774facbd7ad2455fd97f765f5bb0c22e695da3 [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 },
109 .gmemspace = {
110 .gpu_base = 0,
111 .sizebytes = SZ_256K,
112 },
113 .pfp_fw = NULL,
114 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700115 .wait_timeout = 10000, /* in milliseconds */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600116 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117};
118
Jordan Crouse95b33272011-11-11 14:50:12 -0700119
Jordan Crouse505df9c2011-07-28 08:37:59 -0600120/*
121 * This is the master list of all GPU cores that are supported by this
122 * driver.
123 */
124
125#define ANY_ID (~0)
126
127static const struct {
128 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600129 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600130 const char *pm4fw;
131 const char *pfpfw;
132 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700133 unsigned int istore_size;
134 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700135 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530136 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600137} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600138 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700139 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530140 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530141 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
142 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530143 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600144 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700145 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530146 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600147 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700148 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530149 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600150 /*
151 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
152 * a hardware problem.
153 */
154 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700155 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530156 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700157 { ADRENO_REV_A225, 2, 2, 0, 6,
158 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530159 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600160 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700161 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530162 1536, 768, 3, SZ_512K },
163 /* A3XX doesn't use the pix_shader_start */
Jordan Crouse54154c62012-03-27 16:33:26 -0600164 { ADRENO_REV_A305, 3, 0, 5, 0,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530165 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
166 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700167 /* A3XX doesn't use the pix_shader_start */
Jordan Crouse54154c62012-03-27 16:33:26 -0600168 { ADRENO_REV_A320, 3, 2, 0, 0,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700169 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530170 512, 0, 2, SZ_512K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700171
Jordan Crouse505df9c2011-07-28 08:37:59 -0600172};
173
Jordan Crouse9f739212011-07-28 08:37:57 -0600174static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175{
Jordan Crousea78c9172011-07-11 13:14:09 -0600176 irqreturn_t result;
177 struct kgsl_device *device = data;
178 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179
Jordan Crousea78c9172011-07-11 13:14:09 -0600180 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181
182 if (device->requested_state == KGSL_STATE_NONE) {
183 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700184 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185 queue_work(device->work_queue, &device->idle_check_ws);
186 } else if (device->pwrscale.policy != NULL) {
187 queue_work(device->work_queue, &device->idle_check_ws);
188 }
189 }
190
191 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800192 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193 jiffies + device->pwrctrl.interval_timeout);
194 return result;
195}
196
Jordan Crouse9f739212011-07-28 08:37:57 -0600197static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 struct kgsl_pagetable *pagetable)
199{
200 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
201 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
202
203 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
204
205 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
206
207 kgsl_mmu_unmap(pagetable, &device->memstore);
208
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600209 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210}
211
212static int adreno_setup_pt(struct kgsl_device *device,
213 struct kgsl_pagetable *pagetable)
214{
215 int result = 0;
216 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
217 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
218
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
220 GSL_PT_PAGE_RV);
221 if (result)
222 goto error;
223
224 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
225 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
226 if (result)
227 goto unmap_buffer_desc;
228
229 result = kgsl_mmu_map_global(pagetable, &device->memstore,
230 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
231 if (result)
232 goto unmap_memptrs_desc;
233
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600234 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
236 if (result)
237 goto unmap_memstore_desc;
238
239 return result;
240
241unmap_memstore_desc:
242 kgsl_mmu_unmap(pagetable, &device->memstore);
243
244unmap_memptrs_desc:
245 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
246
247unmap_buffer_desc:
248 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
249
250error:
251 return result;
252}
253
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600254static void adreno_setstate(struct kgsl_device *device,
255 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256{
257 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
258 unsigned int link[32];
259 unsigned int *cmds = &link[0];
260 int sizedwords = 0;
261 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
262
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600263 /*
Jordan Crousee3f80ea2012-02-04 14:22:36 -0700264 * A3XX doesn't support the fast path (the registers don't even exist)
265 * so just bail out early
266 */
267
268 if (adreno_is_a3xx(adreno_dev)) {
269 kgsl_mmu_device_setstate(device, flags);
270 return;
271 }
272
273 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600274 * If possible, then set the state via the command stream to avoid
275 * a CPU idle. Otherwise, use the default setstate which uses register
276 * writes For CFF dump we must idle and use the registers so that it is
277 * easier to filter out the mmu accesses from the dump
278 */
279 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
281 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600282 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 *cmds++ = 0x00000000;
284
285 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600286 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600287 *cmds++ = kgsl_pt_get_base_addr(
288 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 sizedwords += 4;
290 }
291
292 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
293 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600294 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 1);
296 *cmds++ = 0x00000000;
297 sizedwords += 2;
298 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600299 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 *cmds++ = mh_mmu_invalidate;
301 sizedwords += 2;
302 }
303
304 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600305 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 /* HW workaround: to resolve MMU page fault interrupts
307 * caused by the VGT.It prevents the CP PFP from filling
308 * the VGT DMA request fifo too early,thereby ensuring
309 * that the VGT will not fetch vertex/bin data until
310 * after the page table base register has been updated.
311 *
312 * Two null DRAW_INDX_BIN packets are inserted right
313 * after the page table base update, followed by a
314 * wait for idle. The null packets will fill up the
315 * VGT DMA request fifo and prevent any further
316 * vertex/bin updates from occurring until the wait
317 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600318 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 *cmds++ = (0x4 << 16) |
320 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
321 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600322 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600323 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600324 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 *cmds++ = 0; /* viz query info */
326 *cmds++ = 0x0003C004; /* draw indicator */
327 *cmds++ = 0; /* bin base */
328 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600329 *cmds++ =
330 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600332 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 *cmds++ = 0; /* viz query info */
334 *cmds++ = 0x0003C004; /* draw indicator */
335 *cmds++ = 0; /* bin base */
336 *cmds++ = 3; /* bin size */
337 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600338 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600340 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341 *cmds++ = 0x00000000;
342 sizedwords += 21;
343 }
344
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600345
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600347 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348 *cmds++ = 0x7fff; /* invalidate all base pointers */
349 sizedwords += 2;
350 }
351
352 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
353 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600354 } else {
355 kgsl_mmu_device_setstate(device, flags);
356 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357}
358
359static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700360a3xx_getchipid(struct kgsl_device *device)
361{
Jordan Crouse54154c62012-03-27 16:33:26 -0600362 unsigned int majorid, minorid, patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700363
Jordan Crouse54154c62012-03-27 16:33:26 -0600364 /*
365 * We could detect the chipID from the hardware but it takes multiple
366 * registers to find the right combination. Since we traffic exclusively
367 * in system on chips, we can be (mostly) confident that a SOC version
368 * will match a GPU (at this juncture at least). So do the lazy/quick
369 * thing and set the chip_id based on the SoC
370 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700371
Jordan Crouse54154c62012-03-27 16:33:26 -0600372 if (cpu_is_apq8064()) {
373 /* A320 */
374 majorid = 2;
375 minorid = 0;
376 patchid = 0;
377 } else if (cpu_is_msm8930()) {
378 /* A305 */
379 majorid = 0;
380 minorid = 5;
381 patchid = 0;
382 }
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700383
Jordan Crouse54154c62012-03-27 16:33:26 -0600384 return (0x03 << 24) | (majorid << 16) | (minorid << 8) | patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700385}
386
387static unsigned int
388a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389{
390 unsigned int chipid = 0;
391 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700392 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393
394 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
395 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
396 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
397
398 /*
399 * adreno 22x gpus are indicated by coreid 2,
400 * but REG_RBBM_PERIPHID1 always contains 0 for this field
401 */
Sudhakara Rao Tentudaebac22012-04-02 14:51:29 -0700402 if (cpu_is_msm8960() || cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 chipid = 2 << 24;
404 else
405 chipid = (coreid & 0xF) << 24;
406
407 chipid |= ((majorid >> 4) & 0xF) << 16;
408
409 minorid = ((revid >> 0) & 0xFF);
410
411 patchid = ((revid >> 16) & 0xFF);
412
413 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700414 /* 8960v3 returns 5 for patch release, but it should be 6 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530415 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416 if (cpu_is_qsd8x50())
417 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700418 else if (cpu_is_msm8960() &&
419 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
420 patchid = 6;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530421 else if (cpu_is_msm8625() && minorid == 0)
422 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423
424 chipid |= (minorid << 8) | patchid;
425
426 return chipid;
427}
428
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700429static unsigned int
430adreno_getchipid(struct kgsl_device *device)
431{
Sudhakara Rao Tentu8ebb2282012-03-06 14:52:58 +0530432 if (cpu_is_apq8064() || cpu_is_msm8930())
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700433 return a3xx_getchipid(device);
434 else
435 return a2xx_getchipid(device);
436}
437
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438static inline bool _rev_match(unsigned int id, unsigned int entry)
439{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600440 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442
443static void
444adreno_identify_gpu(struct adreno_device *adreno_dev)
445{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600446 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447
448 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
449
450 core = (adreno_dev->chip_id >> 24) & 0xff;
451 major = (adreno_dev->chip_id >> 16) & 0xff;
452 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600453 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454
Jordan Crouse505df9c2011-07-28 08:37:59 -0600455 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
456 if (core == adreno_gpulist[i].core &&
457 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600458 _rev_match(minor, adreno_gpulist[i].minor) &&
459 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461 }
462
Jordan Crouse505df9c2011-07-28 08:37:59 -0600463 if (i == ARRAY_SIZE(adreno_gpulist)) {
464 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
465 return;
466 }
467
468 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
469 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
470 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
471 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700472 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
473 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700474 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530475 adreno_dev->gmemspace.sizebytes = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476}
477
478static int __devinit
479adreno_probe(struct platform_device *pdev)
480{
481 struct kgsl_device *device;
482 struct adreno_device *adreno_dev;
483 int status = -EINVAL;
484
485 device = (struct kgsl_device *)pdev->id_entry->driver_data;
486 adreno_dev = ADRENO_DEVICE(device);
487 device->parentdev = &pdev->dev;
488
489 init_completion(&device->recovery_gate);
490
491 status = adreno_ringbuffer_init(device);
492 if (status != 0)
493 goto error;
494
495 status = kgsl_device_platform_probe(device, adreno_isr);
496 if (status)
497 goto error_close_rb;
498
499 adreno_debugfs_init(device);
500
501 kgsl_pwrscale_init(device);
502 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
503
504 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
505 return 0;
506
507error_close_rb:
508 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
509error:
510 device->parentdev = NULL;
511 return status;
512}
513
514static int __devexit adreno_remove(struct platform_device *pdev)
515{
516 struct kgsl_device *device;
517 struct adreno_device *adreno_dev;
518
519 device = (struct kgsl_device *)pdev->id_entry->driver_data;
520 adreno_dev = ADRENO_DEVICE(device);
521
522 kgsl_pwrscale_detach_policy(device);
523 kgsl_pwrscale_close(device);
524
525 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
526 kgsl_device_platform_remove(device);
527
528 return 0;
529}
530
531static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
532{
533 int status = -EINVAL;
534 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535
Jeremy Gebben388c2972011-12-16 09:05:07 -0700536 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537
538 /* Power up the device */
539 kgsl_pwrctrl_enable(device);
540
541 /* Identify the specific GPU */
542 adreno_identify_gpu(adreno_dev);
543
Jordan Crouse505df9c2011-07-28 08:37:59 -0600544 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
545 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
546 adreno_dev->chip_id);
547 goto error_clk_off;
548 }
549
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700550 /* Set up the MMU */
551 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600552 /*
553 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
554 * on older gpus
555 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700556 if (adreno_is_a20x(adreno_dev)) {
557 device->mh.mh_intf_cfg1 = 0;
558 device->mh.mh_intf_cfg2 = 0;
559 }
560
561 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600562 }
563
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700564 status = kgsl_mmu_start(device);
565 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 goto error_clk_off;
567
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700568 /* Start the GPU */
569 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570
571 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700572 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573
574 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700575 if (status == 0) {
576 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
577 return 0;
578 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600581 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582error_clk_off:
583 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584
585 return status;
586}
587
588static int adreno_stop(struct kgsl_device *device)
589{
590 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
591
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592 adreno_dev->drawctxt_active = NULL;
593
594 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
595
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596 kgsl_mmu_stop(device);
597
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700598 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530599 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800600 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600601
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 /* Power down the device */
603 kgsl_pwrctrl_disable(device);
604
605 return 0;
606}
607
608static int
609adreno_recover_hang(struct kgsl_device *device)
610{
611 int ret;
612 unsigned int *rb_buffer;
613 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
614 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
615 unsigned int timestamp;
616 unsigned int num_rb_contents;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617 unsigned int reftimestamp;
618 unsigned int enable_ts;
619 unsigned int soptimestamp;
620 unsigned int eoptimestamp;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700621 unsigned int context_id;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700622 struct kgsl_context *context;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700623 struct adreno_context *adreno_context;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700624 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625
626 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
627 rb_buffer = vmalloc(rb->buffer_desc.size);
628 if (!rb_buffer) {
629 KGSL_MEM_ERR(device,
630 "Failed to allocate memory for recovery: %x\n",
631 rb->buffer_desc.size);
632 return -ENOMEM;
633 }
634 /* Extract valid contents from rb which can stil be executed after
635 * hang */
636 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
637 if (ret)
638 goto done;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700639 kgsl_sharedmem_readl(&device->memstore, &context_id,
640 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
641 current_context));
642 context = idr_find(&device->context_idr, context_id);
643 if (context == NULL) {
644 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
645 context_id);
646 context_id = KGSL_MEMSTORE_GLOBAL;
647 }
648
649 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
650 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
651
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700653 KGSL_MEMSTORE_OFFSET(context_id,
654 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700656 KGSL_MEMSTORE_OFFSET(context_id,
657 ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700659 KGSL_MEMSTORE_OFFSET(context_id,
660 soptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700662 KGSL_MEMSTORE_OFFSET(context_id,
663 eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 /* Make sure memory is synchronized before restarting the GPU */
665 mb();
666 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700667 "Context id that caused a GPU hang: %d\n", context_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668 /* restart device */
669 ret = adreno_stop(device);
670 if (ret)
671 goto done;
672 ret = adreno_start(device, true);
673 if (ret)
674 goto done;
675 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
676 /* Restore timestamp states */
677 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700678 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 soptimestamp);
680 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700681 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682 eoptimestamp);
683 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700684 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685 soptimestamp);
686 if (num_rb_contents) {
687 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700688 KGSL_MEMSTORE_OFFSET(context_id, ref_wait_ts),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689 reftimestamp);
690 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700691 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692 enable_ts);
693 }
694 /* Make sure all writes are posted before the GPU reads them */
695 wmb();
696 /* Mark the invalid context so no more commands are accepted from
697 * that context */
698
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700699 adreno_context = context->devctxt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700
701 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700702 "Context that caused a GPU hang: %d\n", adreno_context->id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700704 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700706 /*
707 * Set the reset status of all contexts to
708 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
709 * since thats the guilty party
710 */
711 while ((context = idr_get_next(&device->context_idr, &next))) {
712 if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
713 context->reset_status) {
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700714 if (context->id != context_id)
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700715 context->reset_status =
716 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
717 else
718 context->reset_status =
719 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
720 }
721 next = next + 1;
722 }
723
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724 /* Restore valid commands in ringbuffer */
725 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700726 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727done:
728 vfree(rb_buffer);
729 return ret;
730}
731
732static int
733adreno_dump_and_recover(struct kgsl_device *device)
734{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735 int result = -ETIMEDOUT;
736
737 if (device->state == KGSL_STATE_HUNG)
738 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700739 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 mutex_unlock(&device->mutex);
741 wait_for_completion(&device->recovery_gate);
742 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700743 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 result = 0;
745 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700746 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700748 /* Detected a hang */
749
750
751 /*
752 * Trigger an automatic dump of the state to
753 * the console
754 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700756
757 /*
758 * Make a GPU snapshot. For now, do it after the PM dump so we
759 * can at least be sure the PM dump will work as it always has
760 */
761 kgsl_device_snapshot(device, 1);
762
Jeremy Gebben388c2972011-12-16 09:05:07 -0700763 result = adreno_recover_hang(device);
764 if (result)
765 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
766 else
767 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
768 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 }
770done:
771 return result;
772}
773
774static int adreno_getproperty(struct kgsl_device *device,
775 enum kgsl_property_type type,
776 void *value,
777 unsigned int sizebytes)
778{
779 int status = -EINVAL;
780 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
781
782 switch (type) {
783 case KGSL_PROP_DEVICE_INFO:
784 {
785 struct kgsl_devinfo devinfo;
786
787 if (sizebytes != sizeof(devinfo)) {
788 status = -EINVAL;
789 break;
790 }
791
792 memset(&devinfo, 0, sizeof(devinfo));
793 devinfo.device_id = device->id+1;
794 devinfo.chip_id = adreno_dev->chip_id;
795 devinfo.mmu_enabled = kgsl_mmu_enabled();
796 devinfo.gpu_id = adreno_dev->gpurev;
797 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
798 gpu_base;
799 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
800 sizebytes;
801
802 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
803 0) {
804 status = -EFAULT;
805 break;
806 }
807 status = 0;
808 }
809 break;
810 case KGSL_PROP_DEVICE_SHADOW:
811 {
812 struct kgsl_shadowprop shadowprop;
813
814 if (sizebytes != sizeof(shadowprop)) {
815 status = -EINVAL;
816 break;
817 }
818 memset(&shadowprop, 0, sizeof(shadowprop));
819 if (device->memstore.hostptr) {
820 /*NOTE: with mmu enabled, gpuaddr doesn't mean
821 * anything to mmap().
822 */
823 shadowprop.gpuaddr = device->memstore.physaddr;
824 shadowprop.size = device->memstore.size;
825 /* GSL needs this to be set, even if it
826 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700827 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
828 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 }
830 if (copy_to_user(value, &shadowprop,
831 sizeof(shadowprop))) {
832 status = -EFAULT;
833 break;
834 }
835 status = 0;
836 }
837 break;
838 case KGSL_PROP_MMU_ENABLE:
839 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600840 int mmu_prop = kgsl_mmu_enabled();
841
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842 if (sizebytes != sizeof(int)) {
843 status = -EINVAL;
844 break;
845 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600846 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847 status = -EFAULT;
848 break;
849 }
850 status = 0;
851 }
852 break;
853 case KGSL_PROP_INTERRUPT_WAITS:
854 {
855 int int_waits = 1;
856 if (sizebytes != sizeof(int)) {
857 status = -EINVAL;
858 break;
859 }
860 if (copy_to_user(value, &int_waits, sizeof(int))) {
861 status = -EFAULT;
862 break;
863 }
864 status = 0;
865 }
866 break;
867 default:
868 status = -EINVAL;
869 }
870
871 return status;
872}
873
Lynus Vaz06a9a902011-10-04 19:25:33 +0530874static inline void adreno_poke(struct kgsl_device *device)
875{
876 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
877 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
878}
879
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880/* Caller must hold the device mutex. */
881int adreno_idle(struct kgsl_device *device, unsigned int timeout)
882{
883 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
884 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
885 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530886 unsigned long wait_timeout =
887 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +0530888 unsigned long wait_time;
889 unsigned long wait_time_part;
890 unsigned int msecs;
891 unsigned int msecs_first;
892 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700894 kgsl_cffdump_regpoll(device->id,
895 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896 0x00000000, 0x80000000);
897 /* first, wait until the CP has consumed all the commands in
898 * the ring buffer
899 */
900retry:
901 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +0530902 msecs = adreno_dev->wait_timeout;
903 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
904 msecs_part = (msecs - msecs_first + 3) / 4;
905 wait_time = jiffies + wait_timeout;
906 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700907 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 do {
Lynus Vaz284d1042012-01-31 16:32:31 +0530909 if (time_after(jiffies, wait_time_part)) {
910 adreno_poke(device);
911 wait_time_part = jiffies +
912 msecs_to_jiffies(msecs_part);
913 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 GSL_RB_GET_READPTR(rb, &rb->rptr);
915 if (time_after(jiffies, wait_time)) {
916 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
917 rb->rptr, rb->wptr);
918 goto err;
919 }
920 } while (rb->rptr != rb->wptr);
921 }
922
923 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530924 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700926 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
927 &rbbm_status);
928 if (adreno_is_a2xx(adreno_dev)) {
929 if (rbbm_status == 0x110)
930 return 0;
931 } else {
932 if (!(rbbm_status & 0x80000000))
933 return 0;
934 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935 }
936
937err:
938 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
939 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530940 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941 goto retry;
942 }
943 return -ETIMEDOUT;
944}
945
946static unsigned int adreno_isidle(struct kgsl_device *device)
947{
948 int status = false;
949 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
950 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
951 unsigned int rbbm_status;
952
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700953 WARN_ON(device->state == KGSL_STATE_INIT);
954 /* If the device isn't active, don't force it on. */
955 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956 /* Is the ring buffer is empty? */
957 GSL_RB_GET_READPTR(rb, &rb->rptr);
958 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
959 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700960 adreno_regread(device,
961 adreno_dev->gpudev->reg_rbbm_status,
962 &rbbm_status);
963
964 if (adreno_is_a2xx(adreno_dev)) {
965 if (rbbm_status == 0x110)
966 status = true;
967 } else {
968 if (!(rbbm_status & 0x80000000))
969 status = true;
970 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971 }
972 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -0700973 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700974 }
975 return status;
976}
977
978/* Caller must hold the device mutex. */
979static int adreno_suspend_context(struct kgsl_device *device)
980{
981 int status = 0;
982 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
983
984 /* switch to NULL ctxt */
985 if (adreno_dev->drawctxt_active != NULL) {
986 adreno_drawctxt_switch(adreno_dev, NULL, 0);
987 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
988 }
989
990 return status;
991}
992
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600993struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700994 unsigned int pt_base,
995 unsigned int gpuaddr,
996 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997{
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700998 struct kgsl_memdesc *result = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1001 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001002 struct kgsl_context *context;
1003 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001005 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1006 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001007
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001008 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1009 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001011 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1012 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001014 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1015 size))
1016 return &device->mmu.setstate_memory;
1017
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001018 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1019
1020 if (entry)
1021 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001023 while (1) {
1024 struct adreno_context *adreno_context = NULL;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001025 context = idr_get_next(&device->context_idr, &next);
1026 if (context == NULL)
1027 break;
1028
1029 adreno_context = (struct adreno_context *)context->devctxt;
1030
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001031 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1032 struct kgsl_memdesc *desc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001033
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001034 desc = &adreno_context->gpustate;
1035 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1036 result = desc;
1037 return result;
1038 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001039
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001040 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1041 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1042 result = desc;
1043 return result;
1044 }
1045 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001046 next = next + 1;
1047 }
1048
1049 return NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001050
1051}
1052
1053uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1054 unsigned int gpuaddr, unsigned int size)
1055{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001056 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001057
1058 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1059
1060 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061}
1062
1063void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1064 unsigned int *value)
1065{
1066 unsigned int *reg;
1067 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1068 reg = (unsigned int *)(device->regspace.mmio_virt_base
1069 + (offsetwords << 2));
1070
1071 if (!in_interrupt())
1072 kgsl_pre_hwaccess(device);
1073
1074 /*ensure this read finishes before the next one.
1075 * i.e. act like normal readl() */
1076 *value = __raw_readl(reg);
1077 rmb();
1078}
1079
1080void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1081 unsigned int value)
1082{
1083 unsigned int *reg;
1084
1085 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1086
1087 if (!in_interrupt())
1088 kgsl_pre_hwaccess(device);
1089
1090 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1091 reg = (unsigned int *)(device->regspace.mmio_virt_base
1092 + (offsetwords << 2));
1093
1094 /*ensure previous writes post before this one,
1095 * i.e. act like normal writel() */
1096 wmb();
1097 __raw_writel(value, reg);
1098}
1099
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001100static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1101{
1102 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
1103
1104 if (k_ctxt != NULL) {
1105 struct adreno_context *a_ctxt = k_ctxt->devctxt;
1106 /*
1107 * if the context was not created with per context timestamp
1108 * support, we must use the global timestamp since issueibcmds
1109 * will be returning that one.
1110 */
1111 if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
1112 context_id = a_ctxt->id;
1113 }
1114
1115 return context_id;
1116}
1117
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001119 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120{
1121 int status;
1122 unsigned int ref_ts, enableflag;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001123 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001124
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001125 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001126 if (!status) {
1127 mutex_lock(&device->mutex);
1128 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001129 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001130 mb();
1131
1132 if (enableflag) {
1133 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001134 KGSL_MEMSTORE_OFFSET(context_id,
1135 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001136 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001137 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001139 KGSL_MEMSTORE_OFFSET(context_id,
1140 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001141 wmb();
1142 }
1143 } else {
1144 unsigned int cmds[2];
1145 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001146 KGSL_MEMSTORE_OFFSET(context_id,
1147 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148 enableflag = 1;
1149 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001150 KGSL_MEMSTORE_OFFSET(context_id,
1151 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 wmb();
1153 /* submit a dummy packet so that even if all
1154 * commands upto timestamp get executed we will still
1155 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001156 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001158 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1159 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001160 }
1161 mutex_unlock(&device->mutex);
1162 }
1163
1164 return status;
1165}
1166
1167/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001168 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169 placing a process in wait q. For conditional interrupts we expect the
1170 process to already be in its wait q when its exit condition checking
1171 function is called.
1172*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001173#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174({ \
1175 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001176 if (io) \
1177 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1178 else \
1179 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001180 __ret; \
1181})
1182
1183/* MUST be called with the device mutex held */
1184static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001185 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 unsigned int timestamp,
1187 unsigned int msecs)
1188{
1189 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001190 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001191 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001192 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001193 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301194 int retries;
1195 unsigned int msecs_first;
1196 unsigned int msecs_part;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001197 unsigned int ts_issued;
1198 unsigned int context_id = _get_context_id(context);
1199
1200 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301202 /* Don't wait forever, set a max value for now */
1203 if (msecs == -1)
1204 msecs = adreno_dev->wait_timeout;
1205
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001206 if (timestamp_cmp(timestamp, ts_issued) > 0) {
1207 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
1208 "last issued ts <%d:0x%x>\n",
1209 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210 status = -EINVAL;
1211 goto done;
1212 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213
Lynus Vaz06a9a902011-10-04 19:25:33 +05301214 /* Keep the first timeout as 100msecs before rewriting
1215 * the WPTR. Less visible impact if the WPTR has not
1216 * been updated properly.
1217 */
1218 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1219 msecs_part = (msecs - msecs_first + 3) / 4;
1220 for (retries = 0; retries < 5; retries++) {
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001221 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001222 /* if the timestamp happens while we're not
1223 * waiting, there's a chance that an interrupt
1224 * will not be generated and thus the timestamp
1225 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301226 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001227 queue_work(device->work_queue, &device->ts_expired_ws);
1228 status = 0;
1229 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001231 adreno_poke(device);
1232 io_cnt = (io_cnt + 1) % 100;
1233 if (io_cnt <
1234 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1235 io = 0;
1236 mutex_unlock(&device->mutex);
1237 /* We need to make sure that the process is
1238 * placed in wait-q before its condition is called
1239 */
1240 status = kgsl_wait_event_interruptible_timeout(
1241 device->wait_queue,
1242 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001243 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001244 msecs_to_jiffies(retries ?
1245 msecs_part : msecs_first), io);
1246 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001247
Jeremy Gebben63904832012-02-07 16:10:55 -07001248 if (status > 0) {
1249 /*completed before the wait finished */
1250 status = 0;
1251 goto done;
1252 } else if (status < 0) {
1253 /*an error occurred*/
1254 goto done;
1255 }
1256 /*this wait timed out*/
1257 }
1258 status = -ETIMEDOUT;
1259 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001260 "Device hang detected while waiting for timestamp: "
1261 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
1262 "wptr: 0x%x\n",
1263 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07001264 adreno_dev->ringbuffer.wptr);
1265 if (!adreno_dump_and_recover(device)) {
1266 /* wait for idle after recovery as the
1267 * timestamp that this process wanted
1268 * to wait on may be invalid */
1269 if (!adreno_idle(device, KGSL_TIMEOUT_DEFAULT))
1270 status = 0;
1271 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272done:
1273 return (int)status;
1274}
1275
1276static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001277 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278{
1279 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001280 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281
1282 if (type == KGSL_TIMESTAMP_CONSUMED)
1283 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1284 else if (type == KGSL_TIMESTAMP_RETIRED)
1285 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001286 KGSL_MEMSTORE_OFFSET(context_id,
1287 eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288 rmb();
1289
1290 return timestamp;
1291}
1292
1293static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1294 unsigned int cmd, void *data)
1295{
1296 int result = 0;
1297 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1298 struct kgsl_context *context;
1299
1300 switch (cmd) {
1301 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1302 binbase = data;
1303
1304 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1305 if (context) {
1306 adreno_drawctxt_set_bin_base_offset(
1307 dev_priv->device, context, binbase->offset);
1308 } else {
1309 result = -EINVAL;
1310 KGSL_DRV_ERR(dev_priv->device,
1311 "invalid drawctxt drawctxt_id %d "
1312 "device_id=%d\n",
1313 binbase->drawctxt_id, dev_priv->device->id);
1314 }
1315 break;
1316
1317 default:
1318 KGSL_DRV_INFO(dev_priv->device,
1319 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001320 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001321 break;
1322 }
1323 return result;
1324
1325}
1326
1327static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1328{
1329 gpu_freq /= 1000000;
1330 return ticks / gpu_freq;
1331}
1332
1333static void adreno_power_stats(struct kgsl_device *device,
1334 struct kgsl_power_stats *stats)
1335{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001336 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001338 unsigned int cycles;
1339
1340 /* Get the busy cycles counted since the counter was last reset */
1341 /* Calling this function also resets and restarts the counter */
1342
1343 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001344
1345 /* In order to calculate idle you have to have run the algorithm *
1346 * at least once to get a start time. */
1347 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001348 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001349 stats->total_time = tmp - pwr->time;
1350 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001351 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001352 pwrlevels[device->pwrctrl.active_pwrlevel].
1353 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001354 } else {
1355 stats->total_time = 0;
1356 stats->busy_time = 0;
1357 pwr->time = ktime_to_us(ktime_get());
1358 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001359}
1360
1361void adreno_irqctrl(struct kgsl_device *device, int state)
1362{
Jordan Crousea78c9172011-07-11 13:14:09 -06001363 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1364 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365}
1366
Jordan Crousea0758f22011-12-07 11:19:22 -07001367static unsigned int adreno_gpuid(struct kgsl_device *device)
1368{
1369 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1370
1371 /* Standard KGSL gpuid format:
1372 * top word is 0x0002 for 2D or 0x0003 for 3D
1373 * Bottom word is core specific identifer
1374 */
1375
1376 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1377}
1378
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001379static const struct kgsl_functable adreno_functable = {
1380 /* Mandatory functions */
1381 .regread = adreno_regread,
1382 .regwrite = adreno_regwrite,
1383 .idle = adreno_idle,
1384 .isidle = adreno_isidle,
1385 .suspend_context = adreno_suspend_context,
1386 .start = adreno_start,
1387 .stop = adreno_stop,
1388 .getproperty = adreno_getproperty,
1389 .waittimestamp = adreno_waittimestamp,
1390 .readtimestamp = adreno_readtimestamp,
1391 .issueibcmds = adreno_ringbuffer_issueibcmds,
1392 .ioctl = adreno_ioctl,
1393 .setup_pt = adreno_setup_pt,
1394 .cleanup_pt = adreno_cleanup_pt,
1395 .power_stats = adreno_power_stats,
1396 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001397 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001398 .snapshot = adreno_snapshot,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001399 /* Optional functions */
1400 .setstate = adreno_setstate,
1401 .drawctxt_create = adreno_drawctxt_create,
1402 .drawctxt_destroy = adreno_drawctxt_destroy,
1403};
1404
1405static struct platform_device_id adreno_id_table[] = {
1406 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1407 { },
1408};
1409MODULE_DEVICE_TABLE(platform, adreno_id_table);
1410
1411static struct platform_driver adreno_platform_driver = {
1412 .probe = adreno_probe,
1413 .remove = __devexit_p(adreno_remove),
1414 .suspend = kgsl_suspend_driver,
1415 .resume = kgsl_resume_driver,
1416 .id_table = adreno_id_table,
1417 .driver = {
1418 .owner = THIS_MODULE,
1419 .name = DEVICE_3D_NAME,
1420 .pm = &kgsl_pm_ops,
1421 }
1422};
1423
1424static int __init kgsl_3d_init(void)
1425{
1426 return platform_driver_register(&adreno_platform_driver);
1427}
1428
1429static void __exit kgsl_3d_exit(void)
1430{
1431 platform_driver_unregister(&adreno_platform_driver);
1432}
1433
1434module_init(kgsl_3d_init);
1435module_exit(kgsl_3d_exit);
1436
1437MODULE_DESCRIPTION("3D Graphics driver");
1438MODULE_VERSION("1.2");
1439MODULE_LICENSE("GPL v2");
1440MODULE_ALIAS("platform:kgsl_3d");