aspm.c 35 KB

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