Statistics
| Branch: | Revision:

root / hw / dma / pl330.c @ c3143ba8

History | View | Annotate | Download (48.2 kB)

1
/*
2
 * ARM PrimeCell PL330 DMA Controller
3
 *
4
 * Copyright (c) 2009 Samsung Electronics.
5
 * Contributed by Kirill Batuzov <batuzovk@ispras.ru>
6
 * Copyright (c) 2012 Peter A.G. Crosthwaite (peter.crosthwaite@petalogix.com)
7
 * Copyright (c) 2012 PetaLogix Pty Ltd.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; version 2 or later.
12
 *
13
 * You should have received a copy of the GNU General Public License along
14
 * with this program; if not, see <http://www.gnu.org/licenses/>.
15
 */
16

    
17
#include "hw/sysbus.h"
18
#include "qemu/timer.h"
19
#include "sysemu/dma.h"
20

    
21
#ifndef PL330_ERR_DEBUG
22
#define PL330_ERR_DEBUG 0
23
#endif
24

    
25
#define DB_PRINT_L(lvl, fmt, args...) do {\
26
    if (PL330_ERR_DEBUG >= lvl) {\
27
        fprintf(stderr, "PL330: %s:" fmt, __func__, ## args);\
28
    } \
29
} while (0);
30

    
31
#define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
32

    
33
#define PL330_PERIPH_NUM            32
34
#define PL330_MAX_BURST_LEN         128
35
#define PL330_INSN_MAXSIZE          6
36

    
37
#define PL330_FIFO_OK               0
38
#define PL330_FIFO_STALL            1
39
#define PL330_FIFO_ERR              (-1)
40

    
41
#define PL330_FAULT_UNDEF_INSTR             (1 <<  0)
42
#define PL330_FAULT_OPERAND_INVALID         (1 <<  1)
43
#define PL330_FAULT_DMAGO_ERR               (1 <<  4)
44
#define PL330_FAULT_EVENT_ERR               (1 <<  5)
45
#define PL330_FAULT_CH_PERIPH_ERR           (1 <<  6)
46
#define PL330_FAULT_CH_RDWR_ERR             (1 <<  7)
47
#define PL330_FAULT_ST_DATA_UNAVAILABLE     (1 << 12)
48
#define PL330_FAULT_FIFOEMPTY_ERR           (1 << 13)
49
#define PL330_FAULT_INSTR_FETCH_ERR         (1 << 16)
50
#define PL330_FAULT_DATA_WRITE_ERR          (1 << 17)
51
#define PL330_FAULT_DATA_READ_ERR           (1 << 18)
52
#define PL330_FAULT_DBG_INSTR               (1 << 30)
53
#define PL330_FAULT_LOCKUP_ERR              (1 << 31)
54

    
55
#define PL330_UNTAGGED              0xff
56

    
57
#define PL330_SINGLE                0x0
58
#define PL330_BURST                 0x1
59

    
60
#define PL330_WATCHDOG_LIMIT        1024
61

    
62
/* IOMEM mapped registers */
63
#define PL330_REG_DSR               0x000
64
#define PL330_REG_DPC               0x004
65
#define PL330_REG_INTEN             0x020
66
#define PL330_REG_INT_EVENT_RIS     0x024
67
#define PL330_REG_INTMIS            0x028
68
#define PL330_REG_INTCLR            0x02C
69
#define PL330_REG_FSRD              0x030
70
#define PL330_REG_FSRC              0x034
71
#define PL330_REG_FTRD              0x038
72
#define PL330_REG_FTR_BASE          0x040
73
#define PL330_REG_CSR_BASE          0x100
74
#define PL330_REG_CPC_BASE          0x104
75
#define PL330_REG_CHANCTRL          0x400
76
#define PL330_REG_DBGSTATUS         0xD00
77
#define PL330_REG_DBGCMD            0xD04
78
#define PL330_REG_DBGINST0          0xD08
79
#define PL330_REG_DBGINST1          0xD0C
80
#define PL330_REG_CR0_BASE          0xE00
81
#define PL330_REG_PERIPH_ID         0xFE0
82

    
83
#define PL330_IOMEM_SIZE    0x1000
84

    
85
#define CFG_BOOT_ADDR 2
86
#define CFG_INS 3
87
#define CFG_PNS 4
88
#define CFG_CRD 5
89

    
90
static const uint32_t pl330_id[] = {
91
    0x30, 0x13, 0x24, 0x00, 0x0D, 0xF0, 0x05, 0xB1
92
};
93

    
94
/* DMA channel states as they are described in PL330 Technical Reference Manual
95
 * Most of them will not be used in emulation.
96
 */
