xref: /openbmc/qemu/hw/misc/macio/mac_dbdma.c (revision 0b8fa32f551e863bb548a11394239239270dd3dc)
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  */
39*0b8fa32fSMarkus Armbruster 
400d75590dSPeter Maydell #include "qemu/osdep.h"
4149ab747fSPaolo Bonzini #include "hw/hw.h"
4249ab747fSPaolo Bonzini #include "hw/ppc/mac_dbdma.h"
4349ab747fSPaolo Bonzini #include "qemu/main-loop.h"
44*0b8fa32fSMarkus Armbruster #include "qemu/module.h"
4503dd024fSPaolo Bonzini #include "qemu/log.h"
4688655881SMark Cave-Ayland #include "sysemu/dma.h"
4749ab747fSPaolo Bonzini 
4849ab747fSPaolo Bonzini /* debug DBDMA */
49ba0b17ddSMark Cave-Ayland #define DEBUG_DBDMA 0
503e49c439SMark Cave-Ayland #define DEBUG_DBDMA_CHANMASK ((1ull << DBDMA_CHANNELS) - 1)
5149ab747fSPaolo Bonzini 
52ba0b17ddSMark Cave-Ayland #define DBDMA_DPRINTF(fmt, ...) do { \
53ba0b17ddSMark Cave-Ayland     if (DEBUG_DBDMA) { \
54ba0b17ddSMark Cave-Ayland         printf("DBDMA: " fmt , ## __VA_ARGS__); \
55ba0b17ddSMark Cave-Ayland     } \
562562755eSEric Blake } while (0)
5749ab747fSPaolo Bonzini 
583e49c439SMark Cave-Ayland #define DBDMA_DPRINTFCH(ch, fmt, ...) do { \
593e49c439SMark Cave-Ayland     if (DEBUG_DBDMA) { \
603e49c439SMark Cave-Ayland         if ((1ul << (ch)->channel) & DEBUG_DBDMA_CHANMASK) { \
613e49c439SMark Cave-Ayland             printf("DBDMA[%02x]: " fmt , (ch)->channel, ## __VA_ARGS__); \
623e49c439SMark Cave-Ayland         } \
633e49c439SMark Cave-Ayland     } \
642562755eSEric Blake } while (0)
653e49c439SMark Cave-Ayland 
6649ab747fSPaolo Bonzini /*
6749ab747fSPaolo Bonzini  */
6849ab747fSPaolo Bonzini 
69d2f0ce21SAlexander Graf static DBDMAState *dbdma_from_ch(DBDMA_channel *ch)
70d2f0ce21SAlexander Graf {
71d2f0ce21SAlexander Graf     return container_of(ch, DBDMAState, channels[ch->channel]);
72d2f0ce21SAlexander Graf }
73d2f0ce21SAlexander Graf 
74ba0b17ddSMark Cave-Ayland #if DEBUG_DBDMA
75b7d67813SMark Cave-Ayland static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd)
7649ab747fSPaolo Bonzini {
77b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "dbdma_cmd %p\n", cmd);
78b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    req_count 0x%04x\n", le16_to_cpu(cmd->req_count));
79b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    command 0x%04x\n", le16_to_cpu(cmd->command));
80b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    phy_addr 0x%08x\n", le32_to_cpu(cmd->phy_addr));
81b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    cmd_dep 0x%08x\n", le32_to_cpu(cmd->cmd_dep));
82b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    res_count 0x%04x\n", le16_to_cpu(cmd->res_count));
83b7d67813SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "    xfer_status 0x%04x\n",
84b7d67813SMark Cave-Ayland                     le16_to_cpu(cmd->xfer_status));
8549ab747fSPaolo Bonzini }
8649ab747fSPaolo Bonzini #else
87b7d67813SMark Cave-Ayland static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd)
8849ab747fSPaolo Bonzini {
8949ab747fSPaolo Bonzini }
9049ab747fSPaolo Bonzini #endif
9149ab747fSPaolo Bonzini static void dbdma_cmdptr_load(DBDMA_channel *ch)
9249ab747fSPaolo Bonzini {
933e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "dbdma_cmdptr_load 0x%08x\n",
9449ab747fSPaolo Bonzini                     ch->regs[DBDMA_CMDPTR_LO]);
9588655881SMark Cave-Ayland     dma_memory_read(&address_space_memory, ch->regs[DBDMA_CMDPTR_LO],
96e1fe50dcSStefan Weil                     &ch->current, sizeof(dbdma_cmd));
9749ab747fSPaolo Bonzini }
9849ab747fSPaolo Bonzini 
9949ab747fSPaolo Bonzini static void dbdma_cmdptr_save(DBDMA_channel *ch)
10049ab747fSPaolo Bonzini {
10177453882SBenjamin Herrenschmidt     DBDMA_DPRINTFCH(ch, "-> update 0x%08x stat=0x%08x, res=0x%04x\n",
10277453882SBenjamin Herrenschmidt                     ch->regs[DBDMA_CMDPTR_LO],
10349ab747fSPaolo Bonzini                     le16_to_cpu(ch->current.xfer_status),
10449ab747fSPaolo Bonzini                     le16_to_cpu(ch->current.res_count));
10588655881SMark Cave-Ayland     dma_memory_write(&address_space_memory, ch->regs[DBDMA_CMDPTR_LO],
106e1fe50dcSStefan Weil                      &ch->current, sizeof(dbdma_cmd));
10749ab747fSPaolo Bonzini }
10849ab747fSPaolo Bonzini 
10949ab747fSPaolo Bonzini static void kill_channel(DBDMA_channel *ch)
11049ab747fSPaolo Bonzini {
1113e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "kill_channel\n");
11249ab747fSPaolo Bonzini 
11349ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] |= DEAD;
11449ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~ACTIVE;
11549ab747fSPaolo Bonzini 
11649ab747fSPaolo Bonzini     qemu_irq_raise(ch->irq);
11749ab747fSPaolo Bonzini }
11849ab747fSPaolo Bonzini 
11949ab747fSPaolo Bonzini static void conditional_interrupt(DBDMA_channel *ch)
12049ab747fSPaolo Bonzini {
12149ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
12249ab747fSPaolo Bonzini     uint16_t intr;
12349ab747fSPaolo Bonzini     uint16_t sel_mask, sel_value;
12449ab747fSPaolo Bonzini     uint32_t status;
12549ab747fSPaolo Bonzini     int cond;
12649ab747fSPaolo Bonzini 
1273e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "%s\n", __func__);
12849ab747fSPaolo Bonzini 
12949ab747fSPaolo Bonzini     intr = le16_to_cpu(current->command) & INTR_MASK;
13049ab747fSPaolo Bonzini 
13149ab747fSPaolo Bonzini     switch(intr) {
13249ab747fSPaolo Bonzini     case INTR_NEVER:  /* don't interrupt */
13349ab747fSPaolo Bonzini         return;
13449ab747fSPaolo Bonzini     case INTR_ALWAYS: /* always interrupt */
13549ab747fSPaolo Bonzini         qemu_irq_raise(ch->irq);
1363e49c439SMark Cave-Ayland         DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
13749ab747fSPaolo Bonzini         return;
13849ab747fSPaolo Bonzini     }
13949ab747fSPaolo Bonzini 
14049ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS] & DEVSTAT;
14149ab747fSPaolo Bonzini 
14249ab747fSPaolo Bonzini     sel_mask = (ch->regs[DBDMA_INTR_SEL] >> 16) & 0x0f;
14349ab747fSPaolo Bonzini     sel_value = ch->regs[DBDMA_INTR_SEL] & 0x0f;
14449ab747fSPaolo Bonzini 
14549ab747fSPaolo Bonzini     cond = (status & sel_mask) == (sel_value & sel_mask);
14649ab747fSPaolo Bonzini 
14749ab747fSPaolo Bonzini     switch(intr) {
14849ab747fSPaolo Bonzini     case INTR_IFSET:  /* intr if condition bit is 1 */
14933ce36bbSAlexander Graf         if (cond) {
15049ab747fSPaolo Bonzini             qemu_irq_raise(ch->irq);
1513e49c439SMark Cave-Ayland             DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
15233ce36bbSAlexander Graf         }
15349ab747fSPaolo Bonzini         return;
15449ab747fSPaolo Bonzini     case INTR_IFCLR:  /* intr if condition bit is 0 */
15533ce36bbSAlexander Graf         if (!cond) {
15649ab747fSPaolo Bonzini             qemu_irq_raise(ch->irq);
1573e49c439SMark Cave-Ayland             DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
15833ce36bbSAlexander Graf         }
15949ab747fSPaolo Bonzini         return;
16049ab747fSPaolo Bonzini     }
16149ab747fSPaolo Bonzini }
16249ab747fSPaolo Bonzini 
16349ab747fSPaolo Bonzini static int conditional_wait(DBDMA_channel *ch)
16449ab747fSPaolo Bonzini {
16549ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
16649ab747fSPaolo Bonzini     uint16_t wait;
16749ab747fSPaolo Bonzini     uint16_t sel_mask, sel_value;
16849ab747fSPaolo Bonzini     uint32_t status;
16949ab747fSPaolo Bonzini     int cond;
17077453882SBenjamin Herrenschmidt     int res = 0;
17149ab747fSPaolo Bonzini 
17249ab747fSPaolo Bonzini     wait = le16_to_cpu(current->command) & WAIT_MASK;
17349ab747fSPaolo Bonzini     switch(wait) {
17449ab747fSPaolo Bonzini     case WAIT_NEVER:  /* don't wait */
17549ab747fSPaolo Bonzini         return 0;
17649ab747fSPaolo Bonzini     case WAIT_ALWAYS: /* always wait */
17777453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "  [WAIT_ALWAYS]\n");
17849ab747fSPaolo Bonzini         return 1;
17949ab747fSPaolo Bonzini     }
18049ab747fSPaolo Bonzini 
18149ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS] & DEVSTAT;
18249ab747fSPaolo Bonzini 
18349ab747fSPaolo Bonzini     sel_mask = (ch->regs[DBDMA_WAIT_SEL] >> 16) & 0x0f;
18449ab747fSPaolo Bonzini     sel_value = ch->regs[DBDMA_WAIT_SEL] & 0x0f;
18549ab747fSPaolo Bonzini 
18649ab747fSPaolo Bonzini     cond = (status & sel_mask) == (sel_value & sel_mask);
18749ab747fSPaolo Bonzini 
18849ab747fSPaolo Bonzini     switch(wait) {
18949ab747fSPaolo Bonzini     case WAIT_IFSET:  /* wait if condition bit is 1 */
19077453882SBenjamin Herrenschmidt         if (cond) {
19177453882SBenjamin Herrenschmidt             res = 1;
19249ab747fSPaolo Bonzini         }
19377453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "  [WAIT_IFSET=%d]\n", res);
19477453882SBenjamin Herrenschmidt         break;
19577453882SBenjamin Herrenschmidt     case WAIT_IFCLR:  /* wait if condition bit is 0 */
19677453882SBenjamin Herrenschmidt         if (!cond) {
19777453882SBenjamin Herrenschmidt             res = 1;
19877453882SBenjamin Herrenschmidt         }
19977453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "  [WAIT_IFCLR=%d]\n", res);
20077453882SBenjamin Herrenschmidt         break;
20177453882SBenjamin Herrenschmidt     }
20277453882SBenjamin Herrenschmidt     return res;
20349ab747fSPaolo Bonzini }
20449ab747fSPaolo Bonzini 
20549ab747fSPaolo Bonzini static void next(DBDMA_channel *ch)
20649ab747fSPaolo Bonzini {
20749ab747fSPaolo Bonzini     uint32_t cp;
20849ab747fSPaolo Bonzini 
20949ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~BT;
21049ab747fSPaolo Bonzini 
21149ab747fSPaolo Bonzini     cp = ch->regs[DBDMA_CMDPTR_LO];
21249ab747fSPaolo Bonzini     ch->regs[DBDMA_CMDPTR_LO] = cp + sizeof(dbdma_cmd);
21349ab747fSPaolo Bonzini     dbdma_cmdptr_load(ch);
21449ab747fSPaolo Bonzini }
21549ab747fSPaolo Bonzini 
21649ab747fSPaolo Bonzini static void branch(DBDMA_channel *ch)
21749ab747fSPaolo Bonzini {
21849ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
21949ab747fSPaolo Bonzini 
2203f0d4128SMark Cave-Ayland     ch->regs[DBDMA_CMDPTR_LO] = le32_to_cpu(current->cmd_dep);
22149ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] |= BT;
22249ab747fSPaolo Bonzini     dbdma_cmdptr_load(ch);
22349ab747fSPaolo Bonzini }
22449ab747fSPaolo Bonzini 
22549ab747fSPaolo Bonzini static void conditional_branch(DBDMA_channel *ch)
22649ab747fSPaolo Bonzini {
22749ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
22849ab747fSPaolo Bonzini     uint16_t br;
22949ab747fSPaolo Bonzini     uint16_t sel_mask, sel_value;
23049ab747fSPaolo Bonzini     uint32_t status;
23149ab747fSPaolo Bonzini     int cond;
23249ab747fSPaolo Bonzini 
23349ab747fSPaolo Bonzini     /* check if we must branch */
23449ab747fSPaolo Bonzini 
23549ab747fSPaolo Bonzini     br = le16_to_cpu(current->command) & BR_MASK;
23649ab747fSPaolo Bonzini 
23749ab747fSPaolo Bonzini     switch(br) {
23849ab747fSPaolo Bonzini     case BR_NEVER:  /* don't branch */
23949ab747fSPaolo Bonzini         next(ch);
24049ab747fSPaolo Bonzini         return;
24149ab747fSPaolo Bonzini     case BR_ALWAYS: /* always branch */
24277453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "  [BR_ALWAYS]\n");
24349ab747fSPaolo Bonzini         branch(ch);
24449ab747fSPaolo Bonzini         return;
24549ab747fSPaolo Bonzini     }
24649ab747fSPaolo Bonzini 
24749ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS] & DEVSTAT;
24849ab747fSPaolo Bonzini 
24949ab747fSPaolo Bonzini     sel_mask = (ch->regs[DBDMA_BRANCH_SEL] >> 16) & 0x0f;
25049ab747fSPaolo Bonzini     sel_value = ch->regs[DBDMA_BRANCH_SEL] & 0x0f;
25149ab747fSPaolo Bonzini 
25249ab747fSPaolo Bonzini     cond = (status & sel_mask) == (sel_value & sel_mask);
25349ab747fSPaolo Bonzini 
25449ab747fSPaolo Bonzini     switch(br) {
25549ab747fSPaolo Bonzini     case BR_IFSET:  /* branch if condition bit is 1 */
25677453882SBenjamin Herrenschmidt         if (cond) {
25777453882SBenjamin Herrenschmidt             DBDMA_DPRINTFCH(ch, "  [BR_IFSET = 1]\n");
25849ab747fSPaolo Bonzini             branch(ch);
25977453882SBenjamin Herrenschmidt         } else {
26077453882SBenjamin Herrenschmidt             DBDMA_DPRINTFCH(ch, "  [BR_IFSET = 0]\n");
26149ab747fSPaolo Bonzini             next(ch);
26277453882SBenjamin Herrenschmidt         }
26349ab747fSPaolo Bonzini         return;
26449ab747fSPaolo Bonzini     case BR_IFCLR:  /* branch if condition bit is 0 */
26577453882SBenjamin Herrenschmidt         if (!cond) {
26677453882SBenjamin Herrenschmidt             DBDMA_DPRINTFCH(ch, "  [BR_IFCLR = 1]\n");
26749ab747fSPaolo Bonzini             branch(ch);
26877453882SBenjamin Herrenschmidt         } else {
26977453882SBenjamin Herrenschmidt             DBDMA_DPRINTFCH(ch, "  [BR_IFCLR = 0]\n");
27049ab747fSPaolo Bonzini             next(ch);
27177453882SBenjamin Herrenschmidt         }
27249ab747fSPaolo Bonzini         return;
27349ab747fSPaolo Bonzini     }
27449ab747fSPaolo Bonzini }
27549ab747fSPaolo Bonzini 
27649ab747fSPaolo Bonzini static void channel_run(DBDMA_channel *ch);
27749ab747fSPaolo Bonzini 
27849ab747fSPaolo Bonzini static void dbdma_end(DBDMA_io *io)
27949ab747fSPaolo Bonzini {
28049ab747fSPaolo Bonzini     DBDMA_channel *ch = io->channel;
28149ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
28249ab747fSPaolo Bonzini 
2833e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "%s\n", __func__);
28433ce36bbSAlexander Graf 
28549ab747fSPaolo Bonzini     if (conditional_wait(ch))
28649ab747fSPaolo Bonzini         goto wait;
28749ab747fSPaolo Bonzini 
28849ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
28949ab747fSPaolo Bonzini     current->res_count = cpu_to_le16(io->len);
29049ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
29149ab747fSPaolo Bonzini     if (io->is_last)
29249ab747fSPaolo Bonzini         ch->regs[DBDMA_STATUS] &= ~FLUSH;
29349ab747fSPaolo Bonzini 
29449ab747fSPaolo Bonzini     conditional_interrupt(ch);
29549ab747fSPaolo Bonzini     conditional_branch(ch);
29649ab747fSPaolo Bonzini 
29749ab747fSPaolo Bonzini wait:
29803ee3b1eSAlexander Graf     /* Indicate that we're ready for a new DMA round */
29903ee3b1eSAlexander Graf     ch->io.processing = false;
30003ee3b1eSAlexander Graf 
30149ab747fSPaolo Bonzini     if ((ch->regs[DBDMA_STATUS] & RUN) &&
30249ab747fSPaolo Bonzini         (ch->regs[DBDMA_STATUS] & ACTIVE))
30349ab747fSPaolo Bonzini         channel_run(ch);
30449ab747fSPaolo Bonzini }
30549ab747fSPaolo Bonzini 
30649ab747fSPaolo Bonzini static void start_output(DBDMA_channel *ch, int key, uint32_t addr,
30749ab747fSPaolo Bonzini                         uint16_t req_count, int is_last)
30849ab747fSPaolo Bonzini {
3093e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "start_output\n");
31049ab747fSPaolo Bonzini 
31149ab747fSPaolo Bonzini     /* KEY_REGS, KEY_DEVICE and KEY_STREAM
31249ab747fSPaolo Bonzini      * are not implemented in the mac-io chip
31349ab747fSPaolo Bonzini      */
31449ab747fSPaolo Bonzini 
3153e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "addr 0x%x key 0x%x\n", addr, key);
31649ab747fSPaolo Bonzini     if (!addr || key > KEY_STREAM3) {
31749ab747fSPaolo Bonzini         kill_channel(ch);
31849ab747fSPaolo Bonzini         return;
31949ab747fSPaolo Bonzini     }
32049ab747fSPaolo Bonzini 
32149ab747fSPaolo Bonzini     ch->io.addr = addr;
32249ab747fSPaolo Bonzini     ch->io.len = req_count;
32349ab747fSPaolo Bonzini     ch->io.is_last = is_last;
32449ab747fSPaolo Bonzini     ch->io.dma_end = dbdma_end;
32549ab747fSPaolo Bonzini     ch->io.is_dma_out = 1;
32603ee3b1eSAlexander Graf     ch->io.processing = true;
32749ab747fSPaolo Bonzini     if (ch->rw) {
32849ab747fSPaolo Bonzini         ch->rw(&ch->io);
32949ab747fSPaolo Bonzini     }
33049ab747fSPaolo Bonzini }
33149ab747fSPaolo Bonzini 
33249ab747fSPaolo Bonzini static void start_input(DBDMA_channel *ch, int key, uint32_t addr,
33349ab747fSPaolo Bonzini                        uint16_t req_count, int is_last)
33449ab747fSPaolo Bonzini {
3353e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "start_input\n");
33649ab747fSPaolo Bonzini 
33749ab747fSPaolo Bonzini     /* KEY_REGS, KEY_DEVICE and KEY_STREAM
33849ab747fSPaolo Bonzini      * are not implemented in the mac-io chip
33949ab747fSPaolo Bonzini      */
34049ab747fSPaolo Bonzini 
3413e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "addr 0x%x key 0x%x\n", addr, key);
34249ab747fSPaolo Bonzini     if (!addr || key > KEY_STREAM3) {
34349ab747fSPaolo Bonzini         kill_channel(ch);
34449ab747fSPaolo Bonzini         return;
34549ab747fSPaolo Bonzini     }
34649ab747fSPaolo Bonzini 
34749ab747fSPaolo Bonzini     ch->io.addr = addr;
34849ab747fSPaolo Bonzini     ch->io.len = req_count;
34949ab747fSPaolo Bonzini     ch->io.is_last = is_last;
35049ab747fSPaolo Bonzini     ch->io.dma_end = dbdma_end;
35149ab747fSPaolo Bonzini     ch->io.is_dma_out = 0;
35203ee3b1eSAlexander Graf     ch->io.processing = true;
35349ab747fSPaolo Bonzini     if (ch->rw) {
35449ab747fSPaolo Bonzini         ch->rw(&ch->io);
35549ab747fSPaolo Bonzini     }
35649ab747fSPaolo Bonzini }
35749ab747fSPaolo Bonzini 
35849ab747fSPaolo Bonzini static void load_word(DBDMA_channel *ch, int key, uint32_t addr,
35949ab747fSPaolo Bonzini                      uint16_t len)
36049ab747fSPaolo Bonzini {
36149ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
36249ab747fSPaolo Bonzini 
363e12f50b9SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "load_word %d bytes, addr=%08x\n", len, addr);
36449ab747fSPaolo Bonzini 
36549ab747fSPaolo Bonzini     /* only implements KEY_SYSTEM */
36649ab747fSPaolo Bonzini 
36749ab747fSPaolo Bonzini     if (key != KEY_SYSTEM) {
36849ab747fSPaolo Bonzini         printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
36949ab747fSPaolo Bonzini         kill_channel(ch);
37049ab747fSPaolo Bonzini         return;
37149ab747fSPaolo Bonzini     }
37249ab747fSPaolo Bonzini 
373e12f50b9SMark Cave-Ayland     dma_memory_read(&address_space_memory, addr, &current->cmd_dep, len);
37449ab747fSPaolo Bonzini 
37549ab747fSPaolo Bonzini     if (conditional_wait(ch))
37649ab747fSPaolo Bonzini         goto wait;
37749ab747fSPaolo Bonzini 
37849ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
37949ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
38049ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~FLUSH;
38149ab747fSPaolo Bonzini 
38249ab747fSPaolo Bonzini     conditional_interrupt(ch);
38349ab747fSPaolo Bonzini     next(ch);
38449ab747fSPaolo Bonzini 
38549ab747fSPaolo Bonzini wait:
386d2f0ce21SAlexander Graf     DBDMA_kick(dbdma_from_ch(ch));
38749ab747fSPaolo Bonzini }
38849ab747fSPaolo Bonzini 
38949ab747fSPaolo Bonzini static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
39049ab747fSPaolo Bonzini                       uint16_t len)
39149ab747fSPaolo Bonzini {
39249ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
39349ab747fSPaolo Bonzini 
394e12f50b9SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "store_word %d bytes, addr=%08x pa=%x\n",
395e12f50b9SMark Cave-Ayland                     len, addr, le32_to_cpu(current->cmd_dep));
39649ab747fSPaolo Bonzini 
39749ab747fSPaolo Bonzini     /* only implements KEY_SYSTEM */
39849ab747fSPaolo Bonzini 
39949ab747fSPaolo Bonzini     if (key != KEY_SYSTEM) {
40049ab747fSPaolo Bonzini         printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
40149ab747fSPaolo Bonzini         kill_channel(ch);
40249ab747fSPaolo Bonzini         return;
40349ab747fSPaolo Bonzini     }
40449ab747fSPaolo Bonzini 
405e12f50b9SMark Cave-Ayland     dma_memory_write(&address_space_memory, addr, &current->cmd_dep, len);
40649ab747fSPaolo Bonzini 
40749ab747fSPaolo Bonzini     if (conditional_wait(ch))
40849ab747fSPaolo Bonzini         goto wait;
40949ab747fSPaolo Bonzini 
41049ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
41149ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
41249ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~FLUSH;
41349ab747fSPaolo Bonzini 
41449ab747fSPaolo Bonzini     conditional_interrupt(ch);
41549ab747fSPaolo Bonzini     next(ch);
41649ab747fSPaolo Bonzini 
41749ab747fSPaolo Bonzini wait:
418d2f0ce21SAlexander Graf     DBDMA_kick(dbdma_from_ch(ch));
41949ab747fSPaolo Bonzini }
42049ab747fSPaolo Bonzini 
42149ab747fSPaolo Bonzini static void nop(DBDMA_channel *ch)
42249ab747fSPaolo Bonzini {
42349ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
42449ab747fSPaolo Bonzini 
42549ab747fSPaolo Bonzini     if (conditional_wait(ch))
42649ab747fSPaolo Bonzini         goto wait;
42749ab747fSPaolo Bonzini 
42849ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
42949ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
43049ab747fSPaolo Bonzini 
43149ab747fSPaolo Bonzini     conditional_interrupt(ch);
43249ab747fSPaolo Bonzini     conditional_branch(ch);
43349ab747fSPaolo Bonzini 
43449ab747fSPaolo Bonzini wait:
435d2f0ce21SAlexander Graf     DBDMA_kick(dbdma_from_ch(ch));
43649ab747fSPaolo Bonzini }
43749ab747fSPaolo Bonzini 
43849ab747fSPaolo Bonzini static void stop(DBDMA_channel *ch)
43949ab747fSPaolo Bonzini {
44077453882SBenjamin Herrenschmidt     ch->regs[DBDMA_STATUS] &= ~(ACTIVE);
44149ab747fSPaolo Bonzini 
44249ab747fSPaolo Bonzini     /* the stop command does not increment command pointer */
44349ab747fSPaolo Bonzini }
44449ab747fSPaolo Bonzini 
44549ab747fSPaolo Bonzini static void channel_run(DBDMA_channel *ch)
44649ab747fSPaolo Bonzini {
44749ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
44849ab747fSPaolo Bonzini     uint16_t cmd, key;
44949ab747fSPaolo Bonzini     uint16_t req_count;
45049ab747fSPaolo Bonzini     uint32_t phy_addr;
45149ab747fSPaolo Bonzini 
4523e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "channel_run\n");
453b7d67813SMark Cave-Ayland     dump_dbdma_cmd(ch, current);
45449ab747fSPaolo Bonzini 
45549ab747fSPaolo Bonzini     /* clear WAKE flag at command fetch */
45649ab747fSPaolo Bonzini 
45749ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~WAKE;
45849ab747fSPaolo Bonzini 
45949ab747fSPaolo Bonzini     cmd = le16_to_cpu(current->command) & COMMAND_MASK;
46049ab747fSPaolo Bonzini 
46149ab747fSPaolo Bonzini     switch (cmd) {
46249ab747fSPaolo Bonzini     case DBDMA_NOP:
46349ab747fSPaolo Bonzini         nop(ch);
46449ab747fSPaolo Bonzini         return;
46549ab747fSPaolo Bonzini 
46649ab747fSPaolo Bonzini     case DBDMA_STOP:
46749ab747fSPaolo Bonzini         stop(ch);
46849ab747fSPaolo Bonzini         return;
46949ab747fSPaolo Bonzini     }
47049ab747fSPaolo Bonzini 
47149ab747fSPaolo Bonzini     key = le16_to_cpu(current->command) & 0x0700;
47249ab747fSPaolo Bonzini     req_count = le16_to_cpu(current->req_count);
47349ab747fSPaolo Bonzini     phy_addr = le32_to_cpu(current->phy_addr);
47449ab747fSPaolo Bonzini 
47549ab747fSPaolo Bonzini     if (key == KEY_STREAM4) {
47649ab747fSPaolo Bonzini         printf("command %x, invalid key 4\n", cmd);
47749ab747fSPaolo Bonzini         kill_channel(ch);
47849ab747fSPaolo Bonzini         return;
47949ab747fSPaolo Bonzini     }
48049ab747fSPaolo Bonzini 
48149ab747fSPaolo Bonzini     switch (cmd) {
48249ab747fSPaolo Bonzini     case OUTPUT_MORE:
48377453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* OUTPUT_MORE *\n");
48449ab747fSPaolo Bonzini         start_output(ch, key, phy_addr, req_count, 0);
48549ab747fSPaolo Bonzini         return;
48649ab747fSPaolo Bonzini 
48749ab747fSPaolo Bonzini     case OUTPUT_LAST:
48877453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* OUTPUT_LAST *\n");
48949ab747fSPaolo Bonzini         start_output(ch, key, phy_addr, req_count, 1);
49049ab747fSPaolo Bonzini         return;
49149ab747fSPaolo Bonzini 
49249ab747fSPaolo Bonzini     case INPUT_MORE:
49377453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* INPUT_MORE *\n");
49449ab747fSPaolo Bonzini         start_input(ch, key, phy_addr, req_count, 0);
49549ab747fSPaolo Bonzini         return;
49649ab747fSPaolo Bonzini 
49749ab747fSPaolo Bonzini     case INPUT_LAST:
49877453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* INPUT_LAST *\n");
49949ab747fSPaolo Bonzini         start_input(ch, key, phy_addr, req_count, 1);
50049ab747fSPaolo Bonzini         return;
50149ab747fSPaolo Bonzini     }
50249ab747fSPaolo Bonzini 
50349ab747fSPaolo Bonzini     if (key < KEY_REGS) {
50449ab747fSPaolo Bonzini         printf("command %x, invalid key %x\n", cmd, key);
50549ab747fSPaolo Bonzini         key = KEY_SYSTEM;
50649ab747fSPaolo Bonzini     }
50749ab747fSPaolo Bonzini 
50849ab747fSPaolo Bonzini     /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
50949ab747fSPaolo Bonzini      * and BRANCH is invalid
51049ab747fSPaolo Bonzini      */
51149ab747fSPaolo Bonzini 
51249ab747fSPaolo Bonzini     req_count = req_count & 0x0007;
51349ab747fSPaolo Bonzini     if (req_count & 0x4) {
51449ab747fSPaolo Bonzini         req_count = 4;
51549ab747fSPaolo Bonzini         phy_addr &= ~3;
51649ab747fSPaolo Bonzini     } else if (req_count & 0x2) {
51749ab747fSPaolo Bonzini         req_count = 2;
51849ab747fSPaolo Bonzini         phy_addr &= ~1;
51949ab747fSPaolo Bonzini     } else
52049ab747fSPaolo Bonzini         req_count = 1;
52149ab747fSPaolo Bonzini 
52249ab747fSPaolo Bonzini     switch (cmd) {
52349ab747fSPaolo Bonzini     case LOAD_WORD:
52477453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* LOAD_WORD *\n");
52549ab747fSPaolo Bonzini         load_word(ch, key, phy_addr, req_count);
52649ab747fSPaolo Bonzini         return;
52749ab747fSPaolo Bonzini 
52849ab747fSPaolo Bonzini     case STORE_WORD:
52977453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "* STORE_WORD *\n");
53049ab747fSPaolo Bonzini         store_word(ch, key, phy_addr, req_count);
53149ab747fSPaolo Bonzini         return;
53249ab747fSPaolo Bonzini     }
53349ab747fSPaolo Bonzini }
53449ab747fSPaolo Bonzini 
53549ab747fSPaolo Bonzini static void DBDMA_run(DBDMAState *s)
53649ab747fSPaolo Bonzini {
53749ab747fSPaolo Bonzini     int channel;
53849ab747fSPaolo Bonzini 
53949ab747fSPaolo Bonzini     for (channel = 0; channel < DBDMA_CHANNELS; channel++) {
54049ab747fSPaolo Bonzini         DBDMA_channel *ch = &s->channels[channel];
54149ab747fSPaolo Bonzini         uint32_t status = ch->regs[DBDMA_STATUS];
54203ee3b1eSAlexander Graf         if (!ch->io.processing && (status & RUN) && (status & ACTIVE)) {
54349ab747fSPaolo Bonzini             channel_run(ch);
54449ab747fSPaolo Bonzini         }
54549ab747fSPaolo Bonzini     }
54649ab747fSPaolo Bonzini }
54749ab747fSPaolo Bonzini 
54849ab747fSPaolo Bonzini static void DBDMA_run_bh(void *opaque)
54949ab747fSPaolo Bonzini {
55049ab747fSPaolo Bonzini     DBDMAState *s = opaque;
55149ab747fSPaolo Bonzini 
5523e49c439SMark Cave-Ayland     DBDMA_DPRINTF("-> DBDMA_run_bh\n");
55349ab747fSPaolo Bonzini     DBDMA_run(s);
5543e49c439SMark Cave-Ayland     DBDMA_DPRINTF("<- DBDMA_run_bh\n");
55549ab747fSPaolo Bonzini }
55649ab747fSPaolo Bonzini 
557d1e562deSAlexander Graf void DBDMA_kick(DBDMAState *dbdma)
558d1e562deSAlexander Graf {
559d2f0ce21SAlexander Graf     qemu_bh_schedule(dbdma->bh);
560d1e562deSAlexander Graf }
561d1e562deSAlexander Graf 
56249ab747fSPaolo Bonzini void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
56349ab747fSPaolo Bonzini                             DBDMA_rw rw, DBDMA_flush flush,
56449ab747fSPaolo Bonzini                             void *opaque)
56549ab747fSPaolo Bonzini {
56649ab747fSPaolo Bonzini     DBDMAState *s = dbdma;
56749ab747fSPaolo Bonzini     DBDMA_channel *ch = &s->channels[nchan];
56849ab747fSPaolo Bonzini 
5693e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "DBDMA_register_channel 0x%x\n", nchan);
57049ab747fSPaolo Bonzini 
5712d7d06d8SHervé Poussineau     assert(rw);
5722d7d06d8SHervé Poussineau     assert(flush);
5732d7d06d8SHervé Poussineau 
57449ab747fSPaolo Bonzini     ch->irq = irq;
57549ab747fSPaolo Bonzini     ch->rw = rw;
57649ab747fSPaolo Bonzini     ch->flush = flush;
57749ab747fSPaolo Bonzini     ch->io.opaque = opaque;
57849ab747fSPaolo Bonzini }
57949ab747fSPaolo Bonzini 
58077453882SBenjamin Herrenschmidt static void dbdma_control_write(DBDMA_channel *ch)
58149ab747fSPaolo Bonzini {
58249ab747fSPaolo Bonzini     uint16_t mask, value;
58349ab747fSPaolo Bonzini     uint32_t status;
58477453882SBenjamin Herrenschmidt     bool do_flush = false;
58549ab747fSPaolo Bonzini 
58649ab747fSPaolo Bonzini     mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
58749ab747fSPaolo Bonzini     value = ch->regs[DBDMA_CONTROL] & 0xffff;
58849ab747fSPaolo Bonzini 
58977453882SBenjamin Herrenschmidt     /* This is the status register which we'll update
59077453882SBenjamin Herrenschmidt      * appropriately and store back
59177453882SBenjamin Herrenschmidt      */
59249ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS];
59349ab747fSPaolo Bonzini 
59477453882SBenjamin Herrenschmidt     /* RUN and PAUSE are bits under SW control only
59577453882SBenjamin Herrenschmidt      * FLUSH and WAKE are set by SW and cleared by HW
59677453882SBenjamin Herrenschmidt      * DEAD, ACTIVE and BT are only under HW control
59777453882SBenjamin Herrenschmidt      *
59877453882SBenjamin Herrenschmidt      * We handle ACTIVE separately at the end of the
59977453882SBenjamin Herrenschmidt      * logic to ensure all cases are covered.
60077453882SBenjamin Herrenschmidt      */
60149ab747fSPaolo Bonzini 
60277453882SBenjamin Herrenschmidt     /* Setting RUN will tentatively activate the channel
60377453882SBenjamin Herrenschmidt      */
60477453882SBenjamin Herrenschmidt     if ((mask & RUN) && (value & RUN)) {
60577453882SBenjamin Herrenschmidt         status |= RUN;
60677453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " Setting RUN !\n");
60749ab747fSPaolo Bonzini     }
60877453882SBenjamin Herrenschmidt 
60977453882SBenjamin Herrenschmidt     /* Clearing RUN 1->0 will stop the channel */
61077453882SBenjamin Herrenschmidt     if ((mask & RUN) && !(value & RUN)) {
61177453882SBenjamin Herrenschmidt         /* This has the side effect of clearing the DEAD bit */
61277453882SBenjamin Herrenschmidt         status &= ~(DEAD | RUN);
61377453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " Clearing RUN !\n");
61477453882SBenjamin Herrenschmidt     }
61577453882SBenjamin Herrenschmidt 
61677453882SBenjamin Herrenschmidt     /* Setting WAKE wakes up an idle channel if it's running
61777453882SBenjamin Herrenschmidt      *
61877453882SBenjamin Herrenschmidt      * Note: The doc doesn't say so but assume that only works
61977453882SBenjamin Herrenschmidt      * on a channel whose RUN bit is set.
62077453882SBenjamin Herrenschmidt      *
62177453882SBenjamin Herrenschmidt      * We set WAKE in status, it's not terribly useful as it will
62277453882SBenjamin Herrenschmidt      * be cleared on the next command fetch but it seems to mimmic
62377453882SBenjamin Herrenschmidt      * the HW behaviour and is useful for the way we handle
62477453882SBenjamin Herrenschmidt      * ACTIVE further down.
62577453882SBenjamin Herrenschmidt      */
62677453882SBenjamin Herrenschmidt     if ((mask & WAKE) && (value & WAKE) && (status & RUN)) {
62777453882SBenjamin Herrenschmidt         status |= WAKE;
62877453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " Setting WAKE !\n");
62977453882SBenjamin Herrenschmidt     }
63077453882SBenjamin Herrenschmidt 
63177453882SBenjamin Herrenschmidt     /* PAUSE being set will deactivate (or prevent activation)
63277453882SBenjamin Herrenschmidt      * of the channel. We just copy it over for now, ACTIVE will
63377453882SBenjamin Herrenschmidt      * be re-evaluated later.
63477453882SBenjamin Herrenschmidt      */
63577453882SBenjamin Herrenschmidt     if (mask & PAUSE) {
63677453882SBenjamin Herrenschmidt         status = (status & ~PAUSE) | (value & PAUSE);
63777453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " %sing PAUSE !\n",
63877453882SBenjamin Herrenschmidt                         (value & PAUSE) ? "sett" : "clear");
63977453882SBenjamin Herrenschmidt     }
64077453882SBenjamin Herrenschmidt 
64177453882SBenjamin Herrenschmidt     /* FLUSH is its own thing */
64277453882SBenjamin Herrenschmidt     if ((mask & FLUSH) && (value & FLUSH))  {
64377453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " Setting FLUSH !\n");
64477453882SBenjamin Herrenschmidt         /* We set flush directly in the status register, we do *NOT*
64577453882SBenjamin Herrenschmidt          * set it in "status" so that it gets naturally cleared when
64677453882SBenjamin Herrenschmidt          * we update the status register further down. That way it
64777453882SBenjamin Herrenschmidt          * will be set only during the HW flush operation so it is
64877453882SBenjamin Herrenschmidt          * visible to any completions happening during that time.
64977453882SBenjamin Herrenschmidt          */
65077453882SBenjamin Herrenschmidt         ch->regs[DBDMA_STATUS] |= FLUSH;
65177453882SBenjamin Herrenschmidt         do_flush = true;
65277453882SBenjamin Herrenschmidt     }
65377453882SBenjamin Herrenschmidt 
65477453882SBenjamin Herrenschmidt     /* If either RUN or PAUSE is clear, so should ACTIVE be,
65577453882SBenjamin Herrenschmidt      * otherwise, ACTIVE will be set if we modified RUN, PAUSE or
65677453882SBenjamin Herrenschmidt      * set WAKE. That means that PAUSE was just cleared, RUN was
65777453882SBenjamin Herrenschmidt      * just set or WAKE was just set.
65877453882SBenjamin Herrenschmidt      */
65977453882SBenjamin Herrenschmidt     if ((status & PAUSE) || !(status & RUN)) {
66049ab747fSPaolo Bonzini         status &= ~ACTIVE;
66177453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, "  -> ACTIVE down !\n");
66277453882SBenjamin Herrenschmidt 
66377453882SBenjamin Herrenschmidt         /* We stopped processing, we want the underlying HW command
66477453882SBenjamin Herrenschmidt          * to complete *before* we clear the ACTIVE bit. Otherwise
66577453882SBenjamin Herrenschmidt          * we can get into a situation where the command status will
66677453882SBenjamin Herrenschmidt          * have RUN or ACTIVE not set which is going to confuse the
66777453882SBenjamin Herrenschmidt          * MacOS driver.
66877453882SBenjamin Herrenschmidt          */
66977453882SBenjamin Herrenschmidt         do_flush = true;
67077453882SBenjamin Herrenschmidt     } else if (mask & (RUN | PAUSE)) {
67177453882SBenjamin Herrenschmidt         status |= ACTIVE;
67277453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
67377453882SBenjamin Herrenschmidt     } else if ((mask & WAKE) && (value & WAKE)) {
67477453882SBenjamin Herrenschmidt         status |= ACTIVE;
67577453882SBenjamin Herrenschmidt         DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
6761cde732dSMark Cave-Ayland     }
6771cde732dSMark Cave-Ayland 
67877453882SBenjamin Herrenschmidt     DBDMA_DPRINTFCH(ch, " new status=0x%08x\n", status);
67977453882SBenjamin Herrenschmidt 
68077453882SBenjamin Herrenschmidt     /* If we need to flush the underlying HW, do it now, this happens
68177453882SBenjamin Herrenschmidt      * both on FLUSH commands and when stopping the channel for safety.
68277453882SBenjamin Herrenschmidt      */
68377453882SBenjamin Herrenschmidt     if (do_flush && ch->flush) {
68449ab747fSPaolo Bonzini         ch->flush(&ch->io);
68549ab747fSPaolo Bonzini     }
68649ab747fSPaolo Bonzini 
68777453882SBenjamin Herrenschmidt     /* Finally update the status register image */
68849ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] = status;
68949ab747fSPaolo Bonzini 
69077453882SBenjamin Herrenschmidt     /* If active, make sure the BH gets to run */
691d2f0ce21SAlexander Graf     if (status & ACTIVE) {
692d2f0ce21SAlexander Graf         DBDMA_kick(dbdma_from_ch(ch));
693d2f0ce21SAlexander Graf     }
694d2f0ce21SAlexander Graf }
69549ab747fSPaolo Bonzini 
69649ab747fSPaolo Bonzini static void dbdma_write(void *opaque, hwaddr addr,
69749ab747fSPaolo Bonzini                         uint64_t value, unsigned size)
69849ab747fSPaolo Bonzini {
69949ab747fSPaolo Bonzini     int channel = addr >> DBDMA_CHANNEL_SHIFT;
70049ab747fSPaolo Bonzini     DBDMAState *s = opaque;
70149ab747fSPaolo Bonzini     DBDMA_channel *ch = &s->channels[channel];
70249ab747fSPaolo Bonzini     int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
70349ab747fSPaolo Bonzini 
7043e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "writel 0x" TARGET_FMT_plx " <= 0x%08"PRIx64"\n",
70558c0c311SAlexander Graf                     addr, value);
7063e49c439SMark Cave-Ayland     DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
70749ab747fSPaolo Bonzini                     (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
70849ab747fSPaolo Bonzini 
7097eaba824SAlexander Graf     /* cmdptr cannot be modified if channel is ACTIVE */
71049ab747fSPaolo Bonzini 
7117eaba824SAlexander Graf     if (reg == DBDMA_CMDPTR_LO && (ch->regs[DBDMA_STATUS] & ACTIVE)) {
71249ab747fSPaolo Bonzini         return;
7137eaba824SAlexander Graf     }
71449ab747fSPaolo Bonzini 
71549ab747fSPaolo Bonzini     ch->regs[reg] = value;
71649ab747fSPaolo Bonzini 
71749ab747fSPaolo Bonzini     switch(reg) {
71849ab747fSPaolo Bonzini     case DBDMA_CONTROL:
71949ab747fSPaolo Bonzini         dbdma_control_write(ch);
72049ab747fSPaolo Bonzini         break;
72149ab747fSPaolo Bonzini     case DBDMA_CMDPTR_LO:
72249ab747fSPaolo Bonzini         /* 16-byte aligned */
72349ab747fSPaolo Bonzini         ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
72449ab747fSPaolo Bonzini         dbdma_cmdptr_load(ch);
72549ab747fSPaolo Bonzini         break;
72649ab747fSPaolo Bonzini     case DBDMA_STATUS:
72749ab747fSPaolo Bonzini     case DBDMA_INTR_SEL:
72849ab747fSPaolo Bonzini     case DBDMA_BRANCH_SEL:
72949ab747fSPaolo Bonzini     case DBDMA_WAIT_SEL:
73049ab747fSPaolo Bonzini         /* nothing to do */
73149ab747fSPaolo Bonzini         break;
73249ab747fSPaolo Bonzini     case DBDMA_XFER_MODE:
73349ab747fSPaolo Bonzini     case DBDMA_CMDPTR_HI:
73449ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_HI:
73549ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_LO:
73649ab747fSPaolo Bonzini     case DBDMA_ADDRESS_HI:
73749ab747fSPaolo Bonzini     case DBDMA_BRANCH_ADDR_HI:
73849ab747fSPaolo Bonzini     case DBDMA_RES1:
73949ab747fSPaolo Bonzini     case DBDMA_RES2:
74049ab747fSPaolo Bonzini     case DBDMA_RES3:
74149ab747fSPaolo Bonzini     case DBDMA_RES4:
74249ab747fSPaolo Bonzini         /* unused */
74349ab747fSPaolo Bonzini         break;
74449ab747fSPaolo Bonzini     }
74549ab747fSPaolo Bonzini }
74649ab747fSPaolo Bonzini 
74749ab747fSPaolo Bonzini static uint64_t dbdma_read(void *opaque, hwaddr addr,
74849ab747fSPaolo Bonzini                            unsigned size)
74949ab747fSPaolo Bonzini {
75049ab747fSPaolo Bonzini     uint32_t value;
75149ab747fSPaolo Bonzini     int channel = addr >> DBDMA_CHANNEL_SHIFT;
75249ab747fSPaolo Bonzini     DBDMAState *s = opaque;
75349ab747fSPaolo Bonzini     DBDMA_channel *ch = &s->channels[channel];
75449ab747fSPaolo Bonzini     int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
75549ab747fSPaolo Bonzini 
75649ab747fSPaolo Bonzini     value = ch->regs[reg];
75749ab747fSPaolo Bonzini 
75849ab747fSPaolo Bonzini     switch(reg) {
75949ab747fSPaolo Bonzini     case DBDMA_CONTROL:
76077453882SBenjamin Herrenschmidt         value = ch->regs[DBDMA_STATUS];
76149ab747fSPaolo Bonzini         break;
76249ab747fSPaolo Bonzini     case DBDMA_STATUS:
76349ab747fSPaolo Bonzini     case DBDMA_CMDPTR_LO:
76449ab747fSPaolo Bonzini     case DBDMA_INTR_SEL:
76549ab747fSPaolo Bonzini     case DBDMA_BRANCH_SEL:
76649ab747fSPaolo Bonzini     case DBDMA_WAIT_SEL:
76749ab747fSPaolo Bonzini         /* nothing to do */
76849ab747fSPaolo Bonzini         break;
76949ab747fSPaolo Bonzini     case DBDMA_XFER_MODE:
77049ab747fSPaolo Bonzini     case DBDMA_CMDPTR_HI:
77149ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_HI:
77249ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_LO:
77349ab747fSPaolo Bonzini     case DBDMA_ADDRESS_HI:
77449ab747fSPaolo Bonzini     case DBDMA_BRANCH_ADDR_HI:
77549ab747fSPaolo Bonzini         /* unused */
77649ab747fSPaolo Bonzini         value = 0;
77749ab747fSPaolo Bonzini         break;
77849ab747fSPaolo Bonzini     case DBDMA_RES1:
77949ab747fSPaolo Bonzini     case DBDMA_RES2:
78049ab747fSPaolo Bonzini     case DBDMA_RES3:
78149ab747fSPaolo Bonzini     case DBDMA_RES4:
78249ab747fSPaolo Bonzini         /* reserved */
78349ab747fSPaolo Bonzini         break;
78449ab747fSPaolo Bonzini     }
78549ab747fSPaolo Bonzini 
78677453882SBenjamin Herrenschmidt     DBDMA_DPRINTFCH(ch, "readl 0x" TARGET_FMT_plx " => 0x%08x\n", addr, value);
78777453882SBenjamin Herrenschmidt     DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
78877453882SBenjamin Herrenschmidt                     (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
78977453882SBenjamin Herrenschmidt 
79049ab747fSPaolo Bonzini     return value;
79149ab747fSPaolo Bonzini }
79249ab747fSPaolo Bonzini 
79349ab747fSPaolo Bonzini static const MemoryRegionOps dbdma_ops = {
79449ab747fSPaolo Bonzini     .read = dbdma_read,
79549ab747fSPaolo Bonzini     .write = dbdma_write,
79649ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
79749ab747fSPaolo Bonzini     .valid = {
79849ab747fSPaolo Bonzini         .min_access_size = 4,
79949ab747fSPaolo Bonzini         .max_access_size = 4,
80049ab747fSPaolo Bonzini     },
80149ab747fSPaolo Bonzini };
80249ab747fSPaolo Bonzini 
803627be2f2SMark Cave-Ayland static const VMStateDescription vmstate_dbdma_io = {
804627be2f2SMark Cave-Ayland     .name = "dbdma_io",
80549ab747fSPaolo Bonzini     .version_id = 0,
80649ab747fSPaolo Bonzini     .minimum_version_id = 0,
80749ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
808627be2f2SMark Cave-Ayland         VMSTATE_UINT64(addr, struct DBDMA_io),
809627be2f2SMark Cave-Ayland         VMSTATE_INT32(len, struct DBDMA_io),
810627be2f2SMark Cave-Ayland         VMSTATE_INT32(is_last, struct DBDMA_io),
811627be2f2SMark Cave-Ayland         VMSTATE_INT32(is_dma_out, struct DBDMA_io),
812627be2f2SMark Cave-Ayland         VMSTATE_BOOL(processing, struct DBDMA_io),
813627be2f2SMark Cave-Ayland         VMSTATE_END_OF_LIST()
814627be2f2SMark Cave-Ayland     }
815627be2f2SMark Cave-Ayland };
816627be2f2SMark Cave-Ayland 
817627be2f2SMark Cave-Ayland static const VMStateDescription vmstate_dbdma_cmd = {
818627be2f2SMark Cave-Ayland     .name = "dbdma_cmd",
819627be2f2SMark Cave-Ayland     .version_id = 0,
820627be2f2SMark Cave-Ayland     .minimum_version_id = 0,
821627be2f2SMark Cave-Ayland     .fields = (VMStateField[]) {
822627be2f2SMark Cave-Ayland         VMSTATE_UINT16(req_count, dbdma_cmd),
823627be2f2SMark Cave-Ayland         VMSTATE_UINT16(command, dbdma_cmd),
824627be2f2SMark Cave-Ayland         VMSTATE_UINT32(phy_addr, dbdma_cmd),
825627be2f2SMark Cave-Ayland         VMSTATE_UINT32(cmd_dep, dbdma_cmd),
826627be2f2SMark Cave-Ayland         VMSTATE_UINT16(res_count, dbdma_cmd),
827627be2f2SMark Cave-Ayland         VMSTATE_UINT16(xfer_status, dbdma_cmd),
828627be2f2SMark Cave-Ayland         VMSTATE_END_OF_LIST()
829627be2f2SMark Cave-Ayland     }
830627be2f2SMark Cave-Ayland };
831627be2f2SMark Cave-Ayland 
832627be2f2SMark Cave-Ayland static const VMStateDescription vmstate_dbdma_channel = {
833627be2f2SMark Cave-Ayland     .name = "dbdma_channel",
834627be2f2SMark Cave-Ayland     .version_id = 1,
835627be2f2SMark Cave-Ayland     .minimum_version_id = 1,
836627be2f2SMark Cave-Ayland     .fields = (VMStateField[]) {
83749ab747fSPaolo Bonzini         VMSTATE_UINT32_ARRAY(regs, struct DBDMA_channel, DBDMA_REGS),
838627be2f2SMark Cave-Ayland         VMSTATE_STRUCT(io, struct DBDMA_channel, 0, vmstate_dbdma_io, DBDMA_io),
839627be2f2SMark Cave-Ayland         VMSTATE_STRUCT(current, struct DBDMA_channel, 0, vmstate_dbdma_cmd,
840627be2f2SMark Cave-Ayland                        dbdma_cmd),
84149ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
84249ab747fSPaolo Bonzini     }
84349ab747fSPaolo Bonzini };
84449ab747fSPaolo Bonzini 
84549ab747fSPaolo Bonzini static const VMStateDescription vmstate_dbdma = {
84649ab747fSPaolo Bonzini     .name = "dbdma",
847627be2f2SMark Cave-Ayland     .version_id = 3,
848627be2f2SMark Cave-Ayland     .minimum_version_id = 3,
84949ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
85049ab747fSPaolo Bonzini         VMSTATE_STRUCT_ARRAY(channels, DBDMAState, DBDMA_CHANNELS, 1,
85149ab747fSPaolo Bonzini                              vmstate_dbdma_channel, DBDMA_channel),
85249ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
85349ab747fSPaolo Bonzini     }
85449ab747fSPaolo Bonzini };
85549ab747fSPaolo Bonzini 
8561d27f351SMark Cave-Ayland static void mac_dbdma_reset(DeviceState *d)
85749ab747fSPaolo Bonzini {
8581d27f351SMark Cave-Ayland     DBDMAState *s = MAC_DBDMA(d);
85949ab747fSPaolo Bonzini     int i;
86049ab747fSPaolo Bonzini 
8611d27f351SMark Cave-Ayland     for (i = 0; i < DBDMA_CHANNELS; i++) {
86249ab747fSPaolo Bonzini         memset(s->channels[i].regs, 0, DBDMA_SIZE);
86349ab747fSPaolo Bonzini     }
8641d27f351SMark Cave-Ayland }
86549ab747fSPaolo Bonzini 
8662d7d06d8SHervé Poussineau static void dbdma_unassigned_rw(DBDMA_io *io)
8672d7d06d8SHervé Poussineau {
8682d7d06d8SHervé Poussineau     DBDMA_channel *ch = io->channel;
86977453882SBenjamin Herrenschmidt     dbdma_cmd *current = &ch->current;
87077453882SBenjamin Herrenschmidt     uint16_t cmd;
8712d7d06d8SHervé Poussineau     qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
8722d7d06d8SHervé Poussineau                   __func__, ch->channel);
8732df77896SMark Cave-Ayland     ch->io.processing = false;
87477453882SBenjamin Herrenschmidt 
87577453882SBenjamin Herrenschmidt     cmd = le16_to_cpu(current->command) & COMMAND_MASK;
87677453882SBenjamin Herrenschmidt     if (cmd == OUTPUT_MORE || cmd == OUTPUT_LAST ||
87777453882SBenjamin Herrenschmidt         cmd == INPUT_MORE || cmd == INPUT_LAST) {
87877453882SBenjamin Herrenschmidt         current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
87977453882SBenjamin Herrenschmidt         current->res_count = cpu_to_le16(io->len);
88077453882SBenjamin Herrenschmidt         dbdma_cmdptr_save(ch);
88177453882SBenjamin Herrenschmidt     }
8822d7d06d8SHervé Poussineau }
8832d7d06d8SHervé Poussineau 
8842d7d06d8SHervé Poussineau static void dbdma_unassigned_flush(DBDMA_io *io)
8852d7d06d8SHervé Poussineau {
8862d7d06d8SHervé Poussineau     DBDMA_channel *ch = io->channel;
8872d7d06d8SHervé Poussineau     qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
8882d7d06d8SHervé Poussineau                   __func__, ch->channel);
8892d7d06d8SHervé Poussineau }
8902d7d06d8SHervé Poussineau 
8911d27f351SMark Cave-Ayland static void mac_dbdma_init(Object *obj)
8921d27f351SMark Cave-Ayland {
8931d27f351SMark Cave-Ayland     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
8941d27f351SMark Cave-Ayland     DBDMAState *s = MAC_DBDMA(obj);
8951d27f351SMark Cave-Ayland     int i;
89649ab747fSPaolo Bonzini 
8973e300fa6SAlexander Graf     for (i = 0; i < DBDMA_CHANNELS; i++) {
8982d7d06d8SHervé Poussineau         DBDMA_channel *ch = &s->channels[i];
8992d7d06d8SHervé Poussineau 
9002d7d06d8SHervé Poussineau         ch->rw = dbdma_unassigned_rw;
9012d7d06d8SHervé Poussineau         ch->flush = dbdma_unassigned_flush;
9022d7d06d8SHervé Poussineau         ch->channel = i;
9032d7d06d8SHervé Poussineau         ch->io.channel = ch;
9043e300fa6SAlexander Graf     }
9053e300fa6SAlexander Graf 
9061d27f351SMark Cave-Ayland     memory_region_init_io(&s->mem, obj, &dbdma_ops, s, "dbdma", 0x1000);
9071d27f351SMark Cave-Ayland     sysbus_init_mmio(sbd, &s->mem);
9081d27f351SMark Cave-Ayland }
9091d27f351SMark Cave-Ayland 
9101d27f351SMark Cave-Ayland static void mac_dbdma_realize(DeviceState *dev, Error **errp)
9111d27f351SMark Cave-Ayland {
9121d27f351SMark Cave-Ayland     DBDMAState *s = MAC_DBDMA(dev);
91349ab747fSPaolo Bonzini 
914d2f0ce21SAlexander Graf     s->bh = qemu_bh_new(DBDMA_run_bh, s);
91549ab747fSPaolo Bonzini }
9161d27f351SMark Cave-Ayland 
9171d27f351SMark Cave-Ayland static void mac_dbdma_class_init(ObjectClass *oc, void *data)
9181d27f351SMark Cave-Ayland {
9191d27f351SMark Cave-Ayland     DeviceClass *dc = DEVICE_CLASS(oc);
9201d27f351SMark Cave-Ayland 
9211d27f351SMark Cave-Ayland     dc->realize = mac_dbdma_realize;
9221d27f351SMark Cave-Ayland     dc->reset = mac_dbdma_reset;
9231d27f351SMark Cave-Ayland     dc->vmsd = &vmstate_dbdma;
9241d27f351SMark Cave-Ayland }
9251d27f351SMark Cave-Ayland 
9261d27f351SMark Cave-Ayland static const TypeInfo mac_dbdma_type_info = {
9271d27f351SMark Cave-Ayland     .name = TYPE_MAC_DBDMA,
9281d27f351SMark Cave-Ayland     .parent = TYPE_SYS_BUS_DEVICE,
9291d27f351SMark Cave-Ayland     .instance_size = sizeof(DBDMAState),
9301d27f351SMark Cave-Ayland     .instance_init = mac_dbdma_init,
9311d27f351SMark Cave-Ayland     .class_init = mac_dbdma_class_init
9321d27f351SMark Cave-Ayland };
9331d27f351SMark Cave-Ayland 
9341d27f351SMark Cave-Ayland static void mac_dbdma_register_types(void)
9351d27f351SMark Cave-Ayland {
9361d27f351SMark Cave-Ayland     type_register_static(&mac_dbdma_type_info);
9371d27f351SMark Cave-Ayland }
9381d27f351SMark Cave-Ayland 
9391d27f351SMark Cave-Ayland type_init(mac_dbdma_register_types)
940