aspm.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Enable PCIe link L0s/L1 state and Clock Power Management
  4. *
  5. * Copyright (C) 2007 Intel
  6. * Copyright (C) Zhang Yanmin (yanmin.zhang@intel.com)
  7. * Copyright (C) Shaohua Li (shaohua.li@intel.com)
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/pci.h>
  13. #include <linux/pci_regs.h>
  14. #include <linux/errno.h>
  15. #include <linux/pm.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/delay.h>
  20. #include <linux/pci-aspm.h>
  21. #include "../pci.h"
  22. #ifdef MODULE_PARAM_PREFIX
  23. #undef MODULE_PARAM_PREFIX
  24. #endif
  25. #define MODULE_PARAM_PREFIX "pcie_aspm."
  26. /* Note: those are not register definitions */
  27. #define ASPM_STATE_L0S_UP (1) /* Upstream direction L0s state */
  28. #define ASPM_STATE_L0S_DW (2) /* Downstream direction L0s state */
  29. #define ASPM_STATE_L1 (4) /* L1 state */
  30. #define ASPM_STATE_L1_1 (8) /* ASPM L1.1 state */
  31. #define ASPM_STATE_L1_2 (0x10) /* ASPM L1.2 state */
  32. #define ASPM_STATE_L1_1_PCIPM (0x20) /* PCI PM L1.1 state */
  33. #define ASPM_STATE_L1_2_PCIPM (0x40) /* PCI PM L1.2 state */
  34. #define ASPM_STATE_L1_SS_PCIPM (ASPM_STATE_L1_1_PCIPM | ASPM_STATE_L1_2_PCIPM)
  35. #define ASPM_STATE_L1_2_MASK (ASPM_STATE_L1_2 | ASPM_STATE_L1_2_PCIPM)
  36. #define ASPM_STATE_L1SS (ASPM_STATE_L1_1 | ASPM_STATE_L1_1_PCIPM |\
  37. ASPM_STATE_L1_2_MASK)
  38. #define ASPM_STATE_L0S (ASPM_STATE_L0S_UP | ASPM_STATE_L0S_DW)
  39. #define ASPM_STATE_ALL (ASPM_STATE_L0S | ASPM_STATE_L1 | \
  40. ASPM_STATE_L1SS)
  41. struct aspm_latency {
  42. u32 l0s; /* L0s latency (nsec) */
  43. u32 l1; /* L1 latency (nsec) */
  44. };
  45. struct pcie_link_state {
  46. struct pci_dev *pdev; /* Upstream component of the Link */
  47. struct pci_dev *downstream; /* Downstream component, function 0 */
  48. struct pcie_link_state *root; /* pointer to the root port link */
  49. struct pcie_link_state *parent; /* pointer to the parent Link state */
  50. struct list_head sibling; /* node in link_list */
  51. struct list_head children; /* list of child link states */
  52. struct list_head link; /* node in parent's children list */
  53. /* ASPM state */
  54. u32 aspm_support:7; /* Supported ASPM state */
  55. u32 aspm_enabled:7; /* Enabled ASPM state */
  56. u32 aspm_capable:7; /* Capable ASPM state with latency */
  57. u32 aspm_default:7; /* Default ASPM state by BIOS */
  58. u32 aspm_disable:7; /* Disabled ASPM state */
  59. /* Clock PM state */
  60. u32 clkpm_capable:1; /* Clock PM capable? */
  61. u32 clkpm_enabled:1; /* Current Clock PM state */
  62. u32 clkpm_default:1; /* Default Clock PM state by BIOS */
  63. /* Exit latencies */
  64. struct aspm_latency latency_up; /* Upstream direction exit latency */
  65. struct aspm_latency latency_dw; /* Downstream direction exit latency */
  66. /*
  67. * Endpoint acceptable latencies. A pcie downstream port only
  68. * has one slot under it, so at most there are 8 functions.
  69. */
  70. struct aspm_latency acceptable[8];
  71. /* L1 PM Substate info */
  72. struct {
  73. u32 up_cap_ptr; /* L1SS cap ptr in upstream dev */
  74. u32 dw_cap_ptr; /* L1SS cap ptr in downstream dev */
  75. u32 ctl1; /* value to be programmed in ctl1 */
  76. u32 ctl2; /* value to be programmed in ctl2 */
  77. } l1ss;
  78. };
  79. static int aspm_disabled, aspm_force;
  80. static bool aspm_support_enabled = true;
  81. static DEFINE_MUTEX(aspm_lock);
  82. static LIST_HEAD(link_list);
  83. #define POLICY_DEFAULT 0 /* BIOS default setting */
  84. #define POLICY_PERFORMANCE 1 /* high performance */
  85. #define POLICY_POWERSAVE 2 /* high power saving */
  86. #define POLICY_POWER_SUPERSAVE 3 /* possibly even more power saving */
  87. #ifdef CONFIG_PCIEASPM_PERFORMANCE
  88. static int aspm_policy = POLICY_PERFORMANCE;
  89. #elif defined CONFIG_PCIEASPM_POWERSAVE
  90. static int aspm_policy = POLICY_POWERSAVE;
  91. #elif defined CONFIG_PCIEASPM_POWER_SUPERSAVE
  92. static int aspm_policy = POLICY_POWER_SUPERSAVE;
  93. #else
  94. static int aspm_policy;
  95. #endif
  96. static const char *policy_str[] = {
  97. [POLICY_DEFAULT] = "default",
  98. [POLICY_PERFORMANCE] = "performance",
  99. [POLICY_POWERSAVE] = "powersave",
  100. [POLICY_POWER_SUPERSAVE] = "powersupersave"
  101. };
  102. #define LINK_RETRAIN_TIMEOUT HZ
  103. static int policy_to_aspm_state(struct pcie_link_state *link)
  104. {
  105. switch (aspm_policy) {
  106. case POLICY_PERFORMANCE:
  107. /* Disable ASPM and Clock PM */
  108. return 0;
  109. case POLICY_POWERSAVE:
  110. /* Enable ASPM L0s/L1 */
  111. return (ASPM_STATE_L0S | ASPM_STATE_L1);
  112. case POLICY_POWER_SUPERSAVE:
  113. /* Enable Everything */
  114. return ASPM_STATE_ALL;
  115. case POLICY_DEFAULT:
  116. return link->aspm_default;
  117. }
  118. return 0;
  119. }
  120. static int policy_to_clkpm_state(struct pcie_link_state *link)
  121. {
  122. switch (aspm_policy) {
  123. case POLICY_PERFORMANCE:
  124. /* Disable ASPM and Clock PM */
  125. return 0;
  126. case POLICY_POWERSAVE:
  127. case POLICY_POWER_SUPERSAVE:
  128. /* Enable Clock PM */
  129. return 1;
  130. case POLICY_DEFAULT:
  131. return link->clkpm_default;
  132. }
  133. return 0;
  134. }
  135. static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
  136. {
  137. struct pci_dev *child;
  138. struct pci_bus *linkbus = link->pdev->subordinate;
  139. u32 val = enable ? PCI_EXP_LNKCTL_CLKREQ_EN : 0;
  140. list_for_each_entry(child, &linkbus->devices, bus_list)
  141. pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
  142. PCI_EXP_LNKCTL_CLKREQ_EN,
  143. val);
  144. link->clkpm_enabled = !!enable;
  145. }
  146. static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
  147. {
  148. /* Don't enable Clock PM if the link is not Clock PM capable */
  149. if (!link->clkpm_capable)
  150. enable = 0;
  151. /* Need nothing if the specified equals to current state */
  152. if (link->clkpm_enabled == enable)
  153. return;
  154. pcie_set_clkpm_nocheck(link, enable);
  155. }
  156. static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
  157. {
  158. int capable = 1, enabled = 1;
  159. u32 reg32;
  160. u16 reg16;
  161. struct pci_dev *child;
  162. struct pci_bus *linkbus = link->pdev->subordinate;
  163. /* All functions should have the same cap and state, take the worst */
  164. list_for_each_entry(child, &linkbus->devices, bus_list) {
  165. pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &reg32);
  166. if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
  167. capable = 0;
  168. enabled = 0;
  169. break;
  170. }
  171. pcie_capability_read_word(child, PCI_EXP_LNKCTL, &reg16);
  172. if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
  173. enabled = 0;
  174. }
  175. link->clkpm_enabled = enabled;
  176. link->clkpm_default = enabled;
  177. link->clkpm_capable = (blacklist) ? 0 : capable;
  178. }
  179. /*
  180. * pcie_aspm_configure_common_clock: check if the 2 ends of a link
  181. * could use common clock. If they are, configure them to use the
  182. * common clock. That will reduce the ASPM state exit latency.
  183. */
  184. static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
  185. {
  186. int same_clock = 1;
  187. u16 reg16, parent_reg, child_reg[8];
  188. unsigned long start_jiffies;
  189. struct pci_dev *child, *parent = link->pdev;
  190. struct pci_bus *linkbus = parent->subordinate;
  191. /*
  192. * All functions of a slot should have the same Slot Clock
  193. * Configuration, so just check one function
  194. */
  195. child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
  196. BUG_ON(!pci_is_pcie(child));
  197. /* Check downstream component if bit Slot Clock Configuration is 1 */
  198. pcie_capability_read_word(child, PCI_EXP_LNKSTA, &reg16);
  199. if (!(reg16 & PCI_EXP_LNKSTA_SLC))
  200. same_clock = 0;
  201. /* Check upstream component if bit Slot Clock Configuration is 1 */
  202. pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &reg16);
  203. if (!(reg16 & PCI_EXP_LNKSTA_SLC))
  204. same_clock = 0;
  205. /* Port might be already in common clock mode */
  206. pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &reg16);
  207. if (same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) {
  208. bool consistent = true;
  209. list_for_each_entry(child, &linkbus->devices, bus_list) {
  210. pcie_capability_read_word(child, PCI_EXP_LNKCTL,
  211. &reg16);
  212. if (!(reg16 & PCI_EXP_LNKCTL_CCC)) {
  213. consistent = false;
  214. break;
  215. }
  216. }
  217. if (consistent)
  218. return;
  219. pci_warn(parent, "ASPM: current common clock configuration is broken, reconfiguring\n");
  220. }
  221. /* Configure downstream component, all functions */
  222. list_for_each_entry(child, &linkbus->devices, bus_list) {
  223. pcie_capability_read_word(child, PCI_EXP_LNKCTL, &reg16);
  224. child_reg[PCI_FUNC(child->devfn)] = reg16;
  225. if (same_clock)
  226. reg16 |= PCI_EXP_LNKCTL_CCC;
  227. else
  228. reg16 &= ~PCI_EXP_LNKCTL_CCC;
  229. pcie_capability_write_word(child, PCI_EXP_LNKCTL, reg16);
  230. }
  231. /* Configure upstream component */
  232. pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &reg16);
  233. parent_reg = reg16;
  234. if (same_clock)
  235. reg16 |= PCI_EXP_LNKCTL_CCC;
  236. else
  237. reg16 &= ~PCI_EXP_LNKCTL_CCC;
  238. pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
  239. /* Retrain link */
  240. reg16 |= PCI_EXP_LNKCTL_RL;
  241. pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
  242. /* Wait for link training end. Break out after waiting for timeout */
  243. start_jiffies = jiffies;
  244. for (;;) {
  245. pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &reg16);
  246. if (!(reg16 & PCI_EXP_LNKSTA_LT))
  247. break;
  248. if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
  249. break;
  250. msleep(1);
  251. }
  252. if (!(reg16 & PCI_EXP_LNKSTA_LT))
  253. return;
  254. /* Training failed. Restore common clock configurations */
  255. pci_err(parent, "ASPM: Could not configure common clock\n");
  256. list_for_each_entry(child, &linkbus->devices, bus_list)
  257. pcie_capability_write_word(child, PCI_EXP_LNKCTL,
  258. child_reg[PCI_FUNC(child->devfn)]);
  259. pcie_capability_write_word(parent, PCI_EXP_LNKCTL, parent_reg);
  260. }
  261. /* Convert L0s latency encoding to ns */
  262. static u32 calc_l0s_latency(u32 encoding)
  263. {
  264. if (encoding == 0x7)
  265. return (5 * 1000); /* > 4us */
  266. return (64 << encoding);
  267. }
  268. /* Convert L0s acceptable latency encoding to ns */
  269. static u32 calc_l0s_acceptable(u32 encoding)
  270. {
  271. if (encoding == 0x7)
  272. return -1U;
  273. return (64 << encoding);
  274. }
  275. /* Convert L1 latency encoding to ns */
  276. static u32 calc_l1_latency(u32 encoding)
  277. {
  278. if (encoding == 0x7)
  279. return (65 * 1000); /* > 64us */
  280. return (1000 << encoding);
  281. }
  282. /* Convert L1 acceptable latency encoding to ns */
  283. static u32 calc_l1_acceptable(u32 encoding)
  284. {
  285. if (encoding == 0x7)
  286. return -1U;
  287. return (1000 << encoding);
  288. }
  289. /* Convert L1SS T_pwr encoding to usec */
  290. static u32 calc_l1ss_pwron(struct pci_dev *pdev, u32 scale, u32 val)
  291. {
  292. switch (scale) {
  293. case 0:
  294. return val * 2;
  295. case 1:
  296. return val * 10;
  297. case 2:
  298. return val * 100;
  299. }
  300. pci_err(pdev, "%s: Invalid T_PwrOn scale: %u\n", __func__, scale);
  301. return 0;
  302. }
  303. static void encode_l12_threshold(u32 threshold_us, u32 *scale, u32 *value)
  304. {
  305. u32 threshold_ns = threshold_us * 1000;
  306. /* See PCIe r3.1, sec 7.33.3 and sec 6.18 */
  307. if (threshold_ns < 32) {
  308. *scale = 0;
  309. *value = threshold_ns;
  310. } else if (threshold_ns < 1024) {
  311. *scale = 1;
  312. *value = threshold_ns >> 5;
  313. } else if (threshold_ns < 32768) {
  314. *scale = 2;
  315. *value = threshold_ns >> 10;
  316. } else if (threshold_ns < 1048576) {
  317. *scale = 3;
  318. *value = threshold_ns >> 15;
  319. } else if (threshold_ns < 33554432) {
  320. *scale = 4;
  321. *value = threshold_ns >> 20;
  322. } else {
  323. *scale = 5;
  324. *value = threshold_ns >> 25;
  325. }
  326. }
  327. struct aspm_register_info {
  328. u32 support:2;
  329. u32 enabled:2;
  330. u32 latency_encoding_l0s;
  331. u32 latency_encoding_l1;
  332. /* L1 substates */
  333. u32 l1ss_cap_ptr;
  334. u32 l1ss_cap;
  335. u32 l1ss_ctl1;
  336. u32 l1ss_ctl2;
  337. };
  338. static void pcie_get_aspm_reg(struct pci_dev *pdev,
  339. struct aspm_register_info *info)
  340. {
  341. u16 reg16;
  342. u32 reg32;
  343. pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &reg32);
  344. info->support = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
  345. info->latency_encoding_l0s = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
  346. info->latency_encoding_l1 = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
  347. pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &reg16);
  348. info->enabled = reg16 & PCI_EXP_LNKCTL_ASPMC;
  349. /* Read L1 PM substate capabilities */
  350. info->l1ss_cap = info->l1ss_ctl1 = info->l1ss_ctl2 = 0;
  351. info->l1ss_cap_ptr = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS);
  352. if (!info->l1ss_cap_ptr)
  353. return;
  354. pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CAP,
  355. &info->l1ss_cap);
  356. if (!(info->l1ss_cap & PCI_L1SS_CAP_L1_PM_SS)) {
  357. info->l1ss_cap = 0;
  358. return;
  359. }
  360. pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL1,
  361. &info->l1ss_ctl1);
  362. pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL2,
  363. &info->l1ss_ctl2);
  364. }
  365. static void pcie_aspm_check_latency(struct pci_dev *endpoint)
  366. {
  367. u32 latency, l1_switch_latency = 0;
  368. struct aspm_latency *acceptable;
  369. struct pcie_link_state *link;
  370. /* Device not in D0 doesn't need latency check */
  371. if ((endpoint->current_state != PCI_D0) &&
  372. (endpoint->current_state != PCI_UNKNOWN))
  373. return;
  374. link = endpoint->bus->self->link_state;
  375. acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
  376. while (link) {
  377. /* Check upstream direction L0s latency */
  378. if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
  379. (link->latency_up.l0s > acceptable->l0s))
  380. link->aspm_capable &= ~ASPM_STATE_L0S_UP;
  381. /* Check downstream direction L0s latency */
  382. if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
  383. (link->latency_dw.l0s > acceptable->l0s))
  384. link->aspm_capable &= ~ASPM_STATE_L0S_DW;
  385. /*
  386. * Check L1 latency.
  387. * Every switch on the path to root complex need 1
  388. * more microsecond for L1. Spec doesn't mention L0s.
  389. *
  390. * The exit latencies for L1 substates are not advertised
  391. * by a device. Since the spec also doesn't mention a way
  392. * to determine max latencies introduced by enabling L1
  393. * substates on the components, it is not clear how to do
  394. * a L1 substate exit latency check. We assume that the
  395. * L1 exit latencies advertised by a device include L1
  396. * substate latencies (and hence do not do any check).
  397. */
  398. latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1);
  399. if ((link->aspm_capable & ASPM_STATE_L1) &&
  400. (latency + l1_switch_latency > acceptable->l1))
  401. link->aspm_capable &= ~ASPM_STATE_L1;
  402. l1_switch_latency += 1000;
  403. link = link->parent;
  404. }
  405. }
  406. /*
  407. * The L1 PM substate capability is only implemented in function 0 in a
  408. * multi function device.
  409. */
  410. static struct pci_dev *pci_function_0(struct pci_bus *linkbus)
  411. {
  412. struct pci_dev *child;
  413. list_for_each_entry(child, &linkbus->devices, bus_list)
  414. if (PCI_FUNC(child->devfn) == 0)
  415. return child;
  416. return NULL;
  417. }
  418. /* Calculate L1.2 PM substate timing parameters */
  419. static void aspm_calc_l1ss_info(struct pcie_link_state *link,
  420. struct aspm_register_info *upreg,
  421. struct aspm_register_info *dwreg)
  422. {
  423. u32 val1, val2, scale1, scale2;
  424. u32 t_common_mode, t_power_on, l1_2_threshold, scale, value;
  425. link->l1ss.up_cap_ptr = upreg->l1ss_cap_ptr;
  426. link->l1ss.dw_cap_ptr = dwreg->l1ss_cap_ptr;
  427. link->l1ss.ctl1 = link->l1ss.ctl2 = 0;
  428. if (!(link->aspm_support & ASPM_STATE_L1_2_MASK))
  429. return;
  430. /* Choose the greater of the two Port Common_Mode_Restore_Times */
  431. val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
  432. val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
  433. t_common_mode = max(val1, val2);
  434. /* Choose the greater of the two Port T_POWER_ON times */
  435. val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
  436. scale1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
  437. val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
  438. scale2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
  439. if (calc_l1ss_pwron(link->pdev, scale1, val1) >
  440. calc_l1ss_pwron(link->downstream, scale2, val2)) {
  441. link->l1ss.ctl2 |= scale1 | (val1 << 3);
  442. t_power_on = calc_l1ss_pwron(link->pdev, scale1, val1);
  443. } else {
  444. link->l1ss.ctl2 |= scale2 | (val2 << 3);
  445. t_power_on = calc_l1ss_pwron(link->downstream, scale2, val2);
  446. }
  447. /*
  448. * Set LTR_L1.2_THRESHOLD to the time required to transition the
  449. * Link from L0 to L1.2 and back to L0 so we enter L1.2 only if
  450. * downstream devices report (via LTR) that they can tolerate at
  451. * least that much latency.
  452. *
  453. * Based on PCIe r3.1, sec 5.5.3.3.1, Figures 5-16 and 5-17, and
  454. * Table 5-11. T(POWER_OFF) is at most 2us and T(L1.2) is at
  455. * least 4us.
  456. */
  457. l1_2_threshold = 2 + 4 + t_common_mode + t_power_on;
  458. encode_l12_threshold(l1_2_threshold, &scale, &value);
  459. link->l1ss.ctl1 |= t_common_mode << 8 | scale << 29 | value << 16;
  460. }
  461. static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
  462. {
  463. struct pci_dev *child = link->downstream, *parent = link->pdev;
  464. struct pci_bus *linkbus = parent->subordinate;
  465. struct aspm_register_info upreg, dwreg;
  466. if (blacklist) {
  467. /* Set enabled/disable so that we will disable ASPM later */
  468. link->aspm_enabled = ASPM_STATE_ALL;
  469. link->aspm_disable = ASPM_STATE_ALL;
  470. return;
  471. }
  472. /* Get upstream/downstream components' register state */
  473. pcie_get_aspm_reg(parent, &upreg);
  474. pcie_get_aspm_reg(child, &dwreg);
  475. /*
  476. * If ASPM not supported, don't mess with the clocks and link,
  477. * bail out now.
  478. */
  479. if (!(upreg.support & dwreg.support))
  480. return;
  481. /* Configure common clock before checking latencies */
  482. pcie_aspm_configure_common_clock(link);
  483. /*
  484. * Re-read upstream/downstream components' register state
  485. * after clock configuration
  486. */
  487. pcie_get_aspm_reg(parent, &upreg);
  488. pcie_get_aspm_reg(child, &dwreg);
  489. /*
  490. * Setup L0s state
  491. *
  492. * Note that we must not enable L0s in either direction on a
  493. * given link unless components on both sides of the link each
  494. * support L0s.
  495. */
  496. if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S)
  497. link->aspm_support |= ASPM_STATE_L0S;
  498. if (dwreg.enabled & PCIE_LINK_STATE_L0S)
  499. link->aspm_enabled |= ASPM_STATE_L0S_UP;
  500. if (upreg.enabled & PCIE_LINK_STATE_L0S)
  501. link->aspm_enabled |= ASPM_STATE_L0S_DW;
  502. link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s);
  503. link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s);
  504. /* Setup L1 state */
  505. if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1)
  506. link->aspm_support |= ASPM_STATE_L1;
  507. if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1)
  508. link->aspm_enabled |= ASPM_STATE_L1;
  509. link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1);
  510. link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1);
  511. /* Setup L1 substate */
  512. if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1)
  513. link->aspm_support |= ASPM_STATE_L1_1;
  514. if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2)
  515. link->aspm_support |= ASPM_STATE_L1_2;
  516. if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1)
  517. link->aspm_support |= ASPM_STATE_L1_1_PCIPM;
  518. if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2)
  519. link->aspm_support |= ASPM_STATE_L1_2_PCIPM;
  520. if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1)
  521. link->aspm_enabled |= ASPM_STATE_L1_1;
  522. if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2)
  523. link->aspm_enabled |= ASPM_STATE_L1_2;
  524. if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1)
  525. link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM;
  526. if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2)
  527. link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM;
  528. if (link->aspm_support & ASPM_STATE_L1SS)
  529. aspm_calc_l1ss_info(link, &upreg, &dwreg);
  530. /* Save default state */
  531. link->aspm_default = link->aspm_enabled;
  532. /* Setup initial capable state. Will be updated later */
  533. link->aspm_capable = link->aspm_support;
  534. /*
  535. * If the downstream component has pci bridge function, don't
  536. * do ASPM for now.
  537. */
  538. list_for_each_entry(child, &linkbus->devices, bus_list) {
  539. if (pci_pcie_type(child) == PCI_EXP_TYPE_PCI_BRIDGE) {
  540. link->aspm_disable = ASPM_STATE_ALL;
  541. break;
  542. }
  543. }
  544. /* Get and check endpoint acceptable latencies */
  545. list_for_each_entry(child, &linkbus->devices, bus_list) {
  546. u32 reg32, encoding;
  547. struct aspm_latency *acceptable =
  548. &link->acceptable[PCI_FUNC(child->devfn)];
  549. if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT &&
  550. pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)
  551. continue;
  552. pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
  553. /* Calculate endpoint L0s acceptable latency */
  554. encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
  555. acceptable->l0s = calc_l0s_acceptable(encoding);
  556. /* Calculate endpoint L1 acceptable latency */
  557. encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
  558. acceptable->l1 = calc_l1_acceptable(encoding);
  559. pcie_aspm_check_latency(child);
  560. }
  561. }
  562. static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos,
  563. u32 clear, u32 set)
  564. {
  565. u32 val;
  566. pci_read_config_dword(pdev, pos, &val);
  567. val &= ~clear;
  568. val |= set;
  569. pci_write_config_dword(pdev, pos, val);
  570. }
  571. /* Configure the ASPM L1 substates */
  572. static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state)
  573. {
  574. u32 val, enable_req;
  575. struct pci_dev *child = link->downstream, *parent = link->pdev;
  576. u32 up_cap_ptr = link->l1ss.up_cap_ptr;
  577. u32 dw_cap_ptr = link->l1ss.dw_cap_ptr;
  578. enable_req = (link->aspm_enabled ^ state) & state;
  579. /*
  580. * Here are the rules specified in the PCIe spec for enabling L1SS:
  581. * - When enabling L1.x, enable bit at parent first, then at child
  582. * - When disabling L1.x, disable bit at child first, then at parent
  583. * - When enabling ASPM L1.x, need to disable L1
  584. * (at child followed by parent).
  585. * - The ASPM/PCIPM L1.2 must be disabled while programming timing
  586. * parameters
  587. *
  588. * To keep it simple, disable all L1SS bits first, and later enable
  589. * what is needed.
  590. */
  591. /* Disable all L1 substates */
  592. pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
  593. PCI_L1SS_CTL1_L1SS_MASK, 0);
  594. pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
  595. PCI_L1SS_CTL1_L1SS_MASK, 0);
  596. /*
  597. * If needed, disable L1, and it gets enabled later
  598. * in pcie_config_aspm_link().
  599. */
  600. if (enable_req & (ASPM_STATE_L1_1 | ASPM_STATE_L1_2)) {
  601. pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
  602. PCI_EXP_LNKCTL_ASPM_L1, 0);
  603. pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
  604. PCI_EXP_LNKCTL_ASPM_L1, 0);
  605. }
  606. if (enable_req & ASPM_STATE_L1_2_MASK) {
  607. /* Program T_POWER_ON times in both ports */
  608. pci_write_config_dword(parent, up_cap_ptr + PCI_L1SS_CTL2,
  609. link->l1ss.ctl2);
  610. pci_write_config_dword(child, dw_cap_ptr + PCI_L1SS_CTL2,
  611. link->l1ss.ctl2);
  612. /* Program Common_Mode_Restore_Time in upstream device */
  613. pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
  614. PCI_L1SS_CTL1_CM_RESTORE_TIME,
  615. link->l1ss.ctl1);
  616. /* Program LTR_L1.2_THRESHOLD time in both ports */
  617. pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
  618. PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
  619. PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
  620. link->l1ss.ctl1);
  621. pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
  622. PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
  623. PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
  624. link->l1ss.ctl1);
  625. }
  626. val = 0;
  627. if (state & ASPM_STATE_L1_1)
  628. val |= PCI_L1SS_CTL1_ASPM_L1_1;
  629. if (state & ASPM_STATE_L1_2)
  630. val |= PCI_L1SS_CTL1_ASPM_L1_2;
  631. if (state & ASPM_STATE_L1_1_PCIPM)
  632. val |= PCI_L1SS_CTL1_PCIPM_L1_1;
  633. if (state & ASPM_STATE_L1_2_PCIPM)
  634. val |= PCI_L1SS_CTL1_PCIPM_L1_2;
  635. /* Enable what we need to enable */
  636. pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
  637. PCI_L1SS_CAP_L1_PM_SS, val);
  638. pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
  639. PCI_L1SS_CAP_L1_PM_SS, val);
  640. }
  641. static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
  642. {
  643. pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
  644. PCI_EXP_LNKCTL_ASPMC, val);
  645. }
  646. static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
  647. {
  648. u32 upstream = 0, dwstream = 0;
  649. struct pci_dev *child = link->downstream, *parent = link->pdev;
  650. struct pci_bus *linkbus = parent->subordinate;
  651. /* Enable only the states that were not explicitly disabled */
  652. state &= (link->aspm_capable & ~link->aspm_disable);
  653. /* Can't enable any substates if L1 is not enabled */
  654. if (!(state & ASPM_STATE_L1))
  655. state &= ~ASPM_STATE_L1SS;
  656. /* Spec says both ports must be in D0 before enabling PCI PM substates*/
  657. if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) {
  658. state &= ~ASPM_STATE_L1_SS_PCIPM;
  659. state |= (link->aspm_enabled & ASPM_STATE_L1_SS_PCIPM);
  660. }
  661. /* Nothing to do if the link is already in the requested state */
  662. if (link->aspm_enabled == state)
  663. return;
  664. /* Convert ASPM state to upstream/downstream ASPM register state */
  665. if (state & ASPM_STATE_L0S_UP)
  666. dwstream |= PCI_EXP_LNKCTL_ASPM_L0S;
  667. if (state & ASPM_STATE_L0S_DW)
  668. upstream |= PCI_EXP_LNKCTL_ASPM_L0S;
  669. if (state & ASPM_STATE_L1) {
  670. upstream |= PCI_EXP_LNKCTL_ASPM_L1;
  671. dwstream |= PCI_EXP_LNKCTL_ASPM_L1;
  672. }
  673. if (link->aspm_capable & ASPM_STATE_L1SS)
  674. pcie_config_aspm_l1ss(link, state);
  675. /*
  676. * Spec 2.0 suggests all functions should be configured the
  677. * same setting for ASPM. Enabling ASPM L1 should be done in
  678. * upstream component first and then downstream, and vice
  679. * versa for disabling ASPM L1. Spec doesn't mention L0S.
  680. */
  681. if (state & ASPM_STATE_L1)
  682. pcie_config_aspm_dev(parent, upstream);
  683. list_for_each_entry(child, &linkbus->devices, bus_list)
  684. pcie_config_aspm_dev(child, dwstream);
  685. if (!(state & ASPM_STATE_L1))
  686. pcie_config_aspm_dev(parent, upstream);
  687. link->aspm_enabled = state;
  688. }
  689. static void pcie_config_aspm_path(struct pcie_link_state *link)
  690. {
  691. while (link) {
  692. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  693. link = link->parent;
  694. }
  695. }
  696. static void free_link_state(struct pcie_link_state *link)
  697. {
  698. link->pdev->link_state = NULL;
  699. kfree(link);
  700. }
  701. static int pcie_aspm_sanity_check(struct pci_dev *pdev)
  702. {
  703. struct pci_dev *child;
  704. u32 reg32;
  705. /*
  706. * Some functions in a slot might not all be PCIe functions,
  707. * very strange. Disable ASPM for the whole slot
  708. */
  709. list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
  710. if (!pci_is_pcie(child))
  711. return -EINVAL;
  712. /*
  713. * If ASPM is disabled then we're not going to change
  714. * the BIOS state. It's safe to continue even if it's a
  715. * pre-1.1 device
  716. */
  717. if (aspm_disabled)
  718. continue;
  719. /*
  720. * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
  721. * RBER bit to determine if a function is 1.1 version device
  722. */
  723. pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
  724. if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
  725. pci_info(child, "disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force'\n");
  726. return -EINVAL;
  727. }
  728. }
  729. return 0;
  730. }
  731. static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
  732. {
  733. struct pcie_link_state *link;
  734. link = kzalloc(sizeof(*link), GFP_KERNEL);
  735. if (!link)
  736. return NULL;
  737. INIT_LIST_HEAD(&link->sibling);
  738. INIT_LIST_HEAD(&link->children);
  739. INIT_LIST_HEAD(&link->link);
  740. link->pdev = pdev;
  741. link->downstream = pci_function_0(pdev->subordinate);
  742. /*
  743. * Root Ports and PCI/PCI-X to PCIe Bridges are roots of PCIe
  744. * hierarchies. Note that some PCIe host implementations omit
  745. * the root ports entirely, in which case a downstream port on
  746. * a switch may become the root of the link state chain for all
  747. * its subordinate endpoints.
  748. */
  749. if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
  750. pci_pcie_type(pdev) == PCI_EXP_TYPE_PCIE_BRIDGE ||
  751. !pdev->bus->parent->self) {
  752. link->root = link;
  753. } else {
  754. struct pcie_link_state *parent;
  755. parent = pdev->bus->parent->self->link_state;
  756. if (!parent) {
  757. kfree(link);
  758. return NULL;
  759. }
  760. link->parent = parent;
  761. link->root = link->parent->root;
  762. list_add(&link->link, &parent->children);
  763. }
  764. list_add(&link->sibling, &link_list);
  765. pdev->link_state = link;
  766. return link;
  767. }
  768. /*
  769. * pcie_aspm_init_link_state: Initiate PCI express link state.
  770. * It is called after the pcie and its children devices are scanned.
  771. * @pdev: the root port or switch downstream port
  772. */
  773. void pcie_aspm_init_link_state(struct pci_dev *pdev)
  774. {
  775. struct pcie_link_state *link;
  776. int blacklist = !!pcie_aspm_sanity_check(pdev);
  777. if (!aspm_support_enabled)
  778. return;
  779. if (pdev->link_state)
  780. return;
  781. /*
  782. * We allocate pcie_link_state for the component on the upstream
  783. * end of a Link, so there's nothing to do unless this device has a
  784. * Link on its secondary side.
  785. */
  786. if (!pdev->has_secondary_link)
  787. return;
  788. /* VIA has a strange chipset, root port is under a bridge */
  789. if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT &&
  790. pdev->bus->self)
  791. return;
  792. down_read(&pci_bus_sem);
  793. if (list_empty(&pdev->subordinate->devices))
  794. goto out;
  795. mutex_lock(&aspm_lock);
  796. link = alloc_pcie_link_state(pdev);
  797. if (!link)
  798. goto unlock;
  799. /*
  800. * Setup initial ASPM state. Note that we need to configure
  801. * upstream links also because capable state of them can be
  802. * update through pcie_aspm_cap_init().
  803. */
  804. pcie_aspm_cap_init(link, blacklist);
  805. /* Setup initial Clock PM state */
  806. pcie_clkpm_cap_init(link, blacklist);
  807. /*
  808. * At this stage drivers haven't had an opportunity to change the
  809. * link policy setting. Enabling ASPM on broken hardware can cripple
  810. * it even before the driver has had a chance to disable ASPM, so
  811. * default to a safe level right now. If we're enabling ASPM beyond
  812. * the BIOS's expectation, we'll do so once pci_enable_device() is
  813. * called.
  814. */
  815. if (aspm_policy != POLICY_POWERSAVE &&
  816. aspm_policy != POLICY_POWER_SUPERSAVE) {
  817. pcie_config_aspm_path(link);
  818. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  819. }
  820. unlock:
  821. mutex_unlock(&aspm_lock);
  822. out:
  823. up_read(&pci_bus_sem);
  824. }
  825. /* Recheck latencies and update aspm_capable for links under the root */
  826. static void pcie_update_aspm_capable(struct pcie_link_state *root)
  827. {
  828. struct pcie_link_state *link;
  829. BUG_ON(root->parent);
  830. list_for_each_entry(link, &link_list, sibling) {
  831. if (link->root != root)
  832. continue;
  833. link->aspm_capable = link->aspm_support;
  834. }
  835. list_for_each_entry(link, &link_list, sibling) {
  836. struct pci_dev *child;
  837. struct pci_bus *linkbus = link->pdev->subordinate;
  838. if (link->root != root)
  839. continue;
  840. list_for_each_entry(child, &linkbus->devices, bus_list) {
  841. if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) &&
  842. (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END))
  843. continue;
  844. pcie_aspm_check_latency(child);
  845. }
  846. }
  847. }
  848. /* @pdev: the endpoint device */
  849. void pcie_aspm_exit_link_state(struct pci_dev *pdev)
  850. {
  851. struct pci_dev *parent = pdev->bus->self;
  852. struct pcie_link_state *link, *root, *parent_link;
  853. if (!parent || !parent->link_state)
  854. return;
  855. down_read(&pci_bus_sem);
  856. mutex_lock(&aspm_lock);
  857. /*
  858. * All PCIe functions are in one slot, remove one function will remove
  859. * the whole slot, so just wait until we are the last function left.
  860. */
  861. if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
  862. goto out;
  863. link = parent->link_state;
  864. root = link->root;
  865. parent_link = link->parent;
  866. /* All functions are removed, so just disable ASPM for the link */
  867. pcie_config_aspm_link(link, 0);
  868. list_del(&link->sibling);
  869. list_del(&link->link);
  870. /* Clock PM is for endpoint device */
  871. free_link_state(link);
  872. /* Recheck latencies and configure upstream links */
  873. if (parent_link) {
  874. pcie_update_aspm_capable(root);
  875. pcie_config_aspm_path(parent_link);
  876. }
  877. out:
  878. mutex_unlock(&aspm_lock);
  879. up_read(&pci_bus_sem);
  880. }
  881. /* @pdev: the root port or switch downstream port */
  882. void pcie_aspm_pm_state_change(struct pci_dev *pdev)
  883. {
  884. struct pcie_link_state *link = pdev->link_state;
  885. if (aspm_disabled || !link)
  886. return;
  887. /*
  888. * Devices changed PM state, we should recheck if latency
  889. * meets all functions' requirement
  890. */
  891. down_read(&pci_bus_sem);
  892. mutex_lock(&aspm_lock);
  893. pcie_update_aspm_capable(link->root);
  894. pcie_config_aspm_path(link);
  895. mutex_unlock(&aspm_lock);
  896. up_read(&pci_bus_sem);
  897. }
  898. void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
  899. {
  900. struct pcie_link_state *link = pdev->link_state;
  901. if (aspm_disabled || !link)
  902. return;
  903. if (aspm_policy != POLICY_POWERSAVE &&
  904. aspm_policy != POLICY_POWER_SUPERSAVE)
  905. return;
  906. down_read(&pci_bus_sem);
  907. mutex_lock(&aspm_lock);
  908. pcie_config_aspm_path(link);
  909. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  910. mutex_unlock(&aspm_lock);
  911. up_read(&pci_bus_sem);
  912. }
  913. static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
  914. {
  915. struct pci_dev *parent = pdev->bus->self;
  916. struct pcie_link_state *link;
  917. if (!pci_is_pcie(pdev))
  918. return;
  919. if (pdev->has_secondary_link)
  920. parent = pdev;
  921. if (!parent || !parent->link_state)
  922. return;
  923. /*
  924. * A driver requested that ASPM be disabled on this device, but
  925. * if we don't have permission to manage ASPM (e.g., on ACPI
  926. * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
  927. * the _OSC method), we can't honor that request. Windows has
  928. * a similar mechanism using "PciASPMOptOut", which is also
  929. * ignored in this situation.
  930. */
  931. if (aspm_disabled) {
  932. pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n");
  933. return;
  934. }
  935. if (sem)
  936. down_read(&pci_bus_sem);
  937. mutex_lock(&aspm_lock);
  938. link = parent->link_state;
  939. if (state & PCIE_LINK_STATE_L0S)
  940. link->aspm_disable |= ASPM_STATE_L0S;
  941. if (state & PCIE_LINK_STATE_L1)
  942. link->aspm_disable |= ASPM_STATE_L1;
  943. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  944. if (state & PCIE_LINK_STATE_CLKPM) {
  945. link->clkpm_capable = 0;
  946. pcie_set_clkpm(link, 0);
  947. }
  948. mutex_unlock(&aspm_lock);
  949. if (sem)
  950. up_read(&pci_bus_sem);
  951. }
  952. void pci_disable_link_state_locked(struct pci_dev *pdev, int state)
  953. {
  954. __pci_disable_link_state(pdev, state, false);
  955. }
  956. EXPORT_SYMBOL(pci_disable_link_state_locked);
  957. /**
  958. * pci_disable_link_state - Disable device's link state, so the link will
  959. * never enter specific states. Note that if the BIOS didn't grant ASPM
  960. * control to the OS, this does nothing because we can't touch the LNKCTL
  961. * register.
  962. *
  963. * @pdev: PCI device
  964. * @state: ASPM link state to disable
  965. */
  966. void pci_disable_link_state(struct pci_dev *pdev, int state)
  967. {
  968. __pci_disable_link_state(pdev, state, true);
  969. }
  970. EXPORT_SYMBOL(pci_disable_link_state);
  971. static int pcie_aspm_set_policy(const char *val,
  972. const struct kernel_param *kp)
  973. {
  974. int i;
  975. struct pcie_link_state *link;
  976. if (aspm_disabled)
  977. return -EPERM;
  978. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  979. if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
  980. break;
  981. if (i >= ARRAY_SIZE(policy_str))
  982. return -EINVAL;
  983. if (i == aspm_policy)
  984. return 0;
  985. down_read(&pci_bus_sem);
  986. mutex_lock(&aspm_lock);
  987. aspm_policy = i;
  988. list_for_each_entry(link, &link_list, sibling) {
  989. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  990. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  991. }
  992. mutex_unlock(&aspm_lock);
  993. up_read(&pci_bus_sem);
  994. return 0;
  995. }
  996. static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp)
  997. {
  998. int i, cnt = 0;
  999. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  1000. if (i == aspm_policy)
  1001. cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
  1002. else
  1003. cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
  1004. return cnt;
  1005. }
  1006. module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
  1007. NULL, 0644);
  1008. #ifdef CONFIG_PCIEASPM_DEBUG
  1009. static ssize_t link_state_show(struct device *dev,
  1010. struct device_attribute *attr,
  1011. char *buf)
  1012. {
  1013. struct pci_dev *pci_device = to_pci_dev(dev);
  1014. struct pcie_link_state *link_state = pci_device->link_state;
  1015. return sprintf(buf, "%d\n", link_state->aspm_enabled);
  1016. }
  1017. static ssize_t link_state_store(struct device *dev,
  1018. struct device_attribute *attr,
  1019. const char *buf,
  1020. size_t n)
  1021. {
  1022. struct pci_dev *pdev = to_pci_dev(dev);
  1023. struct pcie_link_state *link, *root = pdev->link_state->root;
  1024. u32 state;
  1025. if (aspm_disabled)
  1026. return -EPERM;
  1027. if (kstrtouint(buf, 10, &state))
  1028. return -EINVAL;
  1029. if ((state & ~ASPM_STATE_ALL) != 0)
  1030. return -EINVAL;
  1031. down_read(&pci_bus_sem);
  1032. mutex_lock(&aspm_lock);
  1033. list_for_each_entry(link, &link_list, sibling) {
  1034. if (link->root != root)
  1035. continue;
  1036. pcie_config_aspm_link(link, state);
  1037. }
  1038. mutex_unlock(&aspm_lock);
  1039. up_read(&pci_bus_sem);
  1040. return n;
  1041. }
  1042. static ssize_t clk_ctl_show(struct device *dev,
  1043. struct device_attribute *attr,
  1044. char *buf)
  1045. {
  1046. struct pci_dev *pci_device = to_pci_dev(dev);
  1047. struct pcie_link_state *link_state = pci_device->link_state;
  1048. return sprintf(buf, "%d\n", link_state->clkpm_enabled);
  1049. }
  1050. static ssize_t clk_ctl_store(struct device *dev,
  1051. struct device_attribute *attr,
  1052. const char *buf,
  1053. size_t n)
  1054. {
  1055. struct pci_dev *pdev = to_pci_dev(dev);
  1056. bool state;
  1057. if (strtobool(buf, &state))
  1058. return -EINVAL;
  1059. down_read(&pci_bus_sem);
  1060. mutex_lock(&aspm_lock);
  1061. pcie_set_clkpm_nocheck(pdev->link_state, state);
  1062. mutex_unlock(&aspm_lock);
  1063. up_read(&pci_bus_sem);
  1064. return n;
  1065. }
  1066. static DEVICE_ATTR_RW(link_state);
  1067. static DEVICE_ATTR_RW(clk_ctl);
  1068. static char power_group[] = "power";
  1069. void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
  1070. {
  1071. struct pcie_link_state *link_state = pdev->link_state;
  1072. if (!link_state)
  1073. return;
  1074. if (link_state->aspm_support)
  1075. sysfs_add_file_to_group(&pdev->dev.kobj,
  1076. &dev_attr_link_state.attr, power_group);
  1077. if (link_state->clkpm_capable)
  1078. sysfs_add_file_to_group(&pdev->dev.kobj,
  1079. &dev_attr_clk_ctl.attr, power_group);
  1080. }
  1081. void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
  1082. {
  1083. struct pcie_link_state *link_state = pdev->link_state;
  1084. if (!link_state)
  1085. return;
  1086. if (link_state->aspm_support)
  1087. sysfs_remove_file_from_group(&pdev->dev.kobj,
  1088. &dev_attr_link_state.attr, power_group);
  1089. if (link_state->clkpm_capable)
  1090. sysfs_remove_file_from_group(&pdev->dev.kobj,
  1091. &dev_attr_clk_ctl.attr, power_group);
  1092. }
  1093. #endif
  1094. static int __init pcie_aspm_disable(char *str)
  1095. {
  1096. if (!strcmp(str, "off")) {
  1097. aspm_policy = POLICY_DEFAULT;
  1098. aspm_disabled = 1;
  1099. aspm_support_enabled = false;
  1100. printk(KERN_INFO "PCIe ASPM is disabled\n");
  1101. } else if (!strcmp(str, "force")) {
  1102. aspm_force = 1;
  1103. printk(KERN_INFO "PCIe ASPM is forcibly enabled\n");
  1104. }
  1105. return 1;
  1106. }
  1107. __setup("pcie_aspm=", pcie_aspm_disable);
  1108. void pcie_no_aspm(void)
  1109. {
  1110. /*
  1111. * Disabling ASPM is intended to prevent the kernel from modifying
  1112. * existing hardware state, not to clear existing state. To that end:
  1113. * (a) set policy to POLICY_DEFAULT in order to avoid changing state
  1114. * (b) prevent userspace from changing policy
  1115. */
  1116. if (!aspm_force) {
  1117. aspm_policy = POLICY_DEFAULT;
  1118. aspm_disabled = 1;
  1119. }
  1120. }
  1121. bool pcie_aspm_support_enabled(void)
  1122. {
  1123. return aspm_support_enabled;
  1124. }
  1125. EXPORT_SYMBOL(pcie_aspm_support_enabled);