55be263332fe892a3437f8585516692391a2308a
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_interrupts.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <pthread.h>
38 #include <sys/queue.h>
39 #include <stdarg.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <inttypes.h>
44 #include <sys/epoll.h>
45 #include <sys/signalfd.h>
46 #include <sys/ioctl.h>
47
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_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_ring.h>
59 #include <rte_debug.h>
60 #include <rte_log.h>
61 #include <rte_mempool.h>
62 #include <rte_pci.h>
63 #include <rte_malloc.h>
64 #include <rte_errno.h>
65 #include <rte_spinlock.h>
66
67 #include "eal_private.h"
68 #include "eal_vfio.h"
69
70 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
71
72 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
73
74 /**
75  * union for pipe fds.
76  */
77 union intr_pipefds{
78         struct {
79                 int pipefd[2];
80         };
81         struct {
82                 int readfd;
83                 int writefd;
84         };
85 };
86
87 /**
88  * union buffer for reading on different devices
89  */
90 union rte_intr_read_buffer {
91         int uio_intr_count;              /* for uio device */
92 #ifdef VFIO_PRESENT
93         uint64_t vfio_intr_count;        /* for vfio device */
94 #endif
95         uint64_t timerfd_num;            /* for timerfd */
96         char charbuf[16];                /* for others */
97 };
98
99 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
100 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
101
102 struct rte_intr_callback {
103         TAILQ_ENTRY(rte_intr_callback) next;
104         rte_intr_callback_fn cb_fn;  /**< callback address */
105         void *cb_arg;                /**< parameter for callback */
106 };
107
108 struct rte_intr_source {
109         TAILQ_ENTRY(rte_intr_source) next;
110         struct rte_intr_handle intr_handle; /**< interrupt handle */
111         struct rte_intr_cb_list callbacks;  /**< user callbacks */
112         uint32_t active;
113 };
114
115 /* global spinlock for interrupt data operation */
116 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
117
118 /* union buffer for pipe read/write */
119 static union intr_pipefds intr_pipe;
120
121 /* interrupt sources list */
122 static struct rte_intr_source_list intr_sources;
123
124 /* interrupt handling thread */
125 static pthread_t intr_thread;
126
127 /* VFIO interrupts */
128 #ifdef VFIO_PRESENT
129
130 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
131
132 /* enable legacy (INTx) interrupts */
133 static int
134 vfio_enable_intx(struct rte_intr_handle *intr_handle) {
135         struct vfio_irq_set *irq_set;
136         char irq_set_buf[IRQ_SET_BUF_LEN];
137         int len, ret;
138         int *fd_ptr;
139
140         len = sizeof(irq_set_buf);
141
142         /* enable INTx */
143         irq_set = (struct vfio_irq_set *) irq_set_buf;
144         irq_set->argsz = len;
145         irq_set->count = 1;
146         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
147         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
148         irq_set->start = 0;
149         fd_ptr = (int *) &irq_set->data;
150         *fd_ptr = intr_handle->fd;
151
152         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
153
154         if (ret) {
155                 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
156                                                 intr_handle->fd);
157                 return -1;
158         }
159
160         /* unmask INTx after enabling */
161         memset(irq_set, 0, len);
162         len = sizeof(struct vfio_irq_set);
163         irq_set->argsz = len;
164         irq_set->count = 1;
165         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
166         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
167         irq_set->start = 0;
168
169         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
170
171         if (ret) {
172                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
173                                                 intr_handle->fd);
174                 return -1;
175         }
176         return 0;
177 }
178
179 /* disable legacy (INTx) interrupts */
180 static int
181 vfio_disable_intx(struct rte_intr_handle *intr_handle) {
182         struct vfio_irq_set *irq_set;
183         char irq_set_buf[IRQ_SET_BUF_LEN];
184         int len, ret;
185
186         len = sizeof(struct vfio_irq_set);
187
188         /* mask interrupts before disabling */
189         irq_set = (struct vfio_irq_set *) irq_set_buf;
190         irq_set->argsz = len;
191         irq_set->count = 1;
192         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
193         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
194         irq_set->start = 0;
195
196         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
197
198         if (ret) {
199                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
200                                                 intr_handle->fd);
201                 return -1;
202         }
203
204         /* disable INTx*/
205         memset(irq_set, 0, len);
206         irq_set->argsz = len;
207         irq_set->count = 0;
208         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
209         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
210         irq_set->start = 0;
211
212         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
213
214         if (ret) {
215                 RTE_LOG(ERR, EAL,
216                         "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
217                 return -1;
218         }
219         return 0;
220 }
221
222 /* enable MSI-X interrupts */
223 static int
224 vfio_enable_msi(struct rte_intr_handle *intr_handle) {
225         int len, ret;
226         char irq_set_buf[IRQ_SET_BUF_LEN];
227         struct vfio_irq_set *irq_set;
228         int *fd_ptr;
229
230         len = sizeof(irq_set_buf);
231
232         irq_set = (struct vfio_irq_set *) irq_set_buf;
233         irq_set->argsz = len;
234         irq_set->count = 1;
235         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
236         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
237         irq_set->start = 0;
238         fd_ptr = (int *) &irq_set->data;
239         *fd_ptr = intr_handle->fd;
240
241         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
242
243         if (ret) {
244                 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
245                                                 intr_handle->fd);
246                 return -1;
247         }
248
249         /* manually trigger interrupt to enable it */
250         memset(irq_set, 0, len);
251         len = sizeof(struct vfio_irq_set);
252         irq_set->argsz = len;
253         irq_set->count = 1;
254         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
255         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
256         irq_set->start = 0;
257
258         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
259
260         if (ret) {
261                 RTE_LOG(ERR, EAL, "Error triggering MSI interrupts for fd %d\n",
262                                                 intr_handle->fd);
263                 return -1;
264         }
265         return 0;
266 }
267
268 /* disable MSI-X interrupts */
269 static int
270 vfio_disable_msi(struct rte_intr_handle *intr_handle) {
271         struct vfio_irq_set *irq_set;
272         char irq_set_buf[IRQ_SET_BUF_LEN];
273         int len, ret;
274
275         len = sizeof(struct vfio_irq_set);
276
277         irq_set = (struct vfio_irq_set *) irq_set_buf;
278         irq_set->argsz = len;
279         irq_set->count = 0;
280         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
281         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
282         irq_set->start = 0;
283
284         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
285
286         if (ret)
287                 RTE_LOG(ERR, EAL,
288                         "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
289
290         return ret;
291 }
292
293 /* enable MSI-X interrupts */
294 static int
295 vfio_enable_msix(struct rte_intr_handle *intr_handle) {
296         int len, ret;
297         char irq_set_buf[IRQ_SET_BUF_LEN];
298         struct vfio_irq_set *irq_set;
299         int *fd_ptr;
300
301         len = sizeof(irq_set_buf);
302
303         irq_set = (struct vfio_irq_set *) irq_set_buf;
304         irq_set->argsz = len;
305         irq_set->count = 1;
306         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
307         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
308         irq_set->start = 0;
309         fd_ptr = (int *) &irq_set->data;
310         *fd_ptr = intr_handle->fd;
311
312         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
313
314         if (ret) {
315                 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
316                                                 intr_handle->fd);
317                 return -1;
318         }
319
320         /* manually trigger interrupt to enable it */
321         memset(irq_set, 0, len);
322         len = sizeof(struct vfio_irq_set);
323         irq_set->argsz = len;
324         irq_set->count = 1;
325         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
326         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
327         irq_set->start = 0;
328
329         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
330
331         if (ret) {
332                 RTE_LOG(ERR, EAL, "Error triggering MSI-X interrupts for fd %d\n",
333                                                 intr_handle->fd);
334                 return -1;
335         }
336         return 0;
337 }
338
339 /* disable MSI-X interrupts */
340 static int
341 vfio_disable_msix(struct rte_intr_handle *intr_handle) {
342         struct vfio_irq_set *irq_set;
343         char irq_set_buf[IRQ_SET_BUF_LEN];
344         int len, ret;
345
346         len = sizeof(struct vfio_irq_set);
347
348         irq_set = (struct vfio_irq_set *) irq_set_buf;
349         irq_set->argsz = len;
350         irq_set->count = 0;
351         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
352         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
353         irq_set->start = 0;
354
355         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
356
357         if (ret)
358                 RTE_LOG(ERR, EAL,
359                         "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
360
361         return ret;
362 }
363 #endif
364
365 static int
366 uio_intx_intr_disable(struct rte_intr_handle *intr_handle)
367 {
368         unsigned char command_high;
369
370         /* use UIO config file descriptor for uio_pci_generic */
371         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
372                 RTE_LOG(ERR, EAL,
373                         "Error reading interrupts status for fd %d\n",
374                         intr_handle->uio_cfg_fd);
375                 return -1;
376         }
377         /* disable interrupts */
378         command_high |= 0x4;
379         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
380                 RTE_LOG(ERR, EAL,
381                         "Error disabling interrupts for fd %d\n",
382                         intr_handle->uio_cfg_fd);
383                 return -1;
384         }
385
386         return 0;
387 }
388
389 static int
390 uio_intx_intr_enable(struct rte_intr_handle *intr_handle)
391 {
392         unsigned char command_high;
393
394         /* use UIO config file descriptor for uio_pci_generic */
395         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
396                 RTE_LOG(ERR, EAL,
397                         "Error reading interrupts status for fd %d\n",
398                         intr_handle->uio_cfg_fd);
399                 return -1;
400         }
401         /* enable interrupts */
402         command_high &= ~0x4;
403         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
404                 RTE_LOG(ERR, EAL,
405                         "Error enabling interrupts for fd %d\n",
406                         intr_handle->uio_cfg_fd);
407                 return -1;
408         }
409
410         return 0;
411 }
412
413 static int
414 uio_intr_disable(struct rte_intr_handle *intr_handle)
415 {
416         const int value = 0;
417
418         if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
419                 RTE_LOG(ERR, EAL,
420                         "Error disabling interrupts for fd %d (%s)\n",
421                         intr_handle->fd, strerror(errno));
422                 return -1;
423         }
424         return 0;
425 }
426
427 static int
428 uio_intr_enable(struct rte_intr_handle *intr_handle)
429 {
430         const int value = 1;
431
432         if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
433                 RTE_LOG(ERR, EAL,
434                         "Error enabling interrupts for fd %d (%s)\n",
435                         intr_handle->fd, strerror(errno));
436                 return -1;
437         }
438         return 0;
439 }
440
441 int
442 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
443                         rte_intr_callback_fn cb, void *cb_arg)
444 {
445         int ret, wake_thread;
446         struct rte_intr_source *src;
447         struct rte_intr_callback *callback;
448
449         wake_thread = 0;
450
451         /* first do parameter checking */
452         if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
453                 RTE_LOG(ERR, EAL,
454                         "Registering with invalid input parameter\n");
455                 return -EINVAL;
456         }
457
458         /* allocate a new interrupt callback entity */
459         callback = rte_zmalloc("interrupt callback list",
460                                 sizeof(*callback), 0);
461         if (callback == NULL) {
462                 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
463                 return -ENOMEM;
464         }
465         callback->cb_fn = cb;
466         callback->cb_arg = cb_arg;
467
468         rte_spinlock_lock(&intr_lock);
469
470         /* check if there is at least one callback registered for the fd */
471         TAILQ_FOREACH(src, &intr_sources, next) {
472                 if (src->intr_handle.fd == intr_handle->fd) {
473                         /* we had no interrupts for this */
474                         if TAILQ_EMPTY(&src->callbacks)
475                                 wake_thread = 1;
476
477                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
478                         ret = 0;
479                         break;
480                 }
481         }
482
483         /* no existing callbacks for this - add new source */
484         if (src == NULL) {
485                 if ((src = rte_zmalloc("interrupt source list",
486                                 sizeof(*src), 0)) == NULL) {
487                         RTE_LOG(ERR, EAL, "Can not allocate memory\n");
488                         rte_free(callback);
489                         ret = -ENOMEM;
490                 } else {
491                         src->intr_handle = *intr_handle;
492                         TAILQ_INIT(&src->callbacks);
493                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
494                         TAILQ_INSERT_TAIL(&intr_sources, src, next);
495                         wake_thread = 1;
496                         ret = 0;
497                 }
498         }
499
500         rte_spinlock_unlock(&intr_lock);
501
502         /**
503          * check if need to notify the pipe fd waited by epoll_wait to
504          * rebuild the wait list.
505          */
506         if (wake_thread)
507                 if (write(intr_pipe.writefd, "1", 1) < 0)
508                         return -EPIPE;
509
510         return ret;
511 }
512
513 int
514 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
515                         rte_intr_callback_fn cb_fn, void *cb_arg)
516 {
517         int ret;
518         struct rte_intr_source *src;
519         struct rte_intr_callback *cb, *next;
520
521         /* do parameter checking first */
522         if (intr_handle == NULL || intr_handle->fd < 0) {
523                 RTE_LOG(ERR, EAL,
524                 "Unregistering with invalid input parameter\n");
525                 return -EINVAL;
526         }
527
528         rte_spinlock_lock(&intr_lock);
529
530         /* check if the insterrupt source for the fd is existent */
531         TAILQ_FOREACH(src, &intr_sources, next)
532                 if (src->intr_handle.fd == intr_handle->fd)
533                         break;
534
535         /* No interrupt source registered for the fd */
536         if (src == NULL) {
537                 ret = -ENOENT;
538
539         /* interrupt source has some active callbacks right now. */
540         } else if (src->active != 0) {
541                 ret = -EAGAIN;
542
543         /* ok to remove. */
544         } else {
545                 ret = 0;
546
547                 /*walk through the callbacks and remove all that match. */
548                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
549
550                         next = TAILQ_NEXT(cb, next);
551
552                         if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
553                                         cb->cb_arg == cb_arg)) {
554                                 TAILQ_REMOVE(&src->callbacks, cb, next);
555                                 rte_free(cb);
556                                 ret++;
557                         }
558                 }
559
560                 /* all callbacks for that source are removed. */
561                 if (TAILQ_EMPTY(&src->callbacks)) {
562                         TAILQ_REMOVE(&intr_sources, src, next);
563                         rte_free(src);
564                 }
565         }
566
567         rte_spinlock_unlock(&intr_lock);
568
569         /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
570         if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
571                 ret = -EPIPE;
572         }
573
574         return ret;
575 }
576
577 int
578 rte_intr_enable(struct rte_intr_handle *intr_handle)
579 {
580         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
581                 return -1;
582
583         switch (intr_handle->type){
584         /* write to the uio fd to enable the interrupt */
585         case RTE_INTR_HANDLE_UIO:
586                 if (uio_intr_enable(intr_handle))
587                         return -1;
588                 break;
589         case RTE_INTR_HANDLE_UIO_INTX:
590                 if (uio_intx_intr_enable(intr_handle))
591                         return -1;
592                 break;
593         /* not used at this moment */
594         case RTE_INTR_HANDLE_ALARM:
595                 return -1;
596 #ifdef VFIO_PRESENT
597         case RTE_INTR_HANDLE_VFIO_MSIX:
598                 if (vfio_enable_msix(intr_handle))
599                         return -1;
600                 break;
601         case RTE_INTR_HANDLE_VFIO_MSI:
602                 if (vfio_enable_msi(intr_handle))
603                         return -1;
604                 break;
605         case RTE_INTR_HANDLE_VFIO_LEGACY:
606                 if (vfio_enable_intx(intr_handle))
607                         return -1;
608                 break;
609 #endif
610         /* unknown handle type */
611         default:
612                 RTE_LOG(ERR, EAL,
613                         "Unknown handle type of fd %d\n",
614                                         intr_handle->fd);
615                 return -1;
616         }
617
618         return 0;
619 }
620
621 int
622 rte_intr_disable(struct rte_intr_handle *intr_handle)
623 {
624         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
625                 return -1;
626
627         switch (intr_handle->type){
628         /* write to the uio fd to disable the interrupt */
629         case RTE_INTR_HANDLE_UIO:
630                 if (uio_intr_disable(intr_handle))
631                         return -1;
632                 break;
633         case RTE_INTR_HANDLE_UIO_INTX:
634                 if (uio_intx_intr_disable(intr_handle))
635                         return -1;
636                 break;
637         /* not used at this moment */
638         case RTE_INTR_HANDLE_ALARM:
639                 return -1;
640 #ifdef VFIO_PRESENT
641         case RTE_INTR_HANDLE_VFIO_MSIX:
642                 if (vfio_disable_msix(intr_handle))
643                         return -1;
644                 break;
645         case RTE_INTR_HANDLE_VFIO_MSI:
646                 if (vfio_disable_msi(intr_handle))
647                         return -1;
648                 break;
649         case RTE_INTR_HANDLE_VFIO_LEGACY:
650                 if (vfio_disable_intx(intr_handle))
651                         return -1;
652                 break;
653 #endif
654         /* unknown handle type */
655         default:
656                 RTE_LOG(ERR, EAL,
657                         "Unknown handle type of fd %d\n",
658                                         intr_handle->fd);
659                 return -1;
660         }
661
662         return 0;
663 }
664
665 static int
666 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
667 {
668         int n, bytes_read;
669         struct rte_intr_source *src;
670         struct rte_intr_callback *cb;
671         union rte_intr_read_buffer buf;
672         struct rte_intr_callback active_cb;
673
674         for (n = 0; n < nfds; n++) {
675
676                 /**
677                  * if the pipe fd is ready to read, return out to
678                  * rebuild the wait list.
679                  */
680                 if (events[n].data.fd == intr_pipe.readfd){
681                         int r = read(intr_pipe.readfd, buf.charbuf,
682                                         sizeof(buf.charbuf));
683                         RTE_SET_USED(r);
684                         return -1;
685                 }
686                 rte_spinlock_lock(&intr_lock);
687                 TAILQ_FOREACH(src, &intr_sources, next)
688                         if (src->intr_handle.fd ==
689                                         events[n].data.fd)
690                                 break;
691                 if (src == NULL){
692                         rte_spinlock_unlock(&intr_lock);
693                         continue;
694                 }
695
696                 /* mark this interrupt source as active and release the lock. */
697                 src->active = 1;
698                 rte_spinlock_unlock(&intr_lock);
699
700                 /* set the length to be read dor different handle type */
701                 switch (src->intr_handle.type) {
702                 case RTE_INTR_HANDLE_UIO:
703                         bytes_read = sizeof(buf.uio_intr_count);
704                         break;
705                 case RTE_INTR_HANDLE_ALARM:
706                         bytes_read = sizeof(buf.timerfd_num);
707                         break;
708 #ifdef VFIO_PRESENT
709                 case RTE_INTR_HANDLE_VFIO_MSIX:
710                 case RTE_INTR_HANDLE_VFIO_MSI:
711                 case RTE_INTR_HANDLE_VFIO_LEGACY:
712                         bytes_read = sizeof(buf.vfio_intr_count);
713                         break;
714 #endif
715                 default:
716                         bytes_read = 1;
717                         break;
718                 }
719
720                 /**
721                  * read out to clear the ready-to-be-read flag
722                  * for epoll_wait.
723                  */
724                 bytes_read = read(events[n].data.fd, &buf, bytes_read);
725                 if (bytes_read < 0) {
726                         if (errno == EINTR || errno == EWOULDBLOCK)
727                                 continue;
728
729                         RTE_LOG(ERR, EAL, "Error reading from file "
730                                 "descriptor %d: %s\n", events[n].data.fd,
731                                                         strerror(errno));
732                 } else if (bytes_read == 0)
733                         RTE_LOG(ERR, EAL, "Read nothing from file "
734                                 "descriptor %d\n", events[n].data.fd);
735
736                 /* grab a lock, again to call callbacks and update status. */
737                 rte_spinlock_lock(&intr_lock);
738
739                 if (bytes_read > 0) {
740
741                         /* Finally, call all callbacks. */
742                         TAILQ_FOREACH(cb, &src->callbacks, next) {
743
744                                 /* make a copy and unlock. */
745                                 active_cb = *cb;
746                                 rte_spinlock_unlock(&intr_lock);
747
748                                 /* call the actual callback */
749                                 active_cb.cb_fn(&src->intr_handle,
750                                         active_cb.cb_arg);
751
752                                 /*get the lock back. */
753                                 rte_spinlock_lock(&intr_lock);
754                         }
755                 }
756
757                 /* we done with that interrupt source, release it. */
758                 src->active = 0;
759                 rte_spinlock_unlock(&intr_lock);
760         }
761
762         return 0;
763 }
764
765 /**
766  * It handles all the interrupts.
767  *
768  * @param pfd
769  *  epoll file descriptor.
770  * @param totalfds
771  *  The number of file descriptors added in epoll.
772  *
773  * @return
774  *  void
775  */
776 static void
777 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
778 {
779         struct epoll_event events[totalfds];
780         int nfds = 0;
781
782         for(;;) {
783                 nfds = epoll_wait(pfd, events, totalfds,
784                         EAL_INTR_EPOLL_WAIT_FOREVER);
785                 /* epoll_wait fail */
786                 if (nfds < 0) {
787                         if (errno == EINTR)
788                                 continue;
789                         RTE_LOG(ERR, EAL,
790                                 "epoll_wait returns with fail\n");
791                         return;
792                 }
793                 /* epoll_wait timeout, will never happens here */
794                 else if (nfds == 0)
795                         continue;
796                 /* epoll_wait has at least one fd ready to read */
797                 if (eal_intr_process_interrupts(events, nfds) < 0)
798                         return;
799         }
800 }
801
802 /**
803  * It builds/rebuilds up the epoll file descriptor with all the
804  * file descriptors being waited on. Then handles the interrupts.
805  *
806  * @param arg
807  *  pointer. (unused)
808  *
809  * @return
810  *  never return;
811  */
812 static __attribute__((noreturn)) void *
813 eal_intr_thread_main(__rte_unused void *arg)
814 {
815         struct epoll_event ev;
816
817         /* host thread, never break out */
818         for (;;) {
819                 /* build up the epoll fd with all descriptors we are to
820                  * wait on then pass it to the handle_interrupts function
821                  */
822                 static struct epoll_event pipe_event = {
823                         .events = EPOLLIN | EPOLLPRI,
824                 };
825                 struct rte_intr_source *src;
826                 unsigned numfds = 0;
827
828                 /* create epoll fd */
829                 int pfd = epoll_create(1);
830                 if (pfd < 0)
831                         rte_panic("Cannot create epoll instance\n");
832
833                 pipe_event.data.fd = intr_pipe.readfd;
834                 /**
835                  * add pipe fd into wait list, this pipe is used to
836                  * rebuild the wait list.
837                  */
838                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
839                                                 &pipe_event) < 0) {
840                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
841                                         intr_pipe.readfd, strerror(errno));
842                 }
843                 numfds++;
844
845                 rte_spinlock_lock(&intr_lock);
846
847                 TAILQ_FOREACH(src, &intr_sources, next) {
848                         if (src->callbacks.tqh_first == NULL)
849                                 continue; /* skip those with no callbacks */
850                         ev.events = EPOLLIN | EPOLLPRI;
851                         ev.data.fd = src->intr_handle.fd;
852
853                         /**
854                          * add all the uio device file descriptor
855                          * into wait list.
856                          */
857                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
858                                         src->intr_handle.fd, &ev) < 0){
859                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
860                                         src->intr_handle.fd, strerror(errno));
861                         }
862                         else
863                                 numfds++;
864                 }
865                 rte_spinlock_unlock(&intr_lock);
866                 /* serve the interrupt */
867                 eal_intr_handle_interrupts(pfd, numfds);
868
869                 /**
870                  * when we return, we need to rebuild the
871                  * list of fds to monitor.
872                  */
873                 close(pfd);
874         }
875 }
876
877 int
878 rte_eal_intr_init(void)
879 {
880         int ret = 0;
881
882         /* init the global interrupt source head */
883         TAILQ_INIT(&intr_sources);
884
885         /**
886          * create a pipe which will be waited by epoll and notified to
887          * rebuild the wait list of epoll.
888          */
889         if (pipe(intr_pipe.pipefd) < 0)
890                 return -1;
891
892         /* create the host thread to wait/handle the interrupt */
893         ret = pthread_create(&intr_thread, NULL,
894                         eal_intr_thread_main, NULL);
895         if (ret != 0)
896                 RTE_LOG(ERR, EAL,
897                         "Failed to create thread for interrupt handling\n");
898
899         return -ret;
900 }
901
902 static int
903 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
904                         struct rte_epoll_event *events)
905 {
906         unsigned int i, count = 0;
907         struct rte_epoll_event *rev;
908
909         for (i = 0; i < n; i++) {
910                 rev = evs[i].data.ptr;
911                 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
912                                                  RTE_EPOLL_EXEC))
913                         continue;
914
915                 events[count].status        = RTE_EPOLL_VALID;
916                 events[count].fd            = rev->fd;
917                 events[count].epfd          = rev->epfd;
918                 events[count].epdata.event  = rev->epdata.event;
919                 events[count].epdata.data   = rev->epdata.data;
920                 if (rev->epdata.cb_fun)
921                         rev->epdata.cb_fun(rev->fd,
922                                            rev->epdata.cb_arg);
923
924                 rte_compiler_barrier();
925                 rev->status = RTE_EPOLL_VALID;
926                 count++;
927         }
928         return count;
929 }
930
931 static inline int
932 eal_init_tls_epfd(void)
933 {
934         int pfd = epoll_create(255);
935
936         if (pfd < 0) {
937                 RTE_LOG(ERR, EAL,
938                         "Cannot create epoll instance\n");
939                 return -1;
940         }
941         return pfd;
942 }
943
944 int
945 rte_intr_tls_epfd(void)
946 {
947         if (RTE_PER_LCORE(_epfd) == -1)
948                 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
949
950         return RTE_PER_LCORE(_epfd);
951 }
952
953 int
954 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
955                int maxevents, int timeout)
956 {
957         struct epoll_event evs[maxevents];
958         int rc;
959
960         if (!events) {
961                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
962                 return -1;
963         }
964
965         /* using per thread epoll fd */
966         if (epfd == RTE_EPOLL_PER_THREAD)
967                 epfd = rte_intr_tls_epfd();
968
969         while (1) {
970                 rc = epoll_wait(epfd, evs, maxevents, timeout);
971                 if (likely(rc > 0)) {
972                         /* epoll_wait has at least one fd ready to read */
973                         rc = eal_epoll_process_event(evs, rc, events);
974                         break;
975                 } else if (rc < 0) {
976                         if (errno == EINTR)
977                                 continue;
978                         /* epoll_wait fail */
979                         RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
980                                 strerror(errno));
981                         rc = -1;
982                         break;
983                 }
984         }
985
986         return rc;
987 }
988
989 static inline void
990 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
991 {
992         while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
993                                     RTE_EPOLL_INVALID))
994                 while (ev->status != RTE_EPOLL_VALID)
995                         rte_pause();
996         memset(&ev->epdata, 0, sizeof(ev->epdata));
997         ev->fd = -1;
998         ev->epfd = -1;
999 }
1000
1001 int
1002 rte_epoll_ctl(int epfd, int op, int fd,
1003               struct rte_epoll_event *event)
1004 {
1005         struct epoll_event ev;
1006
1007         if (!event) {
1008                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1009                 return -1;
1010         }
1011
1012         /* using per thread epoll fd */
1013         if (epfd == RTE_EPOLL_PER_THREAD)
1014                 epfd = rte_intr_tls_epfd();
1015
1016         if (op == EPOLL_CTL_ADD) {
1017                 event->status = RTE_EPOLL_VALID;
1018                 event->fd = fd;  /* ignore fd in event */
1019                 event->epfd = epfd;
1020                 ev.data.ptr = (void *)event;
1021         }
1022
1023         ev.events = event->epdata.event;
1024         if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1025                 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1026                         op, fd, strerror(errno));
1027                 if (op == EPOLL_CTL_ADD)
1028                         /* rollback status when CTL_ADD fail */
1029                         event->status = RTE_EPOLL_INVALID;
1030                 return -1;
1031         }
1032
1033         if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1034                 eal_epoll_data_safe_free(event);
1035
1036         return 0;
1037 }