blob: a0982006798fa3b993612a14c9ef6af265ab03fd [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2002,2007-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#include <linux/firmware.h>
14#include <linux/slab.h>
15#include <linux/sched.h>
16#include <linux/log2.h>
17
18#include "kgsl.h"
19#include "kgsl_sharedmem.h"
20#include "kgsl_cffdump.h"
21
22#include "adreno.h"
23#include "adreno_pm4types.h"
24#include "adreno_ringbuffer.h"
25
Jeremy Gebbeneebc4612011-08-31 10:15:21 -070026#include "a2xx_reg.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#define GSL_RB_NOP_SIZEDWORDS 2
29/* protected mode error checking below register address 0x800
30* note: if CP_INTERRUPT packet is used then checking needs
31* to change to below register address 0x7C8
32*/
33#define GSL_RB_PROTECTED_MODE_CONTROL 0x200001F2
34
35/* Firmware file names
36 * Legacy names must remain but replacing macro names to
37 * match current kgsl model.
38 * a200 is yamato
39 * a220 is leia
40 */
41#define A200_PFP_FW "yamato_pfp.fw"
42#define A200_PM4_FW "yamato_pm4.fw"
43#define A220_PFP_470_FW "leia_pfp_470.fw"
44#define A220_PM4_470_FW "leia_pm4_470.fw"
45#define A225_PFP_FW "a225_pfp.fw"
46#define A225_PM4_FW "a225_pm4.fw"
47
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048static void adreno_ringbuffer_submit(struct adreno_ringbuffer *rb)
49{
50 BUG_ON(rb->wptr == 0);
51
Lucille Sylvester958dc942011-09-06 18:19:49 -060052 /* Let the pwrscale policy know that new commands have
53 been submitted. */
54 kgsl_pwrscale_busy(rb->device);
55
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056 /*synchronize memory before informing the hardware of the
57 *new commands.
58 */
59 mb();
60
61 adreno_regwrite(rb->device, REG_CP_RB_WPTR, rb->wptr);
62}
63
Carter Cooper6dd94c82011-10-13 14:43:53 -060064static void
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065adreno_ringbuffer_waitspace(struct adreno_ringbuffer *rb, unsigned int numcmds,
66 int wptr_ahead)
67{
68 int nopcount;
69 unsigned int freecmds;
70 unsigned int *cmds;
71 uint cmds_gpu;
72
73 /* if wptr ahead, fill the remaining with NOPs */
74 if (wptr_ahead) {
75 /* -1 for header */
76 nopcount = rb->sizedwords - rb->wptr - 1;
77
78 cmds = (unsigned int *)rb->buffer_desc.hostptr + rb->wptr;
79 cmds_gpu = rb->buffer_desc.gpuaddr + sizeof(uint)*rb->wptr;
80
Jordan Crouse084427d2011-07-28 08:37:58 -060081 GSL_RB_WRITE(cmds, cmds_gpu, cp_nop_packet(nopcount));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082
83 /* Make sure that rptr is not 0 before submitting
84 * commands at the end of ringbuffer. We do not
85 * want the rptr and wptr to become equal when
86 * the ringbuffer is not empty */
87 do {
88 GSL_RB_GET_READPTR(rb, &rb->rptr);
89 } while (!rb->rptr);
90
91 rb->wptr++;
92
93 adreno_ringbuffer_submit(rb);
94
95 rb->wptr = 0;
96 }
97
98 /* wait for space in ringbuffer */
99 do {
100 GSL_RB_GET_READPTR(rb, &rb->rptr);
101
102 freecmds = rb->rptr - rb->wptr;
103
104 } while ((freecmds != 0) && (freecmds <= numcmds));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105}
106
107
108static unsigned int *adreno_ringbuffer_allocspace(struct adreno_ringbuffer *rb,
109 unsigned int numcmds)
110{
111 unsigned int *ptr = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112
113 BUG_ON(numcmds >= rb->sizedwords);
114
115 GSL_RB_GET_READPTR(rb, &rb->rptr);
116 /* check for available space */
117 if (rb->wptr >= rb->rptr) {
118 /* wptr ahead or equal to rptr */
119 /* reserve dwords for nop packet */
120 if ((rb->wptr + numcmds) > (rb->sizedwords -
121 GSL_RB_NOP_SIZEDWORDS))
Carter Cooper6dd94c82011-10-13 14:43:53 -0600122 adreno_ringbuffer_waitspace(rb, numcmds, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700123 } else {
124 /* wptr behind rptr */
125 if ((rb->wptr + numcmds) >= rb->rptr)
Carter Cooper6dd94c82011-10-13 14:43:53 -0600126 adreno_ringbuffer_waitspace(rb, numcmds, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127 /* check for remaining space */
128 /* reserve dwords for nop packet */
129 if ((rb->wptr + numcmds) > (rb->sizedwords -
130 GSL_RB_NOP_SIZEDWORDS))
Carter Cooper6dd94c82011-10-13 14:43:53 -0600131 adreno_ringbuffer_waitspace(rb, numcmds, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132 }
133
Carter Cooper6dd94c82011-10-13 14:43:53 -0600134 ptr = (unsigned int *)rb->buffer_desc.hostptr + rb->wptr;
135 rb->wptr += numcmds;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136
137 return ptr;
138}
139
140static int _load_firmware(struct kgsl_device *device, const char *fwfile,
141 void **data, int *len)
142{
143 const struct firmware *fw = NULL;
144 int ret;
145
146 ret = request_firmware(&fw, fwfile, device->dev);
147
148 if (ret) {
149 KGSL_DRV_ERR(device, "request_firmware(%s) failed: %d\n",
150 fwfile, ret);
151 return ret;
152 }
153
154 *data = kmalloc(fw->size, GFP_KERNEL);
155
156 if (*data) {
157 memcpy(*data, fw->data, fw->size);
158 *len = fw->size;
159 } else
160 KGSL_MEM_ERR(device, "kmalloc(%d) failed\n", fw->size);
161
162 release_firmware(fw);
163 return (*data != NULL) ? 0 : -ENOMEM;
164}
165
166static int adreno_ringbuffer_load_pm4_ucode(struct kgsl_device *device)
167{
168 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169 int i, ret = 0;
170
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171 if (adreno_dev->pm4_fw == NULL) {
172 int len;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600173 void *ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174
Jordan Crouse505df9c2011-07-28 08:37:59 -0600175 ret = _load_firmware(device, adreno_dev->pm4_fwfile,
176 &ptr, &len);
177
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 if (ret)
179 goto err;
180
181 /* PM4 size is 3 dword aligned plus 1 dword of version */
182 if (len % ((sizeof(uint32_t) * 3)) != sizeof(uint32_t)) {
183 KGSL_DRV_ERR(device, "Bad firmware size: %d\n", len);
184 ret = -EINVAL;
Jeremy Gebben79acee62011-08-08 16:44:07 -0600185 kfree(ptr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186 goto err;
187 }
188
189 adreno_dev->pm4_fw_size = len / sizeof(uint32_t);
190 adreno_dev->pm4_fw = ptr;
191 }
192
193 KGSL_DRV_INFO(device, "loading pm4 ucode version: %d\n",
194 adreno_dev->pm4_fw[0]);
195
196 adreno_regwrite(device, REG_CP_DEBUG, 0x02000000);
197 adreno_regwrite(device, REG_CP_ME_RAM_WADDR, 0);
198 for (i = 1; i < adreno_dev->pm4_fw_size; i++)
199 adreno_regwrite(device, REG_CP_ME_RAM_DATA,
200 adreno_dev->pm4_fw[i]);
201err:
202 return ret;
203}
204
205static int adreno_ringbuffer_load_pfp_ucode(struct kgsl_device *device)
206{
207 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 int i, ret = 0;
209
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 if (adreno_dev->pfp_fw == NULL) {
211 int len;
Jordan Crouse505df9c2011-07-28 08:37:59 -0600212 void *ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213
Jordan Crouse505df9c2011-07-28 08:37:59 -0600214 ret = _load_firmware(device, adreno_dev->pfp_fwfile,
215 &ptr, &len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 if (ret)
217 goto err;
218
219 /* PFP size shold be dword aligned */
220 if (len % sizeof(uint32_t) != 0) {
221 KGSL_DRV_ERR(device, "Bad firmware size: %d\n", len);
222 ret = -EINVAL;
Jeremy Gebben79acee62011-08-08 16:44:07 -0600223 kfree(ptr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 goto err;
225 }
226
227 adreno_dev->pfp_fw_size = len / sizeof(uint32_t);
228 adreno_dev->pfp_fw = ptr;
229 }
230
231 KGSL_DRV_INFO(device, "loading pfp ucode version: %d\n",
232 adreno_dev->pfp_fw[0]);
233
234 adreno_regwrite(device, REG_CP_PFP_UCODE_ADDR, 0);
235 for (i = 1; i < adreno_dev->pfp_fw_size; i++)
236 adreno_regwrite(device, REG_CP_PFP_UCODE_DATA,
237 adreno_dev->pfp_fw[i]);
238err:
239 return ret;
240}
241
242int adreno_ringbuffer_start(struct adreno_ringbuffer *rb, unsigned int init_ram)
243{
244 int status;
245 /*cp_rb_cntl_u cp_rb_cntl; */
246 union reg_cp_rb_cntl cp_rb_cntl;
247 unsigned int *cmds, rb_cntl;
248 struct kgsl_device *device = rb->device;
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700249 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 uint cmds_gpu;
251
252 if (rb->flags & KGSL_FLAGS_STARTED)
253 return 0;
254
255 if (init_ram) {
256 rb->timestamp = 0;
257 GSL_RB_INIT_TIMESTAMP(rb);
258 }
259
260 kgsl_sharedmem_set(&rb->memptrs_desc, 0, 0,
261 sizeof(struct kgsl_rbmemptrs));
262
263 kgsl_sharedmem_set(&rb->buffer_desc, 0, 0xAA,
264 (rb->sizedwords << 2));
265
266 adreno_regwrite(device, REG_CP_RB_WPTR_BASE,
267 (rb->memptrs_desc.gpuaddr
268 + GSL_RB_MEMPTRS_WPTRPOLL_OFFSET));
269
270 /* setup WPTR delay */
271 adreno_regwrite(device, REG_CP_RB_WPTR_DELAY, 0 /*0x70000010 */);
272
273 /*setup REG_CP_RB_CNTL */
274 adreno_regread(device, REG_CP_RB_CNTL, &rb_cntl);
275 cp_rb_cntl.val = rb_cntl;
276
277 /*
278 * The size of the ringbuffer in the hardware is the log2
279 * representation of the size in quadwords (sizedwords / 2)
280 */
281 cp_rb_cntl.f.rb_bufsz = ilog2(rb->sizedwords >> 1);
282
283 /*
284 * Specify the quadwords to read before updating mem RPTR.
285 * Like above, pass the log2 representation of the blocksize
286 * in quadwords.
287 */
288 cp_rb_cntl.f.rb_blksz = ilog2(KGSL_RB_BLKSIZE >> 3);
289
290 cp_rb_cntl.f.rb_poll_en = GSL_RB_CNTL_POLL_EN; /* WPTR polling */
291 /* mem RPTR writebacks */
292 cp_rb_cntl.f.rb_no_update = GSL_RB_CNTL_NO_UPDATE;
293
294 adreno_regwrite(device, REG_CP_RB_CNTL, cp_rb_cntl.val);
295
296 adreno_regwrite(device, REG_CP_RB_BASE, rb->buffer_desc.gpuaddr);
297
298 adreno_regwrite(device, REG_CP_RB_RPTR_ADDR,
299 rb->memptrs_desc.gpuaddr +
300 GSL_RB_MEMPTRS_RPTR_OFFSET);
301
302 /* explicitly clear all cp interrupts */
303 adreno_regwrite(device, REG_CP_INT_ACK, 0xFFFFFFFF);
304
305 /* setup scratch/timestamp */
306 adreno_regwrite(device, REG_SCRATCH_ADDR,
307 device->memstore.gpuaddr +
308 KGSL_DEVICE_MEMSTORE_OFFSET(soptimestamp));
309
310 adreno_regwrite(device, REG_SCRATCH_UMSK,
311 GSL_RB_MEMPTRS_SCRATCH_MASK);
312
313 /* load the CP ucode */
314
315 status = adreno_ringbuffer_load_pm4_ucode(device);
316 if (status != 0)
317 return status;
318
319 /* load the prefetch parser ucode */
320 status = adreno_ringbuffer_load_pfp_ucode(device);
321 if (status != 0)
322 return status;
323
324 adreno_regwrite(device, REG_CP_QUEUE_THRESHOLDS, 0x000C0804);
325
326 rb->rptr = 0;
327 rb->wptr = 0;
328
329 /* clear ME_HALT to start micro engine */
330 adreno_regwrite(device, REG_CP_ME_CNTL, 0);
331
332 /* ME_INIT */
333 cmds = adreno_ringbuffer_allocspace(rb, 19);
334 cmds_gpu = rb->buffer_desc.gpuaddr + sizeof(uint)*(rb->wptr-19);
335
Jordan Crouse084427d2011-07-28 08:37:58 -0600336 GSL_RB_WRITE(cmds, cmds_gpu, CP_HDR_ME_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 /* All fields present (bits 9:0) */
338 GSL_RB_WRITE(cmds, cmds_gpu, 0x000003ff);
339 /* Disable/Enable Real-Time Stream processing (present but ignored) */
340 GSL_RB_WRITE(cmds, cmds_gpu, 0x00000000);
341 /* Enable (2D <-> 3D) implicit synchronization (present but ignored) */
342 GSL_RB_WRITE(cmds, cmds_gpu, 0x00000000);
343
344 GSL_RB_WRITE(cmds, cmds_gpu,
Jordan Crouse0e0486f2011-07-28 08:37:58 -0600345 SUBBLOCK_OFFSET(REG_RB_SURFACE_INFO));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346 GSL_RB_WRITE(cmds, cmds_gpu,
Jordan Crouse0e0486f2011-07-28 08:37:58 -0600347 SUBBLOCK_OFFSET(REG_PA_SC_WINDOW_OFFSET));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348 GSL_RB_WRITE(cmds, cmds_gpu,
Jordan Crouse0e0486f2011-07-28 08:37:58 -0600349 SUBBLOCK_OFFSET(REG_VGT_MAX_VTX_INDX));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350 GSL_RB_WRITE(cmds, cmds_gpu,
Jordan Crouse0e0486f2011-07-28 08:37:58 -0600351 SUBBLOCK_OFFSET(REG_SQ_PROGRAM_CNTL));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352 GSL_RB_WRITE(cmds, cmds_gpu,
Jordan Crouse0e0486f2011-07-28 08:37:58 -0600353 SUBBLOCK_OFFSET(REG_RB_DEPTHCONTROL));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354 GSL_RB_WRITE(cmds, cmds_gpu,
Jordan Crouse0e0486f2011-07-28 08:37:58 -0600355 SUBBLOCK_OFFSET(REG_PA_SU_POINT_SIZE));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 GSL_RB_WRITE(cmds, cmds_gpu,
Jordan Crouse0e0486f2011-07-28 08:37:58 -0600357 SUBBLOCK_OFFSET(REG_PA_SC_LINE_CNTL));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358 GSL_RB_WRITE(cmds, cmds_gpu,
Jordan Crouse0e0486f2011-07-28 08:37:58 -0600359 SUBBLOCK_OFFSET(REG_PA_SU_POLY_OFFSET_FRONT_SCALE));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360
Jeremy Gebbenddf6b572011-09-09 13:39:49 -0700361 /* Instruction memory size: */
362 GSL_RB_WRITE(cmds, cmds_gpu,
363 (adreno_encode_istore_size(adreno_dev)
364 | adreno_dev->pix_shader_start));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365 /* Maximum Contexts */
366 GSL_RB_WRITE(cmds, cmds_gpu, 0x00000001);
367 /* Write Confirm Interval and The CP will wait the
368 * wait_interval * 16 clocks between polling */
369 GSL_RB_WRITE(cmds, cmds_gpu, 0x00000000);
370
371 /* NQ and External Memory Swap */
372 GSL_RB_WRITE(cmds, cmds_gpu, 0x00000000);
373 /* Protected mode error checking */
374 GSL_RB_WRITE(cmds, cmds_gpu, GSL_RB_PROTECTED_MODE_CONTROL);
375 /* Disable header dumping and Header dump address */
376 GSL_RB_WRITE(cmds, cmds_gpu, 0x00000000);
377 /* Header dump size */
378 GSL_RB_WRITE(cmds, cmds_gpu, 0x00000000);
379
380 adreno_ringbuffer_submit(rb);
381
382 /* idle device to validate ME INIT */
383 status = adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
384
385 if (status == 0)
386 rb->flags |= KGSL_FLAGS_STARTED;
387
388 return status;
389}
390
Carter Cooper6dd94c82011-10-13 14:43:53 -0600391void adreno_ringbuffer_stop(struct adreno_ringbuffer *rb)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392{
393 if (rb->flags & KGSL_FLAGS_STARTED) {
394 /* ME_HALT */
395 adreno_regwrite(rb->device, REG_CP_ME_CNTL, 0x10000000);
396
397 rb->flags &= ~KGSL_FLAGS_STARTED;
398 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399}
400
401int adreno_ringbuffer_init(struct kgsl_device *device)
402{
403 int status;
404 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
405 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
406
407 rb->device = device;
408 /*
409 * It is silly to convert this to words and then back to bytes
410 * immediately below, but most of the rest of the code deals
411 * in words, so we might as well only do the math once
412 */
413 rb->sizedwords = KGSL_RB_SIZE >> 2;
414
415 /* allocate memory for ringbuffer */
416 status = kgsl_allocate_contiguous(&rb->buffer_desc,
417 (rb->sizedwords << 2));
418
419 if (status != 0) {
420 adreno_ringbuffer_close(rb);
421 return status;
422 }
423
424 /* allocate memory for polling and timestamps */
425 /* This really can be at 4 byte alignment boundry but for using MMU
426 * we need to make it at page boundary */
427 status = kgsl_allocate_contiguous(&rb->memptrs_desc,
428 sizeof(struct kgsl_rbmemptrs));
429
430 if (status != 0) {
431 adreno_ringbuffer_close(rb);
432 return status;
433 }
434
435 /* overlay structure on memptrs memory */
436 rb->memptrs = (struct kgsl_rbmemptrs *) rb->memptrs_desc.hostptr;
437
438 return 0;
439}
440
Carter Cooper6dd94c82011-10-13 14:43:53 -0600441void adreno_ringbuffer_close(struct adreno_ringbuffer *rb)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442{
443 struct adreno_device *adreno_dev = ADRENO_DEVICE(rb->device);
444
445 kgsl_sharedmem_free(&rb->buffer_desc);
446 kgsl_sharedmem_free(&rb->memptrs_desc);
447
448 kfree(adreno_dev->pfp_fw);
449 kfree(adreno_dev->pm4_fw);
450
451 adreno_dev->pfp_fw = NULL;
452 adreno_dev->pm4_fw = NULL;
453
454 memset(rb, 0, sizeof(struct adreno_ringbuffer));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455}
456
457static uint32_t
458adreno_ringbuffer_addcmds(struct adreno_ringbuffer *rb,
459 unsigned int flags, unsigned int *cmds,
460 int sizedwords)
461{
462 unsigned int *ringcmds;
463 unsigned int timestamp;
464 unsigned int total_sizedwords = sizedwords + 6;
465 unsigned int i;
466 unsigned int rcmd_gpu;
467
468 /* reserve space to temporarily turn off protected mode
469 * error checking if needed
470 */
471 total_sizedwords += flags & KGSL_CMD_FLAGS_PMODE ? 4 : 0;
472 total_sizedwords += !(flags & KGSL_CMD_FLAGS_NO_TS_CMP) ? 7 : 0;
473 total_sizedwords += !(flags & KGSL_CMD_FLAGS_NOT_KERNEL_CMD) ? 2 : 0;
474
475 ringcmds = adreno_ringbuffer_allocspace(rb, total_sizedwords);
476 rcmd_gpu = rb->buffer_desc.gpuaddr
477 + sizeof(uint)*(rb->wptr-total_sizedwords);
478
479 if (!(flags & KGSL_CMD_FLAGS_NOT_KERNEL_CMD)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600480 GSL_RB_WRITE(ringcmds, rcmd_gpu, cp_nop_packet(1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 GSL_RB_WRITE(ringcmds, rcmd_gpu, KGSL_CMD_IDENTIFIER);
482 }
483 if (flags & KGSL_CMD_FLAGS_PMODE) {
484 /* disable protected mode error checking */
485 GSL_RB_WRITE(ringcmds, rcmd_gpu,
Jordan Crouse084427d2011-07-28 08:37:58 -0600486 cp_type3_packet(CP_SET_PROTECTED_MODE, 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 GSL_RB_WRITE(ringcmds, rcmd_gpu, 0);
488 }
489
490 for (i = 0; i < sizedwords; i++) {
491 GSL_RB_WRITE(ringcmds, rcmd_gpu, *cmds);
492 cmds++;
493 }
494
495 if (flags & KGSL_CMD_FLAGS_PMODE) {
496 /* re-enable protected mode error checking */
497 GSL_RB_WRITE(ringcmds, rcmd_gpu,
Jordan Crouse084427d2011-07-28 08:37:58 -0600498 cp_type3_packet(CP_SET_PROTECTED_MODE, 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499 GSL_RB_WRITE(ringcmds, rcmd_gpu, 1);
500 }
501
502 rb->timestamp++;
503 timestamp = rb->timestamp;
504
505 /* start-of-pipeline and end-of-pipeline timestamps */
Jordan Crouse084427d2011-07-28 08:37:58 -0600506 GSL_RB_WRITE(ringcmds, rcmd_gpu, cp_type0_packet(REG_CP_TIMESTAMP, 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 GSL_RB_WRITE(ringcmds, rcmd_gpu, rb->timestamp);
Jordan Crouse084427d2011-07-28 08:37:58 -0600508 GSL_RB_WRITE(ringcmds, rcmd_gpu, cp_type3_packet(CP_EVENT_WRITE, 3));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 GSL_RB_WRITE(ringcmds, rcmd_gpu, CACHE_FLUSH_TS);
510 GSL_RB_WRITE(ringcmds, rcmd_gpu,
511 (rb->device->memstore.gpuaddr +
512 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp)));
513 GSL_RB_WRITE(ringcmds, rcmd_gpu, rb->timestamp);
514
515 if (!(flags & KGSL_CMD_FLAGS_NO_TS_CMP)) {
516 /* Conditional execution based on memory values */
517 GSL_RB_WRITE(ringcmds, rcmd_gpu,
Jordan Crouse084427d2011-07-28 08:37:58 -0600518 cp_type3_packet(CP_COND_EXEC, 4));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 GSL_RB_WRITE(ringcmds, rcmd_gpu, (rb->device->memstore.gpuaddr +
520 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable)) >> 2);
521 GSL_RB_WRITE(ringcmds, rcmd_gpu, (rb->device->memstore.gpuaddr +
522 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts)) >> 2);
523 GSL_RB_WRITE(ringcmds, rcmd_gpu, rb->timestamp);
524 /* # of conditional command DWORDs */
525 GSL_RB_WRITE(ringcmds, rcmd_gpu, 2);
526 GSL_RB_WRITE(ringcmds, rcmd_gpu,
Jordan Crouse084427d2011-07-28 08:37:58 -0600527 cp_type3_packet(CP_INTERRUPT, 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 GSL_RB_WRITE(ringcmds, rcmd_gpu, CP_INT_CNTL__RB_INT_MASK);
529 }
530
531 adreno_ringbuffer_submit(rb);
532
533 /* return timestamp of issued coREG_ands */
534 return timestamp;
535}
536
537void
538adreno_ringbuffer_issuecmds(struct kgsl_device *device,
539 unsigned int flags,
540 unsigned int *cmds,
541 int sizedwords)
542{
543 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
544 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
545
546 if (device->state & KGSL_STATE_HUNG)
547 return;
548 adreno_ringbuffer_addcmds(rb, flags, cmds, sizedwords);
549}
550
551int
552adreno_ringbuffer_issueibcmds(struct kgsl_device_private *dev_priv,
553 struct kgsl_context *context,
554 struct kgsl_ibdesc *ibdesc,
555 unsigned int numibs,
556 uint32_t *timestamp,
557 unsigned int flags)
558{
559 struct kgsl_device *device = dev_priv->device;
560 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
561 unsigned int *link;
562 unsigned int *cmds;
563 unsigned int i;
Jeremy Gebben3c127f52011-08-08 17:04:11 -0600564 struct adreno_context *drawctxt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565
566 if (device->state & KGSL_STATE_HUNG)
567 return -EBUSY;
568 if (!(adreno_dev->ringbuffer.flags & KGSL_FLAGS_STARTED) ||
Jeremy Gebben3c127f52011-08-08 17:04:11 -0600569 context == NULL || ibdesc == 0 || numibs == 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 return -EINVAL;
571
Jeremy Gebben3c127f52011-08-08 17:04:11 -0600572 drawctxt = context->devctxt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573
574 if (drawctxt->flags & CTXT_FLAGS_GPU_HANG) {
575 KGSL_CTXT_WARN(device, "Context %p caused a gpu hang.."
576 " will not accept commands for this context\n",
577 drawctxt);
578 return -EDEADLK;
579 }
580 link = kzalloc(sizeof(unsigned int) * numibs * 3, GFP_KERNEL);
581 cmds = link;
582 if (!link) {
583 KGSL_MEM_ERR(device, "Failed to allocate memory for for command"
584 " submission, size %x\n", numibs * 3);
585 return -ENOMEM;
586 }
587 for (i = 0; i < numibs; i++) {
588 (void)kgsl_cffdump_parse_ibs(dev_priv, NULL,
589 ibdesc[i].gpuaddr, ibdesc[i].sizedwords, false);
590
Jordan Crouse084427d2011-07-28 08:37:58 -0600591 *cmds++ = CP_HDR_INDIRECT_BUFFER_PFD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592 *cmds++ = ibdesc[i].gpuaddr;
593 *cmds++ = ibdesc[i].sizedwords;
594 }
595
596 kgsl_setstate(device,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600597 kgsl_mmu_pt_get_flags(device->mmu.hwpagetable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598 device->id));
599
600 adreno_drawctxt_switch(adreno_dev, drawctxt, flags);
601
602 *timestamp = adreno_ringbuffer_addcmds(&adreno_dev->ringbuffer,
603 KGSL_CMD_FLAGS_NOT_KERNEL_CMD,
604 &link[0], (cmds - link));
605
606 KGSL_CMD_INFO(device, "ctxt %d g %08x numibs %d ts %d\n",
607 context->id, (unsigned int)ibdesc, numibs, *timestamp);
608
609 kfree(link);
610
611#ifdef CONFIG_MSM_KGSL_CFF_DUMP
612 /*
613 * insert wait for idle after every IB1
614 * this is conservative but works reliably and is ok
615 * even for performance simulations
616 */
617 adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
618#endif
619
620 return 0;
621}
622
623int adreno_ringbuffer_extract(struct adreno_ringbuffer *rb,
624 unsigned int *temp_rb_buffer,
625 int *rb_size)
626{
627 struct kgsl_device *device = rb->device;
628 unsigned int rb_rptr;
629 unsigned int retired_timestamp;
630 unsigned int temp_idx = 0;
631 unsigned int value;
632 unsigned int val1;
633 unsigned int val2;
634 unsigned int val3;
635 unsigned int copy_rb_contents = 0;
636 unsigned int cur_context;
637 unsigned int j;
638
639 GSL_RB_GET_READPTR(rb, &rb->rptr);
640
641 retired_timestamp = device->ftbl->readtimestamp(device,
642 KGSL_TIMESTAMP_RETIRED);
643 KGSL_DRV_ERR(device, "GPU successfully executed till ts: %x\n",
644 retired_timestamp);
645 /*
646 * We need to go back in history by 4 dwords from the current location
647 * of read pointer as 4 dwords are read to match the end of a command.
648 * Also, take care of wrap around when moving back
649 */
650 if (rb->rptr >= 4)
651 rb_rptr = (rb->rptr - 4) * sizeof(unsigned int);
652 else
653 rb_rptr = rb->buffer_desc.size -
654 ((4 - rb->rptr) * sizeof(unsigned int));
655 /* Read the rb contents going backwards to locate end of last
656 * sucessfully executed command */
657 while ((rb_rptr / sizeof(unsigned int)) != rb->wptr) {
658 kgsl_sharedmem_readl(&rb->buffer_desc, &value, rb_rptr);
659 if (value == retired_timestamp) {
660 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
661 rb->buffer_desc.size);
662 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, rb_rptr);
663 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
664 rb->buffer_desc.size);
665 kgsl_sharedmem_readl(&rb->buffer_desc, &val2, rb_rptr);
666 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
667 rb->buffer_desc.size);
668 kgsl_sharedmem_readl(&rb->buffer_desc, &val3, rb_rptr);
669 /* match the pattern found at the end of a command */
670 if ((val1 == 2 &&
Jordan Crouse084427d2011-07-28 08:37:58 -0600671 val2 == cp_type3_packet(CP_INTERRUPT, 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672 && val3 == CP_INT_CNTL__RB_INT_MASK) ||
Jordan Crouse084427d2011-07-28 08:37:58 -0600673 (val1 == cp_type3_packet(CP_EVENT_WRITE, 3)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 && val2 == CACHE_FLUSH_TS &&
675 val3 == (rb->device->memstore.gpuaddr +
676 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp)))) {
677 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
678 rb->buffer_desc.size);
679 KGSL_DRV_ERR(device,
680 "Found end of last executed "
681 "command at offset: %x\n",
682 rb_rptr / sizeof(unsigned int));
683 break;
684 } else {
685 if (rb_rptr < (3 * sizeof(unsigned int)))
686 rb_rptr = rb->buffer_desc.size -
687 (3 * sizeof(unsigned int))
688 + rb_rptr;
689 else
690 rb_rptr -= (3 * sizeof(unsigned int));
691 }
692 }
693
694 if (rb_rptr == 0)
695 rb_rptr = rb->buffer_desc.size - sizeof(unsigned int);
696 else
697 rb_rptr -= sizeof(unsigned int);
698 }
699
700 if ((rb_rptr / sizeof(unsigned int)) == rb->wptr) {
701 KGSL_DRV_ERR(device,
702 "GPU recovery from hang not possible because last"
703 " successful timestamp is overwritten\n");
704 return -EINVAL;
705 }
706 /* rb_rptr is now pointing to the first dword of the command following
707 * the last sucessfully executed command sequence. Assumption is that
708 * GPU is hung in the command sequence pointed by rb_rptr */
709 /* make sure the GPU is not hung in a command submitted by kgsl
710 * itself */
711 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, rb_rptr);
712 kgsl_sharedmem_readl(&rb->buffer_desc, &val2,
713 adreno_ringbuffer_inc_wrapped(rb_rptr,
714 rb->buffer_desc.size));
Jordan Crouse084427d2011-07-28 08:37:58 -0600715 if (val1 == cp_nop_packet(1) && val2 == KGSL_CMD_IDENTIFIER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716 KGSL_DRV_ERR(device,
717 "GPU recovery from hang not possible because "
718 "of hang in kgsl command\n");
719 return -EINVAL;
720 }
721
722 /* current_context is the context that is presently active in the
723 * GPU, i.e the context in which the hang is caused */
724 kgsl_sharedmem_readl(&device->memstore, &cur_context,
725 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
726 while ((rb_rptr / sizeof(unsigned int)) != rb->wptr) {
727 kgsl_sharedmem_readl(&rb->buffer_desc, &value, rb_rptr);
728 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
729 rb->buffer_desc.size);
730 /* check for context switch indicator */
731 if (value == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
732 kgsl_sharedmem_readl(&rb->buffer_desc, &value, rb_rptr);
733 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
734 rb->buffer_desc.size);
Jordan Crouse084427d2011-07-28 08:37:58 -0600735 BUG_ON(value != cp_type3_packet(CP_MEM_WRITE, 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, rb_rptr);
737 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
738 rb->buffer_desc.size);
739 BUG_ON(val1 != (device->memstore.gpuaddr +
740 KGSL_DEVICE_MEMSTORE_OFFSET(current_context)));
741 kgsl_sharedmem_readl(&rb->buffer_desc, &value, rb_rptr);
742 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
743 rb->buffer_desc.size);
744 BUG_ON((copy_rb_contents == 0) &&
745 (value == cur_context));
746 /*
747 * If we were copying the commands and got to this point
748 * then we need to remove the 3 commands that appear
749 * before KGSL_CONTEXT_TO_MEM_IDENTIFIER
750 */
751 if (temp_idx)
752 temp_idx -= 3;
753 /* if context switches to a context that did not cause
754 * hang then start saving the rb contents as those
755 * commands can be executed */
756 if (value != cur_context) {
757 copy_rb_contents = 1;
Jordan Crouse084427d2011-07-28 08:37:58 -0600758 temp_rb_buffer[temp_idx++] = cp_nop_packet(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 temp_rb_buffer[temp_idx++] =
760 KGSL_CMD_IDENTIFIER;
Jordan Crouse084427d2011-07-28 08:37:58 -0600761 temp_rb_buffer[temp_idx++] = cp_nop_packet(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 temp_rb_buffer[temp_idx++] =
763 KGSL_CONTEXT_TO_MEM_IDENTIFIER;
764 temp_rb_buffer[temp_idx++] =
Jordan Crouse084427d2011-07-28 08:37:58 -0600765 cp_type3_packet(CP_MEM_WRITE, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766 temp_rb_buffer[temp_idx++] = val1;
767 temp_rb_buffer[temp_idx++] = value;
768 } else {
769 copy_rb_contents = 0;
770 }
771 } else if (copy_rb_contents)
772 temp_rb_buffer[temp_idx++] = value;
773 }
774
775 *rb_size = temp_idx;
776 KGSL_DRV_ERR(device, "Extracted rb contents, size: %x\n", *rb_size);
777 for (temp_idx = 0; temp_idx < *rb_size;) {
778 char str[80];
779 int idx = 0;
780 if ((temp_idx + 8) <= *rb_size)
781 j = 8;
782 else
783 j = *rb_size - temp_idx;
784 for (; j != 0; j--)
785 idx += scnprintf(str + idx, 80 - idx,
786 "%8.8X ", temp_rb_buffer[temp_idx++]);
787 printk(KERN_ALERT "%s", str);
788 }
789 return 0;
790}
791
792void
793adreno_ringbuffer_restore(struct adreno_ringbuffer *rb, unsigned int *rb_buff,
794 int num_rb_contents)
795{
796 int i;
797 unsigned int *ringcmds;
798 unsigned int rcmd_gpu;
799
800 if (!num_rb_contents)
801 return;
802
803 if (num_rb_contents > (rb->buffer_desc.size - rb->wptr)) {
804 adreno_regwrite(rb->device, REG_CP_RB_RPTR, 0);
805 rb->rptr = 0;
806 BUG_ON(num_rb_contents > rb->buffer_desc.size);
807 }
808 ringcmds = (unsigned int *)rb->buffer_desc.hostptr + rb->wptr;
809 rcmd_gpu = rb->buffer_desc.gpuaddr + sizeof(unsigned int) * rb->wptr;
810 for (i = 0; i < num_rb_contents; i++)
811 GSL_RB_WRITE(ringcmds, rcmd_gpu, rb_buff[i]);
812 rb->wptr += num_rb_contents;
813 adreno_ringbuffer_submit(rb);
814}