2 * ARM PrimeCell PL330 DMA Controller
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.
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.
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/>.
17 #include "qemu/osdep.h"
18 #include "qemu-common.h"
20 #include "hw/qdev-properties.h"
21 #include "hw/sysbus.h"
22 #include "migration/vmstate.h"
23 #include "qapi/error.h"
24 #include "qemu/timer.h"
25 #include "sysemu/dma.h"
27 #include "qemu/module.h"
29 #ifndef PL330_ERR_DEBUG
30 #define PL330_ERR_DEBUG 0
33 #define DB_PRINT_L(lvl, fmt, args...) do {\
34 if (PL330_ERR_DEBUG >= lvl) {\
35 fprintf(stderr, "PL330: %s:" fmt, __func__, ## args);\
39 #define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
41 #define PL330_PERIPH_NUM 32
42 #define PL330_MAX_BURST_LEN 128
43 #define PL330_INSN_MAXSIZE 6
45 #define PL330_FIFO_OK 0
46 #define PL330_FIFO_STALL 1
47 #define PL330_FIFO_ERR (-1)
49 #define PL330_FAULT_UNDEF_INSTR (1 << 0)
50 #define PL330_FAULT_OPERAND_INVALID (1 << 1)
51 #define PL330_FAULT_DMAGO_ERR (1 << 4)
52 #define PL330_FAULT_EVENT_ERR (1 << 5)
53 #define PL330_FAULT_CH_PERIPH_ERR (1 << 6)
54 #define PL330_FAULT_CH_RDWR_ERR (1 << 7)
55 #define PL330_FAULT_ST_DATA_UNAVAILABLE (1 << 12)
56 #define PL330_FAULT_FIFOEMPTY_ERR (1 << 13)
57 #define PL330_FAULT_INSTR_FETCH_ERR (1 << 16)
58 #define PL330_FAULT_DATA_WRITE_ERR (1 << 17)
59 #define PL330_FAULT_DATA_READ_ERR (1 << 18)
60 #define PL330_FAULT_DBG_INSTR (1 << 30)
61 #define PL330_FAULT_LOCKUP_ERR (1 << 31)
63 #define PL330_UNTAGGED 0xff
65 #define PL330_SINGLE 0x0
66 #define PL330_BURST 0x1
68 #define PL330_WATCHDOG_LIMIT 1024
70 /* IOMEM mapped registers */
71 #define PL330_REG_DSR 0x000
72 #define PL330_REG_DPC 0x004
73 #define PL330_REG_INTEN 0x020
74 #define PL330_REG_INT_EVENT_RIS 0x024
75 #define PL330_REG_INTMIS 0x028
76 #define PL330_REG_INTCLR 0x02C
77 #define PL330_REG_FSRD 0x030
78 #define PL330_REG_FSRC 0x034
79 #define PL330_REG_FTRD 0x038
80 #define PL330_REG_FTR_BASE 0x040
81 #define PL330_REG_CSR_BASE 0x100
82 #define PL330_REG_CPC_BASE 0x104
83 #define PL330_REG_CHANCTRL 0x400
84 #define PL330_REG_DBGSTATUS 0xD00
85 #define PL330_REG_DBGCMD 0xD04
86 #define PL330_REG_DBGINST0 0xD08
87 #define PL330_REG_DBGINST1 0xD0C
88 #define PL330_REG_CR0_BASE 0xE00
89 #define PL330_REG_PERIPH_ID 0xFE0
91 #define PL330_IOMEM_SIZE 0x1000
93 #define CFG_BOOT_ADDR 2
98 static const uint32_t pl330_id
[] = {
99 0x30, 0x13, 0x24, 0x00, 0x0D, 0xF0, 0x05, 0xB1
102 /* DMA channel states as they are described in PL330 Technical Reference Manual
103 * Most of them will not be used in emulation.
106 pl330_chan_stopped
= 0,
107 pl330_chan_executing
= 1,
108 pl330_chan_cache_miss
= 2,
109 pl330_chan_updating_pc
= 3,
110 pl330_chan_waiting_event
= 4,
111 pl330_chan_at_barrier
= 5,
112 pl330_chan_queue_busy
= 6,
113 pl330_chan_waiting_periph
= 7,
114 pl330_chan_killing
= 8,
115 pl330_chan_completing
= 9,
116 pl330_chan_fault_completing
= 14,
117 pl330_chan_fault
= 15,
120 typedef struct PL330State PL330State
;
122 typedef struct PL330Chan
{
130 uint32_t watchdog_timer
;
133 uint8_t request_flag
;
145 static const VMStateDescription vmstate_pl330_chan
= {
146 .name
= "pl330_chan",
148 .minimum_version_id
= 1,
149 .fields
= (VMStateField
[]) {
150 VMSTATE_UINT32(src
, PL330Chan
),
151 VMSTATE_UINT32(dst
, PL330Chan
),
152 VMSTATE_UINT32(pc
, PL330Chan
),
153 VMSTATE_UINT32(control
, PL330Chan
),
154 VMSTATE_UINT32(status
, PL330Chan
),
155 VMSTATE_UINT32_ARRAY(lc
, PL330Chan
, 2),
156 VMSTATE_UINT32(fault_type
, PL330Chan
),
157 VMSTATE_UINT32(watchdog_timer
, PL330Chan
),
158 VMSTATE_BOOL(ns
, PL330Chan
),
159 VMSTATE_UINT8(request_flag
, PL330Chan
),
160 VMSTATE_UINT8(wakeup
, PL330Chan
),
161 VMSTATE_UINT8(wfp_sbp
, PL330Chan
),
162 VMSTATE_UINT8(state
, PL330Chan
),
163 VMSTATE_UINT8(stall
, PL330Chan
),
164 VMSTATE_END_OF_LIST()
168 typedef struct PL330Fifo
{
176 static const VMStateDescription vmstate_pl330_fifo
= {
177 .name
= "pl330_chan",
179 .minimum_version_id
= 1,
180 .fields
= (VMStateField
[]) {
181 VMSTATE_VBUFFER_UINT32(buf
, PL330Fifo
, 1, NULL
, buf_size
),
182 VMSTATE_VBUFFER_UINT32(tag
, PL330Fifo
, 1, NULL
, buf_size
),
183 VMSTATE_UINT32(head
, PL330Fifo
),
184 VMSTATE_UINT32(num
, PL330Fifo
),
185 VMSTATE_UINT32(buf_size
, PL330Fifo
),
186 VMSTATE_END_OF_LIST()
190 typedef struct PL330QueueEntry
{
200 static const VMStateDescription vmstate_pl330_queue_entry
= {
201 .name
= "pl330_queue_entry",
203 .minimum_version_id
= 1,
204 .fields
= (VMStateField
[]) {
205 VMSTATE_UINT32(addr
, PL330QueueEntry
),
206 VMSTATE_UINT32(len
, PL330QueueEntry
),
207 VMSTATE_UINT8(n
, PL330QueueEntry
),
208 VMSTATE_BOOL(inc
, PL330QueueEntry
),
209 VMSTATE_BOOL(z
, PL330QueueEntry
),
210 VMSTATE_UINT8(tag
, PL330QueueEntry
),
211 VMSTATE_UINT8(seqn
, PL330QueueEntry
),
212 VMSTATE_END_OF_LIST()
216 typedef struct PL330Queue
{
218 PL330QueueEntry
*queue
;
222 static const VMStateDescription vmstate_pl330_queue
= {
223 .name
= "pl330_queue",
225 .minimum_version_id
= 2,
226 .fields
= (VMStateField
[]) {
227 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(queue
, PL330Queue
, queue_size
,
228 vmstate_pl330_queue_entry
,
230 VMSTATE_END_OF_LIST()
235 SysBusDevice parent_obj
;
241 /* Config registers. cfg[5] = CfgDn. */
243 #define EVENT_SEC_STATE 3
244 #define PERIPH_SEC_STATE 4
245 /* cfg 0 bits and pieces */
247 uint8_t num_periph_req
;
249 uint8_t mgr_ns_at_rst
;
250 /* cfg 1 bits and pieces */
252 uint8_t num_i_cache_lines
;
253 /* CRD bits and pieces */
259 uint16_t data_buffer_dep
;
264 PL330Queue read_queue
;
265 PL330Queue write_queue
;
268 QEMUTimer
*timer
; /* is used for restore dma. */
274 uint8_t debug_status
;
275 uint8_t num_faulting
;
276 uint8_t periph_busy
[PL330_PERIPH_NUM
];
280 #define TYPE_PL330 "pl330"
281 #define PL330(obj) OBJECT_CHECK(PL330State, (obj), TYPE_PL330)
283 static const VMStateDescription vmstate_pl330
= {
286 .minimum_version_id
= 2,
287 .fields
= (VMStateField
[]) {
288 VMSTATE_STRUCT(manager
, PL330State
, 0, vmstate_pl330_chan
, PL330Chan
),
289 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(chan
, PL330State
, num_chnls
,
290 vmstate_pl330_chan
, PL330Chan
),
291 VMSTATE_VBUFFER_UINT32(lo_seqn
, PL330State
, 1, NULL
, num_chnls
),
292 VMSTATE_VBUFFER_UINT32(hi_seqn
, PL330State
, 1, NULL
, num_chnls
),
293 VMSTATE_STRUCT(fifo
, PL330State
, 0, vmstate_pl330_fifo
, PL330Fifo
),
294 VMSTATE_STRUCT(read_queue
, PL330State
, 0, vmstate_pl330_queue
,
296 VMSTATE_STRUCT(write_queue
, PL330State
, 0, vmstate_pl330_queue
,
298 VMSTATE_TIMER_PTR(timer
, PL330State
),
299 VMSTATE_UINT32(inten
, PL330State
),
300 VMSTATE_UINT32(int_status
, PL330State
),
301 VMSTATE_UINT32(ev_status
, PL330State
),
302 VMSTATE_UINT32_ARRAY(dbg
, PL330State
, 2),
303 VMSTATE_UINT8(debug_status
, PL330State
),
304 VMSTATE_UINT8(num_faulting
, PL330State
),
305 VMSTATE_UINT8_ARRAY(periph_busy
, PL330State
, PL330_PERIPH_NUM
),
306 VMSTATE_END_OF_LIST()
310 typedef struct PL330InsnDesc
{
311 /* OPCODE of the instruction */
313 /* Mask so we can select several sibling instructions, such as
314 DMALD, DMALDS and DMALDB */
316 /* Size of instruction in bytes */
319 void (*exec
)(PL330Chan
*, uint8_t opcode
, uint8_t *args
, int len
);
323 /* MFIFO Implementation
325 * MFIFO is implemented as a cyclic buffer of BUF_SIZE size. Tagged bytes are
326 * stored in this buffer. Data is stored in BUF field, tags - in the
327 * corresponding array elements of TAG field.
330 /* Initialize queue. */
332 static void pl330_fifo_init(PL330Fifo
*s
, uint32_t size
)
334 s
->buf
= g_malloc0(size
);
335 s
->tag
= g_malloc0(size
);
339 /* Cyclic increment */
341 static inline int pl330_fifo_inc(PL330Fifo
*s
, int x
)
343 return (x
+ 1) % s
->buf_size
;
346 /* Number of empty bytes in MFIFO */
348 static inline int pl330_fifo_num_free(PL330Fifo
*s
)
350 return s
->buf_size
- s
->num
;
353 /* Push LEN bytes of data stored in BUF to MFIFO and tag it with TAG.
354 * Zero returned on success, PL330_FIFO_STALL if there is no enough free
355 * space in MFIFO to store requested amount of data. If push was unsuccessful
356 * no data is stored to MFIFO.
359 static int pl330_fifo_push(PL330Fifo
*s
, uint8_t *buf
, int len
, uint8_t tag
)
363 if (s
->buf_size
- s
->num
< len
) {
364 return PL330_FIFO_STALL
;
366 for (i
= 0; i
< len
; i
++) {
367 int push_idx
= (s
->head
+ s
->num
+ i
) % s
->buf_size
;
368 s
->buf
[push_idx
] = buf
[i
];
369 s
->tag
[push_idx
] = tag
;
372 return PL330_FIFO_OK
;
375 /* Get LEN bytes of data from MFIFO and store it to BUF. Tag value of each
376 * byte is verified. Zero returned on success, PL330_FIFO_ERR on tag mismatch
377 * and PL330_FIFO_STALL if there is no enough data in MFIFO. If get was
378 * unsuccessful no data is removed from MFIFO.
381 static int pl330_fifo_get(PL330Fifo
*s
, uint8_t *buf
, int len
, uint8_t tag
)
386 return PL330_FIFO_STALL
;
388 for (i
= 0; i
< len
; i
++) {
389 if (s
->tag
[s
->head
] == tag
) {
390 int get_idx
= (s
->head
+ i
) % s
->buf_size
;
391 buf
[i
] = s
->buf
[get_idx
];
392 } else { /* Tag mismatch - Rollback transaction */
393 return PL330_FIFO_ERR
;
396 s
->head
= (s
->head
+ len
) % s
->buf_size
;
398 return PL330_FIFO_OK
;
401 /* Reset MFIFO. This completely erases all data in it. */
403 static inline void pl330_fifo_reset(PL330Fifo
*s
)
409 /* Return tag of the first byte stored in MFIFO. If MFIFO is empty
410 * PL330_UNTAGGED is returned.
413 static inline uint8_t pl330_fifo_tag(PL330Fifo
*s
)
415 return (!s
->num
) ? PL330_UNTAGGED
: s
->tag
[s
->head
];
418 /* Returns non-zero if tag TAG is present in fifo or zero otherwise */
420 static int pl330_fifo_has_tag(PL330Fifo
*s
, uint8_t tag
)
425 for (n
= 0; n
< s
->num
; n
++) {
426 if (s
->tag
[i
] == tag
) {
429 i
= pl330_fifo_inc(s
, i
);
434 /* Remove all entry tagged with TAG from MFIFO */
436 static void pl330_fifo_tagged_remove(PL330Fifo
*s
, uint8_t tag
)
441 for (n
= 0; n
< s
->num
; n
++) {
442 if (s
->tag
[i
] != tag
) {
443 s
->buf
[t
] = s
->buf
[i
];
444 s
->tag
[t
] = s
->tag
[i
];
445 t
= pl330_fifo_inc(s
, t
);
449 i
= pl330_fifo_inc(s
, i
);
453 /* Read-Write Queue implementation
455 * A Read-Write Queue stores up to QUEUE_SIZE instructions (loads or stores).
456 * Each instruction is described by source (for loads) or destination (for
457 * stores) address ADDR, width of data to be loaded/stored LEN, number of
458 * stores/loads to be performed N, INC bit, Z bit and TAG to identify channel
459 * this instruction belongs to. Queue does not store any information about
460 * nature of the instruction: is it load or store. PL330 has different queues
461 * for loads and stores so this is already known at the top level where it
464 * Queue works as FIFO for instructions with equivalent tags, but can issue
465 * instructions with different tags in arbitrary order. SEQN field attached to
466 * each instruction helps to achieve this. For each TAG queue contains
467 * instructions with consecutive SEQN values ranging from LO_SEQN[TAG] to
468 * HI_SEQN[TAG]-1 inclusive. SEQN is 8-bit unsigned integer, so SEQN=255 is
469 * followed by SEQN=0.
471 * Z bit indicates that zeroes should be stored. No MFIFO fetches are performed
475 static void pl330_queue_reset(PL330Queue
*s
)
479 for (i
= 0; i
< s
->queue_size
; i
++) {
480 s
->queue
[i
].tag
= PL330_UNTAGGED
;
484 /* Initialize queue */
485 static void pl330_queue_init(PL330Queue
*s
, int size
, PL330State
*parent
)
488 s
->queue
= g_new0(PL330QueueEntry
, size
);
489 s
->queue_size
= size
;
492 /* Returns pointer to an empty slot or NULL if queue is full */
493 static PL330QueueEntry
*pl330_queue_find_empty(PL330Queue
*s
)
497 for (i
= 0; i
< s
->queue_size
; i
++) {
498 if (s
->queue
[i
].tag
== PL330_UNTAGGED
) {
505 /* Put instruction in queue.
508 * - non-zero - queue is full
511 static int pl330_queue_put_insn(PL330Queue
*s
, uint32_t addr
,
512 int len
, int n
, bool inc
, bool z
, uint8_t tag
)
514 PL330QueueEntry
*entry
= pl330_queue_find_empty(s
);
525 entry
->seqn
= s
->parent
->hi_seqn
[tag
];
526 s
->parent
->hi_seqn
[tag
]++;
530 /* Returns a pointer to queue slot containing instruction which satisfies
531 * following conditions:
532 * - it has valid tag value (not PL330_UNTAGGED)
533 * - if enforce_seq is set it has to be issuable without violating queue
535 * - if TAG argument is not PL330_UNTAGGED this instruction has tag value
536 * equivalent to the argument TAG value.
537 * If such instruction cannot be found NULL is returned.
540 static PL330QueueEntry
*pl330_queue_find_insn(PL330Queue
*s
, uint8_t tag
,
545 for (i
= 0; i
< s
->queue_size
; i
++) {
546 if (s
->queue
[i
].tag
!= PL330_UNTAGGED
) {
548 s
->queue
[i
].seqn
== s
->parent
->lo_seqn
[s
->queue
[i
].tag
]) &&
549 (s
->queue
[i
].tag
== tag
|| tag
== PL330_UNTAGGED
||
558 /* Removes instruction from queue. */
560 static inline void pl330_queue_remove_insn(PL330Queue
*s
, PL330QueueEntry
*e
)
562 s
->parent
->lo_seqn
[e
->tag
]++;
563 e
->tag
= PL330_UNTAGGED
;
566 /* Removes all instructions tagged with TAG from queue. */
568 static inline void pl330_queue_remove_tagged(PL330Queue
*s
, uint8_t tag
)
572 for (i
= 0; i
< s
->queue_size
; i
++) {
573 if (s
->queue
[i
].tag
== tag
) {
574 s
->queue
[i
].tag
= PL330_UNTAGGED
;
579 /* DMA instruction execution engine */
581 /* Moves DMA channel to the FAULT state and updates it's status. */
583 static inline void pl330_fault(PL330Chan
*ch
, uint32_t flags
)
585 DB_PRINT("ch: %p, flags: %" PRIx32
"\n", ch
, flags
);
586 ch
->fault_type
|= flags
;
587 if (ch
->state
== pl330_chan_fault
) {
590 ch
->state
= pl330_chan_fault
;
591 ch
->parent
->num_faulting
++;
592 if (ch
->parent
->num_faulting
== 1) {
593 DB_PRINT("abort interrupt raised\n");
594 qemu_irq_raise(ch
->parent
->irq_abort
);
599 * For information about instructions see PL330 Technical Reference Manual.
602 * CH - channel executing the instruction
604 * ARGS - array of 8-bit arguments
605 * LEN - number of elements in ARGS array
608 static void pl330_dmaadxh(PL330Chan
*ch
, uint8_t *args
, bool ra
, bool neg
)
610 uint32_t im
= (args
[1] << 8) | args
[0];
615 if (ch
->is_manager
) {
616 pl330_fault(ch
, PL330_FAULT_UNDEF_INSTR
);
626 static void pl330_dmaaddh(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
628 pl330_dmaadxh(ch
, args
, extract32(opcode
, 1, 1), false);
631 static void pl330_dmaadnh(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
633 pl330_dmaadxh(ch
, args
, extract32(opcode
, 1, 1), true);
636 static void pl330_dmaend(PL330Chan
*ch
, uint8_t opcode
,
637 uint8_t *args
, int len
)
639 PL330State
*s
= ch
->parent
;
641 if (ch
->state
== pl330_chan_executing
&& !ch
->is_manager
) {
642 /* Wait for all transfers to complete */
643 if (pl330_fifo_has_tag(&s
->fifo
, ch
->tag
) ||
644 pl330_queue_find_insn(&s
->read_queue
, ch
->tag
, false) != NULL
||
645 pl330_queue_find_insn(&s
->write_queue
, ch
->tag
, false) != NULL
) {
651 DB_PRINT("DMA ending!\n");
652 pl330_fifo_tagged_remove(&s
->fifo
, ch
->tag
);
653 pl330_queue_remove_tagged(&s
->read_queue
, ch
->tag
);
654 pl330_queue_remove_tagged(&s
->write_queue
, ch
->tag
);
655 ch
->state
= pl330_chan_stopped
;
658 static void pl330_dmaflushp(PL330Chan
*ch
, uint8_t opcode
,
659 uint8_t *args
, int len
)
664 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
667 periph_id
= (args
[0] >> 3) & 0x1f;
668 if (periph_id
>= ch
->parent
->num_periph_req
) {
669 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
672 if (ch
->ns
&& !(ch
->parent
->cfg
[CFG_PNS
] & (1 << periph_id
))) {
673 pl330_fault(ch
, PL330_FAULT_CH_PERIPH_ERR
);
679 static void pl330_dmago(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
688 if (!ch
->is_manager
) {
689 pl330_fault(ch
, PL330_FAULT_UNDEF_INSTR
);
693 chan_id
= args
[0] & 7;
694 if ((args
[0] >> 3)) {
695 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
698 if (chan_id
>= ch
->parent
->num_chnls
) {
699 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
702 pc
= (((uint32_t)args
[4]) << 24) | (((uint32_t)args
[3]) << 16) |
703 (((uint32_t)args
[2]) << 8) | (((uint32_t)args
[1]));
704 if (ch
->parent
->chan
[chan_id
].state
!= pl330_chan_stopped
) {
705 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
709 pl330_fault(ch
, PL330_FAULT_DMAGO_ERR
);
712 s
= &ch
->parent
->chan
[chan_id
];
715 s
->state
= pl330_chan_executing
;
718 static void pl330_dmald(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
720 uint8_t bs
= opcode
& 3;
725 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
728 if ((bs
== 1 && ch
->request_flag
== PL330_BURST
) ||
729 (bs
== 3 && ch
->request_flag
== PL330_SINGLE
)) {
733 if (bs
== 1 && ch
->request_flag
== PL330_SINGLE
) {
736 num
= ((ch
->control
>> 4) & 0xf) + 1;
738 size
= (uint32_t)1 << ((ch
->control
>> 1) & 0x7);
739 inc
= !!(ch
->control
& 1);
740 ch
->stall
= pl330_queue_put_insn(&ch
->parent
->read_queue
, ch
->src
,
741 size
, num
, inc
, 0, ch
->tag
);
743 DB_PRINT("channel:%" PRId8
" address:%08" PRIx32
" size:%" PRIx32
744 " num:%" PRId32
" %c\n",
745 ch
->tag
, ch
->src
, size
, num
, inc
? 'Y' : 'N');
746 ch
->src
+= inc
? size
* num
- (ch
->src
& (size
- 1)) : 0;
750 static void pl330_dmaldp(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
755 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
758 periph_id
= (args
[0] >> 3) & 0x1f;
759 if (periph_id
>= ch
->parent
->num_periph_req
) {
760 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
763 if (ch
->ns
&& !(ch
->parent
->cfg
[CFG_PNS
] & (1 << periph_id
))) {
764 pl330_fault(ch
, PL330_FAULT_CH_PERIPH_ERR
);
767 pl330_dmald(ch
, opcode
, args
, len
);
770 static void pl330_dmalp(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
772 uint8_t lc
= (opcode
& 2) >> 1;
774 ch
->lc
[lc
] = args
[0];
777 static void pl330_dmakill(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
779 if (ch
->state
== pl330_chan_fault
||
780 ch
->state
== pl330_chan_fault_completing
) {
781 /* This is the only way for a channel to leave the faulting state */
783 ch
->parent
->num_faulting
--;
784 if (ch
->parent
->num_faulting
== 0) {
785 DB_PRINT("abort interrupt lowered\n");
786 qemu_irq_lower(ch
->parent
->irq_abort
);
789 ch
->state
= pl330_chan_killing
;
790 pl330_fifo_tagged_remove(&ch
->parent
->fifo
, ch
->tag
);
791 pl330_queue_remove_tagged(&ch
->parent
->read_queue
, ch
->tag
);
792 pl330_queue_remove_tagged(&ch
->parent
->write_queue
, ch
->tag
);
793 ch
->state
= pl330_chan_stopped
;
796 static void pl330_dmalpend(PL330Chan
*ch
, uint8_t opcode
,
797 uint8_t *args
, int len
)
799 uint8_t nf
= (opcode
& 0x10) >> 4;
800 uint8_t bs
= opcode
& 3;
801 uint8_t lc
= (opcode
& 4) >> 2;
804 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
807 if ((bs
== 1 && ch
->request_flag
== PL330_BURST
) ||
808 (bs
== 3 && ch
->request_flag
== PL330_SINGLE
)) {
812 if (!nf
|| ch
->lc
[lc
]) {
816 DB_PRINT("loop reiteration\n");
819 /* "ch->pc -= args[0] + len + 1" is incorrect when args[0] == 256 */
821 DB_PRINT("loop fallthrough\n");
826 static void pl330_dmamov(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
828 uint8_t rd
= args
[0] & 7;
831 if ((args
[0] >> 3)) {
832 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
835 im
= (((uint32_t)args
[4]) << 24) | (((uint32_t)args
[3]) << 16) |
836 (((uint32_t)args
[2]) << 8) | (((uint32_t)args
[1]));
848 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
853 static void pl330_dmanop(PL330Chan
*ch
, uint8_t opcode
,
854 uint8_t *args
, int len
)
859 static void pl330_dmarmb(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
861 if (pl330_queue_find_insn(&ch
->parent
->read_queue
, ch
->tag
, false)) {
862 ch
->state
= pl330_chan_at_barrier
;
866 ch
->state
= pl330_chan_executing
;
870 static void pl330_dmasev(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
875 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
878 ev_id
= (args
[0] >> 3) & 0x1f;
879 if (ev_id
>= ch
->parent
->num_events
) {
880 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
883 if (ch
->ns
&& !(ch
->parent
->cfg
[CFG_INS
] & (1 << ev_id
))) {
884 pl330_fault(ch
, PL330_FAULT_EVENT_ERR
);
887 if (ch
->parent
->inten
& (1 << ev_id
)) {
888 ch
->parent
->int_status
|= (1 << ev_id
);
889 DB_PRINT("event interrupt raised %" PRId8
"\n", ev_id
);
890 qemu_irq_raise(ch
->parent
->irq
[ev_id
]);
892 DB_PRINT("event raised %" PRId8
"\n", ev_id
);
893 ch
->parent
->ev_status
|= (1 << ev_id
);
896 static void pl330_dmast(PL330Chan
*ch
, uint8_t opcode
, uint8_t *args
, int len
)
898 uint8_t bs
= opcode
& 3;
903 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
906 if ((bs
== 1 && ch
->request_flag
== PL330_BURST
) ||
907 (bs
== 3 && ch
->request_flag
== PL330_SINGLE
)) {
911 num
= ((ch
->control
>> 18) & 0xf) + 1;
912 size
= (uint32_t)1 << ((ch
->control
>> 15) & 0x7);
913 inc
= !!((ch
->control
>> 14) & 1);
914 ch
->stall
= pl330_queue_put_insn(&ch
->parent
->write_queue
, ch
->dst
,
915 size
, num
, inc
, 0, ch
->tag
);
917 DB_PRINT("channel:%" PRId8
" address:%08" PRIx32
" size:%" PRIx32
918 " num:%" PRId32
" %c\n",
919 ch
->tag
, ch
->dst
, size
, num
, inc
? 'Y' : 'N');
920 ch
->dst
+= inc
? size
* num
- (ch
->dst
& (size
- 1)) : 0;
924 static void pl330_dmastp(PL330Chan
*ch
, uint8_t opcode
,
925 uint8_t *args
, int len
)
930 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
933 periph_id
= (args
[0] >> 3) & 0x1f;
934 if (periph_id
>= ch
->parent
->num_periph_req
) {
935 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
938 if (ch
->ns
&& !(ch
->parent
->cfg
[CFG_PNS
] & (1 << periph_id
))) {
939 pl330_fault(ch
, PL330_FAULT_CH_PERIPH_ERR
);
942 pl330_dmast(ch
, opcode
, args
, len
);
945 static void pl330_dmastz(PL330Chan
*ch
, uint8_t opcode
,
946 uint8_t *args
, int len
)
951 num
= ((ch
->control
>> 18) & 0xf) + 1;
952 size
= (uint32_t)1 << ((ch
->control
>> 15) & 0x7);
953 inc
= !!((ch
->control
>> 14) & 1);
954 ch
->stall
= pl330_queue_put_insn(&ch
->parent
->write_queue
, ch
->dst
,
955 size
, num
, inc
, 1, ch
->tag
);
957 ch
->dst
+= size
* num
;
961 static void pl330_dmawfe(PL330Chan
*ch
, uint8_t opcode
,
962 uint8_t *args
, int len
)
968 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
971 ev_id
= (args
[0] >> 3) & 0x1f;
972 if (ev_id
>= ch
->parent
->num_events
) {
973 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
976 if (ch
->ns
&& !(ch
->parent
->cfg
[CFG_INS
] & (1 << ev_id
))) {
977 pl330_fault(ch
, PL330_FAULT_EVENT_ERR
);
981 ch
->state
= pl330_chan_waiting_event
;
982 if (~ch
->parent
->inten
& ch
->parent
->ev_status
& 1 << ev_id
) {
983 ch
->state
= pl330_chan_executing
;
984 /* If anyone else is currently waiting on the same event, let them
985 * clear the ev_status so they pick up event as well
987 for (i
= 0; i
< ch
->parent
->num_chnls
; ++i
) {
988 PL330Chan
*peer
= &ch
->parent
->chan
[i
];
989 if (peer
->state
== pl330_chan_waiting_event
&&
990 peer
->wakeup
== ev_id
) {
994 ch
->parent
->ev_status
&= ~(1 << ev_id
);
995 DB_PRINT("event lowered %" PRIx8
"\n", ev_id
);
1001 static void pl330_dmawfp(PL330Chan
*ch
, uint8_t opcode
,
1002 uint8_t *args
, int len
)
1004 uint8_t bs
= opcode
& 3;
1008 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
1011 periph_id
= (args
[0] >> 3) & 0x1f;
1012 if (periph_id
>= ch
->parent
->num_periph_req
) {
1013 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
1016 if (ch
->ns
&& !(ch
->parent
->cfg
[CFG_PNS
] & (1 << periph_id
))) {
1017 pl330_fault(ch
, PL330_FAULT_CH_PERIPH_ERR
);
1022 ch
->request_flag
= PL330_SINGLE
;
1026 ch
->request_flag
= PL330_BURST
;
1030 ch
->request_flag
= PL330_BURST
;
1034 pl330_fault(ch
, PL330_FAULT_OPERAND_INVALID
);
1038 if (ch
->parent
->periph_busy
[periph_id
]) {
1039 ch
->state
= pl330_chan_waiting_periph
;
1041 } else if (ch
->state
== pl330_chan_waiting_periph
) {
1042 ch
->state
= pl330_chan_executing
;
1046 static void pl330_dmawmb(PL330Chan
*ch
, uint8_t opcode
,
1047 uint8_t *args
, int len
)
1049 if (pl330_queue_find_insn(&ch
->parent
->write_queue
, ch
->tag
, false)) {
1050 ch
->state
= pl330_chan_at_barrier
;
1054 ch
->state
= pl330_chan_executing
;
1058 /* NULL terminated array of the instruction descriptions. */
1059 static const PL330InsnDesc insn_desc
[] = {
1060 { .opcode
= 0x54, .opmask
= 0xFD, .size
= 3, .exec
= pl330_dmaaddh
, },
1061 { .opcode
= 0x5c, .opmask
= 0xFD, .size
= 3, .exec
= pl330_dmaadnh
, },
1062 { .opcode
= 0x00, .opmask
= 0xFF, .size
= 1, .exec
= pl330_dmaend
, },
1063 { .opcode
= 0x35, .opmask
= 0xFF, .size
= 2, .exec
= pl330_dmaflushp
, },
1064 { .opcode
= 0xA0, .opmask
= 0xFD, .size
= 6, .exec
= pl330_dmago
, },
1065 { .opcode
= 0x04, .opmask
= 0xFC, .size
= 1, .exec
= pl330_dmald
, },
1066 { .opcode
= 0x25, .opmask
= 0xFD, .size
= 2, .exec
= pl330_dmaldp
, },
1067 { .opcode
= 0x20, .opmask
= 0xFD, .size
= 2, .exec
= pl330_dmalp
, },
1068 /* dmastp must be before dmalpend in this list, because their maps
1071 { .opcode
= 0x29, .opmask
= 0xFD, .size
= 2, .exec
= pl330_dmastp
, },
1072 { .opcode
= 0x28, .opmask
= 0xE8, .size
= 2, .exec
= pl330_dmalpend
, },
1073 { .opcode
= 0x01, .opmask
= 0xFF, .size
= 1, .exec
= pl330_dmakill
, },
1074 { .opcode
= 0xBC, .opmask
= 0xFF, .size
= 6, .exec
= pl330_dmamov
, },
1075 { .opcode
= 0x18, .opmask
= 0xFF, .size
= 1, .exec
= pl330_dmanop
, },
1076 { .opcode
= 0x12, .opmask
= 0xFF, .size
= 1, .exec
= pl330_dmarmb
, },
1077 { .opcode
= 0x34, .opmask
= 0xFF, .size
= 2, .exec
= pl330_dmasev
, },
1078 { .opcode
= 0x08, .opmask
= 0xFC, .size
= 1, .exec
= pl330_dmast
, },
1079 { .opcode
= 0x0C, .opmask
= 0xFF, .size
= 1, .exec
= pl330_dmastz
, },
1080 { .opcode
= 0x36, .opmask
= 0xFF, .size
= 2, .exec
= pl330_dmawfe
, },
1081 { .opcode
= 0x30, .opmask
= 0xFC, .size
= 2, .exec
= pl330_dmawfp
, },
1082 { .opcode
= 0x13, .opmask
= 0xFF, .size
= 1, .exec
= pl330_dmawmb
, },
1083 { .opcode
= 0x00, .opmask
= 0x00, .size
= 0, .exec
= NULL
, }
1086 /* Instructions which can be issued via debug registers. */
1087 static const PL330InsnDesc debug_insn_desc
[] = {
1088 { .opcode
= 0xA0, .opmask
= 0xFD, .size
= 6, .exec
= pl330_dmago
, },
1089 { .opcode
= 0x01, .opmask
= 0xFF, .size
= 1, .exec
= pl330_dmakill
, },
1090 { .opcode
= 0x34, .opmask
= 0xFF, .size
= 2, .exec
= pl330_dmasev
, },
1091 { .opcode
= 0x00, .opmask
= 0x00, .size
= 0, .exec
= NULL
, }
1094 static inline const PL330InsnDesc
*pl330_fetch_insn(PL330Chan
*ch
)
1099 dma_memory_read(&address_space_memory
, ch
->pc
, &opcode
, 1);
1100 for (i
= 0; insn_desc
[i
].size
; i
++) {
1101 if ((opcode
& insn_desc
[i
].opmask
) == insn_desc
[i
].opcode
) {
1102 return &insn_desc
[i
];
1108 static inline void pl330_exec_insn(PL330Chan
*ch
, const PL330InsnDesc
*insn
)
1110 uint8_t buf
[PL330_INSN_MAXSIZE
];
1112 assert(insn
->size
<= PL330_INSN_MAXSIZE
);
1113 dma_memory_read(&address_space_memory
, ch
->pc
, buf
, insn
->size
);
1114 insn
->exec(ch
, buf
[0], &buf
[1], insn
->size
- 1);
1117 static inline void pl330_update_pc(PL330Chan
*ch
,
1118 const PL330InsnDesc
*insn
)
1120 ch
->pc
+= insn
->size
;
1123 /* Try to execute current instruction in channel CH. Number of executed
1124 instructions is returned (0 or 1). */
1125 static int pl330_chan_exec(PL330Chan
*ch
)
1127 const PL330InsnDesc
*insn
;
1129 if (ch
->state
!= pl330_chan_executing
&&
1130 ch
->state
!= pl330_chan_waiting_periph
&&
1131 ch
->state
!= pl330_chan_at_barrier
&&
1132 ch
->state
!= pl330_chan_waiting_event
) {
1136 insn
= pl330_fetch_insn(ch
);
1138 DB_PRINT("pl330 undefined instruction\n");
1139 pl330_fault(ch
, PL330_FAULT_UNDEF_INSTR
);
1142 pl330_exec_insn(ch
, insn
);
1144 pl330_update_pc(ch
, insn
);
1145 ch
->watchdog_timer
= 0;
1147 /* WDT only active in exec state */
1148 } else if (ch
->state
== pl330_chan_executing
) {
1149 ch
->watchdog_timer
++;
1150 if (ch
->watchdog_timer
>= PL330_WATCHDOG_LIMIT
) {
1151 pl330_fault(ch
, PL330_FAULT_LOCKUP_ERR
);
1157 /* Try to execute 1 instruction in each channel, one instruction from read
1158 queue and one instruction from write queue. Number of successfully executed
1159 instructions is returned. */
1160 static int pl330_exec_cycle(PL330Chan
*channel
)
1162 PL330State
*s
= channel
->parent
;
1167 uint8_t buf
[PL330_MAX_BURST_LEN
];
1169 /* Execute one instruction in each channel */
1170 num_exec
+= pl330_chan_exec(channel
);
1172 /* Execute one instruction from read queue */
1173 q
= pl330_queue_find_insn(&s
->read_queue
, PL330_UNTAGGED
, true);
1174 if (q
!= NULL
&& q
->len
<= pl330_fifo_num_free(&s
->fifo
)) {
1175 int len
= q
->len
- (q
->addr
& (q
->len
- 1));
1177 dma_memory_read(&address_space_memory
, q
->addr
, buf
, len
);
1178 if (PL330_ERR_DEBUG
> 1) {
1179 DB_PRINT("PL330 read from memory @%08" PRIx32
" (size = %08x):\n",
1181 qemu_hexdump((char *)buf
, stderr
, "", len
);
1183 fifo_res
= pl330_fifo_push(&s
->fifo
, buf
, len
, q
->tag
);
1184 if (fifo_res
== PL330_FIFO_OK
) {
1190 pl330_queue_remove_insn(&s
->read_queue
, q
);
1196 /* Execute one instruction from write queue. */
1197 q
= pl330_queue_find_insn(&s
->write_queue
, pl330_fifo_tag(&s
->fifo
), true);
1199 int len
= q
->len
- (q
->addr
& (q
->len
- 1));
1202 for (i
= 0; i
< len
; i
++) {
1206 fifo_res
= pl330_fifo_get(&s
->fifo
, buf
, len
, q
->tag
);
1208 if (fifo_res
== PL330_FIFO_OK
|| q
->z
) {
1209 dma_memory_write(&address_space_memory
, q
->addr
, buf
, len
);
1210 if (PL330_ERR_DEBUG
> 1) {
1211 DB_PRINT("PL330 read from memory @%08" PRIx32
1212 " (size = %08x):\n", q
->addr
, len
);
1213 qemu_hexdump((char *)buf
, stderr
, "", len
);
1219 } else if (fifo_res
== PL330_FIFO_STALL
) {
1220 pl330_fault(&channel
->parent
->chan
[q
->tag
],
1221 PL330_FAULT_FIFOEMPTY_ERR
);
1225 pl330_queue_remove_insn(&s
->write_queue
, q
);
1232 static int pl330_exec_channel(PL330Chan
*channel
)
1236 /* TODO: Is it all right to execute everything or should we do per-cycle
1238 while (pl330_exec_cycle(channel
)) {
1242 /* Detect deadlock */
1243 if (channel
->state
== pl330_chan_executing
) {
1244 pl330_fault(channel
, PL330_FAULT_LOCKUP_ERR
);
1246 /* Situation when one of the queues has deadlocked but all channels
1247 * have finished their programs should be impossible.
1253 static inline void pl330_exec(PL330State
*s
)
1258 insr_exec
= pl330_exec_channel(&s
->manager
);
1260 for (i
= 0; i
< s
->num_chnls
; i
++) {
1261 insr_exec
+= pl330_exec_channel(&s
->chan
[i
]);
1263 } while (insr_exec
);
1266 static void pl330_exec_cycle_timer(void *opaque
)
1268 PL330State
*s
= (PL330State
*)opaque
;
1272 /* Stop or restore dma operations */
1274 static void pl330_dma_stop_irq(void *opaque
, int irq
, int level
)
1276 PL330State
*s
= (PL330State
*)opaque
;
1278 if (s
->periph_busy
[irq
] != level
) {
1279 s
->periph_busy
[irq
] = level
;
1280 timer_mod(s
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
1284 static void pl330_debug_exec(PL330State
*s
)
1291 const PL330InsnDesc
*insn
;
1293 s
->debug_status
= 1;
1294 chan_id
= (s
->dbg
[0] >> 8) & 0x07;
1295 opcode
= (s
->dbg
[0] >> 16) & 0xff;
1296 args
[0] = (s
->dbg
[0] >> 24) & 0xff;
1297 args
[1] = (s
->dbg
[1] >> 0) & 0xff;
1298 args
[2] = (s
->dbg
[1] >> 8) & 0xff;
1299 args
[3] = (s
->dbg
[1] >> 16) & 0xff;
1300 args
[4] = (s
->dbg
[1] >> 24) & 0xff;
1301 DB_PRINT("chan id: %" PRIx8
"\n", chan_id
);
1302 if (s
->dbg
[0] & 1) {
1303 ch
= &s
->chan
[chan_id
];
1308 for (i
= 0; debug_insn_desc
[i
].size
; i
++) {
1309 if ((opcode
& debug_insn_desc
[i
].opmask
) == debug_insn_desc
[i
].opcode
) {
1310 insn
= &debug_insn_desc
[i
];
1314 pl330_fault(ch
, PL330_FAULT_UNDEF_INSTR
| PL330_FAULT_DBG_INSTR
);
1318 insn
->exec(ch
, opcode
, args
, insn
->size
- 1);
1319 if (ch
->fault_type
) {
1320 ch
->fault_type
|= PL330_FAULT_DBG_INSTR
;
1323 qemu_log_mask(LOG_UNIMP
, "pl330: stall of debug instruction not "
1326 s
->debug_status
= 0;
1329 /* IOMEM mapped registers */
1331 static void pl330_iomem_write(void *opaque
, hwaddr offset
,
1332 uint64_t value
, unsigned size
)
1334 PL330State
*s
= (PL330State
*) opaque
;
1337 DB_PRINT("addr: %08x data: %08x\n", (unsigned)offset
, (unsigned)value
);
1340 case PL330_REG_INTEN
:
1343 case PL330_REG_INTCLR
:
1344 for (i
= 0; i
< s
->num_events
; i
++) {
1345 if (s
->int_status
& s
->inten
& value
& (1 << i
)) {
1346 DB_PRINT("event interrupt lowered %d\n", i
);
1347 qemu_irq_lower(s
->irq
[i
]);
1350 s
->ev_status
&= ~(value
& s
->inten
);
1351 s
->int_status
&= ~(value
& s
->inten
);
1353 case PL330_REG_DBGCMD
:
1354 if ((value
& 3) == 0) {
1355 pl330_debug_exec(s
);
1358 qemu_log_mask(LOG_GUEST_ERROR
, "pl330: write of illegal value %u "
1359 "for offset " TARGET_FMT_plx
"\n", (unsigned)value
,
1363 case PL330_REG_DBGINST0
:
1364 DB_PRINT("s->dbg[0] = %08x\n", (unsigned)value
);
1367 case PL330_REG_DBGINST1
:
1368 DB_PRINT("s->dbg[1] = %08x\n", (unsigned)value
);
1372 qemu_log_mask(LOG_GUEST_ERROR
, "pl330: bad write offset " TARGET_FMT_plx
1378 static inline uint32_t pl330_iomem_read_imp(void *opaque
,
1381 PL330State
*s
= (PL330State
*)opaque
;
1386 if (offset
>= PL330_REG_PERIPH_ID
&& offset
< PL330_REG_PERIPH_ID
+ 32) {
1387 return pl330_id
[(offset
- PL330_REG_PERIPH_ID
) >> 2];
1389 if (offset
>= PL330_REG_CR0_BASE
&& offset
< PL330_REG_CR0_BASE
+ 24) {
1390 return s
->cfg
[(offset
- PL330_REG_CR0_BASE
) >> 2];
1392 if (offset
>= PL330_REG_CHANCTRL
&& offset
< PL330_REG_DBGSTATUS
) {
1393 offset
-= PL330_REG_CHANCTRL
;
1394 chan_id
= offset
>> 5;
1395 if (chan_id
>= s
->num_chnls
) {
1396 qemu_log_mask(LOG_GUEST_ERROR
, "pl330: bad read offset "
1397 TARGET_FMT_plx
"\n", offset
);
1400 switch (offset
& 0x1f) {
1402 return s
->chan
[chan_id
].src
;
1404 return s
->chan
[chan_id
].dst
;
1406 return s
->chan
[chan_id
].control
;
1408 return s
->chan
[chan_id
].lc
[0];
1410 return s
->chan
[chan_id
].lc
[1];
1412 qemu_log_mask(LOG_GUEST_ERROR
, "pl330: bad read offset "
1413 TARGET_FMT_plx
"\n", offset
);
1417 if (offset
>= PL330_REG_CSR_BASE
&& offset
< 0x400) {
1418 offset
-= PL330_REG_CSR_BASE
;
1419 chan_id
= offset
>> 3;
1420 if (chan_id
>= s
->num_chnls
) {
1421 qemu_log_mask(LOG_GUEST_ERROR
, "pl330: bad read offset "
1422 TARGET_FMT_plx
"\n", offset
);
1425 switch ((offset
>> 2) & 1) {
1427 res
= (s
->chan
[chan_id
].ns
<< 21) |
1428 (s
->chan
[chan_id
].wakeup
<< 4) |
1429 (s
->chan
[chan_id
].state
) |
1430 (s
->chan
[chan_id
].wfp_sbp
<< 14);
1433 return s
->chan
[chan_id
].pc
;
1435 qemu_log_mask(LOG_GUEST_ERROR
, "pl330: read error\n");
1439 if (offset
>= PL330_REG_FTR_BASE
&& offset
< 0x100) {
1440 offset
-= PL330_REG_FTR_BASE
;
1441 chan_id
= offset
>> 2;
1442 if (chan_id
>= s
->num_chnls
) {
1443 qemu_log_mask(LOG_GUEST_ERROR
, "pl330: bad read offset "
1444 TARGET_FMT_plx
"\n", offset
);
1447 return s
->chan
[chan_id
].fault_type
;
1451 return (s
->manager
.ns
<< 9) | (s
->manager
.wakeup
<< 4) |
1452 (s
->manager
.state
& 0xf);
1454 return s
->manager
.pc
;
1455 case PL330_REG_INTEN
:
1457 case PL330_REG_INT_EVENT_RIS
:
1458 return s
->ev_status
;
1459 case PL330_REG_INTMIS
:
1460 return s
->int_status
;
1461 case PL330_REG_INTCLR
:
1462 /* Documentation says that we can't read this register
1463 * but linux kernel does it
1466 case PL330_REG_FSRD
:
1467 return s
->manager
.state
? 1 : 0;
1468 case PL330_REG_FSRC
:
1470 for (i
= 0; i
< s
->num_chnls
; i
++) {
1471 if (s
->chan
[i
].state
== pl330_chan_fault
||
1472 s
->chan
[i
].state
== pl330_chan_fault_completing
) {
1477 case PL330_REG_FTRD
:
1478 return s
->manager
.fault_type
;
1479 case PL330_REG_DBGSTATUS
:
1480 return s
->debug_status
;
1482 qemu_log_mask(LOG_GUEST_ERROR
, "pl330: bad read offset "
1483 TARGET_FMT_plx
"\n", offset
);
1488 static uint64_t pl330_iomem_read(void *opaque
, hwaddr offset
,
1491 uint32_t ret
= pl330_iomem_read_imp(opaque
, offset
);
1492 DB_PRINT("addr: %08" HWADDR_PRIx
" data: %08" PRIx32
"\n", offset
, ret
);
1496 static const MemoryRegionOps pl330_ops
= {
1497 .read
= pl330_iomem_read
,
1498 .write
= pl330_iomem_write
,
1499 .endianness
= DEVICE_NATIVE_ENDIAN
,
1501 .min_access_size
= 4,
1502 .max_access_size
= 4,
1506 /* Controller logic and initialization */
1508 static void pl330_chan_reset(PL330Chan
*ch
)
1513 ch
->state
= pl330_chan_stopped
;
1514 ch
->watchdog_timer
= 0;
1521 static void pl330_reset(DeviceState
*d
)
1524 PL330State
*s
= PL330(d
);
1529 s
->debug_status
= 0;
1530 s
->num_faulting
= 0;
1531 s
->manager
.ns
= s
->mgr_ns_at_rst
;
1532 pl330_fifo_reset(&s
->fifo
);
1533 pl330_queue_reset(&s
->read_queue
);
1534 pl330_queue_reset(&s
->write_queue
);
1536 for (i
= 0; i
< s
->num_chnls
; i
++) {
1537 pl330_chan_reset(&s
->chan
[i
]);
1539 for (i
= 0; i
< s
->num_periph_req
; i
++) {
1540 s
->periph_busy
[i
] = 0;
1543 timer_del(s
->timer
);
1546 static void pl330_realize(DeviceState
*dev
, Error
**errp
)
1549 PL330State
*s
= PL330(dev
);
1551 sysbus_init_irq(SYS_BUS_DEVICE(dev
), &s
->irq_abort
);
1552 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl330_ops
, s
,
1553 "dma", PL330_IOMEM_SIZE
);
1554 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->iomem
);
1556 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, pl330_exec_cycle_timer
, s
);
1558 s
->cfg
[0] = (s
->mgr_ns_at_rst
? 0x4 : 0) |
1559 (s
->num_periph_req
> 0 ? 1 : 0) |
1560 ((s
->num_chnls
- 1) & 0x7) << 4 |
1561 ((s
->num_periph_req
- 1) & 0x1f) << 12 |
1562 ((s
->num_events
- 1) & 0x1f) << 17;
1564 switch (s
->i_cache_len
) {
1578 error_setg(errp
, "Bad value for i-cache_len property: %" PRIx8
,
1582 s
->cfg
[1] |= ((s
->num_i_cache_lines
- 1) & 0xf) << 4;
1584 s
->chan
= g_new0(PL330Chan
, s
->num_chnls
);
1585 s
->hi_seqn
= g_new0(uint8_t, s
->num_chnls
);
1586 s
->lo_seqn
= g_new0(uint8_t, s
->num_chnls
);
1587 for (i
= 0; i
< s
->num_chnls
; i
++) {
1588 s
->chan
[i
].parent
= s
;
1589 s
->chan
[i
].tag
= (uint8_t)i
;
1591 s
->manager
.parent
= s
;
1592 s
->manager
.tag
= s
->num_chnls
;
1593 s
->manager
.is_manager
= true;
1595 s
->irq
= g_new0(qemu_irq
, s
->num_events
);
1596 for (i
= 0; i
< s
->num_events
; i
++) {
1597 sysbus_init_irq(SYS_BUS_DEVICE(dev
), &s
->irq
[i
]);
1600 qdev_init_gpio_in(dev
, pl330_dma_stop_irq
, PL330_PERIPH_NUM
);
1602 switch (s
->data_width
) {
1604 s
->cfg
[CFG_CRD
] |= 0x2;
1607 s
->cfg
[CFG_CRD
] |= 0x3;
1610 s
->cfg
[CFG_CRD
] |= 0x4;
1613 error_setg(errp
, "Bad value for data_width property: %" PRIx8
,
1618 s
->cfg
[CFG_CRD
] |= ((s
->wr_cap
- 1) & 0x7) << 4 |
1619 ((s
->wr_q_dep
- 1) & 0xf) << 8 |
1620 ((s
->rd_cap
- 1) & 0x7) << 12 |
1621 ((s
->rd_q_dep
- 1) & 0xf) << 16 |
1622 ((s
->data_buffer_dep
- 1) & 0x1ff) << 20;
1624 pl330_queue_init(&s
->read_queue
, s
->rd_q_dep
, s
);
1625 pl330_queue_init(&s
->write_queue
, s
->wr_q_dep
, s
);
1626 pl330_fifo_init(&s
->fifo
, s
->data_width
/ 4 * s
->data_buffer_dep
);
1629 static Property pl330_properties
[] = {
1631 DEFINE_PROP_UINT32("num_chnls", PL330State
, num_chnls
, 8),
1632 DEFINE_PROP_UINT8("num_periph_req", PL330State
, num_periph_req
, 4),
1633 DEFINE_PROP_UINT8("num_events", PL330State
, num_events
, 16),
1634 DEFINE_PROP_UINT8("mgr_ns_at_rst", PL330State
, mgr_ns_at_rst
, 0),
1636 DEFINE_PROP_UINT8("i-cache_len", PL330State
, i_cache_len
, 4),
1637 DEFINE_PROP_UINT8("num_i-cache_lines", PL330State
, num_i_cache_lines
, 8),
1639 DEFINE_PROP_UINT32("boot_addr", PL330State
, cfg
[CFG_BOOT_ADDR
], 0),
1640 DEFINE_PROP_UINT32("INS", PL330State
, cfg
[CFG_INS
], 0),
1641 DEFINE_PROP_UINT32("PNS", PL330State
, cfg
[CFG_PNS
], 0),
1643 DEFINE_PROP_UINT8("data_width", PL330State
, data_width
, 64),
1644 DEFINE_PROP_UINT8("wr_cap", PL330State
, wr_cap
, 8),
1645 DEFINE_PROP_UINT8("wr_q_dep", PL330State
, wr_q_dep
, 16),
1646 DEFINE_PROP_UINT8("rd_cap", PL330State
, rd_cap
, 8),
1647 DEFINE_PROP_UINT8("rd_q_dep", PL330State
, rd_q_dep
, 16),
1648 DEFINE_PROP_UINT16("data_buffer_dep", PL330State
, data_buffer_dep
, 256),
1650 DEFINE_PROP_END_OF_LIST(),
1653 static void pl330_class_init(ObjectClass
*klass
, void *data
)
1655 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1657 dc
->realize
= pl330_realize
;
1658 dc
->reset
= pl330_reset
;
1659 dc
->props
= pl330_properties
;
1660 dc
->vmsd
= &vmstate_pl330
;
1663 static const TypeInfo pl330_type_info
= {
1665 .parent
= TYPE_SYS_BUS_DEVICE
,
1666 .instance_size
= sizeof(PL330State
),
1667 .class_init
= pl330_class_init
,
1670 static void pl330_register_types(void)
1672 type_register_static(&pl330_type_info
);
1675 type_init(pl330_register_types
)