aspm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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;
  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. /*
  455. * pcie_aspm_init_link_state: Initiate PCI express link state.
  456. * It is called after the pcie and its children devices are scaned.
  457. * @pdev: the root port or switch downstream port
  458. */
  459. void pcie_aspm_init_link_state(struct pci_dev *pdev)
  460. {
  461. unsigned int state;
  462. struct pcie_link_state *link_state;
  463. int error = 0;
  464. if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
  465. return;
  466. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  467. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  468. return;
  469. down_read(&pci_bus_sem);
  470. if (list_empty(&pdev->subordinate->devices))
  471. goto out;
  472. mutex_lock(&aspm_lock);
  473. link_state = kzalloc(sizeof(*link_state), GFP_KERNEL);
  474. if (!link_state)
  475. goto unlock_out;
  476. pdev->link_state = link_state;
  477. pcie_aspm_configure_common_clock(pdev);
  478. pcie_aspm_cap_init(pdev);
  479. /* config link state to avoid BIOS error */
  480. state = pcie_aspm_check_state(pdev, policy_to_aspm_state(pdev));
  481. __pcie_aspm_config_link(pdev, state);
  482. pcie_check_clock_pm(pdev);
  483. link_state->pdev = pdev;
  484. list_add(&link_state->sibiling, &link_list);
  485. unlock_out:
  486. if (error)
  487. free_link_state(pdev);
  488. mutex_unlock(&aspm_lock);
  489. out:
  490. up_read(&pci_bus_sem);
  491. }
  492. /* @pdev: the endpoint device */
  493. void pcie_aspm_exit_link_state(struct pci_dev *pdev)
  494. {
  495. struct pci_dev *parent = pdev->bus->self;
  496. struct pcie_link_state *link_state = parent->link_state;
  497. if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
  498. return;
  499. if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  500. parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  501. return;
  502. down_read(&pci_bus_sem);
  503. mutex_lock(&aspm_lock);
  504. /*
  505. * All PCIe functions are in one slot, remove one function will remove
  506. * the the whole slot, so just wait
  507. */
  508. if (!list_empty(&parent->subordinate->devices))
  509. goto out;
  510. /* All functions are removed, so just disable ASPM for the link */
  511. __pcie_aspm_config_one_dev(parent, 0);
  512. list_del(&link_state->sibiling);
  513. /* Clock PM is for endpoint device */
  514. free_link_state(parent);
  515. out:
  516. mutex_unlock(&aspm_lock);
  517. up_read(&pci_bus_sem);
  518. }
  519. /* @pdev: the root port or switch downstream port */
  520. void pcie_aspm_pm_state_change(struct pci_dev *pdev)
  521. {
  522. struct pcie_link_state *link_state = pdev->link_state;
  523. if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
  524. return;
  525. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  526. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  527. return;
  528. /*
  529. * devices changed PM state, we should recheck if latency meets all
  530. * functions' requirement
  531. */
  532. pcie_aspm_configure_link_state(pdev, link_state->enabled_state);
  533. }
  534. /*
  535. * pci_disable_link_state - disable pci device's link state, so the link will
  536. * never enter specific states
  537. */
  538. void pci_disable_link_state(struct pci_dev *pdev, int state)
  539. {
  540. struct pci_dev *parent = pdev->bus->self;
  541. struct pcie_link_state *link_state;
  542. if (aspm_disabled || !pdev->is_pcie)
  543. return;
  544. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
  545. pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
  546. parent = pdev;
  547. if (!parent || !parent->link_state)
  548. return;
  549. down_read(&pci_bus_sem);
  550. mutex_lock(&aspm_lock);
  551. link_state = parent->link_state;
  552. link_state->support_state &=
  553. ~(state & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1));
  554. if (state & PCIE_LINK_STATE_CLKPM)
  555. link_state->clk_pm_capable = 0;
  556. __pcie_aspm_configure_link_state(parent, link_state->enabled_state);
  557. if (!link_state->clk_pm_capable && link_state->clk_pm_enabled)
  558. pcie_set_clock_pm(parent, 0);
  559. mutex_unlock(&aspm_lock);
  560. up_read(&pci_bus_sem);
  561. }
  562. EXPORT_SYMBOL(pci_disable_link_state);
  563. static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
  564. {
  565. int i;
  566. struct pci_dev *pdev;
  567. struct pcie_link_state *link_state;
  568. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  569. if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
  570. break;
  571. if (i >= ARRAY_SIZE(policy_str))
  572. return -EINVAL;
  573. if (i == aspm_policy)
  574. return 0;
  575. down_read(&pci_bus_sem);
  576. mutex_lock(&aspm_lock);
  577. aspm_policy = i;
  578. list_for_each_entry(link_state, &link_list, sibiling) {
  579. pdev = link_state->pdev;
  580. __pcie_aspm_configure_link_state(pdev,
  581. policy_to_aspm_state(pdev));
  582. if (link_state->clk_pm_capable &&
  583. link_state->clk_pm_enabled != policy_to_clkpm_state(pdev))
  584. pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
  585. }
  586. mutex_unlock(&aspm_lock);
  587. up_read(&pci_bus_sem);
  588. return 0;
  589. }
  590. static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
  591. {
  592. int i, cnt = 0;
  593. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  594. if (i == aspm_policy)
  595. cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
  596. else
  597. cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
  598. return cnt;
  599. }
  600. module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
  601. NULL, 0644);
  602. #ifdef CONFIG_PCIEASPM_DEBUG
  603. static ssize_t link_state_show(struct device *dev,
  604. struct device_attribute *attr,
  605. char *buf)
  606. {
  607. struct pci_dev *pci_device = to_pci_dev(dev);
  608. struct pcie_link_state *link_state = pci_device->link_state;
  609. return sprintf(buf, "%d\n", link_state->enabled_state);
  610. }
  611. static ssize_t link_state_store(struct device *dev,
  612. struct device_attribute *attr,
  613. const char *buf,
  614. size_t n)
  615. {
  616. struct pci_dev *pci_device = to_pci_dev(dev);
  617. int state;
  618. if (n < 1)
  619. return -EINVAL;
  620. state = buf[0]-'0';
  621. if (state >= 0 && state <= 3) {
  622. /* setup link aspm state */
  623. pcie_aspm_configure_link_state(pci_device, state);
  624. return n;
  625. }
  626. return -EINVAL;
  627. }
  628. static ssize_t clk_ctl_show(struct device *dev,
  629. struct device_attribute *attr,
  630. char *buf)
  631. {
  632. struct pci_dev *pci_device = to_pci_dev(dev);
  633. struct pcie_link_state *link_state = pci_device->link_state;
  634. return sprintf(buf, "%d\n", link_state->clk_pm_enabled);
  635. }
  636. static ssize_t clk_ctl_store(struct device *dev,
  637. struct device_attribute *attr,
  638. const char *buf,
  639. size_t n)
  640. {
  641. struct pci_dev *pci_device = to_pci_dev(dev);
  642. int state;
  643. if (n < 1)
  644. return -EINVAL;
  645. state = buf[0]-'0';
  646. down_read(&pci_bus_sem);
  647. mutex_lock(&aspm_lock);
  648. pcie_set_clock_pm(pci_device, !!state);
  649. mutex_unlock(&aspm_lock);
  650. up_read(&pci_bus_sem);
  651. return n;
  652. }
  653. static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
  654. static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
  655. static char power_group[] = "power";
  656. void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
  657. {
  658. struct pcie_link_state *link_state = pdev->link_state;
  659. if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  660. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  661. return;
  662. if (link_state->support_state)
  663. sysfs_add_file_to_group(&pdev->dev.kobj,
  664. &dev_attr_link_state.attr, power_group);
  665. if (link_state->clk_pm_capable)
  666. sysfs_add_file_to_group(&pdev->dev.kobj,
  667. &dev_attr_clk_ctl.attr, power_group);
  668. }
  669. void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
  670. {
  671. struct pcie_link_state *link_state = pdev->link_state;
  672. if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  673. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  674. return;
  675. if (link_state->support_state)
  676. sysfs_remove_file_from_group(&pdev->dev.kobj,
  677. &dev_attr_link_state.attr, power_group);
  678. if (link_state->clk_pm_capable)
  679. sysfs_remove_file_from_group(&pdev->dev.kobj,
  680. &dev_attr_clk_ctl.attr, power_group);
  681. }
  682. #endif
  683. static int __init pcie_aspm_disable(char *str)
  684. {
  685. aspm_disabled = 1;
  686. return 1;
  687. }
  688. __setup("pcie_noaspm", pcie_aspm_disable);
  689. #ifdef CONFIG_ACPI
  690. #include <acpi/acpi_bus.h>
  691. #include <linux/pci-acpi.h>
  692. static void pcie_aspm_platform_init(void)
  693. {
  694. pcie_osc_support_set(OSC_ACTIVE_STATE_PWR_SUPPORT|
  695. OSC_CLOCK_PWR_CAPABILITY_SUPPORT);
  696. }
  697. #else
  698. static inline void pcie_aspm_platform_init(void) { }
  699. #endif
  700. static int __init pcie_aspm_init(void)
  701. {
  702. if (aspm_disabled)
  703. return 0;
  704. pcie_aspm_platform_init();
  705. return 0;
  706. }
  707. fs_initcall(pcie_aspm_init);