blob: 69934221427a5e72a86a1d4e1ed7122ff461f082 [file] [log] [blame]
Lucille Sylvester51b764d2011-12-15 16:51:52 -07001/* Copyright (c) 2002,2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070013#include <linux/uaccess.h>
14#include <linux/vmalloc.h>
15#include <linux/ioctl.h>
16#include <linux/sched.h>
17
18#include <mach/socinfo.h>
19
20#include "kgsl.h"
21#include "kgsl_pwrscale.h"
22#include "kgsl_cffdump.h"
23#include "kgsl_sharedmem.h"
24
25#include "adreno.h"
26#include "adreno_pm4types.h"
27#include "adreno_debugfs.h"
28#include "adreno_postmortem.h"
29
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070030#include "a2xx_reg.h"
Jordan Crouseb4d31bd2012-02-01 22:11:12 -070031#include "a3xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
33#define DRIVER_VERSION_MAJOR 3
34#define DRIVER_VERSION_MINOR 1
35
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036/* Adreno MH arbiter config*/
37#define ADRENO_CFG_MHARB \
38 (0x10 \
39 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
40 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
41 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
42 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
43 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
44 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
45 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
46 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
47 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
48 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
53
54#define ADRENO_MMU_CONFIG \
55 (0x01 \
56 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
57 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
58 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
59 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
60 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
61 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
62 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
67
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068static const struct kgsl_functable adreno_functable;
69
70static struct adreno_device device_3d0 = {
71 .dev = {
72 .name = DEVICE_3D0_NAME,
73 .id = KGSL_DEVICE_3D0,
74 .ver_major = DRIVER_VERSION_MAJOR,
75 .ver_minor = DRIVER_VERSION_MINOR,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060076 .mh = {
77 .mharb = ADRENO_CFG_MHARB,
78 /* Remove 1k boundary check in z470 to avoid a GPU
79 * hang. Notice that this solution won't work if
80 * both EBI and SMI are used
81 */
82 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083 /* turn off memory protection unit by setting
84 acceptable physical address range to include
85 all pages. */
86 .mpu_base = 0x00000000,
87 .mpu_range = 0xFFFFF000,
88 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060089 .mmu = {
90 .config = ADRENO_MMU_CONFIG,
91 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092 .pwrctrl = {
93 .regulator_name = "fs_gfx3d",
94 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095 },
96 .mutex = __MUTEX_INITIALIZER(device_3d0.dev.mutex),
97 .state = KGSL_STATE_INIT,
98 .active_cnt = 0,
99 .iomemname = KGSL_3D0_REG_MEMORY,
100 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600102 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
104 .suspend = kgsl_early_suspend_driver,
105 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600107#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108 },
109 .gmemspace = {
110 .gpu_base = 0,
111 .sizebytes = SZ_256K,
112 },
113 .pfp_fw = NULL,
114 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700115 .wait_timeout = 10000, /* in milliseconds */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116};
117
Jordan Crouse95b33272011-11-11 14:50:12 -0700118
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 Crousec6b3a992012-02-04 10:23:51 -0700134 unsigned int instruction_size; /* Size of an instruction in dwords */
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,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700138 512, 384, 3},
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,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700141 512, 384, 3},
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,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700144 512, 384, 3},
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,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700151 1536, 768, 3 },
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,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700154 1536, 768, 3 },
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,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700157 1536, 768, 3 },
158 /* A3XX doesn't use the pix_shader_start */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700159 { ADRENO_REV_A320, 3, 1, ANY_ID, ANY_ID,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700160 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
161 512, 0, 2 },
162
Jordan Crouse505df9c2011-07-28 08:37:59 -0600163};
164
Jordan Crouse9f739212011-07-28 08:37:57 -0600165static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166{
Jordan Crousea78c9172011-07-11 13:14:09 -0600167 irqreturn_t result;
168 struct kgsl_device *device = data;
169 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170
Jordan Crousea78c9172011-07-11 13:14:09 -0600171 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172
173 if (device->requested_state == KGSL_STATE_NONE) {
174 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700175 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176 queue_work(device->work_queue, &device->idle_check_ws);
177 } else if (device->pwrscale.policy != NULL) {
178 queue_work(device->work_queue, &device->idle_check_ws);
179 }
180 }
181
182 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800183 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184 jiffies + device->pwrctrl.interval_timeout);
185 return result;
186}
187
Jordan Crouse9f739212011-07-28 08:37:57 -0600188static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189 struct kgsl_pagetable *pagetable)
190{
191 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
192 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
193
194 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
195
196 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
197
198 kgsl_mmu_unmap(pagetable, &device->memstore);
199
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600200 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201}
202
203static int adreno_setup_pt(struct kgsl_device *device,
204 struct kgsl_pagetable *pagetable)
205{
206 int result = 0;
207 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
208 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
209
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
211 GSL_PT_PAGE_RV);
212 if (result)
213 goto error;
214
215 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
216 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
217 if (result)
218 goto unmap_buffer_desc;
219
220 result = kgsl_mmu_map_global(pagetable, &device->memstore,
221 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
222 if (result)
223 goto unmap_memptrs_desc;
224
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600225 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
227 if (result)
228 goto unmap_memstore_desc;
229
230 return result;
231
232unmap_memstore_desc:
233 kgsl_mmu_unmap(pagetable, &device->memstore);
234
235unmap_memptrs_desc:
236 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
237
238unmap_buffer_desc:
239 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
240
241error:
242 return result;
243}
244
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600245static void adreno_setstate(struct kgsl_device *device,
246 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247{
248 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
249 unsigned int link[32];
250 unsigned int *cmds = &link[0];
251 int sizedwords = 0;
252 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
253
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600254 /*
255 * If possible, then set the state via the command stream to avoid
256 * a CPU idle. Otherwise, use the default setstate which uses register
257 * writes For CFF dump we must idle and use the registers so that it is
258 * easier to filter out the mmu accesses from the dump
259 */
260 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
262 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600263 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264 *cmds++ = 0x00000000;
265
266 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600267 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600268 *cmds++ = kgsl_pt_get_base_addr(
269 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270 sizedwords += 4;
271 }
272
273 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
274 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600275 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276 1);
277 *cmds++ = 0x00000000;
278 sizedwords += 2;
279 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600280 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 *cmds++ = mh_mmu_invalidate;
282 sizedwords += 2;
283 }
284
285 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600286 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 /* HW workaround: to resolve MMU page fault interrupts
288 * caused by the VGT.It prevents the CP PFP from filling
289 * the VGT DMA request fifo too early,thereby ensuring
290 * that the VGT will not fetch vertex/bin data until
291 * after the page table base register has been updated.
292 *
293 * Two null DRAW_INDX_BIN packets are inserted right
294 * after the page table base update, followed by a
295 * wait for idle. The null packets will fill up the
296 * VGT DMA request fifo and prevent any further
297 * vertex/bin updates from occurring until the wait
298 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600299 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 *cmds++ = (0x4 << 16) |
301 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
302 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600303 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600304 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600305 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 *cmds++ = 0; /* viz query info */
307 *cmds++ = 0x0003C004; /* draw indicator */
308 *cmds++ = 0; /* bin base */
309 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600310 *cmds++ =
311 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600313 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314 *cmds++ = 0; /* viz query info */
315 *cmds++ = 0x0003C004; /* draw indicator */
316 *cmds++ = 0; /* bin base */
317 *cmds++ = 3; /* bin size */
318 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600319 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600321 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 *cmds++ = 0x00000000;
323 sizedwords += 21;
324 }
325
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600326
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600328 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 *cmds++ = 0x7fff; /* invalidate all base pointers */
330 sizedwords += 2;
331 }
332
333 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
334 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600335 } else {
336 kgsl_mmu_device_setstate(device, flags);
337 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338}
339
340static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700341a3xx_getchipid(struct kgsl_device *device)
342{
343 unsigned int chipid = 0;
344 unsigned int coreid, majorid, minorid, patchid;
345 unsigned int version;
346
347 adreno_regread(device, A3XX_RBBM_HW_VERSION, &version);
348
349 coreid = 0x03;
350
351 /* Version might not be set - if it isn't, assume this is 320 */
352 if (version)
353 majorid = version & 0x0F;
354 else
355 majorid = 1;
356
357 minorid = (version >> 4) & 0xFFF;
358 patchid = 0;
359
360 chipid = (coreid << 24) | (majorid << 16) | (minorid << 8) | patchid;
361
362 return chipid;
363}
364
365static unsigned int
366a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367{
368 unsigned int chipid = 0;
369 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700370 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371
372 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
373 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
374 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
375
376 /*
377 * adreno 22x gpus are indicated by coreid 2,
378 * but REG_RBBM_PERIPHID1 always contains 0 for this field
379 */
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700380 if (cpu_is_msm8960() || cpu_is_msm8x60() || cpu_is_msm8930())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 chipid = 2 << 24;
382 else
383 chipid = (coreid & 0xF) << 24;
384
385 chipid |= ((majorid >> 4) & 0xF) << 16;
386
387 minorid = ((revid >> 0) & 0xFF);
388
389 patchid = ((revid >> 16) & 0xFF);
390
391 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700392 /* 8960v3 returns 5 for patch release, but it should be 6 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 if (cpu_is_qsd8x50())
394 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700395 else if (cpu_is_msm8960() &&
396 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
397 patchid = 6;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398
399 chipid |= (minorid << 8) | patchid;
400
401 return chipid;
402}
403
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700404static unsigned int
405adreno_getchipid(struct kgsl_device *device)
406{
407 if (cpu_is_apq8064())
408 return a3xx_getchipid(device);
409 else
410 return a2xx_getchipid(device);
411}
412
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413static inline bool _rev_match(unsigned int id, unsigned int entry)
414{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600415 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417
418static void
419adreno_identify_gpu(struct adreno_device *adreno_dev)
420{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600421 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
423 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
424
425 core = (adreno_dev->chip_id >> 24) & 0xff;
426 major = (adreno_dev->chip_id >> 16) & 0xff;
427 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600428 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429
Jordan Crouse505df9c2011-07-28 08:37:59 -0600430 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
431 if (core == adreno_gpulist[i].core &&
432 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600433 _rev_match(minor, adreno_gpulist[i].minor) &&
434 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436 }
437
Jordan Crouse505df9c2011-07-28 08:37:59 -0600438 if (i == ARRAY_SIZE(adreno_gpulist)) {
439 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
440 return;
441 }
442
443 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
444 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
445 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
446 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700447 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
448 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700449 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450}
451
452static int __devinit
453adreno_probe(struct platform_device *pdev)
454{
455 struct kgsl_device *device;
456 struct adreno_device *adreno_dev;
457 int status = -EINVAL;
458
459 device = (struct kgsl_device *)pdev->id_entry->driver_data;
460 adreno_dev = ADRENO_DEVICE(device);
461 device->parentdev = &pdev->dev;
462
463 init_completion(&device->recovery_gate);
464
465 status = adreno_ringbuffer_init(device);
466 if (status != 0)
467 goto error;
468
469 status = kgsl_device_platform_probe(device, adreno_isr);
470 if (status)
471 goto error_close_rb;
472
473 adreno_debugfs_init(device);
474
475 kgsl_pwrscale_init(device);
476 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
477
478 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
479 return 0;
480
481error_close_rb:
482 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
483error:
484 device->parentdev = NULL;
485 return status;
486}
487
488static int __devexit adreno_remove(struct platform_device *pdev)
489{
490 struct kgsl_device *device;
491 struct adreno_device *adreno_dev;
492
493 device = (struct kgsl_device *)pdev->id_entry->driver_data;
494 adreno_dev = ADRENO_DEVICE(device);
495
496 kgsl_pwrscale_detach_policy(device);
497 kgsl_pwrscale_close(device);
498
499 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
500 kgsl_device_platform_remove(device);
501
502 return 0;
503}
504
505static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
506{
507 int status = -EINVAL;
508 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509
Jeremy Gebben388c2972011-12-16 09:05:07 -0700510 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511
512 /* Power up the device */
513 kgsl_pwrctrl_enable(device);
514
515 /* Identify the specific GPU */
516 adreno_identify_gpu(adreno_dev);
517
Jordan Crouse505df9c2011-07-28 08:37:59 -0600518 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
519 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
520 adreno_dev->chip_id);
521 goto error_clk_off;
522 }
523
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700524 /* Set up the MMU */
525 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600526 /*
527 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
528 * on older gpus
529 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700530 if (adreno_is_a20x(adreno_dev)) {
531 device->mh.mh_intf_cfg1 = 0;
532 device->mh.mh_intf_cfg2 = 0;
533 }
534
535 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600536 }
537
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700538 status = kgsl_mmu_start(device);
539 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540 goto error_clk_off;
541
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700542 /* Start the GPU */
543 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544
545 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700546 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547
548 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700549 if (status == 0) {
550 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
551 return 0;
552 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600555 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556error_clk_off:
557 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558
559 return status;
560}
561
562static int adreno_stop(struct kgsl_device *device)
563{
564 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
565
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 adreno_dev->drawctxt_active = NULL;
567
568 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
569
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 kgsl_mmu_stop(device);
571
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700572 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530573 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600574
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575 /* Power down the device */
576 kgsl_pwrctrl_disable(device);
577
578 return 0;
579}
580
581static int
582adreno_recover_hang(struct kgsl_device *device)
583{
584 int ret;
585 unsigned int *rb_buffer;
586 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
587 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
588 unsigned int timestamp;
589 unsigned int num_rb_contents;
590 unsigned int bad_context;
591 unsigned int reftimestamp;
592 unsigned int enable_ts;
593 unsigned int soptimestamp;
594 unsigned int eoptimestamp;
595 struct adreno_context *drawctxt;
596
597 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
598 rb_buffer = vmalloc(rb->buffer_desc.size);
599 if (!rb_buffer) {
600 KGSL_MEM_ERR(device,
601 "Failed to allocate memory for recovery: %x\n",
602 rb->buffer_desc.size);
603 return -ENOMEM;
604 }
605 /* Extract valid contents from rb which can stil be executed after
606 * hang */
607 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
608 if (ret)
609 goto done;
610 timestamp = rb->timestamp;
611 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
612 kgsl_sharedmem_readl(&device->memstore, &bad_context,
613 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
614 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
615 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
616 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
617 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
618 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
619 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
620 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
621 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
622 /* Make sure memory is synchronized before restarting the GPU */
623 mb();
624 KGSL_CTXT_ERR(device,
625 "Context that caused a GPU hang: %x\n", bad_context);
626 /* restart device */
627 ret = adreno_stop(device);
628 if (ret)
629 goto done;
630 ret = adreno_start(device, true);
631 if (ret)
632 goto done;
633 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
634 /* Restore timestamp states */
635 kgsl_sharedmem_writel(&device->memstore,
636 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
637 soptimestamp);
638 kgsl_sharedmem_writel(&device->memstore,
639 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
640 eoptimestamp);
641 kgsl_sharedmem_writel(&device->memstore,
642 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
643 soptimestamp);
644 if (num_rb_contents) {
645 kgsl_sharedmem_writel(&device->memstore,
646 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
647 reftimestamp);
648 kgsl_sharedmem_writel(&device->memstore,
649 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
650 enable_ts);
651 }
652 /* Make sure all writes are posted before the GPU reads them */
653 wmb();
654 /* Mark the invalid context so no more commands are accepted from
655 * that context */
656
657 drawctxt = (struct adreno_context *) bad_context;
658
659 KGSL_CTXT_ERR(device,
660 "Context that caused a GPU hang: %x\n", bad_context);
661
662 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
663
664 /* Restore valid commands in ringbuffer */
665 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
666 rb->timestamp = timestamp;
667done:
668 vfree(rb_buffer);
669 return ret;
670}
671
672static int
673adreno_dump_and_recover(struct kgsl_device *device)
674{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 int result = -ETIMEDOUT;
676
677 if (device->state == KGSL_STATE_HUNG)
678 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700679 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680 mutex_unlock(&device->mutex);
681 wait_for_completion(&device->recovery_gate);
682 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700683 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684 result = 0;
685 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700686 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700688 /* Detected a hang */
689
690
691 /*
692 * Trigger an automatic dump of the state to
693 * the console
694 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700696
697 /*
698 * Make a GPU snapshot. For now, do it after the PM dump so we
699 * can at least be sure the PM dump will work as it always has
700 */
701 kgsl_device_snapshot(device, 1);
702
Jeremy Gebben388c2972011-12-16 09:05:07 -0700703 result = adreno_recover_hang(device);
704 if (result)
705 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
706 else
707 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
708 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700709 }
710done:
711 return result;
712}
713
714static int adreno_getproperty(struct kgsl_device *device,
715 enum kgsl_property_type type,
716 void *value,
717 unsigned int sizebytes)
718{
719 int status = -EINVAL;
720 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
721
722 switch (type) {
723 case KGSL_PROP_DEVICE_INFO:
724 {
725 struct kgsl_devinfo devinfo;
726
727 if (sizebytes != sizeof(devinfo)) {
728 status = -EINVAL;
729 break;
730 }
731
732 memset(&devinfo, 0, sizeof(devinfo));
733 devinfo.device_id = device->id+1;
734 devinfo.chip_id = adreno_dev->chip_id;
735 devinfo.mmu_enabled = kgsl_mmu_enabled();
736 devinfo.gpu_id = adreno_dev->gpurev;
737 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
738 gpu_base;
739 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
740 sizebytes;
741
742 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
743 0) {
744 status = -EFAULT;
745 break;
746 }
747 status = 0;
748 }
749 break;
750 case KGSL_PROP_DEVICE_SHADOW:
751 {
752 struct kgsl_shadowprop shadowprop;
753
754 if (sizebytes != sizeof(shadowprop)) {
755 status = -EINVAL;
756 break;
757 }
758 memset(&shadowprop, 0, sizeof(shadowprop));
759 if (device->memstore.hostptr) {
760 /*NOTE: with mmu enabled, gpuaddr doesn't mean
761 * anything to mmap().
762 */
763 shadowprop.gpuaddr = device->memstore.physaddr;
764 shadowprop.size = device->memstore.size;
765 /* GSL needs this to be set, even if it
766 appears to be meaningless */
767 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
768 }
769 if (copy_to_user(value, &shadowprop,
770 sizeof(shadowprop))) {
771 status = -EFAULT;
772 break;
773 }
774 status = 0;
775 }
776 break;
777 case KGSL_PROP_MMU_ENABLE:
778 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600779 int mmu_prop = kgsl_mmu_enabled();
780
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 if (sizebytes != sizeof(int)) {
782 status = -EINVAL;
783 break;
784 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600785 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786 status = -EFAULT;
787 break;
788 }
789 status = 0;
790 }
791 break;
792 case KGSL_PROP_INTERRUPT_WAITS:
793 {
794 int int_waits = 1;
795 if (sizebytes != sizeof(int)) {
796 status = -EINVAL;
797 break;
798 }
799 if (copy_to_user(value, &int_waits, sizeof(int))) {
800 status = -EFAULT;
801 break;
802 }
803 status = 0;
804 }
805 break;
806 default:
807 status = -EINVAL;
808 }
809
810 return status;
811}
812
Lynus Vaz06a9a902011-10-04 19:25:33 +0530813static inline void adreno_poke(struct kgsl_device *device)
814{
815 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
816 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
817}
818
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819/* Caller must hold the device mutex. */
820int adreno_idle(struct kgsl_device *device, unsigned int timeout)
821{
822 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
823 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
824 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530825 unsigned long wait_timeout =
826 msecs_to_jiffies(adreno_dev->wait_timeout);
827 unsigned long wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700829 kgsl_cffdump_regpoll(device->id,
830 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 0x00000000, 0x80000000);
832 /* first, wait until the CP has consumed all the commands in
833 * the ring buffer
834 */
835retry:
836 if (rb->flags & KGSL_FLAGS_STARTED) {
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700837 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 do {
839 GSL_RB_GET_READPTR(rb, &rb->rptr);
840 if (time_after(jiffies, wait_time)) {
841 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
842 rb->rptr, rb->wptr);
843 goto err;
844 }
845 } while (rb->rptr != rb->wptr);
846 }
847
848 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530849 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700851 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
852 &rbbm_status);
853 if (adreno_is_a2xx(adreno_dev)) {
854 if (rbbm_status == 0x110)
855 return 0;
856 } else {
857 if (!(rbbm_status & 0x80000000))
858 return 0;
859 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 }
861
862err:
863 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
864 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530865 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 goto retry;
867 }
868 return -ETIMEDOUT;
869}
870
871static unsigned int adreno_isidle(struct kgsl_device *device)
872{
873 int status = false;
874 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
875 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
876 unsigned int rbbm_status;
877
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700878 WARN_ON(device->state == KGSL_STATE_INIT);
879 /* If the device isn't active, don't force it on. */
880 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 /* Is the ring buffer is empty? */
882 GSL_RB_GET_READPTR(rb, &rb->rptr);
883 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
884 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700885 adreno_regread(device,
886 adreno_dev->gpudev->reg_rbbm_status,
887 &rbbm_status);
888
889 if (adreno_is_a2xx(adreno_dev)) {
890 if (rbbm_status == 0x110)
891 status = true;
892 } else {
893 if (!(rbbm_status & 0x80000000))
894 status = true;
895 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896 }
897 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -0700898 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899 }
900 return status;
901}
902
903/* Caller must hold the device mutex. */
904static int adreno_suspend_context(struct kgsl_device *device)
905{
906 int status = 0;
907 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
908
909 /* switch to NULL ctxt */
910 if (adreno_dev->drawctxt_active != NULL) {
911 adreno_drawctxt_switch(adreno_dev, NULL, 0);
912 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
913 }
914
915 return status;
916}
917
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700918const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
919 unsigned int pt_base,
920 unsigned int gpuaddr,
921 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922{
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700923 struct kgsl_memdesc *result = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924 struct kgsl_mem_entry *entry;
925 struct kgsl_process_private *priv;
926 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
927 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700928 struct kgsl_context *context;
929 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700931 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
932 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700934 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
935 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700937 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
938 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939
940 mutex_lock(&kgsl_driver.process_mutex);
941 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600942 if (!kgsl_mmu_pt_equal(priv->pagetable, pt_base))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944 spin_lock(&priv->mem_lock);
945 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
946 sizeof(unsigned int));
947 if (entry) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700948 result = &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949 spin_unlock(&priv->mem_lock);
950 mutex_unlock(&kgsl_driver.process_mutex);
951 return result;
952 }
953 spin_unlock(&priv->mem_lock);
954 }
955 mutex_unlock(&kgsl_driver.process_mutex);
956
957 BUG_ON(!mutex_is_locked(&device->mutex));
958 list_for_each_entry(entry, &device->memqueue, list) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700959 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) {
960 result = &entry->memdesc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700961 return result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962 }
963
964 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700965
966 while (1) {
967 struct adreno_context *adreno_context = NULL;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700968 context = idr_get_next(&device->context_idr, &next);
969 if (context == NULL)
970 break;
971
972 adreno_context = (struct adreno_context *)context->devctxt;
973
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700974 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
975 struct kgsl_memdesc *desc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700976
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700977 desc = &adreno_context->gpustate;
978 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
979 result = desc;
980 return result;
981 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700982
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700983 desc = &adreno_context->context_gmem_shadow.gmemshadow;
984 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
985 result = desc;
986 return result;
987 }
988 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700989 next = next + 1;
990 }
991
992 return NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700993
994}
995
996uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
997 unsigned int gpuaddr, unsigned int size)
998{
999 const struct kgsl_memdesc *memdesc;
1000
1001 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1002
1003 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004}
1005
1006void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1007 unsigned int *value)
1008{
1009 unsigned int *reg;
1010 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1011 reg = (unsigned int *)(device->regspace.mmio_virt_base
1012 + (offsetwords << 2));
1013
1014 if (!in_interrupt())
1015 kgsl_pre_hwaccess(device);
1016
1017 /*ensure this read finishes before the next one.
1018 * i.e. act like normal readl() */
1019 *value = __raw_readl(reg);
1020 rmb();
1021}
1022
1023void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1024 unsigned int value)
1025{
1026 unsigned int *reg;
1027
1028 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1029
1030 if (!in_interrupt())
1031 kgsl_pre_hwaccess(device);
1032
1033 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1034 reg = (unsigned int *)(device->regspace.mmio_virt_base
1035 + (offsetwords << 2));
1036
1037 /*ensure previous writes post before this one,
1038 * i.e. act like normal writel() */
1039 wmb();
1040 __raw_writel(value, reg);
1041}
1042
1043static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1044 unsigned int timestamp)
1045{
1046 int status;
1047 unsigned int ref_ts, enableflag;
1048
1049 status = kgsl_check_timestamp(device, timestamp);
1050 if (!status) {
1051 mutex_lock(&device->mutex);
1052 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1053 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1054 mb();
1055
1056 if (enableflag) {
1057 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1058 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1059 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001060 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061 kgsl_sharedmem_writel(&device->memstore,
1062 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1063 timestamp);
1064 wmb();
1065 }
1066 } else {
1067 unsigned int cmds[2];
1068 kgsl_sharedmem_writel(&device->memstore,
1069 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1070 timestamp);
1071 enableflag = 1;
1072 kgsl_sharedmem_writel(&device->memstore,
1073 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1074 enableflag);
1075 wmb();
1076 /* submit a dummy packet so that even if all
1077 * commands upto timestamp get executed we will still
1078 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001079 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001081 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1082 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083 }
1084 mutex_unlock(&device->mutex);
1085 }
1086
1087 return status;
1088}
1089
1090/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001091 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 placing a process in wait q. For conditional interrupts we expect the
1093 process to already be in its wait q when its exit condition checking
1094 function is called.
1095*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001096#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097({ \
1098 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001099 if (io) \
1100 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1101 else \
1102 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001103 __ret; \
1104})
1105
1106/* MUST be called with the device mutex held */
1107static int adreno_waittimestamp(struct kgsl_device *device,
1108 unsigned int timestamp,
1109 unsigned int msecs)
1110{
1111 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001112 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001113 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001115 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301116 int retries;
1117 unsigned int msecs_first;
1118 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301120 /* Don't wait forever, set a max value for now */
1121 if (msecs == -1)
1122 msecs = adreno_dev->wait_timeout;
1123
Jordan Crousee6239dd2011-11-17 13:39:21 -07001124 if (timestamp_cmp(timestamp, adreno_dev->ringbuffer.timestamp) > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1126 "rb->timestamp: %x\n",
1127 timestamp, adreno_dev->ringbuffer.timestamp);
1128 status = -EINVAL;
1129 goto done;
1130 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001131
Lynus Vaz06a9a902011-10-04 19:25:33 +05301132 /* Keep the first timeout as 100msecs before rewriting
1133 * the WPTR. Less visible impact if the WPTR has not
1134 * been updated properly.
1135 */
1136 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1137 msecs_part = (msecs - msecs_first + 3) / 4;
1138 for (retries = 0; retries < 5; retries++) {
1139 if (!kgsl_check_timestamp(device, timestamp)) {
1140 adreno_poke(device);
1141 io_cnt = (io_cnt + 1) % 100;
1142 if (io_cnt <
1143 pwr->pwrlevels[pwr->active_pwrlevel].
1144 io_fraction)
1145 io = 0;
1146 mutex_unlock(&device->mutex);
1147 /* We need to make sure that the process is
1148 * placed in wait-q before its condition is called
1149 */
1150 status = kgsl_wait_event_interruptible_timeout(
1151 device->wait_queue,
1152 kgsl_check_interrupt_timestamp(device,
1153 timestamp),
1154 msecs_to_jiffies(retries ?
1155 msecs_part : msecs_first), io);
1156 mutex_lock(&device->mutex);
1157
1158 if (status > 0) {
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001159 /*completed before the wait finished */
Lynus Vaz06a9a902011-10-04 19:25:33 +05301160 status = 0;
1161 goto done;
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001162 } else if (status < 0) {
1163 /*an error occurred*/
1164 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001165 }
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001166 /*this wait timed out*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167 }
1168 }
Lynus Vaz06a9a902011-10-04 19:25:33 +05301169 if (!kgsl_check_timestamp(device, timestamp)) {
1170 status = -ETIMEDOUT;
1171 KGSL_DRV_ERR(device,
1172 "Device hang detected while waiting "
1173 "for timestamp: %x, last "
1174 "submitted(rb->timestamp): %x, wptr: "
1175 "%x\n", timestamp,
1176 adreno_dev->ringbuffer.timestamp,
1177 adreno_dev->ringbuffer.wptr);
1178 if (!adreno_dump_and_recover(device)) {
1179 /* wait for idle after recovery as the
1180 * timestamp that this process wanted
1181 * to wait on may be invalid */
1182 if (!adreno_idle(device,
1183 KGSL_TIMEOUT_DEFAULT))
1184 status = 0;
1185 }
1186 } else {
1187 status = 0;
1188 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189
1190done:
1191 return (int)status;
1192}
1193
1194static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1195 enum kgsl_timestamp_type type)
1196{
1197 unsigned int timestamp = 0;
1198
1199 if (type == KGSL_TIMESTAMP_CONSUMED)
1200 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1201 else if (type == KGSL_TIMESTAMP_RETIRED)
1202 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1203 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1204 rmb();
1205
1206 return timestamp;
1207}
1208
1209static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1210 unsigned int cmd, void *data)
1211{
1212 int result = 0;
1213 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1214 struct kgsl_context *context;
1215
1216 switch (cmd) {
1217 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1218 binbase = data;
1219
1220 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1221 if (context) {
1222 adreno_drawctxt_set_bin_base_offset(
1223 dev_priv->device, context, binbase->offset);
1224 } else {
1225 result = -EINVAL;
1226 KGSL_DRV_ERR(dev_priv->device,
1227 "invalid drawctxt drawctxt_id %d "
1228 "device_id=%d\n",
1229 binbase->drawctxt_id, dev_priv->device->id);
1230 }
1231 break;
1232
1233 default:
1234 KGSL_DRV_INFO(dev_priv->device,
1235 "invalid ioctl code %08x\n", cmd);
1236 result = -EINVAL;
1237 break;
1238 }
1239 return result;
1240
1241}
1242
1243static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1244{
1245 gpu_freq /= 1000000;
1246 return ticks / gpu_freq;
1247}
1248
1249static void adreno_power_stats(struct kgsl_device *device,
1250 struct kgsl_power_stats *stats)
1251{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001252 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001253 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001254 unsigned int cycles;
1255
1256 /* Get the busy cycles counted since the counter was last reset */
1257 /* Calling this function also resets and restarts the counter */
1258
1259 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260
1261 /* In order to calculate idle you have to have run the algorithm *
1262 * at least once to get a start time. */
1263 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001264 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001265 stats->total_time = tmp - pwr->time;
1266 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001267 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001268 pwrlevels[device->pwrctrl.active_pwrlevel].
1269 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001270 } else {
1271 stats->total_time = 0;
1272 stats->busy_time = 0;
1273 pwr->time = ktime_to_us(ktime_get());
1274 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001275}
1276
1277void adreno_irqctrl(struct kgsl_device *device, int state)
1278{
Jordan Crousea78c9172011-07-11 13:14:09 -06001279 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1280 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281}
1282
Jordan Crousea0758f22011-12-07 11:19:22 -07001283static unsigned int adreno_gpuid(struct kgsl_device *device)
1284{
1285 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1286
1287 /* Standard KGSL gpuid format:
1288 * top word is 0x0002 for 2D or 0x0003 for 3D
1289 * Bottom word is core specific identifer
1290 */
1291
1292 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1293}
1294
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295static const struct kgsl_functable adreno_functable = {
1296 /* Mandatory functions */
1297 .regread = adreno_regread,
1298 .regwrite = adreno_regwrite,
1299 .idle = adreno_idle,
1300 .isidle = adreno_isidle,
1301 .suspend_context = adreno_suspend_context,
1302 .start = adreno_start,
1303 .stop = adreno_stop,
1304 .getproperty = adreno_getproperty,
1305 .waittimestamp = adreno_waittimestamp,
1306 .readtimestamp = adreno_readtimestamp,
1307 .issueibcmds = adreno_ringbuffer_issueibcmds,
1308 .ioctl = adreno_ioctl,
1309 .setup_pt = adreno_setup_pt,
1310 .cleanup_pt = adreno_cleanup_pt,
1311 .power_stats = adreno_power_stats,
1312 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001313 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001314 .snapshot = adreno_snapshot,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315 /* Optional functions */
1316 .setstate = adreno_setstate,
1317 .drawctxt_create = adreno_drawctxt_create,
1318 .drawctxt_destroy = adreno_drawctxt_destroy,
1319};
1320
1321static struct platform_device_id adreno_id_table[] = {
1322 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1323 { },
1324};
1325MODULE_DEVICE_TABLE(platform, adreno_id_table);
1326
1327static struct platform_driver adreno_platform_driver = {
1328 .probe = adreno_probe,
1329 .remove = __devexit_p(adreno_remove),
1330 .suspend = kgsl_suspend_driver,
1331 .resume = kgsl_resume_driver,
1332 .id_table = adreno_id_table,
1333 .driver = {
1334 .owner = THIS_MODULE,
1335 .name = DEVICE_3D_NAME,
1336 .pm = &kgsl_pm_ops,
1337 }
1338};
1339
1340static int __init kgsl_3d_init(void)
1341{
1342 return platform_driver_register(&adreno_platform_driver);
1343}
1344
1345static void __exit kgsl_3d_exit(void)
1346{
1347 platform_driver_unregister(&adreno_platform_driver);
1348}
1349
1350module_init(kgsl_3d_init);
1351module_exit(kgsl_3d_exit);
1352
1353MODULE_DESCRIPTION("3D Graphics driver");
1354MODULE_VERSION("1.2");
1355MODULE_LICENSE("GPL v2");
1356MODULE_ALIAS("platform:kgsl_3d");