blob: 9de294698af3284b698c1bfd8d61951d90c02ff4 [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 |=
Elyes Haouasd369c662022-11-18 15:06:21 +0100111 ((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;
Nico Huber577c6b92022-08-15 00:08:58 +0200167 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000168 resource->flags |= IORESOURCE_PREFETCH;
Nico Huber577c6b92022-08-15 00:08:58 +0200169 if (CONFIG(PCIEXP_HOTPLUG_PREFETCH_MEM_ABOVE_4G)
170 && dev_path_hotplug(dev))
171 resource->flags |= IORESOURCE_ABOVE_4G;
172 }
Eric Biederman03acab62004-10-14 21:25:53 +0000173 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
174 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000175 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000177 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
178 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000179 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000180 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
181 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000182 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000183 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000184 } else {
185 /* Invalid value. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000186 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
187 printk(BIOS_ERR, " on dev %s at index %02lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000188 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000189 resource->flags = 0;
190 }
191 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000192
Myles Watson29cc9ed2009-07-02 18:56:24 +0000193 /* Don't let the limit exceed which bits can move. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000194 if (resource->limit > limit)
Eric Biederman03acab62004-10-14 21:25:53 +0000195 resource->limit = limit;
Eric Biederman03acab62004-10-14 21:25:53 +0000196
Eric Biederman5cd81732004-03-11 15:01:31 +0000197 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000198}
199
Myles Watson29cc9ed2009-07-02 18:56:24 +0000200/**
201 * Given a device and an index, read the size of the BAR for that register.
202 *
203 * @param dev Pointer to the device structure.
204 * @param index Address of the PCI configuration register.
205 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000206static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000207{
208 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000209 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000210 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000211
Myles Watson29cc9ed2009-07-02 18:56:24 +0000212 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000213 resource = new_resource(dev, index);
214
Myles Watson29cc9ed2009-07-02 18:56:24 +0000215 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000216 value = pci_read_config32(dev, index);
217
Myles Watson29cc9ed2009-07-02 18:56:24 +0000218 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000219 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000220
221 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000222 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000223
Myles Watson032a9652009-05-11 22:24:53 +0000224 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000225 * Start by finding the bits that move. From there:
226 * - Size is the least significant bit of the bits that move.
227 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000228 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000229 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000230 if (moving) {
231 resource->size = 1;
232 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000233 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000234 resource->size <<= 1;
235 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000236 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000237 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000238 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000239 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
240 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000241 if (value != 0) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200242 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000243 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000244 }
245 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000246 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000247 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000248}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000249
Myles Watson29cc9ed2009-07-02 18:56:24 +0000250/**
Patrick Rudolph4e2f95b2018-05-16 14:56:22 +0200251 * Given a device, read the size of the MSI-X table.
252 *
253 * @param dev Pointer to the device structure.
254 * @return MSI-X table size or 0 if not MSI-X capable device
255 */
256size_t pci_msix_table_size(struct device *dev)
257{
258 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
259 if (!pos)
260 return 0;
261
262 const u16 control = pci_read_config16(dev, pos + PCI_MSIX_FLAGS);
263 return (control & PCI_MSIX_FLAGS_QSIZE) + 1;
264}
265
266/**
267 * Given a device, return the table offset and bar the MSI-X tables resides in.
268 *
269 * @param dev Pointer to the device structure.
270 * @param offset Returned value gives the offset in bytes inside the PCI BAR.
271 * @param idx The returned value is the index of the PCI_BASE_ADDRESS register
272 * the MSI-X table is located in.
273 * @return Zero on success
274 */
275int pci_msix_table_bar(struct device *dev, u32 *offset, u8 *idx)
276{
277 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
278 if (!pos || !offset || !idx)
279 return 1;
280
281 *offset = pci_read_config32(dev, pos + PCI_MSIX_TABLE);
282 *idx = (u8)(*offset & PCI_MSIX_PBA_BIR);
283 *offset &= PCI_MSIX_PBA_OFFSET;
284
285 return 0;
286}
287
288/**
289 * Given a device, return a msix_entry pointer or NULL if no table was found.
290 *
291 * @param dev Pointer to the device structure.
292 *
293 * @return NULL on error
294 */
295struct msix_entry *pci_msix_get_table(struct device *dev)
296{
297 struct resource *res;
298 u32 offset;
299 u8 idx;
300
301 if (pci_msix_table_bar(dev, &offset, &idx))
302 return NULL;
303
304 if (idx > 5)
305 return NULL;
306
307 res = probe_resource(dev, idx * 4 + PCI_BASE_ADDRESS_0);
308 if (!res || !res->base || offset >= res->size)
309 return NULL;
310
311 if ((res->flags & IORESOURCE_PCI64) &&
312 (uintptr_t)res->base != res->base)
313 return NULL;
314
315 return (struct msix_entry *)((uintptr_t)res->base + offset);
316}
317
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700318static unsigned int get_rebar_offset(const struct device *dev, unsigned long index)
319{
Nico Huber5ffc2c82022-08-05 12:58:18 +0200320 uint32_t offset = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_RESIZABLE_BAR, 0);
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700321 if (!offset)
322 return 0;
323
324 /* Convert PCI_BASE_ADDRESS_0, ..._1, ..._2 into 0, 1, 2... */
325 const unsigned int find_bar_idx = (index - PCI_BASE_ADDRESS_0) /
326 sizeof(uint32_t);
327
328 /* Although all of the Resizable BAR Control Registers contain an
329 "NBARs" field, it is only valid in the Control Register for BAR 0 */
330 const uint32_t rebar_ctrl0 = pci_read_config32(dev, offset + PCI_REBAR_CTRL_OFFSET);
331 const unsigned int nbars = (rebar_ctrl0 & PCI_REBAR_CTRL_NBARS_MASK) >>
332 PCI_REBAR_CTRL_NBARS_SHIFT;
333
334 for (unsigned int i = 0; i < nbars; i++, offset += sizeof(uint64_t)) {
335 const uint32_t rebar_ctrl = pci_read_config32(
336 dev, offset + PCI_REBAR_CTRL_OFFSET);
337 const uint32_t bar_idx = rebar_ctrl & PCI_REBAR_CTRL_IDX_MASK;
338 if (bar_idx == find_bar_idx)
339 return offset;
340 }
341
342 return 0;
343}
344
345/* Bit 20 = 1 MiB, bit 21 = 2 MiB, bit 22 = 4 MiB, ... bit 63 = 8 EiB */
346static uint64_t get_rebar_sizes_mask(const struct device *dev,
347 unsigned long index)
348{
349 uint64_t size_mask = 0ULL;
350 const uint32_t offset = get_rebar_offset(dev, index);
351 if (!offset)
352 return 0;
353
354 /* Get 1 MB - 128 TB support from CAP register */
355 const uint32_t cap = pci_read_config32(dev, offset + PCI_REBAR_CAP_OFFSET);
356 /* Shift the bits from 4-31 to 0-27 (i.e., down by 4 bits) */
357 size_mask |= ((cap & PCI_REBAR_CAP_SIZE_MASK) >> 4);
358
359 /* Get 256 TB - 8 EB support from CTRL register and store it in bits 28-43 */
360 const uint64_t ctrl = pci_read_config32(dev, offset + PCI_REBAR_CTRL_OFFSET);
361 /* Shift ctrl mask from bit 16 to bit 28, so that the two
362 masks (fom cap and ctrl) form a contiguous bitmask when
363 concatenated (i.e., up by 12 bits). */
364 size_mask |= ((ctrl & PCI_REBAR_CTRL_SIZE_MASK) << 12);
365
366 /* Now that the mask occupies bits 0-43, shift it up to 20-63, so they
367 represent the actual powers of 2. */
368 return size_mask << 20;
369}
370
371static void pci_store_rebar_size(const struct device *dev,
372 const struct resource *resource)
373{
374 const unsigned int num_bits = __fls64(resource->size);
375 const uint32_t offset = get_rebar_offset(dev, resource->index);
376 if (!offset)
377 return;
378
379 pci_update_config32(dev, offset + PCI_REBAR_CTRL_OFFSET,
380 ~PCI_REBAR_CTRL_SIZE_MASK,
381 num_bits << PCI_REBAR_CTRL_SIZE_SHIFT);
382}
383
384static void configure_adjustable_base(const struct device *dev,
385 unsigned long index,
386 struct resource *res)
387{
388 /*
389 * Excerpt from an implementation note from the PCIe spec:
390 *
391 * System software uses this capability in place of the above mentioned
392 * method of determining the resource size[0], and prior to assigning
393 * the base address to the BAR. Potential usable resource sizes are
394 * reported by the Function via its Resizable BAR Capability and Control
395 * registers. It is intended that the software allocate the largest of
396 * the reported sizes that it can, since allocating less address space
397 * than the largest reported size can result in lower
398 * performance. Software then writes the size to the Resizable BAR
399 * Control register for the appropriate BAR for the Function. Following
400 * this, the base address is written to the BAR.
401 *
402 * [0] Referring to using the moving bits in the BAR to determine the
403 * requested size of the MMIO region
404 */
405 const uint64_t size_mask = get_rebar_sizes_mask(dev, index);
406 if (!size_mask)
407 return;
408
409 int max_requested_bits = __fls64(size_mask);
410 if (max_requested_bits > CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS) {
Elyes Haouasaba1c942022-11-09 15:05:23 +0100411 printk(BIOS_WARNING, "Device %s requests a BAR with"
Paul Menzeld579d802022-09-06 08:25:28 +0200412 " %u bits of address space, which coreboot is not"
413 " configured to hand out, truncating to %u bits\n",
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700414 dev_path(dev), max_requested_bits,
415 CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS);
416 max_requested_bits = CONFIG_PCIEXP_DEFAULT_MAX_RESIZABLE_BAR_BITS;
417 }
418
419 if (!(res->flags & IORESOURCE_PCI64) && max_requested_bits > 32) {
Elyes Haouasaba1c942022-11-09 15:05:23 +0100420 printk(BIOS_ERR, "Resizable BAR requested"
Paul Menzeld579d802022-09-06 08:25:28 +0200421 " above 32 bits, but PCI function reported a"
422 " 32-bit BAR.");
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700423 return;
424 }
425
426 /* Configure the resource parameters for the adjustable BAR */
427 res->size = 1ULL << max_requested_bits;
428 res->align = max_requested_bits;
429 res->gran = max_requested_bits;
430 res->limit = (res->flags & IORESOURCE_PCI64) ? UINT64_MAX : UINT32_MAX;
Tim Wawrzynczak2b83fa72022-05-27 12:27:50 -0600431 res->flags |= (res->flags & IORESOURCE_PCI64) ?
432 IORESOURCE_PCIE_RESIZABLE_BAR | IORESOURCE_ABOVE_4G :
433 IORESOURCE_PCIE_RESIZABLE_BAR;
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700434
435 printk(BIOS_INFO, "%s: Adjusting resource index %lu: base: %llx size: %llx "
436 "align: %d gran: %d limit: %llx\n",
437 dev_path(dev), res->index, res->base, res->size,
438 res->align, res->gran, res->limit);
439}
440
Patrick Rudolph4e2f95b2018-05-16 14:56:22 +0200441/**
Myles Watson29cc9ed2009-07-02 18:56:24 +0000442 * Read the base address registers for a given device.
443 *
444 * @param dev Pointer to the dev structure.
445 * @param howmany How many registers to read (6 for device, 2 for bridge).
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000446 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000447static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000448{
449 unsigned long index;
450
Myles Watson29cc9ed2009-07-02 18:56:24 +0000451 for (index = PCI_BASE_ADDRESS_0;
452 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000453 struct resource *resource;
454 resource = pci_get_resource(dev, index);
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700455
456 const bool is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE) != 0;
457 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS) && is_pcie)
458 configure_adjustable_base(dev, index, resource);
459
Myles Watson29cc9ed2009-07-02 18:56:24 +0000460 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000461 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000462
463 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000464}
465
Myles Watson29cc9ed2009-07-02 18:56:24 +0000466static void pci_record_bridge_resource(struct device *dev, resource_t moving,
Martin Roth38ddbfb2019-10-23 21:41:00 -0600467 unsigned int index, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000468{
Eric Biederman03acab62004-10-14 21:25:53 +0000469 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000470 unsigned long gran;
471 resource_t step;
472
Myles Watson29cc9ed2009-07-02 18:56:24 +0000473 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000474
475 if (!moving)
476 return;
477
478 /* Initialize the constraints on the current bus. */
479 resource = new_resource(dev, index);
480 resource->size = 0;
481 gran = 0;
482 step = 1;
483 while ((moving & step) == 0) {
484 gran += 1;
485 step <<= 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000486 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000487 resource->gran = gran;
488 resource->align = gran;
489 resource->limit = moving | (step - 1);
490 resource->flags = type | IORESOURCE_PCI_BRIDGE |
491 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000492}
493
Eric Biederman8ca8d762003-04-22 19:02:15 +0000494static void pci_bridge_read_bases(struct device *dev)
495{
Eric Biederman03acab62004-10-14 21:25:53 +0000496 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000497
Myles Watson29cc9ed2009-07-02 18:56:24 +0000498 /* See if the bridge I/O resources are implemented. */
Elyes Haouasd369c662022-11-18 15:06:21 +0100499 moving_base = ((u32)pci_moving_config8(dev, PCI_IO_BASE)) << 8;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000500 moving_base |=
Elyes Haouasd369c662022-11-18 15:06:21 +0100501 ((u32)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000502
Elyes Haouasd369c662022-11-18 15:06:21 +0100503 moving_limit = ((u32)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000504 moving_limit |=
Elyes Haouasd369c662022-11-18 15:06:21 +0100505 ((u32)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000506
507 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000508
Myles Watson29cc9ed2009-07-02 18:56:24 +0000509 /* Initialize the I/O space constraints on the current bus. */
510 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000511
Myles Watson29cc9ed2009-07-02 18:56:24 +0000512 /* See if the bridge prefmem resources are implemented. */
513 moving_base =
Elyes Haouasd369c662022-11-18 15:06:21 +0100514 ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000515 moving_base |=
Elyes Haouasd369c662022-11-18 15:06:21 +0100516 ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000517
Myles Watson29cc9ed2009-07-02 18:56:24 +0000518 moving_limit =
Elyes Haouasd369c662022-11-18 15:06:21 +0100519 ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000520 moving_limit |=
Elyes Haouasd369c662022-11-18 15:06:21 +0100521 ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
Myles Watson032a9652009-05-11 22:24:53 +0000522
Eric Biederman03acab62004-10-14 21:25:53 +0000523 moving = moving_base & moving_limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000524 /* Initialize the prefetchable memory constraints on the current bus. */
525 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
526 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Myles Watson032a9652009-05-11 22:24:53 +0000527
Myles Watson29cc9ed2009-07-02 18:56:24 +0000528 /* See if the bridge mem resources are implemented. */
Elyes Haouasd369c662022-11-18 15:06:21 +0100529 moving_base = ((u32)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
530 moving_limit = ((u32)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000531
532 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000533
Myles Watson29cc9ed2009-07-02 18:56:24 +0000534 /* Initialize the memory resources on the current bus. */
535 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
536 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000537
Eric Biederman5cd81732004-03-11 15:01:31 +0000538 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000539}
540
Eric Biederman5899fd82003-04-24 06:25:08 +0000541void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000542{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000543 pci_read_bases(dev, 6);
544 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000545}
546
Eric Biederman5899fd82003-04-24 06:25:08 +0000547void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000548{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000549 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000550 pci_read_bases(dev, 2);
551 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000552}
553
Myles Watson29cc9ed2009-07-02 18:56:24 +0000554void pci_domain_read_resources(struct device *dev)
555{
556 struct resource *res;
557
558 /* Initialize the system-wide I/O space constraints. */
559 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
560 res->limit = 0xffffUL;
561 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
562 IORESOURCE_ASSIGNED;
563
564 /* Initialize the system-wide memory resources constraints. */
565 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Furquan Shaikh871baf22020-03-12 17:51:24 -0700566 res->limit = (1ULL << cpu_phys_address_size()) - 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000567 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
568 IORESOURCE_ASSIGNED;
569}
570
Raul E Rangel5cb34e22020-05-04 16:41:22 -0600571void pci_domain_set_resources(struct device *dev)
572{
573 assign_resources(dev->link_list);
574}
575
Nico Huber730b2612020-05-20 00:32:50 +0200576static void pci_store_resource(const struct device *const dev,
577 const struct resource *const resource)
578{
579 unsigned long base_lo, base_hi;
580
581 base_lo = resource->base & 0xffffffff;
582 base_hi = (resource->base >> 32) & 0xffffffff;
583
584 /*
585 * Some chipsets allow us to set/clear the I/O bit
586 * (e.g. VIA 82C686A). So set it to be safe.
587 */
588 if (resource->flags & IORESOURCE_IO)
589 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
590
591 pci_write_config32(dev, resource->index, base_lo);
592 if (resource->flags & IORESOURCE_PCI64)
593 pci_write_config32(dev, resource->index + 4, base_hi);
594}
595
596static void pci_store_bridge_resource(const struct device *const dev,
597 struct resource *const resource)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000598{
Eric Biederman03acab62004-10-14 21:25:53 +0000599 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000600
Nico Huber730b2612020-05-20 00:32:50 +0200601 /*
602 * PCI bridges have no enable bit. They are disabled if the base of
603 * the range is greater than the limit. If the size is zero, disable
604 * by setting the base = limit and end = limit - 2^gran.
605 */
606 if (resource->size == 0) {
607 base = resource->limit;
608 end = resource->limit - (1 << resource->gran);
609 resource->base = base;
610 } else {
611 base = resource->base;
612 end = resource_end(resource);
613 }
614
615 if (resource->index == PCI_IO_BASE) {
616 /* Set the I/O ranges. */
617 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
618 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
619 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
620 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
621 } else if (resource->index == PCI_MEMORY_BASE) {
622 /* Set the memory range. */
623 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
624 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
625 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
626 /* Set the prefetchable memory range. */
627 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
628 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
629 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
630 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
631 } else {
632 /* Don't let me think I stored the resource. */
633 resource->flags &= ~IORESOURCE_STORED;
Julius Wernere9665952022-01-21 17:06:20 -0800634 printk(BIOS_ERR, "invalid resource->index %lx\n", resource->index);
Nico Huber730b2612020-05-20 00:32:50 +0200635 }
636}
637
638static void pci_set_resource(struct device *dev, struct resource *resource)
639{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000640 /* Make certain the resource has actually been assigned a value. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000641 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Nico Huberf5312442020-05-20 01:02:18 +0200642 if (resource->flags & IORESOURCE_BRIDGE) {
643 /* If a bridge resource has no value assigned,
644 we can treat it like an empty resource. */
645 resource->size = 0;
646 } else {
Julius Wernere9665952022-01-21 17:06:20 -0800647 printk(BIOS_ERR, "%s %02lx %s size: 0x%010llx not assigned\n",
Angel Ponsd19cc112021-07-04 11:41:31 +0200648 dev_path(dev), resource->index,
Nico Huberf5312442020-05-20 01:02:18 +0200649 resource_type(resource), resource->size);
650 return;
651 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000652 }
653
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000654 /* If this resource is fixed don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000655 if (resource->flags & IORESOURCE_FIXED)
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000656 return;
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000657
Myles Watson29cc9ed2009-07-02 18:56:24 +0000658 /* If I have already stored this resource don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000659 if (resource->flags & IORESOURCE_STORED)
Eric Biederman5cd81732004-03-11 15:01:31 +0000660 return;
Eric Biederman5cd81732004-03-11 15:01:31 +0000661
Myles Watson29cc9ed2009-07-02 18:56:24 +0000662 /* If the resource is subtractive don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000663 if (resource->flags & IORESOURCE_SUBTRACTIVE)
Eric Biederman03acab62004-10-14 21:25:53 +0000664 return;
Eric Biederman03acab62004-10-14 21:25:53 +0000665
Myles Watson29cc9ed2009-07-02 18:56:24 +0000666 /* Only handle PCI memory and I/O resources for now. */
667 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000668 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000669
Myles Watson29cc9ed2009-07-02 18:56:24 +0000670 /* Enable the resources in the command register. */
Eric Biederman03acab62004-10-14 21:25:53 +0000671 if (resource->size) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000672 if (resource->flags & IORESOURCE_MEM)
Eric Biederman03acab62004-10-14 21:25:53 +0000673 dev->command |= PCI_COMMAND_MEMORY;
Uwe Hermanne4870472010-11-04 23:23:47 +0000674 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000675 dev->command |= PCI_COMMAND_IO;
Felix Singer205b53e2020-09-07 15:21:21 +0200676 if (resource->flags & IORESOURCE_PCI_BRIDGE &&
677 CONFIG(PCI_SET_BUS_MASTER_PCI_BRIDGES))
Eric Biederman03acab62004-10-14 21:25:53 +0000678 dev->command |= PCI_COMMAND_MASTER;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000679 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000680
Myles Watson29cc9ed2009-07-02 18:56:24 +0000681 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000682 resource->flags |= IORESOURCE_STORED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000683
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700684 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
685 if (CONFIG(PCIEXP_SUPPORT_RESIZABLE_BARS) &&
686 (resource->flags & IORESOURCE_PCIE_RESIZABLE_BAR))
687 pci_store_rebar_size(dev, resource);
688
Nico Huber730b2612020-05-20 00:32:50 +0200689 pci_store_resource(dev, resource);
Uwe Hermanne4870472010-11-04 23:23:47 +0000690
Tim Wawrzynczak8c93fed2022-01-13 16:45:07 -0700691 } else {
692 pci_store_bridge_resource(dev, resource);
693 }
694
Eric Biederman03acab62004-10-14 21:25:53 +0000695 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000696}
697
Eric Biederman5899fd82003-04-24 06:25:08 +0000698void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000699{
Myles Watsonc25cc112010-05-21 14:33:48 +0000700 struct resource *res;
Myles Watson894a3472010-06-09 22:41:35 +0000701 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000702 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000703
Uwe Hermanne4870472010-11-04 23:23:47 +0000704 for (res = dev->resource_list; res; res = res->next)
Myles Watsonc25cc112010-05-21 14:33:48 +0000705 pci_set_resource(dev, res);
Uwe Hermanne4870472010-11-04 23:23:47 +0000706
Myles Watson894a3472010-06-09 22:41:35 +0000707 for (bus = dev->link_list; bus; bus = bus->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000708 if (bus->children)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000709 assign_resources(bus);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000710 }
711
Myles Watson29cc9ed2009-07-02 18:56:24 +0000712 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000713 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000714
Myles Watson29cc9ed2009-07-02 18:56:24 +0000715 /* Set a default secondary latency timer. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000716 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000717 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000718
Myles Watson29cc9ed2009-07-02 18:56:24 +0000719 /* Zero the IRQ settings. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000720 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Uwe Hermanne4870472010-11-04 23:23:47 +0000721 if (line)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000722 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Uwe Hermanne4870472010-11-04 23:23:47 +0000723
Myles Watson29cc9ed2009-07-02 18:56:24 +0000724 /* Set the cache line size, so far 64 bytes is good for everyone. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000725 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000726}
727
Eric Biedermane9a271e32003-09-02 03:36:25 +0000728void pci_dev_enable_resources(struct device *dev)
729{
Kyösti Mälkkicac02312019-06-30 08:40:04 +0300730 const struct pci_operations *ops = NULL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000731 u16 command;
Eric Biederman03acab62004-10-14 21:25:53 +0000732
Uwe Hermanne4870472010-11-04 23:23:47 +0000733 /* Set the subsystem vendor and device ID for mainboard devices. */
Kyösti Mälkkicac02312019-06-30 08:40:04 +0300734 if (dev->ops)
735 ops = dev->ops->ops_pci;
Eric Biedermandbec2d42004-10-21 10:44:08 +0000736 if (dev->on_mainboard && ops && ops->set_subsystem) {
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700737 if (CONFIG_SUBSYSTEM_VENDOR_ID)
738 dev->subsystem_vendor = CONFIG_SUBSYSTEM_VENDOR_ID;
Rizwan Qureshifd891292017-04-26 21:00:37 +0530739 else if (!dev->subsystem_vendor)
740 dev->subsystem_vendor = pci_read_config16(dev,
741 PCI_VENDOR_ID);
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700742 if (CONFIG_SUBSYSTEM_DEVICE_ID)
743 dev->subsystem_device = CONFIG_SUBSYSTEM_DEVICE_ID;
Rizwan Qureshifd891292017-04-26 21:00:37 +0530744 else if (!dev->subsystem_device)
745 dev->subsystem_device = pci_read_config16(dev,
746 PCI_DEVICE_ID);
747
Sven Schnelle91321022011-03-01 19:58:47 +0000748 printk(BIOS_DEBUG, "%s subsystem <- %04x/%04x\n",
749 dev_path(dev), dev->subsystem_vendor,
750 dev->subsystem_device);
751 ops->set_subsystem(dev, dev->subsystem_vendor,
752 dev->subsystem_device);
Eric Biederman03acab62004-10-14 21:25:53 +0000753 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000754 command = pci_read_config16(dev, PCI_COMMAND);
755 command |= dev->command;
Uwe Hermanne4870472010-11-04 23:23:47 +0000756
Myles Watson29cc9ed2009-07-02 18:56:24 +0000757 /* v3 has
758 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
759 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000760
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000761 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000762 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000763}
764
765void pci_bus_enable_resources(struct device *dev)
766{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000767 u16 ctrl;
768
Uwe Hermanne4870472010-11-04 23:23:47 +0000769 /*
770 * Enable I/O in command register if there is VGA card
Myles Watson29cc9ed2009-07-02 18:56:24 +0000771 * connected with (even it does not claim I/O resource).
772 */
Myles Watson894a3472010-06-09 22:41:35 +0000773 if (dev->link_list->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000774 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000775 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000776 ctrl |= dev->link_list->bridge_ctrl;
Kyösti Mälkki382e2162019-09-21 16:19:32 +0300777 ctrl |= (PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR); /* Error check. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000778 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000779 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
780
781 pci_dev_enable_resources(dev);
782}
783
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000784void pci_bus_reset(struct bus *bus)
785{
Uwe Hermanne4870472010-11-04 23:23:47 +0000786 u16 ctl;
787
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000788 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
789 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
790 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
791 mdelay(10);
Uwe Hermanne4870472010-11-04 23:23:47 +0000792
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000793 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
794 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
795 delay(1);
796}
797
Elyes HAOUAS88030b72018-09-20 17:26:10 +0200798void pci_dev_set_subsystem(struct device *dev, unsigned int vendor,
799 unsigned int device)
Eric Biederman03acab62004-10-14 21:25:53 +0000800{
Subrata Banik9514d472019-03-20 14:56:27 +0530801 uint8_t offset;
802
803 /* Header type */
804 switch (dev->hdr_type & 0x7f) {
805 case PCI_HEADER_TYPE_NORMAL:
806 offset = PCI_SUBSYSTEM_VENDOR_ID;
807 break;
808 case PCI_HEADER_TYPE_BRIDGE:
809 offset = pci_find_capability(dev, PCI_CAP_ID_SSVID);
810 if (!offset)
811 return;
812 offset += 4; /* Vendor ID at offset 4 */
813 break;
814 case PCI_HEADER_TYPE_CARDBUS:
815 offset = PCI_CB_SUBSYSTEM_VENDOR_ID;
816 break;
817 default:
818 return;
819 }
820
Subrata Banik4a0f0712019-03-20 14:29:47 +0530821 if (!vendor || !device) {
Subrata Banik9514d472019-03-20 14:56:27 +0530822 pci_write_config32(dev, offset,
Subrata Banik4a0f0712019-03-20 14:29:47 +0530823 pci_read_config32(dev, PCI_VENDOR_ID));
824 } else {
Subrata Banik9514d472019-03-20 14:56:27 +0530825 pci_write_config32(dev, offset,
Subrata Banik4a0f0712019-03-20 14:29:47 +0530826 ((device & 0xffff) << 16) | (vendor & 0xffff));
827 }
Eric Biederman03acab62004-10-14 21:25:53 +0000828}
829
Frans Hendriksb71181a2019-10-04 14:06:33 +0200830static int should_run_oprom(struct device *dev, struct rom_header *rom)
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300831{
832 static int should_run = -1;
833
Frans Hendriksb71181a2019-10-04 14:06:33 +0200834 if (CONFIG(VENDORCODE_ELTAN_VBOOT))
835 if (rom != NULL)
836 if (!verified_boot_should_run_oprom(rom))
837 return 0;
838
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300839 if (should_run >= 0)
840 return should_run;
841
Julius Wernercd49cce2019-03-05 16:53:33 -0800842 if (CONFIG(ALWAYS_RUN_OPROM)) {
Aaron Durbin10510252018-01-30 10:04:02 -0700843 should_run = 1;
844 return should_run;
845 }
846
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200847 /* Don't run VGA option ROMs, unless we have to print
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300848 * something on the screen before the kernel is loaded.
849 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700850 should_run = display_init_required();
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300851
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200852 if (!should_run)
853 printk(BIOS_DEBUG, "Not running VGA Option ROM\n");
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300854 return should_run;
855}
856
857static int should_load_oprom(struct device *dev)
858{
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300859 /* If S3_VGA_ROM_RUN is disabled, skip running VGA option
860 * ROMs when coming out of an S3 resume.
861 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800862 if (!CONFIG(S3_VGA_ROM_RUN) && acpi_is_wakeup_s3() &&
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300863 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
864 return 0;
Julius Wernercd49cce2019-03-05 16:53:33 -0800865 if (CONFIG(ALWAYS_LOAD_OPROM))
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300866 return 1;
Frans Hendriksb71181a2019-10-04 14:06:33 +0200867 if (should_run_oprom(dev, NULL))
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300868 return 1;
869
870 return 0;
871}
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300872
Kyösti Mälkki0f300632020-12-19 23:43:56 +0200873static void oprom_pre_graphics_stall(void)
874{
Paul Menzelc4062c72021-02-11 10:43:14 +0100875 if (CONFIG_PRE_GRAPHICS_DELAY_MS)
876 mdelay(CONFIG_PRE_GRAPHICS_DELAY_MS);
Kyösti Mälkki0f300632020-12-19 23:43:56 +0200877}
878
Uwe Hermanne4870472010-11-04 23:23:47 +0000879/** Default handler: only runs the relevant PCI BIOS. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000880void pci_dev_init(struct device *dev)
881{
882 struct rom_header *rom, *ram;
883
Julius Wernercd49cce2019-03-05 16:53:33 -0800884 if (!CONFIG(VGA_ROM_RUN))
Aaron Durbinfbed9a52018-01-30 09:58:51 -0700885 return;
886
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100887 /* Only execute VGA ROMs. */
888 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
Myles Watson17aeeca2009-10-07 18:41:08 +0000889 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000890
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300891 if (!should_load_oprom(dev))
Stefan Reinauer74a0efe2012-03-30 17:10:49 -0700892 return;
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700893 timestamp_add_now(TS_OPROM_INITIALIZE);
Aaron Durbince872cb2013-03-28 15:59:19 -0500894
895 rom = pci_rom_probe(dev);
896 if (rom == NULL)
897 return;
898
899 ram = pci_rom_load(dev, rom);
900 if (ram == NULL)
901 return;
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700902 timestamp_add_now(TS_OPROM_COPY_END);
Aaron Durbince872cb2013-03-28 15:59:19 -0500903
Frans Hendriksb71181a2019-10-04 14:06:33 +0200904 if (!should_run_oprom(dev, rom))
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300905 return;
906
Kyösti Mälkki0f300632020-12-19 23:43:56 +0200907 /* Wait for any configured pre-graphics delay */
908 oprom_pre_graphics_stall();
909
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000910 run_bios(dev, (unsigned long)ram);
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +0200911
Kyösti Mälkkiab56b3b2013-11-28 16:44:51 +0200912 gfx_set_init_done(1);
913 printk(BIOS_DEBUG, "VGA Option ROM was run\n");
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700914 timestamp_add_now(TS_OPROM_END);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000915}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000916
Li-Ta Loe5266692004-03-23 21:28:05 +0000917/** Default device operation for PCI devices */
Subrata Banikffc790b2017-12-11 10:29:49 +0530918struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000919 .set_subsystem = pci_dev_set_subsystem,
920};
921
Eric Biederman8ca8d762003-04-22 19:02:15 +0000922struct device_operations default_pci_ops_dev = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000923 .read_resources = pci_dev_read_resources,
924 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000925 .enable_resources = pci_dev_enable_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -0800926#if CONFIG(HAVE_ACPI_TABLES)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200927 .write_acpi_tables = pci_rom_write_acpi_tables,
Nico Huber68680dd2020-03-31 17:34:52 +0200928 .acpi_fill_ssdt = pci_rom_ssdt,
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200929#endif
Uwe Hermanne4870472010-11-04 23:23:47 +0000930 .init = pci_dev_init,
Uwe Hermanne4870472010-11-04 23:23:47 +0000931 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000932};
Li-Ta Loe5266692004-03-23 21:28:05 +0000933
934/** Default device operations for PCI bridges */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000935struct device_operations default_pci_ops_bus = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000936 .read_resources = pci_bus_read_resources,
937 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000938 .enable_resources = pci_bus_enable_resources,
Uwe Hermanne4870472010-11-04 23:23:47 +0000939 .scan_bus = pci_scan_bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000940 .reset_bus = pci_bus_reset,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000941};
Li-Ta Loe5266692004-03-23 21:28:05 +0000942
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -0600943/** Default device operations for PCI devices marked 'hidden' */
944static struct device_operations default_hidden_pci_ops_dev = {
945 .read_resources = noop_read_resources,
946 .set_resources = noop_set_resources,
947 .scan_bus = scan_static_bus,
948};
949
Li-Ta Loe5266692004-03-23 21:28:05 +0000950/**
Nico Huber061b9052019-09-21 15:58:23 +0200951 * Check for compatibility to route legacy VGA cycles through a bridge.
952 *
953 * Originally, when decoding i/o ports for legacy VGA cycles, bridges
954 * should only consider the 10 least significant bits of the port address.
955 * This means all VGA registers were aliased every 1024 ports!
956 * e.g. 0x3b0 was also decoded as 0x7b0, 0xbb0 etc.
957 *
958 * To avoid this mess, a bridge control bit (VGA16) was introduced in
959 * 2003 to enable decoding of 16-bit port addresses. As we don't want
960 * to make this any more complex for now, we use this bit if possible
961 * and only warn if it's not supported (in set_vga_bridge_bits()).
962 */
963static void pci_bridge_vga_compat(struct bus *const bus)
964{
965 uint16_t bridge_ctrl;
966
967 bridge_ctrl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
968
969 /* Ensure VGA decoding is disabled during probing (it should
970 be by default, but we run blobs nowadays) */
971 bridge_ctrl &= ~PCI_BRIDGE_CTL_VGA;
972 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, bridge_ctrl);
973
974 /* If the upstream bridge doesn't support VGA16, we don't have to check */
975 bus->no_vga16 |= bus->dev->bus->no_vga16;
976 if (bus->no_vga16)
977 return;
978
979 /* Test if we can enable 16-bit decoding */
980 bridge_ctrl |= PCI_BRIDGE_CTL_VGA16;
981 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, bridge_ctrl);
982 bridge_ctrl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
983
984 bus->no_vga16 = !(bridge_ctrl & PCI_BRIDGE_CTL_VGA16);
985}
986
987/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000988 * Detect the type of downstream bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000989 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000990 * This function is a heuristic to detect which type of bus is downstream
991 * of a PCI-to-PCI bridge. This functions by looking for various capability
992 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
993 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +0000994 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000995 * When only a PCI-Express capability is found the type is examined to see
996 * which type of bridge we have.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000997 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000998 * @param dev Pointer to the device structure of the bridge.
999 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001000 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001001static struct device_operations *get_pci_bridge_ops(struct device *dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001002{
Julius Wernercd49cce2019-03-05 16:53:33 -08001003#if CONFIG(PCIX_PLUGIN_SUPPORT)
Ronald G. Minnich78a16672012-11-29 16:28:21 -08001004 unsigned int pcixpos;
1005 pcixpos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1006 if (pcixpos) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001007 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001008 return &default_pcix_ops_bus;
1009 }
1010#endif
Julius Wernercd49cce2019-03-05 16:53:33 -08001011#if CONFIG(PCIEXP_PLUGIN_SUPPORT)
Ronald G. Minnich78a16672012-11-29 16:28:21 -08001012 unsigned int pciexpos;
1013 pciexpos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
1014 if (pciexpos) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001015 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -08001016 flags = pci_read_config16(dev, pciexpos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001017 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001018 case PCI_EXP_TYPE_ROOT_PORT:
1019 case PCI_EXP_TYPE_UPSTREAM:
1020 case PCI_EXP_TYPE_DOWNSTREAM:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001021 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
Uwe Hermanne4870472010-11-04 23:23:47 +00001022 dev_path(dev));
Arthur Heymans24837e72021-03-11 20:34:05 +01001023 if (CONFIG(PCIEXP_HOTPLUG)) {
1024 u16 sltcap;
1025 sltcap = pci_read_config16(dev, pciexpos + PCI_EXP_SLTCAP);
1026 if (sltcap & PCI_EXP_SLTCAP_HPC) {
1027 printk(BIOS_DEBUG, "%s hot-plug capable\n",
1028 dev_path(dev));
1029 return &default_pciexp_hotplug_ops_bus;
1030 }
Jeremy Sollercf2ac542019-10-09 21:40:36 -06001031 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001032 return &default_pciexp_ops_bus;
1033 case PCI_EXP_TYPE_PCI_BRIDGE:
Uwe Hermanne4870472010-11-04 23:23:47 +00001034 printk(BIOS_DEBUG, "%s subordinate PCI\n",
1035 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001036 return &default_pci_ops_bus;
1037 default:
1038 break;
1039 }
1040 }
1041#endif
1042 return &default_pci_ops_bus;
1043}
1044
1045/**
Vadim Bendebury8049fc92012-04-24 12:53:19 -07001046 * Check if a device id matches a PCI driver entry.
1047 *
1048 * The driver entry can either point at a zero terminated array of acceptable
1049 * device IDs, or include a single device ID.
1050 *
Martin Roth98b698c2015-01-06 21:02:52 -07001051 * @param driver pointer to the PCI driver entry being checked
1052 * @param device_id PCI device ID of the device being matched
Vadim Bendebury8049fc92012-04-24 12:53:19 -07001053 */
1054static int device_id_match(struct pci_driver *driver, unsigned short device_id)
1055{
1056 if (driver->devices) {
1057 unsigned short check_id;
1058 const unsigned short *device_list = driver->devices;
1059 while ((check_id = *device_list++) != 0)
1060 if (check_id == device_id)
1061 return 1;
1062 }
1063
1064 return (driver->device == device_id);
1065}
1066
1067/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001068 * Set up PCI device operation.
1069 *
1070 * Check if it already has a driver. If not, use find_device_operations(),
1071 * or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +00001072 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001073 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +00001074 * @see pci_drivers
1075 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001076static void set_pci_ops(struct device *dev)
1077{
1078 struct pci_driver *driver;
Li-Ta Loe5266692004-03-23 21:28:05 +00001079
Uwe Hermanne4870472010-11-04 23:23:47 +00001080 if (dev->ops)
1081 return;
1082
1083 /*
1084 * Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +00001085 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001086 */
Aaron Durbin03758152015-09-03 17:23:08 -05001087 for (driver = &_pci_drivers[0]; driver != &_epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001088 if ((driver->vendor == dev->vendor) &&
Vadim Bendebury8049fc92012-04-24 12:53:19 -07001089 device_id_match(driver, dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +00001090 dev->ops = (struct device_operations *)driver->ops;
Nico Huber7e3e1ea2020-10-12 16:25:40 +02001091 break;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001092 }
1093 }
Li-Ta Loe5266692004-03-23 21:28:05 +00001094
Nico Huber7e3e1ea2020-10-12 16:25:40 +02001095 if (dev->ops) {
1096 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n", dev_path(dev),
1097 driver->vendor, driver->device, (driver->ops->scan_bus ? "bus " : ""));
1098 return;
1099 }
1100
Uwe Hermanne4870472010-11-04 23:23:47 +00001101 /* If I don't have a specific driver use the default operations. */
1102 switch (dev->hdr_type & 0x7f) { /* Header type */
1103 case PCI_HEADER_TYPE_NORMAL:
Eric Biederman8ca8d762003-04-22 19:02:15 +00001104 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
1105 goto bad;
1106 dev->ops = &default_pci_ops_dev;
1107 break;
1108 case PCI_HEADER_TYPE_BRIDGE:
1109 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
1110 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001111 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001112 break;
Julius Wernercd49cce2019-03-05 16:53:33 -08001113#if CONFIG(CARDBUS_PLUGIN_SUPPORT)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001114 case PCI_HEADER_TYPE_CARDBUS:
1115 dev->ops = &default_cardbus_ops_bus;
1116 break;
1117#endif
Felix Singerc96ee7e2021-01-07 06:14:27 +00001118 default:
Uwe Hermanne4870472010-11-04 23:23:47 +00001119bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +00001120 if (dev->enabled) {
Angel Ponsd19cc112021-07-04 11:41:31 +02001121 printk(BIOS_ERR,
1122 "%s [%04x/%04x/%06x] has unknown header type %02x, ignoring.\n",
1123 dev_path(dev), dev->vendor, dev->device,
Uwe Hermanne4870472010-11-04 23:23:47 +00001124 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +00001125 }
Eric Biederman8ca8d762003-04-22 19:02:15 +00001126 }
Eric Biederman8ca8d762003-04-22 19:02:15 +00001127}
1128
1129/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001130 * See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +00001131 *
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001132 * Given a PCI bus structure and a devfn number, find the device structure
1133 * corresponding to the devfn, if present. Then move the device structure
1134 * as the last child on the bus.
Li-Ta Loe5266692004-03-23 21:28:05 +00001135 *
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001136 * @param bus Pointer to the bus structure.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001137 * @param devfn A device/function number.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001138 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +00001139 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001140 */
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001141static struct device *pci_scan_get_dev(struct bus *bus, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001142{
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001143 struct device *dev, **prev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001144
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001145 prev = &bus->children;
1146 for (dev = bus->children; dev; dev = dev->sibling) {
Duncan Lauriebf696222020-10-18 15:10:00 -07001147 if (dev->path.type == DEVICE_PATH_PCI && dev->path.pci.devfn == devfn) {
1148 /* Unlink from the list. */
1149 *prev = dev->sibling;
1150 dev->sibling = NULL;
1151 break;
Eric Biedermanad1b35a2003-10-14 02:36:51 +00001152 }
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001153 prev = &dev->sibling;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001154 }
Myles Watson29cc9ed2009-07-02 18:56:24 +00001155
Uwe Hermanne4870472010-11-04 23:23:47 +00001156 /*
1157 * Just like alloc_dev() add the device to the list of devices on the
Myles Watson29cc9ed2009-07-02 18:56:24 +00001158 * bus. When the list of devices was formed we removed all of the
1159 * parents children, and now we are interleaving static and dynamic
1160 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001161 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001162 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001163 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +00001164
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001165 /* Find the last child on the bus. */
1166 for (child = bus->children; child && child->sibling;)
Eric Biedermane9a271e32003-09-02 03:36:25 +00001167 child = child->sibling;
Uwe Hermanne4870472010-11-04 23:23:47 +00001168
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001169 /* Place the device as last on the bus. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001170 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +00001171 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001172 else
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001173 bus->children = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001174 }
1175
Eric Biederman8ca8d762003-04-22 19:02:15 +00001176 return dev;
1177}
1178
Myles Watson032a9652009-05-11 22:24:53 +00001179/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001180 * Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +00001181 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001182 * Determine the existence of a given PCI device. Allocate a new struct device
1183 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001184 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001185 * @param dev Pointer to the dev structure.
1186 * @param bus Pointer to the bus structure.
1187 * @param devfn A device/function number to look at.
1188 * @return The device structure for the device (if found), NULL otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001189 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001190struct device *pci_probe_dev(struct device *dev, struct bus *bus,
1191 unsigned int devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001192{
Myles Watson29cc9ed2009-07-02 18:56:24 +00001193 u32 id, class;
1194 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001195
Myles Watson29cc9ed2009-07-02 18:56:24 +00001196 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001197 if (!dev) {
1198 struct device dummy;
Uwe Hermanne4870472010-11-04 23:23:47 +00001199
Myles Watson29cc9ed2009-07-02 18:56:24 +00001200 dummy.bus = bus;
1201 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +00001202 dummy.path.pci.devfn = devfn;
Uwe Hermanne4870472010-11-04 23:23:47 +00001203
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001204 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +00001205 /*
1206 * Have we found something? Some broken boards return 0 if a
1207 * slot is empty, but the expected answer is 0xffffffff.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001208 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001209 if (id == 0xffffffff)
Stefan Reinauer7355c752010-04-02 16:30:25 +00001210 return NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +00001211
Stefan Reinauer7355c752010-04-02 16:30:25 +00001212 if ((id == 0x00000000) || (id == 0x0000ffff) ||
1213 (id == 0xffff0000)) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001214 printk(BIOS_SPEW, "%s, bad id 0x%x\n",
1215 dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001216 return NULL;
1217 }
1218 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001219 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +00001220 /*
1221 * Enable/disable the device. Once we have found the device-
Myles Watson29cc9ed2009-07-02 18:56:24 +00001222 * specific operations this operations we will disable the
1223 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +00001224 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001225 * This is geared toward devices that have subfunctions
1226 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +00001227 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001228 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +00001229 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001230 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001231 /* Run the magic enable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001232 if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001233 dev->chip_ops->enable_dev(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +00001234
Myles Watson29cc9ed2009-07-02 18:56:24 +00001235 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001236 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +00001237
Uwe Hermanne4870472010-11-04 23:23:47 +00001238 /*
1239 * If the device does not have a PCI ID disable it. Possibly
Myles Watson29cc9ed2009-07-02 18:56:24 +00001240 * this is because we have already disabled the device. But
1241 * this also handles optional devices that may not always
1242 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001243 */
1244 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001245 if ((id == 0xffffffff) || (id == 0x00000000) ||
1246 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001247 if (dev->enabled) {
Angel Ponsd19cc112021-07-04 11:41:31 +02001248 printk(BIOS_INFO,
1249 "PCI: Static device %s not found, disabling it.\n",
1250 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001251 dev->enabled = 0;
1252 }
1253 return dev;
1254 }
1255 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001256
Myles Watson29cc9ed2009-07-02 18:56:24 +00001257 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001258 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1259 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +00001260
Myles Watson29cc9ed2009-07-02 18:56:24 +00001261 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001262 dev->vendor = id & 0xffff;
1263 dev->device = (id >> 16) & 0xffff;
1264 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001265
1266 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001267 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +00001268
Myles Watson29cc9ed2009-07-02 18:56:24 +00001269 /* Architectural/System devices always need to be bus masters. */
Felix Singerd3d0fd72020-09-07 16:15:14 +02001270 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM &&
1271 CONFIG(PCI_ALLOW_BUS_MASTER_ANY_DEVICE))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001272 dev->command |= PCI_COMMAND_MASTER;
Uwe Hermanne4870472010-11-04 23:23:47 +00001273
1274 /*
1275 * Look at the vendor and device ID, or at least the header type and
Myles Watson29cc9ed2009-07-02 18:56:24 +00001276 * class and figure out which set of configuration methods to use.
1277 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001278 */
1279 set_pci_ops(dev);
1280
Myles Watson29cc9ed2009-07-02 18:56:24 +00001281 /* Now run the magic enable/disable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001282 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001283 dev->ops->enable(dev);
Myles Watson032a9652009-05-11 22:24:53 +00001284
Myles Watson29cc9ed2009-07-02 18:56:24 +00001285 /* Display the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001286 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n", dev_path(dev),
1287 dev->vendor, dev->device, dev->enabled ? "enabled" : "disabled",
1288 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001289
1290 return dev;
1291}
1292
Myles Watson032a9652009-05-11 22:24:53 +00001293/**
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001294 * Test for match between romstage and ramstage device instance.
1295 *
1296 * @param dev Pointer to the device structure.
1297 * @param sdev Simple device model identifier, created with PCI_DEV().
1298 * @return Non-zero if bus:dev.fn of device matches.
1299 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001300unsigned int pci_match_simple_dev(struct device *dev, pci_devfn_t sdev)
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001301{
1302 return dev->bus->secondary == PCI_DEV2SEGBUS(sdev) &&
1303 dev->path.pci.devfn == PCI_DEV2DEVFN(sdev);
1304}
1305
1306/**
Bill XIE513d3592022-08-02 22:55:51 +08001307 * Test whether a capability is available along the whole path from the given
1308 * device to the host bridge.
1309 *
1310 * @param dev Pointer to the device structure.
1311 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
1312 * @return The next matching capability of the given device, if it is available
1313 * along the whole path, or zero if not.
1314 */
1315uint16_t pci_find_cap_recursive(const struct device *dev, uint16_t cap)
1316{
1317 assert(dev->bus);
1318 uint16_t pos = pci_find_capability(dev, cap);
1319 const struct device *bridge = dev->bus->dev;
1320 while (bridge && (bridge->path.type == DEVICE_PATH_PCI)) {
1321 assert(bridge->bus);
1322 if (!pci_find_capability(bridge, cap))
1323 return 0;
1324 bridge = bridge->bus->dev;
1325 }
1326 return pos;
1327}
1328
1329/**
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -06001330 * PCI devices that are marked as "hidden" do not get probed. However, the same
1331 * initialization logic is still performed as if it were. This is useful when
1332 * devices would like to be described in the devicetree.cb file, and/or present
1333 * static PCI resources to the allocator, but the platform firmware hides the
1334 * device (makes the device invisible to PCI enumeration) before PCI enumeration
1335 * takes place.
1336 *
1337 * The expected semantics of PCI devices marked as 'hidden':
1338 * 1) The device is actually present under the specified BDF
1339 * 2) The device config space can still be accessed somehow, but the Vendor ID
1340 * indicates there is no device there (it reads as 0xffffffff).
1341 * 3) The device may still consume PCI resources. Typically, these would have
1342 * been hardcoded elsewhere.
1343 *
1344 * @param dev Pointer to the device structure.
1345 */
1346static void pci_scan_hidden_device(struct device *dev)
1347{
1348 if (dev->chip_ops && dev->chip_ops->enable_dev)
1349 dev->chip_ops->enable_dev(dev);
1350
1351 /*
1352 * If chip_ops->enable_dev did not set dev->ops, then set to a default
1353 * .ops, because PCI enumeration is effectively being skipped, therefore
1354 * no PCI driver will bind to this device. However, children may want to
1355 * be enumerated, so this provides scan_static_bus for the .scan_bus
1356 * callback.
1357 */
1358 if (dev->ops == NULL)
1359 dev->ops = &default_hidden_pci_ops_dev;
1360
1361 if (dev->ops->enable)
1362 dev->ops->enable(dev);
1363
1364 /* Display the device almost as if it were probed normally */
1365 printk(BIOS_DEBUG, "%s [0000/%04x] hidden%s\n", dev_path(dev),
1366 dev->device, dev->ops ? "" : " No operations");
1367}
1368
1369/**
Jianjun Wang777ffff2021-07-24 14:50:36 +08001370 * A PCIe Downstream Port normally leads to a Link with only Device 0 on it
1371 * (PCIe spec r5.0, sec 7.3.1). As an optimization, scan only for Device 0 in
1372 * that situation.
1373 *
1374 * @param bus Pointer to the bus structure.
1375 */
1376static bool pci_bus_only_one_child(struct bus *bus)
1377{
1378 struct device *bridge = bus->dev;
1379 u16 pcie_pos, pcie_flags_reg;
1380 int pcie_type;
1381
Arthur Heymansdb199cc2022-01-06 20:56:01 +01001382 if (!bridge)
1383 return false;
1384
Nico Huberf514b8a2022-02-25 14:25:57 +01001385 if (bridge->path.type != DEVICE_PATH_PCI)
1386 return false;
1387
Jianjun Wang777ffff2021-07-24 14:50:36 +08001388 pcie_pos = pci_find_capability(bridge, PCI_CAP_ID_PCIE);
1389 if (!pcie_pos)
1390 return false;
1391
1392 pcie_flags_reg = pci_read_config16(bridge, pcie_pos + PCI_EXP_FLAGS);
1393
1394 pcie_type = (pcie_flags_reg & PCI_EXP_FLAGS_TYPE) >> 4;
1395
1396 return pciexp_is_downstream_port(pcie_type);
1397}
1398
1399/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001400 * Scan a PCI bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001401 *
Li-Ta Loe5266692004-03-23 21:28:05 +00001402 * Determine the existence of devices and bridges on a PCI bus. If there are
1403 * bridges on the bus, recursively scan the buses behind the bridges.
1404 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001405 * @param bus Pointer to the bus structure.
1406 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1407 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001408 */
Martin Roth38ddbfb2019-10-23 21:41:00 -06001409void pci_scan_bus(struct bus *bus, unsigned int min_devfn,
1410 unsigned int max_devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001411{
1412 unsigned int devfn;
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001413 struct device *dev, **prev;
1414 int once = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001415
Elyes HAOUASf984aec2021-01-16 17:29:17 +01001416 printk(BIOS_DEBUG, "PCI: %s for bus %02x\n", __func__, bus->secondary);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001417
Uwe Hermanne4870472010-11-04 23:23:47 +00001418 /* Maximum sane devfn is 0xFF. */
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001419 if (max_devfn > 0xff) {
Elyes HAOUASf984aec2021-01-16 17:29:17 +01001420 printk(BIOS_ERR, "PCI: %s limits devfn %x - devfn %x\n",
1421 __func__, min_devfn, max_devfn);
1422 printk(BIOS_ERR, "PCI: %s upper limit too big. Using 0xff.\n", __func__);
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001423 max_devfn=0xff;
1424 }
1425
Martin Roth9a8667a2022-11-03 18:40:10 -06001426 post_code(POST_ENTER_PCI_SCAN_BUS);
Uwe Hermanne4870472010-11-04 23:23:47 +00001427
Jianjun Wang777ffff2021-07-24 14:50:36 +08001428 if (pci_bus_only_one_child(bus))
1429 max_devfn = MIN(max_devfn, 0x07);
1430
Uwe Hermanne4870472010-11-04 23:23:47 +00001431 /*
1432 * Probe all devices/functions on this bus with some optimization for
Myles Watson29cc9ed2009-07-02 18:56:24 +00001433 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001434 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001435 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +00001436 if (CONFIG(MINIMAL_PCI_SCANNING)) {
1437 dev = pcidev_path_behind(bus, devfn);
1438 if (!dev || !dev->mandatory)
1439 continue;
1440 }
1441
Uwe Hermanne4870472010-11-04 23:23:47 +00001442 /* First thing setup the device structure. */
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001443 dev = pci_scan_get_dev(bus, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001444
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -06001445 /* Devices marked 'hidden' do not get probed */
1446 if (dev && dev->hidden) {
1447 pci_scan_hidden_device(dev);
1448
1449 /* Skip pci_probe_dev, go to next devfn */
1450 continue;
1451 }
1452
Myles Watson29cc9ed2009-07-02 18:56:24 +00001453 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001454 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001455
Uwe Hermanne4870472010-11-04 23:23:47 +00001456 /*
1457 * If this is not a multi function device, or the device is
Myles Watson29cc9ed2009-07-02 18:56:24 +00001458 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001459 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001460 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001461 if ((PCI_FUNC(devfn) == 0x00) && (!dev
Myles Watson29cc9ed2009-07-02 18:56:24 +00001462 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001463 devfn += 0x07;
1464 }
1465 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001466
Uwe Hermanne4870472010-11-04 23:23:47 +00001467 /*
Elyes HAOUAS0ce74162021-01-16 14:43:49 +01001468 * Warn if any leftover static devices are found.
Uwe Hermanne4870472010-11-04 23:23:47 +00001469 * There's probably a problem in devicetree.cb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001470 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001471
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001472 prev = &bus->children;
1473 for (dev = bus->children; dev; dev = dev->sibling) {
Duncan Lauriebf696222020-10-18 15:10:00 -07001474
1475 /*
1476 * If static device is not PCI then enable it here and don't
1477 * treat it as a leftover device.
1478 */
1479 if (dev->path.type != DEVICE_PATH_PCI) {
1480 enable_static_device(dev);
1481 continue;
1482 }
1483
Tim Wawrzynczakdbcf7b12020-05-13 16:15:08 -06001484 /*
1485 * The device is only considered leftover if it is not hidden
1486 * and it has a Vendor ID of 0 (the default for a device that
1487 * could not be probed).
1488 */
1489 if (dev->vendor != 0 || dev->hidden) {
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001490 prev = &dev->sibling;
1491 continue;
1492 }
1493
1494 /* Unlink it from list. */
1495 *prev = dev->sibling;
1496
1497 if (!once++)
1498 printk(BIOS_WARNING, "PCI: Leftover static devices:\n");
1499 printk(BIOS_WARNING, "%s\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001500 }
1501
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001502 if (once)
1503 printk(BIOS_WARNING, "PCI: Check your devicetree.cb.\n");
1504
Uwe Hermanne4870472010-11-04 23:23:47 +00001505 /*
1506 * For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001507 * scan the bus behind that child.
1508 */
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001509
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +02001510 scan_bridges(bus);
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001511
Uwe Hermanne4870472010-11-04 23:23:47 +00001512 /*
1513 * We've scanned the bus and so we know all about what's on the other
Myles Watson29cc9ed2009-07-02 18:56:24 +00001514 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001515 * Return how far we've got finding sub-buses.
1516 */
Martin Roth9a8667a2022-11-03 18:40:10 -06001517 post_code(POST_EXIT_PCI_SCAN_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001518}
1519
Kyösti Mälkki33452402015-02-23 06:58:26 +02001520typedef enum {
1521 PCI_ROUTE_CLOSE,
1522 PCI_ROUTE_SCAN,
1523 PCI_ROUTE_FINAL,
1524} scan_state;
1525
1526static void pci_bridge_route(struct bus *link, scan_state state)
1527{
1528 struct device *dev = link->dev;
1529 struct bus *parent = dev->bus;
Arthur Heymansf879d362021-11-10 22:09:58 +01001530 uint8_t primary, secondary, subordinate;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001531
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001532 if (state == PCI_ROUTE_SCAN) {
1533 link->secondary = parent->subordinate + 1;
Jeremy Sollercf2ac542019-10-09 21:40:36 -06001534 link->subordinate = link->secondary + dev->hotplug_buses;
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001535 }
1536
Kyösti Mälkki33452402015-02-23 06:58:26 +02001537 if (state == PCI_ROUTE_CLOSE) {
Arthur Heymansf879d362021-11-10 22:09:58 +01001538 primary = 0;
1539 secondary = 0xff;
1540 subordinate = 0xfe;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001541 } else if (state == PCI_ROUTE_SCAN) {
Arthur Heymansf879d362021-11-10 22:09:58 +01001542 primary = parent->secondary;
1543 secondary = link->secondary;
1544 subordinate = 0xff; /* MAX PCI_BUS number here */
Kyösti Mälkki33452402015-02-23 06:58:26 +02001545 } else if (state == PCI_ROUTE_FINAL) {
Arthur Heymansf879d362021-11-10 22:09:58 +01001546 primary = parent->secondary;
1547 secondary = link->secondary;
1548 subordinate = link->subordinate;
Arthur Heymans4a3331d2022-03-23 17:58:46 +01001549 } else {
1550 return;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001551 }
1552
1553 if (state == PCI_ROUTE_SCAN) {
1554 /* Clear all status bits and turn off memory, I/O and master enables. */
1555 link->bridge_cmd = pci_read_config16(dev, PCI_COMMAND);
1556 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1557 pci_write_config16(dev, PCI_STATUS, 0xffff);
1558 }
1559
1560 /*
1561 * Configure the bus numbers for this bridge: the configuration
1562 * transactions will not be propagated by the bridge if it is not
1563 * correctly configured.
1564 */
Arthur Heymansf879d362021-11-10 22:09:58 +01001565 pci_write_config8(dev, PCI_PRIMARY_BUS, primary);
1566 pci_write_config8(dev, PCI_SECONDARY_BUS, secondary);
1567 pci_write_config8(dev, PCI_SUBORDINATE_BUS, subordinate);
Kyösti Mälkki33452402015-02-23 06:58:26 +02001568
1569 if (state == PCI_ROUTE_FINAL) {
1570 pci_write_config16(dev, PCI_COMMAND, link->bridge_cmd);
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001571 parent->subordinate = link->subordinate;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001572 }
1573}
1574
Li-Ta Loe5266692004-03-23 21:28:05 +00001575/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001576 * Scan a PCI bridge and the buses behind the bridge.
Li-Ta Loe5266692004-03-23 21:28:05 +00001577 *
1578 * Determine the existence of buses behind the bridge. Set up the bridge
1579 * according to the result of the scan.
1580 *
1581 * This function is the default scan_bus() method for PCI bridge devices.
1582 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001583 * @param dev Pointer to the bridge device.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001584 * @param do_scan_bus TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +00001585 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001586void do_pci_scan_bridge(struct device *dev,
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001587 void (*do_scan_bus) (struct bus * bus,
Martin Roth38ddbfb2019-10-23 21:41:00 -06001588 unsigned int min_devfn,
1589 unsigned int max_devfn))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001590{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001591 struct bus *bus;
Eric Biederman83b991a2003-10-11 06:20:25 +00001592
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001593 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
Li-Ta Lo3a812852004-12-03 22:39:34 +00001594
Myles Watson894a3472010-06-09 22:41:35 +00001595 if (dev->link_list == NULL) {
1596 struct bus *link;
1597 link = malloc(sizeof(*link));
1598 if (link == NULL)
1599 die("Couldn't allocate a link!\n");
1600 memset(link, 0, sizeof(*link));
1601 link->dev = dev;
1602 dev->link_list = link;
1603 }
1604
1605 bus = dev->link_list;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001606
Nico Huber061b9052019-09-21 15:58:23 +02001607 pci_bridge_vga_compat(bus);
1608
Kyösti Mälkki33452402015-02-23 06:58:26 +02001609 pci_bridge_route(bus, PCI_ROUTE_SCAN);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001610
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001611 do_scan_bus(bus, 0x00, 0xff);
Kyösti Mälkki33452402015-02-23 06:58:26 +02001612
1613 pci_bridge_route(bus, PCI_ROUTE_FINAL);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001614}
Li-Ta Loe5266692004-03-23 21:28:05 +00001615
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001616/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001617 * Scan a PCI bridge and the buses behind the bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001618 *
1619 * Determine the existence of buses behind the bridge. Set up the bridge
1620 * according to the result of the scan.
1621 *
1622 * This function is the default scan_bus() method for PCI bridge devices.
1623 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001624 * @param dev Pointer to the bridge device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001625 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001626void pci_scan_bridge(struct device *dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001627{
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001628 do_pci_scan_bridge(dev, pci_scan_bus);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001629}
1630
Myles Watson29cc9ed2009-07-02 18:56:24 +00001631/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001632 * Scan a PCI domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001633 *
1634 * This function is the default scan_bus() method for PCI domains.
1635 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001636 * @param dev Pointer to the domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001637 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001638void pci_domain_scan_bus(struct device *dev)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001639{
Kyösti Mälkki6f370172015-03-19 15:26:52 +02001640 struct bus *link = dev->link_list;
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001641 pci_scan_bus(link, PCI_DEVFN(0, 0), 0xff);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001642}
1643
Angel Ponsb6519812021-12-31 13:33:50 +01001644void pci_dev_disable_bus_master(const struct device *dev)
1645{
1646 pci_update_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MASTER, 0x0);
1647}
1648
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001649/**
1650 * Take an INT_PIN number (0, 1 - 4) and convert
1651 * it to a string ("NO PIN", "PIN A" - "PIN D")
1652 *
1653 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1654 * @return A string corresponding to the pin number or "Invalid"
1655 */
1656const char *pin_to_str(int pin)
1657{
1658 const char *str[5] = {
1659 "NO PIN",
1660 "PIN A",
1661 "PIN B",
1662 "PIN C",
1663 "PIN D",
1664 };
1665
1666 if (pin >= 0 && pin <= 4)
1667 return str[pin];
1668 else
1669 return "Invalid PIN, not 0 - 4";
1670}
1671
1672/**
1673 * Get the PCI INT_PIN swizzle for a device defined as:
1674 * pin_parent = (pin_child + devn_child) % 4 + 1
1675 * where PIN A = 1 ... PIN_D = 4
1676 *
1677 * Given a PCI device structure 'dev', find the interrupt pin
1678 * that will be triggered on its parent bridge device when
1679 * generating an interrupt. For example: Device 1:3.2 may
1680 * use INT_PIN A but will trigger PIN D on its parent bridge
1681 * device. In this case, this function will return 4 (PIN D).
1682 *
1683 * @param dev A PCI device structure to swizzle interrupt pins for
Martin Roth32bc6b62015-01-04 16:54:35 -07001684 * @param *parent_bridge The PCI device structure for the bridge
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001685 * device 'dev' is attached to
1686 * @return The interrupt pin number (1 - 4) that 'dev' will
1687 * trigger when generating an interrupt
1688 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001689static int swizzle_irq_pins(struct device *dev, struct device **parent_bridge)
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001690{
Aaron Durbinc30d9132017-08-07 16:55:43 -06001691 struct device *parent; /* Our current device's parent device */
1692 struct device *child; /* The child device of the parent */
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001693 uint8_t parent_bus = 0; /* Parent Bus number */
1694 uint16_t parent_devfn = 0; /* Parent Device and Function number */
1695 uint16_t child_devfn = 0; /* Child Device and Function number */
1696 uint8_t swizzled_pin = 0; /* Pin swizzled across a bridge */
1697
1698 /* Start with PIN A = 0 ... D = 3 */
1699 swizzled_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN) - 1;
1700
1701 /* While our current device has parent devices */
1702 child = dev;
1703 for (parent = child->bus->dev; parent; parent = parent->bus->dev) {
1704 parent_bus = parent->bus->secondary;
1705 parent_devfn = parent->path.pci.devfn;
1706 child_devfn = child->path.pci.devfn;
1707
1708 /* Swizzle the INT_PIN for any bridges not on root bus */
1709 swizzled_pin = (PCI_SLOT(child_devfn) + swizzled_pin) % 4;
1710 printk(BIOS_SPEW, "\tWith INT_PIN swizzled to %s\n"
1711 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1712 pin_to_str(swizzled_pin + 1), parent_bus,
1713 PCI_SLOT(parent_devfn), PCI_FUNC(parent_devfn));
1714
1715 /* Continue until we find the root bus */
1716 if (parent_bus > 0) {
1717 /*
1718 * We will go on to the next parent so this parent
1719 * becomes the child
1720 */
1721 child = parent;
1722 continue;
1723 } else {
1724 /*
1725 * Found the root bridge device,
1726 * fill in the structure and exit
1727 */
1728 *parent_bridge = parent;
1729 break;
1730 }
1731 }
1732
1733 /* End with PIN A = 1 ... D = 4 */
1734 return swizzled_pin + 1;
1735}
1736
1737/**
1738 * Given a device structure 'dev', find its interrupt pin
1739 * and its parent bridge 'parent_bdg' device structure.
1740 * If it is behind a bridge, it will return the interrupt
1741 * pin number (1 - 4) of the parent bridge that the device
1742 * interrupt pin has been swizzled to, otherwise it will
1743 * return the interrupt pin that is programmed into the
1744 * PCI config space of the target device. If 'dev' is
1745 * behind a bridge, it will fill in 'parent_bdg' with the
1746 * device structure of the bridge it is behind, otherwise
1747 * it will copy 'dev' into 'parent_bdg'.
1748 *
1749 * @param dev A PCI device structure to get interrupt pins for.
1750 * @param *parent_bdg The PCI device structure for the bridge
1751 * device 'dev' is attached to.
1752 * @return The interrupt pin number (1 - 4) that 'dev' will
1753 * trigger when generating an interrupt.
1754 * Errors: -1 is returned if the device is not enabled
1755 * -2 is returned if a parent bridge could not be found.
1756 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001757int get_pci_irq_pins(struct device *dev, struct device **parent_bdg)
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001758{
1759 uint8_t bus = 0; /* The bus this device is on */
1760 uint16_t devfn = 0; /* This device's device and function numbers */
1761 uint8_t int_pin = 0; /* Interrupt pin used by the device */
1762 uint8_t target_pin = 0; /* Interrupt pin we want to assign an IRQ to */
1763
1764 /* Make sure this device is enabled */
1765 if (!(dev->enabled && (dev->path.type == DEVICE_PATH_PCI)))
1766 return -1;
1767
1768 bus = dev->bus->secondary;
1769 devfn = dev->path.pci.devfn;
1770
1771 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1772 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1773 if (int_pin < 1 || int_pin > 4)
1774 return -1;
1775
1776 printk(BIOS_SPEW, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1777 bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pin_to_str(int_pin));
1778
1779 /* If this device is on a bridge, swizzle its INT_PIN */
1780 if (bus) {
1781 /* Swizzle its INT_PINs */
1782 target_pin = swizzle_irq_pins(dev, parent_bdg);
1783
1784 /* Make sure the swizzle returned valid structures */
1785 if (parent_bdg == NULL) {
Julius Wernere9665952022-01-21 17:06:20 -08001786 printk(BIOS_WARNING, "Could not find parent bridge for this device!\n");
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001787 return -2;
1788 }
1789 } else { /* Device is not behind a bridge */
1790 target_pin = int_pin; /* Return its own interrupt pin */
1791 *parent_bdg = dev; /* Return its own structure */
1792 }
1793
1794 /* Target pin is the interrupt pin we want to assign an IRQ to */
1795 return target_pin;
1796}
1797
Julius Wernercd49cce2019-03-05 16:53:33 -08001798#if CONFIG(PC80_SYSTEM)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001799/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001800 * Assign IRQ numbers.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001801 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001802 * This function assigns IRQs for all functions contained within the indicated
Uwe Hermanne4870472010-11-04 23:23:47 +00001803 * device address. If the device does not exist or does not require interrupts
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001804 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001805 *
1806 * This function should be called for each PCI slot in your system.
1807 *
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001808 * @param dev Pointer to dev structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001809 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1810 * of this slot. The particular IRQ #s that are passed in depend on the
1811 * routing inside your southbridge and on your board.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001812 */
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001813void pci_assign_irqs(struct device *dev, const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001814{
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001815 u8 slot, line, irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001816
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001817 /* Each device may contain up to eight functions. */
1818 slot = dev->path.pci.devfn >> 3;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001819
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001820 for (; dev ; dev = dev->sibling) {
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001821
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001822 if (dev->path.pci.devfn >> 3 != slot)
1823 break;
1824
1825 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001826
Uwe Hermanne4870472010-11-04 23:23:47 +00001827 /* PCI spec says all values except 1..4 are reserved. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001828 if ((line < 1) || (line > 4))
1829 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001830
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001831 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001832
Kyösti Mälkkic19d6a62019-07-04 21:39:28 +03001833 printk(BIOS_DEBUG, "Assigning IRQ %d to %s\n", irq, dev_path(dev));
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001834
Angel Ponsceca5de2021-06-28 11:59:33 +02001835 pci_write_config8(dev, PCI_INTERRUPT_LINE, irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001836
Uwe Hermanne4870472010-11-04 23:23:47 +00001837 /* Change to level triggered. */
Angel Ponsceca5de2021-06-28 11:59:33 +02001838 i8259_configure_irq_trigger(irq, IRQ_LEVEL_TRIGGERED);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001839 }
1840}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001841#endif