2 * PowerMac descriptor-based DMA emulation
4 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
6 * Copyright (c) 2009 Laurent Vivier
8 * some parts from linux-2.6.28, arch/powerpc/include/asm/dbdma.h
10 * Definitions for using the Apple Descriptor-Based DMA controller
11 * in Power Macintosh computers.
13 * Copyright (C) 1996 Paul Mackerras.
15 * some parts from mol 0.9.71
17 * Descriptor based DMA emulation
19 * Copyright (C) 1998-2004 Samuel Rydh (samuel@ibrium.se)
21 * Permission is hereby granted, free of charge, to any person obtaining a copy
22 * of this software and associated documentation files (the "Software"), to deal
23 * in the Software without restriction, including without limitation the rights
24 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 * copies of the Software, and to permit persons to whom the Software is
26 * furnished to do so, subject to the following conditions:
28 * The above copyright notice and this permission notice shall be included in
29 * all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
34 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 #include "qemu/osdep.h"
41 #include "hw/isa/isa.h"
42 #include "hw/ppc/mac_dbdma.h"
43 #include "qemu/main-loop.h"
45 #include "sysemu/dma.h"
49 #define DEBUG_DBDMA_CHANMASK ((1ull << DBDMA_CHANNELS) - 1)
51 #define DBDMA_DPRINTF(fmt, ...) do { \
53 printf("DBDMA: " fmt , ## __VA_ARGS__); \
57 #define DBDMA_DPRINTFCH(ch, fmt, ...) do { \
59 if ((1ul << (ch)->channel) & DEBUG_DBDMA_CHANMASK) { \
60 printf("DBDMA[%02x]: " fmt , (ch)->channel, ## __VA_ARGS__); \
68 static DBDMAState
*dbdma_from_ch(DBDMA_channel
*ch
)
70 return container_of(ch
, DBDMAState
, channels
[ch
->channel
]);
74 static void dump_dbdma_cmd(DBDMA_channel
*ch
, dbdma_cmd
*cmd
)
76 DBDMA_DPRINTFCH(ch
, "dbdma_cmd %p\n", cmd
);
77 DBDMA_DPRINTFCH(ch
, " req_count 0x%04x\n", le16_to_cpu(cmd
->req_count
));
78 DBDMA_DPRINTFCH(ch
, " command 0x%04x\n", le16_to_cpu(cmd
->command
));
79 DBDMA_DPRINTFCH(ch
, " phy_addr 0x%08x\n", le32_to_cpu(cmd
->phy_addr
));
80 DBDMA_DPRINTFCH(ch
, " cmd_dep 0x%08x\n", le32_to_cpu(cmd
->cmd_dep
));
81 DBDMA_DPRINTFCH(ch
, " res_count 0x%04x\n", le16_to_cpu(cmd
->res_count
));
82 DBDMA_DPRINTFCH(ch
, " xfer_status 0x%04x\n",
83 le16_to_cpu(cmd
->xfer_status
));
86 static void dump_dbdma_cmd(DBDMA_channel
*ch
, dbdma_cmd
*cmd
)
90 static void dbdma_cmdptr_load(DBDMA_channel
*ch
)
92 DBDMA_DPRINTFCH(ch
, "dbdma_cmdptr_load 0x%08x\n",
93 ch
->regs
[DBDMA_CMDPTR_LO
]);
94 dma_memory_read(&address_space_memory
, ch
->regs
[DBDMA_CMDPTR_LO
],
95 &ch
->current
, sizeof(dbdma_cmd
));
98 static void dbdma_cmdptr_save(DBDMA_channel
*ch
)
100 DBDMA_DPRINTFCH(ch
, "-> update 0x%08x stat=0x%08x, res=0x%04x\n",
101 ch
->regs
[DBDMA_CMDPTR_LO
],
102 le16_to_cpu(ch
->current
.xfer_status
),
103 le16_to_cpu(ch
->current
.res_count
));
104 dma_memory_write(&address_space_memory
, ch
->regs
[DBDMA_CMDPTR_LO
],
105 &ch
->current
, sizeof(dbdma_cmd
));
108 static void kill_channel(DBDMA_channel
*ch
)
110 DBDMA_DPRINTFCH(ch
, "kill_channel\n");
112 ch
->regs
[DBDMA_STATUS
] |= DEAD
;
113 ch
->regs
[DBDMA_STATUS
] &= ~ACTIVE
;
115 qemu_irq_raise(ch
->irq
);
118 static void conditional_interrupt(DBDMA_channel
*ch
)
120 dbdma_cmd
*current
= &ch
->current
;
122 uint16_t sel_mask
, sel_value
;
126 DBDMA_DPRINTFCH(ch
, "%s\n", __func__
);
128 intr
= le16_to_cpu(current
->command
) & INTR_MASK
;
131 case INTR_NEVER
: /* don't interrupt */
133 case INTR_ALWAYS
: /* always interrupt */
134 qemu_irq_raise(ch
->irq
);
135 DBDMA_DPRINTFCH(ch
, "%s: raise\n", __func__
);
139 status
= ch
->regs
[DBDMA_STATUS
] & DEVSTAT
;
141 sel_mask
= (ch
->regs
[DBDMA_INTR_SEL
] >> 16) & 0x0f;
142 sel_value
= ch
->regs
[DBDMA_INTR_SEL
] & 0x0f;
144 cond
= (status
& sel_mask
) == (sel_value
& sel_mask
);
147 case INTR_IFSET
: /* intr if condition bit is 1 */
149 qemu_irq_raise(ch
->irq
);
150 DBDMA_DPRINTFCH(ch
, "%s: raise\n", __func__
);
153 case INTR_IFCLR
: /* intr if condition bit is 0 */
155 qemu_irq_raise(ch
->irq
);
156 DBDMA_DPRINTFCH(ch
, "%s: raise\n", __func__
);
162 static int conditional_wait(DBDMA_channel
*ch
)
164 dbdma_cmd
*current
= &ch
->current
;
166 uint16_t sel_mask
, sel_value
;
171 wait
= le16_to_cpu(current
->command
) & WAIT_MASK
;
173 case WAIT_NEVER
: /* don't wait */
175 case WAIT_ALWAYS
: /* always wait */
176 DBDMA_DPRINTFCH(ch
, " [WAIT_ALWAYS]\n");
180 status
= ch
->regs
[DBDMA_STATUS
] & DEVSTAT
;
182 sel_mask
= (ch
->regs
[DBDMA_WAIT_SEL
] >> 16) & 0x0f;
183 sel_value
= ch
->regs
[DBDMA_WAIT_SEL
] & 0x0f;
185 cond
= (status
& sel_mask
) == (sel_value
& sel_mask
);
188 case WAIT_IFSET
: /* wait if condition bit is 1 */
192 DBDMA_DPRINTFCH(ch
, " [WAIT_IFSET=%d]\n", res
);
194 case WAIT_IFCLR
: /* wait if condition bit is 0 */
198 DBDMA_DPRINTFCH(ch
, " [WAIT_IFCLR=%d]\n", res
);
204 static void next(DBDMA_channel
*ch
)
208 ch
->regs
[DBDMA_STATUS
] &= ~BT
;
210 cp
= ch
->regs
[DBDMA_CMDPTR_LO
];
211 ch
->regs
[DBDMA_CMDPTR_LO
] = cp
+ sizeof(dbdma_cmd
);
212 dbdma_cmdptr_load(ch
);
215 static void branch(DBDMA_channel
*ch
)
217 dbdma_cmd
*current
= &ch
->current
;
219 ch
->regs
[DBDMA_CMDPTR_LO
] = le32_to_cpu(current
->cmd_dep
);
220 ch
->regs
[DBDMA_STATUS
] |= BT
;
221 dbdma_cmdptr_load(ch
);
224 static void conditional_branch(DBDMA_channel
*ch
)
226 dbdma_cmd
*current
= &ch
->current
;
228 uint16_t sel_mask
, sel_value
;
232 /* check if we must branch */
234 br
= le16_to_cpu(current
->command
) & BR_MASK
;
237 case BR_NEVER
: /* don't branch */
240 case BR_ALWAYS
: /* always branch */
241 DBDMA_DPRINTFCH(ch
, " [BR_ALWAYS]\n");
246 status
= ch
->regs
[DBDMA_STATUS
] & DEVSTAT
;
248 sel_mask
= (ch
->regs
[DBDMA_BRANCH_SEL
] >> 16) & 0x0f;
249 sel_value
= ch
->regs
[DBDMA_BRANCH_SEL
] & 0x0f;
251 cond
= (status
& sel_mask
) == (sel_value
& sel_mask
);
254 case BR_IFSET
: /* branch if condition bit is 1 */
256 DBDMA_DPRINTFCH(ch
, " [BR_IFSET = 1]\n");
259 DBDMA_DPRINTFCH(ch
, " [BR_IFSET = 0]\n");
263 case BR_IFCLR
: /* branch if condition bit is 0 */
265 DBDMA_DPRINTFCH(ch
, " [BR_IFCLR = 1]\n");
268 DBDMA_DPRINTFCH(ch
, " [BR_IFCLR = 0]\n");
275 static void channel_run(DBDMA_channel
*ch
);
277 static void dbdma_end(DBDMA_io
*io
)
279 DBDMA_channel
*ch
= io
->channel
;
280 dbdma_cmd
*current
= &ch
->current
;
282 DBDMA_DPRINTFCH(ch
, "%s\n", __func__
);
284 if (conditional_wait(ch
))
287 current
->xfer_status
= cpu_to_le16(ch
->regs
[DBDMA_STATUS
]);
288 current
->res_count
= cpu_to_le16(io
->len
);
289 dbdma_cmdptr_save(ch
);
291 ch
->regs
[DBDMA_STATUS
] &= ~FLUSH
;
293 conditional_interrupt(ch
);
294 conditional_branch(ch
);
297 /* Indicate that we're ready for a new DMA round */
298 ch
->io
.processing
= false;
300 if ((ch
->regs
[DBDMA_STATUS
] & RUN
) &&
301 (ch
->regs
[DBDMA_STATUS
] & ACTIVE
))
305 static void start_output(DBDMA_channel
*ch
, int key
, uint32_t addr
,
306 uint16_t req_count
, int is_last
)
308 DBDMA_DPRINTFCH(ch
, "start_output\n");
310 /* KEY_REGS, KEY_DEVICE and KEY_STREAM
311 * are not implemented in the mac-io chip
314 DBDMA_DPRINTFCH(ch
, "addr 0x%x key 0x%x\n", addr
, key
);
315 if (!addr
|| key
> KEY_STREAM3
) {
321 ch
->io
.len
= req_count
;
322 ch
->io
.is_last
= is_last
;
323 ch
->io
.dma_end
= dbdma_end
;
324 ch
->io
.is_dma_out
= 1;
325 ch
->io
.processing
= true;
331 static void start_input(DBDMA_channel
*ch
, int key
, uint32_t addr
,
332 uint16_t req_count
, int is_last
)
334 DBDMA_DPRINTFCH(ch
, "start_input\n");
336 /* KEY_REGS, KEY_DEVICE and KEY_STREAM
337 * are not implemented in the mac-io chip
340 DBDMA_DPRINTFCH(ch
, "addr 0x%x key 0x%x\n", addr
, key
);
341 if (!addr
|| key
> KEY_STREAM3
) {
347 ch
->io
.len
= req_count
;
348 ch
->io
.is_last
= is_last
;
349 ch
->io
.dma_end
= dbdma_end
;
350 ch
->io
.is_dma_out
= 0;
351 ch
->io
.processing
= true;
357 static void load_word(DBDMA_channel
*ch
, int key
, uint32_t addr
,
360 dbdma_cmd
*current
= &ch
->current
;
362 DBDMA_DPRINTFCH(ch
, "load_word %d bytes, addr=%08x\n", len
, addr
);
364 /* only implements KEY_SYSTEM */
366 if (key
!= KEY_SYSTEM
) {
367 printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key
);
372 dma_memory_read(&address_space_memory
, addr
, ¤t
->cmd_dep
, len
);
374 if (conditional_wait(ch
))
377 current
->xfer_status
= cpu_to_le16(ch
->regs
[DBDMA_STATUS
]);
378 dbdma_cmdptr_save(ch
);
379 ch
->regs
[DBDMA_STATUS
] &= ~FLUSH
;
381 conditional_interrupt(ch
);
385 DBDMA_kick(dbdma_from_ch(ch
));
388 static void store_word(DBDMA_channel
*ch
, int key
, uint32_t addr
,
391 dbdma_cmd
*current
= &ch
->current
;
393 DBDMA_DPRINTFCH(ch
, "store_word %d bytes, addr=%08x pa=%x\n",
394 len
, addr
, le32_to_cpu(current
->cmd_dep
));
396 /* only implements KEY_SYSTEM */
398 if (key
!= KEY_SYSTEM
) {
399 printf("DBDMA: STORE_WORD, unimplemented key %x\n", key
);
404 dma_memory_write(&address_space_memory
, addr
, ¤t
->cmd_dep
, len
);
406 if (conditional_wait(ch
))
409 current
->xfer_status
= cpu_to_le16(ch
->regs
[DBDMA_STATUS
]);
410 dbdma_cmdptr_save(ch
);
411 ch
->regs
[DBDMA_STATUS
] &= ~FLUSH
;
413 conditional_interrupt(ch
);
417 DBDMA_kick(dbdma_from_ch(ch
));
420 static void nop(DBDMA_channel
*ch
)
422 dbdma_cmd
*current
= &ch
->current
;
424 if (conditional_wait(ch
))
427 current
->xfer_status
= cpu_to_le16(ch
->regs
[DBDMA_STATUS
]);
428 dbdma_cmdptr_save(ch
);
430 conditional_interrupt(ch
);
431 conditional_branch(ch
);
434 DBDMA_kick(dbdma_from_ch(ch
));
437 static void stop(DBDMA_channel
*ch
)
439 ch
->regs
[DBDMA_STATUS
] &= ~(ACTIVE
);
441 /* the stop command does not increment command pointer */
444 static void channel_run(DBDMA_channel
*ch
)
446 dbdma_cmd
*current
= &ch
->current
;
451 DBDMA_DPRINTFCH(ch
, "channel_run\n");
452 dump_dbdma_cmd(ch
, current
);
454 /* clear WAKE flag at command fetch */
456 ch
->regs
[DBDMA_STATUS
] &= ~WAKE
;
458 cmd
= le16_to_cpu(current
->command
) & COMMAND_MASK
;
470 key
= le16_to_cpu(current
->command
) & 0x0700;
471 req_count
= le16_to_cpu(current
->req_count
);
472 phy_addr
= le32_to_cpu(current
->phy_addr
);
474 if (key
== KEY_STREAM4
) {
475 printf("command %x, invalid key 4\n", cmd
);
482 DBDMA_DPRINTFCH(ch
, "* OUTPUT_MORE *\n");
483 start_output(ch
, key
, phy_addr
, req_count
, 0);
487 DBDMA_DPRINTFCH(ch
, "* OUTPUT_LAST *\n");
488 start_output(ch
, key
, phy_addr
, req_count
, 1);
492 DBDMA_DPRINTFCH(ch
, "* INPUT_MORE *\n");
493 start_input(ch
, key
, phy_addr
, req_count
, 0);
497 DBDMA_DPRINTFCH(ch
, "* INPUT_LAST *\n");
498 start_input(ch
, key
, phy_addr
, req_count
, 1);
502 if (key
< KEY_REGS
) {
503 printf("command %x, invalid key %x\n", cmd
, key
);
507 /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
508 * and BRANCH is invalid
511 req_count
= req_count
& 0x0007;
512 if (req_count
& 0x4) {
515 } else if (req_count
& 0x2) {
523 DBDMA_DPRINTFCH(ch
, "* LOAD_WORD *\n");
524 load_word(ch
, key
, phy_addr
, req_count
);
528 DBDMA_DPRINTFCH(ch
, "* STORE_WORD *\n");
529 store_word(ch
, key
, phy_addr
, req_count
);
534 static void DBDMA_run(DBDMAState
*s
)
538 for (channel
= 0; channel
< DBDMA_CHANNELS
; channel
++) {
539 DBDMA_channel
*ch
= &s
->channels
[channel
];
540 uint32_t status
= ch
->regs
[DBDMA_STATUS
];
541 if (!ch
->io
.processing
&& (status
& RUN
) && (status
& ACTIVE
)) {
547 static void DBDMA_run_bh(void *opaque
)
549 DBDMAState
*s
= opaque
;
551 DBDMA_DPRINTF("-> DBDMA_run_bh\n");
553 DBDMA_DPRINTF("<- DBDMA_run_bh\n");
556 void DBDMA_kick(DBDMAState
*dbdma
)
558 qemu_bh_schedule(dbdma
->bh
);
561 void DBDMA_register_channel(void *dbdma
, int nchan
, qemu_irq irq
,
562 DBDMA_rw rw
, DBDMA_flush flush
,
565 DBDMAState
*s
= dbdma
;
566 DBDMA_channel
*ch
= &s
->channels
[nchan
];
568 DBDMA_DPRINTFCH(ch
, "DBDMA_register_channel 0x%x\n", nchan
);
576 ch
->io
.opaque
= opaque
;
579 static void dbdma_control_write(DBDMA_channel
*ch
)
581 uint16_t mask
, value
;
583 bool do_flush
= false;
585 mask
= (ch
->regs
[DBDMA_CONTROL
] >> 16) & 0xffff;
586 value
= ch
->regs
[DBDMA_CONTROL
] & 0xffff;
588 /* This is the status register which we'll update
589 * appropriately and store back
591 status
= ch
->regs
[DBDMA_STATUS
];
593 /* RUN and PAUSE are bits under SW control only
594 * FLUSH and WAKE are set by SW and cleared by HW
595 * DEAD, ACTIVE and BT are only under HW control
597 * We handle ACTIVE separately at the end of the
598 * logic to ensure all cases are covered.
601 /* Setting RUN will tentatively activate the channel
603 if ((mask
& RUN
) && (value
& RUN
)) {
605 DBDMA_DPRINTFCH(ch
, " Setting RUN !\n");
608 /* Clearing RUN 1->0 will stop the channel */
609 if ((mask
& RUN
) && !(value
& RUN
)) {
610 /* This has the side effect of clearing the DEAD bit */
611 status
&= ~(DEAD
| RUN
);
612 DBDMA_DPRINTFCH(ch
, " Clearing RUN !\n");
615 /* Setting WAKE wakes up an idle channel if it's running
617 * Note: The doc doesn't say so but assume that only works
618 * on a channel whose RUN bit is set.
620 * We set WAKE in status, it's not terribly useful as it will
621 * be cleared on the next command fetch but it seems to mimmic
622 * the HW behaviour and is useful for the way we handle
623 * ACTIVE further down.
625 if ((mask
& WAKE
) && (value
& WAKE
) && (status
& RUN
)) {
627 DBDMA_DPRINTFCH(ch
, " Setting WAKE !\n");
630 /* PAUSE being set will deactivate (or prevent activation)
631 * of the channel. We just copy it over for now, ACTIVE will
632 * be re-evaluated later.
635 status
= (status
& ~PAUSE
) | (value
& PAUSE
);
636 DBDMA_DPRINTFCH(ch
, " %sing PAUSE !\n",
637 (value
& PAUSE
) ? "sett" : "clear");
640 /* FLUSH is its own thing */
641 if ((mask
& FLUSH
) && (value
& FLUSH
)) {
642 DBDMA_DPRINTFCH(ch
, " Setting FLUSH !\n");
643 /* We set flush directly in the status register, we do *NOT*
644 * set it in "status" so that it gets naturally cleared when
645 * we update the status register further down. That way it
646 * will be set only during the HW flush operation so it is
647 * visible to any completions happening during that time.
649 ch
->regs
[DBDMA_STATUS
] |= FLUSH
;
653 /* If either RUN or PAUSE is clear, so should ACTIVE be,
654 * otherwise, ACTIVE will be set if we modified RUN, PAUSE or
655 * set WAKE. That means that PAUSE was just cleared, RUN was
656 * just set or WAKE was just set.
658 if ((status
& PAUSE
) || !(status
& RUN
)) {
660 DBDMA_DPRINTFCH(ch
, " -> ACTIVE down !\n");
662 /* We stopped processing, we want the underlying HW command
663 * to complete *before* we clear the ACTIVE bit. Otherwise
664 * we can get into a situation where the command status will
665 * have RUN or ACTIVE not set which is going to confuse the
669 } else if (mask
& (RUN
| PAUSE
)) {
671 DBDMA_DPRINTFCH(ch
, " -> ACTIVE up !\n");
672 } else if ((mask
& WAKE
) && (value
& WAKE
)) {
674 DBDMA_DPRINTFCH(ch
, " -> ACTIVE up !\n");
677 DBDMA_DPRINTFCH(ch
, " new status=0x%08x\n", status
);
679 /* If we need to flush the underlying HW, do it now, this happens
680 * both on FLUSH commands and when stopping the channel for safety.
682 if (do_flush
&& ch
->flush
) {
686 /* Finally update the status register image */
687 ch
->regs
[DBDMA_STATUS
] = status
;
689 /* If active, make sure the BH gets to run */
690 if (status
& ACTIVE
) {
691 DBDMA_kick(dbdma_from_ch(ch
));
695 static void dbdma_write(void *opaque
, hwaddr addr
,
696 uint64_t value
, unsigned size
)
698 int channel
= addr
>> DBDMA_CHANNEL_SHIFT
;
699 DBDMAState
*s
= opaque
;
700 DBDMA_channel
*ch
= &s
->channels
[channel
];
701 int reg
= (addr
- (channel
<< DBDMA_CHANNEL_SHIFT
)) >> 2;
703 DBDMA_DPRINTFCH(ch
, "writel 0x" TARGET_FMT_plx
" <= 0x%08"PRIx64
"\n",
705 DBDMA_DPRINTFCH(ch
, "channel 0x%x reg 0x%x\n",
706 (uint32_t)addr
>> DBDMA_CHANNEL_SHIFT
, reg
);
708 /* cmdptr cannot be modified if channel is ACTIVE */
710 if (reg
== DBDMA_CMDPTR_LO
&& (ch
->regs
[DBDMA_STATUS
] & ACTIVE
)) {
714 ch
->regs
[reg
] = value
;
718 dbdma_control_write(ch
);
720 case DBDMA_CMDPTR_LO
:
721 /* 16-byte aligned */
722 ch
->regs
[DBDMA_CMDPTR_LO
] &= ~0xf;
723 dbdma_cmdptr_load(ch
);
727 case DBDMA_BRANCH_SEL
:
731 case DBDMA_XFER_MODE
:
732 case DBDMA_CMDPTR_HI
:
733 case DBDMA_DATA2PTR_HI
:
734 case DBDMA_DATA2PTR_LO
:
735 case DBDMA_ADDRESS_HI
:
736 case DBDMA_BRANCH_ADDR_HI
:
746 static uint64_t dbdma_read(void *opaque
, hwaddr addr
,
750 int channel
= addr
>> DBDMA_CHANNEL_SHIFT
;
751 DBDMAState
*s
= opaque
;
752 DBDMA_channel
*ch
= &s
->channels
[channel
];
753 int reg
= (addr
- (channel
<< DBDMA_CHANNEL_SHIFT
)) >> 2;
755 value
= ch
->regs
[reg
];
759 value
= ch
->regs
[DBDMA_STATUS
];
762 case DBDMA_CMDPTR_LO
:
764 case DBDMA_BRANCH_SEL
:
768 case DBDMA_XFER_MODE
:
769 case DBDMA_CMDPTR_HI
:
770 case DBDMA_DATA2PTR_HI
:
771 case DBDMA_DATA2PTR_LO
:
772 case DBDMA_ADDRESS_HI
:
773 case DBDMA_BRANCH_ADDR_HI
:
785 DBDMA_DPRINTFCH(ch
, "readl 0x" TARGET_FMT_plx
" => 0x%08x\n", addr
, value
);
786 DBDMA_DPRINTFCH(ch
, "channel 0x%x reg 0x%x\n",
787 (uint32_t)addr
>> DBDMA_CHANNEL_SHIFT
, reg
);
792 static const MemoryRegionOps dbdma_ops
= {
794 .write
= dbdma_write
,
795 .endianness
= DEVICE_LITTLE_ENDIAN
,
797 .min_access_size
= 4,
798 .max_access_size
= 4,
802 static const VMStateDescription vmstate_dbdma_io
= {
805 .minimum_version_id
= 0,
806 .fields
= (VMStateField
[]) {
807 VMSTATE_UINT64(addr
, struct DBDMA_io
),
808 VMSTATE_INT32(len
, struct DBDMA_io
),
809 VMSTATE_INT32(is_last
, struct DBDMA_io
),
810 VMSTATE_INT32(is_dma_out
, struct DBDMA_io
),
811 VMSTATE_BOOL(processing
, struct DBDMA_io
),
812 VMSTATE_END_OF_LIST()
816 static const VMStateDescription vmstate_dbdma_cmd
= {
819 .minimum_version_id
= 0,
820 .fields
= (VMStateField
[]) {
821 VMSTATE_UINT16(req_count
, dbdma_cmd
),
822 VMSTATE_UINT16(command
, dbdma_cmd
),
823 VMSTATE_UINT32(phy_addr
, dbdma_cmd
),
824 VMSTATE_UINT32(cmd_dep
, dbdma_cmd
),
825 VMSTATE_UINT16(res_count
, dbdma_cmd
),
826 VMSTATE_UINT16(xfer_status
, dbdma_cmd
),
827 VMSTATE_END_OF_LIST()
831 static const VMStateDescription vmstate_dbdma_channel
= {
832 .name
= "dbdma_channel",
834 .minimum_version_id
= 1,
835 .fields
= (VMStateField
[]) {
836 VMSTATE_UINT32_ARRAY(regs
, struct DBDMA_channel
, DBDMA_REGS
),
837 VMSTATE_STRUCT(io
, struct DBDMA_channel
, 0, vmstate_dbdma_io
, DBDMA_io
),
838 VMSTATE_STRUCT(current
, struct DBDMA_channel
, 0, vmstate_dbdma_cmd
,
840 VMSTATE_END_OF_LIST()
844 static const VMStateDescription vmstate_dbdma
= {
847 .minimum_version_id
= 3,
848 .fields
= (VMStateField
[]) {
849 VMSTATE_STRUCT_ARRAY(channels
, DBDMAState
, DBDMA_CHANNELS
, 1,
850 vmstate_dbdma_channel
, DBDMA_channel
),
851 VMSTATE_END_OF_LIST()
855 static void mac_dbdma_reset(DeviceState
*d
)
857 DBDMAState
*s
= MAC_DBDMA(d
);
860 for (i
= 0; i
< DBDMA_CHANNELS
; i
++) {
861 memset(s
->channels
[i
].regs
, 0, DBDMA_SIZE
);
865 static void dbdma_unassigned_rw(DBDMA_io
*io
)
867 DBDMA_channel
*ch
= io
->channel
;
868 dbdma_cmd
*current
= &ch
->current
;
870 qemu_log_mask(LOG_GUEST_ERROR
, "%s: use of unassigned channel %d\n",
871 __func__
, ch
->channel
);
872 ch
->io
.processing
= false;
874 cmd
= le16_to_cpu(current
->command
) & COMMAND_MASK
;
875 if (cmd
== OUTPUT_MORE
|| cmd
== OUTPUT_LAST
||
876 cmd
== INPUT_MORE
|| cmd
== INPUT_LAST
) {
877 current
->xfer_status
= cpu_to_le16(ch
->regs
[DBDMA_STATUS
]);
878 current
->res_count
= cpu_to_le16(io
->len
);
879 dbdma_cmdptr_save(ch
);
883 static void dbdma_unassigned_flush(DBDMA_io
*io
)
885 DBDMA_channel
*ch
= io
->channel
;
886 qemu_log_mask(LOG_GUEST_ERROR
, "%s: use of unassigned channel %d\n",
887 __func__
, ch
->channel
);
890 static void mac_dbdma_init(Object
*obj
)
892 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
893 DBDMAState
*s
= MAC_DBDMA(obj
);
896 for (i
= 0; i
< DBDMA_CHANNELS
; i
++) {
897 DBDMA_channel
*ch
= &s
->channels
[i
];
899 ch
->rw
= dbdma_unassigned_rw
;
900 ch
->flush
= dbdma_unassigned_flush
;
905 memory_region_init_io(&s
->mem
, obj
, &dbdma_ops
, s
, "dbdma", 0x1000);
906 sysbus_init_mmio(sbd
, &s
->mem
);
909 static void mac_dbdma_realize(DeviceState
*dev
, Error
**errp
)
911 DBDMAState
*s
= MAC_DBDMA(dev
);
913 s
->bh
= qemu_bh_new(DBDMA_run_bh
, s
);
916 static void mac_dbdma_class_init(ObjectClass
*oc
, void *data
)
918 DeviceClass
*dc
= DEVICE_CLASS(oc
);
920 dc
->realize
= mac_dbdma_realize
;
921 dc
->reset
= mac_dbdma_reset
;
922 dc
->vmsd
= &vmstate_dbdma
;
925 static const TypeInfo mac_dbdma_type_info
= {
926 .name
= TYPE_MAC_DBDMA
,
927 .parent
= TYPE_SYS_BUS_DEVICE
,
928 .instance_size
= sizeof(DBDMAState
),
929 .instance_init
= mac_dbdma_init
,
930 .class_init
= mac_dbdma_class_init
933 static void mac_dbdma_register_types(void)
935 type_register_static(&mac_dbdma_type_info
);
938 type_init(mac_dbdma_register_types
)