aspm.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. /*
  361. * If we don't have LTR for the entire path from the Root Complex
  362. * to this device, we can't use ASPM L1.2 because it relies on the
  363. * LTR_L1.2_THRESHOLD. See PCIe r4.0, secs 5.5.4, 6.18.
  364. */
  365. if (!pdev->ltr_path)
  366. info->l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2;
  367. pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL1,
  368. &info->l1ss_ctl1);
  369. pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL2,
  370. &info->l1ss_ctl2);
  371. }
  372. static void pcie_aspm_check_latency(struct pci_dev *endpoint)
  373. {
  374. u32 latency, l1_switch_latency = 0;
  375. struct aspm_latency *acceptable;
  376. struct pcie_link_state *link;
  377. /* Device not in D0 doesn't need latency check */
  378. if ((endpoint->current_state != PCI_D0) &&
  379. (endpoint->current_state != PCI_UNKNOWN))
  380. return;
  381. link = endpoint->bus->self->link_state;
  382. acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
  383. while (link) {
  384. /* Check upstream direction L0s latency */
  385. if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
  386. (link->latency_up.l0s > acceptable->l0s))
  387. link->aspm_capable &= ~ASPM_STATE_L0S_UP;
  388. /* Check downstream direction L0s latency */
  389. if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
  390. (link->latency_dw.l0s > acceptable->l0s))
  391. link->aspm_capable &= ~ASPM_STATE_L0S_DW;
  392. /*
  393. * Check L1 latency.
  394. * Every switch on the path to root complex need 1
  395. * more microsecond for L1. Spec doesn't mention L0s.
  396. *
  397. * The exit latencies for L1 substates are not advertised
  398. * by a device. Since the spec also doesn't mention a way
  399. * to determine max latencies introduced by enabling L1
  400. * substates on the components, it is not clear how to do
  401. * a L1 substate exit latency check. We assume that the
  402. * L1 exit latencies advertised by a device include L1
  403. * substate latencies (and hence do not do any check).
  404. */
  405. latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1);
  406. if ((link->aspm_capable & ASPM_STATE_L1) &&
  407. (latency + l1_switch_latency > acceptable->l1))
  408. link->aspm_capable &= ~ASPM_STATE_L1;
  409. l1_switch_latency += 1000;
  410. link = link->parent;
  411. }
  412. }
  413. /*
  414. * The L1 PM substate capability is only implemented in function 0 in a
  415. * multi function device.
  416. */
  417. static struct pci_dev *pci_function_0(struct pci_bus *linkbus)
  418. {
  419. struct pci_dev *child;
  420. list_for_each_entry(child, &linkbus->devices, bus_list)
  421. if (PCI_FUNC(child->devfn) == 0)
  422. return child;
  423. return NULL;
  424. }
  425. /* Calculate L1.2 PM substate timing parameters */
  426. static void aspm_calc_l1ss_info(struct pcie_link_state *link,
  427. struct aspm_register_info *upreg,
  428. struct aspm_register_info *dwreg)
  429. {
  430. u32 val1, val2, scale1, scale2;
  431. u32 t_common_mode, t_power_on, l1_2_threshold, scale, value;
  432. link->l1ss.up_cap_ptr = upreg->l1ss_cap_ptr;
  433. link->l1ss.dw_cap_ptr = dwreg->l1ss_cap_ptr;
  434. link->l1ss.ctl1 = link->l1ss.ctl2 = 0;
  435. if (!(link->aspm_support & ASPM_STATE_L1_2_MASK))
  436. return;
  437. /* Choose the greater of the two Port Common_Mode_Restore_Times */
  438. val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
  439. val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
  440. t_common_mode = max(val1, val2);
  441. /* Choose the greater of the two Port T_POWER_ON times */
  442. val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
  443. scale1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
  444. val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
  445. scale2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
  446. if (calc_l1ss_pwron(link->pdev, scale1, val1) >
  447. calc_l1ss_pwron(link->downstream, scale2, val2)) {
  448. link->l1ss.ctl2 |= scale1 | (val1 << 3);
  449. t_power_on = calc_l1ss_pwron(link->pdev, scale1, val1);
  450. } else {
  451. link->l1ss.ctl2 |= scale2 | (val2 << 3);
  452. t_power_on = calc_l1ss_pwron(link->downstream, scale2, val2);
  453. }
  454. /*
  455. * Set LTR_L1.2_THRESHOLD to the time required to transition the
  456. * Link from L0 to L1.2 and back to L0 so we enter L1.2 only if
  457. * downstream devices report (via LTR) that they can tolerate at
  458. * least that much latency.
  459. *
  460. * Based on PCIe r3.1, sec 5.5.3.3.1, Figures 5-16 and 5-17, and
  461. * Table 5-11. T(POWER_OFF) is at most 2us and T(L1.2) is at
  462. * least 4us.
  463. */
  464. l1_2_threshold = 2 + 4 + t_common_mode + t_power_on;
  465. encode_l12_threshold(l1_2_threshold, &scale, &value);
  466. link->l1ss.ctl1 |= t_common_mode << 8 | scale << 29 | value << 16;
  467. }
  468. static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
  469. {
  470. struct pci_dev *child = link->downstream, *parent = link->pdev;
  471. struct pci_bus *linkbus = parent->subordinate;
  472. struct aspm_register_info upreg, dwreg;
  473. if (blacklist) {
  474. /* Set enabled/disable so that we will disable ASPM later */
  475. link->aspm_enabled = ASPM_STATE_ALL;
  476. link->aspm_disable = ASPM_STATE_ALL;
  477. return;
  478. }
  479. /* Get upstream/downstream components' register state */
  480. pcie_get_aspm_reg(parent, &upreg);
  481. pcie_get_aspm_reg(child, &dwreg);
  482. /*
  483. * If ASPM not supported, don't mess with the clocks and link,
  484. * bail out now.
  485. */
  486. if (!(upreg.support & dwreg.support))
  487. return;
  488. /* Configure common clock before checking latencies */
  489. pcie_aspm_configure_common_clock(link);
  490. /*
  491. * Re-read upstream/downstream components' register state
  492. * after clock configuration
  493. */
  494. pcie_get_aspm_reg(parent, &upreg);
  495. pcie_get_aspm_reg(child, &dwreg);
  496. /*
  497. * Setup L0s state
  498. *
  499. * Note that we must not enable L0s in either direction on a
  500. * given link unless components on both sides of the link each
  501. * support L0s.
  502. */
  503. if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S)
  504. link->aspm_support |= ASPM_STATE_L0S;
  505. if (dwreg.enabled & PCIE_LINK_STATE_L0S)
  506. link->aspm_enabled |= ASPM_STATE_L0S_UP;
  507. if (upreg.enabled & PCIE_LINK_STATE_L0S)
  508. link->aspm_enabled |= ASPM_STATE_L0S_DW;
  509. link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s);
  510. link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s);
  511. /* Setup L1 state */
  512. if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1)
  513. link->aspm_support |= ASPM_STATE_L1;
  514. if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1)
  515. link->aspm_enabled |= ASPM_STATE_L1;
  516. link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1);
  517. link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1);
  518. /* Setup L1 substate */
  519. if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1)
  520. link->aspm_support |= ASPM_STATE_L1_1;
  521. if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2)
  522. link->aspm_support |= ASPM_STATE_L1_2;
  523. if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1)
  524. link->aspm_support |= ASPM_STATE_L1_1_PCIPM;
  525. if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2)
  526. link->aspm_support |= ASPM_STATE_L1_2_PCIPM;
  527. if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1)
  528. link->aspm_enabled |= ASPM_STATE_L1_1;
  529. if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2)
  530. link->aspm_enabled |= ASPM_STATE_L1_2;
  531. if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1)
  532. link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM;
  533. if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2)
  534. link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM;
  535. if (link->aspm_support & ASPM_STATE_L1SS)
  536. aspm_calc_l1ss_info(link, &upreg, &dwreg);
  537. /* Save default state */
  538. link->aspm_default = link->aspm_enabled;
  539. /* Setup initial capable state. Will be updated later */
  540. link->aspm_capable = link->aspm_support;
  541. /*
  542. * If the downstream component has pci bridge function, don't
  543. * do ASPM for now.
  544. */
  545. list_for_each_entry(child, &linkbus->devices, bus_list) {
  546. if (pci_pcie_type(child) == PCI_EXP_TYPE_PCI_BRIDGE) {
  547. link->aspm_disable = ASPM_STATE_ALL;
  548. break;
  549. }
  550. }
  551. /* Get and check endpoint acceptable latencies */
  552. list_for_each_entry(child, &linkbus->devices, bus_list) {
  553. u32 reg32, encoding;
  554. struct aspm_latency *acceptable =
  555. &link->acceptable[PCI_FUNC(child->devfn)];
  556. if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT &&
  557. pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)
  558. continue;
  559. pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
  560. /* Calculate endpoint L0s acceptable latency */
  561. encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
  562. acceptable->l0s = calc_l0s_acceptable(encoding);
  563. /* Calculate endpoint L1 acceptable latency */
  564. encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
  565. acceptable->l1 = calc_l1_acceptable(encoding);
  566. pcie_aspm_check_latency(child);
  567. }
  568. }
  569. static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos,
  570. u32 clear, u32 set)
  571. {
  572. u32 val;
  573. pci_read_config_dword(pdev, pos, &val);
  574. val &= ~clear;
  575. val |= set;
  576. pci_write_config_dword(pdev, pos, val);
  577. }
  578. /* Configure the ASPM L1 substates */
  579. static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state)
  580. {
  581. u32 val, enable_req;
  582. struct pci_dev *child = link->downstream, *parent = link->pdev;
  583. u32 up_cap_ptr = link->l1ss.up_cap_ptr;
  584. u32 dw_cap_ptr = link->l1ss.dw_cap_ptr;
  585. enable_req = (link->aspm_enabled ^ state) & state;
  586. /*
  587. * Here are the rules specified in the PCIe spec for enabling L1SS:
  588. * - When enabling L1.x, enable bit at parent first, then at child
  589. * - When disabling L1.x, disable bit at child first, then at parent
  590. * - When enabling ASPM L1.x, need to disable L1
  591. * (at child followed by parent).
  592. * - The ASPM/PCIPM L1.2 must be disabled while programming timing
  593. * parameters
  594. *
  595. * To keep it simple, disable all L1SS bits first, and later enable
  596. * what is needed.
  597. */
  598. /* Disable all L1 substates */
  599. pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
  600. PCI_L1SS_CTL1_L1SS_MASK, 0);
  601. pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
  602. PCI_L1SS_CTL1_L1SS_MASK, 0);
  603. /*
  604. * If needed, disable L1, and it gets enabled later
  605. * in pcie_config_aspm_link().
  606. */
  607. if (enable_req & (ASPM_STATE_L1_1 | ASPM_STATE_L1_2)) {
  608. pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
  609. PCI_EXP_LNKCTL_ASPM_L1, 0);
  610. pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
  611. PCI_EXP_LNKCTL_ASPM_L1, 0);
  612. }
  613. if (enable_req & ASPM_STATE_L1_2_MASK) {
  614. /* Program T_POWER_ON times in both ports */
  615. pci_write_config_dword(parent, up_cap_ptr + PCI_L1SS_CTL2,
  616. link->l1ss.ctl2);
  617. pci_write_config_dword(child, dw_cap_ptr + PCI_L1SS_CTL2,
  618. link->l1ss.ctl2);
  619. /* Program Common_Mode_Restore_Time in upstream device */
  620. pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
  621. PCI_L1SS_CTL1_CM_RESTORE_TIME,
  622. link->l1ss.ctl1);
  623. /* Program LTR_L1.2_THRESHOLD time in both ports */
  624. pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
  625. PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
  626. PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
  627. link->l1ss.ctl1);
  628. pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
  629. PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
  630. PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
  631. link->l1ss.ctl1);
  632. }
  633. val = 0;
  634. if (state & ASPM_STATE_L1_1)
  635. val |= PCI_L1SS_CTL1_ASPM_L1_1;
  636. if (state & ASPM_STATE_L1_2)
  637. val |= PCI_L1SS_CTL1_ASPM_L1_2;
  638. if (state & ASPM_STATE_L1_1_PCIPM)
  639. val |= PCI_L1SS_CTL1_PCIPM_L1_1;
  640. if (state & ASPM_STATE_L1_2_PCIPM)
  641. val |= PCI_L1SS_CTL1_PCIPM_L1_2;
  642. /* Enable what we need to enable */
  643. pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
  644. PCI_L1SS_CAP_L1_PM_SS, val);
  645. pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
  646. PCI_L1SS_CAP_L1_PM_SS, val);
  647. }
  648. static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
  649. {
  650. pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
  651. PCI_EXP_LNKCTL_ASPMC, val);
  652. }
  653. static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
  654. {
  655. u32 upstream = 0, dwstream = 0;
  656. struct pci_dev *child = link->downstream, *parent = link->pdev;
  657. struct pci_bus *linkbus = parent->subordinate;
  658. /* Enable only the states that were not explicitly disabled */
  659. state &= (link->aspm_capable & ~link->aspm_disable);
  660. /* Can't enable any substates if L1 is not enabled */
  661. if (!(state & ASPM_STATE_L1))
  662. state &= ~ASPM_STATE_L1SS;
  663. /* Spec says both ports must be in D0 before enabling PCI PM substates*/
  664. if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) {
  665. state &= ~ASPM_STATE_L1_SS_PCIPM;
  666. state |= (link->aspm_enabled & ASPM_STATE_L1_SS_PCIPM);
  667. }
  668. /* Nothing to do if the link is already in the requested state */
  669. if (link->aspm_enabled == state)
  670. return;
  671. /* Convert ASPM state to upstream/downstream ASPM register state */
  672. if (state & ASPM_STATE_L0S_UP)
  673. dwstream |= PCI_EXP_LNKCTL_ASPM_L0S;
  674. if (state & ASPM_STATE_L0S_DW)
  675. upstream |= PCI_EXP_LNKCTL_ASPM_L0S;
  676. if (state & ASPM_STATE_L1) {
  677. upstream |= PCI_EXP_LNKCTL_ASPM_L1;
  678. dwstream |= PCI_EXP_LNKCTL_ASPM_L1;
  679. }
  680. if (link->aspm_capable & ASPM_STATE_L1SS)
  681. pcie_config_aspm_l1ss(link, state);
  682. /*
  683. * Spec 2.0 suggests all functions should be configured the
  684. * same setting for ASPM. Enabling ASPM L1 should be done in
  685. * upstream component first and then downstream, and vice
  686. * versa for disabling ASPM L1. Spec doesn't mention L0S.
  687. */
  688. if (state & ASPM_STATE_L1)
  689. pcie_config_aspm_dev(parent, upstream);
  690. list_for_each_entry(child, &linkbus->devices, bus_list)
  691. pcie_config_aspm_dev(child, dwstream);
  692. if (!(state & ASPM_STATE_L1))
  693. pcie_config_aspm_dev(parent, upstream);
  694. link->aspm_enabled = state;
  695. }
  696. static void pcie_config_aspm_path(struct pcie_link_state *link)
  697. {
  698. while (link) {
  699. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  700. link = link->parent;
  701. }
  702. }
  703. static void free_link_state(struct pcie_link_state *link)
  704. {
  705. link->pdev->link_state = NULL;
  706. kfree(link);
  707. }
  708. static int pcie_aspm_sanity_check(struct pci_dev *pdev)
  709. {
  710. struct pci_dev *child;
  711. u32 reg32;
  712. /*
  713. * Some functions in a slot might not all be PCIe functions,
  714. * very strange. Disable ASPM for the whole slot
  715. */
  716. list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
  717. if (!pci_is_pcie(child))
  718. return -EINVAL;
  719. /*
  720. * If ASPM is disabled then we're not going to change
  721. * the BIOS state. It's safe to continue even if it's a
  722. * pre-1.1 device
  723. */
  724. if (aspm_disabled)
  725. continue;
  726. /*
  727. * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
  728. * RBER bit to determine if a function is 1.1 version device
  729. */
  730. pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
  731. if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
  732. pci_info(child, "disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force'\n");
  733. return -EINVAL;
  734. }
  735. }
  736. return 0;
  737. }
  738. static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
  739. {
  740. struct pcie_link_state *link;
  741. link = kzalloc(sizeof(*link), GFP_KERNEL);
  742. if (!link)
  743. return NULL;
  744. INIT_LIST_HEAD(&link->sibling);
  745. INIT_LIST_HEAD(&link->children);
  746. INIT_LIST_HEAD(&link->link);
  747. link->pdev = pdev;
  748. link->downstream = pci_function_0(pdev->subordinate);
  749. /*
  750. * Root Ports and PCI/PCI-X to PCIe Bridges are roots of PCIe
  751. * hierarchies. Note that some PCIe host implementations omit
  752. * the root ports entirely, in which case a downstream port on
  753. * a switch may become the root of the link state chain for all
  754. * its subordinate endpoints.
  755. */
  756. if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
  757. pci_pcie_type(pdev) == PCI_EXP_TYPE_PCIE_BRIDGE ||
  758. !pdev->bus->parent->self) {
  759. link->root = link;
  760. } else {
  761. struct pcie_link_state *parent;
  762. parent = pdev->bus->parent->self->link_state;
  763. if (!parent) {
  764. kfree(link);
  765. return NULL;
  766. }
  767. link->parent = parent;
  768. link->root = link->parent->root;
  769. list_add(&link->link, &parent->children);
  770. }
  771. list_add(&link->sibling, &link_list);
  772. pdev->link_state = link;
  773. return link;
  774. }
  775. /*
  776. * pcie_aspm_init_link_state: Initiate PCI express link state.
  777. * It is called after the pcie and its children devices are scanned.
  778. * @pdev: the root port or switch downstream port
  779. */
  780. void pcie_aspm_init_link_state(struct pci_dev *pdev)
  781. {
  782. struct pcie_link_state *link;
  783. int blacklist = !!pcie_aspm_sanity_check(pdev);
  784. if (!aspm_support_enabled)
  785. return;
  786. if (pdev->link_state)
  787. return;
  788. /*
  789. * We allocate pcie_link_state for the component on the upstream
  790. * end of a Link, so there's nothing to do unless this device has a
  791. * Link on its secondary side.
  792. */
  793. if (!pdev->has_secondary_link)
  794. return;
  795. /* VIA has a strange chipset, root port is under a bridge */
  796. if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT &&
  797. pdev->bus->self)
  798. return;
  799. down_read(&pci_bus_sem);
  800. if (list_empty(&pdev->subordinate->devices))
  801. goto out;
  802. mutex_lock(&aspm_lock);
  803. link = alloc_pcie_link_state(pdev);
  804. if (!link)
  805. goto unlock;
  806. /*
  807. * Setup initial ASPM state. Note that we need to configure
  808. * upstream links also because capable state of them can be
  809. * update through pcie_aspm_cap_init().
  810. */
  811. pcie_aspm_cap_init(link, blacklist);
  812. /* Setup initial Clock PM state */
  813. pcie_clkpm_cap_init(link, blacklist);
  814. /*
  815. * At this stage drivers haven't had an opportunity to change the
  816. * link policy setting. Enabling ASPM on broken hardware can cripple
  817. * it even before the driver has had a chance to disable ASPM, so
  818. * default to a safe level right now. If we're enabling ASPM beyond
  819. * the BIOS's expectation, we'll do so once pci_enable_device() is
  820. * called.
  821. */
  822. if (aspm_policy != POLICY_POWERSAVE &&
  823. aspm_policy != POLICY_POWER_SUPERSAVE) {
  824. pcie_config_aspm_path(link);
  825. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  826. }
  827. unlock:
  828. mutex_unlock(&aspm_lock);
  829. out:
  830. up_read(&pci_bus_sem);
  831. }
  832. /* Recheck latencies and update aspm_capable for links under the root */
  833. static void pcie_update_aspm_capable(struct pcie_link_state *root)
  834. {
  835. struct pcie_link_state *link;
  836. BUG_ON(root->parent);
  837. list_for_each_entry(link, &link_list, sibling) {
  838. if (link->root != root)
  839. continue;
  840. link->aspm_capable = link->aspm_support;
  841. }
  842. list_for_each_entry(link, &link_list, sibling) {
  843. struct pci_dev *child;
  844. struct pci_bus *linkbus = link->pdev->subordinate;
  845. if (link->root != root)
  846. continue;
  847. list_for_each_entry(child, &linkbus->devices, bus_list) {
  848. if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) &&
  849. (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END))
  850. continue;
  851. pcie_aspm_check_latency(child);
  852. }
  853. }
  854. }
  855. /* @pdev: the endpoint device */
  856. void pcie_aspm_exit_link_state(struct pci_dev *pdev)
  857. {
  858. struct pci_dev *parent = pdev->bus->self;
  859. struct pcie_link_state *link, *root, *parent_link;
  860. if (!parent || !parent->link_state)
  861. return;
  862. down_read(&pci_bus_sem);
  863. mutex_lock(&aspm_lock);
  864. /*
  865. * All PCIe functions are in one slot, remove one function will remove
  866. * the whole slot, so just wait until we are the last function left.
  867. */
  868. if (!list_empty(&parent->subordinate->devices))
  869. goto out;
  870. link = parent->link_state;
  871. root = link->root;
  872. parent_link = link->parent;
  873. /* All functions are removed, so just disable ASPM for the link */
  874. pcie_config_aspm_link(link, 0);
  875. list_del(&link->sibling);
  876. list_del(&link->link);
  877. /* Clock PM is for endpoint device */
  878. free_link_state(link);
  879. /* Recheck latencies and configure upstream links */
  880. if (parent_link) {
  881. pcie_update_aspm_capable(root);
  882. pcie_config_aspm_path(parent_link);
  883. }
  884. out:
  885. mutex_unlock(&aspm_lock);
  886. up_read(&pci_bus_sem);
  887. }
  888. /* @pdev: the root port or switch downstream port */
  889. void pcie_aspm_pm_state_change(struct pci_dev *pdev)
  890. {
  891. struct pcie_link_state *link = pdev->link_state;
  892. if (aspm_disabled || !link)
  893. return;
  894. /*
  895. * Devices changed PM state, we should recheck if latency
  896. * meets all functions' requirement
  897. */
  898. down_read(&pci_bus_sem);
  899. mutex_lock(&aspm_lock);
  900. pcie_update_aspm_capable(link->root);
  901. pcie_config_aspm_path(link);
  902. mutex_unlock(&aspm_lock);
  903. up_read(&pci_bus_sem);
  904. }
  905. void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
  906. {
  907. struct pcie_link_state *link = pdev->link_state;
  908. if (aspm_disabled || !link)
  909. return;
  910. if (aspm_policy != POLICY_POWERSAVE &&
  911. aspm_policy != POLICY_POWER_SUPERSAVE)
  912. return;
  913. down_read(&pci_bus_sem);
  914. mutex_lock(&aspm_lock);
  915. pcie_config_aspm_path(link);
  916. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  917. mutex_unlock(&aspm_lock);
  918. up_read(&pci_bus_sem);
  919. }
  920. static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
  921. {
  922. struct pci_dev *parent = pdev->bus->self;
  923. struct pcie_link_state *link;
  924. if (!pci_is_pcie(pdev))
  925. return;
  926. if (pdev->has_secondary_link)
  927. parent = pdev;
  928. if (!parent || !parent->link_state)
  929. return;
  930. /*
  931. * A driver requested that ASPM be disabled on this device, but
  932. * if we don't have permission to manage ASPM (e.g., on ACPI
  933. * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
  934. * the _OSC method), we can't honor that request. Windows has
  935. * a similar mechanism using "PciASPMOptOut", which is also
  936. * ignored in this situation.
  937. */
  938. if (aspm_disabled) {
  939. pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n");
  940. return;
  941. }
  942. if (sem)
  943. down_read(&pci_bus_sem);
  944. mutex_lock(&aspm_lock);
  945. link = parent->link_state;
  946. if (state & PCIE_LINK_STATE_L0S)
  947. link->aspm_disable |= ASPM_STATE_L0S;
  948. if (state & PCIE_LINK_STATE_L1)
  949. link->aspm_disable |= ASPM_STATE_L1;
  950. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  951. if (state & PCIE_LINK_STATE_CLKPM) {
  952. link->clkpm_capable = 0;
  953. pcie_set_clkpm(link, 0);
  954. }
  955. mutex_unlock(&aspm_lock);
  956. if (sem)
  957. up_read(&pci_bus_sem);
  958. }
  959. void pci_disable_link_state_locked(struct pci_dev *pdev, int state)
  960. {
  961. __pci_disable_link_state(pdev, state, false);
  962. }
  963. EXPORT_SYMBOL(pci_disable_link_state_locked);
  964. /**
  965. * pci_disable_link_state - Disable device's link state, so the link will
  966. * never enter specific states. Note that if the BIOS didn't grant ASPM
  967. * control to the OS, this does nothing because we can't touch the LNKCTL
  968. * register.
  969. *
  970. * @pdev: PCI device
  971. * @state: ASPM link state to disable
  972. */
  973. void pci_disable_link_state(struct pci_dev *pdev, int state)
  974. {
  975. __pci_disable_link_state(pdev, state, true);
  976. }
  977. EXPORT_SYMBOL(pci_disable_link_state);
  978. static int pcie_aspm_set_policy(const char *val,
  979. const struct kernel_param *kp)
  980. {
  981. int i;
  982. struct pcie_link_state *link;
  983. if (aspm_disabled)
  984. return -EPERM;
  985. i = sysfs_match_string(policy_str, val);
  986. if (i < 0)
  987. return i;
  988. if (i == aspm_policy)
  989. return 0;
  990. down_read(&pci_bus_sem);
  991. mutex_lock(&aspm_lock);
  992. aspm_policy = i;
  993. list_for_each_entry(link, &link_list, sibling) {
  994. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  995. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  996. }
  997. mutex_unlock(&aspm_lock);
  998. up_read(&pci_bus_sem);
  999. return 0;
  1000. }
  1001. static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp)
  1002. {
  1003. int i, cnt = 0;
  1004. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  1005. if (i == aspm_policy)
  1006. cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
  1007. else
  1008. cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
  1009. return cnt;
  1010. }
  1011. module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
  1012. NULL, 0644);
  1013. #ifdef CONFIG_PCIEASPM_DEBUG
  1014. static ssize_t link_state_show(struct device *dev,
  1015. struct device_attribute *attr,
  1016. char *buf)
  1017. {
  1018. struct pci_dev *pci_device = to_pci_dev(dev);
  1019. struct pcie_link_state *link_state = pci_device->link_state;
  1020. return sprintf(buf, "%d\n", link_state->aspm_enabled);
  1021. }
  1022. static ssize_t link_state_store(struct device *dev,
  1023. struct device_attribute *attr,
  1024. const char *buf,
  1025. size_t n)
  1026. {
  1027. struct pci_dev *pdev = to_pci_dev(dev);
  1028. struct pcie_link_state *link, *root = pdev->link_state->root;
  1029. u32 state;
  1030. if (aspm_disabled)
  1031. return -EPERM;
  1032. if (kstrtouint(buf, 10, &state))
  1033. return -EINVAL;
  1034. if ((state & ~ASPM_STATE_ALL) != 0)
  1035. return -EINVAL;
  1036. down_read(&pci_bus_sem);
  1037. mutex_lock(&aspm_lock);
  1038. list_for_each_entry(link, &link_list, sibling) {
  1039. if (link->root != root)
  1040. continue;
  1041. pcie_config_aspm_link(link, state);
  1042. }
  1043. mutex_unlock(&aspm_lock);
  1044. up_read(&pci_bus_sem);
  1045. return n;
  1046. }
  1047. static ssize_t clk_ctl_show(struct device *dev,
  1048. struct device_attribute *attr,
  1049. char *buf)
  1050. {
  1051. struct pci_dev *pci_device = to_pci_dev(dev);
  1052. struct pcie_link_state *link_state = pci_device->link_state;
  1053. return sprintf(buf, "%d\n", link_state->clkpm_enabled);
  1054. }
  1055. static ssize_t clk_ctl_store(struct device *dev,
  1056. struct device_attribute *attr,
  1057. const char *buf,
  1058. size_t n)
  1059. {
  1060. struct pci_dev *pdev = to_pci_dev(dev);
  1061. bool state;
  1062. if (strtobool(buf, &state))
  1063. return -EINVAL;
  1064. down_read(&pci_bus_sem);
  1065. mutex_lock(&aspm_lock);
  1066. pcie_set_clkpm_nocheck(pdev->link_state, state);
  1067. mutex_unlock(&aspm_lock);
  1068. up_read(&pci_bus_sem);
  1069. return n;
  1070. }
  1071. static DEVICE_ATTR_RW(link_state);
  1072. static DEVICE_ATTR_RW(clk_ctl);
  1073. static char power_group[] = "power";
  1074. void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
  1075. {
  1076. struct pcie_link_state *link_state = pdev->link_state;
  1077. if (!link_state)
  1078. return;
  1079. if (link_state->aspm_support)
  1080. sysfs_add_file_to_group(&pdev->dev.kobj,
  1081. &dev_attr_link_state.attr, power_group);
  1082. if (link_state->clkpm_capable)
  1083. sysfs_add_file_to_group(&pdev->dev.kobj,
  1084. &dev_attr_clk_ctl.attr, power_group);
  1085. }
  1086. void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
  1087. {
  1088. struct pcie_link_state *link_state = pdev->link_state;
  1089. if (!link_state)
  1090. return;
  1091. if (link_state->aspm_support)
  1092. sysfs_remove_file_from_group(&pdev->dev.kobj,
  1093. &dev_attr_link_state.attr, power_group);
  1094. if (link_state->clkpm_capable)
  1095. sysfs_remove_file_from_group(&pdev->dev.kobj,
  1096. &dev_attr_clk_ctl.attr, power_group);
  1097. }
  1098. #endif
  1099. static int __init pcie_aspm_disable(char *str)
  1100. {
  1101. if (!strcmp(str, "off")) {
  1102. aspm_policy = POLICY_DEFAULT;
  1103. aspm_disabled = 1;
  1104. aspm_support_enabled = false;
  1105. printk(KERN_INFO "PCIe ASPM is disabled\n");
  1106. } else if (!strcmp(str, "force")) {
  1107. aspm_force = 1;
  1108. printk(KERN_INFO "PCIe ASPM is forcibly enabled\n");
  1109. }
  1110. return 1;
  1111. }
  1112. __setup("pcie_aspm=", pcie_aspm_disable);
  1113. void pcie_no_aspm(void)
  1114. {
  1115. /*
  1116. * Disabling ASPM is intended to prevent the kernel from modifying
  1117. * existing hardware state, not to clear existing state. To that end:
  1118. * (a) set policy to POLICY_DEFAULT in order to avoid changing state
  1119. * (b) prevent userspace from changing policy
  1120. */
  1121. if (!aspm_force) {
  1122. aspm_policy = POLICY_DEFAULT;
  1123. aspm_disabled = 1;
  1124. }
  1125. }
  1126. bool pcie_aspm_support_enabled(void)
  1127. {
  1128. return aspm_support_enabled;
  1129. }
  1130. EXPORT_SYMBOL(pcie_aspm_support_enabled);