Statistics
| Branch: | Revision:

root / hw / pc.c @ 29002d9e

History | View | Annotate | Download (31.5 kB)

1 80cabfad bellard
/*
2 80cabfad bellard
 * QEMU PC System Emulator
3 5fafdf24 ths
 *
4 80cabfad bellard
 * Copyright (c) 2003-2004 Fabrice Bellard
5 5fafdf24 ths
 *
6 80cabfad bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 80cabfad bellard
 * of this software and associated documentation files (the "Software"), to deal
8 80cabfad bellard
 * in the Software without restriction, including without limitation the rights
9 80cabfad bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 80cabfad bellard
 * copies of the Software, and to permit persons to whom the Software is
11 80cabfad bellard
 * furnished to do so, subject to the following conditions:
12 80cabfad bellard
 *
13 80cabfad bellard
 * The above copyright notice and this permission notice shall be included in
14 80cabfad bellard
 * all copies or substantial portions of the Software.
15 80cabfad bellard
 *
16 80cabfad bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 80cabfad bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 80cabfad bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 80cabfad bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 80cabfad bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 80cabfad bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 80cabfad bellard
 * THE SOFTWARE.
23 80cabfad bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "pc.h"
26 488cb996 Gerd Hoffmann
#include "serial.h"
27 aa28b9bf Blue Swirl
#include "apic.h"
28 87ecb68b pbrook
#include "fdc.h"
29 c0897e0c Markus Armbruster
#include "ide.h"
30 87ecb68b pbrook
#include "pci.h"
31 376253ec aliguori
#include "monitor.h"
32 3cce6243 blueswir1
#include "fw_cfg.h"
33 16b29ae1 aliguori
#include "hpet_emul.h"
34 b6f6e3d3 aliguori
#include "smbios.h"
35 ca20cf32 Blue Swirl
#include "loader.h"
36 ca20cf32 Blue Swirl
#include "elf.h"
37 52001445 Adam Lackorzynski
#include "multiboot.h"
38 1d914fa0 Isaku Yamahata
#include "mc146818rtc.h"
39 b1277b03 Jan Kiszka
#include "i8254.h"
40 302fe51b Jan Kiszka
#include "pcspk.h"
41 60ba3cc2 Jan Kiszka
#include "msi.h"
42 822557eb Jan Kiszka
#include "sysbus.h"
43 666daa68 Markus Armbruster
#include "sysemu.h"
44 9b5b76d4 Jan Kiszka
#include "kvm.h"
45 1d31f66b Peter Maydell
#include "kvm_i386.h"
46 9468e9c4 Wei Liu
#include "xen.h"
47 2446333c Blue Swirl
#include "blockdev.h"
48 2b584959 Markus Armbruster
#include "hw/block-common.h"
49 a19cbfb3 Gerd Hoffmann
#include "ui/qemu-spice.h"
50 00cb2a99 Avi Kivity
#include "memory.h"
51 be20f9e9 Avi Kivity
#include "exec-memory.h"
52 c2d8d311 Stefano Stabellini
#include "arch_init.h"
53 ee785fed Chegu Vinod
#include "bitmap.h"
54 80cabfad bellard
55 471fd342 Blue Swirl
/* debug PC/ISA interrupts */
56 471fd342 Blue Swirl
//#define DEBUG_IRQ
57 471fd342 Blue Swirl
58 471fd342 Blue Swirl
#ifdef DEBUG_IRQ
59 471fd342 Blue Swirl
#define DPRINTF(fmt, ...)                                       \
60 471fd342 Blue Swirl
    do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
61 471fd342 Blue Swirl
#else
62 471fd342 Blue Swirl
#define DPRINTF(fmt, ...)
63 471fd342 Blue Swirl
#endif
64 471fd342 Blue Swirl
65 a80274c3 pbrook
/* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables.  */
66 a80274c3 pbrook
#define ACPI_DATA_SIZE       0x10000
67 3cce6243 blueswir1
#define BIOS_CFG_IOPORT 0x510
68 8a92ea2f aliguori
#define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
69 b6f6e3d3 aliguori
#define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
70 6b35e7bf Jes Sorensen
#define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
71 4c5b10b7 Jes Sorensen
#define FW_CFG_E820_TABLE (FW_CFG_ARCH_LOCAL + 3)
72 40ac17cd Gleb Natapov
#define FW_CFG_HPET (FW_CFG_ARCH_LOCAL + 4)
73 80cabfad bellard
74 4c5b10b7 Jes Sorensen
#define E820_NR_ENTRIES                16
75 4c5b10b7 Jes Sorensen
76 4c5b10b7 Jes Sorensen
struct e820_entry {
77 4c5b10b7 Jes Sorensen
    uint64_t address;
78 4c5b10b7 Jes Sorensen
    uint64_t length;
79 4c5b10b7 Jes Sorensen
    uint32_t type;
80 541dc0d4 Stefan Weil
} QEMU_PACKED __attribute((__aligned__(4)));
81 4c5b10b7 Jes Sorensen
82 4c5b10b7 Jes Sorensen
struct e820_table {
83 4c5b10b7 Jes Sorensen
    uint32_t count;
84 4c5b10b7 Jes Sorensen
    struct e820_entry entry[E820_NR_ENTRIES];
85 541dc0d4 Stefan Weil
} QEMU_PACKED __attribute((__aligned__(4)));
86 4c5b10b7 Jes Sorensen
87 4c5b10b7 Jes Sorensen
static struct e820_table e820_table;
88 dd703b99 Blue Swirl
struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
89 4c5b10b7 Jes Sorensen
90 b881fbe9 Jan Kiszka
void gsi_handler(void *opaque, int n, int level)
91 1452411b Avi Kivity
{
92 b881fbe9 Jan Kiszka
    GSIState *s = opaque;
93 1452411b Avi Kivity
94 b881fbe9 Jan Kiszka
    DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
95 b881fbe9 Jan Kiszka
    if (n < ISA_NUM_IRQS) {
96 b881fbe9 Jan Kiszka
        qemu_set_irq(s->i8259_irq[n], level);
97 1632dc6a Avi Kivity
    }
98 b881fbe9 Jan Kiszka
    qemu_set_irq(s->ioapic_irq[n], level);
99 2e9947d2 Jan Kiszka
}
100 1452411b Avi Kivity
101 258711c6 Julien Grall
static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
102 258711c6 Julien Grall
                           unsigned size)
103 80cabfad bellard
{
104 80cabfad bellard
}
105 80cabfad bellard
106 f929aad6 bellard
/* MSDOS compatibility mode FPU exception support */
107 d537cf6c pbrook
static qemu_irq ferr_irq;
108 8e78eb28 Isaku Yamahata
109 8e78eb28 Isaku Yamahata
void pc_register_ferr_irq(qemu_irq irq)
110 8e78eb28 Isaku Yamahata
{
111 8e78eb28 Isaku Yamahata
    ferr_irq = irq;
112 8e78eb28 Isaku Yamahata
}
113 8e78eb28 Isaku Yamahata
114 f929aad6 bellard
/* XXX: add IGNNE support */
115 f929aad6 bellard
void cpu_set_ferr(CPUX86State *s)
116 f929aad6 bellard
{
117 d537cf6c pbrook
    qemu_irq_raise(ferr_irq);
118 f929aad6 bellard
}
119 f929aad6 bellard
120 258711c6 Julien Grall
static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
121 258711c6 Julien Grall
                           unsigned size)
