blob: 805e2b687e2a4687302917a3e6c5984882cf58f8 [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);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530568 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600569
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 /* Power down the device */
571 kgsl_pwrctrl_disable(device);
572
573 return 0;
574}
575
576static int
577adreno_recover_hang(struct kgsl_device *device)
578{
579 int ret;
580 unsigned int *rb_buffer;
581 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
582 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
583 unsigned int timestamp;
584 unsigned int num_rb_contents;
585 unsigned int bad_context;
586 unsigned int reftimestamp;
587 unsigned int enable_ts;
588 unsigned int soptimestamp;
589 unsigned int eoptimestamp;
590 struct adreno_context *drawctxt;
591
592 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
593 rb_buffer = vmalloc(rb->buffer_desc.size);
594 if (!rb_buffer) {
595 KGSL_MEM_ERR(device,
596 "Failed to allocate memory for recovery: %x\n",
597 rb->buffer_desc.size);
598 return -ENOMEM;
599 }
600 /* Extract valid contents from rb which can stil be executed after
601 * hang */
602 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
603 if (ret)
604 goto done;
605 timestamp = rb->timestamp;
606 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
607 kgsl_sharedmem_readl(&device->memstore, &bad_context,
608 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
609 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
610 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
611 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
612 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
613 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
614 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
615 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
616 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
617 /* Make sure memory is synchronized before restarting the GPU */
618 mb();
619 KGSL_CTXT_ERR(device,
620 "Context that caused a GPU hang: %x\n", bad_context);
621 /* restart device */
622 ret = adreno_stop(device);
623 if (ret)
624 goto done;
625 ret = adreno_start(device, true);
626 if (ret)
627 goto done;
628 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
629 /* Restore timestamp states */
630 kgsl_sharedmem_writel(&device->memstore,
631 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
632 soptimestamp);
633 kgsl_sharedmem_writel(&device->memstore,
634 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
635 eoptimestamp);
636 kgsl_sharedmem_writel(&device->memstore,
637 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
638 soptimestamp);
639 if (num_rb_contents) {
640 kgsl_sharedmem_writel(&device->memstore,
641 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
642 reftimestamp);
643 kgsl_sharedmem_writel(&device->memstore,
644 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
645 enable_ts);
646 }
647 /* Make sure all writes are posted before the GPU reads them */
648 wmb();
649 /* Mark the invalid context so no more commands are accepted from
650 * that context */
651
652 drawctxt = (struct adreno_context *) bad_context;
653
654 KGSL_CTXT_ERR(device,
655 "Context that caused a GPU hang: %x\n", bad_context);
656
657 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
658
659 /* Restore valid commands in ringbuffer */
660 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
661 rb->timestamp = timestamp;
662done:
663 vfree(rb_buffer);
664 return ret;
665}
666
667static int
668adreno_dump_and_recover(struct kgsl_device *device)
669{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 int result = -ETIMEDOUT;
671
672 if (device->state == KGSL_STATE_HUNG)
673 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700674 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 mutex_unlock(&device->mutex);
676 wait_for_completion(&device->recovery_gate);
677 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700678 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 result = 0;
680 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700681 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700683 /* Detected a hang */
684
685
686 /*
687 * Trigger an automatic dump of the state to
688 * the console
689 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700691
692 /*
693 * Make a GPU snapshot. For now, do it after the PM dump so we
694 * can at least be sure the PM dump will work as it always has
695 */
696 kgsl_device_snapshot(device, 1);
697
Jeremy Gebben388c2972011-12-16 09:05:07 -0700698 result = adreno_recover_hang(device);
699 if (result)
700 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
701 else
702 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
703 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700704 }
705done:
706 return result;
707}
708
709static int adreno_getproperty(struct kgsl_device *device,
710 enum kgsl_property_type type,
711 void *value,
712 unsigned int sizebytes)
713{
714 int status = -EINVAL;
715 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
716
717 switch (type) {
718 case KGSL_PROP_DEVICE_INFO:
719 {
720 struct kgsl_devinfo devinfo;
721
722 if (sizebytes != sizeof(devinfo)) {
723 status = -EINVAL;
724 break;
725 }
726
727 memset(&devinfo, 0, sizeof(devinfo));
728 devinfo.device_id = device->id+1;
729 devinfo.chip_id = adreno_dev->chip_id;
730 devinfo.mmu_enabled = kgsl_mmu_enabled();
731 devinfo.gpu_id = adreno_dev->gpurev;
732 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
733 gpu_base;
734 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
735 sizebytes;
736
737 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
738 0) {
739 status = -EFAULT;
740 break;
741 }
742 status = 0;
743 }
744 break;
745 case KGSL_PROP_DEVICE_SHADOW:
746 {
747 struct kgsl_shadowprop shadowprop;
748
749 if (sizebytes != sizeof(shadowprop)) {
750 status = -EINVAL;
751 break;
752 }
753 memset(&shadowprop, 0, sizeof(shadowprop));
754 if (device->memstore.hostptr) {
755 /*NOTE: with mmu enabled, gpuaddr doesn't mean
756 * anything to mmap().
757 */
758 shadowprop.gpuaddr = device->memstore.physaddr;
759 shadowprop.size = device->memstore.size;
760 /* GSL needs this to be set, even if it
761 appears to be meaningless */
762 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
763 }
764 if (copy_to_user(value, &shadowprop,
765 sizeof(shadowprop))) {
766 status = -EFAULT;
767 break;
768 }
769 status = 0;
770 }
771 break;
772 case KGSL_PROP_MMU_ENABLE:
773 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600774 int mmu_prop = kgsl_mmu_enabled();
775
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 if (sizebytes != sizeof(int)) {
777 status = -EINVAL;
778 break;
779 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600780 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 status = -EFAULT;
782 break;
783 }
784 status = 0;
785 }
786 break;
787 case KGSL_PROP_INTERRUPT_WAITS:
788 {
789 int int_waits = 1;
790 if (sizebytes != sizeof(int)) {
791 status = -EINVAL;
792 break;
793 }
794 if (copy_to_user(value, &int_waits, sizeof(int))) {
795 status = -EFAULT;
796 break;
797 }
798 status = 0;
799 }
800 break;
801 default:
802 status = -EINVAL;
803 }
804
805 return status;
806}
807
Lynus Vaz06a9a902011-10-04 19:25:33 +0530808static inline void adreno_poke(struct kgsl_device *device)
809{
810 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
811 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
812}
813
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814/* Caller must hold the device mutex. */
815int adreno_idle(struct kgsl_device *device, unsigned int timeout)
816{
817 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
818 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
819 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530820 unsigned long wait_timeout =
821 msecs_to_jiffies(adreno_dev->wait_timeout);
822 unsigned long wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700824 kgsl_cffdump_regpoll(device->id,
825 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826 0x00000000, 0x80000000);
827 /* first, wait until the CP has consumed all the commands in
828 * the ring buffer
829 */
830retry:
831 if (rb->flags & KGSL_FLAGS_STARTED) {
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700832 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 do {
834 GSL_RB_GET_READPTR(rb, &rb->rptr);
835 if (time_after(jiffies, wait_time)) {
836 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
837 rb->rptr, rb->wptr);
838 goto err;
839 }
840 } while (rb->rptr != rb->wptr);
841 }
842
843 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530844 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700846 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
847 &rbbm_status);
848 if (adreno_is_a2xx(adreno_dev)) {
849 if (rbbm_status == 0x110)
850 return 0;
851 } else {
852 if (!(rbbm_status & 0x80000000))
853 return 0;
854 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 }
856
857err:
858 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
859 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530860 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861 goto retry;
862 }
863 return -ETIMEDOUT;
864}
865
866static unsigned int adreno_isidle(struct kgsl_device *device)
867{
868 int status = false;
869 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
870 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
871 unsigned int rbbm_status;
872
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700873 WARN_ON(device->state == KGSL_STATE_INIT);
874 /* If the device isn't active, don't force it on. */
875 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 /* Is the ring buffer is empty? */
877 GSL_RB_GET_READPTR(rb, &rb->rptr);
878 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
879 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700880 adreno_regread(device,
881 adreno_dev->gpudev->reg_rbbm_status,
882 &rbbm_status);
883
884 if (adreno_is_a2xx(adreno_dev)) {
885 if (rbbm_status == 0x110)
886 status = true;
887 } else {
888 if (!(rbbm_status & 0x80000000))
889 status = true;
890 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700891 }
892 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -0700893 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894 }
895 return status;
896}
897
898/* Caller must hold the device mutex. */
899static int adreno_suspend_context(struct kgsl_device *device)
900{
901 int status = 0;
902 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
903
904 /* switch to NULL ctxt */
905 if (adreno_dev->drawctxt_active != NULL) {
906 adreno_drawctxt_switch(adreno_dev, NULL, 0);
907 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
908 }
909
910 return status;
911}
912
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700913const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
914 unsigned int pt_base,
915 unsigned int gpuaddr,
916 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917{
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700918 struct kgsl_memdesc *result = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 struct kgsl_mem_entry *entry;
920 struct kgsl_process_private *priv;
921 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
922 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700923 struct kgsl_context *context;
924 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700926 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
927 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700929 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
930 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700931
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700932 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
933 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934
935 mutex_lock(&kgsl_driver.process_mutex);
936 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600937 if (!kgsl_mmu_pt_equal(priv->pagetable, pt_base))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939 spin_lock(&priv->mem_lock);
940 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
941 sizeof(unsigned int));
942 if (entry) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700943 result = &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944 spin_unlock(&priv->mem_lock);
945 mutex_unlock(&kgsl_driver.process_mutex);
946 return result;
947 }
948 spin_unlock(&priv->mem_lock);
949 }
950 mutex_unlock(&kgsl_driver.process_mutex);
951
952 BUG_ON(!mutex_is_locked(&device->mutex));
953 list_for_each_entry(entry, &device->memqueue, list) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700954 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) {
955 result = &entry->memdesc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700956 return result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957 }
958
959 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700960
961 while (1) {
962 struct adreno_context *adreno_context = NULL;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700963 context = idr_get_next(&device->context_idr, &next);
964 if (context == NULL)
965 break;
966
967 adreno_context = (struct adreno_context *)context->devctxt;
968
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700969 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
970 struct kgsl_memdesc *desc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700971
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700972 desc = &adreno_context->gpustate;
973 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
974 result = desc;
975 return result;
976 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700977
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700978 desc = &adreno_context->context_gmem_shadow.gmemshadow;
979 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
980 result = desc;
981 return result;
982 }
983 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700984 next = next + 1;
985 }
986
987 return NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700988
989}
990
991uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
992 unsigned int gpuaddr, unsigned int size)
993{
994 const struct kgsl_memdesc *memdesc;
995
996 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
997
998 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999}
1000
1001void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1002 unsigned int *value)
1003{
1004 unsigned int *reg;
1005 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1006 reg = (unsigned int *)(device->regspace.mmio_virt_base
1007 + (offsetwords << 2));
1008
1009 if (!in_interrupt())
1010 kgsl_pre_hwaccess(device);
1011
1012 /*ensure this read finishes before the next one.
1013 * i.e. act like normal readl() */
1014 *value = __raw_readl(reg);
1015 rmb();
1016}
1017
1018void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1019 unsigned int value)
1020{
1021 unsigned int *reg;
1022
1023 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1024
1025 if (!in_interrupt())
1026 kgsl_pre_hwaccess(device);
1027
1028 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1029 reg = (unsigned int *)(device->regspace.mmio_virt_base
1030 + (offsetwords << 2));
1031
1032 /*ensure previous writes post before this one,
1033 * i.e. act like normal writel() */
1034 wmb();
1035 __raw_writel(value, reg);
1036}
1037
1038static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1039 unsigned int timestamp)
1040{
1041 int status;
1042 unsigned int ref_ts, enableflag;
1043
1044 status = kgsl_check_timestamp(device, timestamp);
1045 if (!status) {
1046 mutex_lock(&device->mutex);
1047 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1048 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1049 mb();
1050
1051 if (enableflag) {
1052 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1053 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1054 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001055 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 kgsl_sharedmem_writel(&device->memstore,
1057 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1058 timestamp);
1059 wmb();
1060 }
1061 } else {
1062 unsigned int cmds[2];
1063 kgsl_sharedmem_writel(&device->memstore,
1064 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1065 timestamp);
1066 enableflag = 1;
1067 kgsl_sharedmem_writel(&device->memstore,
1068 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1069 enableflag);
1070 wmb();
1071 /* submit a dummy packet so that even if all
1072 * commands upto timestamp get executed we will still
1073 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001074 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001075 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001076 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1077 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001078 }
1079 mutex_unlock(&device->mutex);
1080 }
1081
1082 return status;
1083}
1084
1085/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001086 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087 placing a process in wait q. For conditional interrupts we expect the
1088 process to already be in its wait q when its exit condition checking
1089 function is called.
1090*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001091#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092({ \
1093 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001094 if (io) \
1095 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1096 else \
1097 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 __ret; \
1099})
1100
1101/* MUST be called with the device mutex held */
1102static int adreno_waittimestamp(struct kgsl_device *device,
1103 unsigned int timestamp,
1104 unsigned int msecs)
1105{
1106 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001107 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001108 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001110 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301111 int retries;
1112 unsigned int msecs_first;
1113 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301115 /* Don't wait forever, set a max value for now */
1116 if (msecs == -1)
1117 msecs = adreno_dev->wait_timeout;
1118
Jordan Crousee6239dd2011-11-17 13:39:21 -07001119 if (timestamp_cmp(timestamp, adreno_dev->ringbuffer.timestamp) > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1121 "rb->timestamp: %x\n",
1122 timestamp, adreno_dev->ringbuffer.timestamp);
1123 status = -EINVAL;
1124 goto done;
1125 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001126
Lynus Vaz06a9a902011-10-04 19:25:33 +05301127 /* Keep the first timeout as 100msecs before rewriting
1128 * the WPTR. Less visible impact if the WPTR has not
1129 * been updated properly.
1130 */
1131 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1132 msecs_part = (msecs - msecs_first + 3) / 4;
1133 for (retries = 0; retries < 5; retries++) {
1134 if (!kgsl_check_timestamp(device, timestamp)) {
1135 adreno_poke(device);
1136 io_cnt = (io_cnt + 1) % 100;
1137 if (io_cnt <
1138 pwr->pwrlevels[pwr->active_pwrlevel].
1139 io_fraction)
1140 io = 0;
1141 mutex_unlock(&device->mutex);
1142 /* We need to make sure that the process is
1143 * placed in wait-q before its condition is called
1144 */
1145 status = kgsl_wait_event_interruptible_timeout(
1146 device->wait_queue,
1147 kgsl_check_interrupt_timestamp(device,
1148 timestamp),
1149 msecs_to_jiffies(retries ?
1150 msecs_part : msecs_first), io);
1151 mutex_lock(&device->mutex);
1152
1153 if (status > 0) {
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001154 /*completed before the wait finished */
Lynus Vaz06a9a902011-10-04 19:25:33 +05301155 status = 0;
1156 goto done;
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001157 } else if (status < 0) {
1158 /*an error occurred*/
1159 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001160 }
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001161 /*this wait timed out*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162 }
1163 }
Lynus Vaz06a9a902011-10-04 19:25:33 +05301164 if (!kgsl_check_timestamp(device, timestamp)) {
1165 status = -ETIMEDOUT;
1166 KGSL_DRV_ERR(device,
1167 "Device hang detected while waiting "
1168 "for timestamp: %x, last "
1169 "submitted(rb->timestamp): %x, wptr: "
1170 "%x\n", timestamp,
1171 adreno_dev->ringbuffer.timestamp,
1172 adreno_dev->ringbuffer.wptr);
1173 if (!adreno_dump_and_recover(device)) {
1174 /* wait for idle after recovery as the
1175 * timestamp that this process wanted
1176 * to wait on may be invalid */
1177 if (!adreno_idle(device,
1178 KGSL_TIMEOUT_DEFAULT))
1179 status = 0;
1180 }
1181 } else {
1182 status = 0;
1183 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001184
1185done:
1186 return (int)status;
1187}
1188
1189static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1190 enum kgsl_timestamp_type type)
1191{
1192 unsigned int timestamp = 0;
1193
1194 if (type == KGSL_TIMESTAMP_CONSUMED)
1195 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1196 else if (type == KGSL_TIMESTAMP_RETIRED)
1197 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1198 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1199 rmb();
1200
1201 return timestamp;
1202}
1203
1204static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1205 unsigned int cmd, void *data)
1206{
1207 int result = 0;
1208 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1209 struct kgsl_context *context;
1210
1211 switch (cmd) {
1212 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1213 binbase = data;
1214
1215 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1216 if (context) {
1217 adreno_drawctxt_set_bin_base_offset(
1218 dev_priv->device, context, binbase->offset);
1219 } else {
1220 result = -EINVAL;
1221 KGSL_DRV_ERR(dev_priv->device,
1222 "invalid drawctxt drawctxt_id %d "
1223 "device_id=%d\n",
1224 binbase->drawctxt_id, dev_priv->device->id);
1225 }
1226 break;
1227
1228 default:
1229 KGSL_DRV_INFO(dev_priv->device,
1230 "invalid ioctl code %08x\n", cmd);
1231 result = -EINVAL;
1232 break;
1233 }
1234 return result;
1235
1236}
1237
1238static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1239{
1240 gpu_freq /= 1000000;
1241 return ticks / gpu_freq;
1242}
1243
1244static void adreno_power_stats(struct kgsl_device *device,
1245 struct kgsl_power_stats *stats)
1246{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001247 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001248 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001249 unsigned int cycles;
1250
1251 /* Get the busy cycles counted since the counter was last reset */
1252 /* Calling this function also resets and restarts the counter */
1253
1254 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255
1256 /* In order to calculate idle you have to have run the algorithm *
1257 * at least once to get a start time. */
1258 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001259 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260 stats->total_time = tmp - pwr->time;
1261 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001262 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263 pwrlevels[device->pwrctrl.active_pwrlevel].
1264 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001265 } else {
1266 stats->total_time = 0;
1267 stats->busy_time = 0;
1268 pwr->time = ktime_to_us(ktime_get());
1269 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001270}
1271
1272void adreno_irqctrl(struct kgsl_device *device, int state)
1273{
Jordan Crousea78c9172011-07-11 13:14:09 -06001274 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1275 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001276}
1277
Jordan Crousea0758f22011-12-07 11:19:22 -07001278static unsigned int adreno_gpuid(struct kgsl_device *device)
1279{
1280 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1281
1282 /* Standard KGSL gpuid format:
1283 * top word is 0x0002 for 2D or 0x0003 for 3D
1284 * Bottom word is core specific identifer
1285 */
1286
1287 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1288}
1289
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290static const struct kgsl_functable adreno_functable = {
1291 /* Mandatory functions */
1292 .regread = adreno_regread,
1293 .regwrite = adreno_regwrite,
1294 .idle = adreno_idle,
1295 .isidle = adreno_isidle,
1296 .suspend_context = adreno_suspend_context,
1297 .start = adreno_start,
1298 .stop = adreno_stop,
1299 .getproperty = adreno_getproperty,
1300 .waittimestamp = adreno_waittimestamp,
1301 .readtimestamp = adreno_readtimestamp,
1302 .issueibcmds = adreno_ringbuffer_issueibcmds,
1303 .ioctl = adreno_ioctl,
1304 .setup_pt = adreno_setup_pt,
1305 .cleanup_pt = adreno_cleanup_pt,
1306 .power_stats = adreno_power_stats,
1307 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001308 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001309 .snapshot = adreno_snapshot,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310 /* Optional functions */
1311 .setstate = adreno_setstate,
1312 .drawctxt_create = adreno_drawctxt_create,
1313 .drawctxt_destroy = adreno_drawctxt_destroy,
1314};
1315
1316static struct platform_device_id adreno_id_table[] = {
1317 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1318 { },
1319};
1320MODULE_DEVICE_TABLE(platform, adreno_id_table);
1321
1322static struct platform_driver adreno_platform_driver = {
1323 .probe = adreno_probe,
1324 .remove = __devexit_p(adreno_remove),
1325 .suspend = kgsl_suspend_driver,
1326 .resume = kgsl_resume_driver,
1327 .id_table = adreno_id_table,
1328 .driver = {
1329 .owner = THIS_MODULE,
1330 .name = DEVICE_3D_NAME,
1331 .pm = &kgsl_pm_ops,
1332 }
1333};
1334
1335static int __init kgsl_3d_init(void)
1336{
1337 return platform_driver_register(&adreno_platform_driver);
1338}
1339
1340static void __exit kgsl_3d_exit(void)
1341{
1342 platform_driver_unregister(&adreno_platform_driver);
1343}
1344
1345module_init(kgsl_3d_init);
1346module_exit(kgsl_3d_exit);
1347
1348MODULE_DESCRIPTION("3D Graphics driver");
1349MODULE_VERSION("1.2");
1350MODULE_LICENSE("GPL v2");
1351MODULE_ALIAS("platform:kgsl_3d");