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