aspm.c 25 KB

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