40
#include "qemu/osdep.h"
42
#include "hw/ppc/mac_dbdma.h"
43
#include "migration/vmstate.h"
44
#include "qemu/main-loop.h"
45
#include "qemu/module.h"
47
#include "sysemu/dma.h"
51
#define DEBUG_DBDMA_CHANMASK ((1ull << DBDMA_CHANNELS) - 1)
53
#define DBDMA_DPRINTF(fmt, ...) do { \
55
printf("DBDMA: " fmt , ## __VA_ARGS__); \
59
#define DBDMA_DPRINTFCH(ch, fmt, ...) do { \
61
if ((1ul << (ch)->channel) & DEBUG_DBDMA_CHANMASK) { \
62
printf("DBDMA[%02x]: " fmt , (ch)->channel, ## __VA_ARGS__); \
70
static DBDMAState *dbdma_from_ch(DBDMA_channel *ch)
72
return container_of(ch, DBDMAState, channels[ch->channel]);
76
static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd)
78
DBDMA_DPRINTFCH(ch, "dbdma_cmd %p\n", cmd);
79
DBDMA_DPRINTFCH(ch, " req_count 0x%04x\n", le16_to_cpu(cmd->req_count));
80
DBDMA_DPRINTFCH(ch, " command 0x%04x\n", le16_to_cpu(cmd->command));
81
DBDMA_DPRINTFCH(ch, " phy_addr 0x%08x\n", le32_to_cpu(cmd->phy_addr));
82
DBDMA_DPRINTFCH(ch, " cmd_dep 0x%08x\n", le32_to_cpu(cmd->cmd_dep));
83
DBDMA_DPRINTFCH(ch, " res_count 0x%04x\n", le16_to_cpu(cmd->res_count));
84
DBDMA_DPRINTFCH(ch, " xfer_status 0x%04x\n",
85
le16_to_cpu(cmd->xfer_status));
88
static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd)
92
static void dbdma_cmdptr_load(DBDMA_channel *ch)
94
DBDMA_DPRINTFCH(ch, "dbdma_cmdptr_load 0x%08x\n",
95
ch->regs[DBDMA_CMDPTR_LO]);
96
dma_memory_read(&address_space_memory, ch->regs[DBDMA_CMDPTR_LO],
97
&ch->current, sizeof(dbdma_cmd), MEMTXATTRS_UNSPECIFIED);
100
static void dbdma_cmdptr_save(DBDMA_channel *ch)
102
DBDMA_DPRINTFCH(ch, "-> update 0x%08x stat=0x%08x, res=0x%04x\n",
103
ch->regs[DBDMA_CMDPTR_LO],
104
le16_to_cpu(ch->current.xfer_status),
105
le16_to_cpu(ch->current.res_count));
106
dma_memory_write(&address_space_memory, ch->regs[DBDMA_CMDPTR_LO],
107
&ch->current, sizeof(dbdma_cmd), MEMTXATTRS_UNSPECIFIED);
110
static void kill_channel(DBDMA_channel *ch)
112
DBDMA_DPRINTFCH(ch, "kill_channel\n");
114
ch->regs[DBDMA_STATUS] |= DEAD;
115
ch->regs[DBDMA_STATUS] &= ~ACTIVE;
117
qemu_irq_raise(ch->irq);
120
static void conditional_interrupt(DBDMA_channel *ch)
122
dbdma_cmd *current = &ch->current;
124
uint16_t sel_mask, sel_value;
128
DBDMA_DPRINTFCH(ch, "%s\n", __func__);
130
intr = le16_to_cpu(current->command) & INTR_MASK;
136
qemu_irq_raise(ch->irq);
137
DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
141
status = ch->regs[DBDMA_STATUS] & DEVSTAT;
143
sel_mask = (ch->regs[DBDMA_INTR_SEL] >> 16) & 0x0f;
144
sel_value = ch->regs[DBDMA_INTR_SEL] & 0x0f;
146
cond = (status & sel_mask) == (sel_value & sel_mask);
151
qemu_irq_raise(ch->irq);
152
DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
157
qemu_irq_raise(ch->irq);
158
DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
164
static int conditional_wait(DBDMA_channel *ch)
166
dbdma_cmd *current = &ch->current;
168
uint16_t sel_mask, sel_value;
173
wait = le16_to_cpu(current->command) & WAIT_MASK;
178
DBDMA_DPRINTFCH(ch, " [WAIT_ALWAYS]\n");
182
status = ch->regs[DBDMA_STATUS] & DEVSTAT;
184
sel_mask = (ch->regs[DBDMA_WAIT_SEL] >> 16) & 0x0f;
185
sel_value = ch->regs[DBDMA_WAIT_SEL] & 0x0f;
187
cond = (status & sel_mask) == (sel_value & sel_mask);
194
DBDMA_DPRINTFCH(ch, " [WAIT_IFSET=%d]\n", res);
200
DBDMA_DPRINTFCH(ch, " [WAIT_IFCLR=%d]\n", res);
206
static void next(DBDMA_channel *ch)
210
ch->regs[DBDMA_STATUS] &= ~BT;
212
cp = ch->regs[DBDMA_CMDPTR_LO];
213
ch->regs[DBDMA_CMDPTR_LO] = cp + sizeof(dbdma_cmd);
214
dbdma_cmdptr_load(ch);
217
static void branch(DBDMA_channel *ch)
219
dbdma_cmd *current = &ch->current;
221
ch->regs[DBDMA_CMDPTR_LO] = le32_to_cpu(current->cmd_dep);
222
ch->regs[DBDMA_STATUS] |= BT;
223
dbdma_cmdptr_load(ch);
226
static void conditional_branch(DBDMA_channel *ch)
228
dbdma_cmd *current = &ch->current;
230
uint16_t sel_mask, sel_value;
236
br = le16_to_cpu(current->command) & BR_MASK;
243
DBDMA_DPRINTFCH(ch, " [BR_ALWAYS]\n");
248
status = ch->regs[DBDMA_STATUS] & DEVSTAT;
250
sel_mask = (ch->regs[DBDMA_BRANCH_SEL] >> 16) & 0x0f;
251
sel_value = ch->regs[DBDMA_BRANCH_SEL] & 0x0f;
253
cond = (status & sel_mask) == (sel_value & sel_mask);
258
DBDMA_DPRINTFCH(ch, " [BR_IFSET = 1]\n");
261
DBDMA_DPRINTFCH(ch, " [BR_IFSET = 0]\n");
267
DBDMA_DPRINTFCH(ch, " [BR_IFCLR = 1]\n");
270
DBDMA_DPRINTFCH(ch, " [BR_IFCLR = 0]\n");
277
static void channel_run(DBDMA_channel *ch);
279
static void dbdma_end(DBDMA_io *io)
281
DBDMA_channel *ch = io->channel;
282
dbdma_cmd *current = &ch->current;
284
DBDMA_DPRINTFCH(ch, "%s\n", __func__);
286
if (conditional_wait(ch))
289
current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
290
current->res_count = cpu_to_le16(io->len);
291
dbdma_cmdptr_save(ch);
293
ch->regs[DBDMA_STATUS] &= ~FLUSH;
295
conditional_interrupt(ch);
296
conditional_branch(ch);
300
ch->io.processing = false;
302
if ((ch->regs[DBDMA_STATUS] & RUN) &&
303
(ch->regs[DBDMA_STATUS] & ACTIVE))
307
static void start_output(DBDMA_channel *ch, int key, uint32_t addr,
308
uint16_t req_count, int is_last)
310
DBDMA_DPRINTFCH(ch, "start_output\n");
316
DBDMA_DPRINTFCH(ch, "addr 0x%x key 0x%x\n", addr, key);
317
if (!addr || key > KEY_STREAM3) {
323
ch->io.len = req_count;
324
ch->io.is_last = is_last;
325
ch->io.dma_end = dbdma_end;
326
ch->io.is_dma_out = 1;
327
ch->io.processing = true;
333
static void start_input(DBDMA_channel *ch, int key, uint32_t addr,
334
uint16_t req_count, int is_last)
336
DBDMA_DPRINTFCH(ch, "start_input\n");
342
DBDMA_DPRINTFCH(ch, "addr 0x%x key 0x%x\n", addr, key);
343
if (!addr || key > KEY_STREAM3) {
349
ch->io.len = req_count;
350
ch->io.is_last = is_last;
351
ch->io.dma_end = dbdma_end;
352
ch->io.is_dma_out = 0;
353
ch->io.processing = true;
359
static void load_word(DBDMA_channel *ch, int key, uint32_t addr,
362
dbdma_cmd *current = &ch->current;
364
DBDMA_DPRINTFCH(ch, "load_word %d bytes, addr=%08x\n", len, addr);
368
if (key != KEY_SYSTEM) {
369
printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
374
dma_memory_read(&address_space_memory, addr, ¤t->cmd_dep, len,
375
MEMTXATTRS_UNSPECIFIED);
377
if (conditional_wait(ch))
380
current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
381
dbdma_cmdptr_save(ch);
382
ch->regs[DBDMA_STATUS] &= ~FLUSH;
384
conditional_interrupt(ch);
388
DBDMA_kick(dbdma_from_ch(ch));
391
static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
394
dbdma_cmd *current = &ch->current;
396
DBDMA_DPRINTFCH(ch, "store_word %d bytes, addr=%08x pa=%x\n",
397
len, addr, le32_to_cpu(current->cmd_dep));
401
if (key != KEY_SYSTEM) {
402
printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
407
dma_memory_write(&address_space_memory, addr, ¤t->cmd_dep, len,
408
MEMTXATTRS_UNSPECIFIED);
410
if (conditional_wait(ch))
413
current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
414
dbdma_cmdptr_save(ch);
415
ch->regs[DBDMA_STATUS] &= ~FLUSH;
417
conditional_interrupt(ch);
421
DBDMA_kick(dbdma_from_ch(ch));
424
static void nop(DBDMA_channel *ch)
426
dbdma_cmd *current = &ch->current;
428
if (conditional_wait(ch))
431
current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
432
dbdma_cmdptr_save(ch);
434
conditional_interrupt(ch);
435
conditional_branch(ch);
438
DBDMA_kick(dbdma_from_ch(ch));
441
static void stop(DBDMA_channel *ch)
443
ch->regs[DBDMA_STATUS] &= ~(ACTIVE);
448
static void channel_run(DBDMA_channel *ch)
450
dbdma_cmd *current = &ch->current;
455
DBDMA_DPRINTFCH(ch, "channel_run\n");
456
dump_dbdma_cmd(ch, current);
460
ch->regs[DBDMA_STATUS] &= ~WAKE;
462
cmd = le16_to_cpu(current->command) & COMMAND_MASK;
474
key = le16_to_cpu(current->command) & 0x0700;
475
req_count = le16_to_cpu(current->req_count);
476
phy_addr = le32_to_cpu(current->phy_addr);
478
if (key == KEY_STREAM4) {
479
printf("command %x, invalid key 4\n", cmd);
486
DBDMA_DPRINTFCH(ch, "* OUTPUT_MORE *\n");
487
start_output(ch, key, phy_addr, req_count, 0);
491
DBDMA_DPRINTFCH(ch, "* OUTPUT_LAST *\n");
492
start_output(ch, key, phy_addr, req_count, 1);
496
DBDMA_DPRINTFCH(ch, "* INPUT_MORE *\n");
497
start_input(ch, key, phy_addr, req_count, 0);
501
DBDMA_DPRINTFCH(ch, "* INPUT_LAST *\n");
502
start_input(ch, key, phy_addr, req_count, 1);
506
if (key < KEY_REGS) {
507
printf("command %x, invalid key %x\n", cmd, key);
515
req_count = req_count & 0x0007;
516
if (req_count & 0x4) {
519
} else if (req_count & 0x2) {
527
DBDMA_DPRINTFCH(ch, "* LOAD_WORD *\n");
528
load_word(ch, key, phy_addr, req_count);
532
DBDMA_DPRINTFCH(ch, "* STORE_WORD *\n");
533
store_word(ch, key, phy_addr, req_count);
538
static void DBDMA_run(DBDMAState *s)
542
for (channel = 0; channel < DBDMA_CHANNELS; channel++) {
543
DBDMA_channel *ch = &s->channels[channel];
544
uint32_t status = ch->regs[DBDMA_STATUS];
545
if (!ch->io.processing && (status & RUN) && (status & ACTIVE)) {
551
static void DBDMA_run_bh(void *opaque)
553
DBDMAState *s = opaque;
555
DBDMA_DPRINTF("-> DBDMA_run_bh\n");
557
DBDMA_DPRINTF("<- DBDMA_run_bh\n");
560
void DBDMA_kick(DBDMAState *dbdma)
562
qemu_bh_schedule(dbdma->bh);
565
void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
566
DBDMA_rw rw, DBDMA_flush flush,
569
DBDMAState *s = dbdma;
570
DBDMA_channel *ch = &s->channels[nchan];
572
DBDMA_DPRINTFCH(ch, "DBDMA_register_channel 0x%x\n", nchan);
580
ch->io.opaque = opaque;
583
static void dbdma_control_write(DBDMA_channel *ch)
585
uint16_t mask, value;
587
bool do_flush = false;
589
mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
590
value = ch->regs[DBDMA_CONTROL] & 0xffff;
595
status = ch->regs[DBDMA_STATUS];
607
if ((mask & RUN) && (value & RUN)) {
609
DBDMA_DPRINTFCH(ch, " Setting RUN !\n");
613
if ((mask & RUN) && !(value & RUN)) {
615
status &= ~(DEAD | RUN);
616
DBDMA_DPRINTFCH(ch, " Clearing RUN !\n");
629
if ((mask & WAKE) && (value & WAKE) && (status & RUN)) {
631
DBDMA_DPRINTFCH(ch, " Setting WAKE !\n");
639
status = (status & ~PAUSE) | (value & PAUSE);
640
DBDMA_DPRINTFCH(ch, " %sing PAUSE !\n",
641
(value & PAUSE) ? "sett" : "clear");
645
if ((mask & FLUSH) && (value & FLUSH)) {
646
DBDMA_DPRINTFCH(ch, " Setting FLUSH !\n");
653
ch->regs[DBDMA_STATUS] |= FLUSH;
662
if ((status & PAUSE) || !(status & RUN)) {
664
DBDMA_DPRINTFCH(ch, " -> ACTIVE down !\n");
673
} else if (mask & (RUN | PAUSE)) {
675
DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
676
} else if ((mask & WAKE) && (value & WAKE)) {
678
DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
681
DBDMA_DPRINTFCH(ch, " new status=0x%08x\n", status);
686
if (do_flush && ch->flush) {
691
ch->regs[DBDMA_STATUS] = status;
694
if (status & ACTIVE) {
695
DBDMA_kick(dbdma_from_ch(ch));
699
static void dbdma_write(void *opaque, hwaddr addr,
700
uint64_t value, unsigned size)
702
int channel = addr >> DBDMA_CHANNEL_SHIFT;
703
DBDMAState *s = opaque;
704
DBDMA_channel *ch = &s->channels[channel];
705
int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
707
DBDMA_DPRINTFCH(ch, "writel 0x" HWADDR_FMT_plx " <= 0x%08"PRIx64"\n",
709
DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
710
(uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
714
if (reg == DBDMA_CMDPTR_LO && (ch->regs[DBDMA_STATUS] & ACTIVE)) {
718
ch->regs[reg] = value;
722
dbdma_control_write(ch);
724
case DBDMA_CMDPTR_LO:
726
ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
727
dbdma_cmdptr_load(ch);
731
case DBDMA_BRANCH_SEL:
735
case DBDMA_XFER_MODE:
736
case DBDMA_CMDPTR_HI:
737
case DBDMA_DATA2PTR_HI:
738
case DBDMA_DATA2PTR_LO:
739
case DBDMA_ADDRESS_HI:
740
case DBDMA_BRANCH_ADDR_HI:
750
static uint64_t dbdma_read(void *opaque, hwaddr addr,
754
int channel = addr >> DBDMA_CHANNEL_SHIFT;
755
DBDMAState *s = opaque;
756
DBDMA_channel *ch = &s->channels[channel];
757
int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
759
value = ch->regs[reg];
763
value = ch->regs[DBDMA_STATUS];
766
case DBDMA_CMDPTR_LO:
768
case DBDMA_BRANCH_SEL:
772
case DBDMA_XFER_MODE:
773
case DBDMA_CMDPTR_HI:
774
case DBDMA_DATA2PTR_HI:
775
case DBDMA_DATA2PTR_LO:
776
case DBDMA_ADDRESS_HI:
777
case DBDMA_BRANCH_ADDR_HI:
789
DBDMA_DPRINTFCH(ch, "readl 0x" HWADDR_FMT_plx " => 0x%08x\n", addr, value);
790
DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
791
(uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
796
static const MemoryRegionOps dbdma_ops = {
798
.write = dbdma_write,
799
.endianness = DEVICE_LITTLE_ENDIAN,
801
.min_access_size = 4,
802
.max_access_size = 4,
806
static const VMStateDescription vmstate_dbdma_io = {
809
.minimum_version_id = 0,
810
.fields = (const VMStateField[]) {
811
VMSTATE_UINT64(addr, struct DBDMA_io),
812
VMSTATE_INT32(len, struct DBDMA_io),
813
VMSTATE_INT32(is_last, struct DBDMA_io),
814
VMSTATE_INT32(is_dma_out, struct DBDMA_io),
815
VMSTATE_BOOL(processing, struct DBDMA_io),
816
VMSTATE_END_OF_LIST()
820
static const VMStateDescription vmstate_dbdma_cmd = {
823
.minimum_version_id = 0,
824
.fields = (const VMStateField[]) {
825
VMSTATE_UINT16(req_count, dbdma_cmd),
826
VMSTATE_UINT16(command, dbdma_cmd),
827
VMSTATE_UINT32(phy_addr, dbdma_cmd),
828
VMSTATE_UINT32(cmd_dep, dbdma_cmd),
829
VMSTATE_UINT16(res_count, dbdma_cmd),
830
VMSTATE_UINT16(xfer_status, dbdma_cmd),
831
VMSTATE_END_OF_LIST()
835
static const VMStateDescription vmstate_dbdma_channel = {
836
.name = "dbdma_channel",
838
.minimum_version_id = 1,
839
.fields = (const VMStateField[]) {
840
VMSTATE_UINT32_ARRAY(regs, struct DBDMA_channel, DBDMA_REGS),
841
VMSTATE_STRUCT(io, struct DBDMA_channel, 0, vmstate_dbdma_io, DBDMA_io),
842
VMSTATE_STRUCT(current, struct DBDMA_channel, 0, vmstate_dbdma_cmd,
844
VMSTATE_END_OF_LIST()
848
static const VMStateDescription vmstate_dbdma = {
851
.minimum_version_id = 3,
852
.fields = (const VMStateField[]) {
853
VMSTATE_STRUCT_ARRAY(channels, DBDMAState, DBDMA_CHANNELS, 1,
854
vmstate_dbdma_channel, DBDMA_channel),
855
VMSTATE_END_OF_LIST()
859
static void mac_dbdma_reset(DeviceState *d)
861
DBDMAState *s = MAC_DBDMA(d);
864
for (i = 0; i < DBDMA_CHANNELS; i++) {
865
memset(s->channels[i].regs, 0, DBDMA_SIZE);
869
static void dbdma_unassigned_rw(DBDMA_io *io)
871
DBDMA_channel *ch = io->channel;
872
dbdma_cmd *current = &ch->current;
874
qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
875
__func__, ch->channel);
876
ch->io.processing = false;
878
cmd = le16_to_cpu(current->command) & COMMAND_MASK;
879
if (cmd == OUTPUT_MORE || cmd == OUTPUT_LAST ||
880
cmd == INPUT_MORE || cmd == INPUT_LAST) {
881
current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
882
current->res_count = cpu_to_le16(io->len);
883
dbdma_cmdptr_save(ch);
887
static void dbdma_unassigned_flush(DBDMA_io *io)
889
DBDMA_channel *ch = io->channel;
890
qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
891
__func__, ch->channel);
894
static void mac_dbdma_init(Object *obj)
896
SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
897
DBDMAState *s = MAC_DBDMA(obj);
900
for (i = 0; i < DBDMA_CHANNELS; i++) {
901
DBDMA_channel *ch = &s->channels[i];
903
ch->rw = dbdma_unassigned_rw;
904
ch->flush = dbdma_unassigned_flush;
909
memory_region_init_io(&s->mem, obj, &dbdma_ops, s, "dbdma", 0x1000);
910
sysbus_init_mmio(sbd, &s->mem);
913
static void mac_dbdma_realize(DeviceState *dev, Error **errp)
915
DBDMAState *s = MAC_DBDMA(dev);
917
s->bh = qemu_bh_new_guarded(DBDMA_run_bh, s, &dev->mem_reentrancy_guard);
920
static void mac_dbdma_class_init(ObjectClass *oc, void *data)
922
DeviceClass *dc = DEVICE_CLASS(oc);
924
dc->realize = mac_dbdma_realize;
925
dc->reset = mac_dbdma_reset;
926
dc->vmsd = &vmstate_dbdma;
929
static const TypeInfo mac_dbdma_type_info = {
930
.name = TYPE_MAC_DBDMA,
931
.parent = TYPE_SYS_BUS_DEVICE,
932
.instance_size = sizeof(DBDMAState),
933
.instance_init = mac_dbdma_init,
934
.class_init = mac_dbdma_class_init
937
static void mac_dbdma_register_types(void)
939
type_register_static(&mac_dbdma_type_info);
942
type_init(mac_dbdma_register_types)