blob: b13190d7f4f15aa0009aad262b9c7fcda02111bd [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;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700132 unsigned int istore_size;
133 unsigned int pix_shader_start;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600134} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600135 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700136 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
137 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600138 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700139 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
140 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600141 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700142 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
143 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600144 /*
145 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
146 * a hardware problem.
147 */
148 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700149 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
150 1536, 768 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600151 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700152 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
153 1536, 768 },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600154};
155
Jordan Crouse9f739212011-07-28 08:37:57 -0600156static void adreno_gmeminit(struct adreno_device *adreno_dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157{
158 struct kgsl_device *device = &adreno_dev->dev;
159 union reg_rb_edram_info rb_edram_info;
160 unsigned int gmem_size;
161 unsigned int edram_value = 0;
162
163 /* make sure edram range is aligned to size */
164 BUG_ON(adreno_dev->gmemspace.gpu_base &
165 (adreno_dev->gmemspace.sizebytes - 1));
166
167 /* get edram_size value equivalent */
168 gmem_size = (adreno_dev->gmemspace.sizebytes >> 14);
169 while (gmem_size >>= 1)
170 edram_value++;
171
172 rb_edram_info.val = 0;
173
174 rb_edram_info.f.edram_size = edram_value;
Jordan Crouse9f739212011-07-28 08:37:57 -0600175 rb_edram_info.f.edram_mapping_mode = 0; /* EDRAM_MAP_UPPER */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176
177 /* must be aligned to size */
178 rb_edram_info.f.edram_range = (adreno_dev->gmemspace.gpu_base >> 14);
179
180 adreno_regwrite(device, REG_RB_EDRAM_INFO, rb_edram_info.val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181}
182
Jordan Crouse9f739212011-07-28 08:37:57 -0600183static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184{
Jordan Crousea78c9172011-07-11 13:14:09 -0600185 irqreturn_t result;
186 struct kgsl_device *device = data;
187 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
Jordan Crousea78c9172011-07-11 13:14:09 -0600189 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
191 if (device->requested_state == KGSL_STATE_NONE) {
192 if (device->pwrctrl.nap_allowed == true) {
193 device->requested_state = KGSL_STATE_NAP;
194 queue_work(device->work_queue, &device->idle_check_ws);
195 } else if (device->pwrscale.policy != NULL) {
196 queue_work(device->work_queue, &device->idle_check_ws);
197 }
198 }
199
200 /* Reset the time-out in our idle timer */
201 mod_timer(&device->idle_timer,
202 jiffies + device->pwrctrl.interval_timeout);
203 return result;
204}
205
Jordan Crouse9f739212011-07-28 08:37:57 -0600206static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207 struct kgsl_pagetable *pagetable)
208{
209 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
210 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
211
212 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
213
214 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
215
216 kgsl_mmu_unmap(pagetable, &device->memstore);
217
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600218 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219}
220
221static int adreno_setup_pt(struct kgsl_device *device,
222 struct kgsl_pagetable *pagetable)
223{
224 int result = 0;
225 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
226 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
229 GSL_PT_PAGE_RV);
230 if (result)
231 goto error;
232
233 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
234 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
235 if (result)
236 goto unmap_buffer_desc;
237
238 result = kgsl_mmu_map_global(pagetable, &device->memstore,
239 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
240 if (result)
241 goto unmap_memptrs_desc;
242
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600243 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
245 if (result)
246 goto unmap_memstore_desc;
247
248 return result;
249
250unmap_memstore_desc:
251 kgsl_mmu_unmap(pagetable, &device->memstore);
252
253unmap_memptrs_desc:
254 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
255
256unmap_buffer_desc:
257 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
258
259error:
260 return result;
261}
262
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600263static void adreno_setstate(struct kgsl_device *device,
264 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265{
266 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
267 unsigned int link[32];
268 unsigned int *cmds = &link[0];
269 int sizedwords = 0;
270 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
271
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 /* If possible, then set the state via the command stream to avoid
273 a CPU idle. Otherwise, use the default setstate which uses register
274 writes */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 if (adreno_dev->drawctxt_active) {
276 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
277 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600278 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 *cmds++ = 0x00000000;
280
281 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600282 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600283 *cmds++ = kgsl_pt_get_base_addr(
284 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700285 sizedwords += 4;
286 }
287
288 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
289 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600290 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 1);
292 *cmds++ = 0x00000000;
293 sizedwords += 2;
294 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600295 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296 *cmds++ = mh_mmu_invalidate;
297 sizedwords += 2;
298 }
299
300 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600301 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 /* HW workaround: to resolve MMU page fault interrupts
303 * caused by the VGT.It prevents the CP PFP from filling
304 * the VGT DMA request fifo too early,thereby ensuring
305 * that the VGT will not fetch vertex/bin data until
306 * after the page table base register has been updated.
307 *
308 * Two null DRAW_INDX_BIN packets are inserted right
309 * after the page table base update, followed by a
310 * wait for idle. The null packets will fill up the
311 * VGT DMA request fifo and prevent any further
312 * vertex/bin updates from occurring until the wait
313 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600314 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315 *cmds++ = (0x4 << 16) |
316 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
317 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600318 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600319 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600320 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321 *cmds++ = 0; /* viz query info */
322 *cmds++ = 0x0003C004; /* draw indicator */
323 *cmds++ = 0; /* bin base */
324 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600325 *cmds++ =
326 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600328 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 *cmds++ = 0; /* viz query info */
330 *cmds++ = 0x0003C004; /* draw indicator */
331 *cmds++ = 0; /* bin base */
332 *cmds++ = 3; /* bin size */
333 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600334 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600336 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 *cmds++ = 0x00000000;
338 sizedwords += 21;
339 }
340
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600341
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600343 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 *cmds++ = 0x7fff; /* invalidate all base pointers */
345 sizedwords += 2;
346 }
347
348 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
349 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600350 } else {
351 kgsl_mmu_device_setstate(device, flags);
352 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353}
354
355static unsigned int
356adreno_getchipid(struct kgsl_device *device)
357{
358 unsigned int chipid = 0;
359 unsigned int coreid, majorid, minorid, patchid, revid;
360
361 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
362 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
363 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
364
365 /*
366 * adreno 22x gpus are indicated by coreid 2,
367 * but REG_RBBM_PERIPHID1 always contains 0 for this field
368 */
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700369 if (cpu_is_msm8960() || cpu_is_msm8x60() || cpu_is_msm8930())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370 chipid = 2 << 24;
371 else
372 chipid = (coreid & 0xF) << 24;
373
374 chipid |= ((majorid >> 4) & 0xF) << 16;
375
376 minorid = ((revid >> 0) & 0xFF);
377
378 patchid = ((revid >> 16) & 0xFF);
379
380 /* 8x50 returns 0 for patch release, but it should be 1 */
381 if (cpu_is_qsd8x50())
382 patchid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383
384 chipid |= (minorid << 8) | patchid;
385
386 return chipid;
387}
388
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389static inline bool _rev_match(unsigned int id, unsigned int entry)
390{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600391 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393
394static void
395adreno_identify_gpu(struct adreno_device *adreno_dev)
396{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600397 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398
399 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
400
401 core = (adreno_dev->chip_id >> 24) & 0xff;
402 major = (adreno_dev->chip_id >> 16) & 0xff;
403 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600404 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405
Jordan Crouse505df9c2011-07-28 08:37:59 -0600406 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
407 if (core == adreno_gpulist[i].core &&
408 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600409 _rev_match(minor, adreno_gpulist[i].minor) &&
410 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700412 }
413
Jordan Crouse505df9c2011-07-28 08:37:59 -0600414 if (i == ARRAY_SIZE(adreno_gpulist)) {
415 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
416 return;
417 }
418
419 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
420 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
421 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
422 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700423 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
424 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425}
426
427static int __devinit
428adreno_probe(struct platform_device *pdev)
429{
430 struct kgsl_device *device;
431 struct adreno_device *adreno_dev;
432 int status = -EINVAL;
433
434 device = (struct kgsl_device *)pdev->id_entry->driver_data;
435 adreno_dev = ADRENO_DEVICE(device);
436 device->parentdev = &pdev->dev;
437
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530438 adreno_dev->wait_timeout = 10000; /* default value in milliseconds */
439
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440 init_completion(&device->recovery_gate);
441
442 status = adreno_ringbuffer_init(device);
443 if (status != 0)
444 goto error;
445
446 status = kgsl_device_platform_probe(device, adreno_isr);
447 if (status)
448 goto error_close_rb;
449
450 adreno_debugfs_init(device);
451
452 kgsl_pwrscale_init(device);
453 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
454
455 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
456 return 0;
457
458error_close_rb:
459 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
460error:
461 device->parentdev = NULL;
462 return status;
463}
464
465static int __devexit adreno_remove(struct platform_device *pdev)
466{
467 struct kgsl_device *device;
468 struct adreno_device *adreno_dev;
469
470 device = (struct kgsl_device *)pdev->id_entry->driver_data;
471 adreno_dev = ADRENO_DEVICE(device);
472
473 kgsl_pwrscale_detach_policy(device);
474 kgsl_pwrscale_close(device);
475
476 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
477 kgsl_device_platform_remove(device);
478
479 return 0;
480}
481
482static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
483{
484 int status = -EINVAL;
485 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
486 int init_reftimestamp = 0x7fffffff;
487
488 device->state = KGSL_STATE_INIT;
489 device->requested_state = KGSL_STATE_NONE;
490
491 /* Power up the device */
492 kgsl_pwrctrl_enable(device);
493
494 /* Identify the specific GPU */
495 adreno_identify_gpu(adreno_dev);
496
Jordan Crouse505df9c2011-07-28 08:37:59 -0600497 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
498 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
499 adreno_dev->chip_id);
500 goto error_clk_off;
501 }
502
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600503 if (adreno_is_a20x(adreno_dev)) {
504 /*
505 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
506 * on older gpus
507 */
508 device->mh.mh_intf_cfg1 = 0;
509 device->mh.mh_intf_cfg2 = 0;
510 }
511
512 kgsl_mh_start(device);
513
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 if (kgsl_mmu_start(device))
515 goto error_clk_off;
516
517 /*We need to make sure all blocks are powered up and clocked before
518 *issuing a soft reset. The overrides will then be turned off (set to 0)
519 */
520 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0xfffffffe);
521 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0xffffffff);
522
523 /* Only reset CP block if all blocks have previously been reset */
524 if (!(device->flags & KGSL_FLAGS_SOFT_RESET) ||
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600525 !adreno_is_a22x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0xFFFFFFFF);
527 device->flags |= KGSL_FLAGS_SOFT_RESET;
528 } else
529 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000001);
530
531 /* The core is in an indeterminate state until the reset completes
532 * after 30ms.
533 */
534 msleep(30);
535
536 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000000);
537
538 adreno_regwrite(device, REG_RBBM_CNTL, 0x00004442);
539
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600540 if (adreno_is_a225(adreno_dev)) {
541 /* Enable large instruction store for A225 */
542 adreno_regwrite(device, REG_SQ_FLOW_CONTROL, 0x18000000);
543 }
544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 adreno_regwrite(device, REG_SQ_VS_PROGRAM, 0x00000000);
546 adreno_regwrite(device, REG_SQ_PS_PROGRAM, 0x00000000);
547
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700548 if (cpu_is_msm8960() || cpu_is_msm8930())
Tarun Karra96a12672011-09-23 18:52:39 -0700549 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0x200);
550 else
551 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0);
552
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600553 if (!adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0);
555 else
556 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0x80);
557
Sushmita Susheelendraf3896062011-08-12 16:33:10 -0600558 kgsl_sharedmem_set(&device->memstore, 0, 0, device->memstore.size);
559
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560 kgsl_sharedmem_writel(&device->memstore,
561 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
562 init_reftimestamp);
563
Ranjhith Kalisamyf81dcd02011-09-30 16:45:24 +0530564 adreno_regwrite(device, REG_RBBM_DEBUG, 0x00080000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565
566 /* Make sure interrupts are disabled */
567
568 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
569 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
570 adreno_regwrite(device, REG_SQ_INT_CNTL, 0);
571
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600572 if (adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 adreno_dev->gmemspace.sizebytes = SZ_512K;
574 else
575 adreno_dev->gmemspace.sizebytes = SZ_256K;
576 adreno_gmeminit(adreno_dev);
577
578 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
579
580 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
581 if (status != 0)
582 goto error_irq_off;
583
584 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
585 return status;
586
587error_irq_off:
588 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600589 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590error_clk_off:
591 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592
593 return status;
594}
595
596static int adreno_stop(struct kgsl_device *device)
597{
598 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 adreno_dev->drawctxt_active = NULL;
601
602 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
603
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 kgsl_mmu_stop(device);
605
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600606 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
607 del_timer_sync(&device->idle_timer);
608
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609 /* Power down the device */
610 kgsl_pwrctrl_disable(device);
611
612 return 0;
613}
614
615static int
616adreno_recover_hang(struct kgsl_device *device)
617{
618 int ret;
619 unsigned int *rb_buffer;
620 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
621 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
622 unsigned int timestamp;
623 unsigned int num_rb_contents;
624 unsigned int bad_context;
625 unsigned int reftimestamp;
626 unsigned int enable_ts;
627 unsigned int soptimestamp;
628 unsigned int eoptimestamp;
629 struct adreno_context *drawctxt;
630
631 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
632 rb_buffer = vmalloc(rb->buffer_desc.size);
633 if (!rb_buffer) {
634 KGSL_MEM_ERR(device,
635 "Failed to allocate memory for recovery: %x\n",
636 rb->buffer_desc.size);
637 return -ENOMEM;
638 }
639 /* Extract valid contents from rb which can stil be executed after
640 * hang */
641 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
642 if (ret)
643 goto done;
644 timestamp = rb->timestamp;
645 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
646 kgsl_sharedmem_readl(&device->memstore, &bad_context,
647 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
648 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
649 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
650 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
651 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
652 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
653 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
654 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
655 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
656 /* Make sure memory is synchronized before restarting the GPU */
657 mb();
658 KGSL_CTXT_ERR(device,
659 "Context that caused a GPU hang: %x\n", bad_context);
660 /* restart device */
661 ret = adreno_stop(device);
662 if (ret)
663 goto done;
664 ret = adreno_start(device, true);
665 if (ret)
666 goto done;
667 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
668 /* Restore timestamp states */
669 kgsl_sharedmem_writel(&device->memstore,
670 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
671 soptimestamp);
672 kgsl_sharedmem_writel(&device->memstore,
673 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
674 eoptimestamp);
675 kgsl_sharedmem_writel(&device->memstore,
676 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
677 soptimestamp);
678 if (num_rb_contents) {
679 kgsl_sharedmem_writel(&device->memstore,
680 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
681 reftimestamp);
682 kgsl_sharedmem_writel(&device->memstore,
683 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
684 enable_ts);
685 }
686 /* Make sure all writes are posted before the GPU reads them */
687 wmb();
688 /* Mark the invalid context so no more commands are accepted from
689 * that context */
690
691 drawctxt = (struct adreno_context *) bad_context;
692
693 KGSL_CTXT_ERR(device,
694 "Context that caused a GPU hang: %x\n", bad_context);
695
696 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
697
698 /* Restore valid commands in ringbuffer */
699 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
700 rb->timestamp = timestamp;
701done:
702 vfree(rb_buffer);
703 return ret;
704}
705
706static int
707adreno_dump_and_recover(struct kgsl_device *device)
708{
709 static int recovery;
710 int result = -ETIMEDOUT;
711
712 if (device->state == KGSL_STATE_HUNG)
713 goto done;
714 if (device->state == KGSL_STATE_DUMP_AND_RECOVER && !recovery) {
715 mutex_unlock(&device->mutex);
716 wait_for_completion(&device->recovery_gate);
717 mutex_lock(&device->mutex);
718 if (!(device->state & KGSL_STATE_HUNG))
719 /* recovery success */
720 result = 0;
721 } else {
722 INIT_COMPLETION(device->recovery_gate);
723 /* Detected a hang - trigger an automatic dump */
724 adreno_postmortem_dump(device, 0);
725 if (!recovery) {
726 recovery = 1;
727 result = adreno_recover_hang(device);
728 if (result)
729 device->state = KGSL_STATE_HUNG;
730 recovery = 0;
731 complete_all(&device->recovery_gate);
732 } else
733 KGSL_DRV_ERR(device,
734 "Cannot recover from another hang while "
735 "recovering from a hang\n");
736 }
737done:
738 return result;
739}
740
741static int adreno_getproperty(struct kgsl_device *device,
742 enum kgsl_property_type type,
743 void *value,
744 unsigned int sizebytes)
745{
746 int status = -EINVAL;
747 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
748
749 switch (type) {
750 case KGSL_PROP_DEVICE_INFO:
751 {
752 struct kgsl_devinfo devinfo;
753
754 if (sizebytes != sizeof(devinfo)) {
755 status = -EINVAL;
756 break;
757 }
758
759 memset(&devinfo, 0, sizeof(devinfo));
760 devinfo.device_id = device->id+1;
761 devinfo.chip_id = adreno_dev->chip_id;
762 devinfo.mmu_enabled = kgsl_mmu_enabled();
763 devinfo.gpu_id = adreno_dev->gpurev;
764 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
765 gpu_base;
766 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
767 sizebytes;
768
769 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
770 0) {
771 status = -EFAULT;
772 break;
773 }
774 status = 0;
775 }
776 break;
777 case KGSL_PROP_DEVICE_SHADOW:
778 {
779 struct kgsl_shadowprop shadowprop;
780
781 if (sizebytes != sizeof(shadowprop)) {
782 status = -EINVAL;
783 break;
784 }
785 memset(&shadowprop, 0, sizeof(shadowprop));
786 if (device->memstore.hostptr) {
787 /*NOTE: with mmu enabled, gpuaddr doesn't mean
788 * anything to mmap().
789 */
790 shadowprop.gpuaddr = device->memstore.physaddr;
791 shadowprop.size = device->memstore.size;
792 /* GSL needs this to be set, even if it
793 appears to be meaningless */
794 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
795 }
796 if (copy_to_user(value, &shadowprop,
797 sizeof(shadowprop))) {
798 status = -EFAULT;
799 break;
800 }
801 status = 0;
802 }
803 break;
804 case KGSL_PROP_MMU_ENABLE:
805 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600806 int mmu_prop = kgsl_mmu_enabled();
807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 if (sizebytes != sizeof(int)) {
809 status = -EINVAL;
810 break;
811 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600812 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813 status = -EFAULT;
814 break;
815 }
816 status = 0;
817 }
818 break;
819 case KGSL_PROP_INTERRUPT_WAITS:
820 {
821 int int_waits = 1;
822 if (sizebytes != sizeof(int)) {
823 status = -EINVAL;
824 break;
825 }
826 if (copy_to_user(value, &int_waits, sizeof(int))) {
827 status = -EFAULT;
828 break;
829 }
830 status = 0;
831 }
832 break;
833 default:
834 status = -EINVAL;
835 }
836
837 return status;
838}
839
840/* Caller must hold the device mutex. */
841int adreno_idle(struct kgsl_device *device, unsigned int timeout)
842{
843 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
844 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
845 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530846 unsigned long wait_timeout =
847 msecs_to_jiffies(adreno_dev->wait_timeout);
848 unsigned long wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849
850 kgsl_cffdump_regpoll(device->id, REG_RBBM_STATUS << 2,
851 0x00000000, 0x80000000);
852 /* first, wait until the CP has consumed all the commands in
853 * the ring buffer
854 */
855retry:
856 if (rb->flags & KGSL_FLAGS_STARTED) {
857 do {
858 GSL_RB_GET_READPTR(rb, &rb->rptr);
859 if (time_after(jiffies, wait_time)) {
860 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
861 rb->rptr, rb->wptr);
862 goto err;
863 }
864 } while (rb->rptr != rb->wptr);
865 }
866
867 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530868 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 while (time_before(jiffies, wait_time)) {
870 adreno_regread(device, REG_RBBM_STATUS, &rbbm_status);
871 if (rbbm_status == 0x110)
872 return 0;
873 }
874
875err:
876 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
877 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530878 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879 goto retry;
880 }
881 return -ETIMEDOUT;
882}
883
884static unsigned int adreno_isidle(struct kgsl_device *device)
885{
886 int status = false;
887 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
888 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
889 unsigned int rbbm_status;
890
891 if (rb->flags & KGSL_FLAGS_STARTED) {
892 /* Is the ring buffer is empty? */
893 GSL_RB_GET_READPTR(rb, &rb->rptr);
894 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
895 /* Is the core idle? */
896 adreno_regread(device, REG_RBBM_STATUS,
897 &rbbm_status);
898 if (rbbm_status == 0x110)
899 status = true;
900 }
901 } else {
902 KGSL_DRV_ERR(device, "ringbuffer not started\n");
903 BUG();
904 }
905 return status;
906}
907
908/* Caller must hold the device mutex. */
909static int adreno_suspend_context(struct kgsl_device *device)
910{
911 int status = 0;
912 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
913
914 /* switch to NULL ctxt */
915 if (adreno_dev->drawctxt_active != NULL) {
916 adreno_drawctxt_switch(adreno_dev, NULL, 0);
917 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
918 }
919
920 return status;
921}
922
923uint8_t *kgsl_sharedmem_convertaddr(struct kgsl_device *device,
924 unsigned int pt_base, unsigned int gpuaddr, unsigned int *size)
925{
926 uint8_t *result = NULL;
927 struct kgsl_mem_entry *entry;
928 struct kgsl_process_private *priv;
929 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
930 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
931
932 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr)) {
933 return kgsl_gpuaddr_to_vaddr(&ringbuffer->buffer_desc,
934 gpuaddr, size);
935 }
936
937 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr)) {
938 return kgsl_gpuaddr_to_vaddr(&ringbuffer->memptrs_desc,
939 gpuaddr, size);
940 }
941
942 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr)) {
943 return kgsl_gpuaddr_to_vaddr(&device->memstore,
944 gpuaddr, size);
945 }
946
947 mutex_lock(&kgsl_driver.process_mutex);
948 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600949 if (!kgsl_mmu_pt_equal(priv->pagetable, pt_base))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700951 spin_lock(&priv->mem_lock);
952 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
953 sizeof(unsigned int));
954 if (entry) {
955 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
956 gpuaddr, size);
957 spin_unlock(&priv->mem_lock);
958 mutex_unlock(&kgsl_driver.process_mutex);
959 return result;
960 }
961 spin_unlock(&priv->mem_lock);
962 }
963 mutex_unlock(&kgsl_driver.process_mutex);
964
965 BUG_ON(!mutex_is_locked(&device->mutex));
966 list_for_each_entry(entry, &device->memqueue, list) {
967 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr)) {
968 result = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
969 gpuaddr, size);
970 break;
971 }
972
973 }
974 return result;
975}
976
977void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
978 unsigned int *value)
979{
980 unsigned int *reg;
981 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
982 reg = (unsigned int *)(device->regspace.mmio_virt_base
983 + (offsetwords << 2));
984
985 if (!in_interrupt())
986 kgsl_pre_hwaccess(device);
987
988 /*ensure this read finishes before the next one.
989 * i.e. act like normal readl() */
990 *value = __raw_readl(reg);
991 rmb();
992}
993
994void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
995 unsigned int value)
996{
997 unsigned int *reg;
998
999 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1000
1001 if (!in_interrupt())
1002 kgsl_pre_hwaccess(device);
1003
1004 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1005 reg = (unsigned int *)(device->regspace.mmio_virt_base
1006 + (offsetwords << 2));
1007
1008 /*ensure previous writes post before this one,
1009 * i.e. act like normal writel() */
1010 wmb();
1011 __raw_writel(value, reg);
1012}
1013
1014static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1015 unsigned int timestamp)
1016{
1017 int status;
1018 unsigned int ref_ts, enableflag;
1019
1020 status = kgsl_check_timestamp(device, timestamp);
1021 if (!status) {
1022 mutex_lock(&device->mutex);
1023 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1024 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1025 mb();
1026
1027 if (enableflag) {
1028 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1029 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1030 mb();
1031 if (timestamp_cmp(ref_ts, timestamp)) {
1032 kgsl_sharedmem_writel(&device->memstore,
1033 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1034 timestamp);
1035 wmb();
1036 }
1037 } else {
1038 unsigned int cmds[2];
1039 kgsl_sharedmem_writel(&device->memstore,
1040 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1041 timestamp);
1042 enableflag = 1;
1043 kgsl_sharedmem_writel(&device->memstore,
1044 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1045 enableflag);
1046 wmb();
1047 /* submit a dummy packet so that even if all
1048 * commands upto timestamp get executed we will still
1049 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001050 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051 cmds[1] = 0;
1052 adreno_ringbuffer_issuecmds(device, 0, &cmds[0], 2);
1053 }
1054 mutex_unlock(&device->mutex);
1055 }
1056
1057 return status;
1058}
1059
1060/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001061 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062 placing a process in wait q. For conditional interrupts we expect the
1063 process to already be in its wait q when its exit condition checking
1064 function is called.
1065*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001066#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001067({ \
1068 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001069 if (io) \
1070 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1071 else \
1072 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073 __ret; \
1074})
1075
1076/* MUST be called with the device mutex held */
1077static int adreno_waittimestamp(struct kgsl_device *device,
1078 unsigned int timestamp,
1079 unsigned int msecs)
1080{
1081 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001082 uint io = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001084 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301086 /* Don't wait forever, set a max value for now */
1087 if (msecs == -1)
1088 msecs = adreno_dev->wait_timeout;
1089
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 if (timestamp != adreno_dev->ringbuffer.timestamp &&
1091 timestamp_cmp(timestamp,
1092 adreno_dev->ringbuffer.timestamp)) {
1093 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1094 "rb->timestamp: %x\n",
1095 timestamp, adreno_dev->ringbuffer.timestamp);
1096 status = -EINVAL;
1097 goto done;
1098 }
1099 if (!kgsl_check_timestamp(device, timestamp)) {
Lucille Sylvester02e46292011-09-21 14:59:17 -06001100 if (pwr->active_pwrlevel) {
1101 int low_pwrlevel = pwr->num_pwrlevels -
1102 KGSL_PWRLEVEL_LOW_OFFSET;
1103 if (pwr->active_pwrlevel == low_pwrlevel)
1104 io = 0;
1105 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106 mutex_unlock(&device->mutex);
1107 /* We need to make sure that the process is placed in wait-q
1108 * before its condition is called */
Lucille Sylvester02e46292011-09-21 14:59:17 -06001109 status = kgsl_wait_event_interruptible_timeout(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110 device->wait_queue,
1111 kgsl_check_interrupt_timestamp(device,
Lucille Sylvester02e46292011-09-21 14:59:17 -06001112 timestamp),
1113 msecs_to_jiffies(msecs), io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114 mutex_lock(&device->mutex);
1115
1116 if (status > 0)
1117 status = 0;
1118 else if (status == 0) {
1119 if (!kgsl_check_timestamp(device, timestamp)) {
1120 status = -ETIMEDOUT;
1121 KGSL_DRV_ERR(device,
1122 "Device hang detected while waiting "
1123 "for timestamp: %x, last "
1124 "submitted(rb->timestamp): %x, wptr: "
1125 "%x\n", timestamp,
1126 adreno_dev->ringbuffer.timestamp,
1127 adreno_dev->ringbuffer.wptr);
1128 if (!adreno_dump_and_recover(device)) {
1129 /* wait for idle after recovery as the
1130 * timestamp that this process wanted
1131 * to wait on may be invalid */
1132 if (!adreno_idle(device,
1133 KGSL_TIMEOUT_DEFAULT))
1134 status = 0;
1135 }
1136 }
1137 }
1138 }
1139
1140done:
1141 return (int)status;
1142}
1143
1144static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1145 enum kgsl_timestamp_type type)
1146{
1147 unsigned int timestamp = 0;
1148
1149 if (type == KGSL_TIMESTAMP_CONSUMED)
1150 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1151 else if (type == KGSL_TIMESTAMP_RETIRED)
1152 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1153 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1154 rmb();
1155
1156 return timestamp;
1157}
1158
1159static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1160 unsigned int cmd, void *data)
1161{
1162 int result = 0;
1163 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1164 struct kgsl_context *context;
1165
1166 switch (cmd) {
1167 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1168 binbase = data;
1169
1170 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1171 if (context) {
1172 adreno_drawctxt_set_bin_base_offset(
1173 dev_priv->device, context, binbase->offset);
1174 } else {
1175 result = -EINVAL;
1176 KGSL_DRV_ERR(dev_priv->device,
1177 "invalid drawctxt drawctxt_id %d "
1178 "device_id=%d\n",
1179 binbase->drawctxt_id, dev_priv->device->id);
1180 }
1181 break;
1182
1183 default:
1184 KGSL_DRV_INFO(dev_priv->device,
1185 "invalid ioctl code %08x\n", cmd);
1186 result = -EINVAL;
1187 break;
1188 }
1189 return result;
1190
1191}
1192
1193static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1194{
1195 gpu_freq /= 1000000;
1196 return ticks / gpu_freq;
1197}
1198
1199static void adreno_power_stats(struct kgsl_device *device,
1200 struct kgsl_power_stats *stats)
1201{
1202 unsigned int reg;
1203 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
1204
1205 /* In order to calculate idle you have to have run the algorithm *
1206 * at least once to get a start time. */
1207 if (pwr->time != 0) {
1208 s64 tmp;
1209 /* Stop the performance moniter and read the current *
1210 * busy cycles. */
1211 adreno_regwrite(device,
1212 REG_CP_PERFMON_CNTL,
1213 REG_PERF_MODE_CNT |
1214 REG_PERF_STATE_FREEZE);
1215 adreno_regread(device, REG_RBBM_PERFCOUNTER1_LO, &reg);
1216 tmp = ktime_to_us(ktime_get());
1217 stats->total_time = tmp - pwr->time;
1218 pwr->time = tmp;
1219 stats->busy_time = adreno_ticks_to_us(reg, device->pwrctrl.
1220 pwrlevels[device->pwrctrl.active_pwrlevel].
1221 gpu_freq);
1222
1223 adreno_regwrite(device,
1224 REG_CP_PERFMON_CNTL,
1225 REG_PERF_MODE_CNT |
1226 REG_PERF_STATE_RESET);
1227 } else {
1228 stats->total_time = 0;
1229 stats->busy_time = 0;
1230 pwr->time = ktime_to_us(ktime_get());
1231 }
1232
1233 /* re-enable the performance moniters */
1234 adreno_regread(device, REG_RBBM_PM_OVERRIDE2, &reg);
1235 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, (reg | 0x40));
1236 adreno_regwrite(device, REG_RBBM_PERFCOUNTER1_SELECT, 0x1);
1237 adreno_regwrite(device,
1238 REG_CP_PERFMON_CNTL,
1239 REG_PERF_MODE_CNT | REG_PERF_STATE_ENABLE);
1240}
1241
1242void adreno_irqctrl(struct kgsl_device *device, int state)
1243{
Jordan Crousea78c9172011-07-11 13:14:09 -06001244 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1245 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246}
1247
1248static const struct kgsl_functable adreno_functable = {
1249 /* Mandatory functions */
1250 .regread = adreno_regread,
1251 .regwrite = adreno_regwrite,
1252 .idle = adreno_idle,
1253 .isidle = adreno_isidle,
1254 .suspend_context = adreno_suspend_context,
1255 .start = adreno_start,
1256 .stop = adreno_stop,
1257 .getproperty = adreno_getproperty,
1258 .waittimestamp = adreno_waittimestamp,
1259 .readtimestamp = adreno_readtimestamp,
1260 .issueibcmds = adreno_ringbuffer_issueibcmds,
1261 .ioctl = adreno_ioctl,
1262 .setup_pt = adreno_setup_pt,
1263 .cleanup_pt = adreno_cleanup_pt,
1264 .power_stats = adreno_power_stats,
1265 .irqctrl = adreno_irqctrl,
1266 /* Optional functions */
1267 .setstate = adreno_setstate,
1268 .drawctxt_create = adreno_drawctxt_create,
1269 .drawctxt_destroy = adreno_drawctxt_destroy,
1270};
1271
1272static struct platform_device_id adreno_id_table[] = {
1273 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1274 { },
1275};
1276MODULE_DEVICE_TABLE(platform, adreno_id_table);
1277
1278static struct platform_driver adreno_platform_driver = {
1279 .probe = adreno_probe,
1280 .remove = __devexit_p(adreno_remove),
1281 .suspend = kgsl_suspend_driver,
1282 .resume = kgsl_resume_driver,
1283 .id_table = adreno_id_table,
1284 .driver = {
1285 .owner = THIS_MODULE,
1286 .name = DEVICE_3D_NAME,
1287 .pm = &kgsl_pm_ops,
1288 }
1289};
1290
1291static int __init kgsl_3d_init(void)
1292{
1293 return platform_driver_register(&adreno_platform_driver);
1294}
1295
1296static void __exit kgsl_3d_exit(void)
1297{
1298 platform_driver_unregister(&adreno_platform_driver);
1299}
1300
1301module_init(kgsl_3d_init);
1302module_exit(kgsl_3d_exit);
1303
1304MODULE_DESCRIPTION("3D Graphics driver");
1305MODULE_VERSION("1.2");
1306MODULE_LICENSE("GPL v2");
1307MODULE_ALIAS("platform:kgsl_3d");