122 f929aad6 bellard
{
123 d537cf6c pbrook
    qemu_irq_lower(ferr_irq);
124 f929aad6 bellard
}
125 f929aad6 bellard
126 28ab0e2e bellard
/* TSC handling */
127 28ab0e2e bellard
uint64_t cpu_get_tsc(CPUX86State *env)
128 28ab0e2e bellard
{
129 4a1418e0 Anthony Liguori
    return cpu_get_ticks();
130 28ab0e2e bellard
}
131 28ab0e2e bellard
132 a5954d5c bellard
/* SMM support */
133 f885f1ea Isaku Yamahata
134 f885f1ea Isaku Yamahata
static cpu_set_smm_t smm_set;
135 f885f1ea Isaku Yamahata
static void *smm_arg;
136 f885f1ea Isaku Yamahata
137 f885f1ea Isaku Yamahata
void cpu_smm_register(cpu_set_smm_t callback, void *arg)
138 f885f1ea Isaku Yamahata
{
139 f885f1ea Isaku Yamahata
    assert(smm_set == NULL);
140 f885f1ea Isaku Yamahata
    assert(smm_arg == NULL);
141 f885f1ea Isaku Yamahata
    smm_set = callback;
142 f885f1ea Isaku Yamahata
    smm_arg = arg;
143 f885f1ea Isaku Yamahata
}
144 f885f1ea Isaku Yamahata
145 4a8fa5dc Andreas Färber
void cpu_smm_update(CPUX86State *env)
146 a5954d5c bellard
{
147 f885f1ea Isaku Yamahata
    if (smm_set && smm_arg && env == first_cpu)
148 f885f1ea Isaku Yamahata
        smm_set(!!(env->hflags & HF_SMM_MASK), smm_arg);
149 a5954d5c bellard
}
150 a5954d5c bellard
151 a5954d5c bellard
152 3de388f6 bellard
/* IRQ handling */
153 4a8fa5dc Andreas Färber
int cpu_get_pic_interrupt(CPUX86State *env)
154 3de388f6 bellard
{
155 3de388f6 bellard
    int intno;
156 3de388f6 bellard
157 cf6d64bf Blue Swirl
    intno = apic_get_interrupt(env->apic_state);
158 3de388f6 bellard
    if (intno >= 0) {
159 3de388f6 bellard
        return intno;
160 3de388f6 bellard
    }
161 3de388f6 bellard
    /* read the irq from the PIC */
162 cf6d64bf Blue Swirl
    if (!apic_accept_pic_intr(env->apic_state)) {
163 0e21e12b ths
        return -1;
164 cf6d64bf Blue Swirl
    }
165 0e21e12b ths
166 3de388f6 bellard
    intno = pic_read_irq(isa_pic);
167 3de388f6 bellard
    return intno;
168 3de388f6 bellard
}
169 3de388f6 bellard
170 d537cf6c pbrook
static void pic_irq_request(void *opaque, int irq, int level)
171 3de388f6 bellard
{
172 4a8fa5dc Andreas Färber
    CPUX86State *env = first_cpu;
173 a5b38b51 aurel32
174 471fd342 Blue Swirl
    DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
175 d5529471 aurel32
    if (env->apic_state) {
176 d5529471 aurel32
        while (env) {
177 cf6d64bf Blue Swirl
            if (apic_accept_pic_intr(env->apic_state)) {
178 cf6d64bf Blue Swirl
                apic_deliver_pic_intr(env->apic_state, level);
179 cf6d64bf Blue Swirl
            }
180 d5529471 aurel32
            env = env->next_cpu;
181 d5529471 aurel32
        }
182 d5529471 aurel32
    } else {
183 b614106a aurel32
        if (level)
184 b614106a aurel32
            cpu_interrupt(env, CPU_INTERRUPT_HARD);
185 b614106a aurel32
        else
186 b614106a aurel32
            cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
187 a5b38b51 aurel32
    }
188 3de388f6 bellard
}
189 3de388f6 bellard
190 b0a21b53 bellard
/* PC cmos mappings */
191 b0a21b53 bellard
192 80cabfad bellard
#define REG_EQUIPMENT_BYTE          0x14
193 80cabfad bellard
194 d288c7ba Blue Swirl
static int cmos_get_fd_drive_type(FDriveType fd0)
195 777428f2 bellard
{
196 777428f2 bellard
    int val;
197 777428f2 bellard
198 777428f2 bellard
    switch (fd0) {
199 d288c7ba Blue Swirl
    case FDRIVE_DRV_144:
200 777428f2 bellard
        /* 1.44 Mb 3"5 drive */
201 777428f2 bellard
        val = 4;
202 777428f2 bellard
        break;
203 d288c7ba Blue Swirl
    case FDRIVE_DRV_288:
204 777428f2 bellard
        /* 2.88 Mb 3"5 drive */
205 777428f2 bellard
        val = 5;
206 777428f2 bellard
        break;
207 d288c7ba Blue Swirl
    case FDRIVE_DRV_120:
208 777428f2 bellard
        /* 1.2 Mb 5"5 drive */
209 777428f2 bellard
        val = 2;
210 777428f2 bellard
        break;
211 d288c7ba Blue Swirl
    case FDRIVE_DRV_NONE:
212 777428f2 bellard
    default:
213 777428f2 bellard
        val = 0;
214 777428f2 bellard
        break;
215 777428f2 bellard
    }
216 777428f2 bellard
    return val;
217 777428f2 bellard
}
218 777428f2 bellard
219 9139046c Markus Armbruster
static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
220 9139046c Markus Armbruster
                         int16_t cylinders, int8_t heads, int8_t sectors)
221 ba6c2377 bellard
{
222 ba6c2377 bellard
    rtc_set_memory(s, type_ofs, 47);
223 ba6c2377 bellard
    rtc_set_memory(s, info_ofs, cylinders);
224 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
225 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 2, heads);
226 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 3, 0xff);
227 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 4, 0xff);
228 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
229 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 6, cylinders);
230 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
231 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 8, sectors);
232 ba6c2377 bellard
}
233 ba6c2377 bellard
234 6ac0e82d balrog
/* convert boot_device letter to something recognizable by the bios */
235 6ac0e82d balrog
static int boot_device2nibble(char boot_device)
236 6ac0e82d balrog
{
237 6ac0e82d balrog
    switch(boot_device) {
238 6ac0e82d balrog
    case 'a':
239 6ac0e82d balrog
    case 'b':
240 6ac0e82d balrog
        return 0x01; /* floppy boot */
241 6ac0e82d balrog
    case 'c':
242 6ac0e82d balrog
        return 0x02; /* hard drive boot */
243 6ac0e82d balrog
    case 'd':
244 6ac0e82d balrog
        return 0x03; /* CD-ROM boot */
245 6ac0e82d balrog
    case 'n':
246 6ac0e82d balrog
        return 0x04; /* Network boot */
247 6ac0e82d balrog
    }
248 6ac0e82d balrog
    return 0;
249 6ac0e82d balrog
}
250 6ac0e82d balrog
251 1d914fa0 Isaku Yamahata
static int set_boot_dev(ISADevice *s, const char *boot_device, int fd_bootchk)
252 0ecdffbb aurel32
{
253 0ecdffbb aurel32
#define PC_MAX_BOOT_DEVICES 3
254 0ecdffbb aurel32
    int nbds, bds[3] = { 0, };
255 0ecdffbb aurel32
    int i;
256 0ecdffbb aurel32
257 0ecdffbb aurel32
    nbds = strlen(boot_device);
258 0ecdffbb aurel32
    if (nbds > PC_MAX_BOOT_DEVICES) {
259 1ecda02b Markus Armbruster
        error_report("Too many boot devices for PC");
260 0ecdffbb aurel32
        return(1);
261 0ecdffbb aurel32
    }
262 0ecdffbb aurel32
    for (i = 0; i < nbds; i++) {
263 0ecdffbb aurel32
        bds[i] = boot_device2nibble(boot_device[i]);
264 0ecdffbb aurel32
        if (bds[i] == 0) {
265 1ecda02b Markus Armbruster
            error_report("Invalid boot device for PC: '%c'",
266 1ecda02b Markus Armbruster
                         boot_device[i]);
267 0ecdffbb aurel32
            return(1);
268 0ecdffbb aurel32
        }
269 0ecdffbb aurel32
    }
270 0ecdffbb aurel32
    rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
271 d9346e81 Markus Armbruster
    rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
272 0ecdffbb aurel32
    return(0);
273 0ecdffbb aurel32
}
274 0ecdffbb aurel32
275 d9346e81 Markus Armbruster
static int pc_boot_set(void *opaque, const char *boot_device)
276 d9346e81 Markus Armbruster
{
277 d9346e81 Markus Armbruster
    return set_boot_dev(opaque, boot_device, 0);
278 d9346e81 Markus Armbruster
}
279 d9346e81 Markus Armbruster
280 c0897e0c Markus Armbruster
typedef struct pc_cmos_init_late_arg {
281 c0897e0c Markus Armbruster
    ISADevice *rtc_state;
282 9139046c Markus Armbruster
    BusState *idebus[2];
283 c0897e0c Markus Armbruster
} pc_cmos_init_late_arg;
284 c0897e0c Markus Armbruster
285 c0897e0c Markus Armbruster
static void pc_cmos_init_late(void *opaque)
286 c0897e0c Markus Armbruster
{
287 c0897e0c Markus Armbruster
    pc_cmos_init_late_arg *arg = opaque;
288 c0897e0c Markus Armbruster
    ISADevice *s = arg->rtc_state;
289 9139046c Markus Armbruster
    int16_t cylinders;
290 9139046c Markus Armbruster
    int8_t heads, sectors;
291 c0897e0c Markus Armbruster
    int val;
292 2adc99b2 Markus Armbruster
    int i, trans;
293 c0897e0c Markus Armbruster
294 9139046c Markus Armbruster
    val = 0;
295 9139046c Markus Armbruster
    if (ide_get_geometry(arg->idebus[0], 0,
296 9139046c Markus Armbruster
                         &cylinders, &heads, &sectors) >= 0) {
297 9139046c Markus Armbruster
        cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
298 9139046c Markus Armbruster
        val |= 0xf0;
299 9139046c Markus Armbruster
    }
300 9139046c Markus Armbruster
    if (ide_get_geometry(arg->idebus[0], 1,
301 9139046c Markus Armbruster
                         &cylinders, &heads, &sectors) >= 0) {
302 9139046c Markus Armbruster
        cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
303 9139046c Markus Armbruster
        val |= 0x0f;
304 9139046c Markus Armbruster
    }
305 9139046c Markus Armbruster
    rtc_set_memory(s, 0x12, val);
306 c0897e0c Markus Armbruster
307 c0897e0c Markus Armbruster
    val = 0;
308 c0897e0c Markus Armbruster
    for (i = 0; i < 4; i++) {
309 9139046c Markus Armbruster
        /* NOTE: ide_get_geometry() returns the physical
310 9139046c Markus Armbruster
           geometry.  It is always such that: 1 <= sects <= 63, 1
311 9139046c Markus Armbruster
           <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
312 9139046c Markus Armbruster
           geometry can be different if a translation is done. */
313 9139046c Markus Armbruster
        if (ide_get_geometry(arg->idebus[i / 2], i % 2,
314 9139046c Markus Armbruster
                             &cylinders, &heads, &sectors) >= 0) {
315 2adc99b2 Markus Armbruster
            trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
316 2adc99b2 Markus Armbruster
            assert((trans & ~3) == 0);
317 2adc99b2 Markus Armbruster
            val |= trans << (i * 2);
318 c0897e0c Markus Armbruster
        }
319 c0897e0c Markus Armbruster
    }
320 c0897e0c Markus Armbruster
    rtc_set_memory(s, 0x39, val);
321 c0897e0c Markus Armbruster
322 c0897e0c Markus Armbruster
    qemu_unregister_reset(pc_cmos_init_late, opaque);
323 c0897e0c Markus Armbruster
}
324 c0897e0c Markus Armbruster
325 845773ab Isaku Yamahata
void pc_cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
326 c0897e0c Markus Armbruster
                  const char *boot_device,
