blob: 330c7e34786c059ca1ffd652aa3b3f1914af04e7 [file] [log] [blame]
Uwe Hermannb80dbf02007-04-22 19:08:13 +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 * Copyright (C) 2005 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Uwe Hermannb80dbf02007-04-22 19:08:13 +000015 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000016
17#include <console/console.h>
Duncan Laurie90dcdd42011-10-25 14:15:11 -070018#include <delay.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000019#include <device/device.h>
20#include <device/pci.h>
21#include <device/pci_ids.h>
22#include <device/pciexp.h>
23
Kenji Chen31c6e632014-10-04 01:14:44 +080024#if IS_ENABLED(CONFIG_MMCONF_SUPPORT)
25unsigned int pciexp_find_extended_cap(device_t dev, unsigned int cap)
26{
27 unsigned int this_cap_offset, next_cap_offset;
28 unsigned int this_cap, cafe;
29
30 this_cap_offset = PCIE_EXT_CAP_OFFSET;
31 do {
32 this_cap = pci_mmio_read_config32(dev, this_cap_offset);
33 next_cap_offset = this_cap >> 20;
34 this_cap &= 0xffff;
35 cafe = pci_mmio_read_config32(dev, this_cap_offset + 4);
36 cafe &= 0xffff;
37 if (this_cap == cap)
38 return this_cap_offset;
39 else if (cafe == cap)
40 return this_cap_offset + 4;
41 else
42 this_cap_offset = next_cap_offset;
43 } while (next_cap_offset != 0);
44
45 return 0;
46}
47#endif
48
Duncan Laurie90dcdd42011-10-25 14:15:11 -070049#if CONFIG_PCIEXP_COMMON_CLOCK
50/*
51 * Re-train a PCIe link
52 */
53#define PCIE_TRAIN_RETRY 10000
54static int pciexp_retrain_link(device_t dev, unsigned cap)
55{
56 unsigned try = PCIE_TRAIN_RETRY;
57 u16 lnk;
58
59 /* Start link retraining */
60 lnk = pci_read_config16(dev, cap + PCI_EXP_LNKCTL);
61 lnk |= PCI_EXP_LNKCTL_RL;
62 pci_write_config16(dev, cap + PCI_EXP_LNKCTL, lnk);
63
64 /* Wait for training to complete */
65 while (try--) {
66 lnk = pci_read_config16(dev, cap + PCI_EXP_LNKSTA);
67 if (!(lnk & PCI_EXP_LNKSTA_LT))
68 return 0;
69 udelay(100);
70 }
71
72 printk(BIOS_ERR, "%s: Link Retrain timeout\n", dev_path(dev));
73 return -1;
74}
75
76/*
77 * Check the Slot Clock Configuration for root port and endpoint
78 * and enable Common Clock Configuration if possible. If CCC is
79 * enabled the link must be retrained.
80 */
81static void pciexp_enable_common_clock(device_t root, unsigned root_cap,
82 device_t endp, unsigned endp_cap)
83{
84 u16 root_scc, endp_scc, lnkctl;
85
86 /* Get Slot Clock Configuration for root port */
87 root_scc = pci_read_config16(root, root_cap + PCI_EXP_LNKSTA);
88 root_scc &= PCI_EXP_LNKSTA_SLC;
89
90 /* Get Slot Clock Configuration for endpoint */
91 endp_scc = pci_read_config16(endp, endp_cap + PCI_EXP_LNKSTA);
92 endp_scc &= PCI_EXP_LNKSTA_SLC;
93
94 /* Enable Common Clock Configuration and retrain */
95 if (root_scc && endp_scc) {
96 printk(BIOS_INFO, "Enabling Common Clock Configuration\n");
97
98 /* Set in endpoint */
99 lnkctl = pci_read_config16(endp, endp_cap + PCI_EXP_LNKCTL);
100 lnkctl |= PCI_EXP_LNKCTL_CCC;
101 pci_write_config16(endp, endp_cap + PCI_EXP_LNKCTL, lnkctl);
102
103 /* Set in root port */
104 lnkctl = pci_read_config16(root, root_cap + PCI_EXP_LNKCTL);
105 lnkctl |= PCI_EXP_LNKCTL_CCC;
106 pci_write_config16(root, root_cap + PCI_EXP_LNKCTL, lnkctl);
107
108 /* Retrain link if CCC was enabled */
109 pciexp_retrain_link(root, root_cap);
110 }
111}
112#endif /* CONFIG_PCIEXP_COMMON_CLOCK */
113
Kane Chen18cb1342014-10-01 11:13:54 +0800114#if CONFIG_PCIEXP_CLK_PM
115static void pciexp_enable_clock_power_pm(device_t endp, unsigned endp_cap)
116{
117 /* check if per port clk req is supported in device */
118 u32 endp_ca;
119 u16 lnkctl;
120 endp_ca = pci_read_config32(endp, endp_cap + PCI_EXP_LNKCAP);
121 if ((endp_ca & PCI_EXP_CLK_PM) == 0) {
122 printk(BIOS_INFO, "PCIE CLK PM is not supported by endpoint");
123 return;
124 }
125 lnkctl = pci_read_config16(endp, endp_cap + PCI_EXP_LNKCTL);
126 lnkctl = lnkctl | PCI_EXP_EN_CLK_PM;
127 pci_write_config16(endp, endp_cap + PCI_EXP_LNKCTL, lnkctl);
128}
129#endif /* CONFIG_PCIEXP_CLK_PM */
130
Kenji Chen31c6e632014-10-04 01:14:44 +0800131#if IS_ENABLED(CONFIG_PCIEXP_L1_SUB_STATE) && IS_ENABLED(CONFIG_MMCONF_SUPPORT)
132static void pcie_update_cfg(device_t dev, int reg, u32 mask, u32 or)
133{
134 u32 reg32;
135
136 reg32 = pci_mmio_read_config32(dev, reg);
137 reg32 &= mask;
138 reg32 |= or;
139 pci_mmio_write_config32(dev, reg, reg32);
140}
141
142static void pciexp_config_max_latency(device_t root, device_t dev)
143{
144 unsigned int cap;
145 cap = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_LTR_ID);
Pratik Prajapati0cd0d282015-06-09 12:06:20 -0700146 if ((cap) && (root->ops->ops_pci != NULL) &&
147 (root->ops->ops_pci->set_L1_ss_latency != NULL))
148 root->ops->ops_pci->set_L1_ss_latency(dev, cap + 4);
Kenji Chen31c6e632014-10-04 01:14:44 +0800149}
150
151static void pciexp_enable_ltr(device_t dev)
152{
153 unsigned int cap;
154 cap = pci_find_capability(dev, PCI_CAP_ID_PCIE);
Pratik Prajapati0cd0d282015-06-09 12:06:20 -0700155 if(!cap) {
156 printk(BIOS_INFO, "Failed to enable LTR for dev = %s\n",
157 dev_path(dev));
158 return;
159 }
Kenji Chen31c6e632014-10-04 01:14:44 +0800160 pcie_update_cfg(dev, cap + 0x28, ~(1 << 10), 1 << 10);
161}
162
163static unsigned char pciexp_L1_substate_cal(device_t dev, unsigned int endp_cap,
164 unsigned int *data)
165{
166 unsigned char mult[4] = {2, 10, 100, 0};
167
168 unsigned int L1SubStateSupport = *data & 0xf;
169 unsigned int comm_mode_rst_time = (*data >> 8) & 0xff;
170 unsigned int power_on_scale = (*data >> 16) & 0x3;
171 unsigned int power_on_value = (*data >> 19) & 0x1f;
172
173 unsigned int endp_data = pci_mmio_read_config32(dev, endp_cap + 4);
174 unsigned int endp_L1SubStateSupport = endp_data & 0xf;
175 unsigned int endp_comm_mode_restore_time = (endp_data >> 8) & 0xff;
176 unsigned int endp_power_on_scale = (endp_data >> 16) & 0x3;
177 unsigned int endp_power_on_value = (endp_data >> 19) & 0x1f;
178
179 L1SubStateSupport &= endp_L1SubStateSupport;
180
181 if (L1SubStateSupport == 0)
182 return 0;
183
184 if (power_on_value * mult[power_on_scale] <
185 endp_power_on_value * mult[endp_power_on_scale]) {
186 power_on_value = endp_power_on_value;
187 power_on_scale = endp_power_on_scale;
188 }
189 if (comm_mode_rst_time < endp_comm_mode_restore_time)
190 comm_mode_rst_time = endp_comm_mode_restore_time;
191
192 *data = (comm_mode_rst_time << 8) | (power_on_scale << 16)
193 | (power_on_value << 19) | L1SubStateSupport;
194
195 return 1;
196}
197
198static void pciexp_L1_substate_commit(device_t root, device_t dev,
199 unsigned int root_cap, unsigned int end_cap)
200{
201 device_t dev_t;
202 unsigned char L1_ss_ok;
203 unsigned int rp_L1_support = pci_mmio_read_config32(root, root_cap + 4);
204 unsigned int L1SubStateSupport;
205 unsigned int comm_mode_rst_time;
206 unsigned int power_on_scale;
207 unsigned int endp_power_on_value;
208
209 for (dev_t = dev; dev_t; dev_t = dev_t->sibling) {
210 /*
211 * rp_L1_support is init'd above from root port.
212 * it needs coordination with endpoints to reach in common.
213 * if certain endpoint doesn't support L1 Sub-State, abort
214 * this feature enabling.
215 */
216 L1_ss_ok = pciexp_L1_substate_cal(dev_t, end_cap,
217 &rp_L1_support);
218 if (!L1_ss_ok)
219 return;
220 }
221
222 L1SubStateSupport = rp_L1_support & 0xf;
223 comm_mode_rst_time = (rp_L1_support >> 8) & 0xff;
224 power_on_scale = (rp_L1_support >> 16) & 0x3;
225 endp_power_on_value = (rp_L1_support >> 19) & 0x1f;
226
227 printk(BIOS_INFO, "L1 Sub-State supported from root port %d\n",
228 root->path.pci.devfn >> 3);
229 printk(BIOS_INFO, "L1 Sub-State Support = 0x%x\n", L1SubStateSupport);
230 printk(BIOS_INFO, "CommonModeRestoreTime = 0x%x\n", comm_mode_rst_time);
231 printk(BIOS_INFO, "Power On Value = 0x%x, Power On Scale = 0x%x\n",
232 endp_power_on_value, power_on_scale);
233
234 pciexp_enable_ltr(root);
235
236 pcie_update_cfg(root, root_cap + 0x08, ~0xff00,
237 (comm_mode_rst_time << 8));
238
239 pcie_update_cfg(root, root_cap + 0x0c , 0xffffff04,
240 (endp_power_on_value << 3) | (power_on_scale));
241
242 pcie_update_cfg(root, root_cap + 0x08, ~0xe3ff0000,
243 (1 << 21) | (1 << 23) | (1 << 30));
244
Kenji Chena874a7c2015-01-30 13:57:42 +0800245 pcie_update_cfg(root, root_cap + 0x08, ~0x1f,
Kenji Chen31c6e632014-10-04 01:14:44 +0800246 L1SubStateSupport);
247
248 for (dev_t = dev; dev_t; dev_t = dev_t->sibling) {
Kenji Chen31c6e632014-10-04 01:14:44 +0800249 pcie_update_cfg(dev_t, end_cap + 0x0c , 0xffffff04,
250 (endp_power_on_value << 3) | (power_on_scale));
251
252 pcie_update_cfg(dev_t, end_cap + 0x08, ~0xe3ff0000,
253 (1 << 21) | (1 << 23) | (1 << 30));
254
Kenji Chena874a7c2015-01-30 13:57:42 +0800255 pcie_update_cfg(dev_t, end_cap + 0x08, ~0x1f,
Kenji Chen31c6e632014-10-04 01:14:44 +0800256 L1SubStateSupport);
257
258 pciexp_enable_ltr(dev_t);
259
260 pciexp_config_max_latency(root, dev_t);
261 }
262}
263
264static void pciexp_config_L1_sub_state(device_t root, device_t dev)
265{
266 unsigned int root_cap, end_cap;
267
268 /* Do it for function 0 only */
269 if (dev->path.pci.devfn & 0x7)
270 return;
271
272 root_cap = pciexp_find_extended_cap(root, PCIE_EXT_CAP_L1SS_ID);
273 if (!root_cap)
274 return;
275
276 end_cap = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_L1SS_ID);
277 if (!end_cap) {
278 end_cap = pciexp_find_extended_cap(dev, 0xcafe);
279 if (!end_cap)
280 return;
281 }
282
283 pciexp_L1_substate_commit(root, dev, root_cap, end_cap);
284}
285#endif /* CONFIG_PCIEXP_L1_SUB_STATE */
286
Duncan Laurie90dcdd42011-10-25 14:15:11 -0700287#if CONFIG_PCIEXP_ASPM
288/*
289 * Determine the ASPM L0s or L1 exit latency for a link
290 * by checking both root port and endpoint and returning
291 * the highest latency value.
292 */
293static int pciexp_aspm_latency(device_t root, unsigned root_cap,
294 device_t endp, unsigned endp_cap,
295 enum aspm_type type)
296{
297 int root_lat = 0, endp_lat = 0;
298 u32 root_lnkcap, endp_lnkcap;
299
300 root_lnkcap = pci_read_config32(root, root_cap + PCI_EXP_LNKCAP);
301 endp_lnkcap = pci_read_config32(endp, endp_cap + PCI_EXP_LNKCAP);
302
303 /* Make sure the link supports this ASPM type by checking
304 * capability bits 11:10 with aspm_type offset by 1 */
305 if (!(root_lnkcap & (1 << (type + 9))) ||
306 !(endp_lnkcap & (1 << (type + 9))))
307 return -1;
308
309 /* Find the one with higher latency */
310 switch (type) {
311 case PCIE_ASPM_L0S:
312 root_lat = (root_lnkcap & PCI_EXP_LNKCAP_L0SEL) >> 12;
313 endp_lat = (endp_lnkcap & PCI_EXP_LNKCAP_L0SEL) >> 12;
314 break;
315 case PCIE_ASPM_L1:
316 root_lat = (root_lnkcap & PCI_EXP_LNKCAP_L1EL) >> 15;
317 endp_lat = (endp_lnkcap & PCI_EXP_LNKCAP_L1EL) >> 15;
318 break;
319 default:
320 return -1;
321 }
322
323 return (endp_lat > root_lat) ? endp_lat : root_lat;
324}
325
326/*
327 * Enable ASPM on PCIe root port and endpoint.
328 *
329 * Returns APMC value:
330 * -1 = Error
331 * 0 = no ASPM
332 * 1 = L0s Enabled
333 * 2 = L1 Enabled
334 * 3 = L0s and L1 Enabled
335 */
336static enum aspm_type pciexp_enable_aspm(device_t root, unsigned root_cap,
337 device_t endp, unsigned endp_cap)
338{
339 const char *aspm_type_str[] = { "None", "L0s", "L1", "L0s and L1" };
340 enum aspm_type apmc = PCIE_ASPM_NONE;
341 int exit_latency, ok_latency;
342 u16 lnkctl;
343 u32 devcap;
344
345 /* Get endpoint device capabilities for acceptable limits */
346 devcap = pci_read_config32(endp, endp_cap + PCI_EXP_DEVCAP);
347
348 /* Enable L0s if it is within endpoint acceptable limit */
349 ok_latency = (devcap & PCI_EXP_DEVCAP_L0S) >> 6;
350 exit_latency = pciexp_aspm_latency(root, root_cap, endp, endp_cap,
351 PCIE_ASPM_L0S);
352 if (exit_latency >= 0 && exit_latency <= ok_latency)
353 apmc |= PCIE_ASPM_L0S;
354
355 /* Enable L1 if it is within endpoint acceptable limit */
356 ok_latency = (devcap & PCI_EXP_DEVCAP_L1) >> 9;
357 exit_latency = pciexp_aspm_latency(root, root_cap, endp, endp_cap,
358 PCIE_ASPM_L1);
359 if (exit_latency >= 0 && exit_latency <= ok_latency)
360 apmc |= PCIE_ASPM_L1;
361
362 if (apmc != PCIE_ASPM_NONE) {
363 /* Set APMC in root port first */
364 lnkctl = pci_read_config16(root, root_cap + PCI_EXP_LNKCTL);
365 lnkctl |= apmc;
366 pci_write_config16(root, root_cap + PCI_EXP_LNKCTL, lnkctl);
367
368 /* Set APMC in endpoint device next */
369 lnkctl = pci_read_config16(endp, endp_cap + PCI_EXP_LNKCTL);
370 lnkctl |= apmc;
371 pci_write_config16(endp, endp_cap + PCI_EXP_LNKCTL, lnkctl);
372 }
373
374 printk(BIOS_INFO, "ASPM: Enabled %s\n", aspm_type_str[apmc]);
375 return apmc;
376}
377#endif /* CONFIG_PCIEXP_ASPM */
378
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000379static void pciexp_tune_dev(device_t dev)
380{
Duncan Laurie90dcdd42011-10-25 14:15:11 -0700381 device_t root = dev->bus->dev;
382 unsigned int root_cap, cap;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000383
384 cap = pci_find_capability(dev, PCI_CAP_ID_PCIE);
Uwe Hermannd453dd02010-10-18 00:00:57 +0000385 if (!cap)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000386 return;
Uwe Hermannd453dd02010-10-18 00:00:57 +0000387
Duncan Laurie90dcdd42011-10-25 14:15:11 -0700388 root_cap = pci_find_capability(root, PCI_CAP_ID_PCIE);
389 if (!root_cap)
390 return;
Stefan Reinauerf6eb88a2010-01-17 13:54:08 +0000391
Duncan Laurie90dcdd42011-10-25 14:15:11 -0700392#if CONFIG_PCIEXP_COMMON_CLOCK
393 /* Check for and enable Common Clock */
394 pciexp_enable_common_clock(root, root_cap, dev, cap);
395#endif
Uwe Hermanne4870472010-11-04 23:23:47 +0000396
Kane Chen18cb1342014-10-01 11:13:54 +0800397#if CONFIG_PCIEXP_CLK_PM
398 /* Check if per port CLK req is supported by endpoint*/
399 pciexp_enable_clock_power_pm(dev, cap);
400#endif
401
Kenji Chen31c6e632014-10-04 01:14:44 +0800402#if CONFIG_PCIEXP_L1_SUB_STATE
403 /* Enable L1 Sub-State when both root port and endpoint support */
404 pciexp_config_L1_sub_state(root, dev);
405#endif /* CONFIG_PCIEXP_L1_SUB_STATE */
406
Duncan Laurie90dcdd42011-10-25 14:15:11 -0700407#if CONFIG_PCIEXP_ASPM
408 /* Check for and enable ASPM */
409 enum aspm_type apmc = pciexp_enable_aspm(root, root_cap, dev, cap);
410
411 if (apmc != PCIE_ASPM_NONE) {
412 /* Enable ASPM role based error reporting. */
413 u32 reg32 = pci_read_config32(dev, cap + PCI_EXP_DEVCAP);
414 reg32 |= PCI_EXP_DEVCAP_RBER;
415 pci_write_config32(dev, cap + PCI_EXP_DEVCAP, reg32);
416 }
Stefan Reinauerf6eb88a2010-01-17 13:54:08 +0000417#endif
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000418}
419
Kyösti Mälkkide271a82015-03-18 13:09:47 +0200420void pciexp_scan_bus(struct bus *bus, unsigned int min_devfn,
421 unsigned int max_devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000422{
423 device_t child;
Uwe Hermannd453dd02010-10-18 00:00:57 +0000424
Kyösti Mälkkide271a82015-03-18 13:09:47 +0200425 pci_scan_bus(bus, min_devfn, max_devfn);
Uwe Hermannd453dd02010-10-18 00:00:57 +0000426
427 for (child = bus->children; child; child = child->sibling) {
428 if ((child->path.pci.devfn < min_devfn) ||
429 (child->path.pci.devfn > max_devfn)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000430 continue;
431 }
432 pciexp_tune_dev(child);
433 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000434}
435
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200436void pciexp_scan_bridge(device_t dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000437{
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200438 do_pci_scan_bridge(dev, pciexp_scan_bus);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000439}
440
441/** Default device operations for PCI Express bridges */
442static struct pci_operations pciexp_bus_ops_pci = {
443 .set_subsystem = 0,
444};
445
446struct device_operations default_pciexp_ops_bus = {
447 .read_resources = pci_bus_read_resources,
448 .set_resources = pci_dev_set_resources,
449 .enable_resources = pci_bus_enable_resources,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000450 .init = 0,
451 .scan_bus = pciexp_scan_bridge,
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000452 .enable = 0,
453 .reset_bus = pci_bus_reset,
454 .ops_pci = &pciexp_bus_ops_pci,
455};