coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
pcie.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/pci_def.h>
7 #include <device/pci_ops.h>
8 #include <device/pci_ids.h>
9 #include "chip.h"
10 #include "i82801gx.h"
11 
12 /* Low Power variant has 6 root ports. */
13 #define NUM_ROOT_PORTS 6
14 
15 struct root_port_config {
16  /* RPFN is a write-once register so keep a copy until it is written */
17  u32 orig_rpfn;
18  u32 new_rpfn;
19  int num_ports;
20  struct device *ports[NUM_ROOT_PORTS];
21 };
22 
23 static struct root_port_config rpc;
24 
25 static inline int root_port_is_first(struct device *dev)
26 {
27  return PCI_FUNC(dev->path.pci.devfn) == 0;
28 }
29 
30 static inline int root_port_is_last(struct device *dev)
31 {
32  return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
33 }
34 
35 /* Root ports are numbered 1..N in the documentation. */
36 static inline int root_port_number(struct device *dev)
37 {
38  return PCI_FUNC(dev->path.pci.devfn) + 1;
39 }
40 
41 static void pci_init(struct device *dev)
42 {
43  u16 reg16;
44 
45  printk(BIOS_DEBUG, "Initializing ICH7 PCIe bridge.\n");
46 
47  /* Enable Bus Master */
49 
50  /* Set Cache Line Size to 0x10 */
51  // This has no effect but the OS might expect it
53 
55 
56  /* Enable IO xAPIC on this PCIe port */
57  pci_or_config32(dev, 0xd8, 1 << 7);
58 
59  /* Enable Backbone Clock Gating */
60  pci_or_config32(dev, 0xe1, (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
61 
62  /* Set VC0 transaction class */
63  pci_update_config32(dev, 0x114, ~0x000000ff, 1);
64 
65  /* Mask completion timeouts */
66  pci_or_config32(dev, 0x148, 1 << 14);
67 
68  /* Enable common clock configuration */
69  // Are there cases when we don't want that?
70  pci_or_config16(dev, 0x50, 1 << 6);
71 
72  /* Clear errors in status registers. FIXME: Do something? */
73  reg16 = pci_read_config16(dev, 0x06);
74  //reg16 |= 0xf900;
75  pci_write_config16(dev, 0x06, reg16);
76 
77  reg16 = pci_read_config16(dev, 0x1e);
78  //reg16 |= 0xf900;
79  pci_write_config16(dev, 0x1e, reg16);
80 }
81 
82 static int get_num_ports(void)
83 {
84  struct device *dev = pcidev_on_root(31, 0);
86  return 4;
87  else
88  return 6;
89 }
90 
91 static void root_port_init_config(struct device *dev)
92 {
93  int rp;
94 
95  if (root_port_is_first(dev)) {
99  }
100 
101  rp = root_port_number(dev);
102  if (rp > rpc.num_ports) {
103  printk(BIOS_ERR, "Found Root Port %d, expecting %d\n", rp, rpc.num_ports);
104  return;
105  }
106 
107  /* Cache pci device. */
108  rpc.ports[rp - 1] = dev;
109 }
110 
111 /* Update devicetree with new Root Port function number assignment */
112 static void ich_pcie_device_set_func(int index, int pci_func)
113 {
114  struct device *dev;
115  unsigned int new_devfn;
116 
117  dev = rpc.ports[index];
118 
119  /* Set the new PCI function field for this Root Port. */
120  rpc.new_rpfn &= ~RPFN_FNMASK(index);
121  rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
122 
123  /* Determine the new devfn for this port */
124  new_devfn = PCI_DEVFN(ICH_PCIE_DEV_SLOT, pci_func);
125 
126  if (dev->path.pci.devfn != new_devfn) {
128  "ICH: PCIe map %02x.%1x -> %02x.%1x\n",
129  PCI_SLOT(dev->path.pci.devfn),
130  PCI_FUNC(dev->path.pci.devfn),
131  PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
132 
133  dev->path.pci.devfn = new_devfn;
134  }
135 }
136 
137 static void root_port_commit_config(struct device *dev)
138 {
139  int i;
140  bool coalesce = false;
141 
142  if (dev->chip_info != NULL) {
144  coalesce = config->pcie_port_coalesce;
145  }
146 
147  if (!rpc.ports[0]->enabled)
148  coalesce = true;
149 
150  for (i = 0; i < rpc.num_ports; i++) {
151  struct device *pcie_dev;
152 
153  pcie_dev = rpc.ports[i];
154 
155  if (pcie_dev == NULL) {
156  printk(BIOS_ERR, "Root Port %d device is NULL?\n", i + 1);
157  continue;
158  }
159 
160  if (pcie_dev->enabled)
161  continue;
162 
163  printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(pcie_dev));
164 
165  /* Disable this device if possible */
166  i82801gx_enable(pcie_dev);
167  }
168 
169  if (coalesce) {
170  int current_func;
171 
172  /* For all Root Ports N enabled ports get assigned the lower
173  * PCI function number. The disabled ones get upper PCI
174  * function numbers. */
175  current_func = 0;
176  for (i = 0; i < rpc.num_ports; i++) {
177  if (!rpc.ports[i]->enabled)
178  continue;
179  ich_pcie_device_set_func(i, current_func);
180  current_func++;
181  }
182 
183  /* Allocate the disabled devices' PCI function number. */
184  for (i = 0; i < rpc.num_ports; i++) {
185  if (rpc.ports[i]->enabled)
186  continue;
187  ich_pcie_device_set_func(i, current_func);
188  current_func++;
189  }
190  }
191 
192  printk(BIOS_SPEW, "ICH: RPFN 0x%08x -> 0x%08x\n", rpc.orig_rpfn, rpc.new_rpfn);
193  RCBA32(RPFN) = rpc.new_rpfn;
194 }
195 
196 static void ich_pcie_enable(struct device *dev)
197 {
198  /* Add this device to the root port config structure. */
200 
201  /*
202  * When processing the last PCIe root port we can now
203  * update the Root Port Function Number and Hide register.
204  */
205  if (root_port_is_last(dev))
207 }
208 
209 static struct device_operations device_ops = {
211  .set_resources = pci_dev_set_resources,
212  .enable_resources = pci_bus_enable_resources,
213  .init = pci_init,
214  .enable = ich_pcie_enable,
215  .scan_bus = pci_scan_bridge,
216  .ops_pci = &pci_dev_ops_pci,
217 };
218 
219 static const unsigned short i82801gx_pcie_ids[] = {
220  0x27d0, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
221  0x27d2, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
222  0x27d4, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
223  0x27d6, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
224  0x27e0, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
225  0x27e2, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
226  0
227 };
228 
229 static const struct pci_driver i82801gx_pcie __pci_driver = {
230  .ops = &device_ops,
231  .vendor = PCI_VID_INTEL,
232  .devices = i82801gx_pcie_ids,
233 };
#define printk(level,...)
Definition: stdlib.h:16
DEVTREE_CONST struct device * pcidev_on_root(uint8_t dev, uint8_t fn)
Definition: device_const.c:260
const char * dev_path(const struct device *dev)
Definition: device_util.c:149
void i82801gx_enable(struct device *dev)
Definition: i82801gx.c:54
#define FDVCT
Definition: i82801gx.h:50
#define ICH_PCIE_DEV_SLOT
Definition: i82801gx.h:34
#define PCIE_4_PORTS_MAX
Definition: i82801gx.h:51
static __always_inline void pci_or_config32(const struct device *dev, u16 reg, u32 ormask)
Definition: pci_ops.h:191
static __always_inline void pci_and_config16(const struct device *dev, u16 reg, u16 andmask)
Definition: pci_ops.h:147
static __always_inline void pci_update_config32(const struct device *dev, u16 reg, u32 mask, u32 or)
Definition: pci_ops.h:120
static __always_inline void pci_or_config16(const struct device *dev, u16 reg, u16 ormask)
Definition: pci_ops.h:180
static __always_inline u16 pci_read_config16(const struct device *dev, u16 reg)
Definition: pci_ops.h:52
static __always_inline u32 pci_read_config32(const struct device *dev, u16 reg)
Definition: pci_ops.h:58
static __always_inline void pci_write_config16(const struct device *dev, u16 reg, u16 val)
Definition: pci_ops.h:70
static __always_inline void pci_write_config8(const struct device *dev, u16 reg, u8 val)
Definition: pci_ops.h:64
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define BIOS_SPEW
BIOS_SPEW - Excessively verbose output.
Definition: loglevel.h:142
enum board_config config
Definition: memory.c:448
#define PCI_BRIDGE_CTL_PARITY
Definition: pci_def.h:136
#define PCI_DEVFN(slot, func)
Definition: pci_def.h:548
#define PCI_BRIDGE_CONTROL
Definition: pci_def.h:134
#define PCI_COMMAND_MASTER
Definition: pci_def.h:13
#define PCI_FUNC(devfn)
Definition: pci_def.h:550
#define PCI_CACHE_LINE_SIZE
Definition: pci_def.h:45
#define PCI_COMMAND
Definition: pci_def.h:10
#define PCI_SLOT(devfn)
Definition: pci_def.h:549
void pci_bus_enable_resources(struct device *dev)
Definition: pci_device.c:758
void pci_bus_read_resources(struct device *dev)
Definition: pci_device.c:540
void pci_scan_bridge(struct device *dev)
Scan a PCI bridge and the buses behind the bridge.
Definition: pci_device.c:1598
struct pci_operations pci_dev_ops_pci
Default device operation for PCI devices.
Definition: pci_device.c:911
void pci_dev_set_resources(struct device *dev)
Definition: pci_device.c:691
#define PCI_VID_INTEL
Definition: pci_ids.h:2157
#define RPFN_FNSET(port, func)
Definition: rcba.h:16
#define RPFN
Definition: rcba.h:9
#define RPFN_FNMASK(port)
Definition: rcba.h:18
#define RCBA32(x)
Definition: rcba.h:14
static struct device_operations device_ops
Definition: pcie.c:209
static struct root_port_config rpc
Definition: pcie.c:23
#define NUM_ROOT_PORTS
Definition: pcie.c:13
static void root_port_init_config(struct device *dev)
Definition: pcie.c:91
static void pci_init(struct device *dev)
Definition: pcie.c:41
static void ich_pcie_enable(struct device *dev)
Definition: pcie.c:196
static int root_port_is_first(struct device *dev)
Definition: pcie.c:25
static void root_port_commit_config(struct device *dev)
Definition: pcie.c:137
static int get_num_ports(void)
Definition: pcie.c:82
static void ich_pcie_device_set_func(int index, int pci_func)
Definition: pcie.c:112
static const unsigned short i82801gx_pcie_ids[]
Definition: pcie.c:219
static int root_port_number(struct device *dev)
Definition: pcie.c:36
static const struct pci_driver i82801gx_pcie __pci_driver
Definition: pcie.c:229
static int root_port_is_last(struct device *dev)
Definition: pcie.c:30
#define NULL
Definition: stddef.h:19
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
void(* read_resources)(struct device *dev)
Definition: device.h:39
struct pci_path pci
Definition: path.h:116
Definition: device.h:107
struct device_path path
Definition: device.h:115
DEVTREE_CONST void * chip_info
Definition: device.h:164
unsigned int enabled
Definition: device.h:122
unsigned int devfn
Definition: path.h:54
u32 orig_rpfn
Definition: pcie.c:26
u32 new_rpfn
Definition: pcie.c:27
struct device * ports[MAX_NUM_ROOT_PORTS]
Definition: pcie.c:38
int num_ports
Definition: pcie.c:37