aspm.c 25 KB

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