aspm.c 35 KB

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