2 * QEMU model of Xilinx AXI-DMA block.
4 * Copyright (c) 2011 Edgar E. Iglesias.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/sysbus.h"
26 #include "qemu/timer.h"
27 #include "hw/ptimer.h"
29 #include "qapi/qmp/qerror.h"
31 #include "hw/stream.h"
35 #define TYPE_XILINX_AXI_DMA "xlnx.axi-dma"
36 #define TYPE_XILINX_AXI_DMA_DATA_STREAM "xilinx-axi-dma-data-stream"
37 #define TYPE_XILINX_AXI_DMA_CONTROL_STREAM "xilinx-axi-dma-control-stream"
39 #define XILINX_AXI_DMA(obj) \
40 OBJECT_CHECK(XilinxAXIDMA, (obj), TYPE_XILINX_AXI_DMA)
42 #define XILINX_AXI_DMA_DATA_STREAM(obj) \
43 OBJECT_CHECK(XilinxAXIDMAStreamSlave, (obj),\
44 TYPE_XILINX_AXI_DMA_DATA_STREAM)
46 #define XILINX_AXI_DMA_CONTROL_STREAM(obj) \
47 OBJECT_CHECK(XilinxAXIDMAStreamSlave, (obj),\
48 TYPE_XILINX_AXI_DMA_CONTROL_STREAM)
50 #define R_DMACR (0x00 / 4)
51 #define R_DMASR (0x04 / 4)
52 #define R_CURDESC (0x08 / 4)
53 #define R_TAILDESC (0x10 / 4)
54 #define R_MAX (0x30 / 4)
56 #define CONTROL_PAYLOAD_WORDS 5
57 #define CONTROL_PAYLOAD_SIZE (CONTROL_PAYLOAD_WORDS * (sizeof(uint32_t)))
59 typedef struct XilinxAXIDMA XilinxAXIDMA
;
60 typedef struct XilinxAXIDMAStreamSlave XilinxAXIDMAStreamSlave
;
64 DMACR_TAILPTR_MODE
= 2,
71 DMASR_IOC_IRQ
= 1 << 12,
72 DMASR_DLY_IRQ
= 1 << 13,
74 DMASR_IRQ_MASK
= 7 << 12
79 uint64_t buffer_address
;
83 uint8_t app
[CONTROL_PAYLOAD_SIZE
];
87 SDESC_CTRL_EOF
= (1 << 26),
88 SDESC_CTRL_SOF
= (1 << 27),
90 SDESC_CTRL_LEN_MASK
= (1 << 23) - 1
94 SDESC_STATUS_EOF
= (1 << 26),
95 SDESC_STATUS_SOF_BIT
= 27,
96 SDESC_STATUS_SOF
= (1 << SDESC_STATUS_SOF_BIT
),
97 SDESC_STATUS_COMPLETE
= (1 << 31)
102 ptimer_state
*ptimer
;
109 unsigned int complete_cnt
;
110 uint32_t regs
[R_MAX
];
114 struct XilinxAXIDMAStreamSlave
{
117 struct XilinxAXIDMA
*dma
;
120 struct XilinxAXIDMA
{
124 StreamSlave
*tx_data_dev
;
125 StreamSlave
*tx_control_dev
;
126 XilinxAXIDMAStreamSlave rx_data_dev
;
127 XilinxAXIDMAStreamSlave rx_control_dev
;
129 struct Stream streams
[2];
131 StreamCanPushNotifyFn notify
;
136 * Helper calls to extract info from desriptors and other trivial
139 static inline int stream_desc_sof(struct SDesc
*d
)
141 return d
->control
& SDESC_CTRL_SOF
;
144 static inline int stream_desc_eof(struct SDesc
*d
)
146 return d
->control
& SDESC_CTRL_EOF
;
149 static inline int stream_resetting(struct Stream
*s
)
151 return !!(s
->regs
[R_DMACR
] & DMACR_RESET
);
154 static inline int stream_running(struct Stream
*s
)
156 return s
->regs
[R_DMACR
] & DMACR_RUNSTOP
;
159 static inline int stream_halted(struct Stream
*s
)
161 return s
->regs
[R_DMASR
] & DMASR_HALTED
;
164 static inline int stream_idle(struct Stream
*s
)
166 return !!(s
->regs
[R_DMASR
] & DMASR_IDLE
);
169 static void stream_reset(struct Stream
*s
)
171 s
->regs
[R_DMASR
] = DMASR_HALTED
; /* starts up halted. */
172 s
->regs
[R_DMACR
] = 1 << 16; /* Starts with one in compl threshold. */
175 /* Map an offset addr into a channel index. */
176 static inline int streamid_from_addr(hwaddr addr
)
186 static void stream_desc_show(struct SDesc
*d
)
188 qemu_log("buffer_addr = " PRIx64
"\n", d
->buffer_address
);
189 qemu_log("nxtdesc = " PRIx64
"\n", d
->nxtdesc
);
190 qemu_log("control = %x\n", d
->control
);
191 qemu_log("status = %x\n", d
->status
);
195 static void stream_desc_load(struct Stream
*s
, hwaddr addr
)
197 struct SDesc
*d
= &s
->desc
;
199 cpu_physical_memory_read(addr
, d
, sizeof *d
);
201 /* Convert from LE into host endianness. */
202 d
->buffer_address
= le64_to_cpu(d
->buffer_address
);
203 d
->nxtdesc
= le64_to_cpu(d
->nxtdesc
);
204 d
->control
= le32_to_cpu(d
->control
);
205 d
->status
= le32_to_cpu(d
->status
);
208 static void stream_desc_store(struct Stream
*s
, hwaddr addr
)
210 struct SDesc
*d
= &s
->desc
;
212 /* Convert from host endianness into LE. */
213 d
->buffer_address
= cpu_to_le64(d
->buffer_address
);
214 d
->nxtdesc
= cpu_to_le64(d
->nxtdesc
);
215 d
->control
= cpu_to_le32(d
->control
);
216 d
->status
= cpu_to_le32(d
->status
);
217 cpu_physical_memory_write(addr
, d
, sizeof *d
);
220 static void stream_update_irq(struct Stream
*s
)
222 unsigned int pending
, mask
, irq
;
224 pending
= s
->regs
[R_DMASR
] & DMASR_IRQ_MASK
;
225 mask
= s
->regs
[R_DMACR
] & DMASR_IRQ_MASK
;
227 irq
= pending
& mask
;
229 qemu_set_irq(s
->irq
, !!irq
);
232 static void stream_reload_complete_cnt(struct Stream
*s
)
234 unsigned int comp_th
;
235 comp_th
= (s
->regs
[R_DMACR
] >> 16) & 0xff;
236 s
->complete_cnt
= comp_th
;
239 static void timer_hit(void *opaque
)
241 struct Stream
*s
= opaque
;
243 stream_reload_complete_cnt(s
);
244 s
->regs
[R_DMASR
] |= DMASR_DLY_IRQ
;
245 stream_update_irq(s
);
248 static void stream_complete(struct Stream
*s
)
250 unsigned int comp_delay
;
252 /* Start the delayed timer. */
253 comp_delay
= s
->regs
[R_DMACR
] >> 24;
255 ptimer_stop(s
->ptimer
);
256 ptimer_set_count(s
->ptimer
, comp_delay
);
257 ptimer_run(s
->ptimer
, 1);
261 if (s
->complete_cnt
== 0) {
262 /* Raise the IOC irq. */
263 s
->regs
[R_DMASR
] |= DMASR_IOC_IRQ
;
264 stream_reload_complete_cnt(s
);
268 static void stream_process_mem2s(struct Stream
*s
, StreamSlave
*tx_data_dev
,
269 StreamSlave
*tx_control_dev
)
272 unsigned char txbuf
[16 * 1024];
275 if (!stream_running(s
) || stream_idle(s
)) {
280 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
282 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
283 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
287 if (stream_desc_sof(&s
->desc
)) {
289 stream_push(tx_control_dev
, s
->desc
.app
, sizeof(s
->desc
.app
));
292 txlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
293 if ((txlen
+ s
->pos
) > sizeof txbuf
) {
294 hw_error("%s: too small internal txbuf! %d\n", __func__
,
298 cpu_physical_memory_read(s
->desc
.buffer_address
,
299 txbuf
+ s
->pos
, txlen
);
302 if (stream_desc_eof(&s
->desc
)) {
303 stream_push(tx_data_dev
, txbuf
, s
->pos
);
308 /* Update the descriptor. */
309 s
->desc
.status
= txlen
| SDESC_STATUS_COMPLETE
;
310 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
313 prev_d
= s
->regs
[R_CURDESC
];
314 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
315 if (prev_d
== s
->regs
[R_TAILDESC
]) {
316 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
322 static size_t stream_process_s2mem(struct Stream
*s
, unsigned char *buf
,
330 if (!stream_running(s
) || stream_idle(s
)) {
335 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
337 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
338 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
342 rxlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
348 cpu_physical_memory_write(s
->desc
.buffer_address
, buf
+ pos
, rxlen
);
352 /* Update the descriptor. */
355 memcpy(s
->desc
.app
, s
->app
, sizeof(s
->desc
.app
));
356 s
->desc
.status
|= SDESC_STATUS_EOF
;
359 s
->desc
.status
|= sof
<< SDESC_STATUS_SOF_BIT
;
360 s
->desc
.status
|= SDESC_STATUS_COMPLETE
;
361 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
365 prev_d
= s
->regs
[R_CURDESC
];
366 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
367 if (prev_d
== s
->regs
[R_TAILDESC
]) {
368 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
376 static void xilinx_axidma_reset(DeviceState
*dev
)
379 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
381 for (i
= 0; i
< 2; i
++) {
382 stream_reset(&s
->streams
[i
]);
387 xilinx_axidma_control_stream_push(StreamSlave
*obj
, unsigned char *buf
,
390 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(obj
);
391 struct Stream
*s
= &cs
->dma
->streams
[1];
393 if (len
!= CONTROL_PAYLOAD_SIZE
) {
394 hw_error("AXI DMA requires %d byte control stream payload\n",
395 (int)CONTROL_PAYLOAD_SIZE
);
398 memcpy(s
->app
, buf
, len
);
403 xilinx_axidma_data_stream_can_push(StreamSlave
*obj
,
404 StreamCanPushNotifyFn notify
,
407 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
408 struct Stream
*s
= &ds
->dma
->streams
[1];
410 if (!stream_running(s
) || stream_idle(s
)) {
411 ds
->dma
->notify
= notify
;
412 ds
->dma
->notify_opaque
= notify_opaque
;
420 xilinx_axidma_data_stream_push(StreamSlave
*obj
, unsigned char *buf
, size_t len
)
422 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
423 struct Stream
*s
= &ds
->dma
->streams
[1];
426 ret
= stream_process_s2mem(s
, buf
, len
);
427 stream_update_irq(s
);
431 static uint64_t axidma_read(void *opaque
, hwaddr addr
,
434 XilinxAXIDMA
*d
= opaque
;
439 sid
= streamid_from_addr(addr
);
440 s
= &d
->streams
[sid
];
446 /* Simulate one cycles reset delay. */
447 s
->regs
[addr
] &= ~DMACR_RESET
;
451 s
->regs
[addr
] &= 0xffff;
452 s
->regs
[addr
] |= (s
->complete_cnt
& 0xff) << 16;
453 s
->regs
[addr
] |= (ptimer_get_count(s
->ptimer
) & 0xff) << 24;
458 D(qemu_log("%s ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
459 __func__
, sid
, addr
* 4, r
));
466 static void axidma_write(void *opaque
, hwaddr addr
,
467 uint64_t value
, unsigned size
)
469 XilinxAXIDMA
*d
= opaque
;
473 sid
= streamid_from_addr(addr
);
474 s
= &d
->streams
[sid
];
480 /* Tailptr mode is always on. */
481 value
|= DMACR_TAILPTR_MODE
;
482 /* Remember our previous reset state. */
483 value
|= (s
->regs
[addr
] & DMACR_RESET
);
484 s
->regs
[addr
] = value
;
486 if (value
& DMACR_RESET
) {
490 if ((value
& 1) && !stream_resetting(s
)) {
491 /* Start processing. */
492 s
->regs
[R_DMASR
] &= ~(DMASR_HALTED
| DMASR_IDLE
);
494 stream_reload_complete_cnt(s
);
498 /* Mask away write to clear irq lines. */
499 value
&= ~(value
& DMASR_IRQ_MASK
);
500 s
->regs
[addr
] = value
;
504 s
->regs
[addr
] = value
;
505 s
->regs
[R_DMASR
] &= ~DMASR_IDLE
; /* Not idle. */
507 stream_process_mem2s(s
, d
->tx_data_dev
, d
->tx_control_dev
);
511 D(qemu_log("%s: ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
512 __func__
, sid
, addr
* 4, (unsigned)value
));
513 s
->regs
[addr
] = value
;
516 if (sid
== 1 && d
->notify
) {
517 d
->notify(d
->notify_opaque
);
520 stream_update_irq(s
);
523 static const MemoryRegionOps axidma_ops
= {
525 .write
= axidma_write
,
526 .endianness
= DEVICE_NATIVE_ENDIAN
,
529 static void xilinx_axidma_realize(DeviceState
*dev
, Error
**errp
)
531 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
532 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(&s
->rx_data_dev
);
533 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(
535 Error
*local_errp
= NULL
;
537 object_property_add_link(OBJECT(ds
), "dma", TYPE_XILINX_AXI_DMA
,
538 (Object
**)&ds
->dma
, &local_errp
);
539 object_property_add_link(OBJECT(cs
), "dma", TYPE_XILINX_AXI_DMA
,
540 (Object
**)&cs
->dma
, &local_errp
);
542 goto xilinx_axidma_realize_fail
;
544 object_property_set_link(OBJECT(ds
), OBJECT(s
), "dma", &local_errp
);
545 object_property_set_link(OBJECT(cs
), OBJECT(s
), "dma", &local_errp
);
547 goto xilinx_axidma_realize_fail
;
552 for (i
= 0; i
< 2; i
++) {
553 s
->streams
[i
].nr
= i
;
554 s
->streams
[i
].bh
= qemu_bh_new(timer_hit
, &s
->streams
[i
]);
555 s
->streams
[i
].ptimer
= ptimer_init(s
->streams
[i
].bh
);
556 ptimer_set_freq(s
->streams
[i
].ptimer
, s
->freqhz
);
560 xilinx_axidma_realize_fail
:
566 static void xilinx_axidma_init(Object
*obj
)
568 XilinxAXIDMA
*s
= XILINX_AXI_DMA(obj
);
569 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
572 object_property_add_link(obj
, "axistream-connected", TYPE_STREAM_SLAVE
,
573 (Object
**) &s
->tx_data_dev
, &errp
);
574 assert_no_error(errp
);
575 object_property_add_link(obj
, "axistream-control-connected",
577 (Object
**) &s
->tx_control_dev
, &errp
);
578 assert_no_error(errp
);
580 object_initialize(&s
->rx_data_dev
, TYPE_XILINX_AXI_DMA_DATA_STREAM
);
581 object_initialize(&s
->rx_control_dev
, TYPE_XILINX_AXI_DMA_CONTROL_STREAM
);
582 object_property_add_child(OBJECT(s
), "axistream-connected-target",
583 (Object
*)&s
->rx_data_dev
, &errp
);
584 assert_no_error(errp
);
585 object_property_add_child(OBJECT(s
), "axistream-control-connected-target",
586 (Object
*)&s
->rx_control_dev
, &errp
);
587 assert_no_error(errp
);
589 sysbus_init_irq(sbd
, &s
->streams
[0].irq
);
590 sysbus_init_irq(sbd
, &s
->streams
[1].irq
);
592 memory_region_init_io(&s
->iomem
, &axidma_ops
, s
,
593 "xlnx.axi-dma", R_MAX
* 4 * 2);
594 sysbus_init_mmio(sbd
, &s
->iomem
);
597 static Property axidma_properties
[] = {
598 DEFINE_PROP_UINT32("freqhz", XilinxAXIDMA
, freqhz
, 50000000),
599 DEFINE_PROP_END_OF_LIST(),
602 static void axidma_class_init(ObjectClass
*klass
, void *data
)
604 DeviceClass
*dc
= DEVICE_CLASS(klass
);
606 dc
->realize
= xilinx_axidma_realize
,
607 dc
->reset
= xilinx_axidma_reset
;
608 dc
->props
= axidma_properties
;
611 static StreamSlaveClass xilinx_axidma_data_stream_class
= {
612 .push
= xilinx_axidma_data_stream_push
,
613 .can_push
= xilinx_axidma_data_stream_can_push
,
616 static StreamSlaveClass xilinx_axidma_control_stream_class
= {
617 .push
= xilinx_axidma_control_stream_push
,
620 static void xilinx_axidma_stream_class_init(ObjectClass
*klass
, void *data
)
622 StreamSlaveClass
*ssc
= STREAM_SLAVE_CLASS(klass
);
624 ssc
->push
= ((StreamSlaveClass
*)data
)->push
;
625 ssc
->can_push
= ((StreamSlaveClass
*)data
)->can_push
;
628 static const TypeInfo axidma_info
= {
629 .name
= TYPE_XILINX_AXI_DMA
,
630 .parent
= TYPE_SYS_BUS_DEVICE
,
631 .instance_size
= sizeof(XilinxAXIDMA
),
632 .class_init
= axidma_class_init
,
633 .instance_init
= xilinx_axidma_init
,
636 static const TypeInfo xilinx_axidma_data_stream_info
= {
637 .name
= TYPE_XILINX_AXI_DMA_DATA_STREAM
,
638 .parent
= TYPE_OBJECT
,
639 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
640 .class_init
= xilinx_axidma_stream_class_init
,
641 .class_data
= &xilinx_axidma_data_stream_class
,
642 .interfaces
= (InterfaceInfo
[]) {
643 { TYPE_STREAM_SLAVE
},
648 static const TypeInfo xilinx_axidma_control_stream_info
= {
649 .name
= TYPE_XILINX_AXI_DMA_CONTROL_STREAM
,
650 .parent
= TYPE_OBJECT
,
651 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
652 .class_init
= xilinx_axidma_stream_class_init
,
653 .class_data
= &xilinx_axidma_control_stream_class
,
654 .interfaces
= (InterfaceInfo
[]) {
655 { TYPE_STREAM_SLAVE
},
660 static void xilinx_axidma_register_types(void)
662 type_register_static(&axidma_info
);
663 type_register_static(&xilinx_axidma_data_stream_info
);
664 type_register_static(&xilinx_axidma_control_stream_info
);
667 type_init(xilinx_axidma_register_types
)