xref: /qemu/hw/ide/pci.c (revision c29947bbb0978d312074ec73be968bfab1b6c977)
1977e1244SGerd Hoffmann /*
2977e1244SGerd Hoffmann  * QEMU IDE Emulation: PCI Bus support.
3977e1244SGerd Hoffmann  *
4977e1244SGerd Hoffmann  * Copyright (c) 2003 Fabrice Bellard
5977e1244SGerd Hoffmann  * Copyright (c) 2006 Openedhand Ltd.
6977e1244SGerd Hoffmann  *
7977e1244SGerd Hoffmann  * Permission is hereby granted, free of charge, to any person obtaining a copy
8977e1244SGerd Hoffmann  * of this software and associated documentation files (the "Software"), to deal
9977e1244SGerd Hoffmann  * in the Software without restriction, including without limitation the rights
10977e1244SGerd Hoffmann  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11977e1244SGerd Hoffmann  * copies of the Software, and to permit persons to whom the Software is
12977e1244SGerd Hoffmann  * furnished to do so, subject to the following conditions:
13977e1244SGerd Hoffmann  *
14977e1244SGerd Hoffmann  * The above copyright notice and this permission notice shall be included in
15977e1244SGerd Hoffmann  * all copies or substantial portions of the Software.
16977e1244SGerd Hoffmann  *
17977e1244SGerd Hoffmann  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18977e1244SGerd Hoffmann  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19977e1244SGerd Hoffmann  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20977e1244SGerd Hoffmann  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21977e1244SGerd Hoffmann  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22977e1244SGerd Hoffmann  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23977e1244SGerd Hoffmann  * THE SOFTWARE.
24977e1244SGerd Hoffmann  */
2559f2a787SGerd Hoffmann #include <hw/hw.h>
2659f2a787SGerd Hoffmann #include <hw/pc.h>
2759f2a787SGerd Hoffmann #include <hw/pci.h>
28feef3102SGerd Hoffmann #include <hw/isa.h>
29977e1244SGerd Hoffmann #include "block.h"
30977e1244SGerd Hoffmann #include "block_int.h"
31977e1244SGerd Hoffmann #include "sysemu.h"
32977e1244SGerd Hoffmann #include "dma.h"
3359f2a787SGerd Hoffmann 
3465c0f135SJuan Quintela #include <hw/ide/pci.h>
35977e1244SGerd Hoffmann 
363e7e1558SJuan Quintela void bmdma_cmd_writeb(void *opaque, uint32_t addr, uint32_t val)
37977e1244SGerd Hoffmann {
38977e1244SGerd Hoffmann     BMDMAState *bm = opaque;
39977e1244SGerd Hoffmann #ifdef DEBUG_IDE
40977e1244SGerd Hoffmann     printf("%s: 0x%08x\n", __func__, val);
41977e1244SGerd Hoffmann #endif
42*c29947bbSKevin Wolf 
43*c29947bbSKevin Wolf     /* Ignore writes to SSBM if it keeps the old value */
44*c29947bbSKevin Wolf     if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) {
45977e1244SGerd Hoffmann         if (!(val & BM_CMD_START)) {
46953844d1SAndrea Arcangeli             /*
47953844d1SAndrea Arcangeli              * We can't cancel Scatter Gather DMA in the middle of the
48953844d1SAndrea Arcangeli              * operation or a partial (not full) DMA transfer would reach
49953844d1SAndrea Arcangeli              * the storage so we wait for completion instead (we beahve
50953844d1SAndrea Arcangeli              * like if the DMA was completed by the time the guest trying
51953844d1SAndrea Arcangeli              * to cancel dma with bmdma_cmd_writeb with BM_CMD_START not
52953844d1SAndrea Arcangeli              * set).
53953844d1SAndrea Arcangeli              *
54953844d1SAndrea Arcangeli              * In the future we'll be able to safely cancel the I/O if the
55953844d1SAndrea Arcangeli              * whole DMA operation will be submitted to disk with a single
56953844d1SAndrea Arcangeli              * aio operation with preadv/pwritev.
57953844d1SAndrea Arcangeli              */
58953844d1SAndrea Arcangeli             if (bm->aiocb) {
59953844d1SAndrea Arcangeli                 qemu_aio_flush();
60953844d1SAndrea Arcangeli #ifdef DEBUG_IDE
61953844d1SAndrea Arcangeli                 if (bm->aiocb)
62953844d1SAndrea Arcangeli                     printf("ide_dma_cancel: aiocb still pending");
63953844d1SAndrea Arcangeli                 if (bm->status & BM_STATUS_DMAING)
64953844d1SAndrea Arcangeli                     printf("ide_dma_cancel: BM_STATUS_DMAING still pending");
65953844d1SAndrea Arcangeli #endif
66953844d1SAndrea Arcangeli             }
67977e1244SGerd Hoffmann         } else {
68977e1244SGerd Hoffmann             if (!(bm->status & BM_STATUS_DMAING)) {
69977e1244SGerd Hoffmann                 bm->status |= BM_STATUS_DMAING;
70977e1244SGerd Hoffmann                 /* start dma transfer if possible */
71977e1244SGerd Hoffmann                 if (bm->dma_cb)
72977e1244SGerd Hoffmann                     bm->dma_cb(bm, 0);
73977e1244SGerd Hoffmann             }
74977e1244SGerd Hoffmann         }
75977e1244SGerd Hoffmann     }
76977e1244SGerd Hoffmann 
77*c29947bbSKevin Wolf     bm->cmd = val & 0x09;
78*c29947bbSKevin Wolf }
79*c29947bbSKevin Wolf 
809fbef1acSAvi Kivity static void bmdma_addr_read(IORange *ioport, uint64_t addr,
819fbef1acSAvi Kivity                             unsigned width, uint64_t *data)
82977e1244SGerd Hoffmann {
839fbef1acSAvi Kivity     BMDMAState *bm = container_of(ioport, BMDMAState, addr_ioport);
849fbef1acSAvi Kivity     uint32_t mask = (1ULL << (width * 8)) - 1;
859fbef1acSAvi Kivity 
869fbef1acSAvi Kivity     *data = (bm->addr >> (addr * 8)) & mask;
87977e1244SGerd Hoffmann #ifdef DEBUG_IDE
889fbef1acSAvi Kivity     printf("%s: 0x%08x\n", __func__, (unsigned)*data);
89977e1244SGerd Hoffmann #endif
90977e1244SGerd Hoffmann }
91977e1244SGerd Hoffmann 
929fbef1acSAvi Kivity static void bmdma_addr_write(IORange *ioport, uint64_t addr,
939fbef1acSAvi Kivity                              unsigned width, uint64_t data)
94977e1244SGerd Hoffmann {
959fbef1acSAvi Kivity     BMDMAState *bm = container_of(ioport, BMDMAState, addr_ioport);
969fbef1acSAvi Kivity     int shift = addr * 8;
979fbef1acSAvi Kivity     uint32_t mask = (1ULL << (width * 8)) - 1;
989fbef1acSAvi Kivity 
99977e1244SGerd Hoffmann #ifdef DEBUG_IDE
1009fbef1acSAvi Kivity     printf("%s: 0x%08x\n", __func__, (unsigned)data);
101977e1244SGerd Hoffmann #endif
1029fbef1acSAvi Kivity     bm->addr &= ~(mask << shift);
1039fbef1acSAvi Kivity     bm->addr |= ((data & mask) << shift) & ~3;
104977e1244SGerd Hoffmann     bm->cur_addr = bm->addr;
105977e1244SGerd Hoffmann }
106977e1244SGerd Hoffmann 
1079fbef1acSAvi Kivity const IORangeOps bmdma_addr_ioport_ops = {
1089fbef1acSAvi Kivity     .read = bmdma_addr_read,
1099fbef1acSAvi Kivity     .write = bmdma_addr_write,
1109fbef1acSAvi Kivity };
111977e1244SGerd Hoffmann 
1125ee84c33SJuan Quintela static bool ide_bmdma_current_needed(void *opaque)
1135ee84c33SJuan Quintela {
1145ee84c33SJuan Quintela     BMDMAState *bm = opaque;
1155ee84c33SJuan Quintela 
1165ee84c33SJuan Quintela     return (bm->cur_prd_len != 0);
1175ee84c33SJuan Quintela }
1185ee84c33SJuan Quintela 
1195ee84c33SJuan Quintela static const VMStateDescription vmstate_bmdma_current = {
1205ee84c33SJuan Quintela     .name = "ide bmdma_current",
1215ee84c33SJuan Quintela     .version_id = 1,
1225ee84c33SJuan Quintela     .minimum_version_id = 1,
1235ee84c33SJuan Quintela     .minimum_version_id_old = 1,
1245ee84c33SJuan Quintela     .fields      = (VMStateField []) {
1255ee84c33SJuan Quintela         VMSTATE_UINT32(cur_addr, BMDMAState),
1265ee84c33SJuan Quintela         VMSTATE_UINT32(cur_prd_last, BMDMAState),
1275ee84c33SJuan Quintela         VMSTATE_UINT32(cur_prd_addr, BMDMAState),
1285ee84c33SJuan Quintela         VMSTATE_UINT32(cur_prd_len, BMDMAState),
1295ee84c33SJuan Quintela         VMSTATE_END_OF_LIST()
1305ee84c33SJuan Quintela     }
1315ee84c33SJuan Quintela };
1325ee84c33SJuan Quintela 
1335ee84c33SJuan Quintela 
134407a4f30SJuan Quintela static const VMStateDescription vmstate_bmdma = {
135407a4f30SJuan Quintela     .name = "ide bmdma",
13657338424SJuan Quintela     .version_id = 3,
137407a4f30SJuan Quintela     .minimum_version_id = 0,
138407a4f30SJuan Quintela     .minimum_version_id_old = 0,
139407a4f30SJuan Quintela     .fields      = (VMStateField []) {
140407a4f30SJuan Quintela         VMSTATE_UINT8(cmd, BMDMAState),
141407a4f30SJuan Quintela         VMSTATE_UINT8(status, BMDMAState),
142407a4f30SJuan Quintela         VMSTATE_UINT32(addr, BMDMAState),
143407a4f30SJuan Quintela         VMSTATE_INT64(sector_num, BMDMAState),
144407a4f30SJuan Quintela         VMSTATE_UINT32(nsector, BMDMAState),
145407a4f30SJuan Quintela         VMSTATE_UINT8(unit, BMDMAState),
146407a4f30SJuan Quintela         VMSTATE_END_OF_LIST()
1475ee84c33SJuan Quintela     },
1485ee84c33SJuan Quintela     .subsections = (VMStateSubsection []) {
1495ee84c33SJuan Quintela         {
1505ee84c33SJuan Quintela             .vmsd = &vmstate_bmdma_current,
1515ee84c33SJuan Quintela             .needed = ide_bmdma_current_needed,
1525ee84c33SJuan Quintela         }, {
1535ee84c33SJuan Quintela             /* empty */
1545ee84c33SJuan Quintela         }
155407a4f30SJuan Quintela     }
156407a4f30SJuan Quintela };
157407a4f30SJuan Quintela 
158407a4f30SJuan Quintela static int ide_pci_post_load(void *opaque, int version_id)
159977e1244SGerd Hoffmann {
160977e1244SGerd Hoffmann     PCIIDEState *d = opaque;
161977e1244SGerd Hoffmann     int i;
162977e1244SGerd Hoffmann 
163977e1244SGerd Hoffmann     for(i = 0; i < 2; i++) {
164407a4f30SJuan Quintela         /* current versions always store 0/1, but older version
165407a4f30SJuan Quintela            stored bigger values. We only need last bit */
166407a4f30SJuan Quintela         d->bmdma[i].unit &= 1;
167977e1244SGerd Hoffmann     }
168977e1244SGerd Hoffmann     return 0;
169977e1244SGerd Hoffmann }
170977e1244SGerd Hoffmann 
171407a4f30SJuan Quintela const VMStateDescription vmstate_ide_pci = {
172407a4f30SJuan Quintela     .name = "ide",
17357338424SJuan Quintela     .version_id = 3,
174407a4f30SJuan Quintela     .minimum_version_id = 0,
175407a4f30SJuan Quintela     .minimum_version_id_old = 0,
176407a4f30SJuan Quintela     .post_load = ide_pci_post_load,
177407a4f30SJuan Quintela     .fields      = (VMStateField []) {
178407a4f30SJuan Quintela         VMSTATE_PCI_DEVICE(dev, PCIIDEState),
179407a4f30SJuan Quintela         VMSTATE_STRUCT_ARRAY(bmdma, PCIIDEState, 2, 0,
180407a4f30SJuan Quintela                              vmstate_bmdma, BMDMAState),
181407a4f30SJuan Quintela         VMSTATE_IDE_BUS_ARRAY(bus, PCIIDEState, 2),
182407a4f30SJuan Quintela         VMSTATE_IDE_DRIVES(bus[0].ifs, PCIIDEState),
183407a4f30SJuan Quintela         VMSTATE_IDE_DRIVES(bus[1].ifs, PCIIDEState),
184407a4f30SJuan Quintela         VMSTATE_END_OF_LIST()
185407a4f30SJuan Quintela     }
186407a4f30SJuan Quintela };
187407a4f30SJuan Quintela 
1883e7e1558SJuan Quintela void pci_ide_create_devs(PCIDevice *dev, DriveInfo **hd_table)
189feef3102SGerd Hoffmann {
190feef3102SGerd Hoffmann     PCIIDEState *d = DO_UPCAST(PCIIDEState, dev, dev);
191feef3102SGerd Hoffmann     static const int bus[4]  = { 0, 0, 1, 1 };
192feef3102SGerd Hoffmann     static const int unit[4] = { 0, 1, 0, 1 };
193feef3102SGerd Hoffmann     int i;
194feef3102SGerd Hoffmann 
195feef3102SGerd Hoffmann     for (i = 0; i < 4; i++) {
196feef3102SGerd Hoffmann         if (hd_table[i] == NULL)
197feef3102SGerd Hoffmann             continue;
1981f850f10SGerd Hoffmann         ide_create_drive(d->bus+bus[i], unit[i], hd_table[i]);
199feef3102SGerd Hoffmann     }
200feef3102SGerd Hoffmann }
201