blob: 24b95238f1340abc1ae3c0bd2331743e4473b66f [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Uwe Hermannb80dbf02007-04-22 19:08:13 +00002
3/*
Martin Roth99f83bb2019-09-15 20:57:18 -07004 * Originally based on the Linux kernel (drivers/pci/pci.c).
Myles Watson29cc9ed2009-07-02 18:56:24 +00005 * PCI Bus Services, see include/linux/pci.h for further explanation.
Eric Biederman8ca8d762003-04-22 19:02:15 +00006 */
7
Furquan Shaikh76cedd22020-05-02 10:24:23 -07008#include <acpi/acpi.h>
Bill XIE513d3592022-08-02 22:55:51 +08009#include <assert.h>
Nico Huberae814972023-05-10 18:06:27 +020010#include <cbmem.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020011#include <device/pci_ops.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100012#include <bootmode.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000013#include <console/console.h>
Furquan Shaikh871baf22020-03-12 17:51:24 -070014#include <cpu/cpu.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000015#include <stdlib.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000016#include <string.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100017#include <delay.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100018#include <device/cardbus.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000019#include <device/device.h>
20#include <device/pci.h>
21#include <device/pci_ids.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000022#include <device/pcix.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000023#include <device/pciexp.h>
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -070024#include <lib.h>
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000025#include <pc80/i8259.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020026#include <security/vboot/vbnv.h>
Martin Roth5dd4a2a2018-03-06 16:10:45 -070027#include <timestamp.h>
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +020028#include <types.h>
29
Myles Watson29cc9ed2009-07-02 18:56:24 +000030u8 pci_moving_config8(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000031{
Myles Watson29cc9ed2009-07-02 18:56:24 +000032 u8 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000033
Eric Biederman03acab62004-10-14 21:25:53 +000034 value = pci_read_config8(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000035
Eric Biederman03acab62004-10-14 21:25:53 +000036 pci_write_config8(dev, reg, 0xff);
37 ones = pci_read_config8(dev, reg);
38
39 pci_write_config8(dev, reg, 0x00);
40 zeroes = pci_read_config8(dev, reg);
41
42 pci_write_config8(dev, reg, value);
43
44 return ones ^ zeroes;
45}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000046
Uwe Hermanne4870472010-11-04 23:23:47 +000047u16 pci_moving_config16(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000048{
Myles Watson29cc9ed2009-07-02 18:56:24 +000049 u16 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000050
Eric Biederman03acab62004-10-14 21:25:53 +000051 value = pci_read_config16(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000052
Eric Biederman03acab62004-10-14 21:25:53 +000053 pci_write_config16(dev, reg, 0xffff);
54 ones = pci_read_config16(dev, reg);
55
56 pci_write_config16(dev, reg, 0x0000);
57 zeroes = pci_read_config16(dev, reg);
58
59 pci_write_config16(dev, reg, value);
60
61 return ones ^ zeroes;
62}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000063
Uwe Hermanne4870472010-11-04 23:23:47 +000064u32 pci_moving_config32(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000065{
Myles Watson29cc9ed2009-07-02 18:56:24 +000066 u32 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000067
Eric Biederman03acab62004-10-14 21:25:53 +000068 value = pci_read_config32(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000069
Eric Biederman03acab62004-10-14 21:25:53 +000070 pci_write_config32(dev, reg, 0xffffffff);
71 ones = pci_read_config32(dev, reg);
72
73 pci_write_config32(dev, reg, 0x00000000);
74 zeroes = pci_read_config32(dev, reg);
75
76 pci_write_config32(dev, reg, value);
77
78 return ones ^ zeroes;
79}
80
Myles Watson29cc9ed2009-07-02 18:56:24 +000081/**
Myles Watson29cc9ed2009-07-02 18:56:24 +000082 * Given a device and register, read the size of the BAR for that register.
83 *
84 * @param dev Pointer to the device structure.
85 * @param index Address of the PCI configuration register.
Uwe Hermannc1ee4292010-10-17 19:01:48 +000086 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +000087 */
Eric Biederman03acab62004-10-14 21:25:53 +000088struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +000089{
Eric Biederman5cd81732004-03-11 15:01:31 +000090 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +000091 unsigned long value, attr;
Myles Watson29cc9ed2009-07-02 18:56:24 +000092 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +000093
Myles Watson29cc9ed2009-07-02 18:56:24 +000094 /* Initialize the resources to nothing. */
Eric Biederman03acab62004-10-14 21:25:53 +000095 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +000096
Myles Watson29cc9ed2009-07-02 18:56:24 +000097 /* Get the initial value. */
Eric Biederman03acab62004-10-14 21:25:53 +000098 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +000099
Myles Watson29cc9ed2009-07-02 18:56:24 +0000100 /* See which bits move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000101 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000102
Myles Watson29cc9ed2009-07-02 18:56:24 +0000103 /* Initialize attr to the bits that do not move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000104 attr = value & ~moving;
105
Myles Watson29cc9ed2009-07-02 18:56:24 +0000106 /* If it is a 64bit resource look at the high half as well. */
Eric Biederman03acab62004-10-14 21:25:53 +0000107 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000108 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
109 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
110 /* Find the high bits that move. */
111 moving |=
Elyes Haouasd369c662022-11-18 15:06:21 +0100112 ((resource_t)pci_moving_config32(dev, index + 4)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000113 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000114
Myles Watson032a9652009-05-11 22:24:53 +0000115 /* Find the resource constraints.
Eric Biederman03acab62004-10-14 21:25:53 +0000116 * Start by finding the bits that move. From there:
117 * - Size is the least significant bit of the bits that move.
118 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000119 * See PCI Spec 6.2.5.1.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000120 */
Eric Biederman03acab62004-10-14 21:25:53 +0000121 limit = 0;
122 if (moving) {
123 resource->size = 1;
124 resource->align = resource->gran = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000125 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000126 resource->size <<= 1;
127 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000128 resource->gran += 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000129 }
130 resource->limit = limit = moving | (resource->size - 1);
Nico Huber8193b062015-10-21 15:43:41 +0200131
132 if (pci_base_address_is_memory_space(attr)) {
133 /* Page-align to allow individual mapping of devices. */
134 if (resource->align < 12)
135 resource->align = 12;
136 }
Eric Biederman03acab62004-10-14 21:25:53 +0000137 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000138
Uwe Hermanne4870472010-11-04 23:23:47 +0000139 /*
140 * Some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000141 * really size correctly.
Uwe Hermanne4870472010-11-04 23:23:47 +0000142 *
143 * Example: the Acer M7229 has BARs 1-4 normally read-only,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000144 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
Uwe Hermanne4870472010-11-04 23:23:47 +0000145 * by writing 0xffffffff to it, it will read back as 0x1f1 -- which
146 * is a violation of the spec.
147 *
148 * We catch this case and ignore it by observing which bits move.
149 *
150 * This also catches the common case of unimplemented registers
Eric Biederman03acab62004-10-14 21:25:53 +0000151 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000152 */
Eric Biederman03acab62004-10-14 21:25:53 +0000153 if (moving == 0) {
154 if (value != 0) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200155 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000156 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000157 }
158 resource->flags = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000159 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
160 /* An I/O mapped base address. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000161 resource->flags |= IORESOURCE_IO;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000162 /* I don't want to deal with 32bit I/O resources. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000163 resource->limit = 0xffff;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000164 } else {
165 /* A Memory mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000166 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000167 resource->flags |= IORESOURCE_MEM;
Nico Huber577c6b92022-08-15 00:08:58 +0200168 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000169 resource->flags |= IORESOURCE_PREFETCH;
Nico Huber577c6b92022-08-15 00:08:58 +0200170 if (CONFIG(PCIEXP_HOTPLUG_PREFETCH_MEM_ABOVE_4G)
171 && dev_path_hotplug(dev))
172 resource->flags |= IORESOURCE_ABOVE_4G;
173 }
Eric Biederman03acab62004-10-14 21:25:53 +0000174 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
175 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000176 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000177 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000178 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
179 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000180 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000181 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
182 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000183 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000185 } else {
186 /* Invalid value. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000187 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
188 printk(BIOS_ERR, " on dev %s at index %02lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000189 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000190 resource->flags = 0;
191 }
192 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000193
Myles Watson29cc9ed2009-07-02 18:56:24 +0000194 /* Don't let the limit exceed which bits can move. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000195 if (resource->limit > limit)
Eric Biederman03acab62004-10-14 21:25:53 +0000196 resource->limit = limit;
Eric Biederman03acab62004-10-14 21:25:53 +0000197
Eric Biederman5cd81732004-03-11 15:01:31 +0000198 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000199}
200
Myles Watson29cc9ed2009-07-02 18:56:24 +0000201/**
202 * Given a device and an index, read the size of the BAR for that register.
203 *
204 * @param dev Pointer to the device structure.
205 * @param index Address of the PCI configuration register.
206 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000207static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000208{
209 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000210 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000211 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000212
Myles Watson29cc9ed2009-07-02 18:56:24 +0000213 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000214 resource = new_resource(dev, index);
215
Myles Watson29cc9ed2009-07-02 18:56:24 +0000216 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000217 value = pci_read_config32(dev, index);
218
Myles Watson29cc9ed2009-07-02 18:56:24 +0000219 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000220 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000221
222 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000223 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000224
Myles Watson032a9652009-05-11 22:24:53 +0000225 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000226 * Start by finding the bits that move. From there:
227 * - Size is the least significant bit of the bits that move.
228 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000229 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000230 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000231 if (moving) {
232 resource->size = 1;
233 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000234 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000235 resource->size <<= 1;
236 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000237 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000238 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000239 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000240 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
241 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000242 if (value != 0) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200243 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000244 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000245 }
246 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000247 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000248 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000249}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000250
Myles Watson29cc9ed2009-07-02 18:56:24 +0000251/**
Patrick Rudolph4e2f95b2018-05-16 14:56:22 +0200252 * Given a device, read the size of the MSI-X table.
253 *
254 * @param dev Pointer to the device structure.
255 * @return MSI-X table size or 0 if not MSI-X capable device
256 */
257size_t pci_msix_table_size(struct device *dev)
258{
259 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
260 if (!pos)
261 return 0;
262
263 const u16 control = pci_read_config16(dev, pos + PCI_MSIX_FLAGS);
264 return (control & PCI_MSIX_FLAGS_QSIZE) + 1;
265}
266
267/**
268 * Given a device, return the table offset and bar the MSI-X tables resides in.
269 *
270 * @param dev Pointer to the device structure.
271 * @param offset Returned value gives the offset in bytes inside the PCI BAR.
272 * @param idx The returned value is the index of the PCI_BASE_ADDRESS register
273 * the MSI-X table is located in.
274 * @return Zero on success
275 */
276int pci_msix_table_bar(struct device *dev, u32 *offset, u8 *idx)
277{
278 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
279 if (!pos || !offset || !idx)
280 return 1;
281
282 *offset = pci_read_config32(dev, pos + PCI_MSIX_TABLE);
283 *idx = (u8)(*offset & PCI_MSIX_PBA_BIR);
284 *offset &= PCI_MSIX_PBA_OFFSET;
285
286 return 0;
287}
288
289/**
290 * Given a device, return a msix_entry pointer or NULL if no table was found.
291 *
292 * @param dev Pointer to the device structure.
293 *
294 * @return NULL on error
295 */
296struct msix_entry *pci_msix_get_table(struct device *dev)
297{
298 struct resource *res;
299 u32 offset;
300 u8 idx;
301
302 if (pci_msix_table_bar(dev, &offset, &idx))
303 return NULL;
304
305 if (idx > 5)
306 return NULL;
307
308 res = probe_resource(dev, idx * 4 + PCI_BASE_ADDRESS_0);
309 if (!res || !res->base || offset >= res->size)
310 return NULL;
311
312 if ((res->flags & IORESOURCE_PCI64) &&
313 (uintptr_t)res->base != res->base)
314 return NULL;
315
316 return (struct msix_entry *)((uintptr_t)res->base + offset);
317}
318
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700319static unsigned int get_rebar_offset(const struct device *dev, unsigned long index)
320{
Nico Huber5ffc2c82022-08-05 12:58:18 +0200321 uint32_t offset = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_RESIZABLE_BAR, 0);
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700322 if (!offset)
323 return 0;
324
325 /* Convert PCI_BASE_ADDRESS_0, ..._1, ..._2 into 0, 1, 2... */
326 const unsigned int find_bar_idx = (index - PCI_BASE_ADDRESS_0) /
327 sizeof(uint32_t);
328
329 /* Although all of the Resizable BAR Control Registers contain an
330 "NBARs" field, it is only valid in the Control Register for BAR 0 */
331 const uint32_t rebar_ctrl0 = pci_read_config32(dev, offset + PCI_REBAR_CTRL_OFFSET);
332 const unsigned int nbars = (rebar_ctrl0 & PCI_REBAR_CTRL_NBARS_MASK) >>
333 PCI_REBAR_CTRL_NBARS_SHIFT;
334
335 for (unsigned int i = 0; i < nbars; i++, offset += sizeof(uint64_t)) {
336 const uint32_t rebar_ctrl = pci_read_config32(
337 dev, offset + PCI_REBAR_CTRL_OFFSET);
338 const uint32_t bar_idx = rebar_ctrl & PCI_REBAR_CTRL_IDX_MASK;
339 if (bar_idx == find_bar_idx)
340 return offset;
341 }
342
343 return 0;
344}
345
346/* Bit 20 = 1 MiB, bit 21 = 2 MiB, bit 22 = 4 MiB, ... bit 63 = 8 EiB */
347static uint64_t get_rebar_sizes_mask(const struct device *dev,
348 unsigned long index)
349{
350 uint64_t size_mask = 0ULL;
351 const uint32_t offset = get_rebar_offset(dev, index);
352 if (!offset)
353 return 0;
354
355 /* Get 1 MB - 128 TB support from CAP register */
356 const uint32_t cap = pci_read_config32(dev, offset + PCI_REBAR_CAP_OFFSET);
357 /* Shift the bits from 4-31 to 0-27 (i.e., down by 4 bits) */
358 size_mask |= ((cap & PCI_REBAR_CAP_SIZE_MASK) >> 4);
359
360 /* Get 256 TB - 8 EB support from CTRL register and store it in bits 28-43 */
361 const uint64_t ctrl = pci_read_config32(dev, offset + PCI_REBAR_CTRL_OFFSET);
362 /* Shift ctrl mask from bit 16 to bit 28, so that the two
363 masks (fom cap and ctrl) form a contiguous bitmask when
364 concatenated (i.e., up by 12 bits). */
365 size_mask |= ((ctrl & PCI_REBAR_CTRL_SIZE_MASK) << 12);
366
367 /* Now that the mask occupies bits 0-43, shift it up to 20-63, so they
368 represent the actual powers of 2. */
369 return size_mask << 20;
370}
371
372static void pci_store_rebar_size(const struct device *dev,
373 const struct resource *resource)
374{
375 const unsigned int num_bits = __fls64(resource->size);
376 const uint32_t offset = get_rebar_offset(dev, resource->index);
377 if (!offset)
378 return;
379
380 pci_update_config32(dev, offset + PCI_REBAR_CTRL_OFFSET,
381 ~PCI_REBAR_CTRL_SIZE_MASK,
382 num_bits << PCI_REBAR_CTRL_SIZE_SHIFT);
383}
384
385static void configure_adjustable_base(const struct device *dev,
386 unsigned long index,
387 struct resource *res)
388{
389 /*
390 * Excerpt from an implementation note from the PCIe spec:
391 *
392 * System software uses this capability in place of the above mentioned
393 * method of determining the resource size[0], and prior to assigning
394 * the base address to the BAR. Potential usable resource sizes are
395 * reported by the Function via its Resizable BAR Capability and Control
396 * registers. It is intended that the software allocate the largest of
397 * the reported sizes that it can, since allocating less address space
398 * than the largest reported size can result in lower
399 * performance. Software then writes the size to the Resizable BAR
400 * Control register for the appropriate BAR for the Function. Following
401 * this, the base address is written to the BAR.
402 *
403 * [0] Referring to using the moving bits in the BAR to determine the
404 * requested size of the MMIO region
405 */
406 const uint64_t size_mask = get_rebar_sizes_mask(dev, index);
407 if (!size_mask)
408 return;
409
410 int max_requested_bits = __fls64(size_mask);
411 if (max_requested_bits > CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS) {
Elyes Haouasaba1c942022-11-09 15:05:23 +0100412 printk(BIOS_WARNING, "Device %s requests a BAR with"
Paul Menzeld579d802022-09-06 08:25:28 +0200413 " %u bits of address space, which coreboot is not"
414 " configured to hand out, truncating to %u bits\n",
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700415 dev_path(dev), max_requested_bits,
416 CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS);
417 max_requested_bits = CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS;
418 }
419
420 if (!(res->flags & IORESOURCE_PCI64) && max_requested_bits > 32) {
Elyes Haouasaba1c942022-11-09 15:05:23 +0100421 printk(BIOS_ERR, "Resizable BAR requested"
Paul Menzeld579d802022-09-06 08:25:28 +0200422 " above 32 bits, but PCI function reported a"
423 " 32-bit BAR.");
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700424 return;
425 }
426
427 /* Configure the resource parameters for the adjustable BAR */
428 res->size = 1ULL << max_requested_bits;
429 res->align = max_requested_bits;
430 res->gran = max_requested_bits;
431 res->limit = (res->flags & IORESOURCE_PCI64) ? UINT64_MAX : UINT32_MAX;
Tim Wawrzynczak2b83fa72022-05-27 12:27:50 -0600432 res->flags |= (res->flags & IORESOURCE_PCI64) ?
433 IORESOURCE_PCIE_RESIZABLE_BAR | IORESOURCE_ABOVE_4G :
434 IORESOURCE_PCIE_RESIZABLE_BAR;
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700435
436 printk(BIOS_INFO, "%s: Adjusting resource index %lu: base: %llx size: %llx "
437 "align: %d gran: %d limit: %llx\n",
438 dev_path(dev), res->index, res->base, res->size,
439 res->align, res->gran, res->limit);
440}
441
Patrick Rudolph4e2f95b2018-05-16 14:56:22 +0200442/**
Myles Watson29cc9ed2009-07-02 18:56:24 +0000443 * Read the base address registers for a given device.
444 *
445 * @param dev Pointer to the dev structure.
446 * @param howmany How many registers to read (6 for device, 2 for bridge).
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000447 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000448static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000449{
450 unsigned long index;
451
Myles Watson29cc9ed2009-07-02 18:56:24 +0000452 for (index = PCI_BASE_ADDRESS_0;
453 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000454 struct resource *resource;
455 resource = pci_get_resource(dev, index);
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700456
457 const bool is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE) != 0;
458 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS) && is_pcie)
459 configure_adjustable_base(dev, index, resource);
460
Myles Watson29cc9ed2009-07-02 18:56:24 +0000461 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000462 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000463
464 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000465}
466
Myles Watson29cc9ed2009-07-02 18:56:24 +0000467static void pci_record_bridge_resource(struct device *dev, resource_t moving,
Martin Roth38ddbfb2019-10-23 21:41:00 -0600468 unsigned int index, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000469{
Eric Biederman03acab62004-10-14 21:25:53 +0000470 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000471 unsigned long gran;
472 resource_t step;
473
Myles Watson29cc9ed2009-07-02 18:56:24 +0000474 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000475
476 if (!moving)
477 return;
478
479 /* Initialize the constraints on the current bus. */
480 resource = new_resource(dev, index);
481 resource->size = 0;
482 gran = 0;
483 step = 1;
484 while ((moving & step) == 0) {
485 gran += 1;
486 step <<= 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000487 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000488 resource->gran = gran;
489 resource->align = gran;
490 resource->limit = moving | (step - 1);
491 resource->flags = type | IORESOURCE_PCI_BRIDGE |
492 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000493}
494
Eric Biederman8ca8d762003-04-22 19:02:15 +0000495static void pci_bridge_read_bases(struct device *dev)
496{
Eric Biederman03acab62004-10-14 21:25:53 +0000497 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000498
Myles Watson29cc9ed2009-07-02 18:56:24 +0000499 /* See if the bridge I/O resources are implemented. */
Elyes Haouasd369c662022-11-18 15:06:21 +0100500 moving_base = ((u32)pci_moving_config8(dev, PCI_IO_BASE)) << 8;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000501 moving_base |=
Elyes Haouasd369c662022-11-18 15:06:21 +0100502 ((u32)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000503
Elyes Haouasd369c662022-11-18 15:06:21 +0100504 moving_limit = ((u32)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000505 moving_limit |=
Elyes Haouasd369c662022-11-18 15:06:21 +0100506 ((u32)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000507
508 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000509
Myles Watson29cc9ed2009-07-02 18:56:24 +0000510 /* Initialize the I/O space constraints on the current bus. */
511 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000512
Myles Watson29cc9ed2009-07-02 18:56:24 +0000513 /* See if the bridge prefmem resources are implemented. */
514 moving_base =
Elyes Haouasd369c662022-11-18 15:06:21 +0100515 ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000516 moving_base |=
Elyes Haouasd369c662022-11-18 15:06:21 +0100517 ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000518
Myles Watson29cc9ed2009-07-02 18:56:24 +0000519 moving_limit =
Elyes Haouasd369c662022-11-18 15:06:21 +0100520 ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000521 moving_limit |=
Elyes Haouasd369c662022-11-18 15:06:21 +0100522 ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
Myles Watson032a9652009-05-11 22:24:53 +0000523
Eric Biederman03acab62004-10-14 21:25:53 +0000524 moving = moving_base & moving_limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000525 /* Initialize the prefetchable memory constraints on the current bus. */
526 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
527 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Myles Watson032a9652009-05-11 22:24:53 +0000528
Myles Watson29cc9ed2009-07-02 18:56:24 +0000529 /* See if the bridge mem resources are implemented. */
Elyes Haouasd369c662022-11-18 15:06:21 +0100530 moving_base = ((u32)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
531 moving_limit = ((u32)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000532
533 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000534
Myles Watson29cc9ed2009-07-02 18:56:24 +0000535 /* Initialize the memory resources on the current bus. */
536 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
537 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000538
Eric Biederman5cd81732004-03-11 15:01:31 +0000539 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000540}
541
Eric Biederman5899fd82003-04-24 06:25:08 +0000542void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000543{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000544 pci_read_bases(dev, 6);
545 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000546}
547
Eric Biederman5899fd82003-04-24 06:25:08 +0000548void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000549{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000550 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000551 pci_read_bases(dev, 2);
552 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000553}
554
Myles Watson29cc9ed2009-07-02 18:56:24 +0000555void pci_domain_read_resources(struct device *dev)
556{
557 struct resource *res;
558
559 /* Initialize the system-wide I/O space constraints. */
560 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
561 res->limit = 0xffffUL;
562 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
563 IORESOURCE_ASSIGNED;
564
Nico Huberae814972023-05-10 18:06:27 +0200565 /*
566 * Initialize 32-bit memory resource constraints.
567 *
568 * There are often undeclared chipset resources in lower memory
569 * and memory right below the 4G barrier. Hence, only allow
570 * one big range from cbmem_top to the configured limit.
571 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000572 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Nico Huberae814972023-05-10 18:06:27 +0200573 res->base = (uintptr_t)cbmem_top();
574 res->limit = CONFIG_DOMAIN_RESOURCE_32BIT_LIMIT - 1;
575 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
576 IORESOURCE_ASSIGNED;
577
578 /* Initialize 64-bit memory resource constraints above 4G. */
579 res = new_resource(dev, IOINDEX_SUBTRACTIVE(2, 0));
580 res->base = 4ULL * GiB;
Jeremy Compostellaba757a72023-12-20 09:07:04 -0800581 res->limit = (1ULL << soc_phys_address_size()) - 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000582 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
583 IORESOURCE_ASSIGNED;
584}
585
Raul E Rangel5cb34e22020-05-04 16:41:22 -0600586void pci_domain_set_resources(struct device *dev)
587{
588 assign_resources(dev->link_list);
589}
590
Nico Huber730b2612020-05-20 00:32:50 +0200591static void pci_store_resource(const struct device *const dev,
592 const struct resource *const resource)
593{
594 unsigned long base_lo, base_hi;
595
596 base_lo = resource->base & 0xffffffff;
597 base_hi = (resource->base >> 32) & 0xffffffff;
598
599 /*
600 * Some chipsets allow us to set/clear the I/O bit
601 * (e.g. VIA 82C686A). So set it to be safe.
602 */
603 if (resource->flags & IORESOURCE_IO)
604 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
605
606 pci_write_config32(dev, resource->index, base_lo);
607 if (resource->flags & IORESOURCE_PCI64)
608 pci_write_config32(dev, resource->index + 4, base_hi);
609}
610
611static void pci_store_bridge_resource(const struct device *const dev,
612 struct resource *const resource)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000613{
Eric Biederman03acab62004-10-14 21:25:53 +0000614 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000615
Nico Huber730b2612020-05-20 00:32:50 +0200616 /*
617 * PCI bridges have no enable bit. They are disabled if the base of
618 * the range is greater than the limit. If the size is zero, disable
619 * by setting the base = limit and end = limit - 2^gran.
620 */
621 if (resource->size == 0) {
622 base = resource->limit;
623 end = resource->limit - (1 << resource->gran);
624 resource->base = base;
625 } else {
626 base = resource->base;
627 end = resource_end(resource);
628 }
629
630 if (resource->index == PCI_IO_BASE) {
631 /* Set the I/O ranges. */
632 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
633 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
634 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
635 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
636 } else if (resource->index == PCI_MEMORY_BASE) {
637 /* Set the memory range. */
638 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
639 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
640 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
641 /* Set the prefetchable memory range. */
642 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
643 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
644 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
645 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
646 } else {
647 /* Don't let me think I stored the resource. */
648 resource->flags &= ~IORESOURCE_STORED;
Julius Wernere9665952022-01-21 17:06:20 -0800649 printk(BIOS_ERR, "invalid resource->index %lx\n", resource->index);
Nico Huber730b2612020-05-20 00:32:50 +0200650 }
651}
652
653static void pci_set_resource(struct device *dev, struct resource *resource)
654{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000655 /* Make certain the resource has actually been assigned a value. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000656 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Nico Huberf5312442020-05-20 01:02:18 +0200657 if (resource->flags & IORESOURCE_BRIDGE) {
658 /* If a bridge resource has no value assigned,
659 we can treat it like an empty resource. */
660 resource->size = 0;
661 } else {
Julius Wernere9665952022-01-21 17:06:20 -0800662 printk(BIOS_ERR, "%s %02lx %s size: 0x%010llx not assigned\n",
Angel Ponsd19cc112021-07-04 11:41:31 +0200663 dev_path(dev), resource->index,
Nico Huberf5312442020-05-20 01:02:18 +0200664 resource_type(resource), resource->size);
665 return;
666 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000667 }
668
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000669 /* If this resource is fixed don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000670 if (resource->flags & IORESOURCE_FIXED)
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000671 return;
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000672
Myles Watson29cc9ed2009-07-02 18:56:24 +0000673 /* If I have already stored this resource don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000674 if (resource->flags & IORESOURCE_STORED)
Eric Biederman5cd81732004-03-11 15:01:31 +0000675 return;
Eric Biederman5cd81732004-03-11 15:01:31 +0000676
Myles Watson29cc9ed2009-07-02 18:56:24 +0000677 /* If the resource is subtractive don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000678 if (resource->flags & IORESOURCE_SUBTRACTIVE)
Eric Biederman03acab62004-10-14 21:25:53 +0000679 return;
Eric Biederman03acab62004-10-14 21:25:53 +0000680
Myles Watson29cc9ed2009-07-02 18:56:24 +0000681 /* Only handle PCI memory and I/O resources for now. */
682 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000683 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000684
Myles Watson29cc9ed2009-07-02 18:56:24 +0000685 /* Enable the resources in the command register. */
Eric Biederman03acab62004-10-14 21:25:53 +0000686 if (resource->size) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000687 if (resource->flags & IORESOURCE_MEM)
Eric Biederman03acab62004-10-14 21:25:53 +0000688 dev->command |= PCI_COMMAND_MEMORY;
Uwe Hermanne4870472010-11-04 23:23:47 +0000689 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000690 dev->command |= PCI_COMMAND_IO;
Felix Singer205b53e2020-09-07 15:21:21 +0200691 if (resource->flags & IORESOURCE_PCI_BRIDGE &&
692 CONFIG(PCI_SET_BUS_MASTER_PCI_BRIDGES))
Eric Biederman03acab62004-10-14 21:25:53 +0000693 dev->command |= PCI_COMMAND_MASTER;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000694 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000695
Myles Watson29cc9ed2009-07-02 18:56:24 +0000696 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000697 resource->flags |= IORESOURCE_STORED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000698
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700699 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
700 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS) &&
701 (resource->flags & IORESOURCE_PCIE_RESIZABLE_BAR))
702 pci_store_rebar_size(dev, resource);
703
Nico Huber730b2612020-05-20 00:32:50 +0200704 pci_store_resource(dev, resource);
Uwe Hermanne4870472010-11-04 23:23:47 +0000705
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700706 } else {
707 pci_store_bridge_resource(dev, resource);
708 }
709
Eric Biederman03acab62004-10-14 21:25:53 +0000710 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000711}
712
Eric Biederman5899fd82003-04-24 06:25:08 +0000713void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000714{
Myles Watsonc25cc112010-05-21 14:33:48 +0000715 struct resource *res;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000716 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000717
Uwe Hermanne4870472010-11-04 23:23:47 +0000718 for (res = dev->resource_list; res; res = res->next)
Myles Watsonc25cc112010-05-21 14:33:48 +0000719 pci_set_resource(dev, res);
Uwe Hermanne4870472010-11-04 23:23:47 +0000720
Arthur Heymans80c79a52023-08-24 15:12:19 +0200721 if (dev->link_list && dev->link_list->children)
722 assign_resources(dev->link_list);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000723
Myles Watson29cc9ed2009-07-02 18:56:24 +0000724 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000725 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000726
Myles Watson29cc9ed2009-07-02 18:56:24 +0000727 /* Set a default secondary latency timer. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000728 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000729 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000730
Myles Watson29cc9ed2009-07-02 18:56:24 +0000731 /* Zero the IRQ settings. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000732 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Uwe Hermanne4870472010-11-04 23:23:47 +0000733 if (line)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000734 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Uwe Hermanne4870472010-11-04 23:23:47 +0000735
Myles Watson29cc9ed2009-07-02 18:56:24 +0000736 /* Set the cache line size, so far 64 bytes is good for everyone. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000737 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000738}
739
Eric Biedermane9a271e32003-09-02 03:36:25 +0000740void pci_dev_enable_resources(struct device *dev)
741{
Kyösti Mälkkicac02312019-06-30 08:40:04 +0300742 const struct pci_operations *ops = NULL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000743 u16 command;
Eric Biederman03acab62004-10-14 21:25:53 +0000744
Uwe Hermanne4870472010-11-04 23:23:47 +0000745 /* Set the subsystem vendor and device ID for mainboard devices. */
Kyösti Mälkkicac02312019-06-30 08:40:04 +0300746 if (dev->ops)
747 ops = dev->ops->ops_pci;
Eric Biedermandbec2d42004-10-21 10:44:08 +0000748 if (dev->on_mainboard && ops && ops->set_subsystem) {
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700749 if (CONFIG_SUBSYSTEM_VENDOR_ID)
750 dev->subsystem_vendor = CONFIG_SUBSYSTEM_VENDOR_ID;
Rizwan Qureshifd891292017-04-26 21:00:37 +0530751 else if (!dev->subsystem_vendor)
752 dev->subsystem_vendor = pci_read_config16(dev,
753 PCI_VENDOR_ID);
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700754 if (CONFIG_SUBSYSTEM_DEVICE_ID)
755 dev->subsystem_device = CONFIG_SUBSYSTEM_DEVICE_ID;
Rizwan Qureshifd891292017-04-26 21:00:37 +0530756 else if (!dev->subsystem_device)
757 dev->subsystem_device = pci_read_config16(dev,
758 PCI_DEVICE_ID);
759
Sven Schnelle91321022011-03-01 19:58:47 +0000760 printk(BIOS_DEBUG, "%s subsystem <- %04x/%04x\n",
761 dev_path(dev), dev->subsystem_vendor,
762 dev->subsystem_device);
763 ops->set_subsystem(dev, dev->subsystem_vendor,
764 dev->subsystem_device);
Eric Biederman03acab62004-10-14 21:25:53 +0000765 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000766 command = pci_read_config16(dev, PCI_COMMAND);
767 command |= dev->command;
Uwe Hermanne4870472010-11-04 23:23:47 +0000768
Myles Watson29cc9ed2009-07-02 18:56:24 +0000769 /* v3 has
770 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
771 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000772
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000773 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000774 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000775}
776
777void pci_bus_enable_resources(struct device *dev)
778{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000779 u16 ctrl;
780
Uwe Hermanne4870472010-11-04 23:23:47 +0000781 /*
782 * Enable I/O in command register if there is VGA card
Myles Watson29cc9ed2009-07-02 18:56:24 +0000783 * connected with (even it does not claim I/O resource).
784 */
Myles Watson894a3472010-06-09 22:41:35 +0000785 if (dev->link_list->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000786 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000787 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000788 ctrl |= dev->link_list->bridge_ctrl;
Kyösti Mälkki382e2162019-09-21 16:19:32 +0300789 ctrl |= (PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR); /* Error check. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000790 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000791 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
792
793 pci_dev_enable_resources(dev);
794}
795
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000796void pci_bus_reset(struct bus *bus)
797{
Uwe Hermanne4870472010-11-04 23:23:47 +0000798 u16 ctl;
799
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000800 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
801 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
802 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
803 mdelay(10);
Uwe Hermanne4870472010-11-04 23:23:47 +0000804
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000805 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
806 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
807 delay(1);
808}
809
Elyes HAOUAS88030b72018-09-20 17:26:10 +0200810void pci_dev_set_subsystem(struct device *dev, unsigned int vendor,
811 unsigned int device)
Eric Biederman03acab62004-10-14 21:25:53 +0000812{
Subrata Banik9514d472019-03-20 14:56:27 +0530813 uint8_t offset;
814
815 /* Header type */
816 switch (dev->hdr_type & 0x7f) {
817 case PCI_HEADER_TYPE_NORMAL:
818 offset = PCI_SUBSYSTEM_VENDOR_ID;
819 break;
820 case PCI_HEADER_TYPE_BRIDGE:
821 offset = pci_find_capability(dev, PCI_CAP_ID_SSVID);
822 if (!offset)
823 return;
824 offset += 4; /* Vendor ID at offset 4 */
825 break;
826 case PCI_HEADER_TYPE_CARDBUS:
827 offset = PCI_CB_SUBSYSTEM_VENDOR_ID;
828 break;
829 default:
830 return;
831 }
832
Subrata Banik4a0f0712019-03-20 14:29:47 +0530833 if (!vendor || !device) {
Subrata Banik9514d472019-03-20 14:56:27 +0530834 pci_write_config32(dev, offset,
Subrata Banik4a0f0712019-03-20 14:29:47 +0530835 pci_read_config32(dev, PCI_VENDOR_ID));
836 } else {
Subrata Banik9514d472019-03-20 14:56:27 +0530837 pci_write_config32(dev, offset,
Subrata Banik4a0f0712019-03-20 14:29:47 +0530838 ((device & 0xffff) << 16) | (vendor & 0xffff));
839 }
Eric Biederman03acab62004-10-14 21:25:53 +0000840}
841
Frans Hendriksb71181a2019-10-04 14:06:33 +0200842static int should_run_oprom(struct device *dev, struct rom_header *rom)
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300843{
844 static int should_run = -1;
845
Felix Held3b5b66d2024-01-11 22:26:18 +0100846 if (dev->bus->segment_group) {
847 printk(BIOS_ERR, "Only option ROMs of devices in first PCI segment group can "
848 "be run.\n");
849 return 0;
850 }
851
Frans Hendriksb71181a2019-10-04 14:06:33 +0200852 if (CONFIG(VENDORCODE_ELTAN_VBOOT))
853 if (rom != NULL)
854 if (!verified_boot_should_run_oprom(rom))
855 return 0;
856
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300857 if (should_run >= 0)
858 return should_run;
859
Julius Wernercd49cce2019-03-05 16:53:33 -0800860 if (CONFIG(ALWAYS_RUN_OPROM)) {
Aaron Durbin10510252018-01-30 10:04:02 -0700861 should_run = 1;
862 return should_run;
863 }
864
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200865 /* Don't run VGA option ROMs, unless we have to print
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300866 * something on the screen before the kernel is loaded.
867 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700868 should_run = display_init_required();
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300869
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200870 if (!should_run)
871 printk(BIOS_DEBUG, "Not running VGA Option ROM\n");
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300872 return should_run;
873}
874
875static int should_load_oprom(struct device *dev)
876{
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300877 /* If S3_VGA_ROM_RUN is disabled, skip running VGA option
878 * ROMs when coming out of an S3 resume.
879 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800880 if (!CONFIG(S3_VGA_ROM_RUN) && acpi_is_wakeup_s3() &&
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300881 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
882 return 0;
Julius Wernercd49cce2019-03-05 16:53:33 -0800883 if (CONFIG(ALWAYS_LOAD_OPROM))
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300884 return 1;
Frans Hendriksb71181a2019-10-04 14:06:33 +0200885 if (should_run_oprom(dev, NULL))
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300886 return 1;
887
888 return 0;
889}
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300890
Kyösti Mälkki0f300632020-12-19 23:43:56 +0200891static void oprom_pre_graphics_stall(void)
892{
Paul Menzelc4062c72021-02-11 10:43:14 +0100893 if (CONFIG_PRE_GRAPHICS_DELAY_MS)
894 mdelay(CONFIG_PRE_GRAPHICS_DELAY_MS);
Kyösti Mälkki0f300632020-12-19 23:43:56 +0200895}
896
Uwe Hermanne4870472010-11-04 23:23:47 +0000897/** Default handler: only runs the relevant PCI BIOS. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000898void pci_dev_init(struct device *dev)
899{
900 struct rom_header *rom, *ram;
901
Julius Wernercd49cce2019-03-05 16:53:33 -0800902 if (!CONFIG(VGA_ROM_RUN))
Aaron Durbinfbed9a52018-01-30 09:58:51 -0700903 return;
904
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100905 /* Only execute VGA ROMs. */
906 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
Myles Watson17aeeca2009-10-07 18:41:08 +0000907 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000908
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300909 if (!should_load_oprom(dev))
Stefan Reinauer74a0efe2012-03-30 17:10:49 -0700910 return;
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700911 timestamp_add_now(TS_OPROM_INITIALIZE);
Aaron Durbince872cb2013-03-28 15:59:19 -0500912
913 rom = pci_rom_probe(dev);
914 if (rom == NULL)
915 return;
916
917 ram = pci_rom_load(dev, rom);
918 if (ram == NULL)
919 return;
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700920 timestamp_add_now(TS_OPROM_COPY_END);
Aaron Durbince872cb2013-03-28 15:59:19 -0500921
Frans Hendriksb71181a2019-10-04 14:06:33 +0200922 if (!should_run_oprom(dev, rom))
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300923 return;
924
Kyösti Mälkki0f300632020-12-19 23:43:56 +0200925 /* Wait for any configured pre-graphics delay */
926 oprom_pre_graphics_stall();
927
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000928 run_bios(dev, (unsigned long)ram);
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +0200929
Kyösti Mälkkiab56b3b2013-11-28 16:44:51 +0200930 gfx_set_init_done(1);
931 printk(BIOS_DEBUG, "VGA Option ROM was run\n");
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700932 timestamp_add_now(TS_OPROM_END);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000933}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000934
Li-Ta Loe5266692004-03-23 21:28:05 +0000935/** Default device operation for PCI devices */
Subrata Banikffc790b2017-12-11 10:29:49 +0530936struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000937 .set_subsystem = pci_dev_set_subsystem,
938};
939
Eric Biederman8ca8d762003-04-22 19:02:15 +0000940struct device_operations default_pci_ops_dev = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000941 .read_resources = pci_dev_read_resources,
942 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000943 .enable_resources = pci_dev_enable_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -0800944#if CONFIG(HAVE_ACPI_TABLES)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200945 .write_acpi_tables = pci_rom_write_acpi_tables,
Nico Huber68680dd2020-03-31 17:34:52 +0200946 .acpi_fill_ssdt = pci_rom_ssdt,
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200947#endif
Uwe Hermanne4870472010-11-04 23:23:47 +0000948 .init = pci_dev_init,
Uwe Hermanne4870472010-11-04 23:23:47 +0000949 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000950};
Li-Ta Loe5266692004-03-23 21:28:05 +0000951
952/** Default device operations for PCI bridges */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000953struct device_operations default_pci_ops_bus = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000954 .read_resources = pci_bus_read_resources,
955 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000956 .enable_resources = pci_bus_enable_resources,
Uwe Hermanne4870472010-11-04 23:23:47 +0000957 .scan_bus = pci_scan_bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000958 .reset_bus = pci_bus_reset,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000959};
Li-Ta Loe5266692004-03-23 21:28:05 +0000960
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -0600961/** Default device operations for PCI devices marked 'hidden' */
962static struct device_operations default_hidden_pci_ops_dev = {
963 .read_resources = noop_read_resources,
964 .set_resources = noop_set_resources,
965 .scan_bus = scan_static_bus,
966};
967
Li-Ta Loe5266692004-03-23 21:28:05 +0000968/**
Nico Huber061b9052019-09-21 15:58:23 +0200969 * Check for compatibility to route legacy VGA cycles through a bridge.
970 *
971 * Originally, when decoding i/o ports for legacy VGA cycles, bridges
972 * should only consider the 10 least significant bits of the port address.
973 * This means all VGA registers were aliased every 1024 ports!
974 * e.g. 0x3b0 was also decoded as 0x7b0, 0xbb0 etc.
975 *
976 * To avoid this mess, a bridge control bit (VGA16) was introduced in
977 * 2003 to enable decoding of 16-bit port addresses. As we don't want
978 * to make this any more complex for now, we use this bit if possible
979 * and only warn if it's not supported (in set_vga_bridge_bits()).
980 */
981static void pci_bridge_vga_compat(struct bus *const bus)
982{
983 uint16_t bridge_ctrl;
984
985 bridge_ctrl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
986
987 /* Ensure VGA decoding is disabled during probing (it should
988 be by default, but we run blobs nowadays) */
989 bridge_ctrl &= ~PCI_BRIDGE_CTL_VGA;
990 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, bridge_ctrl);
991
992 /* If the upstream bridge doesn't support VGA16, we don't have to check */
993 bus->no_vga16 |= bus->dev->bus->no_vga16;
994 if (bus->no_vga16)
995 return;
996
997 /* Test if we can enable 16-bit decoding */
998 bridge_ctrl |= PCI_BRIDGE_CTL_VGA16;
999 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, bridge_ctrl);
1000 bridge_ctrl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
1001
1002 bus->no_vga16 = !(bridge_ctrl & PCI_BRIDGE_CTL_VGA16);
1003}
1004
1005/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001006 * Detect the type of downstream bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001007 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001008 * This function is a heuristic to detect which type of bus is downstream
1009 * of a PCI-to-PCI bridge. This functions by looking for various capability
1010 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
1011 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +00001012 *
Uwe Hermanne4870472010-11-04 23:23:47 +00001013 * When only a PCI-Express capability is found the type is examined to see
1014 * which type of bridge we have.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001015 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001016 * @param dev Pointer to the device structure of the bridge.
1017 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001018 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001019static struct device_operations *get_pci_bridge_ops(struct device *dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001020{
Julius Wernercd49cce2019-03-05 16:53:33 -08001021#if CONFIG(PCIX_PLUGIN_SUPPORT)
Ronald G. Minnich78a16672012-11-29 16:28:21 -08001022 unsigned int pcixpos;
1023 pcixpos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1024 if (pcixpos) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001025 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001026 return &default_pcix_ops_bus;
1027 }
1028#endif
Julius Wernercd49cce2019-03-05 16:53:33 -08001029#if CONFIG(PCIEXP_PLUGIN_SUPPORT)
Ronald G. Minnich78a16672012-11-29 16:28:21 -08001030 unsigned int pciexpos;
1031 pciexpos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
1032 if (pciexpos) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001033 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -08001034 flags = pci_read_config16(dev, pciexpos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001035 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001036 case PCI_EXP_TYPE_ROOT_PORT:
1037 case PCI_EXP_TYPE_UPSTREAM:
1038 case PCI_EXP_TYPE_DOWNSTREAM:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001039 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
Uwe Hermanne4870472010-11-04 23:23:47 +00001040 dev_path(dev));
Arthur Heymans24837e72021-03-11 20:34:05 +01001041 if (CONFIG(PCIEXP_HOTPLUG)) {
1042 u16 sltcap;
1043 sltcap = pci_read_config16(dev, pciexpos + PCI_EXP_SLTCAP);
1044 if (sltcap & PCI_EXP_SLTCAP_HPC) {
1045 printk(BIOS_DEBUG, "%s hot-plug capable\n",
1046 dev_path(dev));
1047 return &default_pciexp_hotplug_ops_bus;
1048 }
Jeremy Sollercf2ac542019-10-09 21:40:36 -06001049 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001050 return &default_pciexp_ops_bus;
1051 case PCI_EXP_TYPE_PCI_BRIDGE:
Uwe Hermanne4870472010-11-04 23:23:47 +00001052 printk(BIOS_DEBUG, "%s subordinate PCI\n",
1053 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001054 return &default_pci_ops_bus;
1055 default:
1056 break;
1057 }
1058 }
1059#endif
1060 return &default_pci_ops_bus;
1061}
1062
1063/**
Vadim Bendebury8049fc92012-04-24 12:53:19 -07001064 * Check if a device id matches a PCI driver entry.
1065 *
1066 * The driver entry can either point at a zero terminated array of acceptable
1067 * device IDs, or include a single device ID.
1068 *
Martin Roth98b698c2015-01-06 21:02:52 -07001069 * @param driver pointer to the PCI driver entry being checked
1070 * @param device_id PCI device ID of the device being matched
Vadim Bendebury8049fc92012-04-24 12:53:19 -07001071 */
1072static int device_id_match(struct pci_driver *driver, unsigned short device_id)
1073{
1074 if (driver->devices) {
1075 unsigned short check_id;
1076 const unsigned short *device_list = driver->devices;
1077 while ((check_id = *device_list++) != 0)
1078 if (check_id == device_id)
1079 return 1;
1080 }
1081
1082 return (driver->device == device_id);
1083}
1084
1085/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001086 * Set up PCI device operation.
1087 *
1088 * Check if it already has a driver. If not, use find_device_operations(),
1089 * or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +00001090 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001091 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +00001092 * @see pci_drivers
1093 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001094static void set_pci_ops(struct device *dev)
1095{
1096 struct pci_driver *driver;
Li-Ta Loe5266692004-03-23 21:28:05 +00001097
Uwe Hermanne4870472010-11-04 23:23:47 +00001098 if (dev->ops)
1099 return;
1100
1101 /*
1102 * Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +00001103 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001104 */
Aaron Durbin03758152015-09-03 17:23:08 -05001105 for (driver = &_pci_drivers[0]; driver != &_epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001106 if ((driver->vendor == dev->vendor) &&
Vadim Bendebury8049fc92012-04-24 12:53:19 -07001107 device_id_match(driver, dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +00001108 dev->ops = (struct device_operations *)driver->ops;
Nico Huber7e3e1ea2020-10-12 16:25:40 +02001109 break;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001110 }
1111 }
Li-Ta Loe5266692004-03-23 21:28:05 +00001112
Nico Huber7e3e1ea2020-10-12 16:25:40 +02001113 if (dev->ops) {
1114 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n", dev_path(dev),
1115 driver->vendor, driver->device, (driver->ops->scan_bus ? "bus " : ""));
1116 return;
1117 }
1118
Uwe Hermanne4870472010-11-04 23:23:47 +00001119 /* If I don't have a specific driver use the default operations. */
1120 switch (dev->hdr_type & 0x7f) { /* Header type */
1121 case PCI_HEADER_TYPE_NORMAL:
Eric Biederman8ca8d762003-04-22 19:02:15 +00001122 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
1123 goto bad;
1124 dev->ops = &default_pci_ops_dev;
1125 break;
1126 case PCI_HEADER_TYPE_BRIDGE:
1127 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1128 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001129 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001130 break;
Julius Wernercd49cce2019-03-05 16:53:33 -08001131#if CONFIG(CARDBUS_PLUGIN_SUPPORT)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001132 case PCI_HEADER_TYPE_CARDBUS:
1133 dev->ops = &default_cardbus_ops_bus;
1134 break;
1135#endif
Felix Singerc96ee7e2021-01-07 06:14:27 +00001136 default:
Uwe Hermanne4870472010-11-04 23:23:47 +00001137bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +00001138 if (dev->enabled) {
Angel Ponsd19cc112021-07-04 11:41:31 +02001139 printk(BIOS_ERR,
1140 "%s [%04x/%04x/%06x] has unknown header type %02x, ignoring.\n",
1141 dev_path(dev), dev->vendor, dev->device,
Uwe Hermanne4870472010-11-04 23:23:47 +00001142 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +00001143 }
Eric Biederman8ca8d762003-04-22 19:02:15 +00001144 }
Eric Biederman8ca8d762003-04-22 19:02:15 +00001145}
1146
1147/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001148 * See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +00001149 *
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001150 * Given a PCI bus structure and a devfn number, find the device structure
1151 * corresponding to the devfn, if present. Then move the device structure
1152 * as the last child on the bus.
Li-Ta Loe5266692004-03-23 21:28:05 +00001153 *
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001154 * @param bus Pointer to the bus structure.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001155 * @param devfn A device/function number.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001156 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +00001157 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001158 */
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001159static struct device *pci_scan_get_dev(struct bus *bus, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001160{
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001161 struct device *dev, **prev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001162
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001163 prev = &bus->children;
1164 for (dev = bus->children; dev; dev = dev->sibling) {
Duncan Lauriebf696222020-10-18 15:10:00 -07001165 if (dev->path.type == DEVICE_PATH_PCI && dev->path.pci.devfn == devfn) {
1166 /* Unlink from the list. */
1167 *prev = dev->sibling;
1168 dev->sibling = NULL;
1169 break;
Eric Biedermanad1b35a2003-10-14 02:36:51 +00001170 }
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001171 prev = &dev->sibling;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001172 }
Myles Watson29cc9ed2009-07-02 18:56:24 +00001173
Uwe Hermanne4870472010-11-04 23:23:47 +00001174 /*
1175 * Just like alloc_dev() add the device to the list of devices on the
Myles Watson29cc9ed2009-07-02 18:56:24 +00001176 * bus. When the list of devices was formed we removed all of the
1177 * parents children, and now we are interleaving static and dynamic
1178 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001179 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001180 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001181 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +00001182
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001183 /* Find the last child on the bus. */
1184 for (child = bus->children; child && child->sibling;)
Eric Biedermane9a271e32003-09-02 03:36:25 +00001185 child = child->sibling;
Uwe Hermanne4870472010-11-04 23:23:47 +00001186
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001187 /* Place the device as last on the bus. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001188 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +00001189 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001190 else
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001191 bus->children = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001192 }
1193
Eric Biederman8ca8d762003-04-22 19:02:15 +00001194 return dev;
1195}
1196
Myles Watson032a9652009-05-11 22:24:53 +00001197/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001198 * Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +00001199 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001200 * Determine the existence of a given PCI device. Allocate a new struct device
1201 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001202 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001203 * @param dev Pointer to the dev structure.
1204 * @param bus Pointer to the bus structure.
1205 * @param devfn A device/function number to look at.
1206 * @return The device structure for the device (if found), NULL otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001207 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001208struct device *pci_probe_dev(struct device *dev, struct bus *bus,
1209 unsigned int devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001210{
Myles Watson29cc9ed2009-07-02 18:56:24 +00001211 u32 id, class;
1212 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001213
Myles Watson29cc9ed2009-07-02 18:56:24 +00001214 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001215 if (!dev) {
1216 struct device dummy;
Uwe Hermanne4870472010-11-04 23:23:47 +00001217
Myles Watson29cc9ed2009-07-02 18:56:24 +00001218 dummy.bus = bus;
1219 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +00001220 dummy.path.pci.devfn = devfn;
Uwe Hermanne4870472010-11-04 23:23:47 +00001221
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001222 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +00001223 /*
1224 * Have we found something? Some broken boards return 0 if a
1225 * slot is empty, but the expected answer is 0xffffffff.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001226 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001227 if (id == 0xffffffff)
Stefan Reinauer7355c752010-04-02 16:30:25 +00001228 return NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +00001229
Stefan Reinauer7355c752010-04-02 16:30:25 +00001230 if ((id == 0x00000000) || (id == 0x0000ffff) ||
1231 (id == 0xffff0000)) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001232 printk(BIOS_SPEW, "%s, bad id 0x%x\n",
1233 dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001234 return NULL;
1235 }
1236 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001237 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +00001238 /*
1239 * Enable/disable the device. Once we have found the device-
Myles Watson29cc9ed2009-07-02 18:56:24 +00001240 * specific operations this operations we will disable the
1241 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +00001242 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001243 * This is geared toward devices that have subfunctions
1244 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +00001245 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001246 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +00001247 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001248 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001249 /* Run the magic enable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001250 if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001251 dev->chip_ops->enable_dev(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +00001252
Myles Watson29cc9ed2009-07-02 18:56:24 +00001253 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001254 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +00001255
Uwe Hermanne4870472010-11-04 23:23:47 +00001256 /*
1257 * If the device does not have a PCI ID disable it. Possibly
Myles Watson29cc9ed2009-07-02 18:56:24 +00001258 * this is because we have already disabled the device. But
1259 * this also handles optional devices that may not always
1260 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001261 */
1262 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001263 if ((id == 0xffffffff) || (id == 0x00000000) ||
1264 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001265 if (dev->enabled) {
Angel Ponsd19cc112021-07-04 11:41:31 +02001266 printk(BIOS_INFO,
1267 "PCI: Static device %s not found, disabling it.\n",
1268 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001269 dev->enabled = 0;
1270 }
1271 return dev;
1272 }
1273 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001274
Myles Watson29cc9ed2009-07-02 18:56:24 +00001275 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001276 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1277 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +00001278
Myles Watson29cc9ed2009-07-02 18:56:24 +00001279 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001280 dev->vendor = id & 0xffff;
1281 dev->device = (id >> 16) & 0xffff;
1282 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001283
1284 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001285 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +00001286
Myles Watson29cc9ed2009-07-02 18:56:24 +00001287 /* Architectural/System devices always need to be bus masters. */
Felix Singerd3d0fd72020-09-07 16:15:14 +02001288 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM &&
1289 CONFIG(PCI_ALLOW_BUS_MASTER_ANY_DEVICE))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001290 dev->command |= PCI_COMMAND_MASTER;
Uwe Hermanne4870472010-11-04 23:23:47 +00001291
1292 /*
1293 * Look at the vendor and device ID, or at least the header type and
Myles Watson29cc9ed2009-07-02 18:56:24 +00001294 * class and figure out which set of configuration methods to use.
1295 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001296 */
1297 set_pci_ops(dev);
1298
Myles Watson29cc9ed2009-07-02 18:56:24 +00001299 /* Now run the magic enable/disable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001300 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001301 dev->ops->enable(dev);
Myles Watson032a9652009-05-11 22:24:53 +00001302
Myles Watson29cc9ed2009-07-02 18:56:24 +00001303 /* Display the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001304 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n", dev_path(dev),
1305 dev->vendor, dev->device, dev->enabled ? "enabled" : "disabled",
1306 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001307
1308 return dev;
1309}
1310
Myles Watson032a9652009-05-11 22:24:53 +00001311/**
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001312 * Test for match between romstage and ramstage device instance.
1313 *
1314 * @param dev Pointer to the device structure.
1315 * @param sdev Simple device model identifier, created with PCI_DEV().
1316 * @return Non-zero if bus:dev.fn of device matches.
1317 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001318unsigned int pci_match_simple_dev(struct device *dev, pci_devfn_t sdev)
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001319{
Felix Held3b5b66d2024-01-11 22:26:18 +01001320 return dev->bus->secondary == PCI_DEV2BUS(sdev) &&
1321 dev->bus->segment_group == PCI_DEV2SEG(sdev) &&
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001322 dev->path.pci.devfn == PCI_DEV2DEVFN(sdev);
1323}
1324
1325/**
Bill XIE513d3592022-08-02 22:55:51 +08001326 * Test whether a capability is available along the whole path from the given
1327 * device to the host bridge.
1328 *
1329 * @param dev Pointer to the device structure.
1330 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
1331 * @return The next matching capability of the given device, if it is available
1332 * along the whole path, or zero if not.
1333 */
1334uint16_t pci_find_cap_recursive(const struct device *dev, uint16_t cap)
1335{
1336 assert(dev->bus);
1337 uint16_t pos = pci_find_capability(dev, cap);
1338 const struct device *bridge = dev->bus->dev;
1339 while (bridge && (bridge->path.type == DEVICE_PATH_PCI)) {
1340 assert(bridge->bus);
1341 if (!pci_find_capability(bridge, cap))
1342 return 0;
1343 bridge = bridge->bus->dev;
1344 }
1345 return pos;
1346}
1347
1348/**
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -06001349 * PCI devices that are marked as "hidden" do not get probed. However, the same
1350 * initialization logic is still performed as if it were. This is useful when
1351 * devices would like to be described in the devicetree.cb file, and/or present
1352 * static PCI resources to the allocator, but the platform firmware hides the
1353 * device (makes the device invisible to PCI enumeration) before PCI enumeration
1354 * takes place.
1355 *
1356 * The expected semantics of PCI devices marked as 'hidden':
1357 * 1) The device is actually present under the specified BDF
1358 * 2) The device config space can still be accessed somehow, but the Vendor ID
1359 * indicates there is no device there (it reads as 0xffffffff).
1360 * 3) The device may still consume PCI resources. Typically, these would have
1361 * been hardcoded elsewhere.
1362 *
1363 * @param dev Pointer to the device structure.
1364 */
1365static void pci_scan_hidden_device(struct device *dev)
1366{
1367 if (dev->chip_ops && dev->chip_ops->enable_dev)
1368 dev->chip_ops->enable_dev(dev);
1369
1370 /*
1371 * If chip_ops->enable_dev did not set dev->ops, then set to a default
1372 * .ops, because PCI enumeration is effectively being skipped, therefore
1373 * no PCI driver will bind to this device. However, children may want to
1374 * be enumerated, so this provides scan_static_bus for the .scan_bus
1375 * callback.
1376 */
1377 if (dev->ops == NULL)
1378 dev->ops = &default_hidden_pci_ops_dev;
1379
1380 if (dev->ops->enable)
1381 dev->ops->enable(dev);
1382
1383 /* Display the device almost as if it were probed normally */
1384 printk(BIOS_DEBUG, "%s [0000/%04x] hidden%s\n", dev_path(dev),
1385 dev->device, dev->ops ? "" : " No operations");
1386}
1387
1388/**
Jianjun Wang777ffff2021-07-24 14:50:36 +08001389 * A PCIe Downstream Port normally leads to a Link with only Device 0 on it
1390 * (PCIe spec r5.0, sec 7.3.1). As an optimization, scan only for Device 0 in
1391 * that situation.
1392 *
1393 * @param bus Pointer to the bus structure.
1394 */
1395static bool pci_bus_only_one_child(struct bus *bus)
1396{
1397 struct device *bridge = bus->dev;
1398 u16 pcie_pos, pcie_flags_reg;
1399 int pcie_type;
1400
Arthur Heymansdb199cc2022-01-06 20:56:01 +01001401 if (!bridge)
1402 return false;
1403
Nico Huberf514b8a2022-02-25 14:25:57 +01001404 if (bridge->path.type != DEVICE_PATH_PCI)
1405 return false;
1406
Jianjun Wang777ffff2021-07-24 14:50:36 +08001407 pcie_pos = pci_find_capability(bridge, PCI_CAP_ID_PCIE);
1408 if (!pcie_pos)
1409 return false;
1410
1411 pcie_flags_reg = pci_read_config16(bridge, pcie_pos + PCI_EXP_FLAGS);
1412
1413 pcie_type = (pcie_flags_reg & PCI_EXP_FLAGS_TYPE) >> 4;
1414
1415 return pciexp_is_downstream_port(pcie_type);
1416}
1417
1418/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001419 * Scan a PCI bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001420 *
Li-Ta Loe5266692004-03-23 21:28:05 +00001421 * Determine the existence of devices and bridges on a PCI bus. If there are
1422 * bridges on the bus, recursively scan the buses behind the bridges.
1423 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001424 * @param bus Pointer to the bus structure.
1425 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1426 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001427 */
Martin Roth38ddbfb2019-10-23 21:41:00 -06001428void pci_scan_bus(struct bus *bus, unsigned int min_devfn,
1429 unsigned int max_devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001430{
1431 unsigned int devfn;
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001432 struct device *dev, **prev;
1433 int once = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001434
Felix Held3b5b66d2024-01-11 22:26:18 +01001435 printk(BIOS_DEBUG, "PCI: %s for segment group %02x bus %02x\n", __func__,
1436 bus->segment_group, bus->secondary);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001437
Uwe Hermanne4870472010-11-04 23:23:47 +00001438 /* Maximum sane devfn is 0xFF. */
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001439 if (max_devfn > 0xff) {
Elyes HAOUASf984aec2021-01-16 17:29:17 +01001440 printk(BIOS_ERR, "PCI: %s limits devfn %x - devfn %x\n",
1441 __func__, min_devfn, max_devfn);
1442 printk(BIOS_ERR, "PCI: %s upper limit too big. Using 0xff.\n", __func__);
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001443 max_devfn=0xff;
1444 }
1445
lilacious40cb3fe2023-06-21 23:24:14 +02001446 post_code(POSTCODE_ENTER_PCI_SCAN_BUS);
Uwe Hermanne4870472010-11-04 23:23:47 +00001447
Jianjun Wang777ffff2021-07-24 14:50:36 +08001448 if (pci_bus_only_one_child(bus))
1449 max_devfn = MIN(max_devfn, 0x07);
1450
Uwe Hermanne4870472010-11-04 23:23:47 +00001451 /*
1452 * Probe all devices/functions on this bus with some optimization for
Myles Watson29cc9ed2009-07-02 18:56:24 +00001453 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001454 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001455 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +00001456 if (CONFIG(MINIMAL_PCI_SCANNING)) {
1457 dev = pcidev_path_behind(bus, devfn);
1458 if (!dev || !dev->mandatory)
1459 continue;
1460 }
1461
Uwe Hermanne4870472010-11-04 23:23:47 +00001462 /* First thing setup the device structure. */
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001463 dev = pci_scan_get_dev(bus, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001464
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -06001465 /* Devices marked 'hidden' do not get probed */
1466 if (dev && dev->hidden) {
1467 pci_scan_hidden_device(dev);
1468
1469 /* Skip pci_probe_dev, go to next devfn */
1470 continue;
1471 }
1472
Myles Watson29cc9ed2009-07-02 18:56:24 +00001473 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001474 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001475
Uwe Hermanne4870472010-11-04 23:23:47 +00001476 /*
1477 * If this is not a multi function device, or the device is
Myles Watson29cc9ed2009-07-02 18:56:24 +00001478 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001479 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001480 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001481 if ((PCI_FUNC(devfn) == 0x00) && (!dev
Myles Watson29cc9ed2009-07-02 18:56:24 +00001482 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001483 devfn += 0x07;
1484 }
1485 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001486
Uwe Hermanne4870472010-11-04 23:23:47 +00001487 /*
Elyes HAOUAS0ce74162021-01-16 14:43:49 +01001488 * Warn if any leftover static devices are found.
Uwe Hermanne4870472010-11-04 23:23:47 +00001489 * There's probably a problem in devicetree.cb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001490 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001491
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001492 prev = &bus->children;
1493 for (dev = bus->children; dev; dev = dev->sibling) {
Duncan Lauriebf696222020-10-18 15:10:00 -07001494
1495 /*
1496 * If static device is not PCI then enable it here and don't
1497 * treat it as a leftover device.
1498 */
1499 if (dev->path.type != DEVICE_PATH_PCI) {
1500 enable_static_device(dev);
1501 continue;
1502 }
1503
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -06001504 /*
1505 * The device is only considered leftover if it is not hidden
1506 * and it has a Vendor ID of 0 (the default for a device that
1507 * could not be probed).
1508 */
1509 if (dev->vendor != 0 || dev->hidden) {
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001510 prev = &dev->sibling;
1511 continue;
1512 }
1513
1514 /* Unlink it from list. */
1515 *prev = dev->sibling;
1516
1517 if (!once++)
1518 printk(BIOS_WARNING, "PCI: Leftover static devices:\n");
1519 printk(BIOS_WARNING, "%s\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001520 }
1521
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001522 if (once)
1523 printk(BIOS_WARNING, "PCI: Check your devicetree.cb.\n");
1524
Uwe Hermanne4870472010-11-04 23:23:47 +00001525 /*
1526 * For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001527 * scan the bus behind that child.
1528 */
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001529
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +02001530 scan_bridges(bus);
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001531
Uwe Hermanne4870472010-11-04 23:23:47 +00001532 /*
1533 * We've scanned the bus and so we know all about what's on the other
Myles Watson29cc9ed2009-07-02 18:56:24 +00001534 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001535 * Return how far we've got finding sub-buses.
1536 */
lilacious40cb3fe2023-06-21 23:24:14 +02001537 post_code(POSTCODE_EXIT_PCI_SCAN_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001538}
1539
Kyösti Mälkki33452402015-02-23 06:58:26 +02001540typedef enum {
1541 PCI_ROUTE_CLOSE,
1542 PCI_ROUTE_SCAN,
1543 PCI_ROUTE_FINAL,
1544} scan_state;
1545
1546static void pci_bridge_route(struct bus *link, scan_state state)
1547{
1548 struct device *dev = link->dev;
1549 struct bus *parent = dev->bus;
Arthur Heymansf879d362021-11-10 22:09:58 +01001550 uint8_t primary, secondary, subordinate;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001551
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001552 if (state == PCI_ROUTE_SCAN) {
1553 link->secondary = parent->subordinate + 1;
Jeremy Sollercf2ac542019-10-09 21:40:36 -06001554 link->subordinate = link->secondary + dev->hotplug_buses;
Arthur Heymans20d25772021-11-17 17:25:48 +01001555 link->max_subordinate = parent->max_subordinate
1556 ? parent->max_subordinate
Felix Held3b5b66d2024-01-11 22:26:18 +01001557 : (PCI_BUSES_PER_SEGMENT_GROUP - 1);
1558 link->segment_group = parent->segment_group;
Arthur Heymans20d25772021-11-17 17:25:48 +01001559 }
1560
1561 if (link->secondary > link->max_subordinate)
1562 die("%s: No more busses available!\n", __func__);
1563
1564 /* This ought to only happen with hotplug buses. */
1565 if (link->subordinate > link->max_subordinate) {
1566 printk(BIOS_WARNING, "%s: Limiting subordinate busses\n", __func__);
1567 link->subordinate = link->max_subordinate;
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001568 }
1569
Kyösti Mälkki33452402015-02-23 06:58:26 +02001570 if (state == PCI_ROUTE_CLOSE) {
Arthur Heymansf879d362021-11-10 22:09:58 +01001571 primary = 0;
1572 secondary = 0xff;
1573 subordinate = 0xfe;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001574 } else if (state == PCI_ROUTE_SCAN) {
Arthur Heymansf879d362021-11-10 22:09:58 +01001575 primary = parent->secondary;
1576 secondary = link->secondary;
Arthur Heymans20d25772021-11-17 17:25:48 +01001577 subordinate = link->max_subordinate;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001578 } else if (state == PCI_ROUTE_FINAL) {
Arthur Heymansf879d362021-11-10 22:09:58 +01001579 primary = parent->secondary;
1580 secondary = link->secondary;
1581 subordinate = link->subordinate;
Arthur Heymans4a3331d2022-03-23 17:58:46 +01001582 } else {
1583 return;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001584 }
1585
1586 if (state == PCI_ROUTE_SCAN) {
1587 /* Clear all status bits and turn off memory, I/O and master enables. */
1588 link->bridge_cmd = pci_read_config16(dev, PCI_COMMAND);
1589 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1590 pci_write_config16(dev, PCI_STATUS, 0xffff);
1591 }
1592
1593 /*
1594 * Configure the bus numbers for this bridge: the configuration
1595 * transactions will not be propagated by the bridge if it is not
1596 * correctly configured.
1597 */
Arthur Heymansf879d362021-11-10 22:09:58 +01001598 pci_write_config8(dev, PCI_PRIMARY_BUS, primary);
1599 pci_write_config8(dev, PCI_SECONDARY_BUS, secondary);
1600 pci_write_config8(dev, PCI_SUBORDINATE_BUS, subordinate);
Kyösti Mälkki33452402015-02-23 06:58:26 +02001601
1602 if (state == PCI_ROUTE_FINAL) {
1603 pci_write_config16(dev, PCI_COMMAND, link->bridge_cmd);
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001604 parent->subordinate = link->subordinate;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001605 }
1606}
1607
Li-Ta Loe5266692004-03-23 21:28:05 +00001608/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001609 * Scan a PCI bridge and the buses behind the bridge.
Li-Ta Loe5266692004-03-23 21:28:05 +00001610 *
1611 * Determine the existence of buses behind the bridge. Set up the bridge
1612 * according to the result of the scan.
1613 *
1614 * This function is the default scan_bus() method for PCI bridge devices.
1615 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001616 * @param dev Pointer to the bridge device.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001617 * @param do_scan_bus TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +00001618 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001619void do_pci_scan_bridge(struct device *dev,
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001620 void (*do_scan_bus) (struct bus * bus,
Martin Roth38ddbfb2019-10-23 21:41:00 -06001621 unsigned int min_devfn,
1622 unsigned int max_devfn))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001623{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001624 struct bus *bus;
Eric Biederman83b991a2003-10-11 06:20:25 +00001625
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001626 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
Li-Ta Lo3a812852004-12-03 22:39:34 +00001627
Myles Watson894a3472010-06-09 22:41:35 +00001628 if (dev->link_list == NULL) {
1629 struct bus *link;
1630 link = malloc(sizeof(*link));
1631 if (link == NULL)
1632 die("Couldn't allocate a link!\n");
1633 memset(link, 0, sizeof(*link));
1634 link->dev = dev;
1635 dev->link_list = link;
1636 }
1637
1638 bus = dev->link_list;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001639
Nico Huber061b9052019-09-21 15:58:23 +02001640 pci_bridge_vga_compat(bus);
1641
Kyösti Mälkki33452402015-02-23 06:58:26 +02001642 pci_bridge_route(bus, PCI_ROUTE_SCAN);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001643
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001644 do_scan_bus(bus, 0x00, 0xff);
Kyösti Mälkki33452402015-02-23 06:58:26 +02001645
1646 pci_bridge_route(bus, PCI_ROUTE_FINAL);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001647}
Li-Ta Loe5266692004-03-23 21:28:05 +00001648
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001649/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001650 * Scan a PCI bridge and the buses behind the bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001651 *
1652 * Determine the existence of buses behind the bridge. Set up the bridge
1653 * according to the result of the scan.
1654 *
1655 * This function is the default scan_bus() method for PCI bridge devices.
1656 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001657 * @param dev Pointer to the bridge device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001658 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001659void pci_scan_bridge(struct device *dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001660{
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001661 do_pci_scan_bridge(dev, pci_scan_bus);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001662}
1663
Myles Watson29cc9ed2009-07-02 18:56:24 +00001664/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001665 * Scan a PCI domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001666 *
1667 * This function is the default scan_bus() method for PCI domains.
1668 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001669 * @param dev Pointer to the domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001670 */
Arthur Heymans0b0113f2023-08-31 17:09:28 +02001671void pci_host_bridge_scan_bus(struct device *dev)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001672{
Kyösti Mälkki6f370172015-03-19 15:26:52 +02001673 struct bus *link = dev->link_list;
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001674 pci_scan_bus(link, PCI_DEVFN(0, 0), 0xff);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001675}
1676
Angel Ponsb6519812021-12-31 13:33:50 +01001677void pci_dev_disable_bus_master(const struct device *dev)
1678{
1679 pci_update_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MASTER, 0x0);
1680}
1681
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001682/**
1683 * Take an INT_PIN number (0, 1 - 4) and convert
1684 * it to a string ("NO PIN", "PIN A" - "PIN D")
1685 *
1686 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1687 * @return A string corresponding to the pin number or "Invalid"
1688 */
1689const char *pin_to_str(int pin)
1690{
1691 const char *str[5] = {
1692 "NO PIN",
1693 "PIN A",
1694 "PIN B",
1695 "PIN C",
1696 "PIN D",
1697 };
1698
1699 if (pin >= 0 && pin <= 4)
1700 return str[pin];
1701 else
1702 return "Invalid PIN, not 0 - 4";
1703}
1704
1705/**
1706 * Get the PCI INT_PIN swizzle for a device defined as:
1707 * pin_parent = (pin_child + devn_child) % 4 + 1
1708 * where PIN A = 1 ... PIN_D = 4
1709 *
1710 * Given a PCI device structure 'dev', find the interrupt pin
1711 * that will be triggered on its parent bridge device when
1712 * generating an interrupt. For example: Device 1:3.2 may
1713 * use INT_PIN A but will trigger PIN D on its parent bridge
1714 * device. In this case, this function will return 4 (PIN D).
1715 *
1716 * @param dev A PCI device structure to swizzle interrupt pins for
Martin Roth32bc6b62015-01-04 16:54:35 -07001717 * @param *parent_bridge The PCI device structure for the bridge
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001718 * device 'dev' is attached to
1719 * @return The interrupt pin number (1 - 4) that 'dev' will
1720 * trigger when generating an interrupt
1721 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001722static int swizzle_irq_pins(struct device *dev, struct device **parent_bridge)
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001723{
Aaron Durbinc30d9132017-08-07 16:55:43 -06001724 struct device *parent; /* Our current device's parent device */
1725 struct device *child; /* The child device of the parent */
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001726 uint8_t parent_bus = 0; /* Parent Bus number */
1727 uint16_t parent_devfn = 0; /* Parent Device and Function number */
1728 uint16_t child_devfn = 0; /* Child Device and Function number */
1729 uint8_t swizzled_pin = 0; /* Pin swizzled across a bridge */
1730
1731 /* Start with PIN A = 0 ... D = 3 */
1732 swizzled_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN) - 1;
1733
1734 /* While our current device has parent devices */
1735 child = dev;
1736 for (parent = child->bus->dev; parent; parent = parent->bus->dev) {
1737 parent_bus = parent->bus->secondary;
1738 parent_devfn = parent->path.pci.devfn;
1739 child_devfn = child->path.pci.devfn;
1740
1741 /* Swizzle the INT_PIN for any bridges not on root bus */
1742 swizzled_pin = (PCI_SLOT(child_devfn) + swizzled_pin) % 4;
1743 printk(BIOS_SPEW, "\tWith INT_PIN swizzled to %s\n"
1744 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1745 pin_to_str(swizzled_pin + 1), parent_bus,
1746 PCI_SLOT(parent_devfn), PCI_FUNC(parent_devfn));
1747
1748 /* Continue until we find the root bus */
1749 if (parent_bus > 0) {
1750 /*
1751 * We will go on to the next parent so this parent
1752 * becomes the child
1753 */
1754 child = parent;
1755 continue;
1756 } else {
1757 /*
1758 * Found the root bridge device,
1759 * fill in the structure and exit
1760 */
1761 *parent_bridge = parent;
1762 break;
1763 }
1764 }
1765
1766 /* End with PIN A = 1 ... D = 4 */
1767 return swizzled_pin + 1;
1768}
1769
1770/**
1771 * Given a device structure 'dev', find its interrupt pin
1772 * and its parent bridge 'parent_bdg' device structure.
1773 * If it is behind a bridge, it will return the interrupt
1774 * pin number (1 - 4) of the parent bridge that the device
1775 * interrupt pin has been swizzled to, otherwise it will
1776 * return the interrupt pin that is programmed into the
1777 * PCI config space of the target device. If 'dev' is
1778 * behind a bridge, it will fill in 'parent_bdg' with the
1779 * device structure of the bridge it is behind, otherwise
1780 * it will copy 'dev' into 'parent_bdg'.
1781 *
1782 * @param dev A PCI device structure to get interrupt pins for.
1783 * @param *parent_bdg The PCI device structure for the bridge
1784 * device 'dev' is attached to.
1785 * @return The interrupt pin number (1 - 4) that 'dev' will
1786 * trigger when generating an interrupt.
1787 * Errors: -1 is returned if the device is not enabled
1788 * -2 is returned if a parent bridge could not be found.
1789 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001790int get_pci_irq_pins(struct device *dev, struct device **parent_bdg)
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001791{
1792 uint8_t bus = 0; /* The bus this device is on */
1793 uint16_t devfn = 0; /* This device's device and function numbers */
1794 uint8_t int_pin = 0; /* Interrupt pin used by the device */
1795 uint8_t target_pin = 0; /* Interrupt pin we want to assign an IRQ to */
1796
1797 /* Make sure this device is enabled */
1798 if (!(dev->enabled && (dev->path.type == DEVICE_PATH_PCI)))
1799 return -1;
1800
1801 bus = dev->bus->secondary;
1802 devfn = dev->path.pci.devfn;
1803
1804 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1805 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1806 if (int_pin < 1 || int_pin > 4)
1807 return -1;
1808
1809 printk(BIOS_SPEW, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1810 bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pin_to_str(int_pin));
1811
1812 /* If this device is on a bridge, swizzle its INT_PIN */
1813 if (bus) {
1814 /* Swizzle its INT_PINs */
1815 target_pin = swizzle_irq_pins(dev, parent_bdg);
1816
1817 /* Make sure the swizzle returned valid structures */
1818 if (parent_bdg == NULL) {
Julius Wernere9665952022-01-21 17:06:20 -08001819 printk(BIOS_WARNING, "Could not find parent bridge for this device!\n");
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001820 return -2;
1821 }
1822 } else { /* Device is not behind a bridge */
1823 target_pin = int_pin; /* Return its own interrupt pin */
1824 *parent_bdg = dev; /* Return its own structure */
1825 }
1826
1827 /* Target pin is the interrupt pin we want to assign an IRQ to */
1828 return target_pin;
1829}
1830
Julius Wernercd49cce2019-03-05 16:53:33 -08001831#if CONFIG(PC80_SYSTEM)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001832/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001833 * Assign IRQ numbers.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001834 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001835 * This function assigns IRQs for all functions contained within the indicated
Uwe Hermanne4870472010-11-04 23:23:47 +00001836 * device address. If the device does not exist or does not require interrupts
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001837 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001838 *
1839 * This function should be called for each PCI slot in your system.
1840 *
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001841 * @param dev Pointer to dev structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001842 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1843 * of this slot. The particular IRQ #s that are passed in depend on the
1844 * routing inside your southbridge and on your board.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001845 */
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001846void pci_assign_irqs(struct device *dev, const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001847{
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001848 u8 slot, line, irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001849
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001850 /* Each device may contain up to eight functions. */
1851 slot = dev->path.pci.devfn >> 3;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001852
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001853 for (; dev ; dev = dev->sibling) {
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001854
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001855 if (dev->path.pci.devfn >> 3 != slot)
1856 break;
1857
1858 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001859
Uwe Hermanne4870472010-11-04 23:23:47 +00001860 /* PCI spec says all values except 1..4 are reserved. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001861 if ((line < 1) || (line > 4))
1862 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001863
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001864 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001865
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001866 printk(BIOS_DEBUG, "Assigning IRQ %d to %s\n", irq, dev_path(dev));
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001867
Angel Ponsceca5de2021-06-28 11:59:33 +02001868 pci_write_config8(dev, PCI_INTERRUPT_LINE, irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001869
Uwe Hermanne4870472010-11-04 23:23:47 +00001870 /* Change to level triggered. */
Angel Ponsceca5de2021-06-28 11:59:33 +02001871 i8259_configure_irq_trigger(irq, IRQ_LEVEL_TRIGGERED);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001872 }
1873}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001874#endif