aspm.c 38 KB

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