327 34d4260e Kevin Wolf
                  ISADevice *floppy, BusState *idebus0, BusState *idebus1,
328 63ffb564 Blue Swirl
                  ISADevice *s)
329 80cabfad bellard
{
330 61a8d649 Markus Armbruster
    int val, nb, i;
331 980bda8b Peter Maydell
    FDriveType fd_type[2] = { FDRIVE_DRV_NONE, FDRIVE_DRV_NONE };
332 c0897e0c Markus Armbruster
    static pc_cmos_init_late_arg arg;
333 b0a21b53 bellard
334 b0a21b53 bellard
    /* various important CMOS locations needed by PC/Bochs bios */
335 80cabfad bellard
336 80cabfad bellard
    /* memory size */
337 e89001f7 Markus Armbruster
    /* base memory (first MiB) */
338 e89001f7 Markus Armbruster
    val = MIN(ram_size / 1024, 640);
339 333190eb bellard
    rtc_set_memory(s, 0x15, val);
340 333190eb bellard
    rtc_set_memory(s, 0x16, val >> 8);
341 e89001f7 Markus Armbruster
    /* extended memory (next 64MiB) */
342 e89001f7 Markus Armbruster
    if (ram_size > 1024 * 1024) {
343 e89001f7 Markus Armbruster
        val = (ram_size - 1024 * 1024) / 1024;
344 e89001f7 Markus Armbruster
    } else {
345 e89001f7 Markus Armbruster
        val = 0;
346 e89001f7 Markus Armbruster
    }
347 80cabfad bellard
    if (val > 65535)
348 80cabfad bellard
        val = 65535;
349 b0a21b53 bellard
    rtc_set_memory(s, 0x17, val);
350 b0a21b53 bellard
    rtc_set_memory(s, 0x18, val >> 8);
351 b0a21b53 bellard
    rtc_set_memory(s, 0x30, val);
352 b0a21b53 bellard
    rtc_set_memory(s, 0x31, val >> 8);
353 e89001f7 Markus Armbruster
    /* memory between 16MiB and 4GiB */
354 e89001f7 Markus Armbruster
    if (ram_size > 16 * 1024 * 1024) {
355 e89001f7 Markus Armbruster
        val = (ram_size - 16 * 1024 * 1024) / 65536;
356 e89001f7 Markus Armbruster
    } else {
357 9da98861 bellard
        val = 0;
358 e89001f7 Markus Armbruster
    }
359 80cabfad bellard
    if (val > 65535)
360 80cabfad bellard
        val = 65535;
361 b0a21b53 bellard
    rtc_set_memory(s, 0x34, val);
362 b0a21b53 bellard
    rtc_set_memory(s, 0x35, val >> 8);
363 e89001f7 Markus Armbruster
    /* memory above 4GiB */
364 e89001f7 Markus Armbruster
    val = above_4g_mem_size / 65536;
365 e89001f7 Markus Armbruster
    rtc_set_memory(s, 0x5b, val);
366 e89001f7 Markus Armbruster
    rtc_set_memory(s, 0x5c, val >> 8);
367 e89001f7 Markus Armbruster
    rtc_set_memory(s, 0x5d, val >> 16);
368 3b46e624 ths
369 298e01b6 aurel32
    /* set the number of CPU */
370 298e01b6 aurel32
    rtc_set_memory(s, 0x5f, smp_cpus - 1);
371 298e01b6 aurel32
372 6ac0e82d balrog
    /* set boot devices, and disable floppy signature check if requested */
373 d9346e81 Markus Armbruster
    if (set_boot_dev(s, boot_device, fd_bootchk)) {
374 28c5af54 j_mayer
        exit(1);
375 28c5af54 j_mayer
    }
376 80cabfad bellard
377 b41a2cd1 bellard
    /* floppy type */
378 34d4260e Kevin Wolf
    if (floppy) {
379 34d4260e Kevin Wolf
        for (i = 0; i < 2; i++) {
380 61a8d649 Markus Armbruster
            fd_type[i] = isa_fdc_get_drive_type(floppy, i);
381 63ffb564 Blue Swirl
        }
382 63ffb564 Blue Swirl
    }
383 63ffb564 Blue Swirl
    val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
384 63ffb564 Blue Swirl
        cmos_get_fd_drive_type(fd_type[1]);
385 b0a21b53 bellard
    rtc_set_memory(s, 0x10, val);
386 3b46e624 ths
387 b0a21b53 bellard
    val = 0;
388 b41a2cd1 bellard
    nb = 0;
389 63ffb564 Blue Swirl
    if (fd_type[0] < FDRIVE_DRV_NONE) {
390 80cabfad bellard
        nb++;
391 d288c7ba Blue Swirl
    }
392 63ffb564 Blue Swirl
    if (fd_type[1] < FDRIVE_DRV_NONE) {
393 80cabfad bellard
        nb++;
394 d288c7ba Blue Swirl
    }
395 80cabfad bellard
    switch (nb) {
396 80cabfad bellard
    case 0:
397 80cabfad bellard
        break;
398 80cabfad bellard
    case 1:
399 b0a21b53 bellard
        val |= 0x01; /* 1 drive, ready for boot */
400 80cabfad bellard
        break;
401 80cabfad bellard
    case 2:
402 b0a21b53 bellard
        val |= 0x41; /* 2 drives, ready for boot */
403 80cabfad bellard
        break;
404 80cabfad bellard
    }
405 b0a21b53 bellard
    val |= 0x02; /* FPU is there */
406 b0a21b53 bellard
    val |= 0x04; /* PS/2 mouse installed */
407 b0a21b53 bellard
    rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
408 b0a21b53 bellard
409 ba6c2377 bellard
    /* hard drives */
410 c0897e0c Markus Armbruster
    arg.rtc_state = s;
411 9139046c Markus Armbruster
    arg.idebus[0] = idebus0;
412 9139046c Markus Armbruster
    arg.idebus[1] = idebus1;
413 c0897e0c Markus Armbruster
    qemu_register_reset(pc_cmos_init_late, &arg);
414 80cabfad bellard
}
415 80cabfad bellard
416 4b78a802 Blue Swirl
/* port 92 stuff: could be split off */
417 4b78a802 Blue Swirl
typedef struct Port92State {
418 4b78a802 Blue Swirl
    ISADevice dev;
419 23af670e Richard Henderson
    MemoryRegion io;
420 4b78a802 Blue Swirl
    uint8_t outport;
421 4b78a802 Blue Swirl
    qemu_irq *a20_out;
422 4b78a802 Blue Swirl
} Port92State;
423 4b78a802 Blue Swirl
424 93ef4192 Alexander Graf
static void port92_write(void *opaque, hwaddr addr, uint64_t val,
425 93ef4192 Alexander Graf
                         unsigned size)
426 4b78a802 Blue Swirl
{
427 4b78a802 Blue Swirl
    Port92State *s = opaque;
428 4b78a802 Blue Swirl
429 4b78a802 Blue Swirl
    DPRINTF("port92: write 0x%02x\n", val);
430 4b78a802 Blue Swirl
    s->outport = val;
431 4b78a802 Blue Swirl
    qemu_set_irq(*s->a20_out, (val >> 1) & 1);
432 4b78a802 Blue Swirl
    if (val & 1) {
433 4b78a802 Blue Swirl
        qemu_system_reset_request();
434 4b78a802 Blue Swirl
    }
435 4b78a802 Blue Swirl
}
436 4b78a802 Blue Swirl
437 93ef4192 Alexander Graf
static uint64_t port92_read(void *opaque, hwaddr addr,
438 93ef4192 Alexander Graf
                            unsigned size)