97
typedef enum  {
98
    pl330_chan_stopped = 0,
99
    pl330_chan_executing = 1,
100
    pl330_chan_cache_miss = 2,
101
    pl330_chan_updating_pc = 3,
102
    pl330_chan_waiting_event = 4,
103
    pl330_chan_at_barrier = 5,
104
    pl330_chan_queue_busy = 6,
105
    pl330_chan_waiting_periph = 7,
106
    pl330_chan_killing = 8,
107
    pl330_chan_completing = 9,
108
    pl330_chan_fault_completing = 14,
109
    pl330_chan_fault = 15,
110
} PL330ChanState;
111

    
112
typedef struct PL330State PL330State;
113

    
114
typedef struct PL330Chan {
115
    uint32_t src;
116
    uint32_t dst;
117
    uint32_t pc;
118
    uint32_t control;
119
    uint32_t status;
120
    uint32_t lc[2];
121
    uint32_t fault_type;
122
    uint32_t watchdog_timer;
123

    
124
    bool ns;
125
    uint8_t request_flag;
126
    uint8_t wakeup;
127
    uint8_t wfp_sbp;
128

    
129
    uint8_t state;
130
    uint8_t stall;
131

    
132
    bool is_manager;
133
    PL330State *parent;
134
    uint8_t tag;
135
} PL330Chan;
136

    
137
static const VMStateDescription vmstate_pl330_chan = {
138
    .name = "pl330_chan",
139
    .version_id = 1,
140
    .minimum_version_id = 1,
141
    .minimum_version_id_old = 1,
142
    .fields = (VMStateField[]) {
143
        VMSTATE_UINT32(src, PL330Chan),
144
        VMSTATE_UINT32(dst, PL330Chan),
145
        VMSTATE_UINT32(pc, PL330Chan),
146
        VMSTATE_UINT32(control, PL330Chan),
147
        VMSTATE_UINT32(status, PL330Chan),
148
        VMSTATE_UINT32_ARRAY(lc, PL330Chan, 2),
149
        VMSTATE_UINT32(fault_type, PL330Chan),
150
        VMSTATE_UINT32(watchdog_timer, PL330Chan),
151
        VMSTATE_BOOL(ns, PL330Chan),
152
        VMSTATE_UINT8(request_flag, PL330Chan),
153
        VMSTATE_UINT8(wakeup, PL330Chan),
154
        VMSTATE_UINT8(wfp_sbp, PL330Chan),
155
        VMSTATE_UINT8(state, PL330Chan),
156
        VMSTATE_UINT8(stall, PL330Chan),
157
        VMSTATE_END_OF_LIST()
158
    }
159
};
160

    
161
typedef struct PL330Fifo {
162
    uint8_t *buf;
163
    uint8_t *tag;
164
    uint32_t head;
165
    uint32_t num;
166
    uint32_t buf_size;
167
} PL330Fifo;
168

    
169
static const VMStateDescription vmstate_pl330_fifo = {
170
    .name = "pl330_chan",
171
    .version_id = 1,
172
    .minimum_version_id = 1,
173
    .minimum_version_id_old = 1,
174
    .fields = (VMStateField[]) {
175
        VMSTATE_VBUFFER_UINT32(buf, PL330Fifo, 1, NULL, 0, buf_size),
176
        VMSTATE_VBUFFER_UINT32(tag, PL330Fifo, 1, NULL, 0, buf_size),
177
        VMSTATE_UINT32(head, PL330Fifo),
178
        VMSTATE_UINT32(num, PL330Fifo),
179
        VMSTATE_UINT32(buf_size, PL330Fifo),
180
        VMSTATE_END_OF_LIST()
181
    }
182
};
183

    
184
typedef struct PL330QueueEntry {
185
    uint32_t addr;
186
    uint32_t len;
187
    uint8_t n;
188
    bool inc;
189
    bool z;
190
    uint8_t tag;
191
    uint8_t seqn;
192
} PL330QueueEntry;
193

    
194
static const VMStateDescription vmstate_pl330_queue_entry = {
195
    .name = "pl330_queue_entry",
196
    .version_id = 1,
197
    .minimum_version_id = 1,
198
    .minimum_version_id_old = 1,
199
    .fields = (VMStateField[]) {
200
        VMSTATE_UINT32(addr, PL330QueueEntry),
201
        VMSTATE_UINT32(len, PL330QueueEntry),
202
        VMSTATE_UINT8(n, PL330QueueEntry),
203
        VMSTATE_BOOL(inc, PL330QueueEntry),
204
        VMSTATE_BOOL(z, PL330QueueEntry),
205
        VMSTATE_UINT8(tag, PL330QueueEntry),
206
        VMSTATE_UINT8(seqn, PL330QueueEntry),
207
        VMSTATE_END_OF_LIST()
208
    }
209
};
210

    
211
typedef struct PL330Queue {
212
    PL330State *parent;
213
    PL330QueueEntry *queue;
214
    uint32_t queue_size;
215
} PL330Queue;
216

    
217
static const VMStateDescription vmstate_pl330_queue = {
218
    .name = "pl330_queue",
219
    .version_id = 1,
220
    .minimum_version_id = 1,
221
    .minimum_version_id_old = 1,
222
    .fields = (VMStateField[]) {
223
        VMSTATE_STRUCT_VARRAY_UINT32(queue, PL330Queue, queue_size, 1,
224
                                 vmstate_pl330_queue_entry, PL330QueueEntry),
225
        VMSTATE_END_OF_LIST()
226
    }
227
};
228

    
229
struct PL330State {
230
    SysBusDevice busdev;
231
    MemoryRegion iomem;
232
    qemu_irq irq_abort;
233
    qemu_irq *irq;
234

    
235
    /* Config registers. cfg[5] = CfgDn. */
236
    uint32_t cfg[6];
237
#define EVENT_SEC_STATE 3
238
#define PERIPH_SEC_STATE 4
239
    /* cfg 0 bits and pieces */
240
    uint32_t num_chnls;
241
    uint8_t num_periph_req;
242
    uint8_t num_events;
243
    uint8_t mgr_ns_at_rst;
244
    /* cfg 1 bits and pieces */
245
    uint8_t i_cache_len;
246
    uint8_t num_i_cache_lines;
247
    /* CRD bits and pieces */
248
    uint8_t data_width;
249
    uint8_t wr_cap;
250
    uint8_t wr_q_dep;
251
    uint8_t rd_cap;
252
    uint8_t rd_q_dep;
253
    uint16_t data_buffer_dep;
254

    
255
    PL330Chan manager;
256
    PL330Chan *chan;
257
    PL330Fifo fifo;
258
    PL330Queue read_queue;
259
    PL330Queue write_queue;
260
    uint8_t *lo_seqn;
261
    uint8_t *hi_seqn;
262
    QEMUTimer *timer; /* is used for restore dma. */
263

    
264
    uint32_t inten;
265
    uint32_t int_status;
266
    uint32_t ev_status;
267
    uint32_t dbg[2];
268
    uint8_t debug_status;
269
    uint8_t num_faulting;
270
    uint8_t periph_busy[PL330_PERIPH_NUM];
271

    
272
};
273

    
274
#define TYPE_PL330 "pl330"
275
#define PL330(obj) OBJECT_CHECK(PL330State, (obj), TYPE_PL330)
276

    
277
static const VMStateDescription vmstate_pl330 = {
278
    .name = "pl330",
279
    .version_id = 1,
280
    .minimum_version_id = 1,
281
    .minimum_version_id_old = 1,
282
    .fields = (VMStateField[]) {
283
        VMSTATE_STRUCT(manager, PL330State, 0, vmstate_pl330_chan, PL330Chan),
284
        VMSTATE_STRUCT_VARRAY_UINT32(chan, PL330State, num_chnls, 0,
285
                                     vmstate_pl330_chan, PL330Chan),
286
        VMSTATE_VBUFFER_UINT32(lo_seqn, PL330State, 1, NULL, 0, num_chnls),
287
        VMSTATE_VBUFFER_UINT32(hi_seqn, PL330State, 1, NULL, 0, num_chnls),
288
        VMSTATE_STRUCT(fifo, PL330State, 0, vmstate_pl330_fifo, PL330Fifo),
289
        VMSTATE_STRUCT(read_queue, PL330State, 0, vmstate_pl330_queue,
290
                       PL330Queue),
291
        VMSTATE_STRUCT(write_queue, PL330State, 0, vmstate_pl330_queue,
292
                       PL330Queue),
293
        VMSTATE_TIMER(timer, PL330State),
294
        VMSTATE_UINT32(inten, PL330State),
295
        VMSTATE_UINT32(int_status, PL330State),
296
        VMSTATE_UINT32(ev_status, PL330State),
297
        VMSTATE_UINT32_ARRAY(dbg, PL330State, 2),
298
        VMSTATE_UINT8(debug_status, PL330State),
299
        VMSTATE_UINT8(num_faulting, PL330State),
300
        VMSTATE_UINT8_ARRAY(periph_busy, PL330State, PL330_PERIPH_NUM),
301
        VMSTATE_END_OF_LIST()
302
    }
303
};
304

    
305
typedef struct PL330InsnDesc {
306
    /* OPCODE of the instruction */
307
    uint8_t opcode;
308
    /* Mask so we can select several sibling instructions, such as
309
       DMALD, DMALDS and DMALDB */
310
    uint8_t opmask;
311
    /* Size of instruction in bytes */
312
    uint8_t size;
313
    /* Interpreter */
314
    void (*exec)(PL330Chan *, uint8_t opcode, uint8_t *args, int len);
315
} PL330InsnDesc;
316

    
317

    
318
/* MFIFO Implementation
319
 *
320
 * MFIFO is implemented as a cyclic buffer of BUF_SIZE size. Tagged bytes are
321
 * stored in this buffer. Data is stored in BUF field, tags - in the
322
 * corresponding array elements of TAG field.
323
 */
324

    
325
/* Initialize queue. */
326

    
327
static void pl330_fifo_init(PL330Fifo *s, uint32_t size)
328
{
329
    s->buf = g_malloc0(size);
330
    s->tag = g_malloc0(size);
331
    s->buf_size = size;
332
}
333

    
334
/* Cyclic increment */
335

    
336
static inline int pl330_fifo_inc(PL330Fifo *s, int x)
337
{
338
    return (x + 1) % s->buf_size;
339
}
340

    
341
/* Number of empty bytes in MFIFO */
342

    
343
static inline int pl330_fifo_num_free(PL330Fifo *s)
344
{
345
    return s->buf_size - s->num;
346
}
347

    
348
/* Push LEN bytes of data stored in BUF to MFIFO and tag it with TAG.
349
 * Zero returned on success, PL330_FIFO_STALL if there is no enough free
350
 * space in MFIFO to store requested amount of data. If push was unsuccessful
351
 * no data is stored to MFIFO.
352
 */
353

    
354
static int pl330_fifo_push(PL330Fifo *s, uint8_t *buf, int len, uint8_t tag)
355
{
356
    int i;
357

    
358
    if (s->buf_size - s->num < len) {
359
        return PL330_FIFO_STALL;
360
    }
361
    for (i = 0; i < len; i++) {
362
        int push_idx = (s->head + s->num + i) % s->buf_size;
363
        s->buf[push_idx] = buf[i];
364
        s->tag[push_idx] = tag;
365
    }
366
    s->num += len;
367
    return PL330_FIFO_OK;
368
}
369

    
370
/* Get LEN bytes of data from MFIFO and store it to BUF. Tag value of each
371
 * byte is verified. Zero returned on success, PL330_FIFO_ERR on tag mismatch
372
 * and PL330_FIFO_STALL if there is no enough data in MFIFO. If get was
373
 * unsuccessful no data is removed from MFIFO.
374
 */
375

    
376
static int pl330_fifo_get(PL330Fifo *s, uint8_t *buf, int len, uint8_t tag)
377
{
378
    int i;
379

    
380
    if (s->num < len) {
381
        return PL330_FIFO_STALL;
382
    }
383
    for (i = 0; i < len; i++) {
384
        if (s->tag[s->head] == tag) {
385
            int get_idx = (s->head + i) % s->buf_size;
386
            buf[i] = s->buf[get_idx];
387
        } else { /* Tag mismatch - Rollback transaction */
388
            return PL330_FIFO_ERR;
389
        }
390
    }
391
    s->head = (s->head + len) % s->buf_size;
392
    s->num -= len;
393
    return PL330_FIFO_OK;
394
}
395

    
396
/* Reset MFIFO. This completely erases all data in it. */
397

    
398
static inline void pl330_fifo_reset(PL330Fifo *s)
399
{
400
    s->head = 0;
401
    s->num = 0;
402
}
403

    
404
/* Return tag of the first byte stored in MFIFO. If MFIFO is empty
405
 * PL330_UNTAGGED is returned.
406
 */
