2 * QEMU IDE Emulation: PCI Bus support.
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 #include "block_int.h"
34 #include <hw/ide/pci.h>
36 void bmdma_cmd_writeb(void *opaque, uint32_t addr, uint32_t val)
38 BMDMAState *bm = opaque;
40 printf("%s: 0x%08x\n", __func__, val);
43 /* Ignore writes to SSBM if it keeps the old value */
44 if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) {
45 if (!(val & BM_CMD_START)) {
47 * We can't cancel Scatter Gather DMA in the middle of the
48 * operation or a partial (not full) DMA transfer would reach
49 * the storage so we wait for completion instead (we beahve
50 * like if the DMA was completed by the time the guest trying
51 * to cancel dma with bmdma_cmd_writeb with BM_CMD_START not
54 * In the future we'll be able to safely cancel the I/O if the
55 * whole DMA operation will be submitted to disk with a single
56 * aio operation with preadv/pwritev.
62 printf("ide_dma_cancel: aiocb still pending");
63 if (bm->status & BM_STATUS_DMAING)
64 printf("ide_dma_cancel: BM_STATUS_DMAING still pending");
68 if (!(bm->status & BM_STATUS_DMAING)) {
69 bm->status |= BM_STATUS_DMAING;
70 /* start dma transfer if possible */
80 static void bmdma_addr_read(IORange *ioport, uint64_t addr,
81 unsigned width, uint64_t *data)
83 BMDMAState *bm = container_of(ioport, BMDMAState, addr_ioport);
84 uint32_t mask = (1ULL << (width * 8)) - 1;
86 *data = (bm->addr >> (addr * 8)) & mask;
88 printf("%s: 0x%08x\n", __func__, (unsigned)*data);
92 static void bmdma_addr_write(IORange *ioport, uint64_t addr,
93 unsigned width, uint64_t data)
95 BMDMAState *bm = container_of(ioport, BMDMAState, addr_ioport);
97 uint32_t mask = (1ULL << (width * 8)) - 1;
100 printf("%s: 0x%08x\n", __func__, (unsigned)data);
102 bm->addr &= ~(mask << shift);
103 bm->addr |= ((data & mask) << shift) & ~3;
104 bm->cur_addr = bm->addr;
107 const IORangeOps bmdma_addr_ioport_ops = {
108 .read = bmdma_addr_read,
109 .write = bmdma_addr_write,
112 static bool ide_bmdma_current_needed(void *opaque)
114 BMDMAState *bm = opaque;
116 return (bm->cur_prd_len != 0);
119 static const VMStateDescription vmstate_bmdma_current = {
120 .name = "ide bmdma_current",
122 .minimum_version_id = 1,
123 .minimum_version_id_old = 1,
124 .fields = (VMStateField []) {
125 VMSTATE_UINT32(cur_addr, BMDMAState),
126 VMSTATE_UINT32(cur_prd_last, BMDMAState),
127 VMSTATE_UINT32(cur_prd_addr, BMDMAState),
128 VMSTATE_UINT32(cur_prd_len, BMDMAState),
129 VMSTATE_END_OF_LIST()
134 static const VMStateDescription vmstate_bmdma = {
137 .minimum_version_id = 0,
138 .minimum_version_id_old = 0,
139 .fields = (VMStateField []) {
140 VMSTATE_UINT8(cmd, BMDMAState),
141 VMSTATE_UINT8(status, BMDMAState),
142 VMSTATE_UINT32(addr, BMDMAState),
143 VMSTATE_INT64(sector_num, BMDMAState),
144 VMSTATE_UINT32(nsector, BMDMAState),
145 VMSTATE_UINT8(unit, BMDMAState),
146 VMSTATE_END_OF_LIST()
148 .subsections = (VMStateSubsection []) {
150 .vmsd = &vmstate_bmdma_current,
151 .needed = ide_bmdma_current_needed,
158 static int ide_pci_post_load(void *opaque, int version_id)
160 PCIIDEState *d = opaque;
163 for(i = 0; i < 2; i++) {
164 /* current versions always store 0/1, but older version
165 stored bigger values. We only need last bit */
166 d->bmdma[i].unit &= 1;
171 const VMStateDescription vmstate_ide_pci = {
174 .minimum_version_id = 0,
175 .minimum_version_id_old = 0,
176 .post_load = ide_pci_post_load,
177 .fields = (VMStateField []) {
178 VMSTATE_PCI_DEVICE(dev, PCIIDEState),
179 VMSTATE_STRUCT_ARRAY(bmdma, PCIIDEState, 2, 0,
180 vmstate_bmdma, BMDMAState),
181 VMSTATE_IDE_BUS_ARRAY(bus, PCIIDEState, 2),
182 VMSTATE_IDE_DRIVES(bus[0].ifs, PCIIDEState),
183 VMSTATE_IDE_DRIVES(bus[1].ifs, PCIIDEState),
184 VMSTATE_END_OF_LIST()
188 void pci_ide_create_devs(PCIDevice *dev, DriveInfo **hd_table)
190 PCIIDEState *d = DO_UPCAST(PCIIDEState, dev, dev);
191 static const int bus[4] = { 0, 0, 1, 1 };
192 static const int unit[4] = { 0, 1, 0, 1 };
195 for (i = 0; i < 4; i++) {
196 if (hd_table[i] == NULL)
198 ide_create_drive(d->bus+bus[i], unit[i], hd_table[i]);