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 "hw/stream.h"
40 #define TYPE_XILINX_AXI_DMA "xlnx.axi-dma"
41 #define TYPE_XILINX_AXI_DMA_DATA_STREAM "xilinx-axi-dma-data-stream"
42 #define TYPE_XILINX_AXI_DMA_CONTROL_STREAM "xilinx-axi-dma-control-stream"
44 #define XILINX_AXI_DMA(obj) \
45 OBJECT_CHECK(XilinxAXIDMA, (obj), TYPE_XILINX_AXI_DMA)
47 #define XILINX_AXI_DMA_DATA_STREAM(obj) \
48 OBJECT_CHECK(XilinxAXIDMAStreamSlave, (obj),\
49 TYPE_XILINX_AXI_DMA_DATA_STREAM)
51 #define XILINX_AXI_DMA_CONTROL_STREAM(obj) \
52 OBJECT_CHECK(XilinxAXIDMAStreamSlave, (obj),\
53 TYPE_XILINX_AXI_DMA_CONTROL_STREAM)
55 #define R_DMACR (0x00 / 4)
56 #define R_DMASR (0x04 / 4)
57 #define R_CURDESC (0x08 / 4)
58 #define R_TAILDESC (0x10 / 4)
59 #define R_MAX (0x30 / 4)
61 #define CONTROL_PAYLOAD_WORDS 5
62 #define CONTROL_PAYLOAD_SIZE (CONTROL_PAYLOAD_WORDS * (sizeof(uint32_t)))
64 typedef struct XilinxAXIDMA XilinxAXIDMA
;
65 typedef struct XilinxAXIDMAStreamSlave XilinxAXIDMAStreamSlave
;
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 ptimer_state
*ptimer
;
113 unsigned int complete_cnt
;
114 uint32_t regs
[R_MAX
];
116 unsigned char txbuf
[16 * 1024];
119 struct XilinxAXIDMAStreamSlave
{
122 struct XilinxAXIDMA
*dma
;
125 struct XilinxAXIDMA
{
129 StreamSlave
*tx_data_dev
;
130 StreamSlave
*tx_control_dev
;
131 XilinxAXIDMAStreamSlave rx_data_dev
;
132 XilinxAXIDMAStreamSlave rx_control_dev
;
134 struct Stream streams
[2];
136 StreamCanPushNotifyFn notify
;
141 * Helper calls to extract info from descriptors and other trivial
144 static inline int stream_desc_sof(struct SDesc
*d
)
146 return d
->control
& SDESC_CTRL_SOF
;
149 static inline int stream_desc_eof(struct SDesc
*d
)
151 return d
->control
& SDESC_CTRL_EOF
;
154 static inline int stream_resetting(struct Stream
*s
)
156 return !!(s
->regs
[R_DMACR
] & DMACR_RESET
);
159 static inline int stream_running(struct Stream
*s
)
161 return s
->regs
[R_DMACR
] & DMACR_RUNSTOP
;
164 static inline int stream_idle(struct Stream
*s
)
166 return !!(s
->regs
[R_DMASR
] & DMASR_IDLE
);
169 static void stream_reset(struct Stream
*s
)
171 s
->regs
[R_DMASR
] = DMASR_HALTED
; /* starts up halted. */
172 s
->regs
[R_DMACR
] = 1 << 16; /* Starts with one in compl threshold. */
175 /* Map an offset addr into a channel index. */
176 static inline int streamid_from_addr(hwaddr addr
)
185 static void stream_desc_load(struct Stream
*s
, hwaddr addr
)
187 struct SDesc
*d
= &s
->desc
;
189 cpu_physical_memory_read(addr
, d
, sizeof *d
);
191 /* Convert from LE into host endianness. */
192 d
->buffer_address
= le64_to_cpu(d
->buffer_address
);
193 d
->nxtdesc
= le64_to_cpu(d
->nxtdesc
);
194 d
->control
= le32_to_cpu(d
->control
);
195 d
->status
= le32_to_cpu(d
->status
);
198 static void stream_desc_store(struct Stream
*s
, hwaddr addr
)
200 struct SDesc
*d
= &s
->desc
;
202 /* Convert from host endianness into LE. */
203 d
->buffer_address
= cpu_to_le64(d
->buffer_address
);
204 d
->nxtdesc
= cpu_to_le64(d
->nxtdesc
);
205 d
->control
= cpu_to_le32(d
->control
);
206 d
->status
= cpu_to_le32(d
->status
);
207 cpu_physical_memory_write(addr
, d
, sizeof *d
);
210 static void stream_update_irq(struct Stream
*s
)
212 unsigned int pending
, mask
, irq
;
214 pending
= s
->regs
[R_DMASR
] & DMASR_IRQ_MASK
;
215 mask
= s
->regs
[R_DMACR
] & DMASR_IRQ_MASK
;
217 irq
= pending
& mask
;
219 qemu_set_irq(s
->irq
, !!irq
);
222 static void stream_reload_complete_cnt(struct Stream
*s
)
224 unsigned int comp_th
;
225 comp_th
= (s
->regs
[R_DMACR
] >> 16) & 0xff;
226 s
->complete_cnt
= comp_th
;
229 static void timer_hit(void *opaque
)
231 struct Stream
*s
= opaque
;
233 stream_reload_complete_cnt(s
);
234 s
->regs
[R_DMASR
] |= DMASR_DLY_IRQ
;
235 stream_update_irq(s
);
238 static void stream_complete(struct Stream
*s
)
240 unsigned int comp_delay
;
242 /* Start the delayed timer. */
243 ptimer_transaction_begin(s
->ptimer
);
244 comp_delay
= s
->regs
[R_DMACR
] >> 24;
246 ptimer_stop(s
->ptimer
);
247 ptimer_set_count(s
->ptimer
, comp_delay
);
248 ptimer_run(s
->ptimer
, 1);
252 if (s
->complete_cnt
== 0) {
253 /* Raise the IOC irq. */
254 s
->regs
[R_DMASR
] |= DMASR_IOC_IRQ
;
255 stream_reload_complete_cnt(s
);
257 ptimer_transaction_commit(s
->ptimer
);
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
->ptimer
= ptimer_init(timer_hit
, st
, PTIMER_POLICY_DEFAULT
);
555 ptimer_transaction_begin(st
->ptimer
);
556 ptimer_set_freq(st
->ptimer
, s
->freqhz
);
557 ptimer_transaction_commit(st
->ptimer
);
561 xilinx_axidma_realize_fail
:
562 error_propagate(errp
, local_err
);
565 static void xilinx_axidma_init(Object
*obj
)
567 XilinxAXIDMA
*s
= XILINX_AXI_DMA(obj
);
568 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
570 object_initialize_child(OBJECT(s
), "axistream-connected-target",
571 &s
->rx_data_dev
, sizeof(s
->rx_data_dev
),
572 TYPE_XILINX_AXI_DMA_DATA_STREAM
, &error_abort
,
574 object_initialize_child(OBJECT(s
), "axistream-control-connected-target",
575 &s
->rx_control_dev
, sizeof(s
->rx_control_dev
),
576 TYPE_XILINX_AXI_DMA_CONTROL_STREAM
, &error_abort
,
579 sysbus_init_irq(sbd
, &s
->streams
[0].irq
);
580 sysbus_init_irq(sbd
, &s
->streams
[1].irq
);
582 memory_region_init_io(&s
->iomem
, obj
, &axidma_ops
, s
,
583 "xlnx.axi-dma", R_MAX
* 4 * 2);
584 sysbus_init_mmio(sbd
, &s
->iomem
);
587 static Property axidma_properties
[] = {
588 DEFINE_PROP_UINT32("freqhz", XilinxAXIDMA
, freqhz
, 50000000),
589 DEFINE_PROP_LINK("axistream-connected", XilinxAXIDMA
,
590 tx_data_dev
, TYPE_STREAM_SLAVE
, StreamSlave
*),
591 DEFINE_PROP_LINK("axistream-control-connected", XilinxAXIDMA
,
592 tx_control_dev
, TYPE_STREAM_SLAVE
, StreamSlave
*),
593 DEFINE_PROP_END_OF_LIST(),
596 static void axidma_class_init(ObjectClass
*klass
, void *data
)
598 DeviceClass
*dc
= DEVICE_CLASS(klass
);
600 dc
->realize
= xilinx_axidma_realize
,
601 dc
->reset
= xilinx_axidma_reset
;
602 device_class_set_props(dc
, axidma_properties
);
605 static StreamSlaveClass xilinx_axidma_data_stream_class
= {
606 .push
= xilinx_axidma_data_stream_push
,
607 .can_push
= xilinx_axidma_data_stream_can_push
,
610 static StreamSlaveClass xilinx_axidma_control_stream_class
= {
611 .push
= xilinx_axidma_control_stream_push
,
614 static void xilinx_axidma_stream_class_init(ObjectClass
*klass
, void *data
)
616 StreamSlaveClass
*ssc
= STREAM_SLAVE_CLASS(klass
);
618 ssc
->push
= ((StreamSlaveClass
*)data
)->push
;
619 ssc
->can_push
= ((StreamSlaveClass
*)data
)->can_push
;
622 static const TypeInfo axidma_info
= {
623 .name
= TYPE_XILINX_AXI_DMA
,
624 .parent
= TYPE_SYS_BUS_DEVICE
,
625 .instance_size
= sizeof(XilinxAXIDMA
),
626 .class_init
= axidma_class_init
,
627 .instance_init
= xilinx_axidma_init
,
630 static const TypeInfo xilinx_axidma_data_stream_info
= {
631 .name
= TYPE_XILINX_AXI_DMA_DATA_STREAM
,
632 .parent
= TYPE_OBJECT
,
633 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
634 .class_init
= xilinx_axidma_stream_class_init
,
635 .class_data
= &xilinx_axidma_data_stream_class
,
636 .interfaces
= (InterfaceInfo
[]) {
637 { TYPE_STREAM_SLAVE
},
642 static const TypeInfo xilinx_axidma_control_stream_info
= {
643 .name
= TYPE_XILINX_AXI_DMA_CONTROL_STREAM
,
644 .parent
= TYPE_OBJECT
,
645 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
646 .class_init
= xilinx_axidma_stream_class_init
,
647 .class_data
= &xilinx_axidma_control_stream_class
,
648 .interfaces
= (InterfaceInfo
[]) {
649 { TYPE_STREAM_SLAVE
},
654 static void xilinx_axidma_register_types(void)
656 type_register_static(&axidma_info
);
657 type_register_static(&xilinx_axidma_data_stream_info
);
658 type_register_static(&xilinx_axidma_control_stream_info
);
661 type_init(xilinx_axidma_register_types
)