407

    
408
static inline uint8_t pl330_fifo_tag(PL330Fifo *s)
409
{
410
    return (!s->num) ? PL330_UNTAGGED : s->tag[s->head];
411
}
412

    
413
/* Returns non-zero if tag TAG is present in fifo or zero otherwise */
414

    
415
static int pl330_fifo_has_tag(PL330Fifo *s, uint8_t tag)
416
{
417
    int i, n;
418

    
419
    i = s->head;
420
    for (n = 0; n < s->num; n++) {
421
        if (s->tag[i] == tag) {
422
            return 1;
423
        }
424
        i = pl330_fifo_inc(s, i);
425
    }
426
    return 0;
427
}
428

    
429
/* Remove all entry tagged with TAG from MFIFO */
430

    
431
static void pl330_fifo_tagged_remove(PL330Fifo *s, uint8_t tag)
432
{
433
    int i, t, n;
434

    
435
    t = i = s->head;
436
    for (n = 0; n < s->num; n++) {
437
        if (s->tag[i] != tag) {
438
            s->buf[t] = s->buf[i];
439
            s->tag[t] = s->tag[i];
440
            t = pl330_fifo_inc(s, t);
441
        } else {
442
            s->num = s->num - 1;
443
        }
444
        i = pl330_fifo_inc(s, i);
445
    }
446
}
447

    
448
/* Read-Write Queue implementation
449
 *
450
 * A Read-Write Queue stores up to QUEUE_SIZE instructions (loads or stores).
451
 * Each instruction is described by source (for loads) or destination (for
452
 * stores) address ADDR, width of data to be loaded/stored LEN, number of
453
 * stores/loads to be performed N, INC bit, Z bit and TAG to identify channel
454
 * this instruction belongs to. Queue does not store any information about
455
 * nature of the instruction: is it load or store. PL330 has different queues
456
 * for loads and stores so this is already known at the top level where it
457
 * matters.
458
 *
459
 * Queue works as FIFO for instructions with equivalent tags, but can issue
460
 * instructions with different tags in arbitrary order. SEQN field attached to
461
 * each instruction helps to achieve this. For each TAG queue contains
462
 * instructions with consecutive SEQN values ranging from LO_SEQN[TAG] to
463
 * HI_SEQN[TAG]-1 inclusive. SEQN is 8-bit unsigned integer, so SEQN=255 is
464
 * followed by SEQN=0.
465
 *
466
 * Z bit indicates that zeroes should be stored. No MFIFO fetches are performed
467
 * in this case.
468
 */
469

    
470
static void pl330_queue_reset(PL330Queue *s)
471
{
472
    int i;
473

    
474
    for (i = 0; i < s->queue_size; i++) {
475
        s->queue[i].tag = PL330_UNTAGGED;
476
    }
477
}
478

    
479
/* Initialize queue */
480
static void pl330_queue_init(PL330Queue *s, int size, PL330State *parent)
481
{
482
    s->parent = parent;
483
    s->queue = g_new0(PL330QueueEntry, size);
484
    s->queue_size = size;
485
}
486

    
487
/* Returns pointer to an empty slot or NULL if queue is full */
488
static PL330QueueEntry *pl330_queue_find_empty(PL330Queue *s)
489
{
490
    int i;
491

    
492
    for (i = 0; i < s->queue_size; i++) {
493
        if (s->queue[i].tag == PL330_UNTAGGED) {
494
            return &s->queue[i];
495
        }
496
    }
497
    return NULL;
498
}
499

    
500
/* Put instruction in queue.
501
 * Return value:
502
 * - zero - OK
503
 * - non-zero - queue is full
504
 */
505

    
506
static int pl330_queue_put_insn(PL330Queue *s, uint32_t addr,
507
                                int len, int n, bool inc, bool z, uint8_t tag)
508
{
509
    PL330QueueEntry *entry = pl330_queue_find_empty(s);
510

    
511
    if (!entry) {
512
        return 1;
513
    }
514
    entry->tag = tag;
515
    entry->addr = addr;
516
    entry->len = len;
517
    entry->n = n;
518
    entry->z = z;
519
    entry->inc = inc;
520
    entry->seqn = s->parent->hi_seqn[tag];
521
    s->parent->hi_seqn[tag]++;
522
    return 0;
523
}
524

    
525
/* Returns a pointer to queue slot containing instruction which satisfies
526
 *  following conditions:
527
 *   - it has valid tag value (not PL330_UNTAGGED)
528
 *   - if enforce_seq is set it has to be issuable without violating queue
529
 *     logic (see above)
530
 *   - if TAG argument is not PL330_UNTAGGED this instruction has tag value
531
 *     equivalent to the argument TAG value.
532
 *  If such instruction cannot be found NULL is returned.
533
 */
534

    
535
static PL330QueueEntry *pl330_queue_find_insn(PL330Queue *s, uint8_t tag,
536
                                              bool enforce_seq)
537
{
538
    int i;
539

    
540
    for (i = 0; i < s->queue_size; i++) {
541
        if (s->queue[i].tag != PL330_UNTAGGED) {
542
            if ((!enforce_seq ||
543
                    s->queue[i].seqn == s->parent->lo_seqn[s->queue[i].tag]) &&
544
                    (s->queue[i].tag == tag || tag == PL330_UNTAGGED ||
545
                    s->queue[i].z)) {
546
                return &s->queue[i];
547
            }
548
        }
549
    }
550
    return NULL;
551
}
552

    
553
/* Removes instruction from queue. */
554

    
555
static inline void pl330_queue_remove_insn(PL330Queue *s, PL330QueueEntry *e)
556
{
557
    s->parent->lo_seqn[e->tag]++;
558
    e->tag = PL330_UNTAGGED;
559
}
560

    
561
/* Removes all instructions tagged with TAG from queue. */
562

    
563
static inline void pl330_queue_remove_tagged(PL330Queue *s, uint8_t tag)
564
{
565
    int i;
566

    
567
    for (i = 0; i < s->queue_size; i++) {
568
        if (s->queue[i].tag == tag) {
569
            s->queue[i].tag = PL330_UNTAGGED;
570
        }
571
    }
572
}
573

    
574
/* DMA instruction execution engine */
575

    
576
/* Moves DMA channel to the FAULT state and updates it's status. */
577

    
578
static inline void pl330_fault(PL330Chan *ch, uint32_t flags)
579
{
580
    DB_PRINT("ch: %p, flags: %" PRIx32 "\n", ch, flags);
581
    ch->fault_type |= flags;
582
    if (ch->state == pl330_chan_fault) {
583
        return;
584
    }
585
    ch->state = pl330_chan_fault;
586
    ch->parent->num_faulting++;
587
    if (ch->parent->num_faulting == 1) {
588
        DB_PRINT("abort interrupt raised\n");
589
        qemu_irq_raise(ch->parent->irq_abort);
590
    }
591
}
592

    
593
/*
594
 * For information about instructions see PL330 Technical Reference Manual.
595
 *
596
 * Arguments:
597
 *   CH - channel executing the instruction
598
 *   OPCODE - opcode
599
 *   ARGS - array of 8-bit arguments
600
 *   LEN - number of elements in ARGS array
601
 */
