aspm.c 23 KB

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