439 4b78a802 Blue Swirl
{
440 4b78a802 Blue Swirl
    Port92State *s = opaque;
441 4b78a802 Blue Swirl
    uint32_t ret;
442 4b78a802 Blue Swirl
443 4b78a802 Blue Swirl
    ret = s->outport;
444 4b78a802 Blue Swirl
    DPRINTF("port92: read 0x%02x\n", ret);
445 4b78a802 Blue Swirl
    return ret;
446 4b78a802 Blue Swirl
}
447 4b78a802 Blue Swirl
448 4b78a802 Blue Swirl
static void port92_init(ISADevice *dev, qemu_irq *a20_out)
449 4b78a802 Blue Swirl
{
450 4b78a802 Blue Swirl
    Port92State *s = DO_UPCAST(Port92State, dev, dev);
451 4b78a802 Blue Swirl
452 4b78a802 Blue Swirl
    s->a20_out = a20_out;
453 4b78a802 Blue Swirl
}
454 4b78a802 Blue Swirl
455 4b78a802 Blue Swirl
static const VMStateDescription vmstate_port92_isa = {
456 4b78a802 Blue Swirl
    .name = "port92",
457 4b78a802 Blue Swirl
    .version_id = 1,
458 4b78a802 Blue Swirl
    .minimum_version_id = 1,
459 4b78a802 Blue Swirl
    .minimum_version_id_old = 1,
460 4b78a802 Blue Swirl
    .fields      = (VMStateField []) {
461 4b78a802 Blue Swirl
        VMSTATE_UINT8(outport, Port92State),
462 4b78a802 Blue Swirl
        VMSTATE_END_OF_LIST()
463 4b78a802 Blue Swirl
    }
464 4b78a802 Blue Swirl
};
465 4b78a802 Blue Swirl
466 4b78a802 Blue Swirl
static void port92_reset(DeviceState *d)
467 4b78a802 Blue Swirl
{
468 4b78a802 Blue Swirl
    Port92State *s = container_of(d, Port92State, dev.qdev);
469 4b78a802 Blue Swirl
470 4b78a802 Blue Swirl
    s->outport &= ~1;
471 4b78a802 Blue Swirl
}
472 4b78a802 Blue Swirl
473 23af670e Richard Henderson
static const MemoryRegionOps port92_ops = {
474 93ef4192 Alexander Graf
    .read = port92_read,
475 93ef4192 Alexander Graf
    .write = port92_write,
476 93ef4192 Alexander Graf
    .impl = {
477 93ef4192 Alexander Graf
        .min_access_size = 1,
478 93ef4192 Alexander Graf
        .max_access_size = 1,
479 93ef4192 Alexander Graf
    },
480 93ef4192 Alexander Graf
    .endianness = DEVICE_LITTLE_ENDIAN,
481 23af670e Richard Henderson
};
482 23af670e Richard Henderson
483 4b78a802 Blue Swirl
static int port92_initfn(ISADevice *dev)
484 4b78a802 Blue Swirl
{
485 4b78a802 Blue Swirl
    Port92State *s = DO_UPCAST(Port92State, dev, dev);
486 4b78a802 Blue Swirl
487 23af670e Richard Henderson
    memory_region_init_io(&s->io, &port92_ops, s, "port92", 1);
488 23af670e Richard Henderson
    isa_register_ioport(dev, &s->io, 0x92);
489 23af670e Richard Henderson
490 4b78a802 Blue Swirl
    s->outport = 0;
491 4b78a802 Blue Swirl
    return 0;
492 4b78a802 Blue Swirl
}
493 4b78a802 Blue Swirl
494 8f04ee08 Anthony Liguori
static void port92_class_initfn(ObjectClass *klass, void *data)
495 8f04ee08 Anthony Liguori
{
496 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
497 8f04ee08 Anthony Liguori
    ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
498 8f04ee08 Anthony Liguori
    ic->init = port92_initfn;
499 39bffca2 Anthony Liguori
    dc->no_user = 1;
500 39bffca2 Anthony Liguori
    dc->reset = port92_reset;
501 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_port92_isa;
502 8f04ee08 Anthony Liguori
}
503 8f04ee08 Anthony Liguori
504 39bffca2 Anthony Liguori
static TypeInfo port92_info = {
505 39bffca2 Anthony Liguori
    .name          = "port92",
506 39bffca2 Anthony Liguori
    .parent        = TYPE_ISA_DEVICE,
507 39bffca2 Anthony Liguori
    .instance_size = sizeof(Port92State),
508 39bffca2 Anthony Liguori
    .class_init    = port92_class_initfn,
509 4b78a802 Blue Swirl
};
510 4b78a802 Blue Swirl
511 83f7d43a Andreas Färber
static void port92_register_types(void)
512 4b78a802 Blue Swirl
{
513 39bffca2 Anthony Liguori
    type_register_static(&port92_info);
514 4b78a802 Blue Swirl
}
515 83f7d43a Andreas Färber
516 83f7d43a Andreas Färber
type_init(port92_register_types)
517 4b78a802 Blue Swirl
518 956a3e6b Blue Swirl
static void handle_a20_line_change(void *opaque, int irq, int level)
519 59b8ad81 bellard
{
520 4a8fa5dc Andreas Färber
    CPUX86State *cpu = opaque;
521 e1a23744 bellard
522 956a3e6b Blue Swirl
    /* XXX: send to all CPUs ? */
523 4b78a802 Blue Swirl
    /* XXX: add logic to handle multiple A20 line sources */
524 956a3e6b Blue Swirl
    cpu_x86_set_a20(cpu, level);
525 e1a23744 bellard
}
526 e1a23744 bellard
527 80cabfad bellard
/***********************************************************/
528 80cabfad bellard
/* Bochs BIOS debug ports */
529 80cabfad bellard
530 9596ebb7 pbrook
static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
531 80cabfad bellard
{
532 a2f659ee bellard
    static const char shutdown_str[8] = "Shutdown";
533 a2f659ee bellard
    static int shutdown_index = 0;
534 3b46e624 ths
535 80cabfad bellard
    switch(addr) {
536 a2f659ee bellard
    case 0x8900:
537 a2f659ee bellard
        /* same as Bochs power off */
538 a2f659ee bellard
        if (val == shutdown_str[shutdown_index]) {
539 a2f659ee bellard
            shutdown_index++;
540 a2f659ee bellard
            if (shutdown_index == 8) {
541 a2f659ee bellard
                shutdown_index = 0;
542 a2f659ee bellard
                qemu_system_shutdown_request();
543 a2f659ee bellard
            }
544 a2f659ee bellard
        } else {
545 a2f659ee bellard
            shutdown_index = 0;
546 a2f659ee bellard
        }
547 a2f659ee bellard
        break;
548 80cabfad bellard
549 80cabfad bellard
    case 0x501:
550 80cabfad bellard
    case 0x502:
551 4333979e Anthony Liguori
        exit((val << 1) | 1);
552 80cabfad bellard
    }
553 80cabfad bellard
}
554 80cabfad bellard
555 4c5b10b7 Jes Sorensen
int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
556 4c5b10b7 Jes Sorensen
{
557 8ca209ad Alex Williamson
    int index = le32_to_cpu(e820_table.count);
558 4c5b10b7 Jes Sorensen
    struct e820_entry *entry;
559 4c5b10b7 Jes Sorensen
560 4c5b10b7 Jes Sorensen
    if (index >= E820_NR_ENTRIES)
561 4c5b10b7 Jes Sorensen
        return -EBUSY;
562 8ca209ad Alex Williamson
    entry = &e820_table.entry[index++];
563 4c5b10b7 Jes Sorensen
564 8ca209ad Alex Williamson
    entry->address = cpu_to_le64(address);
565 8ca209ad Alex Williamson
    entry->length = cpu_to_le64(length);
566 8ca209ad Alex Williamson
    entry->type = cpu_to_le32(type);
567 4c5b10b7 Jes Sorensen
568 8ca209ad Alex Williamson
    e820_table.count = cpu_to_le32(index);
569 8ca209ad Alex Williamson
    return index;
570 4c5b10b7 Jes Sorensen
}
571 4c5b10b7 Jes Sorensen
572 258711c6 Julien Grall
static const MemoryRegionPortio bochs_bios_portio_list[] = {
573 258711c6 Julien Grall
    { 0x500, 1, 1, .write = bochs_bios_write, }, /* 0x500 */
574 258711c6 Julien Grall
    { 0x501, 1, 1, .write = bochs_bios_write, }, /* 0x501 */
575 258711c6 Julien Grall
    { 0x501, 2, 2, .write = bochs_bios_write, }, /* 0x501 */
576 258711c6 Julien Grall
    { 0x8900, 1, 1, .write = bochs_bios_write, }, /* 0x8900 */
577 258711c6 Julien Grall
    PORTIO_END_OF_LIST(),
578 258711c6 Julien Grall
};
579 258711c6 Julien Grall
580 bf483392 Alexander Graf
static void *bochs_bios_init(void)
581 80cabfad bellard
{
582 3cce6243 blueswir1
    void *fw_cfg;
583 b6f6e3d3 aliguori
    uint8_t *smbios_table;
584 b6f6e3d3 aliguori
    size_t smbios_len;
585 11c2fd3e aliguori
    uint64_t *numa_fw_cfg;
586 11c2fd3e aliguori
    int i, j;
587 258711c6 Julien Grall
    PortioList *bochs_bios_port_list = g_new(PortioList, 1);
588 3cce6243 blueswir1
589 258711c6 Julien Grall
    portio_list_init(bochs_bios_port_list, bochs_bios_portio_list,
590 258711c6 Julien Grall
                     NULL, "bochs-bios");
591 258711c6 Julien Grall
    portio_list_add(bochs_bios_port_list, get_system_io(), 0x0);
592 3cce6243 blueswir1
593 3cce6243 blueswir1
    fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
594 bf483392 Alexander Graf
595 3cce6243 blueswir1
    fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
596 905fdcb5 blueswir1
    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
597 80deece2 blueswir1
    fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, (uint8_t *)acpi_tables,
598 80deece2 blueswir1
                     acpi_tables_len);
