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