blob: 11c92aec453fda3131bfc30816c9f7483f70d123 [file] [log] [blame]
Kevin O'Connor0525d292008-07-04 06:18:30 -04001// 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'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor0525d292008-07-04 06:18:30 -04007
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04008#include "config.h" // CONFIG_*
9#include "dev-q35.h" // Q35_HOST_BRIDGE_PCIEXBAR_ADDR
Kevin O'Connor4ade5232013-09-18 21:41:48 -040010#include "hw/ata.h" // PORT_ATA1_CMD_BASE
Kevin O'Connor5d369d82013-09-02 20:48:46 -040011#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'Connora88c1972013-06-08 21:51:46 -040014#include "list.h" // struct hlist_node
Kevin O'Connor9dea5902013-09-14 20:23:54 -040015#include "malloc.h" // free
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040016#include "memmap.h" // add_e820
17#include "output.h" // dprintf
18#include "paravirt.h" // RamSize
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040019#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040020#include "util.h" // pci_setup
Kevin O'Connor4ade5232013-09-18 21:41:48 -040021#include "x86.h" // outb
Gerd Hoffmann0f474d02013-11-26 12:48:20 +010022#include "byteorder.h" // le64_to_cpu
23#include "romfile.h" // romfile_loadint
Isaku Yamahata72a590e2012-11-28 10:17:33 +010024
Gerd Hoffmann67a3c7c2013-11-26 13:12:04 +010025#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 Yamahataaf0963d2010-06-22 17:57:53 +090028
Gerd Hoffmann82b39b22011-07-11 09:20:28 +020029enum 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
36static 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 Hoffmanne55c4e82012-06-07 10:34:32 +020042u64 pcimem_start = BUILD_PCIMEM_START;
43u64 pcimem_end = BUILD_PCIMEM_END;
44u64 pcimem64_start = BUILD_PCIMEM64_START;
45u64 pcimem64_end = BUILD_PCIMEM64_END;
46
Alexey Korolevfa51bcd2012-04-18 17:21:19 +120047struct pci_region_entry {
48 struct pci_device *dev;
49 int bar;
Alexey Korolev030288f2012-04-19 17:44:55 +120050 u64 size;
51 u64 align;
Alexey Korolevfa51bcd2012-04-18 17:21:19 +120052 int is64;
53 enum pci_region_type type;
Kevin O'Connora88c1972013-06-08 21:51:46 -040054 struct hlist_node node;
Alexey Korolevfa51bcd2012-04-18 17:21:19 +120055};
56
Alexey Korolev35a770f2012-04-19 17:47:19 +120057struct pci_region {
Alexey Korolev35a770f2012-04-19 17:47:19 +120058 /* pci region assignments */
59 u64 base;
Kevin O'Connora88c1972013-06-08 21:51:46 -040060 struct hlist_head list;
Alexey Korolev35a770f2012-04-19 17:47:19 +120061};
62
Kevin O'Connorb725dcb2011-10-15 11:53:38 -040063struct pci_bus {
Alexey Korolev35a770f2012-04-19 17:47:19 +120064 struct pci_region r[PCI_REGION_TYPE_COUNT];
Kevin O'Connor2c4c2112011-10-15 11:42:48 -040065 struct pci_device *bus_dev;
Kevin O'Connorb725dcb2011-10-15 11:53:38 -040066};
Gerd Hoffmann82b39b22011-07-11 09:20:28 +020067
Kevin O'Connorcbbdcf22011-10-01 13:13:29 -040068static u32 pci_bar(struct pci_device *pci, int region_num)
Isaku Yamahataa65821d2010-06-22 17:57:50 +090069{
70 if (region_num != PCI_ROM_SLOT) {
71 return PCI_BASE_ADDRESS_0 + region_num * 4;
72 }
Isaku Yamahata5d0de152010-06-22 17:57:51 +090073
74#define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
Kevin O'Connorcbbdcf22011-10-01 13:13:29 -040075 u8 type = pci->header_type & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
Isaku Yamahata5d0de152010-06-22 17:57:51 +090076 return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
Isaku Yamahataa65821d2010-06-22 17:57:50 +090077}
78
Kevin O'Connorcbbdcf22011-10-01 13:13:29 -040079static void
Alexey Korolev030288f2012-04-19 17:44:55 +120080pci_set_io_region_addr(struct pci_device *pci, int bar, u64 addr, int is64)
Kevin O'Connor0525d292008-07-04 06:18:30 -040081{
Alexey Korolev030288f2012-04-19 17:44:55 +120082 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 Yamahatab9e47212010-06-22 17:57:47 +090086}
87
Kevin O'Connor5bab7e62011-10-01 11:33:31 -040088
89/****************************************************************
90 * Misc. device init
91 ****************************************************************/
92
93/* host irqs corresponding to PCI irqs A-D */
94const u8 pci_irqs[4] = {
95 10, 10, 11, 11
96};
97
Alex Williamsondbb7a662013-02-21 09:12:23 -070098static 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
105static int (*pci_slot_get_irq)(struct pci_device *pci, int pin) =
106 dummy_pci_slot_get_irq;
Alex Williamsonb9490402013-02-15 14:11:41 -0700107
Kevin O'Connor0ce21382011-10-01 14:52:35 -0400108// Return the global irq number corresponding to a host bus device irq pin.
Alex Williamsonb9490402013-02-15 14:11:41 -0700109static int piix_pci_slot_get_irq(struct pci_device *pci, int pin)
Kevin O'Connor0525d292008-07-04 06:18:30 -0400110{
Gerd Hoffmann0c8f58d2012-05-04 17:33:36 +0200111 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'Connor0ce21382011-10-01 14:52:35 -0400118 return pci_irqs[(pin - 1 + slot_addend) & 3];
Kevin O'Connor0525d292008-07-04 06:18:30 -0400119}
120
Alex Williamsonb9490402013-02-15 14:11:41 -0700121static 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'Connor6e4583c2011-06-19 10:09:26 -0400146/* PIIX3/PIIX4 PCI to ISA bridge */
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500147static void piix_isa_bridge_setup(struct pci_device *pci, void *arg)
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400148{
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'Connor278b19f2011-06-21 22:41:15 -0400159 pci_config_writeb(pci->bdf, 0x60 + i, irq);
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400160 }
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 Yamahata72a590e2012-11-28 10:17:33 +0100166/* ICH9 LPC PCI to ISA bridge */
167/* PCI_VENDOR_ID_INTEL && PCI_DEVICE_ID_INTEL_ICH9_LPC */
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500168void mch_isa_bridge_setup(struct pci_device *dev, void *arg)
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100169{
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 Williamson555a2132013-02-15 14:11:35 -0700185 pci_config_writeb(bdf, ICH9_LPC_PIRQA_ROUT + i, irq);
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100186 /* PIRQ[E-H] routing */
Alex Williamson555a2132013-02-15 14:11:35 -0700187 pci_config_writeb(bdf, ICH9_LPC_PIRQE_ROUT + i, irq);
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100188 }
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 Hoffmann5b631092013-07-25 09:47:18 +0200200 acpi_pm1a_cnt = PORT_ACPI_PM_BASE + 0x04;
Kevin O'Connor118605f2013-07-20 11:06:51 -0400201 pmtimer_setup(PORT_ACPI_PM_BASE + 0x08);
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100202}
203
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500204static void storage_ide_setup(struct pci_device *pci, void *arg)
Kevin O'Connor0d6b8d52010-07-10 13:12:37 -0400205{
206 /* IDE: we map it as in ISA mode */
Alexey Korolev030288f2012-04-19 17:44:55 +1200207 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'Connor0d6b8d52010-07-10 13:12:37 -0400211}
212
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400213/* PIIX3/PIIX4 IDE */
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500214static void piix_ide_setup(struct pci_device *pci, void *arg)
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400215{
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400216 u16 bdf = pci->bdf;
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400217 pci_config_writew(bdf, 0x40, 0x8000); // enable IDE0
218 pci_config_writew(bdf, 0x42, 0x8000); // enable IDE1
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400219}
220
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500221static void pic_ibm_setup(struct pci_device *pci, void *arg)
Kevin O'Connor0d6b8d52010-07-10 13:12:37 -0400222{
223 /* PIC, IBM, MPIC & MPIC2 */
Alexey Korolev030288f2012-04-19 17:44:55 +1200224 pci_set_io_region_addr(pci, 0, 0x80800000 + 0x00040000, 0);
Kevin O'Connor0d6b8d52010-07-10 13:12:37 -0400225}
226
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500227static void apple_macio_setup(struct pci_device *pci, void *arg)
Kevin O'Connor0d6b8d52010-07-10 13:12:37 -0400228{
229 /* macio bridge */
Alexey Korolev030288f2012-04-19 17:44:55 +1200230 pci_set_io_region_addr(pci, 0, 0x80800000, 0);
Kevin O'Connor0d6b8d52010-07-10 13:12:37 -0400231}
232
Marcel Apfelbaum40d020f2014-01-15 14:20:06 +0200233static void piix4_pm_config_setup(u16 bdf)
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400234{
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 Apfelbaum40d020f2014-01-15 14:20:06 +0200242}
243
244static int PiixPmBDF = -1;
245
246/* PIIX4 Power Management device (for ACPI) */
247static void piix4_pm_setup(struct pci_device *pci, void *arg)
248{
249 PiixPmBDF = pci->bdf;
250 piix4_pm_config_setup(pci->bdf);
Gerd Hoffmann455a7c82012-09-06 08:01:00 +0200251
Gerd Hoffmann5b631092013-07-25 09:47:18 +0200252 acpi_pm1a_cnt = PORT_ACPI_PM_BASE + 0x04;
Kevin O'Connor118605f2013-07-20 11:06:51 -0400253 pmtimer_setup(PORT_ACPI_PM_BASE + 0x08);
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400254}
255
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100256/* ICH9 SMBUS */
257/* PCI_VENDOR_ID_INTEL && PCI_DEVICE_ID_INTEL_ICH9_SMBUS */
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500258void ich9_smbus_setup(struct pci_device *dev, void *arg)
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100259{
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'Connor0d6b8d52010-07-10 13:12:37 -0400269static const struct pci_device_id pci_device_tbl[] = {
Kevin O'Connor31dcfb02012-11-20 20:29:26 -0500270 /* PIIX3/PIIX4 PCI to ISA bridge */
271 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_0,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500272 piix_isa_bridge_setup),
Kevin O'Connor31dcfb02012-11-20 20:29:26 -0500273 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_0,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500274 piix_isa_bridge_setup),
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100275 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_LPC,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500276 mch_isa_bridge_setup),
Kevin O'Connor31dcfb02012-11-20 20:29:26 -0500277
278 /* STORAGE IDE */
279 PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500280 PCI_CLASS_STORAGE_IDE, piix_ide_setup),
Kevin O'Connor31dcfb02012-11-20 20:29:26 -0500281 PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500282 PCI_CLASS_STORAGE_IDE, piix_ide_setup),
Kevin O'Connor31dcfb02012-11-20 20:29:26 -0500283 PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500284 storage_ide_setup),
Kevin O'Connor31dcfb02012-11-20 20:29:26 -0500285
286 /* PIC, IBM, MIPC & MPIC2 */
287 PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0x0046, PCI_CLASS_SYSTEM_PIC,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500288 pic_ibm_setup),
Kevin O'Connor31dcfb02012-11-20 20:29:26 -0500289 PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0xFFFF, PCI_CLASS_SYSTEM_PIC,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500290 pic_ibm_setup),
Kevin O'Connor31dcfb02012-11-20 20:29:26 -0500291
Kevin O'Connor0d6b8d52010-07-10 13:12:37 -0400292 /* PIIX4 Power Management device (for ACPI) */
293 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500294 piix4_pm_setup),
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100295 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_SMBUS,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500296 ich9_smbus_setup),
Kevin O'Connor0d6b8d52010-07-10 13:12:37 -0400297
Kevin O'Connor31dcfb02012-11-20 20:29:26 -0500298 /* 0xff00 */
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500299 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'Connor31dcfb02012-11-20 20:29:26 -0500301
Kevin O'Connor0d6b8d52010-07-10 13:12:37 -0400302 PCI_DEVICE_END,
303};
304
Marcel Apfelbaum40d020f2014-01-15 14:20:06 +0200305void 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'Connor278b19f2011-06-21 22:41:15 -0400316static void pci_bios_init_device(struct pci_device *pci)
Kevin O'Connor0525d292008-07-04 06:18:30 -0400317{
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400318 u16 bdf = pci->bdf;
Kevin O'Connor99e37c42011-10-01 10:47:21 -0400319 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'Connor278b19f2011-06-21 22:41:15 -0400321 , pci->vendor, pci->device);
Kevin O'Connor0ce21382011-10-01 14:52:35 -0400322
Kevin O'Connor0525d292008-07-04 06:18:30 -0400323 /* map the interrupt */
Kevin O'Connor0ce21382011-10-01 14:52:35 -0400324 int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
325 if (pin != 0)
Gerd Hoffmann0c8f58d2012-05-04 17:33:36 +0200326 pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pci_slot_get_irq(pci, pin));
Kevin O'Connor0525d292008-07-04 06:18:30 -0400327
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400328 pci_init_device(pci_device_tbl, pci, NULL);
Kevin O'Connor31dcfb02012-11-20 20:29:26 -0500329
330 /* enable memory mappings */
Isaku Yamahatad146ab82012-11-28 10:17:32 +0100331 pci_config_maskw(bdf, PCI_COMMAND, 0,
332 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_SERR);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400333}
334
Kevin O'Connor3f2288f2011-10-15 12:02:14 -0400335static void pci_bios_init_devices(void)
Isaku Yamahataaf0963d2010-06-22 17:57:53 +0900336{
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400337 struct pci_device *pci;
338 foreachpci(pci) {
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400339 pci_bios_init_device(pci);
Isaku Yamahataaf0963d2010-06-22 17:57:53 +0900340 }
341}
342
Alex Williamson7adfd712013-03-20 10:58:47 -0600343static 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'Connor5bab7e62011-10-01 11:33:31 -0400381
382/****************************************************************
Kevin O'Connorb1c35f22012-11-26 11:05:32 -0500383 * Platform device initialization
384 ****************************************************************/
385
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500386void i440fx_mem_addr_setup(struct pci_device *dev, void *arg)
Kevin O'Connorb1c35f22012-11-26 11:05:32 -0500387{
388 if (RamSize <= 0x80000000)
389 pcimem_start = 0x80000000;
390 else if (RamSize <= 0xc0000000)
391 pcimem_start = 0xc0000000;
Alex Williamsonb9490402013-02-15 14:11:41 -0700392
393 pci_slot_get_irq = piix_pci_slot_get_irq;
Kevin O'Connorb1c35f22012-11-26 11:05:32 -0500394}
395
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500396void mch_mem_addr_setup(struct pci_device *dev, void *arg)
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100397{
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 Williamsonb9490402013-02-15 14:11:41 -0700412
413 pci_slot_get_irq = mch_pci_slot_get_irq;
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100414}
415
Kevin O'Connorb1c35f22012-11-26 11:05:32 -0500416static const struct pci_device_id pci_platform_tbl[] = {
417 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500418 i440fx_mem_addr_setup),
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100419 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_Q35_MCH,
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500420 mch_mem_addr_setup),
Kevin O'Connorb1c35f22012-11-26 11:05:32 -0500421 PCI_DEVICE_END
422};
423
424static 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'Connor5bab7e62011-10-01 11:33:31 -0400434 * Bus initialization
435 ****************************************************************/
436
Isaku Yamahataf4416662010-06-22 17:57:52 +0900437static void
438pci_bios_init_bus_rec(int bus, u8 *pci_bus)
439{
Kevin O'Connor2b333e42011-07-02 14:49:41 -0400440 int bdf;
Isaku Yamahataf4416662010-06-22 17:57:52 +0900441 u16 class;
442
443 dprintf(1, "PCI: %s bus = 0x%x\n", __func__, bus);
444
445 /* prevent accidental access to unintended devices */
Kevin O'Connor2b333e42011-07-02 14:49:41 -0400446 foreachbdf(bdf, bus) {
Isaku Yamahataf4416662010-06-22 17:57:52 +0900447 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'Connor2b333e42011-07-02 14:49:41 -0400454 foreachbdf(bdf, bus) {
Isaku Yamahataf4416662010-06-22 17:57:52 +0900455 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
498static void
499pci_bios_init_bus(void)
500{
501 u8 pci_bus = 0;
502 pci_bios_init_bus_rec(0 /* host bus */, &pci_bus);
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200503}
504
Kevin O'Connor5bab7e62011-10-01 11:33:31 -0400505
506/****************************************************************
507 * Bus sizing
508 ****************************************************************/
509
Kevin O'Connorcbbdcf22011-10-01 13:13:29 -0400510static void
Alexey Korolev030288f2012-04-19 17:44:55 +1200511pci_bios_get_bar(struct pci_device *pci, int bar,
512 int *ptype, u64 *psize, int *pis64)
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200513{
Kevin O'Connorcbbdcf22011-10-01 13:13:29 -0400514 u32 ofs = pci_bar(pci, bar);
515 u16 bdf = pci->bdf;
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200516 u32 old = pci_config_readl(bdf, ofs);
Alexey Korolev030288f2012-04-19 17:44:55 +1200517 int is64 = 0, type = PCI_REGION_TYPE_MEM;
518 u64 mask;
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200519
520 if (bar == PCI_ROM_SLOT) {
521 mask = PCI_ROM_ADDRESS_MASK;
522 pci_config_writel(bdf, ofs, mask);
523 } else {
Alexey Korolev030288f2012-04-19 17:44:55 +1200524 if (old & PCI_BASE_ADDRESS_SPACE_IO) {
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200525 mask = PCI_BASE_ADDRESS_IO_MASK;
Alexey Korolev030288f2012-04-19 17:44:55 +1200526 type = PCI_REGION_TYPE_IO;
527 } else {
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200528 mask = PCI_BASE_ADDRESS_MEM_MASK;
Alexey Korolev030288f2012-04-19 17:44:55 +1200529 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 Hoffmann82b39b22011-07-11 09:20:28 +0200534 pci_config_writel(bdf, ofs, ~0);
535 }
Alexey Korolev030288f2012-04-19 17:44:55 +1200536 u64 val = pci_config_readl(bdf, ofs);
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200537 pci_config_writel(bdf, ofs, old);
Alexey Korolev030288f2012-04-19 17:44:55 +1200538 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 Hoffmann82b39b22011-07-11 09:20:28 +0200551}
552
Alexey Korolevac0cd582012-04-19 17:48:54 +1200553static 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'Connora88c1972013-06-08 21:51:46 -0400566 struct pci_region_entry *entry;
567 hlist_for_each_entry(entry, &r->list, node) {
Alexey Korolevac0cd582012-04-19 17:48:54 +1200568 if (!entry->is64)
569 return 0;
Alexey Korolevac0cd582012-04-19 17:48:54 +1200570 }
571 return 1;
572}
573
Alexey Korolev37c111f2012-04-26 16:51:05 +1200574static u64 pci_region_align(struct pci_region *r)
575{
Kevin O'Connora88c1972013-06-08 21:51:46 -0400576 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 Korolev37c111f2012-04-26 16:51:05 +1200582}
583
584static u64 pci_region_sum(struct pci_region *r)
585{
Alexey Korolev37c111f2012-04-26 16:51:05 +1200586 u64 sum = 0;
Kevin O'Connora88c1972013-06-08 21:51:46 -0400587 struct pci_region_entry *entry;
588 hlist_for_each_entry(entry, &r->list, node) {
Alexey Korolev37c111f2012-04-26 16:51:05 +1200589 sum += entry->size;
Kevin O'Connore5d71ca2012-04-26 22:04:34 -0400590 }
591 return sum;
Alexey Korolev37c111f2012-04-26 16:51:05 +1200592}
593
Alexey Koroleve5e5f962012-04-26 17:01:59 +1200594static void pci_region_migrate_64bit_entries(struct pci_region *from,
595 struct pci_region *to)
596{
Kevin O'Connor030a58a2013-06-13 21:24:14 -0400597 struct hlist_node *n, **last = &to->list.first;
Kevin O'Connora88c1972013-06-08 21:51:46 -0400598 struct pci_region_entry *entry;
Kevin O'Connor030a58a2013-06-13 21:24:14 -0400599 hlist_for_each_entry_safe(entry, n, &from->list, node) {
Kevin O'Connora88c1972013-06-08 21:51:46 -0400600 if (!entry->is64)
Kevin O'Connord630d142012-04-26 22:20:56 -0400601 continue;
Gerd Hoffmanna247e672013-11-26 12:57:19 +0100602 if (entry->dev->class == PCI_CLASS_SERIAL_USB)
603 continue;
Kevin O'Connord630d142012-04-26 22:20:56 -0400604 // Move from source list to destination list.
Kevin O'Connora88c1972013-06-08 21:51:46 -0400605 hlist_del(&entry->node);
606 hlist_add(&entry->node, last);
Gerd Hoffmann95c5afc2013-11-26 11:21:23 +0100607 last = &entry->node.next;
Alexey Koroleve5e5f962012-04-26 17:01:59 +1200608 }
609}
610
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200611static struct pci_region_entry *
612pci_region_create_entry(struct pci_bus *bus, struct pci_device *dev,
Alexey Korolev030288f2012-04-19 17:44:55 +1200613 int bar, u64 size, u64 align, int type, int is64)
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200614{
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200615 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'Connor3d1bc9d2012-04-01 12:30:32 -0400624 entry->align = align;
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200625 entry->is64 = is64;
626 entry->type = type;
627 // Insert into list in sorted order.
Kevin O'Connora88c1972013-06-08 21:51:46 -0400628 struct hlist_node **pprev;
629 struct pci_region_entry *pos;
Kevin O'Connor030a58a2013-06-13 21:24:14 -0400630 hlist_for_each_entry_pprev(pos, pprev, &bus->r[type].list, node) {
Kevin O'Connor3d1bc9d2012-04-01 12:30:32 -0400631 if (pos->align < align || (pos->align == align && pos->size < size))
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200632 break;
633 }
Kevin O'Connora88c1972013-06-08 21:51:46 -0400634 hlist_add(&entry->node, pprev);
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200635 return entry;
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200636}
637
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200638static int pci_bios_check_devices(struct pci_bus *busses)
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200639{
Kevin O'Connor2c4c2112011-10-15 11:42:48 -0400640 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 Korolev1a9f47f2012-04-19 17:40:13 +1200645 if (pci->class == PCI_CLASS_BRIDGE_PCI)
Kevin O'Connor2c4c2112011-10-15 11:42:48 -0400646 busses[pci->secondary_bus].bus_dev = pci;
Alexey Korolev1a9f47f2012-04-19 17:40:13 +1200647
Kevin O'Connor2c4c2112011-10-15 11:42:48 -0400648 struct pci_bus *bus = &busses[pci_bdf_to_bus(pci->bdf)];
649 int i;
650 for (i = 0; i < PCI_NUM_REGIONS; i++) {
Alexey Korolev1a9f47f2012-04-19 17:40:13 +1200651 if ((pci->class == PCI_CLASS_BRIDGE_PCI) &&
652 (i >= PCI_BRIDGE_NUM_REGIONS && i < PCI_ROM_SLOT))
653 continue;
Alexey Korolev030288f2012-04-19 17:44:55 +1200654 int type, is64;
655 u64 size;
656 pci_bios_get_bar(pci, i, &type, &size, &is64);
657 if (size == 0)
Kevin O'Connor2c4c2112011-10-15 11:42:48 -0400658 continue;
659
Alexey Korolev5fa24b52012-04-18 17:31:58 +1200660 if (type != PCI_REGION_TYPE_IO && size < PCI_DEVICE_MEM_MIN)
661 size = PCI_DEVICE_MEM_MIN;
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200662 struct pci_region_entry *entry = pci_region_create_entry(
Kevin O'Connor3d1bc9d2012-04-01 12:30:32 -0400663 bus, pci, i, size, size, type, is64);
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200664 if (!entry)
665 return -1;
Kevin O'Connor2c4c2112011-10-15 11:42:48 -0400666
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200667 if (is64)
Kevin O'Connor2c4c2112011-10-15 11:42:48 -0400668 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'Connorcbbdcf22011-10-01 13:13:29 -0400679 int type;
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200680 for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
Alexey Korolev030288f2012-04-19 17:44:55 +1200681 u64 align = (type == PCI_REGION_TYPE_IO) ?
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200682 PCI_BRIDGE_IO_MIN : PCI_BRIDGE_MEM_MIN;
Alexey Korolev37c111f2012-04-26 16:51:05 +1200683 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 Korolevac0cd582012-04-19 17:48:54 +1200687 int is64 = pci_bios_bridge_region_is64(&s->r[type],
688 s->bus_dev, type);
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200689 // entry->bar is -1 if the entry represents a bridge region
690 struct pci_region_entry *entry = pci_region_create_entry(
Alexey Korolevac0cd582012-04-19 17:48:54 +1200691 parent, s->bus_dev, -1, size, align, type, is64);
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200692 if (!entry)
693 return -1;
Alexey Korolev030288f2012-04-19 17:44:55 +1200694 dprintf(1, "PCI: secondary bus %d size %08llx type %s\n",
Alexey Korolevf3c2b062012-04-18 17:26:43 +1200695 entry->dev->secondary_bus, size,
696 region_type_name[entry->type]);
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200697 }
Kevin O'Connor5bab7e62011-10-01 11:33:31 -0400698 }
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200699 return 0;
Kevin O'Connor5bab7e62011-10-01 11:33:31 -0400700}
701
Kevin O'Connore5d71ca2012-04-26 22:04:34 -0400702
703/****************************************************************
704 * BAR assignment
705 ****************************************************************/
706
Kevin O'Connora8dcc5b2011-10-01 12:08:57 -0400707// Setup region bases (given the regions' size and alignment)
Alexey Koroleve5e5f962012-04-26 17:01:59 +1200708static int pci_bios_init_root_regions(struct pci_bus *bus)
Kevin O'Connor5bab7e62011-10-01 11:33:31 -0400709{
Kevin O'Connor5bab7e62011-10-01 11:33:31 -0400710 bus->r[PCI_REGION_TYPE_IO].base = 0xc000;
711
Alexey Korolev37c111f2012-04-26 16:51:05 +1200712 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'Connor3d1bc9d2012-04-01 12:30:32 -0400716 // Swap regions to improve alignment.
Alexey Korolev37c111f2012-04-26 16:51:05 +1200717 r_end = r_start;
718 r_start = &bus->r[PCI_REGION_TYPE_PREFMEM];
Kevin O'Connor5bab7e62011-10-01 11:33:31 -0400719 }
Alexey Korolev37c111f2012-04-26 16:51:05 +1200720 u64 sum = pci_region_sum(r_end);
721 u64 align = pci_region_align(r_end);
Gerd Hoffmanne55c4e82012-06-07 10:34:32 +0200722 r_end->base = ALIGN_DOWN((pcimem_end - sum), align);
Alexey Korolev37c111f2012-04-26 16:51:05 +1200723 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 Hoffmanne55c4e82012-06-07 10:34:32 +0200727 if ((r_start->base < pcimem_start) ||
728 (r_start->base > pcimem_end))
Kevin O'Connora8dcc5b2011-10-01 12:08:57 -0400729 // Memory range requested is larger than available.
Alexey Koroleve5e5f962012-04-26 17:01:59 +1200730 return -1;
731 return 0;
Kevin O'Connor5bab7e62011-10-01 11:33:31 -0400732}
733
Kevin O'Connor5bab7e62011-10-01 11:33:31 -0400734#define PCI_IO_SHIFT 8
735#define PCI_MEMORY_SHIFT 16
736#define PCI_PREF_MEMORY_SHIFT 16
737
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200738static void
Alexey Korolev35a770f2012-04-19 17:47:19 +1200739pci_region_map_one_entry(struct pci_region_entry *entry, u64 addr)
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200740{
741 u16 bdf = entry->dev->bdf;
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200742 if (entry->bar >= 0) {
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200743 dprintf(1, "PCI: map device bdf=%02x:%02x.%x"
Alexey Korolev030288f2012-04-19 17:44:55 +1200744 " bar %d, addr %08llx, size %08llx [%s]\n",
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200745 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 Korolev030288f2012-04-19 17:44:55 +1200748 pci_set_io_region_addr(entry->dev, entry->bar, addr, entry->is64);
Alexey Korolev3a297162012-04-18 17:22:29 +1200749 return;
750 }
751
Alexey Korolev030288f2012-04-19 17:44:55 +1200752 u64 limit = addr + entry->size - 1;
Alexey Korolev3a297162012-04-18 17:22:29 +1200753 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 Korolev030288f2012-04-19 17:44:55 +1200766 pci_config_writel(bdf, PCI_PREF_BASE_UPPER32, addr >> 32);
767 pci_config_writel(bdf, PCI_PREF_LIMIT_UPPER32, limit >> 32);
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200768 }
769}
770
Alexey Korolev35a770f2012-04-19 17:47:19 +1200771static void pci_region_map_entries(struct pci_bus *busses, struct pci_region *r)
772{
Kevin O'Connor030a58a2013-06-13 21:24:14 -0400773 struct hlist_node *n;
Kevin O'Connora88c1972013-06-08 21:51:46 -0400774 struct pci_region_entry *entry;
Kevin O'Connor030a58a2013-06-13 21:24:14 -0400775 hlist_for_each_entry_safe(entry, n, &r->list, node) {
Alexey Korolev35a770f2012-04-19 17:47:19 +1200776 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'Connora88c1972013-06-08 21:51:46 -0400782 hlist_del(&entry->node);
Alexey Korolev35a770f2012-04-19 17:47:19 +1200783 free(entry);
Alexey Korolev35a770f2012-04-19 17:47:19 +1200784 }
785}
786
Kevin O'Connorb725dcb2011-10-15 11:53:38 -0400787static void pci_bios_map_devices(struct pci_bus *busses)
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200788{
Gerd Hoffmannc72370e2013-11-26 12:51:30 +0100789 dprintf(1, "PCI: 32: %016llx - %016llx\n", pcimem_start, pcimem_end);
Alexey Koroleve5e5f962012-04-26 17:01:59 +1200790 if (pci_bios_init_root_regions(busses)) {
791 struct pci_region r64_mem, r64_pref;
Kevin O'Connora88c1972013-06-08 21:51:46 -0400792 r64_mem.list.first = NULL;
793 r64_pref.list.first = NULL;
Alexey Koroleve5e5f962012-04-26 17:01:59 +1200794 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 Hoffmann5283b2e2012-06-12 09:27:15 +0200802 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 Hoffmann0f474d02013-11-26 12:48:20 +0100807 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 Hoffmannf21c0062013-11-26 11:08:17 +0100810 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 Hoffmann5283b2e2012-06-12 09:27:15 +0200815 pcimem64_start = r64_mem.base;
816 pcimem64_end = r64_pref.base + sum_pref;
Gerd Hoffmannf21c0062013-11-26 11:08:17 +0100817 pcimem64_end = ALIGN(pcimem64_end, (1LL<<30)); // 1G hugepage
Gerd Hoffmannc72370e2013-11-26 12:51:30 +0100818 dprintf(1, "PCI: 64: %016llx - %016llx\n", pcimem64_start, pcimem64_end);
Gerd Hoffmann5283b2e2012-06-12 09:27:15 +0200819
Alexey Koroleve5e5f962012-04-26 17:01:59 +1200820 pci_region_map_entries(busses, &r64_mem);
821 pci_region_map_entries(busses, &r64_pref);
Gerd Hoffmann5283b2e2012-06-12 09:27:15 +0200822 } else {
823 // no bars mapped high -> drop 64bit window (see dsdt)
824 pcimem64_start = 0;
Alexey Koroleve5e5f962012-04-26 17:01:59 +1200825 }
Kevin O'Connor2c4c2112011-10-15 11:42:48 -0400826 // Map regions on each device.
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200827 int bus;
828 for (bus = 0; bus<=MaxPCIBus; bus++) {
829 int type;
Alexey Korolev35a770f2012-04-19 17:47:19 +1200830 for (type = 0; type < PCI_REGION_TYPE_COUNT; type++)
831 pci_region_map_entries(busses, &busses[bus].r[type]);
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200832 }
833}
834
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200835
Kevin O'Connor5bab7e62011-10-01 11:33:31 -0400836/****************************************************************
837 * Main setup code
838 ****************************************************************/
Isaku Yamahataf4416662010-06-22 17:57:52 +0900839
Kevin O'Connor0525d292008-07-04 06:18:30 -0400840void
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400841pci_setup(void)
Kevin O'Connor0525d292008-07-04 06:18:30 -0400842{
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500843 if (!CONFIG_QEMU)
Kevin O'Connor0525d292008-07-04 06:18:30 -0400844 return;
845
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400846 dprintf(3, "pci setup\n");
847
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200848 dprintf(1, "=== PCI bus & bridge init ===\n");
Jan Kiszka58e6b3f2011-09-21 08:16:21 +0200849 if (pci_probe_host() != 0) {
850 return;
851 }
Isaku Yamahataf4416662010-06-22 17:57:52 +0900852 pci_bios_init_bus();
853
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200854 dprintf(1, "=== PCI device probing ===\n");
Jan Kiszka58e6b3f2011-09-21 08:16:21 +0200855 pci_probe_devices();
Kevin O'Connor37956dd2011-06-21 22:22:58 -0400856
Kevin O'Connorb1c35f22012-11-26 11:05:32 -0500857 pcimem_start = RamSize;
858 pci_bios_init_platform();
859
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200860 dprintf(1, "=== PCI new allocation pass #1 ===\n");
Kevin O'Connorb725dcb2011-10-15 11:53:38 -0400861 struct pci_bus *busses = malloc_tmp(sizeof(*busses) * (MaxPCIBus + 1));
Kevin O'Connor28a20e12011-10-15 11:07:30 -0400862 if (!busses) {
863 warn_noalloc();
864 return;
865 }
866 memset(busses, 0, sizeof(*busses) * (MaxPCIBus + 1));
Alexey Korolevfa51bcd2012-04-18 17:21:19 +1200867 if (pci_bios_check_devices(busses))
868 return;
869
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200870 dprintf(1, "=== PCI new allocation pass #2 ===\n");
Kevin O'Connorb725dcb2011-10-15 11:53:38 -0400871 pci_bios_map_devices(busses);
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200872
Kevin O'Connor3f2288f2011-10-15 12:02:14 -0400873 pci_bios_init_devices();
Gerd Hoffmann8e301472011-08-09 17:22:42 +0200874
Gerd Hoffmann82b39b22011-07-11 09:20:28 +0200875 free(busses);
Alex Williamson7adfd712013-03-20 10:58:47 -0600876
877 pci_enable_default_vga();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400878}