aspm.c 25 KB

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