aspm.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright(c) 2015-2017 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #ifndef _ASPM_H
  48. #define _ASPM_H
  49. #include "hfi.h"
  50. extern uint aspm_mode;
  51. enum aspm_mode {
  52. ASPM_MODE_DISABLED = 0, /* ASPM always disabled, performance mode */
  53. ASPM_MODE_ENABLED = 1, /* ASPM always enabled, power saving mode */
  54. ASPM_MODE_DYNAMIC = 2, /* ASPM enabled/disabled dynamically */
  55. };
  56. /* Time after which the timer interrupt will re-enable ASPM */
  57. #define ASPM_TIMER_MS 1000
  58. /* Time for which interrupts are ignored after a timer has been scheduled */
  59. #define ASPM_RESCHED_TIMER_MS (ASPM_TIMER_MS / 2)
  60. /* Two interrupts within this time trigger ASPM disable */
  61. #define ASPM_TRIGGER_MS 1
  62. #define ASPM_TRIGGER_NS (ASPM_TRIGGER_MS * 1000 * 1000ull)
  63. #define ASPM_L1_SUPPORTED(reg) \
  64. (((reg & PCI_EXP_LNKCAP_ASPMS) >> 10) & 0x2)
  65. static inline bool aspm_hw_l1_supported(struct hfi1_devdata *dd)
  66. {
  67. struct pci_dev *parent = dd->pcidev->bus->self;
  68. u32 up, dn;
  69. /*
  70. * If the driver does not have access to the upstream component,
  71. * it cannot support ASPM L1 at all.
  72. */
  73. if (!parent)
  74. return false;
  75. pcie_capability_read_dword(dd->pcidev, PCI_EXP_LNKCAP, &dn);
  76. dn = ASPM_L1_SUPPORTED(dn);
  77. pcie_capability_read_dword(parent, PCI_EXP_LNKCAP, &up);
  78. up = ASPM_L1_SUPPORTED(up);
  79. /* ASPM works on A-step but is reported as not supported */
  80. return (!!dn || is_ax(dd)) && !!up;
  81. }
  82. /* Set L1 entrance latency for slower entry to L1 */
  83. static inline void aspm_hw_set_l1_ent_latency(struct hfi1_devdata *dd)
  84. {
  85. u32 l1_ent_lat = 0x4u;
  86. u32 reg32;
  87. pci_read_config_dword(dd->pcidev, PCIE_CFG_REG_PL3, &reg32);
  88. reg32 &= ~PCIE_CFG_REG_PL3_L1_ENT_LATENCY_SMASK;
  89. reg32 |= l1_ent_lat << PCIE_CFG_REG_PL3_L1_ENT_LATENCY_SHIFT;
  90. pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL3, reg32);
  91. }
  92. static inline void aspm_hw_enable_l1(struct hfi1_devdata *dd)
  93. {
  94. struct pci_dev *parent = dd->pcidev->bus->self;
  95. /*
  96. * If the driver does not have access to the upstream component,
  97. * it cannot support ASPM L1 at all.
  98. */
  99. if (!parent)
  100. return;
  101. /* Enable ASPM L1 first in upstream component and then downstream */
  102. pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
  103. PCI_EXP_LNKCTL_ASPMC,
  104. PCI_EXP_LNKCTL_ASPM_L1);
  105. pcie_capability_clear_and_set_word(dd->pcidev, PCI_EXP_LNKCTL,
  106. PCI_EXP_LNKCTL_ASPMC,
  107. PCI_EXP_LNKCTL_ASPM_L1);
  108. }
  109. static inline void aspm_hw_disable_l1(struct hfi1_devdata *dd)
  110. {
  111. struct pci_dev *parent = dd->pcidev->bus->self;
  112. /* Disable ASPM L1 first in downstream component and then upstream */
  113. pcie_capability_clear_and_set_word(dd->pcidev, PCI_EXP_LNKCTL,
  114. PCI_EXP_LNKCTL_ASPMC, 0x0);
  115. if (parent)
  116. pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
  117. PCI_EXP_LNKCTL_ASPMC, 0x0);
  118. }
  119. static inline void aspm_enable(struct hfi1_devdata *dd)
  120. {
  121. if (dd->aspm_enabled || aspm_mode == ASPM_MODE_DISABLED ||
  122. !dd->aspm_supported)
  123. return;
  124. aspm_hw_enable_l1(dd);
  125. dd->aspm_enabled = true;
  126. }
  127. static inline void aspm_disable(struct hfi1_devdata *dd)
  128. {
  129. if (!dd->aspm_enabled || aspm_mode == ASPM_MODE_ENABLED)
  130. return;
  131. aspm_hw_disable_l1(dd);
  132. dd->aspm_enabled = false;
  133. }
  134. static inline void aspm_disable_inc(struct hfi1_devdata *dd)
  135. {
  136. unsigned long flags;
  137. spin_lock_irqsave(&dd->aspm_lock, flags);
  138. aspm_disable(dd);
  139. atomic_inc(&dd->aspm_disabled_cnt);
  140. spin_unlock_irqrestore(&dd->aspm_lock, flags);
  141. }
  142. static inline void aspm_enable_dec(struct hfi1_devdata *dd)
  143. {
  144. unsigned long flags;
  145. spin_lock_irqsave(&dd->aspm_lock, flags);
  146. if (atomic_dec_and_test(&dd->aspm_disabled_cnt))
  147. aspm_enable(dd);
  148. spin_unlock_irqrestore(&dd->aspm_lock, flags);
  149. }
  150. /* ASPM processing for each receive context interrupt */
  151. static inline void aspm_ctx_disable(struct hfi1_ctxtdata *rcd)
  152. {
  153. bool restart_timer;
  154. bool close_interrupts;
  155. unsigned long flags;
  156. ktime_t now, prev;
  157. /* Quickest exit for minimum impact */
  158. if (!rcd->aspm_intr_supported)
  159. return;
  160. spin_lock_irqsave(&rcd->aspm_lock, flags);
  161. /* PSM contexts are open */
  162. if (!rcd->aspm_intr_enable)
  163. goto unlock;
  164. prev = rcd->aspm_ts_last_intr;
  165. now = ktime_get();
  166. rcd->aspm_ts_last_intr = now;
  167. /* An interrupt pair close together in time */
  168. close_interrupts = ktime_to_ns(ktime_sub(now, prev)) < ASPM_TRIGGER_NS;
  169. /* Don't push out our timer till this much time has elapsed */
  170. restart_timer = ktime_to_ns(ktime_sub(now, rcd->aspm_ts_timer_sched)) >
  171. ASPM_RESCHED_TIMER_MS * NSEC_PER_MSEC;
  172. restart_timer = restart_timer && close_interrupts;
  173. /* Disable ASPM and schedule timer */
  174. if (rcd->aspm_enabled && close_interrupts) {
  175. aspm_disable_inc(rcd->dd);
  176. rcd->aspm_enabled = false;
  177. restart_timer = true;
  178. }
  179. if (restart_timer) {
  180. mod_timer(&rcd->aspm_timer,
  181. jiffies + msecs_to_jiffies(ASPM_TIMER_MS));
  182. rcd->aspm_ts_timer_sched = now;
  183. }
  184. unlock:
  185. spin_unlock_irqrestore(&rcd->aspm_lock, flags);
  186. }
  187. /* Timer function for re-enabling ASPM in the absence of interrupt activity */
  188. static inline void aspm_ctx_timer_function(struct timer_list *t)
  189. {
  190. struct hfi1_ctxtdata *rcd = from_timer(rcd, t, aspm_timer);
  191. unsigned long flags;
  192. spin_lock_irqsave(&rcd->aspm_lock, flags);
  193. aspm_enable_dec(rcd->dd);
  194. rcd->aspm_enabled = true;
  195. spin_unlock_irqrestore(&rcd->aspm_lock, flags);
  196. }
  197. /*
  198. * Disable interrupt processing for verbs contexts when PSM or VNIC contexts
  199. * are open.
  200. */
  201. static inline void aspm_disable_all(struct hfi1_devdata *dd)
  202. {
  203. struct hfi1_ctxtdata *rcd;
  204. unsigned long flags;
  205. u16 i;
  206. for (i = 0; i < dd->first_dyn_alloc_ctxt; i++) {
  207. rcd = hfi1_rcd_get_by_index(dd, i);
  208. if (rcd) {
  209. del_timer_sync(&rcd->aspm_timer);
  210. spin_lock_irqsave(&rcd->aspm_lock, flags);
  211. rcd->aspm_intr_enable = false;
  212. spin_unlock_irqrestore(&rcd->aspm_lock, flags);
  213. hfi1_rcd_put(rcd);
  214. }
  215. }
  216. aspm_disable(dd);
  217. atomic_set(&dd->aspm_disabled_cnt, 0);
  218. }
  219. /* Re-enable interrupt processing for verbs contexts */
  220. static inline void aspm_enable_all(struct hfi1_devdata *dd)
  221. {
  222. struct hfi1_ctxtdata *rcd;
  223. unsigned long flags;
  224. u16 i;
  225. aspm_enable(dd);
  226. if (aspm_mode != ASPM_MODE_DYNAMIC)
  227. return;
  228. for (i = 0; i < dd->first_dyn_alloc_ctxt; i++) {
  229. rcd = hfi1_rcd_get_by_index(dd, i);
  230. if (rcd) {
  231. spin_lock_irqsave(&rcd->aspm_lock, flags);
  232. rcd->aspm_intr_enable = true;
  233. rcd->aspm_enabled = true;
  234. spin_unlock_irqrestore(&rcd->aspm_lock, flags);
  235. hfi1_rcd_put(rcd);
  236. }
  237. }
  238. }
  239. static inline void aspm_ctx_init(struct hfi1_ctxtdata *rcd)
  240. {
  241. spin_lock_init(&rcd->aspm_lock);
  242. timer_setup(&rcd->aspm_timer, aspm_ctx_timer_function, 0);
  243. rcd->aspm_intr_supported = rcd->dd->aspm_supported &&
  244. aspm_mode == ASPM_MODE_DYNAMIC &&
  245. rcd->ctxt < rcd->dd->first_dyn_alloc_ctxt;
  246. }
  247. static inline void aspm_init(struct hfi1_devdata *dd)
  248. {
  249. struct hfi1_ctxtdata *rcd;
  250. u16 i;
  251. spin_lock_init(&dd->aspm_lock);
  252. dd->aspm_supported = aspm_hw_l1_supported(dd);
  253. for (i = 0; i < dd->first_dyn_alloc_ctxt; i++) {
  254. rcd = hfi1_rcd_get_by_index(dd, i);
  255. if (rcd)
  256. aspm_ctx_init(rcd);
  257. hfi1_rcd_put(rcd);
  258. }
  259. /* Start with ASPM disabled */
  260. aspm_hw_set_l1_ent_latency(dd);
  261. dd->aspm_enabled = false;
  262. aspm_hw_disable_l1(dd);
  263. /* Now turn on ASPM if configured */
  264. aspm_enable_all(dd);
  265. }
  266. static inline void aspm_exit(struct hfi1_devdata *dd)
  267. {
  268. aspm_disable_all(dd);
  269. /* Turn on ASPM on exit to conserve power */
  270. aspm_enable(dd);
  271. }
  272. #endif /* _ASPM_H */