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