blob: 18bac1bd85e01c34c6b8c6d19e99b30c3b618b66 [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,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 },
97 .mutex = __MUTEX_INITIALIZER(device_3d0.dev.mutex),
98 .state = KGSL_STATE_INIT,
99 .active_cnt = 0,
100 .iomemname = KGSL_3D0_REG_MEMORY,
101 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600103 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
105 .suspend = kgsl_early_suspend_driver,
106 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600108#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 },
110 .gmemspace = {
111 .gpu_base = 0,
112 .sizebytes = SZ_256K,
113 },
114 .pfp_fw = NULL,
115 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700116 .wait_timeout = 10000, /* in milliseconds */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117};
118
Jordan Crouse95b33272011-11-11 14:50:12 -0700119
Jordan Crouse505df9c2011-07-28 08:37:59 -0600120/*
121 * This is the master list of all GPU cores that are supported by this
122 * driver.
123 */
124
125#define ANY_ID (~0)
126
127static const struct {
128 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600129 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600130 const char *pm4fw;
131 const char *pfpfw;
132 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700133 unsigned int istore_size;
134 unsigned int pix_shader_start;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600135} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600136 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700137 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
138 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600139 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700140 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
141 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600142 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700143 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
144 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600145 /*
146 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
147 * a hardware problem.
148 */
149 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700150 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
151 1536, 768 },
Carter Cooperf27ec722011-11-17 15:20:38 -0700152 { ADRENO_REV_A225, 2, 2, 0, 6,
153 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
154 1536, 768 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600155 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700156 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
157 1536, 768 },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600158};
159
Jordan Crouse9f739212011-07-28 08:37:57 -0600160static void adreno_gmeminit(struct adreno_device *adreno_dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161{
162 struct kgsl_device *device = &adreno_dev->dev;
163 union reg_rb_edram_info rb_edram_info;
164 unsigned int gmem_size;
165 unsigned int edram_value = 0;
166
167 /* make sure edram range is aligned to size */
168 BUG_ON(adreno_dev->gmemspace.gpu_base &
169 (adreno_dev->gmemspace.sizebytes - 1));
170
171 /* get edram_size value equivalent */
172 gmem_size = (adreno_dev->gmemspace.sizebytes >> 14);
173 while (gmem_size >>= 1)
174 edram_value++;
175
176 rb_edram_info.val = 0;
177
178 rb_edram_info.f.edram_size = edram_value;
Jordan Crouse9f739212011-07-28 08:37:57 -0600179 rb_edram_info.f.edram_mapping_mode = 0; /* EDRAM_MAP_UPPER */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180
181 /* must be aligned to size */
182 rb_edram_info.f.edram_range = (adreno_dev->gmemspace.gpu_base >> 14);
183
184 adreno_regwrite(device, REG_RB_EDRAM_INFO, rb_edram_info.val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185}
186
Jordan Crouse9f739212011-07-28 08:37:57 -0600187static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188{
Jordan Crousea78c9172011-07-11 13:14:09 -0600189 irqreturn_t result;
190 struct kgsl_device *device = data;
191 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192
Jordan Crousea78c9172011-07-11 13:14:09 -0600193 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
195 if (device->requested_state == KGSL_STATE_NONE) {
196 if (device->pwrctrl.nap_allowed == true) {
197 device->requested_state = KGSL_STATE_NAP;
198 queue_work(device->work_queue, &device->idle_check_ws);
199 } else if (device->pwrscale.policy != NULL) {
200 queue_work(device->work_queue, &device->idle_check_ws);
201 }
202 }
203
204 /* Reset the time-out in our idle timer */
205 mod_timer(&device->idle_timer,
206 jiffies + device->pwrctrl.interval_timeout);
207 return result;
208}
209
Jordan Crouse9f739212011-07-28 08:37:57 -0600210static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 struct kgsl_pagetable *pagetable)
212{
213 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
214 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
215
216 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
217
218 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
219
220 kgsl_mmu_unmap(pagetable, &device->memstore);
221
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600222 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223}
224
225static int adreno_setup_pt(struct kgsl_device *device,
226 struct kgsl_pagetable *pagetable)
227{
228 int result = 0;
229 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
230 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
231
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
233 GSL_PT_PAGE_RV);
234 if (result)
235 goto error;
236
237 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
238 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
239 if (result)
240 goto unmap_buffer_desc;
241
242 result = kgsl_mmu_map_global(pagetable, &device->memstore,
243 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
244 if (result)
245 goto unmap_memptrs_desc;
246
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600247 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
249 if (result)
250 goto unmap_memstore_desc;
251
252 return result;
253
254unmap_memstore_desc:
255 kgsl_mmu_unmap(pagetable, &device->memstore);
256
257unmap_memptrs_desc:
258 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
259
260unmap_buffer_desc:
261 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
262
263error:
264 return result;
265}
266
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600267static void adreno_setstate(struct kgsl_device *device,
268 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269{
270 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
271 unsigned int link[32];
272 unsigned int *cmds = &link[0];
273 int sizedwords = 0;
274 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
275
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276 /* If possible, then set the state via the command stream to avoid
277 a CPU idle. Otherwise, use the default setstate which uses register
278 writes */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 if (adreno_dev->drawctxt_active) {
280 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
281 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600282 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 *cmds++ = 0x00000000;
284
285 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600286 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600287 *cmds++ = kgsl_pt_get_base_addr(
288 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 sizedwords += 4;
290 }
291
292 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
293 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600294 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 1);
296 *cmds++ = 0x00000000;
297 sizedwords += 2;
298 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600299 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 *cmds++ = mh_mmu_invalidate;
301 sizedwords += 2;
302 }
303
304 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600305 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 /* HW workaround: to resolve MMU page fault interrupts
307 * caused by the VGT.It prevents the CP PFP from filling
308 * the VGT DMA request fifo too early,thereby ensuring
309 * that the VGT will not fetch vertex/bin data until
310 * after the page table base register has been updated.
311 *
312 * Two null DRAW_INDX_BIN packets are inserted right
313 * after the page table base update, followed by a
314 * wait for idle. The null packets will fill up the
315 * VGT DMA request fifo and prevent any further
316 * vertex/bin updates from occurring until the wait
317 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600318 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 *cmds++ = (0x4 << 16) |
320 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
321 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600322 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600323 *cmds++ = device->mmu.setstate_memory.gpuaddr;
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 */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600329 *cmds++ =
330 device->mmu.setstate_memory.gpuaddr; /* dma base */
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_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 *cmds++ = 0; /* viz query info */
334 *cmds++ = 0x0003C004; /* draw indicator */
335 *cmds++ = 0; /* bin base */
336 *cmds++ = 3; /* bin size */
337 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600338 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600340 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341 *cmds++ = 0x00000000;
342 sizedwords += 21;
343 }
344
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600345
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600347 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348 *cmds++ = 0x7fff; /* invalidate all base pointers */
349 sizedwords += 2;
350 }
351
352 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
353 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600354 } else {
355 kgsl_mmu_device_setstate(device, flags);
356 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357}
358
359static unsigned int
360adreno_getchipid(struct kgsl_device *device)
361{
362 unsigned int chipid = 0;
363 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700364 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365
366 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
367 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
368 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
369
370 /*
371 * adreno 22x gpus are indicated by coreid 2,
372 * but REG_RBBM_PERIPHID1 always contains 0 for this field
373 */
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700374 if (cpu_is_msm8960() || cpu_is_msm8x60() || cpu_is_msm8930())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 chipid = 2 << 24;
376 else
377 chipid = (coreid & 0xF) << 24;
378
379 chipid |= ((majorid >> 4) & 0xF) << 16;
380
381 minorid = ((revid >> 0) & 0xFF);
382
383 patchid = ((revid >> 16) & 0xFF);
384
385 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700386 /* 8960v3 returns 5 for patch release, but it should be 6 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387 if (cpu_is_qsd8x50())
388 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700389 else if (cpu_is_msm8960() &&
390 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
391 patchid = 6;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392
393 chipid |= (minorid << 8) | patchid;
394
395 return chipid;
396}
397
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398static inline bool _rev_match(unsigned int id, unsigned int entry)
399{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600400 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402
403static void
404adreno_identify_gpu(struct adreno_device *adreno_dev)
405{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600406 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407
408 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
409
410 core = (adreno_dev->chip_id >> 24) & 0xff;
411 major = (adreno_dev->chip_id >> 16) & 0xff;
412 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600413 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414
Jordan Crouse505df9c2011-07-28 08:37:59 -0600415 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
416 if (core == adreno_gpulist[i].core &&
417 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600418 _rev_match(minor, adreno_gpulist[i].minor) &&
419 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421 }
422
Jordan Crouse505df9c2011-07-28 08:37:59 -0600423 if (i == ARRAY_SIZE(adreno_gpulist)) {
424 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
425 return;
426 }
427
428 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
429 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
430 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
431 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700432 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
433 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434}
435
436static int __devinit
437adreno_probe(struct platform_device *pdev)
438{
439 struct kgsl_device *device;
440 struct adreno_device *adreno_dev;
441 int status = -EINVAL;
442
443 device = (struct kgsl_device *)pdev->id_entry->driver_data;
444 adreno_dev = ADRENO_DEVICE(device);
445 device->parentdev = &pdev->dev;
446
447 init_completion(&device->recovery_gate);
448
449 status = adreno_ringbuffer_init(device);
450 if (status != 0)
451 goto error;
452
453 status = kgsl_device_platform_probe(device, adreno_isr);
454 if (status)
455 goto error_close_rb;
456
457 adreno_debugfs_init(device);
458
459 kgsl_pwrscale_init(device);
460 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
461
462 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
463 return 0;
464
465error_close_rb:
466 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
467error:
468 device->parentdev = NULL;
469 return status;
470}
471
472static int __devexit adreno_remove(struct platform_device *pdev)
473{
474 struct kgsl_device *device;
475 struct adreno_device *adreno_dev;
476
477 device = (struct kgsl_device *)pdev->id_entry->driver_data;
478 adreno_dev = ADRENO_DEVICE(device);
479
480 kgsl_pwrscale_detach_policy(device);
481 kgsl_pwrscale_close(device);
482
483 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
484 kgsl_device_platform_remove(device);
485
486 return 0;
487}
488
489static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
490{
491 int status = -EINVAL;
492 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
493 int init_reftimestamp = 0x7fffffff;
494
495 device->state = KGSL_STATE_INIT;
496 device->requested_state = KGSL_STATE_NONE;
497
498 /* Power up the device */
499 kgsl_pwrctrl_enable(device);
500
501 /* Identify the specific GPU */
502 adreno_identify_gpu(adreno_dev);
503
Jordan Crouse505df9c2011-07-28 08:37:59 -0600504 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
505 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
506 adreno_dev->chip_id);
507 goto error_clk_off;
508 }
509
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600510 if (adreno_is_a20x(adreno_dev)) {
511 /*
512 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
513 * on older gpus
514 */
515 device->mh.mh_intf_cfg1 = 0;
516 device->mh.mh_intf_cfg2 = 0;
517 }
518
519 kgsl_mh_start(device);
520
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521 if (kgsl_mmu_start(device))
522 goto error_clk_off;
523
524 /*We need to make sure all blocks are powered up and clocked before
525 *issuing a soft reset. The overrides will then be turned off (set to 0)
526 */
527 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0xfffffffe);
528 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0xffffffff);
529
530 /* Only reset CP block if all blocks have previously been reset */
531 if (!(device->flags & KGSL_FLAGS_SOFT_RESET) ||
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600532 !adreno_is_a22x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0xFFFFFFFF);
534 device->flags |= KGSL_FLAGS_SOFT_RESET;
535 } else
536 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000001);
537
538 /* The core is in an indeterminate state until the reset completes
539 * after 30ms.
540 */
541 msleep(30);
542
543 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000000);
544
545 adreno_regwrite(device, REG_RBBM_CNTL, 0x00004442);
546
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600547 if (adreno_is_a225(adreno_dev)) {
548 /* Enable large instruction store for A225 */
549 adreno_regwrite(device, REG_SQ_FLOW_CONTROL, 0x18000000);
550 }
551
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552 adreno_regwrite(device, REG_SQ_VS_PROGRAM, 0x00000000);
553 adreno_regwrite(device, REG_SQ_PS_PROGRAM, 0x00000000);
554
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700555 if (cpu_is_msm8960() || cpu_is_msm8930())
Tarun Karra96a12672011-09-23 18:52:39 -0700556 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0x200);
557 else
558 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0);
559
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600560 if (!adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0);
562 else
563 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0x80);
564
Sushmita Susheelendraf3896062011-08-12 16:33:10 -0600565 kgsl_sharedmem_set(&device->memstore, 0, 0, device->memstore.size);
566
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567 kgsl_sharedmem_writel(&device->memstore,
568 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
569 init_reftimestamp);
570
Ranjhith Kalisamyf81dcd02011-09-30 16:45:24 +0530571 adreno_regwrite(device, REG_RBBM_DEBUG, 0x00080000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572
573 /* Make sure interrupts are disabled */
574
575 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
576 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
577 adreno_regwrite(device, REG_SQ_INT_CNTL, 0);
578
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600579 if (adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 adreno_dev->gmemspace.sizebytes = SZ_512K;
581 else
582 adreno_dev->gmemspace.sizebytes = SZ_256K;
583 adreno_gmeminit(adreno_dev);
584
585 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
586
587 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
588 if (status != 0)
589 goto error_irq_off;
590
591 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
592 return status;
593
594error_irq_off:
595 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600596 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597error_clk_off:
598 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599
600 return status;
601}
602
603static int adreno_stop(struct kgsl_device *device)
604{
605 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
606
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 adreno_dev->drawctxt_active = NULL;
608
609 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
610
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611 kgsl_mmu_stop(device);
612
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600613 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
614 del_timer_sync(&device->idle_timer);
615
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616 /* Power down the device */
617 kgsl_pwrctrl_disable(device);
618
619 return 0;
620}
621
622static int
623adreno_recover_hang(struct kgsl_device *device)
624{
625 int ret;
626 unsigned int *rb_buffer;
627 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
628 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
629 unsigned int timestamp;
630 unsigned int num_rb_contents;
631 unsigned int bad_context;
632 unsigned int reftimestamp;
633 unsigned int enable_ts;
634 unsigned int soptimestamp;
635 unsigned int eoptimestamp;
636 struct adreno_context *drawctxt;
637
638 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
639 rb_buffer = vmalloc(rb->buffer_desc.size);
640 if (!rb_buffer) {
641 KGSL_MEM_ERR(device,
642 "Failed to allocate memory for recovery: %x\n",
643 rb->buffer_desc.size);
644 return -ENOMEM;
645 }
646 /* Extract valid contents from rb which can stil be executed after
647 * hang */
648 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
649 if (ret)
650 goto done;
651 timestamp = rb->timestamp;
652 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
653 kgsl_sharedmem_readl(&device->memstore, &bad_context,
654 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
655 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
656 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
657 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
658 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
659 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
660 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
661 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
662 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
663 /* Make sure memory is synchronized before restarting the GPU */
664 mb();
665 KGSL_CTXT_ERR(device,
666 "Context that caused a GPU hang: %x\n", bad_context);
667 /* restart device */
668 ret = adreno_stop(device);
669 if (ret)
670 goto done;
671 ret = adreno_start(device, true);
672 if (ret)
673 goto done;
674 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
675 /* Restore timestamp states */
676 kgsl_sharedmem_writel(&device->memstore,
677 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
678 soptimestamp);
679 kgsl_sharedmem_writel(&device->memstore,
680 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
681 eoptimestamp);
682 kgsl_sharedmem_writel(&device->memstore,
683 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
684 soptimestamp);
685 if (num_rb_contents) {
686 kgsl_sharedmem_writel(&device->memstore,
687 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
688 reftimestamp);
689 kgsl_sharedmem_writel(&device->memstore,
690 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
691 enable_ts);
692 }
693 /* Make sure all writes are posted before the GPU reads them */
694 wmb();
695 /* Mark the invalid context so no more commands are accepted from
696 * that context */
697
698 drawctxt = (struct adreno_context *) bad_context;
699
700 KGSL_CTXT_ERR(device,
701 "Context that caused a GPU hang: %x\n", bad_context);
702
703 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
704
705 /* Restore valid commands in ringbuffer */
706 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
707 rb->timestamp = timestamp;
708done:
709 vfree(rb_buffer);
710 return ret;
711}
712
713static int
714adreno_dump_and_recover(struct kgsl_device *device)
715{
716 static int recovery;
717 int result = -ETIMEDOUT;
718
719 if (device->state == KGSL_STATE_HUNG)
720 goto done;
721 if (device->state == KGSL_STATE_DUMP_AND_RECOVER && !recovery) {
722 mutex_unlock(&device->mutex);
723 wait_for_completion(&device->recovery_gate);
724 mutex_lock(&device->mutex);
725 if (!(device->state & KGSL_STATE_HUNG))
726 /* recovery success */
727 result = 0;
728 } else {
729 INIT_COMPLETION(device->recovery_gate);
730 /* Detected a hang - trigger an automatic dump */
731 adreno_postmortem_dump(device, 0);
732 if (!recovery) {
733 recovery = 1;
734 result = adreno_recover_hang(device);
735 if (result)
736 device->state = KGSL_STATE_HUNG;
737 recovery = 0;
738 complete_all(&device->recovery_gate);
739 } else
740 KGSL_DRV_ERR(device,
741 "Cannot recover from another hang while "
742 "recovering from a hang\n");
743 }
744done:
745 return result;
746}
747
748static int adreno_getproperty(struct kgsl_device *device,
749 enum kgsl_property_type type,
750 void *value,
751 unsigned int sizebytes)
752{
753 int status = -EINVAL;
754 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
755
756 switch (type) {
757 case KGSL_PROP_DEVICE_INFO:
758 {
759 struct kgsl_devinfo devinfo;
760
761 if (sizebytes != sizeof(devinfo)) {
762 status = -EINVAL;
763 break;
764 }
765
766 memset(&devinfo, 0, sizeof(devinfo));
767 devinfo.device_id = device->id+1;
768 devinfo.chip_id = adreno_dev->chip_id;
769 devinfo.mmu_enabled = kgsl_mmu_enabled();
770 devinfo.gpu_id = adreno_dev->gpurev;
771 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
772 gpu_base;
773 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
774 sizebytes;
775
776 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
777 0) {
778 status = -EFAULT;
779 break;
780 }
781 status = 0;
782 }
783 break;
784 case KGSL_PROP_DEVICE_SHADOW:
785 {
786 struct kgsl_shadowprop shadowprop;
787
788 if (sizebytes != sizeof(shadowprop)) {
789 status = -EINVAL;
790 break;
791 }
792 memset(&shadowprop, 0, sizeof(shadowprop));
793 if (device->memstore.hostptr) {
794 /*NOTE: with mmu enabled, gpuaddr doesn't mean
795 * anything to mmap().
796 */
797 shadowprop.gpuaddr = device->memstore.physaddr;
798 shadowprop.size = device->memstore.size;
799 /* GSL needs this to be set, even if it
800 appears to be meaningless */
801 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
802 }
803 if (copy_to_user(value, &shadowprop,
804 sizeof(shadowprop))) {
805 status = -EFAULT;
806 break;
807 }
808 status = 0;
809 }
810 break;
811 case KGSL_PROP_MMU_ENABLE:
812 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600813 int mmu_prop = kgsl_mmu_enabled();
814
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 if (sizebytes != sizeof(int)) {
816 status = -EINVAL;
817 break;
818 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600819 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 status = -EFAULT;
821 break;
822 }
823 status = 0;
824 }
825 break;
826 case KGSL_PROP_INTERRUPT_WAITS:
827 {
828 int int_waits = 1;
829 if (sizebytes != sizeof(int)) {
830 status = -EINVAL;
831 break;
832 }
833 if (copy_to_user(value, &int_waits, sizeof(int))) {
834 status = -EFAULT;
835 break;
836 }
837 status = 0;
838 }
839 break;
840 default:
841 status = -EINVAL;
842 }
843
844 return status;
845}
846
Lynus Vaz06a9a902011-10-04 19:25:33 +0530847static inline void adreno_poke(struct kgsl_device *device)
848{
849 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
850 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
851}
852
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853/* Caller must hold the device mutex. */
854int adreno_idle(struct kgsl_device *device, unsigned int timeout)
855{
856 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
857 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
858 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530859 unsigned long wait_timeout =
860 msecs_to_jiffies(adreno_dev->wait_timeout);
861 unsigned long wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862
863 kgsl_cffdump_regpoll(device->id, REG_RBBM_STATUS << 2,
864 0x00000000, 0x80000000);
865 /* first, wait until the CP has consumed all the commands in
866 * the ring buffer
867 */
868retry:
869 if (rb->flags & KGSL_FLAGS_STARTED) {
870 do {
Lynus Vaz06a9a902011-10-04 19:25:33 +0530871 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700872 GSL_RB_GET_READPTR(rb, &rb->rptr);
873 if (time_after(jiffies, wait_time)) {
874 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
875 rb->rptr, rb->wptr);
876 goto err;
877 }
878 } while (rb->rptr != rb->wptr);
879 }
880
881 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530882 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883 while (time_before(jiffies, wait_time)) {
884 adreno_regread(device, REG_RBBM_STATUS, &rbbm_status);
885 if (rbbm_status == 0x110)
886 return 0;
887 }
888
889err:
890 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
891 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530892 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 goto retry;
894 }
895 return -ETIMEDOUT;
896}
897
898static unsigned int adreno_isidle(struct kgsl_device *device)
899{
900 int status = false;
901 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
902 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
903 unsigned int rbbm_status;
904
905 if (rb->flags & KGSL_FLAGS_STARTED) {
906 /* Is the ring buffer is empty? */
907 GSL_RB_GET_READPTR(rb, &rb->rptr);
908 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
909 /* Is the core idle? */
910 adreno_regread(device, REG_RBBM_STATUS,
911 &rbbm_status);
912 if (rbbm_status == 0x110)
913 status = true;
914 }
915 } else {
916 KGSL_DRV_ERR(device, "ringbuffer not started\n");
917 BUG();
918 }
919 return status;
920}
921
922/* Caller must hold the device mutex. */
923static int adreno_suspend_context(struct kgsl_device *device)
924{
925 int status = 0;
926 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
927
928 /* switch to NULL ctxt */
929 if (adreno_dev->drawctxt_active != NULL) {
930 adreno_drawctxt_switch(adreno_dev, NULL, 0);
931 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
932 }
933
934 return status;
935}
936
937uint8_t *kgsl_sharedmem_convertaddr(struct kgsl_device *device,
938 unsigned int pt_base, unsigned int gpuaddr, unsigned int *size)
939{
940 uint8_t *result = NULL;
941 struct kgsl_mem_entry *entry;
942 struct kgsl_process_private *priv;
943 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
944 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
945
946 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr)) {
947 return kgsl_gpuaddr_to_vaddr(&ringbuffer->buffer_desc,
948 gpuaddr, size);
949 }
950
951 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr)) {
952 return kgsl_gpuaddr_to_vaddr(&ringbuffer->memptrs_desc,
953 gpuaddr, size);
954 }
955
956 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr)) {
957 return kgsl_gpuaddr_to_vaddr(&device->memstore,
958 gpuaddr, size);
959 }
960
961 mutex_lock(&kgsl_driver.process_mutex);
962 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600963 if (!kgsl_mmu_pt_equal(priv->pagetable, pt_base))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700964 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 spin_lock(&priv->mem_lock);
966 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
967 sizeof(unsigned int));
968 if (entry) {
969 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
970 gpuaddr, size);
971 spin_unlock(&priv->mem_lock);
972 mutex_unlock(&kgsl_driver.process_mutex);
973 return result;
974 }
975 spin_unlock(&priv->mem_lock);
976 }
977 mutex_unlock(&kgsl_driver.process_mutex);
978
979 BUG_ON(!mutex_is_locked(&device->mutex));
980 list_for_each_entry(entry, &device->memqueue, list) {
981 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr)) {
982 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
983 gpuaddr, size);
984 break;
985 }
986
987 }
988 return result;
989}
990
991void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
992 unsigned int *value)
993{
994 unsigned int *reg;
995 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
996 reg = (unsigned int *)(device->regspace.mmio_virt_base
997 + (offsetwords << 2));
998
999 if (!in_interrupt())
1000 kgsl_pre_hwaccess(device);
1001
1002 /*ensure this read finishes before the next one.
1003 * i.e. act like normal readl() */
1004 *value = __raw_readl(reg);
1005 rmb();
1006}
1007
1008void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1009 unsigned int value)
1010{
1011 unsigned int *reg;
1012
1013 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1014
1015 if (!in_interrupt())
1016 kgsl_pre_hwaccess(device);
1017
1018 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1019 reg = (unsigned int *)(device->regspace.mmio_virt_base
1020 + (offsetwords << 2));
1021
1022 /*ensure previous writes post before this one,
1023 * i.e. act like normal writel() */
1024 wmb();
1025 __raw_writel(value, reg);
1026}
1027
1028static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1029 unsigned int timestamp)
1030{
1031 int status;
1032 unsigned int ref_ts, enableflag;
1033
1034 status = kgsl_check_timestamp(device, timestamp);
1035 if (!status) {
1036 mutex_lock(&device->mutex);
1037 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1038 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1039 mb();
1040
1041 if (enableflag) {
1042 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1043 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1044 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001045 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046 kgsl_sharedmem_writel(&device->memstore,
1047 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1048 timestamp);
1049 wmb();
1050 }
1051 } else {
1052 unsigned int cmds[2];
1053 kgsl_sharedmem_writel(&device->memstore,
1054 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1055 timestamp);
1056 enableflag = 1;
1057 kgsl_sharedmem_writel(&device->memstore,
1058 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1059 enableflag);
1060 wmb();
1061 /* submit a dummy packet so that even if all
1062 * commands upto timestamp get executed we will still
1063 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001064 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001065 cmds[1] = 0;
1066 adreno_ringbuffer_issuecmds(device, 0, &cmds[0], 2);
1067 }
1068 mutex_unlock(&device->mutex);
1069 }
1070
1071 return status;
1072}
1073
1074/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001075 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076 placing a process in wait q. For conditional interrupts we expect the
1077 process to already be in its wait q when its exit condition checking
1078 function is called.
1079*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001080#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001081({ \
1082 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001083 if (io) \
1084 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1085 else \
1086 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087 __ret; \
1088})
1089
1090/* MUST be called with the device mutex held */
1091static int adreno_waittimestamp(struct kgsl_device *device,
1092 unsigned int timestamp,
1093 unsigned int msecs)
1094{
1095 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001096 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001097 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001099 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301100 int retries;
1101 unsigned int msecs_first;
1102 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001103
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301104 /* Don't wait forever, set a max value for now */
1105 if (msecs == -1)
1106 msecs = adreno_dev->wait_timeout;
1107
Jordan Crousee6239dd2011-11-17 13:39:21 -07001108 if (timestamp_cmp(timestamp, adreno_dev->ringbuffer.timestamp) > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1110 "rb->timestamp: %x\n",
1111 timestamp, adreno_dev->ringbuffer.timestamp);
1112 status = -EINVAL;
1113 goto done;
1114 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001115
Lynus Vaz06a9a902011-10-04 19:25:33 +05301116 /* Keep the first timeout as 100msecs before rewriting
1117 * the WPTR. Less visible impact if the WPTR has not
1118 * been updated properly.
1119 */
1120 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1121 msecs_part = (msecs - msecs_first + 3) / 4;
1122 for (retries = 0; retries < 5; retries++) {
1123 if (!kgsl_check_timestamp(device, timestamp)) {
1124 adreno_poke(device);
1125 io_cnt = (io_cnt + 1) % 100;
1126 if (io_cnt <
1127 pwr->pwrlevels[pwr->active_pwrlevel].
1128 io_fraction)
1129 io = 0;
1130 mutex_unlock(&device->mutex);
1131 /* We need to make sure that the process is
1132 * placed in wait-q before its condition is called
1133 */
1134 status = kgsl_wait_event_interruptible_timeout(
1135 device->wait_queue,
1136 kgsl_check_interrupt_timestamp(device,
1137 timestamp),
1138 msecs_to_jiffies(retries ?
1139 msecs_part : msecs_first), io);
1140 mutex_lock(&device->mutex);
1141
1142 if (status > 0) {
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001143 /*completed before the wait finished */
Lynus Vaz06a9a902011-10-04 19:25:33 +05301144 status = 0;
1145 goto done;
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001146 } else if (status < 0) {
1147 /*an error occurred*/
1148 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149 }
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001150 /*this wait timed out*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151 }
1152 }
Lynus Vaz06a9a902011-10-04 19:25:33 +05301153 if (!kgsl_check_timestamp(device, timestamp)) {
1154 status = -ETIMEDOUT;
1155 KGSL_DRV_ERR(device,
1156 "Device hang detected while waiting "
1157 "for timestamp: %x, last "
1158 "submitted(rb->timestamp): %x, wptr: "
1159 "%x\n", timestamp,
1160 adreno_dev->ringbuffer.timestamp,
1161 adreno_dev->ringbuffer.wptr);
1162 if (!adreno_dump_and_recover(device)) {
1163 /* wait for idle after recovery as the
1164 * timestamp that this process wanted
1165 * to wait on may be invalid */
1166 if (!adreno_idle(device,
1167 KGSL_TIMEOUT_DEFAULT))
1168 status = 0;
1169 }
1170 } else {
1171 status = 0;
1172 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173
1174done:
1175 return (int)status;
1176}
1177
1178static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1179 enum kgsl_timestamp_type type)
1180{
1181 unsigned int timestamp = 0;
1182
1183 if (type == KGSL_TIMESTAMP_CONSUMED)
1184 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1185 else if (type == KGSL_TIMESTAMP_RETIRED)
1186 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1187 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1188 rmb();
1189
1190 return timestamp;
1191}
1192
1193static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1194 unsigned int cmd, void *data)
1195{
1196 int result = 0;
1197 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1198 struct kgsl_context *context;
1199
1200 switch (cmd) {
1201 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1202 binbase = data;
1203
1204 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1205 if (context) {
1206 adreno_drawctxt_set_bin_base_offset(
1207 dev_priv->device, context, binbase->offset);
1208 } else {
1209 result = -EINVAL;
1210 KGSL_DRV_ERR(dev_priv->device,
1211 "invalid drawctxt drawctxt_id %d "
1212 "device_id=%d\n",
1213 binbase->drawctxt_id, dev_priv->device->id);
1214 }
1215 break;
1216
1217 default:
1218 KGSL_DRV_INFO(dev_priv->device,
1219 "invalid ioctl code %08x\n", cmd);
1220 result = -EINVAL;
1221 break;
1222 }
1223 return result;
1224
1225}
1226
1227static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1228{
1229 gpu_freq /= 1000000;
1230 return ticks / gpu_freq;
1231}
1232
1233static void adreno_power_stats(struct kgsl_device *device,
1234 struct kgsl_power_stats *stats)
1235{
1236 unsigned int reg;
1237 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
1238
1239 /* In order to calculate idle you have to have run the algorithm *
1240 * at least once to get a start time. */
1241 if (pwr->time != 0) {
1242 s64 tmp;
1243 /* Stop the performance moniter and read the current *
1244 * busy cycles. */
1245 adreno_regwrite(device,
1246 REG_CP_PERFMON_CNTL,
1247 REG_PERF_MODE_CNT |
1248 REG_PERF_STATE_FREEZE);
1249 adreno_regread(device, REG_RBBM_PERFCOUNTER1_LO, &reg);
1250 tmp = ktime_to_us(ktime_get());
1251 stats->total_time = tmp - pwr->time;
1252 pwr->time = tmp;
1253 stats->busy_time = adreno_ticks_to_us(reg, device->pwrctrl.
1254 pwrlevels[device->pwrctrl.active_pwrlevel].
1255 gpu_freq);
1256
1257 adreno_regwrite(device,
1258 REG_CP_PERFMON_CNTL,
1259 REG_PERF_MODE_CNT |
1260 REG_PERF_STATE_RESET);
1261 } else {
1262 stats->total_time = 0;
1263 stats->busy_time = 0;
1264 pwr->time = ktime_to_us(ktime_get());
1265 }
1266
1267 /* re-enable the performance moniters */
1268 adreno_regread(device, REG_RBBM_PM_OVERRIDE2, &reg);
1269 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, (reg | 0x40));
1270 adreno_regwrite(device, REG_RBBM_PERFCOUNTER1_SELECT, 0x1);
1271 adreno_regwrite(device,
1272 REG_CP_PERFMON_CNTL,
1273 REG_PERF_MODE_CNT | REG_PERF_STATE_ENABLE);
1274}
1275
1276void adreno_irqctrl(struct kgsl_device *device, int state)
1277{
Jordan Crousea78c9172011-07-11 13:14:09 -06001278 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1279 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001280}
1281
1282static const struct kgsl_functable adreno_functable = {
1283 /* Mandatory functions */
1284 .regread = adreno_regread,
1285 .regwrite = adreno_regwrite,
1286 .idle = adreno_idle,
1287 .isidle = adreno_isidle,
1288 .suspend_context = adreno_suspend_context,
1289 .start = adreno_start,
1290 .stop = adreno_stop,
1291 .getproperty = adreno_getproperty,
1292 .waittimestamp = adreno_waittimestamp,
1293 .readtimestamp = adreno_readtimestamp,
1294 .issueibcmds = adreno_ringbuffer_issueibcmds,
1295 .ioctl = adreno_ioctl,
1296 .setup_pt = adreno_setup_pt,
1297 .cleanup_pt = adreno_cleanup_pt,
1298 .power_stats = adreno_power_stats,
1299 .irqctrl = adreno_irqctrl,
1300 /* Optional functions */
1301 .setstate = adreno_setstate,
1302 .drawctxt_create = adreno_drawctxt_create,
1303 .drawctxt_destroy = adreno_drawctxt_destroy,
1304};
1305
1306static struct platform_device_id adreno_id_table[] = {
1307 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1308 { },
1309};
1310MODULE_DEVICE_TABLE(platform, adreno_id_table);
1311
1312static struct platform_driver adreno_platform_driver = {
1313 .probe = adreno_probe,
1314 .remove = __devexit_p(adreno_remove),
1315 .suspend = kgsl_suspend_driver,
1316 .resume = kgsl_resume_driver,
1317 .id_table = adreno_id_table,
1318 .driver = {
1319 .owner = THIS_MODULE,
1320 .name = DEVICE_3D_NAME,
1321 .pm = &kgsl_pm_ops,
1322 }
1323};
1324
1325static int __init kgsl_3d_init(void)
1326{
1327 return platform_driver_register(&adreno_platform_driver);
1328}
1329
1330static void __exit kgsl_3d_exit(void)
1331{
1332 platform_driver_unregister(&adreno_platform_driver);
1333}
1334
1335module_init(kgsl_3d_init);
1336module_exit(kgsl_3d_exit);
1337
1338MODULE_DESCRIPTION("3D Graphics driver");
1339MODULE_VERSION("1.2");
1340MODULE_LICENSE("GPL v2");
1341MODULE_ALIAS("platform:kgsl_3d");