1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
11 #include <linux/vfio.h>
12 #include <sys/eventfd.h>
13 #include <sys/ioctl.h>
16 #define MSIX_IRQ_SET_BUF_LEN \
17 (sizeof(struct vfio_irq_set) + sizeof(int) * (PLT_MAX_RXTX_INTR_VEC_ID))
20 irq_get_info(struct plt_intr_handle *intr_handle)
22 struct vfio_irq_info irq = {.argsz = sizeof(irq)};
25 irq.index = VFIO_PCI_MSIX_IRQ_INDEX;
27 rc = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
29 plt_err("Failed to get IRQ info rc=%d errno=%d", rc, errno);
33 plt_base_dbg("Flags=0x%x index=0x%x count=0x%x max_intr_vec_id=0x%x",
34 irq.flags, irq.index, irq.count, PLT_MAX_RXTX_INTR_VEC_ID);
36 if (irq.count > PLT_MAX_RXTX_INTR_VEC_ID) {
37 plt_err("HW max=%d > PLT_MAX_RXTX_INTR_VEC_ID: %d", irq.count,
38 PLT_MAX_RXTX_INTR_VEC_ID);
39 intr_handle->max_intr = PLT_MAX_RXTX_INTR_VEC_ID;
41 intr_handle->max_intr = irq.count;
48 irq_config(struct plt_intr_handle *intr_handle, unsigned int vec)
50 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
51 struct vfio_irq_set *irq_set;
55 if (vec > intr_handle->max_intr) {
56 plt_err("vector=%d greater than max_intr=%d", vec,
57 intr_handle->max_intr);
61 len = sizeof(struct vfio_irq_set) + sizeof(int32_t);
63 irq_set = (struct vfio_irq_set *)irq_set_buf;
69 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
70 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
72 /* Use vec fd to set interrupt vectors */
73 fd_ptr = (int32_t *)&irq_set->data[0];
74 fd_ptr[0] = intr_handle->efds[vec];
76 rc = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
78 plt_err("Failed to set_irqs vector=0x%x rc=%d", vec, rc);
84 irq_init(struct plt_intr_handle *intr_handle)
86 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
87 struct vfio_irq_set *irq_set;
92 if (intr_handle->max_intr > PLT_MAX_RXTX_INTR_VEC_ID) {
93 plt_err("Max_intr=%d greater than PLT_MAX_RXTX_INTR_VEC_ID=%d",
94 intr_handle->max_intr, PLT_MAX_RXTX_INTR_VEC_ID);
98 len = sizeof(struct vfio_irq_set) +
99 sizeof(int32_t) * intr_handle->max_intr;
101 irq_set = (struct vfio_irq_set *)irq_set_buf;
102 irq_set->argsz = len;
104 irq_set->count = intr_handle->max_intr;
106 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
107 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
109 fd_ptr = (int32_t *)&irq_set->data[0];
110 for (i = 0; i < irq_set->count; i++)
113 rc = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
115 plt_err("Failed to set irqs vector rc=%d", rc);
121 dev_irqs_disable(struct plt_intr_handle *intr_handle)
123 /* Clear max_intr to indicate re-init next time */
124 intr_handle->max_intr = 0;
125 return plt_intr_disable(intr_handle);
129 dev_irq_register(struct plt_intr_handle *intr_handle, plt_intr_callback_fn cb,
130 void *data, unsigned int vec)
132 struct plt_intr_handle tmp_handle;
135 /* If no max_intr read from VFIO */
136 if (intr_handle->max_intr == 0) {
137 irq_get_info(intr_handle);
138 irq_init(intr_handle);
141 if (vec > intr_handle->max_intr) {
142 plt_err("Vector=%d greater than max_intr=%d", vec,
143 intr_handle->max_intr);
147 tmp_handle = *intr_handle;
148 /* Create new eventfd for interrupt vector */
149 tmp_handle.fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
150 if (tmp_handle.fd == -1)
153 /* Register vector interrupt callback */
154 rc = plt_intr_callback_register(&tmp_handle, cb, data);
156 plt_err("Failed to register vector:0x%x irq callback.", vec);
160 intr_handle->efds[vec] = tmp_handle.fd;
161 intr_handle->nb_efd =
162 (vec > intr_handle->nb_efd) ? vec : intr_handle->nb_efd;
163 if ((intr_handle->nb_efd + 1) > intr_handle->max_intr)
164 intr_handle->max_intr = intr_handle->nb_efd + 1;
166 plt_base_dbg("Enable vector:0x%x for vfio (efds: %d, max:%d)", vec,
167 intr_handle->nb_efd, intr_handle->max_intr);
169 /* Enable MSIX vectors to VFIO */
170 return irq_config(intr_handle, vec);
174 dev_irq_unregister(struct plt_intr_handle *intr_handle, plt_intr_callback_fn cb,
175 void *data, unsigned int vec)
177 struct plt_intr_handle tmp_handle;
178 uint8_t retries = 5; /* 5 ms */
181 if (vec > intr_handle->max_intr) {
182 plt_err("Error unregistering MSI-X interrupts vec:%d > %d", vec,
183 intr_handle->max_intr);
187 tmp_handle = *intr_handle;
188 tmp_handle.fd = intr_handle->efds[vec];
189 if (tmp_handle.fd == -1)
193 /* Un-register callback func from platform lib */
194 rc = plt_intr_callback_unregister(&tmp_handle, cb, data);
195 /* Retry only if -EAGAIN */
203 plt_err("Error unregistering MSI-X vec %d cb, rc=%d", vec, rc);
207 plt_base_dbg("Disable vector:0x%x for vfio (efds: %d, max:%d)", vec,
208 intr_handle->nb_efd, intr_handle->max_intr);
210 if (intr_handle->efds[vec] != -1)
211 close(intr_handle->efds[vec]);
212 /* Disable MSIX vectors from VFIO */
213 intr_handle->efds[vec] = -1;
214 irq_config(intr_handle, vec);
220 dev_irq_register(struct plt_intr_handle *intr_handle, plt_intr_callback_fn cb,
221 void *data, unsigned int vec)
223 PLT_SET_USED(intr_handle);
232 dev_irq_unregister(struct plt_intr_handle *intr_handle, plt_intr_callback_fn cb,
233 void *data, unsigned int vec)
235 PLT_SET_USED(intr_handle);
242 dev_irqs_disable(struct plt_intr_handle *intr_handle)
244 PLT_SET_USED(intr_handle);
249 #endif /* __linux__ */