aspm.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. /* Note: those are not register definitions */
  27. #define ASPM_STATE_L0S_UP (1) /* Upstream direction L0s state */
  28. #define ASPM_STATE_L0S_DW (2) /* Downstream direction L0s state */
  29. #define ASPM_STATE_L1 (4) /* L1 state */
  30. #define ASPM_STATE_L0S (ASPM_STATE_L0S_UP | ASPM_STATE_L0S_DW)
  31. #define ASPM_STATE_ALL (ASPM_STATE_L0S | ASPM_STATE_L1)
  32. struct aspm_latency {
  33. u32 l0s; /* L0s latency (nsec) */
  34. u32 l1; /* L1 latency (nsec) */
  35. };
  36. struct pcie_link_state {
  37. struct pci_dev *pdev; /* Upstream component of the Link */
  38. struct pcie_link_state *root; /* pointer to the root port link */
  39. struct pcie_link_state *parent; /* pointer to the parent Link state */
  40. struct list_head sibling; /* node in link_list */
  41. struct list_head children; /* list of child link states */
  42. struct list_head link; /* node in parent's children list */
  43. /* ASPM state */
  44. u32 aspm_support:3; /* Supported ASPM state */
  45. u32 aspm_enabled:3; /* Enabled ASPM state */
  46. u32 aspm_capable:3; /* Capable ASPM state with latency */
  47. u32 aspm_default:3; /* Default ASPM state by BIOS */
  48. u32 aspm_disable:3; /* Disabled ASPM state */
  49. /* Clock PM state */
  50. u32 clkpm_capable:1; /* Clock PM capable? */
  51. u32 clkpm_enabled:1; /* Current Clock PM state */
  52. u32 clkpm_default:1; /* Default Clock PM state by BIOS */
  53. /* Exit latencies */
  54. struct aspm_latency latency_up; /* Upstream direction exit latency */
  55. struct aspm_latency latency_dw; /* Downstream direction exit latency */
  56. /*
  57. * Endpoint acceptable latencies. A pcie downstream port only
  58. * has one slot under it, so at most there are 8 functions.
  59. */
  60. struct aspm_latency acceptable[8];
  61. };
  62. static int aspm_disabled, aspm_force;
  63. static bool aspm_support_enabled = true;
  64. static DEFINE_MUTEX(aspm_lock);
  65. static LIST_HEAD(link_list);
  66. #define POLICY_DEFAULT 0 /* BIOS default setting */
  67. #define POLICY_PERFORMANCE 1 /* high performance */
  68. #define POLICY_POWERSAVE 2 /* high power saving */
  69. #ifdef CONFIG_PCIEASPM_PERFORMANCE
  70. static int aspm_policy = POLICY_PERFORMANCE;
  71. #elif defined CONFIG_PCIEASPM_POWERSAVE
  72. static int aspm_policy = POLICY_POWERSAVE;
  73. #else
  74. static int aspm_policy;
  75. #endif
  76. static const char *policy_str[] = {
  77. [POLICY_DEFAULT] = "default",
  78. [POLICY_PERFORMANCE] = "performance",
  79. [POLICY_POWERSAVE] = "powersave"
  80. };
  81. #define LINK_RETRAIN_TIMEOUT HZ
  82. static int policy_to_aspm_state(struct pcie_link_state *link)
  83. {
  84. switch (aspm_policy) {
  85. case POLICY_PERFORMANCE:
  86. /* Disable ASPM and Clock PM */
  87. return 0;
  88. case POLICY_POWERSAVE:
  89. /* Enable ASPM L0s/L1 */
  90. return ASPM_STATE_ALL;
  91. case POLICY_DEFAULT:
  92. return link->aspm_default;
  93. }
  94. return 0;
  95. }
  96. static int policy_to_clkpm_state(struct pcie_link_state *link)
  97. {
  98. switch (aspm_policy) {
  99. case POLICY_PERFORMANCE:
  100. /* Disable ASPM and Clock PM */
  101. return 0;
  102. case POLICY_POWERSAVE:
  103. /* Disable Clock PM */
  104. return 1;
  105. case POLICY_DEFAULT:
  106. return link->clkpm_default;
  107. }
  108. return 0;
  109. }
  110. static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
  111. {
  112. int pos;
  113. u16 reg16;
  114. struct pci_dev *child;
  115. struct pci_bus *linkbus = link->pdev->subordinate;
  116. list_for_each_entry(child, &linkbus->devices, bus_list) {
  117. pos = pci_pcie_cap(child);
  118. if (!pos)
  119. return;
  120. pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
  121. if (enable)
  122. reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
  123. else
  124. reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  125. pci_write_config_word(child, pos + PCI_EXP_LNKCTL, reg16);
  126. }
  127. link->clkpm_enabled = !!enable;
  128. }
  129. static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
  130. {
  131. /* Don't enable Clock PM if the link is not Clock PM capable */
  132. if (!link->clkpm_capable && enable)
  133. enable = 0;
  134. /* Need nothing if the specified equals to current state */
  135. if (link->clkpm_enabled == enable)
  136. return;
  137. pcie_set_clkpm_nocheck(link, enable);
  138. }
  139. static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
  140. {
  141. int pos, capable = 1, enabled = 1;
  142. u32 reg32;
  143. u16 reg16;
  144. struct pci_dev *child;
  145. struct pci_bus *linkbus = link->pdev->subordinate;
  146. /* All functions should have the same cap and state, take the worst */
  147. list_for_each_entry(child, &linkbus->devices, bus_list) {
  148. pos = pci_pcie_cap(child);
  149. if (!pos)
  150. return;
  151. pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, &reg32);
  152. if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
  153. capable = 0;
  154. enabled = 0;
  155. break;
  156. }
  157. pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
  158. if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
  159. enabled = 0;
  160. }
  161. link->clkpm_enabled = enabled;
  162. link->clkpm_default = enabled;
  163. link->clkpm_capable = (blacklist) ? 0 : capable;
  164. }
  165. /*
  166. * pcie_aspm_configure_common_clock: check if the 2 ends of a link
  167. * could use common clock. If they are, configure them to use the
  168. * common clock. That will reduce the ASPM state exit latency.
  169. */
  170. static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
  171. {
  172. int ppos, cpos, same_clock = 1;
  173. u16 reg16, parent_reg, child_reg[8];
  174. unsigned long start_jiffies;
  175. struct pci_dev *child, *parent = link->pdev;
  176. struct pci_bus *linkbus = parent->subordinate;
  177. /*
  178. * All functions of a slot should have the same Slot Clock
  179. * Configuration, so just check one function
  180. */
  181. child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
  182. BUG_ON(!pci_is_pcie(child));
  183. /* Check downstream component if bit Slot Clock Configuration is 1 */
  184. cpos = pci_pcie_cap(child);
  185. pci_read_config_word(child, cpos + PCI_EXP_LNKSTA, &reg16);
  186. if (!(reg16 & PCI_EXP_LNKSTA_SLC))
  187. same_clock = 0;
  188. /* Check upstream component if bit Slot Clock Configuration is 1 */
  189. ppos = pci_pcie_cap(parent);
  190. pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
  191. if (!(reg16 & PCI_EXP_LNKSTA_SLC))
  192. same_clock = 0;
  193. /* Configure downstream component, all functions */
  194. list_for_each_entry(child, &linkbus->devices, bus_list) {
  195. cpos = pci_pcie_cap(child);
  196. pci_read_config_word(child, cpos + PCI_EXP_LNKCTL, &reg16);
  197. child_reg[PCI_FUNC(child->devfn)] = reg16;
  198. if (same_clock)
  199. reg16 |= PCI_EXP_LNKCTL_CCC;
  200. else
  201. reg16 &= ~PCI_EXP_LNKCTL_CCC;
  202. pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, reg16);
  203. }
  204. /* Configure upstream component */
  205. pci_read_config_word(parent, ppos + PCI_EXP_LNKCTL, &reg16);
  206. parent_reg = reg16;
  207. if (same_clock)
  208. reg16 |= PCI_EXP_LNKCTL_CCC;
  209. else
  210. reg16 &= ~PCI_EXP_LNKCTL_CCC;
  211. pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
  212. /* Retrain link */
  213. reg16 |= PCI_EXP_LNKCTL_RL;
  214. pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
  215. /* Wait for link training end. Break out after waiting for timeout */
  216. start_jiffies = jiffies;
  217. for (;;) {
  218. pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
  219. if (!(reg16 & PCI_EXP_LNKSTA_LT))
  220. break;
  221. if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
  222. break;
  223. msleep(1);
  224. }
  225. if (!(reg16 & PCI_EXP_LNKSTA_LT))
  226. return;
  227. /* Training failed. Restore common clock configurations */
  228. dev_printk(KERN_ERR, &parent->dev,
  229. "ASPM: Could not configure common clock\n");
  230. list_for_each_entry(child, &linkbus->devices, bus_list) {
  231. cpos = pci_pcie_cap(child);
  232. pci_write_config_word(child, cpos + PCI_EXP_LNKCTL,
  233. child_reg[PCI_FUNC(child->devfn)]);
  234. }
  235. pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, parent_reg);
  236. }
  237. /* Convert L0s latency encoding to ns */
  238. static u32 calc_l0s_latency(u32 encoding)
  239. {
  240. if (encoding == 0x7)
  241. return (5 * 1000); /* > 4us */
  242. return (64 << encoding);
  243. }
  244. /* Convert L0s acceptable latency encoding to ns */
  245. static u32 calc_l0s_acceptable(u32 encoding)
  246. {
  247. if (encoding == 0x7)
  248. return -1U;
  249. return (64 << encoding);
  250. }
  251. /* Convert L1 latency encoding to ns */
  252. static u32 calc_l1_latency(u32 encoding)
  253. {
  254. if (encoding == 0x7)
  255. return (65 * 1000); /* > 64us */
  256. return (1000 << encoding);
  257. }
  258. /* Convert L1 acceptable latency encoding to ns */
  259. static u32 calc_l1_acceptable(u32 encoding)
  260. {
  261. if (encoding == 0x7)
  262. return -1U;
  263. return (1000 << encoding);
  264. }
  265. struct aspm_register_info {
  266. u32 support:2;
  267. u32 enabled:2;
  268. u32 latency_encoding_l0s;
  269. u32 latency_encoding_l1;
  270. };
  271. static void pcie_get_aspm_reg(struct pci_dev *pdev,
  272. struct aspm_register_info *info)
  273. {
  274. int pos;
  275. u16 reg16;
  276. u32 reg32;
  277. pos = pci_pcie_cap(pdev);
  278. pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
  279. info->support = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
  280. info->latency_encoding_l0s = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
  281. info->latency_encoding_l1 = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
  282. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  283. info->enabled = reg16 & PCI_EXP_LNKCTL_ASPMC;
  284. }
  285. static void pcie_aspm_check_latency(struct pci_dev *endpoint)
  286. {
  287. u32 latency, l1_switch_latency = 0;
  288. struct aspm_latency *acceptable;
  289. struct pcie_link_state *link;
  290. /* Device not in D0 doesn't need latency check */
  291. if ((endpoint->current_state != PCI_D0) &&
  292. (endpoint->current_state != PCI_UNKNOWN))
  293. return;
  294. link = endpoint->bus->self->link_state;
  295. acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
  296. while (link) {
  297. /* Check upstream direction L0s latency */
  298. if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
  299. (link->latency_up.l0s > acceptable->l0s))
  300. link->aspm_capable &= ~ASPM_STATE_L0S_UP;
  301. /* Check downstream direction L0s latency */
  302. if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
  303. (link->latency_dw.l0s > acceptable->l0s))
  304. link->aspm_capable &= ~ASPM_STATE_L0S_DW;
  305. /*
  306. * Check L1 latency.
  307. * Every switch on the path to root complex need 1
  308. * more microsecond for L1. Spec doesn't mention L0s.
  309. */
  310. latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1);
  311. if ((link->aspm_capable & ASPM_STATE_L1) &&
  312. (latency + l1_switch_latency > acceptable->l1))
  313. link->aspm_capable &= ~ASPM_STATE_L1;
  314. l1_switch_latency += 1000;
  315. link = link->parent;
  316. }
  317. }
  318. static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
  319. {
  320. struct pci_dev *child, *parent = link->pdev;
  321. struct pci_bus *linkbus = parent->subordinate;
  322. struct aspm_register_info upreg, dwreg;
  323. if (blacklist) {
  324. /* Set enabled/disable so that we will disable ASPM later */
  325. link->aspm_enabled = ASPM_STATE_ALL;
  326. link->aspm_disable = ASPM_STATE_ALL;
  327. return;
  328. }
  329. /* Configure common clock before checking latencies */
  330. pcie_aspm_configure_common_clock(link);
  331. /* Get upstream/downstream components' register state */
  332. pcie_get_aspm_reg(parent, &upreg);
  333. child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
  334. pcie_get_aspm_reg(child, &dwreg);
  335. /*
  336. * Setup L0s state
  337. *
  338. * Note that we must not enable L0s in either direction on a
  339. * given link unless components on both sides of the link each
  340. * support L0s.
  341. */
  342. if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S)
  343. link->aspm_support |= ASPM_STATE_L0S;
  344. if (dwreg.enabled & PCIE_LINK_STATE_L0S)
  345. link->aspm_enabled |= ASPM_STATE_L0S_UP;
  346. if (upreg.enabled & PCIE_LINK_STATE_L0S)
  347. link->aspm_enabled |= ASPM_STATE_L0S_DW;
  348. link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s);
  349. link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s);
  350. /* Setup L1 state */
  351. if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1)
  352. link->aspm_support |= ASPM_STATE_L1;
  353. if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1)
  354. link->aspm_enabled |= ASPM_STATE_L1;
  355. link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1);
  356. link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1);
  357. /* Save default state */
  358. link->aspm_default = link->aspm_enabled;
  359. /* Setup initial capable state. Will be updated later */
  360. link->aspm_capable = link->aspm_support;
  361. /*
  362. * If the downstream component has pci bridge function, don't
  363. * do ASPM for now.
  364. */
  365. list_for_each_entry(child, &linkbus->devices, bus_list) {
  366. if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
  367. link->aspm_disable = ASPM_STATE_ALL;
  368. break;
  369. }
  370. }
  371. /* Get and check endpoint acceptable latencies */
  372. list_for_each_entry(child, &linkbus->devices, bus_list) {
  373. int pos;
  374. u32 reg32, encoding;
  375. struct aspm_latency *acceptable =
  376. &link->acceptable[PCI_FUNC(child->devfn)];
  377. if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
  378. child->pcie_type != PCI_EXP_TYPE_LEG_END)
  379. continue;
  380. pos = pci_pcie_cap(child);
  381. pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
  382. /* Calculate endpoint L0s acceptable latency */
  383. encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
  384. acceptable->l0s = calc_l0s_acceptable(encoding);
  385. /* Calculate endpoint L1 acceptable latency */
  386. encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
  387. acceptable->l1 = calc_l1_acceptable(encoding);
  388. pcie_aspm_check_latency(child);
  389. }
  390. }
  391. static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
  392. {
  393. u16 reg16;
  394. int pos = pci_pcie_cap(pdev);
  395. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  396. reg16 &= ~0x3;
  397. reg16 |= val;
  398. pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
  399. }
  400. static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
  401. {
  402. u32 upstream = 0, dwstream = 0;
  403. struct pci_dev *child, *parent = link->pdev;
  404. struct pci_bus *linkbus = parent->subordinate;
  405. /* Nothing to do if the link is already in the requested state */
  406. state &= (link->aspm_capable & ~link->aspm_disable);
  407. if (link->aspm_enabled == state)
  408. return;
  409. /* Convert ASPM state to upstream/downstream ASPM register state */
  410. if (state & ASPM_STATE_L0S_UP)
  411. dwstream |= PCIE_LINK_STATE_L0S;
  412. if (state & ASPM_STATE_L0S_DW)
  413. upstream |= PCIE_LINK_STATE_L0S;
  414. if (state & ASPM_STATE_L1) {
  415. upstream |= PCIE_LINK_STATE_L1;
  416. dwstream |= PCIE_LINK_STATE_L1;
  417. }
  418. /*
  419. * Spec 2.0 suggests all functions should be configured the
  420. * same setting for ASPM. Enabling ASPM L1 should be done in
  421. * upstream component first and then downstream, and vice
  422. * versa for disabling ASPM L1. Spec doesn't mention L0S.
  423. */
  424. if (state & ASPM_STATE_L1)
  425. pcie_config_aspm_dev(parent, upstream);
  426. list_for_each_entry(child, &linkbus->devices, bus_list)
  427. pcie_config_aspm_dev(child, dwstream);
  428. if (!(state & ASPM_STATE_L1))
  429. pcie_config_aspm_dev(parent, upstream);
  430. link->aspm_enabled = state;
  431. }
  432. static void pcie_config_aspm_path(struct pcie_link_state *link)
  433. {
  434. while (link) {
  435. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  436. link = link->parent;
  437. }
  438. }
  439. static void free_link_state(struct pcie_link_state *link)
  440. {
  441. link->pdev->link_state = NULL;
  442. kfree(link);
  443. }
  444. static int pcie_aspm_sanity_check(struct pci_dev *pdev)
  445. {
  446. struct pci_dev *child;
  447. int pos;
  448. u32 reg32;
  449. /*
  450. * Some functions in a slot might not all be PCIe functions,
  451. * very strange. Disable ASPM for the whole slot
  452. */
  453. list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
  454. pos = pci_pcie_cap(child);
  455. if (!pos)
  456. return -EINVAL;
  457. /*
  458. * If ASPM is disabled then we're not going to change
  459. * the BIOS state. It's safe to continue even if it's a
  460. * pre-1.1 device
  461. */
  462. if (aspm_disabled)
  463. continue;
  464. /*
  465. * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
  466. * RBER bit to determine if a function is 1.1 version device
  467. */
  468. pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
  469. if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
  470. dev_printk(KERN_INFO, &child->dev, "disabling ASPM"
  471. " on pre-1.1 PCIe device. You can enable it"
  472. " with 'pcie_aspm=force'\n");
  473. return -EINVAL;
  474. }
  475. }
  476. return 0;
  477. }
  478. static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
  479. {
  480. struct pcie_link_state *link;
  481. link = kzalloc(sizeof(*link), GFP_KERNEL);
  482. if (!link)
  483. return NULL;
  484. INIT_LIST_HEAD(&link->sibling);
  485. INIT_LIST_HEAD(&link->children);
  486. INIT_LIST_HEAD(&link->link);
  487. link->pdev = pdev;
  488. if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
  489. struct pcie_link_state *parent;
  490. parent = pdev->bus->parent->self->link_state;
  491. if (!parent) {
  492. kfree(link);
  493. return NULL;
  494. }
  495. link->parent = parent;
  496. list_add(&link->link, &parent->children);
  497. }
  498. /* Setup a pointer to the root port link */
  499. if (!link->parent)
  500. link->root = link;
  501. else
  502. link->root = link->parent->root;
  503. list_add(&link->sibling, &link_list);
  504. pdev->link_state = link;
  505. return link;
  506. }
  507. /*
  508. * pcie_aspm_init_link_state: Initiate PCI express link state.
  509. * It is called after the pcie and its children devices are scaned.
  510. * @pdev: the root port or switch downstream port
  511. */
  512. void pcie_aspm_init_link_state(struct pci_dev *pdev)
  513. {
  514. struct pcie_link_state *link;
  515. int blacklist = !!pcie_aspm_sanity_check(pdev);
  516. if (!aspm_support_enabled)
  517. return;
  518. if (!pci_is_pcie(pdev) || pdev->link_state)
  519. return;
  520. if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  521. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
  522. return;
  523. /* VIA has a strange chipset, root port is under a bridge */
  524. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
  525. pdev->bus->self)
  526. return;
  527. down_read(&pci_bus_sem);
  528. if (list_empty(&pdev->subordinate->devices))
  529. goto out;
  530. mutex_lock(&aspm_lock);
  531. link = alloc_pcie_link_state(pdev);
  532. if (!link)
  533. goto unlock;
  534. /*
  535. * Setup initial ASPM state. Note that we need to configure
  536. * upstream links also because capable state of them can be
  537. * update through pcie_aspm_cap_init().
  538. */
  539. pcie_aspm_cap_init(link, blacklist);
  540. /* Setup initial Clock PM state */
  541. pcie_clkpm_cap_init(link, blacklist);
  542. /*
  543. * At this stage drivers haven't had an opportunity to change the
  544. * link policy setting. Enabling ASPM on broken hardware can cripple
  545. * it even before the driver has had a chance to disable ASPM, so
  546. * default to a safe level right now. If we're enabling ASPM beyond
  547. * the BIOS's expectation, we'll do so once pci_enable_device() is
  548. * called.
  549. */
  550. if (aspm_policy != POLICY_POWERSAVE) {
  551. pcie_config_aspm_path(link);
  552. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  553. }
  554. unlock:
  555. mutex_unlock(&aspm_lock);
  556. out:
  557. up_read(&pci_bus_sem);
  558. }
  559. /* Recheck latencies and update aspm_capable for links under the root */
  560. static void pcie_update_aspm_capable(struct pcie_link_state *root)
  561. {
  562. struct pcie_link_state *link;
  563. BUG_ON(root->parent);
  564. list_for_each_entry(link, &link_list, sibling) {
  565. if (link->root != root)
  566. continue;
  567. link->aspm_capable = link->aspm_support;
  568. }
  569. list_for_each_entry(link, &link_list, sibling) {
  570. struct pci_dev *child;
  571. struct pci_bus *linkbus = link->pdev->subordinate;
  572. if (link->root != root)
  573. continue;
  574. list_for_each_entry(child, &linkbus->devices, bus_list) {
  575. if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT) &&
  576. (child->pcie_type != PCI_EXP_TYPE_LEG_END))
  577. continue;
  578. pcie_aspm_check_latency(child);
  579. }
  580. }
  581. }
  582. /* @pdev: the endpoint device */
  583. void pcie_aspm_exit_link_state(struct pci_dev *pdev)
  584. {
  585. struct pci_dev *parent = pdev->bus->self;
  586. struct pcie_link_state *link, *root, *parent_link;
  587. if (!pci_is_pcie(pdev) || !parent || !parent->link_state)
  588. return;
  589. if ((parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT) &&
  590. (parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM))
  591. return;
  592. down_read(&pci_bus_sem);
  593. mutex_lock(&aspm_lock);
  594. /*
  595. * All PCIe functions are in one slot, remove one function will remove
  596. * the whole slot, so just wait until we are the last function left.
  597. */
  598. if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
  599. goto out;
  600. link = parent->link_state;
  601. root = link->root;
  602. parent_link = link->parent;
  603. /* All functions are removed, so just disable ASPM for the link */
  604. pcie_config_aspm_link(link, 0);
  605. list_del(&link->sibling);
  606. list_del(&link->link);
  607. /* Clock PM is for endpoint device */
  608. free_link_state(link);
  609. /* Recheck latencies and configure upstream links */
  610. if (parent_link) {
  611. pcie_update_aspm_capable(root);
  612. pcie_config_aspm_path(parent_link);
  613. }
  614. out:
  615. mutex_unlock(&aspm_lock);
  616. up_read(&pci_bus_sem);
  617. }
  618. /* @pdev: the root port or switch downstream port */
  619. void pcie_aspm_pm_state_change(struct pci_dev *pdev)
  620. {
  621. struct pcie_link_state *link = pdev->link_state;
  622. if (aspm_disabled || !pci_is_pcie(pdev) || !link)
  623. return;
  624. if ((pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT) &&
  625. (pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM))
  626. return;
  627. /*
  628. * Devices changed PM state, we should recheck if latency
  629. * meets all functions' requirement
  630. */
  631. down_read(&pci_bus_sem);
  632. mutex_lock(&aspm_lock);
  633. pcie_update_aspm_capable(link->root);
  634. pcie_config_aspm_path(link);
  635. mutex_unlock(&aspm_lock);
  636. up_read(&pci_bus_sem);
  637. }
  638. void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
  639. {
  640. struct pcie_link_state *link = pdev->link_state;
  641. if (aspm_disabled || !pci_is_pcie(pdev) || !link)
  642. return;
  643. if (aspm_policy != POLICY_POWERSAVE)
  644. return;
  645. if ((pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT) &&
  646. (pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM))
  647. return;
  648. down_read(&pci_bus_sem);
  649. mutex_lock(&aspm_lock);
  650. pcie_config_aspm_path(link);
  651. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  652. mutex_unlock(&aspm_lock);
  653. up_read(&pci_bus_sem);
  654. }
  655. /*
  656. * pci_disable_link_state - disable pci device's link state, so the link will
  657. * never enter specific states
  658. */
  659. static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem,
  660. bool force)
  661. {
  662. struct pci_dev *parent = pdev->bus->self;
  663. struct pcie_link_state *link;
  664. if (aspm_disabled && !force)
  665. return;
  666. if (!pci_is_pcie(pdev))
  667. return;
  668. if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
  669. pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
  670. parent = pdev;
  671. if (!parent || !parent->link_state)
  672. return;
  673. if (sem)
  674. down_read(&pci_bus_sem);
  675. mutex_lock(&aspm_lock);
  676. link = parent->link_state;
  677. if (state & PCIE_LINK_STATE_L0S)
  678. link->aspm_disable |= ASPM_STATE_L0S;
  679. if (state & PCIE_LINK_STATE_L1)
  680. link->aspm_disable |= ASPM_STATE_L1;
  681. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  682. if (state & PCIE_LINK_STATE_CLKPM) {
  683. link->clkpm_capable = 0;
  684. pcie_set_clkpm(link, 0);
  685. }
  686. mutex_unlock(&aspm_lock);
  687. if (sem)
  688. up_read(&pci_bus_sem);
  689. }
  690. void pci_disable_link_state_locked(struct pci_dev *pdev, int state)
  691. {
  692. __pci_disable_link_state(pdev, state, false, false);
  693. }
  694. EXPORT_SYMBOL(pci_disable_link_state_locked);
  695. void pci_disable_link_state(struct pci_dev *pdev, int state)
  696. {
  697. __pci_disable_link_state(pdev, state, true, false);
  698. }
  699. EXPORT_SYMBOL(pci_disable_link_state);
  700. void pcie_clear_aspm(struct pci_bus *bus)
  701. {
  702. struct pci_dev *child;
  703. if (aspm_force)
  704. return;
  705. /*
  706. * Clear any ASPM setup that the firmware has carried out on this bus
  707. */
  708. list_for_each_entry(child, &bus->devices, bus_list) {
  709. __pci_disable_link_state(child, PCIE_LINK_STATE_L0S |
  710. PCIE_LINK_STATE_L1 |
  711. PCIE_LINK_STATE_CLKPM,
  712. false, true);
  713. }
  714. }
  715. static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
  716. {
  717. int i;
  718. struct pcie_link_state *link;
  719. if (aspm_disabled)
  720. return -EPERM;
  721. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  722. if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
  723. break;
  724. if (i >= ARRAY_SIZE(policy_str))
  725. return -EINVAL;
  726. if (i == aspm_policy)
  727. return 0;
  728. down_read(&pci_bus_sem);
  729. mutex_lock(&aspm_lock);
  730. aspm_policy = i;
  731. list_for_each_entry(link, &link_list, sibling) {
  732. pcie_config_aspm_link(link, policy_to_aspm_state(link));
  733. pcie_set_clkpm(link, policy_to_clkpm_state(link));
  734. }
  735. mutex_unlock(&aspm_lock);
  736. up_read(&pci_bus_sem);
  737. return 0;
  738. }
  739. static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
  740. {
  741. int i, cnt = 0;
  742. for (i = 0; i < ARRAY_SIZE(policy_str); i++)
  743. if (i == aspm_policy)
  744. cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
  745. else
  746. cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
  747. return cnt;
  748. }
  749. module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
  750. NULL, 0644);
  751. #ifdef CONFIG_PCIEASPM_DEBUG
  752. static ssize_t link_state_show(struct device *dev,
  753. struct device_attribute *attr,
  754. char *buf)
  755. {
  756. struct pci_dev *pci_device = to_pci_dev(dev);
  757. struct pcie_link_state *link_state = pci_device->link_state;
  758. return sprintf(buf, "%d\n", link_state->aspm_enabled);
  759. }
  760. static ssize_t link_state_store(struct device *dev,
  761. struct device_attribute *attr,
  762. const char *buf,
  763. size_t n)
  764. {
  765. struct pci_dev *pdev = to_pci_dev(dev);
  766. struct pcie_link_state *link, *root = pdev->link_state->root;
  767. u32 val = buf[0] - '0', state = 0;
  768. if (aspm_disabled)
  769. return -EPERM;
  770. if (n < 1 || val > 3)
  771. return -EINVAL;
  772. /* Convert requested state to ASPM state */
  773. if (val & PCIE_LINK_STATE_L0S)
  774. state |= ASPM_STATE_L0S;
  775. if (val & PCIE_LINK_STATE_L1)
  776. state |= ASPM_STATE_L1;
  777. down_read(&pci_bus_sem);
  778. mutex_lock(&aspm_lock);
  779. list_for_each_entry(link, &link_list, sibling) {
  780. if (link->root != root)
  781. continue;
  782. pcie_config_aspm_link(link, state);
  783. }
  784. mutex_unlock(&aspm_lock);
  785. up_read(&pci_bus_sem);
  786. return n;
  787. }
  788. static ssize_t clk_ctl_show(struct device *dev,
  789. struct device_attribute *attr,
  790. char *buf)
  791. {
  792. struct pci_dev *pci_device = to_pci_dev(dev);
  793. struct pcie_link_state *link_state = pci_device->link_state;
  794. return sprintf(buf, "%d\n", link_state->clkpm_enabled);
  795. }
  796. static ssize_t clk_ctl_store(struct device *dev,
  797. struct device_attribute *attr,
  798. const char *buf,
  799. size_t n)
  800. {
  801. struct pci_dev *pdev = to_pci_dev(dev);
  802. int state;
  803. if (n < 1)
  804. return -EINVAL;
  805. state = buf[0]-'0';
  806. down_read(&pci_bus_sem);
  807. mutex_lock(&aspm_lock);
  808. pcie_set_clkpm_nocheck(pdev->link_state, !!state);
  809. mutex_unlock(&aspm_lock);
  810. up_read(&pci_bus_sem);
  811. return n;
  812. }
  813. static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
  814. static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
  815. static char power_group[] = "power";
  816. void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
  817. {
  818. struct pcie_link_state *link_state = pdev->link_state;
  819. if (!pci_is_pcie(pdev) ||
  820. (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  821. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  822. return;
  823. if (link_state->aspm_support)
  824. sysfs_add_file_to_group(&pdev->dev.kobj,
  825. &dev_attr_link_state.attr, power_group);
  826. if (link_state->clkpm_capable)
  827. sysfs_add_file_to_group(&pdev->dev.kobj,
  828. &dev_attr_clk_ctl.attr, power_group);
  829. }
  830. void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
  831. {
  832. struct pcie_link_state *link_state = pdev->link_state;
  833. if (!pci_is_pcie(pdev) ||
  834. (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
  835. pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
  836. return;
  837. if (link_state->aspm_support)
  838. sysfs_remove_file_from_group(&pdev->dev.kobj,
  839. &dev_attr_link_state.attr, power_group);
  840. if (link_state->clkpm_capable)
  841. sysfs_remove_file_from_group(&pdev->dev.kobj,
  842. &dev_attr_clk_ctl.attr, power_group);
  843. }
  844. #endif
  845. static int __init pcie_aspm_disable(char *str)
  846. {
  847. if (!strcmp(str, "off")) {
  848. aspm_policy = POLICY_DEFAULT;
  849. aspm_disabled = 1;
  850. aspm_support_enabled = false;
  851. printk(KERN_INFO "PCIe ASPM is disabled\n");
  852. } else if (!strcmp(str, "force")) {
  853. aspm_force = 1;
  854. printk(KERN_INFO "PCIe ASPM is forcibly enabled\n");
  855. }
  856. return 1;
  857. }
  858. __setup("pcie_aspm=", pcie_aspm_disable);
  859. void pcie_no_aspm(void)
  860. {
  861. /*
  862. * Disabling ASPM is intended to prevent the kernel from modifying
  863. * existing hardware state, not to clear existing state. To that end:
  864. * (a) set policy to POLICY_DEFAULT in order to avoid changing state
  865. * (b) prevent userspace from changing policy
  866. */
  867. if (!aspm_force) {
  868. aspm_policy = POLICY_DEFAULT;
  869. aspm_disabled = 1;
  870. }
  871. }
  872. /**
  873. * pcie_aspm_enabled - is PCIe ASPM enabled?
  874. *
  875. * Returns true if ASPM has not been disabled by the command-line option
  876. * pcie_aspm=off.
  877. **/
  878. int pcie_aspm_enabled(void)
  879. {
  880. return !aspm_disabled;
  881. }
  882. EXPORT_SYMBOL(pcie_aspm_enabled);
  883. bool pcie_aspm_support_enabled(void)
  884. {
  885. return aspm_support_enabled;
  886. }
  887. EXPORT_SYMBOL(pcie_aspm_support_enabled);