Xeon-SP boards: Factor out OCP VPD `get_cxl_mode()` impl
[coreboot2.git] / src / device / pci_device.c
blobaf3355d7cf211e800d18838181e311bb163119e8
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /*
4 * Originally based on the Linux kernel (drivers/pci/pci.c).
5 * PCI Bus Services, see include/linux/pci.h for further explanation.
6 */
8 #include <acpi/acpi.h>
9 #include <assert.h>
10 #include <cbmem.h>
11 #include <device/pci_ops.h>
12 #include <bootmode.h>
13 #include <console/console.h>
14 #include <cpu/cpu.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <delay.h>
18 #include <device/cardbus.h>
19 #include <device/device.h>
20 #include <device/pci.h>
21 #include <device/pci_ids.h>
22 #include <device/pcix.h>
23 #include <device/pciexp.h>
24 #include <lib.h>
25 #include <pc80/i8259.h>
26 #include <security/vboot/vbnv.h>
27 #include <timestamp.h>
28 #include <types.h>
30 u8 pci_moving_config8(struct device *dev, unsigned int reg)
32 u8 value, ones, zeroes;
34 value = pci_read_config8(dev, reg);
36 pci_write_config8(dev, reg, 0xff);
37 ones = pci_read_config8(dev, reg);
39 pci_write_config8(dev, reg, 0x00);
40 zeroes = pci_read_config8(dev, reg);
42 pci_write_config8(dev, reg, value);
44 return ones ^ zeroes;
47 u16 pci_moving_config16(struct device *dev, unsigned int reg)
49 u16 value, ones, zeroes;
51 value = pci_read_config16(dev, reg);
53 pci_write_config16(dev, reg, 0xffff);
54 ones = pci_read_config16(dev, reg);
56 pci_write_config16(dev, reg, 0x0000);
57 zeroes = pci_read_config16(dev, reg);
59 pci_write_config16(dev, reg, value);
61 return ones ^ zeroes;
64 u32 pci_moving_config32(struct device *dev, unsigned int reg)
66 u32 value, ones, zeroes;
68 value = pci_read_config32(dev, reg);
70 pci_write_config32(dev, reg, 0xffffffff);
71 ones = pci_read_config32(dev, reg);
73 pci_write_config32(dev, reg, 0x00000000);
74 zeroes = pci_read_config32(dev, reg);
76 pci_write_config32(dev, reg, value);
78 return ones ^ zeroes;
81 /**
82 * Given a device and register, read the size of the BAR for that register.
84 * @param dev Pointer to the device structure.
85 * @param index Address of the PCI configuration register.
86 * @return TODO
88 struct resource *pci_get_resource(struct device *dev, unsigned long index)
90 struct resource *resource;
91 unsigned long value, attr;
92 resource_t moving, limit;
94 /* Initialize the resources to nothing. */
95 resource = new_resource(dev, index);
97 /* Get the initial value. */
98 value = pci_read_config32(dev, index);
100 /* See which bits move. */
101 moving = pci_moving_config32(dev, index);
103 /* Initialize attr to the bits that do not move. */
104 attr = value & ~moving;
106 /* If it is a 64bit resource look at the high half as well. */
107 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
108 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
109 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
110 /* Find the high bits that move. */
111 moving |=
112 ((resource_t)pci_moving_config32(dev, index + 4)) << 32;
115 /* Find the resource constraints.
116 * Start by finding the bits that move. From there:
117 * - Size is the least significant bit of the bits that move.
118 * - Limit is all of the bits that move plus all of the lower bits.
119 * See PCI Spec 6.2.5.1.
121 limit = 0;
122 if (moving) {
123 resource->size = 1;
124 resource->align = resource->gran = 0;
125 while (!(moving & resource->size)) {
126 resource->size <<= 1;
127 resource->align += 1;
128 resource->gran += 1;
130 resource->limit = limit = moving | (resource->size - 1);
132 if (pci_base_address_is_memory_space(attr)) {
133 /* Page-align to allow individual mapping of devices. */
134 if (resource->align < 12)
135 resource->align = 12;
140 * Some broken hardware has read-only registers that do not
141 * really size correctly.
143 * Example: the Acer M7229 has BARs 1-4 normally read-only,
144 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
145 * by writing 0xffffffff to it, it will read back as 0x1f1 -- which
146 * is a violation of the spec.
148 * We catch this case and ignore it by observing which bits move.
150 * This also catches the common case of unimplemented registers
151 * that always read back as 0.
153 if (moving == 0) {
154 if (value != 0) {
155 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
156 dev_path(dev), index, value);
158 resource->flags = 0;
159 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
160 /* An I/O mapped base address. */
161 resource->flags |= IORESOURCE_IO;
162 /* I don't want to deal with 32bit I/O resources. */
163 resource->limit = 0xffff;
164 } else {
165 /* A Memory mapped base address. */
166 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
167 resource->flags |= IORESOURCE_MEM;
168 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
169 resource->flags |= IORESOURCE_PREFETCH;
170 if (CONFIG(PCIEXP_HOTPLUG_PREFETCH_MEM_ABOVE_4G)
171 && dev_path_hotplug(dev))
172 resource->flags |= IORESOURCE_ABOVE_4G;
174 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
175 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
176 /* 32bit limit. */
177 resource->limit = 0xffffffffUL;
178 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
179 /* 1MB limit. */
180 resource->limit = 0x000fffffUL;
181 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
182 /* 64bit limit. */
183 resource->limit = 0xffffffffffffffffULL;
184 resource->flags |= IORESOURCE_PCI64;
185 } else {
186 /* Invalid value. */
187 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
188 printk(BIOS_ERR, " on dev %s at index %02lx\n",
189 dev_path(dev), index);
190 resource->flags = 0;
194 /* Don't let the limit exceed which bits can move. */
195 if (resource->limit > limit)
196 resource->limit = limit;
198 return resource;
202 * Given a device and an index, read the size of the BAR for that register.
204 * @param dev Pointer to the device structure.
205 * @param index Address of the PCI configuration register.
207 static void pci_get_rom_resource(struct device *dev, unsigned long index)
209 struct resource *resource;
210 unsigned long value;
211 resource_t moving;
213 /* Initialize the resources to nothing. */
214 resource = new_resource(dev, index);
216 /* Get the initial value. */
217 value = pci_read_config32(dev, index);
219 /* See which bits move. */
220 moving = pci_moving_config32(dev, index);
222 /* Clear the Enable bit. */
223 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
225 /* Find the resource constraints.
226 * Start by finding the bits that move. From there:
227 * - Size is the least significant bit of the bits that move.
228 * - Limit is all of the bits that move plus all of the lower bits.
229 * See PCI Spec 6.2.5.1.
231 if (moving) {
232 resource->size = 1;
233 resource->align = resource->gran = 0;
234 while (!(moving & resource->size)) {
235 resource->size <<= 1;
236 resource->align += 1;
237 resource->gran += 1;
239 resource->limit = moving | (resource->size - 1);
240 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
241 } else {
242 if (value != 0) {
243 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
244 dev_path(dev), index, value);
246 resource->flags = 0;
248 compact_resources(dev);
252 * Given a device, read the size of the MSI-X table.
254 * @param dev Pointer to the device structure.
255 * @return MSI-X table size or 0 if not MSI-X capable device
257 size_t pci_msix_table_size(struct device *dev)
259 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
260 if (!pos)
261 return 0;
263 const u16 control = pci_read_config16(dev, pos + PCI_MSIX_FLAGS);
264 return (control & PCI_MSIX_FLAGS_QSIZE) + 1;
268 * Given a device, return the table offset and bar the MSI-X tables resides in.
270 * @param dev Pointer to the device structure.
271 * @param offset Returned value gives the offset in bytes inside the PCI BAR.
272 * @param idx The returned value is the index of the PCI_BASE_ADDRESS register
273 * the MSI-X table is located in.
274 * @return Zero on success
276 int pci_msix_table_bar(struct device *dev, u32 *offset, u8 *idx)
278 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
279 if (!pos || !offset || !idx)
280 return 1;
282 *offset = pci_read_config32(dev, pos + PCI_MSIX_TABLE);
283 *idx = (u8)(*offset & PCI_MSIX_PBA_BIR);
284 *offset &= PCI_MSIX_PBA_OFFSET;
286 return 0;
290 * Given a device, return a msix_entry pointer or NULL if no table was found.
292 * @param dev Pointer to the device structure.
294 * @return NULL on error
296 struct msix_entry *pci_msix_get_table(struct device *dev)
298 struct resource *res;
299 u32 offset;
300 u8 idx;
302 if (pci_msix_table_bar(dev, &offset, &idx))
303 return NULL;
305 if (idx > 5)
306 return NULL;
308 res = probe_resource(dev, idx * 4 + PCI_BASE_ADDRESS_0);
309 if (!res || !res->base || offset >= res->size)
310 return NULL;
312 if ((res->flags & IORESOURCE_PCI64) &&
313 (uintptr_t)res->base != res->base)
314 return NULL;
316 return (struct msix_entry *)((uintptr_t)res->base + offset);
319 static unsigned int get_rebar_offset(const struct device *dev, unsigned long index)
321 uint32_t offset = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_RESIZABLE_BAR, 0);
322 if (!offset)
323 return 0;
325 /* Convert PCI_BASE_ADDRESS_0, ..._1, ..._2 into 0, 1, 2... */
326 const unsigned int find_bar_idx = (index - PCI_BASE_ADDRESS_0) /
327 sizeof(uint32_t);
329 /* Although all of the Resizable BAR Control Registers contain an
330 "NBARs" field, it is only valid in the Control Register for BAR 0 */
331 const uint32_t rebar_ctrl0 = pci_read_config32(dev, offset + PCI_REBAR_CTRL_OFFSET);
332 const unsigned int nbars = (rebar_ctrl0 & PCI_REBAR_CTRL_NBARS_MASK) >>
333 PCI_REBAR_CTRL_NBARS_SHIFT;
335 for (unsigned int i = 0; i < nbars; i++, offset += sizeof(uint64_t)) {
336 const uint32_t rebar_ctrl = pci_read_config32(
337 dev, offset + PCI_REBAR_CTRL_OFFSET);
338 const uint32_t bar_idx = rebar_ctrl & PCI_REBAR_CTRL_IDX_MASK;
339 if (bar_idx == find_bar_idx)
340 return offset;
343 return 0;
346 /* Bit 20 = 1 MiB, bit 21 = 2 MiB, bit 22 = 4 MiB, ... bit 63 = 8 EiB */
347 static uint64_t get_rebar_sizes_mask(const struct device *dev,
348 unsigned long index)
350 uint64_t size_mask = 0ULL;
351 const uint32_t offset = get_rebar_offset(dev, index);
352 if (!offset)
353 return 0;
355 /* Get 1 MB - 128 TB support from CAP register */
356 const uint32_t cap = pci_read_config32(dev, offset + PCI_REBAR_CAP_OFFSET);
357 /* Shift the bits from 4-31 to 0-27 (i.e., down by 4 bits) */
358 size_mask |= ((cap & PCI_REBAR_CAP_SIZE_MASK) >> 4);
360 /* Get 256 TB - 8 EB support from CTRL register and store it in bits 28-43 */
361 const uint64_t ctrl = pci_read_config32(dev, offset + PCI_REBAR_CTRL_OFFSET);
362 /* Shift ctrl mask from bit 16 to bit 28, so that the two
363 masks (fom cap and ctrl) form a contiguous bitmask when
364 concatenated (i.e., up by 12 bits). */
365 size_mask |= ((ctrl & PCI_REBAR_CTRL_SIZE_MASK) << 12);
367 /* Now that the mask occupies bits 0-43, shift it up to 20-63, so they
368 represent the actual powers of 2. */
369 return size_mask << 20;
372 static void pci_store_rebar_size(const struct device *dev,
373 const struct resource *resource)
375 const unsigned int num_bits = __fls64(resource->size);
376 const uint32_t offset = get_rebar_offset(dev, resource->index);
377 if (!offset)
378 return;
380 pci_update_config32(dev, offset + PCI_REBAR_CTRL_OFFSET,
381 ~PCI_REBAR_CTRL_SIZE_MASK,
382 num_bits << PCI_REBAR_CTRL_SIZE_SHIFT);
385 static void configure_adjustable_base(const struct device *dev,
386 unsigned long index,
387 struct resource *res)
390 * Excerpt from an implementation note from the PCIe spec:
392 * System software uses this capability in place of the above mentioned
393 * method of determining the resource size[0], and prior to assigning
394 * the base address to the BAR. Potential usable resource sizes are
395 * reported by the Function via its Resizable BAR Capability and Control
396 * registers. It is intended that the software allocate the largest of
397 * the reported sizes that it can, since allocating less address space
398 * than the largest reported size can result in lower
399 * performance. Software then writes the size to the Resizable BAR
400 * Control register for the appropriate BAR for the Function. Following
401 * this, the base address is written to the BAR.
403 * [0] Referring to using the moving bits in the BAR to determine the
404 * requested size of the MMIO region
406 const uint64_t size_mask = get_rebar_sizes_mask(dev, index);
407 if (!size_mask)
408 return;
410 int max_requested_bits = __fls64(size_mask);
411 if (max_requested_bits > CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS) {
412 printk(BIOS_WARNING, "Device %s requests a BAR with"
413 " %u bits of address space, which coreboot is not"
414 " configured to hand out, truncating to %u bits\n",
415 dev_path(dev), max_requested_bits,
416 CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS);
417 max_requested_bits = CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS;
420 if (!(res->flags & IORESOURCE_PCI64) && max_requested_bits > 32) {
421 printk(BIOS_ERR, "Resizable BAR requested"
422 " above 32 bits, but PCI function reported a"
423 " 32-bit BAR.");
424 return;
427 /* Configure the resource parameters for the adjustable BAR */
428 res->size = 1ULL << max_requested_bits;
429 res->align = max_requested_bits;
430 res->gran = max_requested_bits;
431 res->limit = (res->flags & IORESOURCE_PCI64) ? UINT64_MAX : UINT32_MAX;
432 res->flags |= (res->flags & IORESOURCE_PCI64) ?
433 IORESOURCE_PCIE_RESIZABLE_BAR | IORESOURCE_ABOVE_4G :
434 IORESOURCE_PCIE_RESIZABLE_BAR;
436 printk(BIOS_INFO, "%s: Adjusting resource index %lu: base: %llx size: %llx "
437 "align: %d gran: %d limit: %llx\n",
438 dev_path(dev), res->index, res->base, res->size,
439 res->align, res->gran, res->limit);
443 * Read the base address registers for a given device.
445 * @param dev Pointer to the dev structure.
446 * @param howmany How many registers to read (6 for device, 2 for bridge).
448 static void pci_read_bases(struct device *dev, unsigned int howmany)
450 unsigned long index;
452 for (index = PCI_BASE_ADDRESS_0;
453 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
454 struct resource *resource;
455 resource = pci_get_resource(dev, index);
457 const bool is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE) != 0;
458 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS) && is_pcie)
459 configure_adjustable_base(dev, index, resource);
461 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
464 compact_resources(dev);
467 static void pci_record_bridge_resource(struct device *dev, resource_t moving,
468 unsigned int index, unsigned long type)
470 struct resource *resource;
471 unsigned long gran;
472 resource_t step;
474 resource = NULL;
476 if (!moving)
477 return;
479 /* Initialize the constraints on the current bus. */
480 resource = new_resource(dev, index);
481 resource->size = 0;
482 gran = 0;
483 step = 1;
484 while ((moving & step) == 0) {
485 gran += 1;
486 step <<= 1;
488 resource->gran = gran;
489 resource->align = gran;
490 resource->limit = moving | (step - 1);
491 resource->flags = type | IORESOURCE_PCI_BRIDGE |
492 IORESOURCE_BRIDGE;
495 static void pci_bridge_read_bases(struct device *dev)
497 resource_t moving_base, moving_limit, moving;
499 /* See if the bridge I/O resources are implemented. */
500 moving_base = ((u32)pci_moving_config8(dev, PCI_IO_BASE)) << 8;
501 moving_base |=
502 ((u32)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
504 moving_limit = ((u32)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
505 moving_limit |=
506 ((u32)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
508 moving = moving_base & moving_limit;
510 /* Initialize the I/O space constraints on the current bus. */
511 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
513 /* See if the bridge prefmem resources are implemented. */
514 moving_base =
515 ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
516 moving_base |=
517 ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
519 moving_limit =
520 ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
521 moving_limit |=
522 ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
524 moving = moving_base & moving_limit;
525 /* Initialize the prefetchable memory constraints on the current bus. */
526 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
527 IORESOURCE_MEM | IORESOURCE_PREFETCH);
529 /* See if the bridge mem resources are implemented. */
530 moving_base = ((u32)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
531 moving_limit = ((u32)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
533 moving = moving_base & moving_limit;
535 /* Initialize the memory resources on the current bus. */
536 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
537 IORESOURCE_MEM);
539 compact_resources(dev);
542 void pci_dev_read_resources(struct device *dev)
544 pci_read_bases(dev, 6);
545 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
548 void pci_bus_read_resources(struct device *dev)
550 pci_bridge_read_bases(dev);
551 pci_read_bases(dev, 2);
552 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
555 void pci_domain_read_resources(struct device *dev)
557 struct resource *res;
559 /* Initialize the system-wide I/O space constraints. */
560 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
561 res->limit = 0xffffUL;
562 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
563 IORESOURCE_ASSIGNED;
566 * Initialize 32-bit memory resource constraints.
568 * There are often undeclared chipset resources in lower memory
569 * and memory right below the 4G barrier. Hence, only allow
570 * one big range from cbmem_top to the configured limit.
572 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
573 res->base = (uintptr_t)cbmem_top();
574 res->limit = CONFIG_DOMAIN_RESOURCE_32BIT_LIMIT - 1;
575 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
576 IORESOURCE_ASSIGNED;
578 /* Initialize 64-bit memory resource constraints above 4G. */
579 res = new_resource(dev, IOINDEX_SUBTRACTIVE(2, 0));
580 res->base = 4ULL * GiB;
581 res->limit = (1ULL << soc_phys_address_size()) - 1;
582 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
583 IORESOURCE_ASSIGNED;
586 void pci_domain_set_resources(struct device *dev)
588 assign_resources(dev->downstream);
591 static void pci_store_resource(const struct device *const dev,
592 const struct resource *const resource)
594 unsigned long base_lo, base_hi;
596 base_lo = resource->base & 0xffffffff;
597 base_hi = (resource->base >> 32) & 0xffffffff;
600 * Some chipsets allow us to set/clear the I/O bit
601 * (e.g. VIA 82C686A). So set it to be safe.
603 if (resource->flags & IORESOURCE_IO)
604 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
606 pci_write_config32(dev, resource->index, base_lo);
607 if (resource->flags & IORESOURCE_PCI64)
608 pci_write_config32(dev, resource->index + 4, base_hi);
611 static void pci_store_bridge_resource(const struct device *const dev,
612 struct resource *const resource)
614 resource_t base, end;
617 * PCI bridges have no enable bit. They are disabled if the base of
618 * the range is greater than the limit. If the size is zero, disable
619 * by setting the base = limit and end = limit - 2^gran.
621 if (resource->size == 0) {
622 base = resource->limit;
623 end = resource->limit - (1 << resource->gran);
624 resource->base = base;
625 } else {
626 base = resource->base;
627 end = resource_end(resource);
630 if (resource->index == PCI_IO_BASE) {
631 /* Set the I/O ranges. */
632 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
633 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
634 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
635 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
636 } else if (resource->index == PCI_MEMORY_BASE) {
637 /* Set the memory range. */
638 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
639 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
640 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
641 /* Set the prefetchable memory range. */
642 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
643 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
644 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
645 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
646 } else {
647 /* Don't let me think I stored the resource. */
648 resource->flags &= ~IORESOURCE_STORED;
649 printk(BIOS_ERR, "invalid resource->index %lx\n", resource->index);
653 static void pci_set_resource(struct device *dev, struct resource *resource)
655 /* Make certain the resource has actually been assigned a value. */
656 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
657 if (resource->flags & IORESOURCE_BRIDGE) {
658 /* If a bridge resource has no value assigned,
659 we can treat it like an empty resource. */
660 resource->size = 0;
661 } else {
662 printk(BIOS_ERR, "%s %02lx %s size: 0x%010llx not assigned\n",
663 dev_path(dev), resource->index,
664 resource_type(resource), resource->size);
665 return;
669 /* If this resource is fixed don't worry about it. */
670 if (resource->flags & IORESOURCE_FIXED)
671 return;
673 /* If I have already stored this resource don't worry about it. */
674 if (resource->flags & IORESOURCE_STORED)
675 return;
677 /* If the resource is subtractive don't worry about it. */
678 if (resource->flags & IORESOURCE_SUBTRACTIVE)
679 return;
681 /* Only handle PCI memory and I/O resources for now. */
682 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
683 return;
685 /* Enable the resources in the command register. */
686 if (resource->size) {
687 if (resource->flags & IORESOURCE_MEM)
688 dev->command |= PCI_COMMAND_MEMORY;
689 if (resource->flags & IORESOURCE_IO)
690 dev->command |= PCI_COMMAND_IO;
691 if (resource->flags & IORESOURCE_PCI_BRIDGE &&
692 CONFIG(PCI_SET_BUS_MASTER_PCI_BRIDGES))
693 dev->command |= PCI_COMMAND_MASTER;
696 /* Now store the resource. */
697 resource->flags |= IORESOURCE_STORED;
699 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
700 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS) &&
701 (resource->flags & IORESOURCE_PCIE_RESIZABLE_BAR))
702 pci_store_rebar_size(dev, resource);
704 pci_store_resource(dev, resource);
706 } else {
707 pci_store_bridge_resource(dev, resource);
710 report_resource_stored(dev, resource, "");
713 void pci_dev_set_resources(struct device *dev)
715 struct resource *res;
716 u8 line;
718 for (res = dev->resource_list; res; res = res->next)
719 pci_set_resource(dev, res);
721 if (dev->downstream && dev->downstream->children)
722 assign_resources(dev->downstream);
724 /* Set a default latency timer. */
725 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
727 /* Set a default secondary latency timer. */
728 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
729 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
731 /* Zero the IRQ settings. */
732 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
733 if (line)
734 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
736 /* Set the cache line size, so far 64 bytes is good for everyone. */
737 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
740 void pci_dev_enable_resources(struct device *dev)
742 const struct pci_operations *ops = NULL;
743 u16 command;
745 /* Set the subsystem vendor and device ID for mainboard devices. */
746 if (dev->ops)
747 ops = dev->ops->ops_pci;
748 if (dev->on_mainboard && ops && ops->set_subsystem) {
749 if (CONFIG_SUBSYSTEM_VENDOR_ID)
750 dev->subsystem_vendor = CONFIG_SUBSYSTEM_VENDOR_ID;
751 else if (!dev->subsystem_vendor)
752 dev->subsystem_vendor = pci_read_config16(dev,
753 PCI_VENDOR_ID);
754 if (CONFIG_SUBSYSTEM_DEVICE_ID)
755 dev->subsystem_device = CONFIG_SUBSYSTEM_DEVICE_ID;
756 else if (!dev->subsystem_device)
757 dev->subsystem_device = pci_read_config16(dev,
758 PCI_DEVICE_ID);
760 printk(BIOS_DEBUG, "%s subsystem <- %04x/%04x\n",
761 dev_path(dev), dev->subsystem_vendor,
762 dev->subsystem_device);
763 ops->set_subsystem(dev, dev->subsystem_vendor,
764 dev->subsystem_device);
766 command = pci_read_config16(dev, PCI_COMMAND);
767 command |= dev->command;
769 /* v3 has
770 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
773 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
774 pci_write_config16(dev, PCI_COMMAND, command);
777 void pci_bus_enable_resources(struct device *dev)
779 u16 ctrl;
782 * Enable I/O in command register if there is VGA card
783 * connected with (even it does not claim I/O resource).
785 if (dev->downstream->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
786 dev->command |= PCI_COMMAND_IO;
787 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
788 ctrl |= dev->downstream->bridge_ctrl;
789 ctrl |= (PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR); /* Error check. */
790 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
791 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
793 pci_dev_enable_resources(dev);
796 void pci_bus_reset(struct bus *bus)
798 u16 ctl;
800 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
801 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
802 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
803 mdelay(10);
805 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
806 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
807 delay(1);
810 void pci_dev_set_subsystem(struct device *dev, unsigned int vendor,
811 unsigned int device)
813 uint8_t offset;
815 /* Header type */
816 switch (dev->hdr_type & 0x7f) {
817 case PCI_HEADER_TYPE_NORMAL:
818 offset = PCI_SUBSYSTEM_VENDOR_ID;
819 break;
820 case PCI_HEADER_TYPE_BRIDGE:
821 offset = pci_find_capability(dev, PCI_CAP_ID_SSVID);
822 if (!offset)
823 return;
824 offset += 4; /* Vendor ID at offset 4 */
825 break;
826 case PCI_HEADER_TYPE_CARDBUS:
827 offset = PCI_CB_SUBSYSTEM_VENDOR_ID;
828 break;
829 default:
830 return;
833 if (!vendor || !device) {
834 pci_write_config32(dev, offset,
835 pci_read_config32(dev, PCI_VENDOR_ID));
836 } else {
837 pci_write_config32(dev, offset,
838 ((device & 0xffff) << 16) | (vendor & 0xffff));
842 static int should_run_oprom(struct device *dev, struct rom_header *rom)
844 static int should_run = -1;
846 if (dev->upstream->segment_group) {
847 printk(BIOS_ERR, "Only option ROMs of devices in first PCI segment group can "
848 "be run.\n");
849 return 0;
852 if (CONFIG(VENDORCODE_ELTAN_VBOOT))
853 if (rom != NULL)
854 if (!verified_boot_should_run_oprom(rom))
855 return 0;
857 if (should_run >= 0)
858 return should_run;
860 if (CONFIG(ALWAYS_RUN_OPROM)) {
861 should_run = 1;
862 return should_run;
865 /* Don't run VGA option ROMs, unless we have to print
866 * something on the screen before the kernel is loaded.
868 should_run = display_init_required();
870 if (!should_run)
871 printk(BIOS_DEBUG, "Not running VGA Option ROM\n");
872 return should_run;
875 static int should_load_oprom(struct device *dev)
877 /* If S3_VGA_ROM_RUN is disabled, skip running VGA option
878 * ROMs when coming out of an S3 resume.
880 if (!CONFIG(S3_VGA_ROM_RUN) && acpi_is_wakeup_s3() &&
881 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
882 return 0;
883 if (CONFIG(ALWAYS_LOAD_OPROM))
884 return 1;
885 if (should_run_oprom(dev, NULL))
886 return 1;
888 return 0;
891 static void oprom_pre_graphics_stall(void)
893 if (CONFIG_PRE_GRAPHICS_DELAY_MS)
894 mdelay(CONFIG_PRE_GRAPHICS_DELAY_MS);
897 /** Default handler: only runs the relevant PCI BIOS. */
898 void pci_dev_init(struct device *dev)
900 struct rom_header *rom, *ram;
902 if (!CONFIG(VGA_ROM_RUN))
903 return;
905 /* Only execute VGA ROMs. */
906 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
907 return;
909 if (!should_load_oprom(dev))
910 return;
911 timestamp_add_now(TS_OPROM_INITIALIZE);
913 rom = pci_rom_probe(dev);
914 if (rom == NULL)
915 return;
917 ram = pci_rom_load(dev, rom);
918 if (ram == NULL)
919 return;
920 timestamp_add_now(TS_OPROM_COPY_END);
922 if (!should_run_oprom(dev, rom))
923 return;
925 /* Wait for any configured pre-graphics delay */
926 oprom_pre_graphics_stall();
928 run_bios(dev, (unsigned long)ram);
930 gfx_set_init_done(1);
931 printk(BIOS_DEBUG, "VGA Option ROM was run\n");
932 timestamp_add_now(TS_OPROM_END);
935 /** Default device operation for PCI devices */
936 struct pci_operations pci_dev_ops_pci = {
937 .set_subsystem = pci_dev_set_subsystem,
940 struct device_operations default_pci_ops_dev = {
941 .read_resources = pci_dev_read_resources,
942 .set_resources = pci_dev_set_resources,
943 .enable_resources = pci_dev_enable_resources,
944 #if CONFIG(HAVE_ACPI_TABLES)
945 .write_acpi_tables = pci_rom_write_acpi_tables,
946 .acpi_fill_ssdt = pci_rom_ssdt,
947 #endif
948 .init = pci_dev_init,
949 .ops_pci = &pci_dev_ops_pci,
952 /** Default device operations for PCI bridges */
953 struct device_operations default_pci_ops_bus = {
954 .read_resources = pci_bus_read_resources,
955 .set_resources = pci_dev_set_resources,
956 .enable_resources = pci_bus_enable_resources,
957 .scan_bus = pci_scan_bridge,
958 .reset_bus = pci_bus_reset,
961 /** Default device operations for PCI devices marked 'hidden' */
962 static struct device_operations default_hidden_pci_ops_dev = {
963 .read_resources = noop_read_resources,
964 .set_resources = noop_set_resources,
965 .scan_bus = scan_static_bus,
969 * Check for compatibility to route legacy VGA cycles through a bridge.
971 * Originally, when decoding i/o ports for legacy VGA cycles, bridges
972 * should only consider the 10 least significant bits of the port address.
973 * This means all VGA registers were aliased every 1024 ports!
974 * e.g. 0x3b0 was also decoded as 0x7b0, 0xbb0 etc.
976 * To avoid this mess, a bridge control bit (VGA16) was introduced in
977 * 2003 to enable decoding of 16-bit port addresses. As we don't want
978 * to make this any more complex for now, we use this bit if possible
979 * and only warn if it's not supported (in set_vga_bridge_bits()).
981 static void pci_bridge_vga_compat(struct bus *const bus)
983 uint16_t bridge_ctrl;
985 bridge_ctrl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
987 /* Ensure VGA decoding is disabled during probing (it should
988 be by default, but we run blobs nowadays) */
989 bridge_ctrl &= ~PCI_BRIDGE_CTL_VGA;
990 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, bridge_ctrl);
992 /* If the upstream bridge doesn't support VGA16, we don't have to check */
993 bus->no_vga16 |= bus->dev->upstream->no_vga16;
994 if (bus->no_vga16)
995 return;
997 /* Test if we can enable 16-bit decoding */
998 bridge_ctrl |= PCI_BRIDGE_CTL_VGA16;
999 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, bridge_ctrl);
1000 bridge_ctrl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
1002 bus->no_vga16 = !(bridge_ctrl & PCI_BRIDGE_CTL_VGA16);
1006 * Detect the type of downstream bridge.
1008 * This function is a heuristic to detect which type of bus is downstream
1009 * of a PCI-to-PCI bridge. This functions by looking for various capability
1010 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
1011 * Hypertransport all seem to have appropriate capabilities.
1013 * When only a PCI-Express capability is found the type is examined to see
1014 * which type of bridge we have.
1016 * @param dev Pointer to the device structure of the bridge.
1017 * @return Appropriate bridge operations.
1019 static struct device_operations *get_pci_bridge_ops(struct device *dev)
1021 #if CONFIG(PCIX_PLUGIN_SUPPORT)
1022 unsigned int pcixpos;
1023 pcixpos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1024 if (pcixpos) {
1025 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
1026 return &default_pcix_ops_bus;
1028 #endif
1029 #if CONFIG(PCIEXP_PLUGIN_SUPPORT)
1030 unsigned int pciexpos;
1031 pciexpos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
1032 if (pciexpos) {
1033 u16 flags;
1034 flags = pci_read_config16(dev, pciexpos + PCI_EXP_FLAGS);
1035 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
1036 case PCI_EXP_TYPE_ROOT_PORT:
1037 case PCI_EXP_TYPE_UPSTREAM:
1038 case PCI_EXP_TYPE_DOWNSTREAM:
1039 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
1040 dev_path(dev));
1041 if (CONFIG(PCIEXP_HOTPLUG)) {
1042 u16 sltcap;
1043 sltcap = pci_read_config16(dev, pciexpos + PCI_EXP_SLTCAP);
1044 if (sltcap & PCI_EXP_SLTCAP_HPC) {
1045 printk(BIOS_DEBUG, "%s hot-plug capable\n",
1046 dev_path(dev));
1047 return &default_pciexp_hotplug_ops_bus;
1050 return &default_pciexp_ops_bus;
1051 case PCI_EXP_TYPE_PCI_BRIDGE:
1052 printk(BIOS_DEBUG, "%s subordinate PCI\n",
1053 dev_path(dev));
1054 return &default_pci_ops_bus;
1055 default:
1056 break;
1059 #endif
1060 return &default_pci_ops_bus;
1064 * Check if a device id matches a PCI driver entry.
1066 * The driver entry can either point at a zero terminated array of acceptable
1067 * device IDs, or include a single device ID.
1069 * @param driver pointer to the PCI driver entry being checked
1070 * @param device_id PCI device ID of the device being matched
1072 static int device_id_match(struct pci_driver *driver, unsigned short device_id)
1074 if (driver->devices) {
1075 unsigned short check_id;
1076 const unsigned short *device_list = driver->devices;
1077 while ((check_id = *device_list++) != 0)
1078 if (check_id == device_id)
1079 return 1;
1082 return (driver->device == device_id);
1086 * Set up PCI device operation.
1088 * Check if it already has a driver. If not, use find_device_operations(),
1089 * or set to a default based on type.
1091 * @param dev Pointer to the device whose pci_ops you want to set.
1092 * @see pci_drivers
1094 static void set_pci_ops(struct device *dev)
1096 struct pci_driver *driver;
1098 if (dev->ops)
1099 return;
1102 * Look through the list of setup drivers and find one for
1103 * this PCI device.
1105 for (driver = &_pci_drivers[0]; driver != &_epci_drivers[0]; driver++) {
1106 if ((driver->vendor == dev->vendor) &&
1107 device_id_match(driver, dev->device)) {
1108 dev->ops = (struct device_operations *)driver->ops;
1109 break;
1113 if (dev->ops) {
1114 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n", dev_path(dev),
1115 driver->vendor, driver->device, (driver->ops->scan_bus ? "bus " : ""));
1116 return;
1119 /* If I don't have a specific driver use the default operations. */
1120 switch (dev->hdr_type & 0x7f) { /* Header type */
1121 case PCI_HEADER_TYPE_NORMAL:
1122 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
1123 goto bad;
1124 dev->ops = &default_pci_ops_dev;
1125 break;
1126 case PCI_HEADER_TYPE_BRIDGE:
1127 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1128 goto bad;
1129 dev->ops = get_pci_bridge_ops(dev);
1130 break;
1131 #if CONFIG(CARDBUS_PLUGIN_SUPPORT)
1132 case PCI_HEADER_TYPE_CARDBUS:
1133 dev->ops = &default_cardbus_ops_bus;
1134 break;
1135 #endif
1136 default:
1137 bad:
1138 if (dev->enabled) {
1139 printk(BIOS_ERR,
1140 "%s [%04x/%04x/%06x] has unknown header type %02x, ignoring.\n",
1141 dev_path(dev), dev->vendor, dev->device,
1142 dev->class >> 8, dev->hdr_type);
1148 * See if we have already allocated a device structure for a given devfn.
1150 * Given a PCI bus structure and a devfn number, find the device structure
1151 * corresponding to the devfn, if present. Then move the device structure
1152 * as the last child on the bus.
1154 * @param bus Pointer to the bus structure.
1155 * @param devfn A device/function number.
1156 * @return Pointer to the device structure found or NULL if we have not
1157 * allocated a device for this devfn yet.
1159 static struct device *pci_scan_get_dev(struct bus *bus, unsigned int devfn)
1161 struct device *dev, **prev;
1163 prev = &bus->children;
1164 for (dev = bus->children; dev; dev = dev->sibling) {
1165 if (dev->path.type == DEVICE_PATH_PCI && dev->path.pci.devfn == devfn) {
1166 /* Unlink from the list. */
1167 *prev = dev->sibling;
1168 dev->sibling = NULL;
1169 break;
1171 prev = &dev->sibling;
1175 * Just like alloc_dev() add the device to the list of devices on the
1176 * bus. When the list of devices was formed we removed all of the
1177 * parents children, and now we are interleaving static and dynamic
1178 * devices in order on the bus.
1180 if (dev) {
1181 struct device *child;
1183 /* Find the last child on the bus. */
1184 for (child = bus->children; child && child->sibling;)
1185 child = child->sibling;
1187 /* Place the device as last on the bus. */
1188 if (child)
1189 child->sibling = dev;
1190 else
1191 bus->children = dev;
1194 return dev;
1198 * Scan a PCI bus.
1200 * Determine the existence of a given PCI device. Allocate a new struct device
1201 * if dev==NULL was passed in and the device exists in hardware.
1203 * @param dev Pointer to the dev structure.
1204 * @param bus Pointer to the bus structure.
1205 * @param devfn A device/function number to look at.
1206 * @return The device structure for the device (if found), NULL otherwise.
1208 struct device *pci_probe_dev(struct device *dev, struct bus *bus,
1209 unsigned int devfn)
1211 u32 id, class;
1212 u8 hdr_type;
1214 /* Detect if a device is present. */
1215 if (!dev) {
1216 struct device dummy;
1218 dummy.upstream = bus;
1219 dummy.path.type = DEVICE_PATH_PCI;
1220 dummy.path.pci.devfn = devfn;
1222 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
1224 * Have we found something? Some broken boards return 0 if a
1225 * slot is empty, but the expected answer is 0xffffffff.
1227 if (id == 0xffffffff)
1228 return NULL;
1230 if ((id == 0x00000000) || (id == 0x0000ffff) ||
1231 (id == 0xffff0000)) {
1232 printk(BIOS_SPEW, "%s, bad id 0x%x\n",
1233 dev_path(&dummy), id);
1234 return NULL;
1236 dev = alloc_dev(bus, &dummy.path);
1237 } else {
1239 * Enable/disable the device. Once we have found the device-
1240 * specific operations this operations we will disable the
1241 * device with those as well.
1243 * This is geared toward devices that have subfunctions
1244 * that do not show up by default.
1246 * If a device is a stuff option on the motherboard
1247 * it may be absent and enable_dev() must cope.
1249 /* Run the magic enable sequence for the device. */
1250 if (dev->chip_ops && dev->chip_ops->enable_dev)
1251 dev->chip_ops->enable_dev(dev);
1253 /* Now read the vendor and device ID. */
1254 id = pci_read_config32(dev, PCI_VENDOR_ID);
1257 * If the device does not have a PCI ID disable it. Possibly
1258 * this is because we have already disabled the device. But
1259 * this also handles optional devices that may not always
1260 * show up.
1262 /* If the chain is fully enumerated quit */
1263 if ((id == 0xffffffff) || (id == 0x00000000) ||
1264 (id == 0x0000ffff) || (id == 0xffff0000)) {
1265 if (dev->enabled) {
1266 printk(BIOS_INFO,
1267 "PCI: Static device %s not found, disabling it.\n",
1268 dev_path(dev));
1269 dev->enabled = 0;
1271 return dev;
1275 /* Read the rest of the PCI configuration information. */
1276 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1277 class = pci_read_config32(dev, PCI_CLASS_REVISION);
1279 /* Store the interesting information in the device structure. */
1280 dev->vendor = id & 0xffff;
1281 dev->device = (id >> 16) & 0xffff;
1282 dev->hdr_type = hdr_type;
1284 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
1285 dev->class = class >> 8;
1287 /* Architectural/System devices always need to be bus masters. */
1288 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM &&
1289 CONFIG(PCI_ALLOW_BUS_MASTER_ANY_DEVICE))
1290 dev->command |= PCI_COMMAND_MASTER;
1293 * Look at the vendor and device ID, or at least the header type and
1294 * class and figure out which set of configuration methods to use.
1295 * Unless we already have some PCI ops.
1297 set_pci_ops(dev);
1299 /* Now run the magic enable/disable sequence for the device. */
1300 if (dev->ops && dev->ops->enable)
1301 dev->ops->enable(dev);
1303 /* Display the device. */
1304 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n", dev_path(dev),
1305 dev->vendor, dev->device, dev->enabled ? "enabled" : "disabled",
1306 dev->ops ? "" : " No operations");
1308 return dev;
1312 * Test for match between romstage and ramstage device instance.
1314 * @param dev Pointer to the device structure.
1315 * @param sdev Simple device model identifier, created with PCI_DEV().
1316 * @return Non-zero if bus:dev.fn of device matches.
1318 unsigned int pci_match_simple_dev(struct device *dev, pci_devfn_t sdev)
1320 return dev->upstream->secondary == PCI_DEV2BUS(sdev) &&
1321 dev->upstream->segment_group == PCI_DEV2SEG(sdev) &&
1322 dev->path.pci.devfn == PCI_DEV2DEVFN(sdev);
1326 * Test whether a capability is available along the whole path from the given
1327 * device to the host bridge.
1329 * @param dev Pointer to the device structure.
1330 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
1331 * @return The next matching capability of the given device, if it is available
1332 * along the whole path, or zero if not.
1334 uint16_t pci_find_cap_recursive(const struct device *dev, uint16_t cap)
1336 assert(dev->upstream);
1337 uint16_t pos = pci_find_capability(dev, cap);
1338 const struct device *bridge = dev->upstream->dev;
1339 while (bridge && (bridge->path.type == DEVICE_PATH_PCI)) {
1340 assert(bridge->upstream);
1341 if (!pci_find_capability(bridge, cap))
1342 return 0;
1343 bridge = bridge->upstream->dev;
1345 return pos;
1349 * PCI devices that are marked as "hidden" do not get probed. However, the same
1350 * initialization logic is still performed as if it were. This is useful when
1351 * devices would like to be described in the devicetree.cb file, and/or present
1352 * static PCI resources to the allocator, but the platform firmware hides the
1353 * device (makes the device invisible to PCI enumeration) before PCI enumeration
1354 * takes place.
1356 * The expected semantics of PCI devices marked as 'hidden':
1357 * 1) The device is actually present under the specified BDF
1358 * 2) The device config space can still be accessed somehow, but the Vendor ID
1359 * indicates there is no device there (it reads as 0xffffffff).
1360 * 3) The device may still consume PCI resources. Typically, these would have
1361 * been hardcoded elsewhere.
1363 * @param dev Pointer to the device structure.
1365 static void pci_scan_hidden_device(struct device *dev)
1367 if (dev->chip_ops && dev->chip_ops->enable_dev)
1368 dev->chip_ops->enable_dev(dev);
1371 * If chip_ops->enable_dev did not set dev->ops, then set to a default
1372 * .ops, because PCI enumeration is effectively being skipped, therefore
1373 * no PCI driver will bind to this device. However, children may want to
1374 * be enumerated, so this provides scan_static_bus for the .scan_bus
1375 * callback.
1377 if (dev->ops == NULL)
1378 dev->ops = &default_hidden_pci_ops_dev;
1380 if (dev->ops->enable)
1381 dev->ops->enable(dev);
1383 /* Display the device almost as if it were probed normally */
1384 printk(BIOS_DEBUG, "%s [0000/%04x] hidden%s\n", dev_path(dev),
1385 dev->device, dev->ops ? "" : " No operations");
1389 * A PCIe Downstream Port normally leads to a Link with only Device 0 on it
1390 * (PCIe spec r5.0, sec 7.3.1). As an optimization, scan only for Device 0 in
1391 * that situation.
1393 * @param bus Pointer to the bus structure.
1395 static bool pci_bus_only_one_child(struct bus *bus)
1397 struct device *bridge = bus->dev;
1398 u16 pcie_pos, pcie_flags_reg;
1399 int pcie_type;
1401 if (!bridge)
1402 return false;
1404 if (bridge->path.type != DEVICE_PATH_PCI)
1405 return false;
1407 pcie_pos = pci_find_capability(bridge, PCI_CAP_ID_PCIE);
1408 if (!pcie_pos)
1409 return false;
1411 pcie_flags_reg = pci_read_config16(bridge, pcie_pos + PCI_EXP_FLAGS);
1413 pcie_type = (pcie_flags_reg & PCI_EXP_FLAGS_TYPE) >> 4;
1415 return pciexp_is_downstream_port(pcie_type);
1419 * Scan a PCI bus.
1421 * Determine the existence of devices and bridges on a PCI bus. If there are
1422 * bridges on the bus, recursively scan the buses behind the bridges.
1424 * @param bus Pointer to the bus structure.
1425 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1426 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
1428 void pci_scan_bus(struct bus *bus, unsigned int min_devfn,
1429 unsigned int max_devfn)
1431 unsigned int devfn;
1432 struct device *dev, **prev;
1433 int once = 0;
1435 printk(BIOS_DEBUG, "PCI: %s for segment group %02x bus %02x\n", __func__,
1436 bus->segment_group, bus->secondary);
1438 /* Maximum sane devfn is 0xFF. */
1439 if (max_devfn > 0xff) {
1440 printk(BIOS_ERR, "PCI: %s limits devfn %x - devfn %x\n",
1441 __func__, min_devfn, max_devfn);
1442 printk(BIOS_ERR, "PCI: %s upper limit too big. Using 0xff.\n", __func__);
1443 max_devfn=0xff;
1446 post_code(POSTCODE_ENTER_PCI_SCAN_BUS);
1448 if (pci_bus_only_one_child(bus))
1449 max_devfn = MIN(max_devfn, 0x07);
1452 * Probe all devices/functions on this bus with some optimization for
1453 * non-existence and single function devices.
1455 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
1456 if (CONFIG(MINIMAL_PCI_SCANNING)) {
1457 dev = pcidev_path_behind(bus, devfn);
1458 if (!dev || !dev->mandatory)
1459 continue;
1462 /* First thing setup the device structure. */
1463 dev = pci_scan_get_dev(bus, devfn);
1465 /* Devices marked 'hidden' do not get probed */
1466 if (dev && dev->hidden) {
1467 pci_scan_hidden_device(dev);
1469 /* Skip pci_probe_dev, go to next devfn */
1470 continue;
1473 /* See if a device is present and setup the device structure. */
1474 dev = pci_probe_dev(dev, bus, devfn);
1477 * If this is not a multi function device, or the device is
1478 * not present don't waste time probing another function.
1479 * Skip to next device.
1481 if ((PCI_FUNC(devfn) == 0x00) && (!dev
1482 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
1483 devfn += 0x07;
1488 * Warn if any leftover static devices are found.
1489 * There's probably a problem in devicetree.cb.
1492 prev = &bus->children;
1493 for (dev = bus->children; dev; dev = dev->sibling) {
1495 * If static device is not PCI then enable it here and don't
1496 * treat it as a leftover device.
1498 if (dev->path.type != DEVICE_PATH_PCI) {
1499 enable_static_device(dev);
1500 continue;
1504 * The device is only considered leftover if it is not hidden
1505 * and it has a Vendor ID of 0 (the default for a device that
1506 * could not be probed).
1508 if (dev->vendor != 0 || dev->hidden) {
1509 prev = &dev->sibling;
1510 continue;
1513 /* Unlink it from list. */
1514 *prev = dev->sibling;
1516 if (!once++)
1517 printk(BIOS_WARNING, "PCI: Leftover static devices:\n");
1518 printk(BIOS_WARNING, "%s\n", dev_path(dev));
1521 if (once)
1522 printk(BIOS_WARNING, "PCI: Check your devicetree.cb.\n");
1525 * For all children that implement scan_bus() (i.e. bridges)
1526 * scan the bus behind that child.
1529 scan_bridges(bus);
1532 * We've scanned the bus and so we know all about what's on the other
1533 * side of any bridges that may be on this bus plus any devices.
1534 * Return how far we've got finding sub-buses.
1536 post_code(POSTCODE_EXIT_PCI_SCAN_BUS);
1539 typedef enum {
1540 PCI_ROUTE_CLOSE,
1541 PCI_ROUTE_SCAN,
1542 PCI_ROUTE_FINAL,
1543 } scan_state;
1545 static void pci_bridge_route(struct bus *link, scan_state state)
1547 struct device *dev = link->dev;
1548 struct bus *parent = dev->upstream;
1549 uint8_t primary, secondary, subordinate;
1551 if (state == PCI_ROUTE_SCAN) {
1552 link->secondary = parent->subordinate + 1;
1553 link->subordinate = link->secondary + dev->hotplug_buses;
1554 link->max_subordinate = parent->max_subordinate
1555 ? parent->max_subordinate
1556 : (PCI_BUSES_PER_SEGMENT_GROUP - 1);
1557 link->segment_group = parent->segment_group;
1560 if (link->secondary > link->max_subordinate)
1561 die("%s: No more busses available!\n", __func__);
1563 /* This ought to only happen with hotplug buses. */
1564 if (link->subordinate > link->max_subordinate) {
1565 printk(BIOS_WARNING, "%s: Limiting subordinate busses\n", __func__);
1566 link->subordinate = link->max_subordinate;
1569 if (state == PCI_ROUTE_CLOSE) {
1570 primary = 0;
1571 secondary = 0xff;
1572 subordinate = 0xfe;
1573 } else if (state == PCI_ROUTE_SCAN) {
1574 primary = parent->secondary;
1575 secondary = link->secondary;
1576 subordinate = link->max_subordinate;
1577 } else if (state == PCI_ROUTE_FINAL) {
1578 primary = parent->secondary;
1579 secondary = link->secondary;
1580 subordinate = link->subordinate;
1581 } else {
1582 return;
1585 if (state == PCI_ROUTE_SCAN) {
1586 /* Clear all status bits and turn off memory, I/O and master enables. */
1587 link->bridge_cmd = pci_read_config16(dev, PCI_COMMAND);
1588 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1589 pci_write_config16(dev, PCI_STATUS, 0xffff);
1593 * Configure the bus numbers for this bridge: the configuration
1594 * transactions will not be propagated by the bridge if it is not
1595 * correctly configured.
1597 pci_write_config8(dev, PCI_PRIMARY_BUS, primary);
1598 pci_write_config8(dev, PCI_SECONDARY_BUS, secondary);
1599 pci_write_config8(dev, PCI_SUBORDINATE_BUS, subordinate);
1601 if (state == PCI_ROUTE_FINAL) {
1602 pci_write_config16(dev, PCI_COMMAND, link->bridge_cmd);
1603 parent->subordinate = link->subordinate;
1608 * Scan a PCI bridge and the buses behind the bridge.
1610 * Determine the existence of buses behind the bridge. Set up the bridge
1611 * according to the result of the scan.
1613 * This function is the default scan_bus() method for PCI bridge devices.
1615 * @param dev Pointer to the bridge device.
1616 * @param do_scan_bus TODO
1618 void do_pci_scan_bridge(struct device *dev,
1619 void (*do_scan_bus) (struct bus * bus,
1620 unsigned int min_devfn,
1621 unsigned int max_devfn))
1623 struct bus *bus;
1625 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
1627 if (dev->downstream == NULL) {
1628 struct bus *link;
1629 link = malloc(sizeof(*link));
1630 if (link == NULL)
1631 die("Couldn't allocate a link!\n");
1632 memset(link, 0, sizeof(*link));
1633 link->dev = dev;
1634 dev->downstream = link;
1637 bus = dev->downstream;
1639 pci_bridge_vga_compat(bus);
1641 pci_bridge_route(bus, PCI_ROUTE_SCAN);
1643 do_scan_bus(bus, 0x00, 0xff);
1645 pci_bridge_route(bus, PCI_ROUTE_FINAL);
1649 * Scan a PCI bridge and the buses behind the bridge.
1651 * Determine the existence of buses behind the bridge. Set up the bridge
1652 * according to the result of the scan.
1654 * This function is the default scan_bus() method for PCI bridge devices.
1656 * @param dev Pointer to the bridge device.
1658 void pci_scan_bridge(struct device *dev)
1660 do_pci_scan_bridge(dev, pci_scan_bus);
1664 * Scan a PCI domain.
1666 * This function is the default scan_bus() method for PCI domains.
1668 * @param dev Pointer to the domain.
1670 void pci_host_bridge_scan_bus(struct device *dev)
1672 struct bus *link = dev->downstream;
1673 pci_scan_bus(link, PCI_DEVFN(0, 0), 0xff);
1676 void pci_dev_disable_bus_master(const struct device *dev)
1678 pci_update_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MASTER, 0x0);
1682 * Take an INT_PIN number (0, 1 - 4) and convert
1683 * it to a string ("NO PIN", "PIN A" - "PIN D")
1685 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1686 * @return A string corresponding to the pin number or "Invalid"
1688 const char *pin_to_str(int pin)
1690 const char *str[5] = {
1691 "NO PIN",
1692 "PIN A",
1693 "PIN B",
1694 "PIN C",
1695 "PIN D",
1698 if (pin >= 0 && pin <= 4)
1699 return str[pin];
1700 else
1701 return "Invalid PIN, not 0 - 4";
1705 * Get the PCI INT_PIN swizzle for a device defined as:
1706 * pin_parent = (pin_child + devn_child) % 4 + 1
1707 * where PIN A = 1 ... PIN_D = 4
1709 * Given a PCI device structure 'dev', find the interrupt pin
1710 * that will be triggered on its parent bridge device when
1711 * generating an interrupt. For example: Device 1:3.2 may
1712 * use INT_PIN A but will trigger PIN D on its parent bridge
1713 * device. In this case, this function will return 4 (PIN D).
1715 * @param dev A PCI device structure to swizzle interrupt pins for
1716 * @param *parent_bridge The PCI device structure for the bridge
1717 * device 'dev' is attached to
1718 * @return The interrupt pin number (1 - 4) that 'dev' will
1719 * trigger when generating an interrupt
1721 static int swizzle_irq_pins(struct device *dev, struct device **parent_bridge)
1723 struct device *parent; /* Our current device's parent device */
1724 struct device *child; /* The child device of the parent */
1725 uint8_t parent_bus = 0; /* Parent Bus number */
1726 uint16_t parent_devfn = 0; /* Parent Device and Function number */
1727 uint16_t child_devfn = 0; /* Child Device and Function number */
1728 uint8_t swizzled_pin = 0; /* Pin swizzled across a bridge */
1730 /* Start with PIN A = 0 ... D = 3 */
1731 swizzled_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN) - 1;
1733 /* While our current device has parent devices */
1734 child = dev;
1735 for (parent = child->upstream->dev; parent; parent = parent->upstream->dev) {
1736 parent_bus = parent->upstream->secondary;
1737 parent_devfn = parent->path.pci.devfn;
1738 child_devfn = child->path.pci.devfn;
1740 /* Swizzle the INT_PIN for any bridges not on root bus */
1741 swizzled_pin = (PCI_SLOT(child_devfn) + swizzled_pin) % 4;
1742 printk(BIOS_SPEW, "\tWith INT_PIN swizzled to %s\n"
1743 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1744 pin_to_str(swizzled_pin + 1), parent_bus,
1745 PCI_SLOT(parent_devfn), PCI_FUNC(parent_devfn));
1747 /* Continue until we find the root bus */
1748 if (parent_bus > 0) {
1750 * We will go on to the next parent so this parent
1751 * becomes the child
1753 child = parent;
1754 continue;
1755 } else {
1757 * Found the root bridge device,
1758 * fill in the structure and exit
1760 *parent_bridge = parent;
1761 break;
1765 /* End with PIN A = 1 ... D = 4 */
1766 return swizzled_pin + 1;
1770 * Given a device structure 'dev', find its interrupt pin
1771 * and its parent bridge 'parent_bdg' device structure.
1772 * If it is behind a bridge, it will return the interrupt
1773 * pin number (1 - 4) of the parent bridge that the device
1774 * interrupt pin has been swizzled to, otherwise it will
1775 * return the interrupt pin that is programmed into the
1776 * PCI config space of the target device. If 'dev' is
1777 * behind a bridge, it will fill in 'parent_bdg' with the
1778 * device structure of the bridge it is behind, otherwise
1779 * it will copy 'dev' into 'parent_bdg'.
1781 * @param dev A PCI device structure to get interrupt pins for.
1782 * @param *parent_bdg The PCI device structure for the bridge
1783 * device 'dev' is attached to.
1784 * @return The interrupt pin number (1 - 4) that 'dev' will
1785 * trigger when generating an interrupt.
1786 * Errors: -1 is returned if the device is not enabled
1787 * -2 is returned if a parent bridge could not be found.
1789 int get_pci_irq_pins(struct device *dev, struct device **parent_bdg)
1791 uint8_t bus = 0; /* The bus this device is on */
1792 uint16_t devfn = 0; /* This device's device and function numbers */
1793 uint8_t int_pin = 0; /* Interrupt pin used by the device */
1794 uint8_t target_pin = 0; /* Interrupt pin we want to assign an IRQ to */
1796 /* Make sure this device is enabled */
1797 if (!(dev->enabled && (dev->path.type == DEVICE_PATH_PCI)))
1798 return -1;
1800 bus = dev->upstream->secondary;
1801 devfn = dev->path.pci.devfn;
1803 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1804 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1805 if (int_pin < 1 || int_pin > 4)
1806 return -1;
1808 printk(BIOS_SPEW, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1809 bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pin_to_str(int_pin));
1811 /* If this device is on a bridge, swizzle its INT_PIN */
1812 if (bus) {
1813 /* Swizzle its INT_PINs */
1814 target_pin = swizzle_irq_pins(dev, parent_bdg);
1816 /* Make sure the swizzle returned valid structures */
1817 if (parent_bdg == NULL) {
1818 printk(BIOS_WARNING, "Could not find parent bridge for this device!\n");
1819 return -2;
1821 } else { /* Device is not behind a bridge */
1822 target_pin = int_pin; /* Return its own interrupt pin */
1823 *parent_bdg = dev; /* Return its own structure */
1826 /* Target pin is the interrupt pin we want to assign an IRQ to */
1827 return target_pin;
1830 #if CONFIG(PC80_SYSTEM)
1832 * Assign IRQ numbers.
1834 * This function assigns IRQs for all functions contained within the indicated
1835 * device address. If the device does not exist or does not require interrupts
1836 * then this function has no effect.
1838 * This function should be called for each PCI slot in your system.
1840 * @param dev Pointer to dev structure.
1841 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1842 * of this slot. The particular IRQ #s that are passed in depend on the
1843 * routing inside your southbridge and on your board.
1845 void pci_assign_irqs(struct device *dev, const unsigned char pIntAtoD[4])
1847 u8 slot, line, irq;
1849 /* Each device may contain up to eight functions. */
1850 slot = dev->path.pci.devfn >> 3;
1852 for (; dev ; dev = dev->sibling) {
1853 if (dev->path.pci.devfn >> 3 != slot)
1854 break;
1856 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1858 /* PCI spec says all values except 1..4 are reserved. */
1859 if ((line < 1) || (line > 4))
1860 continue;
1862 irq = pIntAtoD[line - 1];
1864 printk(BIOS_DEBUG, "Assigning IRQ %d to %s\n", irq, dev_path(dev));
1866 pci_write_config8(dev, PCI_INTERRUPT_LINE, irq);
1868 /* Change to level triggered. */
1869 i8259_configure_irq_trigger(irq, IRQ_LEVEL_TRIGGERED);
1872 #endif