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 case RTE_INTR_HANDLE_VFIO_MSIX:
951 case RTE_INTR_HANDLE_VFIO_MSI:
952 case RTE_INTR_HANDLE_VFIO_LEGACY:
953 bytes_read = sizeof(buf.vfio_intr_count);
955 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
956 case RTE_INTR_HANDLE_VFIO_REQ:
962 case RTE_INTR_HANDLE_VDEV:
963 case RTE_INTR_HANDLE_EXT:
967 case RTE_INTR_HANDLE_DEV_EVENT:
976 if (bytes_read > 0) {
978 * read out to clear the ready-to-be-read flag
981 bytes_read = read(events[n].data.fd, &buf, bytes_read);
982 if (bytes_read < 0) {
983 if (errno == EINTR || errno == EWOULDBLOCK)
986 RTE_LOG(ERR, EAL, "Error reading from file "
987 "descriptor %d: %s\n",
991 * The device is unplugged or buggy, remove
992 * it as an interrupt source and return to
993 * force the wait list to be rebuilt.
995 rte_spinlock_lock(&intr_lock);
996 TAILQ_REMOVE(&intr_sources, src, next);
997 rte_spinlock_unlock(&intr_lock);
999 for (cb = TAILQ_FIRST(&src->callbacks); cb;
1001 next = TAILQ_NEXT(cb, next);
1002 TAILQ_REMOVE(&src->callbacks, cb, next);
1005 rte_intr_instance_free(src->intr_handle);
1008 } else if (bytes_read == 0)
1009 RTE_LOG(ERR, EAL, "Read nothing from file "
1010 "descriptor %d\n", events[n].data.fd);
1015 /* grab a lock, again to call callbacks and update status. */
1016 rte_spinlock_lock(&intr_lock);
1020 /* Finally, call all callbacks. */
1021 TAILQ_FOREACH(cb, &src->callbacks, next) {
1023 /* make a copy and unlock. */
1025 rte_spinlock_unlock(&intr_lock);
1027 /* call the actual callback */
1028 active_cb.cb_fn(active_cb.cb_arg);
1030 /*get the lock back. */
1031 rte_spinlock_lock(&intr_lock);
1034 /* we done with that interrupt source, release it. */
1039 /* check if any callback are supposed to be removed */
1040 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
1041 next = TAILQ_NEXT(cb, next);
1042 if (cb->pending_delete) {
1043 TAILQ_REMOVE(&src->callbacks, cb, next);
1045 cb->ucb_fn(src->intr_handle, cb->cb_arg);
1051 /* all callbacks for that source are removed. */
1052 if (TAILQ_EMPTY(&src->callbacks)) {
1053 TAILQ_REMOVE(&intr_sources, src, next);
1054 rte_intr_instance_free(src->intr_handle);
1058 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
1059 if (rv > 0 && write(intr_pipe.writefd, "1", 1) < 0) {
1060 rte_spinlock_unlock(&intr_lock);
1064 rte_spinlock_unlock(&intr_lock);
1071 * It handles all the interrupts.
1074 * epoll file descriptor.
1076 * The number of file descriptors added in epoll.
1082 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
1084 struct epoll_event events[totalfds];
1088 nfds = epoll_wait(pfd, events, totalfds,
1089 EAL_INTR_EPOLL_WAIT_FOREVER);
1090 /* epoll_wait fail */
1095 "epoll_wait returns with fail\n");
1098 /* epoll_wait timeout, will never happens here */
1101 /* epoll_wait has at least one fd ready to read */
1102 if (eal_intr_process_interrupts(events, nfds) < 0)
1108 * It builds/rebuilds up the epoll file descriptor with all the
1109 * file descriptors being waited on. Then handles the interrupts.
1117 static __rte_noreturn void *
1118 eal_intr_thread_main(__rte_unused void *arg)
1120 /* host thread, never break out */
1122 /* build up the epoll fd with all descriptors we are to
1123 * wait on then pass it to the handle_interrupts function
1125 static struct epoll_event pipe_event = {
1126 .events = EPOLLIN | EPOLLPRI,
1128 struct rte_intr_source *src;
1129 unsigned numfds = 0;
1131 /* create epoll fd */
1132 int pfd = epoll_create(1);
1134 rte_panic("Cannot create epoll instance\n");
1136 pipe_event.data.fd = intr_pipe.readfd;
1138 * add pipe fd into wait list, this pipe is used to
1139 * rebuild the wait list.
1141 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
1143 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
1144 intr_pipe.readfd, strerror(errno));
1148 rte_spinlock_lock(&intr_lock);
1150 TAILQ_FOREACH(src, &intr_sources, next) {
1151 struct epoll_event ev;
1153 if (src->callbacks.tqh_first == NULL)
1154 continue; /* skip those with no callbacks */
1155 memset(&ev, 0, sizeof(ev));
1156 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
1157 ev.data.fd = rte_intr_fd_get(src->intr_handle);
1160 * add all the uio device file descriptor
1163 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
1164 rte_intr_fd_get(src->intr_handle), &ev) < 0) {
1165 rte_panic("Error adding fd %d epoll_ctl, %s\n",
1166 rte_intr_fd_get(src->intr_handle),
1172 rte_spinlock_unlock(&intr_lock);
1173 /* serve the interrupt */
1174 eal_intr_handle_interrupts(pfd, numfds);
1177 * when we return, we need to rebuild the
1178 * list of fds to monitor.
1185 rte_eal_intr_init(void)
1189 /* init the global interrupt source head */
1190 TAILQ_INIT(&intr_sources);
1193 * create a pipe which will be waited by epoll and notified to
1194 * rebuild the wait list of epoll.
1196 if (pipe(intr_pipe.pipefd) < 0) {
1201 /* create the host thread to wait/handle the interrupt */
1202 ret = rte_ctrl_thread_create(&intr_thread, "eal-intr-thread", NULL,
1203 eal_intr_thread_main, NULL);
1207 "Failed to create thread for interrupt handling\n");
1214 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
1216 union rte_intr_read_buffer buf;
1220 switch (rte_intr_type_get(intr_handle)) {
1221 case RTE_INTR_HANDLE_UIO:
1222 case RTE_INTR_HANDLE_UIO_INTX:
1223 bytes_read = sizeof(buf.uio_intr_count);
1226 case RTE_INTR_HANDLE_VFIO_MSIX:
1227 case RTE_INTR_HANDLE_VFIO_MSI:
1228 case RTE_INTR_HANDLE_VFIO_LEGACY:
1229 bytes_read = sizeof(buf.vfio_intr_count);
1232 case RTE_INTR_HANDLE_VDEV:
1233 bytes_read = rte_intr_efd_counter_size_get(intr_handle);
1234 /* For vdev, number of bytes to read is set by driver */
1236 case RTE_INTR_HANDLE_EXT:
1240 RTE_LOG(INFO, EAL, "unexpected intr type\n");
1245 * read out to clear the ready-to-be-read flag
1248 if (bytes_read == 0)
1251 nbytes = read(fd, &buf, bytes_read);
1253 if (errno == EINTR || errno == EWOULDBLOCK ||
1257 "Error reading from fd %d: %s\n",
1258 fd, strerror(errno));
1259 } else if (nbytes == 0)
1260 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
1266 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
1267 struct rte_epoll_event *events)
1269 unsigned int i, count = 0;
1270 struct rte_epoll_event *rev;
1271 uint32_t valid_status;
1273 for (i = 0; i < n; i++) {
1274 rev = evs[i].data.ptr;
1275 valid_status = RTE_EPOLL_VALID;
1276 /* ACQUIRE memory ordering here pairs with RELEASE
1277 * ordering below acting as a lock to synchronize
1278 * the event data updating.
1280 if (!rev || !__atomic_compare_exchange_n(&rev->status,
1281 &valid_status, RTE_EPOLL_EXEC, 0,
1282 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
1285 events[count].status = RTE_EPOLL_VALID;
1286 events[count].fd = rev->fd;
1287 events[count].epfd = rev->epfd;
1288 events[count].epdata.event = evs[i].events;
1289 events[count].epdata.data = rev->epdata.data;
1290 if (rev->epdata.cb_fun)
1291 rev->epdata.cb_fun(rev->fd,
1292 rev->epdata.cb_arg);
1294 /* the status update should be observed after
1295 * the other fields change.
1297 __atomic_store_n(&rev->status, RTE_EPOLL_VALID,
1305 eal_init_tls_epfd(void)
1307 int pfd = epoll_create(255);
1311 "Cannot create epoll instance\n");
1318 rte_intr_tls_epfd(void)
1320 if (RTE_PER_LCORE(_epfd) == -1)
1321 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1323 return RTE_PER_LCORE(_epfd);
1327 eal_epoll_wait(int epfd, struct rte_epoll_event *events,
1328 int maxevents, int timeout, bool interruptible)
1330 struct epoll_event evs[maxevents];
1334 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1338 /* using per thread epoll fd */
1339 if (epfd == RTE_EPOLL_PER_THREAD)
1340 epfd = rte_intr_tls_epfd();
1343 rc = epoll_wait(epfd, evs, maxevents, timeout);
1344 if (likely(rc > 0)) {
1345 /* epoll_wait has at least one fd ready to read */
1346 rc = eal_epoll_process_event(evs, rc, events);
1348 } else if (rc < 0) {
1349 if (errno == EINTR) {
1355 /* epoll_wait fail */
1356 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1361 /* rc == 0, epoll_wait timed out */
1370 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1371 int maxevents, int timeout)
1373 return eal_epoll_wait(epfd, events, maxevents, timeout, false);
1377 rte_epoll_wait_interruptible(int epfd, struct rte_epoll_event *events,
1378 int maxevents, int timeout)
1380 return eal_epoll_wait(epfd, events, maxevents, timeout, true);
1384 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1386 uint32_t valid_status = RTE_EPOLL_VALID;
1388 while (!__atomic_compare_exchange_n(&ev->status, &valid_status,
1389 RTE_EPOLL_INVALID, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
1390 while (__atomic_load_n(&ev->status,
1391 __ATOMIC_RELAXED) != RTE_EPOLL_VALID)
1393 valid_status = RTE_EPOLL_VALID;
1395 memset(&ev->epdata, 0, sizeof(ev->epdata));
1401 rte_epoll_ctl(int epfd, int op, int fd,
1402 struct rte_epoll_event *event)
1404 struct epoll_event ev;
1407 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1411 /* using per thread epoll fd */
1412 if (epfd == RTE_EPOLL_PER_THREAD)
1413 epfd = rte_intr_tls_epfd();
1415 if (op == EPOLL_CTL_ADD) {
1416 __atomic_store_n(&event->status, RTE_EPOLL_VALID,
1418 event->fd = fd; /* ignore fd in event */
1420 ev.data.ptr = (void *)event;
1423 ev.events = event->epdata.event;
1424 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1425 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1426 op, fd, strerror(errno));
1427 if (op == EPOLL_CTL_ADD)
1428 /* rollback status when CTL_ADD fail */
1429 __atomic_store_n(&event->status, RTE_EPOLL_INVALID,
1434 if (op == EPOLL_CTL_DEL && __atomic_load_n(&event->status,
1435 __ATOMIC_RELAXED) != RTE_EPOLL_INVALID)
1436 eal_epoll_data_safe_free(event);
1442 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1443 int op, unsigned int vec, void *data)
1445 struct rte_epoll_event *rev;
1446 struct rte_epoll_data *epdata;
1448 unsigned int efd_idx;
1451 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1452 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1454 if (intr_handle == NULL || rte_intr_nb_efd_get(intr_handle) == 0 ||
1455 efd_idx >= (unsigned int)rte_intr_nb_efd_get(intr_handle)) {
1456 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1461 case RTE_INTR_EVENT_ADD:
1462 epfd_op = EPOLL_CTL_ADD;
1463 rev = rte_intr_elist_index_get(intr_handle, efd_idx);
1464 if (__atomic_load_n(&rev->status,
1465 __ATOMIC_RELAXED) != RTE_EPOLL_INVALID) {
1466 RTE_LOG(INFO, EAL, "Event already been added.\n");
1470 /* attach to intr vector fd */
1471 epdata = &rev->epdata;
1472 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1473 epdata->data = data;
1474 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1475 epdata->cb_arg = (void *)intr_handle;
1476 rc = rte_epoll_ctl(epfd, epfd_op,
1477 rte_intr_efds_index_get(intr_handle, efd_idx), rev);
1480 "efd %d associated with vec %d added on epfd %d"
1481 "\n", rev->fd, vec, epfd);
1485 case RTE_INTR_EVENT_DEL:
1486 epfd_op = EPOLL_CTL_DEL;
1487 rev = rte_intr_elist_index_get(intr_handle, efd_idx);
1488 if (__atomic_load_n(&rev->status,
1489 __ATOMIC_RELAXED) == RTE_EPOLL_INVALID) {
1490 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1494 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1499 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1507 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1510 struct rte_epoll_event *rev;
1512 for (i = 0; i < (uint32_t)rte_intr_nb_efd_get(intr_handle); i++) {
1513 rev = rte_intr_elist_index_get(intr_handle, i);
1514 if (__atomic_load_n(&rev->status,
1515 __ATOMIC_RELAXED) == RTE_EPOLL_INVALID)
1517 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1518 /* force free if the entry valid */
1519 eal_epoll_data_safe_free(rev);
1525 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1529 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1531 assert(nb_efd != 0);
1533 if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VFIO_MSIX) {
1534 for (i = 0; i < n; i++) {
1535 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1538 "can't setup eventfd, error %i (%s)\n",
1539 errno, strerror(errno));
1543 if (rte_intr_efds_index_set(intr_handle, i, fd))
1547 if (rte_intr_nb_efd_set(intr_handle, n))
1550 if (rte_intr_max_intr_set(intr_handle, NB_OTHER_INTR + n))
1552 } else if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV) {
1553 /* only check, initialization would be done in vdev driver.*/
1554 if ((uint64_t)rte_intr_efd_counter_size_get(intr_handle) >
1555 sizeof(union rte_intr_read_buffer)) {
1556 RTE_LOG(ERR, EAL, "the efd_counter_size is oversized");
1560 if (rte_intr_efds_index_set(intr_handle, 0, rte_intr_fd_get(intr_handle)))
1562 if (rte_intr_nb_efd_set(intr_handle, RTE_MIN(nb_efd, 1U)))
1564 if (rte_intr_max_intr_set(intr_handle, NB_OTHER_INTR))
1572 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1576 rte_intr_free_epoll_fd(intr_handle);
1577 if (rte_intr_max_intr_get(intr_handle) > rte_intr_nb_efd_get(intr_handle)) {
1578 for (i = 0; i < (uint32_t)rte_intr_nb_efd_get(intr_handle); i++)
1579 close(rte_intr_efds_index_get(intr_handle, i));
1581 rte_intr_nb_efd_set(intr_handle, 0);
1582 rte_intr_max_intr_set(intr_handle, 0);
1586 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1588 return !(!rte_intr_nb_efd_get(intr_handle));
1592 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1594 if (!rte_intr_dp_is_en(intr_handle))
1597 return !!(rte_intr_max_intr_get(intr_handle) -
1598 rte_intr_nb_efd_get(intr_handle));
1602 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1604 if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VFIO_MSIX)
1607 if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV)
1613 int rte_thread_is_intr(void)
1615 return pthread_equal(intr_thread, pthread_self());