aspm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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_L0S (ASPM_STATE_L0S_UP | ASPM_STATE_L0S_DW)
  31. #define ASPM_STATE_ALL (ASPM_STATE_L0S | ASPM_STATE_L1)
  32. struct aspm_latency {
  33. u32 l0s; /* L0s latency (nsec) */
  34. u32 l1; /* L1 latency (nsec) */
  35. };
  36. struct pcie_link_state {
  37. struct pci_dev *pdev; /* Upstream component of the Link */
  38. struct pcie_link_state *root; /* pointer to the root port link */
  39. struct pcie_link_state *parent; /* pointer to the parent Link state */
  40. struct list_head sibling; /* node in link_list */
  41. struct list_head children; /* list of child link states */
  42. struct list_head link; /* node in parent's children list */
  43. /* ASPM state */
  44. u32 aspm_support:3; /* Supported ASPM state */
  45. u32 aspm_enabled:3; /* Enabled ASPM state */
  46. u32 aspm_capable:3; /* Capable ASPM state with latency */
  47. u32 aspm_default:3; /* Default ASPM state by BIOS */
  48. u32 aspm_disable:3; /* Disabled ASPM state */
  49. /* Clock PM state */
  50. u32 clkpm_capable:1; /* Clock PM capable? */
  51. u32 clkpm_enabled:1; /* Current Clock PM state */
  52. u32 clkpm_default:1; /* Default Clock PM state by BIOS */
  53. /* Exit latencies */
  54. struct aspm_latency latency_up; /* Upstream direction exit latency */
  55. struct aspm_latency latency_dw; /* Downstream direction exit latency */
  56. /*
  57. * Endpoint acceptable latencies. A pcie downstream port only
  58. * has one slot under it, so at most there are 8 functions.
  59. */
  60. struct aspm_latency acceptable[8];
  61. };
  62. static int aspm_disabled, aspm_force;
  63. static bool aspm_support_enabled = true;
  64. static DEFINE_MUTEX(aspm_lock);
  65. static LIST_HEAD(link_list);
  66. #define POLICY_DEFAULT 0 /* BIOS default setting */
  67. #define POLICY_PERFORMANCE 1 /* high performance */
  68. #define POLICY_POWERSAVE 2 /* high power saving */
  69. #ifdef CONFIG_PCIEASPM_PERFORMANCE
  70. static int aspm_policy = POLICY_PERFORMANCE;
  71. #elif defined CONFIG_PCIEASPM_POWERSAVE
  72. static int aspm_policy = POLICY_POWERSAVE;
  73. #else
  74. static int aspm_policy;
  75. #endif
  76. static const char *policy_str[] = {
  77. [POLICY_DEFAULT] = "default",
  78. [POLICY_PERFORMANCE] = "performance",
  79. [POLICY_POWERSAVE] = "powersave"
  80. };
  81. #define LINK_RETRAIN_TIMEOUT HZ
  82. static int policy_to_aspm_state(struct pcie_link_state *link)
  83. {
  84. switch (aspm_policy) {
  85. case POLICY_PERFORMANCE:
  86. /* Disable ASPM and Clock PM */
  87. return 0;
  88. case POLICY_POWERSAVE:
  89. /* Enable ASPM L0s/L1 */
  90. return ASPM_STATE_ALL;
  91. case POLICY_DEFAULT:
  92. return link->aspm_default;
  93. }
  94. return 0;
  95. }
  96. static int policy_to_clkpm_state(struct pcie_link_state *link)
  97. {
  98. switch (aspm_policy) {
  99. case POLICY_PERFORMANCE:
  100. /* Disable ASPM and Clock PM */
  101. return 0;
  102. case POLICY_POWERSAVE:
  103. /* Disable Clock PM */
  104. return 1;
  105. case POLICY_DEFAULT:
  106. return link->clkpm_default;
  107. }
  108. return 0;
  109. }
  110. static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
  111. {
  112. struct pci_dev *child;
  113. struct pci_bus *linkbus = link->pdev->subordinate;
  114. u32 val = enable ? PCI_EXP_LNKCTL_CLKREQ_EN : 0;
  115. list_for_each_entry(child, &linkbus->devices, bus_list)
  116. pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
  117. PCI_EXP_LNKCTL_CLKREQ_EN,
  118. val);
  119. link->clkpm_enabled = !!enable;
  120. }
  121. static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
  122. {
  123. /* Don't enable Clock PM if the link is not Clock PM capable */
  124. if (!link->clkpm_capable)
  125. enable = 0;
  126. /* Need nothing if the specified equals to current state */
  127. if (link->clkpm_enabled == enable)
  128. return;
  129. pcie_set_clkpm_nocheck(link, enable);
  130. }
  131. static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
  132. {
  133. int capable = 1, enabled = 1;
  134. u32 reg32;
  135. u16 reg16;
  136. struct pci_dev *child;
  137. struct pci_bus *linkbus = link->pdev->subordinate;
  138. /* All functions should have the same cap and state, take the worst */
  139. list_for_each_entry(child, &linkbus->devices, bus_list) {
  140. pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &reg32);
  141. if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
  142. capable = 0;
  143. enabled = 0;
  144. break;
  145. }
  146. pcie_capability_read_word(child, PCI_EXP_LNKCTL, &reg16);
  147. if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
  148. enabled = 0;
  149. }
  150. link->clkpm_enabled = enabled;
  151. link->clkpm_default = enabled;
  152. link->clkpm_capable = (blacklist) ? 0 : capable;
  153. }
  154. /*
  155. * pcie_aspm_configure_common_clock: check if the 2 ends of a link
  156. * could use common clock. If they are, configure them to use the
  157. * common clock. That will reduce the ASPM state exit latency.
  158. */
  159. static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
  160. {
  161. int same_clock = 1;
  162. u16 reg16, parent_reg, child_reg[8];
  163. unsigned long start_jiffies;
  164. struct pci_dev *child, *parent = link->pdev;
  165. struct pci_bus *linkbus = parent->subordinate;
  166. /*
  167. * All functions of a slot should have the same Slot Clock
  168. * Configuration, so just check one function
  169. */
  170. child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
  171. BUG_ON(!pci_is_pcie(child));
  172. /* Check downstream component if bit Slot Clock Configuration is 1 */
  173. pcie_capability_read_word(child, PCI_EXP_LNKSTA, &reg16);
  174. if (!(reg16 & PCI_EXP_LNKSTA_SLC))
  175. same_clock = 0;
  176. /* Check upstream component if bit Slot Clock Configuration is 1 */
  177. pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &reg16);
  178. if (!(reg16 & PCI_EXP_LNKSTA_SLC))
  179. same_clock = 0;
  180. /* Configure downstream component, all functions */
  181. list_for_each_entry(child, &linkbus->devices, bus_list) {
  182. pcie_capability_read_word(child, PCI_EXP_LNKCTL, &reg16);
  183. child_reg[PCI_FUNC(child->devfn)] = reg16;
  184. if (same_clock)
  185. reg16 |= PCI_EXP_LNKCTL_CCC;
  186. else
  187. reg16 &= ~PCI_EXP_LNKCTL_CCC;
  188. pcie_capability_write_word(child, PCI_EXP_LNKCTL, reg16);
  189. }
  190. /* Configure upstream component */
  191. pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &reg16);
  192. parent_reg = reg16;
  193. if (same_clock)
  194. reg16 |= PCI_EXP_LNKCTL_CCC;
  195. else
  196. reg16 &= ~PCI_EXP_LNKCTL_CCC;
  197. pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
  198. /* Retrain link */
  199. reg16 |= PCI_EXP_LNKCTL_RL;
  200. pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
  201. /* Wait for link training end. Break out after waiting for timeout */
  202. start_jiffies = jiffies;
  203. for (;;) {
  204. pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &reg16);
  205. if (!(reg16 & PCI_EXP_LNKSTA_LT))
  206. break;
  207. if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
  208. break;
  209. msleep(1);
  210. }
  211. if (!(reg16 & PCI_EXP_LNKSTA_LT))
  212. return;
  213. /* Training failed. Restore common clock configurations */
  214. dev_err(&parent->dev, "ASPM: Could not configure common clock\n");
  215. list_for_each_entry(child, &linkbus->devices, bus_list)
  216. pcie_capability_write_word(child, PCI_EXP_LNKCTL,
  217. child_reg[PCI_FUNC(child->devfn)]);
  218. pcie_capability_write_word(parent, PCI_EXP_LNKCTL, parent_reg);
  219. }
  220. /* Convert L0s latency encoding to ns */
  221. static u32 calc_l0s_latency(u32 encoding)
  222. {
  223. if (encoding == 0x7)
  224. return (5 * 1000); /* > 4us */
  225. return (64 << encoding);
  226. }
  227. /* Convert L0s acceptable latency encoding to ns */
  228. static u32 calc_l0s_acceptable(u32 encoding)
  229. {
  230. if (encoding == 0x7)
  231. return -1U;
  232. return (64 << encoding);
  233. }
  234. /* Convert L1 latency encoding to ns */
  235. static u32 calc_l1_latency(u32 encoding)
  236. {
  237. if (encoding == 0x7)
  238. return (65 * 1000); /* > 64us */
  239. return (1000 << encoding);
  240. }
  241. /* Convert L1 acceptable latency encoding to ns */
  242. static u32 calc_l1_acceptable(u32 encoding)
  243. {
  244. if (encoding == 0x7)
  245. return -1U;
  246. return (1000 << encoding);
  247. }
  248. struct aspm_register_info {
  249. u32 support:2;
  250. u32 enabled:2;
  251. u32 latency_encoding_l0s;
  252. u32 latency_encoding_l1;
  253. };
  254. static void pcie_get_aspm_reg(struct pci_dev *pdev,
  255. struct aspm_register_info *info)
  256. {
  257. u16 reg16;
  258. u32 reg32;
  259. pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &reg32);
  260. info->support = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
  261. info->latency_encoding_l0s = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
  262. info->latency_encoding_l1 = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
  263. pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &reg16);
  264. info->enabled = reg16 & PCI_EXP_LNKCTL_ASPMC;
  265. }
  266. static void pcie_aspm_check_latency(struct pci_dev *endpoint)
  267. {
  268. u32 latency, l1_switch_latency = 0;
  269. struct aspm_latency *acceptable;
  270. struct pcie_link_state *link;
  271. /* Device not in D0 doesn't need latency check */
  272. if ((endpoint->current_state != PCI_D0) &&
  273. (endpoint->current_state != PCI_UNKNOWN))
  274. return;
  275. link = endpoint->bus->self->link_state;
  276. acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
  277. while (link) {
  278. /* Check upstream direction L0s latency */
  279. if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
  280. (link->latency_up.l0s > acceptable->l0s))
  281. link->aspm_capable &= ~ASPM_STATE_L0S_UP;
  282. /* Check downstream direction L0s latency */
  283. if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
  284. (link->latency_dw.l0s > acceptable->l0s))
  285. link->aspm_capable &= ~ASPM_STATE_L0S_DW;
  286. /*
  287. * Check L1 latency.
  288. * Every switch on the path to root complex need 1
  289. * more microsecond for L1. Spec doesn't mention L0s.
  290. */
  291. latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1);
  292. if ((link->aspm_capable & ASPM_STATE_L1) &&
  293. (latency + l1_switch_latency > acceptable->l1))
  294. link->aspm_capable &= ~ASPM_STATE_L1;
  295. l1_switch_latency += 1000;
  296. link = link->parent;
  297. }
  298. }
  299. static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
  300. {
  301. struct pci_dev *child, *parent = link->pdev;
  302. struct pci_bus *linkbus = parent->subordinate;
  303. struct aspm_register_info upreg, dwreg;
  304. if (blacklist) {
  305. /* Set enabled/disable so that we will disable ASPM later */
  306. link->aspm_enabled = ASPM_STATE_ALL;
  307. link->aspm_disable = ASPM_STATE_ALL;
  308. return;
  309. }
  310. /* Get upstream/downstream components' register state */
  311. pcie_get_aspm_reg(parent, &upreg);
  312. child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
  313. pcie_get_aspm_reg(child, &dwreg);
  314. /*
  315. * If ASPM not supported, don't mess with the clocks and link,
  316. * bail out now.
  317. */
  318. if (!(upreg.support & dwreg.support))
  319. return;
  320. /* Configure common clock before checking latencies */
  321. pcie_aspm_configure_common_clock(link);
  322. /*
  323. * Re-read upstream/downstream components' register state
  324. * after clock configuration
  325. */
  326. pcie_get_aspm_reg(parent, &upreg);
  327. pcie_get_aspm_reg(child, &dwreg);
  328. /*
  329. * Setup L0s state
  330. *
  331. * Note that we must not enable L0s in either direction on a
  332. * given link unless components on both sides of the link each
  333. * support L0s.
  334. */
  335. if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S)
  336. link->aspm_support |= ASPM_STATE_L0S;
  337. if (dwreg.enabled & PCIE_LINK_STATE_L0S)
  338. link->aspm_enabled |= ASPM_STATE_L0S_UP;
  339. if (upreg.enabled & PCIE_LINK_STATE_L0S)
  340. link->aspm_enabled |= ASPM_STATE_L0S_DW;
  341. link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s);
  342. link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s);
  343. /* Setup L1 state */
  344. if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1)
  345. link->aspm_support |= ASPM_STATE_L1;
  346. if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1)
  347. link->aspm_enabled |= ASPM_STATE_L1;
  348. link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1);
  349. link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1);
  350. /* Save default state */
  351. link->aspm_default = link->aspm_enabled;
  352. /* Setup initial capable state. Will be updated later */
  353. link->aspm_capable = link->aspm_support;
  354. /*
  355. * If the downstream component has pci bridge function, don't
  356. * do ASPM for now.
  357. */
  358. list_for_each_entry(child, &linkbus->devices, bus_list) {
  359. if (pci_pcie_type(child) == PCI_EXP_TYPE_PCI_BRIDGE) {
  360. link->aspm_disable = ASPM_STATE_ALL;
  361. break;
  362. }
  363. }
  364. /* Get and check endpoint acceptable latencies */
  365. list_for_each_entry(child, &linkbus->devices, bus_list) {
  366. u32 reg32, encoding;
  367. struct aspm_latency *acceptable =
  368. &link->acceptable[PCI_FUNC(child->devfn)];
  369. if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT &&
  370. pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)
  371. continue;
  372. pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
  373. /* Calculate endpoint L0s acceptable latency */
  374. encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
  375. acceptable->l0s = calc_l0s_acceptable(encoding);
  376. /* Calculate endpoint L1 acceptable latency */
  377. encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
  378. acceptable->l1 = calc_l1_acceptable(encoding);
  379. pcie_aspm_check_latency(child);
  380. }
  381. }
  382. static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
  383. {
  384. pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
  385. PCI_EXP_LNKCTL_ASPMC, val);
  386. }
  387. static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
  388. {
  389. u32 upstream = 0, dwstream = 0;
  390. struct pci_dev *child, *parent = link->pdev;
  391. struct pci_bus *linkbus = parent->subordinate;
  392. /* Nothing to do if the link is already in the requested state */
  393. state &= (link->aspm_capable & ~link->aspm_disable);
  394. if (link->aspm_enabled == state)
  395. return;
  396. /* Convert ASPM state to upstream/downstream ASPM register state */
  397. if (state & ASPM_STATE_L0S_UP)
  398. dwstream |= PCI_EXP_LNKCTL_ASPM_L0S;
  399. if (state & ASPM_STATE_L0S_DW)
  400. upstream |= PCI_EXP_LNKCTL_ASPM_L0S;
  401. if (state & ASPM_STATE_L1) {
  402. upstream |= PCI_EXP_LNKCTL_ASPM_L1;
  403. dwstream |= PCI_EXP_LNKCTL_ASPM_L1;
  404. }
  405. /*
  406. * Spec 2.0 suggests all functions should be configured the
  407. * same setting for ASPM. Enabling ASPM L1 should be done in
  408. * upstream component first and then downstream, and vice
  409. * versa for disabling ASPM L1. Spec doesn't mention L0S.
  410. */
  411. if (state & ASPM_STATE_L1)
  412. pcie_config_aspm_dev(parent, upstream);
  413. list_for_each_entry(child, &linkbus->devices, bus_list)
  414. pcie_config_aspm_dev(child, dwstream);
  415. if (!(state & ASPM_STATE_L1))
  416. pcie_config_aspm_dev(parent, upstream);
  417. link->aspm_enabled = state;
  418. }
  419. static void pcie_config_aspm_path(struct pcie_link_state *link)
  420. {
  421. while (link) {
  422. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  423. link = link->parent;
  424. }
  425. }
  426. static void free_link_state(struct pcie_link_state *link)
  427. {
  428. link->pdev->link_state = NULL;
  429. kfree(link);
  430. }
  431. static int pcie_aspm_sanity_check(struct pci_dev *pdev)
  432. {
  433. struct pci_dev *child;
  434. u32 reg32;
  435. /*
  436. * Some functions in a slot might not all be PCIe functions,
  437. * very strange. Disable ASPM for the whole slot
  438. */
  439. list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
  440. if (!pci_is_pcie(child))
  441. return -EINVAL;
  442. /*
  443. * If ASPM is disabled then we're not going to change
  444. * the BIOS state. It's safe to continue even if it's a
  445. * pre-1.1 device
  446. */
  447. if (aspm_disabled)
  448. continue;
  449. /*
  450. * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
  451. * RBER bit to determine if a function is 1.1 version device
  452. */
  453. pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
  454. if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
  455. dev_info(&child->dev, "disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force'\n");
  456. return -EINVAL;
  457. }
  458. }
  459. return 0;
  460. }
  461. static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
  462. {
  463. struct pcie_link_state *link;
  464. link = kzalloc(sizeof(*link), GFP_KERNEL);
  465. if (!link)
  466. return NULL;
  467. INIT_LIST_HEAD(&link->sibling);
  468. INIT_LIST_HEAD(&link->children);
  469. INIT_LIST_HEAD(&link->link);
  470. link->pdev = pdev;
  471. if (pci_pcie_type(pdev) != PCI_EXP_TYPE_ROOT_PORT) {
  472. struct pcie_link_state *parent;
  473. parent = pdev->bus->parent->self->link_state;
  474. if (!parent) {
  475. kfree(link);
  476. return NULL;
  477. }
  478. link->parent = parent;
  479. list_add(&link->link, &parent->children);
  480. }
  481. /* Setup a pointer to the root port link */
  482. if (!link->parent)
  483. link->root = link;
  484. else
  485. link->root = link->parent->root;
  486. list_add(&link->sibling, &link_list);
  487. pdev->link_state = link;
  488. return link;
  489. }
  490. /*
  491. * pcie_aspm_init_link_state: Initiate PCI express link state.
  492. * It is called after the pcie and its children devices are scanned.
  493. * @pdev: the root port or switch downstream port
  494. */
  495. void pcie_aspm_init_link_state(struct pci_dev *pdev)
  496. {
  497. struct pcie_link_state *link;
  498. int blacklist = !!pcie_aspm_sanity_check(pdev);
  499. if (!aspm_support_enabled)
  500. return;
  501. if (pdev->link_state)
  502. return;
  503. /*
  504. * We allocate pcie_link_state for the component on the upstream
  505. * end of a Link, so there's nothing to do unless this device has a
  506. * Link on its secondary side.
  507. */
  508. if (!pdev->has_secondary_link)
  509. return;
  510. /* VIA has a strange chipset, root port is under a bridge */
  511. if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT &&
  512. pdev->bus->self)
  513. return;
  514. down_read(&pci_bus_sem);
  515. if (list_empty(&pdev->subordinate->devices))
  516. goto out;
  517. mutex_lock(&aspm_lock);
  518. link = alloc_pcie_link_state(pdev);
  519. if (!link)
  520. goto unlock;
  521. /*
  522. * Setup initial ASPM state. Note that we need to configure
  523. * upstream links also because capable state of them can be
  524. * update through pcie_aspm_cap_init().
  525. */
  526. pcie_aspm_cap_init(link, blacklist);
  527. /* Setup initial Clock PM state */
  528. pcie_clkpm_cap_init(link, blacklist);
  529. /*
  530. * At this stage drivers haven't had an opportunity to change the
  531. * link policy setting. Enabling ASPM on broken hardware can cripple
  532. * it even before the driver has had a chance to disable ASPM, so
  533. * default to a safe level right now. If we're enabling ASPM beyond
  534. * the BIOS's expectation, we'll do so once pci_enable_device() is
  535. * called.
  536. */
  537. if (aspm_policy != POLICY_POWERSAVE) {
  538. pcie_config_aspm_path(link);
  539. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  540. }
  541. unlock:
  542. mutex_unlock(&aspm_lock);
  543. out:
  544. up_read(&pci_bus_sem);
  545. }
  546. /* Recheck latencies and update aspm_capable for links under the root */
  547. static void pcie_update_aspm_capable(struct pcie_link_state *root)
  548. {
  549. struct pcie_link_state *link;
  550. BUG_ON(root->parent);
  551. list_for_each_entry(link, &link_list, sibling) {
  552. if (link->root != root)
  553. continue;
  554. link->aspm_capable = link->aspm_support;
  555. }
  556. list_for_each_entry(link, &link_list, sibling) {
  557. struct pci_dev *child;
  558. struct pci_bus *linkbus = link->pdev->subordinate;
  559. if (link->root != root)
  560. continue;
  561. list_for_each_entry(child, &linkbus->devices, bus_list) {
  562. if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) &&
  563. (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END))
  564. continue;
  565. pcie_aspm_check_latency(child);
  566. }
  567. }
  568. }
  569. /* @pdev: the endpoint device */
  570. void pcie_aspm_exit_link_state(struct pci_dev *pdev)
  571. {
  572. struct pci_dev *parent = pdev->bus->self;
  573. struct pcie_link_state *link, *root, *parent_link;
  574. if (!parent || !parent->link_state)
  575. return;
  576. down_read(&pci_bus_sem);
  577. mutex_lock(&aspm_lock);
  578. /*
  579. * All PCIe functions are in one slot, remove one function will remove
  580. * the whole slot, so just wait until we are the last function left.
  581. */
  582. if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
  583. goto out;
  584. link = parent->link_state;
  585. root = link->root;
  586. parent_link = link->parent;
  587. /* All functions are removed, so just disable ASPM for the link */
  588. pcie_config_aspm_link(link, 0);
  589. list_del(&link->sibling);
  590. list_del(&link->link);
  591. /* Clock PM is for endpoint device */
  592. free_link_state(link);
  593. /* Recheck latencies and configure upstream links */
  594. if (parent_link) {
  595. pcie_update_aspm_capable(root);
  596. pcie_config_aspm_path(parent_link);
  597. }
  598. out:
  599. mutex_unlock(&aspm_lock);
  600. up_read(&pci_bus_sem);
  601. }
  602. /* @pdev: the root port or switch downstream port */
  603. void pcie_aspm_pm_state_change(struct pci_dev *pdev)
  604. {
  605. struct pcie_link_state *link = pdev->link_state;
  606. if (aspm_disabled || !link)
  607. return;
  608. /*
  609. * Devices changed PM state, we should recheck if latency
  610. * meets all functions' requirement
  611. */
  612. down_read(&pci_bus_sem);
  613. mutex_lock(&aspm_lock);
  614. pcie_update_aspm_capable(link->root);
  615. pcie_config_aspm_path(link);
  616. mutex_unlock(&aspm_lock);
  617. up_read(&pci_bus_sem);
  618. }
  619. void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
  620. {
  621. struct pcie_link_state *link = pdev->link_state;
  622. if (aspm_disabled || !link)
  623. return;
  624. if (aspm_policy != POLICY_POWERSAVE)
  625. return;
  626. down_read(&pci_bus_sem);
  627. mutex_lock(&aspm_lock);
  628. pcie_config_aspm_path(link);
  629. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  630. mutex_unlock(&aspm_lock);
  631. up_read(&pci_bus_sem);
  632. }
  633. static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
  634. {
  635. struct pci_dev *parent = pdev->bus->self;
  636. struct pcie_link_state *link;
  637. if (!pci_is_pcie(pdev))
  638. return;
  639. if (pdev->has_secondary_link)
  640. parent = pdev;
  641. if (!parent || !parent->link_state)
  642. return;
  643. /*
  644. * A driver requested that ASPM be disabled on this device, but
  645. * if we don't have permission to manage ASPM (e.g., on ACPI
  646. * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
  647. * the _OSC method), we can't honor that request. Windows has
  648. * a similar mechanism using "PciASPMOptOut", which is also
  649. * ignored in this situation.
  650. */
  651. if (aspm_disabled) {
  652. dev_warn(&pdev->dev, "can't disable ASPM; OS doesn't have ASPM control\n");
  653. return;
  654. }
  655. if (sem)
  656. down_read(&pci_bus_sem);
  657. mutex_lock(&aspm_lock);
  658. link = parent->link_state;
  659. if (state & PCIE_LINK_STATE_L0S)
  660. link->aspm_disable |= ASPM_STATE_L0S;
  661. if (state & PCIE_LINK_STATE_L1)
  662. link->aspm_disable |= ASPM_STATE_L1;
  663. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  664. if (state & PCIE_LINK_STATE_CLKPM) {
  665. link->clkpm_capable = 0;
  666. pcie_set_clkpm(link, 0);
  667. }
  668. mutex_unlock(&aspm_lock);
  669. if (sem)
  670. up_read(&pci_bus_sem);
  671. }
  672. void pci_disable_link_state_locked(struct pci_dev *pdev, int state)
  673. {
  674. __pci_disable_link_state(pdev, state, false);
  675. }
  676. EXPORT_SYMBOL(pci_disable_link_state_locked);
  677. /**
  678. * pci_disable_link_state - Disable device's link state, so the link will
  679. * never enter specific states. Note that if the BIOS didn't grant ASPM
  680. * control to the OS, this does nothing because we can't touch the LNKCTL
  681. * register.
  682. *
  683. * @pdev: PCI device
  684. * @state: ASPM link state to disable
  685. */
  686. void pci_disable_link_state(struct pci_dev *pdev, int state)
  687. {
  688. __pci_disable_link_state(pdev, state, true);
  689. }
  690. EXPORT_SYMBOL(pci_disable_link_state);
  691. static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
  692. {
  693. int i;
  694. struct pcie_link_state *link;
  695. if (aspm_disabled)
  696. return -EPERM;
  697. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  698. if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
  699. break;
  700. if (i >= ARRAY_SIZE(policy_str))
  701. return -EINVAL;
  702. if (i == aspm_policy)
  703. return 0;
  704. down_read(&pci_bus_sem);
  705. mutex_lock(&aspm_lock);
  706. aspm_policy = i;
  707. list_for_each_entry(link, &link_list, sibling) {
  708. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  709. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  710. }
  711. mutex_unlock(&aspm_lock);
  712. up_read(&pci_bus_sem);
  713. return 0;
  714. }
  715. static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
  716. {
  717. int i, cnt = 0;
  718. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  719. if (i == aspm_policy)
  720. cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
  721. else
  722. cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
  723. return cnt;
  724. }
  725. module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
  726. NULL, 0644);
  727. #ifdef CONFIG_PCIEASPM_DEBUG
  728. static ssize_t link_state_show(struct device *dev,
  729. struct device_attribute *attr,
  730. char *buf)
  731. {
  732. struct pci_dev *pci_device = to_pci_dev(dev);
  733. struct pcie_link_state *link_state = pci_device->link_state;
  734. return sprintf(buf, "%d\n", link_state->aspm_enabled);
  735. }
  736. static ssize_t link_state_store(struct device *dev,
  737. struct device_attribute *attr,
  738. const char *buf,
  739. size_t n)
  740. {
  741. struct pci_dev *pdev = to_pci_dev(dev);
  742. struct pcie_link_state *link, *root = pdev->link_state->root;
  743. u32 state;
  744. if (aspm_disabled)
  745. return -EPERM;
  746. if (kstrtouint(buf, 10, &state))
  747. return -EINVAL;
  748. if ((state & ~ASPM_STATE_ALL) != 0)
  749. return -EINVAL;
  750. down_read(&pci_bus_sem);
  751. mutex_lock(&aspm_lock);
  752. list_for_each_entry(link, &link_list, sibling) {
  753. if (link->root != root)
  754. continue;
  755. pcie_config_aspm_link(link, state);
  756. }
  757. mutex_unlock(&aspm_lock);
  758. up_read(&pci_bus_sem);
  759. return n;
  760. }
  761. static ssize_t clk_ctl_show(struct device *dev,
  762. struct device_attribute *attr,
  763. char *buf)
  764. {
  765. struct pci_dev *pci_device = to_pci_dev(dev);
  766. struct pcie_link_state *link_state = pci_device->link_state;
  767. return sprintf(buf, "%d\n", link_state->clkpm_enabled);
  768. }
  769. static ssize_t clk_ctl_store(struct device *dev,
  770. struct device_attribute *attr,
  771. const char *buf,
  772. size_t n)
  773. {
  774. struct pci_dev *pdev = to_pci_dev(dev);
  775. bool state;
  776. if (strtobool(buf, &state))
  777. return -EINVAL;
  778. down_read(&pci_bus_sem);
  779. mutex_lock(&aspm_lock);
  780. pcie_set_clkpm_nocheck(pdev->link_state, state);
  781. mutex_unlock(&aspm_lock);
  782. up_read(&pci_bus_sem);
  783. return n;
  784. }
  785. static DEVICE_ATTR_RW(link_state);
  786. static DEVICE_ATTR_RW(clk_ctl);
  787. static char power_group[] = "power";
  788. void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
  789. {
  790. struct pcie_link_state *link_state = pdev->link_state;
  791. if (!link_state)
  792. return;
  793. if (link_state->aspm_support)
  794. sysfs_add_file_to_group(&pdev->dev.kobj,
  795. &dev_attr_link_state.attr, power_group);
  796. if (link_state->clkpm_capable)
  797. sysfs_add_file_to_group(&pdev->dev.kobj,
  798. &dev_attr_clk_ctl.attr, power_group);
  799. }
  800. void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
  801. {
  802. struct pcie_link_state *link_state = pdev->link_state;
  803. if (!link_state)
  804. return;
  805. if (link_state->aspm_support)
  806. sysfs_remove_file_from_group(&pdev->dev.kobj,
  807. &dev_attr_link_state.attr, power_group);
  808. if (link_state->clkpm_capable)
  809. sysfs_remove_file_from_group(&pdev->dev.kobj,
  810. &dev_attr_clk_ctl.attr, power_group);
  811. }
  812. #endif
  813. static int __init pcie_aspm_disable(char *str)
  814. {
  815. if (!strcmp(str, "off")) {
  816. aspm_policy = POLICY_DEFAULT;
  817. aspm_disabled = 1;
  818. aspm_support_enabled = false;
  819. printk(KERN_INFO "PCIe ASPM is disabled\n");
  820. } else if (!strcmp(str, "force")) {
  821. aspm_force = 1;
  822. printk(KERN_INFO "PCIe ASPM is forcibly enabled\n");
  823. }
  824. return 1;
  825. }
  826. __setup("pcie_aspm=", pcie_aspm_disable);
  827. void pcie_no_aspm(void)
  828. {
  829. /*
  830. * Disabling ASPM is intended to prevent the kernel from modifying
  831. * existing hardware state, not to clear existing state. To that end:
  832. * (a) set policy to POLICY_DEFAULT in order to avoid changing state
  833. * (b) prevent userspace from changing policy
  834. */
  835. if (!aspm_force) {
  836. aspm_policy = POLICY_DEFAULT;
  837. aspm_disabled = 1;
  838. }
  839. }
  840. bool pcie_aspm_support_enabled(void)
  841. {
  842. return aspm_support_enabled;
  843. }
  844. EXPORT_SYMBOL(pcie_aspm_support_enabled);