Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 3 | * |
| 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 Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 15 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 16 | |
| 17 | #include <console/console.h> |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 18 | #include <delay.h> |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 19 | #include <device/device.h> |
| 20 | #include <device/pci.h> |
| 21 | #include <device/pci_ids.h> |
| 22 | #include <device/pciexp.h> |
| 23 | |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 24 | unsigned int pciexp_find_extended_cap(device_t dev, unsigned int cap) |
| 25 | { |
| 26 | unsigned int this_cap_offset, next_cap_offset; |
| 27 | unsigned int this_cap, cafe; |
| 28 | |
| 29 | this_cap_offset = PCIE_EXT_CAP_OFFSET; |
| 30 | do { |
Kyösti Mälkki | 91bfa8e | 2016-11-20 20:39:56 +0200 | [diff] [blame^] | 31 | this_cap = pci_read_config32(dev, this_cap_offset); |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 32 | next_cap_offset = this_cap >> 20; |
| 33 | this_cap &= 0xffff; |
Kyösti Mälkki | 91bfa8e | 2016-11-20 20:39:56 +0200 | [diff] [blame^] | 34 | cafe = pci_read_config32(dev, this_cap_offset + 4); |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 35 | cafe &= 0xffff; |
| 36 | if (this_cap == cap) |
| 37 | return this_cap_offset; |
| 38 | else if (cafe == cap) |
| 39 | return this_cap_offset + 4; |
| 40 | else |
| 41 | this_cap_offset = next_cap_offset; |
| 42 | } while (next_cap_offset != 0); |
| 43 | |
| 44 | return 0; |
| 45 | } |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 46 | |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Re-train a PCIe link |
| 49 | */ |
| 50 | #define PCIE_TRAIN_RETRY 10000 |
| 51 | static int pciexp_retrain_link(device_t dev, unsigned cap) |
| 52 | { |
| 53 | unsigned try = PCIE_TRAIN_RETRY; |
| 54 | u16 lnk; |
| 55 | |
| 56 | /* Start link retraining */ |
| 57 | lnk = pci_read_config16(dev, cap + PCI_EXP_LNKCTL); |
| 58 | lnk |= PCI_EXP_LNKCTL_RL; |
| 59 | pci_write_config16(dev, cap + PCI_EXP_LNKCTL, lnk); |
| 60 | |
| 61 | /* Wait for training to complete */ |
| 62 | while (try--) { |
| 63 | lnk = pci_read_config16(dev, cap + PCI_EXP_LNKSTA); |
| 64 | if (!(lnk & PCI_EXP_LNKSTA_LT)) |
| 65 | return 0; |
| 66 | udelay(100); |
| 67 | } |
| 68 | |
| 69 | printk(BIOS_ERR, "%s: Link Retrain timeout\n", dev_path(dev)); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Check the Slot Clock Configuration for root port and endpoint |
| 75 | * and enable Common Clock Configuration if possible. If CCC is |
| 76 | * enabled the link must be retrained. |
| 77 | */ |
| 78 | static void pciexp_enable_common_clock(device_t root, unsigned root_cap, |
| 79 | device_t endp, unsigned endp_cap) |
| 80 | { |
| 81 | u16 root_scc, endp_scc, lnkctl; |
| 82 | |
| 83 | /* Get Slot Clock Configuration for root port */ |
| 84 | root_scc = pci_read_config16(root, root_cap + PCI_EXP_LNKSTA); |
| 85 | root_scc &= PCI_EXP_LNKSTA_SLC; |
| 86 | |
| 87 | /* Get Slot Clock Configuration for endpoint */ |
| 88 | endp_scc = pci_read_config16(endp, endp_cap + PCI_EXP_LNKSTA); |
| 89 | endp_scc &= PCI_EXP_LNKSTA_SLC; |
| 90 | |
| 91 | /* Enable Common Clock Configuration and retrain */ |
| 92 | if (root_scc && endp_scc) { |
| 93 | printk(BIOS_INFO, "Enabling Common Clock Configuration\n"); |
| 94 | |
| 95 | /* Set in endpoint */ |
| 96 | lnkctl = pci_read_config16(endp, endp_cap + PCI_EXP_LNKCTL); |
| 97 | lnkctl |= PCI_EXP_LNKCTL_CCC; |
| 98 | pci_write_config16(endp, endp_cap + PCI_EXP_LNKCTL, lnkctl); |
| 99 | |
| 100 | /* Set in root port */ |
| 101 | lnkctl = pci_read_config16(root, root_cap + PCI_EXP_LNKCTL); |
| 102 | lnkctl |= PCI_EXP_LNKCTL_CCC; |
| 103 | pci_write_config16(root, root_cap + PCI_EXP_LNKCTL, lnkctl); |
| 104 | |
| 105 | /* Retrain link if CCC was enabled */ |
| 106 | pciexp_retrain_link(root, root_cap); |
| 107 | } |
| 108 | } |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 109 | |
Kane Chen | 18cb134 | 2014-10-01 11:13:54 +0800 | [diff] [blame] | 110 | static void pciexp_enable_clock_power_pm(device_t endp, unsigned endp_cap) |
| 111 | { |
| 112 | /* check if per port clk req is supported in device */ |
| 113 | u32 endp_ca; |
| 114 | u16 lnkctl; |
| 115 | endp_ca = pci_read_config32(endp, endp_cap + PCI_EXP_LNKCAP); |
| 116 | if ((endp_ca & PCI_EXP_CLK_PM) == 0) { |
| 117 | printk(BIOS_INFO, "PCIE CLK PM is not supported by endpoint"); |
| 118 | return; |
| 119 | } |
| 120 | lnkctl = pci_read_config16(endp, endp_cap + PCI_EXP_LNKCTL); |
| 121 | lnkctl = lnkctl | PCI_EXP_EN_CLK_PM; |
| 122 | pci_write_config16(endp, endp_cap + PCI_EXP_LNKCTL, lnkctl); |
| 123 | } |
Kane Chen | 18cb134 | 2014-10-01 11:13:54 +0800 | [diff] [blame] | 124 | |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 125 | static void pcie_update_cfg(device_t dev, int reg, u32 mask, u32 or) |
| 126 | { |
| 127 | u32 reg32; |
| 128 | |
Kyösti Mälkki | 91bfa8e | 2016-11-20 20:39:56 +0200 | [diff] [blame^] | 129 | reg32 = pci_read_config32(dev, reg); |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 130 | reg32 &= mask; |
| 131 | reg32 |= or; |
Kyösti Mälkki | 91bfa8e | 2016-11-20 20:39:56 +0200 | [diff] [blame^] | 132 | pci_write_config32(dev, reg, reg32); |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void pciexp_config_max_latency(device_t root, device_t dev) |
| 136 | { |
| 137 | unsigned int cap; |
| 138 | cap = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_LTR_ID); |
Pratik Prajapati | 0cd0d28 | 2015-06-09 12:06:20 -0700 | [diff] [blame] | 139 | if ((cap) && (root->ops->ops_pci != NULL) && |
| 140 | (root->ops->ops_pci->set_L1_ss_latency != NULL)) |
| 141 | root->ops->ops_pci->set_L1_ss_latency(dev, cap + 4); |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void pciexp_enable_ltr(device_t dev) |
| 145 | { |
| 146 | unsigned int cap; |
| 147 | cap = pci_find_capability(dev, PCI_CAP_ID_PCIE); |
Pratik Prajapati | 0cd0d28 | 2015-06-09 12:06:20 -0700 | [diff] [blame] | 148 | if(!cap) { |
| 149 | printk(BIOS_INFO, "Failed to enable LTR for dev = %s\n", |
| 150 | dev_path(dev)); |
| 151 | return; |
| 152 | } |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 153 | pcie_update_cfg(dev, cap + 0x28, ~(1 << 10), 1 << 10); |
| 154 | } |
| 155 | |
| 156 | static unsigned char pciexp_L1_substate_cal(device_t dev, unsigned int endp_cap, |
| 157 | unsigned int *data) |
| 158 | { |
| 159 | unsigned char mult[4] = {2, 10, 100, 0}; |
| 160 | |
| 161 | unsigned int L1SubStateSupport = *data & 0xf; |
| 162 | unsigned int comm_mode_rst_time = (*data >> 8) & 0xff; |
| 163 | unsigned int power_on_scale = (*data >> 16) & 0x3; |
| 164 | unsigned int power_on_value = (*data >> 19) & 0x1f; |
| 165 | |
Kyösti Mälkki | 91bfa8e | 2016-11-20 20:39:56 +0200 | [diff] [blame^] | 166 | unsigned int endp_data = pci_read_config32(dev, endp_cap + 4); |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 167 | unsigned int endp_L1SubStateSupport = endp_data & 0xf; |
| 168 | unsigned int endp_comm_mode_restore_time = (endp_data >> 8) & 0xff; |
| 169 | unsigned int endp_power_on_scale = (endp_data >> 16) & 0x3; |
| 170 | unsigned int endp_power_on_value = (endp_data >> 19) & 0x1f; |
| 171 | |
| 172 | L1SubStateSupport &= endp_L1SubStateSupport; |
| 173 | |
| 174 | if (L1SubStateSupport == 0) |
| 175 | return 0; |
| 176 | |
| 177 | if (power_on_value * mult[power_on_scale] < |
| 178 | endp_power_on_value * mult[endp_power_on_scale]) { |
| 179 | power_on_value = endp_power_on_value; |
| 180 | power_on_scale = endp_power_on_scale; |
| 181 | } |
| 182 | if (comm_mode_rst_time < endp_comm_mode_restore_time) |
| 183 | comm_mode_rst_time = endp_comm_mode_restore_time; |
| 184 | |
| 185 | *data = (comm_mode_rst_time << 8) | (power_on_scale << 16) |
| 186 | | (power_on_value << 19) | L1SubStateSupport; |
| 187 | |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | static void pciexp_L1_substate_commit(device_t root, device_t dev, |
| 192 | unsigned int root_cap, unsigned int end_cap) |
| 193 | { |
| 194 | device_t dev_t; |
| 195 | unsigned char L1_ss_ok; |
Kyösti Mälkki | 91bfa8e | 2016-11-20 20:39:56 +0200 | [diff] [blame^] | 196 | unsigned int rp_L1_support = pci_read_config32(root, root_cap + 4); |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 197 | unsigned int L1SubStateSupport; |
| 198 | unsigned int comm_mode_rst_time; |
| 199 | unsigned int power_on_scale; |
| 200 | unsigned int endp_power_on_value; |
| 201 | |
| 202 | for (dev_t = dev; dev_t; dev_t = dev_t->sibling) { |
| 203 | /* |
| 204 | * rp_L1_support is init'd above from root port. |
| 205 | * it needs coordination with endpoints to reach in common. |
| 206 | * if certain endpoint doesn't support L1 Sub-State, abort |
| 207 | * this feature enabling. |
| 208 | */ |
| 209 | L1_ss_ok = pciexp_L1_substate_cal(dev_t, end_cap, |
| 210 | &rp_L1_support); |
| 211 | if (!L1_ss_ok) |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | L1SubStateSupport = rp_L1_support & 0xf; |
| 216 | comm_mode_rst_time = (rp_L1_support >> 8) & 0xff; |
| 217 | power_on_scale = (rp_L1_support >> 16) & 0x3; |
| 218 | endp_power_on_value = (rp_L1_support >> 19) & 0x1f; |
| 219 | |
| 220 | printk(BIOS_INFO, "L1 Sub-State supported from root port %d\n", |
| 221 | root->path.pci.devfn >> 3); |
| 222 | printk(BIOS_INFO, "L1 Sub-State Support = 0x%x\n", L1SubStateSupport); |
| 223 | printk(BIOS_INFO, "CommonModeRestoreTime = 0x%x\n", comm_mode_rst_time); |
| 224 | printk(BIOS_INFO, "Power On Value = 0x%x, Power On Scale = 0x%x\n", |
| 225 | endp_power_on_value, power_on_scale); |
| 226 | |
| 227 | pciexp_enable_ltr(root); |
| 228 | |
| 229 | pcie_update_cfg(root, root_cap + 0x08, ~0xff00, |
| 230 | (comm_mode_rst_time << 8)); |
| 231 | |
| 232 | pcie_update_cfg(root, root_cap + 0x0c , 0xffffff04, |
| 233 | (endp_power_on_value << 3) | (power_on_scale)); |
| 234 | |
| 235 | pcie_update_cfg(root, root_cap + 0x08, ~0xe3ff0000, |
| 236 | (1 << 21) | (1 << 23) | (1 << 30)); |
| 237 | |
Kenji Chen | a874a7c | 2015-01-30 13:57:42 +0800 | [diff] [blame] | 238 | pcie_update_cfg(root, root_cap + 0x08, ~0x1f, |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 239 | L1SubStateSupport); |
| 240 | |
| 241 | for (dev_t = dev; dev_t; dev_t = dev_t->sibling) { |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 242 | pcie_update_cfg(dev_t, end_cap + 0x0c , 0xffffff04, |
| 243 | (endp_power_on_value << 3) | (power_on_scale)); |
| 244 | |
| 245 | pcie_update_cfg(dev_t, end_cap + 0x08, ~0xe3ff0000, |
| 246 | (1 << 21) | (1 << 23) | (1 << 30)); |
| 247 | |
Kenji Chen | a874a7c | 2015-01-30 13:57:42 +0800 | [diff] [blame] | 248 | pcie_update_cfg(dev_t, end_cap + 0x08, ~0x1f, |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 249 | L1SubStateSupport); |
| 250 | |
| 251 | pciexp_enable_ltr(dev_t); |
| 252 | |
| 253 | pciexp_config_max_latency(root, dev_t); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | static void pciexp_config_L1_sub_state(device_t root, device_t dev) |
| 258 | { |
| 259 | unsigned int root_cap, end_cap; |
| 260 | |
| 261 | /* Do it for function 0 only */ |
| 262 | if (dev->path.pci.devfn & 0x7) |
| 263 | return; |
| 264 | |
| 265 | root_cap = pciexp_find_extended_cap(root, PCIE_EXT_CAP_L1SS_ID); |
| 266 | if (!root_cap) |
| 267 | return; |
| 268 | |
| 269 | end_cap = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_L1SS_ID); |
| 270 | if (!end_cap) { |
| 271 | end_cap = pciexp_find_extended_cap(dev, 0xcafe); |
| 272 | if (!end_cap) |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | pciexp_L1_substate_commit(root, dev, root_cap, end_cap); |
| 277 | } |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 278 | |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 279 | /* |
| 280 | * Determine the ASPM L0s or L1 exit latency for a link |
| 281 | * by checking both root port and endpoint and returning |
| 282 | * the highest latency value. |
| 283 | */ |
| 284 | static int pciexp_aspm_latency(device_t root, unsigned root_cap, |
| 285 | device_t endp, unsigned endp_cap, |
| 286 | enum aspm_type type) |
| 287 | { |
| 288 | int root_lat = 0, endp_lat = 0; |
| 289 | u32 root_lnkcap, endp_lnkcap; |
| 290 | |
| 291 | root_lnkcap = pci_read_config32(root, root_cap + PCI_EXP_LNKCAP); |
| 292 | endp_lnkcap = pci_read_config32(endp, endp_cap + PCI_EXP_LNKCAP); |
| 293 | |
| 294 | /* Make sure the link supports this ASPM type by checking |
| 295 | * capability bits 11:10 with aspm_type offset by 1 */ |
| 296 | if (!(root_lnkcap & (1 << (type + 9))) || |
| 297 | !(endp_lnkcap & (1 << (type + 9)))) |
| 298 | return -1; |
| 299 | |
| 300 | /* Find the one with higher latency */ |
| 301 | switch (type) { |
| 302 | case PCIE_ASPM_L0S: |
| 303 | root_lat = (root_lnkcap & PCI_EXP_LNKCAP_L0SEL) >> 12; |
| 304 | endp_lat = (endp_lnkcap & PCI_EXP_LNKCAP_L0SEL) >> 12; |
| 305 | break; |
| 306 | case PCIE_ASPM_L1: |
| 307 | root_lat = (root_lnkcap & PCI_EXP_LNKCAP_L1EL) >> 15; |
| 308 | endp_lat = (endp_lnkcap & PCI_EXP_LNKCAP_L1EL) >> 15; |
| 309 | break; |
| 310 | default: |
| 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | return (endp_lat > root_lat) ? endp_lat : root_lat; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Enable ASPM on PCIe root port and endpoint. |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 319 | */ |
Kyösti Mälkki | df96a70 | 2016-11-29 16:08:38 +0200 | [diff] [blame] | 320 | static void pciexp_enable_aspm(device_t root, unsigned root_cap, |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 321 | device_t endp, unsigned endp_cap) |
| 322 | { |
| 323 | const char *aspm_type_str[] = { "None", "L0s", "L1", "L0s and L1" }; |
| 324 | enum aspm_type apmc = PCIE_ASPM_NONE; |
| 325 | int exit_latency, ok_latency; |
| 326 | u16 lnkctl; |
| 327 | u32 devcap; |
| 328 | |
| 329 | /* Get endpoint device capabilities for acceptable limits */ |
| 330 | devcap = pci_read_config32(endp, endp_cap + PCI_EXP_DEVCAP); |
| 331 | |
| 332 | /* Enable L0s if it is within endpoint acceptable limit */ |
| 333 | ok_latency = (devcap & PCI_EXP_DEVCAP_L0S) >> 6; |
| 334 | exit_latency = pciexp_aspm_latency(root, root_cap, endp, endp_cap, |
| 335 | PCIE_ASPM_L0S); |
| 336 | if (exit_latency >= 0 && exit_latency <= ok_latency) |
| 337 | apmc |= PCIE_ASPM_L0S; |
| 338 | |
| 339 | /* Enable L1 if it is within endpoint acceptable limit */ |
| 340 | ok_latency = (devcap & PCI_EXP_DEVCAP_L1) >> 9; |
| 341 | exit_latency = pciexp_aspm_latency(root, root_cap, endp, endp_cap, |
| 342 | PCIE_ASPM_L1); |
| 343 | if (exit_latency >= 0 && exit_latency <= ok_latency) |
| 344 | apmc |= PCIE_ASPM_L1; |
| 345 | |
| 346 | if (apmc != PCIE_ASPM_NONE) { |
| 347 | /* Set APMC in root port first */ |
| 348 | lnkctl = pci_read_config16(root, root_cap + PCI_EXP_LNKCTL); |
| 349 | lnkctl |= apmc; |
| 350 | pci_write_config16(root, root_cap + PCI_EXP_LNKCTL, lnkctl); |
| 351 | |
| 352 | /* Set APMC in endpoint device next */ |
| 353 | lnkctl = pci_read_config16(endp, endp_cap + PCI_EXP_LNKCTL); |
| 354 | lnkctl |= apmc; |
| 355 | pci_write_config16(endp, endp_cap + PCI_EXP_LNKCTL, lnkctl); |
Kyösti Mälkki | df96a70 | 2016-11-29 16:08:38 +0200 | [diff] [blame] | 356 | |
| 357 | /* Enable ASPM role based error reporting. */ |
| 358 | devcap = pci_read_config32(endp, endp_cap + PCI_EXP_DEVCAP); |
| 359 | devcap |= PCI_EXP_DEVCAP_RBER; |
| 360 | pci_write_config32(endp, endp_cap + PCI_EXP_DEVCAP, devcap); |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | printk(BIOS_INFO, "ASPM: Enabled %s\n", aspm_type_str[apmc]); |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 364 | } |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 365 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 366 | static void pciexp_tune_dev(device_t dev) |
| 367 | { |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 368 | device_t root = dev->bus->dev; |
| 369 | unsigned int root_cap, cap; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 370 | |
| 371 | cap = pci_find_capability(dev, PCI_CAP_ID_PCIE); |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 372 | if (!cap) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 373 | return; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 374 | |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 375 | root_cap = pci_find_capability(root, PCI_CAP_ID_PCIE); |
| 376 | if (!root_cap) |
| 377 | return; |
Stefan Reinauer | f6eb88a | 2010-01-17 13:54:08 +0000 | [diff] [blame] | 378 | |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 379 | /* Check for and enable Common Clock */ |
Kyösti Mälkki | 91bfa8e | 2016-11-20 20:39:56 +0200 | [diff] [blame^] | 380 | if (IS_ENABLED(CONFIG_PCIEXP_COMMON_CLOCK)) |
| 381 | pciexp_enable_common_clock(root, root_cap, dev, cap); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 382 | |
Kane Chen | 18cb134 | 2014-10-01 11:13:54 +0800 | [diff] [blame] | 383 | /* Check if per port CLK req is supported by endpoint*/ |
Kyösti Mälkki | 91bfa8e | 2016-11-20 20:39:56 +0200 | [diff] [blame^] | 384 | if (IS_ENABLED(CONFIG_PCIEXP_CLK_PM)) |
| 385 | pciexp_enable_clock_power_pm(dev, cap); |
Kane Chen | 18cb134 | 2014-10-01 11:13:54 +0800 | [diff] [blame] | 386 | |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 387 | /* Enable L1 Sub-State when both root port and endpoint support */ |
Kyösti Mälkki | 91bfa8e | 2016-11-20 20:39:56 +0200 | [diff] [blame^] | 388 | if (IS_ENABLED(CONFIG_PCIEXP_L1_SUB_STATE)) |
| 389 | pciexp_config_L1_sub_state(root, dev); |
Kenji Chen | 31c6e63 | 2014-10-04 01:14:44 +0800 | [diff] [blame] | 390 | |
Duncan Laurie | 90dcdd4 | 2011-10-25 14:15:11 -0700 | [diff] [blame] | 391 | /* Check for and enable ASPM */ |
Kyösti Mälkki | 91bfa8e | 2016-11-20 20:39:56 +0200 | [diff] [blame^] | 392 | if (IS_ENABLED(CONFIG_PCIEXP_ASPM)) |
| 393 | pciexp_enable_aspm(root, root_cap, dev, cap); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Kyösti Mälkki | de271a8 | 2015-03-18 13:09:47 +0200 | [diff] [blame] | 396 | void pciexp_scan_bus(struct bus *bus, unsigned int min_devfn, |
| 397 | unsigned int max_devfn) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 398 | { |
| 399 | device_t child; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 400 | |
Kyösti Mälkki | de271a8 | 2015-03-18 13:09:47 +0200 | [diff] [blame] | 401 | pci_scan_bus(bus, min_devfn, max_devfn); |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 402 | |
| 403 | for (child = bus->children; child; child = child->sibling) { |
| 404 | if ((child->path.pci.devfn < min_devfn) || |
| 405 | (child->path.pci.devfn > max_devfn)) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 406 | continue; |
| 407 | } |
| 408 | pciexp_tune_dev(child); |
| 409 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Kyösti Mälkki | 580e722 | 2015-03-19 21:04:23 +0200 | [diff] [blame] | 412 | void pciexp_scan_bridge(device_t dev) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 413 | { |
Kyösti Mälkki | 580e722 | 2015-03-19 21:04:23 +0200 | [diff] [blame] | 414 | do_pci_scan_bridge(dev, pciexp_scan_bus); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /** Default device operations for PCI Express bridges */ |
| 418 | static struct pci_operations pciexp_bus_ops_pci = { |
| 419 | .set_subsystem = 0, |
| 420 | }; |
| 421 | |
| 422 | struct device_operations default_pciexp_ops_bus = { |
| 423 | .read_resources = pci_bus_read_resources, |
| 424 | .set_resources = pci_dev_set_resources, |
| 425 | .enable_resources = pci_bus_enable_resources, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 426 | .init = 0, |
| 427 | .scan_bus = pciexp_scan_bridge, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 428 | .enable = 0, |
| 429 | .reset_bus = pci_bus_reset, |
| 430 | .ops_pci = &pciexp_bus_ops_pci, |
| 431 | }; |