aspm.c 21 KB

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