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/main-loop.h"
35 #include "qemu/module.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)
108 ptimer_state
*ptimer
;
115 unsigned int complete_cnt
;
116 uint32_t regs
[R_MAX
];
118 unsigned char txbuf
[16 * 1024];
121 struct XilinxAXIDMAStreamSlave
{
124 struct XilinxAXIDMA
*dma
;
127 struct XilinxAXIDMA
{
131 StreamSlave
*tx_data_dev
;
132 StreamSlave
*tx_control_dev
;
133 XilinxAXIDMAStreamSlave rx_data_dev
;
134 XilinxAXIDMAStreamSlave rx_control_dev
;
136 struct Stream streams
[2];
138 StreamCanPushNotifyFn notify
;
143 * Helper calls to extract info from descriptors and other trivial
146 static inline int stream_desc_sof(struct SDesc
*d
)
148 return d
->control
& SDESC_CTRL_SOF
;
151 static inline int stream_desc_eof(struct SDesc
*d
)
153 return d
->control
& SDESC_CTRL_EOF
;
156 static inline int stream_resetting(struct Stream
*s
)
158 return !!(s
->regs
[R_DMACR
] & DMACR_RESET
);
161 static inline int stream_running(struct Stream
*s
)
163 return s
->regs
[R_DMACR
] & DMACR_RUNSTOP
;
166 static inline int stream_idle(struct Stream
*s
)
168 return !!(s
->regs
[R_DMASR
] & DMASR_IDLE
);
171 static void stream_reset(struct Stream
*s
)
173 s
->regs
[R_DMASR
] = DMASR_HALTED
; /* starts up halted. */
174 s
->regs
[R_DMACR
] = 1 << 16; /* Starts with one in compl threshold. */
177 /* Map an offset addr into a channel index. */
178 static inline int streamid_from_addr(hwaddr addr
)
187 static void stream_desc_load(struct Stream
*s
, hwaddr addr
)
189 struct SDesc
*d
= &s
->desc
;
191 cpu_physical_memory_read(addr
, d
, sizeof *d
);
193 /* Convert from LE into host endianness. */
194 d
->buffer_address
= le64_to_cpu(d
->buffer_address
);
195 d
->nxtdesc
= le64_to_cpu(d
->nxtdesc
);
196 d
->control
= le32_to_cpu(d
->control
);
197 d
->status
= le32_to_cpu(d
->status
);
200 static void stream_desc_store(struct Stream
*s
, hwaddr addr
)
202 struct SDesc
*d
= &s
->desc
;
204 /* Convert from host endianness into LE. */
205 d
->buffer_address
= cpu_to_le64(d
->buffer_address
);
206 d
->nxtdesc
= cpu_to_le64(d
->nxtdesc
);
207 d
->control
= cpu_to_le32(d
->control
);
208 d
->status
= cpu_to_le32(d
->status
);
209 cpu_physical_memory_write(addr
, d
, sizeof *d
);
212 static void stream_update_irq(struct Stream
*s
)
214 unsigned int pending
, mask
, irq
;
216 pending
= s
->regs
[R_DMASR
] & DMASR_IRQ_MASK
;
217 mask
= s
->regs
[R_DMACR
] & DMASR_IRQ_MASK
;
219 irq
= pending
& mask
;
221 qemu_set_irq(s
->irq
, !!irq
);
224 static void stream_reload_complete_cnt(struct Stream
*s
)
226 unsigned int comp_th
;
227 comp_th
= (s
->regs
[R_DMACR
] >> 16) & 0xff;
228 s
->complete_cnt
= comp_th
;
231 static void timer_hit(void *opaque
)
233 struct Stream
*s
= opaque
;
235 stream_reload_complete_cnt(s
);
236 s
->regs
[R_DMASR
] |= DMASR_DLY_IRQ
;
237 stream_update_irq(s
);
240 static void stream_complete(struct Stream
*s
)
242 unsigned int comp_delay
;
244 /* Start the delayed timer. */
245 comp_delay
= s
->regs
[R_DMACR
] >> 24;
247 ptimer_stop(s
->ptimer
);
248 ptimer_set_count(s
->ptimer
, comp_delay
);
249 ptimer_run(s
->ptimer
, 1);
253 if (s
->complete_cnt
== 0) {
254 /* Raise the IOC irq. */
255 s
->regs
[R_DMASR
] |= DMASR_IOC_IRQ
;
256 stream_reload_complete_cnt(s
);
260 static void stream_process_mem2s(struct Stream
*s
, StreamSlave
*tx_data_dev
,
261 StreamSlave
*tx_control_dev
)
266 if (!stream_running(s
) || stream_idle(s
)) {
271 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
273 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
274 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
278 if (stream_desc_sof(&s
->desc
)) {
280 stream_push(tx_control_dev
, s
->desc
.app
, sizeof(s
->desc
.app
));
283 txlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
284 if ((txlen
+ s
->pos
) > sizeof s
->txbuf
) {
285 hw_error("%s: too small internal txbuf! %d\n", __func__
,
289 cpu_physical_memory_read(s
->desc
.buffer_address
,
290 s
->txbuf
+ s
->pos
, txlen
);
293 if (stream_desc_eof(&s
->desc
)) {
294 stream_push(tx_data_dev
, s
->txbuf
, s
->pos
);
299 /* Update the descriptor. */
300 s
->desc
.status
= txlen
| SDESC_STATUS_COMPLETE
;
301 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
304 prev_d
= s
->regs
[R_CURDESC
];
305 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
306 if (prev_d
== s
->regs
[R_TAILDESC
]) {
307 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
313 static size_t stream_process_s2mem(struct Stream
*s
, unsigned char *buf
,
321 if (!stream_running(s
) || stream_idle(s
)) {
326 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
328 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
329 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
333 rxlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
339 cpu_physical_memory_write(s
->desc
.buffer_address
, buf
+ pos
, rxlen
);
343 /* Update the descriptor. */
346 memcpy(s
->desc
.app
, s
->app
, sizeof(s
->desc
.app
));
347 s
->desc
.status
|= SDESC_STATUS_EOF
;
350 s
->desc
.status
|= sof
<< SDESC_STATUS_SOF_BIT
;
351 s
->desc
.status
|= SDESC_STATUS_COMPLETE
;
352 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
356 prev_d
= s
->regs
[R_CURDESC
];
357 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
358 if (prev_d
== s
->regs
[R_TAILDESC
]) {
359 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
367 static void xilinx_axidma_reset(DeviceState
*dev
)
370 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
372 for (i
= 0; i
< 2; i
++) {
373 stream_reset(&s
->streams
[i
]);
378 xilinx_axidma_control_stream_push(StreamSlave
*obj
, unsigned char *buf
,
381 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(obj
);
382 struct Stream
*s
= &cs
->dma
->streams
[1];
384 if (len
!= CONTROL_PAYLOAD_SIZE
) {
385 hw_error("AXI DMA requires %d byte control stream payload\n",
386 (int)CONTROL_PAYLOAD_SIZE
);
389 memcpy(s
->app
, buf
, len
);
394 xilinx_axidma_data_stream_can_push(StreamSlave
*obj
,
395 StreamCanPushNotifyFn notify
,
398 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
399 struct Stream
*s
= &ds
->dma
->streams
[1];
401 if (!stream_running(s
) || stream_idle(s
)) {
402 ds
->dma
->notify
= notify
;
403 ds
->dma
->notify_opaque
= notify_opaque
;
411 xilinx_axidma_data_stream_push(StreamSlave
*obj
, unsigned char *buf
, size_t len
)
413 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
414 struct Stream
*s
= &ds
->dma
->streams
[1];
417 ret
= stream_process_s2mem(s
, buf
, len
);
418 stream_update_irq(s
);
422 static uint64_t axidma_read(void *opaque
, hwaddr addr
,
425 XilinxAXIDMA
*d
= opaque
;
430 sid
= streamid_from_addr(addr
);
431 s
= &d
->streams
[sid
];
437 /* Simulate one cycles reset delay. */
438 s
->regs
[addr
] &= ~DMACR_RESET
;
442 s
->regs
[addr
] &= 0xffff;
443 s
->regs
[addr
] |= (s
->complete_cnt
& 0xff) << 16;
444 s
->regs
[addr
] |= (ptimer_get_count(s
->ptimer
) & 0xff) << 24;
449 D(qemu_log("%s ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
450 __func__
, sid
, addr
* 4, r
));
457 static void axidma_write(void *opaque
, hwaddr addr
,
458 uint64_t value
, unsigned size
)
460 XilinxAXIDMA
*d
= opaque
;
464 sid
= streamid_from_addr(addr
);
465 s
= &d
->streams
[sid
];
471 /* Tailptr mode is always on. */
472 value
|= DMACR_TAILPTR_MODE
;
473 /* Remember our previous reset state. */
474 value
|= (s
->regs
[addr
] & DMACR_RESET
);
475 s
->regs
[addr
] = value
;
477 if (value
& DMACR_RESET
) {
481 if ((value
& 1) && !stream_resetting(s
)) {
482 /* Start processing. */
483 s
->regs
[R_DMASR
] &= ~(DMASR_HALTED
| DMASR_IDLE
);
485 stream_reload_complete_cnt(s
);
489 /* Mask away write to clear irq lines. */
490 value
&= ~(value
& DMASR_IRQ_MASK
);
491 s
->regs
[addr
] = value
;
495 s
->regs
[addr
] = value
;
496 s
->regs
[R_DMASR
] &= ~DMASR_IDLE
; /* Not idle. */
498 stream_process_mem2s(s
, d
->tx_data_dev
, d
->tx_control_dev
);
502 D(qemu_log("%s: ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
503 __func__
, sid
, addr
* 4, (unsigned)value
));
504 s
->regs
[addr
] = value
;
507 if (sid
== 1 && d
->notify
) {
508 StreamCanPushNotifyFn notifytmp
= d
->notify
;
510 notifytmp(d
->notify_opaque
);
512 stream_update_irq(s
);
515 static const MemoryRegionOps axidma_ops
= {
517 .write
= axidma_write
,
518 .endianness
= DEVICE_NATIVE_ENDIAN
,
521 static void xilinx_axidma_realize(DeviceState
*dev
, Error
**errp
)
523 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
524 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(&s
->rx_data_dev
);
525 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(
527 Error
*local_err
= NULL
;
529 object_property_add_link(OBJECT(ds
), "dma", TYPE_XILINX_AXI_DMA
,
531 object_property_allow_set_link
,
532 OBJ_PROP_LINK_STRONG
,
534 object_property_add_link(OBJECT(cs
), "dma", TYPE_XILINX_AXI_DMA
,
536 object_property_allow_set_link
,
537 OBJ_PROP_LINK_STRONG
,
540 goto xilinx_axidma_realize_fail
;
542 object_property_set_link(OBJECT(ds
), OBJECT(s
), "dma", &local_err
);
543 object_property_set_link(OBJECT(cs
), OBJECT(s
), "dma", &local_err
);
545 goto xilinx_axidma_realize_fail
;
550 for (i
= 0; i
< 2; i
++) {
551 struct Stream
*st
= &s
->streams
[i
];
554 st
->bh
= qemu_bh_new(timer_hit
, st
);
555 st
->ptimer
= ptimer_init_with_bh(st
->bh
, PTIMER_POLICY_DEFAULT
);
556 ptimer_set_freq(st
->ptimer
, s
->freqhz
);
560 xilinx_axidma_realize_fail
:
561 error_propagate(errp
, local_err
);
564 static void xilinx_axidma_init(Object
*obj
)
566 XilinxAXIDMA
*s
= XILINX_AXI_DMA(obj
);
567 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
569 object_initialize_child(OBJECT(s
), "axistream-connected-target",
570 &s
->rx_data_dev
, sizeof(s
->rx_data_dev
),
571 TYPE_XILINX_AXI_DMA_DATA_STREAM
, &error_abort
,
573 object_initialize_child(OBJECT(s
), "axistream-control-connected-target",
574 &s
->rx_control_dev
, sizeof(s
->rx_control_dev
),
575 TYPE_XILINX_AXI_DMA_CONTROL_STREAM
, &error_abort
,
578 sysbus_init_irq(sbd
, &s
->streams
[0].irq
);
579 sysbus_init_irq(sbd
, &s
->streams
[1].irq
);
581 memory_region_init_io(&s
->iomem
, obj
, &axidma_ops
, s
,
582 "xlnx.axi-dma", R_MAX
* 4 * 2);
583 sysbus_init_mmio(sbd
, &s
->iomem
);
586 static Property axidma_properties
[] = {
587 DEFINE_PROP_UINT32("freqhz", XilinxAXIDMA
, freqhz
, 50000000),
588 DEFINE_PROP_LINK("axistream-connected", XilinxAXIDMA
,
589 tx_data_dev
, TYPE_STREAM_SLAVE
, StreamSlave
*),
590 DEFINE_PROP_LINK("axistream-control-connected", XilinxAXIDMA
,
591 tx_control_dev
, TYPE_STREAM_SLAVE
, StreamSlave
*),
592 DEFINE_PROP_END_OF_LIST(),
595 static void axidma_class_init(ObjectClass
*klass
, void *data
)
597 DeviceClass
*dc
= DEVICE_CLASS(klass
);
599 dc
->realize
= xilinx_axidma_realize
,
600 dc
->reset
= xilinx_axidma_reset
;
601 dc
->props
= axidma_properties
;
604 static StreamSlaveClass xilinx_axidma_data_stream_class
= {
605 .push
= xilinx_axidma_data_stream_push
,
606 .can_push
= xilinx_axidma_data_stream_can_push
,
609 static StreamSlaveClass xilinx_axidma_control_stream_class
= {
610 .push
= xilinx_axidma_control_stream_push
,
613 static void xilinx_axidma_stream_class_init(ObjectClass
*klass
, void *data
)
615 StreamSlaveClass
*ssc
= STREAM_SLAVE_CLASS(klass
);
617 ssc
->push
= ((StreamSlaveClass
*)data
)->push
;
618 ssc
->can_push
= ((StreamSlaveClass
*)data
)->can_push
;
621 static const TypeInfo axidma_info
= {
622 .name
= TYPE_XILINX_AXI_DMA
,
623 .parent
= TYPE_SYS_BUS_DEVICE
,
624 .instance_size
= sizeof(XilinxAXIDMA
),
625 .class_init
= axidma_class_init
,
626 .instance_init
= xilinx_axidma_init
,
629 static const TypeInfo xilinx_axidma_data_stream_info
= {
630 .name
= TYPE_XILINX_AXI_DMA_DATA_STREAM
,
631 .parent
= TYPE_OBJECT
,
632 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
633 .class_init
= xilinx_axidma_stream_class_init
,
634 .class_data
= &xilinx_axidma_data_stream_class
,
635 .interfaces
= (InterfaceInfo
[]) {
636 { TYPE_STREAM_SLAVE
},
641 static const TypeInfo xilinx_axidma_control_stream_info
= {
642 .name
= TYPE_XILINX_AXI_DMA_CONTROL_STREAM
,
643 .parent
= TYPE_OBJECT
,
644 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
645 .class_init
= xilinx_axidma_stream_class_init
,
646 .class_data
= &xilinx_axidma_control_stream_class
,
647 .interfaces
= (InterfaceInfo
[]) {
648 { TYPE_STREAM_SLAVE
},
653 static void xilinx_axidma_register_types(void)
655 type_register_static(&axidma_info
);
656 type_register_static(&xilinx_axidma_data_stream_info
);
657 type_register_static(&xilinx_axidma_control_stream_info
);
660 type_init(xilinx_axidma_register_types
)