blob: 4cd1aa320895bc7d2b82ce2e703b4391a9e29ce6 [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
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 Kalisamy3fcdb332011-09-02 20:01:06 +0530548 adreno_regwrite(device, REG_RBBM_DEBUG, 0x000C0000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549
550 /* Make sure interrupts are disabled */
551
552 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
553 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
554 adreno_regwrite(device, REG_SQ_INT_CNTL, 0);
555
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600556 if (adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 adreno_dev->gmemspace.sizebytes = SZ_512K;
558 else
559 adreno_dev->gmemspace.sizebytes = SZ_256K;
560 adreno_gmeminit(adreno_dev);
561
562 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
563
564 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
565 if (status != 0)
566 goto error_irq_off;
567
568 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
569 return status;
570
571error_irq_off:
572 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600573 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574error_clk_off:
575 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576
577 return status;
578}
579
580static int adreno_stop(struct kgsl_device *device)
581{
582 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
583
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 adreno_dev->drawctxt_active = NULL;
585
586 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
587
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588 kgsl_mmu_stop(device);
589
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600590 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
591 del_timer_sync(&device->idle_timer);
592
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 /* Power down the device */
594 kgsl_pwrctrl_disable(device);
595
596 return 0;
597}
598
599static int
600adreno_recover_hang(struct kgsl_device *device)
601{
602 int ret;
603 unsigned int *rb_buffer;
604 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
605 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
606 unsigned int timestamp;
607 unsigned int num_rb_contents;
608 unsigned int bad_context;
609 unsigned int reftimestamp;
610 unsigned int enable_ts;
611 unsigned int soptimestamp;
612 unsigned int eoptimestamp;
613 struct adreno_context *drawctxt;
614
615 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
616 rb_buffer = vmalloc(rb->buffer_desc.size);
617 if (!rb_buffer) {
618 KGSL_MEM_ERR(device,
619 "Failed to allocate memory for recovery: %x\n",
620 rb->buffer_desc.size);
621 return -ENOMEM;
622 }
623 /* Extract valid contents from rb which can stil be executed after
624 * hang */
625 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
626 if (ret)
627 goto done;
628 timestamp = rb->timestamp;
629 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
630 kgsl_sharedmem_readl(&device->memstore, &bad_context,
631 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
632 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
633 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
634 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
635 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
636 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
637 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
638 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
639 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
640 /* Make sure memory is synchronized before restarting the GPU */
641 mb();
642 KGSL_CTXT_ERR(device,
643 "Context that caused a GPU hang: %x\n", bad_context);
644 /* restart device */
645 ret = adreno_stop(device);
646 if (ret)
647 goto done;
648 ret = adreno_start(device, true);
649 if (ret)
650 goto done;
651 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
652 /* Restore timestamp states */
653 kgsl_sharedmem_writel(&device->memstore,
654 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
655 soptimestamp);
656 kgsl_sharedmem_writel(&device->memstore,
657 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
658 eoptimestamp);
659 kgsl_sharedmem_writel(&device->memstore,
660 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
661 soptimestamp);
662 if (num_rb_contents) {
663 kgsl_sharedmem_writel(&device->memstore,
664 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
665 reftimestamp);
666 kgsl_sharedmem_writel(&device->memstore,
667 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
668 enable_ts);
669 }
670 /* Make sure all writes are posted before the GPU reads them */
671 wmb();
672 /* Mark the invalid context so no more commands are accepted from
673 * that context */
674
675 drawctxt = (struct adreno_context *) bad_context;
676
677 KGSL_CTXT_ERR(device,
678 "Context that caused a GPU hang: %x\n", bad_context);
679
680 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
681
682 /* Restore valid commands in ringbuffer */
683 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
684 rb->timestamp = timestamp;
685done:
686 vfree(rb_buffer);
687 return ret;
688}
689
690static int
691adreno_dump_and_recover(struct kgsl_device *device)
692{
693 static int recovery;
694 int result = -ETIMEDOUT;
695
696 if (device->state == KGSL_STATE_HUNG)
697 goto done;
698 if (device->state == KGSL_STATE_DUMP_AND_RECOVER && !recovery) {
699 mutex_unlock(&device->mutex);
700 wait_for_completion(&device->recovery_gate);
701 mutex_lock(&device->mutex);
702 if (!(device->state & KGSL_STATE_HUNG))
703 /* recovery success */
704 result = 0;
705 } else {
706 INIT_COMPLETION(device->recovery_gate);
707 /* Detected a hang - trigger an automatic dump */
708 adreno_postmortem_dump(device, 0);
709 if (!recovery) {
710 recovery = 1;
711 result = adreno_recover_hang(device);
712 if (result)
713 device->state = KGSL_STATE_HUNG;
714 recovery = 0;
715 complete_all(&device->recovery_gate);
716 } else
717 KGSL_DRV_ERR(device,
718 "Cannot recover from another hang while "
719 "recovering from a hang\n");
720 }
721done:
722 return result;
723}
724
725static int adreno_getproperty(struct kgsl_device *device,
726 enum kgsl_property_type type,
727 void *value,
728 unsigned int sizebytes)
729{
730 int status = -EINVAL;
731 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
732
733 switch (type) {
734 case KGSL_PROP_DEVICE_INFO:
735 {
736 struct kgsl_devinfo devinfo;
737
738 if (sizebytes != sizeof(devinfo)) {
739 status = -EINVAL;
740 break;
741 }
742
743 memset(&devinfo, 0, sizeof(devinfo));
744 devinfo.device_id = device->id+1;
745 devinfo.chip_id = adreno_dev->chip_id;
746 devinfo.mmu_enabled = kgsl_mmu_enabled();
747 devinfo.gpu_id = adreno_dev->gpurev;
748 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
749 gpu_base;
750 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
751 sizebytes;
752
753 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
754 0) {
755 status = -EFAULT;
756 break;
757 }
758 status = 0;
759 }
760 break;
761 case KGSL_PROP_DEVICE_SHADOW:
762 {
763 struct kgsl_shadowprop shadowprop;
764
765 if (sizebytes != sizeof(shadowprop)) {
766 status = -EINVAL;
767 break;
768 }
769 memset(&shadowprop, 0, sizeof(shadowprop));
770 if (device->memstore.hostptr) {
771 /*NOTE: with mmu enabled, gpuaddr doesn't mean
772 * anything to mmap().
773 */
774 shadowprop.gpuaddr = device->memstore.physaddr;
775 shadowprop.size = device->memstore.size;
776 /* GSL needs this to be set, even if it
777 appears to be meaningless */
778 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
779 }
780 if (copy_to_user(value, &shadowprop,
781 sizeof(shadowprop))) {
782 status = -EFAULT;
783 break;
784 }
785 status = 0;
786 }
787 break;
788 case KGSL_PROP_MMU_ENABLE:
789 {
790#ifdef CONFIG_MSM_KGSL_MMU
791 int mmuProp = 1;
792#else
793 int mmuProp = 0;
794#endif
795 if (sizebytes != sizeof(int)) {
796 status = -EINVAL;
797 break;
798 }
799 if (copy_to_user(value, &mmuProp, sizeof(mmuProp))) {
800 status = -EFAULT;
801 break;
802 }
803 status = 0;
804 }
805 break;
806 case KGSL_PROP_INTERRUPT_WAITS:
807 {
808 int int_waits = 1;
809 if (sizebytes != sizeof(int)) {
810 status = -EINVAL;
811 break;
812 }
813 if (copy_to_user(value, &int_waits, sizeof(int))) {
814 status = -EFAULT;
815 break;
816 }
817 status = 0;
818 }
819 break;
820 default:
821 status = -EINVAL;
822 }
823
824 return status;
825}
826
827/* Caller must hold the device mutex. */
828int adreno_idle(struct kgsl_device *device, unsigned int timeout)
829{
830 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
831 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
832 unsigned int rbbm_status;
833 unsigned long wait_time = jiffies + MAX_WAITGPU_SECS;
834
835 kgsl_cffdump_regpoll(device->id, REG_RBBM_STATUS << 2,
836 0x00000000, 0x80000000);
837 /* first, wait until the CP has consumed all the commands in
838 * the ring buffer
839 */
840retry:
841 if (rb->flags & KGSL_FLAGS_STARTED) {
842 do {
843 GSL_RB_GET_READPTR(rb, &rb->rptr);
844 if (time_after(jiffies, wait_time)) {
845 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
846 rb->rptr, rb->wptr);
847 goto err;
848 }
849 } while (rb->rptr != rb->wptr);
850 }
851
852 /* now, wait for the GPU to finish its operations */
853 wait_time = jiffies + MAX_WAITGPU_SECS;
854 while (time_before(jiffies, wait_time)) {
855 adreno_regread(device, REG_RBBM_STATUS, &rbbm_status);
856 if (rbbm_status == 0x110)
857 return 0;
858 }
859
860err:
861 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
862 if (!adreno_dump_and_recover(device)) {
863 wait_time = jiffies + MAX_WAITGPU_SECS;
864 goto retry;
865 }
866 return -ETIMEDOUT;
867}
868
869static unsigned int adreno_isidle(struct kgsl_device *device)
870{
871 int status = false;
872 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
873 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
874 unsigned int rbbm_status;
875
876 if (rb->flags & KGSL_FLAGS_STARTED) {
877 /* Is the ring buffer is empty? */
878 GSL_RB_GET_READPTR(rb, &rb->rptr);
879 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
880 /* Is the core idle? */
881 adreno_regread(device, REG_RBBM_STATUS,
882 &rbbm_status);
883 if (rbbm_status == 0x110)
884 status = true;
885 }
886 } else {
887 KGSL_DRV_ERR(device, "ringbuffer not started\n");
888 BUG();
889 }
890 return status;
891}
892
893/* Caller must hold the device mutex. */
894static int adreno_suspend_context(struct kgsl_device *device)
895{
896 int status = 0;
897 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
898
899 /* switch to NULL ctxt */
900 if (adreno_dev->drawctxt_active != NULL) {
901 adreno_drawctxt_switch(adreno_dev, NULL, 0);
902 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
903 }
904
905 return status;
906}
907
908uint8_t *kgsl_sharedmem_convertaddr(struct kgsl_device *device,
909 unsigned int pt_base, unsigned int gpuaddr, unsigned int *size)
910{
911 uint8_t *result = NULL;
912 struct kgsl_mem_entry *entry;
913 struct kgsl_process_private *priv;
914 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
915 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
916
917 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr)) {
918 return kgsl_gpuaddr_to_vaddr(&ringbuffer->buffer_desc,
919 gpuaddr, size);
920 }
921
922 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr)) {
923 return kgsl_gpuaddr_to_vaddr(&ringbuffer->memptrs_desc,
924 gpuaddr, size);
925 }
926
927 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr)) {
928 return kgsl_gpuaddr_to_vaddr(&device->memstore,
929 gpuaddr, size);
930 }
931
932 mutex_lock(&kgsl_driver.process_mutex);
933 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
934 if (pt_base != 0
935 && priv->pagetable
936 && priv->pagetable->base.gpuaddr != pt_base) {
937 continue;
938 }
939
940 spin_lock(&priv->mem_lock);
941 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
942 sizeof(unsigned int));
943 if (entry) {
944 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
945 gpuaddr, size);
946 spin_unlock(&priv->mem_lock);
947 mutex_unlock(&kgsl_driver.process_mutex);
948 return result;
949 }
950 spin_unlock(&priv->mem_lock);
951 }
952 mutex_unlock(&kgsl_driver.process_mutex);
953
954 BUG_ON(!mutex_is_locked(&device->mutex));
955 list_for_each_entry(entry, &device->memqueue, list) {
956 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr)) {
957 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
958 gpuaddr, size);
959 break;
960 }
961
962 }
963 return result;
964}
965
966void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
967 unsigned int *value)
968{
969 unsigned int *reg;
970 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
971 reg = (unsigned int *)(device->regspace.mmio_virt_base
972 + (offsetwords << 2));
973
974 if (!in_interrupt())
975 kgsl_pre_hwaccess(device);
976
977 /*ensure this read finishes before the next one.
978 * i.e. act like normal readl() */
979 *value = __raw_readl(reg);
980 rmb();
981}
982
983void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
984 unsigned int value)
985{
986 unsigned int *reg;
987
988 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
989
990 if (!in_interrupt())
991 kgsl_pre_hwaccess(device);
992
993 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
994 reg = (unsigned int *)(device->regspace.mmio_virt_base
995 + (offsetwords << 2));
996
997 /*ensure previous writes post before this one,
998 * i.e. act like normal writel() */
999 wmb();
1000 __raw_writel(value, reg);
1001}
1002
1003static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1004 unsigned int timestamp)
1005{
1006 int status;
1007 unsigned int ref_ts, enableflag;
1008
1009 status = kgsl_check_timestamp(device, timestamp);
1010 if (!status) {
1011 mutex_lock(&device->mutex);
1012 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1013 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1014 mb();
1015
1016 if (enableflag) {
1017 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1018 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1019 mb();
1020 if (timestamp_cmp(ref_ts, timestamp)) {
1021 kgsl_sharedmem_writel(&device->memstore,
1022 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1023 timestamp);
1024 wmb();
1025 }
1026 } else {
1027 unsigned int cmds[2];
1028 kgsl_sharedmem_writel(&device->memstore,
1029 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1030 timestamp);
1031 enableflag = 1;
1032 kgsl_sharedmem_writel(&device->memstore,
1033 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1034 enableflag);
1035 wmb();
1036 /* submit a dummy packet so that even if all
1037 * commands upto timestamp get executed we will still
1038 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001039 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040 cmds[1] = 0;
1041 adreno_ringbuffer_issuecmds(device, 0, &cmds[0], 2);
1042 }
1043 mutex_unlock(&device->mutex);
1044 }
1045
1046 return status;
1047}
1048
1049/*
1050 wait_io_event_interruptible_timeout checks for the exit condition before
1051 placing a process in wait q. For conditional interrupts we expect the
1052 process to already be in its wait q when its exit condition checking
1053 function is called.
1054*/
1055#define kgsl_wait_io_event_interruptible_timeout(wq, condition, timeout)\
1056({ \
1057 long __ret = timeout; \
1058 __wait_io_event_interruptible_timeout(wq, condition, __ret); \
1059 __ret; \
1060})
1061
1062/* MUST be called with the device mutex held */
1063static int adreno_waittimestamp(struct kgsl_device *device,
1064 unsigned int timestamp,
1065 unsigned int msecs)
1066{
1067 long status = 0;
1068 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1069
1070 if (timestamp != adreno_dev->ringbuffer.timestamp &&
1071 timestamp_cmp(timestamp,
1072 adreno_dev->ringbuffer.timestamp)) {
1073 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1074 "rb->timestamp: %x\n",
1075 timestamp, adreno_dev->ringbuffer.timestamp);
1076 status = -EINVAL;
1077 goto done;
1078 }
1079 if (!kgsl_check_timestamp(device, timestamp)) {
1080 mutex_unlock(&device->mutex);
1081 /* We need to make sure that the process is placed in wait-q
1082 * before its condition is called */
1083 status = kgsl_wait_io_event_interruptible_timeout(
1084 device->wait_queue,
1085 kgsl_check_interrupt_timestamp(device,
1086 timestamp), msecs_to_jiffies(msecs));
1087 mutex_lock(&device->mutex);
1088
1089 if (status > 0)
1090 status = 0;
1091 else if (status == 0) {
1092 if (!kgsl_check_timestamp(device, timestamp)) {
1093 status = -ETIMEDOUT;
1094 KGSL_DRV_ERR(device,
1095 "Device hang detected while waiting "
1096 "for timestamp: %x, last "
1097 "submitted(rb->timestamp): %x, wptr: "
1098 "%x\n", timestamp,
1099 adreno_dev->ringbuffer.timestamp,
1100 adreno_dev->ringbuffer.wptr);
1101 if (!adreno_dump_and_recover(device)) {
1102 /* wait for idle after recovery as the
1103 * timestamp that this process wanted
1104 * to wait on may be invalid */
1105 if (!adreno_idle(device,
1106 KGSL_TIMEOUT_DEFAULT))
1107 status = 0;
1108 }
1109 }
1110 }
1111 }
1112
1113done:
1114 return (int)status;
1115}
1116
1117static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1118 enum kgsl_timestamp_type type)
1119{
1120 unsigned int timestamp = 0;
1121
1122 if (type == KGSL_TIMESTAMP_CONSUMED)
1123 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1124 else if (type == KGSL_TIMESTAMP_RETIRED)
1125 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1126 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1127 rmb();
1128
1129 return timestamp;
1130}
1131
1132static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1133 unsigned int cmd, void *data)
1134{
1135 int result = 0;
1136 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1137 struct kgsl_context *context;
1138
1139 switch (cmd) {
1140 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1141 binbase = data;
1142
1143 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1144 if (context) {
1145 adreno_drawctxt_set_bin_base_offset(
1146 dev_priv->device, context, binbase->offset);
1147 } else {
1148 result = -EINVAL;
1149 KGSL_DRV_ERR(dev_priv->device,
1150 "invalid drawctxt drawctxt_id %d "
1151 "device_id=%d\n",
1152 binbase->drawctxt_id, dev_priv->device->id);
1153 }
1154 break;
1155
1156 default:
1157 KGSL_DRV_INFO(dev_priv->device,
1158 "invalid ioctl code %08x\n", cmd);
1159 result = -EINVAL;
1160 break;
1161 }
1162 return result;
1163
1164}
1165
1166static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1167{
1168 gpu_freq /= 1000000;
1169 return ticks / gpu_freq;
1170}
1171
1172static void adreno_power_stats(struct kgsl_device *device,
1173 struct kgsl_power_stats *stats)
1174{
1175 unsigned int reg;
1176 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
1177
1178 /* In order to calculate idle you have to have run the algorithm *
1179 * at least once to get a start time. */
1180 if (pwr->time != 0) {
1181 s64 tmp;
1182 /* Stop the performance moniter and read the current *
1183 * busy cycles. */
1184 adreno_regwrite(device,
1185 REG_CP_PERFMON_CNTL,
1186 REG_PERF_MODE_CNT |
1187 REG_PERF_STATE_FREEZE);
1188 adreno_regread(device, REG_RBBM_PERFCOUNTER1_LO, &reg);
1189 tmp = ktime_to_us(ktime_get());
1190 stats->total_time = tmp - pwr->time;
1191 pwr->time = tmp;
1192 stats->busy_time = adreno_ticks_to_us(reg, device->pwrctrl.
1193 pwrlevels[device->pwrctrl.active_pwrlevel].
1194 gpu_freq);
1195
1196 adreno_regwrite(device,
1197 REG_CP_PERFMON_CNTL,
1198 REG_PERF_MODE_CNT |
1199 REG_PERF_STATE_RESET);
1200 } else {
1201 stats->total_time = 0;
1202 stats->busy_time = 0;
1203 pwr->time = ktime_to_us(ktime_get());
1204 }
1205
1206 /* re-enable the performance moniters */
1207 adreno_regread(device, REG_RBBM_PM_OVERRIDE2, &reg);
1208 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, (reg | 0x40));
1209 adreno_regwrite(device, REG_RBBM_PERFCOUNTER1_SELECT, 0x1);
1210 adreno_regwrite(device,
1211 REG_CP_PERFMON_CNTL,
1212 REG_PERF_MODE_CNT | REG_PERF_STATE_ENABLE);
1213}
1214
1215void adreno_irqctrl(struct kgsl_device *device, int state)
1216{
Jordan Crousea78c9172011-07-11 13:14:09 -06001217 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1218 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001219}
1220
1221static const struct kgsl_functable adreno_functable = {
1222 /* Mandatory functions */
1223 .regread = adreno_regread,
1224 .regwrite = adreno_regwrite,
1225 .idle = adreno_idle,
1226 .isidle = adreno_isidle,
1227 .suspend_context = adreno_suspend_context,
1228 .start = adreno_start,
1229 .stop = adreno_stop,
1230 .getproperty = adreno_getproperty,
1231 .waittimestamp = adreno_waittimestamp,
1232 .readtimestamp = adreno_readtimestamp,
1233 .issueibcmds = adreno_ringbuffer_issueibcmds,
1234 .ioctl = adreno_ioctl,
1235 .setup_pt = adreno_setup_pt,
1236 .cleanup_pt = adreno_cleanup_pt,
1237 .power_stats = adreno_power_stats,
1238 .irqctrl = adreno_irqctrl,
1239 /* Optional functions */
1240 .setstate = adreno_setstate,
1241 .drawctxt_create = adreno_drawctxt_create,
1242 .drawctxt_destroy = adreno_drawctxt_destroy,
1243};
1244
1245static struct platform_device_id adreno_id_table[] = {
1246 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1247 { },
1248};
1249MODULE_DEVICE_TABLE(platform, adreno_id_table);
1250
1251static struct platform_driver adreno_platform_driver = {
1252 .probe = adreno_probe,
1253 .remove = __devexit_p(adreno_remove),
1254 .suspend = kgsl_suspend_driver,
1255 .resume = kgsl_resume_driver,
1256 .id_table = adreno_id_table,
1257 .driver = {
1258 .owner = THIS_MODULE,
1259 .name = DEVICE_3D_NAME,
1260 .pm = &kgsl_pm_ops,
1261 }
1262};
1263
1264static int __init kgsl_3d_init(void)
1265{
1266 return platform_driver_register(&adreno_platform_driver);
1267}
1268
1269static void __exit kgsl_3d_exit(void)
1270{
1271 platform_driver_unregister(&adreno_platform_driver);
1272}
1273
1274module_init(kgsl_3d_init);
1275module_exit(kgsl_3d_exit);
1276
1277MODULE_DESCRIPTION("3D Graphics driver");
1278MODULE_VERSION("1.2");
1279MODULE_LICENSE("GPL v2");
1280MODULE_ALIAS("platform:kgsl_3d");