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