blob: 521564afba475f79573e5cf0b539b9ff6e21da7f [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 */
163 { ADRENO_REV_A305, 3, 1, ANY_ID, ANY_ID,
164 "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 Crouseb4d31bd2012-02-01 22:11:12 -0700167 { ADRENO_REV_A320, 3, 1, ANY_ID, ANY_ID,
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{
361 unsigned int chipid = 0;
362 unsigned int coreid, majorid, minorid, patchid;
363 unsigned int version;
364
365 adreno_regread(device, A3XX_RBBM_HW_VERSION, &version);
366
367 coreid = 0x03;
368
369 /* Version might not be set - if it isn't, assume this is 320 */
370 if (version)
371 majorid = version & 0x0F;
372 else
373 majorid = 1;
374
375 minorid = (version >> 4) & 0xFFF;
376 patchid = 0;
377
378 chipid = (coreid << 24) | (majorid << 16) | (minorid << 8) | patchid;
379
380 return chipid;
381}
382
383static unsigned int
384a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385{
386 unsigned int chipid = 0;
387 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700388 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389
390 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
391 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
392 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
393
394 /*
395 * adreno 22x gpus are indicated by coreid 2,
396 * but REG_RBBM_PERIPHID1 always contains 0 for this field
397 */
Stepan Moskovchenko8eea9cf2011-10-25 14:45:42 -0700398 if (cpu_is_msm8960() || cpu_is_msm8x60() || cpu_is_msm8930())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 chipid = 2 << 24;
400 else
401 chipid = (coreid & 0xF) << 24;
402
403 chipid |= ((majorid >> 4) & 0xF) << 16;
404
405 minorid = ((revid >> 0) & 0xFF);
406
407 patchid = ((revid >> 16) & 0xFF);
408
409 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700410 /* 8960v3 returns 5 for patch release, but it should be 6 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530411 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700412 if (cpu_is_qsd8x50())
413 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700414 else if (cpu_is_msm8960() &&
415 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
416 patchid = 6;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530417 else if (cpu_is_msm8625() && minorid == 0)
418 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419
420 chipid |= (minorid << 8) | patchid;
421
422 return chipid;
423}
424
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700425static unsigned int
426adreno_getchipid(struct kgsl_device *device)
427{
Sudhakara Rao Tentu8ebb2282012-03-06 14:52:58 +0530428 if (cpu_is_apq8064() || cpu_is_msm8930())
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700429 return a3xx_getchipid(device);
430 else
431 return a2xx_getchipid(device);
432}
433
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434static inline bool _rev_match(unsigned int id, unsigned int entry)
435{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600436 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438
439static void
440adreno_identify_gpu(struct adreno_device *adreno_dev)
441{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600442 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443
444 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
445
446 core = (adreno_dev->chip_id >> 24) & 0xff;
447 major = (adreno_dev->chip_id >> 16) & 0xff;
448 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600449 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450
Jordan Crouse505df9c2011-07-28 08:37:59 -0600451 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
452 if (core == adreno_gpulist[i].core &&
453 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600454 _rev_match(minor, adreno_gpulist[i].minor) &&
455 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 }
458
Jordan Crouse505df9c2011-07-28 08:37:59 -0600459 if (i == ARRAY_SIZE(adreno_gpulist)) {
460 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
461 return;
462 }
463
464 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
465 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
466 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
467 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700468 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
469 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700470 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530471 adreno_dev->gmemspace.sizebytes = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472}
473
474static int __devinit
475adreno_probe(struct platform_device *pdev)
476{
477 struct kgsl_device *device;
478 struct adreno_device *adreno_dev;
479 int status = -EINVAL;
480
481 device = (struct kgsl_device *)pdev->id_entry->driver_data;
482 adreno_dev = ADRENO_DEVICE(device);
483 device->parentdev = &pdev->dev;
484
485 init_completion(&device->recovery_gate);
486
487 status = adreno_ringbuffer_init(device);
488 if (status != 0)
489 goto error;
490
491 status = kgsl_device_platform_probe(device, adreno_isr);
492 if (status)
493 goto error_close_rb;
494
495 adreno_debugfs_init(device);
496
497 kgsl_pwrscale_init(device);
498 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
499
500 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
501 return 0;
502
503error_close_rb:
504 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
505error:
506 device->parentdev = NULL;
507 return status;
508}
509
510static int __devexit adreno_remove(struct platform_device *pdev)
511{
512 struct kgsl_device *device;
513 struct adreno_device *adreno_dev;
514
515 device = (struct kgsl_device *)pdev->id_entry->driver_data;
516 adreno_dev = ADRENO_DEVICE(device);
517
518 kgsl_pwrscale_detach_policy(device);
519 kgsl_pwrscale_close(device);
520
521 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
522 kgsl_device_platform_remove(device);
523
524 return 0;
525}
526
527static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
528{
529 int status = -EINVAL;
530 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531
Jeremy Gebben388c2972011-12-16 09:05:07 -0700532 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533
534 /* Power up the device */
535 kgsl_pwrctrl_enable(device);
536
537 /* Identify the specific GPU */
538 adreno_identify_gpu(adreno_dev);
539
Jordan Crouse505df9c2011-07-28 08:37:59 -0600540 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
541 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
542 adreno_dev->chip_id);
543 goto error_clk_off;
544 }
545
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700546 /* Set up the MMU */
547 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600548 /*
549 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
550 * on older gpus
551 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700552 if (adreno_is_a20x(adreno_dev)) {
553 device->mh.mh_intf_cfg1 = 0;
554 device->mh.mh_intf_cfg2 = 0;
555 }
556
557 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600558 }
559
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700560 status = kgsl_mmu_start(device);
561 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562 goto error_clk_off;
563
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700564 /* Start the GPU */
565 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566
567 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700568 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569
570 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700571 if (status == 0) {
572 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
573 return 0;
574 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600577 kgsl_mmu_stop(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578error_clk_off:
579 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
581 return status;
582}
583
584static int adreno_stop(struct kgsl_device *device)
585{
586 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
587
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588 adreno_dev->drawctxt_active = NULL;
589
590 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
591
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592 kgsl_mmu_stop(device);
593
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700594 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530595 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800596 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600597
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598 /* Power down the device */
599 kgsl_pwrctrl_disable(device);
600
601 return 0;
602}
603
604static int
605adreno_recover_hang(struct kgsl_device *device)
606{
607 int ret;
608 unsigned int *rb_buffer;
609 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
610 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
611 unsigned int timestamp;
612 unsigned int num_rb_contents;
Wei Zouc8c01632012-03-24 17:27:26 -0700613 unsigned int bad_context;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 unsigned int reftimestamp;
615 unsigned int enable_ts;
616 unsigned int soptimestamp;
617 unsigned int eoptimestamp;
Wei Zouc8c01632012-03-24 17:27:26 -0700618 struct adreno_context *drawctxt;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700619 struct kgsl_context *context;
620 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621
622 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
623 rb_buffer = vmalloc(rb->buffer_desc.size);
624 if (!rb_buffer) {
625 KGSL_MEM_ERR(device,
626 "Failed to allocate memory for recovery: %x\n",
627 rb->buffer_desc.size);
628 return -ENOMEM;
629 }
630 /* Extract valid contents from rb which can stil be executed after
631 * hang */
632 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
633 if (ret)
634 goto done;
Wei Zouc8c01632012-03-24 17:27:26 -0700635 timestamp = rb->timestamp;
636 KGSL_DRV_ERR(device, "Last issued timestamp: %x\n", timestamp);
637 kgsl_sharedmem_readl(&device->memstore, &bad_context,
638 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
Wei Zouc8c01632012-03-24 17:27:26 -0700640 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
Wei Zouc8c01632012-03-24 17:27:26 -0700642 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
Wei Zouc8c01632012-03-24 17:27:26 -0700644 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
Wei Zouc8c01632012-03-24 17:27:26 -0700646 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647 /* Make sure memory is synchronized before restarting the GPU */
648 mb();
649 KGSL_CTXT_ERR(device,
Wei Zouc8c01632012-03-24 17:27:26 -0700650 "Context that caused a GPU hang: %x\n", bad_context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 /* restart device */
652 ret = adreno_stop(device);
653 if (ret)
654 goto done;
655 ret = adreno_start(device, true);
656 if (ret)
657 goto done;
658 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
659 /* Restore timestamp states */
660 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -0700661 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662 soptimestamp);
663 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -0700664 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665 eoptimestamp);
666 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -0700667 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668 soptimestamp);
669 if (num_rb_contents) {
670 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -0700671 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672 reftimestamp);
673 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -0700674 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 enable_ts);
676 }
677 /* Make sure all writes are posted before the GPU reads them */
678 wmb();
679 /* Mark the invalid context so no more commands are accepted from
680 * that context */
681
Wei Zouc8c01632012-03-24 17:27:26 -0700682 drawctxt = (struct adreno_context *) bad_context;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683
684 KGSL_CTXT_ERR(device,
Wei Zouc8c01632012-03-24 17:27:26 -0700685 "Context that caused a GPU hang: %x\n", bad_context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686
Wei Zouc8c01632012-03-24 17:27:26 -0700687 drawctxt->flags |= CTXT_FLAGS_GPU_HANG;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700689 /*
690 * Set the reset status of all contexts to
691 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
692 * since thats the guilty party
693 */
694 while ((context = idr_get_next(&device->context_idr, &next))) {
695 if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
696 context->reset_status) {
Wei Zouc8c01632012-03-24 17:27:26 -0700697 if (context->devctxt != drawctxt)
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700698 context->reset_status =
699 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
700 else
701 context->reset_status =
702 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
703 }
704 next = next + 1;
705 }
706
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700707 /* Restore valid commands in ringbuffer */
708 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
Wei Zouc8c01632012-03-24 17:27:26 -0700709 rb->timestamp = timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710done:
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 */
Wei Zouc8c01632012-03-24 17:27:26 -0700810 shadowprop.flags = KGSL_FLAGS_INITIALIZED;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 }
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);
Lynus Vaz284d1042012-01-31 16:32:31 +0530870 unsigned long wait_time;
871 unsigned long wait_time_part;
872 unsigned int msecs;
873 unsigned int msecs_first;
874 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700876 kgsl_cffdump_regpoll(device->id,
877 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 0x00000000, 0x80000000);
879 /* first, wait until the CP has consumed all the commands in
880 * the ring buffer
881 */
882retry:
883 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +0530884 msecs = adreno_dev->wait_timeout;
885 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
886 msecs_part = (msecs - msecs_first + 3) / 4;
887 wait_time = jiffies + wait_timeout;
888 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700889 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 do {
Lynus Vaz284d1042012-01-31 16:32:31 +0530891 if (time_after(jiffies, wait_time_part)) {
892 adreno_poke(device);
893 wait_time_part = jiffies +
894 msecs_to_jiffies(msecs_part);
895 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896 GSL_RB_GET_READPTR(rb, &rb->rptr);
897 if (time_after(jiffies, wait_time)) {
898 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
899 rb->rptr, rb->wptr);
900 goto err;
901 }
902 } while (rb->rptr != rb->wptr);
903 }
904
905 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530906 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700908 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
909 &rbbm_status);
910 if (adreno_is_a2xx(adreno_dev)) {
911 if (rbbm_status == 0x110)
912 return 0;
913 } else {
914 if (!(rbbm_status & 0x80000000))
915 return 0;
916 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917 }
918
919err:
920 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
921 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530922 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923 goto retry;
924 }
925 return -ETIMEDOUT;
926}
927
928static unsigned int adreno_isidle(struct kgsl_device *device)
929{
930 int status = false;
931 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
932 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
933 unsigned int rbbm_status;
934
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700935 WARN_ON(device->state == KGSL_STATE_INIT);
936 /* If the device isn't active, don't force it on. */
937 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938 /* Is the ring buffer is empty? */
939 GSL_RB_GET_READPTR(rb, &rb->rptr);
940 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
941 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700942 adreno_regread(device,
943 adreno_dev->gpudev->reg_rbbm_status,
944 &rbbm_status);
945
946 if (adreno_is_a2xx(adreno_dev)) {
947 if (rbbm_status == 0x110)
948 status = true;
949 } else {
950 if (!(rbbm_status & 0x80000000))
951 status = true;
952 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 }
954 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -0700955 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956 }
957 return status;
958}
959
960/* Caller must hold the device mutex. */
961static int adreno_suspend_context(struct kgsl_device *device)
962{
963 int status = 0;
964 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
965
966 /* switch to NULL ctxt */
967 if (adreno_dev->drawctxt_active != NULL) {
968 adreno_drawctxt_switch(adreno_dev, NULL, 0);
969 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
970 }
971
972 return status;
973}
974
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700975const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
976 unsigned int pt_base,
977 unsigned int gpuaddr,
978 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700979{
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700980 struct kgsl_memdesc *result = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
983 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -0700984 struct kgsl_context *context;
985 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700987 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
988 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700990 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
991 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700993 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
994 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700995
Jordan Crouse0fdf3a02012-03-16 14:53:41 -0600996 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
997
998 if (entry)
999 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001001 while (1) {
1002 struct adreno_context *adreno_context = NULL;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001003 context = idr_get_next(&device->context_idr, &next);
1004 if (context == NULL)
1005 break;
1006
1007 adreno_context = (struct adreno_context *)context->devctxt;
1008
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001009 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1010 struct kgsl_memdesc *desc;
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001011
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001012 desc = &adreno_context->gpustate;
1013 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1014 result = desc;
1015 return result;
1016 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001017
Jeremy Gebben775d48b2011-12-12 17:10:19 -07001018 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1019 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size)) {
1020 result = desc;
1021 return result;
1022 }
1023 }
Jeremy Gebbenfaabed72011-11-18 10:03:36 -07001024 next = next + 1;
1025 }
1026
1027 return NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001028
1029}
1030
1031uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1032 unsigned int gpuaddr, unsigned int size)
1033{
1034 const struct kgsl_memdesc *memdesc;
1035
1036 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1037
1038 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039}
1040
1041void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1042 unsigned int *value)
1043{
1044 unsigned int *reg;
1045 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1046 reg = (unsigned int *)(device->regspace.mmio_virt_base
1047 + (offsetwords << 2));
1048
1049 if (!in_interrupt())
1050 kgsl_pre_hwaccess(device);
1051
1052 /*ensure this read finishes before the next one.
1053 * i.e. act like normal readl() */
1054 *value = __raw_readl(reg);
1055 rmb();
1056}
1057
1058void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1059 unsigned int value)
1060{
1061 unsigned int *reg;
1062
1063 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
1064
1065 if (!in_interrupt())
1066 kgsl_pre_hwaccess(device);
1067
1068 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
1069 reg = (unsigned int *)(device->regspace.mmio_virt_base
1070 + (offsetwords << 2));
1071
1072 /*ensure previous writes post before this one,
1073 * i.e. act like normal writel() */
1074 wmb();
1075 __raw_writel(value, reg);
1076}
1077
1078static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Wei Zouc8c01632012-03-24 17:27:26 -07001079 unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080{
1081 int status;
1082 unsigned int ref_ts, enableflag;
1083
Wei Zouc8c01632012-03-24 17:27:26 -07001084 status = kgsl_check_timestamp(device, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085 if (!status) {
1086 mutex_lock(&device->mutex);
1087 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Wei Zouc8c01632012-03-24 17:27:26 -07001088 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 mb();
1090
1091 if (enableflag) {
1092 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Wei Zouc8c01632012-03-24 17:27:26 -07001093 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001095 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -07001097 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1098 timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 wmb();
1100 }
1101 } else {
1102 unsigned int cmds[2];
1103 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -07001104 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts),
1105 timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106 enableflag = 1;
1107 kgsl_sharedmem_writel(&device->memstore,
Wei Zouc8c01632012-03-24 17:27:26 -07001108 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable),
1109 enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110 wmb();
1111 /* submit a dummy packet so that even if all
1112 * commands upto timestamp get executed we will still
1113 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001114 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001115 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001116 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1117 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118 }
1119 mutex_unlock(&device->mutex);
1120 }
1121
1122 return status;
1123}
1124
1125/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001126 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127 placing a process in wait q. For conditional interrupts we expect the
1128 process to already be in its wait q when its exit condition checking
1129 function is called.
1130*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001131#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001132({ \
1133 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001134 if (io) \
1135 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1136 else \
1137 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138 __ret; \
1139})
1140
1141/* MUST be called with the device mutex held */
1142static int adreno_waittimestamp(struct kgsl_device *device,
1143 unsigned int timestamp,
1144 unsigned int msecs)
1145{
1146 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001147 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001148 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001150 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301151 int retries;
1152 unsigned int msecs_first;
1153 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001154
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301155 /* Don't wait forever, set a max value for now */
1156 if (msecs == -1)
1157 msecs = adreno_dev->wait_timeout;
1158
Wei Zouc8c01632012-03-24 17:27:26 -07001159 if (timestamp_cmp(timestamp, adreno_dev->ringbuffer.timestamp) > 0) {
1160 KGSL_DRV_ERR(device, "Cannot wait for invalid ts: %x, "
1161 "rb->timestamp: %x\n",
1162 timestamp, adreno_dev->ringbuffer.timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163 status = -EINVAL;
1164 goto done;
1165 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001166
Lynus Vaz06a9a902011-10-04 19:25:33 +05301167 /* Keep the first timeout as 100msecs before rewriting
1168 * the WPTR. Less visible impact if the WPTR has not
1169 * been updated properly.
1170 */
1171 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1172 msecs_part = (msecs - msecs_first + 3) / 4;
1173 for (retries = 0; retries < 5; retries++) {
Wei Zouc8c01632012-03-24 17:27:26 -07001174 if (kgsl_check_timestamp(device, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001175 /* if the timestamp happens while we're not
1176 * waiting, there's a chance that an interrupt
1177 * will not be generated and thus the timestamp
1178 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301179 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001180 queue_work(device->work_queue, &device->ts_expired_ws);
1181 status = 0;
1182 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001184 adreno_poke(device);
1185 io_cnt = (io_cnt + 1) % 100;
1186 if (io_cnt <
1187 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1188 io = 0;
1189 mutex_unlock(&device->mutex);
1190 /* We need to make sure that the process is
1191 * placed in wait-q before its condition is called
1192 */
1193 status = kgsl_wait_event_interruptible_timeout(
1194 device->wait_queue,
1195 kgsl_check_interrupt_timestamp(device,
Wei Zouc8c01632012-03-24 17:27:26 -07001196 timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001197 msecs_to_jiffies(retries ?
1198 msecs_part : msecs_first), io);
1199 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200
Jeremy Gebben63904832012-02-07 16:10:55 -07001201 if (status > 0) {
1202 /*completed before the wait finished */
1203 status = 0;
1204 goto done;
1205 } else if (status < 0) {
1206 /*an error occurred*/
1207 goto done;
1208 }
1209 /*this wait timed out*/
1210 }
1211 status = -ETIMEDOUT;
1212 KGSL_DRV_ERR(device,
Wei Zouc8c01632012-03-24 17:27:26 -07001213 "Device hang detected while waiting for timestamp: %x,"
1214 "last submitted(rb->timestamp): %x, wptr: %x\n",
1215 timestamp, adreno_dev->ringbuffer.timestamp,
Jeremy Gebben63904832012-02-07 16:10:55 -07001216 adreno_dev->ringbuffer.wptr);
1217 if (!adreno_dump_and_recover(device)) {
1218 /* wait for idle after recovery as the
1219 * timestamp that this process wanted
1220 * to wait on may be invalid */
1221 if (!adreno_idle(device, KGSL_TIMEOUT_DEFAULT))
1222 status = 0;
1223 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224done:
1225 return (int)status;
1226}
1227
1228static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Wei Zouc8c01632012-03-24 17:27:26 -07001229 enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230{
1231 unsigned int timestamp = 0;
1232
1233 if (type == KGSL_TIMESTAMP_CONSUMED)
1234 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
1235 else if (type == KGSL_TIMESTAMP_RETIRED)
1236 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Wei Zouc8c01632012-03-24 17:27:26 -07001237 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238 rmb();
1239
1240 return timestamp;
1241}
1242
1243static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1244 unsigned int cmd, void *data)
1245{
1246 int result = 0;
1247 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1248 struct kgsl_context *context;
1249
1250 switch (cmd) {
1251 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1252 binbase = data;
1253
1254 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1255 if (context) {
1256 adreno_drawctxt_set_bin_base_offset(
1257 dev_priv->device, context, binbase->offset);
1258 } else {
1259 result = -EINVAL;
1260 KGSL_DRV_ERR(dev_priv->device,
1261 "invalid drawctxt drawctxt_id %d "
1262 "device_id=%d\n",
1263 binbase->drawctxt_id, dev_priv->device->id);
1264 }
1265 break;
1266
1267 default:
1268 KGSL_DRV_INFO(dev_priv->device,
1269 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001270 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271 break;
1272 }
1273 return result;
1274
1275}
1276
1277static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1278{
1279 gpu_freq /= 1000000;
1280 return ticks / gpu_freq;
1281}
1282
1283static void adreno_power_stats(struct kgsl_device *device,
1284 struct kgsl_power_stats *stats)
1285{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001286 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001287 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001288 unsigned int cycles;
1289
1290 /* Get the busy cycles counted since the counter was last reset */
1291 /* Calling this function also resets and restarts the counter */
1292
1293 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294
1295 /* In order to calculate idle you have to have run the algorithm *
1296 * at least once to get a start time. */
1297 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001298 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001299 stats->total_time = tmp - pwr->time;
1300 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001301 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 pwrlevels[device->pwrctrl.active_pwrlevel].
1303 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 } else {
1305 stats->total_time = 0;
1306 stats->busy_time = 0;
1307 pwr->time = ktime_to_us(ktime_get());
1308 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309}
1310
1311void adreno_irqctrl(struct kgsl_device *device, int state)
1312{
Jordan Crousea78c9172011-07-11 13:14:09 -06001313 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1314 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315}
1316
Jordan Crousea0758f22011-12-07 11:19:22 -07001317static unsigned int adreno_gpuid(struct kgsl_device *device)
1318{
1319 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1320
1321 /* Standard KGSL gpuid format:
1322 * top word is 0x0002 for 2D or 0x0003 for 3D
1323 * Bottom word is core specific identifer
1324 */
1325
1326 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1327}
1328
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329static const struct kgsl_functable adreno_functable = {
1330 /* Mandatory functions */
1331 .regread = adreno_regread,
1332 .regwrite = adreno_regwrite,
1333 .idle = adreno_idle,
1334 .isidle = adreno_isidle,
1335 .suspend_context = adreno_suspend_context,
1336 .start = adreno_start,
1337 .stop = adreno_stop,
1338 .getproperty = adreno_getproperty,
1339 .waittimestamp = adreno_waittimestamp,
1340 .readtimestamp = adreno_readtimestamp,
1341 .issueibcmds = adreno_ringbuffer_issueibcmds,
1342 .ioctl = adreno_ioctl,
1343 .setup_pt = adreno_setup_pt,
1344 .cleanup_pt = adreno_cleanup_pt,
1345 .power_stats = adreno_power_stats,
1346 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001347 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001348 .snapshot = adreno_snapshot,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001349 /* Optional functions */
1350 .setstate = adreno_setstate,
1351 .drawctxt_create = adreno_drawctxt_create,
1352 .drawctxt_destroy = adreno_drawctxt_destroy,
1353};
1354
1355static struct platform_device_id adreno_id_table[] = {
1356 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1357 { },
1358};
1359MODULE_DEVICE_TABLE(platform, adreno_id_table);
1360
1361static struct platform_driver adreno_platform_driver = {
1362 .probe = adreno_probe,
1363 .remove = __devexit_p(adreno_remove),
1364 .suspend = kgsl_suspend_driver,
1365 .resume = kgsl_resume_driver,
1366 .id_table = adreno_id_table,
1367 .driver = {
1368 .owner = THIS_MODULE,
1369 .name = DEVICE_3D_NAME,
1370 .pm = &kgsl_pm_ops,
1371 }
1372};
1373
1374static int __init kgsl_3d_init(void)
1375{
1376 return platform_driver_register(&adreno_platform_driver);
1377}
1378
1379static void __exit kgsl_3d_exit(void)
1380{
1381 platform_driver_unregister(&adreno_platform_driver);
1382}
1383
1384module_init(kgsl_3d_init);
1385module_exit(kgsl_3d_exit);
1386
1387MODULE_DESCRIPTION("3D Graphics driver");
1388MODULE_VERSION("1.2");
1389MODULE_LICENSE("GPL v2");
1390MODULE_ALIAS("platform:kgsl_3d");