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/pciexp.h>
7 #include <device/pci_ids.h>
8 #include <device/pci_ops.h>
9 #include <assert.h>
10 #include <types.h>
11 
12 #include "chip.h"
13 #include "haswell.h"
14 
15 #if CONFIG(HAVE_ACPI_TABLES)
16 static const char *pcie_acpi_name(const struct device *dev)
17 {
18  assert(dev);
19 
20  if (dev->path.type != DEVICE_PATH_PCI)
21  return NULL;
22 
23  assert(dev->bus);
24  if (dev->bus->secondary == 0)
25  switch (dev->path.pci.devfn) {
26  case PCI_DEVFN(1, 0):
27  return "PEGP";
28  case PCI_DEVFN(1, 1):
29  return "PEG1";
30  case PCI_DEVFN(1, 2):
31  return "PEG2";
32  };
33 
34  struct device *const port = dev->bus->dev;
35  assert(port);
36  assert(port->bus);
37 
38  if (dev->path.pci.devfn == PCI_DEVFN(0, 0) &&
39  port->bus->secondary == 0 &&
40  (port->path.pci.devfn == PCI_DEVFN(1, 0) ||
41  port->path.pci.devfn == PCI_DEVFN(1, 1) ||
42  port->path.pci.devfn == PCI_DEVFN(1, 2)))
43  return "DEV0";
44 
45  return NULL;
46 }
47 #endif
48 
49 static const struct peg_config *get_peg_config(struct device *dev, const uint8_t func)
50 {
51  static const struct peg_config default_config = { 0 };
52 
53  if (!dev || !dev->chip_info)
54  return &default_config;
55 
57 
58  if (func >= ARRAY_SIZE(config->peg_cfg)) {
59  printk(BIOS_ERR, "%s: Found PEG function %u, which doesn't exist on Haswell\n",
60  __func__, func);
61  return &default_config;
62  }
63  return &config->peg_cfg[func];
64 }
65 
66 static void peg_enable(struct device *dev)
67 {
68  const uint8_t func = PCI_FUNC(dev->path.pci.devfn);
69 
70  const struct peg_config *peg_cfg = get_peg_config(dev, func);
71 
72  const bool slot_implemented = !peg_cfg->is_onboard;
73 
74  if (slot_implemented) {
75  /* Default is 1, but register is R/WO and needs to be written to once */
76  pci_or_config16(dev, PEG_CAP, 1 << 8);
77  } else {
78  pci_and_config16(dev, PEG_CAP, ~(1 << 8));
79  }
80 
81  /* Note: this register is write-once */
82  uint32_t slotcap = pci_read_config32(dev, PEG_SLOTCAP);
83 
84  /* Physical slot number (zero for ports connected to onboard devices) */
85  slotcap &= ~(0x1fff << 19);
86  if (slot_implemented) {
87  uint16_t slot_number = peg_cfg->phys_slot_number & 0x1fff;
88  if (slot_number == 0) {
89  /* Slot number must be non-zero and unique */
90  slot_number = func + 1;
91  }
92  slotcap |= slot_number << 19;
93  }
94 
95  /* Default to 1.0 watt scale */
96  slotcap &= ~(3 << 15);
97  slotcap |= (peg_cfg->power_limit_scale & 3) << 15;
98 
100  if (power_limit_value == 0) {
101  /* Default to 75 watts */
102  power_limit_value = 75;
103  }
104  slotcap &= ~(0xff << 7);
105  slotcap |= power_limit_value << 7;
106 
107  pci_write_config32(dev, PEG_SLOTCAP, slotcap);
108 
109  /* Clear errors */
110  pci_write_config16(dev, PCI_STATUS, 0xffff);
111  pci_write_config16(dev, PCI_SEC_STATUS, 0xffff);
112  pci_write_config16(dev, PEG_DSTS, 0xffff);
113  pci_write_config32(dev, PEG_UESTS, 0xffffffff);
114  pci_write_config32(dev, PEG_CESTS, 0xffffffff);
115  pci_write_config32(dev, 0x1f0, 0xffffffff);
116 
117  pci_or_config32(dev, PEG_VC0RCTL, 0x7f << 1);
118 
119  /* Advertise OBFF support using WAKE# signaling only */
120  pci_or_config32(dev, PEG_DCAP2, 1 << 19);
121 
122  pci_or_config32(dev, PEG_UESEV, 1 << 14);
123 
124  /* Select -3.5 dB de-emphasis */
125  pci_or_config32(dev, PEG_LCTL2, 1 << 6);
126 
127  pci_or_config32(dev, PEG_L0SLAT, 1 << 31);
128 
129  pci_update_config32(dev, 0x250, ~(7 << 20), 2 << 20);
130 
131  pci_or_config32(dev, 0x238, 1 << 29);
132 
133  pci_or_config32(dev, 0x1f8, 1 << 16);
134 
135  pci_update_config32(dev, PEG_AFE_PM_TMR, ~0x1f, 0x13);
136 
137  /* Lock DCAP */
138  pci_update_config32(dev, PEG_DCAP, ~0, 0);
139 
140  if (func == 0)
141  pci_or_config32(dev, 0xcd0, 1 << 11);
142 
143  /* Enable support for L0s and L1 */
144  pci_or_config32(dev, PEG_LCAP, 3 << 10);
145 
146  pci_and_config32(dev, 0x200, ~(3 << 26));
147 
148  /* Other fields in this register must not be changed while writing this */
149  pci_or_config16(dev, 0x258, 1 << 2);
150 }
151 
152 static struct device_operations device_ops = {
154  .set_resources = pci_dev_set_resources,
155  .enable_resources = pci_bus_enable_resources,
156  .scan_bus = pciexp_scan_bridge,
157  .reset_bus = pci_bus_reset,
158  .enable = peg_enable,
159  .init = pci_dev_init,
160  .ops_pci = &pci_dev_ops_pci,
161 #if CONFIG(HAVE_ACPI_TABLES)
162  .acpi_name = pcie_acpi_name,
163 #endif
164 };
165 
166 static const unsigned short pci_device_ids[] = {
167  0x0c01, 0x0c05, 0x0c09, 0x0c0d,
168  0x0d01, 0x0d05, 0x0d09, /* Crystal Well */
169  0 };
170 
171 static const struct pci_driver pch_pcie __pci_driver = {
172  .ops = &device_ops,
173  .vendor = PCI_VID_INTEL,
174  .devices = pci_device_ids,
175 };
#define assert(statement)
Definition: assert.h:74
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define printk(level,...)
Definition: stdlib.h:16
#define PEG_CAP
Definition: gm45.h:197
port
Definition: i915.h:29
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_write_config32(const struct device *dev, u16 reg, u32 val)
Definition: pci_ops.h:76
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 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_and_config32(const struct device *dev, u16 reg, u32 andmask)
Definition: pci_ops.h:158
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
enum board_config config
Definition: memory.c:448
static struct device_operations device_ops
Definition: pcie.c:152
static const struct peg_config * get_peg_config(struct device *dev, const uint8_t func)
Definition: pcie.c:49
static const unsigned short pci_device_ids[]
Definition: pcie.c:166
static const struct pci_driver pch_pcie __pci_driver
Definition: pcie.c:171
static void peg_enable(struct device *dev)
Definition: pcie.c:66
@ DEVICE_PATH_PCI
Definition: path.h:9
#define PCI_SEC_STATUS
Definition: pci_def.h:113
#define PCI_STATUS
Definition: pci_def.h:23
#define PCI_DEVFN(slot, func)
Definition: pci_def.h:548
#define PCI_FUNC(devfn)
Definition: pci_def.h:550
void pci_dev_init(struct device *dev)
Default handler: only runs the relevant PCI BIOS.
Definition: pci_device.c:873
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_bus_reset(struct bus *bus)
Definition: pci_device.c:777
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 PEG_DSTS
Definition: pcie_graphics.h:12
#define PEG_AFE_PM_TMR
Definition: pcie_graphics.h:32
#define PEG_LCTL2
Definition: pcie_graphics.h:18
#define PEG_L0SLAT
Definition: pcie_graphics.h:30
#define PEG_DCAP2
Definition: pcie_graphics.h:16
#define PEG_SLOTCAP
Definition: pcie_graphics.h:14
#define PEG_CESTS
Definition: pcie_graphics.h:28
#define PEG_DCAP
Definition: pcie_graphics.h:8
#define PEG_UESEV
Definition: pcie_graphics.h:27
#define PEG_VC0RCTL
Definition: pcie_graphics.h:20
#define PEG_UESTS
Definition: pcie_graphics.h:26
#define PEG_LCAP
Definition: pcie_graphics.h:10
void pciexp_scan_bridge(struct device *dev)
#define NULL
Definition: stddef.h:19
unsigned short uint16_t
Definition: stdint.h:11
unsigned int uint32_t
Definition: stdint.h:14
unsigned char uint8_t
Definition: stdint.h:8
DEVTREE_CONST struct device * dev
Definition: device.h:78
uint16_t secondary
Definition: device.h:84
void(* read_resources)(struct device *dev)
Definition: device.h:39
struct pci_path pci
Definition: path.h:116
enum device_path_type type
Definition: path.h:114
Definition: device.h:107
struct device_path path
Definition: device.h:115
DEVTREE_CONST struct bus * bus
Definition: device.h:108
DEVTREE_CONST void * chip_info
Definition: device.h:164
unsigned int devfn
Definition: path.h:54
Definition: chip.h:9
uint16_t phys_slot_number
Definition: chip.h:13
uint8_t power_limit_value
Definition: chip.h:12
uint8_t power_limit_scale
Definition: chip.h:11
bool is_onboard
Definition: chip.h:10