[collab-maint/qemu-kvm.git] / debian / patches / upstream-stable02-fix-CMOS-info-for-drives-defined-with--device.diff
1 commit 6394bd0e05441c363ebb73597c74c951378810e6
2 Author: Markus Armbruster <armbru@redhat.com>
3 Date: Thu Jun 24 19:58:20 2010 +0200
5 pc: Fix CMOS info for drives defined with -device
7 Drives defined with -drive if=ide get get created along with the IDE
8 controller, inside machine->init(). That's before cmos_init().
9 Drives defined with -device get created during generic device init.
10 That's after cmos_init(). Because of that, CMOS has no information on
11 them (type, geometry, translation). Older versions of Windows such as
12 XP reportedly choke on that.
14 Split off the part of CMOS initialization that needs to know about
15 -device devices, and turn it into a reset handler, so it runs after
16 device creation.
18 Signed-off-by: Markus Armbruster <armbru@redhat.com>
19 Signed-off-by: Kevin Wolf <kwolf@redhat.com>
20 (cherry picked from commit c0897e0cb94e83ec1098867b81870e4f51f225b9)
22 diff --git a/hw/ide.h b/hw/ide.h
23 index f0cb320..4ccb580 100644
24 --- a/hw/ide.h
25 +++ b/hw/ide.h
26 @@ -23,4 +23,6 @@ void mmio_ide_init (target_phys_addr_t membase, target_phys_addr_t membase2,
27 qemu_irq irq, int shift,
28 DriveInfo *hd0, DriveInfo *hd1);
30 +void ide_get_bs(BlockDriverState *bs[], BusState *qbus);
31 +
32 #endif /* HW_IDE_H */
33 diff --git a/hw/ide/qdev.c b/hw/ide/qdev.c
34 index 0b84a4f..a621d5e 100644
35 --- a/hw/ide/qdev.c
36 +++ b/hw/ide/qdev.c
37 @@ -90,6 +90,13 @@ IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive)
38 return DO_UPCAST(IDEDevice, qdev, dev);
39 }
41 +void ide_get_bs(BlockDriverState *bs[], BusState *qbus)
42 +{
43 + IDEBus *bus = DO_UPCAST(IDEBus, qbus, qbus);
44 + bs[0] = bus->master ? bus->master->dinfo->bdrv : NULL;
45 + bs[1] = bus->slave ? bus->slave->dinfo->bdrv : NULL;
46 +}
47 +
48 /* --------------------------------- */
50 typedef struct IDEDrive {
51 diff --git a/hw/pc.c b/hw/pc.c
52 index 04ff78d..685b2ff 100644
53 --- a/hw/pc.c
54 +++ b/hw/pc.c
55 @@ -24,6 +24,7 @@
56 #include "hw.h"
57 #include "pc.h"
58 #include "fdc.h"
59 +#include "ide.h"
60 #include "pci.h"
61 #include "vmware_vga.h"
62 #include "usb-uhci.h"
63 @@ -244,15 +245,66 @@ static int pc_boot_set(void *opaque, const char *boot_device)
64 return(0);
65 }
67 -/* hd_table must contain 4 block drivers */
68 +typedef struct pc_cmos_init_late_arg {
69 + BusState *idebus0, *idebus1;
70 +} pc_cmos_init_late_arg;
71 +
72 +static void pc_cmos_init_late(void *opaque)
73 +{
74 + pc_cmos_init_late_arg *arg = opaque;
75 + RTCState *s = rtc_state;
76 + int val;
77 + BlockDriverState *hd_table[4];
78 + int i;
79 +
80 + ide_get_bs(hd_table, arg->idebus0);
81 + ide_get_bs(hd_table + 2, arg->idebus1);
82 +
83 + rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
84 + if (hd_table[0])
85 + cmos_init_hd(0x19, 0x1b, hd_table[0]);
86 + if (hd_table[1])
87 + cmos_init_hd(0x1a, 0x24, hd_table[1]);
88 +
89 + val = 0;
90 + for (i = 0; i < 4; i++) {
91 + if (hd_table[i]) {
92 + int cylinders, heads, sectors, translation;
93 + /* NOTE: bdrv_get_geometry_hint() returns the physical
94 + geometry. It is always such that: 1 <= sects <= 63, 1
95 + <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
96 + geometry can be different if a translation is done. */
97 + translation = bdrv_get_translation_hint(hd_table[i]);
98 + if (translation == BIOS_ATA_TRANSLATION_AUTO) {
99 + bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, §ors);
100 + if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
101 + /* No translation. */
102 + translation = 0;
103 + } else {
104 + /* LBA translation. */
105 + translation = 1;
106 + }
107 + } else {
108 + translation--;
109 + }
110 + val |= translation << (i * 2);
111 + }
112 + }
113 + rtc_set_memory(s, 0x39, val);
114 +
115 + qemu_unregister_reset(pc_cmos_init_late, opaque);
116 +}
117 +
118 static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
119 - const char *boot_device, DriveInfo **hd_table)
120 + const char *boot_device,
121 + BusState *idebus0, BusState *idebus1)
122 {
123 RTCState *s = rtc_state;
124 int nbds, bds[3] = { 0, };
125 int val;
126 int fd0, fd1, nb;
127 int i;
128 + static pc_cmos_init_late_arg arg;
130 /* various important CMOS locations needed by PC/Bochs bios */
132 @@ -335,37 +387,9 @@ static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
134 /* hard drives */
136 - rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
137 - if (hd_table[0])
138 - cmos_init_hd(0x19, 0x1b, hd_table[0]->bdrv);
139 - if (hd_table[1])
140 - cmos_init_hd(0x1a, 0x24, hd_table[1]->bdrv);
141 -
142 - val = 0;
143 - for (i = 0; i < 4; i++) {
144 - if (hd_table[i]) {
145 - int cylinders, heads, sectors, translation;
146 - /* NOTE: bdrv_get_geometry_hint() returns the physical
147 - geometry. It is always such that: 1 <= sects <= 63, 1
148 - <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
149 - geometry can be different if a translation is done. */
150 - translation = bdrv_get_translation_hint(hd_table[i]->bdrv);
151 - if (translation == BIOS_ATA_TRANSLATION_AUTO) {
152 - bdrv_get_geometry_hint(hd_table[i]->bdrv, &cylinders, &heads, §ors);
153 - if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
154 - /* No translation. */
155 - translation = 0;
156 - } else {
157 - /* LBA translation. */
158 - translation = 1;
159 - }
160 - } else {
161 - translation--;
162 - }
163 - val |= translation << (i * 2);
164 - }
165 - }
166 - rtc_set_memory(s, 0x39, val);
167 + arg.idebus0 = idebus0;
168 + arg.idebus1 = idebus1;
169 + qemu_register_reset(pc_cmos_init_late, &arg);
170 }
172 void ioport_set_a20(int enable)
173 @@ -994,6 +1018,7 @@ static void pc_init1(ram_addr_t ram_size,
174 qemu_irq *i8259;
175 IsaIrqState *isa_irq_state;
176 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
177 + BusState *idebus[MAX_IDE_BUS];
178 DriveInfo *fd[MAX_FD];
179 void *fw_cfg;
181 @@ -1187,11 +1212,16 @@ static void pc_init1(ram_addr_t ram_size,
182 }
184 if (pci_enabled) {
185 - pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
186 + PCIDevice *dev;
187 + dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
188 + idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");
189 + idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");
190 } else {
191 for(i = 0; i < MAX_IDE_BUS; i++) {
192 - isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
193 - hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
194 + ISADevice *dev;
195 + dev = isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
196 + hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
197 + idebus[i] = qdev_get_child_bus(&dev->qdev, "ide.0");
198 }
199 }
201 @@ -1206,7 +1236,8 @@ static void pc_init1(ram_addr_t ram_size,
202 }
203 floppy_controller = fdctrl_init_isa(fd);
205 - cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd);
206 + cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
207 + idebus[0], idebus[1]);
209 if (pci_enabled && usb_enabled) {
210 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
