blob: 1432ccf24c32a8e6f4ce68861dc677a651c3ac10 [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 Croused2b30d22012-05-21 08:41:51 -0600161 { ADRENO_REV_A320, 3, 2, 0, ANY_ID,
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()) {
Jordan Croused2b30d22012-05-21 08:41:51 -0600365 unsigned int version = socinfo_get_version();
366
Jordan Crouse54154c62012-03-27 16:33:26 -0600367 /* A320 */
368 majorid = 2;
369 minorid = 0;
Jordan Croused2b30d22012-05-21 08:41:51 -0600370
371 /*
372 * V1.1 has some GPU work arounds that we need to communicate
373 * up to user space via the patchid
374 */
375
376 if ((SOCINFO_VERSION_MAJOR(version) == 1) &&
377 (SOCINFO_VERSION_MINOR(version) == 1))
378 patchid = 1;
379 else
380 patchid = 0;
Jordan Crouse54154c62012-03-27 16:33:26 -0600381 } else if (cpu_is_msm8930()) {
382 /* A305 */
383 majorid = 0;
384 minorid = 5;
385 patchid = 0;
386 }
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700387
Jordan Crouse54154c62012-03-27 16:33:26 -0600388 return (0x03 << 24) | (majorid << 16) | (minorid << 8) | patchid;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700389}
390
391static unsigned int
392a2xx_getchipid(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393{
394 unsigned int chipid = 0;
395 unsigned int coreid, majorid, minorid, patchid, revid;
Carter Cooperf27ec722011-11-17 15:20:38 -0700396 uint32_t soc_platform_version = socinfo_get_version();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397
398 adreno_regread(device, REG_RBBM_PERIPHID1, &coreid);
399 adreno_regread(device, REG_RBBM_PERIPHID2, &majorid);
400 adreno_regread(device, REG_RBBM_PATCH_RELEASE, &revid);
401
402 /*
403 * adreno 22x gpus are indicated by coreid 2,
404 * but REG_RBBM_PERIPHID1 always contains 0 for this field
405 */
Sudhakara Rao Tentudaebac22012-04-02 14:51:29 -0700406 if (cpu_is_msm8960() || cpu_is_msm8x60())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407 chipid = 2 << 24;
408 else
409 chipid = (coreid & 0xF) << 24;
410
411 chipid |= ((majorid >> 4) & 0xF) << 16;
412
413 minorid = ((revid >> 0) & 0xFF);
414
415 patchid = ((revid >> 16) & 0xFF);
416
417 /* 8x50 returns 0 for patch release, but it should be 1 */
Carter Cooperf27ec722011-11-17 15:20:38 -0700418 /* 8960v3 returns 5 for patch release, but it should be 6 */
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530419 /* 8x25 returns 0 for minor id, but it should be 1 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420 if (cpu_is_qsd8x50())
421 patchid = 1;
Carter Cooperf27ec722011-11-17 15:20:38 -0700422 else if (cpu_is_msm8960() &&
423 SOCINFO_VERSION_MAJOR(soc_platform_version) == 3)
424 patchid = 6;
Ranjhith Kalisamy938e00f2012-02-17 14:39:47 +0530425 else if (cpu_is_msm8625() && minorid == 0)
426 minorid = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427
428 chipid |= (minorid << 8) | patchid;
429
430 return chipid;
431}
432
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700433static unsigned int
434adreno_getchipid(struct kgsl_device *device)
435{
Sudhakara Rao Tentu8ebb2282012-03-06 14:52:58 +0530436 if (cpu_is_apq8064() || cpu_is_msm8930())
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700437 return a3xx_getchipid(device);
438 else
439 return a2xx_getchipid(device);
440}
441
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442static inline bool _rev_match(unsigned int id, unsigned int entry)
443{
Jordan Crouse505df9c2011-07-28 08:37:59 -0600444 return (entry == ANY_ID || entry == id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446
447static void
448adreno_identify_gpu(struct adreno_device *adreno_dev)
449{
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600450 unsigned int i, core, major, minor, patchid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451
452 adreno_dev->chip_id = adreno_getchipid(&adreno_dev->dev);
453
454 core = (adreno_dev->chip_id >> 24) & 0xff;
455 major = (adreno_dev->chip_id >> 16) & 0xff;
456 minor = (adreno_dev->chip_id >> 8) & 0xff;
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600457 patchid = (adreno_dev->chip_id & 0xff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458
Jordan Crouse505df9c2011-07-28 08:37:59 -0600459 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
460 if (core == adreno_gpulist[i].core &&
461 _rev_match(major, adreno_gpulist[i].major) &&
Jeremy Gebbene2e61d42011-09-27 15:45:41 -0600462 _rev_match(minor, adreno_gpulist[i].minor) &&
463 _rev_match(patchid, adreno_gpulist[i].patchid))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465 }
466
Jordan Crouse505df9c2011-07-28 08:37:59 -0600467 if (i == ARRAY_SIZE(adreno_gpulist)) {
468 adreno_dev->gpurev = ADRENO_REV_UNKNOWN;
469 return;
470 }
471
472 adreno_dev->gpurev = adreno_gpulist[i].gpurev;
473 adreno_dev->gpudev = adreno_gpulist[i].gpudev;
474 adreno_dev->pfp_fwfile = adreno_gpulist[i].pfpfw;
475 adreno_dev->pm4_fwfile = adreno_gpulist[i].pm4fw;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700476 adreno_dev->istore_size = adreno_gpulist[i].istore_size;
477 adreno_dev->pix_shader_start = adreno_gpulist[i].pix_shader_start;
Jordan Crouse55d98fd2012-02-04 10:23:51 -0700478 adreno_dev->instruction_size = adreno_gpulist[i].instruction_size;
Jordan Crouse7501d452012-04-19 08:58:44 -0600479 adreno_dev->gmem_size = adreno_gpulist[i].gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480}
481
482static int __devinit
483adreno_probe(struct platform_device *pdev)
484{
485 struct kgsl_device *device;
486 struct adreno_device *adreno_dev;
487 int status = -EINVAL;
488
489 device = (struct kgsl_device *)pdev->id_entry->driver_data;
490 adreno_dev = ADRENO_DEVICE(device);
491 device->parentdev = &pdev->dev;
492
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 status = adreno_ringbuffer_init(device);
494 if (status != 0)
495 goto error;
496
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600497 status = kgsl_device_platform_probe(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 if (status)
499 goto error_close_rb;
500
501 adreno_debugfs_init(device);
502
503 kgsl_pwrscale_init(device);
504 kgsl_pwrscale_attach_policy(device, ADRENO_DEFAULT_PWRSCALE_POLICY);
505
506 device->flags &= ~KGSL_FLAGS_SOFT_RESET;
507 return 0;
508
509error_close_rb:
510 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
511error:
512 device->parentdev = NULL;
513 return status;
514}
515
516static int __devexit adreno_remove(struct platform_device *pdev)
517{
518 struct kgsl_device *device;
519 struct adreno_device *adreno_dev;
520
521 device = (struct kgsl_device *)pdev->id_entry->driver_data;
522 adreno_dev = ADRENO_DEVICE(device);
523
524 kgsl_pwrscale_detach_policy(device);
525 kgsl_pwrscale_close(device);
526
527 adreno_ringbuffer_close(&adreno_dev->ringbuffer);
528 kgsl_device_platform_remove(device);
529
530 return 0;
531}
532
533static int adreno_start(struct kgsl_device *device, unsigned int init_ram)
534{
535 int status = -EINVAL;
536 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537
Jeremy Gebben388c2972011-12-16 09:05:07 -0700538 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539
540 /* Power up the device */
541 kgsl_pwrctrl_enable(device);
542
543 /* Identify the specific GPU */
544 adreno_identify_gpu(adreno_dev);
545
Jordan Crouse505df9c2011-07-28 08:37:59 -0600546 if (adreno_dev->gpurev == ADRENO_REV_UNKNOWN) {
547 KGSL_DRV_ERR(device, "Unknown chip ID %x\n",
548 adreno_dev->chip_id);
549 goto error_clk_off;
550 }
551
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700552 /* Set up the MMU */
553 if (adreno_is_a2xx(adreno_dev)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600554 /*
555 * the MH_CLNT_INTF_CTRL_CONFIG registers aren't present
556 * on older gpus
557 */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700558 if (adreno_is_a20x(adreno_dev)) {
559 device->mh.mh_intf_cfg1 = 0;
560 device->mh.mh_intf_cfg2 = 0;
561 }
562
563 kgsl_mh_start(device);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600564 }
565
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700566 status = kgsl_mmu_start(device);
567 if (status)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568 goto error_clk_off;
569
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700570 /* Start the GPU */
571 adreno_dev->gpudev->start(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572
573 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700574 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575
576 status = adreno_ringbuffer_start(&adreno_dev->ringbuffer, init_ram);
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700577 if (status == 0) {
578 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
579 return 0;
580 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600583 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584error_clk_off:
585 kgsl_pwrctrl_disable(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586
587 return status;
588}
589
590static int adreno_stop(struct kgsl_device *device)
591{
592 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
593
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 adreno_dev->drawctxt_active = NULL;
595
596 adreno_ringbuffer_stop(&adreno_dev->ringbuffer);
597
Shubhraprakash Das79447952012-04-26 18:12:23 -0600598 kgsl_mmu_stop(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700600 device->ftbl->irqctrl(device, 0);
Ranjhith Kalisamyce75b0c2012-02-01 19:31:23 +0530601 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800602 del_timer_sync(&device->idle_timer);
Lucille Sylvester844b1c82011-08-29 15:26:06 -0600603
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 /* Power down the device */
605 kgsl_pwrctrl_disable(device);
606
607 return 0;
608}
609
610static int
611adreno_recover_hang(struct kgsl_device *device)
612{
613 int ret;
614 unsigned int *rb_buffer;
615 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
616 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
617 unsigned int timestamp;
618 unsigned int num_rb_contents;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619 unsigned int reftimestamp;
620 unsigned int enable_ts;
621 unsigned int soptimestamp;
622 unsigned int eoptimestamp;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700623 unsigned int context_id;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700624 struct kgsl_context *context;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700625 struct adreno_context *adreno_context;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700626 int next = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627
628 KGSL_DRV_ERR(device, "Starting recovery from 3D GPU hang....\n");
629 rb_buffer = vmalloc(rb->buffer_desc.size);
630 if (!rb_buffer) {
631 KGSL_MEM_ERR(device,
632 "Failed to allocate memory for recovery: %x\n",
633 rb->buffer_desc.size);
634 return -ENOMEM;
635 }
636 /* Extract valid contents from rb which can stil be executed after
637 * hang */
638 ret = adreno_ringbuffer_extract(rb, rb_buffer, &num_rb_contents);
639 if (ret)
640 goto done;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700641 kgsl_sharedmem_readl(&device->memstore, &context_id,
642 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
643 current_context));
644 context = idr_find(&device->context_idr, context_id);
645 if (context == NULL) {
646 KGSL_DRV_ERR(device, "Last context unknown id:%d\n",
647 context_id);
648 context_id = KGSL_MEMSTORE_GLOBAL;
649 }
650
651 timestamp = rb->timestamp[KGSL_MEMSTORE_GLOBAL];
652 KGSL_DRV_ERR(device, "Last issued global timestamp: %x\n", timestamp);
653
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 kgsl_sharedmem_readl(&device->memstore, &reftimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700655 KGSL_MEMSTORE_OFFSET(context_id,
656 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 kgsl_sharedmem_readl(&device->memstore, &enable_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700658 KGSL_MEMSTORE_OFFSET(context_id,
659 ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 kgsl_sharedmem_readl(&device->memstore, &soptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700661 KGSL_MEMSTORE_OFFSET(context_id,
662 soptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663 kgsl_sharedmem_readl(&device->memstore, &eoptimestamp,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700664 KGSL_MEMSTORE_OFFSET(context_id,
665 eoptimestamp));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666 /* Make sure memory is synchronized before restarting the GPU */
667 mb();
668 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700669 "Context id that caused a GPU hang: %d\n", context_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 /* restart device */
671 ret = adreno_stop(device);
672 if (ret)
673 goto done;
674 ret = adreno_start(device, true);
675 if (ret)
676 goto done;
677 KGSL_DRV_ERR(device, "Device has been restarted after hang\n");
678 /* Restore timestamp states */
679 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700680 KGSL_MEMSTORE_OFFSET(context_id, soptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681 soptimestamp);
682 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700683 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684 eoptimestamp);
Carter Cooperae4c7bc2012-04-10 09:40:49 -0600685
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686 if (num_rb_contents) {
687 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700688 KGSL_MEMSTORE_OFFSET(context_id, ref_wait_ts),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689 reftimestamp);
690 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700691 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692 enable_ts);
693 }
694 /* Make sure all writes are posted before the GPU reads them */
695 wmb();
696 /* Mark the invalid context so no more commands are accepted from
697 * that context */
698
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700699 adreno_context = context->devctxt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700
701 KGSL_CTXT_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700702 "Context that caused a GPU hang: %d\n", adreno_context->id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700704 adreno_context->flags |= CTXT_FLAGS_GPU_HANG;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700706 /*
707 * Set the reset status of all contexts to
708 * INNOCENT_CONTEXT_RESET_EXT except for the bad context
709 * since thats the guilty party
710 */
711 while ((context = idr_get_next(&device->context_idr, &next))) {
712 if (KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT !=
713 context->reset_status) {
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700714 if (context->id != context_id)
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700715 context->reset_status =
716 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
717 else
718 context->reset_status =
719 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
720 }
721 next = next + 1;
722 }
723
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724 /* Restore valid commands in ringbuffer */
725 adreno_ringbuffer_restore(rb, rb_buffer, num_rb_contents);
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700726 rb->timestamp[KGSL_MEMSTORE_GLOBAL] = timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727done:
728 vfree(rb_buffer);
729 return ret;
730}
731
732static int
733adreno_dump_and_recover(struct kgsl_device *device)
734{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735 int result = -ETIMEDOUT;
736
737 if (device->state == KGSL_STATE_HUNG)
738 goto done;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700739 if (device->state == KGSL_STATE_DUMP_AND_RECOVER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 mutex_unlock(&device->mutex);
741 wait_for_completion(&device->recovery_gate);
742 mutex_lock(&device->mutex);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700743 if (device->state != KGSL_STATE_HUNG)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 result = 0;
745 } else {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700746 kgsl_pwrctrl_set_state(device, KGSL_STATE_DUMP_AND_RECOVER);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747 INIT_COMPLETION(device->recovery_gate);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700748 /* Detected a hang */
749
750
751 /*
752 * Trigger an automatic dump of the state to
753 * the console
754 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755 adreno_postmortem_dump(device, 0);
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700756
757 /*
758 * Make a GPU snapshot. For now, do it after the PM dump so we
759 * can at least be sure the PM dump will work as it always has
760 */
761 kgsl_device_snapshot(device, 1);
762
Jeremy Gebben388c2972011-12-16 09:05:07 -0700763 result = adreno_recover_hang(device);
764 if (result)
765 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
766 else
767 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
768 complete_all(&device->recovery_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 }
770done:
771 return result;
772}
773
774static int adreno_getproperty(struct kgsl_device *device,
775 enum kgsl_property_type type,
776 void *value,
777 unsigned int sizebytes)
778{
779 int status = -EINVAL;
780 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
781
782 switch (type) {
783 case KGSL_PROP_DEVICE_INFO:
784 {
785 struct kgsl_devinfo devinfo;
786
787 if (sizebytes != sizeof(devinfo)) {
788 status = -EINVAL;
789 break;
790 }
791
792 memset(&devinfo, 0, sizeof(devinfo));
793 devinfo.device_id = device->id+1;
794 devinfo.chip_id = adreno_dev->chip_id;
795 devinfo.mmu_enabled = kgsl_mmu_enabled();
796 devinfo.gpu_id = adreno_dev->gpurev;
Jordan Crouse7501d452012-04-19 08:58:44 -0600797 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
798 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799
800 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
801 0) {
802 status = -EFAULT;
803 break;
804 }
805 status = 0;
806 }
807 break;
808 case KGSL_PROP_DEVICE_SHADOW:
809 {
810 struct kgsl_shadowprop shadowprop;
811
812 if (sizebytes != sizeof(shadowprop)) {
813 status = -EINVAL;
814 break;
815 }
816 memset(&shadowprop, 0, sizeof(shadowprop));
817 if (device->memstore.hostptr) {
818 /*NOTE: with mmu enabled, gpuaddr doesn't mean
819 * anything to mmap().
820 */
821 shadowprop.gpuaddr = device->memstore.physaddr;
822 shadowprop.size = device->memstore.size;
823 /* GSL needs this to be set, even if it
824 appears to be meaningless */
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700825 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
826 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 }
828 if (copy_to_user(value, &shadowprop,
829 sizeof(shadowprop))) {
830 status = -EFAULT;
831 break;
832 }
833 status = 0;
834 }
835 break;
836 case KGSL_PROP_MMU_ENABLE:
837 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600838 int mmu_prop = kgsl_mmu_enabled();
839
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840 if (sizebytes != sizeof(int)) {
841 status = -EINVAL;
842 break;
843 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600844 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 status = -EFAULT;
846 break;
847 }
848 status = 0;
849 }
850 break;
851 case KGSL_PROP_INTERRUPT_WAITS:
852 {
853 int int_waits = 1;
854 if (sizebytes != sizeof(int)) {
855 status = -EINVAL;
856 break;
857 }
858 if (copy_to_user(value, &int_waits, sizeof(int))) {
859 status = -EFAULT;
860 break;
861 }
862 status = 0;
863 }
864 break;
865 default:
866 status = -EINVAL;
867 }
868
869 return status;
870}
871
Jordan Crousef7370f82012-04-18 09:31:07 -0600872static int adreno_setproperty(struct kgsl_device *device,
873 enum kgsl_property_type type,
874 void *value,
875 unsigned int sizebytes)
876{
877 int status = -EINVAL;
878
879 switch (type) {
880 case KGSL_PROP_PWRCTRL: {
881 unsigned int enable;
882 struct kgsl_device_platform_data *pdata =
883 kgsl_device_get_drvdata(device);
884
885 if (sizebytes != sizeof(enable))
886 break;
887
888 if (copy_from_user(&enable, (void __user *) value,
889 sizeof(enable))) {
890 status = -EFAULT;
891 break;
892 }
893
894 if (enable) {
895 if (pdata->nap_allowed)
896 device->pwrctrl.nap_allowed = true;
897
898 kgsl_pwrscale_enable(device);
899 } else {
900 device->pwrctrl.nap_allowed = false;
901 kgsl_pwrscale_disable(device);
902 }
903
904 status = 0;
905 }
906 break;
907 default:
908 break;
909 }
910
911 return status;
912}
913
Lynus Vaz06a9a902011-10-04 19:25:33 +0530914static inline void adreno_poke(struct kgsl_device *device)
915{
916 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
917 adreno_regwrite(device, REG_CP_RB_WPTR, adreno_dev->ringbuffer.wptr);
918}
919
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920/* Caller must hold the device mutex. */
921int adreno_idle(struct kgsl_device *device, unsigned int timeout)
922{
923 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
924 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
925 unsigned int rbbm_status;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530926 unsigned long wait_timeout =
927 msecs_to_jiffies(adreno_dev->wait_timeout);
Lynus Vaz284d1042012-01-31 16:32:31 +0530928 unsigned long wait_time;
929 unsigned long wait_time_part;
930 unsigned int msecs;
931 unsigned int msecs_first;
932 unsigned int msecs_part;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700934 kgsl_cffdump_regpoll(device->id,
935 adreno_dev->gpudev->reg_rbbm_status << 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936 0x00000000, 0x80000000);
937 /* first, wait until the CP has consumed all the commands in
938 * the ring buffer
939 */
940retry:
941 if (rb->flags & KGSL_FLAGS_STARTED) {
Lynus Vaz284d1042012-01-31 16:32:31 +0530942 msecs = adreno_dev->wait_timeout;
943 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
944 msecs_part = (msecs - msecs_first + 3) / 4;
945 wait_time = jiffies + wait_timeout;
946 wait_time_part = jiffies + msecs_to_jiffies(msecs_first);
Jeremy Gebbenf8594542012-01-13 12:27:21 -0700947 adreno_poke(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 do {
Lynus Vaz284d1042012-01-31 16:32:31 +0530949 if (time_after(jiffies, wait_time_part)) {
950 adreno_poke(device);
951 wait_time_part = jiffies +
952 msecs_to_jiffies(msecs_part);
953 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 GSL_RB_GET_READPTR(rb, &rb->rptr);
955 if (time_after(jiffies, wait_time)) {
956 KGSL_DRV_ERR(device, "rptr: %x, wptr: %x\n",
957 rb->rptr, rb->wptr);
958 goto err;
959 }
960 } while (rb->rptr != rb->wptr);
961 }
962
963 /* now, wait for the GPU to finish its operations */
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530964 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 while (time_before(jiffies, wait_time)) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -0700966 adreno_regread(device, adreno_dev->gpudev->reg_rbbm_status,
967 &rbbm_status);
968 if (adreno_is_a2xx(adreno_dev)) {
969 if (rbbm_status == 0x110)
970 return 0;
971 } else {
972 if (!(rbbm_status & 0x80000000))
973 return 0;
974 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700975 }
976
977err:
978 KGSL_DRV_ERR(device, "spun too long waiting for RB to idle\n");
979 if (!adreno_dump_and_recover(device)) {
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530980 wait_time = jiffies + wait_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981 goto retry;
982 }
983 return -ETIMEDOUT;
984}
985
986static unsigned int adreno_isidle(struct kgsl_device *device)
987{
988 int status = false;
989 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
990 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
991 unsigned int rbbm_status;
992
Lucille Sylvester51b764d2011-12-15 16:51:52 -0700993 WARN_ON(device->state == KGSL_STATE_INIT);
994 /* If the device isn't active, don't force it on. */
995 if (device->state == KGSL_STATE_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996 /* Is the ring buffer is empty? */
997 GSL_RB_GET_READPTR(rb, &rb->rptr);
998 if (!device->active_cnt && (rb->rptr == rb->wptr)) {
999 /* Is the core idle? */
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001000 adreno_regread(device,
1001 adreno_dev->gpudev->reg_rbbm_status,
1002 &rbbm_status);
1003
1004 if (adreno_is_a2xx(adreno_dev)) {
1005 if (rbbm_status == 0x110)
1006 status = true;
1007 } else {
1008 if (!(rbbm_status & 0x80000000))
1009 status = true;
1010 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011 }
1012 } else {
Jeremy Gebbenaeb23872011-12-13 15:58:24 -07001013 status = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 }
1015 return status;
1016}
1017
1018/* Caller must hold the device mutex. */
1019static int adreno_suspend_context(struct kgsl_device *device)
1020{
1021 int status = 0;
1022 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1023
1024 /* switch to NULL ctxt */
1025 if (adreno_dev->drawctxt_active != NULL) {
1026 adreno_drawctxt_switch(adreno_dev, NULL, 0);
1027 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
1028 }
1029
1030 return status;
1031}
1032
Jordan Crouse233b2092012-04-18 09:31:09 -06001033/* Find a memory structure attached to an adreno context */
1034
1035struct kgsl_memdesc *adreno_find_ctxtmem(struct kgsl_device *device,
1036 unsigned int pt_base, unsigned int gpuaddr, unsigned int size)
1037{
1038 struct kgsl_context *context;
1039 struct adreno_context *adreno_context = NULL;
1040 int next = 0;
1041
1042 while (1) {
1043 context = idr_get_next(&device->context_idr, &next);
1044 if (context == NULL)
1045 break;
1046
1047 adreno_context = (struct adreno_context *)context->devctxt;
1048
1049 if (kgsl_mmu_pt_equal(adreno_context->pagetable, pt_base)) {
1050 struct kgsl_memdesc *desc;
1051
1052 desc = &adreno_context->gpustate;
1053 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1054 return desc;
1055
1056 desc = &adreno_context->context_gmem_shadow.gmemshadow;
1057 if (kgsl_gpuaddr_in_memdesc(desc, gpuaddr, size))
1058 return desc;
1059 }
1060 next = next + 1;
1061 }
1062
1063 return NULL;
1064}
1065
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001066struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001067 unsigned int pt_base,
1068 unsigned int gpuaddr,
1069 unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001071 struct kgsl_mem_entry *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1073 struct adreno_ringbuffer *ringbuffer = &adreno_dev->ringbuffer;
1074
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001075 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->buffer_desc, gpuaddr, size))
1076 return &ringbuffer->buffer_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001078 if (kgsl_gpuaddr_in_memdesc(&ringbuffer->memptrs_desc, gpuaddr, size))
1079 return &ringbuffer->memptrs_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001081 if (kgsl_gpuaddr_in_memdesc(&device->memstore, gpuaddr, size))
1082 return &device->memstore;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083
Shubhraprakash Das9a140972012-04-12 13:12:42 -06001084 if (kgsl_gpuaddr_in_memdesc(&device->mmu.setstate_memory, gpuaddr,
1085 size))
1086 return &device->mmu.setstate_memory;
1087
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001088 entry = kgsl_get_mem_entry(pt_base, gpuaddr, size);
1089
1090 if (entry)
1091 return &entry->memdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092
Jordan Crouse233b2092012-04-18 09:31:09 -06001093 return adreno_find_ctxtmem(device, pt_base, gpuaddr, size);
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001094}
1095
1096uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
1097 unsigned int gpuaddr, unsigned int size)
1098{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -06001099 struct kgsl_memdesc *memdesc;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -07001100
1101 memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
1102
1103 return memdesc ? kgsl_gpuaddr_to_vaddr(memdesc, gpuaddr) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104}
1105
1106void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
1107 unsigned int *value)
1108{
1109 unsigned int *reg;
Jordan Crouse7501d452012-04-19 08:58:44 -06001110 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
1111 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001112
1113 if (!in_interrupt())
1114 kgsl_pre_hwaccess(device);
1115
1116 /*ensure this read finishes before the next one.
1117 * i.e. act like normal readl() */
1118 *value = __raw_readl(reg);
1119 rmb();
1120}
1121
1122void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
1123 unsigned int value)
1124{
1125 unsigned int *reg;
1126
Jordan Crouse7501d452012-04-19 08:58:44 -06001127 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001128
1129 if (!in_interrupt())
1130 kgsl_pre_hwaccess(device);
1131
1132 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
Jordan Crouse7501d452012-04-19 08:58:44 -06001133 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001134
1135 /*ensure previous writes post before this one,
1136 * i.e. act like normal writel() */
1137 wmb();
1138 __raw_writel(value, reg);
1139}
1140
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001141static unsigned int _get_context_id(struct kgsl_context *k_ctxt)
1142{
1143 unsigned int context_id = KGSL_MEMSTORE_GLOBAL;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001144 if (k_ctxt != NULL) {
1145 struct adreno_context *a_ctxt = k_ctxt->devctxt;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001146 if (k_ctxt->id == KGSL_CONTEXT_INVALID || a_ctxt == NULL)
1147 context_id = KGSL_CONTEXT_INVALID;
1148 else if (a_ctxt->flags & CTXT_FLAGS_PER_CONTEXT_TS)
1149 context_id = k_ctxt->id;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001150 }
1151
1152 return context_id;
1153}
1154
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155static int kgsl_check_interrupt_timestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001156 struct kgsl_context *context, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157{
1158 int status;
1159 unsigned int ref_ts, enableflag;
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001160 unsigned int context_id;
1161
1162 mutex_lock(&device->mutex);
1163 context_id = _get_context_id(context);
1164 /*
1165 * If the context ID is invalid, we are in a race with
1166 * the context being destroyed by userspace so bail.
1167 */
1168 if (context_id == KGSL_CONTEXT_INVALID) {
1169 KGSL_DRV_WARN(device, "context was detached");
1170 status = -EINVAL;
1171 goto unlock;
1172 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001174 status = kgsl_check_timestamp(device, context, timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175 if (!status) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176 kgsl_sharedmem_readl(&device->memstore, &enableflag,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001177 KGSL_MEMSTORE_OFFSET(context_id, ts_cmp_enable));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 mb();
1179
1180 if (enableflag) {
1181 kgsl_sharedmem_readl(&device->memstore, &ref_ts,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001182 KGSL_MEMSTORE_OFFSET(context_id,
1183 ref_wait_ts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001184 mb();
Jordan Crousee6239dd2011-11-17 13:39:21 -07001185 if (timestamp_cmp(ref_ts, timestamp) >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001187 KGSL_MEMSTORE_OFFSET(context_id,
1188 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189 wmb();
1190 }
1191 } else {
1192 unsigned int cmds[2];
1193 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001194 KGSL_MEMSTORE_OFFSET(context_id,
1195 ref_wait_ts), timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196 enableflag = 1;
1197 kgsl_sharedmem_writel(&device->memstore,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001198 KGSL_MEMSTORE_OFFSET(context_id,
1199 ts_cmp_enable), enableflag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 wmb();
1201 /* submit a dummy packet so that even if all
1202 * commands upto timestamp get executed we will still
1203 * get an interrupt */
Jordan Crouse084427d2011-07-28 08:37:58 -06001204 cmds[0] = cp_type3_packet(CP_NOP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001205 cmds[1] = 0;
Jordan Crousee0ea7622012-01-24 09:32:04 -07001206 adreno_ringbuffer_issuecmds(device, KGSL_CMD_FLAGS_NONE,
1207 &cmds[0], 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 }
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001210unlock:
1211 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001212
1213 return status;
1214}
1215
1216/*
Lucille Sylvester02e46292011-09-21 14:59:17 -06001217 wait_event_interruptible_timeout checks for the exit condition before
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218 placing a process in wait q. For conditional interrupts we expect the
1219 process to already be in its wait q when its exit condition checking
1220 function is called.
1221*/
Lucille Sylvester02e46292011-09-21 14:59:17 -06001222#define kgsl_wait_event_interruptible_timeout(wq, condition, timeout, io)\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223({ \
1224 long __ret = timeout; \
Lucille Sylvester02e46292011-09-21 14:59:17 -06001225 if (io) \
1226 __wait_io_event_interruptible_timeout(wq, condition, __ret);\
1227 else \
1228 __wait_event_interruptible_timeout(wq, condition, __ret);\
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001229 __ret; \
1230})
1231
1232/* MUST be called with the device mutex held */
1233static int adreno_waittimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001234 struct kgsl_context *context,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001235 unsigned int timestamp,
1236 unsigned int msecs)
1237{
1238 long status = 0;
Lucille Sylvester02e46292011-09-21 14:59:17 -06001239 uint io = 1;
Lucille Sylvester596d4c22011-10-19 18:04:01 -06001240 static uint io_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001241 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Lucille Sylvester02e46292011-09-21 14:59:17 -06001242 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lynus Vaz06a9a902011-10-04 19:25:33 +05301243 int retries;
1244 unsigned int msecs_first;
1245 unsigned int msecs_part;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001246 unsigned int ts_issued;
1247 unsigned int context_id = _get_context_id(context);
1248
1249 ts_issued = adreno_dev->ringbuffer.timestamp[context_id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001250
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +05301251 /* Don't wait forever, set a max value for now */
1252 if (msecs == -1)
1253 msecs = adreno_dev->wait_timeout;
1254
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001255 if (timestamp_cmp(timestamp, ts_issued) > 0) {
1256 KGSL_DRV_ERR(device, "Cannot wait for invalid ts <%d:0x%x>, "
1257 "last issued ts <%d:0x%x>\n",
1258 context_id, timestamp, context_id, ts_issued);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 status = -EINVAL;
1260 goto done;
1261 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001262
Lynus Vaz06a9a902011-10-04 19:25:33 +05301263 /* Keep the first timeout as 100msecs before rewriting
1264 * the WPTR. Less visible impact if the WPTR has not
1265 * been updated properly.
1266 */
1267 msecs_first = (msecs <= 100) ? ((msecs + 4) / 5) : 100;
1268 msecs_part = (msecs - msecs_first + 3) / 4;
1269 for (retries = 0; retries < 5; retries++) {
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001270 /*
1271 * If the context ID is invalid, we are in a race with
1272 * the context being destroyed by userspace so bail.
1273 */
1274 if (context_id == KGSL_CONTEXT_INVALID) {
1275 KGSL_DRV_WARN(device, "context was detached");
1276 status = -EINVAL;
1277 goto done;
1278 }
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001279 if (kgsl_check_timestamp(device, context, timestamp)) {
Jeremy Gebben63904832012-02-07 16:10:55 -07001280 /* if the timestamp happens while we're not
1281 * waiting, there's a chance that an interrupt
1282 * will not be generated and thus the timestamp
1283 * work needs to be queued.
Lynus Vaz06a9a902011-10-04 19:25:33 +05301284 */
Jeremy Gebben63904832012-02-07 16:10:55 -07001285 queue_work(device->work_queue, &device->ts_expired_ws);
1286 status = 0;
1287 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288 }
Jeremy Gebben63904832012-02-07 16:10:55 -07001289 adreno_poke(device);
1290 io_cnt = (io_cnt + 1) % 100;
1291 if (io_cnt <
1292 pwr->pwrlevels[pwr->active_pwrlevel].io_fraction)
1293 io = 0;
1294 mutex_unlock(&device->mutex);
1295 /* We need to make sure that the process is
1296 * placed in wait-q before its condition is called
1297 */
1298 status = kgsl_wait_event_interruptible_timeout(
1299 device->wait_queue,
1300 kgsl_check_interrupt_timestamp(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001301 context, timestamp),
Jeremy Gebben63904832012-02-07 16:10:55 -07001302 msecs_to_jiffies(retries ?
1303 msecs_part : msecs_first), io);
1304 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305
Jeremy Gebben63904832012-02-07 16:10:55 -07001306 if (status > 0) {
1307 /*completed before the wait finished */
1308 status = 0;
1309 goto done;
1310 } else if (status < 0) {
1311 /*an error occurred*/
1312 goto done;
1313 }
1314 /*this wait timed out*/
1315 }
1316 status = -ETIMEDOUT;
1317 KGSL_DRV_ERR(device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001318 "Device hang detected while waiting for timestamp: "
1319 "<%d:0x%x>, last submitted timestamp: <%d:0x%x>, "
1320 "wptr: 0x%x\n",
1321 context_id, timestamp, context_id, ts_issued,
Jeremy Gebben63904832012-02-07 16:10:55 -07001322 adreno_dev->ringbuffer.wptr);
1323 if (!adreno_dump_and_recover(device)) {
1324 /* wait for idle after recovery as the
1325 * timestamp that this process wanted
1326 * to wait on may be invalid */
1327 if (!adreno_idle(device, KGSL_TIMEOUT_DEFAULT))
1328 status = 0;
1329 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330done:
1331 return (int)status;
1332}
1333
1334static unsigned int adreno_readtimestamp(struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001335 struct kgsl_context *context, enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001336{
1337 unsigned int timestamp = 0;
Carter Cooper7e7f02e2012-02-15 09:36:31 -07001338 unsigned int context_id = _get_context_id(context);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339
Jeremy Gebben9ad86922012-05-08 15:33:23 -06001340 /*
1341 * If the context ID is invalid, we are in a race with
1342 * the context being destroyed by userspace so bail.
1343 */
1344 if (context_id == KGSL_CONTEXT_INVALID) {
1345 KGSL_DRV_WARN(device, "context was detached");
1346 return timestamp;
1347 }
Jordan Crousec659f382012-04-16 11:10:41 -06001348 switch (type) {
1349 case KGSL_TIMESTAMP_QUEUED: {
1350 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1351 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
1352
1353 timestamp = rb->timestamp[context_id];
1354 break;
1355 }
1356 case KGSL_TIMESTAMP_CONSUMED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001357 adreno_regread(device, REG_CP_TIMESTAMP, &timestamp);
Jordan Crousec659f382012-04-16 11:10:41 -06001358 break;
1359 case KGSL_TIMESTAMP_RETIRED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360 kgsl_sharedmem_readl(&device->memstore, &timestamp,
Jordan Crousec659f382012-04-16 11:10:41 -06001361 KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp));
1362 break;
1363 }
1364
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365 rmb();
1366
1367 return timestamp;
1368}
1369
1370static long adreno_ioctl(struct kgsl_device_private *dev_priv,
1371 unsigned int cmd, void *data)
1372{
1373 int result = 0;
1374 struct kgsl_drawctxt_set_bin_base_offset *binbase;
1375 struct kgsl_context *context;
1376
1377 switch (cmd) {
1378 case IOCTL_KGSL_DRAWCTXT_SET_BIN_BASE_OFFSET:
1379 binbase = data;
1380
1381 context = kgsl_find_context(dev_priv, binbase->drawctxt_id);
1382 if (context) {
1383 adreno_drawctxt_set_bin_base_offset(
1384 dev_priv->device, context, binbase->offset);
1385 } else {
1386 result = -EINVAL;
1387 KGSL_DRV_ERR(dev_priv->device,
1388 "invalid drawctxt drawctxt_id %d "
1389 "device_id=%d\n",
1390 binbase->drawctxt_id, dev_priv->device->id);
1391 }
1392 break;
1393
1394 default:
1395 KGSL_DRV_INFO(dev_priv->device,
1396 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07001397 result = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001398 break;
1399 }
1400 return result;
1401
1402}
1403
1404static inline s64 adreno_ticks_to_us(u32 ticks, u32 gpu_freq)
1405{
1406 gpu_freq /= 1000000;
1407 return ticks / gpu_freq;
1408}
1409
1410static void adreno_power_stats(struct kgsl_device *device,
1411 struct kgsl_power_stats *stats)
1412{
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001413 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001414 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001415 unsigned int cycles;
1416
1417 /* Get the busy cycles counted since the counter was last reset */
1418 /* Calling this function also resets and restarts the counter */
1419
1420 cycles = adreno_dev->gpudev->busy_cycles(adreno_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001421
1422 /* In order to calculate idle you have to have run the algorithm *
1423 * at least once to get a start time. */
1424 if (pwr->time != 0) {
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001425 s64 tmp = ktime_to_us(ktime_get());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001426 stats->total_time = tmp - pwr->time;
1427 pwr->time = tmp;
Jordan Crouseb4d31bd2012-02-01 22:11:12 -07001428 stats->busy_time = adreno_ticks_to_us(cycles, device->pwrctrl.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001429 pwrlevels[device->pwrctrl.active_pwrlevel].
1430 gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001431 } else {
1432 stats->total_time = 0;
1433 stats->busy_time = 0;
1434 pwr->time = ktime_to_us(ktime_get());
1435 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001436}
1437
1438void adreno_irqctrl(struct kgsl_device *device, int state)
1439{
Jordan Crousea78c9172011-07-11 13:14:09 -06001440 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1441 adreno_dev->gpudev->irq_control(adreno_dev, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001442}
1443
Jordan Crousea0758f22011-12-07 11:19:22 -07001444static unsigned int adreno_gpuid(struct kgsl_device *device)
1445{
1446 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1447
1448 /* Standard KGSL gpuid format:
1449 * top word is 0x0002 for 2D or 0x0003 for 3D
1450 * Bottom word is core specific identifer
1451 */
1452
1453 return (0x0003 << 16) | ((int) adreno_dev->gpurev);
1454}
1455
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001456static const struct kgsl_functable adreno_functable = {
1457 /* Mandatory functions */
1458 .regread = adreno_regread,
1459 .regwrite = adreno_regwrite,
1460 .idle = adreno_idle,
1461 .isidle = adreno_isidle,
1462 .suspend_context = adreno_suspend_context,
1463 .start = adreno_start,
1464 .stop = adreno_stop,
1465 .getproperty = adreno_getproperty,
1466 .waittimestamp = adreno_waittimestamp,
1467 .readtimestamp = adreno_readtimestamp,
1468 .issueibcmds = adreno_ringbuffer_issueibcmds,
1469 .ioctl = adreno_ioctl,
1470 .setup_pt = adreno_setup_pt,
1471 .cleanup_pt = adreno_cleanup_pt,
1472 .power_stats = adreno_power_stats,
1473 .irqctrl = adreno_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -07001474 .gpuid = adreno_gpuid,
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001475 .snapshot = adreno_snapshot,
Jordan Crouseb368e9b2012-04-27 14:01:59 -06001476 .irq_handler = adreno_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001477 /* Optional functions */
1478 .setstate = adreno_setstate,
1479 .drawctxt_create = adreno_drawctxt_create,
1480 .drawctxt_destroy = adreno_drawctxt_destroy,
Jordan Crousef7370f82012-04-18 09:31:07 -06001481 .setproperty = adreno_setproperty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001482};
1483
1484static struct platform_device_id adreno_id_table[] = {
1485 { DEVICE_3D0_NAME, (kernel_ulong_t)&device_3d0.dev, },
1486 { },
1487};
1488MODULE_DEVICE_TABLE(platform, adreno_id_table);
1489
1490static struct platform_driver adreno_platform_driver = {
1491 .probe = adreno_probe,
1492 .remove = __devexit_p(adreno_remove),
1493 .suspend = kgsl_suspend_driver,
1494 .resume = kgsl_resume_driver,
1495 .id_table = adreno_id_table,
1496 .driver = {
1497 .owner = THIS_MODULE,
1498 .name = DEVICE_3D_NAME,
1499 .pm = &kgsl_pm_ops,
1500 }
1501};
1502
1503static int __init kgsl_3d_init(void)
1504{
1505 return platform_driver_register(&adreno_platform_driver);
1506}
1507
1508static void __exit kgsl_3d_exit(void)
1509{
1510 platform_driver_unregister(&adreno_platform_driver);
1511}
1512
1513module_init(kgsl_3d_init);
1514module_exit(kgsl_3d_exit);
1515
1516MODULE_DESCRIPTION("3D Graphics driver");
1517MODULE_VERSION("1.2");
1518MODULE_LICENSE("GPL v2");
1519MODULE_ALIAS("platform:kgsl_3d");