599 9b5b76d4 Jan Kiszka
    fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, kvm_allows_irq0_override());
600 b6f6e3d3 aliguori
601 b6f6e3d3 aliguori
    smbios_table = smbios_get_table(&smbios_len);
602 b6f6e3d3 aliguori
    if (smbios_table)
603 b6f6e3d3 aliguori
        fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES,
604 b6f6e3d3 aliguori
                         smbios_table, smbios_len);
605 4c5b10b7 Jes Sorensen
    fw_cfg_add_bytes(fw_cfg, FW_CFG_E820_TABLE, (uint8_t *)&e820_table,
606 4c5b10b7 Jes Sorensen
                     sizeof(struct e820_table));
607 11c2fd3e aliguori
608 40ac17cd Gleb Natapov
    fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, (uint8_t *)&hpet_cfg,
609 40ac17cd Gleb Natapov
                     sizeof(struct hpet_fw_config));
610 11c2fd3e aliguori
    /* allocate memory for the NUMA channel: one (64bit) word for the number
611 11c2fd3e aliguori
     * of nodes, one word for each VCPU->node and one word for each node to
612 11c2fd3e aliguori
     * hold the amount of memory.
613 11c2fd3e aliguori
     */
614 991dfefd Vasilis Liaskovitis
    numa_fw_cfg = g_malloc0((1 + max_cpus + nb_numa_nodes) * 8);
615 11c2fd3e aliguori
    numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
616 991dfefd Vasilis Liaskovitis
    for (i = 0; i < max_cpus; i++) {
617 11c2fd3e aliguori
        for (j = 0; j < nb_numa_nodes; j++) {
618 ee785fed Chegu Vinod
            if (test_bit(i, node_cpumask[j])) {
619 11c2fd3e aliguori
                numa_fw_cfg[i + 1] = cpu_to_le64(j);
620 11c2fd3e aliguori
                break;
621 11c2fd3e aliguori
            }
622 11c2fd3e aliguori
        }
623 11c2fd3e aliguori
    }
624 11c2fd3e aliguori
    for (i = 0; i < nb_numa_nodes; i++) {
625 991dfefd Vasilis Liaskovitis
        numa_fw_cfg[max_cpus + 1 + i] = cpu_to_le64(node_mem[i]);
626 11c2fd3e aliguori
    }
627 11c2fd3e aliguori
    fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, (uint8_t *)numa_fw_cfg,
628 991dfefd Vasilis Liaskovitis
                     (1 + max_cpus + nb_numa_nodes) * 8);
629 bf483392 Alexander Graf
630 bf483392 Alexander Graf
    return fw_cfg;
631 80cabfad bellard
}
632 80cabfad bellard
633 642a4f96 ths
static long get_file_size(FILE *f)
634 642a4f96 ths
{
635 642a4f96 ths
    long where, size;
636 642a4f96 ths
637 642a4f96 ths
    /* XXX: on Unix systems, using fstat() probably makes more sense */
638 642a4f96 ths
639 642a4f96 ths
    where = ftell(f);
640 642a4f96 ths
    fseek(f, 0, SEEK_END);
641 642a4f96 ths
    size = ftell(f);
642 642a4f96 ths
    fseek(f, where, SEEK_SET);
643 642a4f96 ths
644 642a4f96 ths
    return size;
645 642a4f96 ths
}
646 642a4f96 ths
647 f16408df Alexander Graf
static void load_linux(void *fw_cfg,
648 4fc9af53 aliguori
                       const char *kernel_filename,
649 642a4f96 ths
                       const char *initrd_filename,
650 e6ade764 Glauber Costa
                       const char *kernel_cmdline,
651 a8170e5e Avi Kivity
                       hwaddr max_ram_size)
652 642a4f96 ths
{
653 642a4f96 ths
    uint16_t protocol;
654 5cea8590 Paul Brook
    int setup_size, kernel_size, initrd_size = 0, cmdline_size;
655 642a4f96 ths
    uint32_t initrd_max;
656 57a46d05 Alexander Graf
    uint8_t header[8192], *setup, *kernel, *initrd_data;
657 a8170e5e Avi Kivity
    hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
658 45a50b16 Gerd Hoffmann
    FILE *f;
659 bf4e5d92 Pascal Terjan
    char *vmode;
660 642a4f96 ths
661 642a4f96 ths
    /* Align to 16 bytes as a paranoia measure */
662 642a4f96 ths
    cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
663 642a4f96 ths
664 642a4f96 ths
    /* load the kernel header */
665 642a4f96 ths
    f = fopen(kernel_filename, "rb");
666 642a4f96 ths
    if (!f || !(kernel_size = get_file_size(f)) ||
667 f16408df Alexander Graf
        fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
668 f16408df Alexander Graf
        MIN(ARRAY_SIZE(header), kernel_size)) {
669 850810d0 Justin M. Forbes
        fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
670 850810d0 Justin M. Forbes
                kernel_filename, strerror(errno));
671 642a4f96 ths
        exit(1);
672 642a4f96 ths
    }
673 642a4f96 ths
674 642a4f96 ths
    /* kernel protocol version */
675 bc4edd79 bellard
#if 0
676 642a4f96 ths
    fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
677 bc4edd79 bellard
#endif
678 642a4f96 ths
    if (ldl_p(header+0x202) == 0x53726448)
679 642a4f96 ths
        protocol = lduw_p(header+0x206);
680 f16408df Alexander Graf
    else {
681 f16408df Alexander Graf
        /* This looks like a multiboot kernel. If it is, let's stop
682 f16408df Alexander Graf
           treating it like a Linux kernel. */
683 52001445 Adam Lackorzynski
        if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
684 52001445 Adam Lackorzynski
                           kernel_cmdline, kernel_size, header))
685 82663ee2 Blue Swirl
            return;
686 642a4f96 ths
        protocol = 0;
687 f16408df Alexander Graf
    }
688 642a4f96 ths
689 642a4f96 ths
    if (protocol < 0x200 || !(header[0x211] & 0x01)) {
690 642a4f96 ths
        /* Low kernel */
691 a37af289 blueswir1
        real_addr    = 0x90000;
692 a37af289 blueswir1
        cmdline_addr = 0x9a000 - cmdline_size;
693 a37af289 blueswir1
        prot_addr    = 0x10000;
694 642a4f96 ths
    } else if (protocol < 0x202) {
695 642a4f96 ths
        /* High but ancient kernel */
696 a37af289 blueswir1
        real_addr    = 0x90000;
697 a37af289 blueswir1
        cmdline_addr = 0x9a000 - cmdline_size;
698 a37af289 blueswir1
        prot_addr    = 0x100000;
699 642a4f96 ths
    } else {
700 642a4f96 ths
        /* High and recent kernel */
701 a37af289 blueswir1
        real_addr    = 0x10000;
702 a37af289 blueswir1
        cmdline_addr = 0x20000;
703 a37af289 blueswir1
        prot_addr    = 0x100000;
704 642a4f96 ths
    }
705 642a4f96 ths
706 bc4edd79 bellard
#if 0
707 642a4f96 ths
    fprintf(stderr,
708 526ccb7a balrog
            "qemu: real_addr     = 0x" TARGET_FMT_plx "\n"
709 526ccb7a balrog
            "qemu: cmdline_addr  = 0x" TARGET_FMT_plx "\n"
710 526ccb7a balrog
            "qemu: prot_addr     = 0x" TARGET_FMT_plx "\n",
711 a37af289 blueswir1
            real_addr,
712 a37af289 blueswir1
            cmdline_addr,
713 a37af289 blueswir1
            prot_addr);
714 bc4edd79 bellard
#endif
715 642a4f96 ths
716 642a4f96 ths
    /* highest address for loading the initrd */
717 642a4f96 ths
    if (protocol >= 0x203)
718 642a4f96 ths
        initrd_max = ldl_p(header+0x22c);
719 642a4f96 ths
    else
720 642a4f96 ths
        initrd_max = 0x37ffffff;
721 642a4f96 ths
722 e6ade764 Glauber Costa
    if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)
723 e6ade764 Glauber Costa
            initrd_max = max_ram_size-ACPI_DATA_SIZE-1;
724 642a4f96 ths
725 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
726 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);
727 57a46d05 Alexander Graf
    fw_cfg_add_bytes(fw_cfg, FW_CFG_CMDLINE_DATA,
728 57a46d05 Alexander Graf
                     (uint8_t*)strdup(kernel_cmdline),
729 57a46d05 Alexander Graf
                     strlen(kernel_cmdline)+1);
730 642a4f96 ths
731 642a4f96 ths
    if (protocol >= 0x202) {
732 a37af289 blueswir1
        stl_p(header+0x228, cmdline_addr);
733 642a4f96 ths
    } else {
734 642a4f96 ths
        stw_p(header+0x20, 0xA33F);
735 642a4f96 ths
        stw_p(header+0x22, cmdline_addr-real_addr);
736 642a4f96 ths
    }
737 642a4f96 ths
738 bf4e5d92 Pascal Terjan
    /* handle vga= parameter */
739 bf4e5d92 Pascal Terjan
    vmode = strstr(kernel_cmdline, "vga=");
