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 uio_intr_disable(struct rte_intr_handle *intr_handle)
367 unsigned char command_high;
369 /* use UIO config file descriptor for uio_pci_generic */
370 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
372 "Error reading interrupts status for fd %d\n",
373 intr_handle->uio_cfg_fd);
376 /* disable interrupts */
378 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
380 "Error disabling interrupts for fd %d\n",
381 intr_handle->uio_cfg_fd);
389 uio_intr_enable(struct rte_intr_handle *intr_handle)
391 unsigned char command_high;
393 /* use UIO config file descriptor for uio_pci_generic */
394 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
396 "Error reading interrupts status for fd %d\n",
397 intr_handle->uio_cfg_fd);
400 /* enable interrupts */
401 command_high &= ~0x4;
402 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
404 "Error enabling interrupts for fd %d\n",
405 intr_handle->uio_cfg_fd);
413 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
414 rte_intr_callback_fn cb, void *cb_arg)
416 int ret, wake_thread;
417 struct rte_intr_source *src;
418 struct rte_intr_callback *callback;
422 /* first do parameter checking */
423 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
425 "Registering with invalid input parameter\n");
429 /* allocate a new interrupt callback entity */
430 callback = rte_zmalloc("interrupt callback list",
431 sizeof(*callback), 0);
432 if (callback == NULL) {
433 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
436 callback->cb_fn = cb;
437 callback->cb_arg = cb_arg;
439 rte_spinlock_lock(&intr_lock);
441 /* check if there is at least one callback registered for the fd */
442 TAILQ_FOREACH(src, &intr_sources, next) {
443 if (src->intr_handle.fd == intr_handle->fd) {
444 /* we had no interrupts for this */
445 if TAILQ_EMPTY(&src->callbacks)
448 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
454 /* no existing callbacks for this - add new source */
456 if ((src = rte_zmalloc("interrupt source list",
457 sizeof(*src), 0)) == NULL) {
458 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
462 src->intr_handle = *intr_handle;
463 TAILQ_INIT(&src->callbacks);
464 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
465 TAILQ_INSERT_TAIL(&intr_sources, src, next);
471 rte_spinlock_unlock(&intr_lock);
474 * check if need to notify the pipe fd waited by epoll_wait to
475 * rebuild the wait list.
478 if (write(intr_pipe.writefd, "1", 1) < 0)
485 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
486 rte_intr_callback_fn cb_fn, void *cb_arg)
489 struct rte_intr_source *src;
490 struct rte_intr_callback *cb, *next;
492 /* do parameter checking first */
493 if (intr_handle == NULL || intr_handle->fd < 0) {
495 "Unregistering with invalid input parameter\n");
499 rte_spinlock_lock(&intr_lock);
501 /* check if the insterrupt source for the fd is existent */
502 TAILQ_FOREACH(src, &intr_sources, next)
503 if (src->intr_handle.fd == intr_handle->fd)
506 /* No interrupt source registered for the fd */
510 /* interrupt source has some active callbacks right now. */
511 } else if (src->active != 0) {
518 /*walk through the callbacks and remove all that match. */
519 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
521 next = TAILQ_NEXT(cb, next);
523 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
524 cb->cb_arg == cb_arg)) {
525 TAILQ_REMOVE(&src->callbacks, cb, next);
531 /* all callbacks for that source are removed. */
532 if (TAILQ_EMPTY(&src->callbacks)) {
533 TAILQ_REMOVE(&intr_sources, src, next);
538 rte_spinlock_unlock(&intr_lock);
540 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
541 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
549 rte_intr_enable(struct rte_intr_handle *intr_handle)
551 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
554 switch (intr_handle->type){
555 /* write to the uio fd to enable the interrupt */
556 case RTE_INTR_HANDLE_UIO:
557 if (uio_intr_enable(intr_handle))
560 /* not used at this moment */
561 case RTE_INTR_HANDLE_ALARM:
564 case RTE_INTR_HANDLE_VFIO_MSIX:
565 if (vfio_enable_msix(intr_handle))
568 case RTE_INTR_HANDLE_VFIO_MSI:
569 if (vfio_enable_msi(intr_handle))
572 case RTE_INTR_HANDLE_VFIO_LEGACY:
573 if (vfio_enable_intx(intr_handle))
577 /* unknown handle type */
580 "Unknown handle type of fd %d\n",
589 rte_intr_disable(struct rte_intr_handle *intr_handle)
591 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
594 switch (intr_handle->type){
595 /* write to the uio fd to disable the interrupt */
596 case RTE_INTR_HANDLE_UIO:
597 if (uio_intr_disable(intr_handle))
600 /* not used at this moment */
601 case RTE_INTR_HANDLE_ALARM:
604 case RTE_INTR_HANDLE_VFIO_MSIX:
605 if (vfio_disable_msix(intr_handle))
608 case RTE_INTR_HANDLE_VFIO_MSI:
609 if (vfio_disable_msi(intr_handle))
612 case RTE_INTR_HANDLE_VFIO_LEGACY:
613 if (vfio_disable_intx(intr_handle))
617 /* unknown handle type */
620 "Unknown handle type of fd %d\n",
629 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
632 struct rte_intr_source *src;
633 struct rte_intr_callback *cb;
634 union rte_intr_read_buffer buf;
635 struct rte_intr_callback active_cb;
637 for (n = 0; n < nfds; n++) {
640 * if the pipe fd is ready to read, return out to
641 * rebuild the wait list.
643 if (events[n].data.fd == intr_pipe.readfd){
644 int r = read(intr_pipe.readfd, buf.charbuf,
645 sizeof(buf.charbuf));
649 rte_spinlock_lock(&intr_lock);
650 TAILQ_FOREACH(src, &intr_sources, next)
651 if (src->intr_handle.fd ==
655 rte_spinlock_unlock(&intr_lock);
659 /* mark this interrupt source as active and release the lock. */
661 rte_spinlock_unlock(&intr_lock);
663 /* set the length to be read dor different handle type */
664 switch (src->intr_handle.type) {
665 case RTE_INTR_HANDLE_UIO:
666 bytes_read = sizeof(buf.uio_intr_count);
668 case RTE_INTR_HANDLE_ALARM:
669 bytes_read = sizeof(buf.timerfd_num);
672 case RTE_INTR_HANDLE_VFIO_MSIX:
673 case RTE_INTR_HANDLE_VFIO_MSI:
674 case RTE_INTR_HANDLE_VFIO_LEGACY:
675 bytes_read = sizeof(buf.vfio_intr_count);
684 * read out to clear the ready-to-be-read flag
687 bytes_read = read(events[n].data.fd, &buf, bytes_read);
690 RTE_LOG(ERR, EAL, "Error reading from file "
691 "descriptor %d: %s\n", events[n].data.fd,
693 else if (bytes_read == 0)
694 RTE_LOG(ERR, EAL, "Read nothing from file "
695 "descriptor %d\n", events[n].data.fd);
697 /* grab a lock, again to call callbacks and update status. */
698 rte_spinlock_lock(&intr_lock);
700 if (bytes_read > 0) {
702 /* Finally, call all callbacks. */
703 TAILQ_FOREACH(cb, &src->callbacks, next) {
705 /* make a copy and unlock. */
707 rte_spinlock_unlock(&intr_lock);
709 /* call the actual callback */
710 active_cb.cb_fn(&src->intr_handle,
713 /*get the lock back. */
714 rte_spinlock_lock(&intr_lock);
718 /* we done with that interrupt source, release it. */
720 rte_spinlock_unlock(&intr_lock);
727 * It handles all the interrupts.
730 * epoll file descriptor.
732 * The number of file descriptors added in epoll.
738 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
740 struct epoll_event events[totalfds];
744 nfds = epoll_wait(pfd, events, totalfds,
745 EAL_INTR_EPOLL_WAIT_FOREVER);
746 /* epoll_wait fail */
751 "epoll_wait returns with fail\n");
754 /* epoll_wait timeout, will never happens here */
757 /* epoll_wait has at least one fd ready to read */
758 if (eal_intr_process_interrupts(events, nfds) < 0)
764 * It builds/rebuilds up the epoll file descriptor with all the
765 * file descriptors being waited on. Then handles the interrupts.
773 static __attribute__((noreturn)) void *
774 eal_intr_thread_main(__rte_unused void *arg)
776 struct epoll_event ev;
778 /* host thread, never break out */
780 /* build up the epoll fd with all descriptors we are to
781 * wait on then pass it to the handle_interrupts function
783 static struct epoll_event pipe_event = {
784 .events = EPOLLIN | EPOLLPRI,
786 struct rte_intr_source *src;
789 /* create epoll fd */
790 int pfd = epoll_create(1);
792 rte_panic("Cannot create epoll instance\n");
794 pipe_event.data.fd = intr_pipe.readfd;
796 * add pipe fd into wait list, this pipe is used to
797 * rebuild the wait list.
799 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
801 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
802 intr_pipe.readfd, strerror(errno));
806 rte_spinlock_lock(&intr_lock);
808 TAILQ_FOREACH(src, &intr_sources, next) {
809 if (src->callbacks.tqh_first == NULL)
810 continue; /* skip those with no callbacks */
811 ev.events = EPOLLIN | EPOLLPRI;
812 ev.data.fd = src->intr_handle.fd;
815 * add all the uio device file descriptor
818 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
819 src->intr_handle.fd, &ev) < 0){
820 rte_panic("Error adding fd %d epoll_ctl, %s\n",
821 src->intr_handle.fd, strerror(errno));
826 rte_spinlock_unlock(&intr_lock);
827 /* serve the interrupt */
828 eal_intr_handle_interrupts(pfd, numfds);
831 * when we return, we need to rebuild the
832 * list of fds to monitor.
839 rte_eal_intr_init(void)
843 /* init the global interrupt source head */
844 TAILQ_INIT(&intr_sources);
847 * create a pipe which will be waited by epoll and notified to
848 * rebuild the wait list of epoll.
850 if (pipe(intr_pipe.pipefd) < 0)
853 /* create the host thread to wait/handle the interrupt */
854 ret = pthread_create(&intr_thread, NULL,
855 eal_intr_thread_main, NULL);
858 "Failed to create thread for interrupt handling\n");