blob: 6b9adf8d4b6d5ac4e2c7d68f018dd5d5dd1ed4bd [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
36#define KGSL_RBBM_INT_MASK \
37 (RBBM_INT_CNTL__RDERR_INT_MASK | \
38 RBBM_INT_CNTL__DISPLAY_UPDATE_INT_MASK)
39
40/* Adreno MH arbiter config*/
41#define ADRENO_CFG_MHARB \
42 (0x10 \
43 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
44 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
45 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
46 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
47 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
48 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
50 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
51 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
53 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
54 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
55 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
56 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
57
58#define ADRENO_MMU_CONFIG \
59 (0x01 \
60 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
61 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
62 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
68 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
69 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
70 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
71
72/* max msecs to wait for gpu to finish its operation(s) */
73#define MAX_WAITGPU_SECS (HZ + HZ/2)
74
75static const struct kgsl_functable adreno_functable;
76
77static struct adreno_device device_3d0 = {
78 .dev = {
79 .name = DEVICE_3D0_NAME,
80 .id = KGSL_DEVICE_3D0,
81 .ver_major = DRIVER_VERSION_MAJOR,
82 .ver_minor = DRIVER_VERSION_MINOR,
83 .mmu = {
84 .config = ADRENO_MMU_CONFIG,
85 /* turn off memory protection unit by setting
86 acceptable physical address range to include
87 all pages. */
88 .mpu_base = 0x00000000,
89 .mpu_range = 0xFFFFF000,
90 },
91 .pwrctrl = {
92 .regulator_name = "fs_gfx3d",
93 .irq_name = KGSL_3D0_IRQ,
94 .src_clk_name = "grp_src_clk",
95 },
96 .mutex = __MUTEX_INITIALIZER(device_3d0.dev.mutex),
97 .state = KGSL_STATE_INIT,
98 .active_cnt = 0,
99 .iomemname = KGSL_3D0_REG_MEMORY,
100 .ftbl = &adreno_functable,
101 .display_off = {
102#ifdef CONFIG_HAS_EARLYSUSPEND
103 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
104 .suspend = kgsl_early_suspend_driver,
105 .resume = kgsl_late_resume_driver,
106#endif
107 },
108 },
109 .gmemspace = {
110 .gpu_base = 0,
111 .sizebytes = SZ_256K,
112 },
113 .pfp_fw = NULL,
114 .pm4_fw = NULL,
115 .mharb = ADRENO_CFG_MHARB,
116};
117
118static int adreno_gmeminit(struct adreno_device *adreno_dev)
119{
120 struct kgsl_device *device = &adreno_dev->dev;
121 union reg_rb_edram_info rb_edram_info;
122 unsigned int gmem_size;
123 unsigned int edram_value = 0;
124
125 /* make sure edram range is aligned to size */
126 BUG_ON(adreno_dev->gmemspace.gpu_base &
127 (adreno_dev->gmemspace.sizebytes - 1));
128
129 /* get edram_size value equivalent */
130 gmem_size = (adreno_dev->gmemspace.sizebytes >> 14);
131 while (gmem_size >>= 1)
132 edram_value++;
133
134 rb_edram_info.val = 0;
135
136 rb_edram_info.f.edram_size = edram_value;
137 if (!adreno_is_a220(adreno_dev))
138 rb_edram_info.f.edram_mapping_mode = 0; /* EDRAM_MAP_UPPER */
139
140 /* must be aligned to size */
141 rb_edram_info.f.edram_range = (adreno_dev->gmemspace.gpu_base >> 14);
142
143 adreno_regwrite(device, REG_RB_EDRAM_INFO, rb_edram_info.val);
144
145 return 0;
146}
147
148static int adreno_gmemclose(struct kgsl_device *device)
149{
150 adreno_regwrite(device, REG_RB_EDRAM_INFO, 0x00000000);
151
152 return 0;
153}
154
155static void adreno_rbbm_intrcallback(struct kgsl_device *device)
156{
157 unsigned int status = 0;
158 unsigned int rderr = 0;
159
160 adreno_regread(device, REG_RBBM_INT_STATUS, &status);
161
162 if (status & RBBM_INT_CNTL__RDERR_INT_MASK) {
163 union rbbm_read_error_u rerr;
164 adreno_regread(device, REG_RBBM_READ_ERROR, &rderr);
165 rerr.val = rderr;
166 if (rerr.f.read_address == REG_CP_INT_STATUS &&
167 rerr.f.read_error &&
168 rerr.f.read_requester)
169 KGSL_DRV_WARN(device,
170 "rbbm read error interrupt: %08x\n", rderr);
171 else
172 KGSL_DRV_CRIT(device,
173 "rbbm read error interrupt: %08x\n", rderr);
174 } else if (status & RBBM_INT_CNTL__DISPLAY_UPDATE_INT_MASK) {
175 KGSL_DRV_INFO(device, "rbbm display update interrupt\n");
176 } else if (status & RBBM_INT_CNTL__GUI_IDLE_INT_MASK) {
177 KGSL_DRV_INFO(device, "rbbm gui idle interrupt\n");
178 } else {
179 KGSL_CMD_WARN(device,
180 "bad bits in REG_CP_INT_STATUS %08x\n", status);
181 }
182
183 status &= KGSL_RBBM_INT_MASK;
184 adreno_regwrite(device, REG_RBBM_INT_ACK, status);
185}
186
187irqreturn_t adreno_isr(int irq, void *data)
188{
189 irqreturn_t result = IRQ_NONE;
190 struct kgsl_device *device;
191 unsigned int status;
192
193 device = (struct kgsl_device *) data;
194
195 BUG_ON(device == NULL);
196 BUG_ON(device->regspace.sizebytes == 0);
197 BUG_ON(device->regspace.mmio_virt_base == 0);
198
199 adreno_regread(device, REG_MASTER_INT_SIGNAL, &status);
200
201 if (status & MASTER_INT_SIGNAL__MH_INT_STAT) {
202 kgsl_mh_intrcallback(device);
203 result = IRQ_HANDLED;
204 }
205
206 if (status & MASTER_INT_SIGNAL__CP_INT_STAT) {
207 kgsl_cp_intrcallback(device);
208 result = IRQ_HANDLED;
209 }
210
211 if (status & MASTER_INT_SIGNAL__RBBM_INT_STAT) {
212 adreno_rbbm_intrcallback(device);
213 result = IRQ_HANDLED;
214 }
215
216 if (device->requested_state == KGSL_STATE_NONE) {
217 if (device->pwrctrl.nap_allowed == true) {
218 device->requested_state = KGSL_STATE_NAP;
219 queue_work(device->work_queue, &device->idle_check_ws);
220 } else if (device->pwrscale.policy != NULL) {
221 queue_work(device->work_queue, &device->idle_check_ws);
222 }
223 }
224
225 /* Reset the time-out in our idle timer */
226 mod_timer(&device->idle_timer,
227 jiffies + device->pwrctrl.interval_timeout);
228 return result;
229}
230
231static int adreno_cleanup_pt(struct kgsl_device *device,
232 struct kgsl_pagetable *pagetable)
233{
234 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
235 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
236
237 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
238
239 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
240
241 kgsl_mmu_unmap(pagetable, &device->memstore);
242
243 kgsl_mmu_unmap(pagetable, &device->mmu.dummyspace);
244
245 return 0;
246}
247
248static int adreno_setup_pt(struct kgsl_device *device,
249 struct kgsl_pagetable *pagetable)
250{
251 int result = 0;
252 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
253 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
254
255 BUG_ON(rb->buffer_desc.physaddr == 0);
256 BUG_ON(rb->memptrs_desc.physaddr == 0);
257 BUG_ON(device->memstore.physaddr == 0);
258#ifdef CONFIG_MSM_KGSL_MMU
259 BUG_ON(device->mmu.dummyspace.physaddr == 0);
260#endif
261 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
262 GSL_PT_PAGE_RV);
263 if (result)
264 goto error;
265
266 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
267 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
268 if (result)
269 goto unmap_buffer_desc;
270
271 result = kgsl_mmu_map_global(pagetable, &device->memstore,
272 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
273 if (result)
274 goto unmap_memptrs_desc;
275
276 result = kgsl_mmu_map_global(pagetable, &device->mmu.dummyspace,
277 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
278 if (result)
279 goto unmap_memstore_desc;
280
281 return result;
282
283unmap_memstore_desc:
284 kgsl_mmu_unmap(pagetable, &device->memstore);
285
286unmap_memptrs_desc:
287 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
288
289unmap_buffer_desc:
290 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
291
292error:
293 return result;
294}
295
296static void adreno_setstate(struct kgsl_device *device, uint32_t flags)
297{
298 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
299 unsigned int link[32];
300 unsigned int *cmds = &link[0];
301 int sizedwords = 0;
302 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
303
304 if (!kgsl_mmu_enabled() || !flags)
305 return;
306
307 /* If possible, then set the state via the command stream to avoid
308 a CPU idle. Otherwise, use the default setstate which uses register
309 writes */
310
311 if (adreno_dev->drawctxt_active) {
312 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
313 /* wait for graphics pipe to be idle */
314 *cmds++ = pm4_type3_packet(PM4_WAIT_FOR_IDLE, 1);
315 *cmds++ = 0x00000000;
316
317 /* set page table base */
318 *cmds++ = pm4_type0_packet(MH_MMU_PT_BASE, 1);
319 *cmds++ = device->mmu.hwpagetable->base.gpuaddr;
320 sizedwords += 4;
321 }
322
323 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
324 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
325 *cmds++ = pm4_type3_packet(PM4_WAIT_FOR_IDLE,
326 1);
327 *cmds++ = 0x00000000;
328 sizedwords += 2;
329 }
330 *cmds++ = pm4_type0_packet(MH_MMU_INVALIDATE, 1);
331 *cmds++ = mh_mmu_invalidate;
332 sizedwords += 2;
333 }
334
335 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
336 !adreno_is_a220(adreno_dev)) {
337 /* HW workaround: to resolve MMU page fault interrupts
338 * caused by the VGT.It prevents the CP PFP from filling
339 * the VGT DMA request fifo too early,thereby ensuring
340 * that the VGT will not fetch vertex/bin data until
341 * after the page table base register has been updated.
342 *
343 * Two null DRAW_INDX_BIN packets are inserted right
344 * after the page table base update, followed by a
345 * wait for idle. The null packets will fill up the
346 * VGT DMA request fifo and prevent any further
347 * vertex/bin updates from occurring until the wait
348 * has finished. */
349 *cmds++ = pm4_type3_packet(PM4_SET_CONSTANT, 2);
350 *cmds++ = (0x4 << 16) |
351 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
352 *cmds++ = 0; /* disable faceness generation */
353 *cmds++ = pm4_type3_packet(PM4_SET_BIN_BASE_OFFSET, 1);
354 *cmds++ = device->mmu.dummyspace.gpuaddr;
355 *cmds++ = pm4_type3_packet(PM4_DRAW_INDX_BIN, 6);
356 *cmds++ = 0; /* viz query info */
357 *cmds++ = 0x0003C004; /* draw indicator */
358 *cmds++ = 0; /* bin base */
359 *cmds++ = 3; /* bin size */
360 *cmds++ = device->mmu.dummyspace.gpuaddr; /* dma base */
361 *cmds++ = 6; /* dma size */
362 *cmds++ = pm4_type3_packet(PM4_DRAW_INDX_BIN, 6);
363 *cmds++ = 0; /* viz query info */
364 *cmds++ = 0x0003C004; /* draw indicator */
365 *cmds++ = 0; /* bin base */
366 *cmds++ = 3; /* bin size */
367 /* dma base */
368 *cmds++ = device->mmu.dummyspace.gpuaddr;
369 *cmds++ = 6; /* dma size */
370 *cmds++ = pm4_type3_packet(PM4_WAIT_FOR_IDLE, 1);
371 *cmds++ = 0x00000000;
372 sizedwords += 21;
373 }
374
375 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
376 *cmds++ = pm4_type3_packet(PM4_INVALIDATE_STATE, 1);
377 *cmds++ = 0x7fff; /* invalidate all base pointers */
378 sizedwords += 2;
379 }
380
381 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
382 &link[0], sizedwords);
383 } else
384 kgsl_default_setstate(device, flags);
385}
386
387static unsigned int
388adreno_getchipid(struct kgsl_device *device)
389{
390 unsigned int chipid = 0;
391 unsigned int coreid, majorid, minorid, patchid, revid;
392
393 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
394 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
395 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
396
397 /*
398 * adreno 22x gpus are indicated by coreid 2,
399 * but REG_RBBM_PERIPHID1 always contains 0 for this field
400 */
401 if (cpu_is_msm8960() || cpu_is_msm8x60())
402 chipid = 2 << 24;
403 else
404 chipid = (coreid & 0xF) << 24;
405
406 chipid |= ((majorid >> 4) & 0xF) << 16;
407
408 minorid = ((revid >> 0) & 0xFF);
409
410 patchid = ((revid >> 16) & 0xFF);
411
412 /* 8x50 returns 0 for patch release, but it should be 1 */
413 if (cpu_is_qsd8x50())
414 patchid = 1;
415 /* userspace isn't prepared to deal with patch id for these chips yet */
416 else if (cpu_is_msm8960() || cpu_is_msm8x60())
417 patchid = 0;
418
419 chipid |= (minorid << 8) | patchid;
420
421 return chipid;
422}
423
424/* all chipid fields are 8 bits wide so 256 won't occur in a real chipid */
425#define DONT_CARE 256
426static const struct {
427 unsigned int core;
428 unsigned int major;
429 unsigned int minor;
430 enum adreno_gpurev gpurev;
431} gpurev_table[] = {
432 /* major and minor may be DONT_CARE, but core must not be */
433 {0, 2, DONT_CARE, ADRENO_REV_A200},
434 {0, 1, 0, ADRENO_REV_A205},
435 {2, 1, DONT_CARE, ADRENO_REV_A220},
436 {2, 2, DONT_CARE, ADRENO_REV_A225},
437};
438
439static inline bool _rev_match(unsigned int id, unsigned int entry)
440{
441 return (entry == DONT_CARE || entry == id);
442}
443#undef DONT_CARE
444
445static void
446adreno_identify_gpu(struct adreno_device *adreno_dev)
447{
448 enum adreno_gpurev gpurev = ADRENO_REV_UNKNOWN;
449 unsigned int i, core, major, minor;
450
451 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
452
453 core = (adreno_dev->chip_id >> 24) & 0xff;
454 major = (adreno_dev->chip_id >> 16) & 0xff;
455 minor = (adreno_dev->chip_id >> 8) & 0xff;
456
457 for (i = 0; i < ARRAY_SIZE(gpurev_table); i++) {
458 if (core == gpurev_table[i].core &&
459 _rev_match(major, gpurev_table[i].major) &&
460 _rev_match(minor, gpurev_table[i].minor)) {
461 gpurev = gpurev_table[i].gpurev;
462 break;
463 }
464 }
465
466 adreno_dev->gpurev = gpurev;
467}
468
469static int __devinit
470adreno_probe(struct platform_device *pdev)
471{
472 struct kgsl_device *device;
473 struct adreno_device *adreno_dev;
474 int status = -EINVAL;
475
476 device = (struct kgsl_device *)pdev->id_entry->driver_data;
477 adreno_dev = ADRENO_DEVICE(device);
478 device->parentdev = &pdev->dev;
479
480 init_completion(&device->recovery_gate);
481
482 status = adreno_ringbuffer_init(device);
483 if (status != 0)
484 goto error;
485
486 status = kgsl_device_platform_probe(device, adreno_isr);
487 if (status)
488 goto error_close_rb;
489
490 adreno_debugfs_init(device);
491
492 kgsl_pwrscale_init(device);
493 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
494
495 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
496 return 0;
497
498error_close_rb:
499 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
500error:
501 device->parentdev = NULL;
502 return status;
503}
504
505static int __devexit adreno_remove(struct platform_device *pdev)
506{
507 struct kgsl_device *device;
508 struct adreno_device *adreno_dev;
509
510 device = (struct kgsl_device *)pdev->id_entry->driver_data;
511 adreno_dev = ADRENO_DEVICE(device);
512
513 kgsl_pwrscale_detach_policy(device);
514 kgsl_pwrscale_close(device);
515
516 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
517 kgsl_device_platform_remove(device);
518
519 return 0;
520}
521
522static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
523{
524 int status = -EINVAL;
525 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
526 int init_reftimestamp = 0x7fffffff;
527
528 device->state = KGSL_STATE_INIT;
529 device->requested_state = KGSL_STATE_NONE;
530
531 /* Power up the device */
532 kgsl_pwrctrl_enable(device);
533
534 /* Identify the specific GPU */
535 adreno_identify_gpu(adreno_dev);
536
537 if (kgsl_mmu_start(device))
538 goto error_clk_off;
539
540 /*We need to make sure all blocks are powered up and clocked before
541 *issuing a soft reset. The overrides will then be turned off (set to 0)
542 */
543 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0xfffffffe);
544 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0xffffffff);
545
546 /* Only reset CP block if all blocks have previously been reset */
547 if (!(device->flags & KGSL_FLAGS_SOFT_RESET) ||
548 !adreno_is_a220(adreno_dev)) {
549 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0xFFFFFFFF);
550 device->flags |= KGSL_FLAGS_SOFT_RESET;
551 } else
552 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000001);
553
554 /* The core is in an indeterminate state until the reset completes
555 * after 30ms.
556 */
557 msleep(30);
558
559 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000000);
560
561 adreno_regwrite(device, REG_RBBM_CNTL, 0x00004442);
562
563 adreno_regwrite(device, REG_MH_ARBITER_CONFIG,
564 adreno_dev->mharb);
565
566 /* Remove 1k boundary check in z470 to avoid GPU hang.
567 Notice that, this solution won't work if both EBI and SMI are used */
568 if (adreno_is_a220(adreno_dev)) {
569 adreno_regwrite(device, REG_MH_CLNT_INTF_CTRL_CONFIG1,
570 0x00032f07);
571 }
572
573 adreno_regwrite(device, REG_SQ_VS_PROGRAM, 0x00000000);
574 adreno_regwrite(device, REG_SQ_PS_PROGRAM, 0x00000000);
575
576 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0);
577 if (!adreno_is_a220(adreno_dev))
578 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0);
579 else
580 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0x80);
581
582 kgsl_sharedmem_writel(&device->memstore,
583 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
584 init_reftimestamp);
585
586 adreno_regwrite(device, REG_RBBM_DEBUG, 0x00080000);
587
588 /* Make sure interrupts are disabled */
589
590 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
591 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
592 adreno_regwrite(device, REG_SQ_INT_CNTL, 0);
593
594 if (adreno_is_a220(adreno_dev))
595 adreno_dev->gmemspace.sizebytes = SZ_512K;
596 else
597 adreno_dev->gmemspace.sizebytes = SZ_256K;
598 adreno_gmeminit(adreno_dev);
599
600 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
601
602 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
603 if (status != 0)
604 goto error_irq_off;
605
606 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
607 return status;
608
609error_irq_off:
610 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
611error_clk_off:
612 kgsl_pwrctrl_disable(device);
613 kgsl_mmu_stop(device);
614
615 return status;
616}
617
618static int adreno_stop(struct kgsl_device *device)
619{
620 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
621
622 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
623 del_timer(&device->idle_timer);
624
625 adreno_dev->drawctxt_active = NULL;
626
627 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
628
629 adreno_gmemclose(device);
630
631 kgsl_mmu_stop(device);
632
633 /* Power down the device */
634 kgsl_pwrctrl_disable(device);
635
636 return 0;
637}
638
639static int
640adreno_recover_hang(struct kgsl_device *device)
641{
642 int ret;
643 unsigned int *rb_buffer;
644 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
645 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
646 unsigned int timestamp;
647 unsigned int num_rb_contents;
648 unsigned int bad_context;
649 unsigned int reftimestamp;
650 unsigned int enable_ts;
651 unsigned int soptimestamp;
652 unsigned int eoptimestamp;
653 struct adreno_context *drawctxt;
654
655 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
656 rb_buffer = vmalloc(rb->buffer_desc.size);
657 if (!rb_buffer) {
658 KGSL_MEM_ERR(device,
659 "Failed to allocate memory for recovery: %x\n",
660 rb->buffer_desc.size);
661 return -ENOMEM;
662 }
663 /* Extract valid contents from rb which can stil be executed after
664 * hang */
665 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
666 if (ret)
667 goto done;
668 timestamp = rb->timestamp;
669 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
670 kgsl_sharedmem_readl(&device->memstore, &bad_context,
671 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
672 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
673 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
674 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
675 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
676 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
677 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
678 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
679 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
680 /* Make sure memory is synchronized before restarting the GPU */
681 mb();
682 KGSL_CTXT_ERR(device,
683 "Context that caused a GPU hang: %x\n", bad_context);
684 /* restart device */
685 ret = adreno_stop(device);
686 if (ret)
687 goto done;
688 ret = adreno_start(device, true);
689 if (ret)
690 goto done;
691 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
692 /* Restore timestamp states */
693 kgsl_sharedmem_writel(&device->memstore,
694 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
695 soptimestamp);
696 kgsl_sharedmem_writel(&device->memstore,
697 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
698 eoptimestamp);
699 kgsl_sharedmem_writel(&device->memstore,
700 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
701 soptimestamp);
702 if (num_rb_contents) {
703 kgsl_sharedmem_writel(&device->memstore,
704 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
705 reftimestamp);
706 kgsl_sharedmem_writel(&device->memstore,
707 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
708 enable_ts);
709 }
710 /* Make sure all writes are posted before the GPU reads them */
711 wmb();
712 /* Mark the invalid context so no more commands are accepted from
713 * that context */
714
715 drawctxt = (struct adreno_context *) bad_context;
716
717 KGSL_CTXT_ERR(device,
718 "Context that caused a GPU hang: %x\n", bad_context);
719
720 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
721
722 /* Restore valid commands in ringbuffer */
723 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
724 rb->timestamp = timestamp;
725done:
726 vfree(rb_buffer);
727 return ret;
728}
729
730static int
731adreno_dump_and_recover(struct kgsl_device *device)
732{
733 static int recovery;
734 int result = -ETIMEDOUT;
735
736 if (device->state == KGSL_STATE_HUNG)
737 goto done;
738 if (device->state == KGSL_STATE_DUMP_AND_RECOVER && !recovery) {
739 mutex_unlock(&device->mutex);
740 wait_for_completion(&device->recovery_gate);
741 mutex_lock(&device->mutex);
742 if (!(device->state & KGSL_STATE_HUNG))
743 /* recovery success */
744 result = 0;
745 } else {
746 INIT_COMPLETION(device->recovery_gate);
747 /* Detected a hang - trigger an automatic dump */
748 adreno_postmortem_dump(device, 0);
749 if (!recovery) {
750 recovery = 1;
751 result = adreno_recover_hang(device);
752 if (result)
753 device->state = KGSL_STATE_HUNG;
754 recovery = 0;
755 complete_all(&device->recovery_gate);
756 } else
757 KGSL_DRV_ERR(device,
758 "Cannot recover from another hang while "
759 "recovering from a hang\n");
760 }
761done:
762 return result;
763}
764
765static int adreno_getproperty(struct kgsl_device *device,
766 enum kgsl_property_type type,
767 void *value,
768 unsigned int sizebytes)
769{
770 int status = -EINVAL;
771 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
772
773 switch (type) {
774 case KGSL_PROP_DEVICE_INFO:
775 {
776 struct kgsl_devinfo devinfo;
777
778 if (sizebytes != sizeof(devinfo)) {
779 status = -EINVAL;
780 break;
781 }
782
783 memset(&devinfo, 0, sizeof(devinfo));
784 devinfo.device_id = device->id+1;
785 devinfo.chip_id = adreno_dev->chip_id;
786 devinfo.mmu_enabled = kgsl_mmu_enabled();
787 devinfo.gpu_id = adreno_dev->gpurev;
788 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
789 gpu_base;
790 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
791 sizebytes;
792
793 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
794 0) {
795 status = -EFAULT;
796 break;
797 }
798 status = 0;
799 }
800 break;
801 case KGSL_PROP_DEVICE_SHADOW:
802 {
803 struct kgsl_shadowprop shadowprop;
804
805 if (sizebytes != sizeof(shadowprop)) {
806 status = -EINVAL;
807 break;
808 }
809 memset(&shadowprop, 0, sizeof(shadowprop));
810 if (device->memstore.hostptr) {
811 /*NOTE: with mmu enabled, gpuaddr doesn't mean
812 * anything to mmap().
813 */
814 shadowprop.gpuaddr = device->memstore.physaddr;
815 shadowprop.size = device->memstore.size;
816 /* GSL needs this to be set, even if it
817 appears to be meaningless */
818 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
819 }
820 if (copy_to_user(value, &shadowprop,
821 sizeof(shadowprop))) {
822 status = -EFAULT;
823 break;
824 }
825 status = 0;
826 }
827 break;
828 case KGSL_PROP_MMU_ENABLE:
829 {
830#ifdef CONFIG_MSM_KGSL_MMU
831 int mmuProp = 1;
832#else
833 int mmuProp = 0;
834#endif
835 if (sizebytes != sizeof(int)) {
836 status = -EINVAL;
837 break;
838 }
839 if (copy_to_user(value, &mmuProp, sizeof(mmuProp))) {
840 status = -EFAULT;
841 break;
842 }
843 status = 0;
844 }
845 break;
846 case KGSL_PROP_INTERRUPT_WAITS:
847 {
848 int int_waits = 1;
849 if (sizebytes != sizeof(int)) {
850 status = -EINVAL;
851 break;
852 }
853 if (copy_to_user(value, &int_waits, sizeof(int))) {
854 status = -EFAULT;
855 break;
856 }
857 status = 0;
858 }
859 break;
860 default:
861 status = -EINVAL;
862 }
863
864 return status;
865}
866
867/* Caller must hold the device mutex. */
868int adreno_idle(struct kgsl_device *device, unsigned int timeout)
869{
870 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
871 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
872 unsigned int rbbm_status;
873 unsigned long wait_time = jiffies + MAX_WAITGPU_SECS;
874
875 kgsl_cffdump_regpoll(device->id, REG_RBBM_STATUS << 2,
876 0x00000000, 0x80000000);
877 /* first, wait until the CP has consumed all the commands in
878 * the ring buffer
879 */
880retry:
881 if (rb->flags & KGSL_FLAGS_STARTED) {
882 do {
883 GSL_RB_GET_READPTR(rb, &rb->rptr);
884 if (time_after(jiffies, wait_time)) {
885 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
886 rb->rptr, rb->wptr);
887 goto err;
888 }
889 } while (rb->rptr != rb->wptr);
890 }
891
892 /* now, wait for the GPU to finish its operations */
893 wait_time = jiffies + MAX_WAITGPU_SECS;
894 while (time_before(jiffies, wait_time)) {
895 adreno_regread(device, REG_RBBM_STATUS, &rbbm_status);
896 if (rbbm_status == 0x110)
897 return 0;
898 }
899
900err:
901 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
902 if (!adreno_dump_and_recover(device)) {
903 wait_time = jiffies + MAX_WAITGPU_SECS;
904 goto retry;
905 }
906 return -ETIMEDOUT;
907}
908
909static unsigned int adreno_isidle(struct kgsl_device *device)
910{
911 int status = false;
912 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
913 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
914 unsigned int rbbm_status;
915
916 if (rb->flags & KGSL_FLAGS_STARTED) {
917 /* Is the ring buffer is empty? */
918 GSL_RB_GET_READPTR(rb, &rb->rptr);
919 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
920 /* Is the core idle? */
921 adreno_regread(device, REG_RBBM_STATUS,
922 &rbbm_status);
923 if (rbbm_status == 0x110)
924 status = true;
925 }
926 } else {
927 KGSL_DRV_ERR(device, "ringbuffer not started\n");
928 BUG();
929 }
930 return status;
931}
932
933/* Caller must hold the device mutex. */
934static int adreno_suspend_context(struct kgsl_device *device)
935{
936 int status = 0;
937 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
938
939 /* switch to NULL ctxt */
940 if (adreno_dev->drawctxt_active != NULL) {
941 adreno_drawctxt_switch(adreno_dev, NULL, 0);
942 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
943 }
944
945 return status;
946}
947
948uint8_t *kgsl_sharedmem_convertaddr(struct kgsl_device *device,
949 unsigned int pt_base, unsigned int gpuaddr, unsigned int *size)
950{
951 uint8_t *result = NULL;
952 struct kgsl_mem_entry *entry;
953 struct kgsl_process_private *priv;
954 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
955 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
956
957 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr)) {
958 return kgsl_gpuaddr_to_vaddr(&ringbuffer->buffer_desc,
959 gpuaddr, size);
960 }
961
962 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr)) {
963 return kgsl_gpuaddr_to_vaddr(&ringbuffer->memptrs_desc,
964 gpuaddr, size);
965 }
966
967 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr)) {
968 return kgsl_gpuaddr_to_vaddr(&device->memstore,
969 gpuaddr, size);
970 }
971
972 mutex_lock(&kgsl_driver.process_mutex);
973 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
974 if (pt_base != 0
975 && priv->pagetable
976 && priv->pagetable->base.gpuaddr != pt_base) {
977 continue;
978 }
979
980 spin_lock(&priv->mem_lock);
981 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
982 sizeof(unsigned int));
983 if (entry) {
984 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
985 gpuaddr, size);
986 spin_unlock(&priv->mem_lock);
987 mutex_unlock(&kgsl_driver.process_mutex);
988 return result;
989 }
990 spin_unlock(&priv->mem_lock);
991 }
992 mutex_unlock(&kgsl_driver.process_mutex);
993
994 BUG_ON(!mutex_is_locked(&device->mutex));
995 list_for_each_entry(entry, &device->memqueue, list) {
996 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr)) {
997 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
998 gpuaddr, size);
999 break;
1000 }
1001
1002 }
1003 return result;
1004}
1005
1006void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1007 unsigned int *value)
1008{
1009 unsigned int *reg;
1010 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1011 reg = (unsigned int *)(device->regspace.mmio_virt_base
1012 + (offsetwords << 2));
1013
1014 if (!in_interrupt())
1015 kgsl_pre_hwaccess(device);
1016
1017 /*ensure this read finishes before the next one.
1018 * i.e. act like normal readl() */
1019 *value = __raw_readl(reg);
1020 rmb();
1021}
1022
1023void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1024 unsigned int value)
1025{
1026 unsigned int *reg;
1027
1028 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1029
1030 if (!in_interrupt())
1031 kgsl_pre_hwaccess(device);
1032
1033 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1034 reg = (unsigned int *)(device->regspace.mmio_virt_base
1035 + (offsetwords << 2));
1036
1037 /*ensure previous writes post before this one,
1038 * i.e. act like normal writel() */
1039 wmb();
1040 __raw_writel(value, reg);
1041}
1042
1043static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1044 unsigned int timestamp)
1045{
1046 int status;
1047 unsigned int ref_ts, enableflag;
1048
1049 status = kgsl_check_timestamp(device, timestamp);
1050 if (!status) {
1051 mutex_lock(&device->mutex);
1052 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1053 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1054 mb();
1055
1056 if (enableflag) {
1057 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1058 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1059 mb();
1060 if (timestamp_cmp(ref_ts, timestamp)) {
1061 kgsl_sharedmem_writel(&device->memstore,
1062 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1063 timestamp);
1064 wmb();
1065 }
1066 } else {
1067 unsigned int cmds[2];
1068 kgsl_sharedmem_writel(&device->memstore,
1069 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1070 timestamp);
1071 enableflag = 1;
1072 kgsl_sharedmem_writel(&device->memstore,
1073 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1074 enableflag);
1075 wmb();
1076 /* submit a dummy packet so that even if all
1077 * commands upto timestamp get executed we will still
1078 * get an interrupt */
1079 cmds[0] = pm4_type3_packet(PM4_NOP, 1);
1080 cmds[1] = 0;
1081 adreno_ringbuffer_issuecmds(device, 0, &cmds[0], 2);
1082 }
1083 mutex_unlock(&device->mutex);
1084 }
1085
1086 return status;
1087}
1088
1089/*
1090 wait_io_event_interruptible_timeout checks for the exit condition before
1091 placing a process in wait q. For conditional interrupts we expect the
1092 process to already be in its wait q when its exit condition checking
1093 function is called.
1094*/
1095#define kgsl_wait_io_event_interruptible_timeout(wq, condition, timeout)\
1096({ \
1097 long __ret = timeout; \
1098 __wait_io_event_interruptible_timeout(wq, condition, __ret); \
1099 __ret; \
1100})
1101
1102/* MUST be called with the device mutex held */
1103static int adreno_waittimestamp(struct kgsl_device *device,
1104 unsigned int timestamp,
1105 unsigned int msecs)
1106{
1107 long status = 0;
1108 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1109
1110 if (timestamp != adreno_dev->ringbuffer.timestamp &&
1111 timestamp_cmp(timestamp,
1112 adreno_dev->ringbuffer.timestamp)) {
1113 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1114 "rb->timestamp: %x\n",
1115 timestamp, adreno_dev->ringbuffer.timestamp);
1116 status = -EINVAL;
1117 goto done;
1118 }
1119 if (!kgsl_check_timestamp(device, timestamp)) {
1120 mutex_unlock(&device->mutex);
1121 /* We need to make sure that the process is placed in wait-q
1122 * before its condition is called */
1123 status = kgsl_wait_io_event_interruptible_timeout(
1124 device->wait_queue,
1125 kgsl_check_interrupt_timestamp(device,
1126 timestamp), msecs_to_jiffies(msecs));
1127 mutex_lock(&device->mutex);
1128
1129 if (status > 0)
1130 status = 0;
1131 else if (status == 0) {
1132 if (!kgsl_check_timestamp(device, timestamp)) {
1133 status = -ETIMEDOUT;
1134 KGSL_DRV_ERR(device,
1135 "Device hang detected while waiting "
1136 "for timestamp: %x, last "
1137 "submitted(rb->timestamp): %x, wptr: "
1138 "%x\n", timestamp,
1139 adreno_dev->ringbuffer.timestamp,
1140 adreno_dev->ringbuffer.wptr);
1141 if (!adreno_dump_and_recover(device)) {
1142 /* wait for idle after recovery as the
1143 * timestamp that this process wanted
1144 * to wait on may be invalid */
1145 if (!adreno_idle(device,
1146 KGSL_TIMEOUT_DEFAULT))
1147 status = 0;
1148 }
1149 }
1150 }
1151 }
1152
1153done:
1154 return (int)status;
1155}
1156
1157static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1158 enum kgsl_timestamp_type type)
1159{
1160 unsigned int timestamp = 0;
1161
1162 if (type == KGSL_TIMESTAMP_CONSUMED)
1163 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1164 else if (type == KGSL_TIMESTAMP_RETIRED)
1165 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1166 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1167 rmb();
1168
1169 return timestamp;
1170}
1171
1172static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1173 unsigned int cmd, void *data)
1174{
1175 int result = 0;
1176 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1177 struct kgsl_context *context;
1178
1179 switch (cmd) {
1180 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1181 binbase = data;
1182
1183 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1184 if (context) {
1185 adreno_drawctxt_set_bin_base_offset(
1186 dev_priv->device, context, binbase->offset);
1187 } else {
1188 result = -EINVAL;
1189 KGSL_DRV_ERR(dev_priv->device,
1190 "invalid drawctxt drawctxt_id %d "
1191 "device_id=%d\n",
1192 binbase->drawctxt_id, dev_priv->device->id);
1193 }
1194 break;
1195
1196 default:
1197 KGSL_DRV_INFO(dev_priv->device,
1198 "invalid ioctl code %08x\n", cmd);
1199 result = -EINVAL;
1200 break;
1201 }
1202 return result;
1203
1204}
1205
1206static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1207{
1208 gpu_freq /= 1000000;
1209 return ticks / gpu_freq;
1210}
1211
1212static void adreno_power_stats(struct kgsl_device *device,
1213 struct kgsl_power_stats *stats)
1214{
1215 unsigned int reg;
1216 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
1217
1218 /* In order to calculate idle you have to have run the algorithm *
1219 * at least once to get a start time. */
1220 if (pwr->time != 0) {
1221 s64 tmp;
1222 /* Stop the performance moniter and read the current *
1223 * busy cycles. */
1224 adreno_regwrite(device,
1225 REG_CP_PERFMON_CNTL,
1226 REG_PERF_MODE_CNT |
1227 REG_PERF_STATE_FREEZE);
1228 adreno_regread(device, REG_RBBM_PERFCOUNTER1_LO, &reg);
1229 tmp = ktime_to_us(ktime_get());
1230 stats->total_time = tmp - pwr->time;
1231 pwr->time = tmp;
1232 stats->busy_time = adreno_ticks_to_us(reg, device->pwrctrl.
1233 pwrlevels[device->pwrctrl.active_pwrlevel].
1234 gpu_freq);
1235
1236 adreno_regwrite(device,
1237 REG_CP_PERFMON_CNTL,
1238 REG_PERF_MODE_CNT |
1239 REG_PERF_STATE_RESET);
1240 } else {
1241 stats->total_time = 0;
1242 stats->busy_time = 0;
1243 pwr->time = ktime_to_us(ktime_get());
1244 }
1245
1246 /* re-enable the performance moniters */
1247 adreno_regread(device, REG_RBBM_PM_OVERRIDE2, &reg);
1248 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, (reg | 0x40));
1249 adreno_regwrite(device, REG_RBBM_PERFCOUNTER1_SELECT, 0x1);
1250 adreno_regwrite(device,
1251 REG_CP_PERFMON_CNTL,
1252 REG_PERF_MODE_CNT | REG_PERF_STATE_ENABLE);
1253}
1254
1255void adreno_irqctrl(struct kgsl_device *device, int state)
1256{
1257 /* Enable GPU and GPUMMU interrupts */
1258
1259 if (state) {
1260 adreno_regwrite(device, REG_RBBM_INT_CNTL, KGSL_RBBM_INT_MASK);
1261 adreno_regwrite(device, REG_CP_INT_CNTL, KGSL_CP_INT_MASK);
1262 adreno_regwrite(device, MH_INTERRUPT_MASK, KGSL_MMU_INT_MASK);
1263 } else {
1264 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
1265 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
1266 adreno_regwrite(device, MH_INTERRUPT_MASK, 0);
1267 }
1268}
1269
1270static const struct kgsl_functable adreno_functable = {
1271 /* Mandatory functions */
1272 .regread = adreno_regread,
1273 .regwrite = adreno_regwrite,
1274 .idle = adreno_idle,
1275 .isidle = adreno_isidle,
1276 .suspend_context = adreno_suspend_context,
1277 .start = adreno_start,
1278 .stop = adreno_stop,
1279 .getproperty = adreno_getproperty,
1280 .waittimestamp = adreno_waittimestamp,
1281 .readtimestamp = adreno_readtimestamp,
1282 .issueibcmds = adreno_ringbuffer_issueibcmds,
1283 .ioctl = adreno_ioctl,
1284 .setup_pt = adreno_setup_pt,
1285 .cleanup_pt = adreno_cleanup_pt,
1286 .power_stats = adreno_power_stats,
1287 .irqctrl = adreno_irqctrl,
1288 /* Optional functions */
1289 .setstate = adreno_setstate,
1290 .drawctxt_create = adreno_drawctxt_create,
1291 .drawctxt_destroy = adreno_drawctxt_destroy,
1292};
1293
1294static struct platform_device_id adreno_id_table[] = {
1295 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1296 { },
1297};
1298MODULE_DEVICE_TABLE(platform, adreno_id_table);
1299
1300static struct platform_driver adreno_platform_driver = {
1301 .probe = adreno_probe,
1302 .remove = __devexit_p(adreno_remove),
1303 .suspend = kgsl_suspend_driver,
1304 .resume = kgsl_resume_driver,
1305 .id_table = adreno_id_table,
1306 .driver = {
1307 .owner = THIS_MODULE,
1308 .name = DEVICE_3D_NAME,
1309 .pm = &kgsl_pm_ops,
1310 }
1311};
1312
1313static int __init kgsl_3d_init(void)
1314{
1315 return platform_driver_register(&adreno_platform_driver);
1316}
1317
1318static void __exit kgsl_3d_exit(void)
1319{
1320 platform_driver_unregister(&adreno_platform_driver);
1321}
1322
1323module_init(kgsl_3d_init);
1324module_exit(kgsl_3d_exit);
1325
1326MODULE_DESCRIPTION("3D Graphics driver");
1327MODULE_VERSION("1.2");
1328MODULE_LICENSE("GPL v2");
1329MODULE_ALIAS("platform:kgsl_3d");