Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 1 | #include <console/console.h> |
Gerd Hoffmann | aa588e0 | 2013-05-31 09:26:55 +0200 | [diff] [blame] | 2 | #include <cpu/cpu.h> |
Patrick Georgi | c8feedd | 2012-02-16 18:43:25 +0100 | [diff] [blame] | 3 | #include <cpu/x86/lapic_def.h> |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 4 | #include <arch/io.h> |
Uwe Hermann | 74d1a6e | 2010-10-12 17:34:08 +0000 | [diff] [blame] | 5 | #include <arch/ioapic.h> |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 6 | #include <stdint.h> |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 7 | #include <device/device.h> |
| 8 | #include <device/pci.h> |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
Myles Watson | 2e67273 | 2009-11-12 16:38:03 +0000 | [diff] [blame] | 11 | #include <delay.h> |
Sven Schnelle | 164bcfd | 2011-08-14 20:56:34 +0200 | [diff] [blame] | 12 | #include <smbios.h> |
Rudolf Marek | 97be27e | 2010-12-13 19:50:25 +0000 | [diff] [blame] | 13 | #include <cbmem.h> |
Myles Watson | 0520d55 | 2009-05-11 22:44:14 +0000 | [diff] [blame] | 14 | |
Gerd Hoffmann | aa588e0 | 2013-05-31 09:26:55 +0200 | [diff] [blame] | 15 | #include "fw_cfg.h" |
| 16 | |
Stefan Reinauer | 597ff87 | 2013-01-07 13:21:22 -0800 | [diff] [blame] | 17 | #include "memory.c" |
Sven Schnelle | 164bcfd | 2011-08-14 20:56:34 +0200 | [diff] [blame] | 18 | |
Gerd Hoffmann | 9839a38 | 2013-06-17 12:26:17 +0200 | [diff] [blame] | 19 | static unsigned long qemu_get_high_memory_size(void) |
| 20 | { |
| 21 | unsigned long high; |
| 22 | outb (HIGH_HIGHRAM_ADDR, CMOS_ADDR_PORT); |
| 23 | high = ((unsigned long) inb(CMOS_DATA_PORT)) << 22; |
| 24 | outb (MID_HIGHRAM_ADDR, CMOS_ADDR_PORT); |
| 25 | high |= ((unsigned long) inb(CMOS_DATA_PORT)) << 14; |
| 26 | outb (LOW_HIGHRAM_ADDR, CMOS_ADDR_PORT); |
| 27 | high |= ((unsigned long) inb(CMOS_DATA_PORT)) << 6; |
| 28 | return high; |
| 29 | } |
| 30 | |
Gerd Hoffmann | 05d3f49 | 2013-08-06 10:48:41 +0200 | [diff] [blame] | 31 | static void qemu_reserve_ports(struct device *dev, unsigned int idx, |
| 32 | unsigned int base, unsigned int size, |
| 33 | const char *name) |
| 34 | { |
| 35 | unsigned int end = base + size -1; |
| 36 | struct resource *res; |
| 37 | |
| 38 | printk(BIOS_DEBUG, "QEMU: reserve ioports 0x%04x-0x%04x [%s]\n", |
| 39 | base, end, name); |
| 40 | res = new_resource(dev, idx); |
| 41 | res->base = base; |
| 42 | res->size = size; |
| 43 | res->limit = 0xffff; |
| 44 | res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_STORED | |
| 45 | IORESOURCE_ASSIGNED; |
| 46 | } |
| 47 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 48 | static void cpu_pci_domain_set_resources(device_t dev) |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 49 | { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 50 | assign_resources(dev->link_list); |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 51 | } |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 52 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 53 | static void cpu_pci_domain_read_resources(struct device *dev) |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 54 | { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 55 | struct resource *res; |
Gerd Hoffmann | 9839a38 | 2013-06-17 12:26:17 +0200 | [diff] [blame] | 56 | unsigned long tomk = 0, high; |
Gerd Hoffmann | 44b11f2 | 2013-06-17 13:30:50 +0200 | [diff] [blame] | 57 | int idx = 10; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 58 | |
| 59 | pci_domain_read_resources(dev); |
| 60 | |
Gerd Hoffmann | 44b11f2 | 2013-06-17 13:30:50 +0200 | [diff] [blame] | 61 | tomk = qemu_get_memory_size(); |
Gerd Hoffmann | 9839a38 | 2013-06-17 12:26:17 +0200 | [diff] [blame] | 62 | high = qemu_get_high_memory_size(); |
| 63 | printk(BIOS_DEBUG, "Detected %lu MiB RAM below 4G.\n", tomk / 1024); |
| 64 | printk(BIOS_DEBUG, "Detected %lu MiB RAM above 4G.\n", high / 1024); |
Gerd Hoffmann | 44b11f2 | 2013-06-17 13:30:50 +0200 | [diff] [blame] | 65 | |
| 66 | /* Report the memory regions. */ |
| 67 | idx = 10; |
| 68 | ram_resource(dev, idx++, 0, 640); |
| 69 | ram_resource(dev, idx++, 768, tomk - 768); |
Gerd Hoffmann | 9839a38 | 2013-06-17 12:26:17 +0200 | [diff] [blame] | 70 | if (high) |
| 71 | ram_resource(dev, idx++, 4 * 1024 * 1024, high); |
Gerd Hoffmann | 44b11f2 | 2013-06-17 13:30:50 +0200 | [diff] [blame] | 72 | |
Gerd Hoffmann | 05d3f49 | 2013-08-06 10:48:41 +0200 | [diff] [blame] | 73 | /* Reserve I/O ports used by QEMU */ |
| 74 | qemu_reserve_ports(dev, idx++, 0x0510, 0x02, "firmware-config"); |
| 75 | qemu_reserve_ports(dev, idx++, 0x5658, 0x01, "vmware-port"); |
| 76 | if (i440fx) { |
| 77 | qemu_reserve_ports(dev, idx++, 0xae00, 0x10, "pci-hotplug"); |
| 78 | qemu_reserve_ports(dev, idx++, 0xaf00, 0x20, "cpu-hotplug"); |
| 79 | qemu_reserve_ports(dev, idx++, 0xafe0, 0x04, "piix4-gpe0"); |
| 80 | } |
| 81 | if (inb(CONFIG_CONSOLE_QEMU_DEBUGCON_PORT) == 0xe9) { |
| 82 | qemu_reserve_ports(dev, idx++, CONFIG_CONSOLE_QEMU_DEBUGCON_PORT, 1, |
| 83 | "debugcon"); |
| 84 | } |
| 85 | |
Denis 'GNUtoo' Carikli | 378d046 | 2013-06-19 08:30:33 +0200 | [diff] [blame] | 86 | #if !CONFIG_DYNAMIC_CBMEM |
Gerd Hoffmann | 44b11f2 | 2013-06-17 13:30:50 +0200 | [diff] [blame] | 87 | /* Leave some space for ACPI, PIRQ and MP tables */ |
| 88 | high_tables_base = (tomk * 1024) - HIGH_MEMORY_SIZE; |
| 89 | high_tables_size = HIGH_MEMORY_SIZE; |
Denis 'GNUtoo' Carikli | 378d046 | 2013-06-19 08:30:33 +0200 | [diff] [blame] | 90 | #endif |
Gerd Hoffmann | 44b11f2 | 2013-06-17 13:30:50 +0200 | [diff] [blame] | 91 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 92 | /* Reserve space for the IOAPIC. This should be in the Southbridge, |
| 93 | * but I couldn't tell which device to put it in. */ |
| 94 | res = new_resource(dev, 2); |
Uwe Hermann | 74d1a6e | 2010-10-12 17:34:08 +0000 | [diff] [blame] | 95 | res->base = IO_APIC_ADDR; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 96 | res->size = 0x100000UL; |
| 97 | res->limit = 0xffffffffUL; |
| 98 | res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED | |
| 99 | IORESOURCE_ASSIGNED; |
| 100 | |
| 101 | /* Reserve space for the LAPIC. There's one in every processor, but |
| 102 | * the space only needs to be reserved once, so we do it here. */ |
| 103 | res = new_resource(dev, 3); |
Patrick Georgi | c8feedd | 2012-02-16 18:43:25 +0100 | [diff] [blame] | 104 | res->base = LOCAL_APIC_ADDR; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 105 | res->size = 0x10000UL; |
| 106 | res->limit = 0xffffffffUL; |
| 107 | res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED | |
| 108 | IORESOURCE_ASSIGNED; |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Sven Schnelle | 164bcfd | 2011-08-14 20:56:34 +0200 | [diff] [blame] | 111 | #if CONFIG_GENERATE_SMBIOS_TABLES |
| 112 | static int qemu_get_smbios_data16(int handle, unsigned long *current) |
| 113 | { |
| 114 | struct smbios_type16 *t = (struct smbios_type16 *)*current; |
| 115 | int len = sizeof(struct smbios_type16); |
| 116 | |
| 117 | memset(t, 0, sizeof(struct smbios_type16)); |
| 118 | t->type = SMBIOS_PHYS_MEMORY_ARRAY; |
| 119 | t->handle = handle; |
| 120 | t->length = len - 2; |
| 121 | t->location = 3; /* Location: System Board */ |
| 122 | t->use = 3; /* System memory */ |
| 123 | t->memory_error_correction = 3; /* No error correction */ |
| 124 | t->maximum_capacity = qemu_get_memory_size(); |
| 125 | *current += len; |
| 126 | return len; |
| 127 | } |
| 128 | |
| 129 | static int qemu_get_smbios_data17(int handle, int parent_handle, unsigned long *current) |
| 130 | { |
| 131 | struct smbios_type17 *t = (struct smbios_type17 *)*current; |
| 132 | int len; |
| 133 | |
| 134 | memset(t, 0, sizeof(struct smbios_type17)); |
| 135 | t->type = SMBIOS_MEMORY_DEVICE; |
| 136 | t->handle = handle; |
| 137 | t->phys_memory_array_handle = parent_handle; |
| 138 | t->length = sizeof(struct smbios_type17) - 2; |
| 139 | t->size = qemu_get_memory_size() / 1024; |
| 140 | t->data_width = 64; |
| 141 | t->total_width = 64; |
| 142 | t->form_factor = 9; /* DIMM */ |
| 143 | t->device_locator = smbios_add_string(t->eos, "Virtual"); |
| 144 | t->memory_type = 0x12; /* DDR */ |
| 145 | t->type_detail = 0x80; /* Synchronous */ |
| 146 | t->speed = 200; |
| 147 | t->clock_speed = 200; |
| 148 | t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR); |
| 149 | len = t->length + smbios_string_table_len(t->eos); |
| 150 | *current += len; |
| 151 | return len; |
| 152 | } |
| 153 | |
| 154 | static int qemu_get_smbios_data(device_t dev, int *handle, unsigned long *current) |
| 155 | { |
| 156 | int len; |
| 157 | len = qemu_get_smbios_data16(*handle, current); |
| 158 | len += qemu_get_smbios_data17(*handle+1, *handle, current); |
| 159 | *handle += 2; |
| 160 | return len; |
| 161 | } |
| 162 | #endif |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 163 | static struct device_operations pci_domain_ops = { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 164 | .read_resources = cpu_pci_domain_read_resources, |
| 165 | .set_resources = cpu_pci_domain_set_resources, |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 166 | .enable_resources = NULL, |
| 167 | .init = NULL, |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 168 | .scan_bus = pci_domain_scan_bus, |
Kyösti Mälkki | 33e5df3 | 2013-07-03 10:51:34 +0300 | [diff] [blame] | 169 | .ops_pci_bus = pci_bus_default_ops, |
Sven Schnelle | 164bcfd | 2011-08-14 20:56:34 +0200 | [diff] [blame] | 170 | #if CONFIG_GENERATE_SMBIOS_TABLES |
| 171 | .get_smbios_data = qemu_get_smbios_data, |
| 172 | #endif |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 173 | }; |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 174 | |
Gerd Hoffmann | aa588e0 | 2013-05-31 09:26:55 +0200 | [diff] [blame] | 175 | static void cpu_bus_init(device_t dev) |
| 176 | { |
| 177 | initialize_cpus(dev->link_list); |
| 178 | } |
| 179 | |
| 180 | static unsigned int cpu_bus_scan(device_t bus, unsigned int max) |
| 181 | { |
| 182 | int max_cpus = fw_cfg_max_cpus(); |
| 183 | device_t cpu; |
| 184 | int i; |
| 185 | |
| 186 | if (max_cpus < 0) |
| 187 | return 0; |
| 188 | |
| 189 | /* |
| 190 | * TODO: This only handles the simple "qemu -smp $nr" case |
| 191 | * correctly. qemu also allows to specify the number of |
| 192 | * cores, threads & sockets. |
| 193 | */ |
| 194 | printk(BIOS_INFO, "QEMU: max_cpus is %d\n", max_cpus); |
| 195 | for (i = 0; i < max_cpus; i++) { |
| 196 | cpu = add_cpu_device(bus->link_list, i, 1); |
| 197 | if (cpu) |
| 198 | set_cpu_topology(cpu, 1, 0, i, 0); |
| 199 | } |
| 200 | return max_cpus; |
| 201 | } |
| 202 | |
| 203 | static void cpu_bus_noop(device_t dev) |
| 204 | { |
| 205 | } |
| 206 | |
| 207 | static struct device_operations cpu_bus_ops = { |
| 208 | .read_resources = cpu_bus_noop, |
| 209 | .set_resources = cpu_bus_noop, |
| 210 | .enable_resources = cpu_bus_noop, |
| 211 | .init = cpu_bus_init, |
| 212 | .scan_bus = cpu_bus_scan, |
| 213 | }; |
| 214 | |
Paul Menzel | 5f20b35 | 2013-02-24 14:27:03 +0100 | [diff] [blame] | 215 | static void northbridge_enable(struct device *dev) |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 216 | { |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 217 | /* Set the operations if it is a special bus type */ |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 218 | if (dev->path.type == DEVICE_PATH_DOMAIN) { |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 219 | dev->ops = &pci_domain_ops; |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 220 | } |
Gerd Hoffmann | aa588e0 | 2013-05-31 09:26:55 +0200 | [diff] [blame] | 221 | else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) { |
| 222 | dev->ops = &cpu_bus_ops; |
| 223 | } |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Gerd Hoffmann | 00cc7f43 | 2013-06-07 15:46:23 +0200 | [diff] [blame] | 226 | struct chip_operations mainboard_emulation_qemu_i440fx_ops = { |
| 227 | CHIP_NAME("QEMU Northbridge i440fx") |
Paul Menzel | 5f20b35 | 2013-02-24 14:27:03 +0100 | [diff] [blame] | 228 | .enable_dev = northbridge_enable, |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 229 | }; |
Gerd Hoffmann | ee941b38 | 2013-06-07 16:03:44 +0200 | [diff] [blame] | 230 | |
| 231 | struct chip_operations mainboard_emulation_qemu_q35_ops = { |
| 232 | CHIP_NAME("QEMU Northbridge q35") |
| 233 | .enable_dev = northbridge_enable, |
| 234 | }; |