blob: d7c71ab1d746b94f213a5e1e91ccede5a309ab8d [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
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600276 /*
277 * If possible, then set the state via the command stream to avoid
278 * a CPU idle. Otherwise, use the default setstate which uses register
279 * writes For CFF dump we must idle and use the registers so that it is
280 * easier to filter out the mmu accesses from the dump
281 */
282 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
284 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600285 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 *cmds++ = 0x00000000;
287
288 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600289 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600290 *cmds++ = kgsl_pt_get_base_addr(
291 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 sizedwords += 4;
293 }
294
295 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
296 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600297 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 1);
299 *cmds++ = 0x00000000;
300 sizedwords += 2;
301 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600302 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 *cmds++ = mh_mmu_invalidate;
304 sizedwords += 2;
305 }
306
307 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600308 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700309 /* HW workaround: to resolve MMU page fault interrupts
310 * caused by the VGT.It prevents the CP PFP from filling
311 * the VGT DMA request fifo too early,thereby ensuring
312 * that the VGT will not fetch vertex/bin data until
313 * after the page table base register has been updated.
314 *
315 * Two null DRAW_INDX_BIN packets are inserted right
316 * after the page table base update, followed by a
317 * wait for idle. The null packets will fill up the
318 * VGT DMA request fifo and prevent any further
319 * vertex/bin updates from occurring until the wait
320 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600321 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 *cmds++ = (0x4 << 16) |
323 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
324 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600325 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600326 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600327 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 *cmds++ = 0; /* viz query info */
329 *cmds++ = 0x0003C004; /* draw indicator */
330 *cmds++ = 0; /* bin base */
331 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600332 *cmds++ =
333 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600335 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336 *cmds++ = 0; /* viz query info */
337 *cmds++ = 0x0003C004; /* draw indicator */
338 *cmds++ = 0; /* bin base */
339 *cmds++ = 3; /* bin size */
340 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600341 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600343 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 *cmds++ = 0x00000000;
345 sizedwords += 21;
346 }
347
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600348
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700349 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600350 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351 *cmds++ = 0x7fff; /* invalidate all base pointers */
352 sizedwords += 2;
353 }
354
355 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
356 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600357 } else {
358 kgsl_mmu_device_setstate(device, flags);
359 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360}
361
362static unsigned int
363adreno_getchipid(struct kgsl_device *device)
364{
365 unsigned int chipid = 0;
366 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700367 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368
369 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
370 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
371 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
372
373 /*
374 * adreno 22x gpus are indicated by coreid 2,
375 * but REG_RBBM_PERIPHID1 always contains 0 for this field
376 */
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700377 if (cpu_is_msm8960() || cpu_is_msm8x60() || cpu_is_msm8930())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378 chipid = 2 << 24;
379 else
380 chipid = (coreid & 0xF) << 24;
381
382 chipid |= ((majorid >> 4) & 0xF) << 16;
383
384 minorid = ((revid >> 0) & 0xFF);
385
386 patchid = ((revid >> 16) & 0xFF);
387
388 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700389 /* 8960v3 returns 5 for patch release, but it should be 6 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390 if (cpu_is_qsd8x50())
391 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700392 else if (cpu_is_msm8960() &&
393 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
394 patchid = 6;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395
396 chipid |= (minorid << 8) | patchid;
397
398 return chipid;
399}
400
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401static inline bool _rev_match(unsigned int id, unsigned int entry)
402{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600403 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405
406static void
407adreno_identify_gpu(struct adreno_device *adreno_dev)
408{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600409 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410
411 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
412
413 core = (adreno_dev->chip_id >> 24) & 0xff;
414 major = (adreno_dev->chip_id >> 16) & 0xff;
415 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600416 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417
Jordan Crouse505df9c2011-07-28 08:37:59 -0600418 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
419 if (core == adreno_gpulist[i].core &&
420 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600421 _rev_match(minor, adreno_gpulist[i].minor) &&
422 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424 }
425
Jordan Crouse505df9c2011-07-28 08:37:59 -0600426 if (i == ARRAY_SIZE(adreno_gpulist)) {
427 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
428 return;
429 }
430
431 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
432 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
433 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
434 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700435 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
436 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437}
438
439static int __devinit
440adreno_probe(struct platform_device *pdev)
441{
442 struct kgsl_device *device;
443 struct adreno_device *adreno_dev;
444 int status = -EINVAL;
445
446 device = (struct kgsl_device *)pdev->id_entry->driver_data;
447 adreno_dev = ADRENO_DEVICE(device);
448 device->parentdev = &pdev->dev;
449
450 init_completion(&device->recovery_gate);
451
452 status = adreno_ringbuffer_init(device);
453 if (status != 0)
454 goto error;
455
456 status = kgsl_device_platform_probe(device, adreno_isr);
457 if (status)
458 goto error_close_rb;
459
460 adreno_debugfs_init(device);
461
462 kgsl_pwrscale_init(device);
463 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
464
465 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
466 return 0;
467
468error_close_rb:
469 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
470error:
471 device->parentdev = NULL;
472 return status;
473}
474
475static int __devexit adreno_remove(struct platform_device *pdev)
476{
477 struct kgsl_device *device;
478 struct adreno_device *adreno_dev;
479
480 device = (struct kgsl_device *)pdev->id_entry->driver_data;
481 adreno_dev = ADRENO_DEVICE(device);
482
483 kgsl_pwrscale_detach_policy(device);
484 kgsl_pwrscale_close(device);
485
486 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
487 kgsl_device_platform_remove(device);
488
489 return 0;
490}
491
492static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
493{
494 int status = -EINVAL;
495 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
496 int init_reftimestamp = 0x7fffffff;
497
498 device->state = KGSL_STATE_INIT;
499 device->requested_state = KGSL_STATE_NONE;
500
501 /* Power up the device */
502 kgsl_pwrctrl_enable(device);
503
504 /* Identify the specific GPU */
505 adreno_identify_gpu(adreno_dev);
506
Jordan Crouse505df9c2011-07-28 08:37:59 -0600507 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
508 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
509 adreno_dev->chip_id);
510 goto error_clk_off;
511 }
512
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600513 if (adreno_is_a20x(adreno_dev)) {
514 /*
515 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
516 * on older gpus
517 */
518 device->mh.mh_intf_cfg1 = 0;
519 device->mh.mh_intf_cfg2 = 0;
520 }
521
522 kgsl_mh_start(device);
523
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 if (kgsl_mmu_start(device))
525 goto error_clk_off;
526
527 /*We need to make sure all blocks are powered up and clocked before
528 *issuing a soft reset. The overrides will then be turned off (set to 0)
529 */
530 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0xfffffffe);
531 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0xffffffff);
532
533 /* Only reset CP block if all blocks have previously been reset */
534 if (!(device->flags & KGSL_FLAGS_SOFT_RESET) ||
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600535 !adreno_is_a22x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0xFFFFFFFF);
537 device->flags |= KGSL_FLAGS_SOFT_RESET;
538 } else
539 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000001);
540
541 /* The core is in an indeterminate state until the reset completes
542 * after 30ms.
543 */
544 msleep(30);
545
546 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000000);
547
548 adreno_regwrite(device, REG_RBBM_CNTL, 0x00004442);
549
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600550 if (adreno_is_a225(adreno_dev)) {
551 /* Enable large instruction store for A225 */
552 adreno_regwrite(device, REG_SQ_FLOW_CONTROL, 0x18000000);
553 }
554
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 adreno_regwrite(device, REG_SQ_VS_PROGRAM, 0x00000000);
556 adreno_regwrite(device, REG_SQ_PS_PROGRAM, 0x00000000);
557
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700558 if (cpu_is_msm8960() || cpu_is_msm8930())
Tarun Karra96a12672011-09-23 18:52:39 -0700559 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0x200);
560 else
561 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0);
562
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600563 if (!adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0);
565 else
566 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0x80);
567
Sushmita Susheelendraf3896062011-08-12 16:33:10 -0600568 kgsl_sharedmem_set(&device->memstore, 0, 0, device->memstore.size);
569
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 kgsl_sharedmem_writel(&device->memstore,
571 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
572 init_reftimestamp);
573
Ranjhith Kalisamyf81dcd02011-09-30 16:45:24 +0530574 adreno_regwrite(device, REG_RBBM_DEBUG, 0x00080000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575
576 /* Make sure interrupts are disabled */
577
578 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
579 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
580 adreno_regwrite(device, REG_SQ_INT_CNTL, 0);
581
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600582 if (adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583 adreno_dev->gmemspace.sizebytes = SZ_512K;
584 else
585 adreno_dev->gmemspace.sizebytes = SZ_256K;
586 adreno_gmeminit(adreno_dev);
587
588 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
589
590 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
591 if (status != 0)
592 goto error_irq_off;
593
594 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
595 return status;
596
597error_irq_off:
598 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600599 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600error_clk_off:
601 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 return status;
604}
605
606static int adreno_stop(struct kgsl_device *device)
607{
608 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
609
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610 adreno_dev->drawctxt_active = NULL;
611
612 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
613
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 kgsl_mmu_stop(device);
615
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600616 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
617 del_timer_sync(&device->idle_timer);
618
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619 /* Power down the device */
620 kgsl_pwrctrl_disable(device);
621
622 return 0;
623}
624
625static int
626adreno_recover_hang(struct kgsl_device *device)
627{
628 int ret;
629 unsigned int *rb_buffer;
630 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
631 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
632 unsigned int timestamp;
633 unsigned int num_rb_contents;
634 unsigned int bad_context;
635 unsigned int reftimestamp;
636 unsigned int enable_ts;
637 unsigned int soptimestamp;
638 unsigned int eoptimestamp;
639 struct adreno_context *drawctxt;
640
641 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
642 rb_buffer = vmalloc(rb->buffer_desc.size);
643 if (!rb_buffer) {
644 KGSL_MEM_ERR(device,
645 "Failed to allocate memory for recovery: %x\n",
646 rb->buffer_desc.size);
647 return -ENOMEM;
648 }
649 /* Extract valid contents from rb which can stil be executed after
650 * hang */
651 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
652 if (ret)
653 goto done;
654 timestamp = rb->timestamp;
655 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
656 kgsl_sharedmem_readl(&device->memstore, &bad_context,
657 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
658 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
659 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
660 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
661 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
662 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
663 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
664 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
665 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
666 /* Make sure memory is synchronized before restarting the GPU */
667 mb();
668 KGSL_CTXT_ERR(device,
669 "Context that caused a GPU hang: %x\n", bad_context);
670 /* restart device */
671 ret = adreno_stop(device);
672 if (ret)
673 goto done;
674 ret = adreno_start(device, true);
675 if (ret)
676 goto done;
677 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
678 /* Restore timestamp states */
679 kgsl_sharedmem_writel(&device->memstore,
680 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
681 soptimestamp);
682 kgsl_sharedmem_writel(&device->memstore,
683 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
684 eoptimestamp);
685 kgsl_sharedmem_writel(&device->memstore,
686 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
687 soptimestamp);
688 if (num_rb_contents) {
689 kgsl_sharedmem_writel(&device->memstore,
690 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
691 reftimestamp);
692 kgsl_sharedmem_writel(&device->memstore,
693 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
694 enable_ts);
695 }
696 /* Make sure all writes are posted before the GPU reads them */
697 wmb();
698 /* Mark the invalid context so no more commands are accepted from
699 * that context */
700
701 drawctxt = (struct adreno_context *) bad_context;
702
703 KGSL_CTXT_ERR(device,
704 "Context that caused a GPU hang: %x\n", bad_context);
705
706 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
707
708 /* Restore valid commands in ringbuffer */
709 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
710 rb->timestamp = timestamp;
711done:
712 vfree(rb_buffer);
713 return ret;
714}
715
716static int
717adreno_dump_and_recover(struct kgsl_device *device)
718{
719 static int recovery;
720 int result = -ETIMEDOUT;
721
722 if (device->state == KGSL_STATE_HUNG)
723 goto done;
724 if (device->state == KGSL_STATE_DUMP_AND_RECOVER && !recovery) {
725 mutex_unlock(&device->mutex);
726 wait_for_completion(&device->recovery_gate);
727 mutex_lock(&device->mutex);
728 if (!(device->state & KGSL_STATE_HUNG))
729 /* recovery success */
730 result = 0;
731 } else {
732 INIT_COMPLETION(device->recovery_gate);
733 /* Detected a hang - trigger an automatic dump */
734 adreno_postmortem_dump(device, 0);
735 if (!recovery) {
736 recovery = 1;
737 result = adreno_recover_hang(device);
738 if (result)
739 device->state = KGSL_STATE_HUNG;
740 recovery = 0;
741 complete_all(&device->recovery_gate);
742 } else
743 KGSL_DRV_ERR(device,
744 "Cannot recover from another hang while "
745 "recovering from a hang\n");
746 }
747done:
748 return result;
749}
750
751static int adreno_getproperty(struct kgsl_device *device,
752 enum kgsl_property_type type,
753 void *value,
754 unsigned int sizebytes)
755{
756 int status = -EINVAL;
757 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
758
759 switch (type) {
760 case KGSL_PROP_DEVICE_INFO:
761 {
762 struct kgsl_devinfo devinfo;
763
764 if (sizebytes != sizeof(devinfo)) {
765 status = -EINVAL;
766 break;
767 }
768
769 memset(&devinfo, 0, sizeof(devinfo));
770 devinfo.device_id = device->id+1;
771 devinfo.chip_id = adreno_dev->chip_id;
772 devinfo.mmu_enabled = kgsl_mmu_enabled();
773 devinfo.gpu_id = adreno_dev->gpurev;
774 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
775 gpu_base;
776 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
777 sizebytes;
778
779 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
780 0) {
781 status = -EFAULT;
782 break;
783 }
784 status = 0;
785 }
786 break;
787 case KGSL_PROP_DEVICE_SHADOW:
788 {
789 struct kgsl_shadowprop shadowprop;
790
791 if (sizebytes != sizeof(shadowprop)) {
792 status = -EINVAL;
793 break;
794 }
795 memset(&shadowprop, 0, sizeof(shadowprop));
796 if (device->memstore.hostptr) {
797 /*NOTE: with mmu enabled, gpuaddr doesn't mean
798 * anything to mmap().
799 */
800 shadowprop.gpuaddr = device->memstore.physaddr;
801 shadowprop.size = device->memstore.size;
802 /* GSL needs this to be set, even if it
803 appears to be meaningless */
804 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
805 }
806 if (copy_to_user(value, &shadowprop,
807 sizeof(shadowprop))) {
808 status = -EFAULT;
809 break;
810 }
811 status = 0;
812 }
813 break;
814 case KGSL_PROP_MMU_ENABLE:
815 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600816 int mmu_prop = kgsl_mmu_enabled();
817
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818 if (sizebytes != sizeof(int)) {
819 status = -EINVAL;
820 break;
821 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600822 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 status = -EFAULT;
824 break;
825 }
826 status = 0;
827 }
828 break;
829 case KGSL_PROP_INTERRUPT_WAITS:
830 {
831 int int_waits = 1;
832 if (sizebytes != sizeof(int)) {
833 status = -EINVAL;
834 break;
835 }
836 if (copy_to_user(value, &int_waits, sizeof(int))) {
837 status = -EFAULT;
838 break;
839 }
840 status = 0;
841 }
842 break;
843 default:
844 status = -EINVAL;
845 }
846
847 return status;
848}
849
Lynus Vaz06a9a902011-10-04 19:25:33 +0530850static inline void adreno_poke(struct kgsl_device *device)
851{
852 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
853 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
854}
855
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856/* Caller must hold the device mutex. */
857int adreno_idle(struct kgsl_device *device, unsigned int timeout)
858{
859 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
860 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
861 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530862 unsigned long wait_timeout =
863 msecs_to_jiffies(adreno_dev->wait_timeout);
864 unsigned long wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865
866 kgsl_cffdump_regpoll(device->id, REG_RBBM_STATUS << 2,
867 0x00000000, 0x80000000);
868 /* first, wait until the CP has consumed all the commands in
869 * the ring buffer
870 */
871retry:
872 if (rb->flags & KGSL_FLAGS_STARTED) {
873 do {
Lynus Vaz06a9a902011-10-04 19:25:33 +0530874 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875 GSL_RB_GET_READPTR(rb, &rb->rptr);
876 if (time_after(jiffies, wait_time)) {
877 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
878 rb->rptr, rb->wptr);
879 goto err;
880 }
881 } while (rb->rptr != rb->wptr);
882 }
883
884 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530885 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700886 while (time_before(jiffies, wait_time)) {
887 adreno_regread(device, REG_RBBM_STATUS, &rbbm_status);
888 if (rbbm_status == 0x110)
889 return 0;
890 }
891
892err:
893 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
894 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530895 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896 goto retry;
897 }
898 return -ETIMEDOUT;
899}
900
901static unsigned int adreno_isidle(struct kgsl_device *device)
902{
903 int status = false;
904 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
905 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
906 unsigned int rbbm_status;
907
908 if (rb->flags & KGSL_FLAGS_STARTED) {
909 /* Is the ring buffer is empty? */
910 GSL_RB_GET_READPTR(rb, &rb->rptr);
911 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
912 /* Is the core idle? */
913 adreno_regread(device, REG_RBBM_STATUS,
914 &rbbm_status);
915 if (rbbm_status == 0x110)
916 status = true;
917 }
918 } else {
919 KGSL_DRV_ERR(device, "ringbuffer not started\n");
920 BUG();
921 }
922 return status;
923}
924
925/* Caller must hold the device mutex. */
926static int adreno_suspend_context(struct kgsl_device *device)
927{
928 int status = 0;
929 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
930
931 /* switch to NULL ctxt */
932 if (adreno_dev->drawctxt_active != NULL) {
933 adreno_drawctxt_switch(adreno_dev, NULL, 0);
934 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
935 }
936
937 return status;
938}
939
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700940const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
941 unsigned int pt_base,
942 unsigned int gpuaddr,
943 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944{
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700945 struct kgsl_memdesc *result = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 struct kgsl_mem_entry *entry;
947 struct kgsl_process_private *priv;
948 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
949 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700950 struct kgsl_context *context;
951 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700953 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
954 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700955
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700956 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
957 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700958
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700959 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
960 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961
962 mutex_lock(&kgsl_driver.process_mutex);
963 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600964 if (!kgsl_mmu_pt_equal(priv->pagetable, pt_base))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 spin_lock(&priv->mem_lock);
967 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
968 sizeof(unsigned int));
969 if (entry) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700970 result = &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971 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) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700981 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) {
982 result = &entry->memdesc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700983 return result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 }
985
986 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700987
988 while (1) {
989 struct adreno_context *adreno_context = NULL;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700990 context = idr_get_next(&device->context_idr, &next);
991 if (context == NULL)
992 break;
993
994 adreno_context = (struct adreno_context *)context->devctxt;
995
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700996 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
997 struct kgsl_memdesc *desc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700998
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700999 desc = &adreno_context->gpustate;
1000 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1001 result = desc;
1002 return result;
1003 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001004
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001005 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1006 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1007 result = desc;
1008 return result;
1009 }
1010 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001011 next = next + 1;
1012 }
1013
1014 return NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001015
1016}
1017
1018uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1019 unsigned int gpuaddr, unsigned int size)
1020{
1021 const struct kgsl_memdesc *memdesc;
1022
1023 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1024
1025 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026}
1027
1028void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1029 unsigned int *value)
1030{
1031 unsigned int *reg;
1032 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1033 reg = (unsigned int *)(device->regspace.mmio_virt_base
1034 + (offsetwords << 2));
1035
1036 if (!in_interrupt())
1037 kgsl_pre_hwaccess(device);
1038
1039 /*ensure this read finishes before the next one.
1040 * i.e. act like normal readl() */
1041 *value = __raw_readl(reg);
1042 rmb();
1043}
1044
1045void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1046 unsigned int value)
1047{
1048 unsigned int *reg;
1049
1050 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1051
1052 if (!in_interrupt())
1053 kgsl_pre_hwaccess(device);
1054
1055 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1056 reg = (unsigned int *)(device->regspace.mmio_virt_base
1057 + (offsetwords << 2));
1058
1059 /*ensure previous writes post before this one,
1060 * i.e. act like normal writel() */
1061 wmb();
1062 __raw_writel(value, reg);
1063}
1064
1065static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1066 unsigned int timestamp)
1067{
1068 int status;
1069 unsigned int ref_ts, enableflag;
1070
1071 status = kgsl_check_timestamp(device, timestamp);
1072 if (!status) {
1073 mutex_lock(&device->mutex);
1074 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1075 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1076 mb();
1077
1078 if (enableflag) {
1079 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1080 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1081 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001082 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083 kgsl_sharedmem_writel(&device->memstore,
1084 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1085 timestamp);
1086 wmb();
1087 }
1088 } else {
1089 unsigned int cmds[2];
1090 kgsl_sharedmem_writel(&device->memstore,
1091 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1092 timestamp);
1093 enableflag = 1;
1094 kgsl_sharedmem_writel(&device->memstore,
1095 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1096 enableflag);
1097 wmb();
1098 /* submit a dummy packet so that even if all
1099 * commands upto timestamp get executed we will still
1100 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001101 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102 cmds[1] = 0;
1103 adreno_ringbuffer_issuecmds(device, 0, &cmds[0], 2);
1104 }
1105 mutex_unlock(&device->mutex);
1106 }
1107
1108 return status;
1109}
1110
1111/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001112 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113 placing a process in wait q. For conditional interrupts we expect the
1114 process to already be in its wait q when its exit condition checking
1115 function is called.
1116*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001117#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118({ \
1119 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001120 if (io) \
1121 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1122 else \
1123 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001124 __ret; \
1125})
1126
1127/* MUST be called with the device mutex held */
1128static int adreno_waittimestamp(struct kgsl_device *device,
1129 unsigned int timestamp,
1130 unsigned int msecs)
1131{
1132 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001133 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001134 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001136 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301137 int retries;
1138 unsigned int msecs_first;
1139 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301141 /* Don't wait forever, set a max value for now */
1142 if (msecs == -1)
1143 msecs = adreno_dev->wait_timeout;
1144
Jordan Crousee6239dd2011-11-17 13:39:21 -07001145 if (timestamp_cmp(timestamp, adreno_dev->ringbuffer.timestamp) > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001146 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1147 "rb->timestamp: %x\n",
1148 timestamp, adreno_dev->ringbuffer.timestamp);
1149 status = -EINVAL;
1150 goto done;
1151 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152
Lynus Vaz06a9a902011-10-04 19:25:33 +05301153 /* Keep the first timeout as 100msecs before rewriting
1154 * the WPTR. Less visible impact if the WPTR has not
1155 * been updated properly.
1156 */
1157 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1158 msecs_part = (msecs - msecs_first + 3) / 4;
1159 for (retries = 0; retries < 5; retries++) {
1160 if (!kgsl_check_timestamp(device, timestamp)) {
1161 adreno_poke(device);
1162 io_cnt = (io_cnt + 1) % 100;
1163 if (io_cnt <
1164 pwr->pwrlevels[pwr->active_pwrlevel].
1165 io_fraction)
1166 io = 0;
1167 mutex_unlock(&device->mutex);
1168 /* We need to make sure that the process is
1169 * placed in wait-q before its condition is called
1170 */
1171 status = kgsl_wait_event_interruptible_timeout(
1172 device->wait_queue,
1173 kgsl_check_interrupt_timestamp(device,
1174 timestamp),
1175 msecs_to_jiffies(retries ?
1176 msecs_part : msecs_first), io);
1177 mutex_lock(&device->mutex);
1178
1179 if (status > 0) {
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001180 /*completed before the wait finished */
Lynus Vaz06a9a902011-10-04 19:25:33 +05301181 status = 0;
1182 goto done;
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001183 } else if (status < 0) {
1184 /*an error occurred*/
1185 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 }
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001187 /*this wait timed out*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001188 }
1189 }
Lynus Vaz06a9a902011-10-04 19:25:33 +05301190 if (!kgsl_check_timestamp(device, timestamp)) {
1191 status = -ETIMEDOUT;
1192 KGSL_DRV_ERR(device,
1193 "Device hang detected while waiting "
1194 "for timestamp: %x, last "
1195 "submitted(rb->timestamp): %x, wptr: "
1196 "%x\n", timestamp,
1197 adreno_dev->ringbuffer.timestamp,
1198 adreno_dev->ringbuffer.wptr);
1199 if (!adreno_dump_and_recover(device)) {
1200 /* wait for idle after recovery as the
1201 * timestamp that this process wanted
1202 * to wait on may be invalid */
1203 if (!adreno_idle(device,
1204 KGSL_TIMEOUT_DEFAULT))
1205 status = 0;
1206 }
1207 } else {
1208 status = 0;
1209 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210
1211done:
1212 return (int)status;
1213}
1214
1215static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1216 enum kgsl_timestamp_type type)
1217{
1218 unsigned int timestamp = 0;
1219
1220 if (type == KGSL_TIMESTAMP_CONSUMED)
1221 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1222 else if (type == KGSL_TIMESTAMP_RETIRED)
1223 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1224 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1225 rmb();
1226
1227 return timestamp;
1228}
1229
1230static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1231 unsigned int cmd, void *data)
1232{
1233 int result = 0;
1234 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1235 struct kgsl_context *context;
1236
1237 switch (cmd) {
1238 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1239 binbase = data;
1240
1241 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1242 if (context) {
1243 adreno_drawctxt_set_bin_base_offset(
1244 dev_priv->device, context, binbase->offset);
1245 } else {
1246 result = -EINVAL;
1247 KGSL_DRV_ERR(dev_priv->device,
1248 "invalid drawctxt drawctxt_id %d "
1249 "device_id=%d\n",
1250 binbase->drawctxt_id, dev_priv->device->id);
1251 }
1252 break;
1253
1254 default:
1255 KGSL_DRV_INFO(dev_priv->device,
1256 "invalid ioctl code %08x\n", cmd);
1257 result = -EINVAL;
1258 break;
1259 }
1260 return result;
1261
1262}
1263
1264static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1265{
1266 gpu_freq /= 1000000;
1267 return ticks / gpu_freq;
1268}
1269
1270static void adreno_power_stats(struct kgsl_device *device,
1271 struct kgsl_power_stats *stats)
1272{
1273 unsigned int reg;
1274 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
1275
1276 /* In order to calculate idle you have to have run the algorithm *
1277 * at least once to get a start time. */
1278 if (pwr->time != 0) {
1279 s64 tmp;
1280 /* Stop the performance moniter and read the current *
1281 * busy cycles. */
1282 adreno_regwrite(device,
1283 REG_CP_PERFMON_CNTL,
1284 REG_PERF_MODE_CNT |
1285 REG_PERF_STATE_FREEZE);
1286 adreno_regread(device, REG_RBBM_PERFCOUNTER1_LO, &reg);
1287 tmp = ktime_to_us(ktime_get());
1288 stats->total_time = tmp - pwr->time;
1289 pwr->time = tmp;
1290 stats->busy_time = adreno_ticks_to_us(reg, device->pwrctrl.
1291 pwrlevels[device->pwrctrl.active_pwrlevel].
1292 gpu_freq);
1293
1294 adreno_regwrite(device,
1295 REG_CP_PERFMON_CNTL,
1296 REG_PERF_MODE_CNT |
1297 REG_PERF_STATE_RESET);
1298 } else {
1299 stats->total_time = 0;
1300 stats->busy_time = 0;
1301 pwr->time = ktime_to_us(ktime_get());
1302 }
1303
1304 /* re-enable the performance moniters */
1305 adreno_regread(device, REG_RBBM_PM_OVERRIDE2, &reg);
1306 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, (reg | 0x40));
1307 adreno_regwrite(device, REG_RBBM_PERFCOUNTER1_SELECT, 0x1);
1308 adreno_regwrite(device,
1309 REG_CP_PERFMON_CNTL,
1310 REG_PERF_MODE_CNT | REG_PERF_STATE_ENABLE);
1311}
1312
1313void adreno_irqctrl(struct kgsl_device *device, int state)
1314{
Jordan Crousea78c9172011-07-11 13:14:09 -06001315 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1316 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317}
1318
Jordan Crousea0758f22011-12-07 11:19:22 -07001319static unsigned int adreno_gpuid(struct kgsl_device *device)
1320{
1321 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1322
1323 /* Standard KGSL gpuid format:
1324 * top word is 0x0002 for 2D or 0x0003 for 3D
1325 * Bottom word is core specific identifer
1326 */
1327
1328 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1329}
1330
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331static const struct kgsl_functable adreno_functable = {
1332 /* Mandatory functions */
1333 .regread = adreno_regread,
1334 .regwrite = adreno_regwrite,
1335 .idle = adreno_idle,
1336 .isidle = adreno_isidle,
1337 .suspend_context = adreno_suspend_context,
1338 .start = adreno_start,
1339 .stop = adreno_stop,
1340 .getproperty = adreno_getproperty,
1341 .waittimestamp = adreno_waittimestamp,
1342 .readtimestamp = adreno_readtimestamp,
1343 .issueibcmds = adreno_ringbuffer_issueibcmds,
1344 .ioctl = adreno_ioctl,
1345 .setup_pt = adreno_setup_pt,
1346 .cleanup_pt = adreno_cleanup_pt,
1347 .power_stats = adreno_power_stats,
1348 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001349 .gpuid = adreno_gpuid,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350 /* Optional functions */
1351 .setstate = adreno_setstate,
1352 .drawctxt_create = adreno_drawctxt_create,
1353 .drawctxt_destroy = adreno_drawctxt_destroy,
1354};
1355
1356static struct platform_device_id adreno_id_table[] = {
1357 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1358 { },
1359};
1360MODULE_DEVICE_TABLE(platform, adreno_id_table);
1361
1362static struct platform_driver adreno_platform_driver = {
1363 .probe = adreno_probe,
1364 .remove = __devexit_p(adreno_remove),
1365 .suspend = kgsl_suspend_driver,
1366 .resume = kgsl_resume_driver,
1367 .id_table = adreno_id_table,
1368 .driver = {
1369 .owner = THIS_MODULE,
1370 .name = DEVICE_3D_NAME,
1371 .pm = &kgsl_pm_ops,
1372 }
1373};
1374
1375static int __init kgsl_3d_init(void)
1376{
1377 return platform_driver_register(&adreno_platform_driver);
1378}
1379
1380static void __exit kgsl_3d_exit(void)
1381{
1382 platform_driver_unregister(&adreno_platform_driver);
1383}
1384
1385module_init(kgsl_3d_init);
1386module_exit(kgsl_3d_exit);
1387
1388MODULE_DESCRIPTION("3D Graphics driver");
1389MODULE_VERSION("1.2");
1390MODULE_LICENSE("GPL v2");
1391MODULE_ALIAS("platform:kgsl_3d");