blob: b44dcccd957901eb92948f06d648efe62770fc96 [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 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116};
117
Jordan Crouse95b33272011-11-11 14:50:12 -0700118
Jordan Crouse505df9c2011-07-28 08:37:59 -0600119/*
120 * This is the master list of all GPU cores that are supported by this
121 * driver.
122 */
123
124#define ANY_ID (~0)
125
126static const struct {
127 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600128 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600129 const char *pm4fw;
130 const char *pfpfw;
131 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700132 unsigned int istore_size;
133 unsigned int pix_shader_start;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600134} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600135 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700136 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
137 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600138 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700139 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
140 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600141 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700142 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
143 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600144 /*
145 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
146 * a hardware problem.
147 */
148 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700149 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
150 1536, 768 },
Carter Cooperf27ec722011-11-17 15:20:38 -0700151 { ADRENO_REV_A225, 2, 2, 0, 6,
152 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
153 1536, 768 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600154 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700155 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
156 1536, 768 },
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700157 { ADRENO_REV_A320, 3, 1, ANY_ID, ANY_ID,
158 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600159};
160
Jordan Crouse9f739212011-07-28 08:37:57 -0600161static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162{
Jordan Crousea78c9172011-07-11 13:14:09 -0600163 irqreturn_t result;
164 struct kgsl_device *device = data;
165 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166
Jordan Crousea78c9172011-07-11 13:14:09 -0600167 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168
169 if (device->requested_state == KGSL_STATE_NONE) {
170 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700171 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172 queue_work(device->work_queue, &device->idle_check_ws);
173 } else if (device->pwrscale.policy != NULL) {
174 queue_work(device->work_queue, &device->idle_check_ws);
175 }
176 }
177
178 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800179 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180 jiffies + device->pwrctrl.interval_timeout);
181 return result;
182}
183
Jordan Crouse9f739212011-07-28 08:37:57 -0600184static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185 struct kgsl_pagetable *pagetable)
186{
187 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
188 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
189
190 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
191
192 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
193
194 kgsl_mmu_unmap(pagetable, &device->memstore);
195
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600196 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197}
198
199static int adreno_setup_pt(struct kgsl_device *device,
200 struct kgsl_pagetable *pagetable)
201{
202 int result = 0;
203 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
204 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
205
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
207 GSL_PT_PAGE_RV);
208 if (result)
209 goto error;
210
211 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
212 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
213 if (result)
214 goto unmap_buffer_desc;
215
216 result = kgsl_mmu_map_global(pagetable, &device->memstore,
217 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
218 if (result)
219 goto unmap_memptrs_desc;
220
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600221 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
223 if (result)
224 goto unmap_memstore_desc;
225
226 return result;
227
228unmap_memstore_desc:
229 kgsl_mmu_unmap(pagetable, &device->memstore);
230
231unmap_memptrs_desc:
232 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
233
234unmap_buffer_desc:
235 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
236
237error:
238 return result;
239}
240
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600241static void adreno_setstate(struct kgsl_device *device,
242 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243{
244 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
245 unsigned int link[32];
246 unsigned int *cmds = &link[0];
247 int sizedwords = 0;
248 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
249
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600250 /*
251 * If possible, then set the state via the command stream to avoid
252 * a CPU idle. Otherwise, use the default setstate which uses register
253 * writes For CFF dump we must idle and use the registers so that it is
254 * easier to filter out the mmu accesses from the dump
255 */
256 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
258 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600259 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 *cmds++ = 0x00000000;
261
262 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600263 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600264 *cmds++ = kgsl_pt_get_base_addr(
265 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 sizedwords += 4;
267 }
268
269 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
270 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600271 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 1);
273 *cmds++ = 0x00000000;
274 sizedwords += 2;
275 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600276 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277 *cmds++ = mh_mmu_invalidate;
278 sizedwords += 2;
279 }
280
281 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600282 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 /* HW workaround: to resolve MMU page fault interrupts
284 * caused by the VGT.It prevents the CP PFP from filling
285 * the VGT DMA request fifo too early,thereby ensuring
286 * that the VGT will not fetch vertex/bin data until
287 * after the page table base register has been updated.
288 *
289 * Two null DRAW_INDX_BIN packets are inserted right
290 * after the page table base update, followed by a
291 * wait for idle. The null packets will fill up the
292 * VGT DMA request fifo and prevent any further
293 * vertex/bin updates from occurring until the wait
294 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600295 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296 *cmds++ = (0x4 << 16) |
297 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
298 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600299 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600300 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600301 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 *cmds++ = 0; /* viz query info */
303 *cmds++ = 0x0003C004; /* draw indicator */
304 *cmds++ = 0; /* bin base */
305 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600306 *cmds++ =
307 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600309 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 *cmds++ = 0; /* viz query info */
311 *cmds++ = 0x0003C004; /* draw indicator */
312 *cmds++ = 0; /* bin base */
313 *cmds++ = 3; /* bin size */
314 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600315 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600317 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318 *cmds++ = 0x00000000;
319 sizedwords += 21;
320 }
321
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600322
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600324 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 *cmds++ = 0x7fff; /* invalidate all base pointers */
326 sizedwords += 2;
327 }
328
329 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
330 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600331 } else {
332 kgsl_mmu_device_setstate(device, flags);
333 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334}
335
336static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700337a3xx_getchipid(struct kgsl_device *device)
338{
339 unsigned int chipid = 0;
340 unsigned int coreid, majorid, minorid, patchid;
341 unsigned int version;
342
343 adreno_regread(device, A3XX_RBBM_HW_VERSION, &version);
344
345 coreid = 0x03;
346
347 /* Version might not be set - if it isn't, assume this is 320 */
348 if (version)
349 majorid = version & 0x0F;
350 else
351 majorid = 1;
352
353 minorid = (version >> 4) & 0xFFF;
354 patchid = 0;
355
356 chipid = (coreid << 24) | (majorid << 16) | (minorid << 8) | patchid;
357
358 return chipid;
359}
360
361static unsigned int
362a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363{
364 unsigned int chipid = 0;
365 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700366 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367
368 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
369 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
370 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
371
372 /*
373 * adreno 22x gpus are indicated by coreid 2,
374 * but REG_RBBM_PERIPHID1 always contains 0 for this field
375 */
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700376 if (cpu_is_msm8960() || cpu_is_msm8x60() || cpu_is_msm8930())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377 chipid = 2 << 24;
378 else
379 chipid = (coreid & 0xF) << 24;
380
381 chipid |= ((majorid >> 4) & 0xF) << 16;
382
383 minorid = ((revid >> 0) & 0xFF);
384
385 patchid = ((revid >> 16) & 0xFF);
386
387 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700388 /* 8960v3 returns 5 for patch release, but it should be 6 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389 if (cpu_is_qsd8x50())
390 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700391 else if (cpu_is_msm8960() &&
392 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
393 patchid = 6;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394
395 chipid |= (minorid << 8) | patchid;
396
397 return chipid;
398}
399
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700400static unsigned int
401adreno_getchipid(struct kgsl_device *device)
402{
403 if (cpu_is_apq8064())
404 return a3xx_getchipid(device);
405 else
406 return a2xx_getchipid(device);
407}
408
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409static inline bool _rev_match(unsigned int id, unsigned int entry)
410{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600411 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700412}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413
414static void
415adreno_identify_gpu(struct adreno_device *adreno_dev)
416{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600417 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700418
419 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
420
421 core = (adreno_dev->chip_id >> 24) & 0xff;
422 major = (adreno_dev->chip_id >> 16) & 0xff;
423 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600424 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425
Jordan Crouse505df9c2011-07-28 08:37:59 -0600426 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
427 if (core == adreno_gpulist[i].core &&
428 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600429 _rev_match(minor, adreno_gpulist[i].minor) &&
430 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432 }
433
Jordan Crouse505df9c2011-07-28 08:37:59 -0600434 if (i == ARRAY_SIZE(adreno_gpulist)) {
435 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
436 return;
437 }
438
439 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
440 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
441 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
442 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700443 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
444 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445}
446
447static int __devinit
448adreno_probe(struct platform_device *pdev)
449{
450 struct kgsl_device *device;
451 struct adreno_device *adreno_dev;
452 int status = -EINVAL;
453
454 device = (struct kgsl_device *)pdev->id_entry->driver_data;
455 adreno_dev = ADRENO_DEVICE(device);
456 device->parentdev = &pdev->dev;
457
458 init_completion(&device->recovery_gate);
459
460 status = adreno_ringbuffer_init(device);
461 if (status != 0)
462 goto error;
463
464 status = kgsl_device_platform_probe(device, adreno_isr);
465 if (status)
466 goto error_close_rb;
467
468 adreno_debugfs_init(device);
469
470 kgsl_pwrscale_init(device);
471 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
472
473 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
474 return 0;
475
476error_close_rb:
477 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
478error:
479 device->parentdev = NULL;
480 return status;
481}
482
483static int __devexit adreno_remove(struct platform_device *pdev)
484{
485 struct kgsl_device *device;
486 struct adreno_device *adreno_dev;
487
488 device = (struct kgsl_device *)pdev->id_entry->driver_data;
489 adreno_dev = ADRENO_DEVICE(device);
490
491 kgsl_pwrscale_detach_policy(device);
492 kgsl_pwrscale_close(device);
493
494 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
495 kgsl_device_platform_remove(device);
496
497 return 0;
498}
499
500static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
501{
502 int status = -EINVAL;
503 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504
Jeremy Gebben388c2972011-12-16 09:05:07 -0700505 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506
507 /* Power up the device */
508 kgsl_pwrctrl_enable(device);
509
510 /* Identify the specific GPU */
511 adreno_identify_gpu(adreno_dev);
512
Jordan Crouse505df9c2011-07-28 08:37:59 -0600513 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
514 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
515 adreno_dev->chip_id);
516 goto error_clk_off;
517 }
518
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700519 /* Set up the MMU */
520 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600521 /*
522 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
523 * on older gpus
524 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700525 if (adreno_is_a20x(adreno_dev)) {
526 device->mh.mh_intf_cfg1 = 0;
527 device->mh.mh_intf_cfg2 = 0;
528 }
529
530 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600531 }
532
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700533 status = kgsl_mmu_start(device);
534 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 goto error_clk_off;
536
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700537 /* Start the GPU */
538 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539
540 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700541 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542
543 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700544 if (status == 0) {
545 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
546 return 0;
547 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600550 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551error_clk_off:
552 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553
554 return status;
555}
556
557static int adreno_stop(struct kgsl_device *device)
558{
559 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
560
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 adreno_dev->drawctxt_active = NULL;
562
563 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
564
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 kgsl_mmu_stop(device);
566
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700567 device->ftbl->irqctrl(device, 0);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600568
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569 /* Power down the device */
570 kgsl_pwrctrl_disable(device);
571
572 return 0;
573}
574
575static int
576adreno_recover_hang(struct kgsl_device *device)
577{
578 int ret;
579 unsigned int *rb_buffer;
580 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
581 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
582 unsigned int timestamp;
583 unsigned int num_rb_contents;
584 unsigned int bad_context;
585 unsigned int reftimestamp;
586 unsigned int enable_ts;
587 unsigned int soptimestamp;
588 unsigned int eoptimestamp;
589 struct adreno_context *drawctxt;
590
591 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
592 rb_buffer = vmalloc(rb->buffer_desc.size);
593 if (!rb_buffer) {
594 KGSL_MEM_ERR(device,
595 "Failed to allocate memory for recovery: %x\n",
596 rb->buffer_desc.size);
597 return -ENOMEM;
598 }
599 /* Extract valid contents from rb which can stil be executed after
600 * hang */
601 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
602 if (ret)
603 goto done;
604 timestamp = rb->timestamp;
605 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
606 kgsl_sharedmem_readl(&device->memstore, &bad_context,
607 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
608 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
609 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
610 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
611 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
612 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
613 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
614 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
615 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
616 /* Make sure memory is synchronized before restarting the GPU */
617 mb();
618 KGSL_CTXT_ERR(device,
619 "Context that caused a GPU hang: %x\n", bad_context);
620 /* restart device */
621 ret = adreno_stop(device);
622 if (ret)
623 goto done;
624 ret = adreno_start(device, true);
625 if (ret)
626 goto done;
627 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
628 /* Restore timestamp states */
629 kgsl_sharedmem_writel(&device->memstore,
630 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
631 soptimestamp);
632 kgsl_sharedmem_writel(&device->memstore,
633 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
634 eoptimestamp);
635 kgsl_sharedmem_writel(&device->memstore,
636 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
637 soptimestamp);
638 if (num_rb_contents) {
639 kgsl_sharedmem_writel(&device->memstore,
640 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
641 reftimestamp);
642 kgsl_sharedmem_writel(&device->memstore,
643 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
644 enable_ts);
645 }
646 /* Make sure all writes are posted before the GPU reads them */
647 wmb();
648 /* Mark the invalid context so no more commands are accepted from
649 * that context */
650
651 drawctxt = (struct adreno_context *) bad_context;
652
653 KGSL_CTXT_ERR(device,
654 "Context that caused a GPU hang: %x\n", bad_context);
655
656 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
657
658 /* Restore valid commands in ringbuffer */
659 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
660 rb->timestamp = timestamp;
661done:
662 vfree(rb_buffer);
663 return ret;
664}
665
666static int
667adreno_dump_and_recover(struct kgsl_device *device)
668{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 int result = -ETIMEDOUT;
670
671 if (device->state == KGSL_STATE_HUNG)
672 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700673 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 mutex_unlock(&device->mutex);
675 wait_for_completion(&device->recovery_gate);
676 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700677 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678 result = 0;
679 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700680 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700682 /* Detected a hang */
683
684
685 /*
686 * Trigger an automatic dump of the state to
687 * the console
688 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700690
691 /*
692 * Make a GPU snapshot. For now, do it after the PM dump so we
693 * can at least be sure the PM dump will work as it always has
694 */
695 kgsl_device_snapshot(device, 1);
696
Jeremy Gebben388c2972011-12-16 09:05:07 -0700697 result = adreno_recover_hang(device);
698 if (result)
699 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
700 else
701 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
702 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 }
704done:
705 return result;
706}
707
708static int adreno_getproperty(struct kgsl_device *device,
709 enum kgsl_property_type type,
710 void *value,
711 unsigned int sizebytes)
712{
713 int status = -EINVAL;
714 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
715
716 switch (type) {
717 case KGSL_PROP_DEVICE_INFO:
718 {
719 struct kgsl_devinfo devinfo;
720
721 if (sizebytes != sizeof(devinfo)) {
722 status = -EINVAL;
723 break;
724 }
725
726 memset(&devinfo, 0, sizeof(devinfo));
727 devinfo.device_id = device->id+1;
728 devinfo.chip_id = adreno_dev->chip_id;
729 devinfo.mmu_enabled = kgsl_mmu_enabled();
730 devinfo.gpu_id = adreno_dev->gpurev;
731 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
732 gpu_base;
733 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
734 sizebytes;
735
736 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
737 0) {
738 status = -EFAULT;
739 break;
740 }
741 status = 0;
742 }
743 break;
744 case KGSL_PROP_DEVICE_SHADOW:
745 {
746 struct kgsl_shadowprop shadowprop;
747
748 if (sizebytes != sizeof(shadowprop)) {
749 status = -EINVAL;
750 break;
751 }
752 memset(&shadowprop, 0, sizeof(shadowprop));
753 if (device->memstore.hostptr) {
754 /*NOTE: with mmu enabled, gpuaddr doesn't mean
755 * anything to mmap().
756 */
757 shadowprop.gpuaddr = device->memstore.physaddr;
758 shadowprop.size = device->memstore.size;
759 /* GSL needs this to be set, even if it
760 appears to be meaningless */
761 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
762 }
763 if (copy_to_user(value, &shadowprop,
764 sizeof(shadowprop))) {
765 status = -EFAULT;
766 break;
767 }
768 status = 0;
769 }
770 break;
771 case KGSL_PROP_MMU_ENABLE:
772 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600773 int mmu_prop = kgsl_mmu_enabled();
774
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 if (sizebytes != sizeof(int)) {
776 status = -EINVAL;
777 break;
778 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600779 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 status = -EFAULT;
781 break;
782 }
783 status = 0;
784 }
785 break;
786 case KGSL_PROP_INTERRUPT_WAITS:
787 {
788 int int_waits = 1;
789 if (sizebytes != sizeof(int)) {
790 status = -EINVAL;
791 break;
792 }
793 if (copy_to_user(value, &int_waits, sizeof(int))) {
794 status = -EFAULT;
795 break;
796 }
797 status = 0;
798 }
799 break;
800 default:
801 status = -EINVAL;
802 }
803
804 return status;
805}
806
Lynus Vaz06a9a902011-10-04 19:25:33 +0530807static inline void adreno_poke(struct kgsl_device *device)
808{
809 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
810 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
811}
812
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813/* Caller must hold the device mutex. */
814int adreno_idle(struct kgsl_device *device, unsigned int timeout)
815{
816 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
817 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
818 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530819 unsigned long wait_timeout =
820 msecs_to_jiffies(adreno_dev->wait_timeout);
821 unsigned long wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700823 kgsl_cffdump_regpoll(device->id,
824 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 0x00000000, 0x80000000);
826 /* first, wait until the CP has consumed all the commands in
827 * the ring buffer
828 */
829retry:
830 if (rb->flags & KGSL_FLAGS_STARTED) {
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700831 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 do {
833 GSL_RB_GET_READPTR(rb, &rb->rptr);
834 if (time_after(jiffies, wait_time)) {
835 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
836 rb->rptr, rb->wptr);
837 goto err;
838 }
839 } while (rb->rptr != rb->wptr);
840 }
841
842 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530843 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700845 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
846 &rbbm_status);
847 if (adreno_is_a2xx(adreno_dev)) {
848 if (rbbm_status == 0x110)
849 return 0;
850 } else {
851 if (!(rbbm_status & 0x80000000))
852 return 0;
853 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 }
855
856err:
857 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
858 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530859 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 goto retry;
861 }
862 return -ETIMEDOUT;
863}
864
865static unsigned int adreno_isidle(struct kgsl_device *device)
866{
867 int status = false;
868 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
869 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
870 unsigned int rbbm_status;
871
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700872 WARN_ON(device->state == KGSL_STATE_INIT);
873 /* If the device isn't active, don't force it on. */
874 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875 /* Is the ring buffer is empty? */
876 GSL_RB_GET_READPTR(rb, &rb->rptr);
877 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
878 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700879 adreno_regread(device,
880 adreno_dev->gpudev->reg_rbbm_status,
881 &rbbm_status);
882
883 if (adreno_is_a2xx(adreno_dev)) {
884 if (rbbm_status == 0x110)
885 status = true;
886 } else {
887 if (!(rbbm_status & 0x80000000))
888 status = true;
889 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 }
891 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -0700892 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 }
894 return status;
895}
896
897/* Caller must hold the device mutex. */
898static int adreno_suspend_context(struct kgsl_device *device)
899{
900 int status = 0;
901 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
902
903 /* switch to NULL ctxt */
904 if (adreno_dev->drawctxt_active != NULL) {
905 adreno_drawctxt_switch(adreno_dev, NULL, 0);
906 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
907 }
908
909 return status;
910}
911
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700912const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
913 unsigned int pt_base,
914 unsigned int gpuaddr,
915 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916{
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700917 struct kgsl_memdesc *result = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918 struct kgsl_mem_entry *entry;
919 struct kgsl_process_private *priv;
920 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
921 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700922 struct kgsl_context *context;
923 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700925 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
926 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700928 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
929 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700931 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
932 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933
934 mutex_lock(&kgsl_driver.process_mutex);
935 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600936 if (!kgsl_mmu_pt_equal(priv->pagetable, pt_base))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938 spin_lock(&priv->mem_lock);
939 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
940 sizeof(unsigned int));
941 if (entry) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700942 result = &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943 spin_unlock(&priv->mem_lock);
944 mutex_unlock(&kgsl_driver.process_mutex);
945 return result;
946 }
947 spin_unlock(&priv->mem_lock);
948 }
949 mutex_unlock(&kgsl_driver.process_mutex);
950
951 BUG_ON(!mutex_is_locked(&device->mutex));
952 list_for_each_entry(entry, &device->memqueue, list) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700953 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) {
954 result = &entry->memdesc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700955 return result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956 }
957
958 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700959
960 while (1) {
961 struct adreno_context *adreno_context = NULL;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700962 context = idr_get_next(&device->context_idr, &next);
963 if (context == NULL)
964 break;
965
966 adreno_context = (struct adreno_context *)context->devctxt;
967
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700968 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
969 struct kgsl_memdesc *desc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700970
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700971 desc = &adreno_context->gpustate;
972 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
973 result = desc;
974 return result;
975 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700976
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700977 desc = &adreno_context->context_gmem_shadow.gmemshadow;
978 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
979 result = desc;
980 return result;
981 }
982 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700983 next = next + 1;
984 }
985
986 return NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700987
988}
989
990uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
991 unsigned int gpuaddr, unsigned int size)
992{
993 const struct kgsl_memdesc *memdesc;
994
995 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
996
997 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998}
999
1000void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1001 unsigned int *value)
1002{
1003 unsigned int *reg;
1004 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1005 reg = (unsigned int *)(device->regspace.mmio_virt_base
1006 + (offsetwords << 2));
1007
1008 if (!in_interrupt())
1009 kgsl_pre_hwaccess(device);
1010
1011 /*ensure this read finishes before the next one.
1012 * i.e. act like normal readl() */
1013 *value = __raw_readl(reg);
1014 rmb();
1015}
1016
1017void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1018 unsigned int value)
1019{
1020 unsigned int *reg;
1021
1022 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1023
1024 if (!in_interrupt())
1025 kgsl_pre_hwaccess(device);
1026
1027 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1028 reg = (unsigned int *)(device->regspace.mmio_virt_base
1029 + (offsetwords << 2));
1030
1031 /*ensure previous writes post before this one,
1032 * i.e. act like normal writel() */
1033 wmb();
1034 __raw_writel(value, reg);
1035}
1036
1037static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1038 unsigned int timestamp)
1039{
1040 int status;
1041 unsigned int ref_ts, enableflag;
1042
1043 status = kgsl_check_timestamp(device, timestamp);
1044 if (!status) {
1045 mutex_lock(&device->mutex);
1046 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1047 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1048 mb();
1049
1050 if (enableflag) {
1051 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1052 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1053 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001054 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001055 kgsl_sharedmem_writel(&device->memstore,
1056 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1057 timestamp);
1058 wmb();
1059 }
1060 } else {
1061 unsigned int cmds[2];
1062 kgsl_sharedmem_writel(&device->memstore,
1063 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1064 timestamp);
1065 enableflag = 1;
1066 kgsl_sharedmem_writel(&device->memstore,
1067 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1068 enableflag);
1069 wmb();
1070 /* submit a dummy packet so that even if all
1071 * commands upto timestamp get executed we will still
1072 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001073 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001075 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1076 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077 }
1078 mutex_unlock(&device->mutex);
1079 }
1080
1081 return status;
1082}
1083
1084/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001085 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086 placing a process in wait q. For conditional interrupts we expect the
1087 process to already be in its wait q when its exit condition checking
1088 function is called.
1089*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001090#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091({ \
1092 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001093 if (io) \
1094 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1095 else \
1096 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 __ret; \
1098})
1099
1100/* MUST be called with the device mutex held */
1101static int adreno_waittimestamp(struct kgsl_device *device,
1102 unsigned int timestamp,
1103 unsigned int msecs)
1104{
1105 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001106 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001107 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001109 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301110 int retries;
1111 unsigned int msecs_first;
1112 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301114 /* Don't wait forever, set a max value for now */
1115 if (msecs == -1)
1116 msecs = adreno_dev->wait_timeout;
1117
Jordan Crousee6239dd2011-11-17 13:39:21 -07001118 if (timestamp_cmp(timestamp, adreno_dev->ringbuffer.timestamp) > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1120 "rb->timestamp: %x\n",
1121 timestamp, adreno_dev->ringbuffer.timestamp);
1122 status = -EINVAL;
1123 goto done;
1124 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125
Lynus Vaz06a9a902011-10-04 19:25:33 +05301126 /* Keep the first timeout as 100msecs before rewriting
1127 * the WPTR. Less visible impact if the WPTR has not
1128 * been updated properly.
1129 */
1130 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1131 msecs_part = (msecs - msecs_first + 3) / 4;
1132 for (retries = 0; retries < 5; retries++) {
1133 if (!kgsl_check_timestamp(device, timestamp)) {
1134 adreno_poke(device);
1135 io_cnt = (io_cnt + 1) % 100;
1136 if (io_cnt <
1137 pwr->pwrlevels[pwr->active_pwrlevel].
1138 io_fraction)
1139 io = 0;
1140 mutex_unlock(&device->mutex);
1141 /* We need to make sure that the process is
1142 * placed in wait-q before its condition is called
1143 */
1144 status = kgsl_wait_event_interruptible_timeout(
1145 device->wait_queue,
1146 kgsl_check_interrupt_timestamp(device,
1147 timestamp),
1148 msecs_to_jiffies(retries ?
1149 msecs_part : msecs_first), io);
1150 mutex_lock(&device->mutex);
1151
1152 if (status > 0) {
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001153 /*completed before the wait finished */
Lynus Vaz06a9a902011-10-04 19:25:33 +05301154 status = 0;
1155 goto done;
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001156 } else if (status < 0) {
1157 /*an error occurred*/
1158 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159 }
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001160 /*this wait timed out*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161 }
1162 }
Lynus Vaz06a9a902011-10-04 19:25:33 +05301163 if (!kgsl_check_timestamp(device, timestamp)) {
1164 status = -ETIMEDOUT;
1165 KGSL_DRV_ERR(device,
1166 "Device hang detected while waiting "
1167 "for timestamp: %x, last "
1168 "submitted(rb->timestamp): %x, wptr: "
1169 "%x\n", timestamp,
1170 adreno_dev->ringbuffer.timestamp,
1171 adreno_dev->ringbuffer.wptr);
1172 if (!adreno_dump_and_recover(device)) {
1173 /* wait for idle after recovery as the
1174 * timestamp that this process wanted
1175 * to wait on may be invalid */
1176 if (!adreno_idle(device,
1177 KGSL_TIMEOUT_DEFAULT))
1178 status = 0;
1179 }
1180 } else {
1181 status = 0;
1182 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183
1184done:
1185 return (int)status;
1186}
1187
1188static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1189 enum kgsl_timestamp_type type)
1190{
1191 unsigned int timestamp = 0;
1192
1193 if (type == KGSL_TIMESTAMP_CONSUMED)
1194 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1195 else if (type == KGSL_TIMESTAMP_RETIRED)
1196 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1197 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1198 rmb();
1199
1200 return timestamp;
1201}
1202
1203static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1204 unsigned int cmd, void *data)
1205{
1206 int result = 0;
1207 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1208 struct kgsl_context *context;
1209
1210 switch (cmd) {
1211 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1212 binbase = data;
1213
1214 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1215 if (context) {
1216 adreno_drawctxt_set_bin_base_offset(
1217 dev_priv->device, context, binbase->offset);
1218 } else {
1219 result = -EINVAL;
1220 KGSL_DRV_ERR(dev_priv->device,
1221 "invalid drawctxt drawctxt_id %d "
1222 "device_id=%d\n",
1223 binbase->drawctxt_id, dev_priv->device->id);
1224 }
1225 break;
1226
1227 default:
1228 KGSL_DRV_INFO(dev_priv->device,
1229 "invalid ioctl code %08x\n", cmd);
1230 result = -EINVAL;
1231 break;
1232 }
1233 return result;
1234
1235}
1236
1237static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1238{
1239 gpu_freq /= 1000000;
1240 return ticks / gpu_freq;
1241}
1242
1243static void adreno_power_stats(struct kgsl_device *device,
1244 struct kgsl_power_stats *stats)
1245{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001246 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001247 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001248 unsigned int cycles;
1249
1250 /* Get the busy cycles counted since the counter was last reset */
1251 /* Calling this function also resets and restarts the counter */
1252
1253 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001254
1255 /* In order to calculate idle you have to have run the algorithm *
1256 * at least once to get a start time. */
1257 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001258 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 stats->total_time = tmp - pwr->time;
1260 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001261 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001262 pwrlevels[device->pwrctrl.active_pwrlevel].
1263 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001264 } else {
1265 stats->total_time = 0;
1266 stats->busy_time = 0;
1267 pwr->time = ktime_to_us(ktime_get());
1268 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269}
1270
1271void adreno_irqctrl(struct kgsl_device *device, int state)
1272{
Jordan Crousea78c9172011-07-11 13:14:09 -06001273 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1274 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001275}
1276
Jordan Crousea0758f22011-12-07 11:19:22 -07001277static unsigned int adreno_gpuid(struct kgsl_device *device)
1278{
1279 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1280
1281 /* Standard KGSL gpuid format:
1282 * top word is 0x0002 for 2D or 0x0003 for 3D
1283 * Bottom word is core specific identifer
1284 */
1285
1286 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1287}
1288
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289static const struct kgsl_functable adreno_functable = {
1290 /* Mandatory functions */
1291 .regread = adreno_regread,
1292 .regwrite = adreno_regwrite,
1293 .idle = adreno_idle,
1294 .isidle = adreno_isidle,
1295 .suspend_context = adreno_suspend_context,
1296 .start = adreno_start,
1297 .stop = adreno_stop,
1298 .getproperty = adreno_getproperty,
1299 .waittimestamp = adreno_waittimestamp,
1300 .readtimestamp = adreno_readtimestamp,
1301 .issueibcmds = adreno_ringbuffer_issueibcmds,
1302 .ioctl = adreno_ioctl,
1303 .setup_pt = adreno_setup_pt,
1304 .cleanup_pt = adreno_cleanup_pt,
1305 .power_stats = adreno_power_stats,
1306 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001307 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001308 .snapshot = adreno_snapshot,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309 /* Optional functions */
1310 .setstate = adreno_setstate,
1311 .drawctxt_create = adreno_drawctxt_create,
1312 .drawctxt_destroy = adreno_drawctxt_destroy,
1313};
1314
1315static struct platform_device_id adreno_id_table[] = {
1316 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1317 { },
1318};
1319MODULE_DEVICE_TABLE(platform, adreno_id_table);
1320
1321static struct platform_driver adreno_platform_driver = {
1322 .probe = adreno_probe,
1323 .remove = __devexit_p(adreno_remove),
1324 .suspend = kgsl_suspend_driver,
1325 .resume = kgsl_resume_driver,
1326 .id_table = adreno_id_table,
1327 .driver = {
1328 .owner = THIS_MODULE,
1329 .name = DEVICE_3D_NAME,
1330 .pm = &kgsl_pm_ops,
1331 }
1332};
1333
1334static int __init kgsl_3d_init(void)
1335{
1336 return platform_driver_register(&adreno_platform_driver);
1337}
1338
1339static void __exit kgsl_3d_exit(void)
1340{
1341 platform_driver_unregister(&adreno_platform_driver);
1342}
1343
1344module_init(kgsl_3d_init);
1345module_exit(kgsl_3d_exit);
1346
1347MODULE_DESCRIPTION("3D Graphics driver");
1348MODULE_VERSION("1.2");
1349MODULE_LICENSE("GPL v2");
1350MODULE_ALIAS("platform:kgsl_3d");