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