]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ide/piix.c
Include qemu/module.h where needed, drop it from qemu-common.h
[mirror_qemu.git] / hw / ide / piix.c
CommitLineData
4c3df0ec
JQ
1/*
2 * QEMU IDE Emulation: PCI PIIX3/4 support.
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
dfc65f1f 25
53239262 26#include "qemu/osdep.h"
a9c94277 27#include "hw/hw.h"
a9c94277 28#include "hw/pci/pci.h"
0b8fa32f 29#include "qemu/module.h"
b9fe8a7a 30#include "sysemu/block-backend.h"
9c17d615 31#include "sysemu/sysemu.h"
78631611 32#include "sysemu/blockdev.h"
9c17d615 33#include "sysemu/dma.h"
4c3df0ec 34
a9c94277 35#include "hw/ide/pci.h"
3eee2611 36#include "trace.h"
4c3df0ec 37
a8170e5e 38static uint64_t bmdma_read(void *opaque, hwaddr addr, unsigned size)
4c3df0ec
JQ
39{
40 BMDMAState *bm = opaque;
41 uint32_t val;
42
a9deb8c6
AK
43 if (size != 1) {
44 return ((uint64_t)1 << (size * 8)) - 1;
45 }
46
4c3df0ec
JQ
47 switch(addr & 3) {
48 case 0:
49 val = bm->cmd;
50 break;
51 case 2:
52 val = bm->status;
53 break;
54 default:
55 val = 0xff;
56 break;
57 }
3eee2611
JS
58
59 trace_bmdma_read(addr, val);
4c3df0ec
JQ
60 return val;
61}
62
a8170e5e 63static void bmdma_write(void *opaque, hwaddr addr,
a9deb8c6 64 uint64_t val, unsigned size)
4c3df0ec
JQ
65{
66 BMDMAState *bm = opaque;
a9deb8c6
AK
67
68 if (size != 1) {
69 return;
70 }
71
3eee2611
JS
72 trace_bmdma_write(addr, val);
73
4c3df0ec 74 switch(addr & 3) {
a9deb8c6 75 case 0:
0ed8b6f6
BS
76 bmdma_cmd_writeb(bm, val);
77 break;
4c3df0ec
JQ
78 case 2:
79 bm->status = (val & 0x60) | (bm->status & 1) | (bm->status & ~val & 0x06);
80 break;
81 }
82}
83
a348f108 84static const MemoryRegionOps piix_bmdma_ops = {
a9deb8c6
AK
85 .read = bmdma_read,
86 .write = bmdma_write,
87};
88
89static void bmdma_setup_bar(PCIIDEState *d)
4c3df0ec 90{
4c3df0ec
JQ
91 int i;
92
1437c94b 93 memory_region_init(&d->bmdma_bar, OBJECT(d), "piix-bmdma-container", 16);
4c3df0ec
JQ
94 for(i = 0;i < 2; i++) {
95 BMDMAState *bm = &d->bmdma[i];
4c3df0ec 96
1437c94b 97 memory_region_init_io(&bm->extra_io, OBJECT(d), &piix_bmdma_ops, bm,
a9deb8c6
AK
98 "piix-bmdma", 4);
99 memory_region_add_subregion(&d->bmdma_bar, i * 8, &bm->extra_io);
1437c94b
PB
100 memory_region_init_io(&bm->addr_ioport, OBJECT(d),
101 &bmdma_addr_ioport_ops, bm, "bmdma", 4);
a9deb8c6 102 memory_region_add_subregion(&d->bmdma_bar, i * 8 + 4, &bm->addr_ioport);
4c3df0ec
JQ
103 }
104}
105
106static void piix3_reset(void *opaque)
107{
108 PCIIDEState *d = opaque;
f6c11d56
AF
109 PCIDevice *pd = PCI_DEVICE(d);
110 uint8_t *pci_conf = pd->config;
4c3df0ec
JQ
111 int i;
112
4a643563
BS
113 for (i = 0; i < 2; i++) {
114 ide_bus_reset(&d->bus[i]);
4a643563 115 }
4c3df0ec 116
1e68f8c4
MT
117 /* TODO: this is the default. do not override. */
118 pci_conf[PCI_COMMAND] = 0x00;
119 /* TODO: this is the default. do not override. */
120 pci_conf[PCI_COMMAND + 1] = 0x00;
121 /* TODO: use pci_set_word */
122 pci_conf[PCI_STATUS] = PCI_STATUS_FAST_BACK;
123 pci_conf[PCI_STATUS + 1] = PCI_STATUS_DEVSEL_MEDIUM >> 8;
4c3df0ec
JQ
124 pci_conf[0x20] = 0x01; /* BMIBA: 20-23h */
125}
126
61d9d6b0 127static void pci_piix_init_ports(PCIIDEState *d) {
4a91d3b3 128 static const struct {
61d9d6b0
SH
129 int iobase;
130 int iobase2;
131 int isairq;
132 } port_info[] = {
133 {0x1f0, 0x3f6, 14},
134 {0x170, 0x376, 15},
135 };
4a91d3b3 136 int i;
61d9d6b0
SH
137
138 for (i = 0; i < 2; i++) {
c6baf942 139 ide_bus_new(&d->bus[i], sizeof(d->bus[i]), DEVICE(d), i, 2);
4a91d3b3
RH
140 ide_init_ioport(&d->bus[i], NULL, port_info[i].iobase,
141 port_info[i].iobase2);
48a18b3c 142 ide_init2(&d->bus[i], isa_get_irq(NULL, port_info[i].isairq));
61d9d6b0 143
a9deb8c6 144 bmdma_init(&d->bus[i], &d->bmdma[i], d);
61d9d6b0 145 d->bmdma[i].bus = &d->bus[i];
f878c916 146 ide_register_restart_cb(&d->bus[i]);
61d9d6b0
SH
147 }
148}
149
9af21dbe 150static void pci_piix_ide_realize(PCIDevice *dev, Error **errp)
4c3df0ec 151{
f6c11d56
AF
152 PCIIDEState *d = PCI_IDE(dev);
153 uint8_t *pci_conf = dev->config;
4c3df0ec 154
1e68f8c4 155 pci_conf[PCI_CLASS_PROG] = 0x80; // legacy ATA mode
4c3df0ec
JQ
156
157 qemu_register_reset(piix3_reset, d);
4c3df0ec 158
a9deb8c6 159 bmdma_setup_bar(d);
f6c11d56 160 pci_register_bar(dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &d->bmdma_bar);
4c3df0ec 161
02a9594b 162 vmstate_register(DEVICE(dev), 0, &vmstate_ide_pci, d);
4c3df0ec 163
61d9d6b0 164 pci_piix_init_ports(d);
4c3df0ec
JQ
165}
166
ae4d2eb2 167int pci_piix3_xen_ide_unplug(DeviceState *dev, bool aux)
679f4f8b 168{
679f4f8b
SS
169 PCIIDEState *pci_ide;
170 DriveInfo *di;
d4f9e806 171 int i;
6cd38783 172 IDEDevice *idedev;
679f4f8b 173
f6c11d56 174 pci_ide = PCI_IDE(dev);
679f4f8b 175
ae4d2eb2 176 for (i = aux ? 1 : 0; i < 4; i++) {
679f4f8b 177 di = drive_get_by_index(IF_IDE, i);
f9e8fda4 178 if (di != NULL && !di->media_cd) {
b9fe8a7a 179 BlockBackend *blk = blk_by_legacy_dinfo(di);
4be74634 180 DeviceState *ds = blk_get_attached_dev(blk);
49137bf6
JS
181
182 blk_drain(blk);
183 blk_flush(blk);
184
679f4f8b 185 if (ds) {
4be74634 186 blk_detach_dev(blk, ds);
679f4f8b 187 }
4be74634 188 pci_ide->bus[di->bus].ifs[di->unit].blk = NULL;
6cd38783
SS
189 if (!(i % 2)) {
190 idedev = pci_ide->bus[di->bus].master;
191 } else {
192 idedev = pci_ide->bus[di->bus].slave;
193 }
194 idedev->conf.blk = NULL;
d1fc684f 195 monitor_remove_blk(blk);
b9fe8a7a 196 blk_unref(blk);
679f4f8b
SS
197 }
198 }
02a9594b 199 qdev_reset_all(DEVICE(dev));
679f4f8b
SS
200 return 0;
201}
202
203PCIDevice *pci_piix3_xen_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn)
204{
205 PCIDevice *dev;
206
207 dev = pci_create_simple(bus, devfn, "piix3-ide-xen");
679f4f8b
SS
208 pci_ide_create_devs(dev, hd_table);
209 return dev;
210}
211
f90c2bcd 212static void pci_piix_ide_exitfn(PCIDevice *dev)
a9deb8c6 213{
f6c11d56 214 PCIIDEState *d = PCI_IDE(dev);
a9deb8c6
AK
215 unsigned i;
216
217 for (i = 0; i < 2; ++i) {
218 memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].extra_io);
a9deb8c6 219 memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].addr_ioport);
a9deb8c6 220 }
a9deb8c6
AK
221}
222
4c3df0ec
JQ
223/* hd_table must contain 4 block drivers */
224/* NOTE: for the PIIX3, the IRQs and IOports are hardcoded */
57c88866 225PCIDevice *pci_piix3_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn)
4c3df0ec
JQ
226{
227 PCIDevice *dev;
228
556cd098 229 dev = pci_create_simple(bus, devfn, "piix3-ide");
4c3df0ec 230 pci_ide_create_devs(dev, hd_table);
57c88866 231 return dev;
4c3df0ec
JQ
232}
233
234/* hd_table must contain 4 block drivers */
235/* NOTE: for the PIIX4, the IRQs and IOports are hardcoded */
57c88866 236PCIDevice *pci_piix4_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn)
4c3df0ec
JQ
237{
238 PCIDevice *dev;
239
556cd098 240 dev = pci_create_simple(bus, devfn, "piix4-ide");
4c3df0ec 241 pci_ide_create_devs(dev, hd_table);
57c88866 242 return dev;
4c3df0ec
JQ
243}
244
40021f08
AL
245static void piix3_ide_class_init(ObjectClass *klass, void *data)
246{
39bffca2 247 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
248 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
249
9af21dbe 250 k->realize = pci_piix_ide_realize;
40021f08
AL
251 k->exit = pci_piix_ide_exitfn;
252 k->vendor_id = PCI_VENDOR_ID_INTEL;
253 k->device_id = PCI_DEVICE_ID_INTEL_82371SB_1;
254 k->class_id = PCI_CLASS_STORAGE_IDE;
125ee0ed 255 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2897ae02 256 dc->hotpluggable = false;
40021f08
AL
257}
258
8c43a6f0 259static const TypeInfo piix3_ide_info = {
39bffca2 260 .name = "piix3-ide",
f6c11d56 261 .parent = TYPE_PCI_IDE,
39bffca2 262 .class_init = piix3_ide_class_init,
e855761c
AL
263};
264
8c43a6f0 265static const TypeInfo piix3_ide_xen_info = {
39bffca2 266 .name = "piix3-ide-xen",
f6c11d56 267 .parent = TYPE_PCI_IDE,
0f844582 268 .class_init = piix3_ide_class_init,
e855761c
AL
269};
270
40021f08
AL
271static void piix4_ide_class_init(ObjectClass *klass, void *data)
272{
39bffca2 273 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
274 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
275
9af21dbe 276 k->realize = pci_piix_ide_realize;
40021f08
AL
277 k->exit = pci_piix_ide_exitfn;
278 k->vendor_id = PCI_VENDOR_ID_INTEL;
279 k->device_id = PCI_DEVICE_ID_INTEL_82371AB;
280 k->class_id = PCI_CLASS_STORAGE_IDE;
125ee0ed 281 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2897ae02 282 dc->hotpluggable = false;
40021f08
AL
283}
284
8c43a6f0 285static const TypeInfo piix4_ide_info = {
39bffca2 286 .name = "piix4-ide",
f6c11d56 287 .parent = TYPE_PCI_IDE,
39bffca2 288 .class_init = piix4_ide_class_init,
4c3df0ec
JQ
289};
290
83f7d43a 291static void piix_ide_register_types(void)
4c3df0ec 292{
39bffca2
AL
293 type_register_static(&piix3_ide_info);
294 type_register_static(&piix3_ide_xen_info);
295 type_register_static(&piix4_ide_info);
4c3df0ec 296}
83f7d43a
AF
297
298type_init(piix_ide_register_types)