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_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_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 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
413 rte_intr_callback_fn cb, void *cb_arg)
415 int ret, wake_thread;
416 struct rte_intr_source *src;
417 struct rte_intr_callback *callback;
421 /* first do parameter checking */
422 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
424 "Registering with invalid input parameter\n");
428 /* allocate a new interrupt callback entity */
429 callback = rte_zmalloc("interrupt callback list",
430 sizeof(*callback), 0);
431 if (callback == NULL) {
432 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
435 callback->cb_fn = cb;
436 callback->cb_arg = cb_arg;
438 rte_spinlock_lock(&intr_lock);
440 /* check if there is at least one callback registered for the fd */
441 TAILQ_FOREACH(src, &intr_sources, next) {
442 if (src->intr_handle.fd == intr_handle->fd) {
443 /* we had no interrupts for this */
444 if TAILQ_EMPTY(&src->callbacks)
447 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
453 /* no existing callbacks for this - add new source */
455 if ((src = rte_zmalloc("interrupt source list",
456 sizeof(*src), 0)) == NULL) {
457 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
461 src->intr_handle = *intr_handle;
462 TAILQ_INIT(&src->callbacks);
463 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
464 TAILQ_INSERT_TAIL(&intr_sources, src, next);
470 rte_spinlock_unlock(&intr_lock);
473 * check if need to notify the pipe fd waited by epoll_wait to
474 * rebuild the wait list.
477 if (write(intr_pipe.writefd, "1", 1) < 0)
484 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
485 rte_intr_callback_fn cb_fn, void *cb_arg)
488 struct rte_intr_source *src;
489 struct rte_intr_callback *cb, *next;
491 /* do parameter checking first */
492 if (intr_handle == NULL || intr_handle->fd < 0) {
494 "Unregistering with invalid input parameter\n");
498 rte_spinlock_lock(&intr_lock);
500 /* check if the insterrupt source for the fd is existent */
501 TAILQ_FOREACH(src, &intr_sources, next)
502 if (src->intr_handle.fd == intr_handle->fd)
505 /* No interrupt source registered for the fd */
509 /* interrupt source has some active callbacks right now. */
510 } else if (src->active != 0) {
517 /*walk through the callbacks and remove all that match. */
518 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
520 next = TAILQ_NEXT(cb, next);
522 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
523 cb->cb_arg == cb_arg)) {
524 TAILQ_REMOVE(&src->callbacks, cb, next);
530 /* all callbacks for that source are removed. */
531 if (TAILQ_EMPTY(&src->callbacks)) {
532 TAILQ_REMOVE(&intr_sources, src, next);
537 rte_spinlock_unlock(&intr_lock);
539 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
540 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
548 rte_intr_enable(struct rte_intr_handle *intr_handle)
550 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
553 switch (intr_handle->type){
554 /* write to the uio fd to enable the interrupt */
555 case RTE_INTR_HANDLE_UIO:
556 if (uio_intr_enable(intr_handle))
559 /* not used at this moment */
560 case RTE_INTR_HANDLE_ALARM:
563 case RTE_INTR_HANDLE_VFIO_MSIX:
564 if (vfio_enable_msix(intr_handle))
567 case RTE_INTR_HANDLE_VFIO_MSI:
568 if (vfio_enable_msi(intr_handle))
571 case RTE_INTR_HANDLE_VFIO_LEGACY:
572 if (vfio_enable_intx(intr_handle))
576 /* unknown handle type */
579 "Unknown handle type of fd %d\n",
588 rte_intr_disable(struct rte_intr_handle *intr_handle)
590 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
593 switch (intr_handle->type){
594 /* write to the uio fd to disable the interrupt */
595 case RTE_INTR_HANDLE_UIO:
596 if (uio_intr_disable(intr_handle))
599 /* not used at this moment */
600 case RTE_INTR_HANDLE_ALARM:
603 case RTE_INTR_HANDLE_VFIO_MSIX:
604 if (vfio_disable_msix(intr_handle))
607 case RTE_INTR_HANDLE_VFIO_MSI:
608 if (vfio_disable_msi(intr_handle))
611 case RTE_INTR_HANDLE_VFIO_LEGACY:
612 if (vfio_disable_intx(intr_handle))
616 /* unknown handle type */
619 "Unknown handle type of fd %d\n",
628 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
631 struct rte_intr_source *src;
632 struct rte_intr_callback *cb;
633 union rte_intr_read_buffer buf;
634 struct rte_intr_callback active_cb;
636 for (n = 0; n < nfds; n++) {
639 * if the pipe fd is ready to read, return out to
640 * rebuild the wait list.
642 if (events[n].data.fd == intr_pipe.readfd){
643 int r = read(intr_pipe.readfd, buf.charbuf,
644 sizeof(buf.charbuf));
648 rte_spinlock_lock(&intr_lock);
649 TAILQ_FOREACH(src, &intr_sources, next)
650 if (src->intr_handle.fd ==
654 rte_spinlock_unlock(&intr_lock);
658 /* mark this interrupt source as active and release the lock. */
660 rte_spinlock_unlock(&intr_lock);
662 /* set the length to be read dor different handle type */
663 switch (src->intr_handle.type) {
664 case RTE_INTR_HANDLE_UIO:
665 bytes_read = sizeof(buf.uio_intr_count);
667 case RTE_INTR_HANDLE_ALARM:
668 bytes_read = sizeof(buf.timerfd_num);
671 case RTE_INTR_HANDLE_VFIO_MSIX:
672 case RTE_INTR_HANDLE_VFIO_MSI:
673 case RTE_INTR_HANDLE_VFIO_LEGACY:
674 bytes_read = sizeof(buf.vfio_intr_count);
683 * read out to clear the ready-to-be-read flag
686 bytes_read = read(events[n].data.fd, &buf, bytes_read);
689 RTE_LOG(ERR, EAL, "Error reading from file "
690 "descriptor %d: %s\n", events[n].data.fd,
692 else if (bytes_read == 0)
693 RTE_LOG(ERR, EAL, "Read nothing from file "
694 "descriptor %d\n", events[n].data.fd);
696 /* grab a lock, again to call callbacks and update status. */
697 rte_spinlock_lock(&intr_lock);
699 if (bytes_read > 0) {
701 /* Finally, call all callbacks. */
702 TAILQ_FOREACH(cb, &src->callbacks, next) {
704 /* make a copy and unlock. */
706 rte_spinlock_unlock(&intr_lock);
708 /* call the actual callback */
709 active_cb.cb_fn(&src->intr_handle,
712 /*get the lock back. */
713 rte_spinlock_lock(&intr_lock);
717 /* we done with that interrupt source, release it. */
719 rte_spinlock_unlock(&intr_lock);
726 * It handles all the interrupts.
729 * epoll file descriptor.
731 * The number of file descriptors added in epoll.
737 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
739 struct epoll_event events[totalfds];
743 nfds = epoll_wait(pfd, events, totalfds,
744 EAL_INTR_EPOLL_WAIT_FOREVER);
745 /* epoll_wait fail */
750 "epoll_wait returns with fail\n");
753 /* epoll_wait timeout, will never happens here */
756 /* epoll_wait has at least one fd ready to read */
757 if (eal_intr_process_interrupts(events, nfds) < 0)
763 * It builds/rebuilds up the epoll file descriptor with all the
764 * file descriptors being waited on. Then handles the interrupts.
772 static __attribute__((noreturn)) void *
773 eal_intr_thread_main(__rte_unused void *arg)
775 struct epoll_event ev;
777 /* host thread, never break out */
779 /* build up the epoll fd with all descriptors we are to
780 * wait on then pass it to the handle_interrupts function
782 static struct epoll_event pipe_event = {
783 .events = EPOLLIN | EPOLLPRI,
785 struct rte_intr_source *src;
788 /* create epoll fd */
789 int pfd = epoll_create(1);
791 rte_panic("Cannot create epoll instance\n");
793 pipe_event.data.fd = intr_pipe.readfd;
795 * add pipe fd into wait list, this pipe is used to
796 * rebuild the wait list.
798 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
800 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
801 intr_pipe.readfd, strerror(errno));
805 rte_spinlock_lock(&intr_lock);
807 TAILQ_FOREACH(src, &intr_sources, next) {
808 if (src->callbacks.tqh_first == NULL)
809 continue; /* skip those with no callbacks */
810 ev.events = EPOLLIN | EPOLLPRI;
811 ev.data.fd = src->intr_handle.fd;
814 * add all the uio device file descriptor
817 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
818 src->intr_handle.fd, &ev) < 0){
819 rte_panic("Error adding fd %d epoll_ctl, %s\n",
820 src->intr_handle.fd, strerror(errno));
825 rte_spinlock_unlock(&intr_lock);
826 /* serve the interrupt */
827 eal_intr_handle_interrupts(pfd, numfds);
830 * when we return, we need to rebuild the
831 * list of fds to monitor.
838 rte_eal_intr_init(void)
842 /* init the global interrupt source head */
843 TAILQ_INIT(&intr_sources);
846 * create a pipe which will be waited by epoll and notified to
847 * rebuild the wait list of epoll.
849 if (pipe(intr_pipe.pipefd) < 0)
852 /* create the host thread to wait/handle the interrupt */
853 ret = pthread_create(&intr_thread, NULL,
854 eal_intr_thread_main, NULL);
857 "Failed to create thread for interrupt handling\n");