740 bf4e5d92 Pascal Terjan
    if (vmode) {
741 bf4e5d92 Pascal Terjan
        unsigned int video_mode;
742 bf4e5d92 Pascal Terjan
        /* skip "vga=" */
743 bf4e5d92 Pascal Terjan
        vmode += 4;
744 bf4e5d92 Pascal Terjan
        if (!strncmp(vmode, "normal", 6)) {
745 bf4e5d92 Pascal Terjan
            video_mode = 0xffff;
746 bf4e5d92 Pascal Terjan
        } else if (!strncmp(vmode, "ext", 3)) {
747 bf4e5d92 Pascal Terjan
            video_mode = 0xfffe;
748 bf4e5d92 Pascal Terjan
        } else if (!strncmp(vmode, "ask", 3)) {
749 bf4e5d92 Pascal Terjan
            video_mode = 0xfffd;
750 bf4e5d92 Pascal Terjan
        } else {
751 bf4e5d92 Pascal Terjan
            video_mode = strtol(vmode, NULL, 0);
752 bf4e5d92 Pascal Terjan
        }
753 bf4e5d92 Pascal Terjan
        stw_p(header+0x1fa, video_mode);
754 bf4e5d92 Pascal Terjan
    }
755 bf4e5d92 Pascal Terjan
756 642a4f96 ths
    /* loader type */
757 5cbdb3a3 Stefan Weil
    /* High nybble = B reserved for QEMU; low nybble is revision number.
758 642a4f96 ths
       If this code is substantially changed, you may want to consider
759 642a4f96 ths
       incrementing the revision. */
760 642a4f96 ths
    if (protocol >= 0x200)
761 642a4f96 ths
        header[0x210] = 0xB0;
762 642a4f96 ths
763 642a4f96 ths
    /* heap */
764 642a4f96 ths
    if (protocol >= 0x201) {
765 642a4f96 ths
        header[0x211] |= 0x80;        /* CAN_USE_HEAP */
766 642a4f96 ths
        stw_p(header+0x224, cmdline_addr-real_addr-0x200);
767 642a4f96 ths
    }
768 642a4f96 ths
769 642a4f96 ths
    /* load initrd */
770 642a4f96 ths
    if (initrd_filename) {
771 642a4f96 ths
        if (protocol < 0x200) {
772 642a4f96 ths
            fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
773 642a4f96 ths
            exit(1);
774 642a4f96 ths
        }
775 642a4f96 ths
776 45a50b16 Gerd Hoffmann
        initrd_size = get_image_size(initrd_filename);
777 d6fa4b77 M. Mohan Kumar
        if (initrd_size < 0) {
778 d6fa4b77 M. Mohan Kumar
            fprintf(stderr, "qemu: error reading initrd %s\n",
779 d6fa4b77 M. Mohan Kumar
                    initrd_filename);
780 d6fa4b77 M. Mohan Kumar
            exit(1);
781 d6fa4b77 M. Mohan Kumar
        }
782 d6fa4b77 M. Mohan Kumar
783 45a50b16 Gerd Hoffmann
        initrd_addr = (initrd_max-initrd_size) & ~4095;
784 57a46d05 Alexander Graf
785 7267c094 Anthony Liguori
        initrd_data = g_malloc(initrd_size);
786 57a46d05 Alexander Graf
        load_image(initrd_filename, initrd_data);
787 57a46d05 Alexander Graf
788 57a46d05 Alexander Graf
        fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
789 57a46d05 Alexander Graf
        fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
790 57a46d05 Alexander Graf
        fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
791 642a4f96 ths
792 a37af289 blueswir1
        stl_p(header+0x218, initrd_addr);
793 642a4f96 ths
        stl_p(header+0x21c, initrd_size);
794 642a4f96 ths
    }
795 642a4f96 ths
796 45a50b16 Gerd Hoffmann
    /* load kernel and setup */
797 642a4f96 ths
    setup_size = header[0x1f1];
798 642a4f96 ths
    if (setup_size == 0)
799 642a4f96 ths
        setup_size = 4;
800 642a4f96 ths
    setup_size = (setup_size+1)*512;
801 45a50b16 Gerd Hoffmann
    kernel_size -= setup_size;
802 642a4f96 ths
803 7267c094 Anthony Liguori
    setup  = g_malloc(setup_size);
804 7267c094 Anthony Liguori
    kernel = g_malloc(kernel_size);
805 45a50b16 Gerd Hoffmann
    fseek(f, 0, SEEK_SET);
806 5a41ecc5 Kirill A. Shutemov
    if (fread(setup, 1, setup_size, f) != setup_size) {
807 5a41ecc5 Kirill A. Shutemov
        fprintf(stderr, "fread() failed\n");
808 5a41ecc5 Kirill A. Shutemov
        exit(1);
809 5a41ecc5 Kirill A. Shutemov
    }
810 5a41ecc5 Kirill A. Shutemov
    if (fread(kernel, 1, kernel_size, f) != kernel_size) {
811 5a41ecc5 Kirill A. Shutemov
        fprintf(stderr, "fread() failed\n");
812 5a41ecc5 Kirill A. Shutemov
        exit(1);
813 5a41ecc5 Kirill A. Shutemov
    }
814 642a4f96 ths
    fclose(f);
815 45a50b16 Gerd Hoffmann
    memcpy(setup, header, MIN(sizeof(header), setup_size));
816 57a46d05 Alexander Graf
817 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
818 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
819 57a46d05 Alexander Graf
    fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
820 57a46d05 Alexander Graf
821 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
822 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
823 57a46d05 Alexander Graf
    fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
824 57a46d05 Alexander Graf
825 2e55e842 Gleb Natapov
    option_rom[nb_option_roms].name = "linuxboot.bin";
826 2e55e842 Gleb Natapov
    option_rom[nb_option_roms].bootindex = 0;
827 57a46d05 Alexander Graf
    nb_option_roms++;
828 642a4f96 ths
}
829 642a4f96 ths
830 b41a2cd1 bellard
#define NE2000_NB_MAX 6
831 b41a2cd1 bellard
832 675d6f82 Blue Swirl
static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
833 675d6f82 Blue Swirl
                                              0x280, 0x380 };
834 675d6f82 Blue Swirl
static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
835 b41a2cd1 bellard
836 675d6f82 Blue Swirl
static const int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
837 675d6f82 Blue Swirl
static const int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
838 6508fe59 bellard
839 48a18b3c Hervé Poussineau
void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
840 a41b2ff2 pbrook
{
841 a41b2ff2 pbrook
    static int nb_ne2k = 0;
842 a41b2ff2 pbrook
843 a41b2ff2 pbrook
    if (nb_ne2k == NE2000_NB_MAX)
844 a41b2ff2 pbrook
        return;
845 48a18b3c Hervé Poussineau
    isa_ne2000_init(bus, ne2000_io[nb_ne2k],
846 9453c5bc Gerd Hoffmann
                    ne2000_irq[nb_ne2k], nd);
847 a41b2ff2 pbrook
    nb_ne2k++;
848 a41b2ff2 pbrook
}
849 a41b2ff2 pbrook
850 92a16d7a Blue Swirl
DeviceState *cpu_get_current_apic(void)
851 0e26b7b8 Blue Swirl
{
852 0e26b7b8 Blue Swirl
    if (cpu_single_env) {
853 0e26b7b8 Blue Swirl
        return cpu_single_env->apic_state;
854 0e26b7b8 Blue Swirl
    } else {
855 0e26b7b8 Blue Swirl
        return NULL;
856 0e26b7b8 Blue Swirl
    }
857 0e26b7b8 Blue Swirl
}
858 0e26b7b8 Blue Swirl
859 845773ab Isaku Yamahata
void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
860 53b67b30 Blue Swirl
{
861 4a8fa5dc Andreas Färber
    CPUX86State *s = opaque;
862 53b67b30 Blue Swirl
863 53b67b30 Blue Swirl
    if (level) {
864 53b67b30 Blue Swirl
        cpu_interrupt(s, CPU_INTERRUPT_SMI);
865 53b67b30 Blue Swirl
    }
866 53b67b30 Blue Swirl
}
867 53b67b30 Blue Swirl
868 845773ab Isaku Yamahata
void pc_cpus_init(const char *cpu_model)
869 70166477 Isaku Yamahata
{
870 70166477 Isaku Yamahata
    int i;
871 70166477 Isaku Yamahata
872 70166477 Isaku Yamahata
    /* init CPUs */
873 70166477 Isaku Yamahata
    if (cpu_model == NULL) {
874 70166477 Isaku Yamahata
#ifdef TARGET_X86_64
875 70166477 Isaku Yamahata
        cpu_model = "qemu64";
876 70166477 Isaku Yamahata
#else
877 70166477 Isaku Yamahata
        cpu_model = "qemu32";
878 70166477 Isaku Yamahata
#endif
879 70166477 Isaku Yamahata
    }
880 70166477 Isaku Yamahata
881 bdeec802 Igor Mammedov
    for (i = 0; i < smp_cpus; i++) {
882 bdeec802 Igor Mammedov
        if (!cpu_x86_init(cpu_model)) {
883 bdeec802 Igor Mammedov
            fprintf(stderr, "Unable to find x86 CPU definition\n");
884 bdeec802 Igor Mammedov
            exit(1);
885 bdeec802 Igor Mammedov
        }
886 70166477 Isaku Yamahata
    }
887 70166477 Isaku Yamahata
}
888 70166477 Isaku Yamahata
889 459ae5ea Gleb Natapov
void *pc_memory_init(MemoryRegion *system_memory,
890 4aa63af1 Avi Kivity
                    const char *kernel_filename,
891 845773ab Isaku Yamahata
                    const char *kernel_cmdline,
892 845773ab Isaku Yamahata
                    const char *initrd_filename,
893 e0e7e67b Anthony PERARD
                    ram_addr_t below_4g_mem_size,
894 ae0a5466 Avi Kivity
                    ram_addr_t above_4g_mem_size,
895 4463aee6 Jan Kiszka
                    MemoryRegion *rom_memory,
896 ae0a5466 Avi Kivity
                    MemoryRegion **ram_memory)
