aspm.c 28 KB

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