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"
29 #include "hw/ptimer.h"
31 #include "qemu/main-loop.h"
33 #include "hw/stream.h"
37 #define TYPE_XILINX_AXI_DMA "xlnx.axi-dma"
38 #define TYPE_XILINX_AXI_DMA_DATA_STREAM "xilinx-axi-dma-data-stream"
39 #define TYPE_XILINX_AXI_DMA_CONTROL_STREAM "xilinx-axi-dma-control-stream"
41 #define XILINX_AXI_DMA(obj) \
42 OBJECT_CHECK(XilinxAXIDMA, (obj), TYPE_XILINX_AXI_DMA)
44 #define XILINX_AXI_DMA_DATA_STREAM(obj) \
45 OBJECT_CHECK(XilinxAXIDMAStreamSlave, (obj),\
46 TYPE_XILINX_AXI_DMA_DATA_STREAM)
48 #define XILINX_AXI_DMA_CONTROL_STREAM(obj) \
49 OBJECT_CHECK(XilinxAXIDMAStreamSlave, (obj),\
50 TYPE_XILINX_AXI_DMA_CONTROL_STREAM)
52 #define R_DMACR (0x00 / 4)
53 #define R_DMASR (0x04 / 4)
54 #define R_CURDESC (0x08 / 4)
55 #define R_TAILDESC (0x10 / 4)
56 #define R_MAX (0x30 / 4)
58 #define CONTROL_PAYLOAD_WORDS 5
59 #define CONTROL_PAYLOAD_SIZE (CONTROL_PAYLOAD_WORDS * (sizeof(uint32_t)))
61 typedef struct XilinxAXIDMA XilinxAXIDMA
;
62 typedef struct XilinxAXIDMAStreamSlave XilinxAXIDMAStreamSlave
;
66 DMACR_TAILPTR_MODE
= 2,
73 DMASR_IOC_IRQ
= 1 << 12,
74 DMASR_DLY_IRQ
= 1 << 13,
76 DMASR_IRQ_MASK
= 7 << 12
81 uint64_t buffer_address
;
85 uint8_t app
[CONTROL_PAYLOAD_SIZE
];
89 SDESC_CTRL_EOF
= (1 << 26),
90 SDESC_CTRL_SOF
= (1 << 27),
92 SDESC_CTRL_LEN_MASK
= (1 << 23) - 1
96 SDESC_STATUS_EOF
= (1 << 26),
97 SDESC_STATUS_SOF_BIT
= 27,
98 SDESC_STATUS_SOF
= (1 << SDESC_STATUS_SOF_BIT
),
99 SDESC_STATUS_COMPLETE
= (1 << 31)
104 ptimer_state
*ptimer
;
111 unsigned int complete_cnt
;
112 uint32_t regs
[R_MAX
];
114 unsigned char txbuf
[16 * 1024];
117 struct XilinxAXIDMAStreamSlave
{
120 struct XilinxAXIDMA
*dma
;
123 struct XilinxAXIDMA
{
127 StreamSlave
*tx_data_dev
;
128 StreamSlave
*tx_control_dev
;
129 XilinxAXIDMAStreamSlave rx_data_dev
;
130 XilinxAXIDMAStreamSlave rx_control_dev
;
132 struct Stream streams
[2];
134 StreamCanPushNotifyFn notify
;
139 * Helper calls to extract info from descriptors and other trivial
142 static inline int stream_desc_sof(struct SDesc
*d
)
144 return d
->control
& SDESC_CTRL_SOF
;
147 static inline int stream_desc_eof(struct SDesc
*d
)
149 return d
->control
& SDESC_CTRL_EOF
;
152 static inline int stream_resetting(struct Stream
*s
)
154 return !!(s
->regs
[R_DMACR
] & DMACR_RESET
);
157 static inline int stream_running(struct Stream
*s
)
159 return s
->regs
[R_DMACR
] & DMACR_RUNSTOP
;
162 static inline int stream_idle(struct Stream
*s
)
164 return !!(s
->regs
[R_DMASR
] & DMASR_IDLE
);
167 static void stream_reset(struct Stream
*s
)
169 s
->regs
[R_DMASR
] = DMASR_HALTED
; /* starts up halted. */
170 s
->regs
[R_DMACR
] = 1 << 16; /* Starts with one in compl threshold. */
173 /* Map an offset addr into a channel index. */
174 static inline int streamid_from_addr(hwaddr addr
)
183 static void stream_desc_load(struct Stream
*s
, hwaddr addr
)
185 struct SDesc
*d
= &s
->desc
;
187 cpu_physical_memory_read(addr
, d
, sizeof *d
);
189 /* Convert from LE into host endianness. */
190 d
->buffer_address
= le64_to_cpu(d
->buffer_address
);
191 d
->nxtdesc
= le64_to_cpu(d
->nxtdesc
);
192 d
->control
= le32_to_cpu(d
->control
);
193 d
->status
= le32_to_cpu(d
->status
);
196 static void stream_desc_store(struct Stream
*s
, hwaddr addr
)
198 struct SDesc
*d
= &s
->desc
;
200 /* Convert from host endianness into LE. */
201 d
->buffer_address
= cpu_to_le64(d
->buffer_address
);
202 d
->nxtdesc
= cpu_to_le64(d
->nxtdesc
);
203 d
->control
= cpu_to_le32(d
->control
);
204 d
->status
= cpu_to_le32(d
->status
);
205 cpu_physical_memory_write(addr
, d
, sizeof *d
);
208 static void stream_update_irq(struct Stream
*s
)
210 unsigned int pending
, mask
, irq
;
212 pending
= s
->regs
[R_DMASR
] & DMASR_IRQ_MASK
;
213 mask
= s
->regs
[R_DMACR
] & DMASR_IRQ_MASK
;
215 irq
= pending
& mask
;
217 qemu_set_irq(s
->irq
, !!irq
);
220 static void stream_reload_complete_cnt(struct Stream
*s
)
222 unsigned int comp_th
;
223 comp_th
= (s
->regs
[R_DMACR
] >> 16) & 0xff;
224 s
->complete_cnt
= comp_th
;
227 static void timer_hit(void *opaque
)
229 struct Stream
*s
= opaque
;
231 stream_reload_complete_cnt(s
);
232 s
->regs
[R_DMASR
] |= DMASR_DLY_IRQ
;
233 stream_update_irq(s
);
236 static void stream_complete(struct Stream
*s
)
238 unsigned int comp_delay
;
240 /* Start the delayed timer. */
241 comp_delay
= s
->regs
[R_DMACR
] >> 24;
243 ptimer_stop(s
->ptimer
);
244 ptimer_set_count(s
->ptimer
, comp_delay
);
245 ptimer_run(s
->ptimer
, 1);
249 if (s
->complete_cnt
== 0) {
250 /* Raise the IOC irq. */
251 s
->regs
[R_DMASR
] |= DMASR_IOC_IRQ
;
252 stream_reload_complete_cnt(s
);
256 static void stream_process_mem2s(struct Stream
*s
, StreamSlave
*tx_data_dev
,
257 StreamSlave
*tx_control_dev
)
262 if (!stream_running(s
) || stream_idle(s
)) {
267 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
269 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
270 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
274 if (stream_desc_sof(&s
->desc
)) {
276 stream_push(tx_control_dev
, s
->desc
.app
, sizeof(s
->desc
.app
));
279 txlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
280 if ((txlen
+ s
->pos
) > sizeof s
->txbuf
) {
281 hw_error("%s: too small internal txbuf! %d\n", __func__
,
285 cpu_physical_memory_read(s
->desc
.buffer_address
,
286 s
->txbuf
+ s
->pos
, txlen
);
289 if (stream_desc_eof(&s
->desc
)) {
290 stream_push(tx_data_dev
, s
->txbuf
, s
->pos
);
295 /* Update the descriptor. */
296 s
->desc
.status
= txlen
| SDESC_STATUS_COMPLETE
;
297 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
300 prev_d
= s
->regs
[R_CURDESC
];
301 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
302 if (prev_d
== s
->regs
[R_TAILDESC
]) {
303 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
309 static size_t stream_process_s2mem(struct Stream
*s
, unsigned char *buf
,
317 if (!stream_running(s
) || stream_idle(s
)) {
322 stream_desc_load(s
, s
->regs
[R_CURDESC
]);
324 if (s
->desc
.status
& SDESC_STATUS_COMPLETE
) {
325 s
->regs
[R_DMASR
] |= DMASR_HALTED
;
329 rxlen
= s
->desc
.control
& SDESC_CTRL_LEN_MASK
;
335 cpu_physical_memory_write(s
->desc
.buffer_address
, buf
+ pos
, rxlen
);
339 /* Update the descriptor. */
342 memcpy(s
->desc
.app
, s
->app
, sizeof(s
->desc
.app
));
343 s
->desc
.status
|= SDESC_STATUS_EOF
;
346 s
->desc
.status
|= sof
<< SDESC_STATUS_SOF_BIT
;
347 s
->desc
.status
|= SDESC_STATUS_COMPLETE
;
348 stream_desc_store(s
, s
->regs
[R_CURDESC
]);
352 prev_d
= s
->regs
[R_CURDESC
];
353 s
->regs
[R_CURDESC
] = s
->desc
.nxtdesc
;
354 if (prev_d
== s
->regs
[R_TAILDESC
]) {
355 s
->regs
[R_DMASR
] |= DMASR_IDLE
;
363 static void xilinx_axidma_reset(DeviceState
*dev
)
366 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
368 for (i
= 0; i
< 2; i
++) {
369 stream_reset(&s
->streams
[i
]);
374 xilinx_axidma_control_stream_push(StreamSlave
*obj
, unsigned char *buf
,
377 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(obj
);
378 struct Stream
*s
= &cs
->dma
->streams
[1];
380 if (len
!= CONTROL_PAYLOAD_SIZE
) {
381 hw_error("AXI DMA requires %d byte control stream payload\n",
382 (int)CONTROL_PAYLOAD_SIZE
);
385 memcpy(s
->app
, buf
, len
);
390 xilinx_axidma_data_stream_can_push(StreamSlave
*obj
,
391 StreamCanPushNotifyFn notify
,
394 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
395 struct Stream
*s
= &ds
->dma
->streams
[1];
397 if (!stream_running(s
) || stream_idle(s
)) {
398 ds
->dma
->notify
= notify
;
399 ds
->dma
->notify_opaque
= notify_opaque
;
407 xilinx_axidma_data_stream_push(StreamSlave
*obj
, unsigned char *buf
, size_t len
)
409 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(obj
);
410 struct Stream
*s
= &ds
->dma
->streams
[1];
413 ret
= stream_process_s2mem(s
, buf
, len
);
414 stream_update_irq(s
);
418 static uint64_t axidma_read(void *opaque
, hwaddr addr
,
421 XilinxAXIDMA
*d
= opaque
;
426 sid
= streamid_from_addr(addr
);
427 s
= &d
->streams
[sid
];
433 /* Simulate one cycles reset delay. */
434 s
->regs
[addr
] &= ~DMACR_RESET
;
438 s
->regs
[addr
] &= 0xffff;
439 s
->regs
[addr
] |= (s
->complete_cnt
& 0xff) << 16;
440 s
->regs
[addr
] |= (ptimer_get_count(s
->ptimer
) & 0xff) << 24;
445 D(qemu_log("%s ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
446 __func__
, sid
, addr
* 4, r
));
453 static void axidma_write(void *opaque
, hwaddr addr
,
454 uint64_t value
, unsigned size
)
456 XilinxAXIDMA
*d
= opaque
;
460 sid
= streamid_from_addr(addr
);
461 s
= &d
->streams
[sid
];
467 /* Tailptr mode is always on. */
468 value
|= DMACR_TAILPTR_MODE
;
469 /* Remember our previous reset state. */
470 value
|= (s
->regs
[addr
] & DMACR_RESET
);
471 s
->regs
[addr
] = value
;
473 if (value
& DMACR_RESET
) {
477 if ((value
& 1) && !stream_resetting(s
)) {
478 /* Start processing. */
479 s
->regs
[R_DMASR
] &= ~(DMASR_HALTED
| DMASR_IDLE
);
481 stream_reload_complete_cnt(s
);
485 /* Mask away write to clear irq lines. */
486 value
&= ~(value
& DMASR_IRQ_MASK
);
487 s
->regs
[addr
] = value
;
491 s
->regs
[addr
] = value
;
492 s
->regs
[R_DMASR
] &= ~DMASR_IDLE
; /* Not idle. */
494 stream_process_mem2s(s
, d
->tx_data_dev
, d
->tx_control_dev
);
498 D(qemu_log("%s: ch=%d addr=" TARGET_FMT_plx
" v=%x\n",
499 __func__
, sid
, addr
* 4, (unsigned)value
));
500 s
->regs
[addr
] = value
;
503 if (sid
== 1 && d
->notify
) {
504 StreamCanPushNotifyFn notifytmp
= d
->notify
;
506 notifytmp(d
->notify_opaque
);
508 stream_update_irq(s
);
511 static const MemoryRegionOps axidma_ops
= {
513 .write
= axidma_write
,
514 .endianness
= DEVICE_NATIVE_ENDIAN
,
517 static void xilinx_axidma_realize(DeviceState
*dev
, Error
**errp
)
519 XilinxAXIDMA
*s
= XILINX_AXI_DMA(dev
);
520 XilinxAXIDMAStreamSlave
*ds
= XILINX_AXI_DMA_DATA_STREAM(&s
->rx_data_dev
);
521 XilinxAXIDMAStreamSlave
*cs
= XILINX_AXI_DMA_CONTROL_STREAM(
523 Error
*local_err
= NULL
;
525 object_property_add_link(OBJECT(ds
), "dma", TYPE_XILINX_AXI_DMA
,
527 object_property_allow_set_link
,
528 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
530 object_property_add_link(OBJECT(cs
), "dma", TYPE_XILINX_AXI_DMA
,
532 object_property_allow_set_link
,
533 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
536 goto xilinx_axidma_realize_fail
;
538 object_property_set_link(OBJECT(ds
), OBJECT(s
), "dma", &local_err
);
539 object_property_set_link(OBJECT(cs
), OBJECT(s
), "dma", &local_err
);
541 goto xilinx_axidma_realize_fail
;
546 for (i
= 0; i
< 2; i
++) {
547 struct Stream
*st
= &s
->streams
[i
];
550 st
->bh
= qemu_bh_new(timer_hit
, st
);
551 st
->ptimer
= ptimer_init(st
->bh
, PTIMER_POLICY_DEFAULT
);
552 ptimer_set_freq(st
->ptimer
, s
->freqhz
);
556 xilinx_axidma_realize_fail
:
557 error_propagate(errp
, local_err
);
560 static void xilinx_axidma_init(Object
*obj
)
562 XilinxAXIDMA
*s
= XILINX_AXI_DMA(obj
);
563 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
565 object_initialize(&s
->rx_data_dev
, sizeof(s
->rx_data_dev
),
566 TYPE_XILINX_AXI_DMA_DATA_STREAM
);
567 object_initialize(&s
->rx_control_dev
, sizeof(s
->rx_control_dev
),
568 TYPE_XILINX_AXI_DMA_CONTROL_STREAM
);
569 object_property_add_child(OBJECT(s
), "axistream-connected-target",
570 (Object
*)&s
->rx_data_dev
, &error_abort
);
571 object_property_add_child(OBJECT(s
), "axistream-control-connected-target",
572 (Object
*)&s
->rx_control_dev
, &error_abort
);
574 sysbus_init_irq(sbd
, &s
->streams
[0].irq
);
575 sysbus_init_irq(sbd
, &s
->streams
[1].irq
);
577 memory_region_init_io(&s
->iomem
, obj
, &axidma_ops
, s
,
578 "xlnx.axi-dma", R_MAX
* 4 * 2);
579 sysbus_init_mmio(sbd
, &s
->iomem
);
582 static Property axidma_properties
[] = {
583 DEFINE_PROP_UINT32("freqhz", XilinxAXIDMA
, freqhz
, 50000000),
584 DEFINE_PROP_LINK("axistream-connected", XilinxAXIDMA
,
585 tx_data_dev
, TYPE_STREAM_SLAVE
, StreamSlave
*),
586 DEFINE_PROP_LINK("axistream-control-connected", XilinxAXIDMA
,
587 tx_control_dev
, TYPE_STREAM_SLAVE
, StreamSlave
*),
588 DEFINE_PROP_END_OF_LIST(),
591 static void axidma_class_init(ObjectClass
*klass
, void *data
)
593 DeviceClass
*dc
= DEVICE_CLASS(klass
);
595 dc
->realize
= xilinx_axidma_realize
,
596 dc
->reset
= xilinx_axidma_reset
;
597 dc
->props
= axidma_properties
;
600 static StreamSlaveClass xilinx_axidma_data_stream_class
= {
601 .push
= xilinx_axidma_data_stream_push
,
602 .can_push
= xilinx_axidma_data_stream_can_push
,
605 static StreamSlaveClass xilinx_axidma_control_stream_class
= {
606 .push
= xilinx_axidma_control_stream_push
,
609 static void xilinx_axidma_stream_class_init(ObjectClass
*klass
, void *data
)
611 StreamSlaveClass
*ssc
= STREAM_SLAVE_CLASS(klass
);
613 ssc
->push
= ((StreamSlaveClass
*)data
)->push
;
614 ssc
->can_push
= ((StreamSlaveClass
*)data
)->can_push
;
617 static const TypeInfo axidma_info
= {
618 .name
= TYPE_XILINX_AXI_DMA
,
619 .parent
= TYPE_SYS_BUS_DEVICE
,
620 .instance_size
= sizeof(XilinxAXIDMA
),
621 .class_init
= axidma_class_init
,
622 .instance_init
= xilinx_axidma_init
,
625 static const TypeInfo xilinx_axidma_data_stream_info
= {
626 .name
= TYPE_XILINX_AXI_DMA_DATA_STREAM
,
627 .parent
= TYPE_OBJECT
,
628 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
629 .class_init
= xilinx_axidma_stream_class_init
,
630 .class_data
= &xilinx_axidma_data_stream_class
,
631 .interfaces
= (InterfaceInfo
[]) {
632 { TYPE_STREAM_SLAVE
},
637 static const TypeInfo xilinx_axidma_control_stream_info
= {
638 .name
= TYPE_XILINX_AXI_DMA_CONTROL_STREAM
,
639 .parent
= TYPE_OBJECT
,
640 .instance_size
= sizeof(struct XilinxAXIDMAStreamSlave
),
641 .class_init
= xilinx_axidma_stream_class_init
,
642 .class_data
= &xilinx_axidma_control_stream_class
,
643 .interfaces
= (InterfaceInfo
[]) {
644 { TYPE_STREAM_SLAVE
},
649 static void xilinx_axidma_register_types(void)
651 type_register_static(&axidma_info
);
652 type_register_static(&xilinx_axidma_data_stream_info
);
653 type_register_static(&xilinx_axidma_control_stream_info
);
656 type_init(xilinx_axidma_register_types
)