897 80cabfad bellard
{
898 cbc5b5f3 Jordan Justen
    int linux_boot, i;
899 cbc5b5f3 Jordan Justen
    MemoryRegion *ram, *option_rom_mr;
900 00cb2a99 Avi Kivity
    MemoryRegion *ram_below_4g, *ram_above_4g;
901 81a204e4 Eduard - Gabriel Munteanu
    void *fw_cfg;
902 d592d303 bellard
903 80cabfad bellard
    linux_boot = (kernel_filename != NULL);
904 80cabfad bellard
905 00cb2a99 Avi Kivity
    /* Allocate RAM.  We allocate it as a single memory region and use
906 66a0a2cb Dong Xu Wang
     * aliases to address portions of it, mostly for backwards compatibility
907 00cb2a99 Avi Kivity
     * with older qemus that used qemu_ram_alloc().
908 00cb2a99 Avi Kivity
     */
909 7267c094 Anthony Liguori
    ram = g_malloc(sizeof(*ram));
910 c5705a77 Avi Kivity
    memory_region_init_ram(ram, "pc.ram",
911 00cb2a99 Avi Kivity
                           below_4g_mem_size + above_4g_mem_size);
912 c5705a77 Avi Kivity
    vmstate_register_ram_global(ram);
913 ae0a5466 Avi Kivity
    *ram_memory = ram;
914 7267c094 Anthony Liguori
    ram_below_4g = g_malloc(sizeof(*ram_below_4g));
915 00cb2a99 Avi Kivity
    memory_region_init_alias(ram_below_4g, "ram-below-4g", ram,
916 00cb2a99 Avi Kivity
                             0, below_4g_mem_size);
917 00cb2a99 Avi Kivity
    memory_region_add_subregion(system_memory, 0, ram_below_4g);
918 bbe80adf Alex Williamson
    if (above_4g_mem_size > 0) {
919 7267c094 Anthony Liguori
        ram_above_4g = g_malloc(sizeof(*ram_above_4g));
920 00cb2a99 Avi Kivity
        memory_region_init_alias(ram_above_4g, "ram-above-4g", ram,
921 00cb2a99 Avi Kivity
                                 below_4g_mem_size, above_4g_mem_size);
922 00cb2a99 Avi Kivity
        memory_region_add_subregion(system_memory, 0x100000000ULL,
923 00cb2a99 Avi Kivity
                                    ram_above_4g);
924 bbe80adf Alex Williamson
    }
925 82b36dc3 aliguori
926 cbc5b5f3 Jordan Justen
927 cbc5b5f3 Jordan Justen
    /* Initialize PC system firmware */
928 cbc5b5f3 Jordan Justen
    pc_system_firmware_init(rom_memory);
929 00cb2a99 Avi Kivity
930 7267c094 Anthony Liguori
    option_rom_mr = g_malloc(sizeof(*option_rom_mr));
931 c5705a77 Avi Kivity
    memory_region_init_ram(option_rom_mr, "pc.rom", PC_ROM_SIZE);
932 c5705a77 Avi Kivity
    vmstate_register_ram_global(option_rom_mr);
933 4463aee6 Jan Kiszka
    memory_region_add_subregion_overlap(rom_memory,
934 00cb2a99 Avi Kivity
                                        PC_ROM_MIN_VGA,
935 00cb2a99 Avi Kivity
                                        option_rom_mr,
936 00cb2a99 Avi Kivity
                                        1);
937 f753ff16 pbrook
938 bf483392 Alexander Graf
    fw_cfg = bochs_bios_init();
939 8832cb80 Gerd Hoffmann
    rom_set_fw(fw_cfg);
940 1d108d97 Alexander Graf
941 f753ff16 pbrook
    if (linux_boot) {
942 81a204e4 Eduard - Gabriel Munteanu
        load_linux(fw_cfg, kernel_filename, initrd_filename, kernel_cmdline, below_4g_mem_size);
943 f753ff16 pbrook
    }
944 f753ff16 pbrook
945 f753ff16 pbrook
    for (i = 0; i < nb_option_roms; i++) {
946 2e55e842 Gleb Natapov
        rom_add_option(option_rom[i].name, option_rom[i].bootindex);
947 406c8df3 Glauber Costa
    }
948 459ae5ea Gleb Natapov
    return fw_cfg;
949 3d53f5c3 Isaku Yamahata
}
950 3d53f5c3 Isaku Yamahata
951 845773ab Isaku Yamahata
qemu_irq *pc_allocate_cpu_irq(void)
952 845773ab Isaku Yamahata
{
953 845773ab Isaku Yamahata
    return qemu_allocate_irqs(pic_irq_request, NULL, 1);
954 845773ab Isaku Yamahata
}
955 845773ab Isaku Yamahata
956 48a18b3c Hervé Poussineau
DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
957 765d7908 Isaku Yamahata
{
958 ad6d45fa Anthony Liguori
    DeviceState *dev = NULL;
959 ad6d45fa Anthony Liguori
960 16094b75 Aurelien Jarno
    if (pci_bus) {
961 16094b75 Aurelien Jarno
        PCIDevice *pcidev = pci_vga_init(pci_bus);
962 16094b75 Aurelien Jarno
        dev = pcidev ? &pcidev->qdev : NULL;
963 16094b75 Aurelien Jarno
    } else if (isa_bus) {
964 16094b75 Aurelien Jarno
        ISADevice *isadev = isa_vga_init(isa_bus);
965 16094b75 Aurelien Jarno
        dev = isadev ? &isadev->qdev : NULL;
966 765d7908 Isaku Yamahata
    }
967 ad6d45fa Anthony Liguori
    return dev;
968 765d7908 Isaku Yamahata
}
969 765d7908 Isaku Yamahata
970 4556bd8b Blue Swirl
static void cpu_request_exit(void *opaque, int irq, int level)
971 4556bd8b Blue Swirl
{
972 4a8fa5dc Andreas Färber
    CPUX86State *env = cpu_single_env;
973 4556bd8b Blue Swirl
974 4556bd8b Blue Swirl
    if (env && level) {
975 4556bd8b Blue Swirl
        cpu_exit(env);
976 4556bd8b Blue Swirl
    }
977 4556bd8b Blue Swirl
}
978 4556bd8b Blue Swirl
979 258711c6 Julien Grall
static const MemoryRegionOps ioport80_io_ops = {
980 258711c6 Julien Grall
    .write = ioport80_write,
981 258711c6 Julien Grall
    .endianness = DEVICE_NATIVE_ENDIAN,
982 258711c6 Julien Grall
    .impl = {
983 258711c6 Julien Grall
        .min_access_size = 1,
984 258711c6 Julien Grall
        .max_access_size = 1,
985 258711c6 Julien Grall
    },
986 258711c6 Julien Grall
};
987 258711c6 Julien Grall
988 258711c6 Julien Grall
static const MemoryRegionOps ioportF0_io_ops = {
989 258711c6 Julien Grall
    .write = ioportF0_write,
990 258711c6 Julien Grall
    .endianness = DEVICE_NATIVE_ENDIAN,
991 258711c6 Julien Grall
    .impl = {
992 258711c6 Julien Grall
        .min_access_size = 1,
993 258711c6 Julien Grall
        .max_access_size = 1,
994 258711c6 Julien Grall
    },
995 258711c6 Julien Grall
};
996 258711c6 Julien Grall
997 48a18b3c Hervé Poussineau
void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
998 1611977c Anthony PERARD
                          ISADevice **rtc_state,
999 34d4260e Kevin Wolf
                          ISADevice **floppy,
1000 1611977c Anthony PERARD
                          bool no_vmport)
