aspm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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/pci-aspm.h>
  19. #include "../pci.h"
  20. #ifdef MODULE_PARAM_PREFIX
  21. #undef MODULE_PARAM_PREFIX
  22. #endif
  23. #define MODULE_PARAM_PREFIX "pcie_aspm."
  24. struct endpoint_state {
  25. unsigned int l0s_acceptable_latency;
  26. unsigned int l1_acceptable_latency;
  27. };
  28. struct pcie_link_state {
  29. struct list_head sibiling;
  30. struct pci_dev *pdev;
  31. /* ASPM state */
  32. unsigned int support_state;
  33. unsigned int enabled_state;
  34. unsigned int bios_aspm_state;
  35. /* upstream component */
  36. unsigned int l0s_upper_latency;
  37. unsigned int l1_upper_latency;
  38. /* downstream component */
  39. unsigned int l0s_down_latency;
  40. unsigned int l1_down_latency;
  41. /* Clock PM state*/
  42. unsigned int clk_pm_capable;
  43. unsigned int clk_pm_enabled;
  44. unsigned int bios_clk_state;
  45. /*
  46. * A pcie downstream port only has one slot under it, so at most there
  47. * are 8 functions
  48. */
  49. struct endpoint_state endpoints[8];
  50. };
  51. static int aspm_disabled, aspm_force;
  52. static DEFINE_MUTEX(aspm_lock);
  53. static LIST_HEAD(link_list);
  54. #define POLICY_DEFAULT 0 /* BIOS default setting */
  55. #define POLICY_PERFORMANCE 1 /* high performance */
  56. #define POLICY_POWERSAVE 2 /* high power saving */
  57. static int aspm_policy;
  58. static const char *policy_str[] = {
  59. [POLICY_DEFAULT] = "default",
  60. [POLICY_PERFORMANCE] = "performance",
  61. [POLICY_POWERSAVE] = "powersave"
  62. };
  63. static int policy_to_aspm_state(struct pci_dev *pdev)
  64. {
  65. struct pcie_link_state *link_state = pdev->link_state;
  66. switch (aspm_policy) {
  67. case POLICY_PERFORMANCE:
  68. /* Disable ASPM and Clock PM */
  69. return 0;
  70. case POLICY_POWERSAVE:
  71. /* Enable ASPM L0s/L1 */
  72. return PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1;
  73. case POLICY_DEFAULT:
  74. return link_state->bios_aspm_state;
  75. }
  76. return 0;
  77. }
  78. static int policy_to_clkpm_state(struct pci_dev *pdev)
  79. {
  80. struct pcie_link_state *link_state = pdev->link_state;
  81. switch (aspm_policy) {
  82. case POLICY_PERFORMANCE:
  83. /* Disable ASPM and Clock PM */
  84. return 0;
  85. case POLICY_POWERSAVE:
  86. /* Disable Clock PM */
  87. return 1;
  88. case POLICY_DEFAULT:
  89. return link_state->bios_clk_state;
  90. }
  91. return 0;
  92. }
  93. static void pcie_set_clock_pm(struct pci_dev *pdev, int enable)
  94. {
  95. struct pci_dev *child_dev;
  96. int pos;
  97. u16 reg16;
  98. struct pcie_link_state *link_state = pdev->link_state;
  99. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  100. pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
  101. if (!pos)
  102. return;
  103. pci_read_config_word(child_dev, pos + PCI_EXP_LNKCTL, &reg16);
  104. if (enable)
  105. reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
  106. else
  107. reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  108. pci_write_config_word(child_dev, pos + PCI_EXP_LNKCTL, reg16);
  109. }
  110. link_state->clk_pm_enabled = !!enable;
  111. }
  112. static void pcie_check_clock_pm(struct pci_dev *pdev)
  113. {
  114. int pos;
  115. u32 reg32;
  116. u16 reg16;
  117. int capable = 1, enabled = 1;
  118. struct pci_dev *child_dev;
  119. struct pcie_link_state *link_state = pdev->link_state;
  120. /* All functions should have the same cap and state, take the worst */
  121. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  122. pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
  123. if (!pos)
  124. return;
  125. pci_read_config_dword(child_dev, pos + PCI_EXP_LNKCAP, &reg32);
  126. if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
  127. capable = 0;
  128. enabled = 0;
  129. break;
  130. }
  131. pci_read_config_word(child_dev, pos + PCI_EXP_LNKCTL, &reg16);
  132. if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
  133. enabled = 0;
  134. }
  135. link_state->clk_pm_capable = capable;
  136. link_state->clk_pm_enabled = enabled;
  137. link_state->bios_clk_state = enabled;
  138. pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
  139. }
  140. /*
  141. * pcie_aspm_configure_common_clock: check if the 2 ends of a link
  142. * could use common clock. If they are, configure them to use the
  143. * common clock. That will reduce the ASPM state exit latency.
  144. */
  145. static void pcie_aspm_configure_common_clock(struct pci_dev *pdev)
  146. {
  147. int pos, child_pos;
  148. u16 reg16 = 0;
  149. struct pci_dev *child_dev;
  150. int same_clock = 1;
  151. /*
  152. * all functions of a slot should have the same Slot Clock
  153. * Configuration, so just check one function
  154. * */
  155. child_dev = list_entry(pdev->subordinate->devices.next, struct pci_dev,
  156. bus_list);
  157. BUG_ON(!child_dev->is_pcie);
  158. /* Check downstream component if bit Slot Clock Configuration is 1 */
  159. child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
  160. pci_read_config_word(child_dev, child_pos + PCI_EXP_LNKSTA, &reg16);
  161. if (!(reg16 & PCI_EXP_LNKSTA_SLC))
  162. same_clock = 0;
  163. /* Check upstream component if bit Slot Clock Configuration is 1 */
  164. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  165. pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, &reg16);
  166. if (!(reg16 & PCI_EXP_LNKSTA_SLC))
  167. same_clock = 0;
  168. /* Configure downstream component, all functions */
  169. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  170. child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
  171. pci_read_config_word(child_dev, child_pos + PCI_EXP_LNKCTL,
  172. &reg16);
  173. if (same_clock)
  174. reg16 |= PCI_EXP_LNKCTL_CCC;
  175. else
  176. reg16 &= ~PCI_EXP_LNKCTL_CCC;
  177. pci_write_config_word(child_dev, child_pos + PCI_EXP_LNKCTL,
  178. reg16);
  179. }
  180. /* Configure upstream component */
  181. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  182. if (same_clock)
  183. reg16 |= PCI_EXP_LNKCTL_CCC;
  184. else
  185. reg16 &= ~PCI_EXP_LNKCTL_CCC;
  186. pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
  187. /* retrain link */
  188. reg16 |= PCI_EXP_LNKCTL_RL;
  189. pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
  190. /* Wait for link training end */
  191. while (1) {
  192. pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, &reg16);
  193. if (!(reg16 & PCI_EXP_LNKSTA_LT))
  194. break;
  195. cpu_relax();
  196. }
  197. }
  198. /*
  199. * calc_L0S_latency: Convert L0s latency encoding to ns
  200. */
  201. static unsigned int calc_L0S_latency(unsigned int latency_encoding, int ac)
  202. {
  203. unsigned int ns = 64;
  204. if (latency_encoding == 0x7) {
  205. if (ac)
  206. ns = -1U;
  207. else
  208. ns = 5*1000; /* > 4us */
  209. } else
  210. ns *= (1 << latency_encoding);
  211. return ns;
  212. }
  213. /*
  214. * calc_L1_latency: Convert L1 latency encoding to ns
  215. */
  216. static unsigned int calc_L1_latency(unsigned int latency_encoding, int ac)
  217. {
  218. unsigned int ns = 1000;
  219. if (latency_encoding == 0x7) {
  220. if (ac)
  221. ns = -1U;
  222. else
  223. ns = 65*1000; /* > 64us */
  224. } else
  225. ns *= (1 << latency_encoding);
  226. return ns;
  227. }
  228. static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
  229. unsigned int *l0s, unsigned int *l1, unsigned int *enabled)
  230. {
  231. int pos;
  232. u16 reg16;
  233. u32 reg32;
  234. unsigned int latency;
  235. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  236. pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
  237. *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
  238. if (*state != PCIE_LINK_STATE_L0S &&
  239. *state != (PCIE_LINK_STATE_L1|PCIE_LINK_STATE_L0S))
  240. *state = 0;
  241. if (*state == 0)
  242. return;
  243. latency = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
  244. *l0s = calc_L0S_latency(latency, 0);
  245. if (*state & PCIE_LINK_STATE_L1) {
  246. latency = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
  247. *l1 = calc_L1_latency(latency, 0);
  248. }
  249. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  250. *enabled = reg16 & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1);
  251. }
  252. static void pcie_aspm_cap_init(struct pci_dev *pdev)
  253. {
  254. struct pci_dev *child_dev;
  255. u32 state, tmp;
  256. struct pcie_link_state *link_state = pdev->link_state;
  257. /* upstream component states */
  258. pcie_aspm_get_cap_device(pdev, &link_state->support_state,
  259. &link_state->l0s_upper_latency,
  260. &link_state->l1_upper_latency,
  261. &link_state->enabled_state);
  262. /* downstream component states, all functions have the same setting */
  263. child_dev = list_entry(pdev->subordinate->devices.next, struct pci_dev,
  264. bus_list);
  265. pcie_aspm_get_cap_device(child_dev, &state,
  266. &link_state->l0s_down_latency,
  267. &link_state->l1_down_latency,
  268. &tmp);
  269. link_state->support_state &= state;
  270. if (!link_state->support_state)
  271. return;
  272. link_state->enabled_state &= link_state->support_state;
  273. link_state->bios_aspm_state = link_state->enabled_state;
  274. /* ENDPOINT states*/
  275. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  276. int pos;
  277. u32 reg32;
  278. unsigned int latency;
  279. struct endpoint_state *ep_state =
  280. &link_state->endpoints[PCI_FUNC(child_dev->devfn)];
  281. if (child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
  282. child_dev->pcie_type != PCI_EXP_TYPE_LEG_END)
  283. continue;
  284. pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
  285. pci_read_config_dword(child_dev, pos + PCI_EXP_DEVCAP, &reg32);
  286. latency = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
  287. latency = calc_L0S_latency(latency, 1);
  288. ep_state->l0s_acceptable_latency = latency;
  289. if (link_state->support_state & PCIE_LINK_STATE_L1) {
  290. latency = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
  291. latency = calc_L1_latency(latency, 1);
  292. ep_state->l1_acceptable_latency = latency;
  293. }
  294. }
  295. }
  296. static unsigned int __pcie_aspm_check_state_one(struct pci_dev *pdev,
  297. unsigned int state)
  298. {
  299. struct pci_dev *parent_dev, *tmp_dev;
  300. unsigned int latency, l1_latency = 0;
  301. struct pcie_link_state *link_state;
  302. struct endpoint_state *ep_state;
  303. parent_dev = pdev->bus->self;
  304. link_state = parent_dev->link_state;
  305. state &= link_state->support_state;
  306. if (state == 0)
  307. return 0;
  308. ep_state = &link_state->endpoints[PCI_FUNC(pdev->devfn)];
  309. /*
  310. * Check latency for endpoint device.
  311. * TBD: The latency from the endpoint to root complex vary per
  312. * switch's upstream link state above the device. Here we just do a
  313. * simple check which assumes all links above the device can be in L1
  314. * state, that is we just consider the worst case. If switch's upstream
  315. * link can't be put into L0S/L1, then our check is too strictly.
  316. */
  317. tmp_dev = pdev;
  318. while (state & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1)) {
  319. parent_dev = tmp_dev->bus->self;
  320. link_state = parent_dev->link_state;
  321. if (state & PCIE_LINK_STATE_L0S) {
  322. latency = max_t(unsigned int,
  323. link_state->l0s_upper_latency,
  324. link_state->l0s_down_latency);
  325. if (latency > ep_state->l0s_acceptable_latency)
  326. state &= ~PCIE_LINK_STATE_L0S;
  327. }
  328. if (state & PCIE_LINK_STATE_L1) {
  329. latency = max_t(unsigned int,
  330. link_state->l1_upper_latency,
  331. link_state->l1_down_latency);
  332. if (latency + l1_latency >
  333. ep_state->l1_acceptable_latency)
  334. state &= ~PCIE_LINK_STATE_L1;
  335. }
  336. if (!parent_dev->bus->self) /* parent_dev is a root port */
  337. break;
  338. else {
  339. /*
  340. * parent_dev is the downstream port of a switch, make
  341. * tmp_dev the upstream port of the switch
  342. */
  343. tmp_dev = parent_dev->bus->self;
  344. /*
  345. * every switch on the path to root complex need 1 more
  346. * microsecond for L1. Spec doesn't mention L0S.
  347. */
  348. if (state & PCIE_LINK_STATE_L1)
  349. l1_latency += 1000;
  350. }
  351. }
  352. return state;
  353. }
  354. static unsigned int pcie_aspm_check_state(struct pci_dev *pdev,
  355. unsigned int state)
  356. {
  357. struct pci_dev *child_dev;
  358. /* If no child, disable the link */
  359. if (list_empty(&pdev->subordinate->devices))
  360. return 0;
  361. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  362. if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
  363. /*
  364. * If downstream component of a link is pci bridge, we
  365. * disable ASPM for now for the link
  366. * */
  367. state = 0;
  368. break;
  369. }
  370. if ((child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
  371. child_dev->pcie_type != PCI_EXP_TYPE_LEG_END))
  372. continue;
  373. /* Device not in D0 doesn't need check latency */
  374. if (child_dev->current_state == PCI_D1 ||
  375. child_dev->current_state == PCI_D2 ||
  376. child_dev->current_state == PCI_D3hot ||
  377. child_dev->current_state == PCI_D3cold)
  378. continue;
  379. state = __pcie_aspm_check_state_one(child_dev, state);
  380. }
  381. return state;
  382. }
  383. static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
  384. {
  385. u16 reg16;
  386. int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  387. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  388. reg16 &= ~0x3;
  389. reg16 |= state;
  390. pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
  391. }
  392. static void __pcie_aspm_config_link(struct pci_dev *pdev, unsigned int state)
  393. {
  394. struct pci_dev *child_dev;
  395. int valid = 1;
  396. struct pcie_link_state *link_state = pdev->link_state;
  397. /*
  398. * if the downstream component has pci bridge function, don't do ASPM
  399. * now
  400. */
  401. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  402. if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
  403. valid = 0;
  404. break;
  405. }
  406. }
  407. if (!valid)
  408. return;
  409. /*
  410. * spec 2.0 suggests all functions should be configured the same
  411. * setting for ASPM. Enabling ASPM L1 should be done in upstream
  412. * component first and then downstream, and vice versa for disabling
  413. * ASPM L1. Spec doesn't mention L0S.
  414. */
  415. if (state & PCIE_LINK_STATE_L1)
  416. __pcie_aspm_config_one_dev(pdev, state);
  417. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list)
  418. __pcie_aspm_config_one_dev(child_dev, state);
  419. if (!(state & PCIE_LINK_STATE_L1))
  420. __pcie_aspm_config_one_dev(pdev, state);
  421. link_state->enabled_state = state;
  422. }
  423. static void __pcie_aspm_configure_link_state(struct pci_dev *pdev,
  424. unsigned int state)
  425. {
  426. struct pcie_link_state *link_state = pdev->link_state;
  427. if (link_state->support_state == 0)
  428. return;
  429. state &= PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1;
  430. /* state 0 means disabling aspm */
  431. state = pcie_aspm_check_state(pdev, state);
  432. if (link_state->enabled_state == state)
  433. return;
  434. __pcie_aspm_config_link(pdev, state);
  435. }
  436. /*
  437. * pcie_aspm_configure_link_state: enable/disable PCI express link state
  438. * @pdev: the root port or switch downstream port
  439. */
  440. static void pcie_aspm_configure_link_state(struct pci_dev *pdev,
  441. unsigned int state)
  442. {
  443. down_read(&pci_bus_sem);
  444. mutex_lock(&aspm_lock);
  445. __pcie_aspm_configure_link_state(pdev, state);
  446. mutex_unlock(&aspm_lock);
  447. up_read(&pci_bus_sem);
  448. }
  449. static void free_link_state(struct pci_dev *pdev)
  450. {
  451. kfree(pdev->link_state);
  452. pdev->link_state = NULL;
  453. }
  454. static int pcie_aspm_sanity_check(struct pci_dev *pdev)
  455. {
  456. struct pci_dev *child_dev;
  457. int child_pos;
  458. u32 reg32;
  459. /*
  460. * Some functions in a slot might not all be PCIE functions, very
  461. * strange. Disable ASPM for the whole slot
  462. */
  463. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  464. child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
  465. if (!child_pos)
  466. return -EINVAL;
  467. /*
  468. * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
  469. * RBER bit to determine if a function is 1.1 version device
  470. */
  471. pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP,
  472. &reg32);
  473. if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
  474. printk("Pre-1.1 PCIe device detected, "
  475. "disable ASPM for %s. It can be enabled forcedly"
  476. " with 'pcie_aspm=force'\n", pci_name(pdev));
  477. return -EINVAL;
  478. }
  479. }
  480. return 0;
  481. }
  482. /*
  483. * pcie_aspm_init_link_state: Initiate PCI express link state.
  484. * It is called after the pcie and its children devices are scaned.
  485. * @pdev: the root port or switch downstream port
  486. */
  487. void pcie_aspm_init_link_state(struct pci_dev *pdev)
  488. {
  489. unsigned int state;
  490. struct pcie_link_state *link_state;
  491. int error = 0;
  492. if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
  493. return;
  494. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  495. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  496. return;
  497. down_read(&pci_bus_sem);
  498. if (list_empty(&pdev->subordinate->devices))
  499. goto out;
  500. if (pcie_aspm_sanity_check(pdev))
  501. goto out;
  502. mutex_lock(&aspm_lock);
  503. link_state = kzalloc(sizeof(*link_state), GFP_KERNEL);
  504. if (!link_state)
  505. goto unlock_out;
  506. pdev->link_state = link_state;
  507. pcie_aspm_configure_common_clock(pdev);
  508. pcie_aspm_cap_init(pdev);
  509. /* config link state to avoid BIOS error */
  510. state = pcie_aspm_check_state(pdev, policy_to_aspm_state(pdev));
  511. __pcie_aspm_config_link(pdev, state);
  512. pcie_check_clock_pm(pdev);
  513. link_state->pdev = pdev;
  514. list_add(&link_state->sibiling, &link_list);
  515. unlock_out:
  516. if (error)
  517. free_link_state(pdev);
  518. mutex_unlock(&aspm_lock);
  519. out:
  520. up_read(&pci_bus_sem);
  521. }
  522. /* @pdev: the endpoint device */
  523. void pcie_aspm_exit_link_state(struct pci_dev *pdev)
  524. {
  525. struct pci_dev *parent = pdev->bus->self;
  526. struct pcie_link_state *link_state = parent->link_state;
  527. if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
  528. return;
  529. if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  530. parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  531. return;
  532. down_read(&pci_bus_sem);
  533. mutex_lock(&aspm_lock);
  534. /*
  535. * All PCIe functions are in one slot, remove one function will remove
  536. * the the whole slot, so just wait
  537. */
  538. if (!list_empty(&parent->subordinate->devices))
  539. goto out;
  540. /* All functions are removed, so just disable ASPM for the link */
  541. __pcie_aspm_config_one_dev(parent, 0);
  542. list_del(&link_state->sibiling);
  543. /* Clock PM is for endpoint device */
  544. free_link_state(parent);
  545. out:
  546. mutex_unlock(&aspm_lock);
  547. up_read(&pci_bus_sem);
  548. }
  549. /* @pdev: the root port or switch downstream port */
  550. void pcie_aspm_pm_state_change(struct pci_dev *pdev)
  551. {
  552. struct pcie_link_state *link_state = pdev->link_state;
  553. if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
  554. return;
  555. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  556. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  557. return;
  558. /*
  559. * devices changed PM state, we should recheck if latency meets all
  560. * functions' requirement
  561. */
  562. pcie_aspm_configure_link_state(pdev, link_state->enabled_state);
  563. }
  564. /*
  565. * pci_disable_link_state - disable pci device's link state, so the link will
  566. * never enter specific states
  567. */
  568. void pci_disable_link_state(struct pci_dev *pdev, int state)
  569. {
  570. struct pci_dev *parent = pdev->bus->self;
  571. struct pcie_link_state *link_state;
  572. if (aspm_disabled || !pdev->is_pcie)
  573. return;
  574. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
  575. pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
  576. parent = pdev;
  577. if (!parent || !parent->link_state)
  578. return;
  579. down_read(&pci_bus_sem);
  580. mutex_lock(&aspm_lock);
  581. link_state = parent->link_state;
  582. link_state->support_state &=
  583. ~(state & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1));
  584. if (state & PCIE_LINK_STATE_CLKPM)
  585. link_state->clk_pm_capable = 0;
  586. __pcie_aspm_configure_link_state(parent, link_state->enabled_state);
  587. if (!link_state->clk_pm_capable && link_state->clk_pm_enabled)
  588. pcie_set_clock_pm(parent, 0);
  589. mutex_unlock(&aspm_lock);
  590. up_read(&pci_bus_sem);
  591. }
  592. EXPORT_SYMBOL(pci_disable_link_state);
  593. static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
  594. {
  595. int i;
  596. struct pci_dev *pdev;
  597. struct pcie_link_state *link_state;
  598. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  599. if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
  600. break;
  601. if (i >= ARRAY_SIZE(policy_str))
  602. return -EINVAL;
  603. if (i == aspm_policy)
  604. return 0;
  605. down_read(&pci_bus_sem);
  606. mutex_lock(&aspm_lock);
  607. aspm_policy = i;
  608. list_for_each_entry(link_state, &link_list, sibiling) {
  609. pdev = link_state->pdev;
  610. __pcie_aspm_configure_link_state(pdev,
  611. policy_to_aspm_state(pdev));
  612. if (link_state->clk_pm_capable &&
  613. link_state->clk_pm_enabled != policy_to_clkpm_state(pdev))
  614. pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
  615. }
  616. mutex_unlock(&aspm_lock);
  617. up_read(&pci_bus_sem);
  618. return 0;
  619. }
  620. static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
  621. {
  622. int i, cnt = 0;
  623. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  624. if (i == aspm_policy)
  625. cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
  626. else
  627. cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
  628. return cnt;
  629. }
  630. module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
  631. NULL, 0644);
  632. #ifdef CONFIG_PCIEASPM_DEBUG
  633. static ssize_t link_state_show(struct device *dev,
  634. struct device_attribute *attr,
  635. char *buf)
  636. {
  637. struct pci_dev *pci_device = to_pci_dev(dev);
  638. struct pcie_link_state *link_state = pci_device->link_state;
  639. return sprintf(buf, "%d\n", link_state->enabled_state);
  640. }
  641. static ssize_t link_state_store(struct device *dev,
  642. struct device_attribute *attr,
  643. const char *buf,
  644. size_t n)
  645. {
  646. struct pci_dev *pci_device = to_pci_dev(dev);
  647. int state;
  648. if (n < 1)
  649. return -EINVAL;
  650. state = buf[0]-'0';
  651. if (state >= 0 && state <= 3) {
  652. /* setup link aspm state */
  653. pcie_aspm_configure_link_state(pci_device, state);
  654. return n;
  655. }
  656. return -EINVAL;
  657. }
  658. static ssize_t clk_ctl_show(struct device *dev,
  659. struct device_attribute *attr,
  660. char *buf)
  661. {
  662. struct pci_dev *pci_device = to_pci_dev(dev);
  663. struct pcie_link_state *link_state = pci_device->link_state;
  664. return sprintf(buf, "%d\n", link_state->clk_pm_enabled);
  665. }
  666. static ssize_t clk_ctl_store(struct device *dev,
  667. struct device_attribute *attr,
  668. const char *buf,
  669. size_t n)
  670. {
  671. struct pci_dev *pci_device = to_pci_dev(dev);
  672. int state;
  673. if (n < 1)
  674. return -EINVAL;
  675. state = buf[0]-'0';
  676. down_read(&pci_bus_sem);
  677. mutex_lock(&aspm_lock);
  678. pcie_set_clock_pm(pci_device, !!state);
  679. mutex_unlock(&aspm_lock);
  680. up_read(&pci_bus_sem);
  681. return n;
  682. }
  683. static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
  684. static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
  685. static char power_group[] = "power";
  686. void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
  687. {
  688. struct pcie_link_state *link_state = pdev->link_state;
  689. if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  690. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  691. return;
  692. if (link_state->support_state)
  693. sysfs_add_file_to_group(&pdev->dev.kobj,
  694. &dev_attr_link_state.attr, power_group);
  695. if (link_state->clk_pm_capable)
  696. sysfs_add_file_to_group(&pdev->dev.kobj,
  697. &dev_attr_clk_ctl.attr, power_group);
  698. }
  699. void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
  700. {
  701. struct pcie_link_state *link_state = pdev->link_state;
  702. if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  703. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  704. return;
  705. if (link_state->support_state)
  706. sysfs_remove_file_from_group(&pdev->dev.kobj,
  707. &dev_attr_link_state.attr, power_group);
  708. if (link_state->clk_pm_capable)
  709. sysfs_remove_file_from_group(&pdev->dev.kobj,
  710. &dev_attr_clk_ctl.attr, power_group);
  711. }
  712. #endif
  713. static int __init pcie_aspm_disable(char *str)
  714. {
  715. if (!strcmp(str, "off")) {
  716. aspm_disabled = 1;
  717. printk(KERN_INFO "PCIe ASPM is disabled\n");
  718. } else if (!strcmp(str, "force")) {
  719. aspm_force = 1;
  720. printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
  721. }
  722. return 1;
  723. }
  724. __setup("pcie_aspm=", pcie_aspm_disable);
  725. void pcie_no_aspm(void)
  726. {
  727. if (!aspm_force)
  728. aspm_disabled = 1;
  729. }
  730. #ifdef CONFIG_ACPI
  731. #include <acpi/acpi_bus.h>
  732. #include <linux/pci-acpi.h>
  733. static void pcie_aspm_platform_init(void)
  734. {
  735. pcie_osc_support_set(OSC_ACTIVE_STATE_PWR_SUPPORT|
  736. OSC_CLOCK_PWR_CAPABILITY_SUPPORT);
  737. }
  738. #else
  739. static inline void pcie_aspm_platform_init(void) { }
  740. #endif
  741. static int __init pcie_aspm_init(void)
  742. {
  743. if (aspm_disabled)
  744. return 0;
  745. pcie_aspm_platform_init();
  746. return 0;
  747. }
  748. fs_initcall(pcie_aspm_init);