blob: c9f73b5f450de4be6af557ace8c3abc46f8a96cb [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 */
13#include <linux/delay.h>
14#include <linux/uaccess.h>
15#include <linux/vmalloc.h>
16#include <linux/ioctl.h>
17#include <linux/sched.h>
18
19#include <mach/socinfo.h>
20
21#include "kgsl.h"
22#include "kgsl_pwrscale.h"
23#include "kgsl_cffdump.h"
24#include "kgsl_sharedmem.h"
25
26#include "adreno.h"
27#include "adreno_pm4types.h"
28#include "adreno_debugfs.h"
29#include "adreno_postmortem.h"
30
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070031#include "a2xx_reg.h"
Shubhraprakash Das767fdda2011-08-15 15:49:45 -060032#include "kgsl_mmu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34#define DRIVER_VERSION_MAJOR 3
35#define DRIVER_VERSION_MINOR 1
36
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037/* Adreno MH arbiter config*/
38#define ADRENO_CFG_MHARB \
39 (0x10 \
40 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
41 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
42 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
43 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
44 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
45 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
46 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
47 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
48 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
49 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
50 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
51 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
52 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
53 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
54
55#define ADRENO_MMU_CONFIG \
56 (0x01 \
57 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
58 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
59 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
60 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
61 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
62 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
63 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
64 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
65 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
66 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
67 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
68
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069static const struct kgsl_functable adreno_functable;
70
71static struct adreno_device device_3d0 = {
72 .dev = {
73 .name = DEVICE_3D0_NAME,
74 .id = KGSL_DEVICE_3D0,
75 .ver_major = DRIVER_VERSION_MAJOR,
76 .ver_minor = DRIVER_VERSION_MINOR,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060077 .mh = {
78 .mharb = ADRENO_CFG_MHARB,
79 /* Remove 1k boundary check in z470 to avoid a GPU
80 * hang. Notice that this solution won't work if
81 * both EBI and SMI are used
82 */
83 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084 /* turn off memory protection unit by setting
85 acceptable physical address range to include
86 all pages. */
87 .mpu_base = 0x00000000,
88 .mpu_range = 0xFFFFF000,
89 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060090 .mmu = {
91 .config = ADRENO_MMU_CONFIG,
92 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093 .pwrctrl = {
94 .regulator_name = "fs_gfx3d",
95 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 },
97 .mutex = __MUTEX_INITIALIZER(device_3d0.dev.mutex),
98 .state = KGSL_STATE_INIT,
99 .active_cnt = 0,
100 .iomemname = KGSL_3D0_REG_MEMORY,
101 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600103 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
105 .suspend = kgsl_early_suspend_driver,
106 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600108#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 },
110 .gmemspace = {
111 .gpu_base = 0,
112 .sizebytes = SZ_256K,
113 },
114 .pfp_fw = NULL,
115 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700116 .wait_timeout = 10000, /* in milliseconds */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117};
118
Jordan Crouse95b33272011-11-11 14:50:12 -0700119
Jordan Crouse505df9c2011-07-28 08:37:59 -0600120/*
121 * This is the master list of all GPU cores that are supported by this
122 * driver.
123 */
124
125#define ANY_ID (~0)
126
127static const struct {
128 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600129 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600130 const char *pm4fw;
131 const char *pfpfw;
132 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700133 unsigned int istore_size;
134 unsigned int pix_shader_start;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600135} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600136 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700137 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
138 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600139 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700140 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
141 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600142 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700143 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
144 512, 384},
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600145 /*
146 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
147 * a hardware problem.
148 */
149 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700150 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
151 1536, 768 },
Carter Cooperf27ec722011-11-17 15:20:38 -0700152 { ADRENO_REV_A225, 2, 2, 0, 6,
153 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
154 1536, 768 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600155 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700156 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
157 1536, 768 },
Jordan Crouse505df9c2011-07-28 08:37:59 -0600158};
159
Jordan Crouse9f739212011-07-28 08:37:57 -0600160static void adreno_gmeminit(struct adreno_device *adreno_dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161{
162 struct kgsl_device *device = &adreno_dev->dev;
163 union reg_rb_edram_info rb_edram_info;
164 unsigned int gmem_size;
165 unsigned int edram_value = 0;
166
167 /* make sure edram range is aligned to size */
168 BUG_ON(adreno_dev->gmemspace.gpu_base &
169 (adreno_dev->gmemspace.sizebytes - 1));
170
171 /* get edram_size value equivalent */
172 gmem_size = (adreno_dev->gmemspace.sizebytes >> 14);
173 while (gmem_size >>= 1)
174 edram_value++;
175
176 rb_edram_info.val = 0;
177
178 rb_edram_info.f.edram_size = edram_value;
Jordan Crouse9f739212011-07-28 08:37:57 -0600179 rb_edram_info.f.edram_mapping_mode = 0; /* EDRAM_MAP_UPPER */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180
181 /* must be aligned to size */
182 rb_edram_info.f.edram_range = (adreno_dev->gmemspace.gpu_base >> 14);
183
184 adreno_regwrite(device, REG_RB_EDRAM_INFO, rb_edram_info.val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185}
186
Jordan Crouse9f739212011-07-28 08:37:57 -0600187static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188{
Jordan Crousea78c9172011-07-11 13:14:09 -0600189 irqreturn_t result;
190 struct kgsl_device *device = data;
191 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192
Jordan Crousea78c9172011-07-11 13:14:09 -0600193 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
195 if (device->requested_state == KGSL_STATE_NONE) {
196 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700197 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 queue_work(device->work_queue, &device->idle_check_ws);
199 } else if (device->pwrscale.policy != NULL) {
200 queue_work(device->work_queue, &device->idle_check_ws);
201 }
202 }
203
204 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800205 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 jiffies + device->pwrctrl.interval_timeout);
207 return result;
208}
209
Jordan Crouse9f739212011-07-28 08:37:57 -0600210static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 struct kgsl_pagetable *pagetable)
212{
213 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
214 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
215
216 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
217
218 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
219
220 kgsl_mmu_unmap(pagetable, &device->memstore);
221
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600222 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223}
224
225static int adreno_setup_pt(struct kgsl_device *device,
226 struct kgsl_pagetable *pagetable)
227{
228 int result = 0;
229 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
230 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
231
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
233 GSL_PT_PAGE_RV);
234 if (result)
235 goto error;
236
237 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
238 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
239 if (result)
240 goto unmap_buffer_desc;
241
242 result = kgsl_mmu_map_global(pagetable, &device->memstore,
243 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
244 if (result)
245 goto unmap_memptrs_desc;
246
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600247 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
249 if (result)
250 goto unmap_memstore_desc;
251
252 return result;
253
254unmap_memstore_desc:
255 kgsl_mmu_unmap(pagetable, &device->memstore);
256
257unmap_memptrs_desc:
258 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
259
260unmap_buffer_desc:
261 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
262
263error:
264 return result;
265}
266
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600267static void adreno_setstate(struct kgsl_device *device,
268 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269{
270 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
271 unsigned int link[32];
272 unsigned int *cmds = &link[0];
273 int sizedwords = 0;
274 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
275
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600276 /*
277 * If possible, then set the state via the command stream to avoid
278 * a CPU idle. Otherwise, use the default setstate which uses register
279 * writes For CFF dump we must idle and use the registers so that it is
280 * easier to filter out the mmu accesses from the dump
281 */
282 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
284 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600285 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 *cmds++ = 0x00000000;
287
288 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600289 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600290 *cmds++ = kgsl_pt_get_base_addr(
291 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 sizedwords += 4;
293 }
294
295 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
296 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600297 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 1);
299 *cmds++ = 0x00000000;
300 sizedwords += 2;
301 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600302 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 *cmds++ = mh_mmu_invalidate;
304 sizedwords += 2;
305 }
306
307 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600308 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700309 /* HW workaround: to resolve MMU page fault interrupts
310 * caused by the VGT.It prevents the CP PFP from filling
311 * the VGT DMA request fifo too early,thereby ensuring
312 * that the VGT will not fetch vertex/bin data until
313 * after the page table base register has been updated.
314 *
315 * Two null DRAW_INDX_BIN packets are inserted right
316 * after the page table base update, followed by a
317 * wait for idle. The null packets will fill up the
318 * VGT DMA request fifo and prevent any further
319 * vertex/bin updates from occurring until the wait
320 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600321 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 *cmds++ = (0x4 << 16) |
323 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
324 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600325 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600326 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600327 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 *cmds++ = 0; /* viz query info */
329 *cmds++ = 0x0003C004; /* draw indicator */
330 *cmds++ = 0; /* bin base */
331 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600332 *cmds++ =
333 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600335 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336 *cmds++ = 0; /* viz query info */
337 *cmds++ = 0x0003C004; /* draw indicator */
338 *cmds++ = 0; /* bin base */
339 *cmds++ = 3; /* bin size */
340 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600341 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600343 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 *cmds++ = 0x00000000;
345 sizedwords += 21;
346 }
347
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600348
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700349 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600350 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351 *cmds++ = 0x7fff; /* invalidate all base pointers */
352 sizedwords += 2;
353 }
354
355 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
356 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600357 } else {
358 kgsl_mmu_device_setstate(device, flags);
359 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360}
361
362static unsigned int
363adreno_getchipid(struct kgsl_device *device)
364{
365 unsigned int chipid = 0;
366 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700367 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368
369 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
370 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
371 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
372
373 /*
374 * adreno 22x gpus are indicated by coreid 2,
375 * but REG_RBBM_PERIPHID1 always contains 0 for this field
376 */
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700377 if (cpu_is_msm8960() || cpu_is_msm8x60() || cpu_is_msm8930())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378 chipid = 2 << 24;
379 else
380 chipid = (coreid & 0xF) << 24;
381
382 chipid |= ((majorid >> 4) & 0xF) << 16;
383
384 minorid = ((revid >> 0) & 0xFF);
385
386 patchid = ((revid >> 16) & 0xFF);
387
388 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700389 /* 8960v3 returns 5 for patch release, but it should be 6 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390 if (cpu_is_qsd8x50())
391 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700392 else if (cpu_is_msm8960() &&
393 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
394 patchid = 6;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395
396 chipid |= (minorid << 8) | patchid;
397
398 return chipid;
399}
400
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401static inline bool _rev_match(unsigned int id, unsigned int entry)
402{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600403 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405
406static void
407adreno_identify_gpu(struct adreno_device *adreno_dev)
408{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600409 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410
411 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
412
413 core = (adreno_dev->chip_id >> 24) & 0xff;
414 major = (adreno_dev->chip_id >> 16) & 0xff;
415 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600416 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417
Jordan Crouse505df9c2011-07-28 08:37:59 -0600418 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
419 if (core == adreno_gpulist[i].core &&
420 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600421 _rev_match(minor, adreno_gpulist[i].minor) &&
422 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424 }
425
Jordan Crouse505df9c2011-07-28 08:37:59 -0600426 if (i == ARRAY_SIZE(adreno_gpulist)) {
427 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
428 return;
429 }
430
431 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
432 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
433 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
434 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700435 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
436 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437}
438
439static int __devinit
440adreno_probe(struct platform_device *pdev)
441{
442 struct kgsl_device *device;
443 struct adreno_device *adreno_dev;
444 int status = -EINVAL;
445
446 device = (struct kgsl_device *)pdev->id_entry->driver_data;
447 adreno_dev = ADRENO_DEVICE(device);
448 device->parentdev = &pdev->dev;
449
450 init_completion(&device->recovery_gate);
451
452 status = adreno_ringbuffer_init(device);
453 if (status != 0)
454 goto error;
455
456 status = kgsl_device_platform_probe(device, adreno_isr);
457 if (status)
458 goto error_close_rb;
459
460 adreno_debugfs_init(device);
461
462 kgsl_pwrscale_init(device);
463 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
464
465 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
466 return 0;
467
468error_close_rb:
469 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
470error:
471 device->parentdev = NULL;
472 return status;
473}
474
475static int __devexit adreno_remove(struct platform_device *pdev)
476{
477 struct kgsl_device *device;
478 struct adreno_device *adreno_dev;
479
480 device = (struct kgsl_device *)pdev->id_entry->driver_data;
481 adreno_dev = ADRENO_DEVICE(device);
482
483 kgsl_pwrscale_detach_policy(device);
484 kgsl_pwrscale_close(device);
485
486 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
487 kgsl_device_platform_remove(device);
488
489 return 0;
490}
491
492static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
493{
494 int status = -EINVAL;
495 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
496 int init_reftimestamp = 0x7fffffff;
497
Jeremy Gebben388c2972011-12-16 09:05:07 -0700498 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499
500 /* Power up the device */
501 kgsl_pwrctrl_enable(device);
502
503 /* Identify the specific GPU */
504 adreno_identify_gpu(adreno_dev);
505
Jordan Crouse505df9c2011-07-28 08:37:59 -0600506 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
507 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
508 adreno_dev->chip_id);
509 goto error_clk_off;
510 }
511
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600512 if (adreno_is_a20x(adreno_dev)) {
513 /*
514 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
515 * on older gpus
516 */
517 device->mh.mh_intf_cfg1 = 0;
518 device->mh.mh_intf_cfg2 = 0;
519 }
520
521 kgsl_mh_start(device);
522
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 if (kgsl_mmu_start(device))
524 goto error_clk_off;
525
526 /*We need to make sure all blocks are powered up and clocked before
527 *issuing a soft reset. The overrides will then be turned off (set to 0)
528 */
529 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0xfffffffe);
530 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0xffffffff);
531
532 /* Only reset CP block if all blocks have previously been reset */
533 if (!(device->flags & KGSL_FLAGS_SOFT_RESET) ||
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600534 !adreno_is_a22x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0xFFFFFFFF);
536 device->flags |= KGSL_FLAGS_SOFT_RESET;
537 } else
538 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000001);
539
540 /* The core is in an indeterminate state until the reset completes
541 * after 30ms.
542 */
543 msleep(30);
544
545 adreno_regwrite(device, REG_RBBM_SOFT_RESET, 0x00000000);
546
547 adreno_regwrite(device, REG_RBBM_CNTL, 0x00004442);
548
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600549 if (adreno_is_a225(adreno_dev)) {
550 /* Enable large instruction store for A225 */
551 adreno_regwrite(device, REG_SQ_FLOW_CONTROL, 0x18000000);
552 }
553
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 adreno_regwrite(device, REG_SQ_VS_PROGRAM, 0x00000000);
555 adreno_regwrite(device, REG_SQ_PS_PROGRAM, 0x00000000);
556
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700557 if (cpu_is_msm8960() || cpu_is_msm8930())
Tarun Karra96a12672011-09-23 18:52:39 -0700558 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0x200);
559 else
560 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE1, 0);
561
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600562 if (!adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0);
564 else
565 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, 0x80);
566
Sushmita Susheelendraf3896062011-08-12 16:33:10 -0600567 kgsl_sharedmem_set(&device->memstore, 0, 0, device->memstore.size);
568
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569 kgsl_sharedmem_writel(&device->memstore,
570 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
571 init_reftimestamp);
572
Ranjhith Kalisamyf81dcd02011-09-30 16:45:24 +0530573 adreno_regwrite(device, REG_RBBM_DEBUG, 0x00080000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574
575 /* Make sure interrupts are disabled */
576
577 adreno_regwrite(device, REG_RBBM_INT_CNTL, 0);
578 adreno_regwrite(device, REG_CP_INT_CNTL, 0);
579 adreno_regwrite(device, REG_SQ_INT_CNTL, 0);
580
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600581 if (adreno_is_a22x(adreno_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 adreno_dev->gmemspace.sizebytes = SZ_512K;
583 else
584 adreno_dev->gmemspace.sizebytes = SZ_256K;
585 adreno_gmeminit(adreno_dev);
586
587 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700588 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589
590 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
591 if (status != 0)
592 goto error_irq_off;
593
594 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
595 return status;
596
597error_irq_off:
598 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600599 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600error_clk_off:
601 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 return status;
604}
605
606static int adreno_stop(struct kgsl_device *device)
607{
608 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
609
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610 adreno_dev->drawctxt_active = NULL;
611
612 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
613
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 kgsl_mmu_stop(device);
615
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700616 device->ftbl->irqctrl(device, 0);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600617
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618 /* Power down the device */
619 kgsl_pwrctrl_disable(device);
620
621 return 0;
622}
623
624static int
625adreno_recover_hang(struct kgsl_device *device)
626{
627 int ret;
628 unsigned int *rb_buffer;
629 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
630 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
631 unsigned int timestamp;
632 unsigned int num_rb_contents;
633 unsigned int bad_context;
634 unsigned int reftimestamp;
635 unsigned int enable_ts;
636 unsigned int soptimestamp;
637 unsigned int eoptimestamp;
638 struct adreno_context *drawctxt;
639
640 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
641 rb_buffer = vmalloc(rb->buffer_desc.size);
642 if (!rb_buffer) {
643 KGSL_MEM_ERR(device,
644 "Failed to allocate memory for recovery: %x\n",
645 rb->buffer_desc.size);
646 return -ENOMEM;
647 }
648 /* Extract valid contents from rb which can stil be executed after
649 * hang */
650 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
651 if (ret)
652 goto done;
653 timestamp = rb->timestamp;
654 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
655 kgsl_sharedmem_readl(&device->memstore, &bad_context,
656 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
657 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
658 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
659 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
660 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
661 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
662 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
663 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
664 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
665 /* Make sure memory is synchronized before restarting the GPU */
666 mb();
667 KGSL_CTXT_ERR(device,
668 "Context that caused a GPU hang: %x\n", bad_context);
669 /* restart device */
670 ret = adreno_stop(device);
671 if (ret)
672 goto done;
673 ret = adreno_start(device, true);
674 if (ret)
675 goto done;
676 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
677 /* Restore timestamp states */
678 kgsl_sharedmem_writel(&device->memstore,
679 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
680 soptimestamp);
681 kgsl_sharedmem_writel(&device->memstore,
682 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
683 eoptimestamp);
684 kgsl_sharedmem_writel(&device->memstore,
685 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
686 soptimestamp);
687 if (num_rb_contents) {
688 kgsl_sharedmem_writel(&device->memstore,
689 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
690 reftimestamp);
691 kgsl_sharedmem_writel(&device->memstore,
692 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
693 enable_ts);
694 }
695 /* Make sure all writes are posted before the GPU reads them */
696 wmb();
697 /* Mark the invalid context so no more commands are accepted from
698 * that context */
699
700 drawctxt = (struct adreno_context *) bad_context;
701
702 KGSL_CTXT_ERR(device,
703 "Context that caused a GPU hang: %x\n", bad_context);
704
705 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
706
707 /* Restore valid commands in ringbuffer */
708 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
709 rb->timestamp = timestamp;
710done:
711 vfree(rb_buffer);
712 return ret;
713}
714
715static int
716adreno_dump_and_recover(struct kgsl_device *device)
717{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 int result = -ETIMEDOUT;
719
720 if (device->state == KGSL_STATE_HUNG)
721 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700722 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 mutex_unlock(&device->mutex);
724 wait_for_completion(&device->recovery_gate);
725 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700726 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727 result = 0;
728 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700729 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700731 /* Detected a hang */
732
733
734 /*
735 * Trigger an automatic dump of the state to
736 * the console
737 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700739
740 /*
741 * Make a GPU snapshot. For now, do it after the PM dump so we
742 * can at least be sure the PM dump will work as it always has
743 */
744 kgsl_device_snapshot(device, 1);
745
Jeremy Gebben388c2972011-12-16 09:05:07 -0700746 result = adreno_recover_hang(device);
747 if (result)
748 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
749 else
750 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
751 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 }
753done:
754 return result;
755}
756
757static int adreno_getproperty(struct kgsl_device *device,
758 enum kgsl_property_type type,
759 void *value,
760 unsigned int sizebytes)
761{
762 int status = -EINVAL;
763 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
764
765 switch (type) {
766 case KGSL_PROP_DEVICE_INFO:
767 {
768 struct kgsl_devinfo devinfo;
769
770 if (sizebytes != sizeof(devinfo)) {
771 status = -EINVAL;
772 break;
773 }
774
775 memset(&devinfo, 0, sizeof(devinfo));
776 devinfo.device_id = device->id+1;
777 devinfo.chip_id = adreno_dev->chip_id;
778 devinfo.mmu_enabled = kgsl_mmu_enabled();
779 devinfo.gpu_id = adreno_dev->gpurev;
780 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
781 gpu_base;
782 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
783 sizebytes;
784
785 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
786 0) {
787 status = -EFAULT;
788 break;
789 }
790 status = 0;
791 }
792 break;
793 case KGSL_PROP_DEVICE_SHADOW:
794 {
795 struct kgsl_shadowprop shadowprop;
796
797 if (sizebytes != sizeof(shadowprop)) {
798 status = -EINVAL;
799 break;
800 }
801 memset(&shadowprop, 0, sizeof(shadowprop));
802 if (device->memstore.hostptr) {
803 /*NOTE: with mmu enabled, gpuaddr doesn't mean
804 * anything to mmap().
805 */
806 shadowprop.gpuaddr = device->memstore.physaddr;
807 shadowprop.size = device->memstore.size;
808 /* GSL needs this to be set, even if it
809 appears to be meaningless */
810 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
811 }
812 if (copy_to_user(value, &shadowprop,
813 sizeof(shadowprop))) {
814 status = -EFAULT;
815 break;
816 }
817 status = 0;
818 }
819 break;
820 case KGSL_PROP_MMU_ENABLE:
821 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600822 int mmu_prop = kgsl_mmu_enabled();
823
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 if (sizebytes != sizeof(int)) {
825 status = -EINVAL;
826 break;
827 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600828 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 status = -EFAULT;
830 break;
831 }
832 status = 0;
833 }
834 break;
835 case KGSL_PROP_INTERRUPT_WAITS:
836 {
837 int int_waits = 1;
838 if (sizebytes != sizeof(int)) {
839 status = -EINVAL;
840 break;
841 }
842 if (copy_to_user(value, &int_waits, sizeof(int))) {
843 status = -EFAULT;
844 break;
845 }
846 status = 0;
847 }
848 break;
849 default:
850 status = -EINVAL;
851 }
852
853 return status;
854}
855
Lynus Vaz06a9a902011-10-04 19:25:33 +0530856static inline void adreno_poke(struct kgsl_device *device)
857{
858 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
859 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
860}
861
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862/* Caller must hold the device mutex. */
863int adreno_idle(struct kgsl_device *device, unsigned int timeout)
864{
865 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
866 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
867 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530868 unsigned long wait_timeout =
869 msecs_to_jiffies(adreno_dev->wait_timeout);
870 unsigned long wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871
872 kgsl_cffdump_regpoll(device->id, REG_RBBM_STATUS << 2,
873 0x00000000, 0x80000000);
874 /* first, wait until the CP has consumed all the commands in
875 * the ring buffer
876 */
877retry:
878 if (rb->flags & KGSL_FLAGS_STARTED) {
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700879 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 do {
881 GSL_RB_GET_READPTR(rb, &rb->rptr);
882 if (time_after(jiffies, wait_time)) {
883 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
884 rb->rptr, rb->wptr);
885 goto err;
886 }
887 } while (rb->rptr != rb->wptr);
888 }
889
890 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530891 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 while (time_before(jiffies, wait_time)) {
893 adreno_regread(device, REG_RBBM_STATUS, &rbbm_status);
894 if (rbbm_status == 0x110)
895 return 0;
896 }
897
898err:
899 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
900 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530901 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 goto retry;
903 }
904 return -ETIMEDOUT;
905}
906
907static unsigned int adreno_isidle(struct kgsl_device *device)
908{
909 int status = false;
910 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
911 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
912 unsigned int rbbm_status;
913
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700914 WARN_ON(device->state == KGSL_STATE_INIT);
915 /* If the device isn't active, don't force it on. */
916 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917 /* Is the ring buffer is empty? */
918 GSL_RB_GET_READPTR(rb, &rb->rptr);
919 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
920 /* Is the core idle? */
921 adreno_regread(device, REG_RBBM_STATUS,
922 &rbbm_status);
923 if (rbbm_status == 0x110)
924 status = true;
925 }
926 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -0700927 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928 }
929 return status;
930}
931
932/* Caller must hold the device mutex. */
933static int adreno_suspend_context(struct kgsl_device *device)
934{
935 int status = 0;
936 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
937
938 /* switch to NULL ctxt */
939 if (adreno_dev->drawctxt_active != NULL) {
940 adreno_drawctxt_switch(adreno_dev, NULL, 0);
941 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
942 }
943
944 return status;
945}
946
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700947const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
948 unsigned int pt_base,
949 unsigned int gpuaddr,
950 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700951{
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700952 struct kgsl_memdesc *result = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 struct kgsl_mem_entry *entry;
954 struct kgsl_process_private *priv;
955 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
956 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700957 struct kgsl_context *context;
958 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700960 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
961 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700963 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
964 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700966 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
967 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968
969 mutex_lock(&kgsl_driver.process_mutex);
970 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600971 if (!kgsl_mmu_pt_equal(priv->pagetable, pt_base))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973 spin_lock(&priv->mem_lock);
974 entry = kgsl_sharedmem_find_region(priv, gpuaddr,
975 sizeof(unsigned int));
976 if (entry) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700977 result = &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978 spin_unlock(&priv->mem_lock);
979 mutex_unlock(&kgsl_driver.process_mutex);
980 return result;
981 }
982 spin_unlock(&priv->mem_lock);
983 }
984 mutex_unlock(&kgsl_driver.process_mutex);
985
986 BUG_ON(!mutex_is_locked(&device->mutex));
987 list_for_each_entry(entry, &device->memqueue, list) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700988 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) {
989 result = &entry->memdesc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700990 return result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991 }
992
993 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700994
995 while (1) {
996 struct adreno_context *adreno_context = NULL;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700997 context = idr_get_next(&device->context_idr, &next);
998 if (context == NULL)
999 break;
1000
1001 adreno_context = (struct adreno_context *)context->devctxt;
1002
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001003 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1004 struct kgsl_memdesc *desc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001005
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001006 desc = &adreno_context->gpustate;
1007 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1008 result = desc;
1009 return result;
1010 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001011
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001012 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1013 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1014 result = desc;
1015 return result;
1016 }
1017 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001018 next = next + 1;
1019 }
1020
1021 return NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001022
1023}
1024
1025uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1026 unsigned int gpuaddr, unsigned int size)
1027{
1028 const struct kgsl_memdesc *memdesc;
1029
1030 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1031
1032 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033}
1034
1035void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1036 unsigned int *value)
1037{
1038 unsigned int *reg;
1039 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1040 reg = (unsigned int *)(device->regspace.mmio_virt_base
1041 + (offsetwords << 2));
1042
1043 if (!in_interrupt())
1044 kgsl_pre_hwaccess(device);
1045
1046 /*ensure this read finishes before the next one.
1047 * i.e. act like normal readl() */
1048 *value = __raw_readl(reg);
1049 rmb();
1050}
1051
1052void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1053 unsigned int value)
1054{
1055 unsigned int *reg;
1056
1057 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1058
1059 if (!in_interrupt())
1060 kgsl_pre_hwaccess(device);
1061
1062 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1063 reg = (unsigned int *)(device->regspace.mmio_virt_base
1064 + (offsetwords << 2));
1065
1066 /*ensure previous writes post before this one,
1067 * i.e. act like normal writel() */
1068 wmb();
1069 __raw_writel(value, reg);
1070}
1071
1072static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
1073 unsigned int timestamp)
1074{
1075 int status;
1076 unsigned int ref_ts, enableflag;
1077
1078 status = kgsl_check_timestamp(device, timestamp);
1079 if (!status) {
1080 mutex_lock(&device->mutex);
1081 kgsl_sharedmem_readl(&device->memstore, &enableflag,
1082 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
1083 mb();
1084
1085 if (enableflag) {
1086 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
1087 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
1088 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001089 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 kgsl_sharedmem_writel(&device->memstore,
1091 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1092 timestamp);
1093 wmb();
1094 }
1095 } else {
1096 unsigned int cmds[2];
1097 kgsl_sharedmem_writel(&device->memstore,
1098 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1099 timestamp);
1100 enableflag = 1;
1101 kgsl_sharedmem_writel(&device->memstore,
1102 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1103 enableflag);
1104 wmb();
1105 /* submit a dummy packet so that even if all
1106 * commands upto timestamp get executed we will still
1107 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001108 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109 cmds[1] = 0;
1110 adreno_ringbuffer_issuecmds(device, 0, &cmds[0], 2);
1111 }
1112 mutex_unlock(&device->mutex);
1113 }
1114
1115 return status;
1116}
1117
1118/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001119 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120 placing a process in wait q. For conditional interrupts we expect the
1121 process to already be in its wait q when its exit condition checking
1122 function is called.
1123*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001124#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125({ \
1126 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001127 if (io) \
1128 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1129 else \
1130 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001131 __ret; \
1132})
1133
1134/* MUST be called with the device mutex held */
1135static int adreno_waittimestamp(struct kgsl_device *device,
1136 unsigned int timestamp,
1137 unsigned int msecs)
1138{
1139 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001140 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001141 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001143 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301144 int retries;
1145 unsigned int msecs_first;
1146 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001147
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301148 /* Don't wait forever, set a max value for now */
1149 if (msecs == -1)
1150 msecs = adreno_dev->wait_timeout;
1151
Jordan Crousee6239dd2011-11-17 13:39:21 -07001152 if (timestamp_cmp(timestamp, adreno_dev->ringbuffer.timestamp) > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1154 "rb->timestamp: %x\n",
1155 timestamp, adreno_dev->ringbuffer.timestamp);
1156 status = -EINVAL;
1157 goto done;
1158 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159
Lynus Vaz06a9a902011-10-04 19:25:33 +05301160 /* Keep the first timeout as 100msecs before rewriting
1161 * the WPTR. Less visible impact if the WPTR has not
1162 * been updated properly.
1163 */
1164 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1165 msecs_part = (msecs - msecs_first + 3) / 4;
1166 for (retries = 0; retries < 5; retries++) {
1167 if (!kgsl_check_timestamp(device, timestamp)) {
1168 adreno_poke(device);
1169 io_cnt = (io_cnt + 1) % 100;
1170 if (io_cnt <
1171 pwr->pwrlevels[pwr->active_pwrlevel].
1172 io_fraction)
1173 io = 0;
1174 mutex_unlock(&device->mutex);
1175 /* We need to make sure that the process is
1176 * placed in wait-q before its condition is called
1177 */
1178 status = kgsl_wait_event_interruptible_timeout(
1179 device->wait_queue,
1180 kgsl_check_interrupt_timestamp(device,
1181 timestamp),
1182 msecs_to_jiffies(retries ?
1183 msecs_part : msecs_first), io);
1184 mutex_lock(&device->mutex);
1185
1186 if (status > 0) {
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001187 /*completed before the wait finished */
Lynus Vaz06a9a902011-10-04 19:25:33 +05301188 status = 0;
1189 goto done;
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001190 } else if (status < 0) {
1191 /*an error occurred*/
1192 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193 }
Jeremy Gebben3d25b092011-11-29 15:13:15 -07001194 /*this wait timed out*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195 }
1196 }
Lynus Vaz06a9a902011-10-04 19:25:33 +05301197 if (!kgsl_check_timestamp(device, timestamp)) {
1198 status = -ETIMEDOUT;
1199 KGSL_DRV_ERR(device,
1200 "Device hang detected while waiting "
1201 "for timestamp: %x, last "
1202 "submitted(rb->timestamp): %x, wptr: "
1203 "%x\n", timestamp,
1204 adreno_dev->ringbuffer.timestamp,
1205 adreno_dev->ringbuffer.wptr);
1206 if (!adreno_dump_and_recover(device)) {
1207 /* wait for idle after recovery as the
1208 * timestamp that this process wanted
1209 * to wait on may be invalid */
1210 if (!adreno_idle(device,
1211 KGSL_TIMEOUT_DEFAULT))
1212 status = 0;
1213 }
1214 } else {
1215 status = 0;
1216 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001217
1218done:
1219 return (int)status;
1220}
1221
1222static unsigned int adreno_readtimestamp(struct kgsl_device *device,
1223 enum kgsl_timestamp_type type)
1224{
1225 unsigned int timestamp = 0;
1226
1227 if (type == KGSL_TIMESTAMP_CONSUMED)
1228 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1229 else if (type == KGSL_TIMESTAMP_RETIRED)
1230 kgsl_sharedmem_readl(&device->memstore, &timestamp,
1231 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
1232 rmb();
1233
1234 return timestamp;
1235}
1236
1237static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1238 unsigned int cmd, void *data)
1239{
1240 int result = 0;
1241 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1242 struct kgsl_context *context;
1243
1244 switch (cmd) {
1245 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1246 binbase = data;
1247
1248 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1249 if (context) {
1250 adreno_drawctxt_set_bin_base_offset(
1251 dev_priv->device, context, binbase->offset);
1252 } else {
1253 result = -EINVAL;
1254 KGSL_DRV_ERR(dev_priv->device,
1255 "invalid drawctxt drawctxt_id %d "
1256 "device_id=%d\n",
1257 binbase->drawctxt_id, dev_priv->device->id);
1258 }
1259 break;
1260
1261 default:
1262 KGSL_DRV_INFO(dev_priv->device,
1263 "invalid ioctl code %08x\n", cmd);
1264 result = -EINVAL;
1265 break;
1266 }
1267 return result;
1268
1269}
1270
1271static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1272{
1273 gpu_freq /= 1000000;
1274 return ticks / gpu_freq;
1275}
1276
1277static void adreno_power_stats(struct kgsl_device *device,
1278 struct kgsl_power_stats *stats)
1279{
1280 unsigned int reg;
1281 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
1282
1283 /* In order to calculate idle you have to have run the algorithm *
1284 * at least once to get a start time. */
1285 if (pwr->time != 0) {
1286 s64 tmp;
1287 /* Stop the performance moniter and read the current *
1288 * busy cycles. */
1289 adreno_regwrite(device,
1290 REG_CP_PERFMON_CNTL,
1291 REG_PERF_MODE_CNT |
1292 REG_PERF_STATE_FREEZE);
1293 adreno_regread(device, REG_RBBM_PERFCOUNTER1_LO, &reg);
1294 tmp = ktime_to_us(ktime_get());
1295 stats->total_time = tmp - pwr->time;
1296 pwr->time = tmp;
1297 stats->busy_time = adreno_ticks_to_us(reg, device->pwrctrl.
1298 pwrlevels[device->pwrctrl.active_pwrlevel].
1299 gpu_freq);
1300
1301 adreno_regwrite(device,
1302 REG_CP_PERFMON_CNTL,
1303 REG_PERF_MODE_CNT |
1304 REG_PERF_STATE_RESET);
1305 } else {
1306 stats->total_time = 0;
1307 stats->busy_time = 0;
1308 pwr->time = ktime_to_us(ktime_get());
1309 }
1310
1311 /* re-enable the performance moniters */
1312 adreno_regread(device, REG_RBBM_PM_OVERRIDE2, &reg);
1313 adreno_regwrite(device, REG_RBBM_PM_OVERRIDE2, (reg | 0x40));
1314 adreno_regwrite(device, REG_RBBM_PERFCOUNTER1_SELECT, 0x1);
1315 adreno_regwrite(device,
1316 REG_CP_PERFMON_CNTL,
1317 REG_PERF_MODE_CNT | REG_PERF_STATE_ENABLE);
1318}
1319
1320void adreno_irqctrl(struct kgsl_device *device, int state)
1321{
Jordan Crousea78c9172011-07-11 13:14:09 -06001322 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1323 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001324}
1325
Jordan Crousea0758f22011-12-07 11:19:22 -07001326static unsigned int adreno_gpuid(struct kgsl_device *device)
1327{
1328 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1329
1330 /* Standard KGSL gpuid format:
1331 * top word is 0x0002 for 2D or 0x0003 for 3D
1332 * Bottom word is core specific identifer
1333 */
1334
1335 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1336}
1337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001338static const struct kgsl_functable adreno_functable = {
1339 /* Mandatory functions */
1340 .regread = adreno_regread,
1341 .regwrite = adreno_regwrite,
1342 .idle = adreno_idle,
1343 .isidle = adreno_isidle,
1344 .suspend_context = adreno_suspend_context,
1345 .start = adreno_start,
1346 .stop = adreno_stop,
1347 .getproperty = adreno_getproperty,
1348 .waittimestamp = adreno_waittimestamp,
1349 .readtimestamp = adreno_readtimestamp,
1350 .issueibcmds = adreno_ringbuffer_issueibcmds,
1351 .ioctl = adreno_ioctl,
1352 .setup_pt = adreno_setup_pt,
1353 .cleanup_pt = adreno_cleanup_pt,
1354 .power_stats = adreno_power_stats,
1355 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001356 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001357 .snapshot = adreno_snapshot,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001358 /* Optional functions */
1359 .setstate = adreno_setstate,
1360 .drawctxt_create = adreno_drawctxt_create,
1361 .drawctxt_destroy = adreno_drawctxt_destroy,
1362};
1363
1364static struct platform_device_id adreno_id_table[] = {
1365 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1366 { },
1367};
1368MODULE_DEVICE_TABLE(platform, adreno_id_table);
1369
1370static struct platform_driver adreno_platform_driver = {
1371 .probe = adreno_probe,
1372 .remove = __devexit_p(adreno_remove),
1373 .suspend = kgsl_suspend_driver,
1374 .resume = kgsl_resume_driver,
1375 .id_table = adreno_id_table,
1376 .driver = {
1377 .owner = THIS_MODULE,
1378 .name = DEVICE_3D_NAME,
1379 .pm = &kgsl_pm_ops,
1380 }
1381};
1382
1383static int __init kgsl_3d_init(void)
1384{
1385 return platform_driver_register(&adreno_platform_driver);
1386}
1387
1388static void __exit kgsl_3d_exit(void)
1389{
1390 platform_driver_unregister(&adreno_platform_driver);
1391}
1392
1393module_init(kgsl_3d_init);
1394module_exit(kgsl_3d_exit);
1395
1396MODULE_DESCRIPTION("3D Graphics driver");
1397MODULE_VERSION("1.2");
1398MODULE_LICENSE("GPL v2");
1399MODULE_ALIAS("platform:kgsl_3d");