blob: 96390acca23334bb2b53828b1060fd6dd4691638 [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 = {
Jeremy Gebben84d75d02012-03-01 14:47:45 -070072 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070073 .name = DEVICE_3D0_NAME,
74 .id = KGSL_DEVICE_3D0,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060075 .mh = {
76 .mharb = ADRENO_CFG_MHARB,
77 /* Remove 1k boundary check in z470 to avoid a GPU
78 * hang. Notice that this solution won't work if
79 * both EBI and SMI are used
80 */
81 .mh_intf_cfg1 = 0x00032f07,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082 /* turn off memory protection unit by setting
83 acceptable physical address range to include
84 all pages. */
85 .mpu_base = 0x00000000,
86 .mpu_range = 0xFFFFF000,
87 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060088 .mmu = {
89 .config = ADRENO_MMU_CONFIG,
90 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091 .pwrctrl = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092 .irq_name = KGSL_3D0_IRQ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094 .iomemname = KGSL_3D0_REG_MEMORY,
95 .ftbl = &adreno_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -060097 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
99 .suspend = kgsl_early_suspend_driver,
100 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600102#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103 },
Jordan Crouse7501d452012-04-19 08:58:44 -0600104 .gmem_base = 0,
105 .gmem_size = SZ_256K,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 .pfp_fw = NULL,
107 .pm4_fw = NULL,
Jordan Crouse95b33272011-11-11 14:50:12 -0700108 .wait_timeout = 10000, /* in milliseconds */
Jeremy Gebbend0ab6ad2012-04-06 11:13:35 -0600109 .ib_check_level = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110};
111
Jordan Crouse95b33272011-11-11 14:50:12 -0700112
Jordan Crouse505df9c2011-07-28 08:37:59 -0600113/*
114 * This is the master list of all GPU cores that are supported by this
115 * driver.
116 */
117
118#define ANY_ID (~0)
119
120static const struct {
121 enum adreno_gpurev gpurev;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600122 unsigned int core, major, minor, patchid;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600123 const char *pm4fw;
124 const char *pfpfw;
125 struct adreno_gpudev *gpudev;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700126 unsigned int istore_size;
127 unsigned int pix_shader_start;
Jordan Crousec6b3a992012-02-04 10:23:51 -0700128 unsigned int instruction_size; /* Size of an instruction in dwords */
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530129 unsigned int gmem_size; /* size of gmem for gpu*/
Jordan Crouse505df9c2011-07-28 08:37:59 -0600130} adreno_gpulist[] = {
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600131 { ADRENO_REV_A200, 0, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700132 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530133 512, 384, 3, SZ_256K },
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530134 { ADRENO_REV_A203, 0, 1, 1, ANY_ID,
135 "yamato_pm4.fw", "yamato_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530136 512, 384, 3, SZ_256K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600137 { ADRENO_REV_A205, 0, 1, 0, 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 },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600140 { ADRENO_REV_A220, 2, 1, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700141 "leia_pm4_470.fw", "leia_pfp_470.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530142 512, 384, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600143 /*
144 * patchlevel 5 (8960v2) needs special pm4 firmware to work around
145 * a hardware problem.
146 */
147 { ADRENO_REV_A225, 2, 2, 0, 5,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700148 "a225p5_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530149 1536, 768, 3, SZ_512K },
Carter Cooperf27ec722011-11-17 15:20:38 -0700150 { ADRENO_REV_A225, 2, 2, 0, 6,
151 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530152 1536, 768, 3, SZ_512K },
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600153 { ADRENO_REV_A225, 2, 2, ANY_ID, ANY_ID,
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700154 "a225_pm4.fw", "a225_pfp.fw", &adreno_a2xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530155 1536, 768, 3, SZ_512K },
156 /* A3XX doesn't use the pix_shader_start */
Jordan Crouse54154c62012-03-27 16:33:26 -0600157 { ADRENO_REV_A305, 3, 0, 5, 0,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530158 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
159 512, 0, 2, SZ_256K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700160 /* A3XX doesn't use the pix_shader_start */
Jordan Crouse54154c62012-03-27 16:33:26 -0600161 { ADRENO_REV_A320, 3, 2, 0, 0,
Jordan Crousec6b3a992012-02-04 10:23:51 -0700162 "a300_pm4.fw", "a300_pfp.fw", &adreno_a3xx_gpudev,
Sudhakara Rao Tentu79853832012-03-06 15:52:38 +0530163 512, 0, 2, SZ_512K },
Jordan Crousec6b3a992012-02-04 10:23:51 -0700164
Jordan Crouse505df9c2011-07-28 08:37:59 -0600165};
166
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600167static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168{
Jordan Crousea78c9172011-07-11 13:14:09 -0600169 irqreturn_t result;
Jordan Crousea78c9172011-07-11 13:14:09 -0600170 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171
Jordan Crousea78c9172011-07-11 13:14:09 -0600172 result = adreno_dev->gpudev->irq_handler(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173
174 if (device->requested_state == KGSL_STATE_NONE) {
175 if (device->pwrctrl.nap_allowed == true) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700176 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 queue_work(device->work_queue, &device->idle_check_ws);
178 } else if (device->pwrscale.policy != NULL) {
179 queue_work(device->work_queue, &device->idle_check_ws);
180 }
181 }
182
183 /* Reset the time-out in our idle timer */
Tarun Karra68755762012-01-12 16:07:09 -0800184 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185 jiffies + device->pwrctrl.interval_timeout);
186 return result;
187}
188
Jordan Crouse9f739212011-07-28 08:37:57 -0600189static void adreno_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190 struct kgsl_pagetable *pagetable)
191{
192 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
193 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
194
195 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
196
197 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
198
199 kgsl_mmu_unmap(pagetable, &device->memstore);
200
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600201 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202}
203
204static int adreno_setup_pt(struct kgsl_device *device,
205 struct kgsl_pagetable *pagetable)
206{
207 int result = 0;
208 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
209 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
210
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 result = kgsl_mmu_map_global(pagetable, &rb->buffer_desc,
212 GSL_PT_PAGE_RV);
213 if (result)
214 goto error;
215
216 result = kgsl_mmu_map_global(pagetable, &rb->memptrs_desc,
217 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
218 if (result)
219 goto unmap_buffer_desc;
220
221 result = kgsl_mmu_map_global(pagetable, &device->memstore,
222 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
223 if (result)
224 goto unmap_memptrs_desc;
225
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600226 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
228 if (result)
229 goto unmap_memstore_desc;
230
231 return result;
232
233unmap_memstore_desc:
234 kgsl_mmu_unmap(pagetable, &device->memstore);
235
236unmap_memptrs_desc:
237 kgsl_mmu_unmap(pagetable, &rb->memptrs_desc);
238
239unmap_buffer_desc:
240 kgsl_mmu_unmap(pagetable, &rb->buffer_desc);
241
242error:
243 return result;
244}
245
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600246static void adreno_setstate(struct kgsl_device *device,
247 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248{
249 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
250 unsigned int link[32];
251 unsigned int *cmds = &link[0];
252 int sizedwords = 0;
253 unsigned int mh_mmu_invalidate = 0x00000003; /*invalidate all and tc */
254
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600255 /*
Jordan Crousee3f80ea2012-02-04 14:22:36 -0700256 * A3XX doesn't support the fast path (the registers don't even exist)
257 * so just bail out early
258 */
259
260 if (adreno_is_a3xx(adreno_dev)) {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600261 kgsl_mmu_device_setstate(&device->mmu, flags);
Jordan Crousee3f80ea2012-02-04 14:22:36 -0700262 return;
263 }
264
265 /*
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600266 * If possible, then set the state via the command stream to avoid
267 * a CPU idle. Otherwise, use the default setstate which uses register
268 * writes For CFF dump we must idle and use the registers so that it is
269 * easier to filter out the mmu accesses from the dump
270 */
271 if (!kgsl_cff_dump_enable && adreno_dev->drawctxt_active) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 if (flags & KGSL_MMUFLAGS_PTUPDATE) {
273 /* wait for graphics pipe to be idle */
Jordan Crouse084427d2011-07-28 08:37:58 -0600274 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 *cmds++ = 0x00000000;
276
277 /* set page table base */
Jordan Crouse084427d2011-07-28 08:37:58 -0600278 *cmds++ = cp_type0_packet(MH_MMU_PT_BASE, 1);
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600279 *cmds++ = kgsl_mmu_pt_get_base_addr(
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600280 device->mmu.hwpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 sizedwords += 4;
282 }
283
284 if (flags & KGSL_MMUFLAGS_TLBFLUSH) {
285 if (!(flags & KGSL_MMUFLAGS_PTUPDATE)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600286 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 1);
288 *cmds++ = 0x00000000;
289 sizedwords += 2;
290 }
Jordan Crouse084427d2011-07-28 08:37:58 -0600291 *cmds++ = cp_type0_packet(MH_MMU_INVALIDATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 *cmds++ = mh_mmu_invalidate;
293 sizedwords += 2;
294 }
295
296 if (flags & KGSL_MMUFLAGS_PTUPDATE &&
Jeremy Gebben5bb7ece2011-08-02 11:04:48 -0600297 adreno_is_a20x(adreno_dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 /* HW workaround: to resolve MMU page fault interrupts
299 * caused by the VGT.It prevents the CP PFP from filling
300 * the VGT DMA request fifo too early,thereby ensuring
301 * that the VGT will not fetch vertex/bin data until
302 * after the page table base register has been updated.
303 *
304 * Two null DRAW_INDX_BIN packets are inserted right
305 * after the page table base update, followed by a
306 * wait for idle. The null packets will fill up the
307 * VGT DMA request fifo and prevent any further
308 * vertex/bin updates from occurring until the wait
309 * has finished. */
Jordan Crouse084427d2011-07-28 08:37:58 -0600310 *cmds++ = cp_type3_packet(CP_SET_CONSTANT, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 *cmds++ = (0x4 << 16) |
312 (REG_PA_SU_SC_MODE_CNTL - 0x2000);
313 *cmds++ = 0; /* disable faceness generation */
Jordan Crouse084427d2011-07-28 08:37:58 -0600314 *cmds++ = cp_type3_packet(CP_SET_BIN_BASE_OFFSET, 1);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600315 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Jordan Crouse084427d2011-07-28 08:37:58 -0600316 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 *cmds++ = 0; /* viz query info */
318 *cmds++ = 0x0003C004; /* draw indicator */
319 *cmds++ = 0; /* bin base */
320 *cmds++ = 3; /* bin size */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600321 *cmds++ =
322 device->mmu.setstate_memory.gpuaddr; /* dma base */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600324 *cmds++ = cp_type3_packet(CP_DRAW_INDX_BIN, 6);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 *cmds++ = 0; /* viz query info */
326 *cmds++ = 0x0003C004; /* draw indicator */
327 *cmds++ = 0; /* bin base */
328 *cmds++ = 3; /* bin size */
329 /* dma base */
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600330 *cmds++ = device->mmu.setstate_memory.gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 *cmds++ = 6; /* dma size */
Jordan Crouse084427d2011-07-28 08:37:58 -0600332 *cmds++ = cp_type3_packet(CP_WAIT_FOR_IDLE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 *cmds++ = 0x00000000;
334 sizedwords += 21;
335 }
336
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 if (flags & (KGSL_MMUFLAGS_PTUPDATE | KGSL_MMUFLAGS_TLBFLUSH)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600339 *cmds++ = cp_type3_packet(CP_INVALIDATE_STATE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 *cmds++ = 0x7fff; /* invalidate all base pointers */
341 sizedwords += 2;
342 }
343
344 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_PMODE,
345 &link[0], sizedwords);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600346 } else {
Shubhraprakash Das79447952012-04-26 18:12:23 -0600347 kgsl_mmu_device_setstate(&device->mmu, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600348 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700349}
350
351static unsigned int
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700352a3xx_getchipid(struct kgsl_device *device)
353{
Jordan Crouse54154c62012-03-27 16:33:26 -0600354 unsigned int majorid, minorid, patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700355
Jordan Crouse54154c62012-03-27 16:33:26 -0600356 /*
357 * We could detect the chipID from the hardware but it takes multiple
358 * registers to find the right combination. Since we traffic exclusively
359 * in system on chips, we can be (mostly) confident that a SOC version
360 * will match a GPU (at this juncture at least). So do the lazy/quick
361 * thing and set the chip_id based on the SoC
362 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700363
Jordan Crouse54154c62012-03-27 16:33:26 -0600364 if (cpu_is_apq8064()) {
365 /* A320 */
366 majorid = 2;
367 minorid = 0;
368 patchid = 0;
369 } else if (cpu_is_msm8930()) {
370 /* A305 */
371 majorid = 0;
372 minorid = 5;
373 patchid = 0;
374 }
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700375
Jordan Crouse54154c62012-03-27 16:33:26 -0600376 return (0x03 << 24) | (majorid << 16) | (minorid << 8) | patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700377}
378
379static unsigned int
380a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381{
382 unsigned int chipid = 0;
383 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700384 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385
386 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
387 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
388 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
389
390 /*
391 * adreno 22x gpus are indicated by coreid 2,
392 * but REG_RBBM_PERIPHID1 always contains 0 for this field
393 */
Sudhakara Rao Tentudaebac22012-04-02 14:51:29 -0700394 if (cpu_is_msm8960() || cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395 chipid = 2 << 24;
396 else
397 chipid = (coreid & 0xF) << 24;
398
399 chipid |= ((majorid >> 4) & 0xF) << 16;
400
401 minorid = ((revid >> 0) & 0xFF);
402
403 patchid = ((revid >> 16) & 0xFF);
404
405 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700406 /* 8960v3 returns 5 for patch release, but it should be 6 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530407 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 if (cpu_is_qsd8x50())
409 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700410 else if (cpu_is_msm8960() &&
411 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
412 patchid = 6;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530413 else if (cpu_is_msm8625() && minorid == 0)
414 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415
416 chipid |= (minorid << 8) | patchid;
417
418 return chipid;
419}
420
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700421static unsigned int
422adreno_getchipid(struct kgsl_device *device)
423{
Sudhakara Rao Tentu8ebb2282012-03-06 14:52:58 +0530424 if (cpu_is_apq8064() || cpu_is_msm8930())
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700425 return a3xx_getchipid(device);
426 else
427 return a2xx_getchipid(device);
428}
429
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430static inline bool _rev_match(unsigned int id, unsigned int entry)
431{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600432 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434
435static void
436adreno_identify_gpu(struct adreno_device *adreno_dev)
437{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600438 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439
440 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
441
442 core = (adreno_dev->chip_id >> 24) & 0xff;
443 major = (adreno_dev->chip_id >> 16) & 0xff;
444 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600445 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446
Jordan Crouse505df9c2011-07-28 08:37:59 -0600447 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
448 if (core == adreno_gpulist[i].core &&
449 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600450 _rev_match(minor, adreno_gpulist[i].minor) &&
451 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 }
454
Jordan Crouse505df9c2011-07-28 08:37:59 -0600455 if (i == ARRAY_SIZE(adreno_gpulist)) {
456 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
457 return;
458 }
459
460 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
461 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
462 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
463 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700464 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
465 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700466 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600467 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468}
469
470static int __devinit
471adreno_probe(struct platform_device *pdev)
472{
473 struct kgsl_device *device;
474 struct adreno_device *adreno_dev;
475 int status = -EINVAL;
476
477 device = (struct kgsl_device *)pdev->id_entry->driver_data;
478 adreno_dev = ADRENO_DEVICE(device);
479 device->parentdev = &pdev->dev;
480
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 status = adreno_ringbuffer_init(device);
482 if (status != 0)
483 goto error;
484
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600485 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 if (status)
487 goto error_close_rb;
488
489 adreno_debugfs_init(device);
490
491 kgsl_pwrscale_init(device);
492 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
493
494 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
495 return 0;
496
497error_close_rb:
498 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
499error:
500 device->parentdev = NULL;
501 return status;
502}
503
504static int __devexit adreno_remove(struct platform_device *pdev)
505{
506 struct kgsl_device *device;
507 struct adreno_device *adreno_dev;
508
509 device = (struct kgsl_device *)pdev->id_entry->driver_data;
510 adreno_dev = ADRENO_DEVICE(device);
511
512 kgsl_pwrscale_detach_policy(device);
513 kgsl_pwrscale_close(device);
514
515 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
516 kgsl_device_platform_remove(device);
517
518 return 0;
519}
520
521static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
522{
523 int status = -EINVAL;
524 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525
Jeremy Gebben388c2972011-12-16 09:05:07 -0700526 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527
528 /* Power up the device */
529 kgsl_pwrctrl_enable(device);
530
531 /* Identify the specific GPU */
532 adreno_identify_gpu(adreno_dev);
533
Jordan Crouse505df9c2011-07-28 08:37:59 -0600534 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
535 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
536 adreno_dev->chip_id);
537 goto error_clk_off;
538 }
539
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700540 /* Set up the MMU */
541 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600542 /*
543 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
544 * on older gpus
545 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700546 if (adreno_is_a20x(adreno_dev)) {
547 device->mh.mh_intf_cfg1 = 0;
548 device->mh.mh_intf_cfg2 = 0;
549 }
550
551 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600552 }
553
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700554 status = kgsl_mmu_start(device);
555 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556 goto error_clk_off;
557
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700558 /* Start the GPU */
559 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560
561 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700562 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563
564 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700565 if (status == 0) {
566 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
567 return 0;
568 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600571 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572error_clk_off:
573 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574
575 return status;
576}
577
578static int adreno_stop(struct kgsl_device *device)
579{
580 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
581
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 adreno_dev->drawctxt_active = NULL;
583
584 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
585
Shubhraprakash Das79447952012-04-26 18:12:23 -0600586 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700588 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530589 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800590 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600591
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592 /* Power down the device */
593 kgsl_pwrctrl_disable(device);
594
595 return 0;
596}
597
598static int
599adreno_recover_hang(struct kgsl_device *device)
600{
601 int ret;
602 unsigned int *rb_buffer;
603 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
604 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
605 unsigned int timestamp;
606 unsigned int num_rb_contents;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 unsigned int reftimestamp;
608 unsigned int enable_ts;
609 unsigned int soptimestamp;
610 unsigned int eoptimestamp;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700611 unsigned int context_id;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700612 struct kgsl_context *context;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700613 struct adreno_context *adreno_context;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700614 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615
616 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
617 rb_buffer = vmalloc(rb->buffer_desc.size);
618 if (!rb_buffer) {
619 KGSL_MEM_ERR(device,
620 "Failed to allocate memory for recovery: %x\n",
621 rb->buffer_desc.size);
622 return -ENOMEM;
623 }
624 /* Extract valid contents from rb which can stil be executed after
625 * hang */
626 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
627 if (ret)
628 goto done;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700629 kgsl_sharedmem_readl(&device->memstore, &context_id,
630 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
631 current_context));
632 context = idr_find(&device->context_idr, context_id);
633 if (context == NULL) {
634 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
635 context_id);
636 context_id = KGSL_MEMSTORE_GLOBAL;
637 }
638
639 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
640 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
641
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700643 KGSL_MEMSTORE_OFFSET(context_id,
644 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700646 KGSL_MEMSTORE_OFFSET(context_id,
647 ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700649 KGSL_MEMSTORE_OFFSET(context_id,
650 soptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700652 KGSL_MEMSTORE_OFFSET(context_id,
653 eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 /* Make sure memory is synchronized before restarting the GPU */
655 mb();
656 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700657 "Context id that caused a GPU hang: %d\n", context_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658 /* restart device */
659 ret = adreno_stop(device);
660 if (ret)
661 goto done;
662 ret = adreno_start(device, true);
663 if (ret)
664 goto done;
665 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
666 /* Restore timestamp states */
667 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700668 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 soptimestamp);
670 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700671 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672 eoptimestamp);
Carter Cooperae4c7bc2012-04-10 09:40:49 -0600673
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 if (num_rb_contents) {
675 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700676 KGSL_MEMSTORE_OFFSET(context_id, ref_wait_ts),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 reftimestamp);
678 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700679 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680 enable_ts);
681 }
682 /* Make sure all writes are posted before the GPU reads them */
683 wmb();
684 /* Mark the invalid context so no more commands are accepted from
685 * that context */
686
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700687 adreno_context = context->devctxt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688
689 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700690 "Context that caused a GPU hang: %d\n", adreno_context->id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700691
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700692 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700694 /*
695 * Set the reset status of all contexts to
696 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
697 * since thats the guilty party
698 */
699 while ((context = idr_get_next(&device->context_idr, &next))) {
700 if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
701 context->reset_status) {
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700702 if (context->id != context_id)
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700703 context->reset_status =
704 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
705 else
706 context->reset_status =
707 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
708 }
709 next = next + 1;
710 }
711
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712 /* Restore valid commands in ringbuffer */
713 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700714 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715done:
716 vfree(rb_buffer);
717 return ret;
718}
719
720static int
721adreno_dump_and_recover(struct kgsl_device *device)
722{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 int result = -ETIMEDOUT;
724
725 if (device->state == KGSL_STATE_HUNG)
726 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700727 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728 mutex_unlock(&device->mutex);
729 wait_for_completion(&device->recovery_gate);
730 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700731 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 result = 0;
733 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700734 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700736 /* Detected a hang */
737
738
739 /*
740 * Trigger an automatic dump of the state to
741 * the console
742 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700744
745 /*
746 * Make a GPU snapshot. For now, do it after the PM dump so we
747 * can at least be sure the PM dump will work as it always has
748 */
749 kgsl_device_snapshot(device, 1);
750
Jeremy Gebben388c2972011-12-16 09:05:07 -0700751 result = adreno_recover_hang(device);
752 if (result)
753 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
754 else
755 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
756 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757 }
758done:
759 return result;
760}
761
762static int adreno_getproperty(struct kgsl_device *device,
763 enum kgsl_property_type type,
764 void *value,
765 unsigned int sizebytes)
766{
767 int status = -EINVAL;
768 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
769
770 switch (type) {
771 case KGSL_PROP_DEVICE_INFO:
772 {
773 struct kgsl_devinfo devinfo;
774
775 if (sizebytes != sizeof(devinfo)) {
776 status = -EINVAL;
777 break;
778 }
779
780 memset(&devinfo, 0, sizeof(devinfo));
781 devinfo.device_id = device->id+1;
782 devinfo.chip_id = adreno_dev->chip_id;
783 devinfo.mmu_enabled = kgsl_mmu_enabled();
784 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -0600785 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
786 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787
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 */
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700813 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
814 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 }
816 if (copy_to_user(value, &shadowprop,
817 sizeof(shadowprop))) {
818 status = -EFAULT;
819 break;
820 }
821 status = 0;
822 }
823 break;
824 case KGSL_PROP_MMU_ENABLE:
825 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600826 int mmu_prop = kgsl_mmu_enabled();
827
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 if (sizebytes != sizeof(int)) {
829 status = -EINVAL;
830 break;
831 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600832 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 status = -EFAULT;
834 break;
835 }
836 status = 0;
837 }
838 break;
839 case KGSL_PROP_INTERRUPT_WAITS:
840 {
841 int int_waits = 1;
842 if (sizebytes != sizeof(int)) {
843 status = -EINVAL;
844 break;
845 }
846 if (copy_to_user(value, &int_waits, sizeof(int))) {
847 status = -EFAULT;
848 break;
849 }
850 status = 0;
851 }
852 break;
853 default:
854 status = -EINVAL;
855 }
856
857 return status;
858}
859
Jordan Crousef7370f82012-04-18 09:31:07 -0600860static int adreno_setproperty(struct kgsl_device *device,
861 enum kgsl_property_type type,
862 void *value,
863 unsigned int sizebytes)
864{
865 int status = -EINVAL;
866
867 switch (type) {
868 case KGSL_PROP_PWRCTRL: {
869 unsigned int enable;
870 struct kgsl_device_platform_data *pdata =
871 kgsl_device_get_drvdata(device);
872
873 if (sizebytes != sizeof(enable))
874 break;
875
876 if (copy_from_user(&enable, (void __user *) value,
877 sizeof(enable))) {
878 status = -EFAULT;
879 break;
880 }
881
882 if (enable) {
883 if (pdata->nap_allowed)
884 device->pwrctrl.nap_allowed = true;
885
886 kgsl_pwrscale_enable(device);
887 } else {
888 device->pwrctrl.nap_allowed = false;
889 kgsl_pwrscale_disable(device);
890 }
891
892 status = 0;
893 }
894 break;
895 default:
896 break;
897 }
898
899 return status;
900}
901
Lynus Vaz06a9a902011-10-04 19:25:33 +0530902static inline void adreno_poke(struct kgsl_device *device)
903{
904 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
905 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
906}
907
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908/* Caller must hold the device mutex. */
909int adreno_idle(struct kgsl_device *device, unsigned int timeout)
910{
911 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
912 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
913 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530914 unsigned long wait_timeout =
915 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +0530916 unsigned long wait_time;
917 unsigned long wait_time_part;
918 unsigned int msecs;
919 unsigned int msecs_first;
920 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700921
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700922 kgsl_cffdump_regpoll(device->id,
923 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924 0x00000000, 0x80000000);
925 /* first, wait until the CP has consumed all the commands in
926 * the ring buffer
927 */
928retry:
929 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +0530930 msecs = adreno_dev->wait_timeout;
931 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
932 msecs_part = (msecs - msecs_first + 3) / 4;
933 wait_time = jiffies + wait_timeout;
934 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700935 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936 do {
Lynus Vaz284d1042012-01-31 16:32:31 +0530937 if (time_after(jiffies, wait_time_part)) {
938 adreno_poke(device);
939 wait_time_part = jiffies +
940 msecs_to_jiffies(msecs_part);
941 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942 GSL_RB_GET_READPTR(rb, &rb->rptr);
943 if (time_after(jiffies, wait_time)) {
944 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
945 rb->rptr, rb->wptr);
946 goto err;
947 }
948 } while (rb->rptr != rb->wptr);
949 }
950
951 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530952 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700954 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
955 &rbbm_status);
956 if (adreno_is_a2xx(adreno_dev)) {
957 if (rbbm_status == 0x110)
958 return 0;
959 } else {
960 if (!(rbbm_status & 0x80000000))
961 return 0;
962 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963 }
964
965err:
966 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
967 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530968 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700969 goto retry;
970 }
971 return -ETIMEDOUT;
972}
973
974static unsigned int adreno_isidle(struct kgsl_device *device)
975{
976 int status = false;
977 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
978 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
979 unsigned int rbbm_status;
980
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700981 WARN_ON(device->state == KGSL_STATE_INIT);
982 /* If the device isn't active, don't force it on. */
983 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 /* Is the ring buffer is empty? */
985 GSL_RB_GET_READPTR(rb, &rb->rptr);
986 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
987 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700988 adreno_regread(device,
989 adreno_dev->gpudev->reg_rbbm_status,
990 &rbbm_status);
991
992 if (adreno_is_a2xx(adreno_dev)) {
993 if (rbbm_status == 0x110)
994 status = true;
995 } else {
996 if (!(rbbm_status & 0x80000000))
997 status = true;
998 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999 }
1000 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001001 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002 }
1003 return status;
1004}
1005
1006/* Caller must hold the device mutex. */
1007static int adreno_suspend_context(struct kgsl_device *device)
1008{
1009 int status = 0;
1010 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1011
1012 /* switch to NULL ctxt */
1013 if (adreno_dev->drawctxt_active != NULL) {
1014 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1015 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1016 }
1017
1018 return status;
1019}
1020
Jordan Crouse233b2092012-04-18 09:31:09 -06001021/* Find a memory structure attached to an adreno context */
1022
1023struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1024 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1025{
1026 struct kgsl_context *context;
1027 struct adreno_context *adreno_context = NULL;
1028 int next = 0;
1029
1030 while (1) {
1031 context = idr_get_next(&device->context_idr, &next);
1032 if (context == NULL)
1033 break;
1034
1035 adreno_context = (struct adreno_context *)context->devctxt;
1036
1037 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1038 struct kgsl_memdesc *desc;
1039
1040 desc = &adreno_context->gpustate;
1041 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1042 return desc;
1043
1044 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1045 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1046 return desc;
1047 }
1048 next = next + 1;
1049 }
1050
1051 return NULL;
1052}
1053
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001054struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001055 unsigned int pt_base,
1056 unsigned int gpuaddr,
1057 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001060 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1061 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1062
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001063 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1064 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001065
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001066 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1067 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001069 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1070 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001071
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001072 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1073 size))
1074 return &device->mmu.setstate_memory;
1075
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001076 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1077
1078 if (entry)
1079 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080
Jordan Crouse233b2092012-04-18 09:31:09 -06001081 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001082}
1083
1084uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1085 unsigned int gpuaddr, unsigned int size)
1086{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001087 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001088
1089 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1090
1091 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092}
1093
1094void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1095 unsigned int *value)
1096{
1097 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06001098 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
1099 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100
1101 if (!in_interrupt())
1102 kgsl_pre_hwaccess(device);
1103
1104 /*ensure this read finishes before the next one.
1105 * i.e. act like normal readl() */
1106 *value = __raw_readl(reg);
1107 rmb();
1108}
1109
1110void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1111 unsigned int value)
1112{
1113 unsigned int *reg;
1114
Jordan Crouse7501d452012-04-19 08:58:44 -06001115 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001116
1117 if (!in_interrupt())
1118 kgsl_pre_hwaccess(device);
1119
1120 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06001121 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001122
1123 /*ensure previous writes post before this one,
1124 * i.e. act like normal writel() */
1125 wmb();
1126 __raw_writel(value, reg);
1127}
1128
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001129static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1130{
1131 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
1132
1133 if (k_ctxt != NULL) {
1134 struct adreno_context *a_ctxt = k_ctxt->devctxt;
1135 /*
1136 * if the context was not created with per context timestamp
1137 * support, we must use the global timestamp since issueibcmds
1138 * will be returning that one.
1139 */
1140 if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
1141 context_id = a_ctxt->id;
1142 }
1143
1144 return context_id;
1145}
1146
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001147static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001148 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149{
1150 int status;
1151 unsigned int ref_ts, enableflag;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001152 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001154 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155 if (!status) {
1156 mutex_lock(&device->mutex);
1157 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001158 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159 mb();
1160
1161 if (enableflag) {
1162 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001163 KGSL_MEMSTORE_OFFSET(context_id,
1164 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001165 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001166 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001168 KGSL_MEMSTORE_OFFSET(context_id,
1169 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001170 wmb();
1171 }
1172 } else {
1173 unsigned int cmds[2];
1174 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001175 KGSL_MEMSTORE_OFFSET(context_id,
1176 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 enableflag = 1;
1178 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001179 KGSL_MEMSTORE_OFFSET(context_id,
1180 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001181 wmb();
1182 /* submit a dummy packet so that even if all
1183 * commands upto timestamp get executed we will still
1184 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001185 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001187 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1188 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189 }
1190 mutex_unlock(&device->mutex);
1191 }
1192
1193 return status;
1194}
1195
1196/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001197 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198 placing a process in wait q. For conditional interrupts we expect the
1199 process to already be in its wait q when its exit condition checking
1200 function is called.
1201*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001202#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001203({ \
1204 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001205 if (io) \
1206 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1207 else \
1208 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 __ret; \
1210})
1211
1212/* MUST be called with the device mutex held */
1213static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001214 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215 unsigned int timestamp,
1216 unsigned int msecs)
1217{
1218 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001219 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001220 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001222 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301223 int retries;
1224 unsigned int msecs_first;
1225 unsigned int msecs_part;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001226 unsigned int ts_issued;
1227 unsigned int context_id = _get_context_id(context);
1228
1229 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301231 /* Don't wait forever, set a max value for now */
1232 if (msecs == -1)
1233 msecs = adreno_dev->wait_timeout;
1234
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001235 if (timestamp_cmp(timestamp, ts_issued) > 0) {
1236 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
1237 "last issued ts <%d:0x%x>\n",
1238 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001239 status = -EINVAL;
1240 goto done;
1241 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242
Lynus Vaz06a9a902011-10-04 19:25:33 +05301243 /* Keep the first timeout as 100msecs before rewriting
1244 * the WPTR. Less visible impact if the WPTR has not
1245 * been updated properly.
1246 */
1247 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1248 msecs_part = (msecs - msecs_first + 3) / 4;
1249 for (retries = 0; retries < 5; retries++) {
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001250 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001251 /* if the timestamp happens while we're not
1252 * waiting, there's a chance that an interrupt
1253 * will not be generated and thus the timestamp
1254 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301255 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001256 queue_work(device->work_queue, &device->ts_expired_ws);
1257 status = 0;
1258 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001260 adreno_poke(device);
1261 io_cnt = (io_cnt + 1) % 100;
1262 if (io_cnt <
1263 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1264 io = 0;
1265 mutex_unlock(&device->mutex);
1266 /* We need to make sure that the process is
1267 * placed in wait-q before its condition is called
1268 */
1269 status = kgsl_wait_event_interruptible_timeout(
1270 device->wait_queue,
1271 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001272 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001273 msecs_to_jiffies(retries ?
1274 msecs_part : msecs_first), io);
1275 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001276
Jeremy Gebben63904832012-02-07 16:10:55 -07001277 if (status > 0) {
1278 /*completed before the wait finished */
1279 status = 0;
1280 goto done;
1281 } else if (status < 0) {
1282 /*an error occurred*/
1283 goto done;
1284 }
1285 /*this wait timed out*/
1286 }
1287 status = -ETIMEDOUT;
1288 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001289 "Device hang detected while waiting for timestamp: "
1290 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
1291 "wptr: 0x%x\n",
1292 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07001293 adreno_dev->ringbuffer.wptr);
1294 if (!adreno_dump_and_recover(device)) {
1295 /* wait for idle after recovery as the
1296 * timestamp that this process wanted
1297 * to wait on may be invalid */
1298 if (!adreno_idle(device, KGSL_TIMEOUT_DEFAULT))
1299 status = 0;
1300 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301done:
1302 return (int)status;
1303}
1304
1305static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001306 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307{
1308 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001309 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310
Jordan Crousec659f382012-04-16 11:10:41 -06001311 switch (type) {
1312 case KGSL_TIMESTAMP_QUEUED: {
1313 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1314 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1315
1316 timestamp = rb->timestamp[context_id];
1317 break;
1318 }
1319 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001320 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06001321 break;
1322 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001323 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06001324 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
1325 break;
1326 }
1327
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001328 rmb();
1329
1330 return timestamp;
1331}
1332
1333static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1334 unsigned int cmd, void *data)
1335{
1336 int result = 0;
1337 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1338 struct kgsl_context *context;
1339
1340 switch (cmd) {
1341 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1342 binbase = data;
1343
1344 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1345 if (context) {
1346 adreno_drawctxt_set_bin_base_offset(
1347 dev_priv->device, context, binbase->offset);
1348 } else {
1349 result = -EINVAL;
1350 KGSL_DRV_ERR(dev_priv->device,
1351 "invalid drawctxt drawctxt_id %d "
1352 "device_id=%d\n",
1353 binbase->drawctxt_id, dev_priv->device->id);
1354 }
1355 break;
1356
1357 default:
1358 KGSL_DRV_INFO(dev_priv->device,
1359 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001360 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001361 break;
1362 }
1363 return result;
1364
1365}
1366
1367static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1368{
1369 gpu_freq /= 1000000;
1370 return ticks / gpu_freq;
1371}
1372
1373static void adreno_power_stats(struct kgsl_device *device,
1374 struct kgsl_power_stats *stats)
1375{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001376 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001377 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001378 unsigned int cycles;
1379
1380 /* Get the busy cycles counted since the counter was last reset */
1381 /* Calling this function also resets and restarts the counter */
1382
1383 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001384
1385 /* In order to calculate idle you have to have run the algorithm *
1386 * at least once to get a start time. */
1387 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001388 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001389 stats->total_time = tmp - pwr->time;
1390 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001391 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392 pwrlevels[device->pwrctrl.active_pwrlevel].
1393 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001394 } else {
1395 stats->total_time = 0;
1396 stats->busy_time = 0;
1397 pwr->time = ktime_to_us(ktime_get());
1398 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001399}
1400
1401void adreno_irqctrl(struct kgsl_device *device, int state)
1402{
Jordan Crousea78c9172011-07-11 13:14:09 -06001403 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1404 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001405}
1406
Jordan Crousea0758f22011-12-07 11:19:22 -07001407static unsigned int adreno_gpuid(struct kgsl_device *device)
1408{
1409 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1410
1411 /* Standard KGSL gpuid format:
1412 * top word is 0x0002 for 2D or 0x0003 for 3D
1413 * Bottom word is core specific identifer
1414 */
1415
1416 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1417}
1418
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419static const struct kgsl_functable adreno_functable = {
1420 /* Mandatory functions */
1421 .regread = adreno_regread,
1422 .regwrite = adreno_regwrite,
1423 .idle = adreno_idle,
1424 .isidle = adreno_isidle,
1425 .suspend_context = adreno_suspend_context,
1426 .start = adreno_start,
1427 .stop = adreno_stop,
1428 .getproperty = adreno_getproperty,
1429 .waittimestamp = adreno_waittimestamp,
1430 .readtimestamp = adreno_readtimestamp,
1431 .issueibcmds = adreno_ringbuffer_issueibcmds,
1432 .ioctl = adreno_ioctl,
1433 .setup_pt = adreno_setup_pt,
1434 .cleanup_pt = adreno_cleanup_pt,
1435 .power_stats = adreno_power_stats,
1436 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001437 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001438 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001439 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001440 /* Optional functions */
1441 .setstate = adreno_setstate,
1442 .drawctxt_create = adreno_drawctxt_create,
1443 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06001444 .setproperty = adreno_setproperty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445};
1446
1447static struct platform_device_id adreno_id_table[] = {
1448 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1449 { },
1450};
1451MODULE_DEVICE_TABLE(platform, adreno_id_table);
1452
1453static struct platform_driver adreno_platform_driver = {
1454 .probe = adreno_probe,
1455 .remove = __devexit_p(adreno_remove),
1456 .suspend = kgsl_suspend_driver,
1457 .resume = kgsl_resume_driver,
1458 .id_table = adreno_id_table,
1459 .driver = {
1460 .owner = THIS_MODULE,
1461 .name = DEVICE_3D_NAME,
1462 .pm = &kgsl_pm_ops,
1463 }
1464};
1465
1466static int __init kgsl_3d_init(void)
1467{
1468 return platform_driver_register(&adreno_platform_driver);
1469}
1470
1471static void __exit kgsl_3d_exit(void)
1472{
1473 platform_driver_unregister(&adreno_platform_driver);
1474}
1475
1476module_init(kgsl_3d_init);
1477module_exit(kgsl_3d_exit);
1478
1479MODULE_DESCRIPTION("3D Graphics driver");
1480MODULE_VERSION("1.2");
1481MODULE_LICENSE("GPL v2");
1482MODULE_ALIAS("platform:kgsl_3d");