blob: 3db397bce1e9148fc7895b045f89e5b53c215950 [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 */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530135 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600136} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600137 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700138 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530139 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530140 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
141 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530142 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600143 { ADRENO_REV_A205, 0, 1, 0, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700144 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530145 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600146 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700147 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530148 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600149 /*
150 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
151 * a hardware problem.
152 */
153 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700154 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530155 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700156 { ADRENO_REV_A225, 2, 2, 0, 6,
157 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530158 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600159 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700160 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530161 1536, 768, 3, SZ_512K },
162 /* A3XX doesn't use the pix_shader_start */
Jordan Crouse54154c62012-03-27 16:33:26 -0600163 { ADRENO_REV_A305, 3, 0, 5, 0,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530164 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
165 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700166 /* A3XX doesn't use the pix_shader_start */
Jordan Crouse54154c62012-03-27 16:33:26 -0600167 { ADRENO_REV_A320, 3, 2, 0, 0,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700168 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530169 512, 0, 2, SZ_512K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700170
Jordan Crouse505df9c2011-07-28 08:37:59 -0600171};
172
Jordan Crouse9f739212011-07-28 08:37:57 -0600173static irqreturn_t adreno_isr(int irq, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174{
Jordan Crousea78c9172011-07-11 13:14:09 -0600175 irqreturn_t result;
176 struct kgsl_device *device = data;
177 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178
Jordan Crousea78c9172011-07-11 13:14:09 -0600179 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180
181 if (device->requested_state == KGSL_STATE_NONE) {
182 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700183 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184 queue_work(device->work_queue, &device->idle_check_ws);
185 } else if (device->pwrscale.policy != NULL) {
186 queue_work(device->work_queue, &device->idle_check_ws);
187 }
188 }
189
190 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800191 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192 jiffies + device->pwrctrl.interval_timeout);
193 return result;
194}
195
Jordan Crouse9f739212011-07-28 08:37:57 -0600196static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197 struct kgsl_pagetable *pagetable)
198{
199 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
200 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
201
202 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
203
204 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
205
206 kgsl_mmu_unmap(pagetable, &device->memstore);
207
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600208 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209}
210
211static int adreno_setup_pt(struct kgsl_device *device,
212 struct kgsl_pagetable *pagetable)
213{
214 int result = 0;
215 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
216 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
217
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
219 GSL_PT_PAGE_RV);
220 if (result)
221 goto error;
222
223 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
224 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
225 if (result)
226 goto unmap_buffer_desc;
227
228 result = kgsl_mmu_map_global(pagetable, &device->memstore,
229 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
230 if (result)
231 goto unmap_memptrs_desc;
232
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600233 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
235 if (result)
236 goto unmap_memstore_desc;
237
238 return result;
239
240unmap_memstore_desc:
241 kgsl_mmu_unmap(pagetable, &device->memstore);
242
243unmap_memptrs_desc:
244 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
245
246unmap_buffer_desc:
247 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
248
249error:
250 return result;
251}
252
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600253static void adreno_setstate(struct kgsl_device *device,
254 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255{
256 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
257 unsigned int link[32];
258 unsigned int *cmds = &link[0];
259 int sizedwords = 0;
260 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
261
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600262 /*
Jordan Crousee3f80ea2012-02-04 14:22:36 -0700263 * A3XX doesn't support the fast path (the registers don't even exist)
264 * so just bail out early
265 */
266
267 if (adreno_is_a3xx(adreno_dev)) {
268 kgsl_mmu_device_setstate(device, flags);
269 return;
270 }
271
272 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600273 * If possible, then set the state via the command stream to avoid
274 * a CPU idle. Otherwise, use the default setstate which uses register
275 * writes For CFF dump we must idle and use the registers so that it is
276 * easier to filter out the mmu accesses from the dump
277 */
278 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
280 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600281 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282 *cmds++ = 0x00000000;
283
284 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600285 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600286 *cmds++ = kgsl_pt_get_base_addr(
287 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288 sizedwords += 4;
289 }
290
291 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
292 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600293 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294 1);
295 *cmds++ = 0x00000000;
296 sizedwords += 2;
297 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600298 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299 *cmds++ = mh_mmu_invalidate;
300 sizedwords += 2;
301 }
302
303 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600304 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 /* HW workaround: to resolve MMU page fault interrupts
306 * caused by the VGT.It prevents the CP PFP from filling
307 * the VGT DMA request fifo too early,thereby ensuring
308 * that the VGT will not fetch vertex/bin data until
309 * after the page table base register has been updated.
310 *
311 * Two null DRAW_INDX_BIN packets are inserted right
312 * after the page table base update, followed by a
313 * wait for idle. The null packets will fill up the
314 * VGT DMA request fifo and prevent any further
315 * vertex/bin updates from occurring until the wait
316 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600317 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318 *cmds++ = (0x4 << 16) |
319 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
320 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600321 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600322 *cmds++ = device->mmu.setstate_memory.gpuaddr;
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 */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600328 *cmds++ =
329 device->mmu.setstate_memory.gpuaddr; /* dma base */
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_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 *cmds++ = 0; /* viz query info */
333 *cmds++ = 0x0003C004; /* draw indicator */
334 *cmds++ = 0; /* bin base */
335 *cmds++ = 3; /* bin size */
336 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600337 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600339 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 *cmds++ = 0x00000000;
341 sizedwords += 21;
342 }
343
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600344
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600346 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347 *cmds++ = 0x7fff; /* invalidate all base pointers */
348 sizedwords += 2;
349 }
350
351 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
352 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600353 } else {
354 kgsl_mmu_device_setstate(device, flags);
355 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356}
357
358static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700359a3xx_getchipid(struct kgsl_device *device)
360{
Jordan Crouse54154c62012-03-27 16:33:26 -0600361 unsigned int majorid, minorid, patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700362
Jordan Crouse54154c62012-03-27 16:33:26 -0600363 /*
364 * We could detect the chipID from the hardware but it takes multiple
365 * registers to find the right combination. Since we traffic exclusively
366 * in system on chips, we can be (mostly) confident that a SOC version
367 * will match a GPU (at this juncture at least). So do the lazy/quick
368 * thing and set the chip_id based on the SoC
369 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700370
Jordan Crouse54154c62012-03-27 16:33:26 -0600371 if (cpu_is_apq8064()) {
372 /* A320 */
373 majorid = 2;
374 minorid = 0;
375 patchid = 0;
376 } else if (cpu_is_msm8930()) {
377 /* A305 */
378 majorid = 0;
379 minorid = 5;
380 patchid = 0;
381 }
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700382
Jordan Crouse54154c62012-03-27 16:33:26 -0600383 return (0x03 << 24) | (majorid << 16) | (minorid << 8) | patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700384}
385
386static unsigned int
387a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388{
389 unsigned int chipid = 0;
390 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700391 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392
393 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
394 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
395 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
396
397 /*
398 * adreno 22x gpus are indicated by coreid 2,
399 * but REG_RBBM_PERIPHID1 always contains 0 for this field
400 */
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700401 if (cpu_is_msm8960() || cpu_is_msm8x60() || cpu_is_msm8930())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402 chipid = 2 << 24;
403 else
404 chipid = (coreid & 0xF) << 24;
405
406 chipid |= ((majorid >> 4) & 0xF) << 16;
407
408 minorid = ((revid >> 0) & 0xFF);
409
410 patchid = ((revid >> 16) & 0xFF);
411
412 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700413 /* 8960v3 returns 5 for patch release, but it should be 6 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530414 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415 if (cpu_is_qsd8x50())
416 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700417 else if (cpu_is_msm8960() &&
418 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
419 patchid = 6;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530420 else if (cpu_is_msm8625() && minorid == 0)
421 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
423 chipid |= (minorid << 8) | patchid;
424
425 return chipid;
426}
427
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700428static unsigned int
429adreno_getchipid(struct kgsl_device *device)
430{
Sudhakara Rao Tentu8ebb2282012-03-06 14:52:58 +0530431 if (cpu_is_apq8064() || cpu_is_msm8930())
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700432 return a3xx_getchipid(device);
433 else
434 return a2xx_getchipid(device);
435}
436
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437static inline bool _rev_match(unsigned int id, unsigned int entry)
438{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600439 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441
442static void
443adreno_identify_gpu(struct adreno_device *adreno_dev)
444{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600445 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446
447 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
448
449 core = (adreno_dev->chip_id >> 24) & 0xff;
450 major = (adreno_dev->chip_id >> 16) & 0xff;
451 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600452 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453
Jordan Crouse505df9c2011-07-28 08:37:59 -0600454 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
455 if (core == adreno_gpulist[i].core &&
456 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600457 _rev_match(minor, adreno_gpulist[i].minor) &&
458 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460 }
461
Jordan Crouse505df9c2011-07-28 08:37:59 -0600462 if (i == ARRAY_SIZE(adreno_gpulist)) {
463 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
464 return;
465 }
466
467 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
468 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
469 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
470 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700471 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
472 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700473 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530474 adreno_dev->gmemspace.sizebytes = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475}
476
477static int __devinit
478adreno_probe(struct platform_device *pdev)
479{
480 struct kgsl_device *device;
481 struct adreno_device *adreno_dev;
482 int status = -EINVAL;
483
484 device = (struct kgsl_device *)pdev->id_entry->driver_data;
485 adreno_dev = ADRENO_DEVICE(device);
486 device->parentdev = &pdev->dev;
487
488 init_completion(&device->recovery_gate);
489
490 status = adreno_ringbuffer_init(device);
491 if (status != 0)
492 goto error;
493
494 status = kgsl_device_platform_probe(device, adreno_isr);
495 if (status)
496 goto error_close_rb;
497
498 adreno_debugfs_init(device);
499
500 kgsl_pwrscale_init(device);
501 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
502
503 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
504 return 0;
505
506error_close_rb:
507 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
508error:
509 device->parentdev = NULL;
510 return status;
511}
512
513static int __devexit adreno_remove(struct platform_device *pdev)
514{
515 struct kgsl_device *device;
516 struct adreno_device *adreno_dev;
517
518 device = (struct kgsl_device *)pdev->id_entry->driver_data;
519 adreno_dev = ADRENO_DEVICE(device);
520
521 kgsl_pwrscale_detach_policy(device);
522 kgsl_pwrscale_close(device);
523
524 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
525 kgsl_device_platform_remove(device);
526
527 return 0;
528}
529
530static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
531{
532 int status = -EINVAL;
533 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534
Jeremy Gebben388c2972011-12-16 09:05:07 -0700535 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536
537 /* Power up the device */
538 kgsl_pwrctrl_enable(device);
539
540 /* Identify the specific GPU */
541 adreno_identify_gpu(adreno_dev);
542
Jordan Crouse505df9c2011-07-28 08:37:59 -0600543 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
544 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
545 adreno_dev->chip_id);
546 goto error_clk_off;
547 }
548
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700549 /* Set up the MMU */
550 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600551 /*
552 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
553 * on older gpus
554 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700555 if (adreno_is_a20x(adreno_dev)) {
556 device->mh.mh_intf_cfg1 = 0;
557 device->mh.mh_intf_cfg2 = 0;
558 }
559
560 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600561 }
562
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700563 status = kgsl_mmu_start(device);
564 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 goto error_clk_off;
566
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700567 /* Start the GPU */
568 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569
570 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700571 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572
573 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700574 if (status == 0) {
575 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
576 return 0;
577 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600580 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581error_clk_off:
582 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583
584 return status;
585}
586
587static int adreno_stop(struct kgsl_device *device)
588{
589 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
590
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 adreno_dev->drawctxt_active = NULL;
592
593 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
594
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 kgsl_mmu_stop(device);
596
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700597 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530598 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800599 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600600
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601 /* Power down the device */
602 kgsl_pwrctrl_disable(device);
603
604 return 0;
605}
606
607static int
608adreno_recover_hang(struct kgsl_device *device)
609{
610 int ret;
611 unsigned int *rb_buffer;
612 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
613 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
614 unsigned int timestamp;
615 unsigned int num_rb_contents;
Wei Zouc8c01632012-03-24 17:27:26 -0700616 unsigned int bad_context;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617 unsigned int reftimestamp;
618 unsigned int enable_ts;
619 unsigned int soptimestamp;
620 unsigned int eoptimestamp;
Wei Zouc8c01632012-03-24 17:27:26 -0700621 struct adreno_context *drawctxt;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700622 struct kgsl_context *context;
623 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624
625 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
626 rb_buffer = vmalloc(rb->buffer_desc.size);
627 if (!rb_buffer) {
628 KGSL_MEM_ERR(device,
629 "Failed to allocate memory for recovery: %x\n",
630 rb->buffer_desc.size);
631 return -ENOMEM;
632 }
633 /* Extract valid contents from rb which can stil be executed after
634 * hang */
635 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
636 if (ret)
637 goto done;
Wei Zouc8c01632012-03-24 17:27:26 -0700638 timestamp = rb->timestamp;
639 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
640 kgsl_sharedmem_readl(&device->memstore, &bad_context,
641 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
Wei Zouc8c01632012-03-24 17:27:26 -0700643 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
Wei Zouc8c01632012-03-24 17:27:26 -0700645 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
Wei Zouc8c01632012-03-24 17:27:26 -0700647 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
Wei Zouc8c01632012-03-24 17:27:26 -0700649 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 /* Make sure memory is synchronized before restarting the GPU */
651 mb();
652 KGSL_CTXT_ERR(device,
Wei Zouc8c01632012-03-24 17:27:26 -0700653 "Context that caused a GPU hang: %x\n", bad_context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 /* restart device */
655 ret = adreno_stop(device);
656 if (ret)
657 goto done;
658 ret = adreno_start(device, true);
659 if (ret)
660 goto done;
661 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
662 /* Restore timestamp states */
663 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -0700664 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665 soptimestamp);
666 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -0700667 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668 eoptimestamp);
669 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -0700670 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671 soptimestamp);
672 if (num_rb_contents) {
673 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -0700674 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 reftimestamp);
676 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -0700677 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678 enable_ts);
679 }
680 /* Make sure all writes are posted before the GPU reads them */
681 wmb();
682 /* Mark the invalid context so no more commands are accepted from
683 * that context */
684
Wei Zouc8c01632012-03-24 17:27:26 -0700685 drawctxt = (struct adreno_context *) bad_context;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686
687 KGSL_CTXT_ERR(device,
Wei Zouc8c01632012-03-24 17:27:26 -0700688 "Context that caused a GPU hang: %x\n", bad_context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689
Wei Zouc8c01632012-03-24 17:27:26 -0700690 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700691
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700692 /*
693 * Set the reset status of all contexts to
694 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
695 * since thats the guilty party
696 */
697 while ((context = idr_get_next(&device->context_idr, &next))) {
698 if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
699 context->reset_status) {
Wei Zouc8c01632012-03-24 17:27:26 -0700700 if (context->devctxt != drawctxt)
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700701 context->reset_status =
702 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
703 else
704 context->reset_status =
705 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
706 }
707 next = next + 1;
708 }
709
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 /* Restore valid commands in ringbuffer */
711 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
Wei Zouc8c01632012-03-24 17:27:26 -0700712 rb->timestamp = timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713done:
714 vfree(rb_buffer);
715 return ret;
716}
717
718static int
719adreno_dump_and_recover(struct kgsl_device *device)
720{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721 int result = -ETIMEDOUT;
722
723 if (device->state == KGSL_STATE_HUNG)
724 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700725 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726 mutex_unlock(&device->mutex);
727 wait_for_completion(&device->recovery_gate);
728 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700729 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730 result = 0;
731 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700732 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700734 /* Detected a hang */
735
736
737 /*
738 * Trigger an automatic dump of the state to
739 * the console
740 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700742
743 /*
744 * Make a GPU snapshot. For now, do it after the PM dump so we
745 * can at least be sure the PM dump will work as it always has
746 */
747 kgsl_device_snapshot(device, 1);
748
Jeremy Gebben388c2972011-12-16 09:05:07 -0700749 result = adreno_recover_hang(device);
750 if (result)
751 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
752 else
753 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
754 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755 }
756done:
757 return result;
758}
759
760static int adreno_getproperty(struct kgsl_device *device,
761 enum kgsl_property_type type,
762 void *value,
763 unsigned int sizebytes)
764{
765 int status = -EINVAL;
766 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
767
768 switch (type) {
769 case KGSL_PROP_DEVICE_INFO:
770 {
771 struct kgsl_devinfo devinfo;
772
773 if (sizebytes != sizeof(devinfo)) {
774 status = -EINVAL;
775 break;
776 }
777
778 memset(&devinfo, 0, sizeof(devinfo));
779 devinfo.device_id = device->id+1;
780 devinfo.chip_id = adreno_dev->chip_id;
781 devinfo.mmu_enabled = kgsl_mmu_enabled();
782 devinfo.gpu_id = adreno_dev->gpurev;
783 devinfo.gmem_gpubaseaddr = adreno_dev->gmemspace.
784 gpu_base;
785 devinfo.gmem_sizebytes = adreno_dev->gmemspace.
786 sizebytes;
787
788 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
789 0) {
790 status = -EFAULT;
791 break;
792 }
793 status = 0;
794 }
795 break;
796 case KGSL_PROP_DEVICE_SHADOW:
797 {
798 struct kgsl_shadowprop shadowprop;
799
800 if (sizebytes != sizeof(shadowprop)) {
801 status = -EINVAL;
802 break;
803 }
804 memset(&shadowprop, 0, sizeof(shadowprop));
805 if (device->memstore.hostptr) {
806 /*NOTE: with mmu enabled, gpuaddr doesn't mean
807 * anything to mmap().
808 */
809 shadowprop.gpuaddr = device->memstore.physaddr;
810 shadowprop.size = device->memstore.size;
811 /* GSL needs this to be set, even if it
812 appears to be meaningless */
Wei Zouc8c01632012-03-24 17:27:26 -0700813 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 }
815 if (copy_to_user(value, &shadowprop,
816 sizeof(shadowprop))) {
817 status = -EFAULT;
818 break;
819 }
820 status = 0;
821 }
822 break;
823 case KGSL_PROP_MMU_ENABLE:
824 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600825 int mmu_prop = kgsl_mmu_enabled();
826
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 if (sizebytes != sizeof(int)) {
828 status = -EINVAL;
829 break;
830 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600831 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 status = -EFAULT;
833 break;
834 }
835 status = 0;
836 }
837 break;
838 case KGSL_PROP_INTERRUPT_WAITS:
839 {
840 int int_waits = 1;
841 if (sizebytes != sizeof(int)) {
842 status = -EINVAL;
843 break;
844 }
845 if (copy_to_user(value, &int_waits, sizeof(int))) {
846 status = -EFAULT;
847 break;
848 }
849 status = 0;
850 }
851 break;
852 default:
853 status = -EINVAL;
854 }
855
856 return status;
857}
858
Lynus Vaz06a9a902011-10-04 19:25:33 +0530859static inline void adreno_poke(struct kgsl_device *device)
860{
861 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
862 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
863}
864
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865/* Caller must hold the device mutex. */
866int adreno_idle(struct kgsl_device *device, unsigned int timeout)
867{
868 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
869 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
870 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530871 unsigned long wait_timeout =
872 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +0530873 unsigned long wait_time;
874 unsigned long wait_time_part;
875 unsigned int msecs;
876 unsigned int msecs_first;
877 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700879 kgsl_cffdump_regpoll(device->id,
880 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 0x00000000, 0x80000000);
882 /* first, wait until the CP has consumed all the commands in
883 * the ring buffer
884 */
885retry:
886 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +0530887 msecs = adreno_dev->wait_timeout;
888 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
889 msecs_part = (msecs - msecs_first + 3) / 4;
890 wait_time = jiffies + wait_timeout;
891 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700892 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 do {
Lynus Vaz284d1042012-01-31 16:32:31 +0530894 if (time_after(jiffies, wait_time_part)) {
895 adreno_poke(device);
896 wait_time_part = jiffies +
897 msecs_to_jiffies(msecs_part);
898 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899 GSL_RB_GET_READPTR(rb, &rb->rptr);
900 if (time_after(jiffies, wait_time)) {
901 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
902 rb->rptr, rb->wptr);
903 goto err;
904 }
905 } while (rb->rptr != rb->wptr);
906 }
907
908 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530909 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700911 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
912 &rbbm_status);
913 if (adreno_is_a2xx(adreno_dev)) {
914 if (rbbm_status == 0x110)
915 return 0;
916 } else {
917 if (!(rbbm_status & 0x80000000))
918 return 0;
919 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 }
921
922err:
923 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
924 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530925 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 goto retry;
927 }
928 return -ETIMEDOUT;
929}
930
931static unsigned int adreno_isidle(struct kgsl_device *device)
932{
933 int status = false;
934 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
935 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
936 unsigned int rbbm_status;
937
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700938 WARN_ON(device->state == KGSL_STATE_INIT);
939 /* If the device isn't active, don't force it on. */
940 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941 /* Is the ring buffer is empty? */
942 GSL_RB_GET_READPTR(rb, &rb->rptr);
943 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
944 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700945 adreno_regread(device,
946 adreno_dev->gpudev->reg_rbbm_status,
947 &rbbm_status);
948
949 if (adreno_is_a2xx(adreno_dev)) {
950 if (rbbm_status == 0x110)
951 status = true;
952 } else {
953 if (!(rbbm_status & 0x80000000))
954 status = true;
955 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956 }
957 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -0700958 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959 }
960 return status;
961}
962
963/* Caller must hold the device mutex. */
964static int adreno_suspend_context(struct kgsl_device *device)
965{
966 int status = 0;
967 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
968
969 /* switch to NULL ctxt */
970 if (adreno_dev->drawctxt_active != NULL) {
971 adreno_drawctxt_switch(adreno_dev, NULL, 0);
972 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
973 }
974
975 return status;
976}
977
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700978const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
979 unsigned int pt_base,
980 unsigned int gpuaddr,
981 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982{
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700983 struct kgsl_memdesc *result = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
986 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700987 struct kgsl_context *context;
988 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700990 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
991 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700993 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
994 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700995
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700996 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
997 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998
Jordan Crouse0fdf3a02012-03-16 14:53:41 -0600999 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1000
1001 if (entry)
1002 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001004 while (1) {
1005 struct adreno_context *adreno_context = NULL;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001006 context = idr_get_next(&device->context_idr, &next);
1007 if (context == NULL)
1008 break;
1009
1010 adreno_context = (struct adreno_context *)context->devctxt;
1011
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001012 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1013 struct kgsl_memdesc *desc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001014
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001015 desc = &adreno_context->gpustate;
1016 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1017 result = desc;
1018 return result;
1019 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001020
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001021 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1022 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1023 result = desc;
1024 return result;
1025 }
1026 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001027 next = next + 1;
1028 }
1029
1030 return NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001031
1032}
1033
1034uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1035 unsigned int gpuaddr, unsigned int size)
1036{
1037 const struct kgsl_memdesc *memdesc;
1038
1039 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1040
1041 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042}
1043
1044void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1045 unsigned int *value)
1046{
1047 unsigned int *reg;
1048 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1049 reg = (unsigned int *)(device->regspace.mmio_virt_base
1050 + (offsetwords << 2));
1051
1052 if (!in_interrupt())
1053 kgsl_pre_hwaccess(device);
1054
1055 /*ensure this read finishes before the next one.
1056 * i.e. act like normal readl() */
1057 *value = __raw_readl(reg);
1058 rmb();
1059}
1060
1061void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1062 unsigned int value)
1063{
1064 unsigned int *reg;
1065
1066 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1067
1068 if (!in_interrupt())
1069 kgsl_pre_hwaccess(device);
1070
1071 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1072 reg = (unsigned int *)(device->regspace.mmio_virt_base
1073 + (offsetwords << 2));
1074
1075 /*ensure previous writes post before this one,
1076 * i.e. act like normal writel() */
1077 wmb();
1078 __raw_writel(value, reg);
1079}
1080
1081static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Wei Zouc8c01632012-03-24 17:27:26 -07001082 unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083{
1084 int status;
1085 unsigned int ref_ts, enableflag;
1086
Wei Zouc8c01632012-03-24 17:27:26 -07001087 status = kgsl_check_timestamp(device, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088 if (!status) {
1089 mutex_lock(&device->mutex);
1090 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Wei Zouc8c01632012-03-24 17:27:26 -07001091 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 mb();
1093
1094 if (enableflag) {
1095 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Wei Zouc8c01632012-03-24 17:27:26 -07001096 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001098 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -07001100 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1101 timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102 wmb();
1103 }
1104 } else {
1105 unsigned int cmds[2];
1106 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -07001107 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1108 timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109 enableflag = 1;
1110 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -07001111 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1112 enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113 wmb();
1114 /* submit a dummy packet so that even if all
1115 * commands upto timestamp get executed we will still
1116 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001117 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001119 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1120 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 }
1122 mutex_unlock(&device->mutex);
1123 }
1124
1125 return status;
1126}
1127
1128/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001129 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001130 placing a process in wait q. For conditional interrupts we expect the
1131 process to already be in its wait q when its exit condition checking
1132 function is called.
1133*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001134#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135({ \
1136 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001137 if (io) \
1138 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1139 else \
1140 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001141 __ret; \
1142})
1143
1144/* MUST be called with the device mutex held */
1145static int adreno_waittimestamp(struct kgsl_device *device,
1146 unsigned int timestamp,
1147 unsigned int msecs)
1148{
1149 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001150 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001151 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001153 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301154 int retries;
1155 unsigned int msecs_first;
1156 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301158 /* Don't wait forever, set a max value for now */
1159 if (msecs == -1)
1160 msecs = adreno_dev->wait_timeout;
1161
Wei Zouc8c01632012-03-24 17:27:26 -07001162 if (timestamp_cmp(timestamp, adreno_dev->ringbuffer.timestamp) > 0) {
1163 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1164 "rb->timestamp: %x\n",
1165 timestamp, adreno_dev->ringbuffer.timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001166 status = -EINVAL;
1167 goto done;
1168 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169
Lynus Vaz06a9a902011-10-04 19:25:33 +05301170 /* Keep the first timeout as 100msecs before rewriting
1171 * the WPTR. Less visible impact if the WPTR has not
1172 * been updated properly.
1173 */
1174 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1175 msecs_part = (msecs - msecs_first + 3) / 4;
1176 for (retries = 0; retries < 5; retries++) {
Wei Zouc8c01632012-03-24 17:27:26 -07001177 if (kgsl_check_timestamp(device, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001178 /* if the timestamp happens while we're not
1179 * waiting, there's a chance that an interrupt
1180 * will not be generated and thus the timestamp
1181 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301182 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001183 queue_work(device->work_queue, &device->ts_expired_ws);
1184 status = 0;
1185 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001187 adreno_poke(device);
1188 io_cnt = (io_cnt + 1) % 100;
1189 if (io_cnt <
1190 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1191 io = 0;
1192 mutex_unlock(&device->mutex);
1193 /* We need to make sure that the process is
1194 * placed in wait-q before its condition is called
1195 */
1196 status = kgsl_wait_event_interruptible_timeout(
1197 device->wait_queue,
1198 kgsl_check_interrupt_timestamp(device,
Wei Zouc8c01632012-03-24 17:27:26 -07001199 timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001200 msecs_to_jiffies(retries ?
1201 msecs_part : msecs_first), io);
1202 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001203
Jeremy Gebben63904832012-02-07 16:10:55 -07001204 if (status > 0) {
1205 /*completed before the wait finished */
1206 status = 0;
1207 goto done;
1208 } else if (status < 0) {
1209 /*an error occurred*/
1210 goto done;
1211 }
1212 /*this wait timed out*/
1213 }
1214 status = -ETIMEDOUT;
1215 KGSL_DRV_ERR(device,
Wei Zouc8c01632012-03-24 17:27:26 -07001216 "Device hang detected while waiting for timestamp: %x,"
1217 "last submitted(rb->timestamp): %x, wptr: %x\n",
1218 timestamp, adreno_dev->ringbuffer.timestamp,
Jeremy Gebben63904832012-02-07 16:10:55 -07001219 adreno_dev->ringbuffer.wptr);
1220 if (!adreno_dump_and_recover(device)) {
1221 /* wait for idle after recovery as the
1222 * timestamp that this process wanted
1223 * to wait on may be invalid */
1224 if (!adreno_idle(device, KGSL_TIMEOUT_DEFAULT))
1225 status = 0;
1226 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227done:
1228 return (int)status;
1229}
1230
1231static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Wei Zouc8c01632012-03-24 17:27:26 -07001232 enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233{
1234 unsigned int timestamp = 0;
1235
1236 if (type == KGSL_TIMESTAMP_CONSUMED)
1237 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1238 else if (type == KGSL_TIMESTAMP_RETIRED)
1239 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Wei Zouc8c01632012-03-24 17:27:26 -07001240 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001241 rmb();
1242
1243 return timestamp;
1244}
1245
1246static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1247 unsigned int cmd, void *data)
1248{
1249 int result = 0;
1250 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1251 struct kgsl_context *context;
1252
1253 switch (cmd) {
1254 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1255 binbase = data;
1256
1257 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1258 if (context) {
1259 adreno_drawctxt_set_bin_base_offset(
1260 dev_priv->device, context, binbase->offset);
1261 } else {
1262 result = -EINVAL;
1263 KGSL_DRV_ERR(dev_priv->device,
1264 "invalid drawctxt drawctxt_id %d "
1265 "device_id=%d\n",
1266 binbase->drawctxt_id, dev_priv->device->id);
1267 }
1268 break;
1269
1270 default:
1271 KGSL_DRV_INFO(dev_priv->device,
1272 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001273 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001274 break;
1275 }
1276 return result;
1277
1278}
1279
1280static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1281{
1282 gpu_freq /= 1000000;
1283 return ticks / gpu_freq;
1284}
1285
1286static void adreno_power_stats(struct kgsl_device *device,
1287 struct kgsl_power_stats *stats)
1288{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001289 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001291 unsigned int cycles;
1292
1293 /* Get the busy cycles counted since the counter was last reset */
1294 /* Calling this function also resets and restarts the counter */
1295
1296 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297
1298 /* In order to calculate idle you have to have run the algorithm *
1299 * at least once to get a start time. */
1300 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001301 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 stats->total_time = tmp - pwr->time;
1303 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001304 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305 pwrlevels[device->pwrctrl.active_pwrlevel].
1306 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307 } else {
1308 stats->total_time = 0;
1309 stats->busy_time = 0;
1310 pwr->time = ktime_to_us(ktime_get());
1311 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312}
1313
1314void adreno_irqctrl(struct kgsl_device *device, int state)
1315{
Jordan Crousea78c9172011-07-11 13:14:09 -06001316 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1317 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318}
1319
Jordan Crousea0758f22011-12-07 11:19:22 -07001320static unsigned int adreno_gpuid(struct kgsl_device *device)
1321{
1322 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1323
1324 /* Standard KGSL gpuid format:
1325 * top word is 0x0002 for 2D or 0x0003 for 3D
1326 * Bottom word is core specific identifer
1327 */
1328
1329 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1330}
1331
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001332static const struct kgsl_functable adreno_functable = {
1333 /* Mandatory functions */
1334 .regread = adreno_regread,
1335 .regwrite = adreno_regwrite,
1336 .idle = adreno_idle,
1337 .isidle = adreno_isidle,
1338 .suspend_context = adreno_suspend_context,
1339 .start = adreno_start,
1340 .stop = adreno_stop,
1341 .getproperty = adreno_getproperty,
1342 .waittimestamp = adreno_waittimestamp,
1343 .readtimestamp = adreno_readtimestamp,
1344 .issueibcmds = adreno_ringbuffer_issueibcmds,
1345 .ioctl = adreno_ioctl,
1346 .setup_pt = adreno_setup_pt,
1347 .cleanup_pt = adreno_cleanup_pt,
1348 .power_stats = adreno_power_stats,
1349 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001350 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001351 .snapshot = adreno_snapshot,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001352 /* Optional functions */
1353 .setstate = adreno_setstate,
1354 .drawctxt_create = adreno_drawctxt_create,
1355 .drawctxt_destroy = adreno_drawctxt_destroy,
1356};
1357
1358static struct platform_device_id adreno_id_table[] = {
1359 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1360 { },
1361};
1362MODULE_DEVICE_TABLE(platform, adreno_id_table);
1363
1364static struct platform_driver adreno_platform_driver = {
1365 .probe = adreno_probe,
1366 .remove = __devexit_p(adreno_remove),
1367 .suspend = kgsl_suspend_driver,
1368 .resume = kgsl_resume_driver,
1369 .id_table = adreno_id_table,
1370 .driver = {
1371 .owner = THIS_MODULE,
1372 .name = DEVICE_3D_NAME,
1373 .pm = &kgsl_pm_ops,
1374 }
1375};
1376
1377static int __init kgsl_3d_init(void)
1378{
1379 return platform_driver_register(&adreno_platform_driver);
1380}
1381
1382static void __exit kgsl_3d_exit(void)
1383{
1384 platform_driver_unregister(&adreno_platform_driver);
1385}
1386
1387module_init(kgsl_3d_init);
1388module_exit(kgsl_3d_exit);
1389
1390MODULE_DESCRIPTION("3D Graphics driver");
1391MODULE_VERSION("1.2");
1392MODULE_LICENSE("GPL v2");
1393MODULE_ALIAS("platform:kgsl_3d");