1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <sys/epoll.h>
16 #include <sys/signalfd.h>
17 #include <sys/ioctl.h>
18 #include <sys/eventfd.h>
22 #include <rte_common.h>
23 #include <rte_interrupts.h>
24 #include <rte_memory.h>
25 #include <rte_launch.h>
27 #include <rte_per_lcore.h>
28 #include <rte_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_debug.h>
32 #include <rte_errno.h>
33 #include <rte_spinlock.h>
34 #include <rte_pause.h>
36 #include <rte_eal_trace.h>
38 #include "eal_private.h"
40 #include "eal_thread.h"
42 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
43 #define NB_OTHER_INTR 1
45 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
61 * union buffer for reading on different devices
63 union rte_intr_read_buffer {
64 int uio_intr_count; /* for uio device */
66 uint64_t vfio_intr_count; /* for vfio device */
68 uint64_t timerfd_num; /* for timerfd */
69 char charbuf[16]; /* for others */
72 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
73 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
75 struct rte_intr_callback {
76 TAILQ_ENTRY(rte_intr_callback) next;
77 rte_intr_callback_fn cb_fn; /**< callback address */
78 void *cb_arg; /**< parameter for callback */
79 uint8_t pending_delete; /**< delete after callback is called */
80 rte_intr_unregister_callback_fn ucb_fn; /**< fn to call before cb is deleted */
83 struct rte_intr_source {
84 TAILQ_ENTRY(rte_intr_source) next;
85 struct rte_intr_handle *intr_handle; /**< interrupt handle */
86 struct rte_intr_cb_list callbacks; /**< user callbacks */
90 /* global spinlock for interrupt data operation */
91 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
93 /* union buffer for pipe read/write */
94 static union intr_pipefds intr_pipe;
96 /* interrupt sources list */
97 static struct rte_intr_source_list intr_sources;
99 /* interrupt handling thread */
100 static pthread_t intr_thread;
102 /* VFIO interrupts */
105 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
106 /* irq set buffer length for queue interrupts and LSC interrupt */
107 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
108 sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
110 /* enable legacy (INTx) interrupts */
112 vfio_enable_intx(const struct rte_intr_handle *intr_handle) {
113 struct vfio_irq_set *irq_set;
114 char irq_set_buf[IRQ_SET_BUF_LEN];
115 int len, ret, vfio_dev_fd;
118 len = sizeof(irq_set_buf);
121 irq_set = (struct vfio_irq_set *) irq_set_buf;
122 irq_set->argsz = len;
124 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
125 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
127 fd_ptr = (int *) &irq_set->data;
128 *fd_ptr = rte_intr_fd_get(intr_handle);
130 vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
131 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
134 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
135 rte_intr_fd_get(intr_handle));
139 /* unmask INTx after enabling */
140 memset(irq_set, 0, len);
141 len = sizeof(struct vfio_irq_set);
142 irq_set->argsz = len;
144 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
145 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
148 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
151 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
152 rte_intr_fd_get(intr_handle));
158 /* disable legacy (INTx) interrupts */
160 vfio_disable_intx(const struct rte_intr_handle *intr_handle) {
161 struct vfio_irq_set *irq_set;
162 char irq_set_buf[IRQ_SET_BUF_LEN];
163 int len, ret, vfio_dev_fd;
165 len = sizeof(struct vfio_irq_set);
167 /* mask interrupts before disabling */
168 irq_set = (struct vfio_irq_set *) irq_set_buf;
169 irq_set->argsz = len;
171 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
172 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
175 vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
176 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
179 RTE_LOG(ERR, EAL, "Error masking INTx interrupts for fd %d\n",
180 rte_intr_fd_get(intr_handle));
185 memset(irq_set, 0, len);
186 irq_set->argsz = len;
188 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
189 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
192 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
195 RTE_LOG(ERR, EAL, "Error disabling INTx interrupts for fd %d\n",
196 rte_intr_fd_get(intr_handle));
202 /* unmask/ack legacy (INTx) interrupts */
204 vfio_ack_intx(const struct rte_intr_handle *intr_handle)
206 struct vfio_irq_set irq_set;
210 memset(&irq_set, 0, sizeof(irq_set));
211 irq_set.argsz = sizeof(irq_set);
213 irq_set.flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
214 irq_set.index = VFIO_PCI_INTX_IRQ_INDEX;
217 vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
218 if (ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, &irq_set)) {
219 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
220 rte_intr_fd_get(intr_handle));
226 /* enable MSI interrupts */
228 vfio_enable_msi(const struct rte_intr_handle *intr_handle) {
230 char irq_set_buf[IRQ_SET_BUF_LEN];
231 struct vfio_irq_set *irq_set;
232 int *fd_ptr, vfio_dev_fd;
234 len = sizeof(irq_set_buf);
236 irq_set = (struct vfio_irq_set *) irq_set_buf;
237 irq_set->argsz = len;
239 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
240 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
242 fd_ptr = (int *) &irq_set->data;
243 *fd_ptr = rte_intr_fd_get(intr_handle);
245 vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
246 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
249 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
250 rte_intr_fd_get(intr_handle));
256 /* disable MSI interrupts */
258 vfio_disable_msi(const struct rte_intr_handle *intr_handle) {
259 struct vfio_irq_set *irq_set;
260 char irq_set_buf[IRQ_SET_BUF_LEN];
261 int len, ret, vfio_dev_fd;
263 len = sizeof(struct vfio_irq_set);
265 irq_set = (struct vfio_irq_set *) irq_set_buf;
266 irq_set->argsz = len;
268 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
269 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
272 vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
273 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
275 RTE_LOG(ERR, EAL, "Error disabling MSI interrupts for fd %d\n",
276 rte_intr_fd_get(intr_handle));
281 /* enable MSI-X interrupts */
283 vfio_enable_msix(const struct rte_intr_handle *intr_handle) {
285 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
286 struct vfio_irq_set *irq_set;
287 int *fd_ptr, vfio_dev_fd, i;
289 len = sizeof(irq_set_buf);
291 irq_set = (struct vfio_irq_set *) irq_set_buf;
292 irq_set->argsz = len;
293 /* 0 < irq_set->count < RTE_MAX_RXTX_INTR_VEC_ID + 1 */
294 irq_set->count = rte_intr_max_intr_get(intr_handle) ?
295 (rte_intr_max_intr_get(intr_handle) >
296 RTE_MAX_RXTX_INTR_VEC_ID + 1 ? RTE_MAX_RXTX_INTR_VEC_ID + 1 :
297 rte_intr_max_intr_get(intr_handle)) : 1;
299 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
300 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
302 fd_ptr = (int *) &irq_set->data;
303 /* INTR vector offset 0 reserve for non-efds mapping */
304 fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = rte_intr_fd_get(intr_handle);
305 for (i = 0; i < rte_intr_nb_efd_get(intr_handle); i++) {
306 fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] =
307 rte_intr_efds_index_get(intr_handle, i);
310 vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
311 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
314 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
315 rte_intr_fd_get(intr_handle));
322 /* disable MSI-X interrupts */
324 vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
325 struct vfio_irq_set *irq_set;
326 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
327 int len, ret, vfio_dev_fd;
329 len = sizeof(struct vfio_irq_set);
331 irq_set = (struct vfio_irq_set *) irq_set_buf;
332 irq_set->argsz = len;
334 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
335 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
338 vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
339 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
342 RTE_LOG(ERR, EAL, "Error disabling MSI-X interrupts for fd %d\n",
343 rte_intr_fd_get(intr_handle));
348 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
349 /* enable req notifier */
351 vfio_enable_req(const struct rte_intr_handle *intr_handle)
354 char irq_set_buf[IRQ_SET_BUF_LEN];
355 struct vfio_irq_set *irq_set;
356 int *fd_ptr, vfio_dev_fd;
358 len = sizeof(irq_set_buf);
360 irq_set = (struct vfio_irq_set *) irq_set_buf;
361 irq_set->argsz = len;
363 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
364 VFIO_IRQ_SET_ACTION_TRIGGER;
365 irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
367 fd_ptr = (int *) &irq_set->data;
368 *fd_ptr = rte_intr_fd_get(intr_handle);
370 vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
371 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
374 RTE_LOG(ERR, EAL, "Error enabling req interrupts for fd %d\n",
375 rte_intr_fd_get(intr_handle));
382 /* disable req notifier */
384 vfio_disable_req(const struct rte_intr_handle *intr_handle)
386 struct vfio_irq_set *irq_set;
387 char irq_set_buf[IRQ_SET_BUF_LEN];
388 int len, ret, vfio_dev_fd;
390 len = sizeof(struct vfio_irq_set);
392 irq_set = (struct vfio_irq_set *) irq_set_buf;
393 irq_set->argsz = len;
395 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
396 irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
399 vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
400 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
403 RTE_LOG(ERR, EAL, "Error disabling req interrupts for fd %d\n",
404 rte_intr_fd_get(intr_handle));
412 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
414 unsigned char command_high;
417 /* use UIO config file descriptor for uio_pci_generic */
418 uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
419 if (uio_cfg_fd < 0 || pread(uio_cfg_fd, &command_high, 1, 5) != 1) {
421 "Error reading interrupts status for fd %d\n",
425 /* disable interrupts */
427 if (pwrite(uio_cfg_fd, &command_high, 1, 5) != 1) {
429 "Error disabling interrupts for fd %d\n",
438 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
440 unsigned char command_high;
443 /* use UIO config file descriptor for uio_pci_generic */
444 uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
445 if (uio_cfg_fd < 0 || pread(uio_cfg_fd, &command_high, 1, 5) != 1) {
447 "Error reading interrupts status for fd %d\n",
451 /* enable interrupts */
452 command_high &= ~0x4;
453 if (pwrite(uio_cfg_fd, &command_high, 1, 5) != 1) {
455 "Error enabling interrupts for fd %d\n",
464 uio_intr_disable(const struct rte_intr_handle *intr_handle)
468 if (rte_intr_fd_get(intr_handle) < 0 ||
469 write(rte_intr_fd_get(intr_handle), &value, sizeof(value)) < 0) {
470 RTE_LOG(ERR, EAL, "Error disabling interrupts for fd %d (%s)\n",
471 rte_intr_fd_get(intr_handle), strerror(errno));
478 uio_intr_enable(const struct rte_intr_handle *intr_handle)
482 if (rte_intr_fd_get(intr_handle) < 0 ||
483 write(rte_intr_fd_get(intr_handle), &value, sizeof(value)) < 0) {
484 RTE_LOG(ERR, EAL, "Error enabling interrupts for fd %d (%s)\n",
485 rte_intr_fd_get(intr_handle), strerror(errno));
492 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
493 rte_intr_callback_fn cb, void *cb_arg)
495 int ret, wake_thread;
496 struct rte_intr_source *src;
497 struct rte_intr_callback *callback;
501 /* first do parameter checking */
502 if (rte_intr_fd_get(intr_handle) < 0 || cb == NULL) {
503 RTE_LOG(ERR, EAL, "Registering with invalid input parameter\n");
507 /* allocate a new interrupt callback entity */
508 callback = calloc(1, sizeof(*callback));
509 if (callback == NULL) {
510 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
513 callback->cb_fn = cb;
514 callback->cb_arg = cb_arg;
515 callback->pending_delete = 0;
516 callback->ucb_fn = NULL;
518 rte_spinlock_lock(&intr_lock);
520 /* check if there is at least one callback registered for the fd */
521 TAILQ_FOREACH(src, &intr_sources, next) {
522 if (rte_intr_fd_get(src->intr_handle) == rte_intr_fd_get(intr_handle)) {
523 /* we had no interrupts for this */
524 if (TAILQ_EMPTY(&src->callbacks))
527 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
533 /* no existing callbacks for this - add new source */
535 src = calloc(1, sizeof(*src));
537 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
542 src->intr_handle = rte_intr_instance_dup(intr_handle);
543 if (src->intr_handle == NULL) {
544 RTE_LOG(ERR, EAL, "Can not create intr instance\n");
551 TAILQ_INIT(&src->callbacks);
552 TAILQ_INSERT_TAIL(&(src->callbacks), callback,
554 TAILQ_INSERT_TAIL(&intr_sources, src, next);
561 rte_spinlock_unlock(&intr_lock);
564 * check if need to notify the pipe fd waited by epoll_wait to
565 * rebuild the wait list.
568 if (write(intr_pipe.writefd, "1", 1) < 0)
571 rte_eal_trace_intr_callback_register(intr_handle, cb, cb_arg, ret);
576 rte_intr_callback_unregister_pending(const struct rte_intr_handle *intr_handle,
577 rte_intr_callback_fn cb_fn, void *cb_arg,
578 rte_intr_unregister_callback_fn ucb_fn)
581 struct rte_intr_source *src;
582 struct rte_intr_callback *cb, *next;
584 /* do parameter checking first */
585 if (rte_intr_fd_get(intr_handle) < 0) {
586 RTE_LOG(ERR, EAL, "Unregistering with invalid input parameter\n");
590 rte_spinlock_lock(&intr_lock);
592 /* check if the insterrupt source for the fd is existent */
593 TAILQ_FOREACH(src, &intr_sources, next) {
594 if (rte_intr_fd_get(src->intr_handle) == rte_intr_fd_get(intr_handle))
598 /* No interrupt source registered for the fd */
602 /* only usable if the source is active */
603 } else if (src->active == 0) {
609 /* walk through the callbacks and mark all that match. */
610 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
611 next = TAILQ_NEXT(cb, next);
612 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
613 cb->cb_arg == cb_arg)) {
614 cb->pending_delete = 1;
621 rte_spinlock_unlock(&intr_lock);
627 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
628 rte_intr_callback_fn cb_fn, void *cb_arg)
631 struct rte_intr_source *src;
632 struct rte_intr_callback *cb, *next;
634 /* do parameter checking first */
635 if (rte_intr_fd_get(intr_handle) < 0) {
636 RTE_LOG(ERR, EAL, "Unregistering with invalid input parameter\n");
640 rte_spinlock_lock(&intr_lock);
642 /* check if the insterrupt source for the fd is existent */
643 TAILQ_FOREACH(src, &intr_sources, next)
644 if (rte_intr_fd_get(src->intr_handle) == rte_intr_fd_get(intr_handle))
647 /* No interrupt source registered for the fd */
651 /* interrupt source has some active callbacks right now. */
652 } else if (src->active != 0) {
659 /*walk through the callbacks and remove all that match. */
660 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
662 next = TAILQ_NEXT(cb, next);
664 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
665 cb->cb_arg == cb_arg)) {
666 TAILQ_REMOVE(&src->callbacks, cb, next);
672 /* all callbacks for that source are removed. */
673 if (TAILQ_EMPTY(&src->callbacks)) {
674 TAILQ_REMOVE(&intr_sources, src, next);
675 rte_intr_instance_free(src->intr_handle);
680 rte_spinlock_unlock(&intr_lock);
682 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
683 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
687 rte_eal_trace_intr_callback_unregister(intr_handle, cb_fn, cb_arg,
693 rte_intr_callback_unregister_sync(const struct rte_intr_handle *intr_handle,
694 rte_intr_callback_fn cb_fn, void *cb_arg)
698 while ((ret = rte_intr_callback_unregister(intr_handle, cb_fn, cb_arg)) == -EAGAIN)
705 rte_intr_enable(const struct rte_intr_handle *intr_handle)
707 int rc = 0, uio_cfg_fd;
709 if (intr_handle == NULL)
712 if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV) {
717 uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
718 if (rte_intr_fd_get(intr_handle) < 0 || uio_cfg_fd < 0) {
723 switch (rte_intr_type_get(intr_handle)) {
724 /* write to the uio fd to enable the interrupt */
725 case RTE_INTR_HANDLE_UIO:
726 if (uio_intr_enable(intr_handle))
729 case RTE_INTR_HANDLE_UIO_INTX:
730 if (uio_intx_intr_enable(intr_handle))
733 /* not used at this moment */
734 case RTE_INTR_HANDLE_ALARM:
738 case RTE_INTR_HANDLE_VFIO_MSIX:
739 if (vfio_enable_msix(intr_handle))
742 case RTE_INTR_HANDLE_VFIO_MSI:
743 if (vfio_enable_msi(intr_handle))
746 case RTE_INTR_HANDLE_VFIO_LEGACY:
747 if (vfio_enable_intx(intr_handle))
750 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
751 case RTE_INTR_HANDLE_VFIO_REQ:
752 if (vfio_enable_req(intr_handle))
757 /* not used at this moment */
758 case RTE_INTR_HANDLE_DEV_EVENT:
761 /* unknown handle type */
763 RTE_LOG(ERR, EAL, "Unknown handle type of fd %d\n",
764 rte_intr_fd_get(intr_handle));
769 rte_eal_trace_intr_enable(intr_handle, rc);
774 * PMD generally calls this function at the end of its IRQ callback.
775 * Internally, it unmasks the interrupt if possible.
777 * For INTx, unmasking is required as the interrupt is auto-masked prior to
780 * For MSI/MSI-X, unmasking is typically not needed as the interrupt is not
781 * auto-masked. In fact, for interrupt handle types VFIO_MSIX and VFIO_MSI,
782 * this function is no-op.
785 rte_intr_ack(const struct rte_intr_handle *intr_handle)
789 if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV)
792 uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
793 if (rte_intr_fd_get(intr_handle) < 0 || uio_cfg_fd < 0)
796 switch (rte_intr_type_get(intr_handle)) {
797 /* Both acking and enabling are same for UIO */
798 case RTE_INTR_HANDLE_UIO:
799 if (uio_intr_enable(intr_handle))
802 case RTE_INTR_HANDLE_UIO_INTX:
803 if (uio_intx_intr_enable(intr_handle))
806 /* not used at this moment */
807 case RTE_INTR_HANDLE_ALARM:
810 /* VFIO MSI* is implicitly acked unlike INTx, nothing to do */
811 case RTE_INTR_HANDLE_VFIO_MSIX:
812 case RTE_INTR_HANDLE_VFIO_MSI:
814 case RTE_INTR_HANDLE_VFIO_LEGACY:
815 if (vfio_ack_intx(intr_handle))
818 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
819 case RTE_INTR_HANDLE_VFIO_REQ:
823 /* not used at this moment */
824 case RTE_INTR_HANDLE_DEV_EVENT:
826 /* unknown handle type */
828 RTE_LOG(ERR, EAL, "Unknown handle type of fd %d\n",
829 rte_intr_fd_get(intr_handle));
837 rte_intr_disable(const struct rte_intr_handle *intr_handle)
839 int rc = 0, uio_cfg_fd;
841 if (intr_handle == NULL)
844 if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV) {
849 uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
850 if (rte_intr_fd_get(intr_handle) < 0 || uio_cfg_fd < 0) {
855 switch (rte_intr_type_get(intr_handle)) {
856 /* write to the uio fd to disable the interrupt */
857 case RTE_INTR_HANDLE_UIO:
858 if (uio_intr_disable(intr_handle))
861 case RTE_INTR_HANDLE_UIO_INTX:
862 if (uio_intx_intr_disable(intr_handle))
865 /* not used at this moment */
866 case RTE_INTR_HANDLE_ALARM:
870 case RTE_INTR_HANDLE_VFIO_MSIX:
871 if (vfio_disable_msix(intr_handle))
874 case RTE_INTR_HANDLE_VFIO_MSI:
875 if (vfio_disable_msi(intr_handle))
878 case RTE_INTR_HANDLE_VFIO_LEGACY:
879 if (vfio_disable_intx(intr_handle))
882 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
883 case RTE_INTR_HANDLE_VFIO_REQ:
884 if (vfio_disable_req(intr_handle))
889 /* not used at this moment */
890 case RTE_INTR_HANDLE_DEV_EVENT:
893 /* unknown handle type */
895 RTE_LOG(ERR, EAL, "Unknown handle type of fd %d\n",
896 rte_intr_fd_get(intr_handle));
901 rte_eal_trace_intr_disable(intr_handle, rc);
906 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
909 int n, bytes_read, rv;
910 struct rte_intr_source *src;
911 struct rte_intr_callback *cb, *next;
912 union rte_intr_read_buffer buf;
913 struct rte_intr_callback active_cb;
915 for (n = 0; n < nfds; n++) {
918 * if the pipe fd is ready to read, return out to
919 * rebuild the wait list.
921 if (events[n].data.fd == intr_pipe.readfd){
922 int r = read(intr_pipe.readfd, buf.charbuf,
923 sizeof(buf.charbuf));
927 rte_spinlock_lock(&intr_lock);
928 TAILQ_FOREACH(src, &intr_sources, next)
929 if (rte_intr_fd_get(src->intr_handle) == events[n].data.fd)
932 rte_spinlock_unlock(&intr_lock);
936 /* mark this interrupt source as active and release the lock. */
938 rte_spinlock_unlock(&intr_lock);
940 /* set the length to be read dor different handle type */
941 switch (rte_intr_type_get(src->intr_handle)) {
942 case RTE_INTR_HANDLE_UIO:
943 case RTE_INTR_HANDLE_UIO_INTX:
944 bytes_read = sizeof(buf.uio_intr_count);
946 case RTE_INTR_HANDLE_ALARM:
947 bytes_read = sizeof(buf.timerfd_num);
950 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
951 case RTE_INTR_HANDLE_VFIO_REQ:
953 case RTE_INTR_HANDLE_VFIO_MSIX:
954 case RTE_INTR_HANDLE_VFIO_MSI:
955 case RTE_INTR_HANDLE_VFIO_LEGACY:
956 bytes_read = sizeof(buf.vfio_intr_count);
959 case RTE_INTR_HANDLE_VDEV:
960 case RTE_INTR_HANDLE_EXT:
964 case RTE_INTR_HANDLE_DEV_EVENT:
973 if (bytes_read > 0) {
975 * read out to clear the ready-to-be-read flag
978 bytes_read = read(events[n].data.fd, &buf, bytes_read);
979 if (bytes_read < 0) {
980 if (errno == EINTR || errno == EWOULDBLOCK)
983 RTE_LOG(ERR, EAL, "Error reading from file "
984 "descriptor %d: %s\n",
988 * The device is unplugged or buggy, remove
989 * it as an interrupt source and return to
990 * force the wait list to be rebuilt.
992 rte_spinlock_lock(&intr_lock);
993 TAILQ_REMOVE(&intr_sources, src, next);
994 rte_spinlock_unlock(&intr_lock);
996 for (cb = TAILQ_FIRST(&src->callbacks); cb;
998 next = TAILQ_NEXT(cb, next);
999 TAILQ_REMOVE(&src->callbacks, cb, next);
1002 rte_intr_instance_free(src->intr_handle);
1005 } else if (bytes_read == 0)
1006 RTE_LOG(ERR, EAL, "Read nothing from file "
1007 "descriptor %d\n", events[n].data.fd);
1012 /* grab a lock, again to call callbacks and update status. */
1013 rte_spinlock_lock(&intr_lock);
1017 /* Finally, call all callbacks. */
1018 TAILQ_FOREACH(cb, &src->callbacks, next) {
1020 /* make a copy and unlock. */
1022 rte_spinlock_unlock(&intr_lock);
1024 /* call the actual callback */
1025 active_cb.cb_fn(active_cb.cb_arg);
1027 /*get the lock back. */
1028 rte_spinlock_lock(&intr_lock);
1031 /* we done with that interrupt source, release it. */
1036 /* check if any callback are supposed to be removed */
1037 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
1038 next = TAILQ_NEXT(cb, next);
1039 if (cb->pending_delete) {
1040 TAILQ_REMOVE(&src->callbacks, cb, next);
1042 cb->ucb_fn(src->intr_handle, cb->cb_arg);
1048 /* all callbacks for that source are removed. */
1049 if (TAILQ_EMPTY(&src->callbacks)) {
1050 TAILQ_REMOVE(&intr_sources, src, next);
1051 rte_intr_instance_free(src->intr_handle);
1055 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
1056 if (rv > 0 && write(intr_pipe.writefd, "1", 1) < 0) {
1057 rte_spinlock_unlock(&intr_lock);
1061 rte_spinlock_unlock(&intr_lock);
1068 * It handles all the interrupts.
1071 * epoll file descriptor.
1073 * The number of file descriptors added in epoll.
1079 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
1081 struct epoll_event events[totalfds];
1085 nfds = epoll_wait(pfd, events, totalfds,
1086 EAL_INTR_EPOLL_WAIT_FOREVER);
1087 /* epoll_wait fail */
1092 "epoll_wait returns with fail\n");
1095 /* epoll_wait timeout, will never happens here */
1098 /* epoll_wait has at least one fd ready to read */
1099 if (eal_intr_process_interrupts(events, nfds) < 0)
1105 * It builds/rebuilds up the epoll file descriptor with all the
1106 * file descriptors being waited on. Then handles the interrupts.
1114 static __rte_noreturn void *
1115 eal_intr_thread_main(__rte_unused void *arg)
1117 /* host thread, never break out */
1119 /* build up the epoll fd with all descriptors we are to
1120 * wait on then pass it to the handle_interrupts function
1122 static struct epoll_event pipe_event = {
1123 .events = EPOLLIN | EPOLLPRI,
1125 struct rte_intr_source *src;
1126 unsigned numfds = 0;
1128 /* create epoll fd */
1129 int pfd = epoll_create(1);
1131 rte_panic("Cannot create epoll instance\n");
1133 pipe_event.data.fd = intr_pipe.readfd;
1135 * add pipe fd into wait list, this pipe is used to
1136 * rebuild the wait list.
1138 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
1140 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
1141 intr_pipe.readfd, strerror(errno));
1145 rte_spinlock_lock(&intr_lock);
1147 TAILQ_FOREACH(src, &intr_sources, next) {
1148 struct epoll_event ev;
1150 if (src->callbacks.tqh_first == NULL)
1151 continue; /* skip those with no callbacks */
1152 memset(&ev, 0, sizeof(ev));
1153 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
1154 ev.data.fd = rte_intr_fd_get(src->intr_handle);
1157 * add all the uio device file descriptor
1160 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
1161 rte_intr_fd_get(src->intr_handle), &ev) < 0) {
1162 rte_panic("Error adding fd %d epoll_ctl, %s\n",
1163 rte_intr_fd_get(src->intr_handle),
1169 rte_spinlock_unlock(&intr_lock);
1170 /* serve the interrupt */
1171 eal_intr_handle_interrupts(pfd, numfds);
1174 * when we return, we need to rebuild the
1175 * list of fds to monitor.
1182 rte_eal_intr_init(void)
1186 /* init the global interrupt source head */
1187 TAILQ_INIT(&intr_sources);
1190 * create a pipe which will be waited by epoll and notified to
1191 * rebuild the wait list of epoll.
1193 if (pipe(intr_pipe.pipefd) < 0) {
1198 /* create the host thread to wait/handle the interrupt */
1199 ret = rte_ctrl_thread_create(&intr_thread, "eal-intr-thread", NULL,
1200 eal_intr_thread_main, NULL);
1204 "Failed to create thread for interrupt handling\n");
1211 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
1213 union rte_intr_read_buffer buf;
1217 switch (rte_intr_type_get(intr_handle)) {
1218 case RTE_INTR_HANDLE_UIO:
1219 case RTE_INTR_HANDLE_UIO_INTX:
1220 bytes_read = sizeof(buf.uio_intr_count);
1223 case RTE_INTR_HANDLE_VFIO_MSIX:
1224 case RTE_INTR_HANDLE_VFIO_MSI:
1225 case RTE_INTR_HANDLE_VFIO_LEGACY:
1226 bytes_read = sizeof(buf.vfio_intr_count);
1229 case RTE_INTR_HANDLE_VDEV:
1230 bytes_read = rte_intr_efd_counter_size_get(intr_handle);
1231 /* For vdev, number of bytes to read is set by driver */
1233 case RTE_INTR_HANDLE_EXT:
1237 RTE_LOG(INFO, EAL, "unexpected intr type\n");
1242 * read out to clear the ready-to-be-read flag
1245 if (bytes_read == 0)
1248 nbytes = read(fd, &buf, bytes_read);
1250 if (errno == EINTR || errno == EWOULDBLOCK ||
1254 "Error reading from fd %d: %s\n",
1255 fd, strerror(errno));
1256 } else if (nbytes == 0)
1257 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
1263 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
1264 struct rte_epoll_event *events)
1266 unsigned int i, count = 0;
1267 struct rte_epoll_event *rev;
1268 uint32_t valid_status;
1270 for (i = 0; i < n; i++) {
1271 rev = evs[i].data.ptr;
1272 valid_status = RTE_EPOLL_VALID;
1273 /* ACQUIRE memory ordering here pairs with RELEASE
1274 * ordering below acting as a lock to synchronize
1275 * the event data updating.
1277 if (!rev || !__atomic_compare_exchange_n(&rev->status,
1278 &valid_status, RTE_EPOLL_EXEC, 0,
1279 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
1282 events[count].status = RTE_EPOLL_VALID;
1283 events[count].fd = rev->fd;
1284 events[count].epfd = rev->epfd;
1285 events[count].epdata.event = evs[i].events;
1286 events[count].epdata.data = rev->epdata.data;
1287 if (rev->epdata.cb_fun)
1288 rev->epdata.cb_fun(rev->fd,
1289 rev->epdata.cb_arg);
1291 /* the status update should be observed after
1292 * the other fields change.
1294 __atomic_store_n(&rev->status, RTE_EPOLL_VALID,
1302 eal_init_tls_epfd(void)
1304 int pfd = epoll_create(255);
1308 "Cannot create epoll instance\n");
1315 rte_intr_tls_epfd(void)
1317 if (RTE_PER_LCORE(_epfd) == -1)
1318 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1320 return RTE_PER_LCORE(_epfd);
1324 eal_epoll_wait(int epfd, struct rte_epoll_event *events,
1325 int maxevents, int timeout, bool interruptible)
1327 struct epoll_event evs[maxevents];
1331 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1335 /* using per thread epoll fd */
1336 if (epfd == RTE_EPOLL_PER_THREAD)
1337 epfd = rte_intr_tls_epfd();
1340 rc = epoll_wait(epfd, evs, maxevents, timeout);
1341 if (likely(rc > 0)) {
1342 /* epoll_wait has at least one fd ready to read */
1343 rc = eal_epoll_process_event(evs, rc, events);
1345 } else if (rc < 0) {
1346 if (errno == EINTR) {
1352 /* epoll_wait fail */
1353 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1358 /* rc == 0, epoll_wait timed out */
1367 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1368 int maxevents, int timeout)
1370 return eal_epoll_wait(epfd, events, maxevents, timeout, false);
1374 rte_epoll_wait_interruptible(int epfd, struct rte_epoll_event *events,
1375 int maxevents, int timeout)
1377 return eal_epoll_wait(epfd, events, maxevents, timeout, true);
1381 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1383 uint32_t valid_status = RTE_EPOLL_VALID;
1385 while (!__atomic_compare_exchange_n(&ev->status, &valid_status,
1386 RTE_EPOLL_INVALID, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
1387 while (__atomic_load_n(&ev->status,
1388 __ATOMIC_RELAXED) != RTE_EPOLL_VALID)
1390 valid_status = RTE_EPOLL_VALID;
1392 memset(&ev->epdata, 0, sizeof(ev->epdata));
1398 rte_epoll_ctl(int epfd, int op, int fd,
1399 struct rte_epoll_event *event)
1401 struct epoll_event ev;
1404 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1408 /* using per thread epoll fd */
1409 if (epfd == RTE_EPOLL_PER_THREAD)
1410 epfd = rte_intr_tls_epfd();
1412 if (op == EPOLL_CTL_ADD) {
1413 __atomic_store_n(&event->status, RTE_EPOLL_VALID,
1415 event->fd = fd; /* ignore fd in event */
1417 ev.data.ptr = (void *)event;
1420 ev.events = event->epdata.event;
1421 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1422 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1423 op, fd, strerror(errno));
1424 if (op == EPOLL_CTL_ADD)
1425 /* rollback status when CTL_ADD fail */
1426 __atomic_store_n(&event->status, RTE_EPOLL_INVALID,
1431 if (op == EPOLL_CTL_DEL && __atomic_load_n(&event->status,
1432 __ATOMIC_RELAXED) != RTE_EPOLL_INVALID)
1433 eal_epoll_data_safe_free(event);
1439 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1440 int op, unsigned int vec, void *data)
1442 struct rte_epoll_event *rev;
1443 struct rte_epoll_data *epdata;
1445 unsigned int efd_idx;
1448 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1449 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1451 if (intr_handle == NULL || rte_intr_nb_efd_get(intr_handle) == 0 ||
1452 efd_idx >= (unsigned int)rte_intr_nb_efd_get(intr_handle)) {
1453 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1458 case RTE_INTR_EVENT_ADD:
1459 epfd_op = EPOLL_CTL_ADD;
1460 rev = rte_intr_elist_index_get(intr_handle, efd_idx);
1461 if (__atomic_load_n(&rev->status,
1462 __ATOMIC_RELAXED) != RTE_EPOLL_INVALID) {
1463 RTE_LOG(INFO, EAL, "Event already been added.\n");
1467 /* attach to intr vector fd */
1468 epdata = &rev->epdata;
1469 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1470 epdata->data = data;
1471 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1472 epdata->cb_arg = (void *)intr_handle;
1473 rc = rte_epoll_ctl(epfd, epfd_op,
1474 rte_intr_efds_index_get(intr_handle, efd_idx), rev);
1477 "efd %d associated with vec %d added on epfd %d"
1478 "\n", rev->fd, vec, epfd);
1482 case RTE_INTR_EVENT_DEL:
1483 epfd_op = EPOLL_CTL_DEL;
1484 rev = rte_intr_elist_index_get(intr_handle, efd_idx);
1485 if (__atomic_load_n(&rev->status,
1486 __ATOMIC_RELAXED) == RTE_EPOLL_INVALID) {
1487 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1491 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1496 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1504 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1507 struct rte_epoll_event *rev;
1509 for (i = 0; i < (uint32_t)rte_intr_nb_efd_get(intr_handle); i++) {
1510 rev = rte_intr_elist_index_get(intr_handle, i);
1511 if (__atomic_load_n(&rev->status,
1512 __ATOMIC_RELAXED) == RTE_EPOLL_INVALID)
1514 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1515 /* force free if the entry valid */
1516 eal_epoll_data_safe_free(rev);
1522 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1526 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1528 assert(nb_efd != 0);
1530 if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VFIO_MSIX) {
1531 for (i = 0; i < n; i++) {
1532 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1535 "can't setup eventfd, error %i (%s)\n",
1536 errno, strerror(errno));
1540 if (rte_intr_efds_index_set(intr_handle, i, fd))
1544 if (rte_intr_nb_efd_set(intr_handle, n))
1547 if (rte_intr_max_intr_set(intr_handle, NB_OTHER_INTR + n))
1549 } else if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV) {
1550 /* only check, initialization would be done in vdev driver.*/
1551 if ((uint64_t)rte_intr_efd_counter_size_get(intr_handle) >
1552 sizeof(union rte_intr_read_buffer)) {
1553 RTE_LOG(ERR, EAL, "the efd_counter_size is oversized");
1557 if (rte_intr_efds_index_set(intr_handle, 0, rte_intr_fd_get(intr_handle)))
1559 if (rte_intr_nb_efd_set(intr_handle, RTE_MIN(nb_efd, 1U)))
1561 if (rte_intr_max_intr_set(intr_handle, NB_OTHER_INTR))
1569 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1573 rte_intr_free_epoll_fd(intr_handle);
1574 if (rte_intr_max_intr_get(intr_handle) > rte_intr_nb_efd_get(intr_handle)) {
1575 for (i = 0; i < (uint32_t)rte_intr_nb_efd_get(intr_handle); i++)
1576 close(rte_intr_efds_index_get(intr_handle, i));
1578 rte_intr_nb_efd_set(intr_handle, 0);
1579 rte_intr_max_intr_set(intr_handle, 0);
1583 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1585 return !(!rte_intr_nb_efd_get(intr_handle));
1589 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1591 if (!rte_intr_dp_is_en(intr_handle))
1594 return !!(rte_intr_max_intr_get(intr_handle) -
1595 rte_intr_nb_efd_get(intr_handle));
1599 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1601 if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VFIO_MSIX)
1604 if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV)
1610 int rte_thread_is_intr(void)
1612 return pthread_equal(intr_thread, pthread_self());