pci.c 6.3 KB
Newer Older
G
Gerd Hoffmann 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * QEMU IDE Emulation: PCI Bus support.
 *
 * Copyright (c) 2003 Fabrice Bellard
 * Copyright (c) 2006 Openedhand Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
G
Gerd Hoffmann 已提交
25 26 27
#include <hw/hw.h>
#include <hw/pc.h>
#include <hw/pci.h>
G
Gerd Hoffmann 已提交
28
#include <hw/isa.h>
G
Gerd Hoffmann 已提交
29 30 31 32
#include "block.h"
#include "block_int.h"
#include "sysemu.h"
#include "dma.h"
G
Gerd Hoffmann 已提交
33

34
#include <hw/ide/pci.h>
G
Gerd Hoffmann 已提交
35

36
void bmdma_cmd_writeb(void *opaque, uint32_t addr, uint32_t val)
G
Gerd Hoffmann 已提交
37 38 39 40 41
{
    BMDMAState *bm = opaque;
#ifdef DEBUG_IDE
    printf("%s: 0x%08x\n", __func__, val);
#endif
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

    /* Ignore writes to SSBM if it keeps the old value */
    if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) {
        if (!(val & BM_CMD_START)) {
            /*
             * We can't cancel Scatter Gather DMA in the middle of the
             * operation or a partial (not full) DMA transfer would reach
             * the storage so we wait for completion instead (we beahve
             * like if the DMA was completed by the time the guest trying
             * to cancel dma with bmdma_cmd_writeb with BM_CMD_START not
             * set).
             *
             * In the future we'll be able to safely cancel the I/O if the
             * whole DMA operation will be submitted to disk with a single
             * aio operation with preadv/pwritev.
             */
            if (bm->aiocb) {
                qemu_aio_flush();
A
Andrea Arcangeli 已提交
60
#ifdef DEBUG_IDE
61 62 63 64
                if (bm->aiocb)
                    printf("ide_dma_cancel: aiocb still pending");
                if (bm->status & BM_STATUS_DMAING)
                    printf("ide_dma_cancel: BM_STATUS_DMAING still pending");
A
Andrea Arcangeli 已提交
65
#endif
66 67
            }
        } else {
68
            bm->cur_addr = bm->addr;
69 70 71 72 73 74
            if (!(bm->status & BM_STATUS_DMAING)) {
                bm->status |= BM_STATUS_DMAING;
                /* start dma transfer if possible */
                if (bm->dma_cb)
                    bm->dma_cb(bm, 0);
            }
A
Andrea Arcangeli 已提交
75
        }
G
Gerd Hoffmann 已提交
76
    }
77 78

    bm->cmd = val & 0x09;
G
Gerd Hoffmann 已提交
79 80
}

81 82
static void bmdma_addr_read(IORange *ioport, uint64_t addr,
                            unsigned width, uint64_t *data)
G
Gerd Hoffmann 已提交
83
{
84 85
    BMDMAState *bm = container_of(ioport, BMDMAState, addr_ioport);
    uint32_t mask = (1ULL << (width * 8)) - 1;
G
Gerd Hoffmann 已提交
86

87
    *data = (bm->addr >> (addr * 8)) & mask;
G
Gerd Hoffmann 已提交
88
#ifdef DEBUG_IDE
89
    printf("%s: 0x%08x\n", __func__, (unsigned)*data);
G
Gerd Hoffmann 已提交
90 91 92
#endif
}

93 94
static void bmdma_addr_write(IORange *ioport, uint64_t addr,
                             unsigned width, uint64_t data)
G
Gerd Hoffmann 已提交
95
{
96 97 98
    BMDMAState *bm = container_of(ioport, BMDMAState, addr_ioport);
    int shift = addr * 8;
    uint32_t mask = (1ULL << (width * 8)) - 1;
G
Gerd Hoffmann 已提交
99 100

#ifdef DEBUG_IDE
101
    printf("%s: 0x%08x\n", __func__, (unsigned)data);
G
Gerd Hoffmann 已提交
102
#endif
103 104
    bm->addr &= ~(mask << shift);
    bm->addr |= ((data & mask) << shift) & ~3;
G
Gerd Hoffmann 已提交
105 106
}

