blob: bde40451e71367285a9bb81a6443a182117c94e2 [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 /*
Jordan Crousee3f80ea2012-02-04 14:22:36 -0700255 * A3XX doesn't support the fast path (the registers don't even exist)
256 * so just bail out early
257 */
258
259 if (adreno_is_a3xx(adreno_dev)) {
260 kgsl_mmu_device_setstate(device, flags);
261 return;
262 }
263
264 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600265 * If possible, then set the state via the command stream to avoid
266 * a CPU idle. Otherwise, use the default setstate which uses register
267 * writes For CFF dump we must idle and use the registers so that it is
268 * easier to filter out the mmu accesses from the dump
269 */
270 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
272 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600273 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274 *cmds++ = 0x00000000;
275
276 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600277 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600278 *cmds++ = kgsl_pt_get_base_addr(
279 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 sizedwords += 4;
281 }
282
283 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
284 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600285 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 1);
287 *cmds++ = 0x00000000;
288 sizedwords += 2;
289 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600290 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 *cmds++ = mh_mmu_invalidate;
292 sizedwords += 2;
293 }
294
295 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600296 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297 /* HW workaround: to resolve MMU page fault interrupts
298 * caused by the VGT.It prevents the CP PFP from filling
299 * the VGT DMA request fifo too early,thereby ensuring
300 * that the VGT will not fetch vertex/bin data until
301 * after the page table base register has been updated.
302 *
303 * Two null DRAW_INDX_BIN packets are inserted right
304 * after the page table base update, followed by a
305 * wait for idle. The null packets will fill up the
306 * VGT DMA request fifo and prevent any further
307 * vertex/bin updates from occurring until the wait
308 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600309 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 *cmds++ = (0x4 << 16) |
311 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
312 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600313 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600314 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600315 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316 *cmds++ = 0; /* viz query info */
317 *cmds++ = 0x0003C004; /* draw indicator */
318 *cmds++ = 0; /* bin base */
319 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600320 *cmds++ =
321 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600323 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324 *cmds++ = 0; /* viz query info */
325 *cmds++ = 0x0003C004; /* draw indicator */
326 *cmds++ = 0; /* bin base */
327 *cmds++ = 3; /* bin size */
328 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600329 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600331 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 *cmds++ = 0x00000000;
333 sizedwords += 21;
334 }
335
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600336
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600338 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 *cmds++ = 0x7fff; /* invalidate all base pointers */
340 sizedwords += 2;
341 }
342
343 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
344 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600345 } else {
346 kgsl_mmu_device_setstate(device, flags);
347 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348}
349
350static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700351a3xx_getchipid(struct kgsl_device *device)
352{
353 unsigned int chipid = 0;
354 unsigned int coreid, majorid, minorid, patchid;
355 unsigned int version;
356
357 adreno_regread(device, A3XX_RBBM_HW_VERSION, &version);
358
359 coreid = 0x03;
360
361 /* Version might not be set - if it isn't, assume this is 320 */
362 if (version)
363 majorid = version & 0x0F;
364 else
365 majorid = 1;
366
367 minorid = (version >> 4) & 0xFFF;
368 patchid = 0;
369
370 chipid = (coreid << 24) | (majorid << 16) | (minorid << 8) | patchid;
371
372 return chipid;
373}
374
375static unsigned int
376a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377{
378 unsigned int chipid = 0;
379 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700380 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381
382 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
383 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
384 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
385
386 /*
387 * adreno 22x gpus are indicated by coreid 2,
388 * but REG_RBBM_PERIPHID1 always contains 0 for this field
389 */
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700390 if (cpu_is_msm8960() || cpu_is_msm8x60() || cpu_is_msm8930())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391 chipid = 2 << 24;
392 else
393 chipid = (coreid & 0xF) << 24;
394
395 chipid |= ((majorid >> 4) & 0xF) << 16;
396
397 minorid = ((revid >> 0) & 0xFF);
398
399 patchid = ((revid >> 16) & 0xFF);
400
401 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700402 /* 8960v3 returns 5 for patch release, but it should be 6 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 if (cpu_is_qsd8x50())
404 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700405 else if (cpu_is_msm8960() &&
406 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
407 patchid = 6;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408
409 chipid |= (minorid << 8) | patchid;
410
411 return chipid;
412}
413
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700414static unsigned int
415adreno_getchipid(struct kgsl_device *device)
416{
417 if (cpu_is_apq8064())
418 return a3xx_getchipid(device);
419 else
420 return a2xx_getchipid(device);
421}
422
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423static inline bool _rev_match(unsigned int id, unsigned int entry)
424{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600425 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427
428static void
429adreno_identify_gpu(struct adreno_device *adreno_dev)
430{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600431 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432
433 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
434
435 core = (adreno_dev->chip_id >> 24) & 0xff;
436 major = (adreno_dev->chip_id >> 16) & 0xff;
437 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600438 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439
Jordan Crouse505df9c2011-07-28 08:37:59 -0600440 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
441 if (core == adreno_gpulist[i].core &&
442 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600443 _rev_match(minor, adreno_gpulist[i].minor) &&
444 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446 }
447
Jordan Crouse505df9c2011-07-28 08:37:59 -0600448 if (i == ARRAY_SIZE(adreno_gpulist)) {
449 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
450 return;
451 }
452
453 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
454 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
455 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
456 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700457 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
458 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700459 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460}
461
462static int __devinit
463adreno_probe(struct platform_device *pdev)
464{
465 struct kgsl_device *device;
466 struct adreno_device *adreno_dev;
467 int status = -EINVAL;
468
469 device = (struct kgsl_device *)pdev->id_entry->driver_data;
470 adreno_dev = ADRENO_DEVICE(device);
471 device->parentdev = &pdev->dev;
472
473 init_completion(&device->recovery_gate);
474
475 status = adreno_ringbuffer_init(device);
476 if (status != 0)
477 goto error;
478
479 status = kgsl_device_platform_probe(device, adreno_isr);
480 if (status)
481 goto error_close_rb;
482
483 adreno_debugfs_init(device);
484
485 kgsl_pwrscale_init(device);
486 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
487
488 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
489 return 0;
490
491error_close_rb:
492 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
493error:
494 device->parentdev = NULL;
495 return status;
496}
497
498static int __devexit adreno_remove(struct platform_device *pdev)
499{
500 struct kgsl_device *device;
501 struct adreno_device *adreno_dev;
502
503 device = (struct kgsl_device *)pdev->id_entry->driver_data;
504 adreno_dev = ADRENO_DEVICE(device);
505
506 kgsl_pwrscale_detach_policy(device);
507 kgsl_pwrscale_close(device);
508
509 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
510 kgsl_device_platform_remove(device);
511
512 return 0;
513}
514
515static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
516{
517 int status = -EINVAL;
518 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519
Jeremy Gebben388c2972011-12-16 09:05:07 -0700520 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521
522 /* Power up the device */
523 kgsl_pwrctrl_enable(device);
524
525 /* Identify the specific GPU */
526 adreno_identify_gpu(adreno_dev);
527
Jordan Crouse505df9c2011-07-28 08:37:59 -0600528 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
529 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
530 adreno_dev->chip_id);
531 goto error_clk_off;
532 }
533
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700534 /* Set up the MMU */
535 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600536 /*
537 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
538 * on older gpus
539 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700540 if (adreno_is_a20x(adreno_dev)) {
541 device->mh.mh_intf_cfg1 = 0;
542 device->mh.mh_intf_cfg2 = 0;
543 }
544
545 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600546 }
547
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700548 status = kgsl_mmu_start(device);
549 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 goto error_clk_off;
551
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700552 /* Start the GPU */
553 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554
555 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700556 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557
558 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700559 if (status == 0) {
560 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
561 return 0;
562 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600565 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566error_clk_off:
567 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568
569 return status;
570}
571
572static int adreno_stop(struct kgsl_device *device)
573{
574 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
575
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 adreno_dev->drawctxt_active = NULL;
577
578 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
579
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 kgsl_mmu_stop(device);
581
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700582 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530583 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600584
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585 /* Power down the device */
586 kgsl_pwrctrl_disable(device);
587
588 return 0;
589}
590
591static int
592adreno_recover_hang(struct kgsl_device *device)
593{
594 int ret;
595 unsigned int *rb_buffer;
596 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
597 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
598 unsigned int timestamp;
599 unsigned int num_rb_contents;
600 unsigned int bad_context;
601 unsigned int reftimestamp;
602 unsigned int enable_ts;
603 unsigned int soptimestamp;
604 unsigned int eoptimestamp;
605 struct adreno_context *drawctxt;
606
607 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
608 rb_buffer = vmalloc(rb->buffer_desc.size);
609 if (!rb_buffer) {
610 KGSL_MEM_ERR(device,
611 "Failed to allocate memory for recovery: %x\n",
612 rb->buffer_desc.size);
613 return -ENOMEM;
614 }
615 /* Extract valid contents from rb which can stil be executed after
616 * hang */
617 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
618 if (ret)
619 goto done;
620 timestamp = rb->timestamp;
621 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
622 kgsl_sharedmem_readl(&device->memstore, &bad_context,
623 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
624 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
625 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
626 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
627 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
628 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
629 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
630 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
631 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
632 /* Make sure memory is synchronized before restarting the GPU */
633 mb();
634 KGSL_CTXT_ERR(device,
635 "Context that caused a GPU hang: %x\n", bad_context);
636 /* restart device */
637 ret = adreno_stop(device);
638 if (ret)
639 goto done;
640 ret = adreno_start(device, true);
641 if (ret)
642 goto done;
643 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
644 /* Restore timestamp states */
645 kgsl_sharedmem_writel(&device->memstore,
646 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
647 soptimestamp);
648 kgsl_sharedmem_writel(&device->memstore,
649 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
650 eoptimestamp);
651 kgsl_sharedmem_writel(&device->memstore,
652 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
653 soptimestamp);
654 if (num_rb_contents) {
655 kgsl_sharedmem_writel(&device->memstore,
656 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
657 reftimestamp);
658 kgsl_sharedmem_writel(&device->memstore,
659 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
660 enable_ts);
661 }
662 /* Make sure all writes are posted before the GPU reads them */
663 wmb();
664 /* Mark the invalid context so no more commands are accepted from
665 * that context */
666
667 drawctxt = (struct adreno_context *) bad_context;
668
669 KGSL_CTXT_ERR(device,
670 "Context that caused a GPU hang: %x\n", bad_context);
671
672 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
673
674 /* Restore valid commands in ringbuffer */
675 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
676 rb->timestamp = timestamp;
677done:
678 vfree(rb_buffer);
679 return ret;
680}
681
682static int
683adreno_dump_and_recover(struct kgsl_device *device)
684{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685 int result = -ETIMEDOUT;
686
687 if (device->state == KGSL_STATE_HUNG)
688 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700689 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 mutex_unlock(&device->mutex);
691 wait_for_completion(&device->recovery_gate);
692 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700693 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694 result = 0;
695 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700696 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700698 /* Detected a hang */
699
700
701 /*
702 * Trigger an automatic dump of the state to
703 * the console
704 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700706
707 /*
708 * Make a GPU snapshot. For now, do it after the PM dump so we
709 * can at least be sure the PM dump will work as it always has
710 */
711 kgsl_device_snapshot(device, 1);
712
Jeremy Gebben388c2972011-12-16 09:05:07 -0700713 result = adreno_recover_hang(device);
714 if (result)
715 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
716 else
717 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
718 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 }
720done:
721 return result;
722}
723
724static int adreno_getproperty(struct kgsl_device *device,
725 enum kgsl_property_type type,
726 void *value,
727 unsigned int sizebytes)
728{
729 int status = -EINVAL;
730 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
731
732 switch (type) {
733 case KGSL_PROP_DEVICE_INFO:
734 {
735 struct kgsl_devinfo devinfo;
736
737 if (sizebytes != sizeof(devinfo)) {
738 status = -EINVAL;
739 break;
740 }
741
742 memset(&devinfo, 0, sizeof(devinfo));
743 devinfo.device_id = device->id+1;
744 devinfo.chip_id = adreno_dev->chip_id;
745 devinfo.mmu_enabled = kgsl_mmu_enabled();
746 devinfo.gpu_id = adreno_dev->gpurev;
747 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
748 gpu_base;
749 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
750 sizebytes;
751
752 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
753 0) {
754 status = -EFAULT;
755 break;
756 }
757 status = 0;
758 }
759 break;
760 case KGSL_PROP_DEVICE_SHADOW:
761 {
762 struct kgsl_shadowprop shadowprop;
763
764 if (sizebytes != sizeof(shadowprop)) {
765 status = -EINVAL;
766 break;
767 }
768 memset(&shadowprop, 0, sizeof(shadowprop));
769 if (device->memstore.hostptr) {
770 /*NOTE: with mmu enabled, gpuaddr doesn't mean
771 * anything to mmap().
772 */
773 shadowprop.gpuaddr = device->memstore.physaddr;
774 shadowprop.size = device->memstore.size;
775 /* GSL needs this to be set, even if it
776 appears to be meaningless */
777 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
778 }
779 if (copy_to_user(value, &shadowprop,
780 sizeof(shadowprop))) {
781 status = -EFAULT;
782 break;
783 }
784 status = 0;
785 }
786 break;
787 case KGSL_PROP_MMU_ENABLE:
788 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600789 int mmu_prop = kgsl_mmu_enabled();
790
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791 if (sizebytes != sizeof(int)) {
792 status = -EINVAL;
793 break;
794 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600795 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 status = -EFAULT;
797 break;
798 }
799 status = 0;
800 }
801 break;
802 case KGSL_PROP_INTERRUPT_WAITS:
803 {
804 int int_waits = 1;
805 if (sizebytes != sizeof(int)) {
806 status = -EINVAL;
807 break;
808 }
809 if (copy_to_user(value, &int_waits, sizeof(int))) {
810 status = -EFAULT;
811 break;
812 }
813 status = 0;
814 }
815 break;
816 default:
817 status = -EINVAL;
818 }
819
820 return status;
821}
822
Lynus Vaz06a9a902011-10-04 19:25:33 +0530823static inline void adreno_poke(struct kgsl_device *device)
824{
825 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
826 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
827}
828
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829/* Caller must hold the device mutex. */
830int adreno_idle(struct kgsl_device *device, unsigned int timeout)
831{
832 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
833 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
834 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530835 unsigned long wait_timeout =
836 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +0530837 unsigned long wait_time;
838 unsigned long wait_time_part;
839 unsigned int msecs;
840 unsigned int msecs_first;
841 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700843 kgsl_cffdump_regpoll(device->id,
844 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 0x00000000, 0x80000000);
846 /* first, wait until the CP has consumed all the commands in
847 * the ring buffer
848 */
849retry:
850 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +0530851 msecs = adreno_dev->wait_timeout;
852 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
853 msecs_part = (msecs - msecs_first + 3) / 4;
854 wait_time = jiffies + wait_timeout;
855 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700856 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 do {
Lynus Vaz284d1042012-01-31 16:32:31 +0530858 if (time_after(jiffies, wait_time_part)) {
859 adreno_poke(device);
860 wait_time_part = jiffies +
861 msecs_to_jiffies(msecs_part);
862 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863 GSL_RB_GET_READPTR(rb, &rb->rptr);
864 if (time_after(jiffies, wait_time)) {
865 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
866 rb->rptr, rb->wptr);
867 goto err;
868 }
869 } while (rb->rptr != rb->wptr);
870 }
871
872 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530873 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700875 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
876 &rbbm_status);
877 if (adreno_is_a2xx(adreno_dev)) {
878 if (rbbm_status == 0x110)
879 return 0;
880 } else {
881 if (!(rbbm_status & 0x80000000))
882 return 0;
883 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884 }
885
886err:
887 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
888 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530889 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 goto retry;
891 }
892 return -ETIMEDOUT;
893}
894
895static unsigned int adreno_isidle(struct kgsl_device *device)
896{
897 int status = false;
898 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
899 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
900 unsigned int rbbm_status;
901
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700902 WARN_ON(device->state == KGSL_STATE_INIT);
903 /* If the device isn't active, don't force it on. */
904 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 /* Is the ring buffer is empty? */
906 GSL_RB_GET_READPTR(rb, &rb->rptr);
907 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
908 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700909 adreno_regread(device,
910 adreno_dev->gpudev->reg_rbbm_status,
911 &rbbm_status);
912
913 if (adreno_is_a2xx(adreno_dev)) {
914 if (rbbm_status == 0x110)
915 status = true;
916 } else {
917 if (!(rbbm_status & 0x80000000))
918 status = true;
919 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 }
921 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -0700922 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923 }
924 return status;
925}
926
927/* Caller must hold the device mutex. */
928static int adreno_suspend_context(struct kgsl_device *device)
929{
930 int status = 0;
931 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
932
933 /* switch to NULL ctxt */
934 if (adreno_dev->drawctxt_active != NULL) {
935 adreno_drawctxt_switch(adreno_dev, NULL, 0);
936 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
937 }
938
939 return status;
940}
941
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700942const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
943 unsigned int pt_base,
944 unsigned int gpuaddr,
945 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946{
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700947 struct kgsl_memdesc *result = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 struct kgsl_mem_entry *entry;
949 struct kgsl_process_private *priv;
950 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
951 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700952 struct kgsl_context *context;
953 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700955 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
956 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700958 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
959 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700960
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700961 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
962 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963
964 mutex_lock(&kgsl_driver.process_mutex);
965 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600966 if (!kgsl_mmu_pt_equal(priv->pagetable, pt_base))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968 spin_lock(&priv->mem_lock);
969 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
970 sizeof(unsigned int));
971 if (entry) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700972 result = &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973 spin_unlock(&priv->mem_lock);
974 mutex_unlock(&kgsl_driver.process_mutex);
975 return result;
976 }
977 spin_unlock(&priv->mem_lock);
978 }
979 mutex_unlock(&kgsl_driver.process_mutex);
980
981 BUG_ON(!mutex_is_locked(&device->mutex));
982 list_for_each_entry(entry, &device->memqueue, list) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700983 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) {
984 result = &entry->memdesc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700985 return result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986 }
987
988 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700989
990 while (1) {
991 struct adreno_context *adreno_context = NULL;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700992 context = idr_get_next(&device->context_idr, &next);
993 if (context == NULL)
994 break;
995
996 adreno_context = (struct adreno_context *)context->devctxt;
997
Jeremy Gebben775d48b2011-12-12 17:10:19 -0700998 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
999 struct kgsl_memdesc *desc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001000
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001001 desc = &adreno_context->gpustate;
1002 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1003 result = desc;
1004 return result;
1005 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001006
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001007 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1008 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1009 result = desc;
1010 return result;
1011 }
1012 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001013 next = next + 1;
1014 }
1015
1016 return NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001017
1018}
1019
1020uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1021 unsigned int gpuaddr, unsigned int size)
1022{
1023 const struct kgsl_memdesc *memdesc;
1024
1025 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1026
1027 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001028}
1029
1030void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1031 unsigned int *value)
1032{
1033 unsigned int *reg;
1034 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1035 reg = (unsigned int *)(device->regspace.mmio_virt_base
1036 + (offsetwords << 2));
1037
1038 if (!in_interrupt())
1039 kgsl_pre_hwaccess(device);
1040
1041 /*ensure this read finishes before the next one.
1042 * i.e. act like normal readl() */
1043 *value = __raw_readl(reg);
1044 rmb();
1045}
1046
1047void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1048 unsigned int value)
1049{
1050 unsigned int *reg;
1051
1052 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1053
1054 if (!in_interrupt())
1055 kgsl_pre_hwaccess(device);
1056
1057 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1058 reg = (unsigned int *)(device->regspace.mmio_virt_base
1059 + (offsetwords << 2));
1060
1061 /*ensure previous writes post before this one,
1062 * i.e. act like normal writel() */
1063 wmb();
1064 __raw_writel(value, reg);
1065}
1066
1067static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1068 unsigned int timestamp)
1069{
1070 int status;
1071 unsigned int ref_ts, enableflag;
1072
1073 status = kgsl_check_timestamp(device, timestamp);
1074 if (!status) {
1075 mutex_lock(&device->mutex);
1076 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1077 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1078 mb();
1079
1080 if (enableflag) {
1081 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1082 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1083 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001084 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085 kgsl_sharedmem_writel(&device->memstore,
1086 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1087 timestamp);
1088 wmb();
1089 }
1090 } else {
1091 unsigned int cmds[2];
1092 kgsl_sharedmem_writel(&device->memstore,
1093 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1094 timestamp);
1095 enableflag = 1;
1096 kgsl_sharedmem_writel(&device->memstore,
1097 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1098 enableflag);
1099 wmb();
1100 /* submit a dummy packet so that even if all
1101 * commands upto timestamp get executed we will still
1102 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001103 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001105 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1106 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107 }
1108 mutex_unlock(&device->mutex);
1109 }
1110
1111 return status;
1112}
1113
1114/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001115 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001116 placing a process in wait q. For conditional interrupts we expect the
1117 process to already be in its wait q when its exit condition checking
1118 function is called.
1119*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001120#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121({ \
1122 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001123 if (io) \
1124 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1125 else \
1126 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127 __ret; \
1128})
1129
1130/* MUST be called with the device mutex held */
1131static int adreno_waittimestamp(struct kgsl_device *device,
1132 unsigned int timestamp,
1133 unsigned int msecs)
1134{
1135 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001136 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001137 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001139 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301140 int retries;
1141 unsigned int msecs_first;
1142 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301144 /* Don't wait forever, set a max value for now */
1145 if (msecs == -1)
1146 msecs = adreno_dev->wait_timeout;
1147
Jordan Crousee6239dd2011-11-17 13:39:21 -07001148 if (timestamp_cmp(timestamp, adreno_dev->ringbuffer.timestamp) > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1150 "rb->timestamp: %x\n",
1151 timestamp, adreno_dev->ringbuffer.timestamp);
1152 status = -EINVAL;
1153 goto done;
1154 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155
Lynus Vaz06a9a902011-10-04 19:25:33 +05301156 /* Keep the first timeout as 100msecs before rewriting
1157 * the WPTR. Less visible impact if the WPTR has not
1158 * been updated properly.
1159 */
1160 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1161 msecs_part = (msecs - msecs_first + 3) / 4;
1162 for (retries = 0; retries < 5; retries++) {
1163 if (!kgsl_check_timestamp(device, timestamp)) {
1164 adreno_poke(device);
1165 io_cnt = (io_cnt + 1) % 100;
1166 if (io_cnt <
1167 pwr->pwrlevels[pwr->active_pwrlevel].
1168 io_fraction)
1169 io = 0;
1170 mutex_unlock(&device->mutex);
1171 /* We need to make sure that the process is
1172 * placed in wait-q before its condition is called
1173 */
1174 status = kgsl_wait_event_interruptible_timeout(
1175 device->wait_queue,
1176 kgsl_check_interrupt_timestamp(device,
1177 timestamp),
1178 msecs_to_jiffies(retries ?
1179 msecs_part : msecs_first), io);
1180 mutex_lock(&device->mutex);
1181
1182 if (status > 0) {
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001183 /*completed before the wait finished */
Lynus Vaz06a9a902011-10-04 19:25:33 +05301184 status = 0;
1185 goto done;
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001186 } else if (status < 0) {
1187 /*an error occurred*/
1188 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189 }
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001190 /*this wait timed out*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001191 }
1192 }
Lynus Vaz06a9a902011-10-04 19:25:33 +05301193 if (!kgsl_check_timestamp(device, timestamp)) {
1194 status = -ETIMEDOUT;
1195 KGSL_DRV_ERR(device,
1196 "Device hang detected while waiting "
1197 "for timestamp: %x, last "
1198 "submitted(rb->timestamp): %x, wptr: "
1199 "%x\n", timestamp,
1200 adreno_dev->ringbuffer.timestamp,
1201 adreno_dev->ringbuffer.wptr);
1202 if (!adreno_dump_and_recover(device)) {
1203 /* wait for idle after recovery as the
1204 * timestamp that this process wanted
1205 * to wait on may be invalid */
1206 if (!adreno_idle(device,
1207 KGSL_TIMEOUT_DEFAULT))
1208 status = 0;
1209 }
1210 } else {
1211 status = 0;
1212 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213
1214done:
1215 return (int)status;
1216}
1217
1218static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1219 enum kgsl_timestamp_type type)
1220{
1221 unsigned int timestamp = 0;
1222
1223 if (type == KGSL_TIMESTAMP_CONSUMED)
1224 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1225 else if (type == KGSL_TIMESTAMP_RETIRED)
1226 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1227 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1228 rmb();
1229
1230 return timestamp;
1231}
1232
1233static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1234 unsigned int cmd, void *data)
1235{
1236 int result = 0;
1237 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1238 struct kgsl_context *context;
1239
1240 switch (cmd) {
1241 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1242 binbase = data;
1243
1244 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1245 if (context) {
1246 adreno_drawctxt_set_bin_base_offset(
1247 dev_priv->device, context, binbase->offset);
1248 } else {
1249 result = -EINVAL;
1250 KGSL_DRV_ERR(dev_priv->device,
1251 "invalid drawctxt drawctxt_id %d "
1252 "device_id=%d\n",
1253 binbase->drawctxt_id, dev_priv->device->id);
1254 }
1255 break;
1256
1257 default:
1258 KGSL_DRV_INFO(dev_priv->device,
1259 "invalid ioctl code %08x\n", cmd);
1260 result = -EINVAL;
1261 break;
1262 }
1263 return result;
1264
1265}
1266
1267static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1268{
1269 gpu_freq /= 1000000;
1270 return ticks / gpu_freq;
1271}
1272
1273static void adreno_power_stats(struct kgsl_device *device,
1274 struct kgsl_power_stats *stats)
1275{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001276 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001278 unsigned int cycles;
1279
1280 /* Get the busy cycles counted since the counter was last reset */
1281 /* Calling this function also resets and restarts the counter */
1282
1283 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001284
1285 /* In order to calculate idle you have to have run the algorithm *
1286 * at least once to get a start time. */
1287 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001288 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289 stats->total_time = tmp - pwr->time;
1290 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001291 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292 pwrlevels[device->pwrctrl.active_pwrlevel].
1293 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294 } else {
1295 stats->total_time = 0;
1296 stats->busy_time = 0;
1297 pwr->time = ktime_to_us(ktime_get());
1298 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001299}
1300
1301void adreno_irqctrl(struct kgsl_device *device, int state)
1302{
Jordan Crousea78c9172011-07-11 13:14:09 -06001303 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1304 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305}
1306
Jordan Crousea0758f22011-12-07 11:19:22 -07001307static unsigned int adreno_gpuid(struct kgsl_device *device)
1308{
1309 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1310
1311 /* Standard KGSL gpuid format:
1312 * top word is 0x0002 for 2D or 0x0003 for 3D
1313 * Bottom word is core specific identifer
1314 */
1315
1316 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1317}
1318
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001319static const struct kgsl_functable adreno_functable = {
1320 /* Mandatory functions */
1321 .regread = adreno_regread,
1322 .regwrite = adreno_regwrite,
1323 .idle = adreno_idle,
1324 .isidle = adreno_isidle,
1325 .suspend_context = adreno_suspend_context,
1326 .start = adreno_start,
1327 .stop = adreno_stop,
1328 .getproperty = adreno_getproperty,
1329 .waittimestamp = adreno_waittimestamp,
1330 .readtimestamp = adreno_readtimestamp,
1331 .issueibcmds = adreno_ringbuffer_issueibcmds,
1332 .ioctl = adreno_ioctl,
1333 .setup_pt = adreno_setup_pt,
1334 .cleanup_pt = adreno_cleanup_pt,
1335 .power_stats = adreno_power_stats,
1336 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001337 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001338 .snapshot = adreno_snapshot,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339 /* Optional functions */
1340 .setstate = adreno_setstate,
1341 .drawctxt_create = adreno_drawctxt_create,
1342 .drawctxt_destroy = adreno_drawctxt_destroy,
1343};
1344
1345static struct platform_device_id adreno_id_table[] = {
1346 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1347 { },
1348};
1349MODULE_DEVICE_TABLE(platform, adreno_id_table);
1350
1351static struct platform_driver adreno_platform_driver = {
1352 .probe = adreno_probe,
1353 .remove = __devexit_p(adreno_remove),
1354 .suspend = kgsl_suspend_driver,
1355 .resume = kgsl_resume_driver,
1356 .id_table = adreno_id_table,
1357 .driver = {
1358 .owner = THIS_MODULE,
1359 .name = DEVICE_3D_NAME,
1360 .pm = &kgsl_pm_ops,
1361 }
1362};
1363
1364static int __init kgsl_3d_init(void)
1365{
1366 return platform_driver_register(&adreno_platform_driver);
1367}
1368
1369static void __exit kgsl_3d_exit(void)
1370{
1371 platform_driver_unregister(&adreno_platform_driver);
1372}
1373
1374module_init(kgsl_3d_init);
1375module_exit(kgsl_3d_exit);
1376
1377MODULE_DESCRIPTION("3D Graphics driver");
1378MODULE_VERSION("1.2");
1379MODULE_LICENSE("GPL v2");
1380MODULE_ALIAS("platform:kgsl_3d");