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"
38 #include "qom/object.h"
42 #define TYPE_XILINX_AXI_DMA "xlnx.axi-dma"
43 #define TYPE_XILINX_AXI_DMA_DATA_STREAM "xilinx-axi-dma-data-stream"
44 #define TYPE_XILINX_AXI_DMA_CONTROL_STREAM "xilinx-axi-dma-control-stream"
46 typedef struct XilinxAXIDMA XilinxAXIDMA
;
47 DECLARE_INSTANCE_CHECKER(XilinxAXIDMA
, XILINX_AXI_DMA
,
50 typedef struct XilinxAXIDMAStreamSlave XilinxAXIDMAStreamSlave
;
51 DECLARE_INSTANCE_CHECKER(XilinxAXIDMAStreamSlave
, XILINX_AXI_DMA_DATA_STREAM
,
52 TYPE_XILINX_AXI_DMA_DATA_STREAM
)
54 DECLARE_INSTANCE_CHECKER(XilinxAXIDMAStreamSlave
, XILINX_AXI_DMA_CONTROL_STREAM
,
55 TYPE_XILINX_AXI_DMA_CONTROL_STREAM
)
57 #define R_DMACR (0x00 / 4)
58 #define R_DMASR (0x04 / 4)
59 #define R_CURDESC (0x08 / 4)
60 #define R_TAILDESC (0x10 / 4)
61 #define R_MAX (0x30 / 4)
63 #define CONTROL_PAYLOAD_WORDS 5
64 #define CONTROL_PAYLOAD_SIZE (CONTROL_PAYLOAD_WORDS * (sizeof(uint32_t)))
69 DMACR_TAILPTR_MODE
= 2,
76 DMASR_IOC_IRQ
= 1 << 12,
77 DMASR_DLY_IRQ
= 1 << 13,
79 DMASR_IRQ_MASK
= 7 << 12
84 uint64_t buffer_address
;
88 uint8_t app
[CONTROL_PAYLOAD_SIZE
];
92 SDESC_CTRL_EOF
= (1 << 26),
93 SDESC_CTRL_SOF
= (1 << 27),
95 SDESC_CTRL_LEN_MASK
= (1 << 23) - 1
99 SDESC_STATUS_EOF
= (1 << 26),
100 SDESC_STATUS_SOF_BIT
= 27,
101 SDESC_STATUS_SOF
= (1 << SDESC_STATUS_SOF_BIT
),
102 SDESC_STATUS_COMPLETE
= (1 << 31)
106 struct XilinxAXIDMA
*dma
;
107 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. */
180 /* Map an offset addr into a channel index. */
181 static inline int streamid_from_addr(hwaddr addr
)
190 static void stream_desc_load(struct Stream
*s
, hwaddr addr
)
192 struct SDesc
*d
= &s
->desc
;
194 address_space_read(&s
->dma
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, d
, sizeof *d
);
196 /* Convert from LE into host endianness. */
197 d
->buffer_address
= le64_to_cpu(d
->buffer_address
);
198 d
->nxtdesc
= le64_to_cpu(d
->nxtdesc
);
199 d
->control
= le32_to_cpu(d
->control
);
200 d
->status
= le32_to_cpu(d
->status
);
203 static void stream_desc_store(struct Stream
*s
, hwaddr addr
)
205 struct SDesc
*d
= &s
->desc
;
207 /* Convert from host endianness into LE. */
208 d
->buffer_address
= cpu_to_le64(d
->buffer_address
);
209 d
->nxtdesc
= cpu_to_le64(d
->nxtdesc
);
210 d
->control
= cpu_to_le32(d
->control
);
211 d
->status
= cpu_to_le32(d
->status
);
212 address_space_write(&s
->dma
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
216 static void stream_update_irq(struct Stream
*s
)
218 unsigned int pending
, mask
, irq
;
220 pending
= s
->regs
[R_DMASR
] & DMASR_IRQ_MASK
;
221 mask
= s
->regs
[R_DMACR
] & DMASR_IRQ_MASK
;
223 irq
= pending
& mask
;
225 qemu_set_irq(s
->irq
, !!irq
);
228 static void stream_reload_complete_cnt(struct Stream
*s
)
230 unsigned int comp_th
;
231 comp_th
= (s
->regs
[R_DMACR
] >> 16) & 0xff;
232 s
->complete_cnt
= comp_th
;
235 static void timer_hit(void *opaque
)
237 struct Stream
*s
= opaque
;
239 stream_reload_complete_cnt(s
);
240 s
->regs
[R_DMASR
] |= DMASR_DLY_IRQ
;
241 stream_update_irq(s
);
244 static void stream_complete(struct Stream
*s
)
246 unsigned int comp_delay
;
248 /* Start the delayed timer. */
249 ptimer_transaction_begin(s
->ptimer
);
250 comp_delay
= s
->regs
[R_DMACR
] >> 24;
252 ptimer_stop(s
->ptimer
);
253 ptimer_set_count(s
->ptimer
, comp_delay
);
254 ptimer_run(s
->ptimer
, 1);
258 if (s
->complete_cnt
== 0) {
259 /* Raise the IOC irq. */
260 s
->regs
[R_DMASR
] |= DMASR_IOC_IRQ
;
261 stream_reload_complete_cnt(s
);
263 ptimer_transaction_commit(s
->ptimer
);
266 static void stream_process_mem2s(struct Stream
*s
, StreamSlave
*tx_data_dev
,
267 StreamSlave
*tx_control_dev
)
274 if (!stream_running(s
) || stream_idle(s
)) {
279 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
281 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
282 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
286 if (stream_desc_sof(&s
->desc
)) {
287 stream_push(tx_control_dev
, s
->desc
.app
, sizeof(s
->desc
.app
), true);
290 txlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
292 eop
= stream_desc_eof(&s
->desc
);
293 addr
= s
->desc
.buffer_address
;
297 len
= txlen
> sizeof s
->txbuf
? sizeof s
->txbuf
: txlen
;
298 address_space_read(&s
->dma
->as
, addr
,
299 MEMTXATTRS_UNSPECIFIED
,
301 stream_push(tx_data_dev
, s
->txbuf
, len
, eop
&& len
== txlen
);
310 /* Update the descriptor. */
311 s
->desc
.status
= txlen
| SDESC_STATUS_COMPLETE
;
312 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
315 prev_d
= s
->regs
[R_CURDESC
];
316 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
317 if (prev_d
== s
->regs
[R_TAILDESC
]) {
318 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
324 static size_t stream_process_s2mem(struct Stream
*s
, unsigned char *buf
,
325 size_t len
, bool eop
)
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
|= s
->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];
429 ret
= stream_process_s2mem(s
, buf
, len
, eop
);
430 stream_update_irq(s
);
434 static uint64_t axidma_read(void *opaque
, hwaddr addr
,
437 XilinxAXIDMA
*d
= opaque
;
442 sid
= streamid_from_addr(addr
);
443 s
= &d
->streams
[sid
];
449 /* Simulate one cycles reset delay. */
450 s
->regs
[addr
] &= ~DMACR_RESET
;
454 s
->regs
[addr
] &= 0xffff;
455 s
->regs
[addr
] |= (s
->complete_cnt
& 0xff) << 16;
456 s
->regs
[addr
] |= (ptimer_get_count(s
->ptimer
) & 0xff) << 24;
461 D(qemu_log("%s ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
462 __func__
, sid
, addr
* 4, r
));
469 static void axidma_write(void *opaque
, hwaddr addr
,
470 uint64_t value
, unsigned size
)
472 XilinxAXIDMA
*d
= opaque
;
476 sid
= streamid_from_addr(addr
);
477 s
= &d
->streams
[sid
];
483 /* Tailptr mode is always on. */
484 value
|= DMACR_TAILPTR_MODE
;
485 /* Remember our previous reset state. */
486 value
|= (s
->regs
[addr
] & DMACR_RESET
);
487 s
->regs
[addr
] = value
;
489 if (value
& DMACR_RESET
) {
493 if ((value
& 1) && !stream_resetting(s
)) {
494 /* Start processing. */
495 s
->regs
[R_DMASR
] &= ~(DMASR_HALTED
| DMASR_IDLE
);
497 stream_reload_complete_cnt(s
);
501 /* Mask away write to clear irq lines. */
502 value
&= ~(value
& DMASR_IRQ_MASK
);
503 s
->regs
[addr
] = value
;
507 s
->regs
[addr
] = value
;
508 s
->regs
[R_DMASR
] &= ~DMASR_IDLE
; /* Not idle. */
510 stream_process_mem2s(s
, d
->tx_data_dev
, d
->tx_control_dev
);
514 D(qemu_log("%s: ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
515 __func__
, sid
, addr
* 4, (unsigned)value
));
516 s
->regs
[addr
] = value
;
519 if (sid
== 1 && d
->notify
) {
520 StreamCanPushNotifyFn notifytmp
= d
->notify
;
522 notifytmp(d
->notify_opaque
);
524 stream_update_irq(s
);
527 static const MemoryRegionOps axidma_ops
= {
529 .write
= axidma_write
,
530 .endianness
= DEVICE_NATIVE_ENDIAN
,
533 static void xilinx_axidma_realize(DeviceState
*dev
, Error
**errp
)
535 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
536 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(&s
->rx_data_dev
);
537 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(
541 object_property_add_link(OBJECT(ds
), "dma", TYPE_XILINX_AXI_DMA
,
543 object_property_allow_set_link
,
544 OBJ_PROP_LINK_STRONG
);
545 object_property_add_link(OBJECT(cs
), "dma", TYPE_XILINX_AXI_DMA
,
547 object_property_allow_set_link
,
548 OBJ_PROP_LINK_STRONG
);
549 object_property_set_link(OBJECT(ds
), "dma", OBJECT(s
), &error_abort
);
550 object_property_set_link(OBJECT(cs
), "dma", OBJECT(s
), &error_abort
);
552 for (i
= 0; i
< 2; i
++) {
553 struct Stream
*st
= &s
->streams
[i
];
557 st
->ptimer
= ptimer_init(timer_hit
, st
, PTIMER_POLICY_DEFAULT
);
558 ptimer_transaction_begin(st
->ptimer
);
559 ptimer_set_freq(st
->ptimer
, s
->freqhz
);
560 ptimer_transaction_commit(st
->ptimer
);
563 address_space_init(&s
->as
,
564 s
->dma_mr
? s
->dma_mr
: get_system_memory(), "dma");
567 static void xilinx_axidma_init(Object
*obj
)
569 XilinxAXIDMA
*s
= XILINX_AXI_DMA(obj
);
570 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
572 object_initialize_child(OBJECT(s
), "axistream-connected-target",
573 &s
->rx_data_dev
, TYPE_XILINX_AXI_DMA_DATA_STREAM
);
574 object_initialize_child(OBJECT(s
), "axistream-control-connected-target",
576 TYPE_XILINX_AXI_DMA_CONTROL_STREAM
);
577 object_property_add_link(obj
, "dma", TYPE_MEMORY_REGION
,
578 (Object
**)&s
->dma_mr
,
579 qdev_prop_allow_set_link_before_realize
,
580 OBJ_PROP_LINK_STRONG
);
582 sysbus_init_irq(sbd
, &s
->streams
[0].irq
);
583 sysbus_init_irq(sbd
, &s
->streams
[1].irq
);
585 memory_region_init_io(&s
->iomem
, obj
, &axidma_ops
, s
,
586 "xlnx.axi-dma", R_MAX
* 4 * 2);
587 sysbus_init_mmio(sbd
, &s
->iomem
);
590 static Property axidma_properties
[] = {
591 DEFINE_PROP_UINT32("freqhz", XilinxAXIDMA
, freqhz
, 50000000),
592 DEFINE_PROP_LINK("axistream-connected", XilinxAXIDMA
,
593 tx_data_dev
, TYPE_STREAM_SLAVE
, StreamSlave
*),
594 DEFINE_PROP_LINK("axistream-control-connected", XilinxAXIDMA
,
595 tx_control_dev
, TYPE_STREAM_SLAVE
, StreamSlave
*),
596 DEFINE_PROP_END_OF_LIST(),
599 static void axidma_class_init(ObjectClass
*klass
, void *data
)
601 DeviceClass
*dc
= DEVICE_CLASS(klass
);
603 dc
->realize
= xilinx_axidma_realize
,
604 dc
->reset
= xilinx_axidma_reset
;
605 device_class_set_props(dc
, axidma_properties
);
608 static StreamSlaveClass xilinx_axidma_data_stream_class
= {
609 .push
= xilinx_axidma_data_stream_push
,
610 .can_push
= xilinx_axidma_data_stream_can_push
,
613 static StreamSlaveClass xilinx_axidma_control_stream_class
= {
614 .push
= xilinx_axidma_control_stream_push
,
617 static void xilinx_axidma_stream_class_init(ObjectClass
*klass
, void *data
)
619 StreamSlaveClass
*ssc
= STREAM_SLAVE_CLASS(klass
);
621 ssc
->push
= ((StreamSlaveClass
*)data
)->push
;
622 ssc
->can_push
= ((StreamSlaveClass
*)data
)->can_push
;
625 static const TypeInfo axidma_info
= {
626 .name
= TYPE_XILINX_AXI_DMA
,
627 .parent
= TYPE_SYS_BUS_DEVICE
,
628 .instance_size
= sizeof(XilinxAXIDMA
),
629 .class_init
= axidma_class_init
,
630 .instance_init
= xilinx_axidma_init
,
633 static const TypeInfo xilinx_axidma_data_stream_info
= {
634 .name
= TYPE_XILINX_AXI_DMA_DATA_STREAM
,
635 .parent
= TYPE_OBJECT
,
636 .instance_size
= sizeof(XilinxAXIDMAStreamSlave
),
637 .class_init
= xilinx_axidma_stream_class_init
,
638 .class_data
= &xilinx_axidma_data_stream_class
,
639 .interfaces
= (InterfaceInfo
[]) {
640 { TYPE_STREAM_SLAVE
},
645 static const TypeInfo xilinx_axidma_control_stream_info
= {
646 .name
= TYPE_XILINX_AXI_DMA_CONTROL_STREAM
,
647 .parent
= TYPE_OBJECT
,
648 .instance_size
= sizeof(XilinxAXIDMAStreamSlave
),
649 .class_init
= xilinx_axidma_stream_class_init
,
650 .class_data
= &xilinx_axidma_control_stream_class
,
651 .interfaces
= (InterfaceInfo
[]) {
652 { TYPE_STREAM_SLAVE
},
657 static void xilinx_axidma_register_types(void)
659 type_register_static(&axidma_info
);
660 type_register_static(&xilinx_axidma_data_stream_info
);
661 type_register_static(&xilinx_axidma_control_stream_info
);
664 type_init(xilinx_axidma_register_types
)