aspm.c 26 KB

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