aspm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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. /* Save default state */
  310. link->aspm_default = link->aspm_enabled;
  311. if (!link->aspm_support)
  312. return;
  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. /*
  407. * If the downstream component has pci bridge function, don't
  408. * do ASPM now.
  409. */
  410. list_for_each_entry(child, &linkbus->devices, bus_list) {
  411. if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
  412. return;
  413. }
  414. /*
  415. * Spec 2.0 suggests all functions should be configured the
  416. * same setting for ASPM. Enabling ASPM L1 should be done in
  417. * upstream component first and then downstream, and vice
  418. * versa for disabling ASPM L1. Spec doesn't mention L0S.
  419. */
  420. if (state & PCIE_LINK_STATE_L1)
  421. __pcie_aspm_config_one_dev(parent, state);
  422. list_for_each_entry(child, &linkbus->devices, bus_list)
  423. __pcie_aspm_config_one_dev(child, state);
  424. if (!(state & PCIE_LINK_STATE_L1))
  425. __pcie_aspm_config_one_dev(parent, state);
  426. link->aspm_enabled = state;
  427. }
  428. /* Check the whole hierarchy, and configure each link in the hierarchy */
  429. static void __pcie_aspm_configure_link_state(struct pcie_link_state *link,
  430. u32 state)
  431. {
  432. struct pcie_link_state *leaf, *root = link->root;
  433. state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
  434. /* Check all links who have specific root port link */
  435. list_for_each_entry(leaf, &link_list, sibling) {
  436. if (!list_empty(&leaf->children) || (leaf->root != root))
  437. continue;
  438. state = pcie_aspm_check_state(leaf, state);
  439. }
  440. /* Check root port link too in case it hasn't children */
  441. state = pcie_aspm_check_state(root, state);
  442. if (link->aspm_enabled == state)
  443. return;
  444. /*
  445. * We must change the hierarchy. See comments in
  446. * __pcie_aspm_config_link for the order
  447. **/
  448. if (state & PCIE_LINK_STATE_L1) {
  449. list_for_each_entry(leaf, &link_list, sibling) {
  450. if (leaf->root == root)
  451. __pcie_aspm_config_link(leaf, state);
  452. }
  453. } else {
  454. list_for_each_entry_reverse(leaf, &link_list, sibling) {
  455. if (leaf->root == root)
  456. __pcie_aspm_config_link(leaf, state);
  457. }
  458. }
  459. }
  460. /*
  461. * pcie_aspm_configure_link_state: enable/disable PCI express link state
  462. * @pdev: the root port or switch downstream port
  463. */
  464. static void pcie_aspm_configure_link_state(struct pcie_link_state *link,
  465. u32 state)
  466. {
  467. down_read(&pci_bus_sem);
  468. mutex_lock(&aspm_lock);
  469. __pcie_aspm_configure_link_state(link, state);
  470. mutex_unlock(&aspm_lock);
  471. up_read(&pci_bus_sem);
  472. }
  473. static void free_link_state(struct pcie_link_state *link)
  474. {
  475. link->pdev->link_state = NULL;
  476. kfree(link);
  477. }
  478. static int pcie_aspm_sanity_check(struct pci_dev *pdev)
  479. {
  480. struct pci_dev *child;
  481. int pos;
  482. u32 reg32;
  483. /*
  484. * Some functions in a slot might not all be PCIE functions,
  485. * very strange. Disable ASPM for the whole slot
  486. */
  487. list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
  488. pos = pci_find_capability(child, PCI_CAP_ID_EXP);
  489. if (!pos)
  490. return -EINVAL;
  491. /*
  492. * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
  493. * RBER bit to determine if a function is 1.1 version device
  494. */
  495. pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
  496. if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
  497. dev_printk(KERN_INFO, &child->dev, "disabling ASPM"
  498. " on pre-1.1 PCIe device. You can enable it"
  499. " with 'pcie_aspm=force'\n");
  500. return -EINVAL;
  501. }
  502. }
  503. return 0;
  504. }
  505. static struct pcie_link_state *pcie_aspm_setup_link_state(struct pci_dev *pdev)
  506. {
  507. struct pcie_link_state *link;
  508. int blacklist = !!pcie_aspm_sanity_check(pdev);
  509. link = kzalloc(sizeof(*link), GFP_KERNEL);
  510. if (!link)
  511. return NULL;
  512. INIT_LIST_HEAD(&link->sibling);
  513. INIT_LIST_HEAD(&link->children);
  514. INIT_LIST_HEAD(&link->link);
  515. link->pdev = pdev;
  516. if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
  517. struct pcie_link_state *parent;
  518. parent = pdev->bus->parent->self->link_state;
  519. if (!parent) {
  520. kfree(link);
  521. return NULL;
  522. }
  523. link->parent = parent;
  524. list_add(&link->link, &parent->children);
  525. }
  526. /* Setup a pointer to the root port link */
  527. if (!link->parent)
  528. link->root = link;
  529. else
  530. link->root = link->parent->root;
  531. list_add(&link->sibling, &link_list);
  532. pdev->link_state = link;
  533. /* Check ASPM capability */
  534. pcie_aspm_cap_init(link, blacklist);
  535. /* Check Clock PM capability */
  536. pcie_clkpm_cap_init(link, blacklist);
  537. return link;
  538. }
  539. /*
  540. * pcie_aspm_init_link_state: Initiate PCI express link state.
  541. * It is called after the pcie and its children devices are scaned.
  542. * @pdev: the root port or switch downstream port
  543. */
  544. void pcie_aspm_init_link_state(struct pci_dev *pdev)
  545. {
  546. u32 state;
  547. struct pcie_link_state *link;
  548. if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
  549. return;
  550. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  551. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  552. return;
  553. /* VIA has a strange chipset, root port is under a bridge */
  554. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
  555. pdev->bus->self)
  556. return;
  557. down_read(&pci_bus_sem);
  558. if (list_empty(&pdev->subordinate->devices))
  559. goto out;
  560. mutex_lock(&aspm_lock);
  561. link = pcie_aspm_setup_link_state(pdev);
  562. if (!link)
  563. goto unlock;
  564. /*
  565. * Setup initial ASPM state
  566. *
  567. * If link has switch, delay the link config. The leaf link
  568. * initialization will config the whole hierarchy. But we must
  569. * make sure BIOS doesn't set unsupported link state.
  570. */
  571. if (pcie_aspm_downstream_has_switch(link)) {
  572. state = pcie_aspm_check_state(link, link->aspm_default);
  573. __pcie_aspm_config_link(link, state);
  574. } else {
  575. state = policy_to_aspm_state(link);
  576. __pcie_aspm_configure_link_state(link, state);
  577. }
  578. /* Setup initial Clock PM state */
  579. state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
  580. pcie_set_clkpm(link, state);
  581. unlock:
  582. mutex_unlock(&aspm_lock);
  583. out:
  584. up_read(&pci_bus_sem);
  585. }
  586. /* @pdev: the endpoint device */
  587. void pcie_aspm_exit_link_state(struct pci_dev *pdev)
  588. {
  589. struct pci_dev *parent = pdev->bus->self;
  590. struct pcie_link_state *link_state = parent->link_state;
  591. if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
  592. return;
  593. if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  594. parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  595. return;
  596. down_read(&pci_bus_sem);
  597. mutex_lock(&aspm_lock);
  598. /*
  599. * All PCIe functions are in one slot, remove one function will remove
  600. * the whole slot, so just wait until we are the last function left.
  601. */
  602. if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
  603. goto out;
  604. /* All functions are removed, so just disable ASPM for the link */
  605. __pcie_aspm_config_one_dev(parent, 0);
  606. list_del(&link_state->sibling);
  607. list_del(&link_state->link);
  608. /* Clock PM is for endpoint device */
  609. free_link_state(link_state);
  610. out:
  611. mutex_unlock(&aspm_lock);
  612. up_read(&pci_bus_sem);
  613. }
  614. /* @pdev: the root port or switch downstream port */
  615. void pcie_aspm_pm_state_change(struct pci_dev *pdev)
  616. {
  617. struct pcie_link_state *link_state = pdev->link_state;
  618. if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
  619. return;
  620. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  621. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  622. return;
  623. /*
  624. * devices changed PM state, we should recheck if latency meets all
  625. * functions' requirement
  626. */
  627. pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
  628. }
  629. /*
  630. * pci_disable_link_state - disable pci device's link state, so the link will
  631. * never enter specific states
  632. */
  633. void pci_disable_link_state(struct pci_dev *pdev, int state)
  634. {
  635. struct pci_dev *parent = pdev->bus->self;
  636. struct pcie_link_state *link_state;
  637. if (aspm_disabled || !pdev->is_pcie)
  638. return;
  639. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
  640. pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
  641. parent = pdev;
  642. if (!parent || !parent->link_state)
  643. return;
  644. down_read(&pci_bus_sem);
  645. mutex_lock(&aspm_lock);
  646. link_state = parent->link_state;
  647. link_state->aspm_support &= ~state;
  648. __pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
  649. if (state & PCIE_LINK_STATE_CLKPM) {
  650. link_state->clkpm_capable = 0;
  651. pcie_set_clkpm(link_state, 0);
  652. }
  653. mutex_unlock(&aspm_lock);
  654. up_read(&pci_bus_sem);
  655. }
  656. EXPORT_SYMBOL(pci_disable_link_state);
  657. static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
  658. {
  659. int i;
  660. struct pcie_link_state *link_state;
  661. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  662. if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
  663. break;
  664. if (i >= ARRAY_SIZE(policy_str))
  665. return -EINVAL;
  666. if (i == aspm_policy)
  667. return 0;
  668. down_read(&pci_bus_sem);
  669. mutex_lock(&aspm_lock);
  670. aspm_policy = i;
  671. list_for_each_entry(link_state, &link_list, sibling) {
  672. __pcie_aspm_configure_link_state(link_state,
  673. policy_to_aspm_state(link_state));
  674. pcie_set_clkpm(link_state, policy_to_clkpm_state(link_state));
  675. }
  676. mutex_unlock(&aspm_lock);
  677. up_read(&pci_bus_sem);
  678. return 0;
  679. }
  680. static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
  681. {
  682. int i, cnt = 0;
  683. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  684. if (i == aspm_policy)
  685. cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
  686. else
  687. cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
  688. return cnt;
  689. }
  690. module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
  691. NULL, 0644);
  692. #ifdef CONFIG_PCIEASPM_DEBUG
  693. static ssize_t link_state_show(struct device *dev,
  694. struct device_attribute *attr,
  695. char *buf)
  696. {
  697. struct pci_dev *pci_device = to_pci_dev(dev);
  698. struct pcie_link_state *link_state = pci_device->link_state;
  699. return sprintf(buf, "%d\n", link_state->aspm_enabled);
  700. }
  701. static ssize_t link_state_store(struct device *dev,
  702. struct device_attribute *attr,
  703. const char *buf,
  704. size_t n)
  705. {
  706. struct pci_dev *pdev = to_pci_dev(dev);
  707. int state;
  708. if (n < 1)
  709. return -EINVAL;
  710. state = buf[0]-'0';
  711. if (state >= 0 && state <= 3) {
  712. /* setup link aspm state */
  713. pcie_aspm_configure_link_state(pdev->link_state, state);
  714. return n;
  715. }
  716. return -EINVAL;
  717. }
  718. static ssize_t clk_ctl_show(struct device *dev,
  719. struct device_attribute *attr,
  720. char *buf)
  721. {
  722. struct pci_dev *pci_device = to_pci_dev(dev);
  723. struct pcie_link_state *link_state = pci_device->link_state;
  724. return sprintf(buf, "%d\n", link_state->clkpm_enabled);
  725. }
  726. static ssize_t clk_ctl_store(struct device *dev,
  727. struct device_attribute *attr,
  728. const char *buf,
  729. size_t n)
  730. {
  731. struct pci_dev *pdev = to_pci_dev(dev);
  732. int state;
  733. if (n < 1)
  734. return -EINVAL;
  735. state = buf[0]-'0';
  736. down_read(&pci_bus_sem);
  737. mutex_lock(&aspm_lock);
  738. pcie_set_clkpm_nocheck(pdev->link_state, !!state);
  739. mutex_unlock(&aspm_lock);
  740. up_read(&pci_bus_sem);
  741. return n;
  742. }
  743. static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
  744. static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
  745. static char power_group[] = "power";
  746. void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
  747. {
  748. struct pcie_link_state *link_state = pdev->link_state;
  749. if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  750. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  751. return;
  752. if (link_state->aspm_support)
  753. sysfs_add_file_to_group(&pdev->dev.kobj,
  754. &dev_attr_link_state.attr, power_group);
  755. if (link_state->clkpm_capable)
  756. sysfs_add_file_to_group(&pdev->dev.kobj,
  757. &dev_attr_clk_ctl.attr, power_group);
  758. }
  759. void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
  760. {
  761. struct pcie_link_state *link_state = pdev->link_state;
  762. if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  763. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  764. return;
  765. if (link_state->aspm_support)
  766. sysfs_remove_file_from_group(&pdev->dev.kobj,
  767. &dev_attr_link_state.attr, power_group);
  768. if (link_state->clkpm_capable)
  769. sysfs_remove_file_from_group(&pdev->dev.kobj,
  770. &dev_attr_clk_ctl.attr, power_group);
  771. }
  772. #endif
  773. static int __init pcie_aspm_disable(char *str)
  774. {
  775. if (!strcmp(str, "off")) {
  776. aspm_disabled = 1;
  777. printk(KERN_INFO "PCIe ASPM is disabled\n");
  778. } else if (!strcmp(str, "force")) {
  779. aspm_force = 1;
  780. printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
  781. }
  782. return 1;
  783. }
  784. __setup("pcie_aspm=", pcie_aspm_disable);
  785. void pcie_no_aspm(void)
  786. {
  787. if (!aspm_force)
  788. aspm_disabled = 1;
  789. }
  790. /**
  791. * pcie_aspm_enabled - is PCIe ASPM enabled?
  792. *
  793. * Returns true if ASPM has not been disabled by the command-line option
  794. * pcie_aspm=off.
  795. **/
  796. int pcie_aspm_enabled(void)
  797. {
  798. return !aspm_disabled;
  799. }
  800. EXPORT_SYMBOL(pcie_aspm_enabled);