aspm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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_pcie_cap(child);
  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_pcie_cap(child);
  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(!pci_is_pcie(child));
  176. /* Check downstream component if bit Slot Clock Configuration is 1 */
  177. cpos = pci_pcie_cap(child);
  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_pcie_cap(parent);
  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_pcie_cap(child);
  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_pcie_cap(child);
  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_pcie_cap(pdev);
  271. pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
  272. info->support = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
  273. info->latency_encoding_l0s = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
  274. info->latency_encoding_l1 = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
  275. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  276. info->enabled = reg16 & PCI_EXP_LNKCTL_ASPMC;
  277. }
  278. static void pcie_aspm_check_latency(struct pci_dev *endpoint)
  279. {
  280. u32 latency, l1_switch_latency = 0;
  281. struct aspm_latency *acceptable;
  282. struct pcie_link_state *link;
  283. /* Device not in D0 doesn't need latency check */
  284. if ((endpoint->current_state != PCI_D0) &&
  285. (endpoint->current_state != PCI_UNKNOWN))
  286. return;
  287. link = endpoint->bus->self->link_state;
  288. acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
  289. while (link) {
  290. /* Check upstream direction L0s latency */
  291. if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
  292. (link->latency_up.l0s > acceptable->l0s))
  293. link->aspm_capable &= ~ASPM_STATE_L0S_UP;
  294. /* Check downstream direction L0s latency */
  295. if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
  296. (link->latency_dw.l0s > acceptable->l0s))
  297. link->aspm_capable &= ~ASPM_STATE_L0S_DW;
  298. /*
  299. * Check L1 latency.
  300. * Every switch on the path to root complex need 1
  301. * more microsecond for L1. Spec doesn't mention L0s.
  302. */
  303. latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1);
  304. if ((link->aspm_capable & ASPM_STATE_L1) &&
  305. (latency + l1_switch_latency > acceptable->l1))
  306. link->aspm_capable &= ~ASPM_STATE_L1;
  307. l1_switch_latency += 1000;
  308. link = link->parent;
  309. }
  310. }
  311. static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
  312. {
  313. struct pci_dev *child, *parent = link->pdev;
  314. struct pci_bus *linkbus = parent->subordinate;
  315. struct aspm_register_info upreg, dwreg;
  316. if (blacklist) {
  317. /* Set enabled/disable so that we will disable ASPM later */
  318. link->aspm_enabled = ASPM_STATE_ALL;
  319. link->aspm_disable = ASPM_STATE_ALL;
  320. return;
  321. }
  322. /* Configure common clock before checking latencies */
  323. pcie_aspm_configure_common_clock(link);
  324. /* Get upstream/downstream components' register state */
  325. pcie_get_aspm_reg(parent, &upreg);
  326. child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
  327. pcie_get_aspm_reg(child, &dwreg);
  328. /*
  329. * Setup L0s state
  330. *
  331. * Note that we must not enable L0s in either direction on a
  332. * given link unless components on both sides of the link each
  333. * support L0s.
  334. */
  335. if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S)
  336. link->aspm_support |= ASPM_STATE_L0S;
  337. if (dwreg.enabled & PCIE_LINK_STATE_L0S)
  338. link->aspm_enabled |= ASPM_STATE_L0S_UP;
  339. if (upreg.enabled & PCIE_LINK_STATE_L0S)
  340. link->aspm_enabled |= ASPM_STATE_L0S_DW;
  341. link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s);
  342. link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s);
  343. /* Setup L1 state */
  344. if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1)
  345. link->aspm_support |= ASPM_STATE_L1;
  346. if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1)
  347. link->aspm_enabled |= ASPM_STATE_L1;
  348. link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1);
  349. link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1);
  350. /* Save default state */
  351. link->aspm_default = link->aspm_enabled;
  352. /* Setup initial capable state. Will be updated later */
  353. link->aspm_capable = link->aspm_support;
  354. /*
  355. * If the downstream component has pci bridge function, don't
  356. * do ASPM for now.
  357. */
  358. list_for_each_entry(child, &linkbus->devices, bus_list) {
  359. if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
  360. link->aspm_disable = ASPM_STATE_ALL;
  361. break;
  362. }
  363. }
  364. /* Get and check endpoint acceptable latencies */
  365. list_for_each_entry(child, &linkbus->devices, bus_list) {
  366. int pos;
  367. u32 reg32, encoding;
  368. struct aspm_latency *acceptable =
  369. &link->acceptable[PCI_FUNC(child->devfn)];
  370. if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
  371. child->pcie_type != PCI_EXP_TYPE_LEG_END)
  372. continue;
  373. pos = pci_pcie_cap(child);
  374. pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
  375. /* Calculate endpoint L0s acceptable latency */
  376. encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
  377. acceptable->l0s = calc_l0s_acceptable(encoding);
  378. /* Calculate endpoint L1 acceptable latency */
  379. encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
  380. acceptable->l1 = calc_l1_acceptable(encoding);
  381. pcie_aspm_check_latency(child);
  382. }
  383. }
  384. static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
  385. {
  386. u16 reg16;
  387. int pos = pci_pcie_cap(pdev);
  388. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  389. reg16 &= ~0x3;
  390. reg16 |= val;
  391. pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
  392. }
  393. static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
  394. {
  395. u32 upstream = 0, dwstream = 0;
  396. struct pci_dev *child, *parent = link->pdev;
  397. struct pci_bus *linkbus = parent->subordinate;
  398. /* Nothing to do if the link is already in the requested state */
  399. state &= (link->aspm_capable & ~link->aspm_disable);
  400. if (link->aspm_enabled == state)
  401. return;
  402. /* Convert ASPM state to upstream/downstream ASPM register state */
  403. if (state & ASPM_STATE_L0S_UP)
  404. dwstream |= PCIE_LINK_STATE_L0S;
  405. if (state & ASPM_STATE_L0S_DW)
  406. upstream |= PCIE_LINK_STATE_L0S;
  407. if (state & ASPM_STATE_L1) {
  408. upstream |= PCIE_LINK_STATE_L1;
  409. dwstream |= PCIE_LINK_STATE_L1;
  410. }
  411. /*
  412. * Spec 2.0 suggests all functions should be configured the
  413. * same setting for ASPM. Enabling ASPM L1 should be done in
  414. * upstream component first and then downstream, and vice
  415. * versa for disabling ASPM L1. Spec doesn't mention L0S.
  416. */
  417. if (state & ASPM_STATE_L1)
  418. pcie_config_aspm_dev(parent, upstream);
  419. list_for_each_entry(child, &linkbus->devices, bus_list)
  420. pcie_config_aspm_dev(child, dwstream);
  421. if (!(state & ASPM_STATE_L1))
  422. pcie_config_aspm_dev(parent, upstream);
  423. link->aspm_enabled = state;
  424. }
  425. static void pcie_config_aspm_path(struct pcie_link_state *link)
  426. {
  427. while (link) {
  428. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  429. link = link->parent;
  430. }
  431. }
  432. static void free_link_state(struct pcie_link_state *link)
  433. {
  434. link->pdev->link_state = NULL;
  435. kfree(link);
  436. }
  437. static int pcie_aspm_sanity_check(struct pci_dev *pdev)
  438. {
  439. struct pci_dev *child;
  440. int pos;
  441. u32 reg32;
  442. /*
  443. * Some functions in a slot might not all be PCIe functions,
  444. * very strange. Disable ASPM for the whole slot
  445. */
  446. list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
  447. pos = pci_pcie_cap(child);
  448. if (!pos)
  449. return -EINVAL;
  450. /*
  451. * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
  452. * RBER bit to determine if a function is 1.1 version device
  453. */
  454. pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
  455. if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
  456. dev_printk(KERN_INFO, &child->dev, "disabling ASPM"
  457. " on pre-1.1 PCIe device. You can enable it"
  458. " with 'pcie_aspm=force'\n");
  459. return -EINVAL;
  460. }
  461. }
  462. return 0;
  463. }
  464. static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
  465. {
  466. struct pcie_link_state *link;
  467. link = kzalloc(sizeof(*link), GFP_KERNEL);
  468. if (!link)
  469. return NULL;
  470. INIT_LIST_HEAD(&link->sibling);
  471. INIT_LIST_HEAD(&link->children);
  472. INIT_LIST_HEAD(&link->link);
  473. link->pdev = pdev;
  474. if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
  475. struct pcie_link_state *parent;
  476. parent = pdev->bus->parent->self->link_state;
  477. if (!parent) {
  478. kfree(link);
  479. return NULL;
  480. }
  481. link->parent = parent;
  482. list_add(&link->link, &parent->children);
  483. }
  484. /* Setup a pointer to the root port link */
  485. if (!link->parent)
  486. link->root = link;
  487. else
  488. link->root = link->parent->root;
  489. list_add(&link->sibling, &link_list);
  490. pdev->link_state = link;
  491. return link;
  492. }
  493. /*
  494. * pcie_aspm_init_link_state: Initiate PCI express link state.
  495. * It is called after the pcie and its children devices are scaned.
  496. * @pdev: the root port or switch downstream port
  497. */
  498. void pcie_aspm_init_link_state(struct pci_dev *pdev)
  499. {
  500. struct pcie_link_state *link;
  501. int blacklist = !!pcie_aspm_sanity_check(pdev);
  502. if (aspm_disabled || !pci_is_pcie(pdev) || pdev->link_state)
  503. return;
  504. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  505. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  506. return;
  507. /* VIA has a strange chipset, root port is under a bridge */
  508. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
  509. pdev->bus->self)
  510. return;
  511. down_read(&pci_bus_sem);
  512. if (list_empty(&pdev->subordinate->devices))
  513. goto out;
  514. mutex_lock(&aspm_lock);
  515. link = alloc_pcie_link_state(pdev);
  516. if (!link)
  517. goto unlock;
  518. /*
  519. * Setup initial ASPM state. Note that we need to configure
  520. * upstream links also because capable state of them can be
  521. * update through pcie_aspm_cap_init().
  522. */
  523. pcie_aspm_cap_init(link, blacklist);
  524. pcie_config_aspm_path(link);
  525. /* Setup initial Clock PM state */
  526. pcie_clkpm_cap_init(link, blacklist);
  527. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  528. unlock:
  529. mutex_unlock(&aspm_lock);
  530. out:
  531. up_read(&pci_bus_sem);
  532. }
  533. /* Recheck latencies and update aspm_capable for links under the root */
  534. static void pcie_update_aspm_capable(struct pcie_link_state *root)
  535. {
  536. struct pcie_link_state *link;
  537. BUG_ON(root->parent);
  538. list_for_each_entry(link, &link_list, sibling) {
  539. if (link->root != root)
  540. continue;
  541. link->aspm_capable = link->aspm_support;
  542. }
  543. list_for_each_entry(link, &link_list, sibling) {
  544. struct pci_dev *child;
  545. struct pci_bus *linkbus = link->pdev->subordinate;
  546. if (link->root != root)
  547. continue;
  548. list_for_each_entry(child, &linkbus->devices, bus_list) {
  549. if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT) &&
  550. (child->pcie_type != PCI_EXP_TYPE_LEG_END))
  551. continue;
  552. pcie_aspm_check_latency(child);
  553. }
  554. }
  555. }
  556. /* @pdev: the endpoint device */
  557. void pcie_aspm_exit_link_state(struct pci_dev *pdev)
  558. {
  559. struct pci_dev *parent = pdev->bus->self;
  560. struct pcie_link_state *link, *root, *parent_link;
  561. if (aspm_disabled || !pci_is_pcie(pdev) ||
  562. !parent || !parent->link_state)
  563. return;
  564. if ((parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT) &&
  565. (parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM))
  566. return;
  567. down_read(&pci_bus_sem);
  568. mutex_lock(&aspm_lock);
  569. /*
  570. * All PCIe functions are in one slot, remove one function will remove
  571. * the whole slot, so just wait until we are the last function left.
  572. */
  573. if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
  574. goto out;
  575. link = parent->link_state;
  576. root = link->root;
  577. parent_link = link->parent;
  578. /* All functions are removed, so just disable ASPM for the link */
  579. pcie_config_aspm_link(link, 0);
  580. list_del(&link->sibling);
  581. list_del(&link->link);
  582. /* Clock PM is for endpoint device */
  583. free_link_state(link);
  584. /* Recheck latencies and configure upstream links */
  585. if (parent_link) {
  586. pcie_update_aspm_capable(root);
  587. pcie_config_aspm_path(parent_link);
  588. }
  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 || !pci_is_pcie(pdev) || !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 || !pci_is_pcie(pdev))
  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 (!pci_is_pcie(pdev) ||
  745. (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  746. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  747. return;
  748. if (link_state->aspm_support)
  749. sysfs_add_file_to_group(&pdev->dev.kobj,
  750. &dev_attr_link_state.attr, power_group);
  751. if (link_state->clkpm_capable)
  752. sysfs_add_file_to_group(&pdev->dev.kobj,
  753. &dev_attr_clk_ctl.attr, power_group);
  754. }
  755. void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
  756. {
  757. struct pcie_link_state *link_state = pdev->link_state;
  758. if (!pci_is_pcie(pdev) ||
  759. (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  760. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  761. return;
  762. if (link_state->aspm_support)
  763. sysfs_remove_file_from_group(&pdev->dev.kobj,
  764. &dev_attr_link_state.attr, power_group);
  765. if (link_state->clkpm_capable)
  766. sysfs_remove_file_from_group(&pdev->dev.kobj,
  767. &dev_attr_clk_ctl.attr, power_group);
  768. }
  769. #endif
  770. static int __init pcie_aspm_disable(char *str)
  771. {
  772. if (!strcmp(str, "off")) {
  773. aspm_disabled = 1;
  774. printk(KERN_INFO "PCIe ASPM is disabled\n");
  775. } else if (!strcmp(str, "force")) {
  776. aspm_force = 1;
  777. printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
  778. }
  779. return 1;
  780. }
  781. __setup("pcie_aspm=", pcie_aspm_disable);
  782. void pcie_no_aspm(void)
  783. {
  784. if (!aspm_force)
  785. aspm_disabled = 1;
  786. }
  787. /**
  788. * pcie_aspm_enabled - is PCIe ASPM enabled?
  789. *
  790. * Returns true if ASPM has not been disabled by the command-line option
  791. * pcie_aspm=off.
  792. **/
  793. int pcie_aspm_enabled(void)
  794. {
  795. return !aspm_disabled;
  796. }
  797. EXPORT_SYMBOL(pcie_aspm_enabled);