msix.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. /*
  3. * Copyright(c) 2018 Intel Corporation.
  4. *
  5. * This file is provided under a dual BSD/GPLv2 license. When using or
  6. * redistributing this file, you may do so under either license.
  7. *
  8. * GPL LICENSE SUMMARY
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * BSD LICENSE
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. *
  25. * - Redistributions of source code must retain the above copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * - Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in
  29. * the documentation and/or other materials provided with the
  30. * distribution.
  31. * - Neither the name of Intel Corporation nor the names of its
  32. * contributors may be used to endorse or promote products derived
  33. * from this software without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. */
  48. #include "hfi.h"
  49. #include "affinity.h"
  50. #include "sdma.h"
  51. /**
  52. * msix_initialize() - Calculate, request and configure MSIx IRQs
  53. * @dd: valid hfi1 devdata
  54. *
  55. */
  56. int msix_initialize(struct hfi1_devdata *dd)
  57. {
  58. u32 total;
  59. int ret;
  60. struct hfi1_msix_entry *entries;
  61. /*
  62. * MSIx interrupt count:
  63. * one for the general, "slow path" interrupt
  64. * one per used SDMA engine
  65. * one per kernel receive context
  66. * one for each VNIC context
  67. * ...any new IRQs should be added here.
  68. */
  69. total = 1 + dd->num_sdma + dd->n_krcv_queues + dd->num_vnic_contexts;
  70. if (total >= CCE_NUM_MSIX_VECTORS)
  71. return -EINVAL;
  72. ret = pci_alloc_irq_vectors(dd->pcidev, total, total, PCI_IRQ_MSIX);
  73. if (ret < 0) {
  74. dd_dev_err(dd, "pci_alloc_irq_vectors() failed: %d\n", ret);
  75. return ret;
  76. }
  77. entries = kcalloc(total, sizeof(*dd->msix_info.msix_entries),
  78. GFP_KERNEL);
  79. if (!entries) {
  80. pci_free_irq_vectors(dd->pcidev);
  81. return -ENOMEM;
  82. }
  83. dd->msix_info.msix_entries = entries;
  84. spin_lock_init(&dd->msix_info.msix_lock);
  85. bitmap_zero(dd->msix_info.in_use_msix, total);
  86. dd->msix_info.max_requested = total;
  87. dd_dev_info(dd, "%u MSI-X interrupts allocated\n", total);
  88. return 0;
  89. }
  90. /**
  91. * msix_request_irq() - Allocate a free MSIx IRQ
  92. * @dd: valid devdata
  93. * @arg: context information for the IRQ
  94. * @handler: IRQ handler
  95. * @thread: IRQ thread handler (could be NULL)
  96. * @idx: zero base idx if multiple devices are needed
  97. * @type: affinty IRQ type
  98. *
  99. * Allocated an MSIx vector if available, and then create the appropriate
  100. * meta data needed to keep track of the pci IRQ request.
  101. *
  102. * Return:
  103. * < 0 Error
  104. * >= 0 MSIx vector
  105. *
  106. */
  107. static int msix_request_irq(struct hfi1_devdata *dd, void *arg,
  108. irq_handler_t handler, irq_handler_t thread,
  109. u32 idx, enum irq_type type)
  110. {
  111. unsigned long nr;
  112. int irq;
  113. int ret;
  114. const char *err_info;
  115. char name[MAX_NAME_SIZE];
  116. struct hfi1_msix_entry *me;
  117. /* Allocate an MSIx vector */
  118. spin_lock(&dd->msix_info.msix_lock);
  119. nr = find_first_zero_bit(dd->msix_info.in_use_msix,
  120. dd->msix_info.max_requested);
  121. if (nr < dd->msix_info.max_requested)
  122. __set_bit(nr, dd->msix_info.in_use_msix);
  123. spin_unlock(&dd->msix_info.msix_lock);
  124. if (nr == dd->msix_info.max_requested)
  125. return -ENOSPC;
  126. /* Specific verification and determine the name */
  127. switch (type) {
  128. case IRQ_GENERAL:
  129. /* general interrupt must be MSIx vector 0 */
  130. if (nr) {
  131. spin_lock(&dd->msix_info.msix_lock);
  132. __clear_bit(nr, dd->msix_info.in_use_msix);
  133. spin_unlock(&dd->msix_info.msix_lock);
  134. dd_dev_err(dd, "Invalid index %lu for GENERAL IRQ\n",
  135. nr);
  136. return -EINVAL;
  137. }
  138. snprintf(name, sizeof(name), DRIVER_NAME "_%d", dd->unit);
  139. err_info = "general";
  140. break;
  141. case IRQ_SDMA:
  142. snprintf(name, sizeof(name), DRIVER_NAME "_%d sdma%d",
  143. dd->unit, idx);
  144. err_info = "sdma";
  145. break;
  146. case IRQ_RCVCTXT:
  147. snprintf(name, sizeof(name), DRIVER_NAME "_%d kctxt%d",
  148. dd->unit, idx);
  149. err_info = "receive context";
  150. break;
  151. case IRQ_OTHER:
  152. default:
  153. return -EINVAL;
  154. }
  155. name[sizeof(name) - 1] = 0;
  156. irq = pci_irq_vector(dd->pcidev, nr);
  157. ret = pci_request_irq(dd->pcidev, nr, handler, thread, arg, name);
  158. if (ret) {
  159. dd_dev_err(dd,
  160. "%s: request for IRQ %d failed, MSIx %d, err %d\n",
  161. err_info, irq, idx, ret);
  162. spin_lock(&dd->msix_info.msix_lock);
  163. __clear_bit(nr, dd->msix_info.in_use_msix);
  164. spin_unlock(&dd->msix_info.msix_lock);
  165. return ret;
  166. }
  167. /*
  168. * assign arg after pci_request_irq call, so it will be
  169. * cleaned up
  170. */
  171. me = &dd->msix_info.msix_entries[nr];
  172. me->irq = irq;
  173. me->arg = arg;
  174. me->type = type;
  175. /* This is a request, so a failure is not fatal */
  176. ret = hfi1_get_irq_affinity(dd, me);
  177. if (ret)
  178. dd_dev_err(dd, "unable to pin IRQ %d\n", ret);
  179. return nr;
  180. }
  181. /**
  182. * msix_request_rcd_irq() - Helper function for RCVAVAIL IRQs
  183. * @rcd: valid rcd context
  184. *
  185. */
  186. int msix_request_rcd_irq(struct hfi1_ctxtdata *rcd)
  187. {
  188. int nr;
  189. nr = msix_request_irq(rcd->dd, rcd, receive_context_interrupt,
  190. receive_context_thread, rcd->ctxt, IRQ_RCVCTXT);
  191. if (nr < 0)
  192. return nr;
  193. /*
  194. * Set the interrupt register and mask for this
  195. * context's interrupt.
  196. */
  197. rcd->ireg = (IS_RCVAVAIL_START + rcd->ctxt) / 64;
  198. rcd->imask = ((u64)1) << ((IS_RCVAVAIL_START + rcd->ctxt) % 64);
  199. rcd->msix_intr = nr;
  200. remap_intr(rcd->dd, IS_RCVAVAIL_START + rcd->ctxt, nr);
  201. return 0;
  202. }
  203. /**
  204. * msix_request_smda_ira() - Helper for getting SDMA IRQ resources
  205. * @sde: valid sdma engine
  206. *
  207. */
  208. int msix_request_sdma_irq(struct sdma_engine *sde)
  209. {
  210. int nr;
  211. nr = msix_request_irq(sde->dd, sde, sdma_interrupt, NULL,
  212. sde->this_idx, IRQ_SDMA);
  213. if (nr < 0)
  214. return nr;
  215. sde->msix_intr = nr;
  216. remap_sdma_interrupts(sde->dd, sde->this_idx, nr);
  217. return 0;
  218. }
  219. /**
  220. * enable_sdma_src() - Helper to enable SDMA IRQ srcs
  221. * @dd: valid devdata structure
  222. * @i: index of SDMA engine
  223. */
  224. static void enable_sdma_srcs(struct hfi1_devdata *dd, int i)
  225. {
  226. set_intr_bits(dd, IS_SDMA_START + i, IS_SDMA_START + i, true);
  227. set_intr_bits(dd, IS_SDMA_PROGRESS_START + i,
  228. IS_SDMA_PROGRESS_START + i, true);
  229. set_intr_bits(dd, IS_SDMA_IDLE_START + i, IS_SDMA_IDLE_START + i, true);
  230. set_intr_bits(dd, IS_SDMAENG_ERR_START + i, IS_SDMAENG_ERR_START + i,
  231. true);
  232. }
  233. /**
  234. * msix_request_irqs() - Allocate all MSIx IRQs
  235. * @dd: valid devdata structure
  236. *
  237. * Helper function to request the used MSIx IRQs.
  238. *
  239. */
  240. int msix_request_irqs(struct hfi1_devdata *dd)
  241. {
  242. int i;
  243. int ret;
  244. ret = msix_request_irq(dd, dd, general_interrupt, NULL, 0, IRQ_GENERAL);
  245. if (ret < 0)
  246. return ret;
  247. for (i = 0; i < dd->num_sdma; i++) {
  248. struct sdma_engine *sde = &dd->per_sdma[i];
  249. ret = msix_request_sdma_irq(sde);
  250. if (ret)
  251. return ret;
  252. enable_sdma_srcs(sde->dd, i);
  253. }
  254. for (i = 0; i < dd->n_krcv_queues; i++) {
  255. struct hfi1_ctxtdata *rcd = hfi1_rcd_get_by_index_safe(dd, i);
  256. if (rcd)
  257. ret = msix_request_rcd_irq(rcd);
  258. hfi1_rcd_put(rcd);
  259. if (ret)
  260. return ret;
  261. }
  262. return 0;
  263. }
  264. /**
  265. * msix_free_irq() - Free the specified MSIx resources and IRQ
  266. * @dd: valid devdata
  267. * @msix_intr: MSIx vector to free.
  268. *
  269. */
  270. void msix_free_irq(struct hfi1_devdata *dd, u8 msix_intr)
  271. {
  272. struct hfi1_msix_entry *me;
  273. if (msix_intr >= dd->msix_info.max_requested)
  274. return;
  275. me = &dd->msix_info.msix_entries[msix_intr];
  276. if (!me->arg) /* => no irq, no affinity */
  277. return;
  278. hfi1_put_irq_affinity(dd, me);
  279. pci_free_irq(dd->pcidev, msix_intr, me->arg);
  280. me->arg = NULL;
  281. spin_lock(&dd->msix_info.msix_lock);
  282. __clear_bit(msix_intr, dd->msix_info.in_use_msix);
  283. spin_unlock(&dd->msix_info.msix_lock);
  284. }
  285. /**
  286. * hfi1_clean_up_msix_interrupts() - Free all MSIx IRQ resources
  287. * @dd: valid device data data structure
  288. *
  289. * Free the MSIx and associated PCI resources, if they have been allocated.
  290. */
  291. void msix_clean_up_interrupts(struct hfi1_devdata *dd)
  292. {
  293. int i;
  294. struct hfi1_msix_entry *me = dd->msix_info.msix_entries;
  295. /* remove irqs - must happen before disabling/turning off */
  296. for (i = 0; i < dd->msix_info.max_requested; i++, me++)
  297. msix_free_irq(dd, i);
  298. /* clean structures */
  299. kfree(dd->msix_info.msix_entries);
  300. dd->msix_info.msix_entries = NULL;
  301. dd->msix_info.max_requested = 0;
  302. pci_free_irq_vectors(dd->pcidev);
  303. }
  304. /**
  305. * msix_vnic_syncrhonize_irq() - Vnic IRQ synchronize
  306. * @dd: valid devdata
  307. */
  308. void msix_vnic_synchronize_irq(struct hfi1_devdata *dd)
  309. {
  310. int i;
  311. for (i = 0; i < dd->vnic.num_ctxt; i++) {
  312. struct hfi1_ctxtdata *rcd = dd->vnic.ctxt[i];
  313. struct hfi1_msix_entry *me;
  314. me = &dd->msix_info.msix_entries[rcd->msix_intr];
  315. synchronize_irq(me->irq);
  316. }
  317. }