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" 4149ab747fSPaolo Bonzini #include "hw/hw.h" 42*64552b6bSMarkus Armbruster #include "hw/irq.h" 4349ab747fSPaolo Bonzini #include "hw/ppc/mac_dbdma.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 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 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 88b7d67813SMark Cave-Ayland static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd) 8949ab747fSPaolo Bonzini { 9049ab747fSPaolo Bonzini } 9149ab747fSPaolo Bonzini #endif 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], 97e1fe50dcSStefan Weil &ch->current, sizeof(dbdma_cmd)); 9849ab747fSPaolo Bonzini } 9949ab747fSPaolo Bonzini 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], 107e1fe50dcSStefan Weil &ch->current, sizeof(dbdma_cmd)); 10849ab747fSPaolo Bonzini } 10949ab747fSPaolo Bonzini 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 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 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 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 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 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 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 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 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 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 374e12f50b9SMark Cave-Ayland dma_memory_read(&address_space_memory, addr, ¤t->cmd_dep, len); 37549ab747fSPaolo Bonzini 37649ab747fSPaolo Bonzini if (conditional_wait(ch)) 37749ab747fSPaolo Bonzini goto wait; 37849ab747fSPaolo Bonzini 37949ab747fSPaolo Bonzini current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]); 38049ab747fSPaolo Bonzini dbdma_cmdptr_save(ch); 38149ab747fSPaolo Bonzini ch->regs[DBDMA_STATUS] &= ~FLUSH; 38249ab747fSPaolo Bonzini 38349ab747fSPaolo Bonzini conditional_interrupt(ch); 38449ab747fSPaolo Bonzini next(ch); 38549ab747fSPaolo Bonzini 38649ab747fSPaolo Bonzini wait: 387d2f0ce21SAlexander Graf DBDMA_kick(dbdma_from_ch(ch)); 38849ab747fSPaolo Bonzini } 38949ab747fSPaolo Bonzini 39049ab747fSPaolo Bonzini static void store_word(DBDMA_channel *ch, int key, uint32_t addr, 39149ab747fSPaolo Bonzini uint16_t len) 39249ab747fSPaolo Bonzini { 39349ab747fSPaolo Bonzini dbdma_cmd *current = &ch->current; 39449ab747fSPaolo Bonzini 395e12f50b9SMark Cave-Ayland DBDMA_DPRINTFCH(ch, "store_word %d bytes, addr=%08x pa=%x\n", 396e12f50b9SMark Cave-Ayland len, addr, le32_to_cpu(current->cmd_dep)); 39749ab747fSPaolo Bonzini 39849ab747fSPaolo Bonzini /* only implements KEY_SYSTEM */ 39949ab747fSPaolo Bonzini 40049ab747fSPaolo Bonzini if (key != KEY_SYSTEM) { 40149ab747fSPaolo Bonzini printf("DBDMA: STORE_WORD, unimplemented key %x\n", key); 40249ab747fSPaolo Bonzini kill_channel(ch); 40349ab747fSPaolo Bonzini return; 40449ab747fSPaolo Bonzini } 40549ab747fSPaolo Bonzini 406e12f50b9SMark Cave-Ayland dma_memory_write(&address_space_memory, addr, ¤t->cmd_dep, len); 40749ab747fSPaolo Bonzini 40849ab747fSPaolo Bonzini if (conditional_wait(ch)) 40949ab747fSPaolo Bonzini goto wait; 41049ab747fSPaolo Bonzini 41149ab747fSPaolo Bonzini current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]); 41249ab747fSPaolo Bonzini dbdma_cmdptr_save(ch); 41349ab747fSPaolo Bonzini ch->regs[DBDMA_STATUS] &= ~FLUSH; 41449ab747fSPaolo Bonzini 41549ab747fSPaolo Bonzini conditional_interrupt(ch); 41649ab747fSPaolo Bonzini next(ch); 41749ab747fSPaolo Bonzini 41849ab747fSPaolo Bonzini wait: 419d2f0ce21SAlexander Graf DBDMA_kick(dbdma_from_ch(ch)); 42049ab747fSPaolo Bonzini } 42149ab747fSPaolo Bonzini 42249ab747fSPaolo Bonzini static void nop(DBDMA_channel *ch) 42349ab747fSPaolo Bonzini { 42449ab747fSPaolo Bonzini dbdma_cmd *current = &ch->current; 42549ab747fSPaolo Bonzini 42649ab747fSPaolo Bonzini if (conditional_wait(ch)) 42749ab747fSPaolo Bonzini goto wait; 42849ab747fSPaolo Bonzini 42949ab747fSPaolo Bonzini current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]); 43049ab747fSPaolo Bonzini dbdma_cmdptr_save(ch); 43149ab747fSPaolo Bonzini 43249ab747fSPaolo Bonzini conditional_interrupt(ch); 43349ab747fSPaolo Bonzini conditional_branch(ch); 43449ab747fSPaolo Bonzini 43549ab747fSPaolo Bonzini wait: 436d2f0ce21SAlexander Graf DBDMA_kick(dbdma_from_ch(ch)); 43749ab747fSPaolo Bonzini } 43849ab747fSPaolo Bonzini 43949ab747fSPaolo Bonzini static void stop(DBDMA_channel *ch) 44049ab747fSPaolo Bonzini { 44177453882SBenjamin Herrenschmidt ch->regs[DBDMA_STATUS] &= ~(ACTIVE); 44249ab747fSPaolo Bonzini 44349ab747fSPaolo Bonzini /* the stop command does not increment command pointer */ 44449ab747fSPaolo Bonzini } 44549ab747fSPaolo Bonzini 44649ab747fSPaolo Bonzini static void channel_run(DBDMA_channel *ch) 44749ab747fSPaolo Bonzini { 44849ab747fSPaolo Bonzini dbdma_cmd *current = &ch->current; 44949ab747fSPaolo Bonzini uint16_t cmd, key; 45049ab747fSPaolo Bonzini uint16_t req_count; 45149ab747fSPaolo Bonzini uint32_t phy_addr; 45249ab747fSPaolo Bonzini 4533e49c439SMark Cave-Ayland DBDMA_DPRINTFCH(ch, "channel_run\n"); 454b7d67813SMark Cave-Ayland dump_dbdma_cmd(ch, current); 45549ab747fSPaolo Bonzini 45649ab747fSPaolo Bonzini /* clear WAKE flag at command fetch */ 45749ab747fSPaolo Bonzini 45849ab747fSPaolo Bonzini ch->regs[DBDMA_STATUS] &= ~WAKE; 45949ab747fSPaolo Bonzini 46049ab747fSPaolo Bonzini cmd = le16_to_cpu(current->command) & COMMAND_MASK; 46149ab747fSPaolo Bonzini 46249ab747fSPaolo Bonzini switch (cmd) { 46349ab747fSPaolo Bonzini case DBDMA_NOP: 46449ab747fSPaolo Bonzini nop(ch); 46549ab747fSPaolo Bonzini return; 46649ab747fSPaolo Bonzini 46749ab747fSPaolo Bonzini case DBDMA_STOP: 46849ab747fSPaolo Bonzini stop(ch); 46949ab747fSPaolo Bonzini return; 47049ab747fSPaolo Bonzini } 47149ab747fSPaolo Bonzini 47249ab747fSPaolo Bonzini key = le16_to_cpu(current->command) & 0x0700; 47349ab747fSPaolo Bonzini req_count = le16_to_cpu(current->req_count); 47449ab747fSPaolo Bonzini phy_addr = le32_to_cpu(current->phy_addr); 47549ab747fSPaolo Bonzini 47649ab747fSPaolo Bonzini if (key == KEY_STREAM4) { 47749ab747fSPaolo Bonzini printf("command %x, invalid key 4\n", cmd); 47849ab747fSPaolo Bonzini kill_channel(ch); 47949ab747fSPaolo Bonzini return; 48049ab747fSPaolo Bonzini } 48149ab747fSPaolo Bonzini 48249ab747fSPaolo Bonzini switch (cmd) { 48349ab747fSPaolo Bonzini case OUTPUT_MORE: 48477453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, "* OUTPUT_MORE *\n"); 48549ab747fSPaolo Bonzini start_output(ch, key, phy_addr, req_count, 0); 48649ab747fSPaolo Bonzini return; 48749ab747fSPaolo Bonzini 48849ab747fSPaolo Bonzini case OUTPUT_LAST: 48977453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, "* OUTPUT_LAST *\n"); 49049ab747fSPaolo Bonzini start_output(ch, key, phy_addr, req_count, 1); 49149ab747fSPaolo Bonzini return; 49249ab747fSPaolo Bonzini 49349ab747fSPaolo Bonzini case INPUT_MORE: 49477453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, "* INPUT_MORE *\n"); 49549ab747fSPaolo Bonzini start_input(ch, key, phy_addr, req_count, 0); 49649ab747fSPaolo Bonzini return; 49749ab747fSPaolo Bonzini 49849ab747fSPaolo Bonzini case INPUT_LAST: 49977453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, "* INPUT_LAST *\n"); 50049ab747fSPaolo Bonzini start_input(ch, key, phy_addr, req_count, 1); 50149ab747fSPaolo Bonzini return; 50249ab747fSPaolo Bonzini } 50349ab747fSPaolo Bonzini 50449ab747fSPaolo Bonzini if (key < KEY_REGS) { 50549ab747fSPaolo Bonzini printf("command %x, invalid key %x\n", cmd, key); 50649ab747fSPaolo Bonzini key = KEY_SYSTEM; 50749ab747fSPaolo Bonzini } 50849ab747fSPaolo Bonzini 50949ab747fSPaolo Bonzini /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits 51049ab747fSPaolo Bonzini * and BRANCH is invalid 51149ab747fSPaolo Bonzini */ 51249ab747fSPaolo Bonzini 51349ab747fSPaolo Bonzini req_count = req_count & 0x0007; 51449ab747fSPaolo Bonzini if (req_count & 0x4) { 51549ab747fSPaolo Bonzini req_count = 4; 51649ab747fSPaolo Bonzini phy_addr &= ~3; 51749ab747fSPaolo Bonzini } else if (req_count & 0x2) { 51849ab747fSPaolo Bonzini req_count = 2; 51949ab747fSPaolo Bonzini phy_addr &= ~1; 52049ab747fSPaolo Bonzini } else 52149ab747fSPaolo Bonzini req_count = 1; 52249ab747fSPaolo Bonzini 52349ab747fSPaolo Bonzini switch (cmd) { 52449ab747fSPaolo Bonzini case LOAD_WORD: 52577453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, "* LOAD_WORD *\n"); 52649ab747fSPaolo Bonzini load_word(ch, key, phy_addr, req_count); 52749ab747fSPaolo Bonzini return; 52849ab747fSPaolo Bonzini 52949ab747fSPaolo Bonzini case STORE_WORD: 53077453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, "* STORE_WORD *\n"); 53149ab747fSPaolo Bonzini store_word(ch, key, phy_addr, req_count); 53249ab747fSPaolo Bonzini return; 53349ab747fSPaolo Bonzini } 53449ab747fSPaolo Bonzini } 53549ab747fSPaolo Bonzini 53649ab747fSPaolo Bonzini static void DBDMA_run(DBDMAState *s) 53749ab747fSPaolo Bonzini { 53849ab747fSPaolo Bonzini int channel; 53949ab747fSPaolo Bonzini 54049ab747fSPaolo Bonzini for (channel = 0; channel < DBDMA_CHANNELS; channel++) { 54149ab747fSPaolo Bonzini DBDMA_channel *ch = &s->channels[channel]; 54249ab747fSPaolo Bonzini uint32_t status = ch->regs[DBDMA_STATUS]; 54303ee3b1eSAlexander Graf if (!ch->io.processing && (status & RUN) && (status & ACTIVE)) { 54449ab747fSPaolo Bonzini channel_run(ch); 54549ab747fSPaolo Bonzini } 54649ab747fSPaolo Bonzini } 54749ab747fSPaolo Bonzini } 54849ab747fSPaolo Bonzini 54949ab747fSPaolo Bonzini static void DBDMA_run_bh(void *opaque) 55049ab747fSPaolo Bonzini { 55149ab747fSPaolo Bonzini DBDMAState *s = opaque; 55249ab747fSPaolo Bonzini 5533e49c439SMark Cave-Ayland DBDMA_DPRINTF("-> DBDMA_run_bh\n"); 55449ab747fSPaolo Bonzini DBDMA_run(s); 5553e49c439SMark Cave-Ayland DBDMA_DPRINTF("<- DBDMA_run_bh\n"); 55649ab747fSPaolo Bonzini } 55749ab747fSPaolo Bonzini 558d1e562deSAlexander Graf void DBDMA_kick(DBDMAState *dbdma) 559d1e562deSAlexander Graf { 560d2f0ce21SAlexander Graf qemu_bh_schedule(dbdma->bh); 561d1e562deSAlexander Graf } 562d1e562deSAlexander Graf 56349ab747fSPaolo Bonzini void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq, 56449ab747fSPaolo Bonzini DBDMA_rw rw, DBDMA_flush flush, 56549ab747fSPaolo Bonzini void *opaque) 56649ab747fSPaolo Bonzini { 56749ab747fSPaolo Bonzini DBDMAState *s = dbdma; 56849ab747fSPaolo Bonzini DBDMA_channel *ch = &s->channels[nchan]; 56949ab747fSPaolo Bonzini 5703e49c439SMark Cave-Ayland DBDMA_DPRINTFCH(ch, "DBDMA_register_channel 0x%x\n", nchan); 57149ab747fSPaolo Bonzini 5722d7d06d8SHervé Poussineau assert(rw); 5732d7d06d8SHervé Poussineau assert(flush); 5742d7d06d8SHervé Poussineau 57549ab747fSPaolo Bonzini ch->irq = irq; 57649ab747fSPaolo Bonzini ch->rw = rw; 57749ab747fSPaolo Bonzini ch->flush = flush; 57849ab747fSPaolo Bonzini ch->io.opaque = opaque; 57949ab747fSPaolo Bonzini } 58049ab747fSPaolo Bonzini 58177453882SBenjamin Herrenschmidt static void dbdma_control_write(DBDMA_channel *ch) 58249ab747fSPaolo Bonzini { 58349ab747fSPaolo Bonzini uint16_t mask, value; 58449ab747fSPaolo Bonzini uint32_t status; 58577453882SBenjamin Herrenschmidt bool do_flush = false; 58649ab747fSPaolo Bonzini 58749ab747fSPaolo Bonzini mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff; 58849ab747fSPaolo Bonzini value = ch->regs[DBDMA_CONTROL] & 0xffff; 58949ab747fSPaolo Bonzini 59077453882SBenjamin Herrenschmidt /* This is the status register which we'll update 59177453882SBenjamin Herrenschmidt * appropriately and store back 59277453882SBenjamin Herrenschmidt */ 59349ab747fSPaolo Bonzini status = ch->regs[DBDMA_STATUS]; 59449ab747fSPaolo Bonzini 59577453882SBenjamin Herrenschmidt /* RUN and PAUSE are bits under SW control only 59677453882SBenjamin Herrenschmidt * FLUSH and WAKE are set by SW and cleared by HW 59777453882SBenjamin Herrenschmidt * DEAD, ACTIVE and BT are only under HW control 59877453882SBenjamin Herrenschmidt * 59977453882SBenjamin Herrenschmidt * We handle ACTIVE separately at the end of the 60077453882SBenjamin Herrenschmidt * logic to ensure all cases are covered. 60177453882SBenjamin Herrenschmidt */ 60249ab747fSPaolo Bonzini 60377453882SBenjamin Herrenschmidt /* Setting RUN will tentatively activate the channel 60477453882SBenjamin Herrenschmidt */ 60577453882SBenjamin Herrenschmidt if ((mask & RUN) && (value & RUN)) { 60677453882SBenjamin Herrenschmidt status |= RUN; 60777453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, " Setting RUN !\n"); 60849ab747fSPaolo Bonzini } 60977453882SBenjamin Herrenschmidt 61077453882SBenjamin Herrenschmidt /* Clearing RUN 1->0 will stop the channel */ 61177453882SBenjamin Herrenschmidt if ((mask & RUN) && !(value & RUN)) { 61277453882SBenjamin Herrenschmidt /* This has the side effect of clearing the DEAD bit */ 61377453882SBenjamin Herrenschmidt status &= ~(DEAD | RUN); 61477453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, " Clearing RUN !\n"); 61577453882SBenjamin Herrenschmidt } 61677453882SBenjamin Herrenschmidt 61777453882SBenjamin Herrenschmidt /* Setting WAKE wakes up an idle channel if it's running 61877453882SBenjamin Herrenschmidt * 61977453882SBenjamin Herrenschmidt * Note: The doc doesn't say so but assume that only works 62077453882SBenjamin Herrenschmidt * on a channel whose RUN bit is set. 62177453882SBenjamin Herrenschmidt * 62277453882SBenjamin Herrenschmidt * We set WAKE in status, it's not terribly useful as it will 62377453882SBenjamin Herrenschmidt * be cleared on the next command fetch but it seems to mimmic 62477453882SBenjamin Herrenschmidt * the HW behaviour and is useful for the way we handle 62577453882SBenjamin Herrenschmidt * ACTIVE further down. 62677453882SBenjamin Herrenschmidt */ 62777453882SBenjamin Herrenschmidt if ((mask & WAKE) && (value & WAKE) && (status & RUN)) { 62877453882SBenjamin Herrenschmidt status |= WAKE; 62977453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, " Setting WAKE !\n"); 63077453882SBenjamin Herrenschmidt } 63177453882SBenjamin Herrenschmidt 63277453882SBenjamin Herrenschmidt /* PAUSE being set will deactivate (or prevent activation) 63377453882SBenjamin Herrenschmidt * of the channel. We just copy it over for now, ACTIVE will 63477453882SBenjamin Herrenschmidt * be re-evaluated later. 63577453882SBenjamin Herrenschmidt */ 63677453882SBenjamin Herrenschmidt if (mask & PAUSE) { 63777453882SBenjamin Herrenschmidt status = (status & ~PAUSE) | (value & PAUSE); 63877453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, " %sing PAUSE !\n", 63977453882SBenjamin Herrenschmidt (value & PAUSE) ? "sett" : "clear"); 64077453882SBenjamin Herrenschmidt } 64177453882SBenjamin Herrenschmidt 64277453882SBenjamin Herrenschmidt /* FLUSH is its own thing */ 64377453882SBenjamin Herrenschmidt if ((mask & FLUSH) && (value & FLUSH)) { 64477453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, " Setting FLUSH !\n"); 64577453882SBenjamin Herrenschmidt /* We set flush directly in the status register, we do *NOT* 64677453882SBenjamin Herrenschmidt * set it in "status" so that it gets naturally cleared when 64777453882SBenjamin Herrenschmidt * we update the status register further down. That way it 64877453882SBenjamin Herrenschmidt * will be set only during the HW flush operation so it is 64977453882SBenjamin Herrenschmidt * visible to any completions happening during that time. 65077453882SBenjamin Herrenschmidt */ 65177453882SBenjamin Herrenschmidt ch->regs[DBDMA_STATUS] |= FLUSH; 65277453882SBenjamin Herrenschmidt do_flush = true; 65377453882SBenjamin Herrenschmidt } 65477453882SBenjamin Herrenschmidt 65577453882SBenjamin Herrenschmidt /* If either RUN or PAUSE is clear, so should ACTIVE be, 65677453882SBenjamin Herrenschmidt * otherwise, ACTIVE will be set if we modified RUN, PAUSE or 65777453882SBenjamin Herrenschmidt * set WAKE. That means that PAUSE was just cleared, RUN was 65877453882SBenjamin Herrenschmidt * just set or WAKE was just set. 65977453882SBenjamin Herrenschmidt */ 66077453882SBenjamin Herrenschmidt if ((status & PAUSE) || !(status & RUN)) { 66149ab747fSPaolo Bonzini status &= ~ACTIVE; 66277453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, " -> ACTIVE down !\n"); 66377453882SBenjamin Herrenschmidt 66477453882SBenjamin Herrenschmidt /* We stopped processing, we want the underlying HW command 66577453882SBenjamin Herrenschmidt * to complete *before* we clear the ACTIVE bit. Otherwise 66677453882SBenjamin Herrenschmidt * we can get into a situation where the command status will 66777453882SBenjamin Herrenschmidt * have RUN or ACTIVE not set which is going to confuse the 66877453882SBenjamin Herrenschmidt * MacOS driver. 66977453882SBenjamin Herrenschmidt */ 67077453882SBenjamin Herrenschmidt do_flush = true; 67177453882SBenjamin Herrenschmidt } else if (mask & (RUN | PAUSE)) { 67277453882SBenjamin Herrenschmidt status |= ACTIVE; 67377453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n"); 67477453882SBenjamin Herrenschmidt } else if ((mask & WAKE) && (value & WAKE)) { 67577453882SBenjamin Herrenschmidt status |= ACTIVE; 67677453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n"); 6771cde732dSMark Cave-Ayland } 6781cde732dSMark Cave-Ayland 67977453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, " new status=0x%08x\n", status); 68077453882SBenjamin Herrenschmidt 68177453882SBenjamin Herrenschmidt /* If we need to flush the underlying HW, do it now, this happens 68277453882SBenjamin Herrenschmidt * both on FLUSH commands and when stopping the channel for safety. 68377453882SBenjamin Herrenschmidt */ 68477453882SBenjamin Herrenschmidt if (do_flush && ch->flush) { 68549ab747fSPaolo Bonzini ch->flush(&ch->io); 68649ab747fSPaolo Bonzini } 68749ab747fSPaolo Bonzini 68877453882SBenjamin Herrenschmidt /* Finally update the status register image */ 68949ab747fSPaolo Bonzini ch->regs[DBDMA_STATUS] = status; 69049ab747fSPaolo Bonzini 69177453882SBenjamin Herrenschmidt /* If active, make sure the BH gets to run */ 692d2f0ce21SAlexander Graf if (status & ACTIVE) { 693d2f0ce21SAlexander Graf DBDMA_kick(dbdma_from_ch(ch)); 694d2f0ce21SAlexander Graf } 695d2f0ce21SAlexander Graf } 69649ab747fSPaolo Bonzini 69749ab747fSPaolo Bonzini static void dbdma_write(void *opaque, hwaddr addr, 69849ab747fSPaolo Bonzini uint64_t value, unsigned size) 69949ab747fSPaolo Bonzini { 70049ab747fSPaolo Bonzini int channel = addr >> DBDMA_CHANNEL_SHIFT; 70149ab747fSPaolo Bonzini DBDMAState *s = opaque; 70249ab747fSPaolo Bonzini DBDMA_channel *ch = &s->channels[channel]; 70349ab747fSPaolo Bonzini int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2; 70449ab747fSPaolo Bonzini 7053e49c439SMark Cave-Ayland DBDMA_DPRINTFCH(ch, "writel 0x" TARGET_FMT_plx " <= 0x%08"PRIx64"\n", 70658c0c311SAlexander Graf addr, value); 7073e49c439SMark Cave-Ayland DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n", 70849ab747fSPaolo Bonzini (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg); 70949ab747fSPaolo Bonzini 7107eaba824SAlexander Graf /* cmdptr cannot be modified if channel is ACTIVE */ 71149ab747fSPaolo Bonzini 7127eaba824SAlexander Graf if (reg == DBDMA_CMDPTR_LO && (ch->regs[DBDMA_STATUS] & ACTIVE)) { 71349ab747fSPaolo Bonzini return; 7147eaba824SAlexander Graf } 71549ab747fSPaolo Bonzini 71649ab747fSPaolo Bonzini ch->regs[reg] = value; 71749ab747fSPaolo Bonzini 71849ab747fSPaolo Bonzini switch(reg) { 71949ab747fSPaolo Bonzini case DBDMA_CONTROL: 72049ab747fSPaolo Bonzini dbdma_control_write(ch); 72149ab747fSPaolo Bonzini break; 72249ab747fSPaolo Bonzini case DBDMA_CMDPTR_LO: 72349ab747fSPaolo Bonzini /* 16-byte aligned */ 72449ab747fSPaolo Bonzini ch->regs[DBDMA_CMDPTR_LO] &= ~0xf; 72549ab747fSPaolo Bonzini dbdma_cmdptr_load(ch); 72649ab747fSPaolo Bonzini break; 72749ab747fSPaolo Bonzini case DBDMA_STATUS: 72849ab747fSPaolo Bonzini case DBDMA_INTR_SEL: 72949ab747fSPaolo Bonzini case DBDMA_BRANCH_SEL: 73049ab747fSPaolo Bonzini case DBDMA_WAIT_SEL: 73149ab747fSPaolo Bonzini /* nothing to do */ 73249ab747fSPaolo Bonzini break; 73349ab747fSPaolo Bonzini case DBDMA_XFER_MODE: 73449ab747fSPaolo Bonzini case DBDMA_CMDPTR_HI: 73549ab747fSPaolo Bonzini case DBDMA_DATA2PTR_HI: 73649ab747fSPaolo Bonzini case DBDMA_DATA2PTR_LO: 73749ab747fSPaolo Bonzini case DBDMA_ADDRESS_HI: 73849ab747fSPaolo Bonzini case DBDMA_BRANCH_ADDR_HI: 73949ab747fSPaolo Bonzini case DBDMA_RES1: 74049ab747fSPaolo Bonzini case DBDMA_RES2: 74149ab747fSPaolo Bonzini case DBDMA_RES3: 74249ab747fSPaolo Bonzini case DBDMA_RES4: 74349ab747fSPaolo Bonzini /* unused */ 74449ab747fSPaolo Bonzini break; 74549ab747fSPaolo Bonzini } 74649ab747fSPaolo Bonzini } 74749ab747fSPaolo Bonzini 74849ab747fSPaolo Bonzini static uint64_t dbdma_read(void *opaque, hwaddr addr, 74949ab747fSPaolo Bonzini unsigned size) 75049ab747fSPaolo Bonzini { 75149ab747fSPaolo Bonzini uint32_t value; 75249ab747fSPaolo Bonzini int channel = addr >> DBDMA_CHANNEL_SHIFT; 75349ab747fSPaolo Bonzini DBDMAState *s = opaque; 75449ab747fSPaolo Bonzini DBDMA_channel *ch = &s->channels[channel]; 75549ab747fSPaolo Bonzini int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2; 75649ab747fSPaolo Bonzini 75749ab747fSPaolo Bonzini value = ch->regs[reg]; 75849ab747fSPaolo Bonzini 75949ab747fSPaolo Bonzini switch(reg) { 76049ab747fSPaolo Bonzini case DBDMA_CONTROL: 76177453882SBenjamin Herrenschmidt value = ch->regs[DBDMA_STATUS]; 76249ab747fSPaolo Bonzini break; 76349ab747fSPaolo Bonzini case DBDMA_STATUS: 76449ab747fSPaolo Bonzini case DBDMA_CMDPTR_LO: 76549ab747fSPaolo Bonzini case DBDMA_INTR_SEL: 76649ab747fSPaolo Bonzini case DBDMA_BRANCH_SEL: 76749ab747fSPaolo Bonzini case DBDMA_WAIT_SEL: 76849ab747fSPaolo Bonzini /* nothing to do */ 76949ab747fSPaolo Bonzini break; 77049ab747fSPaolo Bonzini case DBDMA_XFER_MODE: 77149ab747fSPaolo Bonzini case DBDMA_CMDPTR_HI: 77249ab747fSPaolo Bonzini case DBDMA_DATA2PTR_HI: 77349ab747fSPaolo Bonzini case DBDMA_DATA2PTR_LO: 77449ab747fSPaolo Bonzini case DBDMA_ADDRESS_HI: 77549ab747fSPaolo Bonzini case DBDMA_BRANCH_ADDR_HI: 77649ab747fSPaolo Bonzini /* unused */ 77749ab747fSPaolo Bonzini value = 0; 77849ab747fSPaolo Bonzini break; 77949ab747fSPaolo Bonzini case DBDMA_RES1: 78049ab747fSPaolo Bonzini case DBDMA_RES2: 78149ab747fSPaolo Bonzini case DBDMA_RES3: 78249ab747fSPaolo Bonzini case DBDMA_RES4: 78349ab747fSPaolo Bonzini /* reserved */ 78449ab747fSPaolo Bonzini break; 78549ab747fSPaolo Bonzini } 78649ab747fSPaolo Bonzini 78777453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, "readl 0x" TARGET_FMT_plx " => 0x%08x\n", addr, value); 78877453882SBenjamin Herrenschmidt DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n", 78977453882SBenjamin Herrenschmidt (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg); 79077453882SBenjamin Herrenschmidt 79149ab747fSPaolo Bonzini return value; 79249ab747fSPaolo Bonzini } 79349ab747fSPaolo Bonzini 79449ab747fSPaolo Bonzini static const MemoryRegionOps dbdma_ops = { 79549ab747fSPaolo Bonzini .read = dbdma_read, 79649ab747fSPaolo Bonzini .write = dbdma_write, 79749ab747fSPaolo Bonzini .endianness = DEVICE_LITTLE_ENDIAN, 79849ab747fSPaolo Bonzini .valid = { 79949ab747fSPaolo Bonzini .min_access_size = 4, 80049ab747fSPaolo Bonzini .max_access_size = 4, 80149ab747fSPaolo Bonzini }, 80249ab747fSPaolo Bonzini }; 80349ab747fSPaolo Bonzini 804627be2f2SMark Cave-Ayland static const VMStateDescription vmstate_dbdma_io = { 805627be2f2SMark Cave-Ayland .name = "dbdma_io", 80649ab747fSPaolo Bonzini .version_id = 0, 80749ab747fSPaolo Bonzini .minimum_version_id = 0, 80849ab747fSPaolo Bonzini .fields = (VMStateField[]) { 809627be2f2SMark Cave-Ayland VMSTATE_UINT64(addr, struct DBDMA_io), 810627be2f2SMark Cave-Ayland VMSTATE_INT32(len, struct DBDMA_io), 811627be2f2SMark Cave-Ayland VMSTATE_INT32(is_last, struct DBDMA_io), 812627be2f2SMark Cave-Ayland VMSTATE_INT32(is_dma_out, struct DBDMA_io), 813627be2f2SMark Cave-Ayland VMSTATE_BOOL(processing, struct DBDMA_io), 814627be2f2SMark Cave-Ayland VMSTATE_END_OF_LIST() 815627be2f2SMark Cave-Ayland } 816627be2f2SMark Cave-Ayland }; 817627be2f2SMark Cave-Ayland 818627be2f2SMark Cave-Ayland static const VMStateDescription vmstate_dbdma_cmd = { 819627be2f2SMark Cave-Ayland .name = "dbdma_cmd", 820627be2f2SMark Cave-Ayland .version_id = 0, 821627be2f2SMark Cave-Ayland .minimum_version_id = 0, 822627be2f2SMark Cave-Ayland .fields = (VMStateField[]) { 823627be2f2SMark Cave-Ayland VMSTATE_UINT16(req_count, dbdma_cmd), 824627be2f2SMark Cave-Ayland VMSTATE_UINT16(command, dbdma_cmd), 825627be2f2SMark Cave-Ayland VMSTATE_UINT32(phy_addr, dbdma_cmd), 826627be2f2SMark Cave-Ayland VMSTATE_UINT32(cmd_dep, dbdma_cmd), 827627be2f2SMark Cave-Ayland VMSTATE_UINT16(res_count, dbdma_cmd), 828627be2f2SMark Cave-Ayland VMSTATE_UINT16(xfer_status, dbdma_cmd), 829627be2f2SMark Cave-Ayland VMSTATE_END_OF_LIST() 830627be2f2SMark Cave-Ayland } 831627be2f2SMark Cave-Ayland }; 832627be2f2SMark Cave-Ayland 833627be2f2SMark Cave-Ayland static const VMStateDescription vmstate_dbdma_channel = { 834627be2f2SMark Cave-Ayland .name = "dbdma_channel", 835627be2f2SMark Cave-Ayland .version_id = 1, 836627be2f2SMark Cave-Ayland .minimum_version_id = 1, 837627be2f2SMark Cave-Ayland .fields = (VMStateField[]) { 83849ab747fSPaolo Bonzini VMSTATE_UINT32_ARRAY(regs, struct DBDMA_channel, DBDMA_REGS), 839627be2f2SMark Cave-Ayland VMSTATE_STRUCT(io, struct DBDMA_channel, 0, vmstate_dbdma_io, DBDMA_io), 840627be2f2SMark Cave-Ayland VMSTATE_STRUCT(current, struct DBDMA_channel, 0, vmstate_dbdma_cmd, 841627be2f2SMark Cave-Ayland dbdma_cmd), 84249ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 84349ab747fSPaolo Bonzini } 84449ab747fSPaolo Bonzini }; 84549ab747fSPaolo Bonzini 84649ab747fSPaolo Bonzini static const VMStateDescription vmstate_dbdma = { 84749ab747fSPaolo Bonzini .name = "dbdma", 848627be2f2SMark Cave-Ayland .version_id = 3, 849627be2f2SMark Cave-Ayland .minimum_version_id = 3, 85049ab747fSPaolo Bonzini .fields = (VMStateField[]) { 85149ab747fSPaolo Bonzini VMSTATE_STRUCT_ARRAY(channels, DBDMAState, DBDMA_CHANNELS, 1, 85249ab747fSPaolo Bonzini vmstate_dbdma_channel, DBDMA_channel), 85349ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 85449ab747fSPaolo Bonzini } 85549ab747fSPaolo Bonzini }; 85649ab747fSPaolo Bonzini 8571d27f351SMark Cave-Ayland static void mac_dbdma_reset(DeviceState *d) 85849ab747fSPaolo Bonzini { 8591d27f351SMark Cave-Ayland DBDMAState *s = MAC_DBDMA(d); 86049ab747fSPaolo Bonzini int i; 86149ab747fSPaolo Bonzini 8621d27f351SMark Cave-Ayland for (i = 0; i < DBDMA_CHANNELS; i++) { 86349ab747fSPaolo Bonzini memset(s->channels[i].regs, 0, DBDMA_SIZE); 86449ab747fSPaolo Bonzini } 8651d27f351SMark Cave-Ayland } 86649ab747fSPaolo Bonzini 8672d7d06d8SHervé Poussineau static void dbdma_unassigned_rw(DBDMA_io *io) 8682d7d06d8SHervé Poussineau { 8692d7d06d8SHervé Poussineau DBDMA_channel *ch = io->channel; 87077453882SBenjamin Herrenschmidt dbdma_cmd *current = &ch->current; 87177453882SBenjamin Herrenschmidt uint16_t cmd; 8722d7d06d8SHervé Poussineau qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n", 8732d7d06d8SHervé Poussineau __func__, ch->channel); 8742df77896SMark Cave-Ayland ch->io.processing = false; 87577453882SBenjamin Herrenschmidt 87677453882SBenjamin Herrenschmidt cmd = le16_to_cpu(current->command) & COMMAND_MASK; 87777453882SBenjamin Herrenschmidt if (cmd == OUTPUT_MORE || cmd == OUTPUT_LAST || 87877453882SBenjamin Herrenschmidt cmd == INPUT_MORE || cmd == INPUT_LAST) { 87977453882SBenjamin Herrenschmidt current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]); 88077453882SBenjamin Herrenschmidt current->res_count = cpu_to_le16(io->len); 88177453882SBenjamin Herrenschmidt dbdma_cmdptr_save(ch); 88277453882SBenjamin Herrenschmidt } 8832d7d06d8SHervé Poussineau } 8842d7d06d8SHervé Poussineau 8852d7d06d8SHervé Poussineau static void dbdma_unassigned_flush(DBDMA_io *io) 8862d7d06d8SHervé Poussineau { 8872d7d06d8SHervé Poussineau DBDMA_channel *ch = io->channel; 8882d7d06d8SHervé Poussineau qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n", 8892d7d06d8SHervé Poussineau __func__, ch->channel); 8902d7d06d8SHervé Poussineau } 8912d7d06d8SHervé Poussineau 8921d27f351SMark Cave-Ayland static void mac_dbdma_init(Object *obj) 8931d27f351SMark Cave-Ayland { 8941d27f351SMark Cave-Ayland SysBusDevice *sbd = SYS_BUS_DEVICE(obj); 8951d27f351SMark Cave-Ayland DBDMAState *s = MAC_DBDMA(obj); 8961d27f351SMark Cave-Ayland int i; 89749ab747fSPaolo Bonzini 8983e300fa6SAlexander Graf for (i = 0; i < DBDMA_CHANNELS; i++) { 8992d7d06d8SHervé Poussineau DBDMA_channel *ch = &s->channels[i]; 9002d7d06d8SHervé Poussineau 9012d7d06d8SHervé Poussineau ch->rw = dbdma_unassigned_rw; 9022d7d06d8SHervé Poussineau ch->flush = dbdma_unassigned_flush; 9032d7d06d8SHervé Poussineau ch->channel = i; 9042d7d06d8SHervé Poussineau ch->io.channel = ch; 9053e300fa6SAlexander Graf } 9063e300fa6SAlexander Graf 9071d27f351SMark Cave-Ayland memory_region_init_io(&s->mem, obj, &dbdma_ops, s, "dbdma", 0x1000); 9081d27f351SMark Cave-Ayland sysbus_init_mmio(sbd, &s->mem); 9091d27f351SMark Cave-Ayland } 9101d27f351SMark Cave-Ayland 9111d27f351SMark Cave-Ayland static void mac_dbdma_realize(DeviceState *dev, Error **errp) 9121d27f351SMark Cave-Ayland { 9131d27f351SMark Cave-Ayland DBDMAState *s = MAC_DBDMA(dev); 91449ab747fSPaolo Bonzini 915d2f0ce21SAlexander Graf s->bh = qemu_bh_new(DBDMA_run_bh, s); 91649ab747fSPaolo Bonzini } 9171d27f351SMark Cave-Ayland 9181d27f351SMark Cave-Ayland static void mac_dbdma_class_init(ObjectClass *oc, void *data) 9191d27f351SMark Cave-Ayland { 9201d27f351SMark Cave-Ayland DeviceClass *dc = DEVICE_CLASS(oc); 9211d27f351SMark Cave-Ayland 9221d27f351SMark Cave-Ayland dc->realize = mac_dbdma_realize; 9231d27f351SMark Cave-Ayland dc->reset = mac_dbdma_reset; 9241d27f351SMark Cave-Ayland dc->vmsd = &vmstate_dbdma; 9251d27f351SMark Cave-Ayland } 9261d27f351SMark Cave-Ayland 9271d27f351SMark Cave-Ayland static const TypeInfo mac_dbdma_type_info = { 9281d27f351SMark Cave-Ayland .name = TYPE_MAC_DBDMA, 9291d27f351SMark Cave-Ayland .parent = TYPE_SYS_BUS_DEVICE, 9301d27f351SMark Cave-Ayland .instance_size = sizeof(DBDMAState), 9311d27f351SMark Cave-Ayland .instance_init = mac_dbdma_init, 9321d27f351SMark Cave-Ayland .class_init = mac_dbdma_class_init 9331d27f351SMark Cave-Ayland }; 9341d27f351SMark Cave-Ayland 9351d27f351SMark Cave-Ayland static void mac_dbdma_register_types(void) 9361d27f351SMark Cave-Ayland { 9371d27f351SMark Cave-Ayland type_register_static(&mac_dbdma_type_info); 9381d27f351SMark Cave-Ayland } 9391d27f351SMark Cave-Ayland 9401d27f351SMark Cave-Ayland type_init(mac_dbdma_register_types) 941