blob: 4207def56c533d155dc834f2ba2bce133ceae82d [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069static const struct kgsl_functable adreno_functable;
70
71static struct adreno_device device_3d0 = {
72 .dev = {
73 .name = DEVICE_3D0_NAME,
74 .id = KGSL_DEVICE_3D0,
75 .ver_major = DRIVER_VERSION_MAJOR,
76 .ver_minor = DRIVER_VERSION_MINOR,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060077 .mh = {
78 .mharb = ADRENO_CFG_MHARB,
79 /* Remove 1k boundary check in z470 to avoid a GPU
80 * hang. Notice that this solution won't work if
81 * both EBI and SMI are used
82 */
83 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084 /* turn off memory protection unit by setting
85 acceptable physical address range to include
86 all pages. */
87 .mpu_base = 0x00000000,
88 .mpu_range = 0xFFFFF000,
89 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060090 .mmu = {
91 .config = ADRENO_MMU_CONFIG,
92 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093 .pwrctrl = {
94 .regulator_name = "fs_gfx3d",
95 .irq_name = KGSL_3D0_IRQ,
Matt Wagantall9dc01632011-08-17 18:55:04 -070096 .src_clk_name = "src_clk",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097 },
98 .mutex = __MUTEX_INITIALIZER(device_3d0.dev.mutex),
99 .state = KGSL_STATE_INIT,
100 .active_cnt = 0,
101 .iomemname = KGSL_3D0_REG_MEMORY,
102 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600104 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
106 .suspend = kgsl_early_suspend_driver,
107 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600109#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110 },
111 .gmemspace = {
112 .gpu_base = 0,
113 .sizebytes = SZ_256K,
114 },
115 .pfp_fw = NULL,
116 .pm4_fw = NULL,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117};
118
Jordan Crouse505df9c2011-07-28 08:37:59 -0600119/*
120 * This is the master list of all GPU cores that are supported by this
121 * driver.
122 */
123
124#define ANY_ID (~0)
125
126static const struct {
127 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600128 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600129 const char *pm4fw;
130 const char *pfpfw;
131 struct adreno_gpudev *gpudev;
132} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600133 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jordan Crouse505df9c2011-07-28 08:37:59 -0600134 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600135 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jordan Crouse505df9c2011-07-28 08:37:59 -0600136 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600137 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jordan Crouse505df9c2011-07-28 08:37:59 -0600138 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600139 /*
140 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
141 * a hardware problem.
142 */
143 { ADRENO_REV_A225, 2, 2, 0, 5,
144 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev },
145 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jordan Crouse505df9c2011-07-28 08:37:59 -0600146 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev },
147};
148
Jordan Crouse9f739212011-07-28 08:37:57 -0600149static void adreno_gmeminit(struct adreno_device *adreno_dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150{
151 struct kgsl_device *device = &adreno_dev->dev;
152 union reg_rb_edram_info rb_edram_info;
153 unsigned int gmem_size;
154 unsigned int edram_value = 0;
155
156 /* make sure edram range is aligned to size */
157 BUG_ON(adreno_dev->gmemspace.gpu_base &
158 (adreno_dev->gmemspace.sizebytes - 1));
159
160 /* get edram_size value equivalent */
161 gmem_size = (adreno_dev->gmemspace.sizebytes >> 14);
162 while (gmem_size >>= 1)
163 edram_value++;
164
165 rb_edram_info.val = 0;
166
167 rb_edram_info.f.edram_size = edram_value;
Jordan Crouse9f739212011-07-28 08:37:57 -0600168 rb_edram_info.f.edram_mapping_mode = 0; /* EDRAM_MAP_UPPER */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169
170 /* must be aligned to size */
171 rb_edram_info.f.edram_range = (adreno_dev->gmemspace.gpu_base >> 14);
172
173 adreno_regwrite(device, REG_RB_EDRAM_INFO, rb_edram_info.val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174}
175
Jordan Crouse9f739212011-07-28 08:37:57 -0600176static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177{
Jordan Crousea78c9172011-07-11 13:14:09 -0600178 irqreturn_t result;
179 struct kgsl_device *device = data;
180 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181
Jordan Crousea78c9172011-07-11 13:14:09 -0600182 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183
184 if (device->requested_state == KGSL_STATE_NONE) {
185 if (device->pwrctrl.nap_allowed == true) {
186 device->requested_state = KGSL_STATE_NAP;
187 queue_work(device->work_queue, &device->idle_check_ws);
188 } else if (device->pwrscale.policy != NULL) {
189 queue_work(device->work_queue, &device->idle_check_ws);
190 }
191 }
192
193 /* Reset the time-out in our idle timer */
194 mod_timer(&device->idle_timer,
195 jiffies + device->pwrctrl.interval_timeout);
196 return result;
197}
198
Jordan Crouse9f739212011-07-28 08:37:57 -0600199static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 struct kgsl_pagetable *pagetable)
201{
202 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
203 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
204
205 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
206
207 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
208
209 kgsl_mmu_unmap(pagetable, &device->memstore);
210
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600211 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212}
213
214static int adreno_setup_pt(struct kgsl_device *device,
215 struct kgsl_pagetable *pagetable)
216{
217 int result = 0;
218 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
219 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
220
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
222 GSL_PT_PAGE_RV);
223 if (result)
224 goto error;
225
226 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
227 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
228 if (result)
229 goto unmap_buffer_desc;
230
231 result = kgsl_mmu_map_global(pagetable, &device->memstore,
232 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
233 if (result)
234 goto unmap_memptrs_desc;
235
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600236 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
238 if (result)
239 goto unmap_memstore_desc;
240
241 return result;
242
243unmap_memstore_desc:
244 kgsl_mmu_unmap(pagetable, &device->memstore);
245
246unmap_memptrs_desc:
247 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
248
249unmap_buffer_desc:
250 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
251
252error:
253 return result;
254}
255
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600256static void adreno_setstate(struct kgsl_device *device,
257 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258{
259 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
260 unsigned int link[32];
261 unsigned int *cmds = &link[0];
262 int sizedwords = 0;
263 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
264
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265 /* If possible, then set the state via the command stream to avoid
266 a CPU idle. Otherwise, use the default setstate which uses register
267 writes */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 if (adreno_dev->drawctxt_active) {
269 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
270 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600271 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 *cmds++ = 0x00000000;
273
274 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600275 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600276 *cmds++ = kgsl_pt_get_base_addr(
277 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 sizedwords += 4;
279 }
280
281 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
282 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600283 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284 1);
285 *cmds++ = 0x00000000;
286 sizedwords += 2;
287 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600288 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 *cmds++ = mh_mmu_invalidate;
290 sizedwords += 2;
291 }
292
293 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600294 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 /* HW workaround: to resolve MMU page fault interrupts
296 * caused by the VGT.It prevents the CP PFP from filling
297 * the VGT DMA request fifo too early,thereby ensuring
298 * that the VGT will not fetch vertex/bin data until
299 * after the page table base register has been updated.
300 *
301 * Two null DRAW_INDX_BIN packets are inserted right
302 * after the page table base update, followed by a
303 * wait for idle. The null packets will fill up the
304 * VGT DMA request fifo and prevent any further
305 * vertex/bin updates from occurring until the wait
306 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600307 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 *cmds++ = (0x4 << 16) |
309 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
310 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600311 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600312 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600313 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314 *cmds++ = 0; /* viz query info */
315 *cmds++ = 0x0003C004; /* draw indicator */
316 *cmds++ = 0; /* bin base */
317 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600318 *cmds++ =
319 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600321 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 *cmds++ = 0; /* viz query info */
323 *cmds++ = 0x0003C004; /* draw indicator */
324 *cmds++ = 0; /* bin base */
325 *cmds++ = 3; /* bin size */
326 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600327 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600329 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330 *cmds++ = 0x00000000;
331 sizedwords += 21;
332 }
333
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600334
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600336 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 *cmds++ = 0x7fff; /* invalidate all base pointers */
338 sizedwords += 2;
339 }
340
341 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
342 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600343 } else {
344 kgsl_mmu_device_setstate(device, flags);
345 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346}
347
348static unsigned int
349adreno_getchipid(struct kgsl_device *device)
350{
351 unsigned int chipid = 0;
352 unsigned int coreid, majorid, minorid, patchid, revid;
353
354 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
355 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
356 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
357
358 /*
359 * adreno 22x gpus are indicated by coreid 2,
360 * but REG_RBBM_PERIPHID1 always contains 0 for this field
361 */
362 if (cpu_is_msm8960() || cpu_is_msm8x60())
363 chipid = 2 << 24;
364 else
365 chipid = (coreid & 0xF) << 24;
366
367 chipid |= ((majorid >> 4) & 0xF) << 16;
368
369 minorid = ((revid >> 0) & 0xFF);
370
371 patchid = ((revid >> 16) & 0xFF);
372
373 /* 8x50 returns 0 for patch release, but it should be 1 */
374 if (cpu_is_qsd8x50())
375 patchid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376
377 chipid |= (minorid << 8) | patchid;
378
379 return chipid;
380}
381
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382static inline bool _rev_match(unsigned int id, unsigned int entry)
383{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600384 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386
387static void
388adreno_identify_gpu(struct adreno_device *adreno_dev)
389{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600390 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391
392 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
393
394 core = (adreno_dev->chip_id >> 24) & 0xff;
395 major = (adreno_dev->chip_id >> 16) & 0xff;
396 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600397 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398
Jordan Crouse505df9c2011-07-28 08:37:59 -0600399 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
400 if (core == adreno_gpulist[i].core &&
401 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600402 _rev_match(minor, adreno_gpulist[i].minor) &&
403 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405 }
406
Jordan Crouse505df9c2011-07-28 08:37:59 -0600407 if (i == ARRAY_SIZE(adreno_gpulist)) {
408 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
409 return;
410 }
411
412 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
413 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
414 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
415 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416}
417
418static int __devinit
419adreno_probe(struct platform_device *pdev)
420{
421 struct kgsl_device *device;
422 struct adreno_device *adreno_dev;
423 int status = -EINVAL;
424
425 device = (struct kgsl_device *)pdev->id_entry->driver_data;
426 adreno_dev = ADRENO_DEVICE(device);
427 device->parentdev = &pdev->dev;
428
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530429 adreno_dev->wait_timeout = 10000; /* default value in milliseconds */
430
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 init_completion(&device->recovery_gate);
432
433 status = adreno_ringbuffer_init(device);
434 if (status != 0)
435 goto error;
436
437 status = kgsl_device_platform_probe(device, adreno_isr);
438 if (status)
439 goto error_close_rb;
440
441 adreno_debugfs_init(device);
442
443 kgsl_pwrscale_init(device);
444 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
445
446 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
447 return 0;
448
449error_close_rb:
450 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
451error:
452 device->parentdev = NULL;
453 return status;
454}
455
456static int __devexit adreno_remove(struct platform_device *pdev)
457{
458 struct kgsl_device *device;
459 struct adreno_device *adreno_dev;
460
461 device = (struct kgsl_device *)pdev->id_entry->driver_data;
462 adreno_dev = ADRENO_DEVICE(device);
463
464 kgsl_pwrscale_detach_policy(device);
465 kgsl_pwrscale_close(device);
466
467 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
468 kgsl_device_platform_remove(device);
469
470 return 0;
471}
472
473static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
474{
475 int status = -EINVAL;
476 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
477 int init_reftimestamp = 0x7fffffff;
478
479 device->state = KGSL_STATE_INIT;
480 device->requested_state = KGSL_STATE_NONE;
481
482 /* Power up the device */
483 kgsl_pwrctrl_enable(device);
484
485 /* Identify the specific GPU */
486 adreno_identify_gpu(adreno_dev);
487
Jordan Crouse505df9c2011-07-28 08:37:59 -0600488 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
489 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
490 adreno_dev->chip_id);
491 goto error_clk_off;
492 }
493
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600494 if (adreno_is_a20x(adreno_dev)) {
495 /*
496 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
497 * on older gpus
498 */
499 device->mh.mh_intf_cfg1 = 0;
500 device->mh.mh_intf_cfg2 = 0;
501 }
502
503 kgsl_mh_start(device);
504
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 if (kgsl_mmu_start(device))
506 goto error_clk_off;
507
508 /*We need to make sure all blocks are powered up and clocked before
509 *issuing a soft reset. The overrides will then be turned off (set to 0)
510 */
511 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0xfffffffe);
512 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0xffffffff);
513
514 /* Only reset CP block if all blocks have previously been reset */
515 if (!(device->flags & KGSL_FLAGS_SOFT_RESET) ||
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600516 !adreno_is_a22x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0xFFFFFFFF);
518 device->flags |= KGSL_FLAGS_SOFT_RESET;
519 } else
520 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000001);
521
522 /* The core is in an indeterminate state until the reset completes
523 * after 30ms.
524 */
525 msleep(30);
526
527 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000000);
528
529 adreno_regwrite(device, REG_RBBM_CNTL, 0x00004442);
530
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600531 if (adreno_is_a225(adreno_dev)) {
532 /* Enable large instruction store for A225 */
533 adreno_regwrite(device, REG_SQ_FLOW_CONTROL, 0x18000000);
534 }
535
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536 adreno_regwrite(device, REG_SQ_VS_PROGRAM, 0x00000000);
537 adreno_regwrite(device, REG_SQ_PS_PROGRAM, 0x00000000);
538
Tarun Karra96a12672011-09-23 18:52:39 -0700539 if (cpu_is_msm8960())
540 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0x200);
541 else
542 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0);
543
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600544 if (!adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0);
546 else
547 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0x80);
548
Sushmita Susheelendraf3896062011-08-12 16:33:10 -0600549 kgsl_sharedmem_set(&device->memstore, 0, 0, device->memstore.size);
550
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 kgsl_sharedmem_writel(&device->memstore,
552 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
553 init_reftimestamp);
554
Ranjhith Kalisamyf81dcd02011-09-30 16:45:24 +0530555 adreno_regwrite(device, REG_RBBM_DEBUG, 0x00080000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556
557 /* Make sure interrupts are disabled */
558
559 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
560 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
561 adreno_regwrite(device, REG_SQ_INT_CNTL, 0);
562
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600563 if (adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 adreno_dev->gmemspace.sizebytes = SZ_512K;
565 else
566 adreno_dev->gmemspace.sizebytes = SZ_256K;
567 adreno_gmeminit(adreno_dev);
568
569 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
570
571 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
572 if (status != 0)
573 goto error_irq_off;
574
575 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
576 return status;
577
578error_irq_off:
579 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600580 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581error_clk_off:
582 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583
584 return status;
585}
586
587static int adreno_stop(struct kgsl_device *device)
588{
589 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
590
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 adreno_dev->drawctxt_active = NULL;
592
593 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
594
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 kgsl_mmu_stop(device);
596
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600597 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
598 del_timer_sync(&device->idle_timer);
599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 /* Power down the device */
601 kgsl_pwrctrl_disable(device);
602
603 return 0;
604}
605
606static int
607adreno_recover_hang(struct kgsl_device *device)
608{
609 int ret;
610 unsigned int *rb_buffer;
611 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
612 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
613 unsigned int timestamp;
614 unsigned int num_rb_contents;
615 unsigned int bad_context;
616 unsigned int reftimestamp;
617 unsigned int enable_ts;
618 unsigned int soptimestamp;
619 unsigned int eoptimestamp;
620 struct adreno_context *drawctxt;
621
622 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
623 rb_buffer = vmalloc(rb->buffer_desc.size);
624 if (!rb_buffer) {
625 KGSL_MEM_ERR(device,
626 "Failed to allocate memory for recovery: %x\n",
627 rb->buffer_desc.size);
628 return -ENOMEM;
629 }
630 /* Extract valid contents from rb which can stil be executed after
631 * hang */
632 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
633 if (ret)
634 goto done;
635 timestamp = rb->timestamp;
636 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
637 kgsl_sharedmem_readl(&device->memstore, &bad_context,
638 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
639 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
640 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
641 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
642 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
643 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
644 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
645 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
646 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
647 /* Make sure memory is synchronized before restarting the GPU */
648 mb();
649 KGSL_CTXT_ERR(device,
650 "Context that caused a GPU hang: %x\n", bad_context);
651 /* restart device */
652 ret = adreno_stop(device);
653 if (ret)
654 goto done;
655 ret = adreno_start(device, true);
656 if (ret)
657 goto done;
658 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
659 /* Restore timestamp states */
660 kgsl_sharedmem_writel(&device->memstore,
661 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
662 soptimestamp);
663 kgsl_sharedmem_writel(&device->memstore,
664 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
665 eoptimestamp);
666 kgsl_sharedmem_writel(&device->memstore,
667 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
668 soptimestamp);
669 if (num_rb_contents) {
670 kgsl_sharedmem_writel(&device->memstore,
671 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
672 reftimestamp);
673 kgsl_sharedmem_writel(&device->memstore,
674 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
675 enable_ts);
676 }
677 /* Make sure all writes are posted before the GPU reads them */
678 wmb();
679 /* Mark the invalid context so no more commands are accepted from
680 * that context */
681
682 drawctxt = (struct adreno_context *) bad_context;
683
684 KGSL_CTXT_ERR(device,
685 "Context that caused a GPU hang: %x\n", bad_context);
686
687 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
688
689 /* Restore valid commands in ringbuffer */
690 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
691 rb->timestamp = timestamp;
692done:
693 vfree(rb_buffer);
694 return ret;
695}
696
697static int
698adreno_dump_and_recover(struct kgsl_device *device)
699{
700 static int recovery;
701 int result = -ETIMEDOUT;
702
703 if (device->state == KGSL_STATE_HUNG)
704 goto done;
705 if (device->state == KGSL_STATE_DUMP_AND_RECOVER && !recovery) {
706 mutex_unlock(&device->mutex);
707 wait_for_completion(&device->recovery_gate);
708 mutex_lock(&device->mutex);
709 if (!(device->state & KGSL_STATE_HUNG))
710 /* recovery success */
711 result = 0;
712 } else {
713 INIT_COMPLETION(device->recovery_gate);
714 /* Detected a hang - trigger an automatic dump */
715 adreno_postmortem_dump(device, 0);
716 if (!recovery) {
717 recovery = 1;
718 result = adreno_recover_hang(device);
719 if (result)
720 device->state = KGSL_STATE_HUNG;
721 recovery = 0;
722 complete_all(&device->recovery_gate);
723 } else
724 KGSL_DRV_ERR(device,
725 "Cannot recover from another hang while "
726 "recovering from a hang\n");
727 }
728done:
729 return result;
730}
731
732static int adreno_getproperty(struct kgsl_device *device,
733 enum kgsl_property_type type,
734 void *value,
735 unsigned int sizebytes)
736{
737 int status = -EINVAL;
738 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
739
740 switch (type) {
741 case KGSL_PROP_DEVICE_INFO:
742 {
743 struct kgsl_devinfo devinfo;
744
745 if (sizebytes != sizeof(devinfo)) {
746 status = -EINVAL;
747 break;
748 }
749
750 memset(&devinfo, 0, sizeof(devinfo));
751 devinfo.device_id = device->id+1;
752 devinfo.chip_id = adreno_dev->chip_id;
753 devinfo.mmu_enabled = kgsl_mmu_enabled();
754 devinfo.gpu_id = adreno_dev->gpurev;
755 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
756 gpu_base;
757 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
758 sizebytes;
759
760 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
761 0) {
762 status = -EFAULT;
763 break;
764 }
765 status = 0;
766 }
767 break;
768 case KGSL_PROP_DEVICE_SHADOW:
769 {
770 struct kgsl_shadowprop shadowprop;
771
772 if (sizebytes != sizeof(shadowprop)) {
773 status = -EINVAL;
774 break;
775 }
776 memset(&shadowprop, 0, sizeof(shadowprop));
777 if (device->memstore.hostptr) {
778 /*NOTE: with mmu enabled, gpuaddr doesn't mean
779 * anything to mmap().
780 */
781 shadowprop.gpuaddr = device->memstore.physaddr;
782 shadowprop.size = device->memstore.size;
783 /* GSL needs this to be set, even if it
784 appears to be meaningless */
785 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
786 }
787 if (copy_to_user(value, &shadowprop,
788 sizeof(shadowprop))) {
789 status = -EFAULT;
790 break;
791 }
792 status = 0;
793 }
794 break;
795 case KGSL_PROP_MMU_ENABLE:
796 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600797 int mmu_prop = kgsl_mmu_enabled();
798
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 if (sizebytes != sizeof(int)) {
800 status = -EINVAL;
801 break;
802 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600803 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 status = -EFAULT;
805 break;
806 }
807 status = 0;
808 }
809 break;
810 case KGSL_PROP_INTERRUPT_WAITS:
811 {
812 int int_waits = 1;
813 if (sizebytes != sizeof(int)) {
814 status = -EINVAL;
815 break;
816 }
817 if (copy_to_user(value, &int_waits, sizeof(int))) {
818 status = -EFAULT;
819 break;
820 }
821 status = 0;
822 }
823 break;
824 default:
825 status = -EINVAL;
826 }
827
828 return status;
829}
830
831/* Caller must hold the device mutex. */
832int adreno_idle(struct kgsl_device *device, unsigned int timeout)
833{
834 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
835 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
836 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530837 unsigned long wait_timeout =
838 msecs_to_jiffies(adreno_dev->wait_timeout);
839 unsigned long wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840
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 */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530859 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 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)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530869 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870 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
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301077 /* Don't wait forever, set a max value for now */
1078 if (msecs == -1)
1079 msecs = adreno_dev->wait_timeout;
1080
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001081 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");