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.
9 #include <device/pci_ops.h>
11 #include <console/console.h>
16 #include <device/cardbus.h>
17 #include <device/device.h>
18 #include <device/pci.h>
19 #include <device/pci_ids.h>
20 #include <device/pcix.h>
21 #include <device/pciexp.h>
22 #include <device/hypertransport.h>
23 #include <pc80/i8259.h>
24 #include <security/vboot/vbnv.h>
25 #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), "
155 "read-only ignoring it\n",
156 dev_path(dev
), index
, value
);
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;
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 attr
&= PCI_BASE_ADDRESS_MEM_LIMIT_MASK
;
171 if (attr
== PCI_BASE_ADDRESS_MEM_LIMIT_32
) {
173 resource
->limit
= 0xffffffffUL
;
174 } else if (attr
== PCI_BASE_ADDRESS_MEM_LIMIT_1M
) {
176 resource
->limit
= 0x000fffffUL
;
177 } else if (attr
== PCI_BASE_ADDRESS_MEM_LIMIT_64
) {
179 resource
->limit
= 0xffffffffffffffffULL
;
180 resource
->flags
|= IORESOURCE_PCI64
;
183 printk(BIOS_ERR
, "Broken BAR with value %lx\n", attr
);
184 printk(BIOS_ERR
, " on dev %s at index %02lx\n",
185 dev_path(dev
), index
);
190 /* Don't let the limit exceed which bits can move. */
191 if (resource
->limit
> limit
)
192 resource
->limit
= limit
;
198 * Given a device and an index, read the size of the BAR for that register.
200 * @param dev Pointer to the device structure.
201 * @param index Address of the PCI configuration register.
203 static void pci_get_rom_resource(struct device
*dev
, unsigned long index
)
205 struct resource
*resource
;
209 /* Initialize the resources to nothing. */
210 resource
= new_resource(dev
, index
);
212 /* Get the initial value. */
213 value
= pci_read_config32(dev
, index
);
215 /* See which bits move. */
216 moving
= pci_moving_config32(dev
, index
);
218 /* Clear the Enable bit. */
219 moving
= moving
& ~PCI_ROM_ADDRESS_ENABLE
;
221 /* Find the resource constraints.
222 * Start by finding the bits that move. From there:
223 * - Size is the least significant bit of the bits that move.
224 * - Limit is all of the bits that move plus all of the lower bits.
225 * See PCI Spec 6.2.5.1.
229 resource
->align
= resource
->gran
= 0;
230 while (!(moving
& resource
->size
)) {
231 resource
->size
<<= 1;
232 resource
->align
+= 1;
235 resource
->limit
= moving
| (resource
->size
- 1);
236 resource
->flags
|= IORESOURCE_MEM
| IORESOURCE_READONLY
;
239 printk(BIOS_DEBUG
, "%s register %02lx(%08lx), "
240 "read-only ignoring it\n",
241 dev_path(dev
), index
, value
);
245 compact_resources(dev
);
249 * Given a device, read the size of the MSI-X table.
251 * @param dev Pointer to the device structure.
252 * @return MSI-X table size or 0 if not MSI-X capable device
254 size_t pci_msix_table_size(struct device
*dev
)
256 const size_t pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
260 const u16 control
= pci_read_config16(dev
, pos
+ PCI_MSIX_FLAGS
);
261 return (control
& PCI_MSIX_FLAGS_QSIZE
) + 1;
265 * Given a device, return the table offset and bar the MSI-X tables resides in.
267 * @param dev Pointer to the device structure.
268 * @param offset Returned value gives the offset in bytes inside the PCI BAR.
269 * @param idx The returned value is the index of the PCI_BASE_ADDRESS register
270 * the MSI-X table is located in.
271 * @return Zero on success
273 int pci_msix_table_bar(struct device
*dev
, u32
*offset
, u8
*idx
)
275 const size_t pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
276 if (!pos
|| !offset
|| !idx
)
279 *offset
= pci_read_config32(dev
, pos
+ PCI_MSIX_TABLE
);
280 *idx
= (u8
)(*offset
& PCI_MSIX_PBA_BIR
);
281 *offset
&= PCI_MSIX_PBA_OFFSET
;
287 * Given a device, return a msix_entry pointer or NULL if no table was found.
289 * @param dev Pointer to the device structure.
291 * @return NULL on error
293 struct msix_entry
*pci_msix_get_table(struct device
*dev
)
295 struct resource
*res
;
299 if (pci_msix_table_bar(dev
, &offset
, &idx
))
305 res
= probe_resource(dev
, idx
* 4 + PCI_BASE_ADDRESS_0
);
306 if (!res
|| !res
->base
|| offset
>= res
->size
)
309 if ((res
->flags
& IORESOURCE_PCI64
) &&
310 (uintptr_t)res
->base
!= res
->base
)
313 return (struct msix_entry
*)((uintptr_t)res
->base
+ offset
);
317 * Read the base address registers for a given device.
319 * @param dev Pointer to the dev structure.
320 * @param howmany How many registers to read (6 for device, 2 for bridge).
322 static void pci_read_bases(struct device
*dev
, unsigned int howmany
)
326 for (index
= PCI_BASE_ADDRESS_0
;
327 (index
< PCI_BASE_ADDRESS_0
+ (howmany
<< 2));) {
328 struct resource
*resource
;
329 resource
= pci_get_resource(dev
, index
);
330 index
+= (resource
->flags
& IORESOURCE_PCI64
) ? 8 : 4;
333 compact_resources(dev
);
336 static void pci_record_bridge_resource(struct device
*dev
, resource_t moving
,
337 unsigned int index
, unsigned long type
)
339 struct resource
*resource
;
348 /* Initialize the constraints on the current bus. */
349 resource
= new_resource(dev
, index
);
353 while ((moving
& step
) == 0) {
357 resource
->gran
= gran
;
358 resource
->align
= gran
;
359 resource
->limit
= moving
| (step
- 1);
360 resource
->flags
= type
| IORESOURCE_PCI_BRIDGE
|
364 static void pci_bridge_read_bases(struct device
*dev
)
366 resource_t moving_base
, moving_limit
, moving
;
368 /* See if the bridge I/O resources are implemented. */
369 moving_base
= ((u32
) pci_moving_config8(dev
, PCI_IO_BASE
)) << 8;
371 ((u32
) pci_moving_config16(dev
, PCI_IO_BASE_UPPER16
)) << 16;
373 moving_limit
= ((u32
) pci_moving_config8(dev
, PCI_IO_LIMIT
)) << 8;
375 ((u32
) pci_moving_config16(dev
, PCI_IO_LIMIT_UPPER16
)) << 16;
377 moving
= moving_base
& moving_limit
;
379 /* Initialize the I/O space constraints on the current bus. */
380 pci_record_bridge_resource(dev
, moving
, PCI_IO_BASE
, IORESOURCE_IO
);
382 /* See if the bridge prefmem resources are implemented. */
384 ((resource_t
) pci_moving_config16(dev
, PCI_PREF_MEMORY_BASE
)) << 16;
386 ((resource_t
) pci_moving_config32(dev
, PCI_PREF_BASE_UPPER32
)) << 32;
389 ((resource_t
) pci_moving_config16(dev
, PCI_PREF_MEMORY_LIMIT
)) << 16;
391 ((resource_t
) pci_moving_config32(dev
, PCI_PREF_LIMIT_UPPER32
)) << 32;
393 moving
= moving_base
& moving_limit
;
394 /* Initialize the prefetchable memory constraints on the current bus. */
395 pci_record_bridge_resource(dev
, moving
, PCI_PREF_MEMORY_BASE
,
396 IORESOURCE_MEM
| IORESOURCE_PREFETCH
);
398 /* See if the bridge mem resources are implemented. */
399 moving_base
= ((u32
) pci_moving_config16(dev
, PCI_MEMORY_BASE
)) << 16;
400 moving_limit
= ((u32
) pci_moving_config16(dev
, PCI_MEMORY_LIMIT
)) << 16;
402 moving
= moving_base
& moving_limit
;
404 /* Initialize the memory resources on the current bus. */
405 pci_record_bridge_resource(dev
, moving
, PCI_MEMORY_BASE
,
408 compact_resources(dev
);
411 void pci_dev_read_resources(struct device
*dev
)
413 pci_read_bases(dev
, 6);
414 pci_get_rom_resource(dev
, PCI_ROM_ADDRESS
);
417 void pci_bus_read_resources(struct device
*dev
)
419 pci_bridge_read_bases(dev
);
420 pci_read_bases(dev
, 2);
421 pci_get_rom_resource(dev
, PCI_ROM_ADDRESS1
);
424 void pci_domain_read_resources(struct device
*dev
)
426 struct resource
*res
;
428 /* Initialize the system-wide I/O space constraints. */
429 res
= new_resource(dev
, IOINDEX_SUBTRACTIVE(0, 0));
430 res
->limit
= 0xffffUL
;
431 res
->flags
= IORESOURCE_IO
| IORESOURCE_SUBTRACTIVE
|
434 /* Initialize the system-wide memory resources constraints. */
435 res
= new_resource(dev
, IOINDEX_SUBTRACTIVE(1, 0));
436 res
->limit
= (1ULL << cpu_phys_address_size()) - 1;
437 res
->flags
= IORESOURCE_MEM
| IORESOURCE_SUBTRACTIVE
|
441 void pci_domain_set_resources(struct device
*dev
)
443 assign_resources(dev
->link_list
);
446 static void pci_store_resource(const struct device
*const dev
,
447 const struct resource
*const resource
)
449 unsigned long base_lo
, base_hi
;
451 base_lo
= resource
->base
& 0xffffffff;
452 base_hi
= (resource
->base
>> 32) & 0xffffffff;
455 * Some chipsets allow us to set/clear the I/O bit
456 * (e.g. VIA 82C686A). So set it to be safe.
458 if (resource
->flags
& IORESOURCE_IO
)
459 base_lo
|= PCI_BASE_ADDRESS_SPACE_IO
;
461 pci_write_config32(dev
, resource
->index
, base_lo
);
462 if (resource
->flags
& IORESOURCE_PCI64
)
463 pci_write_config32(dev
, resource
->index
+ 4, base_hi
);
466 static void pci_store_bridge_resource(const struct device
*const dev
,
467 struct resource
*const resource
)
469 resource_t base
, end
;
472 * PCI bridges have no enable bit. They are disabled if the base of
473 * the range is greater than the limit. If the size is zero, disable
474 * by setting the base = limit and end = limit - 2^gran.
476 if (resource
->size
== 0) {
477 base
= resource
->limit
;
478 end
= resource
->limit
- (1 << resource
->gran
);
479 resource
->base
= base
;
481 base
= resource
->base
;
482 end
= resource_end(resource
);
485 if (resource
->index
== PCI_IO_BASE
) {
486 /* Set the I/O ranges. */
487 pci_write_config8(dev
, PCI_IO_BASE
, base
>> 8);
488 pci_write_config16(dev
, PCI_IO_BASE_UPPER16
, base
>> 16);
489 pci_write_config8(dev
, PCI_IO_LIMIT
, end
>> 8);
490 pci_write_config16(dev
, PCI_IO_LIMIT_UPPER16
, end
>> 16);
491 } else if (resource
->index
== PCI_MEMORY_BASE
) {
492 /* Set the memory range. */
493 pci_write_config16(dev
, PCI_MEMORY_BASE
, base
>> 16);
494 pci_write_config16(dev
, PCI_MEMORY_LIMIT
, end
>> 16);
495 } else if (resource
->index
== PCI_PREF_MEMORY_BASE
) {
496 /* Set the prefetchable memory range. */
497 pci_write_config16(dev
, PCI_PREF_MEMORY_BASE
, base
>> 16);
498 pci_write_config32(dev
, PCI_PREF_BASE_UPPER32
, base
>> 32);
499 pci_write_config16(dev
, PCI_PREF_MEMORY_LIMIT
, end
>> 16);
500 pci_write_config32(dev
, PCI_PREF_LIMIT_UPPER32
, end
>> 32);
502 /* Don't let me think I stored the resource. */
503 resource
->flags
&= ~IORESOURCE_STORED
;
504 printk(BIOS_ERR
, "ERROR: invalid resource->index %lx\n", resource
->index
);
508 static void pci_set_resource(struct device
*dev
, struct resource
*resource
)
510 /* Make certain the resource has actually been assigned a value. */
511 if (!(resource
->flags
& IORESOURCE_ASSIGNED
)) {
512 if (resource
->flags
& IORESOURCE_BRIDGE
) {
513 /* If a bridge resource has no value assigned,
514 we can treat it like an empty resource. */
517 printk(BIOS_ERR
, "ERROR: %s %02lx %s size: 0x%010llx not "
518 "assigned\n", dev_path(dev
), resource
->index
,
519 resource_type(resource
), resource
->size
);
524 /* If this resource is fixed don't worry about it. */
525 if (resource
->flags
& IORESOURCE_FIXED
)
528 /* If I have already stored this resource don't worry about it. */
529 if (resource
->flags
& IORESOURCE_STORED
)
532 /* If the resource is subtractive don't worry about it. */
533 if (resource
->flags
& IORESOURCE_SUBTRACTIVE
)
536 /* Only handle PCI memory and I/O resources for now. */
537 if (!(resource
->flags
& (IORESOURCE_MEM
| IORESOURCE_IO
)))
540 /* Enable the resources in the command register. */
541 if (resource
->size
) {
542 if (resource
->flags
& IORESOURCE_MEM
)
543 dev
->command
|= PCI_COMMAND_MEMORY
;
544 if (resource
->flags
& IORESOURCE_IO
)
545 dev
->command
|= PCI_COMMAND_IO
;
546 if (resource
->flags
& IORESOURCE_PCI_BRIDGE
)
547 dev
->command
|= PCI_COMMAND_MASTER
;
550 /* Now store the resource. */
551 resource
->flags
|= IORESOURCE_STORED
;
553 if (resource
->flags
& IORESOURCE_PCI_BRIDGE
)
554 pci_store_bridge_resource(dev
, resource
);
556 pci_store_resource(dev
, resource
);
558 report_resource_stored(dev
, resource
, "");
561 void pci_dev_set_resources(struct device
*dev
)
563 struct resource
*res
;
567 for (res
= dev
->resource_list
; res
; res
= res
->next
)
568 pci_set_resource(dev
, res
);
570 for (bus
= dev
->link_list
; bus
; bus
= bus
->next
) {
572 assign_resources(bus
);
575 /* Set a default latency timer. */
576 pci_write_config8(dev
, PCI_LATENCY_TIMER
, 0x40);
578 /* Set a default secondary latency timer. */
579 if ((dev
->hdr_type
& 0x7f) == PCI_HEADER_TYPE_BRIDGE
)
580 pci_write_config8(dev
, PCI_SEC_LATENCY_TIMER
, 0x40);
582 /* Zero the IRQ settings. */
583 line
= pci_read_config8(dev
, PCI_INTERRUPT_PIN
);
585 pci_write_config8(dev
, PCI_INTERRUPT_LINE
, 0);
587 /* Set the cache line size, so far 64 bytes is good for everyone. */
588 pci_write_config8(dev
, PCI_CACHE_LINE_SIZE
, 64 >> 2);
591 void pci_dev_enable_resources(struct device
*dev
)
593 const struct pci_operations
*ops
= NULL
;
596 /* Set the subsystem vendor and device ID for mainboard devices. */
598 ops
= dev
->ops
->ops_pci
;
599 if (dev
->on_mainboard
&& ops
&& ops
->set_subsystem
) {
600 if (CONFIG_SUBSYSTEM_VENDOR_ID
)
601 dev
->subsystem_vendor
= CONFIG_SUBSYSTEM_VENDOR_ID
;
602 else if (!dev
->subsystem_vendor
)
603 dev
->subsystem_vendor
= pci_read_config16(dev
,
605 if (CONFIG_SUBSYSTEM_DEVICE_ID
)
606 dev
->subsystem_device
= CONFIG_SUBSYSTEM_DEVICE_ID
;
607 else if (!dev
->subsystem_device
)
608 dev
->subsystem_device
= pci_read_config16(dev
,
611 printk(BIOS_DEBUG
, "%s subsystem <- %04x/%04x\n",
612 dev_path(dev
), dev
->subsystem_vendor
,
613 dev
->subsystem_device
);
614 ops
->set_subsystem(dev
, dev
->subsystem_vendor
,
615 dev
->subsystem_device
);
617 command
= pci_read_config16(dev
, PCI_COMMAND
);
618 command
|= dev
->command
;
621 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
624 printk(BIOS_DEBUG
, "%s cmd <- %02x\n", dev_path(dev
), command
);
625 pci_write_config16(dev
, PCI_COMMAND
, command
);
628 void pci_bus_enable_resources(struct device
*dev
)
633 * Enable I/O in command register if there is VGA card
634 * connected with (even it does not claim I/O resource).
636 if (dev
->link_list
->bridge_ctrl
& PCI_BRIDGE_CTL_VGA
)
637 dev
->command
|= PCI_COMMAND_IO
;
638 ctrl
= pci_read_config16(dev
, PCI_BRIDGE_CONTROL
);
639 ctrl
|= dev
->link_list
->bridge_ctrl
;
640 ctrl
|= (PCI_BRIDGE_CTL_PARITY
| PCI_BRIDGE_CTL_SERR
); /* Error check. */
641 printk(BIOS_DEBUG
, "%s bridge ctrl <- %04x\n", dev_path(dev
), ctrl
);
642 pci_write_config16(dev
, PCI_BRIDGE_CONTROL
, ctrl
);
644 pci_dev_enable_resources(dev
);
647 void pci_bus_reset(struct bus
*bus
)
651 ctl
= pci_read_config16(bus
->dev
, PCI_BRIDGE_CONTROL
);
652 ctl
|= PCI_BRIDGE_CTL_BUS_RESET
;
653 pci_write_config16(bus
->dev
, PCI_BRIDGE_CONTROL
, ctl
);
656 ctl
&= ~PCI_BRIDGE_CTL_BUS_RESET
;
657 pci_write_config16(bus
->dev
, PCI_BRIDGE_CONTROL
, ctl
);
661 void pci_dev_set_subsystem(struct device
*dev
, unsigned int vendor
,
667 switch (dev
->hdr_type
& 0x7f) {
668 case PCI_HEADER_TYPE_NORMAL
:
669 offset
= PCI_SUBSYSTEM_VENDOR_ID
;
671 case PCI_HEADER_TYPE_BRIDGE
:
672 offset
= pci_find_capability(dev
, PCI_CAP_ID_SSVID
);
675 offset
+= 4; /* Vendor ID at offset 4 */
677 case PCI_HEADER_TYPE_CARDBUS
:
678 offset
= PCI_CB_SUBSYSTEM_VENDOR_ID
;
684 if (!vendor
|| !device
) {
685 pci_write_config32(dev
, offset
,
686 pci_read_config32(dev
, PCI_VENDOR_ID
));
688 pci_write_config32(dev
, offset
,
689 ((device
& 0xffff) << 16) | (vendor
& 0xffff));
693 static int should_run_oprom(struct device
*dev
, struct rom_header
*rom
)
695 static int should_run
= -1;
697 if (CONFIG(VENDORCODE_ELTAN_VBOOT
))
699 if (!verified_boot_should_run_oprom(rom
))
705 if (CONFIG(ALWAYS_RUN_OPROM
)) {
710 /* Don't run VGA option ROMs, unless we have to print
711 * something on the screen before the kernel is loaded.
713 should_run
= display_init_required();
716 printk(BIOS_DEBUG
, "Not running VGA Option ROM\n");
720 static int should_load_oprom(struct device
*dev
)
722 /* If S3_VGA_ROM_RUN is disabled, skip running VGA option
723 * ROMs when coming out of an S3 resume.
725 if (!CONFIG(S3_VGA_ROM_RUN
) && acpi_is_wakeup_s3() &&
726 ((dev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
))
728 if (CONFIG(ALWAYS_LOAD_OPROM
))
730 if (should_run_oprom(dev
, NULL
))
736 /** Default handler: only runs the relevant PCI BIOS. */
737 void pci_dev_init(struct device
*dev
)
739 struct rom_header
*rom
, *ram
;
741 if (!CONFIG(VGA_ROM_RUN
))
744 /* Only execute VGA ROMs. */
745 if (((dev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
))
748 if (!should_load_oprom(dev
))
750 timestamp_add_now(TS_OPROM_INITIALIZE
);
752 rom
= pci_rom_probe(dev
);
756 ram
= pci_rom_load(dev
, rom
);
759 timestamp_add_now(TS_OPROM_COPY_END
);
761 if (!should_run_oprom(dev
, rom
))
764 run_bios(dev
, (unsigned long)ram
);
766 gfx_set_init_done(1);
767 printk(BIOS_DEBUG
, "VGA Option ROM was run\n");
768 timestamp_add_now(TS_OPROM_END
);
771 /** Default device operation for PCI devices */
772 struct pci_operations pci_dev_ops_pci
= {
773 .set_subsystem
= pci_dev_set_subsystem
,
776 struct device_operations default_pci_ops_dev
= {
777 .read_resources
= pci_dev_read_resources
,
778 .set_resources
= pci_dev_set_resources
,
779 .enable_resources
= pci_dev_enable_resources
,
780 #if CONFIG(HAVE_ACPI_TABLES)
781 .write_acpi_tables
= pci_rom_write_acpi_tables
,
782 .acpi_fill_ssdt
= pci_rom_ssdt
,
784 .init
= pci_dev_init
,
785 .ops_pci
= &pci_dev_ops_pci
,
788 /** Default device operations for PCI bridges */
789 struct device_operations default_pci_ops_bus
= {
790 .read_resources
= pci_bus_read_resources
,
791 .set_resources
= pci_dev_set_resources
,
792 .enable_resources
= pci_bus_enable_resources
,
793 .scan_bus
= pci_scan_bridge
,
794 .reset_bus
= pci_bus_reset
,
797 /** Default device operations for PCI devices marked 'hidden' */
798 static struct device_operations default_hidden_pci_ops_dev
= {
799 .read_resources
= noop_read_resources
,
800 .set_resources
= noop_set_resources
,
801 .scan_bus
= scan_static_bus
,
805 * Check for compatibility to route legacy VGA cycles through a bridge.
807 * Originally, when decoding i/o ports for legacy VGA cycles, bridges
808 * should only consider the 10 least significant bits of the port address.
809 * This means all VGA registers were aliased every 1024 ports!
810 * e.g. 0x3b0 was also decoded as 0x7b0, 0xbb0 etc.
812 * To avoid this mess, a bridge control bit (VGA16) was introduced in
813 * 2003 to enable decoding of 16-bit port addresses. As we don't want
814 * to make this any more complex for now, we use this bit if possible
815 * and only warn if it's not supported (in set_vga_bridge_bits()).
817 static void pci_bridge_vga_compat(struct bus
*const bus
)
819 uint16_t bridge_ctrl
;
821 bridge_ctrl
= pci_read_config16(bus
->dev
, PCI_BRIDGE_CONTROL
);
823 /* Ensure VGA decoding is disabled during probing (it should
824 be by default, but we run blobs nowadays) */
825 bridge_ctrl
&= ~PCI_BRIDGE_CTL_VGA
;
826 pci_write_config16(bus
->dev
, PCI_BRIDGE_CONTROL
, bridge_ctrl
);
828 /* If the upstream bridge doesn't support VGA16, we don't have to check */
829 bus
->no_vga16
|= bus
->dev
->bus
->no_vga16
;
833 /* Test if we can enable 16-bit decoding */
834 bridge_ctrl
|= PCI_BRIDGE_CTL_VGA16
;
835 pci_write_config16(bus
->dev
, PCI_BRIDGE_CONTROL
, bridge_ctrl
);
836 bridge_ctrl
= pci_read_config16(bus
->dev
, PCI_BRIDGE_CONTROL
);
838 bus
->no_vga16
= !(bridge_ctrl
& PCI_BRIDGE_CTL_VGA16
);
842 * Detect the type of downstream bridge.
844 * This function is a heuristic to detect which type of bus is downstream
845 * of a PCI-to-PCI bridge. This functions by looking for various capability
846 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
847 * Hypertransport all seem to have appropriate capabilities.
849 * When only a PCI-Express capability is found the type is examined to see
850 * which type of bridge we have.
852 * @param dev Pointer to the device structure of the bridge.
853 * @return Appropriate bridge operations.
855 static struct device_operations
*get_pci_bridge_ops(struct device
*dev
)
857 #if CONFIG(PCIX_PLUGIN_SUPPORT)
858 unsigned int pcixpos
;
859 pcixpos
= pci_find_capability(dev
, PCI_CAP_ID_PCIX
);
861 printk(BIOS_DEBUG
, "%s subordinate bus PCI-X\n", dev_path(dev
));
862 return &default_pcix_ops_bus
;
865 #if CONFIG(HYPERTRANSPORT_PLUGIN_SUPPORT)
866 unsigned int htpos
= 0;
867 while ((htpos
= pci_find_next_capability(dev
, PCI_CAP_ID_HT
, htpos
))) {
869 flags
= pci_read_config16(dev
, htpos
+ PCI_CAP_FLAGS
);
870 if ((flags
>> 13) == 1) {
871 /* Host or Secondary Interface */
872 printk(BIOS_DEBUG
, "%s subordinate bus HT\n",
874 return &default_ht_ops_bus
;
878 #if CONFIG(PCIEXP_PLUGIN_SUPPORT)
879 unsigned int pciexpos
;
880 pciexpos
= pci_find_capability(dev
, PCI_CAP_ID_PCIE
);
883 flags
= pci_read_config16(dev
, pciexpos
+ PCI_EXP_FLAGS
);
884 switch ((flags
& PCI_EXP_FLAGS_TYPE
) >> 4) {
885 case PCI_EXP_TYPE_ROOT_PORT
:
886 case PCI_EXP_TYPE_UPSTREAM
:
887 case PCI_EXP_TYPE_DOWNSTREAM
:
888 printk(BIOS_DEBUG
, "%s subordinate bus PCI Express\n",
890 #if CONFIG(PCIEXP_HOTPLUG)
892 sltcap
= pci_read_config16(dev
, pciexpos
+ PCI_EXP_SLTCAP
);
893 if (sltcap
& PCI_EXP_SLTCAP_HPC
) {
894 printk(BIOS_DEBUG
, "%s hot-plug capable\n", dev_path(dev
));
895 return &default_pciexp_hotplug_ops_bus
;
897 #endif /* CONFIG(PCIEXP_HOTPLUG) */
898 return &default_pciexp_ops_bus
;
899 case PCI_EXP_TYPE_PCI_BRIDGE
:
900 printk(BIOS_DEBUG
, "%s subordinate PCI\n",
902 return &default_pci_ops_bus
;
908 return &default_pci_ops_bus
;
912 * Check if a device id matches a PCI driver entry.
914 * The driver entry can either point at a zero terminated array of acceptable
915 * device IDs, or include a single device ID.
917 * @param driver pointer to the PCI driver entry being checked
918 * @param device_id PCI device ID of the device being matched
920 static int device_id_match(struct pci_driver
*driver
, unsigned short device_id
)
922 if (driver
->devices
) {
923 unsigned short check_id
;
924 const unsigned short *device_list
= driver
->devices
;
925 while ((check_id
= *device_list
++) != 0)
926 if (check_id
== device_id
)
930 return (driver
->device
== device_id
);
934 * Set up PCI device operation.
936 * Check if it already has a driver. If not, use find_device_operations(),
937 * or set to a default based on type.
939 * @param dev Pointer to the device whose pci_ops you want to set.
942 static void set_pci_ops(struct device
*dev
)
944 struct pci_driver
*driver
;
950 * Look through the list of setup drivers and find one for
953 for (driver
= &_pci_drivers
[0]; driver
!= &_epci_drivers
[0]; driver
++) {
954 if ((driver
->vendor
== dev
->vendor
) &&
955 device_id_match(driver
, dev
->device
)) {
956 dev
->ops
= (struct device_operations
*)driver
->ops
;
957 printk(BIOS_SPEW
, "%s [%04x/%04x] %sops\n",
958 dev_path(dev
), driver
->vendor
, driver
->device
,
959 (driver
->ops
->scan_bus
? "bus " : ""));
964 /* If I don't have a specific driver use the default operations. */
965 switch (dev
->hdr_type
& 0x7f) { /* Header type */
966 case PCI_HEADER_TYPE_NORMAL
:
967 if ((dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
)
969 dev
->ops
= &default_pci_ops_dev
;
971 case PCI_HEADER_TYPE_BRIDGE
:
972 if ((dev
->class >> 8) != PCI_CLASS_BRIDGE_PCI
)
974 dev
->ops
= get_pci_bridge_ops(dev
);
976 #if CONFIG(CARDBUS_PLUGIN_SUPPORT)
977 case PCI_HEADER_TYPE_CARDBUS
:
978 dev
->ops
= &default_cardbus_ops_bus
;
984 printk(BIOS_ERR
, "%s [%04x/%04x/%06x] has unknown "
985 "header type %02x, ignoring.\n", dev_path(dev
),
986 dev
->vendor
, dev
->device
,
987 dev
->class >> 8, dev
->hdr_type
);
993 * See if we have already allocated a device structure for a given devfn.
995 * Given a PCI bus structure and a devfn number, find the device structure
996 * corresponding to the devfn, if present. Then move the device structure
997 * as the last child on the bus.
999 * @param bus Pointer to the bus structure.
1000 * @param devfn A device/function number.
1001 * @return Pointer to the device structure found or NULL if we have not
1002 * allocated a device for this devfn yet.
1004 static struct device
*pci_scan_get_dev(struct bus
*bus
, unsigned int devfn
)
1006 struct device
*dev
, **prev
;
1008 prev
= &bus
->children
;
1009 for (dev
= bus
->children
; dev
; dev
= dev
->sibling
) {
1010 if (dev
->path
.type
== DEVICE_PATH_PCI
) {
1011 if (dev
->path
.pci
.devfn
== devfn
) {
1012 /* Unlink from the list. */
1013 *prev
= dev
->sibling
;
1014 dev
->sibling
= NULL
;
1018 printk(BIOS_ERR
, "child %s not a PCI device\n",
1021 prev
= &dev
->sibling
;
1025 * Just like alloc_dev() add the device to the list of devices on the
1026 * bus. When the list of devices was formed we removed all of the
1027 * parents children, and now we are interleaving static and dynamic
1028 * devices in order on the bus.
1031 struct device
*child
;
1033 /* Find the last child on the bus. */
1034 for (child
= bus
->children
; child
&& child
->sibling
;)
1035 child
= child
->sibling
;
1037 /* Place the device as last on the bus. */
1039 child
->sibling
= dev
;
1041 bus
->children
= dev
;
1050 * Determine the existence of a given PCI device. Allocate a new struct device
1051 * if dev==NULL was passed in and the device exists in hardware.
1053 * @param dev Pointer to the dev structure.
1054 * @param bus Pointer to the bus structure.
1055 * @param devfn A device/function number to look at.
1056 * @return The device structure for the device (if found), NULL otherwise.
1058 struct device
*pci_probe_dev(struct device
*dev
, struct bus
*bus
,
1064 /* Detect if a device is present. */
1066 struct device dummy
;
1069 dummy
.path
.type
= DEVICE_PATH_PCI
;
1070 dummy
.path
.pci
.devfn
= devfn
;
1072 id
= pci_read_config32(&dummy
, PCI_VENDOR_ID
);
1074 * Have we found something? Some broken boards return 0 if a
1075 * slot is empty, but the expected answer is 0xffffffff.
1077 if (id
== 0xffffffff)
1080 if ((id
== 0x00000000) || (id
== 0x0000ffff) ||
1081 (id
== 0xffff0000)) {
1082 printk(BIOS_SPEW
, "%s, bad id 0x%x\n",
1083 dev_path(&dummy
), id
);
1086 dev
= alloc_dev(bus
, &dummy
.path
);
1089 * Enable/disable the device. Once we have found the device-
1090 * specific operations this operations we will disable the
1091 * device with those as well.
1093 * This is geared toward devices that have subfunctions
1094 * that do not show up by default.
1096 * If a device is a stuff option on the motherboard
1097 * it may be absent and enable_dev() must cope.
1099 /* Run the magic enable sequence for the device. */
1100 if (dev
->chip_ops
&& dev
->chip_ops
->enable_dev
)
1101 dev
->chip_ops
->enable_dev(dev
);
1103 /* Now read the vendor and device ID. */
1104 id
= pci_read_config32(dev
, PCI_VENDOR_ID
);
1107 * If the device does not have a PCI ID disable it. Possibly
1108 * this is because we have already disabled the device. But
1109 * this also handles optional devices that may not always
1112 /* If the chain is fully enumerated quit */
1113 if ((id
== 0xffffffff) || (id
== 0x00000000) ||
1114 (id
== 0x0000ffff) || (id
== 0xffff0000)) {
1116 printk(BIOS_INFO
, "PCI: Static device %s not "
1117 "found, disabling it.\n", dev_path(dev
));
1124 /* Read the rest of the PCI configuration information. */
1125 hdr_type
= pci_read_config8(dev
, PCI_HEADER_TYPE
);
1126 class = pci_read_config32(dev
, PCI_CLASS_REVISION
);
1128 /* Store the interesting information in the device structure. */
1129 dev
->vendor
= id
& 0xffff;
1130 dev
->device
= (id
>> 16) & 0xffff;
1131 dev
->hdr_type
= hdr_type
;
1133 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
1134 dev
->class = class >> 8;
1136 /* Architectural/System devices always need to be bus masters. */
1137 if ((dev
->class >> 16) == PCI_BASE_CLASS_SYSTEM
)
1138 dev
->command
|= PCI_COMMAND_MASTER
;
1141 * Look at the vendor and device ID, or at least the header type and
1142 * class and figure out which set of configuration methods to use.
1143 * Unless we already have some PCI ops.
1147 /* Now run the magic enable/disable sequence for the device. */
1148 if (dev
->ops
&& dev
->ops
->enable
)
1149 dev
->ops
->enable(dev
);
1151 /* Display the device. */
1152 printk(BIOS_DEBUG
, "%s [%04x/%04x] %s%s\n", dev_path(dev
),
1153 dev
->vendor
, dev
->device
, dev
->enabled
? "enabled" : "disabled",
1154 dev
->ops
? "" : " No operations");
1160 * Test for match between romstage and ramstage device instance.
1162 * @param dev Pointer to the device structure.
1163 * @param sdev Simple device model identifier, created with PCI_DEV().
1164 * @return Non-zero if bus:dev.fn of device matches.
1166 unsigned int pci_match_simple_dev(struct device
*dev
, pci_devfn_t sdev
)
1168 return dev
->bus
->secondary
== PCI_DEV2SEGBUS(sdev
) &&
1169 dev
->path
.pci
.devfn
== PCI_DEV2DEVFN(sdev
);
1173 * PCI devices that are marked as "hidden" do not get probed. However, the same
1174 * initialization logic is still performed as if it were. This is useful when
1175 * devices would like to be described in the devicetree.cb file, and/or present
1176 * static PCI resources to the allocator, but the platform firmware hides the
1177 * device (makes the device invisible to PCI enumeration) before PCI enumeration
1180 * The expected semantics of PCI devices marked as 'hidden':
1181 * 1) The device is actually present under the specified BDF
1182 * 2) The device config space can still be accessed somehow, but the Vendor ID
1183 * indicates there is no device there (it reads as 0xffffffff).
1184 * 3) The device may still consume PCI resources. Typically, these would have
1185 * been hardcoded elsewhere.
1187 * @param dev Pointer to the device structure.
1189 static void pci_scan_hidden_device(struct device
*dev
)
1191 if (dev
->chip_ops
&& dev
->chip_ops
->enable_dev
)
1192 dev
->chip_ops
->enable_dev(dev
);
1195 * If chip_ops->enable_dev did not set dev->ops, then set to a default
1196 * .ops, because PCI enumeration is effectively being skipped, therefore
1197 * no PCI driver will bind to this device. However, children may want to
1198 * be enumerated, so this provides scan_static_bus for the .scan_bus
1201 if (dev
->ops
== NULL
)
1202 dev
->ops
= &default_hidden_pci_ops_dev
;
1204 if (dev
->ops
->enable
)
1205 dev
->ops
->enable(dev
);
1207 /* Display the device almost as if it were probed normally */
1208 printk(BIOS_DEBUG
, "%s [0000/%04x] hidden%s\n", dev_path(dev
),
1209 dev
->device
, dev
->ops
? "" : " No operations");
1215 * Determine the existence of devices and bridges on a PCI bus. If there are
1216 * bridges on the bus, recursively scan the buses behind the bridges.
1218 * @param bus Pointer to the bus structure.
1219 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1220 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
1222 void pci_scan_bus(struct bus
*bus
, unsigned int min_devfn
,
1223 unsigned int max_devfn
)
1226 struct device
*dev
, **prev
;
1229 printk(BIOS_DEBUG
, "PCI: pci_scan_bus for bus %02x\n", bus
->secondary
);
1231 /* Maximum sane devfn is 0xFF. */
1232 if (max_devfn
> 0xff) {
1233 printk(BIOS_ERR
, "PCI: pci_scan_bus limits devfn %x - "
1234 "devfn %x\n", min_devfn
, max_devfn
);
1235 printk(BIOS_ERR
, "PCI: pci_scan_bus upper limit too big. "
1243 * Probe all devices/functions on this bus with some optimization for
1244 * non-existence and single function devices.
1246 for (devfn
= min_devfn
; devfn
<= max_devfn
; devfn
++) {
1247 if (CONFIG(MINIMAL_PCI_SCANNING
)) {
1248 dev
= pcidev_path_behind(bus
, devfn
);
1249 if (!dev
|| !dev
->mandatory
)
1253 /* First thing setup the device structure. */
1254 dev
= pci_scan_get_dev(bus
, devfn
);
1256 /* Devices marked 'hidden' do not get probed */
1257 if (dev
&& dev
->hidden
) {
1258 pci_scan_hidden_device(dev
);
1260 /* Skip pci_probe_dev, go to next devfn */
1264 /* See if a device is present and setup the device structure. */
1265 dev
= pci_probe_dev(dev
, bus
, devfn
);
1268 * If this is not a multi function device, or the device is
1269 * not present don't waste time probing another function.
1270 * Skip to next device.
1272 if ((PCI_FUNC(devfn
) == 0x00) && (!dev
1273 || (dev
->enabled
&& ((dev
->hdr_type
& 0x80) != 0x80)))) {
1281 * Warn if any leftover static devices are are found.
1282 * There's probably a problem in devicetree.cb.
1285 prev
= &bus
->children
;
1286 for (dev
= bus
->children
; dev
; dev
= dev
->sibling
) {
1288 * The device is only considered leftover if it is not hidden
1289 * and it has a Vendor ID of 0 (the default for a device that
1290 * could not be probed).
1292 if (dev
->vendor
!= 0 || dev
->hidden
) {
1293 prev
= &dev
->sibling
;
1297 /* Unlink it from list. */
1298 *prev
= dev
->sibling
;
1301 printk(BIOS_WARNING
, "PCI: Leftover static devices:\n");
1302 printk(BIOS_WARNING
, "%s\n", dev_path(dev
));
1306 printk(BIOS_WARNING
, "PCI: Check your devicetree.cb.\n");
1309 * For all children that implement scan_bus() (i.e. bridges)
1310 * scan the bus behind that child.
1316 * We've scanned the bus and so we know all about what's on the other
1317 * side of any bridges that may be on this bus plus any devices.
1318 * Return how far we've got finding sub-buses.
1329 static void pci_bridge_route(struct bus
*link
, scan_state state
)
1331 struct device
*dev
= link
->dev
;
1332 struct bus
*parent
= dev
->bus
;
1335 if (state
== PCI_ROUTE_SCAN
) {
1336 link
->secondary
= parent
->subordinate
+ 1;
1337 link
->subordinate
= link
->secondary
+ dev
->hotplug_buses
;
1340 if (state
== PCI_ROUTE_CLOSE
) {
1341 buses
|= 0xfeff << 8;
1342 } else if (state
== PCI_ROUTE_SCAN
) {
1343 buses
|= parent
->secondary
& 0xff;
1344 buses
|= ((u32
) link
->secondary
& 0xff) << 8;
1345 buses
|= 0xff << 16; /* MAX PCI_BUS number here */
1346 } else if (state
== PCI_ROUTE_FINAL
) {
1347 buses
|= parent
->secondary
& 0xff;
1348 buses
|= ((u32
) link
->secondary
& 0xff) << 8;
1349 buses
|= ((u32
) link
->subordinate
& 0xff) << 16;
1352 if (state
== PCI_ROUTE_SCAN
) {
1353 /* Clear all status bits and turn off memory, I/O and master enables. */
1354 link
->bridge_cmd
= pci_read_config16(dev
, PCI_COMMAND
);
1355 pci_write_config16(dev
, PCI_COMMAND
, 0x0000);
1356 pci_write_config16(dev
, PCI_STATUS
, 0xffff);
1360 * Configure the bus numbers for this bridge: the configuration
1361 * transactions will not be propagated by the bridge if it is not
1362 * correctly configured.
1365 reg
= pci_read_config32(dev
, PCI_PRIMARY_BUS
);
1368 pci_write_config32(dev
, PCI_PRIMARY_BUS
, reg
);
1370 if (state
== PCI_ROUTE_FINAL
) {
1371 pci_write_config16(dev
, PCI_COMMAND
, link
->bridge_cmd
);
1372 parent
->subordinate
= link
->subordinate
;
1377 * Scan a PCI bridge and the buses behind the bridge.
1379 * Determine the existence of buses behind the bridge. Set up the bridge
1380 * according to the result of the scan.
1382 * This function is the default scan_bus() method for PCI bridge devices.
1384 * @param dev Pointer to the bridge device.
1385 * @param do_scan_bus TODO
1387 void do_pci_scan_bridge(struct device
*dev
,
1388 void (*do_scan_bus
) (struct bus
* bus
,
1389 unsigned int min_devfn
,
1390 unsigned int max_devfn
))
1394 printk(BIOS_SPEW
, "%s for %s\n", __func__
, dev_path(dev
));
1396 if (dev
->link_list
== NULL
) {
1398 link
= malloc(sizeof(*link
));
1400 die("Couldn't allocate a link!\n");
1401 memset(link
, 0, sizeof(*link
));
1403 dev
->link_list
= link
;
1406 bus
= dev
->link_list
;
1408 pci_bridge_vga_compat(bus
);
1410 pci_bridge_route(bus
, PCI_ROUTE_SCAN
);
1412 do_scan_bus(bus
, 0x00, 0xff);
1414 pci_bridge_route(bus
, PCI_ROUTE_FINAL
);
1418 * Scan a PCI bridge and the buses behind the bridge.
1420 * Determine the existence of buses behind the bridge. Set up the bridge
1421 * according to the result of the scan.
1423 * This function is the default scan_bus() method for PCI bridge devices.
1425 * @param dev Pointer to the bridge device.
1427 void pci_scan_bridge(struct device
*dev
)
1429 do_pci_scan_bridge(dev
, pci_scan_bus
);
1433 * Scan a PCI domain.
1435 * This function is the default scan_bus() method for PCI domains.
1437 * @param dev Pointer to the domain.
1439 void pci_domain_scan_bus(struct device
*dev
)
1441 struct bus
*link
= dev
->link_list
;
1442 pci_scan_bus(link
, PCI_DEVFN(0, 0), 0xff);
1446 * Take an INT_PIN number (0, 1 - 4) and convert
1447 * it to a string ("NO PIN", "PIN A" - "PIN D")
1449 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1450 * @return A string corresponding to the pin number or "Invalid"
1452 const char *pin_to_str(int pin
)
1454 const char *str
[5] = {
1462 if (pin
>= 0 && pin
<= 4)
1465 return "Invalid PIN, not 0 - 4";
1469 * Get the PCI INT_PIN swizzle for a device defined as:
1470 * pin_parent = (pin_child + devn_child) % 4 + 1
1471 * where PIN A = 1 ... PIN_D = 4
1473 * Given a PCI device structure 'dev', find the interrupt pin
1474 * that will be triggered on its parent bridge device when
1475 * generating an interrupt. For example: Device 1:3.2 may
1476 * use INT_PIN A but will trigger PIN D on its parent bridge
1477 * device. In this case, this function will return 4 (PIN D).
1479 * @param dev A PCI device structure to swizzle interrupt pins for
1480 * @param *parent_bridge The PCI device structure for the bridge
1481 * device 'dev' is attached to
1482 * @return The interrupt pin number (1 - 4) that 'dev' will
1483 * trigger when generating an interrupt
1485 static int swizzle_irq_pins(struct device
*dev
, struct device
**parent_bridge
)
1487 struct device
*parent
; /* Our current device's parent device */
1488 struct device
*child
; /* The child device of the parent */
1489 uint8_t parent_bus
= 0; /* Parent Bus number */
1490 uint16_t parent_devfn
= 0; /* Parent Device and Function number */
1491 uint16_t child_devfn
= 0; /* Child Device and Function number */
1492 uint8_t swizzled_pin
= 0; /* Pin swizzled across a bridge */
1494 /* Start with PIN A = 0 ... D = 3 */
1495 swizzled_pin
= pci_read_config8(dev
, PCI_INTERRUPT_PIN
) - 1;
1497 /* While our current device has parent devices */
1499 for (parent
= child
->bus
->dev
; parent
; parent
= parent
->bus
->dev
) {
1500 parent_bus
= parent
->bus
->secondary
;
1501 parent_devfn
= parent
->path
.pci
.devfn
;
1502 child_devfn
= child
->path
.pci
.devfn
;
1504 /* Swizzle the INT_PIN for any bridges not on root bus */
1505 swizzled_pin
= (PCI_SLOT(child_devfn
) + swizzled_pin
) % 4;
1506 printk(BIOS_SPEW
, "\tWith INT_PIN swizzled to %s\n"
1507 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1508 pin_to_str(swizzled_pin
+ 1), parent_bus
,
1509 PCI_SLOT(parent_devfn
), PCI_FUNC(parent_devfn
));
1511 /* Continue until we find the root bus */
1512 if (parent_bus
> 0) {
1514 * We will go on to the next parent so this parent
1521 * Found the root bridge device,
1522 * fill in the structure and exit
1524 *parent_bridge
= parent
;
1529 /* End with PIN A = 1 ... D = 4 */
1530 return swizzled_pin
+ 1;
1534 * Given a device structure 'dev', find its interrupt pin
1535 * and its parent bridge 'parent_bdg' device structure.
1536 * If it is behind a bridge, it will return the interrupt
1537 * pin number (1 - 4) of the parent bridge that the device
1538 * interrupt pin has been swizzled to, otherwise it will
1539 * return the interrupt pin that is programmed into the
1540 * PCI config space of the target device. If 'dev' is
1541 * behind a bridge, it will fill in 'parent_bdg' with the
1542 * device structure of the bridge it is behind, otherwise
1543 * it will copy 'dev' into 'parent_bdg'.
1545 * @param dev A PCI device structure to get interrupt pins for.
1546 * @param *parent_bdg The PCI device structure for the bridge
1547 * device 'dev' is attached to.
1548 * @return The interrupt pin number (1 - 4) that 'dev' will
1549 * trigger when generating an interrupt.
1550 * Errors: -1 is returned if the device is not enabled
1551 * -2 is returned if a parent bridge could not be found.
1553 int get_pci_irq_pins(struct device
*dev
, struct device
**parent_bdg
)
1555 uint8_t bus
= 0; /* The bus this device is on */
1556 uint16_t devfn
= 0; /* This device's device and function numbers */
1557 uint8_t int_pin
= 0; /* Interrupt pin used by the device */
1558 uint8_t target_pin
= 0; /* Interrupt pin we want to assign an IRQ to */
1560 /* Make sure this device is enabled */
1561 if (!(dev
->enabled
&& (dev
->path
.type
== DEVICE_PATH_PCI
)))
1564 bus
= dev
->bus
->secondary
;
1565 devfn
= dev
->path
.pci
.devfn
;
1567 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1568 int_pin
= pci_read_config8(dev
, PCI_INTERRUPT_PIN
);
1569 if (int_pin
< 1 || int_pin
> 4)
1572 printk(BIOS_SPEW
, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1573 bus
, PCI_SLOT(devfn
), PCI_FUNC(devfn
), pin_to_str(int_pin
));
1575 /* If this device is on a bridge, swizzle its INT_PIN */
1577 /* Swizzle its INT_PINs */
1578 target_pin
= swizzle_irq_pins(dev
, parent_bdg
);
1580 /* Make sure the swizzle returned valid structures */
1581 if (parent_bdg
== NULL
) {
1582 printk(BIOS_WARNING
,
1583 "Warning: Could not find parent bridge for this device!\n");
1586 } else { /* Device is not behind a bridge */
1587 target_pin
= int_pin
; /* Return its own interrupt pin */
1588 *parent_bdg
= dev
; /* Return its own structure */
1591 /* Target pin is the interrupt pin we want to assign an IRQ to */
1595 #if CONFIG(PC80_SYSTEM)
1597 * Assign IRQ numbers.
1599 * This function assigns IRQs for all functions contained within the indicated
1600 * device address. If the device does not exist or does not require interrupts
1601 * then this function has no effect.
1603 * This function should be called for each PCI slot in your system.
1605 * @param dev Pointer to dev structure.
1606 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1607 * of this slot. The particular IRQ #s that are passed in depend on the
1608 * routing inside your southbridge and on your board.
1610 void pci_assign_irqs(struct device
*dev
, const unsigned char pIntAtoD
[4])
1614 /* Each device may contain up to eight functions. */
1615 slot
= dev
->path
.pci
.devfn
>> 3;
1617 for (; dev
; dev
= dev
->sibling
) {
1619 if (dev
->path
.pci
.devfn
>> 3 != slot
)
1622 line
= pci_read_config8(dev
, PCI_INTERRUPT_PIN
);
1624 /* PCI spec says all values except 1..4 are reserved. */
1625 if ((line
< 1) || (line
> 4))
1628 irq
= pIntAtoD
[line
- 1];
1630 printk(BIOS_DEBUG
, "Assigning IRQ %d to %s\n", irq
, dev_path(dev
));
1632 pci_write_config8(dev
, PCI_INTERRUPT_LINE
, pIntAtoD
[line
- 1]);
1634 #ifdef PARANOID_IRQ_ASSIGNMENTS
1635 irq
= pci_read_config8(pdev
, PCI_INTERRUPT_LINE
);
1636 printk(BIOS_DEBUG
, " Readback = %d\n", irq
);
1639 #if CONFIG(PC80_SYSTEM)
1640 /* Change to level triggered. */
1641 i8259_configure_irq_trigger(pIntAtoD
[line
- 1],
1642 IRQ_LEVEL_TRIGGERED
);
1647 void pci_dev_disable_bus_master(const struct device
*dev
)
1649 pci_update_config16(dev
, PCI_COMMAND
, ~PCI_COMMAND_MASTER
, 0x0);