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
28 #include "block_int.h"
32 #include "ide-internal.h"
34 /***********************************************************/
35 /* PCI IDE definitions */
39 #define MRDMODE_INTR_CH0 0x04
40 #define MRDMODE_INTR_CH1 0x08
41 #define MRDMODE_BLK_CH0 0x10
42 #define MRDMODE_BLK_CH1 0x20
43 #define UDIDETCR0 0x73
44 #define UDIDETCR1 0x7B
46 #define IDE_TYPE_PIIX3 0
47 #define IDE_TYPE_CMD646 1
48 #define IDE_TYPE_PIIX4 2
50 typedef struct PCIIDEState {
54 int type; /* see IDE_TYPE_xxx */
57 static void cmd646_update_irq(PCIIDEState *d);
59 static void ide_map(PCIDevice *pci_dev, int region_num,
60 uint32_t addr, uint32_t size, int type)
62 PCIIDEState *d = (PCIIDEState *)pci_dev;
65 if (region_num <= 3) {
66 bus = &d->bus[(region_num >> 1)];
68 register_ioport_read(addr + 2, 1, 1, ide_status_read, bus);
69 register_ioport_write(addr + 2, 1, 1, ide_cmd_write, bus);
71 register_ioport_write(addr, 8, 1, ide_ioport_write, bus);
72 register_ioport_read(addr, 8, 1, ide_ioport_read, bus);
75 register_ioport_write(addr, 2, 2, ide_data_writew, bus);
76 register_ioport_read(addr, 2, 2, ide_data_readw, bus);
77 register_ioport_write(addr, 4, 4, ide_data_writel, bus);
78 register_ioport_read(addr, 4, 4, ide_data_readl, bus);
83 static void bmdma_cmd_writeb(void *opaque, uint32_t addr, uint32_t val)
85 BMDMAState *bm = opaque;
87 printf("%s: 0x%08x\n", __func__, val);
89 if (!(val & BM_CMD_START)) {
90 /* XXX: do it better */
94 if (!(bm->status & BM_STATUS_DMAING)) {
95 bm->status |= BM_STATUS_DMAING;
96 /* start dma transfer if possible */
100 bm->cmd = val & 0x09;
104 static uint32_t bmdma_readb(void *opaque, uint32_t addr)
106 BMDMAState *bm = opaque;
107 PCIIDEState *pci_dev;
115 pci_dev = bm->pci_dev;
116 if (pci_dev->type == IDE_TYPE_CMD646) {
117 val = pci_dev->dev.config[MRDMODE];
126 pci_dev = bm->pci_dev;
127 if (pci_dev->type == IDE_TYPE_CMD646) {
128 if (bm == &pci_dev->bmdma[0])
129 val = pci_dev->dev.config[UDIDETCR0];
131 val = pci_dev->dev.config[UDIDETCR1];
141 printf("bmdma: readb 0x%02x : 0x%02x\n", addr, val);
146 static void bmdma_writeb(void *opaque, uint32_t addr, uint32_t val)
148 BMDMAState *bm = opaque;
149 PCIIDEState *pci_dev;
151 printf("bmdma: writeb 0x%02x : 0x%02x\n", addr, val);
155 pci_dev = bm->pci_dev;
156 if (pci_dev->type == IDE_TYPE_CMD646) {
157 pci_dev->dev.config[MRDMODE] =
158 (pci_dev->dev.config[MRDMODE] & ~0x30) | (val & 0x30);
159 cmd646_update_irq(pci_dev);
163 bm->status = (val & 0x60) | (bm->status & 1) | (bm->status & ~val & 0x06);
166 pci_dev = bm->pci_dev;
167 if (pci_dev->type == IDE_TYPE_CMD646) {
168 if (bm == &pci_dev->bmdma[0])
169 pci_dev->dev.config[UDIDETCR0] = val;
171 pci_dev->dev.config[UDIDETCR1] = val;
177 static uint32_t bmdma_addr_readb(void *opaque, uint32_t addr)
179 BMDMAState *bm = opaque;
181 val = (bm->addr >> ((addr & 3) * 8)) & 0xff;
183 printf("%s: 0x%08x\n", __func__, val);
188 static void bmdma_addr_writeb(void *opaque, uint32_t addr, uint32_t val)
190 BMDMAState *bm = opaque;
191 int shift = (addr & 3) * 8;
193 printf("%s: 0x%08x\n", __func__, val);
195 bm->addr &= ~(0xFF << shift);
196 bm->addr |= ((val & 0xFF) << shift) & ~3;
197 bm->cur_addr = bm->addr;
200 static uint32_t bmdma_addr_readw(void *opaque, uint32_t addr)
202 BMDMAState *bm = opaque;
204 val = (bm->addr >> ((addr & 3) * 8)) & 0xffff;
206 printf("%s: 0x%08x\n", __func__, val);
211 static void bmdma_addr_writew(void *opaque, uint32_t addr, uint32_t val)
213 BMDMAState *bm = opaque;
214 int shift = (addr & 3) * 8;
216 printf("%s: 0x%08x\n", __func__, val);
218 bm->addr &= ~(0xFFFF << shift);
219 bm->addr |= ((val & 0xFFFF) << shift) & ~3;
220 bm->cur_addr = bm->addr;
223 static uint32_t bmdma_addr_readl(void *opaque, uint32_t addr)
225 BMDMAState *bm = opaque;
229 printf("%s: 0x%08x\n", __func__, val);
234 static void bmdma_addr_writel(void *opaque, uint32_t addr, uint32_t val)
236 BMDMAState *bm = opaque;
238 printf("%s: 0x%08x\n", __func__, val);
241 bm->cur_addr = bm->addr;
244 static void bmdma_map(PCIDevice *pci_dev, int region_num,
245 uint32_t addr, uint32_t size, int type)
247 PCIIDEState *d = (PCIIDEState *)pci_dev;
250 for(i = 0;i < 2; i++) {
251 BMDMAState *bm = &d->bmdma[i];
252 d->bus[i].bmdma = bm;
253 bm->pci_dev = DO_UPCAST(PCIIDEState, dev, pci_dev);
255 qemu_add_vm_change_state_handler(ide_dma_restart_cb, bm);
257 register_ioport_write(addr, 1, 1, bmdma_cmd_writeb, bm);
259 register_ioport_write(addr + 1, 3, 1, bmdma_writeb, bm);
260 register_ioport_read(addr, 4, 1, bmdma_readb, bm);
262 register_ioport_write(addr + 4, 4, 1, bmdma_addr_writeb, bm);
263 register_ioport_read(addr + 4, 4, 1, bmdma_addr_readb, bm);
264 register_ioport_write(addr + 4, 4, 2, bmdma_addr_writew, bm);
265 register_ioport_read(addr + 4, 4, 2, bmdma_addr_readw, bm);
266 register_ioport_write(addr + 4, 4, 4, bmdma_addr_writel, bm);
267 register_ioport_read(addr + 4, 4, 4, bmdma_addr_readl, bm);
272 static void pci_ide_save(QEMUFile* f, void *opaque)
274 PCIIDEState *d = opaque;
277 pci_device_save(&d->dev, f);
279 for(i = 0; i < 2; i++) {
280 BMDMAState *bm = &d->bmdma[i];
282 qemu_put_8s(f, &bm->cmd);
283 qemu_put_8s(f, &bm->status);
284 qemu_put_be32s(f, &bm->addr);
285 qemu_put_sbe64s(f, &bm->sector_num);
286 qemu_put_be32s(f, &bm->nsector);
287 ifidx = bm->unit + 2*i;
288 qemu_put_8s(f, &ifidx);
289 /* XXX: if a transfer is pending, we do not save it yet */
292 /* per IDE interface data */
293 for(i = 0; i < 2; i++) {
294 idebus_save(f, &d->bus[i]);
297 /* per IDE drive data */
298 for(i = 0; i < 2; i++) {
299 ide_save(f, &d->bus[i].ifs[0]);
300 ide_save(f, &d->bus[i].ifs[1]);
304 static int pci_ide_load(QEMUFile* f, void *opaque, int version_id)
306 PCIIDEState *d = opaque;
309 if (version_id != 2 && version_id != 3)
311 ret = pci_device_load(&d->dev, f);
315 for(i = 0; i < 2; i++) {
316 BMDMAState *bm = &d->bmdma[i];
318 qemu_get_8s(f, &bm->cmd);
319 qemu_get_8s(f, &bm->status);
320 qemu_get_be32s(f, &bm->addr);
321 qemu_get_sbe64s(f, &bm->sector_num);
322 qemu_get_be32s(f, &bm->nsector);
323 qemu_get_8s(f, &ifidx);
324 bm->unit = ifidx & 1;
325 /* XXX: if a transfer is pending, we do not save it yet */
328 /* per IDE interface data */
329 for(i = 0; i < 2; i++) {
330 idebus_load(f, &d->bus[i], version_id);
333 /* per IDE drive data */
334 for(i = 0; i < 2; i++) {
335 ide_load(f, &d->bus[i].ifs[0], version_id);
336 ide_load(f, &d->bus[i].ifs[1], version_id);
341 /* XXX: call it also when the MRDMODE is changed from the PCI config
343 static void cmd646_update_irq(PCIIDEState *d)
346 pci_level = ((d->dev.config[MRDMODE] & MRDMODE_INTR_CH0) &&
347 !(d->dev.config[MRDMODE] & MRDMODE_BLK_CH0)) ||
348 ((d->dev.config[MRDMODE] & MRDMODE_INTR_CH1) &&
349 !(d->dev.config[MRDMODE] & MRDMODE_BLK_CH1));
350 qemu_set_irq(d->dev.irq[0], pci_level);
353 /* the PCI irq level is the logical OR of the two channels */
354 static void cmd646_set_irq(void *opaque, int channel, int level)
356 PCIIDEState *d = opaque;
359 irq_mask = MRDMODE_INTR_CH0 << channel;
361 d->dev.config[MRDMODE] |= irq_mask;
363 d->dev.config[MRDMODE] &= ~irq_mask;
364 cmd646_update_irq(d);
367 static void cmd646_reset(void *opaque)
369 PCIIDEState *d = opaque;
372 for (i = 0; i < 2; i++)
373 ide_dma_cancel(&d->bmdma[i]);
376 /* CMD646 PCI IDE controller */
377 void pci_cmd646_ide_init(PCIBus *bus, BlockDriverState **hd_table,
378 int secondary_ide_enabled)
384 d = (PCIIDEState *)pci_register_device(bus, "CMD646 IDE",
388 d->type = IDE_TYPE_CMD646;
389 pci_conf = d->dev.config;
390 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_CMD);
391 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_CMD_646);
393 pci_conf[0x08] = 0x07; // IDE controller revision
394 pci_conf[0x09] = 0x8f;
396 pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_IDE);
397 pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
399 pci_conf[0x51] = 0x04; // enable IDE0
400 if (secondary_ide_enabled) {
401 /* XXX: if not enabled, really disable the seconday IDE controller */
402 pci_conf[0x51] |= 0x08; /* enable IDE1 */
405 pci_register_bar((PCIDevice *)d, 0, 0x8,
406 PCI_ADDRESS_SPACE_IO, ide_map);
407 pci_register_bar((PCIDevice *)d, 1, 0x4,
408 PCI_ADDRESS_SPACE_IO, ide_map);
409 pci_register_bar((PCIDevice *)d, 2, 0x8,
410 PCI_ADDRESS_SPACE_IO, ide_map);
411 pci_register_bar((PCIDevice *)d, 3, 0x4,
412 PCI_ADDRESS_SPACE_IO, ide_map);
413 pci_register_bar((PCIDevice *)d, 4, 0x10,
414 PCI_ADDRESS_SPACE_IO, bmdma_map);
416 pci_conf[0x3d] = 0x01; // interrupt on pin 1
418 irq = qemu_allocate_irqs(cmd646_set_irq, d, 2);
419 ide_init2(&d->bus[0], hd_table[0], hd_table[1], irq[0]);
420 ide_init2(&d->bus[1], hd_table[2], hd_table[3], irq[1]);
422 register_savevm("ide", 0, 3, pci_ide_save, pci_ide_load, d);
423 qemu_register_reset(cmd646_reset, d);
427 static void piix3_reset(void *opaque)
429 PCIIDEState *d = opaque;
430 uint8_t *pci_conf = d->dev.config;
433 for (i = 0; i < 2; i++)
434 ide_dma_cancel(&d->bmdma[i]);
436 pci_conf[0x04] = 0x00;
437 pci_conf[0x05] = 0x00;
438 pci_conf[0x06] = 0x80; /* FBC */
439 pci_conf[0x07] = 0x02; // PCI_status_devsel_medium
440 pci_conf[0x20] = 0x01; /* BMIBA: 20-23h */
443 /* hd_table must contain 4 block drivers */
444 /* NOTE: for the PIIX3, the IRQs and IOports are hardcoded */
445 void pci_piix3_ide_init(PCIBus *bus, BlockDriverState **hd_table, int devfn,
452 /* register a function 1 of PIIX3 */
453 d = (PCIIDEState *)pci_register_device(bus, "PIIX3 IDE",
457 d->type = IDE_TYPE_PIIX3;
459 pci_conf = d->dev.config;
460 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
461 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371SB_1);
462 pci_conf[0x09] = 0x80; // legacy ATA mode
463 pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_IDE);
464 pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
466 qemu_register_reset(piix3_reset, d);
469 pci_register_bar((PCIDevice *)d, 4, 0x10,
470 PCI_ADDRESS_SPACE_IO, bmdma_map);
472 ide_init2(&d->bus[0], hd_table[0], hd_table[1], isa_reserve_irq(14));
473 ide_init2(&d->bus[1], hd_table[2], hd_table[3], isa_reserve_irq(15));
474 ide_init_ioport(&d->bus[0], 0x1f0, 0x3f6);
475 ide_init_ioport(&d->bus[1], 0x170, 0x376);
477 for (i = 0; i < 4; i++)
479 hd_table[i]->private = &d->dev;
481 register_savevm("ide", 0, 3, pci_ide_save, pci_ide_load, d);
484 /* hd_table must contain 4 block drivers */
485 /* NOTE: for the PIIX4, the IRQs and IOports are hardcoded */
486 void pci_piix4_ide_init(PCIBus *bus, BlockDriverState **hd_table, int devfn,
492 /* register a function 1 of PIIX4 */
493 d = (PCIIDEState *)pci_register_device(bus, "PIIX4 IDE",
497 d->type = IDE_TYPE_PIIX4;
499 pci_conf = d->dev.config;
500 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
501 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB);
502 pci_conf[0x09] = 0x80; // legacy ATA mode
503 pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_IDE);
504 pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
506 qemu_register_reset(piix3_reset, d);
509 pci_register_bar((PCIDevice *)d, 4, 0x10,
510 PCI_ADDRESS_SPACE_IO, bmdma_map);
513 * These should call isa_reserve_irq() instead when MIPS supports it
515 ide_init2(&d->bus[0], hd_table[0], hd_table[1], pic[14]);
516 ide_init2(&d->bus[1], hd_table[2], hd_table[3], pic[15]);
517 ide_init_ioport(&d->bus[0], 0x1f0, 0x3f6);
518 ide_init_ioport(&d->bus[1], 0x170, 0x376);
520 register_savevm("ide", 0, 3, pci_ide_save, pci_ide_load, d);