1001 ffe513da Isaku Yamahata
{
1002 ffe513da Isaku Yamahata
    int i;
1003 ffe513da Isaku Yamahata
    DriveInfo *fd[MAX_FD];
1004 ce967e2f Jan Kiszka
    DeviceState *hpet = NULL;
1005 ce967e2f Jan Kiszka
    int pit_isa_irq = 0;
1006 ce967e2f Jan Kiszka
    qemu_irq pit_alt_irq = NULL;
1007 7d932dfd Jan Kiszka
    qemu_irq rtc_irq = NULL;
1008 956a3e6b Blue Swirl
    qemu_irq *a20_line;
1009 c2d8d311 Stefano Stabellini
    ISADevice *i8042, *port92, *vmmouse, *pit = NULL;
1010 4556bd8b Blue Swirl
    qemu_irq *cpu_exit_irq;
1011 258711c6 Julien Grall
    MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
1012 258711c6 Julien Grall
    MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
1013 ffe513da Isaku Yamahata
1014 258711c6 Julien Grall
    memory_region_init_io(ioport80_io, &ioport80_io_ops, NULL, "ioport80", 1);
1015 258711c6 Julien Grall
    memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
1016 ffe513da Isaku Yamahata
1017 258711c6 Julien Grall
    memory_region_init_io(ioportF0_io, &ioportF0_io_ops, NULL, "ioportF0", 1);
1018 258711c6 Julien Grall
    memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
1019 ffe513da Isaku Yamahata
1020 5d17c0d2 Jan Kiszka
    /*
1021 5d17c0d2 Jan Kiszka
     * Check if an HPET shall be created.
1022 5d17c0d2 Jan Kiszka
     *
1023 5d17c0d2 Jan Kiszka
     * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1024 5d17c0d2 Jan Kiszka
     * when the HPET wants to take over. Thus we have to disable the latter.
1025 5d17c0d2 Jan Kiszka
     */
1026 5d17c0d2 Jan Kiszka
    if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
1027 ce967e2f Jan Kiszka
        hpet = sysbus_try_create_simple("hpet", HPET_BASE, NULL);
1028 822557eb Jan Kiszka
1029 dd703b99 Blue Swirl
        if (hpet) {
1030 b881fbe9 Jan Kiszka
            for (i = 0; i < GSI_NUM_PINS; i++) {
1031 b881fbe9 Jan Kiszka
                sysbus_connect_irq(sysbus_from_qdev(hpet), i, gsi[i]);
1032 dd703b99 Blue Swirl
            }
1033 ce967e2f Jan Kiszka
            pit_isa_irq = -1;
1034 ce967e2f Jan Kiszka
            pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
1035 ce967e2f Jan Kiszka
            rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
1036 822557eb Jan Kiszka
        }
1037 ffe513da Isaku Yamahata
    }
1038 48a18b3c Hervé Poussineau
    *rtc_state = rtc_init(isa_bus, 2000, rtc_irq);
1039 7d932dfd Jan Kiszka
1040 7d932dfd Jan Kiszka
    qemu_register_boot_set(pc_boot_set, *rtc_state);
1041 7d932dfd Jan Kiszka
1042 c2d8d311 Stefano Stabellini
    if (!xen_enabled()) {
1043 c2d8d311 Stefano Stabellini
        if (kvm_irqchip_in_kernel()) {
1044 c2d8d311 Stefano Stabellini
            pit = kvm_pit_init(isa_bus, 0x40);
1045 c2d8d311 Stefano Stabellini
        } else {
1046 c2d8d311 Stefano Stabellini
            pit = pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
1047 c2d8d311 Stefano Stabellini
        }
1048 c2d8d311 Stefano Stabellini
        if (hpet) {
1049 c2d8d311 Stefano Stabellini
            /* connect PIT to output control line of the HPET */
1050 c2d8d311 Stefano Stabellini
            qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(&pit->qdev, 0));
1051 c2d8d311 Stefano Stabellini
        }
1052 c2d8d311 Stefano Stabellini
        pcspk_init(isa_bus, pit);
1053 ce967e2f Jan Kiszka
    }
1054 ffe513da Isaku Yamahata
1055 ffe513da Isaku Yamahata
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
1056 ffe513da Isaku Yamahata
        if (serial_hds[i]) {
1057 48a18b3c Hervé Poussineau
            serial_isa_init(isa_bus, i, serial_hds[i]);
1058 ffe513da Isaku Yamahata
        }
1059 ffe513da Isaku Yamahata
    }
1060 ffe513da Isaku Yamahata
1061 ffe513da Isaku Yamahata
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
1062 ffe513da Isaku Yamahata
        if (parallel_hds[i]) {
1063 48a18b3c Hervé Poussineau
            parallel_init(isa_bus, i, parallel_hds[i]);
1064 ffe513da Isaku Yamahata
        }
1065 ffe513da Isaku Yamahata
    }
1066 ffe513da Isaku Yamahata
1067 4b78a802 Blue Swirl
    a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
1068 48a18b3c Hervé Poussineau
    i8042 = isa_create_simple(isa_bus, "i8042");
1069 4b78a802 Blue Swirl
    i8042_setup_a20_line(i8042, &a20_line[0]);
1070 1611977c Anthony PERARD
    if (!no_vmport) {
1071 48a18b3c Hervé Poussineau
        vmport_init(isa_bus);
1072 48a18b3c Hervé Poussineau
        vmmouse = isa_try_create(isa_bus, "vmmouse");
1073 1611977c Anthony PERARD
    } else {
1074 1611977c Anthony PERARD
        vmmouse = NULL;
1075 1611977c Anthony PERARD
    }
1076 86d86414 Blue Swirl
    if (vmmouse) {
1077 86d86414 Blue Swirl
        qdev_prop_set_ptr(&vmmouse->qdev, "ps2_mouse", i8042);
1078 43f20196 Jan Kiszka
        qdev_init_nofail(&vmmouse->qdev);
1079 86d86414 Blue Swirl
    }
1080 48a18b3c Hervé Poussineau
    port92 = isa_create_simple(isa_bus, "port92");
1081 4b78a802 Blue Swirl
    port92_init(port92, &a20_line[1]);
1082 956a3e6b Blue Swirl
1083 4556bd8b Blue Swirl
    cpu_exit_irq = qemu_allocate_irqs(cpu_request_exit, NULL, 1);
1084 4556bd8b Blue Swirl
    DMA_init(0, cpu_exit_irq);
1085 ffe513da Isaku Yamahata
1086 ffe513da Isaku Yamahata
    for(i = 0; i < MAX_FD; i++) {
1087 ffe513da Isaku Yamahata
        fd[i] = drive_get(IF_FLOPPY, 0, i);
1088 ffe513da Isaku Yamahata
    }
1089 48a18b3c Hervé Poussineau
    *floppy = fdctrl_init_isa(isa_bus, fd);
1090 ffe513da Isaku Yamahata
}
1091 ffe513da Isaku Yamahata
1092 9011a1a7 Isaku Yamahata
void pc_nic_init(ISABus *isa_bus, PCIBus *pci_bus)
1093 9011a1a7 Isaku Yamahata
{
1094 9011a1a7 Isaku Yamahata
    int i;
1095 9011a1a7 Isaku Yamahata
1096 9011a1a7 Isaku Yamahata
    for (i = 0; i < nb_nics; i++) {
1097 9011a1a7 Isaku Yamahata
        NICInfo *nd = &nd_table[i];
1098 9011a1a7 Isaku Yamahata
1099 9011a1a7 Isaku Yamahata
        if (!pci_bus || (nd->model && strcmp(nd->model, "ne2k_isa") == 0)) {
1100 9011a1a7 Isaku Yamahata
            pc_init_ne2k_isa(isa_bus, nd);
1101 9011a1a7 Isaku Yamahata
        } else {
1102 9011a1a7 Isaku Yamahata
            pci_nic_init_nofail(nd, "e1000", NULL);
1103 9011a1a7 Isaku Yamahata
        }
1104 9011a1a7 Isaku Yamahata
    }
1105 9011a1a7 Isaku Yamahata
}
1106 9011a1a7 Isaku Yamahata
1107 845773ab Isaku Yamahata
void pc_pci_device_init(PCIBus *pci_bus)
1108 e3a5cf42 Isaku Yamahata
{
1109 e3a5cf42 Isaku Yamahata
    int max_bus;
1110 e3a5cf42 Isaku Yamahata
    int bus;
1111 e3a5cf42 Isaku Yamahata
1112 e3a5cf42 Isaku Yamahata
    max_bus = drive_get_max_bus(IF_SCSI);
1113 e3a5cf42 Isaku Yamahata
    for (bus = 0; bus <= max_bus; bus++) {
1114 e3a5cf42 Isaku Yamahata
        pci_create_simple(pci_bus, -1, "lsi53c895a");
1115 e3a5cf42 Isaku Yamahata
    }
1116 e3a5cf42 Isaku Yamahata
}
1117 a39e3564 Jason Baron
1118 a39e3564 Jason Baron
void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
1119 a39e3564 Jason Baron
{
1120 a39e3564 Jason Baron
    DeviceState *dev;
1121 a39e3564 Jason Baron
    SysBusDevice *d;
1122 a39e3564 Jason Baron
    unsigned int i;
1123 a39e3564 Jason Baron
1124 a39e3564 Jason Baron
    if (kvm_irqchip_in_kernel()) {
1125 a39e3564 Jason Baron
        dev = qdev_create(NULL, "kvm-ioapic");
1126 a39e3564 Jason Baron
    } else {
1127 a39e3564 Jason Baron
        dev = qdev_create(NULL, "ioapic");
1128 a39e3564 Jason Baron
    }
1129 a39e3564 Jason Baron
    if (parent_name) {
1130 a39e3564 Jason Baron
        object_property_add_child(object_resolve_path(parent_name, NULL),
1131 a39e3564 Jason Baron
                                  "ioapic", OBJECT(dev), NULL);
1132 a39e3564 Jason Baron
    }
1133 a39e3564 Jason Baron
    qdev_init_nofail(dev);
1134 a39e3564 Jason Baron
    d = sysbus_from_qdev(dev);
1135 a39e3564 Jason Baron
    sysbus_mmio_map(d, 0, 0xfec00000);
1136 a39e3564 Jason Baron
1137 a39e3564 Jason Baron
    for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1138 a39e3564 Jason Baron
        gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
1139 a39e3564 Jason Baron
    }
1140 a39e3564 Jason Baron
}