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 "qemu/main-loop.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_idle(struct Stream
*s
)
161 return !!(s
->regs
[R_DMASR
] & DMASR_IDLE
);
164 static void stream_reset(struct Stream
*s
)
166 s
->regs
[R_DMASR
] = DMASR_HALTED
; /* starts up halted. */
167 s
->regs
[R_DMACR
] = 1 << 16; /* Starts with one in compl threshold. */
170 /* Map an offset addr into a channel index. */
171 static inline int streamid_from_addr(hwaddr addr
)
181 static void stream_desc_show(struct SDesc
*d
)
183 qemu_log("buffer_addr = " PRIx64
"\n", d
->buffer_address
);
184 qemu_log("nxtdesc = " PRIx64
"\n", d
->nxtdesc
);
185 qemu_log("control = %x\n", d
->control
);
186 qemu_log("status = %x\n", d
->status
);
190 static void stream_desc_load(struct Stream
*s
, hwaddr addr
)
192 struct SDesc
*d
= &s
->desc
;
194 cpu_physical_memory_read(addr
, 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 cpu_physical_memory_write(addr
, d
, sizeof *d
);
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 comp_delay
= s
->regs
[R_DMACR
] >> 24;
250 ptimer_stop(s
->ptimer
);
251 ptimer_set_count(s
->ptimer
, comp_delay
);
252 ptimer_run(s
->ptimer
, 1);
256 if (s
->complete_cnt
== 0) {
257 /* Raise the IOC irq. */
258 s
->regs
[R_DMASR
] |= DMASR_IOC_IRQ
;
259 stream_reload_complete_cnt(s
);
263 static void stream_process_mem2s(struct Stream
*s
, StreamSlave
*tx_data_dev
,
264 StreamSlave
*tx_control_dev
)
267 unsigned char txbuf
[16 * 1024];
270 if (!stream_running(s
) || stream_idle(s
)) {
275 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
277 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
278 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
282 if (stream_desc_sof(&s
->desc
)) {
284 stream_push(tx_control_dev
, s
->desc
.app
, sizeof(s
->desc
.app
));
287 txlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
288 if ((txlen
+ s
->pos
) > sizeof txbuf
) {
289 hw_error("%s: too small internal txbuf! %d\n", __func__
,
293 cpu_physical_memory_read(s
->desc
.buffer_address
,
294 txbuf
+ s
->pos
, txlen
);
297 if (stream_desc_eof(&s
->desc
)) {
298 stream_push(tx_data_dev
, txbuf
, s
->pos
);
303 /* Update the descriptor. */
304 s
->desc
.status
= txlen
| SDESC_STATUS_COMPLETE
;
305 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
308 prev_d
= s
->regs
[R_CURDESC
];
309 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
310 if (prev_d
== s
->regs
[R_TAILDESC
]) {
311 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
317 static size_t stream_process_s2mem(struct Stream
*s
, unsigned char *buf
,
325 if (!stream_running(s
) || stream_idle(s
)) {
330 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
332 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
333 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
337 rxlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
343 cpu_physical_memory_write(s
->desc
.buffer_address
, buf
+ pos
, rxlen
);
347 /* Update the descriptor. */
350 memcpy(s
->desc
.app
, s
->app
, sizeof(s
->desc
.app
));
351 s
->desc
.status
|= SDESC_STATUS_EOF
;
354 s
->desc
.status
|= sof
<< SDESC_STATUS_SOF_BIT
;
355 s
->desc
.status
|= SDESC_STATUS_COMPLETE
;
356 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
360 prev_d
= s
->regs
[R_CURDESC
];
361 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
362 if (prev_d
== s
->regs
[R_TAILDESC
]) {
363 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
371 static void xilinx_axidma_reset(DeviceState
*dev
)
374 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
376 for (i
= 0; i
< 2; i
++) {
377 stream_reset(&s
->streams
[i
]);
382 xilinx_axidma_control_stream_push(StreamSlave
*obj
, unsigned char *buf
,
385 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(obj
);
386 struct Stream
*s
= &cs
->dma
->streams
[1];
388 if (len
!= CONTROL_PAYLOAD_SIZE
) {
389 hw_error("AXI DMA requires %d byte control stream payload\n",
390 (int)CONTROL_PAYLOAD_SIZE
);
393 memcpy(s
->app
, buf
, len
);
398 xilinx_axidma_data_stream_can_push(StreamSlave
*obj
,
399 StreamCanPushNotifyFn notify
,
402 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
403 struct Stream
*s
= &ds
->dma
->streams
[1];
405 if (!stream_running(s
) || stream_idle(s
)) {
406 ds
->dma
->notify
= notify
;
407 ds
->dma
->notify_opaque
= notify_opaque
;
415 xilinx_axidma_data_stream_push(StreamSlave
*obj
, unsigned char *buf
, size_t len
)
417 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
418 struct Stream
*s
= &ds
->dma
->streams
[1];
421 ret
= stream_process_s2mem(s
, buf
, len
);
422 stream_update_irq(s
);
426 static uint64_t axidma_read(void *opaque
, hwaddr addr
,
429 XilinxAXIDMA
*d
= opaque
;
434 sid
= streamid_from_addr(addr
);
435 s
= &d
->streams
[sid
];
441 /* Simulate one cycles reset delay. */
442 s
->regs
[addr
] &= ~DMACR_RESET
;
446 s
->regs
[addr
] &= 0xffff;
447 s
->regs
[addr
] |= (s
->complete_cnt
& 0xff) << 16;
448 s
->regs
[addr
] |= (ptimer_get_count(s
->ptimer
) & 0xff) << 24;
453 D(qemu_log("%s ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
454 __func__
, sid
, addr
* 4, r
));
461 static void axidma_write(void *opaque
, hwaddr addr
,
462 uint64_t value
, unsigned size
)
464 XilinxAXIDMA
*d
= opaque
;
468 sid
= streamid_from_addr(addr
);
469 s
= &d
->streams
[sid
];
475 /* Tailptr mode is always on. */
476 value
|= DMACR_TAILPTR_MODE
;
477 /* Remember our previous reset state. */
478 value
|= (s
->regs
[addr
] & DMACR_RESET
);
479 s
->regs
[addr
] = value
;
481 if (value
& DMACR_RESET
) {
485 if ((value
& 1) && !stream_resetting(s
)) {
486 /* Start processing. */
487 s
->regs
[R_DMASR
] &= ~(DMASR_HALTED
| DMASR_IDLE
);
489 stream_reload_complete_cnt(s
);
493 /* Mask away write to clear irq lines. */
494 value
&= ~(value
& DMASR_IRQ_MASK
);
495 s
->regs
[addr
] = value
;
499 s
->regs
[addr
] = value
;
500 s
->regs
[R_DMASR
] &= ~DMASR_IDLE
; /* Not idle. */
502 stream_process_mem2s(s
, d
->tx_data_dev
, d
->tx_control_dev
);
506 D(qemu_log("%s: ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
507 __func__
, sid
, addr
* 4, (unsigned)value
));
508 s
->regs
[addr
] = value
;
511 if (sid
== 1 && d
->notify
) {
512 StreamCanPushNotifyFn notifytmp
= d
->notify
;
514 notifytmp(d
->notify_opaque
);
516 stream_update_irq(s
);
519 static const MemoryRegionOps axidma_ops
= {
521 .write
= axidma_write
,
522 .endianness
= DEVICE_NATIVE_ENDIAN
,
525 static void xilinx_axidma_realize(DeviceState
*dev
, Error
**errp
)
527 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
528 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(&s
->rx_data_dev
);
529 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(
531 Error
*local_err
= NULL
;
533 object_property_add_link(OBJECT(ds
), "dma", TYPE_XILINX_AXI_DMA
,
535 object_property_allow_set_link
,
536 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
538 object_property_add_link(OBJECT(cs
), "dma", TYPE_XILINX_AXI_DMA
,
540 object_property_allow_set_link
,
541 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
544 goto xilinx_axidma_realize_fail
;
546 object_property_set_link(OBJECT(ds
), OBJECT(s
), "dma", &local_err
);
547 object_property_set_link(OBJECT(cs
), OBJECT(s
), "dma", &local_err
);
549 goto xilinx_axidma_realize_fail
;
554 for (i
= 0; i
< 2; i
++) {
555 struct Stream
*st
= &s
->streams
[i
];
558 st
->bh
= qemu_bh_new(timer_hit
, st
);
559 st
->ptimer
= ptimer_init(st
->bh
);
560 ptimer_set_freq(st
->ptimer
, s
->freqhz
);
564 xilinx_axidma_realize_fail
:
570 static void xilinx_axidma_init(Object
*obj
)
572 XilinxAXIDMA
*s
= XILINX_AXI_DMA(obj
);
573 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
575 object_property_add_link(obj
, "axistream-connected", TYPE_STREAM_SLAVE
,
576 (Object
**)&s
->tx_data_dev
,
577 qdev_prop_allow_set_link_before_realize
,
578 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
580 object_property_add_link(obj
, "axistream-control-connected",
582 (Object
**)&s
->tx_control_dev
,
583 qdev_prop_allow_set_link_before_realize
,
584 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
587 object_initialize(&s
->rx_data_dev
, sizeof(s
->rx_data_dev
),
588 TYPE_XILINX_AXI_DMA_DATA_STREAM
);
589 object_initialize(&s
->rx_control_dev
, sizeof(s
->rx_control_dev
),
590 TYPE_XILINX_AXI_DMA_CONTROL_STREAM
);
591 object_property_add_child(OBJECT(s
), "axistream-connected-target",
592 (Object
*)&s
->rx_data_dev
, &error_abort
);
593 object_property_add_child(OBJECT(s
), "axistream-control-connected-target",
594 (Object
*)&s
->rx_control_dev
, &error_abort
);
596 sysbus_init_irq(sbd
, &s
->streams
[0].irq
);
597 sysbus_init_irq(sbd
, &s
->streams
[1].irq
);
599 memory_region_init_io(&s
->iomem
, obj
, &axidma_ops
, s
,
600 "xlnx.axi-dma", R_MAX
* 4 * 2);
601 sysbus_init_mmio(sbd
, &s
->iomem
);
604 static Property axidma_properties
[] = {
605 DEFINE_PROP_UINT32("freqhz", XilinxAXIDMA
, freqhz
, 50000000),
606 DEFINE_PROP_END_OF_LIST(),
609 static void axidma_class_init(ObjectClass
*klass
, void *data
)
611 DeviceClass
*dc
= DEVICE_CLASS(klass
);
613 dc
->realize
= xilinx_axidma_realize
,
614 dc
->reset
= xilinx_axidma_reset
;
615 dc
->props
= axidma_properties
;
618 static StreamSlaveClass xilinx_axidma_data_stream_class
= {
619 .push
= xilinx_axidma_data_stream_push
,
620 .can_push
= xilinx_axidma_data_stream_can_push
,
623 static StreamSlaveClass xilinx_axidma_control_stream_class
= {
624 .push
= xilinx_axidma_control_stream_push
,
627 static void xilinx_axidma_stream_class_init(ObjectClass
*klass
, void *data
)
629 StreamSlaveClass
*ssc
= STREAM_SLAVE_CLASS(klass
);
631 ssc
->push
= ((StreamSlaveClass
*)data
)->push
;
632 ssc
->can_push
= ((StreamSlaveClass
*)data
)->can_push
;
635 static const TypeInfo axidma_info
= {
636 .name
= TYPE_XILINX_AXI_DMA
,
637 .parent
= TYPE_SYS_BUS_DEVICE
,
638 .instance_size
= sizeof(XilinxAXIDMA
),
639 .class_init
= axidma_class_init
,
640 .instance_init
= xilinx_axidma_init
,
643 static const TypeInfo xilinx_axidma_data_stream_info
= {
644 .name
= TYPE_XILINX_AXI_DMA_DATA_STREAM
,
645 .parent
= TYPE_OBJECT
,
646 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
647 .class_init
= xilinx_axidma_stream_class_init
,
648 .class_data
= &xilinx_axidma_data_stream_class
,
649 .interfaces
= (InterfaceInfo
[]) {
650 { TYPE_STREAM_SLAVE
},
655 static const TypeInfo xilinx_axidma_control_stream_info
= {
656 .name
= TYPE_XILINX_AXI_DMA_CONTROL_STREAM
,
657 .parent
= TYPE_OBJECT
,
658 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
659 .class_init
= xilinx_axidma_stream_class_init
,
660 .class_data
= &xilinx_axidma_control_stream_class
,
661 .interfaces
= (InterfaceInfo
[]) {
662 { TYPE_STREAM_SLAVE
},
667 static void xilinx_axidma_register_types(void)
669 type_register_static(&axidma_info
);
670 type_register_static(&xilinx_axidma_data_stream_info
);
671 type_register_static(&xilinx_axidma_control_stream_info
);
674 type_init(xilinx_axidma_register_types
)