aspm.c 27 KB

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