4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/queue.h>
44 #include <sys/epoll.h>
45 #include <sys/signalfd.h>
46 #include <sys/ioctl.h>
48 #include <rte_common.h>
49 #include <rte_interrupts.h>
50 #include <rte_memory.h>
51 #include <rte_memzone.h>
52 #include <rte_launch.h>
53 #include <rte_tailq.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
60 #include <rte_debug.h>
62 #include <rte_mempool.h>
64 #include <rte_malloc.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
68 #include "eal_private.h"
71 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
87 * union buffer for reading on different devices
89 union rte_intr_read_buffer {
90 int uio_intr_count; /* for uio device */
92 uint64_t vfio_intr_count; /* for vfio device */
94 uint64_t timerfd_num; /* for timerfd */
95 char charbuf[16]; /* for others */
98 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
99 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
101 struct rte_intr_callback {
102 TAILQ_ENTRY(rte_intr_callback) next;
103 rte_intr_callback_fn cb_fn; /**< callback address */
104 void *cb_arg; /**< parameter for callback */
107 struct rte_intr_source {
108 TAILQ_ENTRY(rte_intr_source) next;
109 struct rte_intr_handle intr_handle; /**< interrupt handle */
110 struct rte_intr_cb_list callbacks; /**< user callbacks */
114 /* global spinlock for interrupt data operation */
115 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
117 /* union buffer for pipe read/write */
118 static union intr_pipefds intr_pipe;
120 /* interrupt sources list */
121 static struct rte_intr_source_list intr_sources;
123 /* interrupt handling thread */
124 static pthread_t intr_thread;
126 /* VFIO interrupts */
129 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
131 /* enable legacy (INTx) interrupts */
133 vfio_enable_intx(struct rte_intr_handle *intr_handle) {
134 struct vfio_irq_set *irq_set;
135 char irq_set_buf[IRQ_SET_BUF_LEN];
139 len = sizeof(irq_set_buf);
142 irq_set = (struct vfio_irq_set *) irq_set_buf;
143 irq_set->argsz = len;
145 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
146 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
148 fd_ptr = (int *) &irq_set->data;
149 *fd_ptr = intr_handle->fd;
151 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
154 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
159 /* unmask INTx after enabling */
160 memset(irq_set, 0, len);
161 len = sizeof(struct vfio_irq_set);
162 irq_set->argsz = len;
164 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
165 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
168 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
171 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
178 /* disable legacy (INTx) interrupts */
180 vfio_disable_intx(struct rte_intr_handle *intr_handle) {
181 struct vfio_irq_set *irq_set;
182 char irq_set_buf[IRQ_SET_BUF_LEN];
185 len = sizeof(struct vfio_irq_set);
187 /* mask interrupts before disabling */
188 irq_set = (struct vfio_irq_set *) irq_set_buf;
189 irq_set->argsz = len;
191 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
192 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
195 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
198 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
204 memset(irq_set, 0, len);
205 irq_set->argsz = len;
207 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
208 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
211 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
215 "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
221 /* enable MSI-X interrupts */
223 vfio_enable_msi(struct rte_intr_handle *intr_handle) {
225 char irq_set_buf[IRQ_SET_BUF_LEN];
226 struct vfio_irq_set *irq_set;
229 len = sizeof(irq_set_buf);
231 irq_set = (struct vfio_irq_set *) irq_set_buf;
232 irq_set->argsz = len;
234 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
235 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
237 fd_ptr = (int *) &irq_set->data;
238 *fd_ptr = intr_handle->fd;
240 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
243 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
248 /* manually trigger interrupt to enable it */
249 memset(irq_set, 0, len);
250 len = sizeof(struct vfio_irq_set);
251 irq_set->argsz = len;
253 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
254 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
257 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
260 RTE_LOG(ERR, EAL, "Error triggering MSI interrupts for fd %d\n",
267 /* disable MSI-X interrupts */
269 vfio_disable_msi(struct rte_intr_handle *intr_handle) {
270 struct vfio_irq_set *irq_set;
271 char irq_set_buf[IRQ_SET_BUF_LEN];
274 len = sizeof(struct vfio_irq_set);
276 irq_set = (struct vfio_irq_set *) irq_set_buf;
277 irq_set->argsz = len;
279 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
280 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
283 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
287 "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
292 /* enable MSI-X interrupts */
294 vfio_enable_msix(struct rte_intr_handle *intr_handle) {
296 char irq_set_buf[IRQ_SET_BUF_LEN];
297 struct vfio_irq_set *irq_set;
300 len = sizeof(irq_set_buf);
302 irq_set = (struct vfio_irq_set *) irq_set_buf;
303 irq_set->argsz = len;
305 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
306 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
308 fd_ptr = (int *) &irq_set->data;
309 *fd_ptr = intr_handle->fd;
311 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
314 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
319 /* manually trigger interrupt to enable it */
320 memset(irq_set, 0, len);
321 len = sizeof(struct vfio_irq_set);
322 irq_set->argsz = len;
324 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
325 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
328 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
331 RTE_LOG(ERR, EAL, "Error triggering MSI-X interrupts for fd %d\n",
338 /* disable MSI-X interrupts */
340 vfio_disable_msix(struct rte_intr_handle *intr_handle) {
341 struct vfio_irq_set *irq_set;
342 char irq_set_buf[IRQ_SET_BUF_LEN];
345 len = sizeof(struct vfio_irq_set);
347 irq_set = (struct vfio_irq_set *) irq_set_buf;
348 irq_set->argsz = len;
350 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
351 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
354 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
358 "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
365 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
366 rte_intr_callback_fn cb, void *cb_arg)
368 int ret, wake_thread;
369 struct rte_intr_source *src;
370 struct rte_intr_callback *callback;
374 /* first do parameter checking */
375 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
377 "Registering with invalid input parameter\n");
381 /* allocate a new interrupt callback entity */
382 callback = rte_zmalloc("interrupt callback list",
383 sizeof(*callback), 0);
384 if (callback == NULL) {
385 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
388 callback->cb_fn = cb;
389 callback->cb_arg = cb_arg;
391 rte_spinlock_lock(&intr_lock);
393 /* check if there is at least one callback registered for the fd */
394 TAILQ_FOREACH(src, &intr_sources, next) {
395 if (src->intr_handle.fd == intr_handle->fd) {
396 /* we had no interrupts for this */
397 if TAILQ_EMPTY(&src->callbacks)
400 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
406 /* no existing callbacks for this - add new source */
408 if ((src = rte_zmalloc("interrupt source list",
409 sizeof(*src), 0)) == NULL) {
410 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
414 src->intr_handle = *intr_handle;
415 TAILQ_INIT(&src->callbacks);
416 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
417 TAILQ_INSERT_TAIL(&intr_sources, src, next);
423 rte_spinlock_unlock(&intr_lock);
426 * check if need to notify the pipe fd waited by epoll_wait to
427 * rebuild the wait list.
430 if (write(intr_pipe.writefd, "1", 1) < 0)
437 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
438 rte_intr_callback_fn cb_fn, void *cb_arg)
441 struct rte_intr_source *src;
442 struct rte_intr_callback *cb, *next;
444 /* do parameter checking first */
445 if (intr_handle == NULL || intr_handle->fd < 0) {
447 "Unregistering with invalid input parameter\n");
451 rte_spinlock_lock(&intr_lock);
453 /* check if the insterrupt source for the fd is existent */
454 TAILQ_FOREACH(src, &intr_sources, next)
455 if (src->intr_handle.fd == intr_handle->fd)
458 /* No interrupt source registered for the fd */
462 /* interrupt source has some active callbacks right now. */
463 } else if (src->active != 0) {
470 /*walk through the callbacks and remove all that match. */
471 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
473 next = TAILQ_NEXT(cb, next);
475 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
476 cb->cb_arg == cb_arg)) {
477 TAILQ_REMOVE(&src->callbacks, cb, next);
483 /* all callbacks for that source are removed. */
484 if (TAILQ_EMPTY(&src->callbacks)) {
485 TAILQ_REMOVE(&intr_sources, src, next);
490 rte_spinlock_unlock(&intr_lock);
492 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
493 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
501 rte_intr_enable(struct rte_intr_handle *intr_handle)
505 if (!intr_handle || intr_handle->fd < 0)
508 switch (intr_handle->type){
509 /* write to the uio fd to enable the interrupt */
510 case RTE_INTR_HANDLE_UIO:
511 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
513 "Error enabling interrupts for fd %d\n",
518 /* not used at this moment */
519 case RTE_INTR_HANDLE_ALARM:
522 case RTE_INTR_HANDLE_VFIO_MSIX:
523 if (vfio_enable_msix(intr_handle))
526 case RTE_INTR_HANDLE_VFIO_MSI:
527 if (vfio_enable_msi(intr_handle))
530 case RTE_INTR_HANDLE_VFIO_LEGACY:
531 if (vfio_enable_intx(intr_handle))
535 /* unknown handle type */
538 "Unknown handle type of fd %d\n",
547 rte_intr_disable(struct rte_intr_handle *intr_handle)
551 if (!intr_handle || intr_handle->fd < 0)
554 switch (intr_handle->type){
555 /* write to the uio fd to disable the interrupt */
556 case RTE_INTR_HANDLE_UIO:
557 if (write(intr_handle->fd, &value, sizeof(value)) < 0){
559 "Error disabling interrupts for fd %d\n",
564 /* not used at this moment */
565 case RTE_INTR_HANDLE_ALARM:
568 case RTE_INTR_HANDLE_VFIO_MSIX:
569 if (vfio_disable_msix(intr_handle))
572 case RTE_INTR_HANDLE_VFIO_MSI:
573 if (vfio_disable_msi(intr_handle))
576 case RTE_INTR_HANDLE_VFIO_LEGACY:
577 if (vfio_disable_intx(intr_handle))
581 /* unknown handle type */
584 "Unknown handle type of fd %d\n",
593 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
596 struct rte_intr_source *src;
597 struct rte_intr_callback *cb;
598 union rte_intr_read_buffer buf;
599 struct rte_intr_callback active_cb;
601 for (n = 0; n < nfds; n++) {
604 * if the pipe fd is ready to read, return out to
605 * rebuild the wait list.
607 if (events[n].data.fd == intr_pipe.readfd){
608 int r = read(intr_pipe.readfd, buf.charbuf,
609 sizeof(buf.charbuf));
613 rte_spinlock_lock(&intr_lock);
614 TAILQ_FOREACH(src, &intr_sources, next)
615 if (src->intr_handle.fd ==
619 rte_spinlock_unlock(&intr_lock);
623 /* mark this interrupt source as active and release the lock. */
625 rte_spinlock_unlock(&intr_lock);
627 /* set the length to be read dor different handle type */
628 switch (src->intr_handle.type) {
629 case RTE_INTR_HANDLE_UIO:
630 bytes_read = sizeof(buf.uio_intr_count);
632 case RTE_INTR_HANDLE_ALARM:
633 bytes_read = sizeof(buf.timerfd_num);
636 case RTE_INTR_HANDLE_VFIO_MSIX:
637 case RTE_INTR_HANDLE_VFIO_MSI:
638 case RTE_INTR_HANDLE_VFIO_LEGACY:
639 bytes_read = sizeof(buf.vfio_intr_count);
648 * read out to clear the ready-to-be-read flag
651 bytes_read = read(events[n].data.fd, &buf, bytes_read);
654 RTE_LOG(ERR, EAL, "Error reading from file "
655 "descriptor %d: %s\n", events[n].data.fd,
657 else if (bytes_read == 0)
658 RTE_LOG(ERR, EAL, "Read nothing from file "
659 "descriptor %d\n", events[n].data.fd);
661 /* grab a lock, again to call callbacks and update status. */
662 rte_spinlock_lock(&intr_lock);
664 if (bytes_read > 0) {
666 /* Finally, call all callbacks. */
667 TAILQ_FOREACH(cb, &src->callbacks, next) {
669 /* make a copy and unlock. */
671 rte_spinlock_unlock(&intr_lock);
673 /* call the actual callback */
674 active_cb.cb_fn(&src->intr_handle,
677 /*get the lock back. */
678 rte_spinlock_lock(&intr_lock);
682 /* we done with that interrupt source, release it. */
684 rte_spinlock_unlock(&intr_lock);
691 * It handles all the interrupts.
694 * epoll file descriptor.
696 * The number of file descriptors added in epoll.
702 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
704 struct epoll_event events[totalfds];
708 nfds = epoll_wait(pfd, events, totalfds,
709 EAL_INTR_EPOLL_WAIT_FOREVER);
710 /* epoll_wait fail */
715 "epoll_wait returns with fail\n");
718 /* epoll_wait timeout, will never happens here */
721 /* epoll_wait has at least one fd ready to read */
722 if (eal_intr_process_interrupts(events, nfds) < 0)
728 * It builds/rebuilds up the epoll file descriptor with all the
729 * file descriptors being waited on. Then handles the interrupts.
737 static __attribute__((noreturn)) void *
738 eal_intr_thread_main(__rte_unused void *arg)
740 struct epoll_event ev;
742 /* host thread, never break out */
744 /* build up the epoll fd with all descriptors we are to
745 * wait on then pass it to the handle_interrupts function
747 static struct epoll_event pipe_event = {
748 .events = EPOLLIN | EPOLLPRI,
750 struct rte_intr_source *src;
753 /* create epoll fd */
754 int pfd = epoll_create(1);
756 rte_panic("Cannot create epoll instance\n");
758 pipe_event.data.fd = intr_pipe.readfd;
760 * add pipe fd into wait list, this pipe is used to
761 * rebuild the wait list.
763 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
765 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
766 intr_pipe.readfd, strerror(errno));
770 rte_spinlock_lock(&intr_lock);
772 TAILQ_FOREACH(src, &intr_sources, next) {
773 if (src->callbacks.tqh_first == NULL)
774 continue; /* skip those with no callbacks */
775 ev.events = EPOLLIN | EPOLLPRI;
776 ev.data.fd = src->intr_handle.fd;
779 * add all the uio device file descriptor
782 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
783 src->intr_handle.fd, &ev) < 0){
784 rte_panic("Error adding fd %d epoll_ctl, %s\n",
785 src->intr_handle.fd, strerror(errno));
790 rte_spinlock_unlock(&intr_lock);
791 /* serve the interrupt */
792 eal_intr_handle_interrupts(pfd, numfds);
795 * when we return, we need to rebuild the
796 * list of fds to monitor.
803 rte_eal_intr_init(void)
807 /* init the global interrupt source head */
808 TAILQ_INIT(&intr_sources);
811 * create a pipe which will be waited by epoll and notified to
812 * rebuild the wait list of epoll.
814 if (pipe(intr_pipe.pipefd) < 0)
817 /* create the host thread to wait/handle the interrupt */
818 ret = pthread_create(&intr_thread, NULL,
819 eal_intr_thread_main, NULL);
822 "Failed to create thread for interrupt handling\n");