Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 1 | // Initialize PCI devices (on emulators) |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2006 Fabrice Bellard |
| 5 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 7 | |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 8 | #include "config.h" // CONFIG_* |
| 9 | #include "dev-q35.h" // Q35_HOST_BRIDGE_PCIEXBAR_ADDR |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 10 | #include "hw/ata.h" // PORT_ATA1_CMD_BASE |
Kevin O'Connor | 5d369d8 | 2013-09-02 20:48:46 -0400 | [diff] [blame] | 11 | #include "hw/pci.h" // pci_config_readl |
| 12 | #include "hw/pci_ids.h" // PCI_VENDOR_ID_INTEL |
| 13 | #include "hw/pci_regs.h" // PCI_COMMAND |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 14 | #include "list.h" // struct hlist_node |
Kevin O'Connor | 9dea590 | 2013-09-14 20:23:54 -0400 | [diff] [blame] | 15 | #include "malloc.h" // free |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 16 | #include "memmap.h" // add_e820 |
| 17 | #include "output.h" // dprintf |
| 18 | #include "paravirt.h" // RamSize |
Kevin O'Connor | fa9c66a | 2013-09-14 19:10:40 -0400 | [diff] [blame] | 19 | #include "string.h" // memset |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 20 | #include "util.h" // pci_setup |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 21 | #include "x86.h" // outb |
Gerd Hoffmann | 0f474d0 | 2013-11-26 12:48:20 +0100 | [diff] [blame] | 22 | #include "byteorder.h" // le64_to_cpu |
| 23 | #include "romfile.h" // romfile_loadint |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 24 | |
Gerd Hoffmann | 67a3c7c | 2013-11-26 13:12:04 +0100 | [diff] [blame] | 25 | #define PCI_DEVICE_MEM_MIN (1<<12) // 4k == page size |
| 26 | #define PCI_BRIDGE_MEM_MIN (1<<21) // 2M == hugepage size |
| 27 | #define PCI_BRIDGE_IO_MIN 0x1000 // mandated by pci bridge spec |
Isaku Yamahata | af0963d | 2010-06-22 17:57:53 +0900 | [diff] [blame] | 28 | |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 29 | enum pci_region_type { |
| 30 | PCI_REGION_TYPE_IO, |
| 31 | PCI_REGION_TYPE_MEM, |
| 32 | PCI_REGION_TYPE_PREFMEM, |
| 33 | PCI_REGION_TYPE_COUNT, |
| 34 | }; |
| 35 | |
| 36 | static const char *region_type_name[] = { |
| 37 | [ PCI_REGION_TYPE_IO ] = "io", |
| 38 | [ PCI_REGION_TYPE_MEM ] = "mem", |
| 39 | [ PCI_REGION_TYPE_PREFMEM ] = "prefmem", |
| 40 | }; |
| 41 | |
Gerd Hoffmann | e55c4e8 | 2012-06-07 10:34:32 +0200 | [diff] [blame] | 42 | u64 pcimem_start = BUILD_PCIMEM_START; |
| 43 | u64 pcimem_end = BUILD_PCIMEM_END; |
| 44 | u64 pcimem64_start = BUILD_PCIMEM64_START; |
| 45 | u64 pcimem64_end = BUILD_PCIMEM64_END; |
| 46 | |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 47 | struct pci_region_entry { |
| 48 | struct pci_device *dev; |
| 49 | int bar; |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 50 | u64 size; |
| 51 | u64 align; |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 52 | int is64; |
| 53 | enum pci_region_type type; |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 54 | struct hlist_node node; |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 55 | }; |
| 56 | |
Alexey Korolev | 35a770f | 2012-04-19 17:47:19 +1200 | [diff] [blame] | 57 | struct pci_region { |
Alexey Korolev | 35a770f | 2012-04-19 17:47:19 +1200 | [diff] [blame] | 58 | /* pci region assignments */ |
| 59 | u64 base; |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 60 | struct hlist_head list; |
Alexey Korolev | 35a770f | 2012-04-19 17:47:19 +1200 | [diff] [blame] | 61 | }; |
| 62 | |
Kevin O'Connor | b725dcb | 2011-10-15 11:53:38 -0400 | [diff] [blame] | 63 | struct pci_bus { |
Alexey Korolev | 35a770f | 2012-04-19 17:47:19 +1200 | [diff] [blame] | 64 | struct pci_region r[PCI_REGION_TYPE_COUNT]; |
Kevin O'Connor | 2c4c211 | 2011-10-15 11:42:48 -0400 | [diff] [blame] | 65 | struct pci_device *bus_dev; |
Kevin O'Connor | b725dcb | 2011-10-15 11:53:38 -0400 | [diff] [blame] | 66 | }; |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 67 | |
Kevin O'Connor | cbbdcf2 | 2011-10-01 13:13:29 -0400 | [diff] [blame] | 68 | static u32 pci_bar(struct pci_device *pci, int region_num) |
Isaku Yamahata | a65821d | 2010-06-22 17:57:50 +0900 | [diff] [blame] | 69 | { |
| 70 | if (region_num != PCI_ROM_SLOT) { |
| 71 | return PCI_BASE_ADDRESS_0 + region_num * 4; |
| 72 | } |
Isaku Yamahata | 5d0de15 | 2010-06-22 17:57:51 +0900 | [diff] [blame] | 73 | |
| 74 | #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80 |
Kevin O'Connor | cbbdcf2 | 2011-10-01 13:13:29 -0400 | [diff] [blame] | 75 | u8 type = pci->header_type & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
Isaku Yamahata | 5d0de15 | 2010-06-22 17:57:51 +0900 | [diff] [blame] | 76 | return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS; |
Isaku Yamahata | a65821d | 2010-06-22 17:57:50 +0900 | [diff] [blame] | 77 | } |
| 78 | |
Kevin O'Connor | cbbdcf2 | 2011-10-01 13:13:29 -0400 | [diff] [blame] | 79 | static void |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 80 | pci_set_io_region_addr(struct pci_device *pci, int bar, u64 addr, int is64) |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 81 | { |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 82 | u32 ofs = pci_bar(pci, bar); |
| 83 | pci_config_writel(pci->bdf, ofs, addr); |
| 84 | if (is64) |
| 85 | pci_config_writel(pci->bdf, ofs + 4, addr >> 32); |
Isaku Yamahata | b9e4721 | 2010-06-22 17:57:47 +0900 | [diff] [blame] | 86 | } |
| 87 | |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 88 | |
| 89 | /**************************************************************** |
| 90 | * Misc. device init |
| 91 | ****************************************************************/ |
| 92 | |
| 93 | /* host irqs corresponding to PCI irqs A-D */ |
| 94 | const u8 pci_irqs[4] = { |
| 95 | 10, 10, 11, 11 |
| 96 | }; |
| 97 | |
Alex Williamson | dbb7a66 | 2013-02-21 09:12:23 -0700 | [diff] [blame] | 98 | static int dummy_pci_slot_get_irq(struct pci_device *pci, int pin) |
| 99 | { |
| 100 | dprintf(1, "pci_slot_get_irq called with unknown routing\n"); |
| 101 | |
| 102 | return 0xff; /* PCI defined "unknown" or "no connection" for x86 */ |
| 103 | } |
| 104 | |
| 105 | static int (*pci_slot_get_irq)(struct pci_device *pci, int pin) = |
| 106 | dummy_pci_slot_get_irq; |
Alex Williamson | b949040 | 2013-02-15 14:11:41 -0700 | [diff] [blame] | 107 | |
Kevin O'Connor | 0ce2138 | 2011-10-01 14:52:35 -0400 | [diff] [blame] | 108 | // Return the global irq number corresponding to a host bus device irq pin. |
Alex Williamson | b949040 | 2013-02-15 14:11:41 -0700 | [diff] [blame] | 109 | static int piix_pci_slot_get_irq(struct pci_device *pci, int pin) |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 110 | { |
Gerd Hoffmann | 0c8f58d | 2012-05-04 17:33:36 +0200 | [diff] [blame] | 111 | int slot_addend = 0; |
| 112 | |
| 113 | while (pci->parent != NULL) { |
| 114 | slot_addend += pci_bdf_to_dev(pci->bdf); |
| 115 | pci = pci->parent; |
| 116 | } |
| 117 | slot_addend += pci_bdf_to_dev(pci->bdf) - 1; |
Kevin O'Connor | 0ce2138 | 2011-10-01 14:52:35 -0400 | [diff] [blame] | 118 | return pci_irqs[(pin - 1 + slot_addend) & 3]; |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Alex Williamson | b949040 | 2013-02-15 14:11:41 -0700 | [diff] [blame] | 121 | static int mch_pci_slot_get_irq(struct pci_device *pci, int pin) |
| 122 | { |
| 123 | int irq, slot, pin_addend = 0; |
| 124 | |
| 125 | while (pci->parent != NULL) { |
| 126 | pin_addend += pci_bdf_to_dev(pci->bdf); |
| 127 | pci = pci->parent; |
| 128 | } |
| 129 | slot = pci_bdf_to_dev(pci->bdf); |
| 130 | |
| 131 | switch (slot) { |
| 132 | /* Slots 0-24 rotate slot:pin mapping similar to piix above, but |
| 133 | with a different starting index - see q35-acpi-dsdt.dsl */ |
| 134 | case 0 ... 24: |
| 135 | irq = pci_irqs[(pin - 1 + pin_addend + slot) & 3]; |
| 136 | break; |
| 137 | /* Slots 25-31 all use LNKA mapping (or LNKE, but A:D = E:H) */ |
| 138 | case 25 ... 31: |
| 139 | irq = pci_irqs[(pin - 1 + pin_addend) & 3]; |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | return irq; |
| 144 | } |
| 145 | |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 146 | /* PIIX3/PIIX4 PCI to ISA bridge */ |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 147 | static void piix_isa_bridge_setup(struct pci_device *pci, void *arg) |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 148 | { |
| 149 | int i, irq; |
| 150 | u8 elcr[2]; |
| 151 | |
| 152 | elcr[0] = 0x00; |
| 153 | elcr[1] = 0x00; |
| 154 | for (i = 0; i < 4; i++) { |
| 155 | irq = pci_irqs[i]; |
| 156 | /* set to trigger level */ |
| 157 | elcr[irq >> 3] |= (1 << (irq & 7)); |
| 158 | /* activate irq remapping in PIIX */ |
Kevin O'Connor | 278b19f | 2011-06-21 22:41:15 -0400 | [diff] [blame] | 159 | pci_config_writeb(pci->bdf, 0x60 + i, irq); |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 160 | } |
| 161 | outb(elcr[0], 0x4d0); |
| 162 | outb(elcr[1], 0x4d1); |
| 163 | dprintf(1, "PIIX3/PIIX4 init: elcr=%02x %02x\n", elcr[0], elcr[1]); |
| 164 | } |
| 165 | |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 166 | /* ICH9 LPC PCI to ISA bridge */ |
| 167 | /* PCI_VENDOR_ID_INTEL && PCI_DEVICE_ID_INTEL_ICH9_LPC */ |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 168 | void mch_isa_bridge_setup(struct pci_device *dev, void *arg) |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 169 | { |
| 170 | u16 bdf = dev->bdf; |
| 171 | int i, irq; |
| 172 | u8 elcr[2]; |
| 173 | |
| 174 | elcr[0] = 0x00; |
| 175 | elcr[1] = 0x00; |
| 176 | |
| 177 | for (i = 0; i < 4; i++) { |
| 178 | irq = pci_irqs[i]; |
| 179 | /* set to trigger level */ |
| 180 | elcr[irq >> 3] |= (1 << (irq & 7)); |
| 181 | |
| 182 | /* activate irq remapping in LPC */ |
| 183 | |
| 184 | /* PIRQ[A-D] routing */ |
Alex Williamson | 555a213 | 2013-02-15 14:11:35 -0700 | [diff] [blame] | 185 | pci_config_writeb(bdf, ICH9_LPC_PIRQA_ROUT + i, irq); |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 186 | /* PIRQ[E-H] routing */ |
Alex Williamson | 555a213 | 2013-02-15 14:11:35 -0700 | [diff] [blame] | 187 | pci_config_writeb(bdf, ICH9_LPC_PIRQE_ROUT + i, irq); |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 188 | } |
| 189 | outb(elcr[0], ICH9_LPC_PORT_ELCR1); |
| 190 | outb(elcr[1], ICH9_LPC_PORT_ELCR2); |
| 191 | dprintf(1, "Q35 LPC init: elcr=%02x %02x\n", elcr[0], elcr[1]); |
| 192 | |
| 193 | /* pm io base */ |
| 194 | pci_config_writel(bdf, ICH9_LPC_PMBASE, |
| 195 | PORT_ACPI_PM_BASE | ICH9_LPC_PMBASE_RTE); |
| 196 | |
| 197 | /* acpi enable, SCI: IRQ9 000b = irq9*/ |
| 198 | pci_config_writeb(bdf, ICH9_LPC_ACPI_CTRL, ICH9_LPC_ACPI_CTRL_ACPI_EN); |
| 199 | |
Gerd Hoffmann | 5b63109 | 2013-07-25 09:47:18 +0200 | [diff] [blame] | 200 | acpi_pm1a_cnt = PORT_ACPI_PM_BASE + 0x04; |
Kevin O'Connor | 118605f | 2013-07-20 11:06:51 -0400 | [diff] [blame] | 201 | pmtimer_setup(PORT_ACPI_PM_BASE + 0x08); |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 204 | static void storage_ide_setup(struct pci_device *pci, void *arg) |
Kevin O'Connor | 0d6b8d5 | 2010-07-10 13:12:37 -0400 | [diff] [blame] | 205 | { |
| 206 | /* IDE: we map it as in ISA mode */ |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 207 | pci_set_io_region_addr(pci, 0, PORT_ATA1_CMD_BASE, 0); |
| 208 | pci_set_io_region_addr(pci, 1, PORT_ATA1_CTRL_BASE, 0); |
| 209 | pci_set_io_region_addr(pci, 2, PORT_ATA2_CMD_BASE, 0); |
| 210 | pci_set_io_region_addr(pci, 3, PORT_ATA2_CTRL_BASE, 0); |
Kevin O'Connor | 0d6b8d5 | 2010-07-10 13:12:37 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 213 | /* PIIX3/PIIX4 IDE */ |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 214 | static void piix_ide_setup(struct pci_device *pci, void *arg) |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 215 | { |
Kevin O'Connor | 278b19f | 2011-06-21 22:41:15 -0400 | [diff] [blame] | 216 | u16 bdf = pci->bdf; |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 217 | pci_config_writew(bdf, 0x40, 0x8000); // enable IDE0 |
| 218 | pci_config_writew(bdf, 0x42, 0x8000); // enable IDE1 |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 221 | static void pic_ibm_setup(struct pci_device *pci, void *arg) |
Kevin O'Connor | 0d6b8d5 | 2010-07-10 13:12:37 -0400 | [diff] [blame] | 222 | { |
| 223 | /* PIC, IBM, MPIC & MPIC2 */ |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 224 | pci_set_io_region_addr(pci, 0, 0x80800000 + 0x00040000, 0); |
Kevin O'Connor | 0d6b8d5 | 2010-07-10 13:12:37 -0400 | [diff] [blame] | 225 | } |
| 226 | |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 227 | static void apple_macio_setup(struct pci_device *pci, void *arg) |
Kevin O'Connor | 0d6b8d5 | 2010-07-10 13:12:37 -0400 | [diff] [blame] | 228 | { |
| 229 | /* macio bridge */ |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 230 | pci_set_io_region_addr(pci, 0, 0x80800000, 0); |
Kevin O'Connor | 0d6b8d5 | 2010-07-10 13:12:37 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Marcel Apfelbaum | 40d020f | 2014-01-15 14:20:06 +0200 | [diff] [blame^] | 233 | static void piix4_pm_config_setup(u16 bdf) |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 234 | { |
| 235 | // acpi sci is hardwired to 9 |
| 236 | pci_config_writeb(bdf, PCI_INTERRUPT_LINE, 9); |
| 237 | |
| 238 | pci_config_writel(bdf, 0x40, PORT_ACPI_PM_BASE | 1); |
| 239 | pci_config_writeb(bdf, 0x80, 0x01); /* enable PM io space */ |
| 240 | pci_config_writel(bdf, 0x90, PORT_SMB_BASE | 1); |
| 241 | pci_config_writeb(bdf, 0xd2, 0x09); /* enable SMBus io space */ |
Marcel Apfelbaum | 40d020f | 2014-01-15 14:20:06 +0200 | [diff] [blame^] | 242 | } |
| 243 | |
| 244 | static int PiixPmBDF = -1; |
| 245 | |
| 246 | /* PIIX4 Power Management device (for ACPI) */ |
| 247 | static void piix4_pm_setup(struct pci_device *pci, void *arg) |
| 248 | { |
| 249 | PiixPmBDF = pci->bdf; |
| 250 | piix4_pm_config_setup(pci->bdf); |
Gerd Hoffmann | 455a7c8 | 2012-09-06 08:01:00 +0200 | [diff] [blame] | 251 | |
Gerd Hoffmann | 5b63109 | 2013-07-25 09:47:18 +0200 | [diff] [blame] | 252 | acpi_pm1a_cnt = PORT_ACPI_PM_BASE + 0x04; |
Kevin O'Connor | 118605f | 2013-07-20 11:06:51 -0400 | [diff] [blame] | 253 | pmtimer_setup(PORT_ACPI_PM_BASE + 0x08); |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 254 | } |
| 255 | |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 256 | /* ICH9 SMBUS */ |
| 257 | /* PCI_VENDOR_ID_INTEL && PCI_DEVICE_ID_INTEL_ICH9_SMBUS */ |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 258 | void ich9_smbus_setup(struct pci_device *dev, void *arg) |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 259 | { |
| 260 | u16 bdf = dev->bdf; |
| 261 | /* map smbus into io space */ |
| 262 | pci_config_writel(bdf, ICH9_SMB_SMB_BASE, |
| 263 | PORT_SMB_BASE | PCI_BASE_ADDRESS_SPACE_IO); |
| 264 | |
| 265 | /* enable SMBus */ |
| 266 | pci_config_writeb(bdf, ICH9_SMB_HOSTC, ICH9_SMB_HOSTC_HST_EN); |
| 267 | } |
| 268 | |
Kevin O'Connor | 0d6b8d5 | 2010-07-10 13:12:37 -0400 | [diff] [blame] | 269 | static const struct pci_device_id pci_device_tbl[] = { |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 270 | /* PIIX3/PIIX4 PCI to ISA bridge */ |
| 271 | PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_0, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 272 | piix_isa_bridge_setup), |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 273 | PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_0, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 274 | piix_isa_bridge_setup), |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 275 | PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_LPC, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 276 | mch_isa_bridge_setup), |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 277 | |
| 278 | /* STORAGE IDE */ |
| 279 | PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 280 | PCI_CLASS_STORAGE_IDE, piix_ide_setup), |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 281 | PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 282 | PCI_CLASS_STORAGE_IDE, piix_ide_setup), |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 283 | PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 284 | storage_ide_setup), |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 285 | |
| 286 | /* PIC, IBM, MIPC & MPIC2 */ |
| 287 | PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0x0046, PCI_CLASS_SYSTEM_PIC, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 288 | pic_ibm_setup), |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 289 | PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0xFFFF, PCI_CLASS_SYSTEM_PIC, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 290 | pic_ibm_setup), |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 291 | |
Kevin O'Connor | 0d6b8d5 | 2010-07-10 13:12:37 -0400 | [diff] [blame] | 292 | /* PIIX4 Power Management device (for ACPI) */ |
| 293 | PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 294 | piix4_pm_setup), |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 295 | PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_SMBUS, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 296 | ich9_smbus_setup), |
Kevin O'Connor | 0d6b8d5 | 2010-07-10 13:12:37 -0400 | [diff] [blame] | 297 | |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 298 | /* 0xff00 */ |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 299 | PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0017, 0xff00, apple_macio_setup), |
| 300 | PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0022, 0xff00, apple_macio_setup), |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 301 | |
Kevin O'Connor | 0d6b8d5 | 2010-07-10 13:12:37 -0400 | [diff] [blame] | 302 | PCI_DEVICE_END, |
| 303 | }; |
| 304 | |
Marcel Apfelbaum | 40d020f | 2014-01-15 14:20:06 +0200 | [diff] [blame^] | 305 | void pci_resume(void) |
| 306 | { |
| 307 | if (!CONFIG_QEMU) { |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | if (PiixPmBDF >= 0) { |
| 312 | piix4_pm_config_setup(PiixPmBDF); |
| 313 | } |
| 314 | } |
| 315 | |
Kevin O'Connor | 278b19f | 2011-06-21 22:41:15 -0400 | [diff] [blame] | 316 | static void pci_bios_init_device(struct pci_device *pci) |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 317 | { |
Kevin O'Connor | 278b19f | 2011-06-21 22:41:15 -0400 | [diff] [blame] | 318 | u16 bdf = pci->bdf; |
Kevin O'Connor | 99e37c4 | 2011-10-01 10:47:21 -0400 | [diff] [blame] | 319 | dprintf(1, "PCI: init bdf=%02x:%02x.%x id=%04x:%04x\n" |
| 320 | , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf) |
Kevin O'Connor | 278b19f | 2011-06-21 22:41:15 -0400 | [diff] [blame] | 321 | , pci->vendor, pci->device); |
Kevin O'Connor | 0ce2138 | 2011-10-01 14:52:35 -0400 | [diff] [blame] | 322 | |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 323 | /* map the interrupt */ |
Kevin O'Connor | 0ce2138 | 2011-10-01 14:52:35 -0400 | [diff] [blame] | 324 | int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN); |
| 325 | if (pin != 0) |
Gerd Hoffmann | 0c8f58d | 2012-05-04 17:33:36 +0200 | [diff] [blame] | 326 | pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pci_slot_get_irq(pci, pin)); |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 327 | |
Kevin O'Connor | 278b19f | 2011-06-21 22:41:15 -0400 | [diff] [blame] | 328 | pci_init_device(pci_device_tbl, pci, NULL); |
Kevin O'Connor | 31dcfb0 | 2012-11-20 20:29:26 -0500 | [diff] [blame] | 329 | |
| 330 | /* enable memory mappings */ |
Isaku Yamahata | d146ab8 | 2012-11-28 10:17:32 +0100 | [diff] [blame] | 331 | pci_config_maskw(bdf, PCI_COMMAND, 0, |
| 332 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_SERR); |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Kevin O'Connor | 3f2288f | 2011-10-15 12:02:14 -0400 | [diff] [blame] | 335 | static void pci_bios_init_devices(void) |
Isaku Yamahata | af0963d | 2010-06-22 17:57:53 +0900 | [diff] [blame] | 336 | { |
Kevin O'Connor | 278b19f | 2011-06-21 22:41:15 -0400 | [diff] [blame] | 337 | struct pci_device *pci; |
| 338 | foreachpci(pci) { |
Kevin O'Connor | 278b19f | 2011-06-21 22:41:15 -0400 | [diff] [blame] | 339 | pci_bios_init_device(pci); |
Isaku Yamahata | af0963d | 2010-06-22 17:57:53 +0900 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Alex Williamson | 7adfd71 | 2013-03-20 10:58:47 -0600 | [diff] [blame] | 343 | static void pci_enable_default_vga(void) |
| 344 | { |
| 345 | struct pci_device *pci; |
| 346 | |
| 347 | foreachpci(pci) { |
| 348 | if (is_pci_vga(pci)) { |
| 349 | dprintf(1, "PCI: Using %02x:%02x.%x for primary VGA\n", |
| 350 | pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf), |
| 351 | pci_bdf_to_fn(pci->bdf)); |
| 352 | return; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | pci = pci_find_class(PCI_CLASS_DISPLAY_VGA); |
| 357 | if (!pci) { |
| 358 | dprintf(1, "PCI: No VGA devices found\n"); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | dprintf(1, "PCI: Enabling %02x:%02x.%x for primary VGA\n", |
| 363 | pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf), |
| 364 | pci_bdf_to_fn(pci->bdf)); |
| 365 | |
| 366 | pci_config_maskw(pci->bdf, PCI_COMMAND, 0, |
| 367 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY); |
| 368 | |
| 369 | while (pci->parent) { |
| 370 | pci = pci->parent; |
| 371 | |
| 372 | dprintf(1, "PCI: Setting VGA enable on bridge %02x:%02x.%x\n", |
| 373 | pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf), |
| 374 | pci_bdf_to_fn(pci->bdf)); |
| 375 | |
| 376 | pci_config_maskw(pci->bdf, PCI_BRIDGE_CONTROL, 0, PCI_BRIDGE_CTL_VGA); |
| 377 | pci_config_maskw(pci->bdf, PCI_COMMAND, 0, |
| 378 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY); |
| 379 | } |
| 380 | } |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 381 | |
| 382 | /**************************************************************** |
Kevin O'Connor | b1c35f2 | 2012-11-26 11:05:32 -0500 | [diff] [blame] | 383 | * Platform device initialization |
| 384 | ****************************************************************/ |
| 385 | |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 386 | void i440fx_mem_addr_setup(struct pci_device *dev, void *arg) |
Kevin O'Connor | b1c35f2 | 2012-11-26 11:05:32 -0500 | [diff] [blame] | 387 | { |
| 388 | if (RamSize <= 0x80000000) |
| 389 | pcimem_start = 0x80000000; |
| 390 | else if (RamSize <= 0xc0000000) |
| 391 | pcimem_start = 0xc0000000; |
Alex Williamson | b949040 | 2013-02-15 14:11:41 -0700 | [diff] [blame] | 392 | |
| 393 | pci_slot_get_irq = piix_pci_slot_get_irq; |
Kevin O'Connor | b1c35f2 | 2012-11-26 11:05:32 -0500 | [diff] [blame] | 394 | } |
| 395 | |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 396 | void mch_mem_addr_setup(struct pci_device *dev, void *arg) |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 397 | { |
| 398 | u64 addr = Q35_HOST_BRIDGE_PCIEXBAR_ADDR; |
| 399 | u32 size = Q35_HOST_BRIDGE_PCIEXBAR_SIZE; |
| 400 | |
| 401 | /* setup mmconfig */ |
| 402 | u16 bdf = dev->bdf; |
| 403 | u32 upper = addr >> 32; |
| 404 | u32 lower = (addr & 0xffffffff) | Q35_HOST_BRIDGE_PCIEXBAREN; |
| 405 | pci_config_writel(bdf, Q35_HOST_BRIDGE_PCIEXBAR, 0); |
| 406 | pci_config_writel(bdf, Q35_HOST_BRIDGE_PCIEXBAR + 4, upper); |
| 407 | pci_config_writel(bdf, Q35_HOST_BRIDGE_PCIEXBAR, lower); |
| 408 | add_e820(addr, size, E820_RESERVED); |
| 409 | |
| 410 | /* setup pci i/o window (above mmconfig) */ |
| 411 | pcimem_start = addr + size; |
Alex Williamson | b949040 | 2013-02-15 14:11:41 -0700 | [diff] [blame] | 412 | |
| 413 | pci_slot_get_irq = mch_pci_slot_get_irq; |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Kevin O'Connor | b1c35f2 | 2012-11-26 11:05:32 -0500 | [diff] [blame] | 416 | static const struct pci_device_id pci_platform_tbl[] = { |
| 417 | PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 418 | i440fx_mem_addr_setup), |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 419 | PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_Q35_MCH, |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 420 | mch_mem_addr_setup), |
Kevin O'Connor | b1c35f2 | 2012-11-26 11:05:32 -0500 | [diff] [blame] | 421 | PCI_DEVICE_END |
| 422 | }; |
| 423 | |
| 424 | static void pci_bios_init_platform(void) |
| 425 | { |
| 426 | struct pci_device *pci; |
| 427 | foreachpci(pci) { |
| 428 | pci_init_device(pci_platform_tbl, pci, NULL); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /**************************************************************** |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 434 | * Bus initialization |
| 435 | ****************************************************************/ |
| 436 | |
Isaku Yamahata | f441666 | 2010-06-22 17:57:52 +0900 | [diff] [blame] | 437 | static void |
| 438 | pci_bios_init_bus_rec(int bus, u8 *pci_bus) |
| 439 | { |
Kevin O'Connor | 2b333e4 | 2011-07-02 14:49:41 -0400 | [diff] [blame] | 440 | int bdf; |
Isaku Yamahata | f441666 | 2010-06-22 17:57:52 +0900 | [diff] [blame] | 441 | u16 class; |
| 442 | |
| 443 | dprintf(1, "PCI: %s bus = 0x%x\n", __func__, bus); |
| 444 | |
| 445 | /* prevent accidental access to unintended devices */ |
Kevin O'Connor | 2b333e4 | 2011-07-02 14:49:41 -0400 | [diff] [blame] | 446 | foreachbdf(bdf, bus) { |
Isaku Yamahata | f441666 | 2010-06-22 17:57:52 +0900 | [diff] [blame] | 447 | class = pci_config_readw(bdf, PCI_CLASS_DEVICE); |
| 448 | if (class == PCI_CLASS_BRIDGE_PCI) { |
| 449 | pci_config_writeb(bdf, PCI_SECONDARY_BUS, 255); |
| 450 | pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 0); |
| 451 | } |
| 452 | } |
| 453 | |
Kevin O'Connor | 2b333e4 | 2011-07-02 14:49:41 -0400 | [diff] [blame] | 454 | foreachbdf(bdf, bus) { |
Isaku Yamahata | f441666 | 2010-06-22 17:57:52 +0900 | [diff] [blame] | 455 | class = pci_config_readw(bdf, PCI_CLASS_DEVICE); |
| 456 | if (class != PCI_CLASS_BRIDGE_PCI) { |
| 457 | continue; |
| 458 | } |
| 459 | dprintf(1, "PCI: %s bdf = 0x%x\n", __func__, bdf); |
| 460 | |
| 461 | u8 pribus = pci_config_readb(bdf, PCI_PRIMARY_BUS); |
| 462 | if (pribus != bus) { |
| 463 | dprintf(1, "PCI: primary bus = 0x%x -> 0x%x\n", pribus, bus); |
| 464 | pci_config_writeb(bdf, PCI_PRIMARY_BUS, bus); |
| 465 | } else { |
| 466 | dprintf(1, "PCI: primary bus = 0x%x\n", pribus); |
| 467 | } |
| 468 | |
| 469 | u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS); |
| 470 | (*pci_bus)++; |
| 471 | if (*pci_bus != secbus) { |
| 472 | dprintf(1, "PCI: secondary bus = 0x%x -> 0x%x\n", |
| 473 | secbus, *pci_bus); |
| 474 | secbus = *pci_bus; |
| 475 | pci_config_writeb(bdf, PCI_SECONDARY_BUS, secbus); |
| 476 | } else { |
| 477 | dprintf(1, "PCI: secondary bus = 0x%x\n", secbus); |
| 478 | } |
| 479 | |
| 480 | /* set to max for access to all subordinate buses. |
| 481 | later set it to accurate value */ |
| 482 | u8 subbus = pci_config_readb(bdf, PCI_SUBORDINATE_BUS); |
| 483 | pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 255); |
| 484 | |
| 485 | pci_bios_init_bus_rec(secbus, pci_bus); |
| 486 | |
| 487 | if (subbus != *pci_bus) { |
| 488 | dprintf(1, "PCI: subordinate bus = 0x%x -> 0x%x\n", |
| 489 | subbus, *pci_bus); |
| 490 | subbus = *pci_bus; |
| 491 | } else { |
| 492 | dprintf(1, "PCI: subordinate bus = 0x%x\n", subbus); |
| 493 | } |
| 494 | pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, subbus); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static void |
| 499 | pci_bios_init_bus(void) |
| 500 | { |
| 501 | u8 pci_bus = 0; |
| 502 | pci_bios_init_bus_rec(0 /* host bus */, &pci_bus); |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 505 | |
| 506 | /**************************************************************** |
| 507 | * Bus sizing |
| 508 | ****************************************************************/ |
| 509 | |
Kevin O'Connor | cbbdcf2 | 2011-10-01 13:13:29 -0400 | [diff] [blame] | 510 | static void |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 511 | pci_bios_get_bar(struct pci_device *pci, int bar, |
| 512 | int *ptype, u64 *psize, int *pis64) |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 513 | { |
Kevin O'Connor | cbbdcf2 | 2011-10-01 13:13:29 -0400 | [diff] [blame] | 514 | u32 ofs = pci_bar(pci, bar); |
| 515 | u16 bdf = pci->bdf; |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 516 | u32 old = pci_config_readl(bdf, ofs); |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 517 | int is64 = 0, type = PCI_REGION_TYPE_MEM; |
| 518 | u64 mask; |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 519 | |
| 520 | if (bar == PCI_ROM_SLOT) { |
| 521 | mask = PCI_ROM_ADDRESS_MASK; |
| 522 | pci_config_writel(bdf, ofs, mask); |
| 523 | } else { |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 524 | if (old & PCI_BASE_ADDRESS_SPACE_IO) { |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 525 | mask = PCI_BASE_ADDRESS_IO_MASK; |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 526 | type = PCI_REGION_TYPE_IO; |
| 527 | } else { |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 528 | mask = PCI_BASE_ADDRESS_MEM_MASK; |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 529 | if (old & PCI_BASE_ADDRESS_MEM_PREFETCH) |
| 530 | type = PCI_REGION_TYPE_PREFMEM; |
| 531 | is64 = ((old & PCI_BASE_ADDRESS_MEM_TYPE_MASK) |
| 532 | == PCI_BASE_ADDRESS_MEM_TYPE_64); |
| 533 | } |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 534 | pci_config_writel(bdf, ofs, ~0); |
| 535 | } |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 536 | u64 val = pci_config_readl(bdf, ofs); |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 537 | pci_config_writel(bdf, ofs, old); |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 538 | if (is64) { |
| 539 | u32 hold = pci_config_readl(bdf, ofs + 4); |
| 540 | pci_config_writel(bdf, ofs + 4, ~0); |
| 541 | u32 high = pci_config_readl(bdf, ofs + 4); |
| 542 | pci_config_writel(bdf, ofs + 4, hold); |
| 543 | val |= ((u64)high << 32); |
| 544 | mask |= ((u64)0xffffffff << 32); |
| 545 | *psize = (~(val & mask)) + 1; |
| 546 | } else { |
| 547 | *psize = ((~(val & mask)) + 1) & 0xffffffff; |
| 548 | } |
| 549 | *ptype = type; |
| 550 | *pis64 = is64; |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Alexey Korolev | ac0cd58 | 2012-04-19 17:48:54 +1200 | [diff] [blame] | 553 | static int pci_bios_bridge_region_is64(struct pci_region *r, |
| 554 | struct pci_device *pci, int type) |
| 555 | { |
| 556 | if (type != PCI_REGION_TYPE_PREFMEM) |
| 557 | return 0; |
| 558 | u32 pmem = pci_config_readl(pci->bdf, PCI_PREF_MEMORY_BASE); |
| 559 | if (!pmem) { |
| 560 | pci_config_writel(pci->bdf, PCI_PREF_MEMORY_BASE, 0xfff0fff0); |
| 561 | pmem = pci_config_readl(pci->bdf, PCI_PREF_MEMORY_BASE); |
| 562 | pci_config_writel(pci->bdf, PCI_PREF_MEMORY_BASE, 0x0); |
| 563 | } |
| 564 | if ((pmem & PCI_PREF_RANGE_TYPE_MASK) != PCI_PREF_RANGE_TYPE_64) |
| 565 | return 0; |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 566 | struct pci_region_entry *entry; |
| 567 | hlist_for_each_entry(entry, &r->list, node) { |
Alexey Korolev | ac0cd58 | 2012-04-19 17:48:54 +1200 | [diff] [blame] | 568 | if (!entry->is64) |
| 569 | return 0; |
Alexey Korolev | ac0cd58 | 2012-04-19 17:48:54 +1200 | [diff] [blame] | 570 | } |
| 571 | return 1; |
| 572 | } |
| 573 | |
Alexey Korolev | 37c111f | 2012-04-26 16:51:05 +1200 | [diff] [blame] | 574 | static u64 pci_region_align(struct pci_region *r) |
| 575 | { |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 576 | struct pci_region_entry *entry; |
| 577 | hlist_for_each_entry(entry, &r->list, node) { |
| 578 | // The first entry in the sorted list has the largest alignment |
| 579 | return entry->align; |
| 580 | } |
| 581 | return 1; |
Alexey Korolev | 37c111f | 2012-04-26 16:51:05 +1200 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static u64 pci_region_sum(struct pci_region *r) |
| 585 | { |
Alexey Korolev | 37c111f | 2012-04-26 16:51:05 +1200 | [diff] [blame] | 586 | u64 sum = 0; |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 587 | struct pci_region_entry *entry; |
| 588 | hlist_for_each_entry(entry, &r->list, node) { |
Alexey Korolev | 37c111f | 2012-04-26 16:51:05 +1200 | [diff] [blame] | 589 | sum += entry->size; |
Kevin O'Connor | e5d71ca | 2012-04-26 22:04:34 -0400 | [diff] [blame] | 590 | } |
| 591 | return sum; |
Alexey Korolev | 37c111f | 2012-04-26 16:51:05 +1200 | [diff] [blame] | 592 | } |
| 593 | |
Alexey Korolev | e5e5f96 | 2012-04-26 17:01:59 +1200 | [diff] [blame] | 594 | static void pci_region_migrate_64bit_entries(struct pci_region *from, |
| 595 | struct pci_region *to) |
| 596 | { |
Kevin O'Connor | 030a58a | 2013-06-13 21:24:14 -0400 | [diff] [blame] | 597 | struct hlist_node *n, **last = &to->list.first; |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 598 | struct pci_region_entry *entry; |
Kevin O'Connor | 030a58a | 2013-06-13 21:24:14 -0400 | [diff] [blame] | 599 | hlist_for_each_entry_safe(entry, n, &from->list, node) { |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 600 | if (!entry->is64) |
Kevin O'Connor | d630d14 | 2012-04-26 22:20:56 -0400 | [diff] [blame] | 601 | continue; |
Gerd Hoffmann | a247e67 | 2013-11-26 12:57:19 +0100 | [diff] [blame] | 602 | if (entry->dev->class == PCI_CLASS_SERIAL_USB) |
| 603 | continue; |
Kevin O'Connor | d630d14 | 2012-04-26 22:20:56 -0400 | [diff] [blame] | 604 | // Move from source list to destination list. |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 605 | hlist_del(&entry->node); |
| 606 | hlist_add(&entry->node, last); |
Gerd Hoffmann | 95c5afc | 2013-11-26 11:21:23 +0100 | [diff] [blame] | 607 | last = &entry->node.next; |
Alexey Korolev | e5e5f96 | 2012-04-26 17:01:59 +1200 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 611 | static struct pci_region_entry * |
| 612 | pci_region_create_entry(struct pci_bus *bus, struct pci_device *dev, |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 613 | int bar, u64 size, u64 align, int type, int is64) |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 614 | { |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 615 | struct pci_region_entry *entry = malloc_tmp(sizeof(*entry)); |
| 616 | if (!entry) { |
| 617 | warn_noalloc(); |
| 618 | return NULL; |
| 619 | } |
| 620 | memset(entry, 0, sizeof(*entry)); |
| 621 | entry->dev = dev; |
| 622 | entry->bar = bar; |
| 623 | entry->size = size; |
Kevin O'Connor | 3d1bc9d | 2012-04-01 12:30:32 -0400 | [diff] [blame] | 624 | entry->align = align; |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 625 | entry->is64 = is64; |
| 626 | entry->type = type; |
| 627 | // Insert into list in sorted order. |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 628 | struct hlist_node **pprev; |
| 629 | struct pci_region_entry *pos; |
Kevin O'Connor | 030a58a | 2013-06-13 21:24:14 -0400 | [diff] [blame] | 630 | hlist_for_each_entry_pprev(pos, pprev, &bus->r[type].list, node) { |
Kevin O'Connor | 3d1bc9d | 2012-04-01 12:30:32 -0400 | [diff] [blame] | 631 | if (pos->align < align || (pos->align == align && pos->size < size)) |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 632 | break; |
| 633 | } |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 634 | hlist_add(&entry->node, pprev); |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 635 | return entry; |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 638 | static int pci_bios_check_devices(struct pci_bus *busses) |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 639 | { |
Kevin O'Connor | 2c4c211 | 2011-10-15 11:42:48 -0400 | [diff] [blame] | 640 | dprintf(1, "PCI: check devices\n"); |
| 641 | |
| 642 | // Calculate resources needed for regular (non-bus) devices. |
| 643 | struct pci_device *pci; |
| 644 | foreachpci(pci) { |
Alexey Korolev | 1a9f47f | 2012-04-19 17:40:13 +1200 | [diff] [blame] | 645 | if (pci->class == PCI_CLASS_BRIDGE_PCI) |
Kevin O'Connor | 2c4c211 | 2011-10-15 11:42:48 -0400 | [diff] [blame] | 646 | busses[pci->secondary_bus].bus_dev = pci; |
Alexey Korolev | 1a9f47f | 2012-04-19 17:40:13 +1200 | [diff] [blame] | 647 | |
Kevin O'Connor | 2c4c211 | 2011-10-15 11:42:48 -0400 | [diff] [blame] | 648 | struct pci_bus *bus = &busses[pci_bdf_to_bus(pci->bdf)]; |
| 649 | int i; |
| 650 | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
Alexey Korolev | 1a9f47f | 2012-04-19 17:40:13 +1200 | [diff] [blame] | 651 | if ((pci->class == PCI_CLASS_BRIDGE_PCI) && |
| 652 | (i >= PCI_BRIDGE_NUM_REGIONS && i < PCI_ROM_SLOT)) |
| 653 | continue; |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 654 | int type, is64; |
| 655 | u64 size; |
| 656 | pci_bios_get_bar(pci, i, &type, &size, &is64); |
| 657 | if (size == 0) |
Kevin O'Connor | 2c4c211 | 2011-10-15 11:42:48 -0400 | [diff] [blame] | 658 | continue; |
| 659 | |
Alexey Korolev | 5fa24b5 | 2012-04-18 17:31:58 +1200 | [diff] [blame] | 660 | if (type != PCI_REGION_TYPE_IO && size < PCI_DEVICE_MEM_MIN) |
| 661 | size = PCI_DEVICE_MEM_MIN; |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 662 | struct pci_region_entry *entry = pci_region_create_entry( |
Kevin O'Connor | 3d1bc9d | 2012-04-01 12:30:32 -0400 | [diff] [blame] | 663 | bus, pci, i, size, size, type, is64); |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 664 | if (!entry) |
| 665 | return -1; |
Kevin O'Connor | 2c4c211 | 2011-10-15 11:42:48 -0400 | [diff] [blame] | 666 | |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 667 | if (is64) |
Kevin O'Connor | 2c4c211 | 2011-10-15 11:42:48 -0400 | [diff] [blame] | 668 | i++; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | // Propagate required bus resources to parent busses. |
| 673 | int secondary_bus; |
| 674 | for (secondary_bus=MaxPCIBus; secondary_bus>0; secondary_bus--) { |
| 675 | struct pci_bus *s = &busses[secondary_bus]; |
| 676 | if (!s->bus_dev) |
| 677 | continue; |
| 678 | struct pci_bus *parent = &busses[pci_bdf_to_bus(s->bus_dev->bdf)]; |
Kevin O'Connor | cbbdcf2 | 2011-10-01 13:13:29 -0400 | [diff] [blame] | 679 | int type; |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 680 | for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) { |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 681 | u64 align = (type == PCI_REGION_TYPE_IO) ? |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 682 | PCI_BRIDGE_IO_MIN : PCI_BRIDGE_MEM_MIN; |
Alexey Korolev | 37c111f | 2012-04-26 16:51:05 +1200 | [diff] [blame] | 683 | if (pci_region_align(&s->r[type]) > align) |
| 684 | align = pci_region_align(&s->r[type]); |
| 685 | u64 sum = pci_region_sum(&s->r[type]); |
| 686 | u64 size = ALIGN(sum, align); |
Alexey Korolev | ac0cd58 | 2012-04-19 17:48:54 +1200 | [diff] [blame] | 687 | int is64 = pci_bios_bridge_region_is64(&s->r[type], |
| 688 | s->bus_dev, type); |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 689 | // entry->bar is -1 if the entry represents a bridge region |
| 690 | struct pci_region_entry *entry = pci_region_create_entry( |
Alexey Korolev | ac0cd58 | 2012-04-19 17:48:54 +1200 | [diff] [blame] | 691 | parent, s->bus_dev, -1, size, align, type, is64); |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 692 | if (!entry) |
| 693 | return -1; |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 694 | dprintf(1, "PCI: secondary bus %d size %08llx type %s\n", |
Alexey Korolev | f3c2b06 | 2012-04-18 17:26:43 +1200 | [diff] [blame] | 695 | entry->dev->secondary_bus, size, |
| 696 | region_type_name[entry->type]); |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 697 | } |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 698 | } |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 699 | return 0; |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 700 | } |
| 701 | |
Kevin O'Connor | e5d71ca | 2012-04-26 22:04:34 -0400 | [diff] [blame] | 702 | |
| 703 | /**************************************************************** |
| 704 | * BAR assignment |
| 705 | ****************************************************************/ |
| 706 | |
Kevin O'Connor | a8dcc5b | 2011-10-01 12:08:57 -0400 | [diff] [blame] | 707 | // Setup region bases (given the regions' size and alignment) |
Alexey Korolev | e5e5f96 | 2012-04-26 17:01:59 +1200 | [diff] [blame] | 708 | static int pci_bios_init_root_regions(struct pci_bus *bus) |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 709 | { |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 710 | bus->r[PCI_REGION_TYPE_IO].base = 0xc000; |
| 711 | |
Alexey Korolev | 37c111f | 2012-04-26 16:51:05 +1200 | [diff] [blame] | 712 | struct pci_region *r_end = &bus->r[PCI_REGION_TYPE_PREFMEM]; |
| 713 | struct pci_region *r_start = &bus->r[PCI_REGION_TYPE_MEM]; |
| 714 | |
| 715 | if (pci_region_align(r_start) < pci_region_align(r_end)) { |
Kevin O'Connor | 3d1bc9d | 2012-04-01 12:30:32 -0400 | [diff] [blame] | 716 | // Swap regions to improve alignment. |
Alexey Korolev | 37c111f | 2012-04-26 16:51:05 +1200 | [diff] [blame] | 717 | r_end = r_start; |
| 718 | r_start = &bus->r[PCI_REGION_TYPE_PREFMEM]; |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 719 | } |
Alexey Korolev | 37c111f | 2012-04-26 16:51:05 +1200 | [diff] [blame] | 720 | u64 sum = pci_region_sum(r_end); |
| 721 | u64 align = pci_region_align(r_end); |
Gerd Hoffmann | e55c4e8 | 2012-06-07 10:34:32 +0200 | [diff] [blame] | 722 | r_end->base = ALIGN_DOWN((pcimem_end - sum), align); |
Alexey Korolev | 37c111f | 2012-04-26 16:51:05 +1200 | [diff] [blame] | 723 | sum = pci_region_sum(r_start); |
| 724 | align = pci_region_align(r_start); |
| 725 | r_start->base = ALIGN_DOWN((r_end->base - sum), align); |
| 726 | |
Gerd Hoffmann | e55c4e8 | 2012-06-07 10:34:32 +0200 | [diff] [blame] | 727 | if ((r_start->base < pcimem_start) || |
| 728 | (r_start->base > pcimem_end)) |
Kevin O'Connor | a8dcc5b | 2011-10-01 12:08:57 -0400 | [diff] [blame] | 729 | // Memory range requested is larger than available. |
Alexey Korolev | e5e5f96 | 2012-04-26 17:01:59 +1200 | [diff] [blame] | 730 | return -1; |
| 731 | return 0; |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 732 | } |
| 733 | |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 734 | #define PCI_IO_SHIFT 8 |
| 735 | #define PCI_MEMORY_SHIFT 16 |
| 736 | #define PCI_PREF_MEMORY_SHIFT 16 |
| 737 | |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 738 | static void |
Alexey Korolev | 35a770f | 2012-04-19 17:47:19 +1200 | [diff] [blame] | 739 | pci_region_map_one_entry(struct pci_region_entry *entry, u64 addr) |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 740 | { |
| 741 | u16 bdf = entry->dev->bdf; |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 742 | if (entry->bar >= 0) { |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 743 | dprintf(1, "PCI: map device bdf=%02x:%02x.%x" |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 744 | " bar %d, addr %08llx, size %08llx [%s]\n", |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 745 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf), |
| 746 | entry->bar, addr, entry->size, region_type_name[entry->type]); |
| 747 | |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 748 | pci_set_io_region_addr(entry->dev, entry->bar, addr, entry->is64); |
Alexey Korolev | 3a29716 | 2012-04-18 17:22:29 +1200 | [diff] [blame] | 749 | return; |
| 750 | } |
| 751 | |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 752 | u64 limit = addr + entry->size - 1; |
Alexey Korolev | 3a29716 | 2012-04-18 17:22:29 +1200 | [diff] [blame] | 753 | if (entry->type == PCI_REGION_TYPE_IO) { |
| 754 | pci_config_writeb(bdf, PCI_IO_BASE, addr >> PCI_IO_SHIFT); |
| 755 | pci_config_writew(bdf, PCI_IO_BASE_UPPER16, 0); |
| 756 | pci_config_writeb(bdf, PCI_IO_LIMIT, limit >> PCI_IO_SHIFT); |
| 757 | pci_config_writew(bdf, PCI_IO_LIMIT_UPPER16, 0); |
| 758 | } |
| 759 | if (entry->type == PCI_REGION_TYPE_MEM) { |
| 760 | pci_config_writew(bdf, PCI_MEMORY_BASE, addr >> PCI_MEMORY_SHIFT); |
| 761 | pci_config_writew(bdf, PCI_MEMORY_LIMIT, limit >> PCI_MEMORY_SHIFT); |
| 762 | } |
| 763 | if (entry->type == PCI_REGION_TYPE_PREFMEM) { |
| 764 | pci_config_writew(bdf, PCI_PREF_MEMORY_BASE, addr >> PCI_PREF_MEMORY_SHIFT); |
| 765 | pci_config_writew(bdf, PCI_PREF_MEMORY_LIMIT, limit >> PCI_PREF_MEMORY_SHIFT); |
Alexey Korolev | 030288f | 2012-04-19 17:44:55 +1200 | [diff] [blame] | 766 | pci_config_writel(bdf, PCI_PREF_BASE_UPPER32, addr >> 32); |
| 767 | pci_config_writel(bdf, PCI_PREF_LIMIT_UPPER32, limit >> 32); |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
Alexey Korolev | 35a770f | 2012-04-19 17:47:19 +1200 | [diff] [blame] | 771 | static void pci_region_map_entries(struct pci_bus *busses, struct pci_region *r) |
| 772 | { |
Kevin O'Connor | 030a58a | 2013-06-13 21:24:14 -0400 | [diff] [blame] | 773 | struct hlist_node *n; |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 774 | struct pci_region_entry *entry; |
Kevin O'Connor | 030a58a | 2013-06-13 21:24:14 -0400 | [diff] [blame] | 775 | hlist_for_each_entry_safe(entry, n, &r->list, node) { |
Alexey Korolev | 35a770f | 2012-04-19 17:47:19 +1200 | [diff] [blame] | 776 | u64 addr = r->base; |
| 777 | r->base += entry->size; |
| 778 | if (entry->bar == -1) |
| 779 | // Update bus base address if entry is a bridge region |
| 780 | busses[entry->dev->secondary_bus].r[entry->type].base = addr; |
| 781 | pci_region_map_one_entry(entry, addr); |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 782 | hlist_del(&entry->node); |
Alexey Korolev | 35a770f | 2012-04-19 17:47:19 +1200 | [diff] [blame] | 783 | free(entry); |
Alexey Korolev | 35a770f | 2012-04-19 17:47:19 +1200 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
Kevin O'Connor | b725dcb | 2011-10-15 11:53:38 -0400 | [diff] [blame] | 787 | static void pci_bios_map_devices(struct pci_bus *busses) |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 788 | { |
Gerd Hoffmann | c72370e | 2013-11-26 12:51:30 +0100 | [diff] [blame] | 789 | dprintf(1, "PCI: 32: %016llx - %016llx\n", pcimem_start, pcimem_end); |
Alexey Korolev | e5e5f96 | 2012-04-26 17:01:59 +1200 | [diff] [blame] | 790 | if (pci_bios_init_root_regions(busses)) { |
| 791 | struct pci_region r64_mem, r64_pref; |
Kevin O'Connor | a88c197 | 2013-06-08 21:51:46 -0400 | [diff] [blame] | 792 | r64_mem.list.first = NULL; |
| 793 | r64_pref.list.first = NULL; |
Alexey Korolev | e5e5f96 | 2012-04-26 17:01:59 +1200 | [diff] [blame] | 794 | pci_region_migrate_64bit_entries(&busses[0].r[PCI_REGION_TYPE_MEM], |
| 795 | &r64_mem); |
| 796 | pci_region_migrate_64bit_entries(&busses[0].r[PCI_REGION_TYPE_PREFMEM], |
| 797 | &r64_pref); |
| 798 | |
| 799 | if (pci_bios_init_root_regions(busses)) |
| 800 | panic("PCI: out of 32bit address space\n"); |
| 801 | |
Gerd Hoffmann | 5283b2e | 2012-06-12 09:27:15 +0200 | [diff] [blame] | 802 | u64 sum_mem = pci_region_sum(&r64_mem); |
| 803 | u64 sum_pref = pci_region_sum(&r64_pref); |
| 804 | u64 align_mem = pci_region_align(&r64_mem); |
| 805 | u64 align_pref = pci_region_align(&r64_pref); |
| 806 | |
Gerd Hoffmann | 0f474d0 | 2013-11-26 12:48:20 +0100 | [diff] [blame] | 807 | r64_mem.base = le64_to_cpu(romfile_loadint("etc/reserved-memory-end", 0)); |
| 808 | if (r64_mem.base < 0x100000000LL + RamSizeOver4G) |
| 809 | r64_mem.base = 0x100000000LL + RamSizeOver4G; |
Gerd Hoffmann | f21c006 | 2013-11-26 11:08:17 +0100 | [diff] [blame] | 810 | r64_mem.base = ALIGN(r64_mem.base, align_mem); |
| 811 | r64_mem.base = ALIGN(r64_mem.base, (1LL<<30)); // 1G hugepage |
| 812 | r64_pref.base = r64_mem.base + sum_mem; |
| 813 | r64_pref.base = ALIGN(r64_pref.base, align_pref); |
| 814 | r64_pref.base = ALIGN(r64_pref.base, (1LL<<30)); // 1G hugepage |
Gerd Hoffmann | 5283b2e | 2012-06-12 09:27:15 +0200 | [diff] [blame] | 815 | pcimem64_start = r64_mem.base; |
| 816 | pcimem64_end = r64_pref.base + sum_pref; |
Gerd Hoffmann | f21c006 | 2013-11-26 11:08:17 +0100 | [diff] [blame] | 817 | pcimem64_end = ALIGN(pcimem64_end, (1LL<<30)); // 1G hugepage |
Gerd Hoffmann | c72370e | 2013-11-26 12:51:30 +0100 | [diff] [blame] | 818 | dprintf(1, "PCI: 64: %016llx - %016llx\n", pcimem64_start, pcimem64_end); |
Gerd Hoffmann | 5283b2e | 2012-06-12 09:27:15 +0200 | [diff] [blame] | 819 | |
Alexey Korolev | e5e5f96 | 2012-04-26 17:01:59 +1200 | [diff] [blame] | 820 | pci_region_map_entries(busses, &r64_mem); |
| 821 | pci_region_map_entries(busses, &r64_pref); |
Gerd Hoffmann | 5283b2e | 2012-06-12 09:27:15 +0200 | [diff] [blame] | 822 | } else { |
| 823 | // no bars mapped high -> drop 64bit window (see dsdt) |
| 824 | pcimem64_start = 0; |
Alexey Korolev | e5e5f96 | 2012-04-26 17:01:59 +1200 | [diff] [blame] | 825 | } |
Kevin O'Connor | 2c4c211 | 2011-10-15 11:42:48 -0400 | [diff] [blame] | 826 | // Map regions on each device. |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 827 | int bus; |
| 828 | for (bus = 0; bus<=MaxPCIBus; bus++) { |
| 829 | int type; |
Alexey Korolev | 35a770f | 2012-04-19 17:47:19 +1200 | [diff] [blame] | 830 | for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) |
| 831 | pci_region_map_entries(busses, &busses[bus].r[type]); |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 835 | |
Kevin O'Connor | 5bab7e6 | 2011-10-01 11:33:31 -0400 | [diff] [blame] | 836 | /**************************************************************** |
| 837 | * Main setup code |
| 838 | ****************************************************************/ |
Isaku Yamahata | f441666 | 2010-06-22 17:57:52 +0900 | [diff] [blame] | 839 | |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 840 | void |
Kevin O'Connor | 40f5b5a | 2009-09-13 10:46:57 -0400 | [diff] [blame] | 841 | pci_setup(void) |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 842 | { |
Kevin O'Connor | a2a86e2 | 2013-02-13 19:35:12 -0500 | [diff] [blame] | 843 | if (!CONFIG_QEMU) |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 844 | return; |
| 845 | |
Kevin O'Connor | 40f5b5a | 2009-09-13 10:46:57 -0400 | [diff] [blame] | 846 | dprintf(3, "pci setup\n"); |
| 847 | |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 848 | dprintf(1, "=== PCI bus & bridge init ===\n"); |
Jan Kiszka | 58e6b3f | 2011-09-21 08:16:21 +0200 | [diff] [blame] | 849 | if (pci_probe_host() != 0) { |
| 850 | return; |
| 851 | } |
Isaku Yamahata | f441666 | 2010-06-22 17:57:52 +0900 | [diff] [blame] | 852 | pci_bios_init_bus(); |
| 853 | |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 854 | dprintf(1, "=== PCI device probing ===\n"); |
Jan Kiszka | 58e6b3f | 2011-09-21 08:16:21 +0200 | [diff] [blame] | 855 | pci_probe_devices(); |
Kevin O'Connor | 37956dd | 2011-06-21 22:22:58 -0400 | [diff] [blame] | 856 | |
Kevin O'Connor | b1c35f2 | 2012-11-26 11:05:32 -0500 | [diff] [blame] | 857 | pcimem_start = RamSize; |
| 858 | pci_bios_init_platform(); |
| 859 | |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 860 | dprintf(1, "=== PCI new allocation pass #1 ===\n"); |
Kevin O'Connor | b725dcb | 2011-10-15 11:53:38 -0400 | [diff] [blame] | 861 | struct pci_bus *busses = malloc_tmp(sizeof(*busses) * (MaxPCIBus + 1)); |
Kevin O'Connor | 28a20e1 | 2011-10-15 11:07:30 -0400 | [diff] [blame] | 862 | if (!busses) { |
| 863 | warn_noalloc(); |
| 864 | return; |
| 865 | } |
| 866 | memset(busses, 0, sizeof(*busses) * (MaxPCIBus + 1)); |
Alexey Korolev | fa51bcd | 2012-04-18 17:21:19 +1200 | [diff] [blame] | 867 | if (pci_bios_check_devices(busses)) |
| 868 | return; |
| 869 | |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 870 | dprintf(1, "=== PCI new allocation pass #2 ===\n"); |
Kevin O'Connor | b725dcb | 2011-10-15 11:53:38 -0400 | [diff] [blame] | 871 | pci_bios_map_devices(busses); |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 872 | |
Kevin O'Connor | 3f2288f | 2011-10-15 12:02:14 -0400 | [diff] [blame] | 873 | pci_bios_init_devices(); |
Gerd Hoffmann | 8e30147 | 2011-08-09 17:22:42 +0200 | [diff] [blame] | 874 | |
Gerd Hoffmann | 82b39b2 | 2011-07-11 09:20:28 +0200 | [diff] [blame] | 875 | free(busses); |
Alex Williamson | 7adfd71 | 2013-03-20 10:58:47 -0600 | [diff] [blame] | 876 | |
| 877 | pci_enable_default_vga(); |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 878 | } |