aspm.c 27 KB

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