1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
/*
* Copyright 2007 OpenMoko, Inc.
*
* This driver is based on Xati,
* Copyright 2004 Eric Anholt
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <sys/time.h>
#include <unistd.h>
#include "glamo-log.h"
#include "glamo.h"
#include "glamo-regs.h"
#include "glamo-cmdq.h"
#include "glamo-draw.h"
static void GLAMOCMDQResetCP(GlamoPtr pGlamo);
static void GLAMODumpRegs(GlamoPtr pGlamo, CARD16 from, CARD16 to);
#define CQ_LEN 255
#define CQ_MASK ((CQ_LEN + 1) * 1024 - 1)
#define CQ_MASKL (CQ_MASK & 0xffff)
#define CQ_MASKH (CQ_MASK >> 16)
#ifndef NDEBUG
static void
GLAMODebugFifo(GlamoPtr pGlamo)
{
GLAMOCardInfo *glamoc = pGlamo->glamoc;
char *mmio = glamoc->reg_base;
CARD32 offset;
ErrorF("GLAMO_REG_CMDQ_STATUS: 0x%04x\n",
MMIO_IN16(mmio, GLAMO_REG_CMDQ_STATUS));
offset = MMIO_IN16(mmio, GLAMO_REG_CMDQ_WRITE_ADDRL);
offset |= (MMIO_IN16(mmio, GLAMO_REG_CMDQ_WRITE_ADDRH) << 16) & 0x7;
ErrorF("GLAMO_REG_CMDQ_WRITE_ADDR: 0x%08x\n", (unsigned int) offset);
offset = MMIO_IN16(mmio, GLAMO_REG_CMDQ_READ_ADDRL);
offset |= (MMIO_IN16(mmio, GLAMO_REG_CMDQ_READ_ADDRH) << 16) & 0x7;
ErrorF("GLAMO_REG_CMDQ_READ_ADDR: 0x%08x\n", (unsigned int) offset);
}
#endif
void
GLAMOEngineReset(GlamoPtr pGlamo, enum GLAMOEngine engine)
{
CARD32 reg;
CARD16 mask;
volatile char *mmio = pGlamo->reg_base;
if (!mmio)
return;
switch (engine) {
case GLAMO_ENGINE_CMDQ:
reg = GLAMO_REG_CLOCK_2D;
mask = GLAMO_CLOCK_2D_CMDQ_RESET;
break;
case GLAMO_ENGINE_ISP:
reg = GLAMO_REG_CLOCK_ISP;
mask = GLAMO_CLOCK_ISP2_RESET;
break;
case GLAMO_ENGINE_2D:
reg = GLAMO_REG_CLOCK_2D;
mask = GLAMO_CLOCK_2D_RESET;
break;
default:
return;
break;
}
MMIOSetBitMask(mmio, reg, mask, 0xffff);
sleep(1);
MMIOSetBitMask(mmio, reg, mask, 0);
sleep(1);
}
void
GLAMOEngineDisable(GlamoPtr pGlamo, enum GLAMOEngine engine)
{
volatile char *mmio = pGlamo->reg_base;
if (!mmio)
return;
switch (engine) {
case GLAMO_ENGINE_CMDQ:
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_2D,
GLAMO_CLOCK_2D_EN_M6CLK,
0);
MMIOSetBitMask(mmio, GLAMO_REG_HOSTBUS(2),
GLAMO_HOSTBUS2_MMIO_EN_CMDQ,
0);
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_GEN5_1,
GLAMO_CLOCK_GEN51_EN_DIV_MCLK,
0);
break;
case GLAMO_ENGINE_ISP:
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_ISP,
GLAMO_CLOCK_ISP_EN_M2CLK |
GLAMO_CLOCK_ISP_EN_I1CLK,
0);
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_GEN5_2,
GLAMO_CLOCK_GEN52_EN_DIV_ICLK,
0);
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_GEN5_1,
GLAMO_CLOCK_GEN51_EN_DIV_JCLK,
0);
MMIOSetBitMask(mmio, GLAMO_REG_HOSTBUS(2),
GLAMO_HOSTBUS2_MMIO_EN_ISP,
0);
break;
case GLAMO_ENGINE_2D:
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_2D,
GLAMO_CLOCK_2D_EN_M7CLK |
GLAMO_CLOCK_2D_EN_GCLK |
GLAMO_CLOCK_2D_DG_M7CLK |
GLAMO_CLOCK_2D_DG_GCLK,
0);
MMIOSetBitMask(mmio, GLAMO_REG_HOSTBUS(2),
GLAMO_HOSTBUS2_MMIO_EN_2D,
0);
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_GEN5_1,
GLAMO_CLOCK_GEN51_EN_DIV_GCLK,
0);
break;
default:
break;
}
}
void
GLAMOEngineEnable(GlamoPtr pGlamo, enum GLAMOEngine engine)
{
volatile char *mmio = pGlamo->reg_base;
if (!mmio)
return;
switch (engine) {
case GLAMO_ENGINE_CMDQ:
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_2D,
GLAMO_CLOCK_2D_EN_M6CLK,
0xffff);
MMIOSetBitMask(mmio, GLAMO_REG_HOSTBUS(2),
GLAMO_HOSTBUS2_MMIO_EN_CMDQ,
0xffff);
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_GEN5_1,
GLAMO_CLOCK_GEN51_EN_DIV_MCLK,
0xffff);
break;
case GLAMO_ENGINE_ISP:
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_ISP,
GLAMO_CLOCK_ISP_EN_M2CLK |
GLAMO_CLOCK_ISP_EN_I1CLK,
0xffff);
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_GEN5_2,
GLAMO_CLOCK_GEN52_EN_DIV_ICLK,
0xffff);
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_GEN5_1,
GLAMO_CLOCK_GEN51_EN_DIV_JCLK,
0xffff);
MMIOSetBitMask(mmio, GLAMO_REG_HOSTBUS(2),
GLAMO_HOSTBUS2_MMIO_EN_ISP,
0xffff);
break;
case GLAMO_ENGINE_2D:
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_2D,
GLAMO_CLOCK_2D_EN_M7CLK |
GLAMO_CLOCK_2D_EN_GCLK |
GLAMO_CLOCK_2D_DG_M7CLK |
GLAMO_CLOCK_2D_DG_GCLK,
0xffff);
MMIOSetBitMask(mmio, GLAMO_REG_HOSTBUS(2),
GLAMO_HOSTBUS2_MMIO_EN_2D,
0xffff);
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_GEN5_1,
GLAMO_CLOCK_GEN51_EN_DIV_GCLK,
0xffff);
break;
default:
break;
}
}
int
GLAMOEngineBusy(GlamoPtr pGlamo, enum GLAMOEngine engine)
{
volatile char *mmio = pGlamo->reg_base;
CARD16 status, mask, val;
if (!mmio)
return FALSE;
if (pGlamo->cmd_queue_cache != NULL)
GLAMOFlushCMDQCache(pGlamo, 0);
switch (engine)
{
case GLAMO_ENGINE_CMDQ:
mask = 0x3;
val = mask;
break;
case GLAMO_ENGINE_ISP:
mask = 0x3 | (1 << 8);
val = 0x3;
break;
case GLAMO_ENGINE_2D:
mask = 0x3 | (1 << 4);
val = 0x3;
break;
case GLAMO_ENGINE_ALL:
default:
mask = 1 << 2;
val = mask;
break;
}
status = MMIO_IN16(mmio, GLAMO_REG_CMDQ_STATUS);
return !((status & mask) == val);
}
static void
GLAMOEngineWaitReal(GlamoPtr pGlamo,
enum GLAMOEngine engine,
Bool do_flush)
{
volatile char *mmio = pGlamo->reg_base;
CARD16 status, mask, val;
if (!mmio)
return;
if (pGlamo->cmd_queue_cache != NULL && do_flush)
GLAMOFlushCMDQCache(pGlamo, 0);
switch (engine)
{
case GLAMO_ENGINE_CMDQ:
mask = 0x3;
val = mask;
break;
case GLAMO_ENGINE_ISP:
mask = 0x3 | (1 << 8);
val = 0x3;
break;
case GLAMO_ENGINE_2D:
mask = 0x3 | (1 << 4);
val = 0x3;
break;
case GLAMO_ENGINE_ALL:
default:
mask = 1 << 2;
val = mask;
break;
}
do {
status = MMIO_IN16(mmio, GLAMO_REG_CMDQ_STATUS);
} while ((status & mask) != val);
}
void
GLAMOEngineWait(GlamoPtr pGlamo,
enum GLAMOEngine engine)
{
GLAMOEngineWaitReal(pGlamo, engine, TRUE);
}
MemBuf *
GLAMOCreateCMDQCache(GlamoPtr pGlamo)
{
MemBuf *buf;
buf = (MemBuf *)xcalloc(1, sizeof(MemBuf));
if (buf == NULL)
return NULL;
buf->size = pGlamo->ring_len;
buf->address = xcalloc(1, buf->size);
if (buf->address == NULL) {
xfree(buf);
return NULL;
}
buf->used = 0;
return buf;
}
static void
GLAMODispatchCMDQCache(GlamoPtr pGlamo)
{
MemBuf *buf = pGlamo->cmd_queue_cache;
volatile char *mmio = pGlamo->reg_base;
char *addr;
size_t count, ring_count;
size_t rest_size;
size_t ring_read;
size_t old_ring_write = pGlamo->ring_write;
if (!buf->used)
return;
addr = ((char *)buf->address);
count = buf->used;
ring_count = pGlamo->ring_len;
pGlamo->ring_write = (((pGlamo->ring_write + count) & CQ_MASK) + 1) & ~1;
/* Wait until there is enough space to queue the cmd buffer */
if (pGlamo->ring_write > old_ring_write) {
do {
ring_read = MMIO_IN16(mmio, GLAMO_REG_CMDQ_READ_ADDRL) & CQ_MASKL;
ring_read |= ((MMIO_IN16(mmio, GLAMO_REG_CMDQ_READ_ADDRH) & CQ_MASKH) << 16);
} while(ring_read > old_ring_write && ring_read < pGlamo->ring_write);
} else {
do {
ring_read = MMIO_IN16(mmio, GLAMO_REG_CMDQ_READ_ADDRL) & CQ_MASKL;
ring_read |= ((MMIO_IN16(mmio, GLAMO_REG_CMDQ_READ_ADDRH) & CQ_MASKH) << 16);
} while(ring_read > old_ring_write || ring_read < pGlamo->ring_write);
}
/* Wrap around */
if (old_ring_write >= pGlamo->ring_write) {
rest_size = (ring_count - old_ring_write);
memcpy((char*)(pGlamo->ring_addr) + old_ring_write, addr, rest_size);
memcpy((char*)(pGlamo->ring_addr), addr+rest_size, count - rest_size);
/* ring_write being 0 will result in a deadlock because the cmdq read
* will never stop. To avoid such an behaviour insert an empty
* instruction. */
if(pGlamo->ring_write == 0) {
memset((char*)(pGlamo->ring_addr), 0, 4);
pGlamo->ring_write = 4;
}
/* Before changing write read has to stop */
GLAMOEngineWaitReal(pGlamo, GLAMO_ENGINE_CMDQ, FALSE);
/* The write position has to change to trigger a read */
if(old_ring_write == pGlamo->ring_write) {
memset((char*)(pGlamo->ring_addr + pGlamo->ring_write), 0, 4);
pGlamo->ring_write += 4;
/* MMIO_OUT16(mmio, GLAMO_REG_CMDQ_WRITE_ADDRH,
((pGlamo->ring_write-4) >> 16) & CQ_MASKH);
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_WRITE_ADDRL,
(pGlamo->ring_write-4) & CQ_MASKL);*/
}
} else {
memcpy((char*)(pGlamo->ring_addr) + old_ring_write, addr, count);
GLAMOEngineWaitReal(pGlamo, GLAMO_ENGINE_CMDQ, FALSE);
// GLAMODumpRegs(pGlamo, GLAMO_REG_CMDQ_WRITE_ADDRL, GLAMO_REG_CMDQ_READ_ADDRH);
}
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_2D,
GLAMO_CLOCK_2D_EN_M6CLK,
0);
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_WRITE_ADDRH,
(pGlamo->ring_write >> 16) & CQ_MASKH);
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_WRITE_ADDRL,
pGlamo->ring_write & CQ_MASKL);
MMIOSetBitMask(mmio, GLAMO_REG_CLOCK_2D,
GLAMO_CLOCK_2D_EN_M6CLK,
0xffff);
buf->used = 0;
GLAMOEngineWaitReal(pGlamo, GLAMO_ENGINE_ALL, FALSE);
}
void
GLAMOFlushCMDQCache(GlamoPtr pGlamo, Bool discard)
{
GLAMODispatchCMDQCache(pGlamo);
}
static void
GLAMOCMDQResetCP(GlamoPtr pGlamo)
{
volatile char *mmio = pGlamo->reg_base;
int cq_len = CQ_LEN;
CARD32 queue_offset = 0;
xf86DrvMsg(0, X_WARNING,
"GLAMOCMDQResetCP %p %d\n", (void *)pGlamo->ring_addr, pGlamo->ring_len );
/* make the decoder happy? */
memset((char*)pGlamo->ring_addr, 0, pGlamo->ring_len);
pGlamo->ring_write = 0;
GLAMOEngineReset(pGlamo, GLAMO_ENGINE_CMDQ);
queue_offset = pGlamo->exa_cmd_queue->offset;
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_BASE_ADDRL,
queue_offset & 0xffff);
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_BASE_ADDRH,
(queue_offset >> 16) & 0x7f);
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_LEN, cq_len);
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_WRITE_ADDRH, 0);
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_WRITE_ADDRL, 0);
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_READ_ADDRH, 0);
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_READ_ADDRL, 0);
MMIO_OUT16(mmio, GLAMO_REG_CMDQ_CONTROL,
1 << 12 |
5 << 8 |
8 << 4);
GLAMOEngineWaitReal(pGlamo, GLAMO_ENGINE_ALL, FALSE);
}
static Bool
GLAMOCMDQInit(GlamoPtr pGlamo,
Bool force)
{
int cq_len = CQ_LEN;
xf86DrvMsg(0, X_WARNING,
"GLAMOCMDQInit here1\n");
if (!force && pGlamo->exa_cmd_queue)
return TRUE;
xf86DrvMsg(0, X_WARNING,
"GLAMOCMDQInit here2\n");
pGlamo->ring_len = (cq_len + 1) * 1024;
pGlamo->exa_cmd_queue =
exaOffscreenAlloc(pGlamo->pScreen, pGlamo->ring_len,
pGlamo->exa->pixmapOffsetAlign,
TRUE, NULL, NULL);
xf86DrvMsg(0, X_WARNING,
"GLAMOCMDQInit here3\n");
if (!pGlamo->exa_cmd_queue)
return FALSE;
pGlamo->ring_addr =
(CARD16 *) (pGlamo->fbstart +
pGlamo->exa_cmd_queue->offset);
GLAMOEngineEnable(pGlamo, GLAMO_ENGINE_CMDQ);
xf86DrvMsg(0, X_WARNING,
"GLAMOCMDQInit here4\n");
GLAMOCMDQResetCP(pGlamo);
xf86DrvMsg(0, X_WARNING,
"GLAMOCMDQInit here5\n");
return TRUE;
}
void
GLAMOCMDQCacheSetup(GlamoPtr pGlamo)
{
xf86DrvMsg(0, X_WARNING,
"GLAMOCMDQCacheSetup here1\n");
GLAMOCMDQInit(pGlamo, TRUE);
xf86DrvMsg(0, X_WARNING,
"GLAMOCMDQCacheSetup here2\n");
if (pGlamo->cmd_queue_cache)
return;
xf86DrvMsg(0, X_WARNING,
"GLAMOCMDQCacheSetup here3\n");
pGlamo->cmd_queue_cache = GLAMOCreateCMDQCache(pGlamo);
if (pGlamo->cmd_queue_cache == FALSE)
FatalError("Failed to allocate cmd queue cache buffer.\n");
}
void
GLAMOCMQCacheTeardown(GlamoPtr pGlamo)
{
GLAMOEngineWait(pGlamo, GLAMO_ENGINE_ALL);
xfree(pGlamo->cmd_queue_cache->address);
xfree(pGlamo->cmd_queue_cache);
pGlamo->cmd_queue_cache = NULL;
if(0)
GLAMODumpRegs(pGlamo, 0, 0);
}
static void
GLAMODumpRegs(GlamoPtr pGlamo,
CARD16 from,
CARD16 to)
{
int i=0;
for (i=from; i <= to; i += 2) {
ErrorF("reg:%p, val:%#x\n",
pGlamo->reg_base+i,
*(VOL16*)(pGlamo->reg_base+i));
}
}
|