1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2019 Marvell International Ltd.
6 #include <rte_common.h>
8 #include <rte_interrupts.h>
10 #include "otx2_common.h"
16 #include <linux/vfio.h>
17 #include <sys/eventfd.h>
18 #include <sys/ioctl.h>
21 #define MAX_INTR_VEC_ID RTE_MAX_RXTX_INTR_VEC_ID
22 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
23 sizeof(int) * (MAX_INTR_VEC_ID))
26 irq_get_info(struct rte_intr_handle *intr_handle)
28 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
31 irq.index = VFIO_PCI_MSIX_IRQ_INDEX;
33 rc = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
35 otx2_err("Failed to get IRQ info rc=%d errno=%d", rc, errno);
39 otx2_base_dbg("Flags=0x%x index=0x%x count=0x%x max_intr_vec_id=0x%x",
40 irq.flags, irq.index, irq.count, MAX_INTR_VEC_ID);
42 if (irq.count > MAX_INTR_VEC_ID) {
43 otx2_err("HW max=%d > MAX_INTR_VEC_ID: %d",
44 intr_handle->max_intr, MAX_INTR_VEC_ID);
45 intr_handle->max_intr = MAX_INTR_VEC_ID;
47 intr_handle->max_intr = irq.count;
54 irq_config(struct rte_intr_handle *intr_handle, unsigned int vec)
56 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
57 struct vfio_irq_set *irq_set;
61 if (vec > intr_handle->max_intr) {
62 otx2_err("vector=%d greater than max_intr=%d", vec,
63 intr_handle->max_intr);
67 len = sizeof(struct vfio_irq_set) + sizeof(int32_t);
69 irq_set = (struct vfio_irq_set *)irq_set_buf;
74 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
75 VFIO_IRQ_SET_ACTION_TRIGGER;
76 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
78 /* Use vec fd to set interrupt vectors */
79 fd_ptr = (int32_t *)&irq_set->data[0];
80 fd_ptr[0] = intr_handle->efds[vec];
82 rc = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
84 otx2_err("Failed to set_irqs vector=0x%x rc=%d", vec, rc);
90 irq_init(struct rte_intr_handle *intr_handle)
92 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
93 struct vfio_irq_set *irq_set;
98 if (intr_handle->max_intr > MAX_INTR_VEC_ID) {
99 otx2_err("Max_intr=%d greater than MAX_INTR_VEC_ID=%d",
100 intr_handle->max_intr, MAX_INTR_VEC_ID);
104 len = sizeof(struct vfio_irq_set) +
105 sizeof(int32_t) * intr_handle->max_intr;
107 irq_set = (struct vfio_irq_set *)irq_set_buf;
108 irq_set->argsz = len;
110 irq_set->count = intr_handle->max_intr;
111 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
112 VFIO_IRQ_SET_ACTION_TRIGGER;
113 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
115 fd_ptr = (int32_t *)&irq_set->data[0];
116 for (i = 0; i < irq_set->count; i++)
119 rc = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
121 otx2_err("Failed to set irqs vector rc=%d", rc);
131 otx2_disable_irqs(struct rte_intr_handle *intr_handle)
133 /* Clear max_intr to indicate re-init next time */
134 intr_handle->max_intr = 0;
135 return rte_intr_disable(intr_handle);
143 otx2_register_irq(struct rte_intr_handle *intr_handle,
144 rte_intr_callback_fn cb, void *data, unsigned int vec)
146 struct rte_intr_handle tmp_handle;
149 /* If no max_intr read from VFIO */
150 if (intr_handle->max_intr == 0) {
151 irq_get_info(intr_handle);
152 irq_init(intr_handle);
155 if (vec > intr_handle->max_intr) {
156 otx2_err("Vector=%d greater than max_intr=%d", vec,
157 intr_handle->max_intr);
161 tmp_handle = *intr_handle;
162 /* Create new eventfd for interrupt vector */
163 tmp_handle.fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
164 if (tmp_handle.fd == -1)
167 /* Register vector interrupt callback */
168 rc = rte_intr_callback_register(&tmp_handle, cb, data);
170 otx2_err("Failed to register vector:0x%x irq callback.", vec);
174 intr_handle->efds[vec] = tmp_handle.fd;
175 intr_handle->nb_efd = (vec > intr_handle->nb_efd) ?
176 vec : intr_handle->nb_efd;
177 if ((intr_handle->nb_efd + 1) > intr_handle->max_intr)
178 intr_handle->max_intr = intr_handle->nb_efd + 1;
180 otx2_base_dbg("Enable vector:0x%x for vfio (efds: %d, max:%d)",
181 vec, intr_handle->nb_efd, intr_handle->max_intr);
183 /* Enable MSIX vectors to VFIO */
184 return irq_config(intr_handle, vec);
192 otx2_unregister_irq(struct rte_intr_handle *intr_handle,
193 rte_intr_callback_fn cb, void *data, unsigned int vec)
195 struct rte_intr_handle tmp_handle;
197 if (vec > intr_handle->max_intr) {
198 otx2_err("Error unregistering MSI-X interrupts vec:%d > %d",
199 vec, intr_handle->max_intr);
203 tmp_handle = *intr_handle;
204 tmp_handle.fd = intr_handle->efds[vec];
205 if (tmp_handle.fd == -1)
208 /* Un-register callback func from eal lib */
209 rte_intr_callback_unregister(&tmp_handle, cb, data);
211 otx2_base_dbg("Disable vector:0x%x for vfio (efds: %d, max:%d)",
212 vec, intr_handle->nb_efd, intr_handle->max_intr);
214 if (intr_handle->efds[vec] != -1)
215 close(intr_handle->efds[vec]);
216 /* Disable MSIX vectors from VFIO */
217 intr_handle->efds[vec] = -1;
218 irq_config(intr_handle, vec);
227 int otx2_register_irq(__rte_unused struct rte_intr_handle *intr_handle,
228 __rte_unused rte_intr_callback_fn cb,
229 __rte_unused void *data, __rte_unused unsigned int vec)
239 void otx2_unregister_irq(__rte_unused struct rte_intr_handle *intr_handle,
240 __rte_unused rte_intr_callback_fn cb,
241 __rte_unused void *data, __rte_unused unsigned int vec)
249 int otx2_disable_irqs(__rte_unused struct rte_intr_handle *intr_handle)
254 #endif /* RTE_EAL_VFIO */