blob: 44c47a75486a352e14b008515c2e4d03760e06f2 [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>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020010#include <device/pci_ops.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100011#include <bootmode.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000012#include <console/console.h>
Furquan Shaikh871baf22020-03-12 17:51:24 -070013#include <cpu/cpu.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000014#include <stdlib.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000015#include <string.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100016#include <delay.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100017#include <device/cardbus.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000018#include <device/device.h>
19#include <device/pci.h>
20#include <device/pci_ids.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000021#include <device/pcix.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000022#include <device/pciexp.h>
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -070023#include <lib.h>
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000024#include <pc80/i8259.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020025#include <security/vboot/vbnv.h>
Martin Roth5dd4a2a2018-03-06 16:10:45 -070026#include <timestamp.h>
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +020027#include <types.h>
28
Myles Watson29cc9ed2009-07-02 18:56:24 +000029u8 pci_moving_config8(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000030{
Myles Watson29cc9ed2009-07-02 18:56:24 +000031 u8 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000032
Eric Biederman03acab62004-10-14 21:25:53 +000033 value = pci_read_config8(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000034
Eric Biederman03acab62004-10-14 21:25:53 +000035 pci_write_config8(dev, reg, 0xff);
36 ones = pci_read_config8(dev, reg);
37
38 pci_write_config8(dev, reg, 0x00);
39 zeroes = pci_read_config8(dev, reg);
40
41 pci_write_config8(dev, reg, value);
42
43 return ones ^ zeroes;
44}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000045
Uwe Hermanne4870472010-11-04 23:23:47 +000046u16 pci_moving_config16(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000047{
Myles Watson29cc9ed2009-07-02 18:56:24 +000048 u16 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000049
Eric Biederman03acab62004-10-14 21:25:53 +000050 value = pci_read_config16(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000051
Eric Biederman03acab62004-10-14 21:25:53 +000052 pci_write_config16(dev, reg, 0xffff);
53 ones = pci_read_config16(dev, reg);
54
55 pci_write_config16(dev, reg, 0x0000);
56 zeroes = pci_read_config16(dev, reg);
57
58 pci_write_config16(dev, reg, value);
59
60 return ones ^ zeroes;
61}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000062
Uwe Hermanne4870472010-11-04 23:23:47 +000063u32 pci_moving_config32(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000064{
Myles Watson29cc9ed2009-07-02 18:56:24 +000065 u32 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000066
Eric Biederman03acab62004-10-14 21:25:53 +000067 value = pci_read_config32(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000068
Eric Biederman03acab62004-10-14 21:25:53 +000069 pci_write_config32(dev, reg, 0xffffffff);
70 ones = pci_read_config32(dev, reg);
71
72 pci_write_config32(dev, reg, 0x00000000);
73 zeroes = pci_read_config32(dev, reg);
74
75 pci_write_config32(dev, reg, value);
76
77 return ones ^ zeroes;
78}
79
Myles Watson29cc9ed2009-07-02 18:56:24 +000080/**
Myles Watson29cc9ed2009-07-02 18:56:24 +000081 * Given a device and register, read the size of the BAR for that register.
82 *
83 * @param dev Pointer to the device structure.
84 * @param index Address of the PCI configuration register.
Uwe Hermannc1ee4292010-10-17 19:01:48 +000085 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +000086 */
Eric Biederman03acab62004-10-14 21:25:53 +000087struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +000088{
Eric Biederman5cd81732004-03-11 15:01:31 +000089 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +000090 unsigned long value, attr;
Myles Watson29cc9ed2009-07-02 18:56:24 +000091 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +000092
Myles Watson29cc9ed2009-07-02 18:56:24 +000093 /* Initialize the resources to nothing. */
Eric Biederman03acab62004-10-14 21:25:53 +000094 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +000095
Myles Watson29cc9ed2009-07-02 18:56:24 +000096 /* Get the initial value. */
Eric Biederman03acab62004-10-14 21:25:53 +000097 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +000098
Myles Watson29cc9ed2009-07-02 18:56:24 +000099 /* See which bits move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000100 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000101
Myles Watson29cc9ed2009-07-02 18:56:24 +0000102 /* Initialize attr to the bits that do not move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000103 attr = value & ~moving;
104
Myles Watson29cc9ed2009-07-02 18:56:24 +0000105 /* If it is a 64bit resource look at the high half as well. */
Eric Biederman03acab62004-10-14 21:25:53 +0000106 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000107 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
108 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
109 /* Find the high bits that move. */
110 moving |=
111 ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000112 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000113
Myles Watson032a9652009-05-11 22:24:53 +0000114 /* Find the resource constraints.
Eric Biederman03acab62004-10-14 21:25:53 +0000115 * Start by finding the bits that move. From there:
116 * - Size is the least significant bit of the bits that move.
117 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000118 * See PCI Spec 6.2.5.1.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000119 */
Eric Biederman03acab62004-10-14 21:25:53 +0000120 limit = 0;
121 if (moving) {
122 resource->size = 1;
123 resource->align = resource->gran = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000124 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000125 resource->size <<= 1;
126 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000127 resource->gran += 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000128 }
129 resource->limit = limit = moving | (resource->size - 1);
Nico Huber8193b062015-10-21 15:43:41 +0200130
131 if (pci_base_address_is_memory_space(attr)) {
132 /* Page-align to allow individual mapping of devices. */
133 if (resource->align < 12)
134 resource->align = 12;
135 }
Eric Biederman03acab62004-10-14 21:25:53 +0000136 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000137
Uwe Hermanne4870472010-11-04 23:23:47 +0000138 /*
139 * Some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000140 * really size correctly.
Uwe Hermanne4870472010-11-04 23:23:47 +0000141 *
142 * Example: the Acer M7229 has BARs 1-4 normally read-only,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000143 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
Uwe Hermanne4870472010-11-04 23:23:47 +0000144 * by writing 0xffffffff to it, it will read back as 0x1f1 -- which
145 * is a violation of the spec.
146 *
147 * We catch this case and ignore it by observing which bits move.
148 *
149 * This also catches the common case of unimplemented registers
Eric Biederman03acab62004-10-14 21:25:53 +0000150 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000151 */
Eric Biederman03acab62004-10-14 21:25:53 +0000152 if (moving == 0) {
153 if (value != 0) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200154 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000155 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000156 }
157 resource->flags = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000158 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
159 /* An I/O mapped base address. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000160 resource->flags |= IORESOURCE_IO;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000161 /* I don't want to deal with 32bit I/O resources. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000162 resource->limit = 0xffff;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000163 } else {
164 /* A Memory mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000165 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000166 resource->flags |= IORESOURCE_MEM;
Uwe Hermanne4870472010-11-04 23:23:47 +0000167 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000168 resource->flags |= IORESOURCE_PREFETCH;
Eric Biederman03acab62004-10-14 21:25:53 +0000169 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
170 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000171 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000172 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000173 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
174 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000175 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000176 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
177 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000178 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000179 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000180 } else {
181 /* Invalid value. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000182 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
183 printk(BIOS_ERR, " on dev %s at index %02lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000184 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000185 resource->flags = 0;
186 }
187 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000188
Myles Watson29cc9ed2009-07-02 18:56:24 +0000189 /* Don't let the limit exceed which bits can move. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000190 if (resource->limit > limit)
Eric Biederman03acab62004-10-14 21:25:53 +0000191 resource->limit = limit;
Eric Biederman03acab62004-10-14 21:25:53 +0000192
Eric Biederman5cd81732004-03-11 15:01:31 +0000193 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000194}
195
Myles Watson29cc9ed2009-07-02 18:56:24 +0000196/**
197 * Given a device and an index, read the size of the BAR for that register.
198 *
199 * @param dev Pointer to the device structure.
200 * @param index Address of the PCI configuration register.
201 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000202static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000203{
204 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000205 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000206 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000207
Myles Watson29cc9ed2009-07-02 18:56:24 +0000208 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000209 resource = new_resource(dev, index);
210
Myles Watson29cc9ed2009-07-02 18:56:24 +0000211 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000212 value = pci_read_config32(dev, index);
213
Myles Watson29cc9ed2009-07-02 18:56:24 +0000214 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000215 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000216
217 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000218 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000219
Myles Watson032a9652009-05-11 22:24:53 +0000220 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000221 * Start by finding the bits that move. From there:
222 * - Size is the least significant bit of the bits that move.
223 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000224 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000225 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000226 if (moving) {
227 resource->size = 1;
228 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000229 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000230 resource->size <<= 1;
231 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000232 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000233 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000234 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000235 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
236 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000237 if (value != 0) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200238 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000239 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000240 }
241 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000242 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000243 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000244}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000245
Myles Watson29cc9ed2009-07-02 18:56:24 +0000246/**
Patrick Rudolph4e2f95b2018-05-16 14:56:22 +0200247 * Given a device, read the size of the MSI-X table.
248 *
249 * @param dev Pointer to the device structure.
250 * @return MSI-X table size or 0 if not MSI-X capable device
251 */
252size_t pci_msix_table_size(struct device *dev)
253{
254 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
255 if (!pos)
256 return 0;
257
258 const u16 control = pci_read_config16(dev, pos + PCI_MSIX_FLAGS);
259 return (control & PCI_MSIX_FLAGS_QSIZE) + 1;
260}
261
262/**
263 * Given a device, return the table offset and bar the MSI-X tables resides in.
264 *
265 * @param dev Pointer to the device structure.
266 * @param offset Returned value gives the offset in bytes inside the PCI BAR.
267 * @param idx The returned value is the index of the PCI_BASE_ADDRESS register
268 * the MSI-X table is located in.
269 * @return Zero on success
270 */
271int pci_msix_table_bar(struct device *dev, u32 *offset, u8 *idx)
272{
273 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
274 if (!pos || !offset || !idx)
275 return 1;
276
277 *offset = pci_read_config32(dev, pos + PCI_MSIX_TABLE);
278 *idx = (u8)(*offset & PCI_MSIX_PBA_BIR);
279 *offset &= PCI_MSIX_PBA_OFFSET;
280
281 return 0;
282}
283
284/**
285 * Given a device, return a msix_entry pointer or NULL if no table was found.
286 *
287 * @param dev Pointer to the device structure.
288 *
289 * @return NULL on error
290 */
291struct msix_entry *pci_msix_get_table(struct device *dev)
292{
293 struct resource *res;
294 u32 offset;
295 u8 idx;
296
297 if (pci_msix_table_bar(dev, &offset, &idx))
298 return NULL;
299
300 if (idx > 5)
301 return NULL;
302
303 res = probe_resource(dev, idx * 4 + PCI_BASE_ADDRESS_0);
304 if (!res || !res->base || offset >= res->size)
305 return NULL;
306
307 if ((res->flags & IORESOURCE_PCI64) &&
308 (uintptr_t)res->base != res->base)
309 return NULL;
310
311 return (struct msix_entry *)((uintptr_t)res->base + offset);
312}
313
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700314static unsigned int get_rebar_offset(const struct device *dev, unsigned long index)
315{
316 uint32_t offset = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_RESIZABLE_BAR);
317 if (!offset)
318 return 0;
319
320 /* Convert PCI_BASE_ADDRESS_0, ..._1, ..._2 into 0, 1, 2... */
321 const unsigned int find_bar_idx = (index - PCI_BASE_ADDRESS_0) /
322 sizeof(uint32_t);
323
324 /* Although all of the Resizable BAR Control Registers contain an
325 "NBARs" field, it is only valid in the Control Register for BAR 0 */
326 const uint32_t rebar_ctrl0 = pci_read_config32(dev, offset + PCI_REBAR_CTRL_OFFSET);
327 const unsigned int nbars = (rebar_ctrl0 & PCI_REBAR_CTRL_NBARS_MASK) >>
328 PCI_REBAR_CTRL_NBARS_SHIFT;
329
330 for (unsigned int i = 0; i < nbars; i++, offset += sizeof(uint64_t)) {
331 const uint32_t rebar_ctrl = pci_read_config32(
332 dev, offset + PCI_REBAR_CTRL_OFFSET);
333 const uint32_t bar_idx = rebar_ctrl & PCI_REBAR_CTRL_IDX_MASK;
334 if (bar_idx == find_bar_idx)
335 return offset;
336 }
337
338 return 0;
339}
340
341/* Bit 20 = 1 MiB, bit 21 = 2 MiB, bit 22 = 4 MiB, ... bit 63 = 8 EiB */
342static uint64_t get_rebar_sizes_mask(const struct device *dev,
343 unsigned long index)
344{
345 uint64_t size_mask = 0ULL;
346 const uint32_t offset = get_rebar_offset(dev, index);
347 if (!offset)
348 return 0;
349
350 /* Get 1 MB - 128 TB support from CAP register */
351 const uint32_t cap = pci_read_config32(dev, offset + PCI_REBAR_CAP_OFFSET);
352 /* Shift the bits from 4-31 to 0-27 (i.e., down by 4 bits) */
353 size_mask |= ((cap & PCI_REBAR_CAP_SIZE_MASK) >> 4);
354
355 /* Get 256 TB - 8 EB support from CTRL register and store it in bits 28-43 */
356 const uint64_t ctrl = pci_read_config32(dev, offset + PCI_REBAR_CTRL_OFFSET);
357 /* Shift ctrl mask from bit 16 to bit 28, so that the two
358 masks (fom cap and ctrl) form a contiguous bitmask when
359 concatenated (i.e., up by 12 bits). */
360 size_mask |= ((ctrl & PCI_REBAR_CTRL_SIZE_MASK) << 12);
361
362 /* Now that the mask occupies bits 0-43, shift it up to 20-63, so they
363 represent the actual powers of 2. */
364 return size_mask << 20;
365}
366
367static void pci_store_rebar_size(const struct device *dev,
368 const struct resource *resource)
369{
370 const unsigned int num_bits = __fls64(resource->size);
371 const uint32_t offset = get_rebar_offset(dev, resource->index);
372 if (!offset)
373 return;
374
375 pci_update_config32(dev, offset + PCI_REBAR_CTRL_OFFSET,
376 ~PCI_REBAR_CTRL_SIZE_MASK,
377 num_bits << PCI_REBAR_CTRL_SIZE_SHIFT);
378}
379
380static void configure_adjustable_base(const struct device *dev,
381 unsigned long index,
382 struct resource *res)
383{
384 /*
385 * Excerpt from an implementation note from the PCIe spec:
386 *
387 * System software uses this capability in place of the above mentioned
388 * method of determining the resource size[0], and prior to assigning
389 * the base address to the BAR. Potential usable resource sizes are
390 * reported by the Function via its Resizable BAR Capability and Control
391 * registers. It is intended that the software allocate the largest of
392 * the reported sizes that it can, since allocating less address space
393 * than the largest reported size can result in lower
394 * performance. Software then writes the size to the Resizable BAR
395 * Control register for the appropriate BAR for the Function. Following
396 * this, the base address is written to the BAR.
397 *
398 * [0] Referring to using the moving bits in the BAR to determine the
399 * requested size of the MMIO region
400 */
401 const uint64_t size_mask = get_rebar_sizes_mask(dev, index);
402 if (!size_mask)
403 return;
404
405 int max_requested_bits = __fls64(size_mask);
406 if (max_requested_bits > CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS) {
407 printk(BIOS_WARNING, "WARNING: Device %s requests a BAR with"
408 "%u bits of address space, which coreboot is not"
409 "configured to hand out, truncating to %u bits\n",
410 dev_path(dev), max_requested_bits,
411 CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS);
412 max_requested_bits = CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS;
413 }
414
415 if (!(res->flags & IORESOURCE_PCI64) && max_requested_bits > 32) {
416 printk(BIOS_ERR, "ERROR: Resizable BAR requested"
417 "above 32 bits, but PCI function reported a"
418 "32-bit BAR.");
419 return;
420 }
421
422 /* Configure the resource parameters for the adjustable BAR */
423 res->size = 1ULL << max_requested_bits;
424 res->align = max_requested_bits;
425 res->gran = max_requested_bits;
426 res->limit = (res->flags & IORESOURCE_PCI64) ? UINT64_MAX : UINT32_MAX;
Tim Wawrzynczak2b83fa72022-05-27 12:27:50 -0600427 res->flags |= (res->flags & IORESOURCE_PCI64) ?
428 IORESOURCE_PCIE_RESIZABLE_BAR | IORESOURCE_ABOVE_4G :
429 IORESOURCE_PCIE_RESIZABLE_BAR;
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700430
431 printk(BIOS_INFO, "%s: Adjusting resource index %lu: base: %llx size: %llx "
432 "align: %d gran: %d limit: %llx\n",
433 dev_path(dev), res->index, res->base, res->size,
434 res->align, res->gran, res->limit);
435}
436
Patrick Rudolph4e2f95b2018-05-16 14:56:22 +0200437/**
Myles Watson29cc9ed2009-07-02 18:56:24 +0000438 * Read the base address registers for a given device.
439 *
440 * @param dev Pointer to the dev structure.
441 * @param howmany How many registers to read (6 for device, 2 for bridge).
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000442 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000443static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000444{
445 unsigned long index;
446
Myles Watson29cc9ed2009-07-02 18:56:24 +0000447 for (index = PCI_BASE_ADDRESS_0;
448 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000449 struct resource *resource;
450 resource = pci_get_resource(dev, index);
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700451
452 const bool is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE) != 0;
453 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS) && is_pcie)
454 configure_adjustable_base(dev, index, resource);
455
Myles Watson29cc9ed2009-07-02 18:56:24 +0000456 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000457 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000458
459 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000460}
461
Myles Watson29cc9ed2009-07-02 18:56:24 +0000462static void pci_record_bridge_resource(struct device *dev, resource_t moving,
Martin Roth38ddbfb2019-10-23 21:41:00 -0600463 unsigned int index, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000464{
Eric Biederman03acab62004-10-14 21:25:53 +0000465 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000466 unsigned long gran;
467 resource_t step;
468
Myles Watson29cc9ed2009-07-02 18:56:24 +0000469 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000470
471 if (!moving)
472 return;
473
474 /* Initialize the constraints on the current bus. */
475 resource = new_resource(dev, index);
476 resource->size = 0;
477 gran = 0;
478 step = 1;
479 while ((moving & step) == 0) {
480 gran += 1;
481 step <<= 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000482 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000483 resource->gran = gran;
484 resource->align = gran;
485 resource->limit = moving | (step - 1);
486 resource->flags = type | IORESOURCE_PCI_BRIDGE |
487 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000488}
489
Eric Biederman8ca8d762003-04-22 19:02:15 +0000490static void pci_bridge_read_bases(struct device *dev)
491{
Eric Biederman03acab62004-10-14 21:25:53 +0000492 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000493
Myles Watson29cc9ed2009-07-02 18:56:24 +0000494 /* See if the bridge I/O resources are implemented. */
495 moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
496 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000497 ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000498
Myles Watson29cc9ed2009-07-02 18:56:24 +0000499 moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
500 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000501 ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000502
503 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000504
Myles Watson29cc9ed2009-07-02 18:56:24 +0000505 /* Initialize the I/O space constraints on the current bus. */
506 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000507
Myles Watson29cc9ed2009-07-02 18:56:24 +0000508 /* See if the bridge prefmem resources are implemented. */
509 moving_base =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000510 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000511 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000512 ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000513
Myles Watson29cc9ed2009-07-02 18:56:24 +0000514 moving_limit =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000515 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000516 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000517 ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
Myles Watson032a9652009-05-11 22:24:53 +0000518
Eric Biederman03acab62004-10-14 21:25:53 +0000519 moving = moving_base & moving_limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000520 /* Initialize the prefetchable memory constraints on the current bus. */
521 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
522 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Myles Watson032a9652009-05-11 22:24:53 +0000523
Myles Watson29cc9ed2009-07-02 18:56:24 +0000524 /* See if the bridge mem resources are implemented. */
525 moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
526 moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000527
528 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000529
Myles Watson29cc9ed2009-07-02 18:56:24 +0000530 /* Initialize the memory resources on the current bus. */
531 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
532 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000533
Eric Biederman5cd81732004-03-11 15:01:31 +0000534 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000535}
536
Eric Biederman5899fd82003-04-24 06:25:08 +0000537void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000538{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000539 pci_read_bases(dev, 6);
540 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000541}
542
Eric Biederman5899fd82003-04-24 06:25:08 +0000543void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000544{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000545 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000546 pci_read_bases(dev, 2);
547 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000548}
549
Myles Watson29cc9ed2009-07-02 18:56:24 +0000550void pci_domain_read_resources(struct device *dev)
551{
552 struct resource *res;
553
554 /* Initialize the system-wide I/O space constraints. */
555 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
556 res->limit = 0xffffUL;
557 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
558 IORESOURCE_ASSIGNED;
559
560 /* Initialize the system-wide memory resources constraints. */
561 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Furquan Shaikh871baf22020-03-12 17:51:24 -0700562 res->limit = (1ULL << cpu_phys_address_size()) - 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000563 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
564 IORESOURCE_ASSIGNED;
565}
566
Raul E Rangel5cb34e22020-05-04 16:41:22 -0600567void pci_domain_set_resources(struct device *dev)
568{
569 assign_resources(dev->link_list);
570}
571
Nico Huber730b2612020-05-20 00:32:50 +0200572static void pci_store_resource(const struct device *const dev,
573 const struct resource *const resource)
574{
575 unsigned long base_lo, base_hi;
576
577 base_lo = resource->base & 0xffffffff;
578 base_hi = (resource->base >> 32) & 0xffffffff;
579
580 /*
581 * Some chipsets allow us to set/clear the I/O bit
582 * (e.g. VIA 82C686A). So set it to be safe.
583 */
584 if (resource->flags & IORESOURCE_IO)
585 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
586
587 pci_write_config32(dev, resource->index, base_lo);
588 if (resource->flags & IORESOURCE_PCI64)
589 pci_write_config32(dev, resource->index + 4, base_hi);
590}
591
592static void pci_store_bridge_resource(const struct device *const dev,
593 struct resource *const resource)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000594{
Eric Biederman03acab62004-10-14 21:25:53 +0000595 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000596
Nico Huber730b2612020-05-20 00:32:50 +0200597 /*
598 * PCI bridges have no enable bit. They are disabled if the base of
599 * the range is greater than the limit. If the size is zero, disable
600 * by setting the base = limit and end = limit - 2^gran.
601 */
602 if (resource->size == 0) {
603 base = resource->limit;
604 end = resource->limit - (1 << resource->gran);
605 resource->base = base;
606 } else {
607 base = resource->base;
608 end = resource_end(resource);
609 }
610
611 if (resource->index == PCI_IO_BASE) {
612 /* Set the I/O ranges. */
613 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
614 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
615 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
616 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
617 } else if (resource->index == PCI_MEMORY_BASE) {
618 /* Set the memory range. */
619 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
620 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
621 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
622 /* Set the prefetchable memory range. */
623 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
624 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
625 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
626 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
627 } else {
628 /* Don't let me think I stored the resource. */
629 resource->flags &= ~IORESOURCE_STORED;
Julius Wernere9665952022-01-21 17:06:20 -0800630 printk(BIOS_ERR, "invalid resource->index %lx\n", resource->index);
Nico Huber730b2612020-05-20 00:32:50 +0200631 }
632}
633
634static void pci_set_resource(struct device *dev, struct resource *resource)
635{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000636 /* Make certain the resource has actually been assigned a value. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000637 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Nico Huberf5312442020-05-20 01:02:18 +0200638 if (resource->flags & IORESOURCE_BRIDGE) {
639 /* If a bridge resource has no value assigned,
640 we can treat it like an empty resource. */
641 resource->size = 0;
642 } else {
Julius Wernere9665952022-01-21 17:06:20 -0800643 printk(BIOS_ERR, "%s %02lx %s size: 0x%010llx not assigned\n",
Angel Ponsd19cc112021-07-04 11:41:31 +0200644 dev_path(dev), resource->index,
Nico Huberf5312442020-05-20 01:02:18 +0200645 resource_type(resource), resource->size);
646 return;
647 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000648 }
649
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000650 /* If this resource is fixed don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000651 if (resource->flags & IORESOURCE_FIXED)
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000652 return;
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000653
Myles Watson29cc9ed2009-07-02 18:56:24 +0000654 /* If I have already stored this resource don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000655 if (resource->flags & IORESOURCE_STORED)
Eric Biederman5cd81732004-03-11 15:01:31 +0000656 return;
Eric Biederman5cd81732004-03-11 15:01:31 +0000657
Myles Watson29cc9ed2009-07-02 18:56:24 +0000658 /* If the resource is subtractive don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000659 if (resource->flags & IORESOURCE_SUBTRACTIVE)
Eric Biederman03acab62004-10-14 21:25:53 +0000660 return;
Eric Biederman03acab62004-10-14 21:25:53 +0000661
Myles Watson29cc9ed2009-07-02 18:56:24 +0000662 /* Only handle PCI memory and I/O resources for now. */
663 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000664 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000665
Myles Watson29cc9ed2009-07-02 18:56:24 +0000666 /* Enable the resources in the command register. */
Eric Biederman03acab62004-10-14 21:25:53 +0000667 if (resource->size) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000668 if (resource->flags & IORESOURCE_MEM)
Eric Biederman03acab62004-10-14 21:25:53 +0000669 dev->command |= PCI_COMMAND_MEMORY;
Uwe Hermanne4870472010-11-04 23:23:47 +0000670 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000671 dev->command |= PCI_COMMAND_IO;
Felix Singer205b53e2020-09-07 15:21:21 +0200672 if (resource->flags & IORESOURCE_PCI_BRIDGE &&
673 CONFIG(PCI_SET_BUS_MASTER_PCI_BRIDGES))
Eric Biederman03acab62004-10-14 21:25:53 +0000674 dev->command |= PCI_COMMAND_MASTER;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000675 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000676
Myles Watson29cc9ed2009-07-02 18:56:24 +0000677 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000678 resource->flags |= IORESOURCE_STORED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000679
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700680 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
681 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS) &&
682 (resource->flags & IORESOURCE_PCIE_RESIZABLE_BAR))
683 pci_store_rebar_size(dev, resource);
684
Nico Huber730b2612020-05-20 00:32:50 +0200685 pci_store_resource(dev, resource);
Uwe Hermanne4870472010-11-04 23:23:47 +0000686
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700687 } else {
688 pci_store_bridge_resource(dev, resource);
689 }
690
Eric Biederman03acab62004-10-14 21:25:53 +0000691 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000692}
693
Eric Biederman5899fd82003-04-24 06:25:08 +0000694void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000695{
Myles Watsonc25cc112010-05-21 14:33:48 +0000696 struct resource *res;
Myles Watson894a3472010-06-09 22:41:35 +0000697 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000698 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000699
Uwe Hermanne4870472010-11-04 23:23:47 +0000700 for (res = dev->resource_list; res; res = res->next)
Myles Watsonc25cc112010-05-21 14:33:48 +0000701 pci_set_resource(dev, res);
Uwe Hermanne4870472010-11-04 23:23:47 +0000702
Myles Watson894a3472010-06-09 22:41:35 +0000703 for (bus = dev->link_list; bus; bus = bus->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000704 if (bus->children)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000705 assign_resources(bus);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000706 }
707
Myles Watson29cc9ed2009-07-02 18:56:24 +0000708 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000709 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000710
Myles Watson29cc9ed2009-07-02 18:56:24 +0000711 /* Set a default secondary latency timer. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000712 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000713 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000714
Myles Watson29cc9ed2009-07-02 18:56:24 +0000715 /* Zero the IRQ settings. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000716 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Uwe Hermanne4870472010-11-04 23:23:47 +0000717 if (line)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000718 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Uwe Hermanne4870472010-11-04 23:23:47 +0000719
Myles Watson29cc9ed2009-07-02 18:56:24 +0000720 /* Set the cache line size, so far 64 bytes is good for everyone. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000721 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000722}
723
Eric Biedermane9a271e32003-09-02 03:36:25 +0000724void pci_dev_enable_resources(struct device *dev)
725{
Kyösti Mälkkicac02312019-06-30 08:40:04 +0300726 const struct pci_operations *ops = NULL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000727 u16 command;
Eric Biederman03acab62004-10-14 21:25:53 +0000728
Uwe Hermanne4870472010-11-04 23:23:47 +0000729 /* Set the subsystem vendor and device ID for mainboard devices. */
Kyösti Mälkkicac02312019-06-30 08:40:04 +0300730 if (dev->ops)
731 ops = dev->ops->ops_pci;
Eric Biedermandbec2d42004-10-21 10:44:08 +0000732 if (dev->on_mainboard && ops && ops->set_subsystem) {
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700733 if (CONFIG_SUBSYSTEM_VENDOR_ID)
734 dev->subsystem_vendor = CONFIG_SUBSYSTEM_VENDOR_ID;
Rizwan Qureshifd891292017-04-26 21:00:37 +0530735 else if (!dev->subsystem_vendor)
736 dev->subsystem_vendor = pci_read_config16(dev,
737 PCI_VENDOR_ID);
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700738 if (CONFIG_SUBSYSTEM_DEVICE_ID)
739 dev->subsystem_device = CONFIG_SUBSYSTEM_DEVICE_ID;
Rizwan Qureshifd891292017-04-26 21:00:37 +0530740 else if (!dev->subsystem_device)
741 dev->subsystem_device = pci_read_config16(dev,
742 PCI_DEVICE_ID);
743
Sven Schnelle91321022011-03-01 19:58:47 +0000744 printk(BIOS_DEBUG, "%s subsystem <- %04x/%04x\n",
745 dev_path(dev), dev->subsystem_vendor,
746 dev->subsystem_device);
747 ops->set_subsystem(dev, dev->subsystem_vendor,
748 dev->subsystem_device);
Eric Biederman03acab62004-10-14 21:25:53 +0000749 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000750 command = pci_read_config16(dev, PCI_COMMAND);
751 command |= dev->command;
Uwe Hermanne4870472010-11-04 23:23:47 +0000752
Myles Watson29cc9ed2009-07-02 18:56:24 +0000753 /* v3 has
754 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
755 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000756
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000757 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000758 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000759}
760
761void pci_bus_enable_resources(struct device *dev)
762{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000763 u16 ctrl;
764
Uwe Hermanne4870472010-11-04 23:23:47 +0000765 /*
766 * Enable I/O in command register if there is VGA card
Myles Watson29cc9ed2009-07-02 18:56:24 +0000767 * connected with (even it does not claim I/O resource).
768 */
Myles Watson894a3472010-06-09 22:41:35 +0000769 if (dev->link_list->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000770 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000771 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000772 ctrl |= dev->link_list->bridge_ctrl;
Kyösti Mälkki382e2162019-09-21 16:19:32 +0300773 ctrl |= (PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR); /* Error check. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000774 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000775 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
776
777 pci_dev_enable_resources(dev);
778}
779
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000780void pci_bus_reset(struct bus *bus)
781{
Uwe Hermanne4870472010-11-04 23:23:47 +0000782 u16 ctl;
783
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000784 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
785 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
786 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
787 mdelay(10);
Uwe Hermanne4870472010-11-04 23:23:47 +0000788
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000789 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
790 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
791 delay(1);
792}
793
Elyes HAOUAS88030b72018-09-20 17:26:10 +0200794void pci_dev_set_subsystem(struct device *dev, unsigned int vendor,
795 unsigned int device)
Eric Biederman03acab62004-10-14 21:25:53 +0000796{
Subrata Banik9514d472019-03-20 14:56:27 +0530797 uint8_t offset;
798
799 /* Header type */
800 switch (dev->hdr_type & 0x7f) {
801 case PCI_HEADER_TYPE_NORMAL:
802 offset = PCI_SUBSYSTEM_VENDOR_ID;
803 break;
804 case PCI_HEADER_TYPE_BRIDGE:
805 offset = pci_find_capability(dev, PCI_CAP_ID_SSVID);
806 if (!offset)
807 return;
808 offset += 4; /* Vendor ID at offset 4 */
809 break;
810 case PCI_HEADER_TYPE_CARDBUS:
811 offset = PCI_CB_SUBSYSTEM_VENDOR_ID;
812 break;
813 default:
814 return;
815 }
816
Subrata Banik4a0f0712019-03-20 14:29:47 +0530817 if (!vendor || !device) {
Subrata Banik9514d472019-03-20 14:56:27 +0530818 pci_write_config32(dev, offset,
Subrata Banik4a0f0712019-03-20 14:29:47 +0530819 pci_read_config32(dev, PCI_VENDOR_ID));
820 } else {
Subrata Banik9514d472019-03-20 14:56:27 +0530821 pci_write_config32(dev, offset,
Subrata Banik4a0f0712019-03-20 14:29:47 +0530822 ((device & 0xffff) << 16) | (vendor & 0xffff));
823 }
Eric Biederman03acab62004-10-14 21:25:53 +0000824}
825
Frans Hendriksb71181a2019-10-04 14:06:33 +0200826static int should_run_oprom(struct device *dev, struct rom_header *rom)
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300827{
828 static int should_run = -1;
829
Frans Hendriksb71181a2019-10-04 14:06:33 +0200830 if (CONFIG(VENDORCODE_ELTAN_VBOOT))
831 if (rom != NULL)
832 if (!verified_boot_should_run_oprom(rom))
833 return 0;
834
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300835 if (should_run >= 0)
836 return should_run;
837
Julius Wernercd49cce2019-03-05 16:53:33 -0800838 if (CONFIG(ALWAYS_RUN_OPROM)) {
Aaron Durbin10510252018-01-30 10:04:02 -0700839 should_run = 1;
840 return should_run;
841 }
842
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200843 /* Don't run VGA option ROMs, unless we have to print
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300844 * something on the screen before the kernel is loaded.
845 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700846 should_run = display_init_required();
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300847
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200848 if (!should_run)
849 printk(BIOS_DEBUG, "Not running VGA Option ROM\n");
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300850 return should_run;
851}
852
853static int should_load_oprom(struct device *dev)
854{
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300855 /* If S3_VGA_ROM_RUN is disabled, skip running VGA option
856 * ROMs when coming out of an S3 resume.
857 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800858 if (!CONFIG(S3_VGA_ROM_RUN) && acpi_is_wakeup_s3() &&
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300859 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
860 return 0;
Julius Wernercd49cce2019-03-05 16:53:33 -0800861 if (CONFIG(ALWAYS_LOAD_OPROM))
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300862 return 1;
Frans Hendriksb71181a2019-10-04 14:06:33 +0200863 if (should_run_oprom(dev, NULL))
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300864 return 1;
865
866 return 0;
867}
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300868
Kyösti Mälkki0f300632020-12-19 23:43:56 +0200869static void oprom_pre_graphics_stall(void)
870{
Paul Menzelc4062c72021-02-11 10:43:14 +0100871 if (CONFIG_PRE_GRAPHICS_DELAY_MS)
872 mdelay(CONFIG_PRE_GRAPHICS_DELAY_MS);
Kyösti Mälkki0f300632020-12-19 23:43:56 +0200873}
874
Uwe Hermanne4870472010-11-04 23:23:47 +0000875/** Default handler: only runs the relevant PCI BIOS. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000876void pci_dev_init(struct device *dev)
877{
878 struct rom_header *rom, *ram;
879
Julius Wernercd49cce2019-03-05 16:53:33 -0800880 if (!CONFIG(VGA_ROM_RUN))
Aaron Durbinfbed9a52018-01-30 09:58:51 -0700881 return;
882
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100883 /* Only execute VGA ROMs. */
884 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
Myles Watson17aeeca2009-10-07 18:41:08 +0000885 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000886
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300887 if (!should_load_oprom(dev))
Stefan Reinauer74a0efe2012-03-30 17:10:49 -0700888 return;
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700889 timestamp_add_now(TS_OPROM_INITIALIZE);
Aaron Durbince872cb2013-03-28 15:59:19 -0500890
891 rom = pci_rom_probe(dev);
892 if (rom == NULL)
893 return;
894
895 ram = pci_rom_load(dev, rom);
896 if (ram == NULL)
897 return;
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700898 timestamp_add_now(TS_OPROM_COPY_END);
Aaron Durbince872cb2013-03-28 15:59:19 -0500899
Frans Hendriksb71181a2019-10-04 14:06:33 +0200900 if (!should_run_oprom(dev, rom))
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300901 return;
902
Kyösti Mälkki0f300632020-12-19 23:43:56 +0200903 /* Wait for any configured pre-graphics delay */
904 oprom_pre_graphics_stall();
905
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000906 run_bios(dev, (unsigned long)ram);
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +0200907
Kyösti Mälkkiab56b3b2013-11-28 16:44:51 +0200908 gfx_set_init_done(1);
909 printk(BIOS_DEBUG, "VGA Option ROM was run\n");
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700910 timestamp_add_now(TS_OPROM_END);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000911}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000912
Li-Ta Loe5266692004-03-23 21:28:05 +0000913/** Default device operation for PCI devices */
Subrata Banikffc790b2017-12-11 10:29:49 +0530914struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000915 .set_subsystem = pci_dev_set_subsystem,
916};
917
Eric Biederman8ca8d762003-04-22 19:02:15 +0000918struct device_operations default_pci_ops_dev = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000919 .read_resources = pci_dev_read_resources,
920 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000921 .enable_resources = pci_dev_enable_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -0800922#if CONFIG(HAVE_ACPI_TABLES)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200923 .write_acpi_tables = pci_rom_write_acpi_tables,
Nico Huber68680dd2020-03-31 17:34:52 +0200924 .acpi_fill_ssdt = pci_rom_ssdt,
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200925#endif
Uwe Hermanne4870472010-11-04 23:23:47 +0000926 .init = pci_dev_init,
Uwe Hermanne4870472010-11-04 23:23:47 +0000927 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000928};
Li-Ta Loe5266692004-03-23 21:28:05 +0000929
930/** Default device operations for PCI bridges */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000931struct device_operations default_pci_ops_bus = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000932 .read_resources = pci_bus_read_resources,
933 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000934 .enable_resources = pci_bus_enable_resources,
Uwe Hermanne4870472010-11-04 23:23:47 +0000935 .scan_bus = pci_scan_bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000936 .reset_bus = pci_bus_reset,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000937};
Li-Ta Loe5266692004-03-23 21:28:05 +0000938
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -0600939/** Default device operations for PCI devices marked 'hidden' */
940static struct device_operations default_hidden_pci_ops_dev = {
941 .read_resources = noop_read_resources,
942 .set_resources = noop_set_resources,
943 .scan_bus = scan_static_bus,
944};
945
Li-Ta Loe5266692004-03-23 21:28:05 +0000946/**
Nico Huber061b9052019-09-21 15:58:23 +0200947 * Check for compatibility to route legacy VGA cycles through a bridge.
948 *
949 * Originally, when decoding i/o ports for legacy VGA cycles, bridges
950 * should only consider the 10 least significant bits of the port address.
951 * This means all VGA registers were aliased every 1024 ports!
952 * e.g. 0x3b0 was also decoded as 0x7b0, 0xbb0 etc.
953 *
954 * To avoid this mess, a bridge control bit (VGA16) was introduced in
955 * 2003 to enable decoding of 16-bit port addresses. As we don't want
956 * to make this any more complex for now, we use this bit if possible
957 * and only warn if it's not supported (in set_vga_bridge_bits()).
958 */
959static void pci_bridge_vga_compat(struct bus *const bus)
960{
961 uint16_t bridge_ctrl;
962
963 bridge_ctrl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
964
965 /* Ensure VGA decoding is disabled during probing (it should
966 be by default, but we run blobs nowadays) */
967 bridge_ctrl &= ~PCI_BRIDGE_CTL_VGA;
968 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, bridge_ctrl);
969
970 /* If the upstream bridge doesn't support VGA16, we don't have to check */
971 bus->no_vga16 |= bus->dev->bus->no_vga16;
972 if (bus->no_vga16)
973 return;
974
975 /* Test if we can enable 16-bit decoding */
976 bridge_ctrl |= PCI_BRIDGE_CTL_VGA16;
977 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, bridge_ctrl);
978 bridge_ctrl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
979
980 bus->no_vga16 = !(bridge_ctrl & PCI_BRIDGE_CTL_VGA16);
981}
982
983/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000984 * Detect the type of downstream bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000985 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000986 * This function is a heuristic to detect which type of bus is downstream
987 * of a PCI-to-PCI bridge. This functions by looking for various capability
988 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
989 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +0000990 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000991 * When only a PCI-Express capability is found the type is examined to see
992 * which type of bridge we have.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000993 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000994 * @param dev Pointer to the device structure of the bridge.
995 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000996 */
Aaron Durbinc30d9132017-08-07 16:55:43 -0600997static struct device_operations *get_pci_bridge_ops(struct device *dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000998{
Julius Wernercd49cce2019-03-05 16:53:33 -0800999#if CONFIG(PCIX_PLUGIN_SUPPORT)
Ronald G. Minnich78a16672012-11-29 16:28:21 -08001000 unsigned int pcixpos;
1001 pcixpos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1002 if (pcixpos) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001003 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001004 return &default_pcix_ops_bus;
1005 }
1006#endif
Julius Wernercd49cce2019-03-05 16:53:33 -08001007#if CONFIG(PCIEXP_PLUGIN_SUPPORT)
Ronald G. Minnich78a16672012-11-29 16:28:21 -08001008 unsigned int pciexpos;
1009 pciexpos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
1010 if (pciexpos) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001011 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -08001012 flags = pci_read_config16(dev, pciexpos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001013 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001014 case PCI_EXP_TYPE_ROOT_PORT:
1015 case PCI_EXP_TYPE_UPSTREAM:
1016 case PCI_EXP_TYPE_DOWNSTREAM:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001017 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
Uwe Hermanne4870472010-11-04 23:23:47 +00001018 dev_path(dev));
Arthur Heymans24837e72021-03-11 20:34:05 +01001019 if (CONFIG(PCIEXP_HOTPLUG)) {
1020 u16 sltcap;
1021 sltcap = pci_read_config16(dev, pciexpos + PCI_EXP_SLTCAP);
1022 if (sltcap & PCI_EXP_SLTCAP_HPC) {
1023 printk(BIOS_DEBUG, "%s hot-plug capable\n",
1024 dev_path(dev));
1025 return &default_pciexp_hotplug_ops_bus;
1026 }
Jeremy Sollercf2ac542019-10-09 21:40:36 -06001027 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001028 return &default_pciexp_ops_bus;
1029 case PCI_EXP_TYPE_PCI_BRIDGE:
Uwe Hermanne4870472010-11-04 23:23:47 +00001030 printk(BIOS_DEBUG, "%s subordinate PCI\n",
1031 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001032 return &default_pci_ops_bus;
1033 default:
1034 break;
1035 }
1036 }
1037#endif
1038 return &default_pci_ops_bus;
1039}
1040
1041/**
Vadim Bendebury8049fc92012-04-24 12:53:19 -07001042 * Check if a device id matches a PCI driver entry.
1043 *
1044 * The driver entry can either point at a zero terminated array of acceptable
1045 * device IDs, or include a single device ID.
1046 *
Martin Roth98b698c2015-01-06 21:02:52 -07001047 * @param driver pointer to the PCI driver entry being checked
1048 * @param device_id PCI device ID of the device being matched
Vadim Bendebury8049fc92012-04-24 12:53:19 -07001049 */
1050static int device_id_match(struct pci_driver *driver, unsigned short device_id)
1051{
1052 if (driver->devices) {
1053 unsigned short check_id;
1054 const unsigned short *device_list = driver->devices;
1055 while ((check_id = *device_list++) != 0)
1056 if (check_id == device_id)
1057 return 1;
1058 }
1059
1060 return (driver->device == device_id);
1061}
1062
1063/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001064 * Set up PCI device operation.
1065 *
1066 * Check if it already has a driver. If not, use find_device_operations(),
1067 * or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +00001068 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001069 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +00001070 * @see pci_drivers
1071 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001072static void set_pci_ops(struct device *dev)
1073{
1074 struct pci_driver *driver;
Li-Ta Loe5266692004-03-23 21:28:05 +00001075
Uwe Hermanne4870472010-11-04 23:23:47 +00001076 if (dev->ops)
1077 return;
1078
1079 /*
1080 * Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +00001081 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001082 */
Aaron Durbin03758152015-09-03 17:23:08 -05001083 for (driver = &_pci_drivers[0]; driver != &_epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001084 if ((driver->vendor == dev->vendor) &&
Vadim Bendebury8049fc92012-04-24 12:53:19 -07001085 device_id_match(driver, dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +00001086 dev->ops = (struct device_operations *)driver->ops;
Nico Huber7e3e1ea2020-10-12 16:25:40 +02001087 break;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001088 }
1089 }
Li-Ta Loe5266692004-03-23 21:28:05 +00001090
Nico Huber7e3e1ea2020-10-12 16:25:40 +02001091 if (dev->ops) {
1092 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n", dev_path(dev),
1093 driver->vendor, driver->device, (driver->ops->scan_bus ? "bus " : ""));
1094 return;
1095 }
1096
Uwe Hermanne4870472010-11-04 23:23:47 +00001097 /* If I don't have a specific driver use the default operations. */
1098 switch (dev->hdr_type & 0x7f) { /* Header type */
1099 case PCI_HEADER_TYPE_NORMAL:
Eric Biederman8ca8d762003-04-22 19:02:15 +00001100 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
1101 goto bad;
1102 dev->ops = &default_pci_ops_dev;
1103 break;
1104 case PCI_HEADER_TYPE_BRIDGE:
1105 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1106 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001107 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001108 break;
Julius Wernercd49cce2019-03-05 16:53:33 -08001109#if CONFIG(CARDBUS_PLUGIN_SUPPORT)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001110 case PCI_HEADER_TYPE_CARDBUS:
1111 dev->ops = &default_cardbus_ops_bus;
1112 break;
1113#endif
Felix Singerc96ee7e2021-01-07 06:14:27 +00001114 default:
Uwe Hermanne4870472010-11-04 23:23:47 +00001115bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +00001116 if (dev->enabled) {
Angel Ponsd19cc112021-07-04 11:41:31 +02001117 printk(BIOS_ERR,
1118 "%s [%04x/%04x/%06x] has unknown header type %02x, ignoring.\n",
1119 dev_path(dev), dev->vendor, dev->device,
Uwe Hermanne4870472010-11-04 23:23:47 +00001120 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +00001121 }
Eric Biederman8ca8d762003-04-22 19:02:15 +00001122 }
Eric Biederman8ca8d762003-04-22 19:02:15 +00001123}
1124
1125/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001126 * See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +00001127 *
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001128 * Given a PCI bus structure and a devfn number, find the device structure
1129 * corresponding to the devfn, if present. Then move the device structure
1130 * as the last child on the bus.
Li-Ta Loe5266692004-03-23 21:28:05 +00001131 *
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001132 * @param bus Pointer to the bus structure.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001133 * @param devfn A device/function number.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001134 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +00001135 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001136 */
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001137static struct device *pci_scan_get_dev(struct bus *bus, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001138{
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001139 struct device *dev, **prev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001140
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001141 prev = &bus->children;
1142 for (dev = bus->children; dev; dev = dev->sibling) {
Duncan Lauriebf696222020-10-18 15:10:00 -07001143 if (dev->path.type == DEVICE_PATH_PCI && dev->path.pci.devfn == devfn) {
1144 /* Unlink from the list. */
1145 *prev = dev->sibling;
1146 dev->sibling = NULL;
1147 break;
Eric Biedermanad1b35a2003-10-14 02:36:51 +00001148 }
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001149 prev = &dev->sibling;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001150 }
Myles Watson29cc9ed2009-07-02 18:56:24 +00001151
Uwe Hermanne4870472010-11-04 23:23:47 +00001152 /*
1153 * Just like alloc_dev() add the device to the list of devices on the
Myles Watson29cc9ed2009-07-02 18:56:24 +00001154 * bus. When the list of devices was formed we removed all of the
1155 * parents children, and now we are interleaving static and dynamic
1156 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001157 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001158 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001159 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +00001160
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001161 /* Find the last child on the bus. */
1162 for (child = bus->children; child && child->sibling;)
Eric Biedermane9a271e32003-09-02 03:36:25 +00001163 child = child->sibling;
Uwe Hermanne4870472010-11-04 23:23:47 +00001164
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001165 /* Place the device as last on the bus. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001166 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +00001167 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001168 else
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001169 bus->children = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001170 }
1171
Eric Biederman8ca8d762003-04-22 19:02:15 +00001172 return dev;
1173}
1174
Myles Watson032a9652009-05-11 22:24:53 +00001175/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001176 * Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +00001177 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001178 * Determine the existence of a given PCI device. Allocate a new struct device
1179 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001180 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001181 * @param dev Pointer to the dev structure.
1182 * @param bus Pointer to the bus structure.
1183 * @param devfn A device/function number to look at.
1184 * @return The device structure for the device (if found), NULL otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001185 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001186struct device *pci_probe_dev(struct device *dev, struct bus *bus,
1187 unsigned int devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001188{
Myles Watson29cc9ed2009-07-02 18:56:24 +00001189 u32 id, class;
1190 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001191
Myles Watson29cc9ed2009-07-02 18:56:24 +00001192 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001193 if (!dev) {
1194 struct device dummy;
Uwe Hermanne4870472010-11-04 23:23:47 +00001195
Myles Watson29cc9ed2009-07-02 18:56:24 +00001196 dummy.bus = bus;
1197 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +00001198 dummy.path.pci.devfn = devfn;
Uwe Hermanne4870472010-11-04 23:23:47 +00001199
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001200 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +00001201 /*
1202 * Have we found something? Some broken boards return 0 if a
1203 * slot is empty, but the expected answer is 0xffffffff.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001204 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001205 if (id == 0xffffffff)
Stefan Reinauer7355c752010-04-02 16:30:25 +00001206 return NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +00001207
Stefan Reinauer7355c752010-04-02 16:30:25 +00001208 if ((id == 0x00000000) || (id == 0x0000ffff) ||
1209 (id == 0xffff0000)) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001210 printk(BIOS_SPEW, "%s, bad id 0x%x\n",
1211 dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001212 return NULL;
1213 }
1214 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001215 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +00001216 /*
1217 * Enable/disable the device. Once we have found the device-
Myles Watson29cc9ed2009-07-02 18:56:24 +00001218 * specific operations this operations we will disable the
1219 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +00001220 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001221 * This is geared toward devices that have subfunctions
1222 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +00001223 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001224 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +00001225 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001226 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001227 /* Run the magic enable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001228 if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001229 dev->chip_ops->enable_dev(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +00001230
Myles Watson29cc9ed2009-07-02 18:56:24 +00001231 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001232 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +00001233
Uwe Hermanne4870472010-11-04 23:23:47 +00001234 /*
1235 * If the device does not have a PCI ID disable it. Possibly
Myles Watson29cc9ed2009-07-02 18:56:24 +00001236 * this is because we have already disabled the device. But
1237 * this also handles optional devices that may not always
1238 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001239 */
1240 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001241 if ((id == 0xffffffff) || (id == 0x00000000) ||
1242 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001243 if (dev->enabled) {
Angel Ponsd19cc112021-07-04 11:41:31 +02001244 printk(BIOS_INFO,
1245 "PCI: Static device %s not found, disabling it.\n",
1246 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001247 dev->enabled = 0;
1248 }
1249 return dev;
1250 }
1251 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001252
Myles Watson29cc9ed2009-07-02 18:56:24 +00001253 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001254 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1255 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +00001256
Myles Watson29cc9ed2009-07-02 18:56:24 +00001257 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001258 dev->vendor = id & 0xffff;
1259 dev->device = (id >> 16) & 0xffff;
1260 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001261
1262 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001263 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +00001264
Myles Watson29cc9ed2009-07-02 18:56:24 +00001265 /* Architectural/System devices always need to be bus masters. */
Felix Singerd3d0fd72020-09-07 16:15:14 +02001266 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM &&
1267 CONFIG(PCI_ALLOW_BUS_MASTER_ANY_DEVICE))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001268 dev->command |= PCI_COMMAND_MASTER;
Uwe Hermanne4870472010-11-04 23:23:47 +00001269
1270 /*
1271 * Look at the vendor and device ID, or at least the header type and
Myles Watson29cc9ed2009-07-02 18:56:24 +00001272 * class and figure out which set of configuration methods to use.
1273 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001274 */
1275 set_pci_ops(dev);
1276
Myles Watson29cc9ed2009-07-02 18:56:24 +00001277 /* Now run the magic enable/disable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001278 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001279 dev->ops->enable(dev);
Myles Watson032a9652009-05-11 22:24:53 +00001280
Myles Watson29cc9ed2009-07-02 18:56:24 +00001281 /* Display the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001282 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n", dev_path(dev),
1283 dev->vendor, dev->device, dev->enabled ? "enabled" : "disabled",
1284 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001285
1286 return dev;
1287}
1288
Myles Watson032a9652009-05-11 22:24:53 +00001289/**
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001290 * Test for match between romstage and ramstage device instance.
1291 *
1292 * @param dev Pointer to the device structure.
1293 * @param sdev Simple device model identifier, created with PCI_DEV().
1294 * @return Non-zero if bus:dev.fn of device matches.
1295 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001296unsigned int pci_match_simple_dev(struct device *dev, pci_devfn_t sdev)
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001297{
1298 return dev->bus->secondary == PCI_DEV2SEGBUS(sdev) &&
1299 dev->path.pci.devfn == PCI_DEV2DEVFN(sdev);
1300}
1301
1302/**
Bill XIE513d3592022-08-02 22:55:51 +08001303 * Test whether a capability is available along the whole path from the given
1304 * device to the host bridge.
1305 *
1306 * @param dev Pointer to the device structure.
1307 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
1308 * @return The next matching capability of the given device, if it is available
1309 * along the whole path, or zero if not.
1310 */
1311uint16_t pci_find_cap_recursive(const struct device *dev, uint16_t cap)
1312{
1313 assert(dev->bus);
1314 uint16_t pos = pci_find_capability(dev, cap);
1315 const struct device *bridge = dev->bus->dev;
1316 while (bridge && (bridge->path.type == DEVICE_PATH_PCI)) {
1317 assert(bridge->bus);
1318 if (!pci_find_capability(bridge, cap))
1319 return 0;
1320 bridge = bridge->bus->dev;
1321 }
1322 return pos;
1323}
1324
1325/**
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -06001326 * PCI devices that are marked as "hidden" do not get probed. However, the same
1327 * initialization logic is still performed as if it were. This is useful when
1328 * devices would like to be described in the devicetree.cb file, and/or present
1329 * static PCI resources to the allocator, but the platform firmware hides the
1330 * device (makes the device invisible to PCI enumeration) before PCI enumeration
1331 * takes place.
1332 *
1333 * The expected semantics of PCI devices marked as 'hidden':
1334 * 1) The device is actually present under the specified BDF
1335 * 2) The device config space can still be accessed somehow, but the Vendor ID
1336 * indicates there is no device there (it reads as 0xffffffff).
1337 * 3) The device may still consume PCI resources. Typically, these would have
1338 * been hardcoded elsewhere.
1339 *
1340 * @param dev Pointer to the device structure.
1341 */
1342static void pci_scan_hidden_device(struct device *dev)
1343{
1344 if (dev->chip_ops && dev->chip_ops->enable_dev)
1345 dev->chip_ops->enable_dev(dev);
1346
1347 /*
1348 * If chip_ops->enable_dev did not set dev->ops, then set to a default
1349 * .ops, because PCI enumeration is effectively being skipped, therefore
1350 * no PCI driver will bind to this device. However, children may want to
1351 * be enumerated, so this provides scan_static_bus for the .scan_bus
1352 * callback.
1353 */
1354 if (dev->ops == NULL)
1355 dev->ops = &default_hidden_pci_ops_dev;
1356
1357 if (dev->ops->enable)
1358 dev->ops->enable(dev);
1359
1360 /* Display the device almost as if it were probed normally */
1361 printk(BIOS_DEBUG, "%s [0000/%04x] hidden%s\n", dev_path(dev),
1362 dev->device, dev->ops ? "" : " No operations");
1363}
1364
1365/**
Jianjun Wang777ffff2021-07-24 14:50:36 +08001366 * A PCIe Downstream Port normally leads to a Link with only Device 0 on it
1367 * (PCIe spec r5.0, sec 7.3.1). As an optimization, scan only for Device 0 in
1368 * that situation.
1369 *
1370 * @param bus Pointer to the bus structure.
1371 */
1372static bool pci_bus_only_one_child(struct bus *bus)
1373{
1374 struct device *bridge = bus->dev;
1375 u16 pcie_pos, pcie_flags_reg;
1376 int pcie_type;
1377
Arthur Heymansdb199cc2022-01-06 20:56:01 +01001378 if (!bridge)
1379 return false;
1380
Nico Huberf514b8a2022-02-25 14:25:57 +01001381 if (bridge->path.type != DEVICE_PATH_PCI)
1382 return false;
1383
Jianjun Wang777ffff2021-07-24 14:50:36 +08001384 pcie_pos = pci_find_capability(bridge, PCI_CAP_ID_PCIE);
1385 if (!pcie_pos)
1386 return false;
1387
1388 pcie_flags_reg = pci_read_config16(bridge, pcie_pos + PCI_EXP_FLAGS);
1389
1390 pcie_type = (pcie_flags_reg & PCI_EXP_FLAGS_TYPE) >> 4;
1391
1392 return pciexp_is_downstream_port(pcie_type);
1393}
1394
1395/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001396 * Scan a PCI bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001397 *
Li-Ta Loe5266692004-03-23 21:28:05 +00001398 * Determine the existence of devices and bridges on a PCI bus. If there are
1399 * bridges on the bus, recursively scan the buses behind the bridges.
1400 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001401 * @param bus Pointer to the bus structure.
1402 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1403 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001404 */
Martin Roth38ddbfb2019-10-23 21:41:00 -06001405void pci_scan_bus(struct bus *bus, unsigned int min_devfn,
1406 unsigned int max_devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001407{
1408 unsigned int devfn;
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001409 struct device *dev, **prev;
1410 int once = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001411
Elyes HAOUASf984aec2021-01-16 17:29:17 +01001412 printk(BIOS_DEBUG, "PCI: %s for bus %02x\n", __func__, bus->secondary);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001413
Uwe Hermanne4870472010-11-04 23:23:47 +00001414 /* Maximum sane devfn is 0xFF. */
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001415 if (max_devfn > 0xff) {
Elyes HAOUASf984aec2021-01-16 17:29:17 +01001416 printk(BIOS_ERR, "PCI: %s limits devfn %x - devfn %x\n",
1417 __func__, min_devfn, max_devfn);
1418 printk(BIOS_ERR, "PCI: %s upper limit too big. Using 0xff.\n", __func__);
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001419 max_devfn=0xff;
1420 }
1421
Eric Biederman8ca8d762003-04-22 19:02:15 +00001422 post_code(0x24);
Uwe Hermanne4870472010-11-04 23:23:47 +00001423
Jianjun Wang777ffff2021-07-24 14:50:36 +08001424 if (pci_bus_only_one_child(bus))
1425 max_devfn = MIN(max_devfn, 0x07);
1426
Uwe Hermanne4870472010-11-04 23:23:47 +00001427 /*
1428 * Probe all devices/functions on this bus with some optimization for
Myles Watson29cc9ed2009-07-02 18:56:24 +00001429 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001430 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001431 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +00001432 if (CONFIG(MINIMAL_PCI_SCANNING)) {
1433 dev = pcidev_path_behind(bus, devfn);
1434 if (!dev || !dev->mandatory)
1435 continue;
1436 }
1437
Uwe Hermanne4870472010-11-04 23:23:47 +00001438 /* First thing setup the device structure. */
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001439 dev = pci_scan_get_dev(bus, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001440
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -06001441 /* Devices marked 'hidden' do not get probed */
1442 if (dev && dev->hidden) {
1443 pci_scan_hidden_device(dev);
1444
1445 /* Skip pci_probe_dev, go to next devfn */
1446 continue;
1447 }
1448
Myles Watson29cc9ed2009-07-02 18:56:24 +00001449 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001450 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001451
Uwe Hermanne4870472010-11-04 23:23:47 +00001452 /*
1453 * If this is not a multi function device, or the device is
Myles Watson29cc9ed2009-07-02 18:56:24 +00001454 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001455 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001456 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001457 if ((PCI_FUNC(devfn) == 0x00) && (!dev
Myles Watson29cc9ed2009-07-02 18:56:24 +00001458 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001459 devfn += 0x07;
1460 }
1461 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001462
Eric Biederman8ca8d762003-04-22 19:02:15 +00001463 post_code(0x25);
1464
Uwe Hermanne4870472010-11-04 23:23:47 +00001465 /*
Elyes HAOUAS0ce74162021-01-16 14:43:49 +01001466 * Warn if any leftover static devices are found.
Uwe Hermanne4870472010-11-04 23:23:47 +00001467 * There's probably a problem in devicetree.cb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001468 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001469
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001470 prev = &bus->children;
1471 for (dev = bus->children; dev; dev = dev->sibling) {
Duncan Lauriebf696222020-10-18 15:10:00 -07001472
1473 /*
1474 * If static device is not PCI then enable it here and don't
1475 * treat it as a leftover device.
1476 */
1477 if (dev->path.type != DEVICE_PATH_PCI) {
1478 enable_static_device(dev);
1479 continue;
1480 }
1481
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -06001482 /*
1483 * The device is only considered leftover if it is not hidden
1484 * and it has a Vendor ID of 0 (the default for a device that
1485 * could not be probed).
1486 */
1487 if (dev->vendor != 0 || dev->hidden) {
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001488 prev = &dev->sibling;
1489 continue;
1490 }
1491
1492 /* Unlink it from list. */
1493 *prev = dev->sibling;
1494
1495 if (!once++)
1496 printk(BIOS_WARNING, "PCI: Leftover static devices:\n");
1497 printk(BIOS_WARNING, "%s\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001498 }
1499
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001500 if (once)
1501 printk(BIOS_WARNING, "PCI: Check your devicetree.cb.\n");
1502
Uwe Hermanne4870472010-11-04 23:23:47 +00001503 /*
1504 * For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001505 * scan the bus behind that child.
1506 */
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001507
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +02001508 scan_bridges(bus);
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001509
Uwe Hermanne4870472010-11-04 23:23:47 +00001510 /*
1511 * We've scanned the bus and so we know all about what's on the other
Myles Watson29cc9ed2009-07-02 18:56:24 +00001512 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001513 * Return how far we've got finding sub-buses.
1514 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001515 post_code(0x55);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001516}
1517
Kyösti Mälkki33452402015-02-23 06:58:26 +02001518typedef enum {
1519 PCI_ROUTE_CLOSE,
1520 PCI_ROUTE_SCAN,
1521 PCI_ROUTE_FINAL,
1522} scan_state;
1523
1524static void pci_bridge_route(struct bus *link, scan_state state)
1525{
1526 struct device *dev = link->dev;
1527 struct bus *parent = dev->bus;
Arthur Heymansf879d362021-11-10 22:09:58 +01001528 uint8_t primary, secondary, subordinate;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001529
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001530 if (state == PCI_ROUTE_SCAN) {
1531 link->secondary = parent->subordinate + 1;
Jeremy Sollercf2ac542019-10-09 21:40:36 -06001532 link->subordinate = link->secondary + dev->hotplug_buses;
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001533 }
1534
Kyösti Mälkki33452402015-02-23 06:58:26 +02001535 if (state == PCI_ROUTE_CLOSE) {
Arthur Heymansf879d362021-11-10 22:09:58 +01001536 primary = 0;
1537 secondary = 0xff;
1538 subordinate = 0xfe;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001539 } else if (state == PCI_ROUTE_SCAN) {
Arthur Heymansf879d362021-11-10 22:09:58 +01001540 primary = parent->secondary;
1541 secondary = link->secondary;
1542 subordinate = 0xff; /* MAX PCI_BUS number here */
Kyösti Mälkki33452402015-02-23 06:58:26 +02001543 } else if (state == PCI_ROUTE_FINAL) {
Arthur Heymansf879d362021-11-10 22:09:58 +01001544 primary = parent->secondary;
1545 secondary = link->secondary;
1546 subordinate = link->subordinate;
Arthur Heymans4a3331d2022-03-23 17:58:46 +01001547 } else {
1548 return;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001549 }
1550
1551 if (state == PCI_ROUTE_SCAN) {
1552 /* Clear all status bits and turn off memory, I/O and master enables. */
1553 link->bridge_cmd = pci_read_config16(dev, PCI_COMMAND);
1554 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1555 pci_write_config16(dev, PCI_STATUS, 0xffff);
1556 }
1557
1558 /*
1559 * Configure the bus numbers for this bridge: the configuration
1560 * transactions will not be propagated by the bridge if it is not
1561 * correctly configured.
1562 */
Arthur Heymansf879d362021-11-10 22:09:58 +01001563 pci_write_config8(dev, PCI_PRIMARY_BUS, primary);
1564 pci_write_config8(dev, PCI_SECONDARY_BUS, secondary);
1565 pci_write_config8(dev, PCI_SUBORDINATE_BUS, subordinate);
Kyösti Mälkki33452402015-02-23 06:58:26 +02001566
1567 if (state == PCI_ROUTE_FINAL) {
1568 pci_write_config16(dev, PCI_COMMAND, link->bridge_cmd);
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001569 parent->subordinate = link->subordinate;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001570 }
1571}
1572
Li-Ta Loe5266692004-03-23 21:28:05 +00001573/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001574 * Scan a PCI bridge and the buses behind the bridge.
Li-Ta Loe5266692004-03-23 21:28:05 +00001575 *
1576 * Determine the existence of buses behind the bridge. Set up the bridge
1577 * according to the result of the scan.
1578 *
1579 * This function is the default scan_bus() method for PCI bridge devices.
1580 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001581 * @param dev Pointer to the bridge device.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001582 * @param do_scan_bus TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +00001583 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001584void do_pci_scan_bridge(struct device *dev,
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001585 void (*do_scan_bus) (struct bus * bus,
Martin Roth38ddbfb2019-10-23 21:41:00 -06001586 unsigned int min_devfn,
1587 unsigned int max_devfn))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001588{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001589 struct bus *bus;
Eric Biederman83b991a2003-10-11 06:20:25 +00001590
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001591 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
Li-Ta Lo3a812852004-12-03 22:39:34 +00001592
Myles Watson894a3472010-06-09 22:41:35 +00001593 if (dev->link_list == NULL) {
1594 struct bus *link;
1595 link = malloc(sizeof(*link));
1596 if (link == NULL)
1597 die("Couldn't allocate a link!\n");
1598 memset(link, 0, sizeof(*link));
1599 link->dev = dev;
1600 dev->link_list = link;
1601 }
1602
1603 bus = dev->link_list;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001604
Nico Huber061b9052019-09-21 15:58:23 +02001605 pci_bridge_vga_compat(bus);
1606
Kyösti Mälkki33452402015-02-23 06:58:26 +02001607 pci_bridge_route(bus, PCI_ROUTE_SCAN);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001608
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001609 do_scan_bus(bus, 0x00, 0xff);
Kyösti Mälkki33452402015-02-23 06:58:26 +02001610
1611 pci_bridge_route(bus, PCI_ROUTE_FINAL);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001612}
Li-Ta Loe5266692004-03-23 21:28:05 +00001613
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001614/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001615 * Scan a PCI bridge and the buses behind the bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001616 *
1617 * Determine the existence of buses behind the bridge. Set up the bridge
1618 * according to the result of the scan.
1619 *
1620 * This function is the default scan_bus() method for PCI bridge devices.
1621 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001622 * @param dev Pointer to the bridge device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001623 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001624void pci_scan_bridge(struct device *dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001625{
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001626 do_pci_scan_bridge(dev, pci_scan_bus);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001627}
1628
Myles Watson29cc9ed2009-07-02 18:56:24 +00001629/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001630 * Scan a PCI domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001631 *
1632 * This function is the default scan_bus() method for PCI domains.
1633 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001634 * @param dev Pointer to the domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001635 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001636void pci_domain_scan_bus(struct device *dev)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001637{
Kyösti Mälkki6f370172015-03-19 15:26:52 +02001638 struct bus *link = dev->link_list;
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001639 pci_scan_bus(link, PCI_DEVFN(0, 0), 0xff);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001640}
1641
Angel Ponsb6519812021-12-31 13:33:50 +01001642void pci_dev_disable_bus_master(const struct device *dev)
1643{
1644 pci_update_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MASTER, 0x0);
1645}
1646
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001647/**
1648 * Take an INT_PIN number (0, 1 - 4) and convert
1649 * it to a string ("NO PIN", "PIN A" - "PIN D")
1650 *
1651 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1652 * @return A string corresponding to the pin number or "Invalid"
1653 */
1654const char *pin_to_str(int pin)
1655{
1656 const char *str[5] = {
1657 "NO PIN",
1658 "PIN A",
1659 "PIN B",
1660 "PIN C",
1661 "PIN D",
1662 };
1663
1664 if (pin >= 0 && pin <= 4)
1665 return str[pin];
1666 else
1667 return "Invalid PIN, not 0 - 4";
1668}
1669
1670/**
1671 * Get the PCI INT_PIN swizzle for a device defined as:
1672 * pin_parent = (pin_child + devn_child) % 4 + 1
1673 * where PIN A = 1 ... PIN_D = 4
1674 *
1675 * Given a PCI device structure 'dev', find the interrupt pin
1676 * that will be triggered on its parent bridge device when
1677 * generating an interrupt. For example: Device 1:3.2 may
1678 * use INT_PIN A but will trigger PIN D on its parent bridge
1679 * device. In this case, this function will return 4 (PIN D).
1680 *
1681 * @param dev A PCI device structure to swizzle interrupt pins for
Martin Roth32bc6b62015-01-04 16:54:35 -07001682 * @param *parent_bridge The PCI device structure for the bridge
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001683 * device 'dev' is attached to
1684 * @return The interrupt pin number (1 - 4) that 'dev' will
1685 * trigger when generating an interrupt
1686 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001687static int swizzle_irq_pins(struct device *dev, struct device **parent_bridge)
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001688{
Aaron Durbinc30d9132017-08-07 16:55:43 -06001689 struct device *parent; /* Our current device's parent device */
1690 struct device *child; /* The child device of the parent */
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001691 uint8_t parent_bus = 0; /* Parent Bus number */
1692 uint16_t parent_devfn = 0; /* Parent Device and Function number */
1693 uint16_t child_devfn = 0; /* Child Device and Function number */
1694 uint8_t swizzled_pin = 0; /* Pin swizzled across a bridge */
1695
1696 /* Start with PIN A = 0 ... D = 3 */
1697 swizzled_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN) - 1;
1698
1699 /* While our current device has parent devices */
1700 child = dev;
1701 for (parent = child->bus->dev; parent; parent = parent->bus->dev) {
1702 parent_bus = parent->bus->secondary;
1703 parent_devfn = parent->path.pci.devfn;
1704 child_devfn = child->path.pci.devfn;
1705
1706 /* Swizzle the INT_PIN for any bridges not on root bus */
1707 swizzled_pin = (PCI_SLOT(child_devfn) + swizzled_pin) % 4;
1708 printk(BIOS_SPEW, "\tWith INT_PIN swizzled to %s\n"
1709 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1710 pin_to_str(swizzled_pin + 1), parent_bus,
1711 PCI_SLOT(parent_devfn), PCI_FUNC(parent_devfn));
1712
1713 /* Continue until we find the root bus */
1714 if (parent_bus > 0) {
1715 /*
1716 * We will go on to the next parent so this parent
1717 * becomes the child
1718 */
1719 child = parent;
1720 continue;
1721 } else {
1722 /*
1723 * Found the root bridge device,
1724 * fill in the structure and exit
1725 */
1726 *parent_bridge = parent;
1727 break;
1728 }
1729 }
1730
1731 /* End with PIN A = 1 ... D = 4 */
1732 return swizzled_pin + 1;
1733}
1734
1735/**
1736 * Given a device structure 'dev', find its interrupt pin
1737 * and its parent bridge 'parent_bdg' device structure.
1738 * If it is behind a bridge, it will return the interrupt
1739 * pin number (1 - 4) of the parent bridge that the device
1740 * interrupt pin has been swizzled to, otherwise it will
1741 * return the interrupt pin that is programmed into the
1742 * PCI config space of the target device. If 'dev' is
1743 * behind a bridge, it will fill in 'parent_bdg' with the
1744 * device structure of the bridge it is behind, otherwise
1745 * it will copy 'dev' into 'parent_bdg'.
1746 *
1747 * @param dev A PCI device structure to get interrupt pins for.
1748 * @param *parent_bdg The PCI device structure for the bridge
1749 * device 'dev' is attached to.
1750 * @return The interrupt pin number (1 - 4) that 'dev' will
1751 * trigger when generating an interrupt.
1752 * Errors: -1 is returned if the device is not enabled
1753 * -2 is returned if a parent bridge could not be found.
1754 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001755int get_pci_irq_pins(struct device *dev, struct device **parent_bdg)
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001756{
1757 uint8_t bus = 0; /* The bus this device is on */
1758 uint16_t devfn = 0; /* This device's device and function numbers */
1759 uint8_t int_pin = 0; /* Interrupt pin used by the device */
1760 uint8_t target_pin = 0; /* Interrupt pin we want to assign an IRQ to */
1761
1762 /* Make sure this device is enabled */
1763 if (!(dev->enabled && (dev->path.type == DEVICE_PATH_PCI)))
1764 return -1;
1765
1766 bus = dev->bus->secondary;
1767 devfn = dev->path.pci.devfn;
1768
1769 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1770 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1771 if (int_pin < 1 || int_pin > 4)
1772 return -1;
1773
1774 printk(BIOS_SPEW, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1775 bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pin_to_str(int_pin));
1776
1777 /* If this device is on a bridge, swizzle its INT_PIN */
1778 if (bus) {
1779 /* Swizzle its INT_PINs */
1780 target_pin = swizzle_irq_pins(dev, parent_bdg);
1781
1782 /* Make sure the swizzle returned valid structures */
1783 if (parent_bdg == NULL) {
Julius Wernere9665952022-01-21 17:06:20 -08001784 printk(BIOS_WARNING, "Could not find parent bridge for this device!\n");
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001785 return -2;
1786 }
1787 } else { /* Device is not behind a bridge */
1788 target_pin = int_pin; /* Return its own interrupt pin */
1789 *parent_bdg = dev; /* Return its own structure */
1790 }
1791
1792 /* Target pin is the interrupt pin we want to assign an IRQ to */
1793 return target_pin;
1794}
1795
Julius Wernercd49cce2019-03-05 16:53:33 -08001796#if CONFIG(PC80_SYSTEM)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001797/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001798 * Assign IRQ numbers.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001799 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001800 * This function assigns IRQs for all functions contained within the indicated
Uwe Hermanne4870472010-11-04 23:23:47 +00001801 * device address. If the device does not exist or does not require interrupts
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001802 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001803 *
1804 * This function should be called for each PCI slot in your system.
1805 *
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001806 * @param dev Pointer to dev structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001807 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1808 * of this slot. The particular IRQ #s that are passed in depend on the
1809 * routing inside your southbridge and on your board.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001810 */
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001811void pci_assign_irqs(struct device *dev, const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001812{
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001813 u8 slot, line, irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001814
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001815 /* Each device may contain up to eight functions. */
1816 slot = dev->path.pci.devfn >> 3;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001817
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001818 for (; dev ; dev = dev->sibling) {
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001819
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001820 if (dev->path.pci.devfn >> 3 != slot)
1821 break;
1822
1823 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001824
Uwe Hermanne4870472010-11-04 23:23:47 +00001825 /* PCI spec says all values except 1..4 are reserved. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001826 if ((line < 1) || (line > 4))
1827 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001828
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001829 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001830
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001831 printk(BIOS_DEBUG, "Assigning IRQ %d to %s\n", irq, dev_path(dev));
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001832
Angel Ponsceca5de2021-06-28 11:59:33 +02001833 pci_write_config8(dev, PCI_INTERRUPT_LINE, irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001834
Uwe Hermanne4870472010-11-04 23:23:47 +00001835 /* Change to level triggered. */
Angel Ponsceca5de2021-06-28 11:59:33 +02001836 i8259_configure_irq_trigger(irq, IRQ_LEVEL_TRIGGERED);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001837 }
1838}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001839#endif