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"
43 #define TYPE_XILINX_AXI_DMA "xlnx.axi-dma"
44 #define TYPE_XILINX_AXI_DMA_DATA_STREAM "xilinx-axi-dma-data-stream"
45 #define TYPE_XILINX_AXI_DMA_CONTROL_STREAM "xilinx-axi-dma-control-stream"
47 OBJECT_DECLARE_SIMPLE_TYPE(XilinxAXIDMA
, XILINX_AXI_DMA
)
49 typedef struct XilinxAXIDMAStreamSink XilinxAXIDMAStreamSink
;
50 DECLARE_INSTANCE_CHECKER(XilinxAXIDMAStreamSink
, XILINX_AXI_DMA_DATA_STREAM
,
51 TYPE_XILINX_AXI_DMA_DATA_STREAM
)
53 DECLARE_INSTANCE_CHECKER(XilinxAXIDMAStreamSink
, XILINX_AXI_DMA_CONTROL_STREAM
,
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)))
68 DMACR_TAILPTR_MODE
= 2,
75 DMASR_SLVERR
= 1 << 5,
76 DMASR_DECERR
= 1 << 6,
77 DMASR_IOC_IRQ
= 1 << 12,
78 DMASR_DLY_IRQ
= 1 << 13,
79 DMASR_ERR_IRQ
= 1 << 14,
81 DMASR_IRQ_MASK
= 7 << 12
86 uint64_t buffer_address
;
90 uint8_t app
[CONTROL_PAYLOAD_SIZE
];
94 SDESC_CTRL_EOF
= (1 << 26),
95 SDESC_CTRL_SOF
= (1 << 27),
97 SDESC_CTRL_LEN_MASK
= (1 << 23) - 1
101 SDESC_STATUS_EOF
= (1 << 26),
102 SDESC_STATUS_SOF_BIT
= 27,
103 SDESC_STATUS_SOF
= (1 << SDESC_STATUS_SOF_BIT
),
104 SDESC_STATUS_COMPLETE
= (1 << 31)
108 struct XilinxAXIDMA
*dma
;
109 ptimer_state
*ptimer
;
116 unsigned int complete_cnt
;
117 uint32_t regs
[R_MAX
];
119 unsigned char txbuf
[16 * 1024];
122 struct XilinxAXIDMAStreamSink
{
125 struct XilinxAXIDMA
*dma
;
128 struct XilinxAXIDMA
{
131 MemoryRegion
*dma_mr
;
135 StreamSink
*tx_data_dev
;
136 StreamSink
*tx_control_dev
;
137 XilinxAXIDMAStreamSink rx_data_dev
;
138 XilinxAXIDMAStreamSink rx_control_dev
;
140 struct Stream streams
[2];
142 StreamCanPushNotifyFn notify
;
147 * Helper calls to extract info from descriptors and other trivial
150 static inline int stream_desc_sof(struct SDesc
*d
)
152 return d
->control
& SDESC_CTRL_SOF
;
155 static inline int stream_desc_eof(struct SDesc
*d
)
157 return d
->control
& SDESC_CTRL_EOF
;
160 static inline int stream_resetting(struct Stream
*s
)
162 return !!(s
->regs
[R_DMACR
] & DMACR_RESET
);
165 static inline int stream_running(struct Stream
*s
)
167 return s
->regs
[R_DMACR
] & DMACR_RUNSTOP
;
170 static inline int stream_idle(struct Stream
*s
)
172 return !!(s
->regs
[R_DMASR
] & DMASR_IDLE
);
175 static inline int stream_halted(struct Stream
*s
)
177 return !!(s
->regs
[R_DMASR
] & DMASR_HALTED
);
180 static void stream_reset(struct Stream
*s
)
182 s
->regs
[R_DMASR
] = DMASR_HALTED
; /* starts up halted. */
183 s
->regs
[R_DMACR
] = 1 << 16; /* Starts with one in compl threshold. */
187 /* Map an offset addr into a channel index. */
188 static inline int streamid_from_addr(hwaddr addr
)
197 static MemTxResult
stream_desc_load(struct Stream
*s
, hwaddr addr
)
199 struct SDesc
*d
= &s
->desc
;
201 MemTxResult result
= address_space_read(&s
->dma
->as
,
202 addr
, MEMTXATTRS_UNSPECIFIED
,
204 if (result
!= MEMTX_OK
) {
205 trace_xilinx_axidma_loading_desc_fail(result
);
207 if (result
== MEMTX_DECODE_ERROR
) {
208 s
->regs
[R_DMASR
] |= DMASR_DECERR
;
210 s
->regs
[R_DMASR
] |= DMASR_SLVERR
;
213 s
->regs
[R_DMACR
] &= ~DMACR_RUNSTOP
;
214 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
215 s
->regs
[R_DMASR
] |= DMASR_ERR_IRQ
;
219 /* Convert from LE into host endianness. */
220 d
->buffer_address
= le64_to_cpu(d
->buffer_address
);
221 d
->nxtdesc
= le64_to_cpu(d
->nxtdesc
);
222 d
->control
= le32_to_cpu(d
->control
);
223 d
->status
= le32_to_cpu(d
->status
);
227 static void stream_desc_store(struct Stream
*s
, hwaddr addr
)
229 struct SDesc
*d
= &s
->desc
;
231 /* Convert from host endianness into LE. */
232 d
->buffer_address
= cpu_to_le64(d
->buffer_address
);
233 d
->nxtdesc
= cpu_to_le64(d
->nxtdesc
);
234 d
->control
= cpu_to_le32(d
->control
);
235 d
->status
= cpu_to_le32(d
->status
);
236 address_space_write(&s
->dma
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
240 static void stream_update_irq(struct Stream
*s
)
242 unsigned int pending
, mask
, irq
;
244 pending
= s
->regs
[R_DMASR
] & DMASR_IRQ_MASK
;
245 mask
= s
->regs
[R_DMACR
] & DMASR_IRQ_MASK
;
247 irq
= pending
& mask
;
249 qemu_set_irq(s
->irq
, !!irq
);
252 static void stream_reload_complete_cnt(struct Stream
*s
)
254 unsigned int comp_th
;
255 comp_th
= (s
->regs
[R_DMACR
] >> 16) & 0xff;
256 s
->complete_cnt
= comp_th
;
259 static void timer_hit(void *opaque
)
261 struct Stream
*s
= opaque
;
263 stream_reload_complete_cnt(s
);
264 s
->regs
[R_DMASR
] |= DMASR_DLY_IRQ
;
265 stream_update_irq(s
);
268 static void stream_complete(struct Stream
*s
)
270 unsigned int comp_delay
;
272 /* Start the delayed timer. */
273 ptimer_transaction_begin(s
->ptimer
);
274 comp_delay
= s
->regs
[R_DMACR
] >> 24;
276 ptimer_stop(s
->ptimer
);
277 ptimer_set_count(s
->ptimer
, comp_delay
);
278 ptimer_run(s
->ptimer
, 1);
282 if (s
->complete_cnt
== 0) {
283 /* Raise the IOC irq. */
284 s
->regs
[R_DMASR
] |= DMASR_IOC_IRQ
;
285 stream_reload_complete_cnt(s
);
287 ptimer_transaction_commit(s
->ptimer
);
290 static void stream_process_mem2s(struct Stream
*s
, StreamSink
*tx_data_dev
,
291 StreamSink
*tx_control_dev
)
298 if (!stream_running(s
) || stream_idle(s
) || stream_halted(s
)) {
303 if (MEMTX_OK
!= stream_desc_load(s
, s
->regs
[R_CURDESC
])) {
307 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
308 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
312 if (stream_desc_sof(&s
->desc
)) {
313 stream_push(tx_control_dev
, s
->desc
.app
, sizeof(s
->desc
.app
), true);
316 txlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
318 eop
= stream_desc_eof(&s
->desc
);
319 addr
= s
->desc
.buffer_address
;
323 len
= txlen
> sizeof s
->txbuf
? sizeof s
->txbuf
: txlen
;
324 address_space_read(&s
->dma
->as
, addr
,
325 MEMTXATTRS_UNSPECIFIED
,
327 stream_push(tx_data_dev
, s
->txbuf
, len
, eop
&& len
== txlen
);
336 /* Update the descriptor. */
337 s
->desc
.status
= txlen
| SDESC_STATUS_COMPLETE
;
338 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
341 prev_d
= s
->regs
[R_CURDESC
];
342 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
343 if (prev_d
== s
->regs
[R_TAILDESC
]) {
344 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
350 static size_t stream_process_s2mem(struct Stream
*s
, unsigned char *buf
,
351 size_t len
, bool eop
)
357 if (!stream_running(s
) || stream_idle(s
) || stream_halted(s
)) {
362 if (MEMTX_OK
!= stream_desc_load(s
, s
->regs
[R_CURDESC
])) {
366 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
367 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
371 rxlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
377 address_space_write(&s
->dma
->as
, s
->desc
.buffer_address
,
378 MEMTXATTRS_UNSPECIFIED
, buf
+ pos
, rxlen
);
382 /* Update the descriptor. */
385 memcpy(s
->desc
.app
, s
->app
, sizeof(s
->desc
.app
));
386 s
->desc
.status
|= SDESC_STATUS_EOF
;
389 s
->desc
.status
|= s
->sof
<< SDESC_STATUS_SOF_BIT
;
390 s
->desc
.status
|= SDESC_STATUS_COMPLETE
;
391 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
395 prev_d
= s
->regs
[R_CURDESC
];
396 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
397 if (prev_d
== s
->regs
[R_TAILDESC
]) {
398 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
406 static void xilinx_axidma_reset(DeviceState
*dev
)
409 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
411 for (i
= 0; i
< 2; i
++) {
412 stream_reset(&s
->streams
[i
]);
417 xilinx_axidma_control_stream_push(StreamSink
*obj
, unsigned char *buf
,
418 size_t len
, bool eop
)
420 XilinxAXIDMAStreamSink
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(obj
);
421 struct Stream
*s
= &cs
->dma
->streams
[1];
423 if (len
!= CONTROL_PAYLOAD_SIZE
) {
424 hw_error("AXI DMA requires %d byte control stream payload\n",
425 (int)CONTROL_PAYLOAD_SIZE
);
428 memcpy(s
->app
, buf
, len
);
433 xilinx_axidma_data_stream_can_push(StreamSink
*obj
,
434 StreamCanPushNotifyFn notify
,
437 XilinxAXIDMAStreamSink
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
438 struct Stream
*s
= &ds
->dma
->streams
[1];
440 if (!stream_running(s
) || stream_idle(s
) || stream_halted(s
)) {
441 ds
->dma
->notify
= notify
;
442 ds
->dma
->notify_opaque
= notify_opaque
;
450 xilinx_axidma_data_stream_push(StreamSink
*obj
, unsigned char *buf
, size_t len
,
453 XilinxAXIDMAStreamSink
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
454 struct Stream
*s
= &ds
->dma
->streams
[1];
457 ret
= stream_process_s2mem(s
, buf
, len
, eop
);
458 stream_update_irq(s
);
462 static uint64_t axidma_read(void *opaque
, hwaddr addr
,
465 XilinxAXIDMA
*d
= opaque
;
470 sid
= streamid_from_addr(addr
);
471 s
= &d
->streams
[sid
];
477 /* Simulate one cycles reset delay. */
478 s
->regs
[addr
] &= ~DMACR_RESET
;
482 s
->regs
[addr
] &= 0xffff;
483 s
->regs
[addr
] |= (s
->complete_cnt
& 0xff) << 16;
484 s
->regs
[addr
] |= (ptimer_get_count(s
->ptimer
) & 0xff) << 24;
489 D(qemu_log("%s ch=%d addr=" HWADDR_FMT_plx
" v=%x\n",
490 __func__
, sid
, addr
* 4, r
));
497 static void axidma_write(void *opaque
, hwaddr addr
,
498 uint64_t value
, unsigned size
)
500 XilinxAXIDMA
*d
= opaque
;
504 sid
= streamid_from_addr(addr
);
505 s
= &d
->streams
[sid
];
511 /* Tailptr mode is always on. */
512 value
|= DMACR_TAILPTR_MODE
;
513 /* Remember our previous reset state. */
514 value
|= (s
->regs
[addr
] & DMACR_RESET
);
515 s
->regs
[addr
] = value
;
517 if (value
& DMACR_RESET
) {
521 if ((value
& 1) && !stream_resetting(s
)) {
522 /* Start processing. */
523 s
->regs
[R_DMASR
] &= ~(DMASR_HALTED
| DMASR_IDLE
);
525 stream_reload_complete_cnt(s
);
529 /* Mask away write to clear irq lines. */
530 value
&= ~(value
& DMASR_IRQ_MASK
);
531 s
->regs
[addr
] = value
;
535 s
->regs
[addr
] = value
;
536 s
->regs
[R_DMASR
] &= ~DMASR_IDLE
; /* Not idle. */
538 stream_process_mem2s(s
, d
->tx_data_dev
, d
->tx_control_dev
);
542 D(qemu_log("%s: ch=%d addr=" HWADDR_FMT_plx
" v=%x\n",
543 __func__
, sid
, addr
* 4, (unsigned)value
));
544 s
->regs
[addr
] = value
;
547 if (sid
== 1 && d
->notify
) {
548 StreamCanPushNotifyFn notifytmp
= d
->notify
;
550 notifytmp(d
->notify_opaque
);
552 stream_update_irq(s
);
555 static const MemoryRegionOps axidma_ops
= {
557 .write
= axidma_write
,
558 .endianness
= DEVICE_NATIVE_ENDIAN
,
561 static void xilinx_axidma_realize(DeviceState
*dev
, Error
**errp
)
563 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
564 XilinxAXIDMAStreamSink
*ds
= XILINX_AXI_DMA_DATA_STREAM(&s
->rx_data_dev
);
565 XilinxAXIDMAStreamSink
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(
569 object_property_add_link(OBJECT(ds
), "dma", TYPE_XILINX_AXI_DMA
,
571 object_property_allow_set_link
,
572 OBJ_PROP_LINK_STRONG
);
573 object_property_add_link(OBJECT(cs
), "dma", TYPE_XILINX_AXI_DMA
,
575 object_property_allow_set_link
,
576 OBJ_PROP_LINK_STRONG
);
577 object_property_set_link(OBJECT(ds
), "dma", OBJECT(s
), &error_abort
);
578 object_property_set_link(OBJECT(cs
), "dma", OBJECT(s
), &error_abort
);
580 for (i
= 0; i
< 2; i
++) {
581 struct Stream
*st
= &s
->streams
[i
];
585 st
->ptimer
= ptimer_init(timer_hit
, st
, PTIMER_POLICY_LEGACY
);
586 ptimer_transaction_begin(st
->ptimer
);
587 ptimer_set_freq(st
->ptimer
, s
->freqhz
);
588 ptimer_transaction_commit(st
->ptimer
);
591 address_space_init(&s
->as
,
592 s
->dma_mr
? s
->dma_mr
: get_system_memory(), "dma");
595 static void xilinx_axidma_init(Object
*obj
)
597 XilinxAXIDMA
*s
= XILINX_AXI_DMA(obj
);
598 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
600 object_initialize_child(OBJECT(s
), "axistream-connected-target",
601 &s
->rx_data_dev
, TYPE_XILINX_AXI_DMA_DATA_STREAM
);
602 object_initialize_child(OBJECT(s
), "axistream-control-connected-target",
604 TYPE_XILINX_AXI_DMA_CONTROL_STREAM
);
606 sysbus_init_irq(sbd
, &s
->streams
[0].irq
);
607 sysbus_init_irq(sbd
, &s
->streams
[1].irq
);
609 memory_region_init_io(&s
->iomem
, obj
, &axidma_ops
, s
,
610 "xlnx.axi-dma", R_MAX
* 4 * 2);
611 sysbus_init_mmio(sbd
, &s
->iomem
);
614 static Property axidma_properties
[] = {
615 DEFINE_PROP_UINT32("freqhz", XilinxAXIDMA
, freqhz
, 50000000),
616 DEFINE_PROP_LINK("axistream-connected", XilinxAXIDMA
,
617 tx_data_dev
, TYPE_STREAM_SINK
, StreamSink
*),
618 DEFINE_PROP_LINK("axistream-control-connected", XilinxAXIDMA
,
619 tx_control_dev
, TYPE_STREAM_SINK
, StreamSink
*),
620 DEFINE_PROP_LINK("dma", XilinxAXIDMA
, dma_mr
,
621 TYPE_MEMORY_REGION
, MemoryRegion
*),
622 DEFINE_PROP_END_OF_LIST(),
625 static void axidma_class_init(ObjectClass
*klass
, void *data
)
627 DeviceClass
*dc
= DEVICE_CLASS(klass
);
629 dc
->realize
= xilinx_axidma_realize
,
630 dc
->reset
= xilinx_axidma_reset
;
631 device_class_set_props(dc
, axidma_properties
);
634 static StreamSinkClass xilinx_axidma_data_stream_class
= {
635 .push
= xilinx_axidma_data_stream_push
,
636 .can_push
= xilinx_axidma_data_stream_can_push
,
639 static StreamSinkClass xilinx_axidma_control_stream_class
= {
640 .push
= xilinx_axidma_control_stream_push
,
643 static void xilinx_axidma_stream_class_init(ObjectClass
*klass
, void *data
)
645 StreamSinkClass
*ssc
= STREAM_SINK_CLASS(klass
);
647 ssc
->push
= ((StreamSinkClass
*)data
)->push
;
648 ssc
->can_push
= ((StreamSinkClass
*)data
)->can_push
;
651 static const TypeInfo axidma_info
= {
652 .name
= TYPE_XILINX_AXI_DMA
,
653 .parent
= TYPE_SYS_BUS_DEVICE
,
654 .instance_size
= sizeof(XilinxAXIDMA
),
655 .class_init
= axidma_class_init
,
656 .instance_init
= xilinx_axidma_init
,
659 static const TypeInfo xilinx_axidma_data_stream_info
= {
660 .name
= TYPE_XILINX_AXI_DMA_DATA_STREAM
,
661 .parent
= TYPE_OBJECT
,
662 .instance_size
= sizeof(XilinxAXIDMAStreamSink
),
663 .class_init
= xilinx_axidma_stream_class_init
,
664 .class_data
= &xilinx_axidma_data_stream_class
,
665 .interfaces
= (InterfaceInfo
[]) {
666 { TYPE_STREAM_SINK
},
671 static const TypeInfo xilinx_axidma_control_stream_info
= {
672 .name
= TYPE_XILINX_AXI_DMA_CONTROL_STREAM
,
673 .parent
= TYPE_OBJECT
,
674 .instance_size
= sizeof(XilinxAXIDMAStreamSink
),
675 .class_init
= xilinx_axidma_stream_class_init
,
676 .class_data
= &xilinx_axidma_control_stream_class
,
677 .interfaces
= (InterfaceInfo
[]) {
678 { TYPE_STREAM_SINK
},
683 static void xilinx_axidma_register_types(void)
685 type_register_static(&axidma_info
);
686 type_register_static(&xilinx_axidma_data_stream_info
);
687 type_register_static(&xilinx_axidma_control_stream_info
);
690 type_init(xilinx_axidma_register_types
)