blob: d30427d1ff707a443cf5c668dd27a3100bd4055b [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2002,2007-2011, Code Aurora Forum. All rights reserved.
2 *
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 */
13#include <linux/delay.h>
14#include <linux/uaccess.h>
15#include <linux/vmalloc.h>
16#include <linux/ioctl.h>
17#include <linux/sched.h>
18
19#include <mach/socinfo.h>
20
21#include "kgsl.h"
22#include "kgsl_pwrscale.h"
23#include "kgsl_cffdump.h"
24#include "kgsl_sharedmem.h"
25
26#include "adreno.h"
27#include "adreno_pm4types.h"
28#include "adreno_debugfs.h"
29#include "adreno_postmortem.h"
30
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070031#include "a2xx_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
68/* max msecs to wait for gpu to finish its operation(s) */
69#define MAX_WAITGPU_SECS (HZ + HZ/2)
70
71static const struct kgsl_functable adreno_functable;
72
73static struct adreno_device device_3d0 = {
74 .dev = {
75 .name = DEVICE_3D0_NAME,
76 .id = KGSL_DEVICE_3D0,
77 .ver_major = DRIVER_VERSION_MAJOR,
78 .ver_minor = DRIVER_VERSION_MINOR,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060079 .mh = {
80 .mharb = ADRENO_CFG_MHARB,
81 /* Remove 1k boundary check in z470 to avoid a GPU
82 * hang. Notice that this solution won't work if
83 * both EBI and SMI are used
84 */
85 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070086 /* turn off memory protection unit by setting
87 acceptable physical address range to include
88 all pages. */
89 .mpu_base = 0x00000000,
90 .mpu_range = 0xFFFFF000,
91 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060092 .mmu = {
93 .config = ADRENO_MMU_CONFIG,
94 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095 .pwrctrl = {
96 .regulator_name = "fs_gfx3d",
97 .irq_name = KGSL_3D0_IRQ,
Matt Wagantall9dc01632011-08-17 18:55:04 -070098 .src_clk_name = "src_clk",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 },
100 .mutex = __MUTEX_INITIALIZER(device_3d0.dev.mutex),
101 .state = KGSL_STATE_INIT,
102 .active_cnt = 0,
103 .iomemname = KGSL_3D0_REG_MEMORY,
104 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600106 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
108 .suspend = kgsl_early_suspend_driver,
109 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600111#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 },
113 .gmemspace = {
114 .gpu_base = 0,
115 .sizebytes = SZ_256K,
116 },
117 .pfp_fw = NULL,
118 .pm4_fw = NULL,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119};
120
Jordan Crouse505df9c2011-07-28 08:37:59 -0600121/*
122 * This is the master list of all GPU cores that are supported by this
123 * driver.
124 */
125
126#define ANY_ID (~0)
127
128static const struct {
129 enum adreno_gpurev gpurev;
130 unsigned int core, major, minor;
131 const char *pm4fw;
132 const char *pfpfw;
133 struct adreno_gpudev *gpudev;
134} adreno_gpulist[] = {
135 { ADRENO_REV_A200, 0, 2, ANY_ID,
136 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev },
137 { ADRENO_REV_A205, 0, 1, 0,
138 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev },
139 { ADRENO_REV_A220, 2, 1, ANY_ID,
140 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev },
141 { ADRENO_REV_A225, 2, 2, ANY_ID,
142 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev },
143};
144
Jordan Crouse9f739212011-07-28 08:37:57 -0600145static void adreno_gmeminit(struct adreno_device *adreno_dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146{
147 struct kgsl_device *device = &adreno_dev->dev;
148 union reg_rb_edram_info rb_edram_info;
149 unsigned int gmem_size;
150 unsigned int edram_value = 0;
151
152 /* make sure edram range is aligned to size */
153 BUG_ON(adreno_dev->gmemspace.gpu_base &
154 (adreno_dev->gmemspace.sizebytes - 1));
155
156 /* get edram_size value equivalent */
157 gmem_size = (adreno_dev->gmemspace.sizebytes >> 14);
158 while (gmem_size >>= 1)
159 edram_value++;
160
161 rb_edram_info.val = 0;
162
163 rb_edram_info.f.edram_size = edram_value;
Jordan Crouse9f739212011-07-28 08:37:57 -0600164 rb_edram_info.f.edram_mapping_mode = 0; /* EDRAM_MAP_UPPER */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165
166 /* must be aligned to size */
167 rb_edram_info.f.edram_range = (adreno_dev->gmemspace.gpu_base >> 14);
168
169 adreno_regwrite(device, REG_RB_EDRAM_INFO, rb_edram_info.val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170}
171
Jordan Crouse9f739212011-07-28 08:37:57 -0600172static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173{
Jordan Crousea78c9172011-07-11 13:14:09 -0600174 irqreturn_t result;
175 struct kgsl_device *device = data;
176 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177
Jordan Crousea78c9172011-07-11 13:14:09 -0600178 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179
180 if (device->requested_state == KGSL_STATE_NONE) {
181 if (device->pwrctrl.nap_allowed == true) {
182 device->requested_state = KGSL_STATE_NAP;
183 queue_work(device->work_queue, &device->idle_check_ws);
184 } else if (device->pwrscale.policy != NULL) {
185 queue_work(device->work_queue, &device->idle_check_ws);
186 }
187 }
188
189 /* Reset the time-out in our idle timer */
190 mod_timer(&device->idle_timer,
191 jiffies + device->pwrctrl.interval_timeout);
192 return result;
193}
194
Jordan Crouse9f739212011-07-28 08:37:57 -0600195static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196 struct kgsl_pagetable *pagetable)
197{
198 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
199 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
200
201 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
202
203 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
204
205 kgsl_mmu_unmap(pagetable, &device->memstore);
206
207 kgsl_mmu_unmap(pagetable, &device->mmu.dummyspace);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208}
209
210static int adreno_setup_pt(struct kgsl_device *device,
211 struct kgsl_pagetable *pagetable)
212{
213 int result = 0;
214 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
215 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
216
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
218 GSL_PT_PAGE_RV);
219 if (result)
220 goto error;
221
222 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
223 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
224 if (result)
225 goto unmap_buffer_desc;
226
227 result = kgsl_mmu_map_global(pagetable, &device->memstore,
228 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
229 if (result)
230 goto unmap_memptrs_desc;
231
232 result = kgsl_mmu_map_global(pagetable, &device->mmu.dummyspace,
233 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
234 if (result)
235 goto unmap_memstore_desc;
236
237 return result;
238
239unmap_memstore_desc:
240 kgsl_mmu_unmap(pagetable, &device->memstore);
241
242unmap_memptrs_desc:
243 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
244
245unmap_buffer_desc:
246 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
247
248error:
249 return result;
250}
251
252static void adreno_setstate(struct kgsl_device *device, uint32_t flags)
253{
254 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
255 unsigned int link[32];
256 unsigned int *cmds = &link[0];
257 int sizedwords = 0;
258 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
259
260 if (!kgsl_mmu_enabled() || !flags)
261 return;
262
263 /* If possible, then set the state via the command stream to avoid
264 a CPU idle. Otherwise, use the default setstate which uses register
265 writes */
266
267 if (adreno_dev->drawctxt_active) {
268 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
269 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600270 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271 *cmds++ = 0x00000000;
272
273 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600274 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 *cmds++ = device->mmu.hwpagetable->base.gpuaddr;
276 sizedwords += 4;
277 }
278
279 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
280 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600281 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282 1);
283 *cmds++ = 0x00000000;
284 sizedwords += 2;
285 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600286 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 *cmds++ = mh_mmu_invalidate;
288 sizedwords += 2;
289 }
290
291 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600292 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 /* HW workaround: to resolve MMU page fault interrupts
294 * caused by the VGT.It prevents the CP PFP from filling
295 * the VGT DMA request fifo too early,thereby ensuring
296 * that the VGT will not fetch vertex/bin data until
297 * after the page table base register has been updated.
298 *
299 * Two null DRAW_INDX_BIN packets are inserted right
300 * after the page table base update, followed by a
301 * wait for idle. The null packets will fill up the
302 * VGT DMA request fifo and prevent any further
303 * vertex/bin updates from occurring until the wait
304 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600305 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 *cmds++ = (0x4 << 16) |
307 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
308 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600309 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 *cmds++ = device->mmu.dummyspace.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600311 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312 *cmds++ = 0; /* viz query info */
313 *cmds++ = 0x0003C004; /* draw indicator */
314 *cmds++ = 0; /* bin base */
315 *cmds++ = 3; /* bin size */
316 *cmds++ = device->mmu.dummyspace.gpuaddr; /* dma base */
317 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600318 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 *cmds++ = 0; /* viz query info */
320 *cmds++ = 0x0003C004; /* draw indicator */
321 *cmds++ = 0; /* bin base */
322 *cmds++ = 3; /* bin size */
323 /* dma base */
324 *cmds++ = device->mmu.dummyspace.gpuaddr;
325 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600326 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327 *cmds++ = 0x00000000;
328 sizedwords += 21;
329 }
330
331 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600332 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 *cmds++ = 0x7fff; /* invalidate all base pointers */
334 sizedwords += 2;
335 }
336
337 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
338 &link[0], sizedwords);
339 } else
340 kgsl_default_setstate(device, flags);
341}
342
343static unsigned int
344adreno_getchipid(struct kgsl_device *device)
345{
346 unsigned int chipid = 0;
347 unsigned int coreid, majorid, minorid, patchid, revid;
348
349 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
350 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
351 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
352
353 /*
354 * adreno 22x gpus are indicated by coreid 2,
355 * but REG_RBBM_PERIPHID1 always contains 0 for this field
356 */
357 if (cpu_is_msm8960() || cpu_is_msm8x60())
358 chipid = 2 << 24;
359 else
360 chipid = (coreid & 0xF) << 24;
361
Jeremy Gebbenaafef442011-08-26 12:27:43 -0700362 if (cpu_is_msm8960()) {
363 KGSL_DRV_ERR(device, "forcing a220 chipid\n");
364 majorid = 1<<4;
365 }
366
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367 chipid |= ((majorid >> 4) & 0xF) << 16;
368
369 minorid = ((revid >> 0) & 0xFF);
370
371 patchid = ((revid >> 16) & 0xFF);
372
373 /* 8x50 returns 0 for patch release, but it should be 1 */
374 if (cpu_is_qsd8x50())
375 patchid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376
377 chipid |= (minorid << 8) | patchid;
378
379 return chipid;
380}
381
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382static inline bool _rev_match(unsigned int id, unsigned int entry)
383{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600384 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386
387static void
388adreno_identify_gpu(struct adreno_device *adreno_dev)
389{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390 unsigned int i, core, major, minor;
391
392 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
393
394 core = (adreno_dev->chip_id >> 24) & 0xff;
395 major = (adreno_dev->chip_id >> 16) & 0xff;
396 minor = (adreno_dev->chip_id >> 8) & 0xff;
397
Jordan Crouse505df9c2011-07-28 08:37:59 -0600398 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
399 if (core == adreno_gpulist[i].core &&
400 _rev_match(major, adreno_gpulist[i].major) &&
401 _rev_match(minor, adreno_gpulist[i].minor)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402 break;
403 }
404 }
405
Jordan Crouse505df9c2011-07-28 08:37:59 -0600406 if (i == ARRAY_SIZE(adreno_gpulist)) {
407 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
408 return;
409 }
410
411 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
412 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
413 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
414 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415}
416
417static int __devinit
418adreno_probe(struct platform_device *pdev)
419{
420 struct kgsl_device *device;
421 struct adreno_device *adreno_dev;
422 int status = -EINVAL;
423
424 device = (struct kgsl_device *)pdev->id_entry->driver_data;
425 adreno_dev = ADRENO_DEVICE(device);
426 device->parentdev = &pdev->dev;
427
428 init_completion(&device->recovery_gate);
429
430 status = adreno_ringbuffer_init(device);
431 if (status != 0)
432 goto error;
433
434 status = kgsl_device_platform_probe(device, adreno_isr);
435 if (status)
436 goto error_close_rb;
437
438 adreno_debugfs_init(device);
439
440 kgsl_pwrscale_init(device);
441 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
442
443 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
444 return 0;
445
446error_close_rb:
447 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
448error:
449 device->parentdev = NULL;
450 return status;
451}
452
453static int __devexit adreno_remove(struct platform_device *pdev)
454{
455 struct kgsl_device *device;
456 struct adreno_device *adreno_dev;
457
458 device = (struct kgsl_device *)pdev->id_entry->driver_data;
459 adreno_dev = ADRENO_DEVICE(device);
460
461 kgsl_pwrscale_detach_policy(device);
462 kgsl_pwrscale_close(device);
463
464 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
465 kgsl_device_platform_remove(device);
466
467 return 0;
468}
469
470static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
471{
472 int status = -EINVAL;
473 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
474 int init_reftimestamp = 0x7fffffff;
475
476 device->state = KGSL_STATE_INIT;
477 device->requested_state = KGSL_STATE_NONE;
478
479 /* Power up the device */
480 kgsl_pwrctrl_enable(device);
481
482 /* Identify the specific GPU */
483 adreno_identify_gpu(adreno_dev);
484
Jordan Crouse505df9c2011-07-28 08:37:59 -0600485 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
486 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
487 adreno_dev->chip_id);
488 goto error_clk_off;
489 }
490
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600491 if (adreno_is_a20x(adreno_dev)) {
492 /*
493 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
494 * on older gpus
495 */
496 device->mh.mh_intf_cfg1 = 0;
497 device->mh.mh_intf_cfg2 = 0;
498 }
499
500 kgsl_mh_start(device);
501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 if (kgsl_mmu_start(device))
503 goto error_clk_off;
504
505 /*We need to make sure all blocks are powered up and clocked before
506 *issuing a soft reset. The overrides will then be turned off (set to 0)
507 */
508 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0xfffffffe);
509 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0xffffffff);
510
511 /* Only reset CP block if all blocks have previously been reset */
512 if (!(device->flags & KGSL_FLAGS_SOFT_RESET) ||
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600513 !adreno_is_a22x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0xFFFFFFFF);
515 device->flags |= KGSL_FLAGS_SOFT_RESET;
516 } else
517 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000001);
518
519 /* The core is in an indeterminate state until the reset completes
520 * after 30ms.
521 */
522 msleep(30);
523
524 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000000);
525
526 adreno_regwrite(device, REG_RBBM_CNTL, 0x00004442);
527
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600528 if (adreno_is_a225(adreno_dev)) {
529 /* Enable large instruction store for A225 */
530 adreno_regwrite(device, REG_SQ_FLOW_CONTROL, 0x18000000);
531 }
532
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 adreno_regwrite(device, REG_SQ_VS_PROGRAM, 0x00000000);
534 adreno_regwrite(device, REG_SQ_PS_PROGRAM, 0x00000000);
535
536 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0);
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600537 if (!adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0);
539 else
540 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0x80);
541
Sushmita Susheelendraf3896062011-08-12 16:33:10 -0600542 kgsl_sharedmem_set(&device->memstore, 0, 0, device->memstore.size);
543
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544 kgsl_sharedmem_writel(&device->memstore,
545 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
546 init_reftimestamp);
547
Ranjhith Kalisamy22d15a72011-09-07 20:07:38 +0530548 if (adreno_is_a20x(adreno_dev))
549 adreno_regwrite(device, REG_RBBM_DEBUG, 0x000C0000);
550 else
551 adreno_regwrite(device, REG_RBBM_DEBUG, 0x00080000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552
553 /* Make sure interrupts are disabled */
554
555 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
556 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
557 adreno_regwrite(device, REG_SQ_INT_CNTL, 0);
558
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600559 if (adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560 adreno_dev->gmemspace.sizebytes = SZ_512K;
561 else
562 adreno_dev->gmemspace.sizebytes = SZ_256K;
563 adreno_gmeminit(adreno_dev);
564
565 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
566
567 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
568 if (status != 0)
569 goto error_irq_off;
570
571 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
572 return status;
573
574error_irq_off:
575 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600576 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577error_clk_off:
578 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579
580 return status;
581}
582
583static int adreno_stop(struct kgsl_device *device)
584{
585 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
586
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 adreno_dev->drawctxt_active = NULL;
588
589 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
590
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 kgsl_mmu_stop(device);
592
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600593 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
594 del_timer_sync(&device->idle_timer);
595
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596 /* Power down the device */
597 kgsl_pwrctrl_disable(device);
598
599 return 0;
600}
601
602static int
603adreno_recover_hang(struct kgsl_device *device)
604{
605 int ret;
606 unsigned int *rb_buffer;
607 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
608 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
609 unsigned int timestamp;
610 unsigned int num_rb_contents;
611 unsigned int bad_context;
612 unsigned int reftimestamp;
613 unsigned int enable_ts;
614 unsigned int soptimestamp;
615 unsigned int eoptimestamp;
616 struct adreno_context *drawctxt;
617
618 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
619 rb_buffer = vmalloc(rb->buffer_desc.size);
620 if (!rb_buffer) {
621 KGSL_MEM_ERR(device,
622 "Failed to allocate memory for recovery: %x\n",
623 rb->buffer_desc.size);
624 return -ENOMEM;
625 }
626 /* Extract valid contents from rb which can stil be executed after
627 * hang */
628 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
629 if (ret)
630 goto done;
631 timestamp = rb->timestamp;
632 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
633 kgsl_sharedmem_readl(&device->memstore, &bad_context,
634 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
635 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
636 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
637 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
638 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
639 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
640 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
641 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
642 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
643 /* Make sure memory is synchronized before restarting the GPU */
644 mb();
645 KGSL_CTXT_ERR(device,
646 "Context that caused a GPU hang: %x\n", bad_context);
647 /* restart device */
648 ret = adreno_stop(device);
649 if (ret)
650 goto done;
651 ret = adreno_start(device, true);
652 if (ret)
653 goto done;
654 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
655 /* Restore timestamp states */
656 kgsl_sharedmem_writel(&device->memstore,
657 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
658 soptimestamp);
659 kgsl_sharedmem_writel(&device->memstore,
660 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
661 eoptimestamp);
662 kgsl_sharedmem_writel(&device->memstore,
663 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
664 soptimestamp);
665 if (num_rb_contents) {
666 kgsl_sharedmem_writel(&device->memstore,
667 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
668 reftimestamp);
669 kgsl_sharedmem_writel(&device->memstore,
670 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
671 enable_ts);
672 }
673 /* Make sure all writes are posted before the GPU reads them */
674 wmb();
675 /* Mark the invalid context so no more commands are accepted from
676 * that context */
677
678 drawctxt = (struct adreno_context *) bad_context;
679
680 KGSL_CTXT_ERR(device,
681 "Context that caused a GPU hang: %x\n", bad_context);
682
683 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
684
685 /* Restore valid commands in ringbuffer */
686 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
687 rb->timestamp = timestamp;
688done:
689 vfree(rb_buffer);
690 return ret;
691}
692
693static int
694adreno_dump_and_recover(struct kgsl_device *device)
695{
696 static int recovery;
697 int result = -ETIMEDOUT;
698
699 if (device->state == KGSL_STATE_HUNG)
700 goto done;
701 if (device->state == KGSL_STATE_DUMP_AND_RECOVER && !recovery) {
702 mutex_unlock(&device->mutex);
703 wait_for_completion(&device->recovery_gate);
704 mutex_lock(&device->mutex);
705 if (!(device->state & KGSL_STATE_HUNG))
706 /* recovery success */
707 result = 0;
708 } else {
709 INIT_COMPLETION(device->recovery_gate);
710 /* Detected a hang - trigger an automatic dump */
711 adreno_postmortem_dump(device, 0);
712 if (!recovery) {
713 recovery = 1;
714 result = adreno_recover_hang(device);
715 if (result)
716 device->state = KGSL_STATE_HUNG;
717 recovery = 0;
718 complete_all(&device->recovery_gate);
719 } else
720 KGSL_DRV_ERR(device,
721 "Cannot recover from another hang while "
722 "recovering from a hang\n");
723 }
724done:
725 return result;
726}
727
728static int adreno_getproperty(struct kgsl_device *device,
729 enum kgsl_property_type type,
730 void *value,
731 unsigned int sizebytes)
732{
733 int status = -EINVAL;
734 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
735
736 switch (type) {
737 case KGSL_PROP_DEVICE_INFO:
738 {
739 struct kgsl_devinfo devinfo;
740
741 if (sizebytes != sizeof(devinfo)) {
742 status = -EINVAL;
743 break;
744 }
745
746 memset(&devinfo, 0, sizeof(devinfo));
747 devinfo.device_id = device->id+1;
748 devinfo.chip_id = adreno_dev->chip_id;
749 devinfo.mmu_enabled = kgsl_mmu_enabled();
750 devinfo.gpu_id = adreno_dev->gpurev;
751 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
752 gpu_base;
753 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
754 sizebytes;
755
756 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
757 0) {
758 status = -EFAULT;
759 break;
760 }
761 status = 0;
762 }
763 break;
764 case KGSL_PROP_DEVICE_SHADOW:
765 {
766 struct kgsl_shadowprop shadowprop;
767
768 if (sizebytes != sizeof(shadowprop)) {
769 status = -EINVAL;
770 break;
771 }
772 memset(&shadowprop, 0, sizeof(shadowprop));
773 if (device->memstore.hostptr) {
774 /*NOTE: with mmu enabled, gpuaddr doesn't mean
775 * anything to mmap().
776 */
777 shadowprop.gpuaddr = device->memstore.physaddr;
778 shadowprop.size = device->memstore.size;
779 /* GSL needs this to be set, even if it
780 appears to be meaningless */
781 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
782 }
783 if (copy_to_user(value, &shadowprop,
784 sizeof(shadowprop))) {
785 status = -EFAULT;
786 break;
787 }
788 status = 0;
789 }
790 break;
791 case KGSL_PROP_MMU_ENABLE:
792 {
793#ifdef CONFIG_MSM_KGSL_MMU
794 int mmuProp = 1;
795#else
796 int mmuProp = 0;
797#endif
798 if (sizebytes != sizeof(int)) {
799 status = -EINVAL;
800 break;
801 }
802 if (copy_to_user(value, &mmuProp, sizeof(mmuProp))) {
803 status = -EFAULT;
804 break;
805 }
806 status = 0;
807 }
808 break;
809 case KGSL_PROP_INTERRUPT_WAITS:
810 {
811 int int_waits = 1;
812 if (sizebytes != sizeof(int)) {
813 status = -EINVAL;
814 break;
815 }
816 if (copy_to_user(value, &int_waits, sizeof(int))) {
817 status = -EFAULT;
818 break;
819 }
820 status = 0;
821 }
822 break;
823 default:
824 status = -EINVAL;
825 }
826
827 return status;
828}
829
830/* Caller must hold the device mutex. */
831int adreno_idle(struct kgsl_device *device, unsigned int timeout)
832{
833 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
834 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
835 unsigned int rbbm_status;
836 unsigned long wait_time = jiffies + MAX_WAITGPU_SECS;
837
838 kgsl_cffdump_regpoll(device->id, REG_RBBM_STATUS << 2,
839 0x00000000, 0x80000000);
840 /* first, wait until the CP has consumed all the commands in
841 * the ring buffer
842 */
843retry:
844 if (rb->flags & KGSL_FLAGS_STARTED) {
845 do {
846 GSL_RB_GET_READPTR(rb, &rb->rptr);
847 if (time_after(jiffies, wait_time)) {
848 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
849 rb->rptr, rb->wptr);
850 goto err;
851 }
852 } while (rb->rptr != rb->wptr);
853 }
854
855 /* now, wait for the GPU to finish its operations */
856 wait_time = jiffies + MAX_WAITGPU_SECS;
857 while (time_before(jiffies, wait_time)) {
858 adreno_regread(device, REG_RBBM_STATUS, &rbbm_status);
859 if (rbbm_status == 0x110)
860 return 0;
861 }
862
863err:
864 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
865 if (!adreno_dump_and_recover(device)) {
866 wait_time = jiffies + MAX_WAITGPU_SECS;
867 goto retry;
868 }
869 return -ETIMEDOUT;
870}
871
872static unsigned int adreno_isidle(struct kgsl_device *device)
873{
874 int status = false;
875 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
876 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
877 unsigned int rbbm_status;
878
879 if (rb->flags & KGSL_FLAGS_STARTED) {
880 /* Is the ring buffer is empty? */
881 GSL_RB_GET_READPTR(rb, &rb->rptr);
882 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
883 /* Is the core idle? */
884 adreno_regread(device, REG_RBBM_STATUS,
885 &rbbm_status);
886 if (rbbm_status == 0x110)
887 status = true;
888 }
889 } else {
890 KGSL_DRV_ERR(device, "ringbuffer not started\n");
891 BUG();
892 }
893 return status;
894}
895
896/* Caller must hold the device mutex. */
897static int adreno_suspend_context(struct kgsl_device *device)
898{
899 int status = 0;
900 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
901
902 /* switch to NULL ctxt */
903 if (adreno_dev->drawctxt_active != NULL) {
904 adreno_drawctxt_switch(adreno_dev, NULL, 0);
905 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
906 }
907
908 return status;
909}
910
911uint8_t *kgsl_sharedmem_convertaddr(struct kgsl_device *device,
912 unsigned int pt_base, unsigned int gpuaddr, unsigned int *size)
913{
914 uint8_t *result = NULL;
915 struct kgsl_mem_entry *entry;
916 struct kgsl_process_private *priv;
917 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
918 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
919
920 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr)) {
921 return kgsl_gpuaddr_to_vaddr(&ringbuffer->buffer_desc,
922 gpuaddr, size);
923 }
924
925 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr)) {
926 return kgsl_gpuaddr_to_vaddr(&ringbuffer->memptrs_desc,
927 gpuaddr, size);
928 }
929
930 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr)) {
931 return kgsl_gpuaddr_to_vaddr(&device->memstore,
932 gpuaddr, size);
933 }
934
935 mutex_lock(&kgsl_driver.process_mutex);
936 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
937 if (pt_base != 0
938 && priv->pagetable
939 && priv->pagetable->base.gpuaddr != pt_base) {
940 continue;
941 }
942
943 spin_lock(&priv->mem_lock);
944 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
945 sizeof(unsigned int));
946 if (entry) {
947 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
948 gpuaddr, size);
949 spin_unlock(&priv->mem_lock);
950 mutex_unlock(&kgsl_driver.process_mutex);
951 return result;
952 }
953 spin_unlock(&priv->mem_lock);
954 }
955 mutex_unlock(&kgsl_driver.process_mutex);
956
957 BUG_ON(!mutex_is_locked(&device->mutex));
958 list_for_each_entry(entry, &device->memqueue, list) {
959 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr)) {
960 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
961 gpuaddr, size);
962 break;
963 }
964
965 }
966 return result;
967}
968
969void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
970 unsigned int *value)
971{
972 unsigned int *reg;
973 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
974 reg = (unsigned int *)(device->regspace.mmio_virt_base
975 + (offsetwords << 2));
976
977 if (!in_interrupt())
978 kgsl_pre_hwaccess(device);
979
980 /*ensure this read finishes before the next one.
981 * i.e. act like normal readl() */
982 *value = __raw_readl(reg);
983 rmb();
984}
985
986void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
987 unsigned int value)
988{
989 unsigned int *reg;
990
991 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
992
993 if (!in_interrupt())
994 kgsl_pre_hwaccess(device);
995
996 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
997 reg = (unsigned int *)(device->regspace.mmio_virt_base
998 + (offsetwords << 2));
999
1000 /*ensure previous writes post before this one,
1001 * i.e. act like normal writel() */
1002 wmb();
1003 __raw_writel(value, reg);
1004}
1005
1006static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1007 unsigned int timestamp)
1008{
1009 int status;
1010 unsigned int ref_ts, enableflag;
1011
1012 status = kgsl_check_timestamp(device, timestamp);
1013 if (!status) {
1014 mutex_lock(&device->mutex);
1015 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1016 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1017 mb();
1018
1019 if (enableflag) {
1020 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1021 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1022 mb();
1023 if (timestamp_cmp(ref_ts, timestamp)) {
1024 kgsl_sharedmem_writel(&device->memstore,
1025 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1026 timestamp);
1027 wmb();
1028 }
1029 } else {
1030 unsigned int cmds[2];
1031 kgsl_sharedmem_writel(&device->memstore,
1032 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1033 timestamp);
1034 enableflag = 1;
1035 kgsl_sharedmem_writel(&device->memstore,
1036 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1037 enableflag);
1038 wmb();
1039 /* submit a dummy packet so that even if all
1040 * commands upto timestamp get executed we will still
1041 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001042 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043 cmds[1] = 0;
1044 adreno_ringbuffer_issuecmds(device, 0, &cmds[0], 2);
1045 }
1046 mutex_unlock(&device->mutex);
1047 }
1048
1049 return status;
1050}
1051
1052/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001053 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054 placing a process in wait q. For conditional interrupts we expect the
1055 process to already be in its wait q when its exit condition checking
1056 function is called.
1057*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001058#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059({ \
1060 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001061 if (io) \
1062 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1063 else \
1064 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001065 __ret; \
1066})
1067
1068/* MUST be called with the device mutex held */
1069static int adreno_waittimestamp(struct kgsl_device *device,
1070 unsigned int timestamp,
1071 unsigned int msecs)
1072{
1073 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001074 uint io = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001075 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001076 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077
1078 if (timestamp != adreno_dev->ringbuffer.timestamp &&
1079 timestamp_cmp(timestamp,
1080 adreno_dev->ringbuffer.timestamp)) {
1081 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1082 "rb->timestamp: %x\n",
1083 timestamp, adreno_dev->ringbuffer.timestamp);
1084 status = -EINVAL;
1085 goto done;
1086 }
1087 if (!kgsl_check_timestamp(device, timestamp)) {
Lucille Sylvester02e46292011-09-21 14:59:17 -06001088 if (pwr->active_pwrlevel) {
1089 int low_pwrlevel = pwr->num_pwrlevels -
1090 KGSL_PWRLEVEL_LOW_OFFSET;
1091 if (pwr->active_pwrlevel == low_pwrlevel)
1092 io = 0;
1093 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094 mutex_unlock(&device->mutex);
1095 /* We need to make sure that the process is placed in wait-q
1096 * before its condition is called */
Lucille Sylvester02e46292011-09-21 14:59:17 -06001097 status = kgsl_wait_event_interruptible_timeout(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 device->wait_queue,
1099 kgsl_check_interrupt_timestamp(device,
Lucille Sylvester02e46292011-09-21 14:59:17 -06001100 timestamp),
1101 msecs_to_jiffies(msecs), io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102 mutex_lock(&device->mutex);
1103
1104 if (status > 0)
1105 status = 0;
1106 else if (status == 0) {
1107 if (!kgsl_check_timestamp(device, timestamp)) {
1108 status = -ETIMEDOUT;
1109 KGSL_DRV_ERR(device,
1110 "Device hang detected while waiting "
1111 "for timestamp: %x, last "
1112 "submitted(rb->timestamp): %x, wptr: "
1113 "%x\n", timestamp,
1114 adreno_dev->ringbuffer.timestamp,
1115 adreno_dev->ringbuffer.wptr);
1116 if (!adreno_dump_and_recover(device)) {
1117 /* wait for idle after recovery as the
1118 * timestamp that this process wanted
1119 * to wait on may be invalid */
1120 if (!adreno_idle(device,
1121 KGSL_TIMEOUT_DEFAULT))
1122 status = 0;
1123 }
1124 }
1125 }
1126 }
1127
1128done:
1129 return (int)status;
1130}
1131
1132static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1133 enum kgsl_timestamp_type type)
1134{
1135 unsigned int timestamp = 0;
1136
1137 if (type == KGSL_TIMESTAMP_CONSUMED)
1138 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1139 else if (type == KGSL_TIMESTAMP_RETIRED)
1140 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1141 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1142 rmb();
1143
1144 return timestamp;
1145}
1146
1147static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1148 unsigned int cmd, void *data)
1149{
1150 int result = 0;
1151 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1152 struct kgsl_context *context;
1153
1154 switch (cmd) {
1155 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1156 binbase = data;
1157
1158 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1159 if (context) {
1160 adreno_drawctxt_set_bin_base_offset(
1161 dev_priv->device, context, binbase->offset);
1162 } else {
1163 result = -EINVAL;
1164 KGSL_DRV_ERR(dev_priv->device,
1165 "invalid drawctxt drawctxt_id %d "
1166 "device_id=%d\n",
1167 binbase->drawctxt_id, dev_priv->device->id);
1168 }
1169 break;
1170
1171 default:
1172 KGSL_DRV_INFO(dev_priv->device,
1173 "invalid ioctl code %08x\n", cmd);
1174 result = -EINVAL;
1175 break;
1176 }
1177 return result;
1178
1179}
1180
1181static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1182{
1183 gpu_freq /= 1000000;
1184 return ticks / gpu_freq;
1185}
1186
1187static void adreno_power_stats(struct kgsl_device *device,
1188 struct kgsl_power_stats *stats)
1189{
1190 unsigned int reg;
1191 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
1192
1193 /* In order to calculate idle you have to have run the algorithm *
1194 * at least once to get a start time. */
1195 if (pwr->time != 0) {
1196 s64 tmp;
1197 /* Stop the performance moniter and read the current *
1198 * busy cycles. */
1199 adreno_regwrite(device,
1200 REG_CP_PERFMON_CNTL,
1201 REG_PERF_MODE_CNT |
1202 REG_PERF_STATE_FREEZE);
1203 adreno_regread(device, REG_RBBM_PERFCOUNTER1_LO, &reg);
1204 tmp = ktime_to_us(ktime_get());
1205 stats->total_time = tmp - pwr->time;
1206 pwr->time = tmp;
1207 stats->busy_time = adreno_ticks_to_us(reg, device->pwrctrl.
1208 pwrlevels[device->pwrctrl.active_pwrlevel].
1209 gpu_freq);
1210
1211 adreno_regwrite(device,
1212 REG_CP_PERFMON_CNTL,
1213 REG_PERF_MODE_CNT |
1214 REG_PERF_STATE_RESET);
1215 } else {
1216 stats->total_time = 0;
1217 stats->busy_time = 0;
1218 pwr->time = ktime_to_us(ktime_get());
1219 }
1220
1221 /* re-enable the performance moniters */
1222 adreno_regread(device, REG_RBBM_PM_OVERRIDE2, &reg);
1223 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, (reg | 0x40));
1224 adreno_regwrite(device, REG_RBBM_PERFCOUNTER1_SELECT, 0x1);
1225 adreno_regwrite(device,
1226 REG_CP_PERFMON_CNTL,
1227 REG_PERF_MODE_CNT | REG_PERF_STATE_ENABLE);
1228}
1229
1230void adreno_irqctrl(struct kgsl_device *device, int state)
1231{
Jordan Crousea78c9172011-07-11 13:14:09 -06001232 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1233 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001234}
1235
1236static const struct kgsl_functable adreno_functable = {
1237 /* Mandatory functions */
1238 .regread = adreno_regread,
1239 .regwrite = adreno_regwrite,
1240 .idle = adreno_idle,
1241 .isidle = adreno_isidle,
1242 .suspend_context = adreno_suspend_context,
1243 .start = adreno_start,
1244 .stop = adreno_stop,
1245 .getproperty = adreno_getproperty,
1246 .waittimestamp = adreno_waittimestamp,
1247 .readtimestamp = adreno_readtimestamp,
1248 .issueibcmds = adreno_ringbuffer_issueibcmds,
1249 .ioctl = adreno_ioctl,
1250 .setup_pt = adreno_setup_pt,
1251 .cleanup_pt = adreno_cleanup_pt,
1252 .power_stats = adreno_power_stats,
1253 .irqctrl = adreno_irqctrl,
1254 /* Optional functions */
1255 .setstate = adreno_setstate,
1256 .drawctxt_create = adreno_drawctxt_create,
1257 .drawctxt_destroy = adreno_drawctxt_destroy,
1258};
1259
1260static struct platform_device_id adreno_id_table[] = {
1261 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1262 { },
1263};
1264MODULE_DEVICE_TABLE(platform, adreno_id_table);
1265
1266static struct platform_driver adreno_platform_driver = {
1267 .probe = adreno_probe,
1268 .remove = __devexit_p(adreno_remove),
1269 .suspend = kgsl_suspend_driver,
1270 .resume = kgsl_resume_driver,
1271 .id_table = adreno_id_table,
1272 .driver = {
1273 .owner = THIS_MODULE,
1274 .name = DEVICE_3D_NAME,
1275 .pm = &kgsl_pm_ops,
1276 }
1277};
1278
1279static int __init kgsl_3d_init(void)
1280{
1281 return platform_driver_register(&adreno_platform_driver);
1282}
1283
1284static void __exit kgsl_3d_exit(void)
1285{
1286 platform_driver_unregister(&adreno_platform_driver);
1287}
1288
1289module_init(kgsl_3d_init);
1290module_exit(kgsl_3d_exit);
1291
1292MODULE_DESCRIPTION("3D Graphics driver");
1293MODULE_VERSION("1.2");
1294MODULE_LICENSE("GPL v2");
1295MODULE_ALIAS("platform:kgsl_3d");