aspm.c 27 KB

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