aspm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. u32 *l0s, u32 *l1, u32 *enabled)
  263. {
  264. int pos;
  265. u16 reg16;
  266. u32 reg32, latency;
  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. latency = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
  277. *l0s = calc_L0S_latency(latency, 0);
  278. if (*state & PCIE_LINK_STATE_L1) {
  279. latency = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
  280. *l1 = calc_L1_latency(latency, 0);
  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_cap_init(struct pcie_link_state *link, int blacklist)
  286. {
  287. u32 support, l0s, l1, enabled;
  288. struct pci_dev *child, *parent = link->pdev;
  289. struct pci_bus *linkbus = parent->subordinate;
  290. if (blacklist) {
  291. /* Set support state to 0, so we will disable ASPM later */
  292. link->aspm_support = 0;
  293. link->aspm_default = 0;
  294. link->aspm_enabled = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
  295. return;
  296. }
  297. /* Configure common clock before checking latencies */
  298. pcie_aspm_configure_common_clock(link);
  299. /* upstream component states */
  300. pcie_aspm_get_cap_device(parent, &support, &l0s, &l1, &enabled);
  301. link->aspm_support = support;
  302. link->latency.l0s = l0s;
  303. link->latency.l1 = l1;
  304. link->aspm_enabled = enabled;
  305. /* downstream component states, all functions have the same setting */
  306. child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
  307. pcie_aspm_get_cap_device(child, &support, &l0s, &l1, &enabled);
  308. link->aspm_support &= support;
  309. link->latency.l0s = max_t(u32, link->latency.l0s, l0s);
  310. link->latency.l1 = max_t(u32, link->latency.l1, l1);
  311. if (!link->aspm_support)
  312. return;
  313. link->aspm_enabled &= link->aspm_support;
  314. link->aspm_default = link->aspm_enabled;
  315. /* ENDPOINT states*/
  316. list_for_each_entry(child, &linkbus->devices, bus_list) {
  317. int pos;
  318. u32 reg32;
  319. unsigned int latency;
  320. struct aspm_latency *acceptable =
  321. &link->acceptable[PCI_FUNC(child->devfn)];
  322. if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
  323. child->pcie_type != PCI_EXP_TYPE_LEG_END)
  324. continue;
  325. pos = pci_find_capability(child, PCI_CAP_ID_EXP);
  326. pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
  327. latency = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
  328. latency = calc_L0S_latency(latency, 1);
  329. acceptable->l0s = latency;
  330. if (link->aspm_support & PCIE_LINK_STATE_L1) {
  331. latency = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
  332. latency = calc_L1_latency(latency, 1);
  333. acceptable->l1 = latency;
  334. }
  335. }
  336. }
  337. /**
  338. * __pcie_aspm_check_state_one - check latency for endpoint device.
  339. * @endpoint: pointer to the struct pci_dev of endpoint device
  340. *
  341. * TBD: The latency from the endpoint to root complex vary per switch's
  342. * upstream link state above the device. Here we just do a simple check
  343. * which assumes all links above the device can be in L1 state, that
  344. * is we just consider the worst case. If switch's upstream link can't
  345. * be put into L0S/L1, then our check is too strictly.
  346. */
  347. static u32 __pcie_aspm_check_state_one(struct pci_dev *endpoint, u32 state)
  348. {
  349. u32 l1_switch_latency = 0;
  350. struct aspm_latency *acceptable;
  351. struct pcie_link_state *link;
  352. link = endpoint->bus->self->link_state;
  353. state &= link->aspm_support;
  354. acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
  355. while (link && state) {
  356. if ((state & PCIE_LINK_STATE_L0S) &&
  357. (link->latency.l0s > acceptable->l0s))
  358. state &= ~PCIE_LINK_STATE_L0S;
  359. if ((state & PCIE_LINK_STATE_L1) &&
  360. (link->latency.l1 + l1_switch_latency > acceptable->l1))
  361. state &= ~PCIE_LINK_STATE_L1;
  362. link = link->parent;
  363. /*
  364. * Every switch on the path to root complex need 1
  365. * more microsecond for L1. Spec doesn't mention L0s.
  366. */
  367. l1_switch_latency += 1000;
  368. }
  369. return state;
  370. }
  371. static u32 pcie_aspm_check_state(struct pcie_link_state *link, u32 state)
  372. {
  373. pci_power_t power_state;
  374. struct pci_dev *child;
  375. struct pci_bus *linkbus = link->pdev->subordinate;
  376. /* If no child, ignore the link */
  377. if (list_empty(&linkbus->devices))
  378. return state;
  379. list_for_each_entry(child, &linkbus->devices, bus_list) {
  380. /*
  381. * If downstream component of a link is pci bridge, we
  382. * disable ASPM for now for the link
  383. */
  384. if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
  385. return 0;
  386. if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
  387. child->pcie_type != PCI_EXP_TYPE_LEG_END))
  388. continue;
  389. /* Device not in D0 doesn't need check latency */
  390. power_state = child->current_state;
  391. if (power_state == PCI_D1 || power_state == PCI_D2 ||
  392. power_state == PCI_D3hot || power_state == PCI_D3cold)
  393. continue;
  394. state = __pcie_aspm_check_state_one(child, state);
  395. }
  396. return state;
  397. }
  398. static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
  399. {
  400. u16 reg16;
  401. int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  402. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  403. reg16 &= ~0x3;
  404. reg16 |= state;
  405. pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
  406. }
  407. static void __pcie_aspm_config_link(struct pcie_link_state *link, u32 state)
  408. {
  409. struct pci_dev *child, *parent = link->pdev;
  410. struct pci_bus *linkbus = parent->subordinate;
  411. /* If no child, disable the link */
  412. if (list_empty(&linkbus->devices))
  413. state = 0;
  414. /*
  415. * If the downstream component has pci bridge function, don't
  416. * do ASPM now.
  417. */
  418. list_for_each_entry(child, &linkbus->devices, bus_list) {
  419. if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
  420. return;
  421. }
  422. /*
  423. * Spec 2.0 suggests all functions should be configured the
  424. * same setting for ASPM. Enabling ASPM L1 should be done in
  425. * upstream component first and then downstream, and vice
  426. * versa for disabling ASPM L1. Spec doesn't mention L0S.
  427. */
  428. if (state & PCIE_LINK_STATE_L1)
  429. __pcie_aspm_config_one_dev(parent, state);
  430. list_for_each_entry(child, &linkbus->devices, bus_list)
  431. __pcie_aspm_config_one_dev(child, state);
  432. if (!(state & PCIE_LINK_STATE_L1))
  433. __pcie_aspm_config_one_dev(parent, state);
  434. link->aspm_enabled = state;
  435. }
  436. static struct pcie_link_state *get_root_port_link(struct pcie_link_state *link)
  437. {
  438. struct pcie_link_state *root_port_link = link;
  439. while (root_port_link->parent)
  440. root_port_link = root_port_link->parent;
  441. return root_port_link;
  442. }
  443. /* Check the whole hierarchy, and configure each link in the hierarchy */
  444. static void __pcie_aspm_configure_link_state(struct pcie_link_state *link,
  445. u32 state)
  446. {
  447. struct pcie_link_state *leaf, *root = get_root_port_link(link);
  448. state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
  449. /* Check all links who have specific root port link */
  450. list_for_each_entry(leaf, &link_list, sibling) {
  451. if (!list_empty(&leaf->children) ||
  452. get_root_port_link(leaf) != root)
  453. continue;
  454. state = pcie_aspm_check_state(leaf, state);
  455. }
  456. /* Check root port link too in case it hasn't children */
  457. state = pcie_aspm_check_state(root, state);
  458. if (link->aspm_enabled == state)
  459. return;
  460. /*
  461. * We must change the hierarchy. See comments in
  462. * __pcie_aspm_config_link for the order
  463. **/
  464. if (state & PCIE_LINK_STATE_L1) {
  465. list_for_each_entry(leaf, &link_list, sibling) {
  466. if (get_root_port_link(leaf) == root)
  467. __pcie_aspm_config_link(leaf, state);
  468. }
  469. } else {
  470. list_for_each_entry_reverse(leaf, &link_list, sibling) {
  471. if (get_root_port_link(leaf) == root)
  472. __pcie_aspm_config_link(leaf, state);
  473. }
  474. }
  475. }
  476. /*
  477. * pcie_aspm_configure_link_state: enable/disable PCI express link state
  478. * @pdev: the root port or switch downstream port
  479. */
  480. static void pcie_aspm_configure_link_state(struct pcie_link_state *link,
  481. u32 state)
  482. {
  483. down_read(&pci_bus_sem);
  484. mutex_lock(&aspm_lock);
  485. __pcie_aspm_configure_link_state(link, state);
  486. mutex_unlock(&aspm_lock);
  487. up_read(&pci_bus_sem);
  488. }
  489. static void free_link_state(struct pcie_link_state *link)
  490. {
  491. link->pdev->link_state = NULL;
  492. kfree(link);
  493. }
  494. static int pcie_aspm_sanity_check(struct pci_dev *pdev)
  495. {
  496. struct pci_dev *child_dev;
  497. int child_pos;
  498. u32 reg32;
  499. /*
  500. * Some functions in a slot might not all be PCIE functions, very
  501. * strange. Disable ASPM for the whole slot
  502. */
  503. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  504. child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
  505. if (!child_pos)
  506. return -EINVAL;
  507. /*
  508. * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
  509. * RBER bit to determine if a function is 1.1 version device
  510. */
  511. pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP,
  512. &reg32);
  513. if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
  514. dev_printk(KERN_INFO, &child_dev->dev, "disabling ASPM"
  515. " on pre-1.1 PCIe device. You can enable it"
  516. " with 'pcie_aspm=force'\n");
  517. return -EINVAL;
  518. }
  519. }
  520. return 0;
  521. }
  522. static struct pcie_link_state *pcie_aspm_setup_link_state(struct pci_dev *pdev)
  523. {
  524. struct pcie_link_state *link;
  525. int blacklist = !!pcie_aspm_sanity_check(pdev);
  526. link = kzalloc(sizeof(*link), GFP_KERNEL);
  527. if (!link)
  528. return NULL;
  529. INIT_LIST_HEAD(&link->sibling);
  530. INIT_LIST_HEAD(&link->children);
  531. INIT_LIST_HEAD(&link->link);
  532. link->pdev = pdev;
  533. link->has_switch = pcie_aspm_downstream_has_switch(link);
  534. if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
  535. struct pcie_link_state *parent;
  536. parent = pdev->bus->parent->self->link_state;
  537. if (!parent) {
  538. kfree(link);
  539. return NULL;
  540. }
  541. link->parent = parent;
  542. list_add(&link->link, &parent->children);
  543. }
  544. list_add(&link->sibling, &link_list);
  545. pdev->link_state = link;
  546. /* Check ASPM capability */
  547. pcie_aspm_cap_init(link, blacklist);
  548. /* Check Clock PM capability */
  549. pcie_clkpm_cap_init(link, blacklist);
  550. return link;
  551. }
  552. /*
  553. * pcie_aspm_init_link_state: Initiate PCI express link state.
  554. * It is called after the pcie and its children devices are scaned.
  555. * @pdev: the root port or switch downstream port
  556. */
  557. void pcie_aspm_init_link_state(struct pci_dev *pdev)
  558. {
  559. u32 state;
  560. struct pcie_link_state *link;
  561. if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
  562. return;
  563. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  564. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  565. return;
  566. /* VIA has a strange chipset, root port is under a bridge */
  567. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
  568. pdev->bus->self)
  569. return;
  570. down_read(&pci_bus_sem);
  571. if (list_empty(&pdev->subordinate->devices))
  572. goto out;
  573. mutex_lock(&aspm_lock);
  574. link = pcie_aspm_setup_link_state(pdev);
  575. if (!link)
  576. goto unlock;
  577. /*
  578. * Setup initial ASPM state
  579. *
  580. * If link has switch, delay the link config. The leaf link
  581. * initialization will config the whole hierarchy. But we must
  582. * make sure BIOS doesn't set unsupported link state.
  583. */
  584. if (link->has_switch) {
  585. state = pcie_aspm_check_state(link, link->aspm_default);
  586. __pcie_aspm_config_link(link, state);
  587. } else {
  588. state = policy_to_aspm_state(link);
  589. __pcie_aspm_configure_link_state(link, state);
  590. }
  591. /* Setup initial Clock PM state */
  592. state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
  593. pcie_set_clkpm(link, state);
  594. unlock:
  595. mutex_unlock(&aspm_lock);
  596. out:
  597. up_read(&pci_bus_sem);
  598. }
  599. /* @pdev: the endpoint device */
  600. void pcie_aspm_exit_link_state(struct pci_dev *pdev)
  601. {
  602. struct pci_dev *parent = pdev->bus->self;
  603. struct pcie_link_state *link_state = parent->link_state;
  604. if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
  605. return;
  606. if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  607. parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  608. return;
  609. down_read(&pci_bus_sem);
  610. mutex_lock(&aspm_lock);
  611. /*
  612. * All PCIe functions are in one slot, remove one function will remove
  613. * the whole slot, so just wait until we are the last function left.
  614. */
  615. if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
  616. goto out;
  617. /* All functions are removed, so just disable ASPM for the link */
  618. __pcie_aspm_config_one_dev(parent, 0);
  619. list_del(&link_state->sibling);
  620. list_del(&link_state->link);
  621. /* Clock PM is for endpoint device */
  622. free_link_state(link_state);
  623. out:
  624. mutex_unlock(&aspm_lock);
  625. up_read(&pci_bus_sem);
  626. }
  627. /* @pdev: the root port or switch downstream port */
  628. void pcie_aspm_pm_state_change(struct pci_dev *pdev)
  629. {
  630. struct pcie_link_state *link_state = pdev->link_state;
  631. if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
  632. return;
  633. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  634. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  635. return;
  636. /*
  637. * devices changed PM state, we should recheck if latency meets all
  638. * functions' requirement
  639. */
  640. pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
  641. }
  642. /*
  643. * pci_disable_link_state - disable pci device's link state, so the link will
  644. * never enter specific states
  645. */
  646. void pci_disable_link_state(struct pci_dev *pdev, int state)
  647. {
  648. struct pci_dev *parent = pdev->bus->self;
  649. struct pcie_link_state *link_state;
  650. if (aspm_disabled || !pdev->is_pcie)
  651. return;
  652. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
  653. pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
  654. parent = pdev;
  655. if (!parent || !parent->link_state)
  656. return;
  657. down_read(&pci_bus_sem);
  658. mutex_lock(&aspm_lock);
  659. link_state = parent->link_state;
  660. link_state->aspm_support &= ~state;
  661. __pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
  662. if (state & PCIE_LINK_STATE_CLKPM) {
  663. link_state->clkpm_capable = 0;
  664. pcie_set_clkpm(link_state, 0);
  665. }
  666. mutex_unlock(&aspm_lock);
  667. up_read(&pci_bus_sem);
  668. }
  669. EXPORT_SYMBOL(pci_disable_link_state);
  670. static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
  671. {
  672. int i;
  673. struct pcie_link_state *link_state;
  674. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  675. if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
  676. break;
  677. if (i >= ARRAY_SIZE(policy_str))
  678. return -EINVAL;
  679. if (i == aspm_policy)
  680. return 0;
  681. down_read(&pci_bus_sem);
  682. mutex_lock(&aspm_lock);
  683. aspm_policy = i;
  684. list_for_each_entry(link_state, &link_list, sibling) {
  685. __pcie_aspm_configure_link_state(link_state,
  686. policy_to_aspm_state(link_state));
  687. pcie_set_clkpm(link_state, policy_to_clkpm_state(link_state));
  688. }
  689. mutex_unlock(&aspm_lock);
  690. up_read(&pci_bus_sem);
  691. return 0;
  692. }
  693. static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
  694. {
  695. int i, cnt = 0;
  696. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  697. if (i == aspm_policy)
  698. cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
  699. else
  700. cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
  701. return cnt;
  702. }
  703. module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
  704. NULL, 0644);
  705. #ifdef CONFIG_PCIEASPM_DEBUG
  706. static ssize_t link_state_show(struct device *dev,
  707. struct device_attribute *attr,
  708. char *buf)
  709. {
  710. struct pci_dev *pci_device = to_pci_dev(dev);
  711. struct pcie_link_state *link_state = pci_device->link_state;
  712. return sprintf(buf, "%d\n", link_state->aspm_enabled);
  713. }
  714. static ssize_t link_state_store(struct device *dev,
  715. struct device_attribute *attr,
  716. const char *buf,
  717. size_t n)
  718. {
  719. struct pci_dev *pdev = to_pci_dev(dev);
  720. int state;
  721. if (n < 1)
  722. return -EINVAL;
  723. state = buf[0]-'0';
  724. if (state >= 0 && state <= 3) {
  725. /* setup link aspm state */
  726. pcie_aspm_configure_link_state(pdev->link_state, state);
  727. return n;
  728. }
  729. return -EINVAL;
  730. }
  731. static ssize_t clk_ctl_show(struct device *dev,
  732. struct device_attribute *attr,
  733. char *buf)
  734. {
  735. struct pci_dev *pci_device = to_pci_dev(dev);
  736. struct pcie_link_state *link_state = pci_device->link_state;
  737. return sprintf(buf, "%d\n", link_state->clkpm_enabled);
  738. }
  739. static ssize_t clk_ctl_store(struct device *dev,
  740. struct device_attribute *attr,
  741. const char *buf,
  742. size_t n)
  743. {
  744. struct pci_dev *pdev = to_pci_dev(dev);
  745. int state;
  746. if (n < 1)
  747. return -EINVAL;
  748. state = buf[0]-'0';
  749. down_read(&pci_bus_sem);
  750. mutex_lock(&aspm_lock);
  751. pcie_set_clkpm_nocheck(pdev->link_state, !!state);
  752. mutex_unlock(&aspm_lock);
  753. up_read(&pci_bus_sem);
  754. return n;
  755. }
  756. static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
  757. static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
  758. static char power_group[] = "power";
  759. void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
  760. {
  761. struct pcie_link_state *link_state = pdev->link_state;
  762. if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  763. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  764. return;
  765. if (link_state->aspm_support)
  766. sysfs_add_file_to_group(&pdev->dev.kobj,
  767. &dev_attr_link_state.attr, power_group);
  768. if (link_state->clkpm_capable)
  769. sysfs_add_file_to_group(&pdev->dev.kobj,
  770. &dev_attr_clk_ctl.attr, power_group);
  771. }
  772. void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
  773. {
  774. struct pcie_link_state *link_state = pdev->link_state;
  775. if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  776. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  777. return;
  778. if (link_state->aspm_support)
  779. sysfs_remove_file_from_group(&pdev->dev.kobj,
  780. &dev_attr_link_state.attr, power_group);
  781. if (link_state->clkpm_capable)
  782. sysfs_remove_file_from_group(&pdev->dev.kobj,
  783. &dev_attr_clk_ctl.attr, power_group);
  784. }
  785. #endif
  786. static int __init pcie_aspm_disable(char *str)
  787. {
  788. if (!strcmp(str, "off")) {
  789. aspm_disabled = 1;
  790. printk(KERN_INFO "PCIe ASPM is disabled\n");
  791. } else if (!strcmp(str, "force")) {
  792. aspm_force = 1;
  793. printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
  794. }
  795. return 1;
  796. }
  797. __setup("pcie_aspm=", pcie_aspm_disable);
  798. void pcie_no_aspm(void)
  799. {
  800. if (!aspm_force)
  801. aspm_disabled = 1;
  802. }
  803. /**
  804. * pcie_aspm_enabled - is PCIe ASPM enabled?
  805. *
  806. * Returns true if ASPM has not been disabled by the command-line option
  807. * pcie_aspm=off.
  808. **/
  809. int pcie_aspm_enabled(void)
  810. {
  811. return !aspm_disabled;
  812. }
  813. EXPORT_SYMBOL(pcie_aspm_enabled);