blob: 7516a1f6f0d068cebe41a8159d66915c32e04fa4 [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;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600131 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600132 const char *pm4fw;
133 const char *pfpfw;
134 struct adreno_gpudev *gpudev;
135} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600136 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jordan Crouse505df9c2011-07-28 08:37:59 -0600137 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600138 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jordan Crouse505df9c2011-07-28 08:37:59 -0600139 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600140 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jordan Crouse505df9c2011-07-28 08:37:59 -0600141 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600142 /*
143 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
144 * a hardware problem.
145 */
146 { ADRENO_REV_A225, 2, 2, 0, 5,
147 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev },
148 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jordan Crouse505df9c2011-07-28 08:37:59 -0600149 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev },
150};
151
Jordan Crouse9f739212011-07-28 08:37:57 -0600152static void adreno_gmeminit(struct adreno_device *adreno_dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153{
154 struct kgsl_device *device = &adreno_dev->dev;
155 union reg_rb_edram_info rb_edram_info;
156 unsigned int gmem_size;
157 unsigned int edram_value = 0;
158
159 /* make sure edram range is aligned to size */
160 BUG_ON(adreno_dev->gmemspace.gpu_base &
161 (adreno_dev->gmemspace.sizebytes - 1));
162
163 /* get edram_size value equivalent */
164 gmem_size = (adreno_dev->gmemspace.sizebytes >> 14);
165 while (gmem_size >>= 1)
166 edram_value++;
167
168 rb_edram_info.val = 0;
169
170 rb_edram_info.f.edram_size = edram_value;
Jordan Crouse9f739212011-07-28 08:37:57 -0600171 rb_edram_info.f.edram_mapping_mode = 0; /* EDRAM_MAP_UPPER */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172
173 /* must be aligned to size */
174 rb_edram_info.f.edram_range = (adreno_dev->gmemspace.gpu_base >> 14);
175
176 adreno_regwrite(device, REG_RB_EDRAM_INFO, rb_edram_info.val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177}
178
Jordan Crouse9f739212011-07-28 08:37:57 -0600179static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180{
Jordan Crousea78c9172011-07-11 13:14:09 -0600181 irqreturn_t result;
182 struct kgsl_device *device = data;
183 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184
Jordan Crousea78c9172011-07-11 13:14:09 -0600185 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186
187 if (device->requested_state == KGSL_STATE_NONE) {
188 if (device->pwrctrl.nap_allowed == true) {
189 device->requested_state = KGSL_STATE_NAP;
190 queue_work(device->work_queue, &device->idle_check_ws);
191 } else if (device->pwrscale.policy != NULL) {
192 queue_work(device->work_queue, &device->idle_check_ws);
193 }
194 }
195
196 /* Reset the time-out in our idle timer */
197 mod_timer(&device->idle_timer,
198 jiffies + device->pwrctrl.interval_timeout);
199 return result;
200}
201
Jordan Crouse9f739212011-07-28 08:37:57 -0600202static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203 struct kgsl_pagetable *pagetable)
204{
205 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
206 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
207
208 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
209
210 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
211
212 kgsl_mmu_unmap(pagetable, &device->memstore);
213
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600214 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215}
216
217static int adreno_setup_pt(struct kgsl_device *device,
218 struct kgsl_pagetable *pagetable)
219{
220 int result = 0;
221 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
222 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
223
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
225 GSL_PT_PAGE_RV);
226 if (result)
227 goto error;
228
229 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
230 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
231 if (result)
232 goto unmap_buffer_desc;
233
234 result = kgsl_mmu_map_global(pagetable, &device->memstore,
235 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
236 if (result)
237 goto unmap_memptrs_desc;
238
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600239 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
241 if (result)
242 goto unmap_memstore_desc;
243
244 return result;
245
246unmap_memstore_desc:
247 kgsl_mmu_unmap(pagetable, &device->memstore);
248
249unmap_memptrs_desc:
250 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
251
252unmap_buffer_desc:
253 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
254
255error:
256 return result;
257}
258
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600259static void adreno_setstate(struct kgsl_device *device,
260 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261{
262 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
263 unsigned int link[32];
264 unsigned int *cmds = &link[0];
265 int sizedwords = 0;
266 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
267
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 /* If possible, then set the state via the command stream to avoid
269 a CPU idle. Otherwise, use the default setstate which uses register
270 writes */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271 if (adreno_dev->drawctxt_active) {
272 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
273 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600274 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 *cmds++ = 0x00000000;
276
277 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600278 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600279 *cmds++ = kgsl_pt_get_base_addr(
280 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 sizedwords += 4;
282 }
283
284 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
285 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600286 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 1);
288 *cmds++ = 0x00000000;
289 sizedwords += 2;
290 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600291 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 *cmds++ = mh_mmu_invalidate;
293 sizedwords += 2;
294 }
295
296 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600297 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 /* HW workaround: to resolve MMU page fault interrupts
299 * caused by the VGT.It prevents the CP PFP from filling
300 * the VGT DMA request fifo too early,thereby ensuring
301 * that the VGT will not fetch vertex/bin data until
302 * after the page table base register has been updated.
303 *
304 * Two null DRAW_INDX_BIN packets are inserted right
305 * after the page table base update, followed by a
306 * wait for idle. The null packets will fill up the
307 * VGT DMA request fifo and prevent any further
308 * vertex/bin updates from occurring until the wait
309 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600310 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 *cmds++ = (0x4 << 16) |
312 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
313 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600314 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600315 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600316 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 *cmds++ = 0; /* viz query info */
318 *cmds++ = 0x0003C004; /* draw indicator */
319 *cmds++ = 0; /* bin base */
320 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600321 *cmds++ =
322 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600324 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 *cmds++ = 0; /* viz query info */
326 *cmds++ = 0x0003C004; /* draw indicator */
327 *cmds++ = 0; /* bin base */
328 *cmds++ = 3; /* bin size */
329 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600330 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600332 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 *cmds++ = 0x00000000;
334 sizedwords += 21;
335 }
336
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600339 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 *cmds++ = 0x7fff; /* invalidate all base pointers */
341 sizedwords += 2;
342 }
343
344 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
345 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600346 } else {
347 kgsl_mmu_device_setstate(device, flags);
348 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700349}
350
351static unsigned int
352adreno_getchipid(struct kgsl_device *device)
353{
354 unsigned int chipid = 0;
355 unsigned int coreid, majorid, minorid, patchid, revid;
356
357 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
358 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
359 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
360
361 /*
362 * adreno 22x gpus are indicated by coreid 2,
363 * but REG_RBBM_PERIPHID1 always contains 0 for this field
364 */
365 if (cpu_is_msm8960() || cpu_is_msm8x60())
366 chipid = 2 << 24;
367 else
368 chipid = (coreid & 0xF) << 24;
369
Jeremy Gebbenaafef442011-08-26 12:27:43 -0700370 if (cpu_is_msm8960()) {
371 KGSL_DRV_ERR(device, "forcing a220 chipid\n");
372 majorid = 1<<4;
373 }
374
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 chipid |= ((majorid >> 4) & 0xF) << 16;
376
377 minorid = ((revid >> 0) & 0xFF);
378
379 patchid = ((revid >> 16) & 0xFF);
380
381 /* 8x50 returns 0 for patch release, but it should be 1 */
382 if (cpu_is_qsd8x50())
383 patchid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384
385 chipid |= (minorid << 8) | patchid;
386
387 return chipid;
388}
389
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390static inline bool _rev_match(unsigned int id, unsigned int entry)
391{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600392 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394
395static void
396adreno_identify_gpu(struct adreno_device *adreno_dev)
397{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600398 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399
400 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
401
402 core = (adreno_dev->chip_id >> 24) & 0xff;
403 major = (adreno_dev->chip_id >> 16) & 0xff;
404 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600405 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406
Jordan Crouse505df9c2011-07-28 08:37:59 -0600407 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
408 if (core == adreno_gpulist[i].core &&
409 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600410 _rev_match(minor, adreno_gpulist[i].minor) &&
411 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700412 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413 }
414
Jordan Crouse505df9c2011-07-28 08:37:59 -0600415 if (i == ARRAY_SIZE(adreno_gpulist)) {
416 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
417 return;
418 }
419
420 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
421 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
422 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
423 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424}
425
426static int __devinit
427adreno_probe(struct platform_device *pdev)
428{
429 struct kgsl_device *device;
430 struct adreno_device *adreno_dev;
431 int status = -EINVAL;
432
433 device = (struct kgsl_device *)pdev->id_entry->driver_data;
434 adreno_dev = ADRENO_DEVICE(device);
435 device->parentdev = &pdev->dev;
436
437 init_completion(&device->recovery_gate);
438
439 status = adreno_ringbuffer_init(device);
440 if (status != 0)
441 goto error;
442
443 status = kgsl_device_platform_probe(device, adreno_isr);
444 if (status)
445 goto error_close_rb;
446
447 adreno_debugfs_init(device);
448
449 kgsl_pwrscale_init(device);
450 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
451
452 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
453 return 0;
454
455error_close_rb:
456 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
457error:
458 device->parentdev = NULL;
459 return status;
460}
461
462static int __devexit adreno_remove(struct platform_device *pdev)
463{
464 struct kgsl_device *device;
465 struct adreno_device *adreno_dev;
466
467 device = (struct kgsl_device *)pdev->id_entry->driver_data;
468 adreno_dev = ADRENO_DEVICE(device);
469
470 kgsl_pwrscale_detach_policy(device);
471 kgsl_pwrscale_close(device);
472
473 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
474 kgsl_device_platform_remove(device);
475
476 return 0;
477}
478
479static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
480{
481 int status = -EINVAL;
482 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
483 int init_reftimestamp = 0x7fffffff;
484
485 device->state = KGSL_STATE_INIT;
486 device->requested_state = KGSL_STATE_NONE;
487
488 /* Power up the device */
489 kgsl_pwrctrl_enable(device);
490
491 /* Identify the specific GPU */
492 adreno_identify_gpu(adreno_dev);
493
Jordan Crouse505df9c2011-07-28 08:37:59 -0600494 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
495 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
496 adreno_dev->chip_id);
497 goto error_clk_off;
498 }
499
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600500 if (adreno_is_a20x(adreno_dev)) {
501 /*
502 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
503 * on older gpus
504 */
505 device->mh.mh_intf_cfg1 = 0;
506 device->mh.mh_intf_cfg2 = 0;
507 }
508
509 kgsl_mh_start(device);
510
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 if (kgsl_mmu_start(device))
512 goto error_clk_off;
513
514 /*We need to make sure all blocks are powered up and clocked before
515 *issuing a soft reset. The overrides will then be turned off (set to 0)
516 */
517 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0xfffffffe);
518 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0xffffffff);
519
520 /* Only reset CP block if all blocks have previously been reset */
521 if (!(device->flags & KGSL_FLAGS_SOFT_RESET) ||
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600522 !adreno_is_a22x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0xFFFFFFFF);
524 device->flags |= KGSL_FLAGS_SOFT_RESET;
525 } else
526 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000001);
527
528 /* The core is in an indeterminate state until the reset completes
529 * after 30ms.
530 */
531 msleep(30);
532
533 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000000);
534
535 adreno_regwrite(device, REG_RBBM_CNTL, 0x00004442);
536
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600537 if (adreno_is_a225(adreno_dev)) {
538 /* Enable large instruction store for A225 */
539 adreno_regwrite(device, REG_SQ_FLOW_CONTROL, 0x18000000);
540 }
541
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542 adreno_regwrite(device, REG_SQ_VS_PROGRAM, 0x00000000);
543 adreno_regwrite(device, REG_SQ_PS_PROGRAM, 0x00000000);
544
Tarun Karra96a12672011-09-23 18:52:39 -0700545 if (cpu_is_msm8960())
546 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0x200);
547 else
548 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0);
549
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600550 if (!adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0);
552 else
553 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0x80);
554
Sushmita Susheelendraf3896062011-08-12 16:33:10 -0600555 kgsl_sharedmem_set(&device->memstore, 0, 0, device->memstore.size);
556
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 kgsl_sharedmem_writel(&device->memstore,
558 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
559 init_reftimestamp);
560
Ranjhith Kalisamyf81dcd02011-09-30 16:45:24 +0530561 adreno_regwrite(device, REG_RBBM_DEBUG, 0x00080000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562
563 /* Make sure interrupts are disabled */
564
565 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
566 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
567 adreno_regwrite(device, REG_SQ_INT_CNTL, 0);
568
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600569 if (adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 adreno_dev->gmemspace.sizebytes = SZ_512K;
571 else
572 adreno_dev->gmemspace.sizebytes = SZ_256K;
573 adreno_gmeminit(adreno_dev);
574
575 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
576
577 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
578 if (status != 0)
579 goto error_irq_off;
580
581 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
582 return status;
583
584error_irq_off:
585 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600586 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587error_clk_off:
588 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589
590 return status;
591}
592
593static int adreno_stop(struct kgsl_device *device)
594{
595 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
596
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 adreno_dev->drawctxt_active = NULL;
598
599 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
600
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601 kgsl_mmu_stop(device);
602
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600603 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
604 del_timer_sync(&device->idle_timer);
605
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606 /* Power down the device */
607 kgsl_pwrctrl_disable(device);
608
609 return 0;
610}
611
612static int
613adreno_recover_hang(struct kgsl_device *device)
614{
615 int ret;
616 unsigned int *rb_buffer;
617 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
618 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
619 unsigned int timestamp;
620 unsigned int num_rb_contents;
621 unsigned int bad_context;
622 unsigned int reftimestamp;
623 unsigned int enable_ts;
624 unsigned int soptimestamp;
625 unsigned int eoptimestamp;
626 struct adreno_context *drawctxt;
627
628 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
629 rb_buffer = vmalloc(rb->buffer_desc.size);
630 if (!rb_buffer) {
631 KGSL_MEM_ERR(device,
632 "Failed to allocate memory for recovery: %x\n",
633 rb->buffer_desc.size);
634 return -ENOMEM;
635 }
636 /* Extract valid contents from rb which can stil be executed after
637 * hang */
638 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
639 if (ret)
640 goto done;
641 timestamp = rb->timestamp;
642 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
643 kgsl_sharedmem_readl(&device->memstore, &bad_context,
644 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
645 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
646 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
647 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
648 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
649 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
650 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
651 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
652 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
653 /* Make sure memory is synchronized before restarting the GPU */
654 mb();
655 KGSL_CTXT_ERR(device,
656 "Context that caused a GPU hang: %x\n", bad_context);
657 /* restart device */
658 ret = adreno_stop(device);
659 if (ret)
660 goto done;
661 ret = adreno_start(device, true);
662 if (ret)
663 goto done;
664 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
665 /* Restore timestamp states */
666 kgsl_sharedmem_writel(&device->memstore,
667 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
668 soptimestamp);
669 kgsl_sharedmem_writel(&device->memstore,
670 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
671 eoptimestamp);
672 kgsl_sharedmem_writel(&device->memstore,
673 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
674 soptimestamp);
675 if (num_rb_contents) {
676 kgsl_sharedmem_writel(&device->memstore,
677 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
678 reftimestamp);
679 kgsl_sharedmem_writel(&device->memstore,
680 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
681 enable_ts);
682 }
683 /* Make sure all writes are posted before the GPU reads them */
684 wmb();
685 /* Mark the invalid context so no more commands are accepted from
686 * that context */
687
688 drawctxt = (struct adreno_context *) bad_context;
689
690 KGSL_CTXT_ERR(device,
691 "Context that caused a GPU hang: %x\n", bad_context);
692
693 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
694
695 /* Restore valid commands in ringbuffer */
696 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
697 rb->timestamp = timestamp;
698done:
699 vfree(rb_buffer);
700 return ret;
701}
702
703static int
704adreno_dump_and_recover(struct kgsl_device *device)
705{
706 static int recovery;
707 int result = -ETIMEDOUT;
708
709 if (device->state == KGSL_STATE_HUNG)
710 goto done;
711 if (device->state == KGSL_STATE_DUMP_AND_RECOVER && !recovery) {
712 mutex_unlock(&device->mutex);
713 wait_for_completion(&device->recovery_gate);
714 mutex_lock(&device->mutex);
715 if (!(device->state & KGSL_STATE_HUNG))
716 /* recovery success */
717 result = 0;
718 } else {
719 INIT_COMPLETION(device->recovery_gate);
720 /* Detected a hang - trigger an automatic dump */
721 adreno_postmortem_dump(device, 0);
722 if (!recovery) {
723 recovery = 1;
724 result = adreno_recover_hang(device);
725 if (result)
726 device->state = KGSL_STATE_HUNG;
727 recovery = 0;
728 complete_all(&device->recovery_gate);
729 } else
730 KGSL_DRV_ERR(device,
731 "Cannot recover from another hang while "
732 "recovering from a hang\n");
733 }
734done:
735 return result;
736}
737
738static int adreno_getproperty(struct kgsl_device *device,
739 enum kgsl_property_type type,
740 void *value,
741 unsigned int sizebytes)
742{
743 int status = -EINVAL;
744 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
745
746 switch (type) {
747 case KGSL_PROP_DEVICE_INFO:
748 {
749 struct kgsl_devinfo devinfo;
750
751 if (sizebytes != sizeof(devinfo)) {
752 status = -EINVAL;
753 break;
754 }
755
756 memset(&devinfo, 0, sizeof(devinfo));
757 devinfo.device_id = device->id+1;
758 devinfo.chip_id = adreno_dev->chip_id;
759 devinfo.mmu_enabled = kgsl_mmu_enabled();
760 devinfo.gpu_id = adreno_dev->gpurev;
761 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
762 gpu_base;
763 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
764 sizebytes;
765
766 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
767 0) {
768 status = -EFAULT;
769 break;
770 }
771 status = 0;
772 }
773 break;
774 case KGSL_PROP_DEVICE_SHADOW:
775 {
776 struct kgsl_shadowprop shadowprop;
777
778 if (sizebytes != sizeof(shadowprop)) {
779 status = -EINVAL;
780 break;
781 }
782 memset(&shadowprop, 0, sizeof(shadowprop));
783 if (device->memstore.hostptr) {
784 /*NOTE: with mmu enabled, gpuaddr doesn't mean
785 * anything to mmap().
786 */
787 shadowprop.gpuaddr = device->memstore.physaddr;
788 shadowprop.size = device->memstore.size;
789 /* GSL needs this to be set, even if it
790 appears to be meaningless */
791 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
792 }
793 if (copy_to_user(value, &shadowprop,
794 sizeof(shadowprop))) {
795 status = -EFAULT;
796 break;
797 }
798 status = 0;
799 }
800 break;
801 case KGSL_PROP_MMU_ENABLE:
802 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600803 int mmu_prop = kgsl_mmu_enabled();
804
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805 if (sizebytes != sizeof(int)) {
806 status = -EINVAL;
807 break;
808 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600809 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810 status = -EFAULT;
811 break;
812 }
813 status = 0;
814 }
815 break;
816 case KGSL_PROP_INTERRUPT_WAITS:
817 {
818 int int_waits = 1;
819 if (sizebytes != sizeof(int)) {
820 status = -EINVAL;
821 break;
822 }
823 if (copy_to_user(value, &int_waits, sizeof(int))) {
824 status = -EFAULT;
825 break;
826 }
827 status = 0;
828 }
829 break;
830 default:
831 status = -EINVAL;
832 }
833
834 return status;
835}
836
837/* Caller must hold the device mutex. */
838int adreno_idle(struct kgsl_device *device, unsigned int timeout)
839{
840 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
841 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
842 unsigned int rbbm_status;
843 unsigned long wait_time = jiffies + MAX_WAITGPU_SECS;
844
845 kgsl_cffdump_regpoll(device->id, REG_RBBM_STATUS << 2,
846 0x00000000, 0x80000000);
847 /* first, wait until the CP has consumed all the commands in
848 * the ring buffer
849 */
850retry:
851 if (rb->flags & KGSL_FLAGS_STARTED) {
852 do {
853 GSL_RB_GET_READPTR(rb, &rb->rptr);
854 if (time_after(jiffies, wait_time)) {
855 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
856 rb->rptr, rb->wptr);
857 goto err;
858 }
859 } while (rb->rptr != rb->wptr);
860 }
861
862 /* now, wait for the GPU to finish its operations */
863 wait_time = jiffies + MAX_WAITGPU_SECS;
864 while (time_before(jiffies, wait_time)) {
865 adreno_regread(device, REG_RBBM_STATUS, &rbbm_status);
866 if (rbbm_status == 0x110)
867 return 0;
868 }
869
870err:
871 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
872 if (!adreno_dump_and_recover(device)) {
873 wait_time = jiffies + MAX_WAITGPU_SECS;
874 goto retry;
875 }
876 return -ETIMEDOUT;
877}
878
879static unsigned int adreno_isidle(struct kgsl_device *device)
880{
881 int status = false;
882 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
883 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
884 unsigned int rbbm_status;
885
886 if (rb->flags & KGSL_FLAGS_STARTED) {
887 /* Is the ring buffer is empty? */
888 GSL_RB_GET_READPTR(rb, &rb->rptr);
889 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
890 /* Is the core idle? */
891 adreno_regread(device, REG_RBBM_STATUS,
892 &rbbm_status);
893 if (rbbm_status == 0x110)
894 status = true;
895 }
896 } else {
897 KGSL_DRV_ERR(device, "ringbuffer not started\n");
898 BUG();
899 }
900 return status;
901}
902
903/* Caller must hold the device mutex. */
904static int adreno_suspend_context(struct kgsl_device *device)
905{
906 int status = 0;
907 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
908
909 /* switch to NULL ctxt */
910 if (adreno_dev->drawctxt_active != NULL) {
911 adreno_drawctxt_switch(adreno_dev, NULL, 0);
912 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
913 }
914
915 return status;
916}
917
918uint8_t *kgsl_sharedmem_convertaddr(struct kgsl_device *device,
919 unsigned int pt_base, unsigned int gpuaddr, unsigned int *size)
920{
921 uint8_t *result = NULL;
922 struct kgsl_mem_entry *entry;
923 struct kgsl_process_private *priv;
924 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
925 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
926
927 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr)) {
928 return kgsl_gpuaddr_to_vaddr(&ringbuffer->buffer_desc,
929 gpuaddr, size);
930 }
931
932 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr)) {
933 return kgsl_gpuaddr_to_vaddr(&ringbuffer->memptrs_desc,
934 gpuaddr, size);
935 }
936
937 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr)) {
938 return kgsl_gpuaddr_to_vaddr(&device->memstore,
939 gpuaddr, size);
940 }
941
942 mutex_lock(&kgsl_driver.process_mutex);
943 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600944 if (!kgsl_mmu_pt_equal(priv->pagetable, pt_base))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 spin_lock(&priv->mem_lock);
947 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
948 sizeof(unsigned int));
949 if (entry) {
950 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
951 gpuaddr, size);
952 spin_unlock(&priv->mem_lock);
953 mutex_unlock(&kgsl_driver.process_mutex);
954 return result;
955 }
956 spin_unlock(&priv->mem_lock);
957 }
958 mutex_unlock(&kgsl_driver.process_mutex);
959
960 BUG_ON(!mutex_is_locked(&device->mutex));
961 list_for_each_entry(entry, &device->memqueue, list) {
962 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr)) {
963 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
964 gpuaddr, size);
965 break;
966 }
967
968 }
969 return result;
970}
971
972void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
973 unsigned int *value)
974{
975 unsigned int *reg;
976 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
977 reg = (unsigned int *)(device->regspace.mmio_virt_base
978 + (offsetwords << 2));
979
980 if (!in_interrupt())
981 kgsl_pre_hwaccess(device);
982
983 /*ensure this read finishes before the next one.
984 * i.e. act like normal readl() */
985 *value = __raw_readl(reg);
986 rmb();
987}
988
989void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
990 unsigned int value)
991{
992 unsigned int *reg;
993
994 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
995
996 if (!in_interrupt())
997 kgsl_pre_hwaccess(device);
998
999 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1000 reg = (unsigned int *)(device->regspace.mmio_virt_base
1001 + (offsetwords << 2));
1002
1003 /*ensure previous writes post before this one,
1004 * i.e. act like normal writel() */
1005 wmb();
1006 __raw_writel(value, reg);
1007}
1008
1009static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1010 unsigned int timestamp)
1011{
1012 int status;
1013 unsigned int ref_ts, enableflag;
1014
1015 status = kgsl_check_timestamp(device, timestamp);
1016 if (!status) {
1017 mutex_lock(&device->mutex);
1018 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1019 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1020 mb();
1021
1022 if (enableflag) {
1023 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1024 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1025 mb();
1026 if (timestamp_cmp(ref_ts, timestamp)) {
1027 kgsl_sharedmem_writel(&device->memstore,
1028 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1029 timestamp);
1030 wmb();
1031 }
1032 } else {
1033 unsigned int cmds[2];
1034 kgsl_sharedmem_writel(&device->memstore,
1035 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1036 timestamp);
1037 enableflag = 1;
1038 kgsl_sharedmem_writel(&device->memstore,
1039 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1040 enableflag);
1041 wmb();
1042 /* submit a dummy packet so that even if all
1043 * commands upto timestamp get executed we will still
1044 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001045 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046 cmds[1] = 0;
1047 adreno_ringbuffer_issuecmds(device, 0, &cmds[0], 2);
1048 }
1049 mutex_unlock(&device->mutex);
1050 }
1051
1052 return status;
1053}
1054
1055/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001056 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001057 placing a process in wait q. For conditional interrupts we expect the
1058 process to already be in its wait q when its exit condition checking
1059 function is called.
1060*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001061#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062({ \
1063 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001064 if (io) \
1065 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1066 else \
1067 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 __ret; \
1069})
1070
1071/* MUST be called with the device mutex held */
1072static int adreno_waittimestamp(struct kgsl_device *device,
1073 unsigned int timestamp,
1074 unsigned int msecs)
1075{
1076 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001077 uint io = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001078 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001079 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080
1081 if (timestamp != adreno_dev->ringbuffer.timestamp &&
1082 timestamp_cmp(timestamp,
1083 adreno_dev->ringbuffer.timestamp)) {
1084 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1085 "rb->timestamp: %x\n",
1086 timestamp, adreno_dev->ringbuffer.timestamp);
1087 status = -EINVAL;
1088 goto done;
1089 }
1090 if (!kgsl_check_timestamp(device, timestamp)) {
Lucille Sylvester02e46292011-09-21 14:59:17 -06001091 if (pwr->active_pwrlevel) {
1092 int low_pwrlevel = pwr->num_pwrlevels -
1093 KGSL_PWRLEVEL_LOW_OFFSET;
1094 if (pwr->active_pwrlevel == low_pwrlevel)
1095 io = 0;
1096 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 mutex_unlock(&device->mutex);
1098 /* We need to make sure that the process is placed in wait-q
1099 * before its condition is called */
Lucille Sylvester02e46292011-09-21 14:59:17 -06001100 status = kgsl_wait_event_interruptible_timeout(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001101 device->wait_queue,
1102 kgsl_check_interrupt_timestamp(device,
Lucille Sylvester02e46292011-09-21 14:59:17 -06001103 timestamp),
1104 msecs_to_jiffies(msecs), io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105 mutex_lock(&device->mutex);
1106
1107 if (status > 0)
1108 status = 0;
1109 else if (status == 0) {
1110 if (!kgsl_check_timestamp(device, timestamp)) {
1111 status = -ETIMEDOUT;
1112 KGSL_DRV_ERR(device,
1113 "Device hang detected while waiting "
1114 "for timestamp: %x, last "
1115 "submitted(rb->timestamp): %x, wptr: "
1116 "%x\n", timestamp,
1117 adreno_dev->ringbuffer.timestamp,
1118 adreno_dev->ringbuffer.wptr);
1119 if (!adreno_dump_and_recover(device)) {
1120 /* wait for idle after recovery as the
1121 * timestamp that this process wanted
1122 * to wait on may be invalid */
1123 if (!adreno_idle(device,
1124 KGSL_TIMEOUT_DEFAULT))
1125 status = 0;
1126 }
1127 }
1128 }
1129 }
1130
1131done:
1132 return (int)status;
1133}
1134
1135static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1136 enum kgsl_timestamp_type type)
1137{
1138 unsigned int timestamp = 0;
1139
1140 if (type == KGSL_TIMESTAMP_CONSUMED)
1141 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1142 else if (type == KGSL_TIMESTAMP_RETIRED)
1143 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1144 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1145 rmb();
1146
1147 return timestamp;
1148}
1149
1150static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1151 unsigned int cmd, void *data)
1152{
1153 int result = 0;
1154 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1155 struct kgsl_context *context;
1156
1157 switch (cmd) {
1158 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1159 binbase = data;
1160
1161 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1162 if (context) {
1163 adreno_drawctxt_set_bin_base_offset(
1164 dev_priv->device, context, binbase->offset);
1165 } else {
1166 result = -EINVAL;
1167 KGSL_DRV_ERR(dev_priv->device,
1168 "invalid drawctxt drawctxt_id %d "
1169 "device_id=%d\n",
1170 binbase->drawctxt_id, dev_priv->device->id);
1171 }
1172 break;
1173
1174 default:
1175 KGSL_DRV_INFO(dev_priv->device,
1176 "invalid ioctl code %08x\n", cmd);
1177 result = -EINVAL;
1178 break;
1179 }
1180 return result;
1181
1182}
1183
1184static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1185{
1186 gpu_freq /= 1000000;
1187 return ticks / gpu_freq;
1188}
1189
1190static void adreno_power_stats(struct kgsl_device *device,
1191 struct kgsl_power_stats *stats)
1192{
1193 unsigned int reg;
1194 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
1195
1196 /* In order to calculate idle you have to have run the algorithm *
1197 * at least once to get a start time. */
1198 if (pwr->time != 0) {
1199 s64 tmp;
1200 /* Stop the performance moniter and read the current *
1201 * busy cycles. */
1202 adreno_regwrite(device,
1203 REG_CP_PERFMON_CNTL,
1204 REG_PERF_MODE_CNT |
1205 REG_PERF_STATE_FREEZE);
1206 adreno_regread(device, REG_RBBM_PERFCOUNTER1_LO, &reg);
1207 tmp = ktime_to_us(ktime_get());
1208 stats->total_time = tmp - pwr->time;
1209 pwr->time = tmp;
1210 stats->busy_time = adreno_ticks_to_us(reg, device->pwrctrl.
1211 pwrlevels[device->pwrctrl.active_pwrlevel].
1212 gpu_freq);
1213
1214 adreno_regwrite(device,
1215 REG_CP_PERFMON_CNTL,
1216 REG_PERF_MODE_CNT |
1217 REG_PERF_STATE_RESET);
1218 } else {
1219 stats->total_time = 0;
1220 stats->busy_time = 0;
1221 pwr->time = ktime_to_us(ktime_get());
1222 }
1223
1224 /* re-enable the performance moniters */
1225 adreno_regread(device, REG_RBBM_PM_OVERRIDE2, &reg);
1226 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, (reg | 0x40));
1227 adreno_regwrite(device, REG_RBBM_PERFCOUNTER1_SELECT, 0x1);
1228 adreno_regwrite(device,
1229 REG_CP_PERFMON_CNTL,
1230 REG_PERF_MODE_CNT | REG_PERF_STATE_ENABLE);
1231}
1232
1233void adreno_irqctrl(struct kgsl_device *device, int state)
1234{
Jordan Crousea78c9172011-07-11 13:14:09 -06001235 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1236 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001237}
1238
1239static const struct kgsl_functable adreno_functable = {
1240 /* Mandatory functions */
1241 .regread = adreno_regread,
1242 .regwrite = adreno_regwrite,
1243 .idle = adreno_idle,
1244 .isidle = adreno_isidle,
1245 .suspend_context = adreno_suspend_context,
1246 .start = adreno_start,
1247 .stop = adreno_stop,
1248 .getproperty = adreno_getproperty,
1249 .waittimestamp = adreno_waittimestamp,
1250 .readtimestamp = adreno_readtimestamp,
1251 .issueibcmds = adreno_ringbuffer_issueibcmds,
1252 .ioctl = adreno_ioctl,
1253 .setup_pt = adreno_setup_pt,
1254 .cleanup_pt = adreno_cleanup_pt,
1255 .power_stats = adreno_power_stats,
1256 .irqctrl = adreno_irqctrl,
1257 /* Optional functions */
1258 .setstate = adreno_setstate,
1259 .drawctxt_create = adreno_drawctxt_create,
1260 .drawctxt_destroy = adreno_drawctxt_destroy,
1261};
1262
1263static struct platform_device_id adreno_id_table[] = {
1264 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1265 { },
1266};
1267MODULE_DEVICE_TABLE(platform, adreno_id_table);
1268
1269static struct platform_driver adreno_platform_driver = {
1270 .probe = adreno_probe,
1271 .remove = __devexit_p(adreno_remove),
1272 .suspend = kgsl_suspend_driver,
1273 .resume = kgsl_resume_driver,
1274 .id_table = adreno_id_table,
1275 .driver = {
1276 .owner = THIS_MODULE,
1277 .name = DEVICE_3D_NAME,
1278 .pm = &kgsl_pm_ops,
1279 }
1280};
1281
1282static int __init kgsl_3d_init(void)
1283{
1284 return platform_driver_register(&adreno_platform_driver);
1285}
1286
1287static void __exit kgsl_3d_exit(void)
1288{
1289 platform_driver_unregister(&adreno_platform_driver);
1290}
1291
1292module_init(kgsl_3d_init);
1293module_exit(kgsl_3d_exit);
1294
1295MODULE_DESCRIPTION("3D Graphics driver");
1296MODULE_VERSION("1.2");
1297MODULE_LICENSE("GPL v2");
1298MODULE_ALIAS("platform:kgsl_3d");