2 * QEMU model of the ZynqMP generic DMA
4 * Copyright (c) 2014 Xilinx Inc.
5 * Copyright (c) 2018 FEIMTECH AB
7 * Written by Edgar E. Iglesias <edgar.iglesias@xilinx.com>,
8 * Francisco Iglesias <francisco.iglesias@feimtech.se>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "qemu/osdep.h"
30 #include "hw/dma/xlnx-zdma.h"
31 #include "qemu/bitops.h"
33 #include "qapi/error.h"
35 #ifndef XLNX_ZDMA_ERR_DEBUG
36 #define XLNX_ZDMA_ERR_DEBUG 0
39 REG32(ZDMA_ERR_CTRL
, 0x0)
40 FIELD(ZDMA_ERR_CTRL
, APB_ERR_RES
, 0, 1)
41 REG32(ZDMA_CH_ISR
, 0x100)
42 FIELD(ZDMA_CH_ISR
, DMA_PAUSE
, 11, 1)
43 FIELD(ZDMA_CH_ISR
, DMA_DONE
, 10, 1)
44 FIELD(ZDMA_CH_ISR
, AXI_WR_DATA
, 9, 1)
45 FIELD(ZDMA_CH_ISR
, AXI_RD_DATA
, 8, 1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_CTRL0
, MODE
, 4, 2)
97 FIELD(ZDMA_CH_CTRL0
, RATE_CTRL
, 3, 1)
98 FIELD(ZDMA_CH_CTRL0
, CONT_ADDR
, 2, 1)
99 FIELD(ZDMA_CH_CTRL0
, CONT
, 1, 1)
100 REG32(ZDMA_CH_CTRL1
, 0x114)
101 FIELD(ZDMA_CH_CTRL1
, DST_ISSUE
, 5, 5)
102 FIELD(ZDMA_CH_CTRL1
, SRC_ISSUE
, 0, 5)
103 REG32(ZDMA_CH_FCI
, 0x118)
104 FIELD(ZDMA_CH_FCI
, PROG_CELL_CNT
, 2, 2)
105 FIELD(ZDMA_CH_FCI
, SIDE
, 1, 1)
106 FIELD(ZDMA_CH_FCI
, EN
, 0, 1)
107 REG32(ZDMA_CH_STATUS
, 0x11c)
108 FIELD(ZDMA_CH_STATUS
, STATE
, 0, 2)
109 REG32(ZDMA_CH_DATA_ATTR
, 0x120)
110 FIELD(ZDMA_CH_DATA_ATTR
, ARBURST
, 26, 2)
111 FIELD(ZDMA_CH_DATA_ATTR
, ARCACHE
, 22, 4)
112 FIELD(ZDMA_CH_DATA_ATTR
, ARQOS
, 18, 4)
113 FIELD(ZDMA_CH_DATA_ATTR
, ARLEN
, 14, 4)
114 FIELD(ZDMA_CH_DATA_ATTR
, AWBURST
, 12, 2)
115 FIELD(ZDMA_CH_DATA_ATTR
, AWCACHE
, 8, 4)
116 FIELD(ZDMA_CH_DATA_ATTR
, AWQOS
, 4, 4)
117 FIELD(ZDMA_CH_DATA_ATTR
, AWLEN
, 0, 4)
118 REG32(ZDMA_CH_DSCR_ATTR
, 0x124)
119 FIELD(ZDMA_CH_DSCR_ATTR
, AXCOHRNT
, 8, 1)
120 FIELD(ZDMA_CH_DSCR_ATTR
, AXCACHE
, 4, 4)
121 FIELD(ZDMA_CH_DSCR_ATTR
, AXQOS
, 0, 4)
122 REG32(ZDMA_CH_SRC_DSCR_WORD0
, 0x128)
123 REG32(ZDMA_CH_SRC_DSCR_WORD1
, 0x12c)
124 FIELD(ZDMA_CH_SRC_DSCR_WORD1
, MSB
, 0, 17)
125 REG32(ZDMA_CH_SRC_DSCR_WORD2
, 0x130)
126 FIELD(ZDMA_CH_SRC_DSCR_WORD2
, SIZE
, 0, 30)
127 REG32(ZDMA_CH_SRC_DSCR_WORD3
, 0x134)
128 FIELD(ZDMA_CH_SRC_DSCR_WORD3
, CMD
, 3, 2)
129 FIELD(ZDMA_CH_SRC_DSCR_WORD3
, INTR
, 2, 1)
130 FIELD(ZDMA_CH_SRC_DSCR_WORD3
, TYPE
, 1, 1)
131 FIELD(ZDMA_CH_SRC_DSCR_WORD3
, COHRNT
, 0, 1)
132 REG32(ZDMA_CH_DST_DSCR_WORD0
, 0x138)
133 REG32(ZDMA_CH_DST_DSCR_WORD1
, 0x13c)
134 FIELD(ZDMA_CH_DST_DSCR_WORD1
, MSB
, 0, 17)
135 REG32(ZDMA_CH_DST_DSCR_WORD2
, 0x140)
136 FIELD(ZDMA_CH_DST_DSCR_WORD2
, SIZE
, 0, 30)
137 REG32(ZDMA_CH_DST_DSCR_WORD3
, 0x144)
138 FIELD(ZDMA_CH_DST_DSCR_WORD3
, INTR
, 2, 1)
139 FIELD(ZDMA_CH_DST_DSCR_WORD3
, TYPE
, 1, 1)
140 FIELD(ZDMA_CH_DST_DSCR_WORD3
, COHRNT
, 0, 1)
141 REG32(ZDMA_CH_WR_ONLY_WORD0
, 0x148)
142 REG32(ZDMA_CH_WR_ONLY_WORD1
, 0x14c)
143 REG32(ZDMA_CH_WR_ONLY_WORD2
, 0x150)
144 REG32(ZDMA_CH_WR_ONLY_WORD3
, 0x154)
145 REG32(ZDMA_CH_SRC_START_LSB
, 0x158)
146 REG32(ZDMA_CH_SRC_START_MSB
, 0x15c)
147 FIELD(ZDMA_CH_SRC_START_MSB
, ADDR
, 0, 17)
148 REG32(ZDMA_CH_DST_START_LSB
, 0x160)
149 REG32(ZDMA_CH_DST_START_MSB
, 0x164)
150 FIELD(ZDMA_CH_DST_START_MSB
, ADDR
, 0, 17)
151 REG32(ZDMA_CH_RATE_CTRL
, 0x18c)
152 FIELD(ZDMA_CH_RATE_CTRL
, CNT
, 0, 12)
153 REG32(ZDMA_CH_SRC_CUR_PYLD_LSB
, 0x168)
154 REG32(ZDMA_CH_SRC_CUR_PYLD_MSB
, 0x16c)
155 FIELD(ZDMA_CH_SRC_CUR_PYLD_MSB
, ADDR
, 0, 17)
156 REG32(ZDMA_CH_DST_CUR_PYLD_LSB
, 0x170)
157 REG32(ZDMA_CH_DST_CUR_PYLD_MSB
, 0x174)
158 FIELD(ZDMA_CH_DST_CUR_PYLD_MSB
, ADDR
, 0, 17)
159 REG32(ZDMA_CH_SRC_CUR_DSCR_LSB
, 0x178)
160 REG32(ZDMA_CH_SRC_CUR_DSCR_MSB
, 0x17c)
161 FIELD(ZDMA_CH_SRC_CUR_DSCR_MSB
, ADDR
, 0, 17)
162 REG32(ZDMA_CH_DST_CUR_DSCR_LSB
, 0x180)
163 REG32(ZDMA_CH_DST_CUR_DSCR_MSB
, 0x184)
164 FIELD(ZDMA_CH_DST_CUR_DSCR_MSB
, ADDR
, 0, 17)
165 REG32(ZDMA_CH_TOTAL_BYTE
, 0x188)
166 REG32(ZDMA_CH_RATE_CNTL
, 0x18c)
167 FIELD(ZDMA_CH_RATE_CNTL
, CNT
, 0, 12)
168 REG32(ZDMA_CH_IRQ_SRC_ACCT
, 0x190)
169 FIELD(ZDMA_CH_IRQ_SRC_ACCT
, CNT
, 0, 8)
170 REG32(ZDMA_CH_IRQ_DST_ACCT
, 0x194)
171 FIELD(ZDMA_CH_IRQ_DST_ACCT
, CNT
, 0, 8)
172 REG32(ZDMA_CH_DBG0
, 0x198)
173 FIELD(ZDMA_CH_DBG0
, CMN_BUF_FREE
, 0, 9)
174 REG32(ZDMA_CH_DBG1
, 0x19c)
175 FIELD(ZDMA_CH_DBG1
, CMN_BUF_OCC
, 0, 9)
176 REG32(ZDMA_CH_CTRL2
, 0x200)
177 FIELD(ZDMA_CH_CTRL2
, EN
, 0, 1)
205 static void zdma_ch_imr_update_irq(XlnxZDMA
*s
)
209 pending
= s
->regs
[R_ZDMA_CH_ISR
] & ~s
->regs
[R_ZDMA_CH_IMR
];
211 qemu_set_irq(s
->irq_zdma_ch_imr
, pending
);
214 static void zdma_ch_isr_postw(RegisterInfo
*reg
, uint64_t val64
)
216 XlnxZDMA
*s
= XLNX_ZDMA(reg
->opaque
);
217 zdma_ch_imr_update_irq(s
);
220 static uint64_t zdma_ch_ien_prew(RegisterInfo
*reg
, uint64_t val64
)
222 XlnxZDMA
*s
= XLNX_ZDMA(reg
->opaque
);
223 uint32_t val
= val64
;
225 s
->regs
[R_ZDMA_CH_IMR
] &= ~val
;
226 zdma_ch_imr_update_irq(s
);
230 static uint64_t zdma_ch_ids_prew(RegisterInfo
*reg
, uint64_t val64
)
232 XlnxZDMA
*s
= XLNX_ZDMA(reg
->opaque
);
233 uint32_t val
= val64
;
235 s
->regs
[R_ZDMA_CH_IMR
] |= val
;
236 zdma_ch_imr_update_irq(s
);
240 static void zdma_set_state(XlnxZDMA
*s
, XlnxZDMAState state
)
243 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_STATUS
, STATE
, state
);
245 /* Signal error if we have an error condition. */
247 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_STATUS
, STATE
, 3);
251 static void zdma_src_done(XlnxZDMA
*s
)
254 cnt
= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_IRQ_SRC_ACCT
, CNT
);
256 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_IRQ_SRC_ACCT
, CNT
, cnt
);
257 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_ISR
, SRC_DSCR_DONE
, true);
259 /* Did we overflow? */
260 if (cnt
!= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_IRQ_SRC_ACCT
, CNT
)) {
261 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_ISR
, IRQ_SRC_ACCT_ERR
, true);
263 zdma_ch_imr_update_irq(s
);
266 static void zdma_dst_done(XlnxZDMA
*s
)
269 cnt
= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_IRQ_DST_ACCT
, CNT
);
271 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_IRQ_DST_ACCT
, CNT
, cnt
);
272 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_ISR
, DST_DSCR_DONE
, true);
274 /* Did we overflow? */
275 if (cnt
!= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_IRQ_DST_ACCT
, CNT
)) {
276 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_ISR
, IRQ_DST_ACCT_ERR
, true);
278 zdma_ch_imr_update_irq(s
);
281 static uint64_t zdma_get_regaddr64(XlnxZDMA
*s
, unsigned int basereg
)
285 addr
= s
->regs
[basereg
+ 1];
287 addr
|= s
->regs
[basereg
];
292 static void zdma_put_regaddr64(XlnxZDMA
*s
, unsigned int basereg
, uint64_t addr
)
294 s
->regs
[basereg
] = addr
;
295 s
->regs
[basereg
+ 1] = addr
>> 32;
298 static bool zdma_load_descriptor(XlnxZDMA
*s
, uint64_t addr
, void *buf
)
300 /* ZDMA descriptors must be aligned to their own size. */
301 if (addr
% sizeof(XlnxZDMADescr
)) {
302 qemu_log_mask(LOG_GUEST_ERROR
,
303 "zdma: unaligned descriptor at %" PRIx64
,
305 memset(buf
, 0x0, sizeof(XlnxZDMADescr
));
310 address_space_rw(s
->dma_as
, addr
, s
->attr
,
311 buf
, sizeof(XlnxZDMADescr
), false);
315 static void zdma_load_src_descriptor(XlnxZDMA
*s
)
318 unsigned int ptype
= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_CTRL0
, POINT_TYPE
);
320 if (ptype
== PT_REG
) {
321 memcpy(&s
->dsc_src
, &s
->regs
[R_ZDMA_CH_SRC_DSCR_WORD0
],
326 src_addr
= zdma_get_regaddr64(s
, R_ZDMA_CH_SRC_CUR_DSCR_LSB
);
328 if (!zdma_load_descriptor(s
, src_addr
, &s
->dsc_src
)) {
329 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_ISR
, AXI_RD_SRC_DSCR
, true);
333 static void zdma_load_dst_descriptor(XlnxZDMA
*s
)
336 unsigned int ptype
= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_CTRL0
, POINT_TYPE
);
338 if (ptype
== PT_REG
) {
339 memcpy(&s
->dsc_dst
, &s
->regs
[R_ZDMA_CH_DST_DSCR_WORD0
],
344 dst_addr
= zdma_get_regaddr64(s
, R_ZDMA_CH_DST_CUR_DSCR_LSB
);
346 if (!zdma_load_descriptor(s
, dst_addr
, &s
->dsc_dst
)) {
347 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_ISR
, AXI_RD_DST_DSCR
, true);
351 static uint64_t zdma_update_descr_addr(XlnxZDMA
*s
, bool type
,
352 unsigned int basereg
)
356 if (type
== DTYPE_LINEAR
) {
357 next
= zdma_get_regaddr64(s
, basereg
);
358 next
+= sizeof(s
->dsc_dst
);
359 zdma_put_regaddr64(s
, basereg
, next
);
361 addr
= zdma_get_regaddr64(s
, basereg
);
362 addr
+= sizeof(s
->dsc_dst
);
363 address_space_rw(s
->dma_as
, addr
, s
->attr
, (void *) &next
, 8, false);
364 zdma_put_regaddr64(s
, basereg
, next
);
369 static void zdma_write_dst(XlnxZDMA
*s
, uint8_t *buf
, uint32_t len
)
371 uint32_t dst_size
, dlen
;
372 bool dst_intr
, dst_type
;
373 unsigned int ptype
= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_CTRL0
, POINT_TYPE
);
374 unsigned int rw_mode
= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_CTRL0
, MODE
);
375 unsigned int burst_type
= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_DATA_ATTR
,
378 /* FIXED burst types are only supported in simple dma mode. */
379 if (ptype
!= PT_REG
) {
380 burst_type
= AXI_BURST_INCR
;
384 dst_size
= FIELD_EX32(s
->dsc_dst
.words
[2], ZDMA_CH_DST_DSCR_WORD2
,
386 dst_type
= FIELD_EX32(s
->dsc_dst
.words
[3], ZDMA_CH_DST_DSCR_WORD3
,
388 if (dst_size
== 0 && ptype
== PT_MEM
) {
390 next
= zdma_update_descr_addr(s
, dst_type
,
391 R_ZDMA_CH_DST_CUR_DSCR_LSB
);
392 zdma_load_descriptor(s
, next
, &s
->dsc_dst
);
393 dst_size
= FIELD_EX32(s
->dsc_dst
.words
[2], ZDMA_CH_DST_DSCR_WORD2
,
395 dst_type
= FIELD_EX32(s
->dsc_dst
.words
[3], ZDMA_CH_DST_DSCR_WORD3
,
399 /* Match what hardware does by ignoring the dst_size and only using
400 * the src size for Simple register mode. */
401 if (ptype
== PT_REG
&& rw_mode
!= RW_MODE_WO
) {
405 dst_intr
= FIELD_EX32(s
->dsc_dst
.words
[3], ZDMA_CH_DST_DSCR_WORD3
,
408 dlen
= len
> dst_size
? dst_size
: len
;
409 if (burst_type
== AXI_BURST_FIXED
) {
410 if (dlen
> (s
->cfg
.bus_width
/ 8)) {
411 dlen
= s
->cfg
.bus_width
/ 8;
415 address_space_rw(s
->dma_as
, s
->dsc_dst
.addr
, s
->attr
, buf
, dlen
,
417 if (burst_type
== AXI_BURST_INCR
) {
418 s
->dsc_dst
.addr
+= dlen
;
424 if (dst_size
== 0 && dst_intr
) {
428 /* Write back to buffered descriptor. */
429 s
->dsc_dst
.words
[2] = FIELD_DP32(s
->dsc_dst
.words
[2],
430 ZDMA_CH_DST_DSCR_WORD2
,
436 static void zdma_process_descr(XlnxZDMA
*s
)
439 uint32_t src_size
, len
;
440 unsigned int src_cmd
;
441 bool src_intr
, src_type
;
442 unsigned int ptype
= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_CTRL0
, POINT_TYPE
);
443 unsigned int rw_mode
= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_CTRL0
, MODE
);
444 unsigned int burst_type
= ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_DATA_ATTR
,
447 src_addr
= s
->dsc_src
.addr
;
448 src_size
= FIELD_EX32(s
->dsc_src
.words
[2], ZDMA_CH_SRC_DSCR_WORD2
, SIZE
);
449 src_cmd
= FIELD_EX32(s
->dsc_src
.words
[3], ZDMA_CH_SRC_DSCR_WORD3
, CMD
);
450 src_type
= FIELD_EX32(s
->dsc_src
.words
[3], ZDMA_CH_SRC_DSCR_WORD3
, TYPE
);
451 src_intr
= FIELD_EX32(s
->dsc_src
.words
[3], ZDMA_CH_SRC_DSCR_WORD3
, INTR
);
453 /* FIXED burst types and non-rw modes are only supported in
456 if (ptype
!= PT_REG
) {
457 if (rw_mode
!= RW_MODE_RW
) {
458 qemu_log_mask(LOG_GUEST_ERROR
,
459 "zDMA: rw-mode=%d but not simple DMA mode.\n",
462 if (burst_type
!= AXI_BURST_INCR
) {
463 qemu_log_mask(LOG_GUEST_ERROR
,
464 "zDMA: burst_type=%d but not simple DMA mode.\n",
467 burst_type
= AXI_BURST_INCR
;
468 rw_mode
= RW_MODE_RW
;
471 if (rw_mode
== RW_MODE_WO
) {
472 /* In Simple DMA Write-Only, we need to push DST size bytes
473 * regardless of what SRC size is set to. */
474 src_size
= FIELD_EX32(s
->dsc_dst
.words
[2], ZDMA_CH_DST_DSCR_WORD2
,
476 memcpy(s
->buf
, &s
->regs
[R_ZDMA_CH_WR_ONLY_WORD0
], s
->cfg
.bus_width
/ 8);
480 len
= src_size
> ARRAY_SIZE(s
->buf
) ? ARRAY_SIZE(s
->buf
) : src_size
;
481 if (burst_type
== AXI_BURST_FIXED
) {
482 if (len
> (s
->cfg
.bus_width
/ 8)) {
483 len
= s
->cfg
.bus_width
/ 8;
487 if (rw_mode
== RW_MODE_WO
) {
488 if (len
> s
->cfg
.bus_width
/ 8) {
489 len
= s
->cfg
.bus_width
/ 8;
492 address_space_rw(s
->dma_as
, src_addr
, s
->attr
, s
->buf
, len
,
494 if (burst_type
== AXI_BURST_INCR
) {
499 if (rw_mode
!= RW_MODE_RO
) {
500 zdma_write_dst(s
, s
->buf
, len
);
503 s
->regs
[R_ZDMA_CH_TOTAL_BYTE
] += len
;
507 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_ISR
, DMA_DONE
, true);
513 /* Load next descriptor. */
514 if (ptype
== PT_REG
|| src_cmd
== CMD_STOP
) {
515 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_CTRL2
, EN
, 0);
516 zdma_set_state(s
, DISABLED
);
520 if (src_cmd
== CMD_HALT
) {
521 zdma_set_state(s
, PAUSED
);
522 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_ISR
, DMA_PAUSE
, 1);
523 zdma_ch_imr_update_irq(s
);
527 zdma_update_descr_addr(s
, src_type
, R_ZDMA_CH_SRC_CUR_DSCR_LSB
);
530 static void zdma_run(XlnxZDMA
*s
)
532 while (s
->state
== ENABLED
&& !s
->error
) {
533 zdma_load_src_descriptor(s
);
536 zdma_set_state(s
, DISABLED
);
538 zdma_process_descr(s
);
542 zdma_ch_imr_update_irq(s
);
545 static void zdma_update_descr_addr_from_start(XlnxZDMA
*s
)
547 uint64_t src_addr
, dst_addr
;
549 src_addr
= zdma_get_regaddr64(s
, R_ZDMA_CH_SRC_START_LSB
);
550 zdma_put_regaddr64(s
, R_ZDMA_CH_SRC_CUR_DSCR_LSB
, src_addr
);
551 dst_addr
= zdma_get_regaddr64(s
, R_ZDMA_CH_DST_START_LSB
);
552 zdma_put_regaddr64(s
, R_ZDMA_CH_DST_CUR_DSCR_LSB
, dst_addr
);
553 zdma_load_dst_descriptor(s
);
556 static void zdma_ch_ctrlx_postw(RegisterInfo
*reg
, uint64_t val64
)
558 XlnxZDMA
*s
= XLNX_ZDMA(reg
->opaque
);
560 if (ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_CTRL2
, EN
)) {
563 if (s
->state
== PAUSED
&&
564 ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_CTRL0
, CONT
)) {
565 if (ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_CTRL0
, CONT_ADDR
) == 1) {
566 zdma_update_descr_addr_from_start(s
);
568 bool src_type
= FIELD_EX32(s
->dsc_src
.words
[3],
569 ZDMA_CH_SRC_DSCR_WORD3
, TYPE
);
570 zdma_update_descr_addr(s
, src_type
,
571 R_ZDMA_CH_SRC_CUR_DSCR_LSB
);
573 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_CTRL0
, CONT
, false);
574 zdma_set_state(s
, ENABLED
);
575 } else if (s
->state
== DISABLED
) {
576 zdma_update_descr_addr_from_start(s
);
577 zdma_set_state(s
, ENABLED
);
580 /* Leave Paused state? */
581 if (s
->state
== PAUSED
&&
582 ARRAY_FIELD_EX32(s
->regs
, ZDMA_CH_CTRL0
, CONT
)) {
583 zdma_set_state(s
, DISABLED
);
590 static RegisterAccessInfo zdma_regs_info
[] = {
591 { .name
= "ZDMA_ERR_CTRL", .addr
= A_ZDMA_ERR_CTRL
,
593 },{ .name
= "ZDMA_CH_ISR", .addr
= A_ZDMA_CH_ISR
,
596 .post_write
= zdma_ch_isr_postw
,
597 },{ .name
= "ZDMA_CH_IMR", .addr
= A_ZDMA_CH_IMR
,
601 },{ .name
= "ZDMA_CH_IEN", .addr
= A_ZDMA_CH_IEN
,
603 .pre_write
= zdma_ch_ien_prew
,
604 },{ .name
= "ZDMA_CH_IDS", .addr
= A_ZDMA_CH_IDS
,
606 .pre_write
= zdma_ch_ids_prew
,
607 },{ .name
= "ZDMA_CH_CTRL0", .addr
= A_ZDMA_CH_CTRL0
,
610 .post_write
= zdma_ch_ctrlx_postw
,
611 },{ .name
= "ZDMA_CH_CTRL1", .addr
= A_ZDMA_CH_CTRL1
,
614 },{ .name
= "ZDMA_CH_FCI", .addr
= A_ZDMA_CH_FCI
,
616 },{ .name
= "ZDMA_CH_STATUS", .addr
= A_ZDMA_CH_STATUS
,
619 },{ .name
= "ZDMA_CH_DATA_ATTR", .addr
= A_ZDMA_CH_DATA_ATTR
,
622 },{ .name
= "ZDMA_CH_DSCR_ATTR", .addr
= A_ZDMA_CH_DSCR_ATTR
,
624 },{ .name
= "ZDMA_CH_SRC_DSCR_WORD0", .addr
= A_ZDMA_CH_SRC_DSCR_WORD0
,
625 },{ .name
= "ZDMA_CH_SRC_DSCR_WORD1", .addr
= A_ZDMA_CH_SRC_DSCR_WORD1
,
627 },{ .name
= "ZDMA_CH_SRC_DSCR_WORD2", .addr
= A_ZDMA_CH_SRC_DSCR_WORD2
,
629 },{ .name
= "ZDMA_CH_SRC_DSCR_WORD3", .addr
= A_ZDMA_CH_SRC_DSCR_WORD3
,
631 },{ .name
= "ZDMA_CH_DST_DSCR_WORD0", .addr
= A_ZDMA_CH_DST_DSCR_WORD0
,
632 },{ .name
= "ZDMA_CH_DST_DSCR_WORD1", .addr
= A_ZDMA_CH_DST_DSCR_WORD1
,
634 },{ .name
= "ZDMA_CH_DST_DSCR_WORD2", .addr
= A_ZDMA_CH_DST_DSCR_WORD2
,
636 },{ .name
= "ZDMA_CH_DST_DSCR_WORD3", .addr
= A_ZDMA_CH_DST_DSCR_WORD3
,
638 },{ .name
= "ZDMA_CH_WR_ONLY_WORD0", .addr
= A_ZDMA_CH_WR_ONLY_WORD0
,
639 },{ .name
= "ZDMA_CH_WR_ONLY_WORD1", .addr
= A_ZDMA_CH_WR_ONLY_WORD1
,
640 },{ .name
= "ZDMA_CH_WR_ONLY_WORD2", .addr
= A_ZDMA_CH_WR_ONLY_WORD2
,
641 },{ .name
= "ZDMA_CH_WR_ONLY_WORD3", .addr
= A_ZDMA_CH_WR_ONLY_WORD3
,
642 },{ .name
= "ZDMA_CH_SRC_START_LSB", .addr
= A_ZDMA_CH_SRC_START_LSB
,
643 },{ .name
= "ZDMA_CH_SRC_START_MSB", .addr
= A_ZDMA_CH_SRC_START_MSB
,
645 },{ .name
= "ZDMA_CH_DST_START_LSB", .addr
= A_ZDMA_CH_DST_START_LSB
,
646 },{ .name
= "ZDMA_CH_DST_START_MSB", .addr
= A_ZDMA_CH_DST_START_MSB
,
648 },{ .name
= "ZDMA_CH_SRC_CUR_PYLD_LSB", .addr
= A_ZDMA_CH_SRC_CUR_PYLD_LSB
,
650 },{ .name
= "ZDMA_CH_SRC_CUR_PYLD_MSB", .addr
= A_ZDMA_CH_SRC_CUR_PYLD_MSB
,
653 },{ .name
= "ZDMA_CH_DST_CUR_PYLD_LSB", .addr
= A_ZDMA_CH_DST_CUR_PYLD_LSB
,
655 },{ .name
= "ZDMA_CH_DST_CUR_PYLD_MSB", .addr
= A_ZDMA_CH_DST_CUR_PYLD_MSB
,
658 },{ .name
= "ZDMA_CH_SRC_CUR_DSCR_LSB", .addr
= A_ZDMA_CH_SRC_CUR_DSCR_LSB
,
660 },{ .name
= "ZDMA_CH_SRC_CUR_DSCR_MSB", .addr
= A_ZDMA_CH_SRC_CUR_DSCR_MSB
,
663 },{ .name
= "ZDMA_CH_DST_CUR_DSCR_LSB", .addr
= A_ZDMA_CH_DST_CUR_DSCR_LSB
,
665 },{ .name
= "ZDMA_CH_DST_CUR_DSCR_MSB", .addr
= A_ZDMA_CH_DST_CUR_DSCR_MSB
,
668 },{ .name
= "ZDMA_CH_TOTAL_BYTE", .addr
= A_ZDMA_CH_TOTAL_BYTE
,
670 },{ .name
= "ZDMA_CH_RATE_CNTL", .addr
= A_ZDMA_CH_RATE_CNTL
,
672 },{ .name
= "ZDMA_CH_IRQ_SRC_ACCT", .addr
= A_ZDMA_CH_IRQ_SRC_ACCT
,
676 },{ .name
= "ZDMA_CH_IRQ_DST_ACCT", .addr
= A_ZDMA_CH_IRQ_DST_ACCT
,
680 },{ .name
= "ZDMA_CH_DBG0", .addr
= A_ZDMA_CH_DBG0
,
683 },{ .name
= "ZDMA_CH_DBG1", .addr
= A_ZDMA_CH_DBG1
,
686 },{ .name
= "ZDMA_CH_CTRL2", .addr
= A_ZDMA_CH_CTRL2
,
688 .post_write
= zdma_ch_ctrlx_postw
,
692 static void zdma_reset(DeviceState
*dev
)
694 XlnxZDMA
*s
= XLNX_ZDMA(dev
);
697 for (i
= 0; i
< ARRAY_SIZE(s
->regs_info
); ++i
) {
698 register_reset(&s
->regs_info
[i
]);
701 zdma_ch_imr_update_irq(s
);
704 static uint64_t zdma_read(void *opaque
, hwaddr addr
, unsigned size
)
706 XlnxZDMA
*s
= XLNX_ZDMA(opaque
);
707 RegisterInfo
*r
= &s
->regs_info
[addr
/ 4];
710 gchar
*path
= object_get_canonical_path(OBJECT(s
));
711 qemu_log("%s: Decode error: read from %" HWADDR_PRIx
"\n",
715 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_ISR
, INV_APB
, true);
716 zdma_ch_imr_update_irq(s
);
719 return register_read(r
, ~0, NULL
, false);
722 static void zdma_write(void *opaque
, hwaddr addr
, uint64_t value
,
725 XlnxZDMA
*s
= XLNX_ZDMA(opaque
);
726 RegisterInfo
*r
= &s
->regs_info
[addr
/ 4];
729 gchar
*path
= object_get_canonical_path(OBJECT(s
));
730 qemu_log("%s: Decode error: write to %" HWADDR_PRIx
"=%" PRIx64
"\n",
734 ARRAY_FIELD_DP32(s
->regs
, ZDMA_CH_ISR
, INV_APB
, true);
735 zdma_ch_imr_update_irq(s
);
738 register_write(r
, value
, ~0, NULL
, false);
741 static const MemoryRegionOps zdma_ops
= {
744 .endianness
= DEVICE_LITTLE_ENDIAN
,
746 .min_access_size
= 4,
747 .max_access_size
= 4,
751 static void zdma_realize(DeviceState
*dev
, Error
**errp
)
753 XlnxZDMA
*s
= XLNX_ZDMA(dev
);
756 for (i
= 0; i
< ARRAY_SIZE(zdma_regs_info
); ++i
) {
757 RegisterInfo
*r
= &s
->regs_info
[zdma_regs_info
[i
].addr
/ 4];
759 *r
= (RegisterInfo
) {
760 .data
= (uint8_t *)&s
->regs
[
761 zdma_regs_info
[i
].addr
/ 4],
762 .data_size
= sizeof(uint32_t),
763 .access
= &zdma_regs_info
[i
],
769 s
->dma_as
= g_malloc0(sizeof(AddressSpace
));
770 address_space_init(s
->dma_as
, s
->dma_mr
, NULL
);
772 s
->dma_as
= &address_space_memory
;
774 s
->attr
= MEMTXATTRS_UNSPECIFIED
;
777 static void zdma_init(Object
*obj
)
779 XlnxZDMA
*s
= XLNX_ZDMA(obj
);
780 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
782 memory_region_init_io(&s
->iomem
, obj
, &zdma_ops
, s
,
783 TYPE_XLNX_ZDMA
, ZDMA_R_MAX
* 4);
784 sysbus_init_mmio(sbd
, &s
->iomem
);
785 sysbus_init_irq(sbd
, &s
->irq_zdma_ch_imr
);
787 object_property_add_link(obj
, "dma", TYPE_MEMORY_REGION
,
788 (Object
**)&s
->dma_mr
,
789 qdev_prop_allow_set_link_before_realize
,
790 OBJ_PROP_LINK_STRONG
,
794 static const VMStateDescription vmstate_zdma
= {
795 .name
= TYPE_XLNX_ZDMA
,
797 .minimum_version_id
= 1,
798 .minimum_version_id_old
= 1,
799 .fields
= (VMStateField
[]) {
800 VMSTATE_UINT32_ARRAY(regs
, XlnxZDMA
, ZDMA_R_MAX
),
801 VMSTATE_UINT32(state
, XlnxZDMA
),
802 VMSTATE_UINT32_ARRAY(dsc_src
.words
, XlnxZDMA
, 4),
803 VMSTATE_UINT32_ARRAY(dsc_dst
.words
, XlnxZDMA
, 4),
804 VMSTATE_END_OF_LIST(),
808 static Property zdma_props
[] = {
809 DEFINE_PROP_UINT32("bus-width", XlnxZDMA
, cfg
.bus_width
, 64),
810 DEFINE_PROP_END_OF_LIST(),
813 static void zdma_class_init(ObjectClass
*klass
, void *data
)
815 DeviceClass
*dc
= DEVICE_CLASS(klass
);
817 dc
->reset
= zdma_reset
;
818 dc
->realize
= zdma_realize
;
819 dc
->props
= zdma_props
;
820 dc
->vmsd
= &vmstate_zdma
;
823 static const TypeInfo zdma_info
= {
824 .name
= TYPE_XLNX_ZDMA
,
825 .parent
= TYPE_SYS_BUS_DEVICE
,
826 .instance_size
= sizeof(XlnxZDMA
),
827 .class_init
= zdma_class_init
,
828 .instance_init
= zdma_init
,
831 static void zdma_register_types(void)
833 type_register_static(&zdma_info
);
836 type_init(zdma_register_types
)