107 108 109 110
const IORangeOps bmdma_addr_ioport_ops = {
    .read = bmdma_addr_read,
    .write = bmdma_addr_write,
};
G
Gerd Hoffmann 已提交
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
static bool ide_bmdma_current_needed(void *opaque)
{
    BMDMAState *bm = opaque;

    return (bm->cur_prd_len != 0);
}

static const VMStateDescription vmstate_bmdma_current = {
    .name = "ide bmdma_current",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField []) {
        VMSTATE_UINT32(cur_addr, BMDMAState),
        VMSTATE_UINT32(cur_prd_last, BMDMAState),
        VMSTATE_UINT32(cur_prd_addr, BMDMAState),
        VMSTATE_UINT32(cur_prd_len, BMDMAState),
        VMSTATE_END_OF_LIST()
    }
};


J
Juan Quintela 已提交
134 135
static const VMStateDescription vmstate_bmdma = {
    .name = "ide bmdma",
136
    .version_id = 3,
J
Juan Quintela 已提交
137 138 139 140 141 142 143 144 145 146
    .minimum_version_id = 0,
    .minimum_version_id_old = 0,
    .fields      = (VMStateField []) {
        VMSTATE_UINT8(cmd, BMDMAState),
        VMSTATE_UINT8(status, BMDMAState),
        VMSTATE_UINT32(addr, BMDMAState),
        VMSTATE_INT64(sector_num, BMDMAState),
        VMSTATE_UINT32(nsector, BMDMAState),
        VMSTATE_UINT8(unit, BMDMAState),
        VMSTATE_END_OF_LIST()
147 148 149 150 151 152 153 154
    },
    .subsections = (VMStateSubsection []) {
        {
            .vmsd = &vmstate_bmdma_current,
            .needed = ide_bmdma_current_needed,
        }, {
            /* empty */
        }
G
Gerd Hoffmann 已提交
155
    }
J
Juan Quintela 已提交
156
};
G
Gerd Hoffmann 已提交
157

J
Juan Quintela 已提交
158
static int ide_pci_post_load(void *opaque, int version_id)
G
Gerd Hoffmann 已提交
159 160
{
    PCIIDEState *d = opaque;
J
Juan Quintela 已提交
161
    int i;
G
Gerd Hoffmann 已提交
162 163

    for(i = 0; i < 2; i++) {
J
Juan Quintela 已提交
164 165 166
        /* current versions always store 0/1, but older version
           stored bigger values. We only need last bit */
        d->bmdma[i].unit &= 1;
G
Gerd Hoffmann 已提交
167 168 169 170
    }
    return 0;
}

J
Juan Quintela 已提交
171 172
const VMStateDescription vmstate_ide_pci = {
    .name = "ide",
173
    .version_id = 3,
J
Juan Quintela 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187
    .minimum_version_id = 0,
    .minimum_version_id_old = 0,
    .post_load = ide_pci_post_load,
    .fields      = (VMStateField []) {
        VMSTATE_PCI_DEVICE(dev, PCIIDEState),
        VMSTATE_STRUCT_ARRAY(bmdma, PCIIDEState, 2, 0,
                             vmstate_bmdma, BMDMAState),
        VMSTATE_IDE_BUS_ARRAY(bus, PCIIDEState, 2),
        VMSTATE_IDE_DRIVES(bus[0].ifs, PCIIDEState),
        VMSTATE_IDE_DRIVES(bus[1].ifs, PCIIDEState),
        VMSTATE_END_OF_LIST()
    }
};

188
void pci_ide_create_devs(PCIDevice *dev, DriveInfo **hd_table)
G
Gerd Hoffmann 已提交
189 190 191 192 193 194 195 196 197
{
    PCIIDEState *d = DO_UPCAST(PCIIDEState, dev, dev);
    static const int bus[4]  = { 0, 0, 1, 1 };
    static const int unit[4] = { 0, 1, 0, 1 };
    int i;

    for (i = 0; i < 4; i++) {
        if (hd_table[i] == NULL)
            continue;
198
        ide_create_drive(d->bus+bus[i], unit[i], hd_table[i]);
G
Gerd Hoffmann 已提交
199 200
    }
}