blob: e6c463a72168af29a32cff8538ac9a5dcdaffecd [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
31#include "a200_reg.h"
32
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,
98 .src_clk_name = "grp_src_clk",
99 },
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
362 chipid |= ((majorid >> 4) & 0xF) << 16;
363
364 minorid = ((revid >> 0) & 0xFF);
365
366 patchid = ((revid >> 16) & 0xFF);
367
368 /* 8x50 returns 0 for patch release, but it should be 1 */
369 if (cpu_is_qsd8x50())
370 patchid = 1;
371 /* userspace isn't prepared to deal with patch id for these chips yet */
372 else if (cpu_is_msm8960() || cpu_is_msm8x60())
373 patchid = 0;
374
375 chipid |= (minorid << 8) | patchid;
376
377 return chipid;
378}
379
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380static inline bool _rev_match(unsigned int id, unsigned int entry)
381{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600382 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384
385static void
386adreno_identify_gpu(struct adreno_device *adreno_dev)
387{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 unsigned int i, core, major, minor;
389
390 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
391
392 core = (adreno_dev->chip_id >> 24) & 0xff;
393 major = (adreno_dev->chip_id >> 16) & 0xff;
394 minor = (adreno_dev->chip_id >> 8) & 0xff;
395
Jordan Crouse505df9c2011-07-28 08:37:59 -0600396 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
397 if (core == adreno_gpulist[i].core &&
398 _rev_match(major, adreno_gpulist[i].major) &&
399 _rev_match(minor, adreno_gpulist[i].minor)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 break;
401 }
402 }
403
Jordan Crouse505df9c2011-07-28 08:37:59 -0600404 if (i == ARRAY_SIZE(adreno_gpulist)) {
405 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
406 return;
407 }
408
409 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
410 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
411 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
412 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413}
414
415static int __devinit
416adreno_probe(struct platform_device *pdev)
417{
418 struct kgsl_device *device;
419 struct adreno_device *adreno_dev;
420 int status = -EINVAL;
421
422 device = (struct kgsl_device *)pdev->id_entry->driver_data;
423 adreno_dev = ADRENO_DEVICE(device);
424 device->parentdev = &pdev->dev;
425
426 init_completion(&device->recovery_gate);
427
428 status = adreno_ringbuffer_init(device);
429 if (status != 0)
430 goto error;
431
432 status = kgsl_device_platform_probe(device, adreno_isr);
433 if (status)
434 goto error_close_rb;
435
436 adreno_debugfs_init(device);
437
438 kgsl_pwrscale_init(device);
439 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
440
441 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
442 return 0;
443
444error_close_rb:
445 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
446error:
447 device->parentdev = NULL;
448 return status;
449}
450
451static int __devexit adreno_remove(struct platform_device *pdev)
452{
453 struct kgsl_device *device;
454 struct adreno_device *adreno_dev;
455
456 device = (struct kgsl_device *)pdev->id_entry->driver_data;
457 adreno_dev = ADRENO_DEVICE(device);
458
459 kgsl_pwrscale_detach_policy(device);
460 kgsl_pwrscale_close(device);
461
462 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
463 kgsl_device_platform_remove(device);
464
465 return 0;
466}
467
468static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
469{
470 int status = -EINVAL;
471 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
472 int init_reftimestamp = 0x7fffffff;
473
474 device->state = KGSL_STATE_INIT;
475 device->requested_state = KGSL_STATE_NONE;
476
477 /* Power up the device */
478 kgsl_pwrctrl_enable(device);
479
480 /* Identify the specific GPU */
481 adreno_identify_gpu(adreno_dev);
482
Jordan Crouse505df9c2011-07-28 08:37:59 -0600483 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
484 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
485 adreno_dev->chip_id);
486 goto error_clk_off;
487 }
488
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600489 if (adreno_is_a20x(adreno_dev)) {
490 /*
491 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
492 * on older gpus
493 */
494 device->mh.mh_intf_cfg1 = 0;
495 device->mh.mh_intf_cfg2 = 0;
496 }
497
498 kgsl_mh_start(device);
499
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 if (kgsl_mmu_start(device))
501 goto error_clk_off;
502
503 /*We need to make sure all blocks are powered up and clocked before
504 *issuing a soft reset. The overrides will then be turned off (set to 0)
505 */
506 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0xfffffffe);
507 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0xffffffff);
508
509 /* Only reset CP block if all blocks have previously been reset */
510 if (!(device->flags & KGSL_FLAGS_SOFT_RESET) ||
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600511 !adreno_is_a22x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0xFFFFFFFF);
513 device->flags |= KGSL_FLAGS_SOFT_RESET;
514 } else
515 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000001);
516
517 /* The core is in an indeterminate state until the reset completes
518 * after 30ms.
519 */
520 msleep(30);
521
522 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000000);
523
524 adreno_regwrite(device, REG_RBBM_CNTL, 0x00004442);
525
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600526 if (adreno_is_a225(adreno_dev)) {
527 /* Enable large instruction store for A225 */
528 adreno_regwrite(device, REG_SQ_FLOW_CONTROL, 0x18000000);
529 }
530
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531 adreno_regwrite(device, REG_SQ_VS_PROGRAM, 0x00000000);
532 adreno_regwrite(device, REG_SQ_PS_PROGRAM, 0x00000000);
533
534 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0);
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600535 if (!adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0);
537 else
538 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0x80);
539
540 kgsl_sharedmem_writel(&device->memstore,
541 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
542 init_reftimestamp);
543
544 adreno_regwrite(device, REG_RBBM_DEBUG, 0x00080000);
545
546 /* Make sure interrupts are disabled */
547
548 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
549 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
550 adreno_regwrite(device, REG_SQ_INT_CNTL, 0);
551
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600552 if (adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553 adreno_dev->gmemspace.sizebytes = SZ_512K;
554 else
555 adreno_dev->gmemspace.sizebytes = SZ_256K;
556 adreno_gmeminit(adreno_dev);
557
558 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
559
560 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
561 if (status != 0)
562 goto error_irq_off;
563
564 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
565 return status;
566
567error_irq_off:
568 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600569 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570error_clk_off:
571 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572
573 return status;
574}
575
576static int adreno_stop(struct kgsl_device *device)
577{
578 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
579
580 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben1757a852011-07-11 16:04:38 -0600581 del_timer_sync(&device->idle_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582
583 adreno_dev->drawctxt_active = NULL;
584
585 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
586
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 kgsl_mmu_stop(device);
588
589 /* Power down the device */
590 kgsl_pwrctrl_disable(device);
591
592 return 0;
593}
594
595static int
596adreno_recover_hang(struct kgsl_device *device)
597{
598 int ret;
599 unsigned int *rb_buffer;
600 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
601 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
602 unsigned int timestamp;
603 unsigned int num_rb_contents;
604 unsigned int bad_context;
605 unsigned int reftimestamp;
606 unsigned int enable_ts;
607 unsigned int soptimestamp;
608 unsigned int eoptimestamp;
609 struct adreno_context *drawctxt;
610
611 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
612 rb_buffer = vmalloc(rb->buffer_desc.size);
613 if (!rb_buffer) {
614 KGSL_MEM_ERR(device,
615 "Failed to allocate memory for recovery: %x\n",
616 rb->buffer_desc.size);
617 return -ENOMEM;
618 }
619 /* Extract valid contents from rb which can stil be executed after
620 * hang */
621 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
622 if (ret)
623 goto done;
624 timestamp = rb->timestamp;
625 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
626 kgsl_sharedmem_readl(&device->memstore, &bad_context,
627 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
628 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
629 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
630 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
631 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
632 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
633 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
634 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
635 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
636 /* Make sure memory is synchronized before restarting the GPU */
637 mb();
638 KGSL_CTXT_ERR(device,
639 "Context that caused a GPU hang: %x\n", bad_context);
640 /* restart device */
641 ret = adreno_stop(device);
642 if (ret)
643 goto done;
644 ret = adreno_start(device, true);
645 if (ret)
646 goto done;
647 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
648 /* Restore timestamp states */
649 kgsl_sharedmem_writel(&device->memstore,
650 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
651 soptimestamp);
652 kgsl_sharedmem_writel(&device->memstore,
653 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
654 eoptimestamp);
655 kgsl_sharedmem_writel(&device->memstore,
656 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
657 soptimestamp);
658 if (num_rb_contents) {
659 kgsl_sharedmem_writel(&device->memstore,
660 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
661 reftimestamp);
662 kgsl_sharedmem_writel(&device->memstore,
663 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
664 enable_ts);
665 }
666 /* Make sure all writes are posted before the GPU reads them */
667 wmb();
668 /* Mark the invalid context so no more commands are accepted from
669 * that context */
670
671 drawctxt = (struct adreno_context *) bad_context;
672
673 KGSL_CTXT_ERR(device,
674 "Context that caused a GPU hang: %x\n", bad_context);
675
676 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
677
678 /* Restore valid commands in ringbuffer */
679 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
680 rb->timestamp = timestamp;
681done:
682 vfree(rb_buffer);
683 return ret;
684}
685
686static int
687adreno_dump_and_recover(struct kgsl_device *device)
688{
689 static int recovery;
690 int result = -ETIMEDOUT;
691
692 if (device->state == KGSL_STATE_HUNG)
693 goto done;
694 if (device->state == KGSL_STATE_DUMP_AND_RECOVER && !recovery) {
695 mutex_unlock(&device->mutex);
696 wait_for_completion(&device->recovery_gate);
697 mutex_lock(&device->mutex);
698 if (!(device->state & KGSL_STATE_HUNG))
699 /* recovery success */
700 result = 0;
701 } else {
702 INIT_COMPLETION(device->recovery_gate);
703 /* Detected a hang - trigger an automatic dump */
704 adreno_postmortem_dump(device, 0);
705 if (!recovery) {
706 recovery = 1;
707 result = adreno_recover_hang(device);
708 if (result)
709 device->state = KGSL_STATE_HUNG;
710 recovery = 0;
711 complete_all(&device->recovery_gate);
712 } else
713 KGSL_DRV_ERR(device,
714 "Cannot recover from another hang while "
715 "recovering from a hang\n");
716 }
717done:
718 return result;
719}
720
721static int adreno_getproperty(struct kgsl_device *device,
722 enum kgsl_property_type type,
723 void *value,
724 unsigned int sizebytes)
725{
726 int status = -EINVAL;
727 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
728
729 switch (type) {
730 case KGSL_PROP_DEVICE_INFO:
731 {
732 struct kgsl_devinfo devinfo;
733
734 if (sizebytes != sizeof(devinfo)) {
735 status = -EINVAL;
736 break;
737 }
738
739 memset(&devinfo, 0, sizeof(devinfo));
740 devinfo.device_id = device->id+1;
741 devinfo.chip_id = adreno_dev->chip_id;
742 devinfo.mmu_enabled = kgsl_mmu_enabled();
743 devinfo.gpu_id = adreno_dev->gpurev;
744 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
745 gpu_base;
746 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
747 sizebytes;
748
749 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
750 0) {
751 status = -EFAULT;
752 break;
753 }
754 status = 0;
755 }
756 break;
757 case KGSL_PROP_DEVICE_SHADOW:
758 {
759 struct kgsl_shadowprop shadowprop;
760
761 if (sizebytes != sizeof(shadowprop)) {
762 status = -EINVAL;
763 break;
764 }
765 memset(&shadowprop, 0, sizeof(shadowprop));
766 if (device->memstore.hostptr) {
767 /*NOTE: with mmu enabled, gpuaddr doesn't mean
768 * anything to mmap().
769 */
770 shadowprop.gpuaddr = device->memstore.physaddr;
771 shadowprop.size = device->memstore.size;
772 /* GSL needs this to be set, even if it
773 appears to be meaningless */
774 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
775 }
776 if (copy_to_user(value, &shadowprop,
777 sizeof(shadowprop))) {
778 status = -EFAULT;
779 break;
780 }
781 status = 0;
782 }
783 break;
784 case KGSL_PROP_MMU_ENABLE:
785 {
786#ifdef CONFIG_MSM_KGSL_MMU
787 int mmuProp = 1;
788#else
789 int mmuProp = 0;
790#endif
791 if (sizebytes != sizeof(int)) {
792 status = -EINVAL;
793 break;
794 }
795 if (copy_to_user(value, &mmuProp, sizeof(mmuProp))) {
796 status = -EFAULT;
797 break;
798 }
799 status = 0;
800 }
801 break;
802 case KGSL_PROP_INTERRUPT_WAITS:
803 {
804 int int_waits = 1;
805 if (sizebytes != sizeof(int)) {
806 status = -EINVAL;
807 break;
808 }
809 if (copy_to_user(value, &int_waits, sizeof(int))) {
810 status = -EFAULT;
811 break;
812 }
813 status = 0;
814 }
815 break;
816 default:
817 status = -EINVAL;
818 }
819
820 return status;
821}
822
823/* Caller must hold the device mutex. */
824int adreno_idle(struct kgsl_device *device, unsigned int timeout)
825{
826 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
827 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
828 unsigned int rbbm_status;
829 unsigned long wait_time = jiffies + MAX_WAITGPU_SECS;
830
831 kgsl_cffdump_regpoll(device->id, REG_RBBM_STATUS << 2,
832 0x00000000, 0x80000000);
833 /* first, wait until the CP has consumed all the commands in
834 * the ring buffer
835 */
836retry:
837 if (rb->flags & KGSL_FLAGS_STARTED) {
838 do {
839 GSL_RB_GET_READPTR(rb, &rb->rptr);
840 if (time_after(jiffies, wait_time)) {
841 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
842 rb->rptr, rb->wptr);
843 goto err;
844 }
845 } while (rb->rptr != rb->wptr);
846 }
847
848 /* now, wait for the GPU to finish its operations */
849 wait_time = jiffies + MAX_WAITGPU_SECS;
850 while (time_before(jiffies, wait_time)) {
851 adreno_regread(device, REG_RBBM_STATUS, &rbbm_status);
852 if (rbbm_status == 0x110)
853 return 0;
854 }
855
856err:
857 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
858 if (!adreno_dump_and_recover(device)) {
859 wait_time = jiffies + MAX_WAITGPU_SECS;
860 goto retry;
861 }
862 return -ETIMEDOUT;
863}
864
865static unsigned int adreno_isidle(struct kgsl_device *device)
866{
867 int status = false;
868 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
869 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
870 unsigned int rbbm_status;
871
872 if (rb->flags & KGSL_FLAGS_STARTED) {
873 /* Is the ring buffer is empty? */
874 GSL_RB_GET_READPTR(rb, &rb->rptr);
875 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
876 /* Is the core idle? */
877 adreno_regread(device, REG_RBBM_STATUS,
878 &rbbm_status);
879 if (rbbm_status == 0x110)
880 status = true;
881 }
882 } else {
883 KGSL_DRV_ERR(device, "ringbuffer not started\n");
884 BUG();
885 }
886 return status;
887}
888
889/* Caller must hold the device mutex. */
890static int adreno_suspend_context(struct kgsl_device *device)
891{
892 int status = 0;
893 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
894
895 /* switch to NULL ctxt */
896 if (adreno_dev->drawctxt_active != NULL) {
897 adreno_drawctxt_switch(adreno_dev, NULL, 0);
898 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
899 }
900
901 return status;
902}
903
904uint8_t *kgsl_sharedmem_convertaddr(struct kgsl_device *device,
905 unsigned int pt_base, unsigned int gpuaddr, unsigned int *size)
906{
907 uint8_t *result = NULL;
908 struct kgsl_mem_entry *entry;
909 struct kgsl_process_private *priv;
910 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
911 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
912
913 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr)) {
914 return kgsl_gpuaddr_to_vaddr(&ringbuffer->buffer_desc,
915 gpuaddr, size);
916 }
917
918 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr)) {
919 return kgsl_gpuaddr_to_vaddr(&ringbuffer->memptrs_desc,
920 gpuaddr, size);
921 }
922
923 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr)) {
924 return kgsl_gpuaddr_to_vaddr(&device->memstore,
925 gpuaddr, size);
926 }
927
928 mutex_lock(&kgsl_driver.process_mutex);
929 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
930 if (pt_base != 0
931 && priv->pagetable
932 && priv->pagetable->base.gpuaddr != pt_base) {
933 continue;
934 }
935
936 spin_lock(&priv->mem_lock);
937 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
938 sizeof(unsigned int));
939 if (entry) {
940 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
941 gpuaddr, size);
942 spin_unlock(&priv->mem_lock);
943 mutex_unlock(&kgsl_driver.process_mutex);
944 return result;
945 }
946 spin_unlock(&priv->mem_lock);
947 }
948 mutex_unlock(&kgsl_driver.process_mutex);
949
950 BUG_ON(!mutex_is_locked(&device->mutex));
951 list_for_each_entry(entry, &device->memqueue, list) {
952 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr)) {
953 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
954 gpuaddr, size);
955 break;
956 }
957
958 }
959 return result;
960}
961
962void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
963 unsigned int *value)
964{
965 unsigned int *reg;
966 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
967 reg = (unsigned int *)(device->regspace.mmio_virt_base
968 + (offsetwords << 2));
969
970 if (!in_interrupt())
971 kgsl_pre_hwaccess(device);
972
973 /*ensure this read finishes before the next one.
974 * i.e. act like normal readl() */
975 *value = __raw_readl(reg);
976 rmb();
977}
978
979void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
980 unsigned int value)
981{
982 unsigned int *reg;
983
984 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
985
986 if (!in_interrupt())
987 kgsl_pre_hwaccess(device);
988
989 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
990 reg = (unsigned int *)(device->regspace.mmio_virt_base
991 + (offsetwords << 2));
992
993 /*ensure previous writes post before this one,
994 * i.e. act like normal writel() */
995 wmb();
996 __raw_writel(value, reg);
997}
998
999static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1000 unsigned int timestamp)
1001{
1002 int status;
1003 unsigned int ref_ts, enableflag;
1004
1005 status = kgsl_check_timestamp(device, timestamp);
1006 if (!status) {
1007 mutex_lock(&device->mutex);
1008 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1009 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1010 mb();
1011
1012 if (enableflag) {
1013 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1014 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1015 mb();
1016 if (timestamp_cmp(ref_ts, timestamp)) {
1017 kgsl_sharedmem_writel(&device->memstore,
1018 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1019 timestamp);
1020 wmb();
1021 }
1022 } else {
1023 unsigned int cmds[2];
1024 kgsl_sharedmem_writel(&device->memstore,
1025 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1026 timestamp);
1027 enableflag = 1;
1028 kgsl_sharedmem_writel(&device->memstore,
1029 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1030 enableflag);
1031 wmb();
1032 /* submit a dummy packet so that even if all
1033 * commands upto timestamp get executed we will still
1034 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001035 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036 cmds[1] = 0;
1037 adreno_ringbuffer_issuecmds(device, 0, &cmds[0], 2);
1038 }
1039 mutex_unlock(&device->mutex);
1040 }
1041
1042 return status;
1043}
1044
1045/*
1046 wait_io_event_interruptible_timeout checks for the exit condition before
1047 placing a process in wait q. For conditional interrupts we expect the
1048 process to already be in its wait q when its exit condition checking
1049 function is called.
1050*/
1051#define kgsl_wait_io_event_interruptible_timeout(wq, condition, timeout)\
1052({ \
1053 long __ret = timeout; \
1054 __wait_io_event_interruptible_timeout(wq, condition, __ret); \
1055 __ret; \
1056})
1057
1058/* MUST be called with the device mutex held */
1059static int adreno_waittimestamp(struct kgsl_device *device,
1060 unsigned int timestamp,
1061 unsigned int msecs)
1062{
1063 long status = 0;
1064 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1065
1066 if (timestamp != adreno_dev->ringbuffer.timestamp &&
1067 timestamp_cmp(timestamp,
1068 adreno_dev->ringbuffer.timestamp)) {
1069 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1070 "rb->timestamp: %x\n",
1071 timestamp, adreno_dev->ringbuffer.timestamp);
1072 status = -EINVAL;
1073 goto done;
1074 }
1075 if (!kgsl_check_timestamp(device, timestamp)) {
1076 mutex_unlock(&device->mutex);
1077 /* We need to make sure that the process is placed in wait-q
1078 * before its condition is called */
1079 status = kgsl_wait_io_event_interruptible_timeout(
1080 device->wait_queue,
1081 kgsl_check_interrupt_timestamp(device,
1082 timestamp), msecs_to_jiffies(msecs));
1083 mutex_lock(&device->mutex);
1084
1085 if (status > 0)
1086 status = 0;
1087 else if (status == 0) {
1088 if (!kgsl_check_timestamp(device, timestamp)) {
1089 status = -ETIMEDOUT;
1090 KGSL_DRV_ERR(device,
1091 "Device hang detected while waiting "
1092 "for timestamp: %x, last "
1093 "submitted(rb->timestamp): %x, wptr: "
1094 "%x\n", timestamp,
1095 adreno_dev->ringbuffer.timestamp,
1096 adreno_dev->ringbuffer.wptr);
1097 if (!adreno_dump_and_recover(device)) {
1098 /* wait for idle after recovery as the
1099 * timestamp that this process wanted
1100 * to wait on may be invalid */
1101 if (!adreno_idle(device,
1102 KGSL_TIMEOUT_DEFAULT))
1103 status = 0;
1104 }
1105 }
1106 }
1107 }
1108
1109done:
1110 return (int)status;
1111}
1112
1113static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1114 enum kgsl_timestamp_type type)
1115{
1116 unsigned int timestamp = 0;
1117
1118 if (type == KGSL_TIMESTAMP_CONSUMED)
1119 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1120 else if (type == KGSL_TIMESTAMP_RETIRED)
1121 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1122 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1123 rmb();
1124
1125 return timestamp;
1126}
1127
1128static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1129 unsigned int cmd, void *data)
1130{
1131 int result = 0;
1132 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1133 struct kgsl_context *context;
1134
1135 switch (cmd) {
1136 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1137 binbase = data;
1138
1139 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1140 if (context) {
1141 adreno_drawctxt_set_bin_base_offset(
1142 dev_priv->device, context, binbase->offset);
1143 } else {
1144 result = -EINVAL;
1145 KGSL_DRV_ERR(dev_priv->device,
1146 "invalid drawctxt drawctxt_id %d "
1147 "device_id=%d\n",
1148 binbase->drawctxt_id, dev_priv->device->id);
1149 }
1150 break;
1151
1152 default:
1153 KGSL_DRV_INFO(dev_priv->device,
1154 "invalid ioctl code %08x\n", cmd);
1155 result = -EINVAL;
1156 break;
1157 }
1158 return result;
1159
1160}
1161
1162static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1163{
1164 gpu_freq /= 1000000;
1165 return ticks / gpu_freq;
1166}
1167
1168static void adreno_power_stats(struct kgsl_device *device,
1169 struct kgsl_power_stats *stats)
1170{
1171 unsigned int reg;
1172 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
1173
1174 /* In order to calculate idle you have to have run the algorithm *
1175 * at least once to get a start time. */
1176 if (pwr->time != 0) {
1177 s64 tmp;
1178 /* Stop the performance moniter and read the current *
1179 * busy cycles. */
1180 adreno_regwrite(device,
1181 REG_CP_PERFMON_CNTL,
1182 REG_PERF_MODE_CNT |
1183 REG_PERF_STATE_FREEZE);
1184 adreno_regread(device, REG_RBBM_PERFCOUNTER1_LO, &reg);
1185 tmp = ktime_to_us(ktime_get());
1186 stats->total_time = tmp - pwr->time;
1187 pwr->time = tmp;
1188 stats->busy_time = adreno_ticks_to_us(reg, device->pwrctrl.
1189 pwrlevels[device->pwrctrl.active_pwrlevel].
1190 gpu_freq);
1191
1192 adreno_regwrite(device,
1193 REG_CP_PERFMON_CNTL,
1194 REG_PERF_MODE_CNT |
1195 REG_PERF_STATE_RESET);
1196 } else {
1197 stats->total_time = 0;
1198 stats->busy_time = 0;
1199 pwr->time = ktime_to_us(ktime_get());
1200 }
1201
1202 /* re-enable the performance moniters */
1203 adreno_regread(device, REG_RBBM_PM_OVERRIDE2, &reg);
1204 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, (reg | 0x40));
1205 adreno_regwrite(device, REG_RBBM_PERFCOUNTER1_SELECT, 0x1);
1206 adreno_regwrite(device,
1207 REG_CP_PERFMON_CNTL,
1208 REG_PERF_MODE_CNT | REG_PERF_STATE_ENABLE);
1209}
1210
1211void adreno_irqctrl(struct kgsl_device *device, int state)
1212{
Jordan Crousea78c9172011-07-11 13:14:09 -06001213 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1214 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215}
1216
1217static const struct kgsl_functable adreno_functable = {
1218 /* Mandatory functions */
1219 .regread = adreno_regread,
1220 .regwrite = adreno_regwrite,
1221 .idle = adreno_idle,
1222 .isidle = adreno_isidle,
1223 .suspend_context = adreno_suspend_context,
1224 .start = adreno_start,
1225 .stop = adreno_stop,
1226 .getproperty = adreno_getproperty,
1227 .waittimestamp = adreno_waittimestamp,
1228 .readtimestamp = adreno_readtimestamp,
1229 .issueibcmds = adreno_ringbuffer_issueibcmds,
1230 .ioctl = adreno_ioctl,
1231 .setup_pt = adreno_setup_pt,
1232 .cleanup_pt = adreno_cleanup_pt,
1233 .power_stats = adreno_power_stats,
1234 .irqctrl = adreno_irqctrl,
1235 /* Optional functions */
1236 .setstate = adreno_setstate,
1237 .drawctxt_create = adreno_drawctxt_create,
1238 .drawctxt_destroy = adreno_drawctxt_destroy,
1239};
1240
1241static struct platform_device_id adreno_id_table[] = {
1242 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1243 { },
1244};
1245MODULE_DEVICE_TABLE(platform, adreno_id_table);
1246
1247static struct platform_driver adreno_platform_driver = {
1248 .probe = adreno_probe,
1249 .remove = __devexit_p(adreno_remove),
1250 .suspend = kgsl_suspend_driver,
1251 .resume = kgsl_resume_driver,
1252 .id_table = adreno_id_table,
1253 .driver = {
1254 .owner = THIS_MODULE,
1255 .name = DEVICE_3D_NAME,
1256 .pm = &kgsl_pm_ops,
1257 }
1258};
1259
1260static int __init kgsl_3d_init(void)
1261{
1262 return platform_driver_register(&adreno_platform_driver);
1263}
1264
1265static void __exit kgsl_3d_exit(void)
1266{
1267 platform_driver_unregister(&adreno_platform_driver);
1268}
1269
1270module_init(kgsl_3d_init);
1271module_exit(kgsl_3d_exit);
1272
1273MODULE_DESCRIPTION("3D Graphics driver");
1274MODULE_VERSION("1.2");
1275MODULE_LICENSE("GPL v2");
1276MODULE_ALIAS("platform:kgsl_3d");