xref: /openbmc/qemu/hw/misc/macio/mac_dbdma.c (revision 28ae3179fc52d2e4d870b635c4a412aab99759e7)
149ab747fSPaolo Bonzini /*
249ab747fSPaolo Bonzini  * PowerMac descriptor-based DMA emulation
349ab747fSPaolo Bonzini  *
449ab747fSPaolo Bonzini  * Copyright (c) 2005-2007 Fabrice Bellard
549ab747fSPaolo Bonzini  * Copyright (c) 2007 Jocelyn Mayer
649ab747fSPaolo Bonzini  * Copyright (c) 2009 Laurent Vivier
749ab747fSPaolo Bonzini  *
849ab747fSPaolo Bonzini  * some parts from linux-2.6.28, arch/powerpc/include/asm/dbdma.h
949ab747fSPaolo Bonzini  *
1049ab747fSPaolo Bonzini  *   Definitions for using the Apple Descriptor-Based DMA controller
1149ab747fSPaolo Bonzini  *   in Power Macintosh computers.
1249ab747fSPaolo Bonzini  *
1349ab747fSPaolo Bonzini  *   Copyright (C) 1996 Paul Mackerras.
1449ab747fSPaolo Bonzini  *
1549ab747fSPaolo Bonzini  * some parts from mol 0.9.71
1649ab747fSPaolo Bonzini  *
1749ab747fSPaolo Bonzini  *   Descriptor based DMA emulation
1849ab747fSPaolo Bonzini  *
1949ab747fSPaolo Bonzini  *   Copyright (C) 1998-2004 Samuel Rydh (samuel@ibrium.se)
2049ab747fSPaolo Bonzini  *
2149ab747fSPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
2249ab747fSPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
2349ab747fSPaolo Bonzini  * in the Software without restriction, including without limitation the rights
2449ab747fSPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2549ab747fSPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
2649ab747fSPaolo Bonzini  * furnished to do so, subject to the following conditions:
2749ab747fSPaolo Bonzini  *
2849ab747fSPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
2949ab747fSPaolo Bonzini  * all copies or substantial portions of the Software.
3049ab747fSPaolo Bonzini  *
3149ab747fSPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3249ab747fSPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3349ab747fSPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
3449ab747fSPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3549ab747fSPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3649ab747fSPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3749ab747fSPaolo Bonzini  * THE SOFTWARE.
3849ab747fSPaolo Bonzini  */
390b8fa32fSMarkus Armbruster 
400d75590dSPeter Maydell #include "qemu/osdep.h"
4164552b6bSMarkus Armbruster #include "hw/irq.h"
4249ab747fSPaolo Bonzini #include "hw/ppc/mac_dbdma.h"
43d6454270SMarkus Armbruster #include "migration/vmstate.h"
4449ab747fSPaolo Bonzini #include "qemu/main-loop.h"
450b8fa32fSMarkus Armbruster #include "qemu/module.h"
4603dd024fSPaolo Bonzini #include "qemu/log.h"
4788655881SMark Cave-Ayland #include "sysemu/dma.h"
4849ab747fSPaolo Bonzini 
4949ab747fSPaolo Bonzini /* debug DBDMA */
50ba0b17ddSMark Cave-Ayland #define DEBUG_DBDMA 0
513e49c439SMark Cave-Ayland #define DEBUG_DBDMA_CHANMASK ((1ull << DBDMA_CHANNELS) - 1)
5249ab747fSPaolo Bonzini 
53ba0b17ddSMark Cave-Ayland #define DBDMA_DPRINTF(fmt, ...) do { \
54ba0b17ddSMark Cave-Ayland     if (DEBUG_DBDMA) { \
55ba0b17ddSMark Cave-Ayland         printf("DBDMA: " fmt , ## __VA_ARGS__); \
56ba0b17ddSMark Cave-Ayland     } \
572562755eSEric Blake } while (0)
5849ab747fSPaolo Bonzini 
593e49c439SMark Cave-Ayland #define DBDMA_DPRINTFCH(ch, fmt, ...) do { \
603e49c439SMark Cave-Ayland     if (DEBUG_DBDMA) { \
613e49c439SMark Cave-Ayland         if ((1ul << (ch)->channel) & DEBUG_DBDMA_CHANMASK) { \
623e49c439SMark Cave-Ayland             printf("DBDMA[%02x]: " fmt , (ch)->channel, ## __VA_ARGS__); \
633e49c439SMark Cave-Ayland         } \
643e49c439SMark Cave-Ayland     } \
652562755eSEric Blake } while (0)
663e49c439SMark Cave-Ayland 
6749ab747fSPaolo Bonzini /*
6849ab747fSPaolo Bonzini  */
6949ab747fSPaolo Bonzini 
dbdma_from_ch(DBDMA_channel * ch)70d2f0ce21SAlexander Graf static DBDMAState *dbdma_from_ch(DBDMA_channel *ch)
71d2f0ce21SAlexander Graf {
72d2f0ce21SAlexander Graf     return container_of(ch, DBDMAState, channels[ch->channel]);
73d2f0ce21SAlexander Graf }
74d2f0ce21SAlexander Graf 
75ba0b17ddSMark Cave-Ayland #if DEBUG_DBDMA
dump_dbdma_cmd(DBDMA_channel * ch,dbdma_cmd * cmd)76b7d67813SMark Cave-Ayland static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd)
7749ab747fSPaolo Bonzini {
78b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "dbdma_cmd %p\n", cmd);
79b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    req_count 0x%04x\n", le16_to_cpu(cmd->req_count));
80b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    command 0x%04x\n", le16_to_cpu(cmd->command));
81b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    phy_addr 0x%08x\n", le32_to_cpu(cmd->phy_addr));
82b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    cmd_dep 0x%08x\n", le32_to_cpu(cmd->cmd_dep));
83b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    res_count 0x%04x\n", le16_to_cpu(cmd->res_count));
84b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    xfer_status 0x%04x\n",
85b7d67813SMark Cave-Ayland                     le16_to_cpu(cmd->xfer_status));
8649ab747fSPaolo Bonzini }
8749ab747fSPaolo Bonzini #else
dump_dbdma_cmd(DBDMA_channel * ch,dbdma_cmd * cmd)88b7d67813SMark Cave-Ayland static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd)
8949ab747fSPaolo Bonzini {
9049ab747fSPaolo Bonzini }
9149ab747fSPaolo Bonzini #endif
dbdma_cmdptr_load(DBDMA_channel * ch)9249ab747fSPaolo Bonzini static void dbdma_cmdptr_load(DBDMA_channel *ch)
9349ab747fSPaolo Bonzini {
943e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "dbdma_cmdptr_load 0x%08x\n",
9549ab747fSPaolo Bonzini                     ch->regs[DBDMA_CMDPTR_LO]);
9688655881SMark Cave-Ayland     dma_memory_read(&address_space_memory, ch->regs[DBDMA_CMDPTR_LO],
97ba06fe8aSPhilippe Mathieu-Daudé                     &ch->current, sizeof(dbdma_cmd), MEMTXATTRS_UNSPECIFIED);
9849ab747fSPaolo Bonzini }
9949ab747fSPaolo Bonzini 
dbdma_cmdptr_save(DBDMA_channel * ch)10049ab747fSPaolo Bonzini static void dbdma_cmdptr_save(DBDMA_channel *ch)
10149ab747fSPaolo Bonzini {
10277453882SBenjamin Herrenschmidt     DBDMA_DPRINTFCH(ch, "-> update 0x%08x stat=0x%08x, res=0x%04x\n",
10377453882SBenjamin Herrenschmidt                     ch->regs[DBDMA_CMDPTR_LO],
10449ab747fSPaolo Bonzini                     le16_to_cpu(ch->current.xfer_status),
10549ab747fSPaolo Bonzini                     le16_to_cpu(ch->current.res_count));
10688655881SMark Cave-Ayland     dma_memory_write(&address_space_memory, ch->regs[DBDMA_CMDPTR_LO],
107ba06fe8aSPhilippe Mathieu-Daudé                      &ch->current, sizeof(dbdma_cmd), MEMTXATTRS_UNSPECIFIED);
10849ab747fSPaolo Bonzini }
10949ab747fSPaolo Bonzini 
kill_channel(DBDMA_channel * ch)11049ab747fSPaolo Bonzini static void kill_channel(DBDMA_channel *ch)
11149ab747fSPaolo Bonzini {
1123e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "kill_channel\n");
11349ab747fSPaolo Bonzini 
11449ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] |= DEAD;
11549ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~ACTIVE;
11649ab747fSPaolo Bonzini 
11749ab747fSPaolo Bonzini     qemu_irq_raise(ch->irq);
11849ab747fSPaolo Bonzini }
11949ab747fSPaolo Bonzini 
conditional_interrupt(DBDMA_channel * ch)12049ab747fSPaolo Bonzini static void conditional_interrupt(DBDMA_channel *ch)
12149ab747fSPaolo Bonzini {
12249ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
12349ab747fSPaolo Bonzini     uint16_t intr;
12449ab747fSPaolo Bonzini     uint16_t sel_mask, sel_value;
12549ab747fSPaolo Bonzini     uint32_t status;
12649ab747fSPaolo Bonzini     int cond;
12749ab747fSPaolo Bonzini 
1283e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "%s\n", __func__);
12949ab747fSPaolo Bonzini 
13049ab747fSPaolo Bonzini     intr = le16_to_cpu(current->command) & INTR_MASK;
13149ab747fSPaolo Bonzini 
13249ab747fSPaolo Bonzini     switch(intr) {
13349ab747fSPaolo Bonzini     case INTR_NEVER:  /* don't interrupt */
13449ab747fSPaolo Bonzini         return;
13549ab747fSPaolo Bonzini     case INTR_ALWAYS: /* always interrupt */
13649ab747fSPaolo Bonzini         qemu_irq_raise(ch->irq);
1373e49c439SMark Cave-Ayland         DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
13849ab747fSPaolo Bonzini         return;
13949ab747fSPaolo Bonzini     }
14049ab747fSPaolo Bonzini 
14149ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS] & DEVSTAT;
14249ab747fSPaolo Bonzini 
14349ab747fSPaolo Bonzini     sel_mask = (ch->regs[DBDMA_INTR_SEL] >> 16) & 0x0f;
14449ab747fSPaolo Bonzini     sel_value = ch->regs[DBDMA_INTR_SEL] & 0x0f;
14549ab747fSPaolo Bonzini 
14649ab747fSPaolo Bonzini     cond = (status & sel_mask) == (sel_value & sel_mask);
14749ab747fSPaolo Bonzini 
14849ab747fSPaolo Bonzini     switch(intr) {
14949ab747fSPaolo Bonzini     case INTR_IFSET:  /* intr if condition bit is 1 */
15033ce36bbSAlexander Graf         if (cond) {
15149ab747fSPaolo Bonzini             qemu_irq_raise(ch->irq);
1523e49c439SMark Cave-Ayland             DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
15333ce36bbSAlexander Graf         }
15449ab747fSPaolo Bonzini         return;
15549ab747fSPaolo Bonzini     case INTR_IFCLR:  /* intr if condition bit is 0 */
15633ce36bbSAlexander Graf         if (!cond) {
15749ab747fSPaolo Bonzini             qemu_irq_raise(ch->irq);
1583e49c439SMark Cave-Ayland             DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
15933ce36bbSAlexander Graf         }
16049ab747fSPaolo Bonzini         return;
16149ab747fSPaolo Bonzini     }
16249ab747fSPaolo Bonzini }
16349ab747fSPaolo Bonzini 
conditional_wait(DBDMA_channel * ch)16449ab747fSPaolo Bonzini static int conditional_wait(DBDMA_channel *ch)
16549ab747fSPaolo Bonzini {
16649ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
16749ab747fSPaolo Bonzini     uint16_t wait;
16849ab747fSPaolo Bonzini     uint16_t sel_mask, sel_value;
16949ab747fSPaolo Bonzini     uint32_t status;
17049ab747fSPaolo Bonzini     int cond;
17177453882SBenjamin Herrenschmidt     int res = 0;
17249ab747fSPaolo Bonzini 
17349ab747fSPaolo Bonzini     wait = le16_to_cpu(current->command) & WAIT_MASK;
17449ab747fSPaolo Bonzini     switch(wait) {
17549ab747fSPaolo Bonzini     case WAIT_NEVER:  /* don't wait */
17649ab747fSPaolo Bonzini         return 0;
17749ab747fSPaolo Bonzini     case WAIT_ALWAYS: /* always wait */
17877453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "  [WAIT_ALWAYS]\n");
17949ab747fSPaolo Bonzini         return 1;
18049ab747fSPaolo Bonzini     }
18149ab747fSPaolo Bonzini 
18249ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS] & DEVSTAT;
18349ab747fSPaolo Bonzini 
18449ab747fSPaolo Bonzini     sel_mask = (ch->regs[DBDMA_WAIT_SEL] >> 16) & 0x0f;
18549ab747fSPaolo Bonzini     sel_value = ch->regs[DBDMA_WAIT_SEL] & 0x0f;
18649ab747fSPaolo Bonzini 
18749ab747fSPaolo Bonzini     cond = (status & sel_mask) == (sel_value & sel_mask);
18849ab747fSPaolo Bonzini 
18949ab747fSPaolo Bonzini     switch(wait) {
19049ab747fSPaolo Bonzini     case WAIT_IFSET:  /* wait if condition bit is 1 */
19177453882SBenjamin Herrenschmidt         if (cond) {
19277453882SBenjamin Herrenschmidt             res = 1;
19349ab747fSPaolo Bonzini         }
19477453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "  [WAIT_IFSET=%d]\n", res);
19577453882SBenjamin Herrenschmidt         break;
19677453882SBenjamin Herrenschmidt     case WAIT_IFCLR:  /* wait if condition bit is 0 */
19777453882SBenjamin Herrenschmidt         if (!cond) {
19877453882SBenjamin Herrenschmidt             res = 1;
19977453882SBenjamin Herrenschmidt         }
20077453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "  [WAIT_IFCLR=%d]\n", res);
20177453882SBenjamin Herrenschmidt         break;
20277453882SBenjamin Herrenschmidt     }
20377453882SBenjamin Herrenschmidt     return res;
20449ab747fSPaolo Bonzini }
20549ab747fSPaolo Bonzini 
next(DBDMA_channel * ch)20649ab747fSPaolo Bonzini static void next(DBDMA_channel *ch)
20749ab747fSPaolo Bonzini {
20849ab747fSPaolo Bonzini     uint32_t cp;
20949ab747fSPaolo Bonzini 
21049ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~BT;
21149ab747fSPaolo Bonzini 
21249ab747fSPaolo Bonzini     cp = ch->regs[DBDMA_CMDPTR_LO];
21349ab747fSPaolo Bonzini     ch->regs[DBDMA_CMDPTR_LO] = cp + sizeof(dbdma_cmd);
21449ab747fSPaolo Bonzini     dbdma_cmdptr_load(ch);
21549ab747fSPaolo Bonzini }
21649ab747fSPaolo Bonzini 
branch(DBDMA_channel * ch)21749ab747fSPaolo Bonzini static void branch(DBDMA_channel *ch)
21849ab747fSPaolo Bonzini {
21949ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
22049ab747fSPaolo Bonzini 
2213f0d4128SMark Cave-Ayland     ch->regs[DBDMA_CMDPTR_LO] = le32_to_cpu(current->cmd_dep);
22249ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] |= BT;
22349ab747fSPaolo Bonzini     dbdma_cmdptr_load(ch);
22449ab747fSPaolo Bonzini }
22549ab747fSPaolo Bonzini 
conditional_branch(DBDMA_channel * ch)22649ab747fSPaolo Bonzini static void conditional_branch(DBDMA_channel *ch)
22749ab747fSPaolo Bonzini {
22849ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
22949ab747fSPaolo Bonzini     uint16_t br;
23049ab747fSPaolo Bonzini     uint16_t sel_mask, sel_value;
23149ab747fSPaolo Bonzini     uint32_t status;
23249ab747fSPaolo Bonzini     int cond;
23349ab747fSPaolo Bonzini 
23449ab747fSPaolo Bonzini     /* check if we must branch */
23549ab747fSPaolo Bonzini 
23649ab747fSPaolo Bonzini     br = le16_to_cpu(current->command) & BR_MASK;
23749ab747fSPaolo Bonzini 
23849ab747fSPaolo Bonzini     switch(br) {
23949ab747fSPaolo Bonzini     case BR_NEVER:  /* don't branch */
24049ab747fSPaolo Bonzini         next(ch);
24149ab747fSPaolo Bonzini         return;
24249ab747fSPaolo Bonzini     case BR_ALWAYS: /* always branch */
24377453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "  [BR_ALWAYS]\n");
24449ab747fSPaolo Bonzini         branch(ch);
24549ab747fSPaolo Bonzini         return;
24649ab747fSPaolo Bonzini     }
24749ab747fSPaolo Bonzini 
24849ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS] & DEVSTAT;
24949ab747fSPaolo Bonzini 
25049ab747fSPaolo Bonzini     sel_mask = (ch->regs[DBDMA_BRANCH_SEL] >> 16) & 0x0f;
25149ab747fSPaolo Bonzini     sel_value = ch->regs[DBDMA_BRANCH_SEL] & 0x0f;
25249ab747fSPaolo Bonzini 
25349ab747fSPaolo Bonzini     cond = (status & sel_mask) == (sel_value & sel_mask);
25449ab747fSPaolo Bonzini 
25549ab747fSPaolo Bonzini     switch(br) {
25649ab747fSPaolo Bonzini     case BR_IFSET:  /* branch if condition bit is 1 */
25777453882SBenjamin Herrenschmidt         if (cond) {
25877453882SBenjamin Herrenschmidt             DBDMA_DPRINTFCH(ch, "  [BR_IFSET = 1]\n");
25949ab747fSPaolo Bonzini             branch(ch);
26077453882SBenjamin Herrenschmidt         } else {
26177453882SBenjamin Herrenschmidt             DBDMA_DPRINTFCH(ch, "  [BR_IFSET = 0]\n");
26249ab747fSPaolo Bonzini             next(ch);
26377453882SBenjamin Herrenschmidt         }
26449ab747fSPaolo Bonzini         return;
26549ab747fSPaolo Bonzini     case BR_IFCLR:  /* branch if condition bit is 0 */
26677453882SBenjamin Herrenschmidt         if (!cond) {
26777453882SBenjamin Herrenschmidt             DBDMA_DPRINTFCH(ch, "  [BR_IFCLR = 1]\n");
26849ab747fSPaolo Bonzini             branch(ch);
26977453882SBenjamin Herrenschmidt         } else {
27077453882SBenjamin Herrenschmidt             DBDMA_DPRINTFCH(ch, "  [BR_IFCLR = 0]\n");
27149ab747fSPaolo Bonzini             next(ch);
27277453882SBenjamin Herrenschmidt         }
27349ab747fSPaolo Bonzini         return;
27449ab747fSPaolo Bonzini     }
27549ab747fSPaolo Bonzini }
27649ab747fSPaolo Bonzini 
27749ab747fSPaolo Bonzini static void channel_run(DBDMA_channel *ch);
27849ab747fSPaolo Bonzini 
dbdma_end(DBDMA_io * io)27949ab747fSPaolo Bonzini static void dbdma_end(DBDMA_io *io)
28049ab747fSPaolo Bonzini {
28149ab747fSPaolo Bonzini     DBDMA_channel *ch = io->channel;
28249ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
28349ab747fSPaolo Bonzini 
2843e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "%s\n", __func__);
28533ce36bbSAlexander Graf 
28649ab747fSPaolo Bonzini     if (conditional_wait(ch))
28749ab747fSPaolo Bonzini         goto wait;
28849ab747fSPaolo Bonzini 
28949ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
29049ab747fSPaolo Bonzini     current->res_count = cpu_to_le16(io->len);
29149ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
29249ab747fSPaolo Bonzini     if (io->is_last)
29349ab747fSPaolo Bonzini         ch->regs[DBDMA_STATUS] &= ~FLUSH;
29449ab747fSPaolo Bonzini 
29549ab747fSPaolo Bonzini     conditional_interrupt(ch);
29649ab747fSPaolo Bonzini     conditional_branch(ch);
29749ab747fSPaolo Bonzini 
29849ab747fSPaolo Bonzini wait:
29903ee3b1eSAlexander Graf     /* Indicate that we're ready for a new DMA round */
30003ee3b1eSAlexander Graf     ch->io.processing = false;
30103ee3b1eSAlexander Graf 
30249ab747fSPaolo Bonzini     if ((ch->regs[DBDMA_STATUS] & RUN) &&
30349ab747fSPaolo Bonzini         (ch->regs[DBDMA_STATUS] & ACTIVE))
30449ab747fSPaolo Bonzini         channel_run(ch);
30549ab747fSPaolo Bonzini }
30649ab747fSPaolo Bonzini 
start_output(DBDMA_channel * ch,int key,uint32_t addr,uint16_t req_count,int is_last)30749ab747fSPaolo Bonzini static void start_output(DBDMA_channel *ch, int key, uint32_t addr,
30849ab747fSPaolo Bonzini                         uint16_t req_count, int is_last)
30949ab747fSPaolo Bonzini {
3103e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "start_output\n");
31149ab747fSPaolo Bonzini 
31249ab747fSPaolo Bonzini     /* KEY_REGS, KEY_DEVICE and KEY_STREAM
31349ab747fSPaolo Bonzini      * are not implemented in the mac-io chip
31449ab747fSPaolo Bonzini      */
31549ab747fSPaolo Bonzini 
3163e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "addr 0x%x key 0x%x\n", addr, key);
31749ab747fSPaolo Bonzini     if (!addr || key > KEY_STREAM3) {
31849ab747fSPaolo Bonzini         kill_channel(ch);
31949ab747fSPaolo Bonzini         return;
32049ab747fSPaolo Bonzini     }
32149ab747fSPaolo Bonzini 
32249ab747fSPaolo Bonzini     ch->io.addr = addr;
32349ab747fSPaolo Bonzini     ch->io.len = req_count;
32449ab747fSPaolo Bonzini     ch->io.is_last = is_last;
32549ab747fSPaolo Bonzini     ch->io.dma_end = dbdma_end;
32649ab747fSPaolo Bonzini     ch->io.is_dma_out = 1;
32703ee3b1eSAlexander Graf     ch->io.processing = true;
32849ab747fSPaolo Bonzini     if (ch->rw) {
32949ab747fSPaolo Bonzini         ch->rw(&ch->io);
33049ab747fSPaolo Bonzini     }
33149ab747fSPaolo Bonzini }
33249ab747fSPaolo Bonzini 
start_input(DBDMA_channel * ch,int key,uint32_t addr,uint16_t req_count,int is_last)33349ab747fSPaolo Bonzini static void start_input(DBDMA_channel *ch, int key, uint32_t addr,
33449ab747fSPaolo Bonzini                        uint16_t req_count, int is_last)
33549ab747fSPaolo Bonzini {
3363e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "start_input\n");
33749ab747fSPaolo Bonzini 
33849ab747fSPaolo Bonzini     /* KEY_REGS, KEY_DEVICE and KEY_STREAM
33949ab747fSPaolo Bonzini      * are not implemented in the mac-io chip
34049ab747fSPaolo Bonzini      */
34149ab747fSPaolo Bonzini 
3423e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "addr 0x%x key 0x%x\n", addr, key);
34349ab747fSPaolo Bonzini     if (!addr || key > KEY_STREAM3) {
34449ab747fSPaolo Bonzini         kill_channel(ch);
34549ab747fSPaolo Bonzini         return;
34649ab747fSPaolo Bonzini     }
34749ab747fSPaolo Bonzini 
34849ab747fSPaolo Bonzini     ch->io.addr = addr;
34949ab747fSPaolo Bonzini     ch->io.len = req_count;
35049ab747fSPaolo Bonzini     ch->io.is_last = is_last;
35149ab747fSPaolo Bonzini     ch->io.dma_end = dbdma_end;
35249ab747fSPaolo Bonzini     ch->io.is_dma_out = 0;
35303ee3b1eSAlexander Graf     ch->io.processing = true;
35449ab747fSPaolo Bonzini     if (ch->rw) {
35549ab747fSPaolo Bonzini         ch->rw(&ch->io);
35649ab747fSPaolo Bonzini     }
35749ab747fSPaolo Bonzini }
35849ab747fSPaolo Bonzini 
load_word(DBDMA_channel * ch,int key,uint32_t addr,uint16_t len)35949ab747fSPaolo Bonzini static void load_word(DBDMA_channel *ch, int key, uint32_t addr,
36049ab747fSPaolo Bonzini                      uint16_t len)
36149ab747fSPaolo Bonzini {
36249ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
36349ab747fSPaolo Bonzini 
364e12f50b9SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "load_word %d bytes, addr=%08x\n", len, addr);
36549ab747fSPaolo Bonzini 
36649ab747fSPaolo Bonzini     /* only implements KEY_SYSTEM */
36749ab747fSPaolo Bonzini 
36849ab747fSPaolo Bonzini     if (key != KEY_SYSTEM) {
36949ab747fSPaolo Bonzini         printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
37049ab747fSPaolo Bonzini         kill_channel(ch);
37149ab747fSPaolo Bonzini         return;
37249ab747fSPaolo Bonzini     }
37349ab747fSPaolo Bonzini 
374ba06fe8aSPhilippe Mathieu-Daudé     dma_memory_read(&address_space_memory, addr, &current->cmd_dep, len,
375ba06fe8aSPhilippe Mathieu-Daudé                     MEMTXATTRS_UNSPECIFIED);
37649ab747fSPaolo Bonzini 
37749ab747fSPaolo Bonzini     if (conditional_wait(ch))
37849ab747fSPaolo Bonzini         goto wait;
37949ab747fSPaolo Bonzini 
38049ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
38149ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
38249ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~FLUSH;
38349ab747fSPaolo Bonzini 
38449ab747fSPaolo Bonzini     conditional_interrupt(ch);
38549ab747fSPaolo Bonzini     next(ch);
38649ab747fSPaolo Bonzini 
38749ab747fSPaolo Bonzini wait:
388d2f0ce21SAlexander Graf     DBDMA_kick(dbdma_from_ch(ch));
38949ab747fSPaolo Bonzini }
39049ab747fSPaolo Bonzini 
store_word(DBDMA_channel * ch,int key,uint32_t addr,uint16_t len)39149ab747fSPaolo Bonzini static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
39249ab747fSPaolo Bonzini                       uint16_t len)
39349ab747fSPaolo Bonzini {
39449ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
39549ab747fSPaolo Bonzini 
396e12f50b9SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "store_word %d bytes, addr=%08x pa=%x\n",
397e12f50b9SMark Cave-Ayland                     len, addr, le32_to_cpu(current->cmd_dep));
39849ab747fSPaolo Bonzini 
39949ab747fSPaolo Bonzini     /* only implements KEY_SYSTEM */
40049ab747fSPaolo Bonzini 
40149ab747fSPaolo Bonzini     if (key != KEY_SYSTEM) {
40249ab747fSPaolo Bonzini         printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
40349ab747fSPaolo Bonzini         kill_channel(ch);
40449ab747fSPaolo Bonzini         return;
40549ab747fSPaolo Bonzini     }
40649ab747fSPaolo Bonzini 
407ba06fe8aSPhilippe Mathieu-Daudé     dma_memory_write(&address_space_memory, addr, &current->cmd_dep, len,
408ba06fe8aSPhilippe Mathieu-Daudé                      MEMTXATTRS_UNSPECIFIED);
40949ab747fSPaolo Bonzini 
41049ab747fSPaolo Bonzini     if (conditional_wait(ch))
41149ab747fSPaolo Bonzini         goto wait;
41249ab747fSPaolo Bonzini 
41349ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
41449ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
41549ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~FLUSH;
41649ab747fSPaolo Bonzini 
41749ab747fSPaolo Bonzini     conditional_interrupt(ch);
41849ab747fSPaolo Bonzini     next(ch);
41949ab747fSPaolo Bonzini 
42049ab747fSPaolo Bonzini wait:
421d2f0ce21SAlexander Graf     DBDMA_kick(dbdma_from_ch(ch));
42249ab747fSPaolo Bonzini }
42349ab747fSPaolo Bonzini 
nop(DBDMA_channel * ch)42449ab747fSPaolo Bonzini static void nop(DBDMA_channel *ch)
42549ab747fSPaolo Bonzini {
42649ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
42749ab747fSPaolo Bonzini 
42849ab747fSPaolo Bonzini     if (conditional_wait(ch))
42949ab747fSPaolo Bonzini         goto wait;
43049ab747fSPaolo Bonzini 
43149ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
43249ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
43349ab747fSPaolo Bonzini 
43449ab747fSPaolo Bonzini     conditional_interrupt(ch);
43549ab747fSPaolo Bonzini     conditional_branch(ch);
43649ab747fSPaolo Bonzini 
43749ab747fSPaolo Bonzini wait:
438d2f0ce21SAlexander Graf     DBDMA_kick(dbdma_from_ch(ch));
43949ab747fSPaolo Bonzini }
44049ab747fSPaolo Bonzini 
stop(DBDMA_channel * ch)44149ab747fSPaolo Bonzini static void stop(DBDMA_channel *ch)
44249ab747fSPaolo Bonzini {
44377453882SBenjamin Herrenschmidt     ch->regs[DBDMA_STATUS] &= ~(ACTIVE);
44449ab747fSPaolo Bonzini 
44549ab747fSPaolo Bonzini     /* the stop command does not increment command pointer */
44649ab747fSPaolo Bonzini }
44749ab747fSPaolo Bonzini 
channel_run(DBDMA_channel * ch)44849ab747fSPaolo Bonzini static void channel_run(DBDMA_channel *ch)
44949ab747fSPaolo Bonzini {
45049ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
45149ab747fSPaolo Bonzini     uint16_t cmd, key;
45249ab747fSPaolo Bonzini     uint16_t req_count;
45349ab747fSPaolo Bonzini     uint32_t phy_addr;
45449ab747fSPaolo Bonzini 
4553e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "channel_run\n");
456b7d67813SMark Cave-Ayland     dump_dbdma_cmd(ch, current);
45749ab747fSPaolo Bonzini 
45849ab747fSPaolo Bonzini     /* clear WAKE flag at command fetch */
45949ab747fSPaolo Bonzini 
46049ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~WAKE;
46149ab747fSPaolo Bonzini 
46249ab747fSPaolo Bonzini     cmd = le16_to_cpu(current->command) & COMMAND_MASK;
46349ab747fSPaolo Bonzini 
46449ab747fSPaolo Bonzini     switch (cmd) {
46549ab747fSPaolo Bonzini     case DBDMA_NOP:
46649ab747fSPaolo Bonzini         nop(ch);
46749ab747fSPaolo Bonzini         return;
46849ab747fSPaolo Bonzini 
46949ab747fSPaolo Bonzini     case DBDMA_STOP:
47049ab747fSPaolo Bonzini         stop(ch);
47149ab747fSPaolo Bonzini         return;
47249ab747fSPaolo Bonzini     }
47349ab747fSPaolo Bonzini 
47449ab747fSPaolo Bonzini     key = le16_to_cpu(current->command) & 0x0700;
47549ab747fSPaolo Bonzini     req_count = le16_to_cpu(current->req_count);
47649ab747fSPaolo Bonzini     phy_addr = le32_to_cpu(current->phy_addr);
47749ab747fSPaolo Bonzini 
47849ab747fSPaolo Bonzini     if (key == KEY_STREAM4) {
47949ab747fSPaolo Bonzini         printf("command %x, invalid key 4\n", cmd);
48049ab747fSPaolo Bonzini         kill_channel(ch);
48149ab747fSPaolo Bonzini         return;
48249ab747fSPaolo Bonzini     }
48349ab747fSPaolo Bonzini 
48449ab747fSPaolo Bonzini     switch (cmd) {
48549ab747fSPaolo Bonzini     case OUTPUT_MORE:
48677453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* OUTPUT_MORE *\n");
48749ab747fSPaolo Bonzini         start_output(ch, key, phy_addr, req_count, 0);
48849ab747fSPaolo Bonzini         return;
48949ab747fSPaolo Bonzini 
49049ab747fSPaolo Bonzini     case OUTPUT_LAST:
49177453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* OUTPUT_LAST *\n");
49249ab747fSPaolo Bonzini         start_output(ch, key, phy_addr, req_count, 1);
49349ab747fSPaolo Bonzini         return;
49449ab747fSPaolo Bonzini 
49549ab747fSPaolo Bonzini     case INPUT_MORE:
49677453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* INPUT_MORE *\n");
49749ab747fSPaolo Bonzini         start_input(ch, key, phy_addr, req_count, 0);
49849ab747fSPaolo Bonzini         return;
49949ab747fSPaolo Bonzini 
50049ab747fSPaolo Bonzini     case INPUT_LAST:
50177453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* INPUT_LAST *\n");
50249ab747fSPaolo Bonzini         start_input(ch, key, phy_addr, req_count, 1);
50349ab747fSPaolo Bonzini         return;
50449ab747fSPaolo Bonzini     }
50549ab747fSPaolo Bonzini 
50649ab747fSPaolo Bonzini     if (key < KEY_REGS) {
50749ab747fSPaolo Bonzini         printf("command %x, invalid key %x\n", cmd, key);
50849ab747fSPaolo Bonzini         key = KEY_SYSTEM;
50949ab747fSPaolo Bonzini     }
51049ab747fSPaolo Bonzini 
51149ab747fSPaolo Bonzini     /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
51249ab747fSPaolo Bonzini      * and BRANCH is invalid
51349ab747fSPaolo Bonzini      */
51449ab747fSPaolo Bonzini 
51549ab747fSPaolo Bonzini     req_count = req_count & 0x0007;
51649ab747fSPaolo Bonzini     if (req_count & 0x4) {
51749ab747fSPaolo Bonzini         req_count = 4;
51849ab747fSPaolo Bonzini         phy_addr &= ~3;
51949ab747fSPaolo Bonzini     } else if (req_count & 0x2) {
52049ab747fSPaolo Bonzini         req_count = 2;
52149ab747fSPaolo Bonzini         phy_addr &= ~1;
52249ab747fSPaolo Bonzini     } else
52349ab747fSPaolo Bonzini         req_count = 1;
52449ab747fSPaolo Bonzini 
52549ab747fSPaolo Bonzini     switch (cmd) {
52649ab747fSPaolo Bonzini     case LOAD_WORD:
52777453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* LOAD_WORD *\n");
52849ab747fSPaolo Bonzini         load_word(ch, key, phy_addr, req_count);
52949ab747fSPaolo Bonzini         return;
53049ab747fSPaolo Bonzini 
53149ab747fSPaolo Bonzini     case STORE_WORD:
53277453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* STORE_WORD *\n");
53349ab747fSPaolo Bonzini         store_word(ch, key, phy_addr, req_count);
53449ab747fSPaolo Bonzini         return;
53549ab747fSPaolo Bonzini     }
53649ab747fSPaolo Bonzini }
53749ab747fSPaolo Bonzini 
DBDMA_run(DBDMAState * s)53849ab747fSPaolo Bonzini static void DBDMA_run(DBDMAState *s)
53949ab747fSPaolo Bonzini {
54049ab747fSPaolo Bonzini     int channel;
54149ab747fSPaolo Bonzini 
54249ab747fSPaolo Bonzini     for (channel = 0; channel < DBDMA_CHANNELS; channel++) {
54349ab747fSPaolo Bonzini         DBDMA_channel *ch = &s->channels[channel];
54449ab747fSPaolo Bonzini         uint32_t status = ch->regs[DBDMA_STATUS];
54503ee3b1eSAlexander Graf         if (!ch->io.processing && (status & RUN) && (status & ACTIVE)) {
54649ab747fSPaolo Bonzini             channel_run(ch);
54749ab747fSPaolo Bonzini         }
54849ab747fSPaolo Bonzini     }
54949ab747fSPaolo Bonzini }
55049ab747fSPaolo Bonzini 
DBDMA_run_bh(void * opaque)55149ab747fSPaolo Bonzini static void DBDMA_run_bh(void *opaque)
55249ab747fSPaolo Bonzini {
55349ab747fSPaolo Bonzini     DBDMAState *s = opaque;
55449ab747fSPaolo Bonzini 
5553e49c439SMark Cave-Ayland     DBDMA_DPRINTF("-> DBDMA_run_bh\n");
55649ab747fSPaolo Bonzini     DBDMA_run(s);
5573e49c439SMark Cave-Ayland     DBDMA_DPRINTF("<- DBDMA_run_bh\n");
55849ab747fSPaolo Bonzini }
55949ab747fSPaolo Bonzini 
DBDMA_kick(DBDMAState * dbdma)560d1e562deSAlexander Graf void DBDMA_kick(DBDMAState *dbdma)
561d1e562deSAlexander Graf {
562d2f0ce21SAlexander Graf     qemu_bh_schedule(dbdma->bh);
563d1e562deSAlexander Graf }
564d1e562deSAlexander Graf 
DBDMA_register_channel(void * dbdma,int nchan,qemu_irq irq,DBDMA_rw rw,DBDMA_flush flush,void * opaque)56549ab747fSPaolo Bonzini void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
56649ab747fSPaolo Bonzini                             DBDMA_rw rw, DBDMA_flush flush,
56749ab747fSPaolo Bonzini                             void *opaque)
56849ab747fSPaolo Bonzini {
56949ab747fSPaolo Bonzini     DBDMAState *s = dbdma;
57049ab747fSPaolo Bonzini     DBDMA_channel *ch = &s->channels[nchan];
57149ab747fSPaolo Bonzini 
5723e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "DBDMA_register_channel 0x%x\n", nchan);
57349ab747fSPaolo Bonzini 
5742d7d06d8SHervé Poussineau     assert(rw);
5752d7d06d8SHervé Poussineau     assert(flush);
5762d7d06d8SHervé Poussineau 
57749ab747fSPaolo Bonzini     ch->irq = irq;
57849ab747fSPaolo Bonzini     ch->rw = rw;
57949ab747fSPaolo Bonzini     ch->flush = flush;
58049ab747fSPaolo Bonzini     ch->io.opaque = opaque;
58149ab747fSPaolo Bonzini }
58249ab747fSPaolo Bonzini 
dbdma_control_write(DBDMA_channel * ch)58377453882SBenjamin Herrenschmidt static void dbdma_control_write(DBDMA_channel *ch)
58449ab747fSPaolo Bonzini {
58549ab747fSPaolo Bonzini     uint16_t mask, value;
58649ab747fSPaolo Bonzini     uint32_t status;
58777453882SBenjamin Herrenschmidt     bool do_flush = false;
58849ab747fSPaolo Bonzini 
58949ab747fSPaolo Bonzini     mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
59049ab747fSPaolo Bonzini     value = ch->regs[DBDMA_CONTROL] & 0xffff;
59149ab747fSPaolo Bonzini 
59277453882SBenjamin Herrenschmidt     /* This is the status register which we'll update
59377453882SBenjamin Herrenschmidt      * appropriately and store back
59477453882SBenjamin Herrenschmidt      */
59549ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS];
59649ab747fSPaolo Bonzini 
59777453882SBenjamin Herrenschmidt     /* RUN and PAUSE are bits under SW control only
59877453882SBenjamin Herrenschmidt      * FLUSH and WAKE are set by SW and cleared by HW
59977453882SBenjamin Herrenschmidt      * DEAD, ACTIVE and BT are only under HW control
60077453882SBenjamin Herrenschmidt      *
60177453882SBenjamin Herrenschmidt      * We handle ACTIVE separately at the end of the
60277453882SBenjamin Herrenschmidt      * logic to ensure all cases are covered.
60377453882SBenjamin Herrenschmidt      */
60449ab747fSPaolo Bonzini 
60577453882SBenjamin Herrenschmidt     /* Setting RUN will tentatively activate the channel
60677453882SBenjamin Herrenschmidt      */
60777453882SBenjamin Herrenschmidt     if ((mask & RUN) && (value & RUN)) {
60877453882SBenjamin Herrenschmidt         status |= RUN;
60977453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " Setting RUN !\n");
61049ab747fSPaolo Bonzini     }
61177453882SBenjamin Herrenschmidt 
61277453882SBenjamin Herrenschmidt     /* Clearing RUN 1->0 will stop the channel */
61377453882SBenjamin Herrenschmidt     if ((mask & RUN) && !(value & RUN)) {
61477453882SBenjamin Herrenschmidt         /* This has the side effect of clearing the DEAD bit */
61577453882SBenjamin Herrenschmidt         status &= ~(DEAD | RUN);
61677453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " Clearing RUN !\n");
61777453882SBenjamin Herrenschmidt     }
61877453882SBenjamin Herrenschmidt 
61977453882SBenjamin Herrenschmidt     /* Setting WAKE wakes up an idle channel if it's running
62077453882SBenjamin Herrenschmidt      *
62177453882SBenjamin Herrenschmidt      * Note: The doc doesn't say so but assume that only works
62277453882SBenjamin Herrenschmidt      * on a channel whose RUN bit is set.
62377453882SBenjamin Herrenschmidt      *
62477453882SBenjamin Herrenschmidt      * We set WAKE in status, it's not terribly useful as it will
62577453882SBenjamin Herrenschmidt      * be cleared on the next command fetch but it seems to mimmic
62677453882SBenjamin Herrenschmidt      * the HW behaviour and is useful for the way we handle
62777453882SBenjamin Herrenschmidt      * ACTIVE further down.
62877453882SBenjamin Herrenschmidt      */
62977453882SBenjamin Herrenschmidt     if ((mask & WAKE) && (value & WAKE) && (status & RUN)) {
63077453882SBenjamin Herrenschmidt         status |= WAKE;
63177453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " Setting WAKE !\n");
63277453882SBenjamin Herrenschmidt     }
63377453882SBenjamin Herrenschmidt 
63477453882SBenjamin Herrenschmidt     /* PAUSE being set will deactivate (or prevent activation)
63577453882SBenjamin Herrenschmidt      * of the channel. We just copy it over for now, ACTIVE will
63677453882SBenjamin Herrenschmidt      * be re-evaluated later.
63777453882SBenjamin Herrenschmidt      */
63877453882SBenjamin Herrenschmidt     if (mask & PAUSE) {
63977453882SBenjamin Herrenschmidt         status = (status & ~PAUSE) | (value & PAUSE);
64077453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " %sing PAUSE !\n",
64177453882SBenjamin Herrenschmidt                         (value & PAUSE) ? "sett" : "clear");
64277453882SBenjamin Herrenschmidt     }
64377453882SBenjamin Herrenschmidt 
64477453882SBenjamin Herrenschmidt     /* FLUSH is its own thing */
64577453882SBenjamin Herrenschmidt     if ((mask & FLUSH) && (value & FLUSH))  {
64677453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " Setting FLUSH !\n");
64777453882SBenjamin Herrenschmidt         /* We set flush directly in the status register, we do *NOT*
64877453882SBenjamin Herrenschmidt          * set it in "status" so that it gets naturally cleared when
64977453882SBenjamin Herrenschmidt          * we update the status register further down. That way it
65077453882SBenjamin Herrenschmidt          * will be set only during the HW flush operation so it is
65177453882SBenjamin Herrenschmidt          * visible to any completions happening during that time.
65277453882SBenjamin Herrenschmidt          */
65377453882SBenjamin Herrenschmidt         ch->regs[DBDMA_STATUS] |= FLUSH;
65477453882SBenjamin Herrenschmidt         do_flush = true;
65577453882SBenjamin Herrenschmidt     }
65677453882SBenjamin Herrenschmidt 
65777453882SBenjamin Herrenschmidt     /* If either RUN or PAUSE is clear, so should ACTIVE be,
65877453882SBenjamin Herrenschmidt      * otherwise, ACTIVE will be set if we modified RUN, PAUSE or
65977453882SBenjamin Herrenschmidt      * set WAKE. That means that PAUSE was just cleared, RUN was
66077453882SBenjamin Herrenschmidt      * just set or WAKE was just set.
66177453882SBenjamin Herrenschmidt      */
66277453882SBenjamin Herrenschmidt     if ((status & PAUSE) || !(status & RUN)) {
66349ab747fSPaolo Bonzini         status &= ~ACTIVE;
66477453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "  -> ACTIVE down !\n");
66577453882SBenjamin Herrenschmidt 
66677453882SBenjamin Herrenschmidt         /* We stopped processing, we want the underlying HW command
66777453882SBenjamin Herrenschmidt          * to complete *before* we clear the ACTIVE bit. Otherwise
66877453882SBenjamin Herrenschmidt          * we can get into a situation where the command status will
66977453882SBenjamin Herrenschmidt          * have RUN or ACTIVE not set which is going to confuse the
67077453882SBenjamin Herrenschmidt          * MacOS driver.
67177453882SBenjamin Herrenschmidt          */
67277453882SBenjamin Herrenschmidt         do_flush = true;
67377453882SBenjamin Herrenschmidt     } else if (mask & (RUN | PAUSE)) {
67477453882SBenjamin Herrenschmidt         status |= ACTIVE;
67577453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
67677453882SBenjamin Herrenschmidt     } else if ((mask & WAKE) && (value & WAKE)) {
67777453882SBenjamin Herrenschmidt         status |= ACTIVE;
67877453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
6791cde732dSMark Cave-Ayland     }
6801cde732dSMark Cave-Ayland 
68177453882SBenjamin Herrenschmidt     DBDMA_DPRINTFCH(ch, " new status=0x%08x\n", status);
68277453882SBenjamin Herrenschmidt 
68377453882SBenjamin Herrenschmidt     /* If we need to flush the underlying HW, do it now, this happens
68477453882SBenjamin Herrenschmidt      * both on FLUSH commands and when stopping the channel for safety.
68577453882SBenjamin Herrenschmidt      */
68677453882SBenjamin Herrenschmidt     if (do_flush && ch->flush) {
68749ab747fSPaolo Bonzini         ch->flush(&ch->io);
68849ab747fSPaolo Bonzini     }
68949ab747fSPaolo Bonzini 
69077453882SBenjamin Herrenschmidt     /* Finally update the status register image */
69149ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] = status;
69249ab747fSPaolo Bonzini 
69377453882SBenjamin Herrenschmidt     /* If active, make sure the BH gets to run */
694d2f0ce21SAlexander Graf     if (status & ACTIVE) {
695d2f0ce21SAlexander Graf         DBDMA_kick(dbdma_from_ch(ch));
696d2f0ce21SAlexander Graf     }
697d2f0ce21SAlexander Graf }
69849ab747fSPaolo Bonzini 
dbdma_write(void * opaque,hwaddr addr,uint64_t value,unsigned size)69949ab747fSPaolo Bonzini static void dbdma_write(void *opaque, hwaddr addr,
70049ab747fSPaolo Bonzini                         uint64_t value, unsigned size)
70149ab747fSPaolo Bonzini {
70249ab747fSPaolo Bonzini     int channel = addr >> DBDMA_CHANNEL_SHIFT;
70349ab747fSPaolo Bonzini     DBDMAState *s = opaque;
70449ab747fSPaolo Bonzini     DBDMA_channel *ch = &s->channels[channel];
70549ab747fSPaolo Bonzini     int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
70649ab747fSPaolo Bonzini 
707883f2c59SPhilippe Mathieu-Daudé     DBDMA_DPRINTFCH(ch, "writel 0x" HWADDR_FMT_plx " <= 0x%08"PRIx64"\n",
70858c0c311SAlexander Graf                     addr, value);
7093e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
71049ab747fSPaolo Bonzini                     (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
71149ab747fSPaolo Bonzini 
7127eaba824SAlexander Graf     /* cmdptr cannot be modified if channel is ACTIVE */
71349ab747fSPaolo Bonzini 
7147eaba824SAlexander Graf     if (reg == DBDMA_CMDPTR_LO && (ch->regs[DBDMA_STATUS] & ACTIVE)) {
71549ab747fSPaolo Bonzini         return;
7167eaba824SAlexander Graf     }
71749ab747fSPaolo Bonzini 
71849ab747fSPaolo Bonzini     ch->regs[reg] = value;
71949ab747fSPaolo Bonzini 
72049ab747fSPaolo Bonzini     switch(reg) {
72149ab747fSPaolo Bonzini     case DBDMA_CONTROL:
72249ab747fSPaolo Bonzini         dbdma_control_write(ch);
72349ab747fSPaolo Bonzini         break;
72449ab747fSPaolo Bonzini     case DBDMA_CMDPTR_LO:
72549ab747fSPaolo Bonzini         /* 16-byte aligned */
72649ab747fSPaolo Bonzini         ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
72749ab747fSPaolo Bonzini         dbdma_cmdptr_load(ch);
72849ab747fSPaolo Bonzini         break;
72949ab747fSPaolo Bonzini     case DBDMA_STATUS:
73049ab747fSPaolo Bonzini     case DBDMA_INTR_SEL:
73149ab747fSPaolo Bonzini     case DBDMA_BRANCH_SEL:
73249ab747fSPaolo Bonzini     case DBDMA_WAIT_SEL:
73349ab747fSPaolo Bonzini         /* nothing to do */
73449ab747fSPaolo Bonzini         break;
73549ab747fSPaolo Bonzini     case DBDMA_XFER_MODE:
73649ab747fSPaolo Bonzini     case DBDMA_CMDPTR_HI:
73749ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_HI:
73849ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_LO:
73949ab747fSPaolo Bonzini     case DBDMA_ADDRESS_HI:
74049ab747fSPaolo Bonzini     case DBDMA_BRANCH_ADDR_HI:
74149ab747fSPaolo Bonzini     case DBDMA_RES1:
74249ab747fSPaolo Bonzini     case DBDMA_RES2:
74349ab747fSPaolo Bonzini     case DBDMA_RES3:
74449ab747fSPaolo Bonzini     case DBDMA_RES4:
74549ab747fSPaolo Bonzini         /* unused */
74649ab747fSPaolo Bonzini         break;
74749ab747fSPaolo Bonzini     }
74849ab747fSPaolo Bonzini }
74949ab747fSPaolo Bonzini 
dbdma_read(void * opaque,hwaddr addr,unsigned size)75049ab747fSPaolo Bonzini static uint64_t dbdma_read(void *opaque, hwaddr addr,
75149ab747fSPaolo Bonzini                            unsigned size)
75249ab747fSPaolo Bonzini {
75349ab747fSPaolo Bonzini     uint32_t value;
75449ab747fSPaolo Bonzini     int channel = addr >> DBDMA_CHANNEL_SHIFT;
75549ab747fSPaolo Bonzini     DBDMAState *s = opaque;
75649ab747fSPaolo Bonzini     DBDMA_channel *ch = &s->channels[channel];
75749ab747fSPaolo Bonzini     int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
75849ab747fSPaolo Bonzini 
75949ab747fSPaolo Bonzini     value = ch->regs[reg];
76049ab747fSPaolo Bonzini 
76149ab747fSPaolo Bonzini     switch(reg) {
76249ab747fSPaolo Bonzini     case DBDMA_CONTROL:
76377453882SBenjamin Herrenschmidt         value = ch->regs[DBDMA_STATUS];
76449ab747fSPaolo Bonzini         break;
76549ab747fSPaolo Bonzini     case DBDMA_STATUS:
76649ab747fSPaolo Bonzini     case DBDMA_CMDPTR_LO:
76749ab747fSPaolo Bonzini     case DBDMA_INTR_SEL:
76849ab747fSPaolo Bonzini     case DBDMA_BRANCH_SEL:
76949ab747fSPaolo Bonzini     case DBDMA_WAIT_SEL:
77049ab747fSPaolo Bonzini         /* nothing to do */
77149ab747fSPaolo Bonzini         break;
77249ab747fSPaolo Bonzini     case DBDMA_XFER_MODE:
77349ab747fSPaolo Bonzini     case DBDMA_CMDPTR_HI:
77449ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_HI:
77549ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_LO:
77649ab747fSPaolo Bonzini     case DBDMA_ADDRESS_HI:
77749ab747fSPaolo Bonzini     case DBDMA_BRANCH_ADDR_HI:
77849ab747fSPaolo Bonzini         /* unused */
77949ab747fSPaolo Bonzini         value = 0;
78049ab747fSPaolo Bonzini         break;
78149ab747fSPaolo Bonzini     case DBDMA_RES1:
78249ab747fSPaolo Bonzini     case DBDMA_RES2:
78349ab747fSPaolo Bonzini     case DBDMA_RES3:
78449ab747fSPaolo Bonzini     case DBDMA_RES4:
78549ab747fSPaolo Bonzini         /* reserved */
78649ab747fSPaolo Bonzini         break;
78749ab747fSPaolo Bonzini     }
78849ab747fSPaolo Bonzini 
789883f2c59SPhilippe Mathieu-Daudé     DBDMA_DPRINTFCH(ch, "readl 0x" HWADDR_FMT_plx " => 0x%08x\n", addr, value);
79077453882SBenjamin Herrenschmidt     DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
79177453882SBenjamin Herrenschmidt                     (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
79277453882SBenjamin Herrenschmidt 
79349ab747fSPaolo Bonzini     return value;
79449ab747fSPaolo Bonzini }
79549ab747fSPaolo Bonzini 
79649ab747fSPaolo Bonzini static const MemoryRegionOps dbdma_ops = {
79749ab747fSPaolo Bonzini     .read = dbdma_read,
79849ab747fSPaolo Bonzini     .write = dbdma_write,
79949ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
80049ab747fSPaolo Bonzini     .valid = {
80149ab747fSPaolo Bonzini         .min_access_size = 4,
80249ab747fSPaolo Bonzini         .max_access_size = 4,
80349ab747fSPaolo Bonzini     },
80449ab747fSPaolo Bonzini };
80549ab747fSPaolo Bonzini 
806627be2f2SMark Cave-Ayland static const VMStateDescription vmstate_dbdma_io = {
807627be2f2SMark Cave-Ayland     .name = "dbdma_io",
80849ab747fSPaolo Bonzini     .version_id = 0,
80949ab747fSPaolo Bonzini     .minimum_version_id = 0,
810ce933d70SRichard Henderson     .fields = (const VMStateField[]) {
811627be2f2SMark Cave-Ayland         VMSTATE_UINT64(addr, struct DBDMA_io),
812627be2f2SMark Cave-Ayland         VMSTATE_INT32(len, struct DBDMA_io),
813627be2f2SMark Cave-Ayland         VMSTATE_INT32(is_last, struct DBDMA_io),
814627be2f2SMark Cave-Ayland         VMSTATE_INT32(is_dma_out, struct DBDMA_io),
815627be2f2SMark Cave-Ayland         VMSTATE_BOOL(processing, struct DBDMA_io),
816627be2f2SMark Cave-Ayland         VMSTATE_END_OF_LIST()
817627be2f2SMark Cave-Ayland     }
818627be2f2SMark Cave-Ayland };
819627be2f2SMark Cave-Ayland 
820627be2f2SMark Cave-Ayland static const VMStateDescription vmstate_dbdma_cmd = {
821627be2f2SMark Cave-Ayland     .name = "dbdma_cmd",
822627be2f2SMark Cave-Ayland     .version_id = 0,
823627be2f2SMark Cave-Ayland     .minimum_version_id = 0,
824ce933d70SRichard Henderson     .fields = (const VMStateField[]) {
825627be2f2SMark Cave-Ayland         VMSTATE_UINT16(req_count, dbdma_cmd),
826627be2f2SMark Cave-Ayland         VMSTATE_UINT16(command, dbdma_cmd),
827627be2f2SMark Cave-Ayland         VMSTATE_UINT32(phy_addr, dbdma_cmd),
828627be2f2SMark Cave-Ayland         VMSTATE_UINT32(cmd_dep, dbdma_cmd),
829627be2f2SMark Cave-Ayland         VMSTATE_UINT16(res_count, dbdma_cmd),
830627be2f2SMark Cave-Ayland         VMSTATE_UINT16(xfer_status, dbdma_cmd),
831627be2f2SMark Cave-Ayland         VMSTATE_END_OF_LIST()
832627be2f2SMark Cave-Ayland     }
833627be2f2SMark Cave-Ayland };
834627be2f2SMark Cave-Ayland 
835627be2f2SMark Cave-Ayland static const VMStateDescription vmstate_dbdma_channel = {
836627be2f2SMark Cave-Ayland     .name = "dbdma_channel",
837627be2f2SMark Cave-Ayland     .version_id = 1,
838627be2f2SMark Cave-Ayland     .minimum_version_id = 1,
839ce933d70SRichard Henderson     .fields = (const VMStateField[]) {
84049ab747fSPaolo Bonzini         VMSTATE_UINT32_ARRAY(regs, struct DBDMA_channel, DBDMA_REGS),
841627be2f2SMark Cave-Ayland         VMSTATE_STRUCT(io, struct DBDMA_channel, 0, vmstate_dbdma_io, DBDMA_io),
842627be2f2SMark Cave-Ayland         VMSTATE_STRUCT(current, struct DBDMA_channel, 0, vmstate_dbdma_cmd,
843627be2f2SMark Cave-Ayland                        dbdma_cmd),
84449ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
84549ab747fSPaolo Bonzini     }
84649ab747fSPaolo Bonzini };
84749ab747fSPaolo Bonzini 
84849ab747fSPaolo Bonzini static const VMStateDescription vmstate_dbdma = {
84949ab747fSPaolo Bonzini     .name = "dbdma",
850627be2f2SMark Cave-Ayland     .version_id = 3,
851627be2f2SMark Cave-Ayland     .minimum_version_id = 3,
852ce933d70SRichard Henderson     .fields = (const VMStateField[]) {
85349ab747fSPaolo Bonzini         VMSTATE_STRUCT_ARRAY(channels, DBDMAState, DBDMA_CHANNELS, 1,
85449ab747fSPaolo Bonzini                              vmstate_dbdma_channel, DBDMA_channel),
85549ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
85649ab747fSPaolo Bonzini     }
85749ab747fSPaolo Bonzini };
85849ab747fSPaolo Bonzini 
mac_dbdma_reset(DeviceState * d)8591d27f351SMark Cave-Ayland static void mac_dbdma_reset(DeviceState *d)
86049ab747fSPaolo Bonzini {
8611d27f351SMark Cave-Ayland     DBDMAState *s = MAC_DBDMA(d);
86249ab747fSPaolo Bonzini     int i;
86349ab747fSPaolo Bonzini 
8641d27f351SMark Cave-Ayland     for (i = 0; i < DBDMA_CHANNELS; i++) {
86549ab747fSPaolo Bonzini         memset(s->channels[i].regs, 0, DBDMA_SIZE);
86649ab747fSPaolo Bonzini     }
8671d27f351SMark Cave-Ayland }
86849ab747fSPaolo Bonzini 
dbdma_unassigned_rw(DBDMA_io * io)8692d7d06d8SHervé Poussineau static void dbdma_unassigned_rw(DBDMA_io *io)
8702d7d06d8SHervé Poussineau {
8712d7d06d8SHervé Poussineau     DBDMA_channel *ch = io->channel;
87277453882SBenjamin Herrenschmidt     dbdma_cmd *current = &ch->current;
87377453882SBenjamin Herrenschmidt     uint16_t cmd;
8742d7d06d8SHervé Poussineau     qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
8752d7d06d8SHervé Poussineau                   __func__, ch->channel);
8762df77896SMark Cave-Ayland     ch->io.processing = false;
87777453882SBenjamin Herrenschmidt 
87877453882SBenjamin Herrenschmidt     cmd = le16_to_cpu(current->command) & COMMAND_MASK;
87977453882SBenjamin Herrenschmidt     if (cmd == OUTPUT_MORE || cmd == OUTPUT_LAST ||
88077453882SBenjamin Herrenschmidt         cmd == INPUT_MORE || cmd == INPUT_LAST) {
88177453882SBenjamin Herrenschmidt         current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
88277453882SBenjamin Herrenschmidt         current->res_count = cpu_to_le16(io->len);
88377453882SBenjamin Herrenschmidt         dbdma_cmdptr_save(ch);
88477453882SBenjamin Herrenschmidt     }
8852d7d06d8SHervé Poussineau }
8862d7d06d8SHervé Poussineau 
dbdma_unassigned_flush(DBDMA_io * io)8872d7d06d8SHervé Poussineau static void dbdma_unassigned_flush(DBDMA_io *io)
8882d7d06d8SHervé Poussineau {
8892d7d06d8SHervé Poussineau     DBDMA_channel *ch = io->channel;
8902d7d06d8SHervé Poussineau     qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
8912d7d06d8SHervé Poussineau                   __func__, ch->channel);
8922d7d06d8SHervé Poussineau }
8932d7d06d8SHervé Poussineau 
mac_dbdma_init(Object * obj)8941d27f351SMark Cave-Ayland static void mac_dbdma_init(Object *obj)
8951d27f351SMark Cave-Ayland {
8961d27f351SMark Cave-Ayland     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
8971d27f351SMark Cave-Ayland     DBDMAState *s = MAC_DBDMA(obj);
8981d27f351SMark Cave-Ayland     int i;
89949ab747fSPaolo Bonzini 
9003e300fa6SAlexander Graf     for (i = 0; i < DBDMA_CHANNELS; i++) {
9012d7d06d8SHervé Poussineau         DBDMA_channel *ch = &s->channels[i];
9022d7d06d8SHervé Poussineau 
9032d7d06d8SHervé Poussineau         ch->rw = dbdma_unassigned_rw;
9042d7d06d8SHervé Poussineau         ch->flush = dbdma_unassigned_flush;
9052d7d06d8SHervé Poussineau         ch->channel = i;
9062d7d06d8SHervé Poussineau         ch->io.channel = ch;
9073e300fa6SAlexander Graf     }
9083e300fa6SAlexander Graf 
9091d27f351SMark Cave-Ayland     memory_region_init_io(&s->mem, obj, &dbdma_ops, s, "dbdma", 0x1000);
9101d27f351SMark Cave-Ayland     sysbus_init_mmio(sbd, &s->mem);
9111d27f351SMark Cave-Ayland }
9121d27f351SMark Cave-Ayland 
mac_dbdma_realize(DeviceState * dev,Error ** errp)9131d27f351SMark Cave-Ayland static void mac_dbdma_realize(DeviceState *dev, Error **errp)
9141d27f351SMark Cave-Ayland {
9151d27f351SMark Cave-Ayland     DBDMAState *s = MAC_DBDMA(dev);
91649ab747fSPaolo Bonzini 
917f63192b0SAlexander Bulekov     s->bh = qemu_bh_new_guarded(DBDMA_run_bh, s, &dev->mem_reentrancy_guard);
91849ab747fSPaolo Bonzini }
9191d27f351SMark Cave-Ayland 
mac_dbdma_class_init(ObjectClass * oc,void * data)9201d27f351SMark Cave-Ayland static void mac_dbdma_class_init(ObjectClass *oc, void *data)
9211d27f351SMark Cave-Ayland {
9221d27f351SMark Cave-Ayland     DeviceClass *dc = DEVICE_CLASS(oc);
9231d27f351SMark Cave-Ayland 
9241d27f351SMark Cave-Ayland     dc->realize = mac_dbdma_realize;
925*e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, mac_dbdma_reset);
9261d27f351SMark Cave-Ayland     dc->vmsd = &vmstate_dbdma;
9271d27f351SMark Cave-Ayland }
9281d27f351SMark Cave-Ayland 
9291d27f351SMark Cave-Ayland static const TypeInfo mac_dbdma_type_info = {
9301d27f351SMark Cave-Ayland     .name = TYPE_MAC_DBDMA,
9311d27f351SMark Cave-Ayland     .parent = TYPE_SYS_BUS_DEVICE,
9321d27f351SMark Cave-Ayland     .instance_size = sizeof(DBDMAState),
9331d27f351SMark Cave-Ayland     .instance_init = mac_dbdma_init,
9341d27f351SMark Cave-Ayland     .class_init = mac_dbdma_class_init
9351d27f351SMark Cave-Ayland };
9361d27f351SMark Cave-Ayland 
mac_dbdma_register_types(void)9371d27f351SMark Cave-Ayland static void mac_dbdma_register_types(void)
9381d27f351SMark Cave-Ayland {
9391d27f351SMark Cave-Ayland     type_register_static(&mac_dbdma_type_info);
9401d27f351SMark Cave-Ayland }
9411d27f351SMark Cave-Ayland 
9421d27f351SMark Cave-Ayland type_init(mac_dbdma_register_types)
943