aspm.c 25 KB

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