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 "qemu/osdep.h"
26 #include "hw/sysbus.h"
27 #include "qapi/error.h"
28 #include "qemu/timer.h"
31 #include "hw/ptimer.h"
32 #include "hw/qdev-properties.h"
34 #include "qemu/module.h"
36 #include "sysemu/dma.h"
37 #include "hw/stream.h"
41 #define TYPE_XILINX_AXI_DMA "xlnx.axi-dma"
42 #define TYPE_XILINX_AXI_DMA_DATA_STREAM "xilinx-axi-dma-data-stream"
43 #define TYPE_XILINX_AXI_DMA_CONTROL_STREAM "xilinx-axi-dma-control-stream"
45 #define XILINX_AXI_DMA(obj) \
46 OBJECT_CHECK(XilinxAXIDMA, (obj), TYPE_XILINX_AXI_DMA)
48 #define XILINX_AXI_DMA_DATA_STREAM(obj) \
49 OBJECT_CHECK(XilinxAXIDMAStreamSlave, (obj),\
50 TYPE_XILINX_AXI_DMA_DATA_STREAM)
52 #define XILINX_AXI_DMA_CONTROL_STREAM(obj) \
53 OBJECT_CHECK(XilinxAXIDMAStreamSlave, (obj),\
54 TYPE_XILINX_AXI_DMA_CONTROL_STREAM)
56 #define R_DMACR (0x00 / 4)
57 #define R_DMASR (0x04 / 4)
58 #define R_CURDESC (0x08 / 4)
59 #define R_TAILDESC (0x10 / 4)
60 #define R_MAX (0x30 / 4)
62 #define CONTROL_PAYLOAD_WORDS 5
63 #define CONTROL_PAYLOAD_SIZE (CONTROL_PAYLOAD_WORDS * (sizeof(uint32_t)))
65 typedef struct XilinxAXIDMA XilinxAXIDMA
;
66 typedef struct XilinxAXIDMAStreamSlave XilinxAXIDMAStreamSlave
;
70 DMACR_TAILPTR_MODE
= 2,
77 DMASR_IOC_IRQ
= 1 << 12,
78 DMASR_DLY_IRQ
= 1 << 13,
80 DMASR_IRQ_MASK
= 7 << 12
85 uint64_t buffer_address
;
89 uint8_t app
[CONTROL_PAYLOAD_SIZE
];
93 SDESC_CTRL_EOF
= (1 << 26),
94 SDESC_CTRL_SOF
= (1 << 27),
96 SDESC_CTRL_LEN_MASK
= (1 << 23) - 1
100 SDESC_STATUS_EOF
= (1 << 26),
101 SDESC_STATUS_SOF_BIT
= 27,
102 SDESC_STATUS_SOF
= (1 << SDESC_STATUS_SOF_BIT
),
103 SDESC_STATUS_COMPLETE
= (1 << 31)
107 struct XilinxAXIDMA
*dma
;
108 ptimer_state
*ptimer
;
114 unsigned int complete_cnt
;
115 uint32_t regs
[R_MAX
];
117 unsigned char txbuf
[16 * 1024];
120 struct XilinxAXIDMAStreamSlave
{
123 struct XilinxAXIDMA
*dma
;
126 struct XilinxAXIDMA
{
129 MemoryRegion
*dma_mr
;
133 StreamSlave
*tx_data_dev
;
134 StreamSlave
*tx_control_dev
;
135 XilinxAXIDMAStreamSlave rx_data_dev
;
136 XilinxAXIDMAStreamSlave rx_control_dev
;
138 struct Stream streams
[2];
140 StreamCanPushNotifyFn notify
;
145 * Helper calls to extract info from descriptors and other trivial
148 static inline int stream_desc_sof(struct SDesc
*d
)
150 return d
->control
& SDESC_CTRL_SOF
;
153 static inline int stream_desc_eof(struct SDesc
*d
)
155 return d
->control
& SDESC_CTRL_EOF
;
158 static inline int stream_resetting(struct Stream
*s
)
160 return !!(s
->regs
[R_DMACR
] & DMACR_RESET
);
163 static inline int stream_running(struct Stream
*s
)
165 return s
->regs
[R_DMACR
] & DMACR_RUNSTOP
;
168 static inline int stream_idle(struct Stream
*s
)
170 return !!(s
->regs
[R_DMASR
] & DMASR_IDLE
);
173 static void stream_reset(struct Stream
*s
)
175 s
->regs
[R_DMASR
] = DMASR_HALTED
; /* starts up halted. */
176 s
->regs
[R_DMACR
] = 1 << 16; /* Starts with one in compl threshold. */
179 /* Map an offset addr into a channel index. */
180 static inline int streamid_from_addr(hwaddr addr
)
189 static void stream_desc_load(struct Stream
*s
, hwaddr addr
)
191 struct SDesc
*d
= &s
->desc
;
193 address_space_read(&s
->dma
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, d
, sizeof *d
);
195 /* Convert from LE into host endianness. */
196 d
->buffer_address
= le64_to_cpu(d
->buffer_address
);
197 d
->nxtdesc
= le64_to_cpu(d
->nxtdesc
);
198 d
->control
= le32_to_cpu(d
->control
);
199 d
->status
= le32_to_cpu(d
->status
);
202 static void stream_desc_store(struct Stream
*s
, hwaddr addr
)
204 struct SDesc
*d
= &s
->desc
;
206 /* Convert from host endianness into LE. */
207 d
->buffer_address
= cpu_to_le64(d
->buffer_address
);
208 d
->nxtdesc
= cpu_to_le64(d
->nxtdesc
);
209 d
->control
= cpu_to_le32(d
->control
);
210 d
->status
= cpu_to_le32(d
->status
);
211 address_space_write(&s
->dma
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
215 static void stream_update_irq(struct Stream
*s
)
217 unsigned int pending
, mask
, irq
;
219 pending
= s
->regs
[R_DMASR
] & DMASR_IRQ_MASK
;
220 mask
= s
->regs
[R_DMACR
] & DMASR_IRQ_MASK
;
222 irq
= pending
& mask
;
224 qemu_set_irq(s
->irq
, !!irq
);
227 static void stream_reload_complete_cnt(struct Stream
*s
)
229 unsigned int comp_th
;
230 comp_th
= (s
->regs
[R_DMACR
] >> 16) & 0xff;
231 s
->complete_cnt
= comp_th
;
234 static void timer_hit(void *opaque
)
236 struct Stream
*s
= opaque
;
238 stream_reload_complete_cnt(s
);
239 s
->regs
[R_DMASR
] |= DMASR_DLY_IRQ
;
240 stream_update_irq(s
);
243 static void stream_complete(struct Stream
*s
)
245 unsigned int comp_delay
;
247 /* Start the delayed timer. */
248 ptimer_transaction_begin(s
->ptimer
);
249 comp_delay
= s
->regs
[R_DMACR
] >> 24;
251 ptimer_stop(s
->ptimer
);
252 ptimer_set_count(s
->ptimer
, comp_delay
);
253 ptimer_run(s
->ptimer
, 1);
257 if (s
->complete_cnt
== 0) {
258 /* Raise the IOC irq. */
259 s
->regs
[R_DMASR
] |= DMASR_IOC_IRQ
;
260 stream_reload_complete_cnt(s
);
262 ptimer_transaction_commit(s
->ptimer
);
265 static void stream_process_mem2s(struct Stream
*s
, StreamSlave
*tx_data_dev
,
266 StreamSlave
*tx_control_dev
)
273 if (!stream_running(s
) || stream_idle(s
)) {
278 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
280 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
281 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
285 if (stream_desc_sof(&s
->desc
)) {
286 stream_push(tx_control_dev
, s
->desc
.app
, sizeof(s
->desc
.app
), true);
289 txlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
291 eop
= stream_desc_eof(&s
->desc
);
292 addr
= s
->desc
.buffer_address
;
296 len
= txlen
> sizeof s
->txbuf
? sizeof s
->txbuf
: txlen
;
297 address_space_read(&s
->dma
->as
, addr
,
298 MEMTXATTRS_UNSPECIFIED
,
300 stream_push(tx_data_dev
, s
->txbuf
, len
, eop
&& len
== txlen
);
309 /* Update the descriptor. */
310 s
->desc
.status
= txlen
| SDESC_STATUS_COMPLETE
;
311 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
314 prev_d
= s
->regs
[R_CURDESC
];
315 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
316 if (prev_d
== s
->regs
[R_TAILDESC
]) {
317 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
323 static size_t stream_process_s2mem(struct Stream
*s
, unsigned char *buf
,
331 if (!stream_running(s
) || stream_idle(s
)) {
336 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
338 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
339 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
343 rxlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
349 address_space_write(&s
->dma
->as
, s
->desc
.buffer_address
,
350 MEMTXATTRS_UNSPECIFIED
, buf
+ pos
, rxlen
);
354 /* Update the descriptor. */
357 memcpy(s
->desc
.app
, s
->app
, sizeof(s
->desc
.app
));
358 s
->desc
.status
|= SDESC_STATUS_EOF
;
361 s
->desc
.status
|= sof
<< SDESC_STATUS_SOF_BIT
;
362 s
->desc
.status
|= SDESC_STATUS_COMPLETE
;
363 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
367 prev_d
= s
->regs
[R_CURDESC
];
368 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
369 if (prev_d
== s
->regs
[R_TAILDESC
]) {
370 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
378 static void xilinx_axidma_reset(DeviceState
*dev
)
381 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
383 for (i
= 0; i
< 2; i
++) {
384 stream_reset(&s
->streams
[i
]);
389 xilinx_axidma_control_stream_push(StreamSlave
*obj
, unsigned char *buf
,
390 size_t len
, bool eop
)
392 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(obj
);
393 struct Stream
*s
= &cs
->dma
->streams
[1];
395 if (len
!= CONTROL_PAYLOAD_SIZE
) {
396 hw_error("AXI DMA requires %d byte control stream payload\n",
397 (int)CONTROL_PAYLOAD_SIZE
);
400 memcpy(s
->app
, buf
, len
);
405 xilinx_axidma_data_stream_can_push(StreamSlave
*obj
,
406 StreamCanPushNotifyFn notify
,
409 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
410 struct Stream
*s
= &ds
->dma
->streams
[1];
412 if (!stream_running(s
) || stream_idle(s
)) {
413 ds
->dma
->notify
= notify
;
414 ds
->dma
->notify_opaque
= notify_opaque
;
422 xilinx_axidma_data_stream_push(StreamSlave
*obj
, unsigned char *buf
, size_t len
,
425 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
426 struct Stream
*s
= &ds
->dma
->streams
[1];
430 ret
= stream_process_s2mem(s
, buf
, len
);
431 stream_update_irq(s
);
435 static uint64_t axidma_read(void *opaque
, hwaddr addr
,
438 XilinxAXIDMA
*d
= opaque
;
443 sid
= streamid_from_addr(addr
);
444 s
= &d
->streams
[sid
];
450 /* Simulate one cycles reset delay. */
451 s
->regs
[addr
] &= ~DMACR_RESET
;
455 s
->regs
[addr
] &= 0xffff;
456 s
->regs
[addr
] |= (s
->complete_cnt
& 0xff) << 16;
457 s
->regs
[addr
] |= (ptimer_get_count(s
->ptimer
) & 0xff) << 24;
462 D(qemu_log("%s ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
463 __func__
, sid
, addr
* 4, r
));
470 static void axidma_write(void *opaque
, hwaddr addr
,
471 uint64_t value
, unsigned size
)
473 XilinxAXIDMA
*d
= opaque
;
477 sid
= streamid_from_addr(addr
);
478 s
= &d
->streams
[sid
];
484 /* Tailptr mode is always on. */
485 value
|= DMACR_TAILPTR_MODE
;
486 /* Remember our previous reset state. */
487 value
|= (s
->regs
[addr
] & DMACR_RESET
);
488 s
->regs
[addr
] = value
;
490 if (value
& DMACR_RESET
) {
494 if ((value
& 1) && !stream_resetting(s
)) {
495 /* Start processing. */
496 s
->regs
[R_DMASR
] &= ~(DMASR_HALTED
| DMASR_IDLE
);
498 stream_reload_complete_cnt(s
);
502 /* Mask away write to clear irq lines. */
503 value
&= ~(value
& DMASR_IRQ_MASK
);
504 s
->regs
[addr
] = value
;
508 s
->regs
[addr
] = value
;
509 s
->regs
[R_DMASR
] &= ~DMASR_IDLE
; /* Not idle. */
511 stream_process_mem2s(s
, d
->tx_data_dev
, d
->tx_control_dev
);
515 D(qemu_log("%s: ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
516 __func__
, sid
, addr
* 4, (unsigned)value
));
517 s
->regs
[addr
] = value
;
520 if (sid
== 1 && d
->notify
) {
521 StreamCanPushNotifyFn notifytmp
= d
->notify
;
523 notifytmp(d
->notify_opaque
);
525 stream_update_irq(s
);
528 static const MemoryRegionOps axidma_ops
= {
530 .write
= axidma_write
,
531 .endianness
= DEVICE_NATIVE_ENDIAN
,
534 static void xilinx_axidma_realize(DeviceState
*dev
, Error
**errp
)
536 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
537 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(&s
->rx_data_dev
);
538 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(
540 Error
*local_err
= NULL
;
543 object_property_add_link(OBJECT(ds
), "dma", TYPE_XILINX_AXI_DMA
,
545 object_property_allow_set_link
,
546 OBJ_PROP_LINK_STRONG
,
548 object_property_add_link(OBJECT(cs
), "dma", TYPE_XILINX_AXI_DMA
,
550 object_property_allow_set_link
,
551 OBJ_PROP_LINK_STRONG
,
554 goto xilinx_axidma_realize_fail
;
556 object_property_set_link(OBJECT(ds
), OBJECT(s
), "dma", &local_err
);
557 object_property_set_link(OBJECT(cs
), OBJECT(s
), "dma", &local_err
);
559 goto xilinx_axidma_realize_fail
;
562 for (i
= 0; i
< 2; i
++) {
563 struct Stream
*st
= &s
->streams
[i
];
567 st
->ptimer
= ptimer_init(timer_hit
, st
, PTIMER_POLICY_DEFAULT
);
568 ptimer_transaction_begin(st
->ptimer
);
569 ptimer_set_freq(st
->ptimer
, s
->freqhz
);
570 ptimer_transaction_commit(st
->ptimer
);
573 address_space_init(&s
->as
,
574 s
->dma_mr
? s
->dma_mr
: get_system_memory(), "dma");
577 xilinx_axidma_realize_fail
:
578 error_propagate(errp
, local_err
);
581 static void xilinx_axidma_init(Object
*obj
)
583 XilinxAXIDMA
*s
= XILINX_AXI_DMA(obj
);
584 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
586 object_initialize_child(OBJECT(s
), "axistream-connected-target",
587 &s
->rx_data_dev
, sizeof(s
->rx_data_dev
),
588 TYPE_XILINX_AXI_DMA_DATA_STREAM
, &error_abort
,
590 object_initialize_child(OBJECT(s
), "axistream-control-connected-target",
591 &s
->rx_control_dev
, sizeof(s
->rx_control_dev
),
592 TYPE_XILINX_AXI_DMA_CONTROL_STREAM
, &error_abort
,
594 object_property_add_link(obj
, "dma", TYPE_MEMORY_REGION
,
595 (Object
**)&s
->dma_mr
,
596 qdev_prop_allow_set_link_before_realize
,
597 OBJ_PROP_LINK_STRONG
,
600 sysbus_init_irq(sbd
, &s
->streams
[0].irq
);
601 sysbus_init_irq(sbd
, &s
->streams
[1].irq
);
603 memory_region_init_io(&s
->iomem
, obj
, &axidma_ops
, s
,
604 "xlnx.axi-dma", R_MAX
* 4 * 2);
605 sysbus_init_mmio(sbd
, &s
->iomem
);
608 static Property axidma_properties
[] = {
609 DEFINE_PROP_UINT32("freqhz", XilinxAXIDMA
, freqhz
, 50000000),
610 DEFINE_PROP_LINK("axistream-connected", XilinxAXIDMA
,
611 tx_data_dev
, TYPE_STREAM_SLAVE
, StreamSlave
*),
612 DEFINE_PROP_LINK("axistream-control-connected", XilinxAXIDMA
,
613 tx_control_dev
, TYPE_STREAM_SLAVE
, StreamSlave
*),
614 DEFINE_PROP_END_OF_LIST(),
617 static void axidma_class_init(ObjectClass
*klass
, void *data
)
619 DeviceClass
*dc
= DEVICE_CLASS(klass
);
621 dc
->realize
= xilinx_axidma_realize
,
622 dc
->reset
= xilinx_axidma_reset
;
623 device_class_set_props(dc
, axidma_properties
);
626 static StreamSlaveClass xilinx_axidma_data_stream_class
= {
627 .push
= xilinx_axidma_data_stream_push
,
628 .can_push
= xilinx_axidma_data_stream_can_push
,
631 static StreamSlaveClass xilinx_axidma_control_stream_class
= {
632 .push
= xilinx_axidma_control_stream_push
,
635 static void xilinx_axidma_stream_class_init(ObjectClass
*klass
, void *data
)
637 StreamSlaveClass
*ssc
= STREAM_SLAVE_CLASS(klass
);
639 ssc
->push
= ((StreamSlaveClass
*)data
)->push
;
640 ssc
->can_push
= ((StreamSlaveClass
*)data
)->can_push
;
643 static const TypeInfo axidma_info
= {
644 .name
= TYPE_XILINX_AXI_DMA
,
645 .parent
= TYPE_SYS_BUS_DEVICE
,
646 .instance_size
= sizeof(XilinxAXIDMA
),
647 .class_init
= axidma_class_init
,
648 .instance_init
= xilinx_axidma_init
,
651 static const TypeInfo xilinx_axidma_data_stream_info
= {
652 .name
= TYPE_XILINX_AXI_DMA_DATA_STREAM
,
653 .parent
= TYPE_OBJECT
,
654 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
655 .class_init
= xilinx_axidma_stream_class_init
,
656 .class_data
= &xilinx_axidma_data_stream_class
,
657 .interfaces
= (InterfaceInfo
[]) {
658 { TYPE_STREAM_SLAVE
},
663 static const TypeInfo xilinx_axidma_control_stream_info
= {
664 .name
= TYPE_XILINX_AXI_DMA_CONTROL_STREAM
,
665 .parent
= TYPE_OBJECT
,
666 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
667 .class_init
= xilinx_axidma_stream_class_init
,
668 .class_data
= &xilinx_axidma_control_stream_class
,
669 .interfaces
= (InterfaceInfo
[]) {
670 { TYPE_STREAM_SLAVE
},
675 static void xilinx_axidma_register_types(void)
677 type_register_static(&axidma_info
);
678 type_register_static(&xilinx_axidma_data_stream_info
);
679 type_register_static(&xilinx_axidma_control_stream_info
);
682 type_init(xilinx_axidma_register_types
)