blob: b275788157945c5138649de4990a3392d2b706ea [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
4 * It was originally based on the Linux kernel (drivers/pci/pci.c).
Martin Rothbb5953d2016-04-11 20:53:39 -06005 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
6 * David Mosberger-Tang
Uwe Hermannb80dbf02007-04-22 19:08:13 +00007 *
Martin Rothbb5953d2016-04-11 20:53:39 -06008 * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
9 *
Uwe Hermannb80dbf02007-04-22 19:08:13 +000010 * Copyright (C) 2003-2004 Linux Networx
11 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
12 * Copyright (C) 2003-2006 Ronald G. Minnich <rminnich@gmail.com>
13 * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
14 * Copyright (C) 2005-2006 Tyan
15 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
Patrick Georgi16cdbb22009-04-21 20:14:31 +000016 * Copyright (C) 2005-2009 coresystems GmbH
17 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
Mike Loptien0f5cf5e2014-05-12 21:46:31 -060018 * Copyright (C) 2014 Sage Electronic Engineering, LLC.
Martin Rothbb5953d2016-04-11 20:53:39 -060019 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; version 2 of the License.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
Uwe Hermannb80dbf02007-04-22 19:08:13 +000028 */
29
30/*
Myles Watson29cc9ed2009-07-02 18:56:24 +000031 * PCI Bus Services, see include/linux/pci.h for further explanation.
Eric Biederman8ca8d762003-04-22 19:02:15 +000032 */
33
Edward O'Callaghan6c992502014-06-20 21:19:06 +100034#include <arch/acpi.h>
35#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020036#include <device/pci_ops.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100037#include <bootmode.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000038#include <console/console.h>
39#include <stdlib.h>
40#include <stdint.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000041#include <string.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100042#include <delay.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100043#include <device/cardbus.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000044#include <device/device.h>
45#include <device/pci.h>
46#include <device/pci_ids.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000047#include <device/pcix.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000048#include <device/pciexp.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100049#include <device/hypertransport.h>
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000050#include <pc80/i8259.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020051#include <security/vboot/vbnv.h>
Martin Roth5dd4a2a2018-03-06 16:10:45 -070052#include <timestamp.h>
Eric Biederman03acab62004-10-14 21:25:53 +000053
Myles Watson29cc9ed2009-07-02 18:56:24 +000054u8 pci_moving_config8(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000055{
Myles Watson29cc9ed2009-07-02 18:56:24 +000056 u8 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000057
Eric Biederman03acab62004-10-14 21:25:53 +000058 value = pci_read_config8(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000059
Eric Biederman03acab62004-10-14 21:25:53 +000060 pci_write_config8(dev, reg, 0xff);
61 ones = pci_read_config8(dev, reg);
62
63 pci_write_config8(dev, reg, 0x00);
64 zeroes = pci_read_config8(dev, reg);
65
66 pci_write_config8(dev, reg, value);
67
68 return ones ^ zeroes;
69}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000070
Uwe Hermanne4870472010-11-04 23:23:47 +000071u16 pci_moving_config16(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000072{
Myles Watson29cc9ed2009-07-02 18:56:24 +000073 u16 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000074
Eric Biederman03acab62004-10-14 21:25:53 +000075 value = pci_read_config16(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000076
Eric Biederman03acab62004-10-14 21:25:53 +000077 pci_write_config16(dev, reg, 0xffff);
78 ones = pci_read_config16(dev, reg);
79
80 pci_write_config16(dev, reg, 0x0000);
81 zeroes = pci_read_config16(dev, reg);
82
83 pci_write_config16(dev, reg, value);
84
85 return ones ^ zeroes;
86}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000087
Uwe Hermanne4870472010-11-04 23:23:47 +000088u32 pci_moving_config32(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000089{
Myles Watson29cc9ed2009-07-02 18:56:24 +000090 u32 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000091
Eric Biederman03acab62004-10-14 21:25:53 +000092 value = pci_read_config32(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000093
Eric Biederman03acab62004-10-14 21:25:53 +000094 pci_write_config32(dev, reg, 0xffffffff);
95 ones = pci_read_config32(dev, reg);
96
97 pci_write_config32(dev, reg, 0x00000000);
98 zeroes = pci_read_config32(dev, reg);
99
100 pci_write_config32(dev, reg, value);
101
102 return ones ^ zeroes;
103}
104
Myles Watson29cc9ed2009-07-02 18:56:24 +0000105/**
106 * Given a device, a capability type, and a last position, return the next
107 * matching capability. Always start at the head of the list.
108 *
109 * @param dev Pointer to the device structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000110 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000111 * @param last Location of the PCI capability register to start from.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000112 * @return The next matching capability.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000113 */
114unsigned pci_find_next_capability(struct device *dev, unsigned cap,
115 unsigned last)
Eric Biederman03acab62004-10-14 21:25:53 +0000116{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000117 unsigned pos = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000118 u16 status;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000119 unsigned reps = 48;
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000120
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000121 status = pci_read_config16(dev, PCI_STATUS);
Uwe Hermanne4870472010-11-04 23:23:47 +0000122 if (!(status & PCI_STATUS_CAP_LIST))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000123 return 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000124
Myles Watson29cc9ed2009-07-02 18:56:24 +0000125 switch (dev->hdr_type & 0x7f) {
Eric Biederman03acab62004-10-14 21:25:53 +0000126 case PCI_HEADER_TYPE_NORMAL:
127 case PCI_HEADER_TYPE_BRIDGE:
128 pos = PCI_CAPABILITY_LIST;
129 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000130 case PCI_HEADER_TYPE_CARDBUS:
131 pos = PCI_CB_CAPABILITY_LIST;
132 break;
133 default:
134 return 0;
Eric Biederman03acab62004-10-14 21:25:53 +0000135 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000136
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000137 pos = pci_read_config8(dev, pos);
Uwe Hermanne4870472010-11-04 23:23:47 +0000138 while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */
Eric Biederman03acab62004-10-14 21:25:53 +0000139 int this_cap;
Uwe Hermanne4870472010-11-04 23:23:47 +0000140
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000141 pos &= ~3;
Eric Biederman03acab62004-10-14 21:25:53 +0000142 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +0000143 printk(BIOS_SPEW, "Capability: type 0x%02x @ 0x%02x\n",
144 this_cap, pos);
145 if (this_cap == 0xff)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000146 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000147
148 if (!last && (this_cap == cap))
Eric Biederman03acab62004-10-14 21:25:53 +0000149 return pos;
Uwe Hermanne4870472010-11-04 23:23:47 +0000150
151 if (last == pos)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000152 last = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000153
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000154 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman03acab62004-10-14 21:25:53 +0000155 }
156 return 0;
157}
158
Myles Watson29cc9ed2009-07-02 18:56:24 +0000159/**
160 * Given a device, and a capability type, return the next matching
161 * capability. Always start at the head of the list.
162 *
163 * @param dev Pointer to the device structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000164 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
165 * @return The next matching capability.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000166 */
Aaron Durbinc30d9132017-08-07 16:55:43 -0600167unsigned int pci_find_capability(struct device *dev, unsigned int cap)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000168{
169 return pci_find_next_capability(dev, cap, 0);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000170}
171
Myles Watson29cc9ed2009-07-02 18:56:24 +0000172/**
173 * Given a device and register, read the size of the BAR for that register.
174 *
175 * @param dev Pointer to the device structure.
176 * @param index Address of the PCI configuration register.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000177 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000178 */
Eric Biederman03acab62004-10-14 21:25:53 +0000179struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000180{
Eric Biederman5cd81732004-03-11 15:01:31 +0000181 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000182 unsigned long value, attr;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000183 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184
Myles Watson29cc9ed2009-07-02 18:56:24 +0000185 /* Initialize the resources to nothing. */
Eric Biederman03acab62004-10-14 21:25:53 +0000186 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000187
Myles Watson29cc9ed2009-07-02 18:56:24 +0000188 /* Get the initial value. */
Eric Biederman03acab62004-10-14 21:25:53 +0000189 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000190
Myles Watson29cc9ed2009-07-02 18:56:24 +0000191 /* See which bits move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000192 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000193
Myles Watson29cc9ed2009-07-02 18:56:24 +0000194 /* Initialize attr to the bits that do not move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000195 attr = value & ~moving;
196
Myles Watson29cc9ed2009-07-02 18:56:24 +0000197 /* If it is a 64bit resource look at the high half as well. */
Eric Biederman03acab62004-10-14 21:25:53 +0000198 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000199 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
200 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
201 /* Find the high bits that move. */
202 moving |=
203 ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000204 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000205
Myles Watson032a9652009-05-11 22:24:53 +0000206 /* Find the resource constraints.
Eric Biederman03acab62004-10-14 21:25:53 +0000207 * Start by finding the bits that move. From there:
208 * - Size is the least significant bit of the bits that move.
209 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000210 * See PCI Spec 6.2.5.1.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000211 */
Eric Biederman03acab62004-10-14 21:25:53 +0000212 limit = 0;
213 if (moving) {
214 resource->size = 1;
215 resource->align = resource->gran = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000216 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000217 resource->size <<= 1;
218 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000219 resource->gran += 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000220 }
221 resource->limit = limit = moving | (resource->size - 1);
Nico Huber8193b062015-10-21 15:43:41 +0200222
223 if (pci_base_address_is_memory_space(attr)) {
224 /* Page-align to allow individual mapping of devices. */
225 if (resource->align < 12)
226 resource->align = 12;
227 }
Eric Biederman03acab62004-10-14 21:25:53 +0000228 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000229
Uwe Hermanne4870472010-11-04 23:23:47 +0000230 /*
231 * Some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000232 * really size correctly.
Uwe Hermanne4870472010-11-04 23:23:47 +0000233 *
234 * Example: the Acer M7229 has BARs 1-4 normally read-only,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000235 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
Uwe Hermanne4870472010-11-04 23:23:47 +0000236 * by writing 0xffffffff to it, it will read back as 0x1f1 -- which
237 * is a violation of the spec.
238 *
239 * We catch this case and ignore it by observing which bits move.
240 *
241 * This also catches the common case of unimplemented registers
Eric Biederman03acab62004-10-14 21:25:53 +0000242 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000243 */
Eric Biederman03acab62004-10-14 21:25:53 +0000244 if (moving == 0) {
245 if (value != 0) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000246 printk(BIOS_DEBUG, "%s register %02lx(%08lx), "
247 "read-only ignoring it\n",
248 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000249 }
250 resource->flags = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000251 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
252 /* An I/O mapped base address. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000253 resource->flags |= IORESOURCE_IO;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000254 /* I don't want to deal with 32bit I/O resources. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000255 resource->limit = 0xffff;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000256 } else {
257 /* A Memory mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000258 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000259 resource->flags |= IORESOURCE_MEM;
Uwe Hermanne4870472010-11-04 23:23:47 +0000260 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000261 resource->flags |= IORESOURCE_PREFETCH;
Eric Biederman03acab62004-10-14 21:25:53 +0000262 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
263 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000264 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000265 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000266 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
267 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000268 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000269 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
270 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000271 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000272 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000273 } else {
274 /* Invalid value. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000275 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
276 printk(BIOS_ERR, " on dev %s at index %02lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000277 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000278 resource->flags = 0;
279 }
280 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000281
Myles Watson29cc9ed2009-07-02 18:56:24 +0000282 /* Don't let the limit exceed which bits can move. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000283 if (resource->limit > limit)
Eric Biederman03acab62004-10-14 21:25:53 +0000284 resource->limit = limit;
Eric Biederman03acab62004-10-14 21:25:53 +0000285
Eric Biederman5cd81732004-03-11 15:01:31 +0000286 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000287}
288
Myles Watson29cc9ed2009-07-02 18:56:24 +0000289/**
290 * Given a device and an index, read the size of the BAR for that register.
291 *
292 * @param dev Pointer to the device structure.
293 * @param index Address of the PCI configuration register.
294 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000295static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000296{
297 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000298 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000299 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000300
Myles Watson29cc9ed2009-07-02 18:56:24 +0000301 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000302 resource = new_resource(dev, index);
303
Myles Watson29cc9ed2009-07-02 18:56:24 +0000304 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000305 value = pci_read_config32(dev, index);
306
Myles Watson29cc9ed2009-07-02 18:56:24 +0000307 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000308 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000309
310 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000311 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000312
Myles Watson032a9652009-05-11 22:24:53 +0000313 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000314 * Start by finding the bits that move. From there:
315 * - Size is the least significant bit of the bits that move.
316 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000317 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000318 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000319 if (moving) {
320 resource->size = 1;
321 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000322 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000323 resource->size <<= 1;
324 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000325 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000326 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000327 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000328 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
329 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000330 if (value != 0) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000331 printk(BIOS_DEBUG, "%s register %02lx(%08lx), "
332 "read-only ignoring it\n",
333 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000334 }
335 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000336 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000337 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000338}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000339
Myles Watson29cc9ed2009-07-02 18:56:24 +0000340/**
Patrick Rudolph4e2f95b2018-05-16 14:56:22 +0200341 * Given a device, read the size of the MSI-X table.
342 *
343 * @param dev Pointer to the device structure.
344 * @return MSI-X table size or 0 if not MSI-X capable device
345 */
346size_t pci_msix_table_size(struct device *dev)
347{
348 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
349 if (!pos)
350 return 0;
351
352 const u16 control = pci_read_config16(dev, pos + PCI_MSIX_FLAGS);
353 return (control & PCI_MSIX_FLAGS_QSIZE) + 1;
354}
355
356/**
357 * Given a device, return the table offset and bar the MSI-X tables resides in.
358 *
359 * @param dev Pointer to the device structure.
360 * @param offset Returned value gives the offset in bytes inside the PCI BAR.
361 * @param idx The returned value is the index of the PCI_BASE_ADDRESS register
362 * the MSI-X table is located in.
363 * @return Zero on success
364 */
365int pci_msix_table_bar(struct device *dev, u32 *offset, u8 *idx)
366{
367 const size_t pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
368 if (!pos || !offset || !idx)
369 return 1;
370
371 *offset = pci_read_config32(dev, pos + PCI_MSIX_TABLE);
372 *idx = (u8)(*offset & PCI_MSIX_PBA_BIR);
373 *offset &= PCI_MSIX_PBA_OFFSET;
374
375 return 0;
376}
377
378/**
379 * Given a device, return a msix_entry pointer or NULL if no table was found.
380 *
381 * @param dev Pointer to the device structure.
382 *
383 * @return NULL on error
384 */
385struct msix_entry *pci_msix_get_table(struct device *dev)
386{
387 struct resource *res;
388 u32 offset;
389 u8 idx;
390
391 if (pci_msix_table_bar(dev, &offset, &idx))
392 return NULL;
393
394 if (idx > 5)
395 return NULL;
396
397 res = probe_resource(dev, idx * 4 + PCI_BASE_ADDRESS_0);
398 if (!res || !res->base || offset >= res->size)
399 return NULL;
400
401 if ((res->flags & IORESOURCE_PCI64) &&
402 (uintptr_t)res->base != res->base)
403 return NULL;
404
405 return (struct msix_entry *)((uintptr_t)res->base + offset);
406}
407
408/**
Myles Watson29cc9ed2009-07-02 18:56:24 +0000409 * Read the base address registers for a given device.
410 *
411 * @param dev Pointer to the dev structure.
412 * @param howmany How many registers to read (6 for device, 2 for bridge).
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000413 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000414static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000415{
416 unsigned long index;
417
Myles Watson29cc9ed2009-07-02 18:56:24 +0000418 for (index = PCI_BASE_ADDRESS_0;
419 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000420 struct resource *resource;
421 resource = pci_get_resource(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000422 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000423 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000424
425 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000426}
427
Myles Watson29cc9ed2009-07-02 18:56:24 +0000428static void pci_record_bridge_resource(struct device *dev, resource_t moving,
429 unsigned index, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000430{
Eric Biederman03acab62004-10-14 21:25:53 +0000431 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000432 unsigned long gran;
433 resource_t step;
434
Myles Watson29cc9ed2009-07-02 18:56:24 +0000435 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000436
437 if (!moving)
438 return;
439
440 /* Initialize the constraints on the current bus. */
441 resource = new_resource(dev, index);
442 resource->size = 0;
443 gran = 0;
444 step = 1;
445 while ((moving & step) == 0) {
446 gran += 1;
447 step <<= 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000448 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000449 resource->gran = gran;
450 resource->align = gran;
451 resource->limit = moving | (step - 1);
452 resource->flags = type | IORESOURCE_PCI_BRIDGE |
453 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000454}
455
Eric Biederman8ca8d762003-04-22 19:02:15 +0000456static void pci_bridge_read_bases(struct device *dev)
457{
Eric Biederman03acab62004-10-14 21:25:53 +0000458 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000459
Myles Watson29cc9ed2009-07-02 18:56:24 +0000460 /* See if the bridge I/O resources are implemented. */
461 moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
462 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000463 ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000464
Myles Watson29cc9ed2009-07-02 18:56:24 +0000465 moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
466 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000467 ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000468
469 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000470
Myles Watson29cc9ed2009-07-02 18:56:24 +0000471 /* Initialize the I/O space constraints on the current bus. */
472 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000473
Myles Watson29cc9ed2009-07-02 18:56:24 +0000474 /* See if the bridge prefmem resources are implemented. */
475 moving_base =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000476 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000477 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000478 ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000479
Myles Watson29cc9ed2009-07-02 18:56:24 +0000480 moving_limit =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000481 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000482 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000483 ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
Myles Watson032a9652009-05-11 22:24:53 +0000484
Eric Biederman03acab62004-10-14 21:25:53 +0000485 moving = moving_base & moving_limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000486 /* Initialize the prefetchable memory constraints on the current bus. */
487 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
488 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Myles Watson032a9652009-05-11 22:24:53 +0000489
Myles Watson29cc9ed2009-07-02 18:56:24 +0000490 /* See if the bridge mem resources are implemented. */
491 moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
492 moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000493
494 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000495
Myles Watson29cc9ed2009-07-02 18:56:24 +0000496 /* Initialize the memory resources on the current bus. */
497 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
498 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000499
Eric Biederman5cd81732004-03-11 15:01:31 +0000500 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000501}
502
Eric Biederman5899fd82003-04-24 06:25:08 +0000503void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000504{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000505 pci_read_bases(dev, 6);
506 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000507}
508
Eric Biederman5899fd82003-04-24 06:25:08 +0000509void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000510{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000511 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000512 pci_read_bases(dev, 2);
513 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000514}
515
Myles Watson29cc9ed2009-07-02 18:56:24 +0000516void pci_domain_read_resources(struct device *dev)
517{
518 struct resource *res;
519
520 /* Initialize the system-wide I/O space constraints. */
521 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
522 res->limit = 0xffffUL;
523 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
524 IORESOURCE_ASSIGNED;
525
526 /* Initialize the system-wide memory resources constraints. */
527 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
528 res->limit = 0xffffffffULL;
529 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
530 IORESOURCE_ASSIGNED;
531}
532
Eric Biederman8ca8d762003-04-22 19:02:15 +0000533static void pci_set_resource(struct device *dev, struct resource *resource)
534{
Eric Biederman03acab62004-10-14 21:25:53 +0000535 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000536
Myles Watson29cc9ed2009-07-02 18:56:24 +0000537 /* Make certain the resource has actually been assigned a value. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000538 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000539 printk(BIOS_ERR, "ERROR: %s %02lx %s size: 0x%010llx not "
540 "assigned\n", dev_path(dev), resource->index,
541 resource_type(resource), resource->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000542 return;
543 }
544
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000545 /* If this resource is fixed don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000546 if (resource->flags & IORESOURCE_FIXED)
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000547 return;
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000548
Myles Watson29cc9ed2009-07-02 18:56:24 +0000549 /* If I have already stored this resource don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000550 if (resource->flags & IORESOURCE_STORED)
Eric Biederman5cd81732004-03-11 15:01:31 +0000551 return;
Eric Biederman5cd81732004-03-11 15:01:31 +0000552
Myles Watson29cc9ed2009-07-02 18:56:24 +0000553 /* If the resource is subtractive don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000554 if (resource->flags & IORESOURCE_SUBTRACTIVE)
Eric Biederman03acab62004-10-14 21:25:53 +0000555 return;
Eric Biederman03acab62004-10-14 21:25:53 +0000556
Myles Watson29cc9ed2009-07-02 18:56:24 +0000557 /* Only handle PCI memory and I/O resources for now. */
558 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000559 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000560
Myles Watson29cc9ed2009-07-02 18:56:24 +0000561 /* Enable the resources in the command register. */
Eric Biederman03acab62004-10-14 21:25:53 +0000562 if (resource->size) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000563 if (resource->flags & IORESOURCE_MEM)
Eric Biederman03acab62004-10-14 21:25:53 +0000564 dev->command |= PCI_COMMAND_MEMORY;
Uwe Hermanne4870472010-11-04 23:23:47 +0000565 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000566 dev->command |= PCI_COMMAND_IO;
Uwe Hermanne4870472010-11-04 23:23:47 +0000567 if (resource->flags & IORESOURCE_PCI_BRIDGE)
Eric Biederman03acab62004-10-14 21:25:53 +0000568 dev->command |= PCI_COMMAND_MASTER;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000569 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000570
Myles Watson29cc9ed2009-07-02 18:56:24 +0000571 /* Get the base address. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000572 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000573
Myles Watson29cc9ed2009-07-02 18:56:24 +0000574 /* Get the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000575 end = resource_end(resource);
Myles Watson032a9652009-05-11 22:24:53 +0000576
Myles Watson29cc9ed2009-07-02 18:56:24 +0000577 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000578 resource->flags |= IORESOURCE_STORED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000579
Uwe Hermanne4870472010-11-04 23:23:47 +0000580 /*
581 * PCI bridges have no enable bit. They are disabled if the base of
582 * the range is greater than the limit. If the size is zero, disable
Myles Watson29cc9ed2009-07-02 18:56:24 +0000583 * by setting the base = limit and end = limit - 2^gran.
584 */
585 if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
586 base = resource->limit;
587 end = resource->limit - (1 << resource->gran);
588 resource->base = base;
589 }
590
Eric Biederman8ca8d762003-04-22 19:02:15 +0000591 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000592 unsigned long base_lo, base_hi;
Uwe Hermanne4870472010-11-04 23:23:47 +0000593
594 /*
595 * Some chipsets allow us to set/clear the I/O bit
596 * (e.g. VIA 82C686A). So set it to be safe.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000597 */
Eric Biederman03acab62004-10-14 21:25:53 +0000598 base_lo = base & 0xffffffff;
599 base_hi = (base >> 32) & 0xffffffff;
Uwe Hermanne4870472010-11-04 23:23:47 +0000600 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000601 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman03acab62004-10-14 21:25:53 +0000602 pci_write_config32(dev, resource->index, base_lo);
Uwe Hermanne4870472010-11-04 23:23:47 +0000603 if (resource->flags & IORESOURCE_PCI64)
Eric Biederman03acab62004-10-14 21:25:53 +0000604 pci_write_config32(dev, resource->index + 4, base_hi);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000605 } else if (resource->index == PCI_IO_BASE) {
606 /* Set the I/O ranges. */
607 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000608 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000609 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000610 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000611 } else if (resource->index == PCI_MEMORY_BASE) {
612 /* Set the memory range. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000613 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000614 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000615 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
616 /* Set the prefetchable memory range. */
Eric Biederman03acab62004-10-14 21:25:53 +0000617 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
618 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
619 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
620 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000621 } else {
622 /* Don't let me think I stored the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000623 resource->flags &= ~IORESOURCE_STORED;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000624 printk(BIOS_ERR, "ERROR: invalid resource->index %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000625 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000626 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000627
Eric Biederman03acab62004-10-14 21:25:53 +0000628 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000629}
630
Eric Biederman5899fd82003-04-24 06:25:08 +0000631void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000632{
Myles Watsonc25cc112010-05-21 14:33:48 +0000633 struct resource *res;
Myles Watson894a3472010-06-09 22:41:35 +0000634 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000635 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000636
Uwe Hermanne4870472010-11-04 23:23:47 +0000637 for (res = dev->resource_list; res; res = res->next)
Myles Watsonc25cc112010-05-21 14:33:48 +0000638 pci_set_resource(dev, res);
Uwe Hermanne4870472010-11-04 23:23:47 +0000639
Myles Watson894a3472010-06-09 22:41:35 +0000640 for (bus = dev->link_list; bus; bus = bus->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000641 if (bus->children)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000642 assign_resources(bus);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000643 }
644
Myles Watson29cc9ed2009-07-02 18:56:24 +0000645 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000646 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000647
Myles Watson29cc9ed2009-07-02 18:56:24 +0000648 /* Set a default secondary latency timer. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000649 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000650 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000651
Myles Watson29cc9ed2009-07-02 18:56:24 +0000652 /* Zero the IRQ settings. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000653 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Uwe Hermanne4870472010-11-04 23:23:47 +0000654 if (line)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000655 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Uwe Hermanne4870472010-11-04 23:23:47 +0000656
Myles Watson29cc9ed2009-07-02 18:56:24 +0000657 /* Set the cache line size, so far 64 bytes is good for everyone. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000658 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000659}
660
Eric Biedermane9a271e32003-09-02 03:36:25 +0000661void pci_dev_enable_resources(struct device *dev)
662{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000663 const struct pci_operations *ops;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000664 u16 command;
Eric Biederman03acab62004-10-14 21:25:53 +0000665
Uwe Hermanne4870472010-11-04 23:23:47 +0000666 /* Set the subsystem vendor and device ID for mainboard devices. */
Eric Biederman03acab62004-10-14 21:25:53 +0000667 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000668 if (dev->on_mainboard && ops && ops->set_subsystem) {
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700669 if (CONFIG_SUBSYSTEM_VENDOR_ID)
670 dev->subsystem_vendor = CONFIG_SUBSYSTEM_VENDOR_ID;
Rizwan Qureshifd891292017-04-26 21:00:37 +0530671 else if (!dev->subsystem_vendor)
672 dev->subsystem_vendor = pci_read_config16(dev,
673 PCI_VENDOR_ID);
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700674 if (CONFIG_SUBSYSTEM_DEVICE_ID)
675 dev->subsystem_device = CONFIG_SUBSYSTEM_DEVICE_ID;
Rizwan Qureshifd891292017-04-26 21:00:37 +0530676 else if (!dev->subsystem_device)
677 dev->subsystem_device = pci_read_config16(dev,
678 PCI_DEVICE_ID);
679
Sven Schnelle91321022011-03-01 19:58:47 +0000680 printk(BIOS_DEBUG, "%s subsystem <- %04x/%04x\n",
681 dev_path(dev), dev->subsystem_vendor,
682 dev->subsystem_device);
683 ops->set_subsystem(dev, dev->subsystem_vendor,
684 dev->subsystem_device);
Eric Biederman03acab62004-10-14 21:25:53 +0000685 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000686 command = pci_read_config16(dev, PCI_COMMAND);
687 command |= dev->command;
Uwe Hermanne4870472010-11-04 23:23:47 +0000688
Myles Watson29cc9ed2009-07-02 18:56:24 +0000689 /* v3 has
690 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
691 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000692
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000693 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000694 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000695}
696
697void pci_bus_enable_resources(struct device *dev)
698{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000699 u16 ctrl;
700
Uwe Hermanne4870472010-11-04 23:23:47 +0000701 /*
702 * Enable I/O in command register if there is VGA card
Myles Watson29cc9ed2009-07-02 18:56:24 +0000703 * connected with (even it does not claim I/O resource).
704 */
Myles Watson894a3472010-06-09 22:41:35 +0000705 if (dev->link_list->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000706 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000707 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000708 ctrl |= dev->link_list->bridge_ctrl;
Uwe Hermanne4870472010-11-04 23:23:47 +0000709 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* Error check. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000710 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000711 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
712
713 pci_dev_enable_resources(dev);
714}
715
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000716void pci_bus_reset(struct bus *bus)
717{
Uwe Hermanne4870472010-11-04 23:23:47 +0000718 u16 ctl;
719
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000720 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
721 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
722 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
723 mdelay(10);
Uwe Hermanne4870472010-11-04 23:23:47 +0000724
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000725 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
726 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
727 delay(1);
728}
729
Elyes HAOUAS88030b72018-09-20 17:26:10 +0200730void pci_dev_set_subsystem(struct device *dev, unsigned int vendor,
731 unsigned int device)
Eric Biederman03acab62004-10-14 21:25:53 +0000732{
Myles Watson032a9652009-05-11 22:24:53 +0000733 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000734 ((device & 0xffff) << 16) | (vendor & 0xffff));
Eric Biederman03acab62004-10-14 21:25:53 +0000735}
736
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300737static int should_run_oprom(struct device *dev)
738{
739 static int should_run = -1;
740
741 if (should_run >= 0)
742 return should_run;
743
Aaron Durbin10510252018-01-30 10:04:02 -0700744 if (IS_ENABLED(CONFIG_ALWAYS_RUN_OPROM)) {
745 should_run = 1;
746 return should_run;
747 }
748
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200749 /* Don't run VGA option ROMs, unless we have to print
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300750 * something on the screen before the kernel is loaded.
751 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700752 should_run = display_init_required();
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300753
Aaron Durbinfbed9a52018-01-30 09:58:51 -0700754 if (!should_run && IS_ENABLED(CONFIG_CHROMEOS))
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200755 should_run = vboot_wants_oprom();
Aaron Durbinfbed9a52018-01-30 09:58:51 -0700756
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200757 if (!should_run)
758 printk(BIOS_DEBUG, "Not running VGA Option ROM\n");
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300759 return should_run;
760}
761
762static int should_load_oprom(struct device *dev)
763{
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300764 /* If S3_VGA_ROM_RUN is disabled, skip running VGA option
765 * ROMs when coming out of an S3 resume.
766 */
Kyösti Mälkki58ceb002014-06-20 06:21:01 +0300767 if (!IS_ENABLED(CONFIG_S3_VGA_ROM_RUN) && acpi_is_wakeup_s3() &&
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300768 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
769 return 0;
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300770 if (IS_ENABLED(CONFIG_ALWAYS_LOAD_OPROM))
771 return 1;
772 if (should_run_oprom(dev))
773 return 1;
774
775 return 0;
776}
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300777
Uwe Hermanne4870472010-11-04 23:23:47 +0000778/** Default handler: only runs the relevant PCI BIOS. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000779void pci_dev_init(struct device *dev)
780{
781 struct rom_header *rom, *ram;
782
Aaron Durbinfbed9a52018-01-30 09:58:51 -0700783 if (!IS_ENABLED(CONFIG_VGA_ROM_RUN))
784 return;
785
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100786 /* Only execute VGA ROMs. */
787 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
Myles Watson17aeeca2009-10-07 18:41:08 +0000788 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000789
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300790 if (!should_load_oprom(dev))
Stefan Reinauer74a0efe2012-03-30 17:10:49 -0700791 return;
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700792 timestamp_add_now(TS_OPROM_INITIALIZE);
Aaron Durbince872cb2013-03-28 15:59:19 -0500793
794 rom = pci_rom_probe(dev);
795 if (rom == NULL)
796 return;
797
798 ram = pci_rom_load(dev, rom);
799 if (ram == NULL)
800 return;
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700801 timestamp_add_now(TS_OPROM_COPY_END);
Aaron Durbince872cb2013-03-28 15:59:19 -0500802
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300803 if (!should_run_oprom(dev))
804 return;
805
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000806 run_bios(dev, (unsigned long)ram);
Kyösti Mälkkiab56b3b2013-11-28 16:44:51 +0200807 gfx_set_init_done(1);
808 printk(BIOS_DEBUG, "VGA Option ROM was run\n");
Martin Roth5dd4a2a2018-03-06 16:10:45 -0700809 timestamp_add_now(TS_OPROM_END);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000810}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000811
Li-Ta Loe5266692004-03-23 21:28:05 +0000812/** Default device operation for PCI devices */
Subrata Banikffc790b2017-12-11 10:29:49 +0530813struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000814 .set_subsystem = pci_dev_set_subsystem,
815};
816
Eric Biederman8ca8d762003-04-22 19:02:15 +0000817struct device_operations default_pci_ops_dev = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000818 .read_resources = pci_dev_read_resources,
819 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000820 .enable_resources = pci_dev_enable_resources,
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200821#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
822 .write_acpi_tables = pci_rom_write_acpi_tables,
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200823 .acpi_fill_ssdt_generator = pci_rom_ssdt,
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200824#endif
Uwe Hermanne4870472010-11-04 23:23:47 +0000825 .init = pci_dev_init,
826 .scan_bus = 0,
827 .enable = 0,
828 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000829};
Li-Ta Loe5266692004-03-23 21:28:05 +0000830
831/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000832static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000833 .set_subsystem = 0,
834};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000835
Eric Biederman8ca8d762003-04-22 19:02:15 +0000836struct device_operations default_pci_ops_bus = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000837 .read_resources = pci_bus_read_resources,
838 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000839 .enable_resources = pci_bus_enable_resources,
Uwe Hermanne4870472010-11-04 23:23:47 +0000840 .init = 0,
841 .scan_bus = pci_scan_bridge,
842 .enable = 0,
843 .reset_bus = pci_bus_reset,
844 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000845};
Li-Ta Loe5266692004-03-23 21:28:05 +0000846
847/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000848 * Detect the type of downstream bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000849 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000850 * This function is a heuristic to detect which type of bus is downstream
851 * of a PCI-to-PCI bridge. This functions by looking for various capability
852 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
853 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +0000854 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000855 * When only a PCI-Express capability is found the type is examined to see
856 * which type of bridge we have.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000857 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000858 * @param dev Pointer to the device structure of the bridge.
859 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000860 */
Aaron Durbinc30d9132017-08-07 16:55:43 -0600861static struct device_operations *get_pci_bridge_ops(struct device *dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000862{
Martin Rothb3b114c2017-06-24 14:00:01 -0600863#if IS_ENABLED(CONFIG_PCIX_PLUGIN_SUPPORT)
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800864 unsigned int pcixpos;
865 pcixpos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
866 if (pcixpos) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000867 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000868 return &default_pcix_ops_bus;
869 }
870#endif
Martin Rothb3b114c2017-06-24 14:00:01 -0600871#if IS_ENABLED(CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT)
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800872 unsigned int htpos = 0;
873 while ((htpos = pci_find_next_capability(dev, PCI_CAP_ID_HT, htpos))) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000874 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800875 flags = pci_read_config16(dev, htpos + PCI_CAP_FLAGS);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000876 if ((flags >> 13) == 1) {
877 /* Host or Secondary Interface */
Uwe Hermanne4870472010-11-04 23:23:47 +0000878 printk(BIOS_DEBUG, "%s subordinate bus HT\n",
879 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000880 return &default_ht_ops_bus;
881 }
882 }
883#endif
Martin Rothb3b114c2017-06-24 14:00:01 -0600884#if IS_ENABLED(CONFIG_PCIEXP_PLUGIN_SUPPORT)
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800885 unsigned int pciexpos;
886 pciexpos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
887 if (pciexpos) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000888 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800889 flags = pci_read_config16(dev, pciexpos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000890 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000891 case PCI_EXP_TYPE_ROOT_PORT:
892 case PCI_EXP_TYPE_UPSTREAM:
893 case PCI_EXP_TYPE_DOWNSTREAM:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000894 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000895 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000896 return &default_pciexp_ops_bus;
897 case PCI_EXP_TYPE_PCI_BRIDGE:
Uwe Hermanne4870472010-11-04 23:23:47 +0000898 printk(BIOS_DEBUG, "%s subordinate PCI\n",
899 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000900 return &default_pci_ops_bus;
901 default:
902 break;
903 }
904 }
905#endif
906 return &default_pci_ops_bus;
907}
908
909/**
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700910 * Check if a device id matches a PCI driver entry.
911 *
912 * The driver entry can either point at a zero terminated array of acceptable
913 * device IDs, or include a single device ID.
914 *
Martin Roth98b698c2015-01-06 21:02:52 -0700915 * @param driver pointer to the PCI driver entry being checked
916 * @param device_id PCI device ID of the device being matched
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700917 */
918static int device_id_match(struct pci_driver *driver, unsigned short device_id)
919{
920 if (driver->devices) {
921 unsigned short check_id;
922 const unsigned short *device_list = driver->devices;
923 while ((check_id = *device_list++) != 0)
924 if (check_id == device_id)
925 return 1;
926 }
927
928 return (driver->device == device_id);
929}
930
931/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000932 * Set up PCI device operation.
933 *
934 * Check if it already has a driver. If not, use find_device_operations(),
935 * or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +0000936 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000937 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +0000938 * @see pci_drivers
939 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000940static void set_pci_ops(struct device *dev)
941{
942 struct pci_driver *driver;
Li-Ta Loe5266692004-03-23 21:28:05 +0000943
Uwe Hermanne4870472010-11-04 23:23:47 +0000944 if (dev->ops)
945 return;
946
947 /*
948 * Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +0000949 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000950 */
Aaron Durbin03758152015-09-03 17:23:08 -0500951 for (driver = &_pci_drivers[0]; driver != &_epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000952 if ((driver->vendor == dev->vendor) &&
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700953 device_id_match(driver, dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +0000954 dev->ops = (struct device_operations *)driver->ops;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000955 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000956 dev_path(dev), driver->vendor, driver->device,
957 (driver->ops->scan_bus ? "bus " : ""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000958 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000959 }
960 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000961
Uwe Hermanne4870472010-11-04 23:23:47 +0000962 /* If I don't have a specific driver use the default operations. */
963 switch (dev->hdr_type & 0x7f) { /* Header type */
964 case PCI_HEADER_TYPE_NORMAL:
Eric Biederman8ca8d762003-04-22 19:02:15 +0000965 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
966 goto bad;
967 dev->ops = &default_pci_ops_dev;
968 break;
969 case PCI_HEADER_TYPE_BRIDGE:
970 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
971 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000972 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000973 break;
Martin Rothb3b114c2017-06-24 14:00:01 -0600974#if IS_ENABLED(CONFIG_CARDBUS_PLUGIN_SUPPORT)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000975 case PCI_HEADER_TYPE_CARDBUS:
976 dev->ops = &default_cardbus_ops_bus;
977 break;
978#endif
Uwe Hermanne4870472010-11-04 23:23:47 +0000979default:
980bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000981 if (dev->enabled) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000982 printk(BIOS_ERR, "%s [%04x/%04x/%06x] has unknown "
983 "header type %02x, ignoring.\n", dev_path(dev),
984 dev->vendor, dev->device,
985 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000986 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000987 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000988}
989
990/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000991 * See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000992 *
Kyösti Mälkki8712aa12019-01-09 11:31:25 +0200993 * Given a PCI bus structure and a devfn number, find the device structure
994 * corresponding to the devfn, if present. Then move the device structure
995 * as the last child on the bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000996 *
Kyösti Mälkki8712aa12019-01-09 11:31:25 +0200997 * @param bus Pointer to the bus structure.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000998 * @param devfn A device/function number.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000999 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +00001000 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001001 */
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001002static struct device *pci_scan_get_dev(struct bus *bus, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001003{
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001004 struct device *dev, **prev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001005
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001006 prev = &bus->children;
1007 for (dev = bus->children; dev; dev = dev->sibling) {
1008 if (dev->path.type == DEVICE_PATH_PCI) {
1009 if (dev->path.pci.devfn == devfn) {
1010 /* Unlink from the list. */
1011 *prev = dev->sibling;
1012 dev->sibling = NULL;
1013 break;
1014 }
1015 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +00001016 printk(BIOS_ERR, "child %s not a PCI device\n",
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001017 dev_path(dev));
Eric Biedermanad1b35a2003-10-14 02:36:51 +00001018 }
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001019 prev = &dev->sibling;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001020 }
Myles Watson29cc9ed2009-07-02 18:56:24 +00001021
Uwe Hermanne4870472010-11-04 23:23:47 +00001022 /*
1023 * Just like alloc_dev() add the device to the list of devices on the
Myles Watson29cc9ed2009-07-02 18:56:24 +00001024 * bus. When the list of devices was formed we removed all of the
1025 * parents children, and now we are interleaving static and dynamic
1026 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001027 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001028 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001029 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +00001030
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001031 /* Find the last child on the bus. */
1032 for (child = bus->children; child && child->sibling;)
Eric Biedermane9a271e32003-09-02 03:36:25 +00001033 child = child->sibling;
Uwe Hermanne4870472010-11-04 23:23:47 +00001034
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001035 /* Place the device as last on the bus. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001036 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +00001037 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001038 else
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001039 bus->children = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001040 }
1041
Eric Biederman8ca8d762003-04-22 19:02:15 +00001042 return dev;
1043}
1044
Myles Watson032a9652009-05-11 22:24:53 +00001045/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001046 * Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +00001047 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001048 * Determine the existence of a given PCI device. Allocate a new struct device
1049 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001050 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001051 * @param dev Pointer to the dev structure.
1052 * @param bus Pointer to the bus structure.
1053 * @param devfn A device/function number to look at.
1054 * @return The device structure for the device (if found), NULL otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001055 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001056struct device *pci_probe_dev(struct device *dev, struct bus *bus,
1057 unsigned int devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001058{
Myles Watson29cc9ed2009-07-02 18:56:24 +00001059 u32 id, class;
1060 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001061
Myles Watson29cc9ed2009-07-02 18:56:24 +00001062 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001063 if (!dev) {
1064 struct device dummy;
Uwe Hermanne4870472010-11-04 23:23:47 +00001065
Myles Watson29cc9ed2009-07-02 18:56:24 +00001066 dummy.bus = bus;
1067 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +00001068 dummy.path.pci.devfn = devfn;
Uwe Hermanne4870472010-11-04 23:23:47 +00001069
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001070 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +00001071 /*
1072 * Have we found something? Some broken boards return 0 if a
1073 * slot is empty, but the expected answer is 0xffffffff.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001074 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001075 if (id == 0xffffffff)
Stefan Reinauer7355c752010-04-02 16:30:25 +00001076 return NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +00001077
Stefan Reinauer7355c752010-04-02 16:30:25 +00001078 if ((id == 0x00000000) || (id == 0x0000ffff) ||
1079 (id == 0xffff0000)) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001080 printk(BIOS_SPEW, "%s, bad id 0x%x\n",
1081 dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001082 return NULL;
1083 }
1084 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001085 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +00001086 /*
1087 * Enable/disable the device. Once we have found the device-
Myles Watson29cc9ed2009-07-02 18:56:24 +00001088 * specific operations this operations we will disable the
1089 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +00001090 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001091 * This is geared toward devices that have subfunctions
1092 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +00001093 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001094 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +00001095 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001096 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001097 /* Run the magic enable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001098 if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001099 dev->chip_ops->enable_dev(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +00001100
Myles Watson29cc9ed2009-07-02 18:56:24 +00001101 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001102 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +00001103
Uwe Hermanne4870472010-11-04 23:23:47 +00001104 /*
1105 * If the device does not have a PCI ID disable it. Possibly
Myles Watson29cc9ed2009-07-02 18:56:24 +00001106 * this is because we have already disabled the device. But
1107 * this also handles optional devices that may not always
1108 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001109 */
1110 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001111 if ((id == 0xffffffff) || (id == 0x00000000) ||
1112 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001113 if (dev->enabled) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001114 printk(BIOS_INFO, "PCI: Static device %s not "
1115 "found, disabling it.\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001116 dev->enabled = 0;
1117 }
1118 return dev;
1119 }
1120 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001121
Myles Watson29cc9ed2009-07-02 18:56:24 +00001122 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001123 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1124 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +00001125
Myles Watson29cc9ed2009-07-02 18:56:24 +00001126 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001127 dev->vendor = id & 0xffff;
1128 dev->device = (id >> 16) & 0xffff;
1129 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001130
1131 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001132 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +00001133
Myles Watson29cc9ed2009-07-02 18:56:24 +00001134 /* Architectural/System devices always need to be bus masters. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001135 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001136 dev->command |= PCI_COMMAND_MASTER;
Uwe Hermanne4870472010-11-04 23:23:47 +00001137
1138 /*
1139 * Look at the vendor and device ID, or at least the header type and
Myles Watson29cc9ed2009-07-02 18:56:24 +00001140 * class and figure out which set of configuration methods to use.
1141 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001142 */
1143 set_pci_ops(dev);
1144
Myles Watson29cc9ed2009-07-02 18:56:24 +00001145 /* Now run the magic enable/disable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001146 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001147 dev->ops->enable(dev);
Myles Watson032a9652009-05-11 22:24:53 +00001148
Myles Watson29cc9ed2009-07-02 18:56:24 +00001149 /* Display the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001150 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n", dev_path(dev),
1151 dev->vendor, dev->device, dev->enabled ? "enabled" : "disabled",
1152 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001153
1154 return dev;
1155}
1156
Myles Watson032a9652009-05-11 22:24:53 +00001157/**
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001158 * Test for match between romstage and ramstage device instance.
1159 *
1160 * @param dev Pointer to the device structure.
1161 * @param sdev Simple device model identifier, created with PCI_DEV().
1162 * @return Non-zero if bus:dev.fn of device matches.
1163 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001164unsigned int pci_match_simple_dev(struct device *dev, pci_devfn_t sdev)
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001165{
1166 return dev->bus->secondary == PCI_DEV2SEGBUS(sdev) &&
1167 dev->path.pci.devfn == PCI_DEV2DEVFN(sdev);
1168}
1169
1170/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001171 * Scan a PCI bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001172 *
Li-Ta Loe5266692004-03-23 21:28:05 +00001173 * Determine the existence of devices and bridges on a PCI bus. If there are
1174 * bridges on the bus, recursively scan the buses behind the bridges.
1175 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001176 * @param bus Pointer to the bus structure.
1177 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1178 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001179 */
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001180void pci_scan_bus(struct bus *bus, unsigned min_devfn,
1181 unsigned max_devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001182{
1183 unsigned int devfn;
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001184 struct device *dev, **prev;
1185 int once = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001186
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001187 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %02x\n", bus->secondary);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001188
Uwe Hermanne4870472010-11-04 23:23:47 +00001189 /* Maximum sane devfn is 0xFF. */
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001190 if (max_devfn > 0xff) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001191 printk(BIOS_ERR, "PCI: pci_scan_bus limits devfn %x - "
1192 "devfn %x\n", min_devfn, max_devfn);
1193 printk(BIOS_ERR, "PCI: pci_scan_bus upper limit too big. "
1194 "Using 0xff.\n");
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001195 max_devfn=0xff;
1196 }
1197
Eric Biederman8ca8d762003-04-22 19:02:15 +00001198 post_code(0x24);
Uwe Hermanne4870472010-11-04 23:23:47 +00001199
1200 /*
1201 * Probe all devices/functions on this bus with some optimization for
Myles Watson29cc9ed2009-07-02 18:56:24 +00001202 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001203 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001204 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001205 /* First thing setup the device structure. */
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001206 dev = pci_scan_get_dev(bus, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001207
Myles Watson29cc9ed2009-07-02 18:56:24 +00001208 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001209 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001210
Uwe Hermanne4870472010-11-04 23:23:47 +00001211 /*
1212 * If this is not a multi function device, or the device is
Myles Watson29cc9ed2009-07-02 18:56:24 +00001213 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001214 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001215 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001216 if ((PCI_FUNC(devfn) == 0x00) && (!dev
Myles Watson29cc9ed2009-07-02 18:56:24 +00001217 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001218 devfn += 0x07;
1219 }
1220 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001221
Eric Biederman8ca8d762003-04-22 19:02:15 +00001222 post_code(0x25);
1223
Uwe Hermanne4870472010-11-04 23:23:47 +00001224 /*
1225 * Warn if any leftover static devices are are found.
1226 * There's probably a problem in devicetree.cb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001227 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001228
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001229 prev = &bus->children;
1230 for (dev = bus->children; dev; dev = dev->sibling) {
1231 /* If we read valid vendor id, it is not leftover device. */
1232 if (dev->vendor != 0) {
1233 prev = &dev->sibling;
1234 continue;
1235 }
1236
1237 /* Unlink it from list. */
1238 *prev = dev->sibling;
1239
1240 if (!once++)
1241 printk(BIOS_WARNING, "PCI: Leftover static devices:\n");
1242 printk(BIOS_WARNING, "%s\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001243 }
1244
Kyösti Mälkki8712aa12019-01-09 11:31:25 +02001245 if (once)
1246 printk(BIOS_WARNING, "PCI: Check your devicetree.cb.\n");
1247
Uwe Hermanne4870472010-11-04 23:23:47 +00001248 /*
1249 * For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001250 * scan the bus behind that child.
1251 */
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001252
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +02001253 scan_bridges(bus);
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001254
Uwe Hermanne4870472010-11-04 23:23:47 +00001255 /*
1256 * We've scanned the bus and so we know all about what's on the other
Myles Watson29cc9ed2009-07-02 18:56:24 +00001257 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001258 * Return how far we've got finding sub-buses.
1259 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001260 post_code(0x55);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001261}
1262
Kyösti Mälkki33452402015-02-23 06:58:26 +02001263typedef enum {
1264 PCI_ROUTE_CLOSE,
1265 PCI_ROUTE_SCAN,
1266 PCI_ROUTE_FINAL,
1267} scan_state;
1268
1269static void pci_bridge_route(struct bus *link, scan_state state)
1270{
1271 struct device *dev = link->dev;
1272 struct bus *parent = dev->bus;
1273 u32 reg, buses = 0;
1274
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001275 if (state == PCI_ROUTE_SCAN) {
1276 link->secondary = parent->subordinate + 1;
1277 link->subordinate = link->secondary;
1278 }
1279
Kyösti Mälkki33452402015-02-23 06:58:26 +02001280 if (state == PCI_ROUTE_CLOSE) {
1281 buses |= 0xfeff << 8;
1282 } else if (state == PCI_ROUTE_SCAN) {
Timothy Pearson7d8a4782015-10-24 20:34:57 -05001283 buses |= parent->secondary & 0xff;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001284 buses |= ((u32) link->secondary & 0xff) << 8;
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001285 buses |= 0xff << 16; /* MAX PCI_BUS number here */
Kyösti Mälkki33452402015-02-23 06:58:26 +02001286 } else if (state == PCI_ROUTE_FINAL) {
1287 buses |= parent->secondary & 0xff;
1288 buses |= ((u32) link->secondary & 0xff) << 8;
1289 buses |= ((u32) link->subordinate & 0xff) << 16;
1290 }
1291
1292 if (state == PCI_ROUTE_SCAN) {
1293 /* Clear all status bits and turn off memory, I/O and master enables. */
1294 link->bridge_cmd = pci_read_config16(dev, PCI_COMMAND);
1295 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1296 pci_write_config16(dev, PCI_STATUS, 0xffff);
1297 }
1298
1299 /*
1300 * Configure the bus numbers for this bridge: the configuration
1301 * transactions will not be propagated by the bridge if it is not
1302 * correctly configured.
1303 */
1304
1305 reg = pci_read_config32(dev, PCI_PRIMARY_BUS);
1306 reg &= 0xff000000;
1307 reg |= buses;
1308 pci_write_config32(dev, PCI_PRIMARY_BUS, reg);
1309
1310 if (state == PCI_ROUTE_FINAL) {
1311 pci_write_config16(dev, PCI_COMMAND, link->bridge_cmd);
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001312 parent->subordinate = link->subordinate;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001313 }
1314}
1315
Li-Ta Loe5266692004-03-23 21:28:05 +00001316/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001317 * Scan a PCI bridge and the buses behind the bridge.
Li-Ta Loe5266692004-03-23 21:28:05 +00001318 *
1319 * Determine the existence of buses behind the bridge. Set up the bridge
1320 * according to the result of the scan.
1321 *
1322 * This function is the default scan_bus() method for PCI bridge devices.
1323 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001324 * @param dev Pointer to the bridge device.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001325 * @param do_scan_bus TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +00001326 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001327void do_pci_scan_bridge(struct device *dev,
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001328 void (*do_scan_bus) (struct bus * bus,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001329 unsigned min_devfn,
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001330 unsigned max_devfn))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001331{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001332 struct bus *bus;
Eric Biederman83b991a2003-10-11 06:20:25 +00001333
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001334 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
Li-Ta Lo3a812852004-12-03 22:39:34 +00001335
Myles Watson894a3472010-06-09 22:41:35 +00001336 if (dev->link_list == NULL) {
1337 struct bus *link;
1338 link = malloc(sizeof(*link));
1339 if (link == NULL)
1340 die("Couldn't allocate a link!\n");
1341 memset(link, 0, sizeof(*link));
1342 link->dev = dev;
1343 dev->link_list = link;
1344 }
1345
1346 bus = dev->link_list;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001347
Kyösti Mälkki33452402015-02-23 06:58:26 +02001348 pci_bridge_route(bus, PCI_ROUTE_SCAN);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001349
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001350 do_scan_bus(bus, 0x00, 0xff);
Kyösti Mälkki33452402015-02-23 06:58:26 +02001351
1352 pci_bridge_route(bus, PCI_ROUTE_FINAL);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001353}
Li-Ta Loe5266692004-03-23 21:28:05 +00001354
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001355/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001356 * Scan a PCI bridge and the buses behind the bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001357 *
1358 * Determine the existence of buses behind the bridge. Set up the bridge
1359 * according to the result of the scan.
1360 *
1361 * This function is the default scan_bus() method for PCI bridge devices.
1362 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001363 * @param dev Pointer to the bridge device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001364 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001365void pci_scan_bridge(struct device *dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001366{
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001367 do_pci_scan_bridge(dev, pci_scan_bus);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001368}
1369
Myles Watson29cc9ed2009-07-02 18:56:24 +00001370/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001371 * Scan a PCI domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001372 *
1373 * This function is the default scan_bus() method for PCI domains.
1374 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001375 * @param dev Pointer to the domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001376 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001377void pci_domain_scan_bus(struct device *dev)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001378{
Kyösti Mälkki6f370172015-03-19 15:26:52 +02001379 struct bus *link = dev->link_list;
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001380 pci_scan_bus(link, PCI_DEVFN(0, 0), 0xff);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001381}
1382
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001383/**
1384 * Take an INT_PIN number (0, 1 - 4) and convert
1385 * it to a string ("NO PIN", "PIN A" - "PIN D")
1386 *
1387 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1388 * @return A string corresponding to the pin number or "Invalid"
1389 */
1390const char *pin_to_str(int pin)
1391{
1392 const char *str[5] = {
1393 "NO PIN",
1394 "PIN A",
1395 "PIN B",
1396 "PIN C",
1397 "PIN D",
1398 };
1399
1400 if (pin >= 0 && pin <= 4)
1401 return str[pin];
1402 else
1403 return "Invalid PIN, not 0 - 4";
1404}
1405
1406/**
1407 * Get the PCI INT_PIN swizzle for a device defined as:
1408 * pin_parent = (pin_child + devn_child) % 4 + 1
1409 * where PIN A = 1 ... PIN_D = 4
1410 *
1411 * Given a PCI device structure 'dev', find the interrupt pin
1412 * that will be triggered on its parent bridge device when
1413 * generating an interrupt. For example: Device 1:3.2 may
1414 * use INT_PIN A but will trigger PIN D on its parent bridge
1415 * device. In this case, this function will return 4 (PIN D).
1416 *
1417 * @param dev A PCI device structure to swizzle interrupt pins for
Martin Roth32bc6b62015-01-04 16:54:35 -07001418 * @param *parent_bridge The PCI device structure for the bridge
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001419 * device 'dev' is attached to
1420 * @return The interrupt pin number (1 - 4) that 'dev' will
1421 * trigger when generating an interrupt
1422 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001423static int swizzle_irq_pins(struct device *dev, struct device **parent_bridge)
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001424{
Aaron Durbinc30d9132017-08-07 16:55:43 -06001425 struct device *parent; /* Our current device's parent device */
1426 struct device *child; /* The child device of the parent */
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001427 uint8_t parent_bus = 0; /* Parent Bus number */
1428 uint16_t parent_devfn = 0; /* Parent Device and Function number */
1429 uint16_t child_devfn = 0; /* Child Device and Function number */
1430 uint8_t swizzled_pin = 0; /* Pin swizzled across a bridge */
1431
1432 /* Start with PIN A = 0 ... D = 3 */
1433 swizzled_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN) - 1;
1434
1435 /* While our current device has parent devices */
1436 child = dev;
1437 for (parent = child->bus->dev; parent; parent = parent->bus->dev) {
1438 parent_bus = parent->bus->secondary;
1439 parent_devfn = parent->path.pci.devfn;
1440 child_devfn = child->path.pci.devfn;
1441
1442 /* Swizzle the INT_PIN for any bridges not on root bus */
1443 swizzled_pin = (PCI_SLOT(child_devfn) + swizzled_pin) % 4;
1444 printk(BIOS_SPEW, "\tWith INT_PIN swizzled to %s\n"
1445 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1446 pin_to_str(swizzled_pin + 1), parent_bus,
1447 PCI_SLOT(parent_devfn), PCI_FUNC(parent_devfn));
1448
1449 /* Continue until we find the root bus */
1450 if (parent_bus > 0) {
1451 /*
1452 * We will go on to the next parent so this parent
1453 * becomes the child
1454 */
1455 child = parent;
1456 continue;
1457 } else {
1458 /*
1459 * Found the root bridge device,
1460 * fill in the structure and exit
1461 */
1462 *parent_bridge = parent;
1463 break;
1464 }
1465 }
1466
1467 /* End with PIN A = 1 ... D = 4 */
1468 return swizzled_pin + 1;
1469}
1470
1471/**
1472 * Given a device structure 'dev', find its interrupt pin
1473 * and its parent bridge 'parent_bdg' device structure.
1474 * If it is behind a bridge, it will return the interrupt
1475 * pin number (1 - 4) of the parent bridge that the device
1476 * interrupt pin has been swizzled to, otherwise it will
1477 * return the interrupt pin that is programmed into the
1478 * PCI config space of the target device. If 'dev' is
1479 * behind a bridge, it will fill in 'parent_bdg' with the
1480 * device structure of the bridge it is behind, otherwise
1481 * it will copy 'dev' into 'parent_bdg'.
1482 *
1483 * @param dev A PCI device structure to get interrupt pins for.
1484 * @param *parent_bdg The PCI device structure for the bridge
1485 * device 'dev' is attached to.
1486 * @return The interrupt pin number (1 - 4) that 'dev' will
1487 * trigger when generating an interrupt.
1488 * Errors: -1 is returned if the device is not enabled
1489 * -2 is returned if a parent bridge could not be found.
1490 */
Aaron Durbinc30d9132017-08-07 16:55:43 -06001491int get_pci_irq_pins(struct device *dev, struct device **parent_bdg)
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001492{
1493 uint8_t bus = 0; /* The bus this device is on */
1494 uint16_t devfn = 0; /* This device's device and function numbers */
1495 uint8_t int_pin = 0; /* Interrupt pin used by the device */
1496 uint8_t target_pin = 0; /* Interrupt pin we want to assign an IRQ to */
1497
1498 /* Make sure this device is enabled */
1499 if (!(dev->enabled && (dev->path.type == DEVICE_PATH_PCI)))
1500 return -1;
1501
1502 bus = dev->bus->secondary;
1503 devfn = dev->path.pci.devfn;
1504
1505 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1506 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1507 if (int_pin < 1 || int_pin > 4)
1508 return -1;
1509
1510 printk(BIOS_SPEW, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1511 bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pin_to_str(int_pin));
1512
1513 /* If this device is on a bridge, swizzle its INT_PIN */
1514 if (bus) {
1515 /* Swizzle its INT_PINs */
1516 target_pin = swizzle_irq_pins(dev, parent_bdg);
1517
1518 /* Make sure the swizzle returned valid structures */
1519 if (parent_bdg == NULL) {
1520 printk(BIOS_WARNING,
1521 "Warning: Could not find parent bridge for this device!\n");
1522 return -2;
1523 }
1524 } else { /* Device is not behind a bridge */
1525 target_pin = int_pin; /* Return its own interrupt pin */
1526 *parent_bdg = dev; /* Return its own structure */
1527 }
1528
1529 /* Target pin is the interrupt pin we want to assign an IRQ to */
1530 return target_pin;
1531}
1532
Martin Rothb3b114c2017-06-24 14:00:01 -06001533#if IS_ENABLED(CONFIG_PC80_SYSTEM)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001534/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001535 * Assign IRQ numbers.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001536 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001537 * This function assigns IRQs for all functions contained within the indicated
Uwe Hermanne4870472010-11-04 23:23:47 +00001538 * device address. If the device does not exist or does not require interrupts
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001539 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001540 *
1541 * This function should be called for each PCI slot in your system.
1542 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001543 * @param bus Pointer to the bus structure.
1544 * @param slot TODO
1545 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1546 * of this slot. The particular IRQ #s that are passed in depend on the
1547 * routing inside your southbridge and on your board.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001548 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001549void pci_assign_irqs(unsigned bus, unsigned slot,
Uwe Hermanne4870472010-11-04 23:23:47 +00001550 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001551{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001552 unsigned int funct;
Aaron Durbinc30d9132017-08-07 16:55:43 -06001553 struct device *pdev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001554 u8 line, irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001555
Uwe Hermanne4870472010-11-04 23:23:47 +00001556 /* Each slot may contain up to eight functions. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001557 for (funct = 0; funct < 8; funct++) {
1558 pdev = dev_find_slot(bus, (slot << 3) + funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001559
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001560 if (!pdev)
1561 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001562
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001563 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001564
Uwe Hermanne4870472010-11-04 23:23:47 +00001565 /* PCI spec says all values except 1..4 are reserved. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001566 if ((line < 1) || (line > 4))
1567 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001568
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001569 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001570
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001571 printk(BIOS_DEBUG, "Assigning IRQ %d to %d:%x.%d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +00001572 irq, bus, slot, funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001573
Stefan Reinauer14e22772010-04-27 06:56:47 +00001574 pci_write_config8(pdev, PCI_INTERRUPT_LINE,
Uwe Hermanne4870472010-11-04 23:23:47 +00001575 pIntAtoD[line - 1]);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001576
1577#ifdef PARANOID_IRQ_ASSIGNMENTS
Myles Watson17aeeca2009-10-07 18:41:08 +00001578 irq = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001579 printk(BIOS_DEBUG, " Readback = %d\n", irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001580#endif
1581
Martin Rothb3b114c2017-06-24 14:00:01 -06001582#if IS_ENABLED(CONFIG_PC80_SYSTEM)
Uwe Hermanne4870472010-11-04 23:23:47 +00001583 /* Change to level triggered. */
1584 i8259_configure_irq_trigger(pIntAtoD[line - 1],
1585 IRQ_LEVEL_TRIGGERED);
Stefan Reinauer5fb62162010-12-16 23:52:04 +00001586#endif
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001587 }
1588}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001589#endif