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
29 #include "block_int.h"
33 #include <hw/ide/internal.h>
35 /***********************************************************/
36 /* PCI IDE definitions */
40 #define MRDMODE_INTR_CH0 0x04
41 #define MRDMODE_INTR_CH1 0x08
42 #define MRDMODE_BLK_CH0 0x10
43 #define MRDMODE_BLK_CH1 0x20
44 #define UDIDETCR0 0x73
45 #define UDIDETCR1 0x7B
47 #define IDE_TYPE_PIIX3 0
48 #define IDE_TYPE_CMD646 1
49 #define IDE_TYPE_PIIX4 2
51 typedef struct PCIIDEState
{
55 int type
; /* see IDE_TYPE_xxx */
58 static void cmd646_update_irq(PCIIDEState
*d
);
60 static void ide_map(PCIDevice
*pci_dev
, int region_num
,
61 uint32_t addr
, uint32_t size
, int type
)
63 PCIIDEState
*d
= (PCIIDEState
*)pci_dev
;
66 if (region_num
<= 3) {
67 bus
= &d
->bus
[(region_num
>> 1)];
69 register_ioport_read(addr
+ 2, 1, 1, ide_status_read
, bus
);
70 register_ioport_write(addr
+ 2, 1, 1, ide_cmd_write
, bus
);
72 register_ioport_write(addr
, 8, 1, ide_ioport_write
, bus
);
73 register_ioport_read(addr
, 8, 1, ide_ioport_read
, bus
);
76 register_ioport_write(addr
, 2, 2, ide_data_writew
, bus
);
77 register_ioport_read(addr
, 2, 2, ide_data_readw
, bus
);
78 register_ioport_write(addr
, 4, 4, ide_data_writel
, bus
);
79 register_ioport_read(addr
, 4, 4, ide_data_readl
, bus
);
84 static void bmdma_cmd_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
86 BMDMAState
*bm
= opaque
;
88 printf("%s: 0x%08x\n", __func__
, val
);
90 if (!(val
& BM_CMD_START
)) {
91 /* XXX: do it better */
95 if (!(bm
->status
& BM_STATUS_DMAING
)) {
96 bm
->status
|= BM_STATUS_DMAING
;
97 /* start dma transfer if possible */
101 bm
->cmd
= val
& 0x09;
105 static uint32_t bmdma_readb(void *opaque
, uint32_t addr
)
107 BMDMAState
*bm
= opaque
;
108 PCIIDEState
*pci_dev
;
116 pci_dev
= bm
->pci_dev
;
117 if (pci_dev
->type
== IDE_TYPE_CMD646
) {
118 val
= pci_dev
->dev
.config
[MRDMODE
];
127 pci_dev
= bm
->pci_dev
;
128 if (pci_dev
->type
== IDE_TYPE_CMD646
) {
129 if (bm
== &pci_dev
->bmdma
[0])
130 val
= pci_dev
->dev
.config
[UDIDETCR0
];
132 val
= pci_dev
->dev
.config
[UDIDETCR1
];
142 printf("bmdma: readb 0x%02x : 0x%02x\n", addr
, val
);
147 static void bmdma_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
149 BMDMAState
*bm
= opaque
;
150 PCIIDEState
*pci_dev
;
152 printf("bmdma: writeb 0x%02x : 0x%02x\n", addr
, val
);
156 pci_dev
= bm
->pci_dev
;
157 if (pci_dev
->type
== IDE_TYPE_CMD646
) {
158 pci_dev
->dev
.config
[MRDMODE
] =
159 (pci_dev
->dev
.config
[MRDMODE
] & ~0x30) | (val
& 0x30);
160 cmd646_update_irq(pci_dev
);
164 bm
->status
= (val
& 0x60) | (bm
->status
& 1) | (bm
->status
& ~val
& 0x06);
167 pci_dev
= bm
->pci_dev
;
168 if (pci_dev
->type
== IDE_TYPE_CMD646
) {
169 if (bm
== &pci_dev
->bmdma
[0])
170 pci_dev
->dev
.config
[UDIDETCR0
] = val
;
172 pci_dev
->dev
.config
[UDIDETCR1
] = val
;
178 static uint32_t bmdma_addr_readb(void *opaque
, uint32_t addr
)
180 BMDMAState
*bm
= opaque
;
182 val
= (bm
->addr
>> ((addr
& 3) * 8)) & 0xff;
184 printf("%s: 0x%08x\n", __func__
, val
);
189 static void bmdma_addr_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
191 BMDMAState
*bm
= opaque
;
192 int shift
= (addr
& 3) * 8;
194 printf("%s: 0x%08x\n", __func__
, val
);
196 bm
->addr
&= ~(0xFF << shift
);
197 bm
->addr
|= ((val
& 0xFF) << shift
) & ~3;
198 bm
->cur_addr
= bm
->addr
;
201 static uint32_t bmdma_addr_readw(void *opaque
, uint32_t addr
)
203 BMDMAState
*bm
= opaque
;
205 val
= (bm
->addr
>> ((addr
& 3) * 8)) & 0xffff;
207 printf("%s: 0x%08x\n", __func__
, val
);
212 static void bmdma_addr_writew(void *opaque
, uint32_t addr
, uint32_t val
)
214 BMDMAState
*bm
= opaque
;
215 int shift
= (addr
& 3) * 8;
217 printf("%s: 0x%08x\n", __func__
, val
);
219 bm
->addr
&= ~(0xFFFF << shift
);
220 bm
->addr
|= ((val
& 0xFFFF) << shift
) & ~3;
221 bm
->cur_addr
= bm
->addr
;
224 static uint32_t bmdma_addr_readl(void *opaque
, uint32_t addr
)
226 BMDMAState
*bm
= opaque
;
230 printf("%s: 0x%08x\n", __func__
, val
);
235 static void bmdma_addr_writel(void *opaque
, uint32_t addr
, uint32_t val
)
237 BMDMAState
*bm
= opaque
;
239 printf("%s: 0x%08x\n", __func__
, val
);
242 bm
->cur_addr
= bm
->addr
;
245 static void bmdma_map(PCIDevice
*pci_dev
, int region_num
,
246 uint32_t addr
, uint32_t size
, int type
)
248 PCIIDEState
*d
= (PCIIDEState
*)pci_dev
;
251 for(i
= 0;i
< 2; i
++) {
252 BMDMAState
*bm
= &d
->bmdma
[i
];
253 d
->bus
[i
].bmdma
= bm
;
254 bm
->pci_dev
= DO_UPCAST(PCIIDEState
, dev
, pci_dev
);
256 qemu_add_vm_change_state_handler(ide_dma_restart_cb
, bm
);
258 register_ioport_write(addr
, 1, 1, bmdma_cmd_writeb
, bm
);
260 register_ioport_write(addr
+ 1, 3, 1, bmdma_writeb
, bm
);
261 register_ioport_read(addr
, 4, 1, bmdma_readb
, bm
);
263 register_ioport_write(addr
+ 4, 4, 1, bmdma_addr_writeb
, bm
);
264 register_ioport_read(addr
+ 4, 4, 1, bmdma_addr_readb
, bm
);
265 register_ioport_write(addr
+ 4, 4, 2, bmdma_addr_writew
, bm
);
266 register_ioport_read(addr
+ 4, 4, 2, bmdma_addr_readw
, bm
);
267 register_ioport_write(addr
+ 4, 4, 4, bmdma_addr_writel
, bm
);
268 register_ioport_read(addr
+ 4, 4, 4, bmdma_addr_readl
, bm
);
273 static void pci_ide_save(QEMUFile
* f
, void *opaque
)
275 PCIIDEState
*d
= opaque
;
278 pci_device_save(&d
->dev
, f
);
280 for(i
= 0; i
< 2; i
++) {
281 BMDMAState
*bm
= &d
->bmdma
[i
];
283 qemu_put_8s(f
, &bm
->cmd
);
284 qemu_put_8s(f
, &bm
->status
);
285 qemu_put_be32s(f
, &bm
->addr
);
286 qemu_put_sbe64s(f
, &bm
->sector_num
);
287 qemu_put_be32s(f
, &bm
->nsector
);
288 ifidx
= bm
->unit
+ 2*i
;
289 qemu_put_8s(f
, &ifidx
);
290 /* XXX: if a transfer is pending, we do not save it yet */
293 /* per IDE interface data */
294 for(i
= 0; i
< 2; i
++) {
295 idebus_save(f
, &d
->bus
[i
]);
298 /* per IDE drive data */
299 for(i
= 0; i
< 2; i
++) {
300 ide_save(f
, &d
->bus
[i
].ifs
[0]);
301 ide_save(f
, &d
->bus
[i
].ifs
[1]);
305 static int pci_ide_load(QEMUFile
* f
, void *opaque
, int version_id
)
307 PCIIDEState
*d
= opaque
;
310 if (version_id
!= 2 && version_id
!= 3)
312 ret
= pci_device_load(&d
->dev
, f
);
316 for(i
= 0; i
< 2; i
++) {
317 BMDMAState
*bm
= &d
->bmdma
[i
];
319 qemu_get_8s(f
, &bm
->cmd
);
320 qemu_get_8s(f
, &bm
->status
);
321 qemu_get_be32s(f
, &bm
->addr
);
322 qemu_get_sbe64s(f
, &bm
->sector_num
);
323 qemu_get_be32s(f
, &bm
->nsector
);
324 qemu_get_8s(f
, &ifidx
);
325 bm
->unit
= ifidx
& 1;
326 /* XXX: if a transfer is pending, we do not save it yet */
329 /* per IDE interface data */
330 for(i
= 0; i
< 2; i
++) {
331 idebus_load(f
, &d
->bus
[i
], version_id
);
334 /* per IDE drive data */
335 for(i
= 0; i
< 2; i
++) {
336 ide_load(f
, &d
->bus
[i
].ifs
[0], version_id
);
337 ide_load(f
, &d
->bus
[i
].ifs
[1], version_id
);
342 /* XXX: call it also when the MRDMODE is changed from the PCI config
344 static void cmd646_update_irq(PCIIDEState
*d
)
347 pci_level
= ((d
->dev
.config
[MRDMODE
] & MRDMODE_INTR_CH0
) &&
348 !(d
->dev
.config
[MRDMODE
] & MRDMODE_BLK_CH0
)) ||
349 ((d
->dev
.config
[MRDMODE
] & MRDMODE_INTR_CH1
) &&
350 !(d
->dev
.config
[MRDMODE
] & MRDMODE_BLK_CH1
));
351 qemu_set_irq(d
->dev
.irq
[0], pci_level
);
354 /* the PCI irq level is the logical OR of the two channels */
355 static void cmd646_set_irq(void *opaque
, int channel
, int level
)
357 PCIIDEState
*d
= opaque
;
360 irq_mask
= MRDMODE_INTR_CH0
<< channel
;
362 d
->dev
.config
[MRDMODE
] |= irq_mask
;
364 d
->dev
.config
[MRDMODE
] &= ~irq_mask
;
365 cmd646_update_irq(d
);
368 static void cmd646_reset(void *opaque
)
370 PCIIDEState
*d
= opaque
;
373 for (i
= 0; i
< 2; i
++)
374 ide_dma_cancel(&d
->bmdma
[i
]);
377 /* CMD646 PCI IDE controller */
378 void pci_cmd646_ide_init(PCIBus
*bus
, DriveInfo
**hd_table
,
379 int secondary_ide_enabled
)
385 d
= (PCIIDEState
*)pci_register_device(bus
, "CMD646 IDE",
389 d
->type
= IDE_TYPE_CMD646
;
390 pci_conf
= d
->dev
.config
;
391 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_CMD
);
392 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_CMD_646
);
394 pci_conf
[0x08] = 0x07; // IDE controller revision
395 pci_conf
[0x09] = 0x8f;
397 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_IDE
);
398 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
400 pci_conf
[0x51] = 0x04; // enable IDE0
401 if (secondary_ide_enabled
) {
402 /* XXX: if not enabled, really disable the seconday IDE controller */
403 pci_conf
[0x51] |= 0x08; /* enable IDE1 */
406 pci_register_bar((PCIDevice
*)d
, 0, 0x8,
407 PCI_ADDRESS_SPACE_IO
, ide_map
);
408 pci_register_bar((PCIDevice
*)d
, 1, 0x4,
409 PCI_ADDRESS_SPACE_IO
, ide_map
);
410 pci_register_bar((PCIDevice
*)d
, 2, 0x8,
411 PCI_ADDRESS_SPACE_IO
, ide_map
);
412 pci_register_bar((PCIDevice
*)d
, 3, 0x4,
413 PCI_ADDRESS_SPACE_IO
, ide_map
);
414 pci_register_bar((PCIDevice
*)d
, 4, 0x10,
415 PCI_ADDRESS_SPACE_IO
, bmdma_map
);
417 pci_conf
[0x3d] = 0x01; // interrupt on pin 1
419 irq
= qemu_allocate_irqs(cmd646_set_irq
, d
, 2);
420 ide_init2(&d
->bus
[0], hd_table
[0], hd_table
[1], irq
[0]);
421 ide_init2(&d
->bus
[1], hd_table
[2], hd_table
[3], irq
[1]);
423 register_savevm("ide", 0, 3, pci_ide_save
, pci_ide_load
, d
);
424 qemu_register_reset(cmd646_reset
, d
);
428 static void piix3_reset(void *opaque
)
430 PCIIDEState
*d
= opaque
;
431 uint8_t *pci_conf
= d
->dev
.config
;
434 for (i
= 0; i
< 2; i
++)
435 ide_dma_cancel(&d
->bmdma
[i
]);
437 pci_conf
[0x04] = 0x00;
438 pci_conf
[0x05] = 0x00;
439 pci_conf
[0x06] = 0x80; /* FBC */
440 pci_conf
[0x07] = 0x02; // PCI_status_devsel_medium
441 pci_conf
[0x20] = 0x01; /* BMIBA: 20-23h */
444 /* hd_table must contain 4 block drivers */
445 /* NOTE: for the PIIX3, the IRQs and IOports are hardcoded */
446 void pci_piix3_ide_init(PCIBus
*bus
, DriveInfo
**hd_table
, int devfn
)
451 /* register a function 1 of PIIX3 */
452 d
= (PCIIDEState
*)pci_register_device(bus
, "PIIX3 IDE",
456 d
->type
= IDE_TYPE_PIIX3
;
458 pci_conf
= d
->dev
.config
;
459 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
460 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371SB_1
);
461 pci_conf
[0x09] = 0x80; // legacy ATA mode
462 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_IDE
);
463 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
465 qemu_register_reset(piix3_reset
, d
);
468 pci_register_bar((PCIDevice
*)d
, 4, 0x10,
469 PCI_ADDRESS_SPACE_IO
, bmdma_map
);
471 ide_init2(&d
->bus
[0], hd_table
[0], hd_table
[1], isa_reserve_irq(14));
472 ide_init2(&d
->bus
[1], hd_table
[2], hd_table
[3], isa_reserve_irq(15));
473 ide_init_ioport(&d
->bus
[0], 0x1f0, 0x3f6);
474 ide_init_ioport(&d
->bus
[1], 0x170, 0x376);
476 register_savevm("ide", 0, 3, pci_ide_save
, pci_ide_load
, d
);
479 /* hd_table must contain 4 block drivers */
480 /* NOTE: for the PIIX4, the IRQs and IOports are hardcoded */
481 void pci_piix4_ide_init(PCIBus
*bus
, DriveInfo
**hd_table
, int devfn
)
486 /* register a function 1 of PIIX4 */
487 d
= (PCIIDEState
*)pci_register_device(bus
, "PIIX4 IDE",
491 d
->type
= IDE_TYPE_PIIX4
;
493 pci_conf
= d
->dev
.config
;
494 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
495 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371AB
);
496 pci_conf
[0x09] = 0x80; // legacy ATA mode
497 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_IDE
);
498 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
500 qemu_register_reset(piix3_reset
, d
);
503 pci_register_bar((PCIDevice
*)d
, 4, 0x10,
504 PCI_ADDRESS_SPACE_IO
, bmdma_map
);
506 ide_init2(&d
->bus
[0], hd_table
[0], hd_table
[1], isa_reserve_irq(14));
507 ide_init2(&d
->bus
[1], hd_table
[2], hd_table
[3], isa_reserve_irq(15));
508 ide_init_ioport(&d
->bus
[0], 0x1f0, 0x3f6);
509 ide_init_ioport(&d
->bus
[1], 0x170, 0x376);
511 register_savevm("ide", 0, 3, pci_ide_save
, pci_ide_load
, d
);