aspm.c 26 KB

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