aspm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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 sibling;
  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. u32 aspm_support:2; /* Supported ASPM state */
  39. u32 aspm_enabled:2; /* Enabled ASPM state */
  40. u32 aspm_default:2; /* Default ASPM state by BIOS */
  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->aspm_default;
  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. *l0s = *l1 = *enabled = 0;
  282. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  283. pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
  284. *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
  285. if (*state != PCIE_LINK_STATE_L0S &&
  286. *state != (PCIE_LINK_STATE_L1|PCIE_LINK_STATE_L0S))
  287. *state = 0;
  288. if (*state == 0)
  289. return;
  290. latency = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
  291. *l0s = calc_L0S_latency(latency, 0);
  292. if (*state & PCIE_LINK_STATE_L1) {
  293. latency = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
  294. *l1 = calc_L1_latency(latency, 0);
  295. }
  296. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  297. *enabled = reg16 & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1);
  298. }
  299. static void pcie_aspm_cap_init(struct pci_dev *pdev)
  300. {
  301. struct pci_dev *child_dev;
  302. u32 support, l0s, l1, enabled;
  303. struct pcie_link_state *link_state = pdev->link_state;
  304. /* upstream component states */
  305. pcie_aspm_get_cap_device(pdev, &support, &l0s, &l1, &enabled);
  306. link_state->aspm_support = support;
  307. link_state->l0s_upper_latency = l0s;
  308. link_state->l1_upper_latency = l1;
  309. link_state->aspm_enabled = enabled;
  310. /* downstream component states, all functions have the same setting */
  311. child_dev = list_entry(pdev->subordinate->devices.next, struct pci_dev,
  312. bus_list);
  313. pcie_aspm_get_cap_device(child_dev, &support, &l0s, &l1, &enabled);
  314. link_state->aspm_support &= support;
  315. link_state->l0s_down_latency = l0s;
  316. link_state->l1_down_latency = l1;
  317. if (!link_state->aspm_support)
  318. return;
  319. link_state->aspm_enabled &= link_state->aspm_support;
  320. link_state->aspm_default = link_state->aspm_enabled;
  321. /* ENDPOINT states*/
  322. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  323. int pos;
  324. u32 reg32;
  325. unsigned int latency;
  326. struct endpoint_state *ep_state =
  327. &link_state->endpoints[PCI_FUNC(child_dev->devfn)];
  328. if (child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
  329. child_dev->pcie_type != PCI_EXP_TYPE_LEG_END)
  330. continue;
  331. pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
  332. pci_read_config_dword(child_dev, pos + PCI_EXP_DEVCAP, &reg32);
  333. latency = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
  334. latency = calc_L0S_latency(latency, 1);
  335. ep_state->l0s_acceptable_latency = latency;
  336. if (link_state->aspm_support & PCIE_LINK_STATE_L1) {
  337. latency = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
  338. latency = calc_L1_latency(latency, 1);
  339. ep_state->l1_acceptable_latency = latency;
  340. }
  341. }
  342. }
  343. static unsigned int __pcie_aspm_check_state_one(struct pci_dev *pdev,
  344. unsigned int state)
  345. {
  346. struct pci_dev *parent_dev, *tmp_dev;
  347. unsigned int latency, l1_latency = 0;
  348. struct pcie_link_state *link_state;
  349. struct endpoint_state *ep_state;
  350. parent_dev = pdev->bus->self;
  351. link_state = parent_dev->link_state;
  352. state &= link_state->aspm_support;
  353. if (state == 0)
  354. return 0;
  355. ep_state = &link_state->endpoints[PCI_FUNC(pdev->devfn)];
  356. /*
  357. * Check latency for endpoint device.
  358. * TBD: The latency from the endpoint to root complex vary per
  359. * switch's upstream link state above the device. Here we just do a
  360. * simple check which assumes all links above the device can be in L1
  361. * state, that is we just consider the worst case. If switch's upstream
  362. * link can't be put into L0S/L1, then our check is too strictly.
  363. */
  364. tmp_dev = pdev;
  365. while (state & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1)) {
  366. parent_dev = tmp_dev->bus->self;
  367. link_state = parent_dev->link_state;
  368. if (state & PCIE_LINK_STATE_L0S) {
  369. latency = max_t(unsigned int,
  370. link_state->l0s_upper_latency,
  371. link_state->l0s_down_latency);
  372. if (latency > ep_state->l0s_acceptable_latency)
  373. state &= ~PCIE_LINK_STATE_L0S;
  374. }
  375. if (state & PCIE_LINK_STATE_L1) {
  376. latency = max_t(unsigned int,
  377. link_state->l1_upper_latency,
  378. link_state->l1_down_latency);
  379. if (latency + l1_latency >
  380. ep_state->l1_acceptable_latency)
  381. state &= ~PCIE_LINK_STATE_L1;
  382. }
  383. if (!parent_dev->bus->self) /* parent_dev is a root port */
  384. break;
  385. else {
  386. /*
  387. * parent_dev is the downstream port of a switch, make
  388. * tmp_dev the upstream port of the switch
  389. */
  390. tmp_dev = parent_dev->bus->self;
  391. /*
  392. * every switch on the path to root complex need 1 more
  393. * microsecond for L1. Spec doesn't mention L0S.
  394. */
  395. if (state & PCIE_LINK_STATE_L1)
  396. l1_latency += 1000;
  397. }
  398. }
  399. return state;
  400. }
  401. static unsigned int pcie_aspm_check_state(struct pci_dev *pdev,
  402. unsigned int state)
  403. {
  404. struct pci_dev *child_dev;
  405. /* If no child, ignore the link */
  406. if (list_empty(&pdev->subordinate->devices))
  407. return state;
  408. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  409. if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
  410. /*
  411. * If downstream component of a link is pci bridge, we
  412. * disable ASPM for now for the link
  413. * */
  414. state = 0;
  415. break;
  416. }
  417. if ((child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
  418. child_dev->pcie_type != PCI_EXP_TYPE_LEG_END))
  419. continue;
  420. /* Device not in D0 doesn't need check latency */
  421. if (child_dev->current_state == PCI_D1 ||
  422. child_dev->current_state == PCI_D2 ||
  423. child_dev->current_state == PCI_D3hot ||
  424. child_dev->current_state == PCI_D3cold)
  425. continue;
  426. state = __pcie_aspm_check_state_one(child_dev, state);
  427. }
  428. return state;
  429. }
  430. static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
  431. {
  432. u16 reg16;
  433. int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  434. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  435. reg16 &= ~0x3;
  436. reg16 |= state;
  437. pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
  438. }
  439. static void __pcie_aspm_config_link(struct pci_dev *pdev, unsigned int state)
  440. {
  441. struct pci_dev *child_dev;
  442. int valid = 1;
  443. struct pcie_link_state *link_state = pdev->link_state;
  444. /* If no child, disable the link */
  445. if (list_empty(&pdev->subordinate->devices))
  446. state = 0;
  447. /*
  448. * if the downstream component has pci bridge function, don't do ASPM
  449. * now
  450. */
  451. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  452. if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
  453. valid = 0;
  454. break;
  455. }
  456. }
  457. if (!valid)
  458. return;
  459. /*
  460. * spec 2.0 suggests all functions should be configured the same
  461. * setting for ASPM. Enabling ASPM L1 should be done in upstream
  462. * component first and then downstream, and vice versa for disabling
  463. * ASPM L1. Spec doesn't mention L0S.
  464. */
  465. if (state & PCIE_LINK_STATE_L1)
  466. __pcie_aspm_config_one_dev(pdev, state);
  467. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list)
  468. __pcie_aspm_config_one_dev(child_dev, state);
  469. if (!(state & PCIE_LINK_STATE_L1))
  470. __pcie_aspm_config_one_dev(pdev, state);
  471. link_state->aspm_enabled = state;
  472. }
  473. static struct pcie_link_state *get_root_port_link(struct pcie_link_state *link)
  474. {
  475. struct pcie_link_state *root_port_link = link;
  476. while (root_port_link->parent)
  477. root_port_link = root_port_link->parent;
  478. return root_port_link;
  479. }
  480. /* check the whole hierarchy, and configure each link in the hierarchy */
  481. static void __pcie_aspm_configure_link_state(struct pci_dev *pdev,
  482. unsigned int state)
  483. {
  484. struct pcie_link_state *link_state = pdev->link_state;
  485. struct pcie_link_state *root_port_link = get_root_port_link(link_state);
  486. struct pcie_link_state *leaf;
  487. state &= PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1;
  488. /* check all links who have specific root port link */
  489. list_for_each_entry(leaf, &link_list, sibling) {
  490. if (!list_empty(&leaf->children) ||
  491. get_root_port_link(leaf) != root_port_link)
  492. continue;
  493. state = pcie_aspm_check_state(leaf->pdev, state);
  494. }
  495. /* check root port link too in case it hasn't children */
  496. state = pcie_aspm_check_state(root_port_link->pdev, state);
  497. if (link_state->aspm_enabled == state)
  498. return;
  499. /*
  500. * we must change the hierarchy. See comments in
  501. * __pcie_aspm_config_link for the order
  502. **/
  503. if (state & PCIE_LINK_STATE_L1) {
  504. list_for_each_entry(leaf, &link_list, sibling) {
  505. if (get_root_port_link(leaf) == root_port_link)
  506. __pcie_aspm_config_link(leaf->pdev, state);
  507. }
  508. } else {
  509. list_for_each_entry_reverse(leaf, &link_list, sibling) {
  510. if (get_root_port_link(leaf) == root_port_link)
  511. __pcie_aspm_config_link(leaf->pdev, state);
  512. }
  513. }
  514. }
  515. /*
  516. * pcie_aspm_configure_link_state: enable/disable PCI express link state
  517. * @pdev: the root port or switch downstream port
  518. */
  519. static void pcie_aspm_configure_link_state(struct pci_dev *pdev,
  520. unsigned int state)
  521. {
  522. down_read(&pci_bus_sem);
  523. mutex_lock(&aspm_lock);
  524. __pcie_aspm_configure_link_state(pdev, state);
  525. mutex_unlock(&aspm_lock);
  526. up_read(&pci_bus_sem);
  527. }
  528. static void free_link_state(struct pci_dev *pdev)
  529. {
  530. kfree(pdev->link_state);
  531. pdev->link_state = NULL;
  532. }
  533. static int pcie_aspm_sanity_check(struct pci_dev *pdev)
  534. {
  535. struct pci_dev *child_dev;
  536. int child_pos;
  537. u32 reg32;
  538. /*
  539. * Some functions in a slot might not all be PCIE functions, very
  540. * strange. Disable ASPM for the whole slot
  541. */
  542. list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
  543. child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
  544. if (!child_pos)
  545. return -EINVAL;
  546. /*
  547. * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
  548. * RBER bit to determine if a function is 1.1 version device
  549. */
  550. pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP,
  551. &reg32);
  552. if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
  553. dev_printk(KERN_INFO, &child_dev->dev, "disabling ASPM"
  554. " on pre-1.1 PCIe device. You can enable it"
  555. " with 'pcie_aspm=force'\n");
  556. return -EINVAL;
  557. }
  558. }
  559. return 0;
  560. }
  561. /*
  562. * pcie_aspm_init_link_state: Initiate PCI express link state.
  563. * It is called after the pcie and its children devices are scaned.
  564. * @pdev: the root port or switch downstream port
  565. */
  566. void pcie_aspm_init_link_state(struct pci_dev *pdev)
  567. {
  568. unsigned int state;
  569. struct pcie_link_state *link_state;
  570. int error = 0;
  571. int blacklist;
  572. if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
  573. return;
  574. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  575. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  576. return;
  577. /* VIA has a strange chipset, root port is under a bridge */
  578. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
  579. pdev->bus->self)
  580. return;
  581. down_read(&pci_bus_sem);
  582. if (list_empty(&pdev->subordinate->devices))
  583. goto out;
  584. blacklist = !!pcie_aspm_sanity_check(pdev);
  585. mutex_lock(&aspm_lock);
  586. link_state = kzalloc(sizeof(*link_state), GFP_KERNEL);
  587. if (!link_state)
  588. goto unlock_out;
  589. link_state->downstream_has_switch = pcie_aspm_downstream_has_switch(pdev);
  590. INIT_LIST_HEAD(&link_state->children);
  591. INIT_LIST_HEAD(&link_state->link);
  592. if (pdev->bus->self) {/* this is a switch */
  593. struct pcie_link_state *parent_link_state;
  594. parent_link_state = pdev->bus->parent->self->link_state;
  595. if (!parent_link_state) {
  596. kfree(link_state);
  597. goto unlock_out;
  598. }
  599. list_add(&link_state->link, &parent_link_state->children);
  600. link_state->parent = parent_link_state;
  601. }
  602. pdev->link_state = link_state;
  603. if (!blacklist) {
  604. pcie_aspm_configure_common_clock(pdev);
  605. pcie_aspm_cap_init(pdev);
  606. } else {
  607. link_state->aspm_enabled =
  608. (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
  609. link_state->aspm_default = 0;
  610. /* Set support state to 0, so we will disable ASPM later */
  611. link_state->aspm_support = 0;
  612. }
  613. link_state->pdev = pdev;
  614. list_add(&link_state->sibling, &link_list);
  615. if (link_state->downstream_has_switch) {
  616. /*
  617. * If link has switch, delay the link config. The leaf link
  618. * initialization will config the whole hierarchy. but we must
  619. * make sure BIOS doesn't set unsupported link state
  620. **/
  621. state = pcie_aspm_check_state(pdev, link_state->aspm_default);
  622. __pcie_aspm_config_link(pdev, state);
  623. } else
  624. __pcie_aspm_configure_link_state(pdev,
  625. policy_to_aspm_state(pdev));
  626. pcie_check_clock_pm(pdev, blacklist);
  627. unlock_out:
  628. if (error)
  629. free_link_state(pdev);
  630. mutex_unlock(&aspm_lock);
  631. out:
  632. up_read(&pci_bus_sem);
  633. }
  634. /* @pdev: the endpoint device */
  635. void pcie_aspm_exit_link_state(struct pci_dev *pdev)
  636. {
  637. struct pci_dev *parent = pdev->bus->self;
  638. struct pcie_link_state *link_state = parent->link_state;
  639. if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
  640. return;
  641. if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  642. parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  643. return;
  644. down_read(&pci_bus_sem);
  645. mutex_lock(&aspm_lock);
  646. /*
  647. * All PCIe functions are in one slot, remove one function will remove
  648. * the whole slot, so just wait until we are the last function left.
  649. */
  650. if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
  651. goto out;
  652. /* All functions are removed, so just disable ASPM for the link */
  653. __pcie_aspm_config_one_dev(parent, 0);
  654. list_del(&link_state->sibling);
  655. list_del(&link_state->link);
  656. /* Clock PM is for endpoint device */
  657. free_link_state(parent);
  658. out:
  659. mutex_unlock(&aspm_lock);
  660. up_read(&pci_bus_sem);
  661. }
  662. /* @pdev: the root port or switch downstream port */
  663. void pcie_aspm_pm_state_change(struct pci_dev *pdev)
  664. {
  665. struct pcie_link_state *link_state = pdev->link_state;
  666. if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
  667. return;
  668. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  669. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  670. return;
  671. /*
  672. * devices changed PM state, we should recheck if latency meets all
  673. * functions' requirement
  674. */
  675. pcie_aspm_configure_link_state(pdev, link_state->aspm_enabled);
  676. }
  677. /*
  678. * pci_disable_link_state - disable pci device's link state, so the link will
  679. * never enter specific states
  680. */
  681. void pci_disable_link_state(struct pci_dev *pdev, int state)
  682. {
  683. struct pci_dev *parent = pdev->bus->self;
  684. struct pcie_link_state *link_state;
  685. if (aspm_disabled || !pdev->is_pcie)
  686. return;
  687. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
  688. pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
  689. parent = pdev;
  690. if (!parent || !parent->link_state)
  691. return;
  692. down_read(&pci_bus_sem);
  693. mutex_lock(&aspm_lock);
  694. link_state = parent->link_state;
  695. link_state->aspm_support &= ~state;
  696. if (state & PCIE_LINK_STATE_CLKPM)
  697. link_state->clk_pm_capable = 0;
  698. __pcie_aspm_configure_link_state(parent, link_state->aspm_enabled);
  699. if (!link_state->clk_pm_capable && link_state->clk_pm_enabled)
  700. pcie_set_clock_pm(parent, 0);
  701. mutex_unlock(&aspm_lock);
  702. up_read(&pci_bus_sem);
  703. }
  704. EXPORT_SYMBOL(pci_disable_link_state);
  705. static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
  706. {
  707. int i;
  708. struct pci_dev *pdev;
  709. struct pcie_link_state *link_state;
  710. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  711. if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
  712. break;
  713. if (i >= ARRAY_SIZE(policy_str))
  714. return -EINVAL;
  715. if (i == aspm_policy)
  716. return 0;
  717. down_read(&pci_bus_sem);
  718. mutex_lock(&aspm_lock);
  719. aspm_policy = i;
  720. list_for_each_entry(link_state, &link_list, sibling) {
  721. pdev = link_state->pdev;
  722. __pcie_aspm_configure_link_state(pdev,
  723. policy_to_aspm_state(pdev));
  724. if (link_state->clk_pm_capable &&
  725. link_state->clk_pm_enabled != policy_to_clkpm_state(pdev))
  726. pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
  727. }
  728. mutex_unlock(&aspm_lock);
  729. up_read(&pci_bus_sem);
  730. return 0;
  731. }
  732. static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
  733. {
  734. int i, cnt = 0;
  735. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  736. if (i == aspm_policy)
  737. cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
  738. else
  739. cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
  740. return cnt;
  741. }
  742. module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
  743. NULL, 0644);
  744. #ifdef CONFIG_PCIEASPM_DEBUG
  745. static ssize_t link_state_show(struct device *dev,
  746. struct device_attribute *attr,
  747. char *buf)
  748. {
  749. struct pci_dev *pci_device = to_pci_dev(dev);
  750. struct pcie_link_state *link_state = pci_device->link_state;
  751. return sprintf(buf, "%d\n", link_state->aspm_enabled);
  752. }
  753. static ssize_t link_state_store(struct device *dev,
  754. struct device_attribute *attr,
  755. const char *buf,
  756. size_t n)
  757. {
  758. struct pci_dev *pci_device = to_pci_dev(dev);
  759. int state;
  760. if (n < 1)
  761. return -EINVAL;
  762. state = buf[0]-'0';
  763. if (state >= 0 && state <= 3) {
  764. /* setup link aspm state */
  765. pcie_aspm_configure_link_state(pci_device, state);
  766. return n;
  767. }
  768. return -EINVAL;
  769. }
  770. static ssize_t clk_ctl_show(struct device *dev,
  771. struct device_attribute *attr,
  772. char *buf)
  773. {
  774. struct pci_dev *pci_device = to_pci_dev(dev);
  775. struct pcie_link_state *link_state = pci_device->link_state;
  776. return sprintf(buf, "%d\n", link_state->clk_pm_enabled);
  777. }
  778. static ssize_t clk_ctl_store(struct device *dev,
  779. struct device_attribute *attr,
  780. const char *buf,
  781. size_t n)
  782. {
  783. struct pci_dev *pci_device = to_pci_dev(dev);
  784. int state;
  785. if (n < 1)
  786. return -EINVAL;
  787. state = buf[0]-'0';
  788. down_read(&pci_bus_sem);
  789. mutex_lock(&aspm_lock);
  790. pcie_set_clock_pm(pci_device, !!state);
  791. mutex_unlock(&aspm_lock);
  792. up_read(&pci_bus_sem);
  793. return n;
  794. }
  795. static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
  796. static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
  797. static char power_group[] = "power";
  798. void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
  799. {
  800. struct pcie_link_state *link_state = pdev->link_state;
  801. if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  802. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  803. return;
  804. if (link_state->aspm_support)
  805. sysfs_add_file_to_group(&pdev->dev.kobj,
  806. &dev_attr_link_state.attr, power_group);
  807. if (link_state->clk_pm_capable)
  808. sysfs_add_file_to_group(&pdev->dev.kobj,
  809. &dev_attr_clk_ctl.attr, power_group);
  810. }
  811. void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
  812. {
  813. struct pcie_link_state *link_state = pdev->link_state;
  814. if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  815. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  816. return;
  817. if (link_state->aspm_support)
  818. sysfs_remove_file_from_group(&pdev->dev.kobj,
  819. &dev_attr_link_state.attr, power_group);
  820. if (link_state->clk_pm_capable)
  821. sysfs_remove_file_from_group(&pdev->dev.kobj,
  822. &dev_attr_clk_ctl.attr, power_group);
  823. }
  824. #endif
  825. static int __init pcie_aspm_disable(char *str)
  826. {
  827. if (!strcmp(str, "off")) {
  828. aspm_disabled = 1;
  829. printk(KERN_INFO "PCIe ASPM is disabled\n");
  830. } else if (!strcmp(str, "force")) {
  831. aspm_force = 1;
  832. printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
  833. }
  834. return 1;
  835. }
  836. __setup("pcie_aspm=", pcie_aspm_disable);
  837. void pcie_no_aspm(void)
  838. {
  839. if (!aspm_force)
  840. aspm_disabled = 1;
  841. }
  842. /**
  843. * pcie_aspm_enabled - is PCIe ASPM enabled?
  844. *
  845. * Returns true if ASPM has not been disabled by the command-line option
  846. * pcie_aspm=off.
  847. **/
  848. int pcie_aspm_enabled(void)
  849. {
  850. return !aspm_disabled;
  851. }
  852. EXPORT_SYMBOL(pcie_aspm_enabled);