1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * Originally based on the Linux kernel (drivers/pci/pci.c).
5 * PCI Bus Services, see include/linux/pci.h for further explanation.
10 #include <device/pci_ops.h>
12 #include <console/console.h>
17 #include <device/cardbus.h>
18 #include <device/device.h>
19 #include <device/pci.h>
20 #include <device/pci_ids.h>
21 #include <device/pcix.h>
22 #include <device/pciexp.h>
24 #include <pc80/i8259.h>
25 #include <security/vboot/vbnv.h>
26 #include <timestamp.h>
29 u8
pci_moving_config8(struct device
*dev
, unsigned int reg
)
31 u8 value
, ones
, zeroes
;
33 value
= pci_read_config8(dev
, reg
);
35 pci_write_config8(dev
, reg
, 0xff);
36 ones
= pci_read_config8(dev
, reg
);
38 pci_write_config8(dev
, reg
, 0x00);
39 zeroes
= pci_read_config8(dev
, reg
);
41 pci_write_config8(dev
, reg
, value
);
46 u16
pci_moving_config16(struct device
*dev
, unsigned int reg
)
48 u16 value
, ones
, zeroes
;
50 value
= pci_read_config16(dev
, reg
);
52 pci_write_config16(dev
, reg
, 0xffff);
53 ones
= pci_read_config16(dev
, reg
);
55 pci_write_config16(dev
, reg
, 0x0000);
56 zeroes
= pci_read_config16(dev
, reg
);
58 pci_write_config16(dev
, reg
, value
);
63 u32
pci_moving_config32(struct device
*dev
, unsigned int reg
)
65 u32 value
, ones
, zeroes
;
67 value
= pci_read_config32(dev
, reg
);
69 pci_write_config32(dev
, reg
, 0xffffffff);
70 ones
= pci_read_config32(dev
, reg
);
72 pci_write_config32(dev
, reg
, 0x00000000);
73 zeroes
= pci_read_config32(dev
, reg
);
75 pci_write_config32(dev
, reg
, value
);
81 * Given a device and register, read the size of the BAR for that register.
83 * @param dev Pointer to the device structure.
84 * @param index Address of the PCI configuration register.
87 struct resource
*pci_get_resource(struct device
*dev
, unsigned long index
)
89 struct resource
*resource
;
90 unsigned long value
, attr
;
91 resource_t moving
, limit
;
93 /* Initialize the resources to nothing. */
94 resource
= new_resource(dev
, index
);
96 /* Get the initial value. */
97 value
= pci_read_config32(dev
, index
);
99 /* See which bits move. */
100 moving
= pci_moving_config32(dev
, index
);
102 /* Initialize attr to the bits that do not move. */
103 attr
= value
& ~moving
;
105 /* If it is a 64bit resource look at the high half as well. */
106 if (((attr
& PCI_BASE_ADDRESS_SPACE_IO
) == 0) &&
107 ((attr
& PCI_BASE_ADDRESS_MEM_LIMIT_MASK
) ==
108 PCI_BASE_ADDRESS_MEM_LIMIT_64
)) {
109 /* Find the high bits that move. */
111 ((resource_t
)pci_moving_config32(dev
, index
+ 4)) << 32;
114 /* Find the resource constraints.
115 * Start by finding the bits that move. From there:
116 * - Size is the least significant bit of the bits that move.
117 * - Limit is all of the bits that move plus all of the lower bits.
118 * See PCI Spec 6.2.5.1.
123 resource
->align
= resource
->gran
= 0;
124 while (!(moving
& resource
->size
)) {
125 resource
->size
<<= 1;
126 resource
->align
+= 1;
129 resource
->limit
= limit
= moving
| (resource
->size
- 1);
131 if (pci_base_address_is_memory_space(attr
)) {
132 /* Page-align to allow individual mapping of devices. */
133 if (resource
->align
< 12)
134 resource
->align
= 12;
139 * Some broken hardware has read-only registers that do not
140 * really size correctly.
142 * Example: the Acer M7229 has BARs 1-4 normally read-only,
143 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
144 * by writing 0xffffffff to it, it will read back as 0x1f1 -- which
145 * is a violation of the spec.
147 * We catch this case and ignore it by observing which bits move.
149 * This also catches the common case of unimplemented registers
150 * that always read back as 0.
154 printk(BIOS_DEBUG
, "%s register %02lx(%08lx), read-only ignoring it\n",
155 dev_path(dev
), index
, value
);
158 } else if (attr
& PCI_BASE_ADDRESS_SPACE_IO
) {
159 /* An I/O mapped base address. */
160 resource
->flags
|= IORESOURCE_IO
;
161 /* I don't want to deal with 32bit I/O resources. */
162 resource
->limit
= 0xffff;
164 /* A Memory mapped base address. */
165 attr
&= PCI_BASE_ADDRESS_MEM_ATTR_MASK
;
166 resource
->flags
|= IORESOURCE_MEM
;
167 if (attr
& PCI_BASE_ADDRESS_MEM_PREFETCH
) {
168 resource
->flags
|= IORESOURCE_PREFETCH
;
169 if (CONFIG(PCIEXP_HOTPLUG_PREFETCH_MEM_ABOVE_4G
)
170 && dev_path_hotplug(dev
))
171 resource
->flags
|= IORESOURCE_ABOVE_4G
;
173 attr
&= PCI_BASE_ADDRESS_MEM_LIMIT_MASK
;
174 if (attr
== PCI_BASE_ADDRESS_MEM_LIMIT_32
) {
176 resource
->limit
= 0xffffffffUL
;
177 } else if (attr
== PCI_BASE_ADDRESS_MEM_LIMIT_1M
) {
179 resource
->limit
= 0x000fffffUL
;
180 } else if (attr
== PCI_BASE_ADDRESS_MEM_LIMIT_64
) {
182 resource
->limit
= 0xffffffffffffffffULL
;
183 resource
->flags
|= IORESOURCE_PCI64
;
186 printk(BIOS_ERR
, "Broken BAR with value %lx\n", attr
);
187 printk(BIOS_ERR
, " on dev %s at index %02lx\n",
188 dev_path(dev
), index
);
193 /* Don't let the limit exceed which bits can move. */
194 if (resource
->limit
> limit
)
195 resource
->limit
= limit
;
201 * Given a device and an index, read the size of the BAR for that register.
203 * @param dev Pointer to the device structure.
204 * @param index Address of the PCI configuration register.
206 static void pci_get_rom_resource(struct device
*dev
, unsigned long index
)
208 struct resource
*resource
;
212 /* Initialize the resources to nothing. */
213 resource
= new_resource(dev
, index
);
215 /* Get the initial value. */
216 value
= pci_read_config32(dev
, index
);
218 /* See which bits move. */
219 moving
= pci_moving_config32(dev
, index
);
221 /* Clear the Enable bit. */
222 moving
= moving
& ~PCI_ROM_ADDRESS_ENABLE
;
224 /* Find the resource constraints.
225 * Start by finding the bits that move. From there:
226 * - Size is the least significant bit of the bits that move.
227 * - Limit is all of the bits that move plus all of the lower bits.
228 * See PCI Spec 6.2.5.1.
232 resource
->align
= resource
->gran
= 0;
233 while (!(moving
& resource
->size
)) {
234 resource
->size
<<= 1;
235 resource
->align
+= 1;
238 resource
->limit
= moving
| (resource
->size
- 1);
239 resource
->flags
|= IORESOURCE_MEM
| IORESOURCE_READONLY
;
242 printk(BIOS_DEBUG
, "%s register %02lx(%08lx), read-only ignoring it\n",
243 dev_path(dev
), index
, value
);
247 compact_resources(dev
);
251 * Given a device, read the size of the MSI-X table.
253 * @param dev Pointer to the device structure.
254 * @return MSI-X table size or 0 if not MSI-X capable device
256 size_t pci_msix_table_size(struct device
*dev
)
258 const size_t pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
262 const u16 control
= pci_read_config16(dev
, pos
+ PCI_MSIX_FLAGS
);
263 return (control
& PCI_MSIX_FLAGS_QSIZE
) + 1;
267 * Given a device, return the table offset and bar the MSI-X tables resides in.
269 * @param dev Pointer to the device structure.
270 * @param offset Returned value gives the offset in bytes inside the PCI BAR.
271 * @param idx The returned value is the index of the PCI_BASE_ADDRESS register
272 * the MSI-X table is located in.
273 * @return Zero on success
275 int pci_msix_table_bar(struct device
*dev
, u32
*offset
, u8
*idx
)
277 const size_t pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
278 if (!pos
|| !offset
|| !idx
)
281 *offset
= pci_read_config32(dev
, pos
+ PCI_MSIX_TABLE
);
282 *idx
= (u8
)(*offset
& PCI_MSIX_PBA_BIR
);
283 *offset
&= PCI_MSIX_PBA_OFFSET
;
289 * Given a device, return a msix_entry pointer or NULL if no table was found.
291 * @param dev Pointer to the device structure.
293 * @return NULL on error
295 struct msix_entry
*pci_msix_get_table(struct device
*dev
)
297 struct resource
*res
;
301 if (pci_msix_table_bar(dev
, &offset
, &idx
))
307 res
= probe_resource(dev
, idx
* 4 + PCI_BASE_ADDRESS_0
);
308 if (!res
|| !res
->base
|| offset
>= res
->size
)
311 if ((res
->flags
& IORESOURCE_PCI64
) &&
312 (uintptr_t)res
->base
!= res
->base
)
315 return (struct msix_entry
*)((uintptr_t)res
->base
+ offset
);
318 static unsigned int get_rebar_offset(const struct device
*dev
, unsigned long index
)
320 uint32_t offset
= pciexp_find_extended_cap(dev
, PCIE_EXT_CAP_RESIZABLE_BAR
, 0);
324 /* Convert PCI_BASE_ADDRESS_0, ..._1, ..._2 into 0, 1, 2... */
325 const unsigned int find_bar_idx
= (index
- PCI_BASE_ADDRESS_0
) /
328 /* Although all of the Resizable BAR Control Registers contain an
329 "NBARs" field, it is only valid in the Control Register for BAR 0 */
330 const uint32_t rebar_ctrl0
= pci_read_config32(dev
, offset
+ PCI_REBAR_CTRL_OFFSET
);
331 const unsigned int nbars
= (rebar_ctrl0
& PCI_REBAR_CTRL_NBARS_MASK
) >>
332 PCI_REBAR_CTRL_NBARS_SHIFT
;
334 for (unsigned int i
= 0; i
< nbars
; i
++, offset
+= sizeof(uint64_t)) {
335 const uint32_t rebar_ctrl
= pci_read_config32(
336 dev
, offset
+ PCI_REBAR_CTRL_OFFSET
);
337 const uint32_t bar_idx
= rebar_ctrl
& PCI_REBAR_CTRL_IDX_MASK
;
338 if (bar_idx
== find_bar_idx
)
345 /* Bit 20 = 1 MiB, bit 21 = 2 MiB, bit 22 = 4 MiB, ... bit 63 = 8 EiB */
346 static uint64_t get_rebar_sizes_mask(const struct device
*dev
,
349 uint64_t size_mask
= 0ULL;
350 const uint32_t offset
= get_rebar_offset(dev
, index
);
354 /* Get 1 MB - 128 TB support from CAP register */
355 const uint32_t cap
= pci_read_config32(dev
, offset
+ PCI_REBAR_CAP_OFFSET
);
356 /* Shift the bits from 4-31 to 0-27 (i.e., down by 4 bits) */
357 size_mask
|= ((cap
& PCI_REBAR_CAP_SIZE_MASK
) >> 4);
359 /* Get 256 TB - 8 EB support from CTRL register and store it in bits 28-43 */
360 const uint64_t ctrl
= pci_read_config32(dev
, offset
+ PCI_REBAR_CTRL_OFFSET
);
361 /* Shift ctrl mask from bit 16 to bit 28, so that the two
362 masks (fom cap and ctrl) form a contiguous bitmask when
363 concatenated (i.e., up by 12 bits). */
364 size_mask
|= ((ctrl
& PCI_REBAR_CTRL_SIZE_MASK
) << 12);
366 /* Now that the mask occupies bits 0-43, shift it up to 20-63, so they
367 represent the actual powers of 2. */
368 return size_mask
<< 20;
371 static void pci_store_rebar_size(const struct device
*dev
,
372 const struct resource
*resource
)
374 const unsigned int num_bits
= __fls64(resource
->size
);
375 const uint32_t offset
= get_rebar_offset(dev
, resource
->index
);
379 pci_update_config32(dev
, offset
+ PCI_REBAR_CTRL_OFFSET
,
380 ~PCI_REBAR_CTRL_SIZE_MASK
,
381 num_bits
<< PCI_REBAR_CTRL_SIZE_SHIFT
);
384 static void configure_adjustable_base(const struct device
*dev
,
386 struct resource
*res
)
389 * Excerpt from an implementation note from the PCIe spec:
391 * System software uses this capability in place of the above mentioned
392 * method of determining the resource size[0], and prior to assigning
393 * the base address to the BAR. Potential usable resource sizes are
394 * reported by the Function via its Resizable BAR Capability and Control
395 * registers. It is intended that the software allocate the largest of
396 * the reported sizes that it can, since allocating less address space
397 * than the largest reported size can result in lower
398 * performance. Software then writes the size to the Resizable BAR
399 * Control register for the appropriate BAR for the Function. Following
400 * this, the base address is written to the BAR.
402 * [0] Referring to using the moving bits in the BAR to determine the
403 * requested size of the MMIO region
405 const uint64_t size_mask
= get_rebar_sizes_mask(dev
, index
);
409 int max_requested_bits
= __fls64(size_mask
);
410 if (max_requested_bits
> CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS
) {
411 printk(BIOS_WARNING
, "Device %s requests a BAR with"
412 " %u bits of address space, which coreboot is not"
413 " configured to hand out, truncating to %u bits\n",
414 dev_path(dev
), max_requested_bits
,
415 CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS
);
416 max_requested_bits
= CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS
;
419 if (!(res
->flags
& IORESOURCE_PCI64
) && max_requested_bits
> 32) {
420 printk(BIOS_ERR
, "Resizable BAR requested"
421 " above 32 bits, but PCI function reported a"
426 /* Configure the resource parameters for the adjustable BAR */
427 res
->size
= 1ULL << max_requested_bits
;
428 res
->align
= max_requested_bits
;
429 res
->gran
= max_requested_bits
;
430 res
->limit
= (res
->flags
& IORESOURCE_PCI64
) ? UINT64_MAX
: UINT32_MAX
;
431 res
->flags
|= (res
->flags
& IORESOURCE_PCI64
) ?
432 IORESOURCE_PCIE_RESIZABLE_BAR
| IORESOURCE_ABOVE_4G
:
433 IORESOURCE_PCIE_RESIZABLE_BAR
;
435 printk(BIOS_INFO
, "%s: Adjusting resource index %lu: base: %llx size: %llx "
436 "align: %d gran: %d limit: %llx\n",
437 dev_path(dev
), res
->index
, res
->base
, res
->size
,
438 res
->align
, res
->gran
, res
->limit
);
442 * Read the base address registers for a given device.
444 * @param dev Pointer to the dev structure.
445 * @param howmany How many registers to read (6 for device, 2 for bridge).
447 static void pci_read_bases(struct device
*dev
, unsigned int howmany
)
451 for (index
= PCI_BASE_ADDRESS_0
;
452 (index
< PCI_BASE_ADDRESS_0
+ (howmany
<< 2));) {
453 struct resource
*resource
;
454 resource
= pci_get_resource(dev
, index
);
456 const bool is_pcie
= pci_find_capability(dev
, PCI_CAP_ID_PCIE
) != 0;
457 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS
) && is_pcie
)
458 configure_adjustable_base(dev
, index
, resource
);
460 index
+= (resource
->flags
& IORESOURCE_PCI64
) ? 8 : 4;
463 compact_resources(dev
);
466 static void pci_record_bridge_resource(struct device
*dev
, resource_t moving
,
467 unsigned int index
, unsigned long type
)
469 struct resource
*resource
;
478 /* Initialize the constraints on the current bus. */
479 resource
= new_resource(dev
, index
);
483 while ((moving
& step
) == 0) {
487 resource
->gran
= gran
;
488 resource
->align
= gran
;
489 resource
->limit
= moving
| (step
- 1);
490 resource
->flags
= type
| IORESOURCE_PCI_BRIDGE
|
494 static void pci_bridge_read_bases(struct device
*dev
)
496 resource_t moving_base
, moving_limit
, moving
;
498 /* See if the bridge I/O resources are implemented. */
499 moving_base
= ((u32
)pci_moving_config8(dev
, PCI_IO_BASE
)) << 8;
501 ((u32
)pci_moving_config16(dev
, PCI_IO_BASE_UPPER16
)) << 16;
503 moving_limit
= ((u32
)pci_moving_config8(dev
, PCI_IO_LIMIT
)) << 8;
505 ((u32
)pci_moving_config16(dev
, PCI_IO_LIMIT_UPPER16
)) << 16;
507 moving
= moving_base
& moving_limit
;
509 /* Initialize the I/O space constraints on the current bus. */
510 pci_record_bridge_resource(dev
, moving
, PCI_IO_BASE
, IORESOURCE_IO
);
512 /* See if the bridge prefmem resources are implemented. */
514 ((resource_t
)pci_moving_config16(dev
, PCI_PREF_MEMORY_BASE
)) << 16;
516 ((resource_t
)pci_moving_config32(dev
, PCI_PREF_BASE_UPPER32
)) << 32;
519 ((resource_t
)pci_moving_config16(dev
, PCI_PREF_MEMORY_LIMIT
)) << 16;
521 ((resource_t
)pci_moving_config32(dev
, PCI_PREF_LIMIT_UPPER32
)) << 32;
523 moving
= moving_base
& moving_limit
;
524 /* Initialize the prefetchable memory constraints on the current bus. */
525 pci_record_bridge_resource(dev
, moving
, PCI_PREF_MEMORY_BASE
,
526 IORESOURCE_MEM
| IORESOURCE_PREFETCH
);
528 /* See if the bridge mem resources are implemented. */
529 moving_base
= ((u32
)pci_moving_config16(dev
, PCI_MEMORY_BASE
)) << 16;
530 moving_limit
= ((u32
)pci_moving_config16(dev
, PCI_MEMORY_LIMIT
)) << 16;
532 moving
= moving_base
& moving_limit
;
534 /* Initialize the memory resources on the current bus. */
535 pci_record_bridge_resource(dev
, moving
, PCI_MEMORY_BASE
,
538 compact_resources(dev
);
541 void pci_dev_read_resources(struct device
*dev
)
543 pci_read_bases(dev
, 6);
544 pci_get_rom_resource(dev
, PCI_ROM_ADDRESS
);
547 void pci_bus_read_resources(struct device
*dev
)
549 pci_bridge_read_bases(dev
);
550 pci_read_bases(dev
, 2);
551 pci_get_rom_resource(dev
, PCI_ROM_ADDRESS1
);
554 void pci_domain_read_resources(struct device
*dev
)
556 struct resource
*res
;
558 /* Initialize the system-wide I/O space constraints. */
559 res
= new_resource(dev
, IOINDEX_SUBTRACTIVE(0, 0));
560 res
->limit
= 0xffffUL
;
561 res
->flags
= IORESOURCE_IO
| IORESOURCE_SUBTRACTIVE
|
564 /* Initialize the system-wide memory resources constraints. */
565 res
= new_resource(dev
, IOINDEX_SUBTRACTIVE(1, 0));
566 res
->limit
= (1ULL << cpu_phys_address_size()) - 1;
567 res
->flags
= IORESOURCE_MEM
| IORESOURCE_SUBTRACTIVE
|
571 void pci_domain_set_resources(struct device
*dev
)
573 assign_resources(dev
->link_list
);
576 static void pci_store_resource(const struct device
*const dev
,
577 const struct resource
*const resource
)
579 unsigned long base_lo
, base_hi
;
581 base_lo
= resource
->base
& 0xffffffff;
582 base_hi
= (resource
->base
>> 32) & 0xffffffff;
585 * Some chipsets allow us to set/clear the I/O bit
586 * (e.g. VIA 82C686A). So set it to be safe.
588 if (resource
->flags
& IORESOURCE_IO
)
589 base_lo
|= PCI_BASE_ADDRESS_SPACE_IO
;
591 pci_write_config32(dev
, resource
->index
, base_lo
);
592 if (resource
->flags
& IORESOURCE_PCI64
)
593 pci_write_config32(dev
, resource
->index
+ 4, base_hi
);
596 static void pci_store_bridge_resource(const struct device
*const dev
,
597 struct resource
*const resource
)
599 resource_t base
, end
;
602 * PCI bridges have no enable bit. They are disabled if the base of
603 * the range is greater than the limit. If the size is zero, disable
604 * by setting the base = limit and end = limit - 2^gran.
606 if (resource
->size
== 0) {
607 base
= resource
->limit
;
608 end
= resource
->limit
- (1 << resource
->gran
);
609 resource
->base
= base
;
611 base
= resource
->base
;
612 end
= resource_end(resource
);
615 if (resource
->index
== PCI_IO_BASE
) {
616 /* Set the I/O ranges. */
617 pci_write_config8(dev
, PCI_IO_BASE
, base
>> 8);
618 pci_write_config16(dev
, PCI_IO_BASE_UPPER16
, base
>> 16);
619 pci_write_config8(dev
, PCI_IO_LIMIT
, end
>> 8);
620 pci_write_config16(dev
, PCI_IO_LIMIT_UPPER16
, end
>> 16);
621 } else if (resource
->index
== PCI_MEMORY_BASE
) {
622 /* Set the memory range. */
623 pci_write_config16(dev
, PCI_MEMORY_BASE
, base
>> 16);
624 pci_write_config16(dev
, PCI_MEMORY_LIMIT
, end
>> 16);
625 } else if (resource
->index
== PCI_PREF_MEMORY_BASE
) {
626 /* Set the prefetchable memory range. */
627 pci_write_config16(dev
, PCI_PREF_MEMORY_BASE
, base
>> 16);
628 pci_write_config32(dev
, PCI_PREF_BASE_UPPER32
, base
>> 32);
629 pci_write_config16(dev
, PCI_PREF_MEMORY_LIMIT
, end
>> 16);
630 pci_write_config32(dev
, PCI_PREF_LIMIT_UPPER32
, end
>> 32);
632 /* Don't let me think I stored the resource. */
633 resource
->flags
&= ~IORESOURCE_STORED
;
634 printk(BIOS_ERR
, "invalid resource->index %lx\n", resource
->index
);
638 static void pci_set_resource(struct device
*dev
, struct resource
*resource
)
640 /* Make certain the resource has actually been assigned a value. */
641 if (!(resource
->flags
& IORESOURCE_ASSIGNED
)) {
642 if (resource
->flags
& IORESOURCE_BRIDGE
) {
643 /* If a bridge resource has no value assigned,
644 we can treat it like an empty resource. */
647 printk(BIOS_ERR
, "%s %02lx %s size: 0x%010llx not assigned\n",
648 dev_path(dev
), resource
->index
,
649 resource_type(resource
), resource
->size
);
654 /* If this resource is fixed don't worry about it. */
655 if (resource
->flags
& IORESOURCE_FIXED
)
658 /* If I have already stored this resource don't worry about it. */
659 if (resource
->flags
& IORESOURCE_STORED
)
662 /* If the resource is subtractive don't worry about it. */
663 if (resource
->flags
& IORESOURCE_SUBTRACTIVE
)
666 /* Only handle PCI memory and I/O resources for now. */
667 if (!(resource
->flags
& (IORESOURCE_MEM
| IORESOURCE_IO
)))
670 /* Enable the resources in the command register. */
671 if (resource
->size
) {
672 if (resource
->flags
& IORESOURCE_MEM
)
673 dev
->command
|= PCI_COMMAND_MEMORY
;
674 if (resource
->flags
& IORESOURCE_IO
)
675 dev
->command
|= PCI_COMMAND_IO
;
676 if (resource
->flags
& IORESOURCE_PCI_BRIDGE
&&
677 CONFIG(PCI_SET_BUS_MASTER_PCI_BRIDGES
))
678 dev
->command
|= PCI_COMMAND_MASTER
;
681 /* Now store the resource. */
682 resource
->flags
|= IORESOURCE_STORED
;
684 if (!(resource
->flags
& IORESOURCE_PCI_BRIDGE
)) {
685 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS
) &&
686 (resource
->flags
& IORESOURCE_PCIE_RESIZABLE_BAR
))
687 pci_store_rebar_size(dev
, resource
);
689 pci_store_resource(dev
, resource
);
692 pci_store_bridge_resource(dev
, resource
);
695 report_resource_stored(dev
, resource
, "");
698 void pci_dev_set_resources(struct device
*dev
)
700 struct resource
*res
;
704 for (res
= dev
->resource_list
; res
; res
= res
->next
)
705 pci_set_resource(dev
, res
);
707 for (bus
= dev
->link_list
; bus
; bus
= bus
->next
) {
709 assign_resources(bus
);
712 /* Set a default latency timer. */
713 pci_write_config8(dev
, PCI_LATENCY_TIMER
, 0x40);
715 /* Set a default secondary latency timer. */
716 if ((dev
->hdr_type
& 0x7f) == PCI_HEADER_TYPE_BRIDGE
)
717 pci_write_config8(dev
, PCI_SEC_LATENCY_TIMER
, 0x40);
719 /* Zero the IRQ settings. */
720 line
= pci_read_config8(dev
, PCI_INTERRUPT_PIN
);
722 pci_write_config8(dev
, PCI_INTERRUPT_LINE
, 0);
724 /* Set the cache line size, so far 64 bytes is good for everyone. */
725 pci_write_config8(dev
, PCI_CACHE_LINE_SIZE
, 64 >> 2);
728 void pci_dev_enable_resources(struct device
*dev
)
730 const struct pci_operations
*ops
= NULL
;
733 /* Set the subsystem vendor and device ID for mainboard devices. */
735 ops
= dev
->ops
->ops_pci
;
736 if (dev
->on_mainboard
&& ops
&& ops
->set_subsystem
) {
737 if (CONFIG_SUBSYSTEM_VENDOR_ID
)
738 dev
->subsystem_vendor
= CONFIG_SUBSYSTEM_VENDOR_ID
;
739 else if (!dev
->subsystem_vendor
)
740 dev
->subsystem_vendor
= pci_read_config16(dev
,
742 if (CONFIG_SUBSYSTEM_DEVICE_ID
)
743 dev
->subsystem_device
= CONFIG_SUBSYSTEM_DEVICE_ID
;
744 else if (!dev
->subsystem_device
)
745 dev
->subsystem_device
= pci_read_config16(dev
,
748 printk(BIOS_DEBUG
, "%s subsystem <- %04x/%04x\n",
749 dev_path(dev
), dev
->subsystem_vendor
,
750 dev
->subsystem_device
);
751 ops
->set_subsystem(dev
, dev
->subsystem_vendor
,
752 dev
->subsystem_device
);
754 command
= pci_read_config16(dev
, PCI_COMMAND
);
755 command
|= dev
->command
;
758 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
761 printk(BIOS_DEBUG
, "%s cmd <- %02x\n", dev_path(dev
), command
);
762 pci_write_config16(dev
, PCI_COMMAND
, command
);
765 void pci_bus_enable_resources(struct device
*dev
)
770 * Enable I/O in command register if there is VGA card
771 * connected with (even it does not claim I/O resource).
773 if (dev
->link_list
->bridge_ctrl
& PCI_BRIDGE_CTL_VGA
)
774 dev
->command
|= PCI_COMMAND_IO
;
775 ctrl
= pci_read_config16(dev
, PCI_BRIDGE_CONTROL
);
776 ctrl
|= dev
->link_list
->bridge_ctrl
;
777 ctrl
|= (PCI_BRIDGE_CTL_PARITY
| PCI_BRIDGE_CTL_SERR
); /* Error check. */
778 printk(BIOS_DEBUG
, "%s bridge ctrl <- %04x\n", dev_path(dev
), ctrl
);
779 pci_write_config16(dev
, PCI_BRIDGE_CONTROL
, ctrl
);
781 pci_dev_enable_resources(dev
);
784 void pci_bus_reset(struct bus
*bus
)
788 ctl
= pci_read_config16(bus
->dev
, PCI_BRIDGE_CONTROL
);
789 ctl
|= PCI_BRIDGE_CTL_BUS_RESET
;
790 pci_write_config16(bus
->dev
, PCI_BRIDGE_CONTROL
, ctl
);
793 ctl
&= ~PCI_BRIDGE_CTL_BUS_RESET
;
794 pci_write_config16(bus
->dev
, PCI_BRIDGE_CONTROL
, ctl
);
798 void pci_dev_set_subsystem(struct device
*dev
, unsigned int vendor
,
804 switch (dev
->hdr_type
& 0x7f) {
805 case PCI_HEADER_TYPE_NORMAL
:
806 offset
= PCI_SUBSYSTEM_VENDOR_ID
;
808 case PCI_HEADER_TYPE_BRIDGE
:
809 offset
= pci_find_capability(dev
, PCI_CAP_ID_SSVID
);
812 offset
+= 4; /* Vendor ID at offset 4 */
814 case PCI_HEADER_TYPE_CARDBUS
:
815 offset
= PCI_CB_SUBSYSTEM_VENDOR_ID
;
821 if (!vendor
|| !device
) {
822 pci_write_config32(dev
, offset
,
823 pci_read_config32(dev
, PCI_VENDOR_ID
));
825 pci_write_config32(dev
, offset
,
826 ((device
& 0xffff) << 16) | (vendor
& 0xffff));
830 static int should_run_oprom(struct device
*dev
, struct rom_header
*rom
)
832 static int should_run
= -1;
834 if (CONFIG(VENDORCODE_ELTAN_VBOOT
))
836 if (!verified_boot_should_run_oprom(rom
))
842 if (CONFIG(ALWAYS_RUN_OPROM
)) {
847 /* Don't run VGA option ROMs, unless we have to print
848 * something on the screen before the kernel is loaded.
850 should_run
= display_init_required();
853 printk(BIOS_DEBUG
, "Not running VGA Option ROM\n");
857 static int should_load_oprom(struct device
*dev
)
859 /* If S3_VGA_ROM_RUN is disabled, skip running VGA option
860 * ROMs when coming out of an S3 resume.
862 if (!CONFIG(S3_VGA_ROM_RUN
) && acpi_is_wakeup_s3() &&
863 ((dev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
))
865 if (CONFIG(ALWAYS_LOAD_OPROM
))
867 if (should_run_oprom(dev
, NULL
))
873 static void oprom_pre_graphics_stall(void)
875 if (CONFIG_PRE_GRAPHICS_DELAY_MS
)
876 mdelay(CONFIG_PRE_GRAPHICS_DELAY_MS
);
879 /** Default handler: only runs the relevant PCI BIOS. */
880 void pci_dev_init(struct device
*dev
)
882 struct rom_header
*rom
, *ram
;
884 if (!CONFIG(VGA_ROM_RUN
))
887 /* Only execute VGA ROMs. */
888 if (((dev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
))
891 if (!should_load_oprom(dev
))
893 timestamp_add_now(TS_OPROM_INITIALIZE
);
895 rom
= pci_rom_probe(dev
);
899 ram
= pci_rom_load(dev
, rom
);
902 timestamp_add_now(TS_OPROM_COPY_END
);
904 if (!should_run_oprom(dev
, rom
))
907 /* Wait for any configured pre-graphics delay */
908 oprom_pre_graphics_stall();
910 run_bios(dev
, (unsigned long)ram
);
912 gfx_set_init_done(1);
913 printk(BIOS_DEBUG
, "VGA Option ROM was run\n");
914 timestamp_add_now(TS_OPROM_END
);
917 /** Default device operation for PCI devices */
918 struct pci_operations pci_dev_ops_pci
= {
919 .set_subsystem
= pci_dev_set_subsystem
,
922 struct device_operations default_pci_ops_dev
= {
923 .read_resources
= pci_dev_read_resources
,
924 .set_resources
= pci_dev_set_resources
,
925 .enable_resources
= pci_dev_enable_resources
,
926 #if CONFIG(HAVE_ACPI_TABLES)
927 .write_acpi_tables
= pci_rom_write_acpi_tables
,
928 .acpi_fill_ssdt
= pci_rom_ssdt
,
930 .init
= pci_dev_init
,
931 .ops_pci
= &pci_dev_ops_pci
,
934 /** Default device operations for PCI bridges */
935 struct device_operations default_pci_ops_bus
= {
936 .read_resources
= pci_bus_read_resources
,
937 .set_resources
= pci_dev_set_resources
,
938 .enable_resources
= pci_bus_enable_resources
,
939 .scan_bus
= pci_scan_bridge
,
940 .reset_bus
= pci_bus_reset
,
943 /** Default device operations for PCI devices marked 'hidden' */
944 static struct device_operations default_hidden_pci_ops_dev
= {
945 .read_resources
= noop_read_resources
,
946 .set_resources
= noop_set_resources
,
947 .scan_bus
= scan_static_bus
,
951 * Check for compatibility to route legacy VGA cycles through a bridge.
953 * Originally, when decoding i/o ports for legacy VGA cycles, bridges
954 * should only consider the 10 least significant bits of the port address.
955 * This means all VGA registers were aliased every 1024 ports!
956 * e.g. 0x3b0 was also decoded as 0x7b0, 0xbb0 etc.
958 * To avoid this mess, a bridge control bit (VGA16) was introduced in
959 * 2003 to enable decoding of 16-bit port addresses. As we don't want
960 * to make this any more complex for now, we use this bit if possible
961 * and only warn if it's not supported (in set_vga_bridge_bits()).
963 static void pci_bridge_vga_compat(struct bus
*const bus
)
965 uint16_t bridge_ctrl
;
967 bridge_ctrl
= pci_read_config16(bus
->dev
, PCI_BRIDGE_CONTROL
);
969 /* Ensure VGA decoding is disabled during probing (it should
970 be by default, but we run blobs nowadays) */
971 bridge_ctrl
&= ~PCI_BRIDGE_CTL_VGA
;
972 pci_write_config16(bus
->dev
, PCI_BRIDGE_CONTROL
, bridge_ctrl
);
974 /* If the upstream bridge doesn't support VGA16, we don't have to check */
975 bus
->no_vga16
|= bus
->dev
->bus
->no_vga16
;
979 /* Test if we can enable 16-bit decoding */
980 bridge_ctrl
|= PCI_BRIDGE_CTL_VGA16
;
981 pci_write_config16(bus
->dev
, PCI_BRIDGE_CONTROL
, bridge_ctrl
);
982 bridge_ctrl
= pci_read_config16(bus
->dev
, PCI_BRIDGE_CONTROL
);
984 bus
->no_vga16
= !(bridge_ctrl
& PCI_BRIDGE_CTL_VGA16
);
988 * Detect the type of downstream bridge.
990 * This function is a heuristic to detect which type of bus is downstream
991 * of a PCI-to-PCI bridge. This functions by looking for various capability
992 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
993 * Hypertransport all seem to have appropriate capabilities.
995 * When only a PCI-Express capability is found the type is examined to see
996 * which type of bridge we have.
998 * @param dev Pointer to the device structure of the bridge.
999 * @return Appropriate bridge operations.
1001 static struct device_operations
*get_pci_bridge_ops(struct device
*dev
)
1003 #if CONFIG(PCIX_PLUGIN_SUPPORT)
1004 unsigned int pcixpos
;
1005 pcixpos
= pci_find_capability(dev
, PCI_CAP_ID_PCIX
);
1007 printk(BIOS_DEBUG
, "%s subordinate bus PCI-X\n", dev_path(dev
));
1008 return &default_pcix_ops_bus
;
1011 #if CONFIG(PCIEXP_PLUGIN_SUPPORT)
1012 unsigned int pciexpos
;
1013 pciexpos
= pci_find_capability(dev
, PCI_CAP_ID_PCIE
);
1016 flags
= pci_read_config16(dev
, pciexpos
+ PCI_EXP_FLAGS
);
1017 switch ((flags
& PCI_EXP_FLAGS_TYPE
) >> 4) {
1018 case PCI_EXP_TYPE_ROOT_PORT
:
1019 case PCI_EXP_TYPE_UPSTREAM
:
1020 case PCI_EXP_TYPE_DOWNSTREAM
:
1021 printk(BIOS_DEBUG
, "%s subordinate bus PCI Express\n",
1023 if (CONFIG(PCIEXP_HOTPLUG
)) {
1025 sltcap
= pci_read_config16(dev
, pciexpos
+ PCI_EXP_SLTCAP
);
1026 if (sltcap
& PCI_EXP_SLTCAP_HPC
) {
1027 printk(BIOS_DEBUG
, "%s hot-plug capable\n",
1029 return &default_pciexp_hotplug_ops_bus
;
1032 return &default_pciexp_ops_bus
;
1033 case PCI_EXP_TYPE_PCI_BRIDGE
:
1034 printk(BIOS_DEBUG
, "%s subordinate PCI\n",
1036 return &default_pci_ops_bus
;
1042 return &default_pci_ops_bus
;
1046 * Check if a device id matches a PCI driver entry.
1048 * The driver entry can either point at a zero terminated array of acceptable
1049 * device IDs, or include a single device ID.
1051 * @param driver pointer to the PCI driver entry being checked
1052 * @param device_id PCI device ID of the device being matched
1054 static int device_id_match(struct pci_driver
*driver
, unsigned short device_id
)
1056 if (driver
->devices
) {
1057 unsigned short check_id
;
1058 const unsigned short *device_list
= driver
->devices
;
1059 while ((check_id
= *device_list
++) != 0)
1060 if (check_id
== device_id
)
1064 return (driver
->device
== device_id
);
1068 * Set up PCI device operation.
1070 * Check if it already has a driver. If not, use find_device_operations(),
1071 * or set to a default based on type.
1073 * @param dev Pointer to the device whose pci_ops you want to set.
1076 static void set_pci_ops(struct device
*dev
)
1078 struct pci_driver
*driver
;
1084 * Look through the list of setup drivers and find one for
1087 for (driver
= &_pci_drivers
[0]; driver
!= &_epci_drivers
[0]; driver
++) {
1088 if ((driver
->vendor
== dev
->vendor
) &&
1089 device_id_match(driver
, dev
->device
)) {
1090 dev
->ops
= (struct device_operations
*)driver
->ops
;
1096 printk(BIOS_SPEW
, "%s [%04x/%04x] %sops\n", dev_path(dev
),
1097 driver
->vendor
, driver
->device
, (driver
->ops
->scan_bus
? "bus " : ""));
1101 /* If I don't have a specific driver use the default operations. */
1102 switch (dev
->hdr_type
& 0x7f) { /* Header type */
1103 case PCI_HEADER_TYPE_NORMAL
:
1104 if ((dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
)
1106 dev
->ops
= &default_pci_ops_dev
;
1108 case PCI_HEADER_TYPE_BRIDGE
:
1109 if ((dev
->class >> 8) != PCI_CLASS_BRIDGE_PCI
)
1111 dev
->ops
= get_pci_bridge_ops(dev
);
1113 #if CONFIG(CARDBUS_PLUGIN_SUPPORT)
1114 case PCI_HEADER_TYPE_CARDBUS
:
1115 dev
->ops
= &default_cardbus_ops_bus
;
1122 "%s [%04x/%04x/%06x] has unknown header type %02x, ignoring.\n",
1123 dev_path(dev
), dev
->vendor
, dev
->device
,
1124 dev
->class >> 8, dev
->hdr_type
);
1130 * See if we have already allocated a device structure for a given devfn.
1132 * Given a PCI bus structure and a devfn number, find the device structure
1133 * corresponding to the devfn, if present. Then move the device structure
1134 * as the last child on the bus.
1136 * @param bus Pointer to the bus structure.
1137 * @param devfn A device/function number.
1138 * @return Pointer to the device structure found or NULL if we have not
1139 * allocated a device for this devfn yet.
1141 static struct device
*pci_scan_get_dev(struct bus
*bus
, unsigned int devfn
)
1143 struct device
*dev
, **prev
;
1145 prev
= &bus
->children
;
1146 for (dev
= bus
->children
; dev
; dev
= dev
->sibling
) {
1147 if (dev
->path
.type
== DEVICE_PATH_PCI
&& dev
->path
.pci
.devfn
== devfn
) {
1148 /* Unlink from the list. */
1149 *prev
= dev
->sibling
;
1150 dev
->sibling
= NULL
;
1153 prev
= &dev
->sibling
;
1157 * Just like alloc_dev() add the device to the list of devices on the
1158 * bus. When the list of devices was formed we removed all of the
1159 * parents children, and now we are interleaving static and dynamic
1160 * devices in order on the bus.
1163 struct device
*child
;
1165 /* Find the last child on the bus. */
1166 for (child
= bus
->children
; child
&& child
->sibling
;)
1167 child
= child
->sibling
;
1169 /* Place the device as last on the bus. */
1171 child
->sibling
= dev
;
1173 bus
->children
= dev
;
1182 * Determine the existence of a given PCI device. Allocate a new struct device
1183 * if dev==NULL was passed in and the device exists in hardware.
1185 * @param dev Pointer to the dev structure.
1186 * @param bus Pointer to the bus structure.
1187 * @param devfn A device/function number to look at.
1188 * @return The device structure for the device (if found), NULL otherwise.
1190 struct device
*pci_probe_dev(struct device
*dev
, struct bus
*bus
,
1196 /* Detect if a device is present. */
1198 struct device dummy
;
1201 dummy
.path
.type
= DEVICE_PATH_PCI
;
1202 dummy
.path
.pci
.devfn
= devfn
;
1204 id
= pci_read_config32(&dummy
, PCI_VENDOR_ID
);
1206 * Have we found something? Some broken boards return 0 if a
1207 * slot is empty, but the expected answer is 0xffffffff.
1209 if (id
== 0xffffffff)
1212 if ((id
== 0x00000000) || (id
== 0x0000ffff) ||
1213 (id
== 0xffff0000)) {
1214 printk(BIOS_SPEW
, "%s, bad id 0x%x\n",
1215 dev_path(&dummy
), id
);
1218 dev
= alloc_dev(bus
, &dummy
.path
);
1221 * Enable/disable the device. Once we have found the device-
1222 * specific operations this operations we will disable the
1223 * device with those as well.
1225 * This is geared toward devices that have subfunctions
1226 * that do not show up by default.
1228 * If a device is a stuff option on the motherboard
1229 * it may be absent and enable_dev() must cope.
1231 /* Run the magic enable sequence for the device. */
1232 if (dev
->chip_ops
&& dev
->chip_ops
->enable_dev
)
1233 dev
->chip_ops
->enable_dev(dev
);
1235 /* Now read the vendor and device ID. */
1236 id
= pci_read_config32(dev
, PCI_VENDOR_ID
);
1239 * If the device does not have a PCI ID disable it. Possibly
1240 * this is because we have already disabled the device. But
1241 * this also handles optional devices that may not always
1244 /* If the chain is fully enumerated quit */
1245 if ((id
== 0xffffffff) || (id
== 0x00000000) ||
1246 (id
== 0x0000ffff) || (id
== 0xffff0000)) {
1249 "PCI: Static device %s not found, disabling it.\n",
1257 /* Read the rest of the PCI configuration information. */
1258 hdr_type
= pci_read_config8(dev
, PCI_HEADER_TYPE
);
1259 class = pci_read_config32(dev
, PCI_CLASS_REVISION
);
1261 /* Store the interesting information in the device structure. */
1262 dev
->vendor
= id
& 0xffff;
1263 dev
->device
= (id
>> 16) & 0xffff;
1264 dev
->hdr_type
= hdr_type
;
1266 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
1267 dev
->class = class >> 8;
1269 /* Architectural/System devices always need to be bus masters. */
1270 if ((dev
->class >> 16) == PCI_BASE_CLASS_SYSTEM
&&
1271 CONFIG(PCI_ALLOW_BUS_MASTER_ANY_DEVICE
))
1272 dev
->command
|= PCI_COMMAND_MASTER
;
1275 * Look at the vendor and device ID, or at least the header type and
1276 * class and figure out which set of configuration methods to use.
1277 * Unless we already have some PCI ops.
1281 /* Now run the magic enable/disable sequence for the device. */
1282 if (dev
->ops
&& dev
->ops
->enable
)
1283 dev
->ops
->enable(dev
);
1285 /* Display the device. */
1286 printk(BIOS_DEBUG
, "%s [%04x/%04x] %s%s\n", dev_path(dev
),
1287 dev
->vendor
, dev
->device
, dev
->enabled
? "enabled" : "disabled",
1288 dev
->ops
? "" : " No operations");
1294 * Test for match between romstage and ramstage device instance.
1296 * @param dev Pointer to the device structure.
1297 * @param sdev Simple device model identifier, created with PCI_DEV().
1298 * @return Non-zero if bus:dev.fn of device matches.
1300 unsigned int pci_match_simple_dev(struct device
*dev
, pci_devfn_t sdev
)
1302 return dev
->bus
->secondary
== PCI_DEV2SEGBUS(sdev
) &&
1303 dev
->path
.pci
.devfn
== PCI_DEV2DEVFN(sdev
);
1307 * Test whether a capability is available along the whole path from the given
1308 * device to the host bridge.
1310 * @param dev Pointer to the device structure.
1311 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
1312 * @return The next matching capability of the given device, if it is available
1313 * along the whole path, or zero if not.
1315 uint16_t pci_find_cap_recursive(const struct device
*dev
, uint16_t cap
)
1318 uint16_t pos
= pci_find_capability(dev
, cap
);
1319 const struct device
*bridge
= dev
->bus
->dev
;
1320 while (bridge
&& (bridge
->path
.type
== DEVICE_PATH_PCI
)) {
1321 assert(bridge
->bus
);
1322 if (!pci_find_capability(bridge
, cap
))
1324 bridge
= bridge
->bus
->dev
;
1330 * PCI devices that are marked as "hidden" do not get probed. However, the same
1331 * initialization logic is still performed as if it were. This is useful when
1332 * devices would like to be described in the devicetree.cb file, and/or present
1333 * static PCI resources to the allocator, but the platform firmware hides the
1334 * device (makes the device invisible to PCI enumeration) before PCI enumeration
1337 * The expected semantics of PCI devices marked as 'hidden':
1338 * 1) The device is actually present under the specified BDF
1339 * 2) The device config space can still be accessed somehow, but the Vendor ID
1340 * indicates there is no device there (it reads as 0xffffffff).
1341 * 3) The device may still consume PCI resources. Typically, these would have
1342 * been hardcoded elsewhere.
1344 * @param dev Pointer to the device structure.
1346 static void pci_scan_hidden_device(struct device
*dev
)
1348 if (dev
->chip_ops
&& dev
->chip_ops
->enable_dev
)
1349 dev
->chip_ops
->enable_dev(dev
);
1352 * If chip_ops->enable_dev did not set dev->ops, then set to a default
1353 * .ops, because PCI enumeration is effectively being skipped, therefore
1354 * no PCI driver will bind to this device. However, children may want to
1355 * be enumerated, so this provides scan_static_bus for the .scan_bus
1358 if (dev
->ops
== NULL
)
1359 dev
->ops
= &default_hidden_pci_ops_dev
;
1361 if (dev
->ops
->enable
)
1362 dev
->ops
->enable(dev
);
1364 /* Display the device almost as if it were probed normally */
1365 printk(BIOS_DEBUG
, "%s [0000/%04x] hidden%s\n", dev_path(dev
),
1366 dev
->device
, dev
->ops
? "" : " No operations");
1370 * A PCIe Downstream Port normally leads to a Link with only Device 0 on it
1371 * (PCIe spec r5.0, sec 7.3.1). As an optimization, scan only for Device 0 in
1374 * @param bus Pointer to the bus structure.
1376 static bool pci_bus_only_one_child(struct bus
*bus
)
1378 struct device
*bridge
= bus
->dev
;
1379 u16 pcie_pos
, pcie_flags_reg
;
1385 if (bridge
->path
.type
!= DEVICE_PATH_PCI
)
1388 pcie_pos
= pci_find_capability(bridge
, PCI_CAP_ID_PCIE
);
1392 pcie_flags_reg
= pci_read_config16(bridge
, pcie_pos
+ PCI_EXP_FLAGS
);
1394 pcie_type
= (pcie_flags_reg
& PCI_EXP_FLAGS_TYPE
) >> 4;
1396 return pciexp_is_downstream_port(pcie_type
);
1402 * Determine the existence of devices and bridges on a PCI bus. If there are
1403 * bridges on the bus, recursively scan the buses behind the bridges.
1405 * @param bus Pointer to the bus structure.
1406 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1407 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
1409 void pci_scan_bus(struct bus
*bus
, unsigned int min_devfn
,
1410 unsigned int max_devfn
)
1413 struct device
*dev
, **prev
;
1416 printk(BIOS_DEBUG
, "PCI: %s for bus %02x\n", __func__
, bus
->secondary
);
1418 /* Maximum sane devfn is 0xFF. */
1419 if (max_devfn
> 0xff) {
1420 printk(BIOS_ERR
, "PCI: %s limits devfn %x - devfn %x\n",
1421 __func__
, min_devfn
, max_devfn
);
1422 printk(BIOS_ERR
, "PCI: %s upper limit too big. Using 0xff.\n", __func__
);
1426 post_code(POST_ENTER_PCI_SCAN_BUS
);
1428 if (pci_bus_only_one_child(bus
))
1429 max_devfn
= MIN(max_devfn
, 0x07);
1432 * Probe all devices/functions on this bus with some optimization for
1433 * non-existence and single function devices.
1435 for (devfn
= min_devfn
; devfn
<= max_devfn
; devfn
++) {
1436 if (CONFIG(MINIMAL_PCI_SCANNING
)) {
1437 dev
= pcidev_path_behind(bus
, devfn
);
1438 if (!dev
|| !dev
->mandatory
)
1442 /* First thing setup the device structure. */
1443 dev
= pci_scan_get_dev(bus
, devfn
);
1445 /* Devices marked 'hidden' do not get probed */
1446 if (dev
&& dev
->hidden
) {
1447 pci_scan_hidden_device(dev
);
1449 /* Skip pci_probe_dev, go to next devfn */
1453 /* See if a device is present and setup the device structure. */
1454 dev
= pci_probe_dev(dev
, bus
, devfn
);
1457 * If this is not a multi function device, or the device is
1458 * not present don't waste time probing another function.
1459 * Skip to next device.
1461 if ((PCI_FUNC(devfn
) == 0x00) && (!dev
1462 || (dev
->enabled
&& ((dev
->hdr_type
& 0x80) != 0x80)))) {
1468 * Warn if any leftover static devices are found.
1469 * There's probably a problem in devicetree.cb.
1472 prev
= &bus
->children
;
1473 for (dev
= bus
->children
; dev
; dev
= dev
->sibling
) {
1476 * If static device is not PCI then enable it here and don't
1477 * treat it as a leftover device.
1479 if (dev
->path
.type
!= DEVICE_PATH_PCI
) {
1480 enable_static_device(dev
);
1485 * The device is only considered leftover if it is not hidden
1486 * and it has a Vendor ID of 0 (the default for a device that
1487 * could not be probed).
1489 if (dev
->vendor
!= 0 || dev
->hidden
) {
1490 prev
= &dev
->sibling
;
1494 /* Unlink it from list. */
1495 *prev
= dev
->sibling
;
1498 printk(BIOS_WARNING
, "PCI: Leftover static devices:\n");
1499 printk(BIOS_WARNING
, "%s\n", dev_path(dev
));
1503 printk(BIOS_WARNING
, "PCI: Check your devicetree.cb.\n");
1506 * For all children that implement scan_bus() (i.e. bridges)
1507 * scan the bus behind that child.
1513 * We've scanned the bus and so we know all about what's on the other
1514 * side of any bridges that may be on this bus plus any devices.
1515 * Return how far we've got finding sub-buses.
1517 post_code(POST_EXIT_PCI_SCAN_BUS
);
1526 static void pci_bridge_route(struct bus
*link
, scan_state state
)
1528 struct device
*dev
= link
->dev
;
1529 struct bus
*parent
= dev
->bus
;
1530 uint8_t primary
, secondary
, subordinate
;
1532 if (state
== PCI_ROUTE_SCAN
) {
1533 link
->secondary
= parent
->subordinate
+ 1;
1534 link
->subordinate
= link
->secondary
+ dev
->hotplug_buses
;
1535 link
->max_subordinate
= parent
->max_subordinate
1536 ? parent
->max_subordinate
1537 : (CONFIG_ECAM_MMCONF_BUS_NUMBER
- 1);
1540 if (link
->secondary
> link
->max_subordinate
)
1541 die("%s: No more busses available!\n", __func__
);
1543 /* This ought to only happen with hotplug buses. */
1544 if (link
->subordinate
> link
->max_subordinate
) {
1545 printk(BIOS_WARNING
, "%s: Limiting subordinate busses\n", __func__
);
1546 link
->subordinate
= link
->max_subordinate
;
1549 if (state
== PCI_ROUTE_CLOSE
) {
1553 } else if (state
== PCI_ROUTE_SCAN
) {
1554 primary
= parent
->secondary
;
1555 secondary
= link
->secondary
;
1556 subordinate
= link
->max_subordinate
;
1557 } else if (state
== PCI_ROUTE_FINAL
) {
1558 primary
= parent
->secondary
;
1559 secondary
= link
->secondary
;
1560 subordinate
= link
->subordinate
;
1565 if (state
== PCI_ROUTE_SCAN
) {
1566 /* Clear all status bits and turn off memory, I/O and master enables. */
1567 link
->bridge_cmd
= pci_read_config16(dev
, PCI_COMMAND
);
1568 pci_write_config16(dev
, PCI_COMMAND
, 0x0000);
1569 pci_write_config16(dev
, PCI_STATUS
, 0xffff);
1573 * Configure the bus numbers for this bridge: the configuration
1574 * transactions will not be propagated by the bridge if it is not
1575 * correctly configured.
1577 pci_write_config8(dev
, PCI_PRIMARY_BUS
, primary
);
1578 pci_write_config8(dev
, PCI_SECONDARY_BUS
, secondary
);
1579 pci_write_config8(dev
, PCI_SUBORDINATE_BUS
, subordinate
);
1581 if (state
== PCI_ROUTE_FINAL
) {
1582 pci_write_config16(dev
, PCI_COMMAND
, link
->bridge_cmd
);
1583 parent
->subordinate
= link
->subordinate
;
1588 * Scan a PCI bridge and the buses behind the bridge.
1590 * Determine the existence of buses behind the bridge. Set up the bridge
1591 * according to the result of the scan.
1593 * This function is the default scan_bus() method for PCI bridge devices.
1595 * @param dev Pointer to the bridge device.
1596 * @param do_scan_bus TODO
1598 void do_pci_scan_bridge(struct device
*dev
,
1599 void (*do_scan_bus
) (struct bus
* bus
,
1600 unsigned int min_devfn
,
1601 unsigned int max_devfn
))
1605 printk(BIOS_SPEW
, "%s for %s\n", __func__
, dev_path(dev
));
1607 if (dev
->link_list
== NULL
) {
1609 link
= malloc(sizeof(*link
));
1611 die("Couldn't allocate a link!\n");
1612 memset(link
, 0, sizeof(*link
));
1614 dev
->link_list
= link
;
1617 bus
= dev
->link_list
;
1619 pci_bridge_vga_compat(bus
);
1621 pci_bridge_route(bus
, PCI_ROUTE_SCAN
);
1623 do_scan_bus(bus
, 0x00, 0xff);
1625 pci_bridge_route(bus
, PCI_ROUTE_FINAL
);
1629 * Scan a PCI bridge and the buses behind the bridge.
1631 * Determine the existence of buses behind the bridge. Set up the bridge
1632 * according to the result of the scan.
1634 * This function is the default scan_bus() method for PCI bridge devices.
1636 * @param dev Pointer to the bridge device.
1638 void pci_scan_bridge(struct device
*dev
)
1640 do_pci_scan_bridge(dev
, pci_scan_bus
);
1644 * Scan a PCI domain.
1646 * This function is the default scan_bus() method for PCI domains.
1648 * @param dev Pointer to the domain.
1650 void pci_domain_scan_bus(struct device
*dev
)
1652 struct bus
*link
= dev
->link_list
;
1653 pci_scan_bus(link
, PCI_DEVFN(0, 0), 0xff);
1656 void pci_dev_disable_bus_master(const struct device
*dev
)
1658 pci_update_config16(dev
, PCI_COMMAND
, ~PCI_COMMAND_MASTER
, 0x0);
1662 * Take an INT_PIN number (0, 1 - 4) and convert
1663 * it to a string ("NO PIN", "PIN A" - "PIN D")
1665 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1666 * @return A string corresponding to the pin number or "Invalid"
1668 const char *pin_to_str(int pin
)
1670 const char *str
[5] = {
1678 if (pin
>= 0 && pin
<= 4)
1681 return "Invalid PIN, not 0 - 4";
1685 * Get the PCI INT_PIN swizzle for a device defined as:
1686 * pin_parent = (pin_child + devn_child) % 4 + 1
1687 * where PIN A = 1 ... PIN_D = 4
1689 * Given a PCI device structure 'dev', find the interrupt pin
1690 * that will be triggered on its parent bridge device when
1691 * generating an interrupt. For example: Device 1:3.2 may
1692 * use INT_PIN A but will trigger PIN D on its parent bridge
1693 * device. In this case, this function will return 4 (PIN D).
1695 * @param dev A PCI device structure to swizzle interrupt pins for
1696 * @param *parent_bridge The PCI device structure for the bridge
1697 * device 'dev' is attached to
1698 * @return The interrupt pin number (1 - 4) that 'dev' will
1699 * trigger when generating an interrupt
1701 static int swizzle_irq_pins(struct device
*dev
, struct device
**parent_bridge
)
1703 struct device
*parent
; /* Our current device's parent device */
1704 struct device
*child
; /* The child device of the parent */
1705 uint8_t parent_bus
= 0; /* Parent Bus number */
1706 uint16_t parent_devfn
= 0; /* Parent Device and Function number */
1707 uint16_t child_devfn
= 0; /* Child Device and Function number */
1708 uint8_t swizzled_pin
= 0; /* Pin swizzled across a bridge */
1710 /* Start with PIN A = 0 ... D = 3 */
1711 swizzled_pin
= pci_read_config8(dev
, PCI_INTERRUPT_PIN
) - 1;
1713 /* While our current device has parent devices */
1715 for (parent
= child
->bus
->dev
; parent
; parent
= parent
->bus
->dev
) {
1716 parent_bus
= parent
->bus
->secondary
;
1717 parent_devfn
= parent
->path
.pci
.devfn
;
1718 child_devfn
= child
->path
.pci
.devfn
;
1720 /* Swizzle the INT_PIN for any bridges not on root bus */
1721 swizzled_pin
= (PCI_SLOT(child_devfn
) + swizzled_pin
) % 4;
1722 printk(BIOS_SPEW
, "\tWith INT_PIN swizzled to %s\n"
1723 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1724 pin_to_str(swizzled_pin
+ 1), parent_bus
,
1725 PCI_SLOT(parent_devfn
), PCI_FUNC(parent_devfn
));
1727 /* Continue until we find the root bus */
1728 if (parent_bus
> 0) {
1730 * We will go on to the next parent so this parent
1737 * Found the root bridge device,
1738 * fill in the structure and exit
1740 *parent_bridge
= parent
;
1745 /* End with PIN A = 1 ... D = 4 */
1746 return swizzled_pin
+ 1;
1750 * Given a device structure 'dev', find its interrupt pin
1751 * and its parent bridge 'parent_bdg' device structure.
1752 * If it is behind a bridge, it will return the interrupt
1753 * pin number (1 - 4) of the parent bridge that the device
1754 * interrupt pin has been swizzled to, otherwise it will
1755 * return the interrupt pin that is programmed into the
1756 * PCI config space of the target device. If 'dev' is
1757 * behind a bridge, it will fill in 'parent_bdg' with the
1758 * device structure of the bridge it is behind, otherwise
1759 * it will copy 'dev' into 'parent_bdg'.
1761 * @param dev A PCI device structure to get interrupt pins for.
1762 * @param *parent_bdg The PCI device structure for the bridge
1763 * device 'dev' is attached to.
1764 * @return The interrupt pin number (1 - 4) that 'dev' will
1765 * trigger when generating an interrupt.
1766 * Errors: -1 is returned if the device is not enabled
1767 * -2 is returned if a parent bridge could not be found.
1769 int get_pci_irq_pins(struct device
*dev
, struct device
**parent_bdg
)
1771 uint8_t bus
= 0; /* The bus this device is on */
1772 uint16_t devfn
= 0; /* This device's device and function numbers */
1773 uint8_t int_pin
= 0; /* Interrupt pin used by the device */
1774 uint8_t target_pin
= 0; /* Interrupt pin we want to assign an IRQ to */
1776 /* Make sure this device is enabled */
1777 if (!(dev
->enabled
&& (dev
->path
.type
== DEVICE_PATH_PCI
)))
1780 bus
= dev
->bus
->secondary
;
1781 devfn
= dev
->path
.pci
.devfn
;
1783 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1784 int_pin
= pci_read_config8(dev
, PCI_INTERRUPT_PIN
);
1785 if (int_pin
< 1 || int_pin
> 4)
1788 printk(BIOS_SPEW
, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1789 bus
, PCI_SLOT(devfn
), PCI_FUNC(devfn
), pin_to_str(int_pin
));
1791 /* If this device is on a bridge, swizzle its INT_PIN */
1793 /* Swizzle its INT_PINs */
1794 target_pin
= swizzle_irq_pins(dev
, parent_bdg
);
1796 /* Make sure the swizzle returned valid structures */
1797 if (parent_bdg
== NULL
) {
1798 printk(BIOS_WARNING
, "Could not find parent bridge for this device!\n");
1801 } else { /* Device is not behind a bridge */
1802 target_pin
= int_pin
; /* Return its own interrupt pin */
1803 *parent_bdg
= dev
; /* Return its own structure */
1806 /* Target pin is the interrupt pin we want to assign an IRQ to */
1810 #if CONFIG(PC80_SYSTEM)
1812 * Assign IRQ numbers.
1814 * This function assigns IRQs for all functions contained within the indicated
1815 * device address. If the device does not exist or does not require interrupts
1816 * then this function has no effect.
1818 * This function should be called for each PCI slot in your system.
1820 * @param dev Pointer to dev structure.
1821 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1822 * of this slot. The particular IRQ #s that are passed in depend on the
1823 * routing inside your southbridge and on your board.
1825 void pci_assign_irqs(struct device
*dev
, const unsigned char pIntAtoD
[4])
1829 /* Each device may contain up to eight functions. */
1830 slot
= dev
->path
.pci
.devfn
>> 3;
1832 for (; dev
; dev
= dev
->sibling
) {
1834 if (dev
->path
.pci
.devfn
>> 3 != slot
)
1837 line
= pci_read_config8(dev
, PCI_INTERRUPT_PIN
);
1839 /* PCI spec says all values except 1..4 are reserved. */
1840 if ((line
< 1) || (line
> 4))
1843 irq
= pIntAtoD
[line
- 1];
1845 printk(BIOS_DEBUG
, "Assigning IRQ %d to %s\n", irq
, dev_path(dev
));
1847 pci_write_config8(dev
, PCI_INTERRUPT_LINE
, irq
);
1849 /* Change to level triggered. */
1850 i8259_configure_irq_trigger(irq
, IRQ_LEVEL_TRIGGERED
);