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>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
59 #include <rte_debug.h>
61 #include <rte_mempool.h>
63 #include <rte_malloc.h>
64 #include <rte_errno.h>
65 #include <rte_spinlock.h>
67 #include "eal_private.h"
70 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
86 * union buffer for reading on different devices
88 union rte_intr_read_buffer {
89 int uio_intr_count; /* for uio device */
91 uint64_t vfio_intr_count; /* for vfio device */
93 uint64_t timerfd_num; /* for timerfd */
94 char charbuf[16]; /* for others */
97 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
98 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
100 struct rte_intr_callback {
101 TAILQ_ENTRY(rte_intr_callback) next;
102 rte_intr_callback_fn cb_fn; /**< callback address */
103 void *cb_arg; /**< parameter for callback */
106 struct rte_intr_source {
107 TAILQ_ENTRY(rte_intr_source) next;
108 struct rte_intr_handle intr_handle; /**< interrupt handle */
109 struct rte_intr_cb_list callbacks; /**< user callbacks */
113 /* global spinlock for interrupt data operation */
114 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
116 /* union buffer for pipe read/write */
117 static union intr_pipefds intr_pipe;
119 /* interrupt sources list */
120 static struct rte_intr_source_list intr_sources;
122 /* interrupt handling thread */
123 static pthread_t intr_thread;
125 /* VFIO interrupts */
128 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
130 /* enable legacy (INTx) interrupts */
132 vfio_enable_intx(struct rte_intr_handle *intr_handle) {
133 struct vfio_irq_set *irq_set;
134 char irq_set_buf[IRQ_SET_BUF_LEN];
138 len = sizeof(irq_set_buf);
141 irq_set = (struct vfio_irq_set *) irq_set_buf;
142 irq_set->argsz = len;
144 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
145 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
147 fd_ptr = (int *) &irq_set->data;
148 *fd_ptr = intr_handle->fd;
150 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
153 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
158 /* unmask INTx after enabling */
159 memset(irq_set, 0, len);
160 len = sizeof(struct vfio_irq_set);
161 irq_set->argsz = len;
163 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
164 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
167 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
170 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
177 /* disable legacy (INTx) interrupts */
179 vfio_disable_intx(struct rte_intr_handle *intr_handle) {
180 struct vfio_irq_set *irq_set;
181 char irq_set_buf[IRQ_SET_BUF_LEN];
184 len = sizeof(struct vfio_irq_set);
186 /* mask interrupts before disabling */
187 irq_set = (struct vfio_irq_set *) irq_set_buf;
188 irq_set->argsz = len;
190 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
191 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
194 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
197 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
203 memset(irq_set, 0, len);
204 irq_set->argsz = len;
206 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
207 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
210 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
214 "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
220 /* enable MSI-X interrupts */
222 vfio_enable_msi(struct rte_intr_handle *intr_handle) {
224 char irq_set_buf[IRQ_SET_BUF_LEN];
225 struct vfio_irq_set *irq_set;
228 len = sizeof(irq_set_buf);
230 irq_set = (struct vfio_irq_set *) irq_set_buf;
231 irq_set->argsz = len;
233 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
234 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
236 fd_ptr = (int *) &irq_set->data;
237 *fd_ptr = intr_handle->fd;
239 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
242 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
247 /* manually trigger interrupt to enable it */
248 memset(irq_set, 0, len);
249 len = sizeof(struct vfio_irq_set);
250 irq_set->argsz = len;
252 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
253 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
256 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
259 RTE_LOG(ERR, EAL, "Error triggering MSI interrupts for fd %d\n",
266 /* disable MSI-X interrupts */
268 vfio_disable_msi(struct rte_intr_handle *intr_handle) {
269 struct vfio_irq_set *irq_set;
270 char irq_set_buf[IRQ_SET_BUF_LEN];
273 len = sizeof(struct vfio_irq_set);
275 irq_set = (struct vfio_irq_set *) irq_set_buf;
276 irq_set->argsz = len;
278 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
279 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
282 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
286 "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
291 /* enable MSI-X interrupts */
293 vfio_enable_msix(struct rte_intr_handle *intr_handle) {
295 char irq_set_buf[IRQ_SET_BUF_LEN];
296 struct vfio_irq_set *irq_set;
299 len = sizeof(irq_set_buf);
301 irq_set = (struct vfio_irq_set *) irq_set_buf;
302 irq_set->argsz = len;
304 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
305 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
307 fd_ptr = (int *) &irq_set->data;
308 *fd_ptr = intr_handle->fd;
310 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
313 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
318 /* manually trigger interrupt to enable it */
319 memset(irq_set, 0, len);
320 len = sizeof(struct vfio_irq_set);
321 irq_set->argsz = len;
323 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
324 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
327 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
330 RTE_LOG(ERR, EAL, "Error triggering MSI-X interrupts for fd %d\n",
337 /* disable MSI-X interrupts */
339 vfio_disable_msix(struct rte_intr_handle *intr_handle) {
340 struct vfio_irq_set *irq_set;
341 char irq_set_buf[IRQ_SET_BUF_LEN];
344 len = sizeof(struct vfio_irq_set);
346 irq_set = (struct vfio_irq_set *) irq_set_buf;
347 irq_set->argsz = len;
349 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
350 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
353 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
357 "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
364 uio_intx_intr_disable(struct rte_intr_handle *intr_handle)
366 unsigned char command_high;
368 /* use UIO config file descriptor for uio_pci_generic */
369 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
371 "Error reading interrupts status for fd %d\n",
372 intr_handle->uio_cfg_fd);
375 /* disable interrupts */
377 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
379 "Error disabling interrupts for fd %d\n",
380 intr_handle->uio_cfg_fd);
388 uio_intx_intr_enable(struct rte_intr_handle *intr_handle)
390 unsigned char command_high;
392 /* use UIO config file descriptor for uio_pci_generic */
393 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
395 "Error reading interrupts status for fd %d\n",
396 intr_handle->uio_cfg_fd);
399 /* enable interrupts */
400 command_high &= ~0x4;
401 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
403 "Error enabling interrupts for fd %d\n",
404 intr_handle->uio_cfg_fd);
412 uio_intr_disable(struct rte_intr_handle *intr_handle)
416 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
418 "Error disabling interrupts for fd %d (%s)\n",
419 intr_handle->fd, strerror(errno));
426 uio_intr_enable(struct rte_intr_handle *intr_handle)
430 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
432 "Error enabling interrupts for fd %d (%s)\n",
433 intr_handle->fd, strerror(errno));
440 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
441 rte_intr_callback_fn cb, void *cb_arg)
443 int ret, wake_thread;
444 struct rte_intr_source *src;
445 struct rte_intr_callback *callback;
449 /* first do parameter checking */
450 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
452 "Registering with invalid input parameter\n");
456 /* allocate a new interrupt callback entity */
457 callback = rte_zmalloc("interrupt callback list",
458 sizeof(*callback), 0);
459 if (callback == NULL) {
460 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
463 callback->cb_fn = cb;
464 callback->cb_arg = cb_arg;
466 rte_spinlock_lock(&intr_lock);
468 /* check if there is at least one callback registered for the fd */
469 TAILQ_FOREACH(src, &intr_sources, next) {
470 if (src->intr_handle.fd == intr_handle->fd) {
471 /* we had no interrupts for this */
472 if TAILQ_EMPTY(&src->callbacks)
475 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
481 /* no existing callbacks for this - add new source */
483 if ((src = rte_zmalloc("interrupt source list",
484 sizeof(*src), 0)) == NULL) {
485 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
489 src->intr_handle = *intr_handle;
490 TAILQ_INIT(&src->callbacks);
491 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
492 TAILQ_INSERT_TAIL(&intr_sources, src, next);
498 rte_spinlock_unlock(&intr_lock);
501 * check if need to notify the pipe fd waited by epoll_wait to
502 * rebuild the wait list.
505 if (write(intr_pipe.writefd, "1", 1) < 0)
512 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
513 rte_intr_callback_fn cb_fn, void *cb_arg)
516 struct rte_intr_source *src;
517 struct rte_intr_callback *cb, *next;
519 /* do parameter checking first */
520 if (intr_handle == NULL || intr_handle->fd < 0) {
522 "Unregistering with invalid input parameter\n");
526 rte_spinlock_lock(&intr_lock);
528 /* check if the insterrupt source for the fd is existent */
529 TAILQ_FOREACH(src, &intr_sources, next)
530 if (src->intr_handle.fd == intr_handle->fd)
533 /* No interrupt source registered for the fd */
537 /* interrupt source has some active callbacks right now. */
538 } else if (src->active != 0) {
545 /*walk through the callbacks and remove all that match. */
546 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
548 next = TAILQ_NEXT(cb, next);
550 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
551 cb->cb_arg == cb_arg)) {
552 TAILQ_REMOVE(&src->callbacks, cb, next);
558 /* all callbacks for that source are removed. */
559 if (TAILQ_EMPTY(&src->callbacks)) {
560 TAILQ_REMOVE(&intr_sources, src, next);
565 rte_spinlock_unlock(&intr_lock);
567 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
568 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
576 rte_intr_enable(struct rte_intr_handle *intr_handle)
578 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
581 switch (intr_handle->type){
582 /* write to the uio fd to enable the interrupt */
583 case RTE_INTR_HANDLE_UIO:
584 if (uio_intr_enable(intr_handle))
587 case RTE_INTR_HANDLE_UIO_INTX:
588 if (uio_intx_intr_enable(intr_handle))
591 /* not used at this moment */
592 case RTE_INTR_HANDLE_ALARM:
595 case RTE_INTR_HANDLE_VFIO_MSIX:
596 if (vfio_enable_msix(intr_handle))
599 case RTE_INTR_HANDLE_VFIO_MSI:
600 if (vfio_enable_msi(intr_handle))
603 case RTE_INTR_HANDLE_VFIO_LEGACY:
604 if (vfio_enable_intx(intr_handle))
608 /* unknown handle type */
611 "Unknown handle type of fd %d\n",
620 rte_intr_disable(struct rte_intr_handle *intr_handle)
622 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
625 switch (intr_handle->type){
626 /* write to the uio fd to disable the interrupt */
627 case RTE_INTR_HANDLE_UIO:
628 if (uio_intr_disable(intr_handle))
631 case RTE_INTR_HANDLE_UIO_INTX:
632 if (uio_intx_intr_disable(intr_handle))
635 /* not used at this moment */
636 case RTE_INTR_HANDLE_ALARM:
639 case RTE_INTR_HANDLE_VFIO_MSIX:
640 if (vfio_disable_msix(intr_handle))
643 case RTE_INTR_HANDLE_VFIO_MSI:
644 if (vfio_disable_msi(intr_handle))
647 case RTE_INTR_HANDLE_VFIO_LEGACY:
648 if (vfio_disable_intx(intr_handle))
652 /* unknown handle type */
655 "Unknown handle type of fd %d\n",
664 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
667 struct rte_intr_source *src;
668 struct rte_intr_callback *cb;
669 union rte_intr_read_buffer buf;
670 struct rte_intr_callback active_cb;
672 for (n = 0; n < nfds; n++) {
675 * if the pipe fd is ready to read, return out to
676 * rebuild the wait list.
678 if (events[n].data.fd == intr_pipe.readfd){
679 int r = read(intr_pipe.readfd, buf.charbuf,
680 sizeof(buf.charbuf));
684 rte_spinlock_lock(&intr_lock);
685 TAILQ_FOREACH(src, &intr_sources, next)
686 if (src->intr_handle.fd ==
690 rte_spinlock_unlock(&intr_lock);
694 /* mark this interrupt source as active and release the lock. */
696 rte_spinlock_unlock(&intr_lock);
698 /* set the length to be read dor different handle type */
699 switch (src->intr_handle.type) {
700 case RTE_INTR_HANDLE_UIO:
701 bytes_read = sizeof(buf.uio_intr_count);
703 case RTE_INTR_HANDLE_ALARM:
704 bytes_read = sizeof(buf.timerfd_num);
707 case RTE_INTR_HANDLE_VFIO_MSIX:
708 case RTE_INTR_HANDLE_VFIO_MSI:
709 case RTE_INTR_HANDLE_VFIO_LEGACY:
710 bytes_read = sizeof(buf.vfio_intr_count);
719 * read out to clear the ready-to-be-read flag
722 bytes_read = read(events[n].data.fd, &buf, bytes_read);
725 RTE_LOG(ERR, EAL, "Error reading from file "
726 "descriptor %d: %s\n", events[n].data.fd,
728 else if (bytes_read == 0)
729 RTE_LOG(ERR, EAL, "Read nothing from file "
730 "descriptor %d\n", events[n].data.fd);
732 /* grab a lock, again to call callbacks and update status. */
733 rte_spinlock_lock(&intr_lock);
735 if (bytes_read > 0) {
737 /* Finally, call all callbacks. */
738 TAILQ_FOREACH(cb, &src->callbacks, next) {
740 /* make a copy and unlock. */
742 rte_spinlock_unlock(&intr_lock);
744 /* call the actual callback */
745 active_cb.cb_fn(&src->intr_handle,
748 /*get the lock back. */
749 rte_spinlock_lock(&intr_lock);
753 /* we done with that interrupt source, release it. */
755 rte_spinlock_unlock(&intr_lock);
762 * It handles all the interrupts.
765 * epoll file descriptor.
767 * The number of file descriptors added in epoll.
773 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
775 struct epoll_event events[totalfds];
779 nfds = epoll_wait(pfd, events, totalfds,
780 EAL_INTR_EPOLL_WAIT_FOREVER);
781 /* epoll_wait fail */
786 "epoll_wait returns with fail\n");
789 /* epoll_wait timeout, will never happens here */
792 /* epoll_wait has at least one fd ready to read */
793 if (eal_intr_process_interrupts(events, nfds) < 0)
799 * It builds/rebuilds up the epoll file descriptor with all the
800 * file descriptors being waited on. Then handles the interrupts.
808 static __attribute__((noreturn)) void *
809 eal_intr_thread_main(__rte_unused void *arg)
811 struct epoll_event ev;
813 /* host thread, never break out */
815 /* build up the epoll fd with all descriptors we are to
816 * wait on then pass it to the handle_interrupts function
818 static struct epoll_event pipe_event = {
819 .events = EPOLLIN | EPOLLPRI,
821 struct rte_intr_source *src;
824 /* create epoll fd */
825 int pfd = epoll_create(1);
827 rte_panic("Cannot create epoll instance\n");
829 pipe_event.data.fd = intr_pipe.readfd;
831 * add pipe fd into wait list, this pipe is used to
832 * rebuild the wait list.
834 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
836 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
837 intr_pipe.readfd, strerror(errno));
841 rte_spinlock_lock(&intr_lock);
843 TAILQ_FOREACH(src, &intr_sources, next) {
844 if (src->callbacks.tqh_first == NULL)
845 continue; /* skip those with no callbacks */
846 ev.events = EPOLLIN | EPOLLPRI;
847 ev.data.fd = src->intr_handle.fd;
850 * add all the uio device file descriptor
853 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
854 src->intr_handle.fd, &ev) < 0){
855 rte_panic("Error adding fd %d epoll_ctl, %s\n",
856 src->intr_handle.fd, strerror(errno));
861 rte_spinlock_unlock(&intr_lock);
862 /* serve the interrupt */
863 eal_intr_handle_interrupts(pfd, numfds);
866 * when we return, we need to rebuild the
867 * list of fds to monitor.
874 rte_eal_intr_init(void)
878 /* init the global interrupt source head */
879 TAILQ_INIT(&intr_sources);
882 * create a pipe which will be waited by epoll and notified to
883 * rebuild the wait list of epoll.
885 if (pipe(intr_pipe.pipefd) < 0)
888 /* create the host thread to wait/handle the interrupt */
889 ret = pthread_create(&intr_thread, NULL,
890 eal_intr_thread_main, NULL);
893 "Failed to create thread for interrupt handling\n");