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