602

    
603
static void pl330_dmaaddh(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
604
{
605
    uint16_t im = (((uint16_t)args[1]) << 8) | ((uint16_t)args[0]);
606
    uint8_t ra = (opcode >> 1) & 1;
607

    
608
    if (ch->is_manager) {
609
        pl330_fault(ch, PL330_FAULT_UNDEF_INSTR);
610
        return;
611
    }
612
    if (ra) {
613
        ch->dst += im;
614
    } else {
615
        ch->src += im;
616
    }
617
}
618

    
619
static void pl330_dmaend(PL330Chan *ch, uint8_t opcode,
620
                         uint8_t *args, int len)
621
{
622
    PL330State *s = ch->parent;
623

    
624
    if (ch->state == pl330_chan_executing && !ch->is_manager) {
625
        /* Wait for all transfers to complete */
626
        if (pl330_fifo_has_tag(&s->fifo, ch->tag) ||
627
            pl330_queue_find_insn(&s->read_queue, ch->tag, false) != NULL ||
628
            pl330_queue_find_insn(&s->write_queue, ch->tag, false) != NULL) {
629

    
630
            ch->stall = 1;
631
            return;
632
        }
633
    }
634
    DB_PRINT("DMA ending!\n");
635
    pl330_fifo_tagged_remove(&s->fifo, ch->tag);
636
    pl330_queue_remove_tagged(&s->read_queue, ch->tag);
637
    pl330_queue_remove_tagged(&s->write_queue, ch->tag);
638
    ch->state = pl330_chan_stopped;
639
}
640

    
641
static void pl330_dmaflushp(PL330Chan *ch, uint8_t opcode,
642
                                            uint8_t *args, int len)
643
{
644
    uint8_t periph_id;
645

    
646
    if (args[0] & 7) {
647
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
648
        return;
649
    }
650
    periph_id = (args[0] >> 3) & 0x1f;
651
    if (periph_id >= ch->parent->num_periph_req) {
652
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
653
        return;
654
    }
655
    if (ch->ns && !(ch->parent->cfg[CFG_PNS] & (1 << periph_id))) {
656
        pl330_fault(ch, PL330_FAULT_CH_PERIPH_ERR);
657
        return;
658
    }
659
    /* Do nothing */
660
}
661

    
662
static void pl330_dmago(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
663
{
664
    uint8_t chan_id;
665
    uint8_t ns;
666
    uint32_t pc;
667
    PL330Chan *s;
668

    
669
    DB_PRINT("\n");
670

    
671
    if (!ch->is_manager) {
672
        pl330_fault(ch, PL330_FAULT_UNDEF_INSTR);
673
        return;
674
    }
675
    ns = !!(opcode & 2);
676
    chan_id = args[0] & 7;
677
    if ((args[0] >> 3)) {
678
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
679
        return;
680
    }
681
    if (chan_id >= ch->parent->num_chnls) {
682
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
683
        return;
684
    }
685
    pc = (((uint32_t)args[4]) << 24) | (((uint32_t)args[3]) << 16) |
686
         (((uint32_t)args[2]) << 8)  | (((uint32_t)args[1]));
687
    if (ch->parent->chan[chan_id].state != pl330_chan_stopped) {
688
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
689
        return;
690
    }
691
    if (ch->ns && !ns) {
692
        pl330_fault(ch, PL330_FAULT_DMAGO_ERR);
693
        return;
694
    }
695
    s = &ch->parent->chan[chan_id];
696
    s->ns = ns;
697
    s->pc = pc;
698
    s->state = pl330_chan_executing;
699
}
700

    
701
static void pl330_dmald(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
702
{
703
    uint8_t bs = opcode & 3;
704
    uint32_t size, num;
705
    bool inc;
706

    
707
    if (bs == 2) {
708
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
709
        return;
710
    }
711
    if ((bs == 1 && ch->request_flag == PL330_BURST) ||
712
        (bs == 3 && ch->request_flag == PL330_SINGLE)) {
713
        /* Perform NOP */
714
        return;
715
    }
716
    if (bs == 1 && ch->request_flag == PL330_SINGLE) {
717
        num = 1;
718
    } else {
719
        num = ((ch->control >> 4) & 0xf) + 1;
720
    }
721
    size = (uint32_t)1 << ((ch->control >> 1) & 0x7);
722
    inc = !!(ch->control & 1);
723
    ch->stall = pl330_queue_put_insn(&ch->parent->read_queue, ch->src,
724
                                    size, num, inc, 0, ch->tag);
725
    if (!ch->stall) {
726
        DB_PRINT("channel:%" PRId8 " address:%08" PRIx32 " size:%" PRIx32
727
                 " num:%" PRId32 " %c\n",
728
                 ch->tag, ch->src, size, num, inc ? 'Y' : 'N');
729
        ch->src += inc ? size * num - (ch->src & (size - 1)) : 0;
730
    }
731
}
732

    
733
static void pl330_dmaldp(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
734
{
735
    uint8_t periph_id;
736

    
737
    if (args[0] & 7) {
738
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
739
        return;
740
    }
741
    periph_id = (args[0] >> 3) & 0x1f;
742
    if (periph_id >= ch->parent->num_periph_req) {
743
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
744
        return;
745
    }
746
    if (ch->ns && !(ch->parent->cfg[CFG_PNS] & (1 << periph_id))) {
747
        pl330_fault(ch, PL330_FAULT_CH_PERIPH_ERR);
748
        return;
749
    }
750
    pl330_dmald(ch, opcode, args, len);
751
}
752

    
753
static void pl330_dmalp(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
754
{
755
    uint8_t lc = (opcode & 2) >> 1;
756

    
757
    ch->lc[lc] = args[0];
758
}
759

    
760
static void pl330_dmakill(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
761
{
762
    if (ch->state == pl330_chan_fault ||
763
        ch->state == pl330_chan_fault_completing) {
764
        /* This is the only way for a channel to leave the faulting state */
765
        ch->fault_type = 0;
766
        ch->parent->num_faulting--;
767
        if (ch->parent->num_faulting == 0) {
768
            DB_PRINT("abort interrupt lowered\n");
769
            qemu_irq_lower(ch->parent->irq_abort);
770
        }
771
    }
772
    ch->state = pl330_chan_killing;
773
    pl330_fifo_tagged_remove(&ch->parent->fifo, ch->tag);
774
    pl330_queue_remove_tagged(&ch->parent->read_queue, ch->tag);
775
    pl330_queue_remove_tagged(&ch->parent->write_queue, ch->tag);
776
    ch->state = pl330_chan_stopped;
777
}
778

    
779
static void pl330_dmalpend(PL330Chan *ch, uint8_t opcode,
780
                                    uint8_t *args, int len)
781
{
782
    uint8_t nf = (opcode & 0x10) >> 4;
783
    uint8_t bs = opcode & 3;
784
    uint8_t lc = (opcode & 4) >> 2;
785

    
786
    if (bs == 2) {
787
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
788
        return;
789
    }
790
    if ((bs == 1 && ch->request_flag == PL330_BURST) ||
791
        (bs == 3 && ch->request_flag == PL330_SINGLE)) {
792
        /* Perform NOP */
793
        return;
794
    }
795
    if (!nf || ch->lc[lc]) {
796
        if (nf) {
797
            ch->lc[lc]--;
798
        }
799
        DB_PRINT("loop reiteration\n");
800
        ch->pc -= args[0];
801
        ch->pc -= len + 1;
802
        /* "ch->pc -= args[0] + len + 1" is incorrect when args[0] == 256 */
803
    } else {
804
        DB_PRINT("loop fallthrough\n");
805
    }
806
}
807

    
808

    
809
static void pl330_dmamov(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
810
{
811
    uint8_t rd = args[0] & 7;
812
    uint32_t im;
813

    
814
    if ((args[0] >> 3)) {
815
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
816
        return;
817
    }
818
    im = (((uint32_t)args[4]) << 24) | (((uint32_t)args[3]) << 16) |
819
         (((uint32_t)args[2]) << 8)  | (((uint32_t)args[1]));
820
    switch (rd) {
821
    case 0:
822
        ch->src = im;
823
        break;
824
    case 1:
825
        ch->control = im;
826
        break;
827
    case 2:
828
        ch->dst = im;
829
        break;
830
    default:
831
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
832
        return;
833
    }
834
}
835

    
836
static void pl330_dmanop(PL330Chan *ch, uint8_t opcode,
837
                         uint8_t *args, int len)
838
{
839
    /* NOP is NOP. */
840
}
841

    
842
static void pl330_dmarmb(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
843
{
844
   if (pl330_queue_find_insn(&ch->parent->read_queue, ch->tag, false)) {
845
        ch->state = pl330_chan_at_barrier;
846
        ch->stall = 1;
847
        return;
848
    } else {
849
        ch->state = pl330_chan_executing;
850
    }
851
}
852

    
853
static void pl330_dmasev(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
854
{
855
    uint8_t ev_id;
856

    
857
    if (args[0] & 7) {
858
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
859
        return;
860
    }
861
    ev_id = (args[0] >> 3) & 0x1f;
862
    if (ev_id >= ch->parent->num_events) {
863
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
864
        return;
865
    }
866
    if (ch->ns && !(ch->parent->cfg[CFG_INS] & (1 << ev_id))) {
867
        pl330_fault(ch, PL330_FAULT_EVENT_ERR);
868
        return;
869
    }
870
    if (ch->parent->inten & (1 << ev_id)) {
871
        ch->parent->int_status |= (1 << ev_id);
872
        DB_PRINT("event interrupt raised %" PRId8 "\n", ev_id);
873
        qemu_irq_raise(ch->parent->irq[ev_id]);
874
    }
875
    ch->parent->ev_status |= (1 << ev_id);
876
}
877

    
878
static void pl330_dmast(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
879
{
880
    uint8_t bs = opcode & 3;
881
    uint32_t size, num;
882
    bool inc;
883

    
884
    if (bs == 2) {
885
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
886
        return;
887
    }
888
    if ((bs == 1 && ch->request_flag == PL330_BURST) ||
889
        (bs == 3 && ch->request_flag == PL330_SINGLE)) {
890
        /* Perform NOP */
891
        return;
892
    }
893
    num = ((ch->control >> 18) & 0xf) + 1;
894
    size = (uint32_t)1 << ((ch->control >> 15) & 0x7);
895
    inc = !!((ch->control >> 14) & 1);
896
    ch->stall = pl330_queue_put_insn(&ch->parent->write_queue, ch->dst,
897
                                    size, num, inc, 0, ch->tag);
898
    if (!ch->stall) {
899
        DB_PRINT("channel:%" PRId8 " address:%08" PRIx32 " size:%" PRIx32
900
                 " num:%" PRId32 " %c\n",
901
                 ch->tag, ch->dst, size, num, inc ? 'Y' : 'N');
902
        ch->dst += inc ? size * num - (ch->dst & (size - 1)) : 0;
903
    }
904
}
905

    
906
static void pl330_dmastp(PL330Chan *ch, uint8_t opcode,
907
                         uint8_t *args, int len)
908
{
909
    uint8_t periph_id;
910

    
911
    if (args[0] & 7) {
912
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
913
        return;
914
    }
915
    periph_id = (args[0] >> 3) & 0x1f;
916
    if (periph_id >= ch->parent->num_periph_req) {
917
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
918
        return;
919
    }
920
    if (ch->ns && !(ch->parent->cfg[CFG_PNS] & (1 << periph_id))) {
921
        pl330_fault(ch, PL330_FAULT_CH_PERIPH_ERR);
922
        return;
923
    }
924
    pl330_dmast(ch, opcode, args, len);
925
}
926

    
927
static void pl330_dmastz(PL330Chan *ch, uint8_t opcode,
928
                         uint8_t *args, int len)
929
{
930
    uint32_t size, num;
931
    bool inc;
932

    
933
    num = ((ch->control >> 18) & 0xf) + 1;
934
    size = (uint32_t)1 << ((ch->control >> 15) & 0x7);
935
    inc = !!((ch->control >> 14) & 1);
936
    ch->stall = pl330_queue_put_insn(&ch->parent->write_queue, ch->dst,
937
                                    size, num, inc, 1, ch->tag);
938
    if (inc) {
939
        ch->dst += size * num;
940
    }
941
}
942

    
943
static void pl330_dmawfe(PL330Chan *ch, uint8_t opcode,
944
                         uint8_t *args, int len)
945
{
946
    uint8_t ev_id;
947
    int i;
948

    
949
    if (args[0] & 5) {
950
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
951
        return;
952
    }
953
    ev_id = (args[0] >> 3) & 0x1f;
954
    if (ev_id >= ch->parent->num_events) {
955
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
956
        return;
957
    }
958
    if (ch->ns && !(ch->parent->cfg[CFG_INS] & (1 << ev_id))) {
959
        pl330_fault(ch, PL330_FAULT_EVENT_ERR);
960
        return;
961
    }
962
    ch->wakeup = ev_id;
963
    ch->state = pl330_chan_waiting_event;
964
    if (~ch->parent->inten & ch->parent->ev_status & 1 << ev_id) {
965
        ch->state = pl330_chan_executing;
966
        /* If anyone else is currently waiting on the same event, let them
967
         * clear the ev_status so they pick up event as well
968
         */
969
        for (i = 0; i < ch->parent->num_chnls; ++i) {
970
            PL330Chan *peer = &ch->parent->chan[i];
971
            if (peer->state == pl330_chan_waiting_event &&
972
                    peer->wakeup == ev_id) {
973
                return;
974
            }
975
        }
976
        ch->parent->ev_status &= ~(1 << ev_id);
977
    } else {
978
        ch->stall = 1;
979
    }
980
}
981

    
982
static void pl330_dmawfp(PL330Chan *ch, uint8_t opcode,
983
                         uint8_t *args, int len)
984
{
985
    uint8_t bs = opcode & 3;
986
    uint8_t periph_id;
987

    
988
    if (args[0] & 7) {
989
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
990
        return;
991
    }
992
    periph_id = (args[0] >> 3) & 0x1f;
993
    if (periph_id >= ch->parent->num_periph_req) {
994
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
995
        return;
996
    }
997
    if (ch->ns && !(ch->parent->cfg[CFG_PNS] & (1 << periph_id))) {
998
        pl330_fault(ch, PL330_FAULT_CH_PERIPH_ERR);
999
        return;
1000
    }
1001
    switch (bs) {
1002
    case 0: /* S */
1003
        ch->request_flag = PL330_SINGLE;
1004
        ch->wfp_sbp = 0;
1005
        break;
1006
    case 1: /* P */
1007
        ch->request_flag = PL330_BURST;
1008
        ch->wfp_sbp = 2;
1009
        break;
1010
    case 2: /* B */
1011
        ch->request_flag = PL330_BURST;
1012
        ch->wfp_sbp = 1;
1013
        break;
1014
    default:
1015
        pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
1016
        return;
1017
    }
1018

    
1019
    if (ch->parent->periph_busy[periph_id]) {
1020
        ch->state = pl330_chan_waiting_periph;
1021
        ch->stall = 1;
1022
    } else if (ch->state == pl330_chan_waiting_periph) {
1023
        ch->state = pl330_chan_executing;
1024
    }
1025
}
1026

    
1027
static void pl330_dmawmb(PL330Chan *ch, uint8_t opcode,
1028
                         uint8_t *args, int len)
1029
{
1030
    if (pl330_queue_find_insn(&ch->parent->write_queue, ch->tag, false)) {
1031
        ch->state = pl330_chan_at_barrier;
1032
        ch->stall = 1;
1033
        return;
1034
    } else {
1035
        ch->state = pl330_chan_executing;
1036
    }
1037
}
1038

    
1039
/* NULL terminated array of the instruction descriptions. */
1040
static const PL330InsnDesc insn_desc[] = {
1041
    { .opcode = 0x54, .opmask = 0xFD, .size = 3, .exec = pl330_dmaaddh, },
1042
    { .opcode = 0x00, .opmask = 0xFF, .size = 1, .exec = pl330_dmaend, },
1043
    { .opcode = 0x35, .opmask = 0xFF, .size = 2, .exec = pl330_dmaflushp, },
1044
    { .opcode = 0xA0, .opmask = 0xFD, .size = 6, .exec = pl330_dmago, },
1045
    { .opcode = 0x04, .opmask = 0xFC, .size = 1, .exec = pl330_dmald, },
1046
    { .opcode = 0x25, .opmask = 0xFD, .size = 2, .exec = pl330_dmaldp, },
1047
    { .opcode = 0x20, .opmask = 0xFD, .size = 2, .exec = pl330_dmalp, },
1048
    /* dmastp  must be before dmalpend in this list, because their maps
1049
     * are overlapping
1050
     */
1051
    { .opcode = 0x29, .opmask = 0xFD, .size = 2, .exec = pl330_dmastp, },
1052
    { .opcode = 0x28, .opmask = 0xE8, .size = 2, .exec = pl330_dmalpend, },
1053
    { .opcode = 0x01, .opmask = 0xFF, .size = 1, .exec = pl330_dmakill, },
1054
    { .opcode = 0xBC, .opmask = 0xFF, .size = 6, .exec = pl330_dmamov, },
1055
    { .opcode = 0x18, .opmask = 0xFF, .size = 1, .exec = pl330_dmanop, },
1056
    { .opcode = 0x12, .opmask = 0xFF, .size = 1, .exec = pl330_dmarmb, },
1057
    { .opcode = 0x34, .opmask = 0xFF, .size = 2, .exec = pl330_dmasev, },
1058
    { .opcode = 0x08, .opmask = 0xFC, .size = 1, .exec = pl330_dmast, },
1059
    { .opcode = 0x0C, .opmask = 0xFF, .size = 1, .exec = pl330_dmastz, },
1060
    { .opcode = 0x36, .opmask = 0xFF, .size = 2, .exec = pl330_dmawfe, },
1061
    { .opcode = 0x30, .opmask = 0xFC, .size = 2, .exec = pl330_dmawfp, },
1062
    { .opcode = 0x13, .opmask = 0xFF, .size = 1, .exec = pl330_dmawmb, },
1063
    { .opcode = 0x00, .opmask = 0x00, .size = 0, .exec = NULL, }
1064
};
1065

    
1066
/* Instructions which can be issued via debug registers. */
1067
static const PL330InsnDesc debug_insn_desc[] = {
1068
    { .opcode = 0xA0, .opmask = 0xFD, .size = 6, .exec = pl330_dmago, },
1069
    { .opcode = 0x01, .opmask = 0xFF, .size = 1, .exec = pl330_dmakill, },
1070
    { .opcode = 0x34, .opmask = 0xFF, .size = 2, .exec = pl330_dmasev, },
1071
    { .opcode = 0x00, .opmask = 0x00, .size = 0, .exec = NULL, }
1072
};
1073

    
1074
static inline const PL330InsnDesc *pl330_fetch_insn(PL330Chan *ch)
1075
{
1076
    uint8_t opcode;
1077
    int i;
1078

    
1079
    dma_memory_read(&address_space_memory, ch->pc, &opcode, 1);
1080
    for (i = 0; insn_desc[i].size; i++) {
1081
        if ((opcode & insn_desc[i].opmask) == insn_desc[i].opcode) {
1082
            return &insn_desc[i];
1083
        }
1084
    }
1085
    return NULL;
1086
}
1087

    
1088
static inline void pl330_exec_insn(PL330Chan *ch, const PL330InsnDesc *insn)
1089
{
1090
    uint8_t buf[PL330_INSN_MAXSIZE];
1091

    
1092
    assert(insn->size <= PL330_INSN_MAXSIZE);
1093
    dma_memory_read(&address_space_memory, ch->pc, buf, insn->size);
1094
    insn->exec(ch, buf[0], &buf[1], insn->size - 1);
1095
}
1096

    
1097
static inline void pl330_update_pc(PL330Chan *ch,
1098
                                   const PL330InsnDesc *insn)
1099
{
1100
    ch->pc += insn->size;
1101
}
1102

    
1103
/* Try to execute current instruction in channel CH. Number of executed
1104
   instructions is returned (0 or 1). */
1105
static int pl330_chan_exec(PL330Chan *ch)
1106
{
1107
    const PL330InsnDesc *insn;
1108

    
1109
    if (ch->state != pl330_chan_executing &&
1110
            ch->state != pl330_chan_waiting_periph &&
1111
            ch->state != pl330_chan_at_barrier &&
1112
            ch->state != pl330_chan_waiting_event) {
1113
        return 0;
1114
    }
1115
    ch->stall = 0;
1116
    insn = pl330_fetch_insn(ch);
1117
    if (!insn) {
1118
        DB_PRINT("pl330 undefined instruction\n");
1119
        pl330_fault(ch, PL330_FAULT_UNDEF_INSTR);
1120
        return 0;
1121
    }
1122
    pl330_exec_insn(ch, insn);
1123
    if (!ch->stall) {
1124
        pl330_update_pc(ch, insn);
1125
        ch->watchdog_timer = 0;
1126
        return 1;
1127
    /* WDT only active in exec state */
1128
    } else if (ch->state == pl330_chan_executing) {
1129
        ch->watchdog_timer++;
1130
        if (ch->watchdog_timer >= PL330_WATCHDOG_LIMIT) {
1131
            pl330_fault(ch, PL330_FAULT_LOCKUP_ERR);
1132
        }
1133
    }
1134
    return 0;
1135
}
1136

    
1137
/* Try to execute 1 instruction in each channel, one instruction from read
1138
   queue and one instruction from write queue. Number of successfully executed
1139
   instructions is returned. */
1140
static int pl330_exec_cycle(PL330Chan *channel)
1141
{
1142
    PL330State *s = channel->parent;
1143
    PL330QueueEntry *q;
1144
    int i;
1145
    int num_exec = 0;
1146
    int fifo_res = 0;
1147
    uint8_t buf[PL330_MAX_BURST_LEN];
1148

    
1149
    /* Execute one instruction in each channel */
1150
    num_exec += pl330_chan_exec(channel);
1151

    
1152
    /* Execute one instruction from read queue */
1153
    q = pl330_queue_find_insn(&s->read_queue, PL330_UNTAGGED, true);
1154
    if (q != NULL && q->len <= pl330_fifo_num_free(&s->fifo)) {
1155
        int len = q->len - (q->addr & (q->len - 1));
1156

    
1157
        dma_memory_read(&address_space_memory, q->addr, buf, len);
1158
        if (PL330_ERR_DEBUG > 1) {
1159
            DB_PRINT("PL330 read from memory @%08" PRIx32 " (size = %08x):\n",
1160
                      q->addr, len);
1161
            qemu_hexdump((char *)buf, stderr, "", len);
1162
        }
1163
        fifo_res = pl330_fifo_push(&s->fifo, buf, len, q->tag);
1164
        if (fifo_res == PL330_FIFO_OK) {
1165
            if (q->inc) {
1166
                q->addr += len;
1167
            }
1168
            q->n--;
1169
            if (!q->n) {
1170
                pl330_queue_remove_insn(&s->read_queue, q);
1171
            }
1172
            num_exec++;
1173
        }
1174
    }
1175

    
1176
    /* Execute one instruction from write queue. */
1177
    q = pl330_queue_find_insn(&s->write_queue, pl330_fifo_tag(&s->fifo), true);
1178
    if (q != NULL) {
1179
        int len = q->len - (q->addr & (q->len - 1));
1180

    
1181
        if (q->z) {
1182
            for (i = 0; i < len; i++) {
1183
                buf[i] = 0;
1184
            }
1185
        } else {
1186
            fifo_res = pl330_fifo_get(&s->fifo, buf, len, q->tag);
1187
        }
1188
        if (fifo_res == PL330_FIFO_OK || q->z) {
1189
            dma_memory_write(&address_space_memory, q->addr, buf, len);
1190
            if (PL330_ERR_DEBUG > 1) {
1191
                DB_PRINT("PL330 read from memory @%08" PRIx32
1192
                         " (size = %08x):\n", q->addr, len);
1193
                qemu_hexdump((char *)buf, stderr, "", len);
1194
            }
1195
            if (q->inc) {
1196
                q->addr += len;
1197
            }
1198
            num_exec++;
1199
        } else if (fifo_res == PL330_FIFO_STALL) {
1200
            pl330_fault(&channel->parent->chan[q->tag],
1201
                                PL330_FAULT_FIFOEMPTY_ERR);
1202
        }
1203
        q->n--;
1204
        if (!q->n) {
1205
            pl330_queue_remove_insn(&s->write_queue, q);
1206
        }
1207
    }
1208

    
1209
    return num_exec;
1210
}
1211

    
1212
static int pl330_exec_channel(PL330Chan *channel)
1213
{
1214
    int insr_exec = 0;
1215

    
1216
    /* TODO: Is it all right to execute everything or should we do per-cycle
1217
       simulation? */
1218
    while (pl330_exec_cycle(channel)) {
1219
        insr_exec++;
1220
    }
1221

    
1222
    /* Detect deadlock */
1223
    if (channel->state == pl330_chan_executing) {
1224
        pl330_fault(channel, PL330_FAULT_LOCKUP_ERR);
1225
    }
1226
    /* Situation when one of the queues has deadlocked but all channels
1227
     * have finished their programs should be impossible.
1228
     */
1229

    
1230
    return insr_exec;
1231
}
1232

    
1233
static inline void pl330_exec(PL330State *s)
1234
{
1235
    DB_PRINT("\n");
1236
    int i, insr_exec;
1237
    do {
1238
        insr_exec = pl330_exec_channel(&s->manager);
1239

    
1240
        for (i = 0; i < s->num_chnls; i++) {
1241
            insr_exec += pl330_exec_channel(&s->chan[i]);
1242
        }
1243
    } while (insr_exec);
1244
}
1245

    
1246
static void pl330_exec_cycle_timer(void *opaque)
1247
{
1248
    PL330State *s = (PL330State *)opaque;
1249
    pl330_exec(s);
1250
}
1251

    
1252
/* Stop or restore dma operations */
1253

    
1254
static void pl330_dma_stop_irq(void *opaque, int irq, int level)
1255
{
1256
    PL330State *s = (PL330State *)opaque;
1257

    
1258
    if (s->periph_busy[irq] != level) {
1259
        s->periph_busy[irq] = level;
1260
        timer_mod(s->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
1261
    }
1262
}
1263

    
1264
static void pl330_debug_exec(PL330State *s)
1265
{
1266
    uint8_t args[5];
1267
    uint8_t opcode;
1268
    uint8_t chan_id;
1269
    int i;
1270
    PL330Chan *ch;
1271
    const PL330InsnDesc *insn;
1272

    
1273
    s->debug_status = 1;
1274
    chan_id = (s->dbg[0] >>  8) & 0x07;
1275
    opcode  = (s->dbg[0] >> 16) & 0xff;
1276
    args[0] = (s->dbg[0] >> 24) & 0xff;
1277
    args[1] = (s->dbg[1] >>  0) & 0xff;
1278
    args[2] = (s->dbg[1] >>  8) & 0xff;
1279
    args[3] = (s->dbg[1] >> 16) & 0xff;
1280
    args[4] = (s->dbg[1] >> 24) & 0xff;
1281
    DB_PRINT("chan id: %" PRIx8 "\n", chan_id);
1282
    if (s->dbg[0] & 1) {
1283
        ch = &s->chan[chan_id];
1284
    } else {
1285
        ch = &s->manager;
1286
    }
1287
    insn = NULL;
1288
    for (i = 0; debug_insn_desc[i].size; i++) {
1289
        if ((opcode & debug_insn_desc[i].opmask) == debug_insn_desc[i].opcode) {
1290
            insn = &debug_insn_desc[i];
1291
        }
1292
    }
1293
    if (!insn) {
1294
        pl330_fault(ch, PL330_FAULT_UNDEF_INSTR | PL330_FAULT_DBG_INSTR);
1295
        return ;
1296
    }
1297
    ch->stall = 0;
1298
    insn->exec(ch, opcode, args, insn->size - 1);
1299
    if (ch->fault_type) {
1300
        ch->fault_type |= PL330_FAULT_DBG_INSTR;
1301
    }
1302
    if (ch->stall) {
1303
        qemu_log_mask(LOG_UNIMP, "pl330: stall of debug instruction not "
1304
                      "implemented\n");
1305
    }
1306
    s->debug_status = 0;
1307
}
1308

    
1309
/* IOMEM mapped registers */
1310

    
1311
static void pl330_iomem_write(void *opaque, hwaddr offset,
1312
                              uint64_t value, unsigned size)
1313
{
1314
    PL330State *s = (PL330State *) opaque;
1315
    int i;
1316

    
1317
    DB_PRINT("addr: %08x data: %08x\n", (unsigned)offset, (unsigned)value);
1318

    
1319
    switch (offset) {
1320
    case PL330_REG_INTEN:
1321
        s->inten = value;
1322
        break;
1323
    case PL330_REG_INTCLR:
1324
        for (i = 0; i < s->num_events; i++) {
1325
            if (s->int_status & s->inten & value & (1 << i)) {
1326
                DB_PRINT("event interrupt lowered %d\n", i);
1327
                qemu_irq_lower(s->irq[i]);
1328
            }
1329
        }
1330
        s->ev_status &= ~(value & s->inten);
1331
        s->int_status &= ~(value & s->inten);
1332
        break;
1333
    case PL330_REG_DBGCMD:
1334
        if ((value & 3) == 0) {
1335
            pl330_debug_exec(s);
1336
            pl330_exec(s);
1337
        } else {
1338
            qemu_log_mask(LOG_GUEST_ERROR, "pl330: write of illegal value %u "
1339
                          "for offset " TARGET_FMT_plx "\n", (unsigned)value,
1340
                          offset);
1341
        }
1342
        break;
1343
    case PL330_REG_DBGINST0:
1344
        DB_PRINT("s->dbg[0] = %08x\n", (unsigned)value);
1345
        s->dbg[0] = value;
1346
        break;
1347
    case PL330_REG_DBGINST1:
1348
        DB_PRINT("s->dbg[1] = %08x\n", (unsigned)value);
1349
        s->dbg[1] = value;
1350
        break;
1351
    default:
1352
        qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad write offset " TARGET_FMT_plx
1353
                      "\n", offset);
1354
        break;
1355
    }
1356
}
1357

    
1358
static inline uint32_t pl330_iomem_read_imp(void *opaque,
1359
        hwaddr offset)
1360
{
1361
    PL330State *s = (PL330State *)opaque;
1362
    int chan_id;
1363
    int i;
1364
    uint32_t res;
1365

    
1366
    if (offset >= PL330_REG_PERIPH_ID && offset < PL330_REG_PERIPH_ID + 32) {
1367
        return pl330_id[(offset - PL330_REG_PERIPH_ID) >> 2];
1368
    }
1369
    if (offset >= PL330_REG_CR0_BASE && offset < PL330_REG_CR0_BASE + 24) {
1370
        return s->cfg[(offset - PL330_REG_CR0_BASE) >> 2];
1371
    }
1372
    if (offset >= PL330_REG_CHANCTRL && offset < PL330_REG_DBGSTATUS) {
1373
        offset -= PL330_REG_CHANCTRL;
1374
        chan_id = offset >> 5;
1375
        if (chan_id >= s->num_chnls) {
1376
            qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad read offset "
1377
                          TARGET_FMT_plx "\n", offset);
1378
            return 0;
1379
        }
1380
        switch (offset & 0x1f) {
1381
        case 0x00:
1382
            return s->chan[chan_id].src;
1383
        case 0x04:
1384
            return s->chan[chan_id].dst;
1385
        case 0x08:
1386
            return s->chan[chan_id].control;
1387
        case 0x0C:
1388
            return s->chan[chan_id].lc[0];
1389
        case 0x10:
1390
            return s->chan[chan_id].lc[1];
1391
        default:
1392
            qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad read offset "
1393
                          TARGET_FMT_plx "\n", offset);
1394
            return 0;
1395
        }
1396
    }
1397
    if (offset >= PL330_REG_CSR_BASE && offset < 0x400) {
1398
        offset -= PL330_REG_CSR_BASE;
1399
        chan_id = offset >> 3;
1400
        if (chan_id >= s->num_chnls) {
1401
            qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad read offset "
1402
                          TARGET_FMT_plx "\n", offset);
1403
            return 0;
1404
        }
1405
        switch ((offset >> 2) & 1) {
1406
        case 0x0:
1407
            res = (s->chan[chan_id].ns << 21) |
1408
                    (s->chan[chan_id].wakeup << 4) |
1409
                    (s->chan[chan_id].state) |
1410
                    (s->chan[chan_id].wfp_sbp << 14);
1411
            return res;
1412
        case 0x1:
1413
            return s->chan[chan_id].pc;
1414
        default:
1415
            qemu_log_mask(LOG_GUEST_ERROR, "pl330: read error\n");
1416
            return 0;
1417
        }
1418
    }
1419
    if (offset >= PL330_REG_FTR_BASE && offset < 0x100) {
1420
        offset -= PL330_REG_FTR_BASE;
1421
        chan_id = offset >> 2;
1422
        if (chan_id >= s->num_chnls) {
1423
            qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad read offset "
1424
                          TARGET_FMT_plx "\n", offset);
1425
            return 0;
1426
        }
1427
        return s->chan[chan_id].fault_type;
1428
    }
1429
    switch (offset) {
1430
    case PL330_REG_DSR:
1431
        return (s->manager.ns << 9) | (s->manager.wakeup << 4) |
1432
            (s->manager.state & 0xf);
1433
    case PL330_REG_DPC:
1434
        return s->manager.pc;
1435
    case PL330_REG_INTEN:
1436
        return s->inten;
1437
    case PL330_REG_INT_EVENT_RIS:
1438
        return s->ev_status;
1439
    case PL330_REG_INTMIS:
1440
        return s->int_status;
1441
    case PL330_REG_INTCLR:
1442
        /* Documentation says that we can't read this register
1443
         * but linux kernel does it
1444
         */
1445
        return 0;
1446
    case PL330_REG_FSRD:
1447
        return s->manager.state ? 1 : 0;
1448
    case PL330_REG_FSRC:
1449
        res = 0;
1450
        for (i = 0; i < s->num_chnls; i++) {
1451
            if (s->chan[i].state == pl330_chan_fault ||
1452
                s->chan[i].state == pl330_chan_fault_completing) {
1453
                res |= 1 << i;
1454
            }
1455
        }
1456
        return res;
1457
    case PL330_REG_FTRD:
1458
        return s->manager.fault_type;
1459
    case PL330_REG_DBGSTATUS:
1460
        return s->debug_status;
1461
    default:
1462
        qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad read offset "
1463
                      TARGET_FMT_plx "\n", offset);
1464
    }
1465
    return 0;
1466
}
1467

    
1468
static uint64_t pl330_iomem_read(void *opaque, hwaddr offset,
1469
        unsigned size)
1470
{
1471
    uint32_t ret = pl330_iomem_read_imp(opaque, offset);
1472
    DB_PRINT("addr: %08" HWADDR_PRIx " data: %08" PRIx32 "\n", offset, ret);
1473
    return ret;
1474
}
1475

    
1476
static const MemoryRegionOps pl330_ops = {
1477
    .read = pl330_iomem_read,
1478
    .write = pl330_iomem_write,
1479
    .endianness = DEVICE_NATIVE_ENDIAN,
1480
    .impl = {
1481
        .min_access_size = 4,
1482
        .max_access_size = 4,
1483
    }
1484
};
1485

    
1486
/* Controller logic and initialization */
1487

    
1488
static void pl330_chan_reset(PL330Chan *ch)
1489
{
1490
    ch->src = 0;
1491
    ch->dst = 0;
1492
    ch->pc = 0;
1493
    ch->state = pl330_chan_stopped;
1494
    ch->watchdog_timer = 0;
1495
    ch->stall = 0;
1496
    ch->control = 0;
1497
    ch->status = 0;
1498
    ch->fault_type = 0;
1499
}
1500

    
1501
static void pl330_reset(DeviceState *d)
1502
{
1503
    int i;
1504
    PL330State *s = PL330(d);
1505

    
1506
    s->inten = 0;
1507
    s->int_status = 0;
1508
    s->ev_status = 0;
1509
    s->debug_status = 0;
1510
    s->num_faulting = 0;
1511
    s->manager.ns = s->mgr_ns_at_rst;
1512
    pl330_fifo_reset(&s->fifo);
1513
    pl330_queue_reset(&s->read_queue);
1514
    pl330_queue_reset(&s->write_queue);
1515

    
1516
    for (i = 0; i < s->num_chnls; i++) {
1517
        pl330_chan_reset(&s->chan[i]);
1518
    }
1519
    for (i = 0; i < s->num_periph_req; i++) {
1520
        s->periph_busy[i] = 0;
1521
    }
1522

    
1523
    timer_del(s->timer);
1524
}
1525

    
1526
static void pl330_realize(DeviceState *dev, Error **errp)
1527
{
1528
    int i;
1529
    PL330State *s = PL330(dev);
1530

    
1531
    sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq_abort);
1532
    memory_region_init_io(&s->iomem, OBJECT(s), &pl330_ops, s,
1533
                          "dma", PL330_IOMEM_SIZE);
1534
    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
1535

    
1536
    s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, pl330_exec_cycle_timer, s);
1537

    
1538
    s->cfg[0] = (s->mgr_ns_at_rst ? 0x4 : 0) |
1539
                (s->num_periph_req > 0 ? 1 : 0) |
1540
                ((s->num_chnls - 1) & 0x7) << 4 |
1541
                ((s->num_periph_req - 1) & 0x1f) << 12 |
1542
                ((s->num_events - 1) & 0x1f) << 17;
1543

    
1544
    switch (s->i_cache_len) {
1545
    case (4):
1546
        s->cfg[1] |= 2;
1547
        break;
1548
    case (8):
1549
        s->cfg[1] |= 3;
1550
        break;
1551
    case (16):
1552
        s->cfg[1] |= 4;
1553
        break;
1554
    case (32):
1555
        s->cfg[1] |= 5;
1556
        break;
1557
    default:
1558
        error_setg(errp, "Bad value for i-cache_len property: %" PRIx8 "\n",
1559
                   s->i_cache_len);
1560
        return;
1561
    }
1562
    s->cfg[1] |= ((s->num_i_cache_lines - 1) & 0xf) << 4;
1563

    
1564
    s->chan = g_new0(PL330Chan, s->num_chnls);
1565
    s->hi_seqn = g_new0(uint8_t, s->num_chnls);
1566
    s->lo_seqn = g_new0(uint8_t, s->num_chnls);
1567
    for (i = 0; i < s->num_chnls; i++) {
1568
        s->chan[i].parent = s;
1569
        s->chan[i].tag = (uint8_t)i;
1570
    }
1571
    s->manager.parent = s;
1572
    s->manager.tag = s->num_chnls;
1573
    s->manager.is_manager = true;
1574

    
1575
    s->irq = g_new0(qemu_irq, s->num_events);
1576
    for (i = 0; i < s->num_events; i++) {
1577
        sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq[i]);
1578
    }
1579

    
1580
    qdev_init_gpio_in(dev, pl330_dma_stop_irq, PL330_PERIPH_NUM);
1581

    
1582
    switch (s->data_width) {
1583
    case (32):
1584
        s->cfg[CFG_CRD] |= 0x2;
1585
        break;
1586
    case (64):
1587
        s->cfg[CFG_CRD] |= 0x3;
1588
        break;
1589
    case (128):
1590
        s->cfg[CFG_CRD] |= 0x4;
1591
        break;
1592
    default:
1593
        error_setg(errp, "Bad value for data_width property: %" PRIx8 "\n",
1594
                   s->data_width);
1595
        return;
1596
    }
1597

    
1598
    s->cfg[CFG_CRD] |= ((s->wr_cap - 1) & 0x7) << 4 |
1599
                    ((s->wr_q_dep - 1) & 0xf) << 8 |
1600
                    ((s->rd_cap - 1) & 0x7) << 12 |
1601
                    ((s->rd_q_dep - 1) & 0xf) << 16 |
1602
                    ((s->data_buffer_dep - 1) & 0x1ff) << 20;
1603

    
1604
    pl330_queue_init(&s->read_queue, s->rd_q_dep, s);
1605
    pl330_queue_init(&s->write_queue, s->wr_q_dep, s);
1606
    pl330_fifo_init(&s->fifo, s->data_buffer_dep);
1607
}
1608

    
1609
static Property pl330_properties[] = {
1610
    /* CR0 */
1611
    DEFINE_PROP_UINT32("num_chnls", PL330State, num_chnls, 8),
1612
    DEFINE_PROP_UINT8("num_periph_req", PL330State, num_periph_req, 4),
1613
    DEFINE_PROP_UINT8("num_events", PL330State, num_events, 16),
1614
    DEFINE_PROP_UINT8("mgr_ns_at_rst", PL330State, mgr_ns_at_rst, 0),
1615
    /* CR1 */
1616
    DEFINE_PROP_UINT8("i-cache_len", PL330State, i_cache_len, 4),
1617
    DEFINE_PROP_UINT8("num_i-cache_lines", PL330State, num_i_cache_lines, 8),
1618
    /* CR2-4 */
1619
    DEFINE_PROP_UINT32("boot_addr", PL330State, cfg[CFG_BOOT_ADDR], 0),
1620
    DEFINE_PROP_UINT32("INS", PL330State, cfg[CFG_INS], 0),
1621
    DEFINE_PROP_UINT32("PNS", PL330State, cfg[CFG_PNS], 0),
1622
    /* CRD */
1623
    DEFINE_PROP_UINT8("data_width", PL330State, data_width, 64),
1624
    DEFINE_PROP_UINT8("wr_cap", PL330State, wr_cap, 8),
1625
    DEFINE_PROP_UINT8("wr_q_dep", PL330State, wr_q_dep, 16),
1626
    DEFINE_PROP_UINT8("rd_cap", PL330State, rd_cap, 8),
1627
    DEFINE_PROP_UINT8("rd_q_dep", PL330State, rd_q_dep, 16),
1628
    DEFINE_PROP_UINT16("data_buffer_dep", PL330State, data_buffer_dep, 256),
1629

    
1630
    DEFINE_PROP_END_OF_LIST(),
1631
};
1632

    
1633
static void pl330_class_init(ObjectClass *klass, void *data)
1634
{
1635
    DeviceClass *dc = DEVICE_CLASS(klass);
1636

    
1637
    dc->realize = pl330_realize;
1638
    dc->reset = pl330_reset;
1639
    dc->props = pl330_properties;
1640
    dc->vmsd = &vmstate_pl330;
1641
}
1642

    
1643
static const TypeInfo pl330_type_info = {
1644
    .name           = TYPE_PL330,
1645
    .parent         = TYPE_SYS_BUS_DEVICE,
1646
    .instance_size  = sizeof(PL330State),
1647
    .class_init      = pl330_class_init,
1648
};
1649

    
1650
static void pl330_register_types(void)
1651
{
1652
    type_register_static(&pl330_type_info);
1653
}
1654

    
1655
type_init(pl330_register_types)