blob: f1748d77ebba8559c47a3738809c6369d9c0cda6 [file] [log] [blame]
Vijay Krishnamoorthybef66932012-01-24 09:32:05 -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 */
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);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396 rb->flags &= ~KGSL_FLAGS_STARTED;
397 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398}
399
400int adreno_ringbuffer_init(struct kgsl_device *device)
401{
402 int status;
403 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
404 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
405
406 rb->device = device;
407 /*
408 * It is silly to convert this to words and then back to bytes
409 * immediately below, but most of the rest of the code deals
410 * in words, so we might as well only do the math once
411 */
412 rb->sizedwords = KGSL_RB_SIZE >> 2;
413
414 /* allocate memory for ringbuffer */
415 status = kgsl_allocate_contiguous(&rb->buffer_desc,
416 (rb->sizedwords << 2));
417
418 if (status != 0) {
419 adreno_ringbuffer_close(rb);
420 return status;
421 }
422
423 /* allocate memory for polling and timestamps */
424 /* This really can be at 4 byte alignment boundry but for using MMU
425 * we need to make it at page boundary */
426 status = kgsl_allocate_contiguous(&rb->memptrs_desc,
427 sizeof(struct kgsl_rbmemptrs));
428
429 if (status != 0) {
430 adreno_ringbuffer_close(rb);
431 return status;
432 }
433
434 /* overlay structure on memptrs memory */
435 rb->memptrs = (struct kgsl_rbmemptrs *) rb->memptrs_desc.hostptr;
436
437 return 0;
438}
439
Carter Cooper6dd94c82011-10-13 14:43:53 -0600440void adreno_ringbuffer_close(struct adreno_ringbuffer *rb)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441{
442 struct adreno_device *adreno_dev = ADRENO_DEVICE(rb->device);
443
444 kgsl_sharedmem_free(&rb->buffer_desc);
445 kgsl_sharedmem_free(&rb->memptrs_desc);
446
447 kfree(adreno_dev->pfp_fw);
448 kfree(adreno_dev->pm4_fw);
449
450 adreno_dev->pfp_fw = NULL;
451 adreno_dev->pm4_fw = NULL;
452
453 memset(rb, 0, sizeof(struct adreno_ringbuffer));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454}
455
456static uint32_t
457adreno_ringbuffer_addcmds(struct adreno_ringbuffer *rb,
458 unsigned int flags, unsigned int *cmds,
459 int sizedwords)
460{
461 unsigned int *ringcmds;
462 unsigned int timestamp;
463 unsigned int total_sizedwords = sizedwords + 6;
464 unsigned int i;
465 unsigned int rcmd_gpu;
466
467 /* reserve space to temporarily turn off protected mode
468 * error checking if needed
469 */
470 total_sizedwords += flags & KGSL_CMD_FLAGS_PMODE ? 4 : 0;
471 total_sizedwords += !(flags & KGSL_CMD_FLAGS_NO_TS_CMP) ? 7 : 0;
472 total_sizedwords += !(flags & KGSL_CMD_FLAGS_NOT_KERNEL_CMD) ? 2 : 0;
473
474 ringcmds = adreno_ringbuffer_allocspace(rb, total_sizedwords);
475 rcmd_gpu = rb->buffer_desc.gpuaddr
476 + sizeof(uint)*(rb->wptr-total_sizedwords);
477
478 if (!(flags & KGSL_CMD_FLAGS_NOT_KERNEL_CMD)) {
Jordan Crouse084427d2011-07-28 08:37:58 -0600479 GSL_RB_WRITE(ringcmds, rcmd_gpu, cp_nop_packet(1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 GSL_RB_WRITE(ringcmds, rcmd_gpu, KGSL_CMD_IDENTIFIER);
481 }
482 if (flags & KGSL_CMD_FLAGS_PMODE) {
483 /* disable protected mode error checking */
484 GSL_RB_WRITE(ringcmds, rcmd_gpu,
Jordan Crouse084427d2011-07-28 08:37:58 -0600485 cp_type3_packet(CP_SET_PROTECTED_MODE, 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 GSL_RB_WRITE(ringcmds, rcmd_gpu, 0);
487 }
488
489 for (i = 0; i < sizedwords; i++) {
490 GSL_RB_WRITE(ringcmds, rcmd_gpu, *cmds);
491 cmds++;
492 }
493
494 if (flags & KGSL_CMD_FLAGS_PMODE) {
495 /* re-enable protected mode error checking */
496 GSL_RB_WRITE(ringcmds, rcmd_gpu,
Jordan Crouse084427d2011-07-28 08:37:58 -0600497 cp_type3_packet(CP_SET_PROTECTED_MODE, 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 GSL_RB_WRITE(ringcmds, rcmd_gpu, 1);
499 }
500
501 rb->timestamp++;
502 timestamp = rb->timestamp;
503
504 /* start-of-pipeline and end-of-pipeline timestamps */
Jordan Crouse084427d2011-07-28 08:37:58 -0600505 GSL_RB_WRITE(ringcmds, rcmd_gpu, cp_type0_packet(REG_CP_TIMESTAMP, 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 GSL_RB_WRITE(ringcmds, rcmd_gpu, rb->timestamp);
Jordan Crouse084427d2011-07-28 08:37:58 -0600507 GSL_RB_WRITE(ringcmds, rcmd_gpu, cp_type3_packet(CP_EVENT_WRITE, 3));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 GSL_RB_WRITE(ringcmds, rcmd_gpu, CACHE_FLUSH_TS);
509 GSL_RB_WRITE(ringcmds, rcmd_gpu,
510 (rb->device->memstore.gpuaddr +
511 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp)));
512 GSL_RB_WRITE(ringcmds, rcmd_gpu, rb->timestamp);
513
514 if (!(flags & KGSL_CMD_FLAGS_NO_TS_CMP)) {
515 /* Conditional execution based on memory values */
516 GSL_RB_WRITE(ringcmds, rcmd_gpu,
Jordan Crouse084427d2011-07-28 08:37:58 -0600517 cp_type3_packet(CP_COND_EXEC, 4));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518 GSL_RB_WRITE(ringcmds, rcmd_gpu, (rb->device->memstore.gpuaddr +
519 KGSL_DEVICE_MEMSTORE_OFFSET(ts_cmp_enable)) >> 2);
520 GSL_RB_WRITE(ringcmds, rcmd_gpu, (rb->device->memstore.gpuaddr +
521 KGSL_DEVICE_MEMSTORE_OFFSET(ref_wait_ts)) >> 2);
522 GSL_RB_WRITE(ringcmds, rcmd_gpu, rb->timestamp);
523 /* # of conditional command DWORDs */
524 GSL_RB_WRITE(ringcmds, rcmd_gpu, 2);
525 GSL_RB_WRITE(ringcmds, rcmd_gpu,
Jordan Crouse084427d2011-07-28 08:37:58 -0600526 cp_type3_packet(CP_INTERRUPT, 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 GSL_RB_WRITE(ringcmds, rcmd_gpu, CP_INT_CNTL__RB_INT_MASK);
528 }
529
530 adreno_ringbuffer_submit(rb);
531
532 /* return timestamp of issued coREG_ands */
533 return timestamp;
534}
535
536void
537adreno_ringbuffer_issuecmds(struct kgsl_device *device,
538 unsigned int flags,
539 unsigned int *cmds,
540 int sizedwords)
541{
542 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
543 struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
544
545 if (device->state & KGSL_STATE_HUNG)
546 return;
547 adreno_ringbuffer_addcmds(rb, flags, cmds, sizedwords);
548}
549
550int
551adreno_ringbuffer_issueibcmds(struct kgsl_device_private *dev_priv,
552 struct kgsl_context *context,
553 struct kgsl_ibdesc *ibdesc,
554 unsigned int numibs,
555 uint32_t *timestamp,
556 unsigned int flags)
557{
558 struct kgsl_device *device = dev_priv->device;
559 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
560 unsigned int *link;
561 unsigned int *cmds;
562 unsigned int i;
Jeremy Gebben3c127f52011-08-08 17:04:11 -0600563 struct adreno_context *drawctxt;
Vijay Krishnamoorthybef66932012-01-24 09:32:05 -0700564 unsigned int start_index = 0;
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 }
Vijay Krishnamoorthybef66932012-01-24 09:32:05 -0700587
588 /*When preamble is enabled, the preamble buffer with state restoration
589 commands are stored in the first node of the IB chain. We can skip that
590 if a context switch hasn't occured */
591
592 if (drawctxt->flags & CTXT_FLAGS_PREAMBLE &&
593 adreno_dev->drawctxt_active == drawctxt)
594 start_index = 1;
595
596 for (i = start_index; i < numibs; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 (void)kgsl_cffdump_parse_ibs(dev_priv, NULL,
598 ibdesc[i].gpuaddr, ibdesc[i].sizedwords, false);
599
Jordan Crouse084427d2011-07-28 08:37:58 -0600600 *cmds++ = CP_HDR_INDIRECT_BUFFER_PFD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601 *cmds++ = ibdesc[i].gpuaddr;
602 *cmds++ = ibdesc[i].sizedwords;
603 }
604
605 kgsl_setstate(device,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600606 kgsl_mmu_pt_get_flags(device->mmu.hwpagetable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 device->id));
608
609 adreno_drawctxt_switch(adreno_dev, drawctxt, flags);
610
611 *timestamp = adreno_ringbuffer_addcmds(&adreno_dev->ringbuffer,
612 KGSL_CMD_FLAGS_NOT_KERNEL_CMD,
613 &link[0], (cmds - link));
614
615 KGSL_CMD_INFO(device, "ctxt %d g %08x numibs %d ts %d\n",
616 context->id, (unsigned int)ibdesc, numibs, *timestamp);
617
618 kfree(link);
619
620#ifdef CONFIG_MSM_KGSL_CFF_DUMP
621 /*
622 * insert wait for idle after every IB1
623 * this is conservative but works reliably and is ok
624 * even for performance simulations
625 */
626 adreno_idle(device, KGSL_TIMEOUT_DEFAULT);
627#endif
628
629 return 0;
630}
631
632int adreno_ringbuffer_extract(struct adreno_ringbuffer *rb,
633 unsigned int *temp_rb_buffer,
634 int *rb_size)
635{
636 struct kgsl_device *device = rb->device;
637 unsigned int rb_rptr;
638 unsigned int retired_timestamp;
639 unsigned int temp_idx = 0;
640 unsigned int value;
641 unsigned int val1;
642 unsigned int val2;
643 unsigned int val3;
644 unsigned int copy_rb_contents = 0;
645 unsigned int cur_context;
646 unsigned int j;
647
648 GSL_RB_GET_READPTR(rb, &rb->rptr);
649
650 retired_timestamp = device->ftbl->readtimestamp(device,
651 KGSL_TIMESTAMP_RETIRED);
652 KGSL_DRV_ERR(device, "GPU successfully executed till ts: %x\n",
653 retired_timestamp);
654 /*
655 * We need to go back in history by 4 dwords from the current location
656 * of read pointer as 4 dwords are read to match the end of a command.
657 * Also, take care of wrap around when moving back
658 */
659 if (rb->rptr >= 4)
660 rb_rptr = (rb->rptr - 4) * sizeof(unsigned int);
661 else
662 rb_rptr = rb->buffer_desc.size -
663 ((4 - rb->rptr) * sizeof(unsigned int));
664 /* Read the rb contents going backwards to locate end of last
665 * sucessfully executed command */
666 while ((rb_rptr / sizeof(unsigned int)) != rb->wptr) {
667 kgsl_sharedmem_readl(&rb->buffer_desc, &value, rb_rptr);
668 if (value == retired_timestamp) {
669 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
670 rb->buffer_desc.size);
671 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, rb_rptr);
672 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
673 rb->buffer_desc.size);
674 kgsl_sharedmem_readl(&rb->buffer_desc, &val2, rb_rptr);
675 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
676 rb->buffer_desc.size);
677 kgsl_sharedmem_readl(&rb->buffer_desc, &val3, rb_rptr);
678 /* match the pattern found at the end of a command */
679 if ((val1 == 2 &&
Jordan Crouse084427d2011-07-28 08:37:58 -0600680 val2 == cp_type3_packet(CP_INTERRUPT, 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681 && val3 == CP_INT_CNTL__RB_INT_MASK) ||
Jordan Crouse084427d2011-07-28 08:37:58 -0600682 (val1 == cp_type3_packet(CP_EVENT_WRITE, 3)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683 && val2 == CACHE_FLUSH_TS &&
684 val3 == (rb->device->memstore.gpuaddr +
685 KGSL_DEVICE_MEMSTORE_OFFSET(eoptimestamp)))) {
686 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
687 rb->buffer_desc.size);
688 KGSL_DRV_ERR(device,
689 "Found end of last executed "
690 "command at offset: %x\n",
691 rb_rptr / sizeof(unsigned int));
692 break;
693 } else {
694 if (rb_rptr < (3 * sizeof(unsigned int)))
695 rb_rptr = rb->buffer_desc.size -
696 (3 * sizeof(unsigned int))
697 + rb_rptr;
698 else
699 rb_rptr -= (3 * sizeof(unsigned int));
700 }
701 }
702
703 if (rb_rptr == 0)
704 rb_rptr = rb->buffer_desc.size - sizeof(unsigned int);
705 else
706 rb_rptr -= sizeof(unsigned int);
707 }
708
709 if ((rb_rptr / sizeof(unsigned int)) == rb->wptr) {
710 KGSL_DRV_ERR(device,
711 "GPU recovery from hang not possible because last"
712 " successful timestamp is overwritten\n");
713 return -EINVAL;
714 }
715 /* rb_rptr is now pointing to the first dword of the command following
716 * the last sucessfully executed command sequence. Assumption is that
717 * GPU is hung in the command sequence pointed by rb_rptr */
718 /* make sure the GPU is not hung in a command submitted by kgsl
719 * itself */
720 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, rb_rptr);
721 kgsl_sharedmem_readl(&rb->buffer_desc, &val2,
722 adreno_ringbuffer_inc_wrapped(rb_rptr,
723 rb->buffer_desc.size));
Jordan Crouse084427d2011-07-28 08:37:58 -0600724 if (val1 == cp_nop_packet(1) && val2 == KGSL_CMD_IDENTIFIER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 KGSL_DRV_ERR(device,
726 "GPU recovery from hang not possible because "
727 "of hang in kgsl command\n");
728 return -EINVAL;
729 }
730
731 /* current_context is the context that is presently active in the
732 * GPU, i.e the context in which the hang is caused */
733 kgsl_sharedmem_readl(&device->memstore, &cur_context,
734 KGSL_DEVICE_MEMSTORE_OFFSET(current_context));
735 while ((rb_rptr / sizeof(unsigned int)) != rb->wptr) {
736 kgsl_sharedmem_readl(&rb->buffer_desc, &value, rb_rptr);
737 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
738 rb->buffer_desc.size);
739 /* check for context switch indicator */
740 if (value == KGSL_CONTEXT_TO_MEM_IDENTIFIER) {
741 kgsl_sharedmem_readl(&rb->buffer_desc, &value, rb_rptr);
742 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
743 rb->buffer_desc.size);
Jordan Crouse084427d2011-07-28 08:37:58 -0600744 BUG_ON(value != cp_type3_packet(CP_MEM_WRITE, 2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745 kgsl_sharedmem_readl(&rb->buffer_desc, &val1, rb_rptr);
746 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
747 rb->buffer_desc.size);
748 BUG_ON(val1 != (device->memstore.gpuaddr +
749 KGSL_DEVICE_MEMSTORE_OFFSET(current_context)));
750 kgsl_sharedmem_readl(&rb->buffer_desc, &value, rb_rptr);
751 rb_rptr = adreno_ringbuffer_inc_wrapped(rb_rptr,
752 rb->buffer_desc.size);
753 BUG_ON((copy_rb_contents == 0) &&
754 (value == cur_context));
755 /*
756 * If we were copying the commands and got to this point
757 * then we need to remove the 3 commands that appear
758 * before KGSL_CONTEXT_TO_MEM_IDENTIFIER
759 */
760 if (temp_idx)
761 temp_idx -= 3;
762 /* if context switches to a context that did not cause
763 * hang then start saving the rb contents as those
764 * commands can be executed */
765 if (value != cur_context) {
766 copy_rb_contents = 1;
Jordan Crouse084427d2011-07-28 08:37:58 -0600767 temp_rb_buffer[temp_idx++] = cp_nop_packet(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768 temp_rb_buffer[temp_idx++] =
769 KGSL_CMD_IDENTIFIER;
Jordan Crouse084427d2011-07-28 08:37:58 -0600770 temp_rb_buffer[temp_idx++] = cp_nop_packet(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771 temp_rb_buffer[temp_idx++] =
772 KGSL_CONTEXT_TO_MEM_IDENTIFIER;
773 temp_rb_buffer[temp_idx++] =
Jordan Crouse084427d2011-07-28 08:37:58 -0600774 cp_type3_packet(CP_MEM_WRITE, 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 temp_rb_buffer[temp_idx++] = val1;
776 temp_rb_buffer[temp_idx++] = value;
777 } else {
778 copy_rb_contents = 0;
779 }
780 } else if (copy_rb_contents)
781 temp_rb_buffer[temp_idx++] = value;
782 }
783
784 *rb_size = temp_idx;
785 KGSL_DRV_ERR(device, "Extracted rb contents, size: %x\n", *rb_size);
786 for (temp_idx = 0; temp_idx < *rb_size;) {
787 char str[80];
788 int idx = 0;
789 if ((temp_idx + 8) <= *rb_size)
790 j = 8;
791 else
792 j = *rb_size - temp_idx;
793 for (; j != 0; j--)
794 idx += scnprintf(str + idx, 80 - idx,
795 "%8.8X ", temp_rb_buffer[temp_idx++]);
796 printk(KERN_ALERT "%s", str);
797 }
798 return 0;
799}
800
801void
802adreno_ringbuffer_restore(struct adreno_ringbuffer *rb, unsigned int *rb_buff,
803 int num_rb_contents)
804{
805 int i;
806 unsigned int *ringcmds;
807 unsigned int rcmd_gpu;
808
809 if (!num_rb_contents)
810 return;
811
812 if (num_rb_contents > (rb->buffer_desc.size - rb->wptr)) {
813 adreno_regwrite(rb->device, REG_CP_RB_RPTR, 0);
814 rb->rptr = 0;
815 BUG_ON(num_rb_contents > rb->buffer_desc.size);
816 }
817 ringcmds = (unsigned int *)rb->buffer_desc.hostptr + rb->wptr;
818 rcmd_gpu = rb->buffer_desc.gpuaddr + sizeof(unsigned int) * rb->wptr;
819 for (i = 0; i < num_rb_contents; i++)
820 GSL_RB_WRITE(ringcmds, rcmd_gpu, rb_buff[i]);
821 rb->wptr += num_rb_contents;
822 adreno_ringbuffer_submit(rb);
823}