New upstream version 17.11.5
[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_launch.h>
55 #include <rte_eal.h>
56 #include <rte_per_lcore.h>
57 #include <rte_lcore.h>
58 #include <rte_atomic.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_debug.h>
61 #include <rte_log.h>
62 #include <rte_malloc.h>
63 #include <rte_errno.h>
64 #include <rte_spinlock.h>
65 #include <rte_pause.h>
66
67 #include "eal_private.h"
68 #include "eal_vfio.h"
69 #include "eal_thread.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(const 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(const 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_MASK;
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 masking 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(const 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(const 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(const 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         /* 0 < irq_set->count < RTE_MAX_RXTX_INTR_VEC_ID + 1 */
294         irq_set->count = intr_handle->max_intr ?
295                 (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID + 1 ?
296                 RTE_MAX_RXTX_INTR_VEC_ID + 1 : intr_handle->max_intr) : 1;
297         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
298         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
299         irq_set->start = 0;
300         fd_ptr = (int *) &irq_set->data;
301         /* INTR vector offset 0 reserve for non-efds mapping */
302         fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = intr_handle->fd;
303         memcpy(&fd_ptr[RTE_INTR_VEC_RXTX_OFFSET], intr_handle->efds,
304                 sizeof(*intr_handle->efds) * intr_handle->nb_efd);
305
306         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
307
308         if (ret) {
309                 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
310                                                 intr_handle->fd);
311                 return -1;
312         }
313
314         return 0;
315 }
316
317 /* disable MSI-X interrupts */
318 static int
319 vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
320         struct vfio_irq_set *irq_set;
321         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
322         int len, ret;
323
324         len = sizeof(struct vfio_irq_set);
325
326         irq_set = (struct vfio_irq_set *) irq_set_buf;
327         irq_set->argsz = len;
328         irq_set->count = 0;
329         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
330         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
331         irq_set->start = 0;
332
333         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
334
335         if (ret)
336                 RTE_LOG(ERR, EAL,
337                         "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
338
339         return ret;
340 }
341 #endif
342
343 static int
344 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
345 {
346         unsigned char command_high;
347
348         /* use UIO config file descriptor for uio_pci_generic */
349         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
350                 RTE_LOG(ERR, EAL,
351                         "Error reading interrupts status for fd %d\n",
352                         intr_handle->uio_cfg_fd);
353                 return -1;
354         }
355         /* disable interrupts */
356         command_high |= 0x4;
357         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
358                 RTE_LOG(ERR, EAL,
359                         "Error disabling interrupts for fd %d\n",
360                         intr_handle->uio_cfg_fd);
361                 return -1;
362         }
363
364         return 0;
365 }
366
367 static int
368 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
369 {
370         unsigned char command_high;
371
372         /* use UIO config file descriptor for uio_pci_generic */
373         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
374                 RTE_LOG(ERR, EAL,
375                         "Error reading interrupts status for fd %d\n",
376                         intr_handle->uio_cfg_fd);
377                 return -1;
378         }
379         /* enable interrupts */
380         command_high &= ~0x4;
381         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
382                 RTE_LOG(ERR, EAL,
383                         "Error enabling interrupts for fd %d\n",
384                         intr_handle->uio_cfg_fd);
385                 return -1;
386         }
387
388         return 0;
389 }
390
391 static int
392 uio_intr_disable(const struct rte_intr_handle *intr_handle)
393 {
394         const int value = 0;
395
396         if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
397                 RTE_LOG(ERR, EAL,
398                         "Error disabling interrupts for fd %d (%s)\n",
399                         intr_handle->fd, strerror(errno));
400                 return -1;
401         }
402         return 0;
403 }
404
405 static int
406 uio_intr_enable(const struct rte_intr_handle *intr_handle)
407 {
408         const int value = 1;
409
410         if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
411                 RTE_LOG(ERR, EAL,
412                         "Error enabling interrupts for fd %d (%s)\n",
413                         intr_handle->fd, strerror(errno));
414                 return -1;
415         }
416         return 0;
417 }
418
419 int
420 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
421                         rte_intr_callback_fn cb, void *cb_arg)
422 {
423         int ret, wake_thread;
424         struct rte_intr_source *src;
425         struct rte_intr_callback *callback;
426
427         wake_thread = 0;
428
429         /* first do parameter checking */
430         if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
431                 RTE_LOG(ERR, EAL,
432                         "Registering with invalid input parameter\n");
433                 return -EINVAL;
434         }
435
436         /* allocate a new interrupt callback entity */
437         callback = rte_zmalloc("interrupt callback list",
438                                 sizeof(*callback), 0);
439         if (callback == NULL) {
440                 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
441                 return -ENOMEM;
442         }
443         callback->cb_fn = cb;
444         callback->cb_arg = cb_arg;
445
446         rte_spinlock_lock(&intr_lock);
447
448         /* check if there is at least one callback registered for the fd */
449         TAILQ_FOREACH(src, &intr_sources, next) {
450                 if (src->intr_handle.fd == intr_handle->fd) {
451                         /* we had no interrupts for this */
452                         if (TAILQ_EMPTY(&src->callbacks))
453                                 wake_thread = 1;
454
455                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
456                         ret = 0;
457                         break;
458                 }
459         }
460
461         /* no existing callbacks for this - add new source */
462         if (src == NULL) {
463                 if ((src = rte_zmalloc("interrupt source list",
464                                 sizeof(*src), 0)) == NULL) {
465                         RTE_LOG(ERR, EAL, "Can not allocate memory\n");
466                         rte_free(callback);
467                         ret = -ENOMEM;
468                 } else {
469                         src->intr_handle = *intr_handle;
470                         TAILQ_INIT(&src->callbacks);
471                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
472                         TAILQ_INSERT_TAIL(&intr_sources, src, next);
473                         wake_thread = 1;
474                         ret = 0;
475                 }
476         }
477
478         rte_spinlock_unlock(&intr_lock);
479
480         /**
481          * check if need to notify the pipe fd waited by epoll_wait to
482          * rebuild the wait list.
483          */
484         if (wake_thread)
485                 if (write(intr_pipe.writefd, "1", 1) < 0)
486                         return -EPIPE;
487
488         return ret;
489 }
490
491 int
492 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
493                         rte_intr_callback_fn cb_fn, void *cb_arg)
494 {
495         int ret;
496         struct rte_intr_source *src;
497         struct rte_intr_callback *cb, *next;
498
499         /* do parameter checking first */
500         if (intr_handle == NULL || intr_handle->fd < 0) {
501                 RTE_LOG(ERR, EAL,
502                 "Unregistering with invalid input parameter\n");
503                 return -EINVAL;
504         }
505
506         rte_spinlock_lock(&intr_lock);
507
508         /* check if the insterrupt source for the fd is existent */
509         TAILQ_FOREACH(src, &intr_sources, next)
510                 if (src->intr_handle.fd == intr_handle->fd)
511                         break;
512
513         /* No interrupt source registered for the fd */
514         if (src == NULL) {
515                 ret = -ENOENT;
516
517         /* interrupt source has some active callbacks right now. */
518         } else if (src->active != 0) {
519                 ret = -EAGAIN;
520
521         /* ok to remove. */
522         } else {
523                 ret = 0;
524
525                 /*walk through the callbacks and remove all that match. */
526                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
527
528                         next = TAILQ_NEXT(cb, next);
529
530                         if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
531                                         cb->cb_arg == cb_arg)) {
532                                 TAILQ_REMOVE(&src->callbacks, cb, next);
533                                 rte_free(cb);
534                                 ret++;
535                         }
536                 }
537
538                 /* all callbacks for that source are removed. */
539                 if (TAILQ_EMPTY(&src->callbacks)) {
540                         TAILQ_REMOVE(&intr_sources, src, next);
541                         rte_free(src);
542                 }
543         }
544
545         rte_spinlock_unlock(&intr_lock);
546
547         /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
548         if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
549                 ret = -EPIPE;
550         }
551
552         return ret;
553 }
554
555 int
556 rte_intr_enable(const struct rte_intr_handle *intr_handle)
557 {
558         if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
559                 return 0;
560
561         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
562                 return -1;
563
564         switch (intr_handle->type){
565         /* write to the uio fd to enable the interrupt */
566         case RTE_INTR_HANDLE_UIO:
567                 if (uio_intr_enable(intr_handle))
568                         return -1;
569                 break;
570         case RTE_INTR_HANDLE_UIO_INTX:
571                 if (uio_intx_intr_enable(intr_handle))
572                         return -1;
573                 break;
574         /* not used at this moment */
575         case RTE_INTR_HANDLE_ALARM:
576                 return -1;
577 #ifdef VFIO_PRESENT
578         case RTE_INTR_HANDLE_VFIO_MSIX:
579                 if (vfio_enable_msix(intr_handle))
580                         return -1;
581                 break;
582         case RTE_INTR_HANDLE_VFIO_MSI:
583                 if (vfio_enable_msi(intr_handle))
584                         return -1;
585                 break;
586         case RTE_INTR_HANDLE_VFIO_LEGACY:
587                 if (vfio_enable_intx(intr_handle))
588                         return -1;
589                 break;
590 #endif
591         /* unknown handle type */
592         default:
593                 RTE_LOG(ERR, EAL,
594                         "Unknown handle type of fd %d\n",
595                                         intr_handle->fd);
596                 return -1;
597         }
598
599         return 0;
600 }
601
602 int
603 rte_intr_disable(const struct rte_intr_handle *intr_handle)
604 {
605         if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
606                 return 0;
607
608         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
609                 return -1;
610
611         switch (intr_handle->type){
612         /* write to the uio fd to disable the interrupt */
613         case RTE_INTR_HANDLE_UIO:
614                 if (uio_intr_disable(intr_handle))
615                         return -1;
616                 break;
617         case RTE_INTR_HANDLE_UIO_INTX:
618                 if (uio_intx_intr_disable(intr_handle))
619                         return -1;
620                 break;
621         /* not used at this moment */
622         case RTE_INTR_HANDLE_ALARM:
623                 return -1;
624 #ifdef VFIO_PRESENT
625         case RTE_INTR_HANDLE_VFIO_MSIX:
626                 if (vfio_disable_msix(intr_handle))
627                         return -1;
628                 break;
629         case RTE_INTR_HANDLE_VFIO_MSI:
630                 if (vfio_disable_msi(intr_handle))
631                         return -1;
632                 break;
633         case RTE_INTR_HANDLE_VFIO_LEGACY:
634                 if (vfio_disable_intx(intr_handle))
635                         return -1;
636                 break;
637 #endif
638         /* unknown handle type */
639         default:
640                 RTE_LOG(ERR, EAL,
641                         "Unknown handle type of fd %d\n",
642                                         intr_handle->fd);
643                 return -1;
644         }
645
646         return 0;
647 }
648
649 static int
650 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
651 {
652         bool call = false;
653         int n, bytes_read;
654         struct rte_intr_source *src;
655         struct rte_intr_callback *cb, *next;
656         union rte_intr_read_buffer buf;
657         struct rte_intr_callback active_cb;
658
659         for (n = 0; n < nfds; n++) {
660
661                 /**
662                  * if the pipe fd is ready to read, return out to
663                  * rebuild the wait list.
664                  */
665                 if (events[n].data.fd == intr_pipe.readfd){
666                         int r = read(intr_pipe.readfd, buf.charbuf,
667                                         sizeof(buf.charbuf));
668                         RTE_SET_USED(r);
669                         return -1;
670                 }
671                 rte_spinlock_lock(&intr_lock);
672                 TAILQ_FOREACH(src, &intr_sources, next)
673                         if (src->intr_handle.fd ==
674                                         events[n].data.fd)
675                                 break;
676                 if (src == NULL){
677                         rte_spinlock_unlock(&intr_lock);
678                         continue;
679                 }
680
681                 /* mark this interrupt source as active and release the lock. */
682                 src->active = 1;
683                 rte_spinlock_unlock(&intr_lock);
684
685                 /* set the length to be read dor different handle type */
686                 switch (src->intr_handle.type) {
687                 case RTE_INTR_HANDLE_UIO:
688                 case RTE_INTR_HANDLE_UIO_INTX:
689                         bytes_read = sizeof(buf.uio_intr_count);
690                         break;
691                 case RTE_INTR_HANDLE_ALARM:
692                         bytes_read = sizeof(buf.timerfd_num);
693                         break;
694 #ifdef VFIO_PRESENT
695                 case RTE_INTR_HANDLE_VFIO_MSIX:
696                 case RTE_INTR_HANDLE_VFIO_MSI:
697                 case RTE_INTR_HANDLE_VFIO_LEGACY:
698                         bytes_read = sizeof(buf.vfio_intr_count);
699                         break;
700 #endif
701                 case RTE_INTR_HANDLE_VDEV:
702                 case RTE_INTR_HANDLE_EXT:
703                         bytes_read = 0;
704                         call = true;
705                         break;
706
707                 default:
708                         bytes_read = 1;
709                         break;
710                 }
711
712                 if (bytes_read > 0) {
713                         /**
714                          * read out to clear the ready-to-be-read flag
715                          * for epoll_wait.
716                          */
717                         bytes_read = read(events[n].data.fd, &buf, bytes_read);
718                         if (bytes_read < 0) {
719                                 if (errno == EINTR || errno == EWOULDBLOCK)
720                                         continue;
721
722                                 RTE_LOG(ERR, EAL, "Error reading from file "
723                                         "descriptor %d: %s\n",
724                                         events[n].data.fd,
725                                         strerror(errno));
726                                 /*
727                                  * The device is unplugged or buggy, remove
728                                  * it as an interrupt source and return to
729                                  * force the wait list to be rebuilt.
730                                  */
731                                 rte_spinlock_lock(&intr_lock);
732                                 TAILQ_REMOVE(&intr_sources, src, next);
733                                 rte_spinlock_unlock(&intr_lock);
734
735                                 for (cb = TAILQ_FIRST(&src->callbacks); cb;
736                                                         cb = next) {
737                                         next = TAILQ_NEXT(cb, next);
738                                         TAILQ_REMOVE(&src->callbacks, cb, next);
739                                         free(cb);
740                                 }
741                                 free(src);
742                                 return -1;
743                         } else if (bytes_read == 0)
744                                 RTE_LOG(ERR, EAL, "Read nothing from file "
745                                         "descriptor %d\n", events[n].data.fd);
746                         else
747                                 call = true;
748                 }
749
750                 /* grab a lock, again to call callbacks and update status. */
751                 rte_spinlock_lock(&intr_lock);
752
753                 if (call) {
754
755                         /* Finally, call all callbacks. */
756                         TAILQ_FOREACH(cb, &src->callbacks, next) {
757
758                                 /* make a copy and unlock. */
759                                 active_cb = *cb;
760                                 rte_spinlock_unlock(&intr_lock);
761
762                                 /* call the actual callback */
763                                 active_cb.cb_fn(active_cb.cb_arg);
764
765                                 /*get the lock back. */
766                                 rte_spinlock_lock(&intr_lock);
767                         }
768                 }
769
770                 /* we done with that interrupt source, release it. */
771                 src->active = 0;
772                 rte_spinlock_unlock(&intr_lock);
773         }
774
775         return 0;
776 }
777
778 /**
779  * It handles all the interrupts.
780  *
781  * @param pfd
782  *  epoll file descriptor.
783  * @param totalfds
784  *  The number of file descriptors added in epoll.
785  *
786  * @return
787  *  void
788  */
789 static void
790 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
791 {
792         struct epoll_event events[totalfds];
793         int nfds = 0;
794
795         for(;;) {
796                 nfds = epoll_wait(pfd, events, totalfds,
797                         EAL_INTR_EPOLL_WAIT_FOREVER);
798                 /* epoll_wait fail */
799                 if (nfds < 0) {
800                         if (errno == EINTR)
801                                 continue;
802                         RTE_LOG(ERR, EAL,
803                                 "epoll_wait returns with fail\n");
804                         return;
805                 }
806                 /* epoll_wait timeout, will never happens here */
807                 else if (nfds == 0)
808                         continue;
809                 /* epoll_wait has at least one fd ready to read */
810                 if (eal_intr_process_interrupts(events, nfds) < 0)
811                         return;
812         }
813 }
814
815 /**
816  * It builds/rebuilds up the epoll file descriptor with all the
817  * file descriptors being waited on. Then handles the interrupts.
818  *
819  * @param arg
820  *  pointer. (unused)
821  *
822  * @return
823  *  never return;
824  */
825 static __attribute__((noreturn)) void *
826 eal_intr_thread_main(__rte_unused void *arg)
827 {
828         struct epoll_event ev;
829
830         /* host thread, never break out */
831         for (;;) {
832                 /* build up the epoll fd with all descriptors we are to
833                  * wait on then pass it to the handle_interrupts function
834                  */
835                 static struct epoll_event pipe_event = {
836                         .events = EPOLLIN | EPOLLPRI,
837                 };
838                 struct rte_intr_source *src;
839                 unsigned numfds = 0;
840
841                 /* create epoll fd */
842                 int pfd = epoll_create(1);
843                 if (pfd < 0)
844                         rte_panic("Cannot create epoll instance\n");
845
846                 pipe_event.data.fd = intr_pipe.readfd;
847                 /**
848                  * add pipe fd into wait list, this pipe is used to
849                  * rebuild the wait list.
850                  */
851                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
852                                                 &pipe_event) < 0) {
853                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
854                                         intr_pipe.readfd, strerror(errno));
855                 }
856                 numfds++;
857
858                 rte_spinlock_lock(&intr_lock);
859
860                 TAILQ_FOREACH(src, &intr_sources, next) {
861                         if (src->callbacks.tqh_first == NULL)
862                                 continue; /* skip those with no callbacks */
863                         ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
864                         ev.data.fd = src->intr_handle.fd;
865
866                         /**
867                          * add all the uio device file descriptor
868                          * into wait list.
869                          */
870                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
871                                         src->intr_handle.fd, &ev) < 0){
872                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
873                                         src->intr_handle.fd, strerror(errno));
874                         }
875                         else
876                                 numfds++;
877                 }
878                 rte_spinlock_unlock(&intr_lock);
879                 /* serve the interrupt */
880                 eal_intr_handle_interrupts(pfd, numfds);
881
882                 /**
883                  * when we return, we need to rebuild the
884                  * list of fds to monitor.
885                  */
886                 close(pfd);
887         }
888 }
889
890 int
891 rte_eal_intr_init(void)
892 {
893         int ret = 0, ret_1 = 0;
894         char thread_name[RTE_MAX_THREAD_NAME_LEN];
895
896         /* init the global interrupt source head */
897         TAILQ_INIT(&intr_sources);
898
899         /**
900          * create a pipe which will be waited by epoll and notified to
901          * rebuild the wait list of epoll.
902          */
903         if (pipe(intr_pipe.pipefd) < 0) {
904                 rte_errno = errno;
905                 return -1;
906         }
907
908         /* create the host thread to wait/handle the interrupt */
909         ret = pthread_create(&intr_thread, NULL,
910                         eal_intr_thread_main, NULL);
911         if (ret != 0) {
912                 rte_errno = ret;
913                 RTE_LOG(ERR, EAL,
914                         "Failed to create thread for interrupt handling\n");
915         } else {
916                 /* Set thread_name for aid in debugging. */
917                 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
918                         "eal-intr-thread");
919                 ret_1 = rte_thread_setname(intr_thread, thread_name);
920                 if (ret_1 != 0)
921                         RTE_LOG(DEBUG, EAL,
922                         "Failed to set thread name for interrupt handling\n");
923         }
924
925         return -ret;
926 }
927
928 static void
929 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
930 {
931         union rte_intr_read_buffer buf;
932         int bytes_read = 0;
933         int nbytes;
934
935         switch (intr_handle->type) {
936         case RTE_INTR_HANDLE_UIO:
937         case RTE_INTR_HANDLE_UIO_INTX:
938                 bytes_read = sizeof(buf.uio_intr_count);
939                 break;
940 #ifdef VFIO_PRESENT
941         case RTE_INTR_HANDLE_VFIO_MSIX:
942         case RTE_INTR_HANDLE_VFIO_MSI:
943         case RTE_INTR_HANDLE_VFIO_LEGACY:
944                 bytes_read = sizeof(buf.vfio_intr_count);
945                 break;
946 #endif
947         case RTE_INTR_HANDLE_VDEV:
948                 bytes_read = intr_handle->efd_counter_size;
949                 /* For vdev, number of bytes to read is set by driver */
950                 break;
951         case RTE_INTR_HANDLE_EXT:
952                 return;
953         default:
954                 bytes_read = 1;
955                 RTE_LOG(INFO, EAL, "unexpected intr type\n");
956                 break;
957         }
958
959         /**
960          * read out to clear the ready-to-be-read flag
961          * for epoll_wait.
962          */
963         if (bytes_read == 0)
964                 return;
965         do {
966                 nbytes = read(fd, &buf, bytes_read);
967                 if (nbytes < 0) {
968                         if (errno == EINTR || errno == EWOULDBLOCK ||
969                             errno == EAGAIN)
970                                 continue;
971                         RTE_LOG(ERR, EAL,
972                                 "Error reading from fd %d: %s\n",
973                                 fd, strerror(errno));
974                 } else if (nbytes == 0)
975                         RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
976                 return;
977         } while (1);
978 }
979
980 static int
981 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
982                         struct rte_epoll_event *events)
983 {
984         unsigned int i, count = 0;
985         struct rte_epoll_event *rev;
986
987         for (i = 0; i < n; i++) {
988                 rev = evs[i].data.ptr;
989                 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
990                                                  RTE_EPOLL_EXEC))
991                         continue;
992
993                 events[count].status        = RTE_EPOLL_VALID;
994                 events[count].fd            = rev->fd;
995                 events[count].epfd          = rev->epfd;
996                 events[count].epdata.event  = rev->epdata.event;
997                 events[count].epdata.data   = rev->epdata.data;
998                 if (rev->epdata.cb_fun)
999                         rev->epdata.cb_fun(rev->fd,
1000                                            rev->epdata.cb_arg);
1001
1002                 rte_compiler_barrier();
1003                 rev->status = RTE_EPOLL_VALID;
1004                 count++;
1005         }
1006         return count;
1007 }
1008
1009 static inline int
1010 eal_init_tls_epfd(void)
1011 {
1012         int pfd = epoll_create(255);
1013
1014         if (pfd < 0) {
1015                 RTE_LOG(ERR, EAL,
1016                         "Cannot create epoll instance\n");
1017                 return -1;
1018         }
1019         return pfd;
1020 }
1021
1022 int
1023 rte_intr_tls_epfd(void)
1024 {
1025         if (RTE_PER_LCORE(_epfd) == -1)
1026                 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1027
1028         return RTE_PER_LCORE(_epfd);
1029 }
1030
1031 int
1032 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1033                int maxevents, int timeout)
1034 {
1035         struct epoll_event evs[maxevents];
1036         int rc;
1037
1038         if (!events) {
1039                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1040                 return -1;
1041         }
1042
1043         /* using per thread epoll fd */
1044         if (epfd == RTE_EPOLL_PER_THREAD)
1045                 epfd = rte_intr_tls_epfd();
1046
1047         while (1) {
1048                 rc = epoll_wait(epfd, evs, maxevents, timeout);
1049                 if (likely(rc > 0)) {
1050                         /* epoll_wait has at least one fd ready to read */
1051                         rc = eal_epoll_process_event(evs, rc, events);
1052                         break;
1053                 } else if (rc < 0) {
1054                         if (errno == EINTR)
1055                                 continue;
1056                         /* epoll_wait fail */
1057                         RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1058                                 strerror(errno));
1059                         rc = -1;
1060                         break;
1061                 } else {
1062                         /* rc == 0, epoll_wait timed out */
1063                         break;
1064                 }
1065         }
1066
1067         return rc;
1068 }
1069
1070 static inline void
1071 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1072 {
1073         while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1074                                     RTE_EPOLL_INVALID))
1075                 while (ev->status != RTE_EPOLL_VALID)
1076                         rte_pause();
1077         memset(&ev->epdata, 0, sizeof(ev->epdata));
1078         ev->fd = -1;
1079         ev->epfd = -1;
1080 }
1081
1082 int
1083 rte_epoll_ctl(int epfd, int op, int fd,
1084               struct rte_epoll_event *event)
1085 {
1086         struct epoll_event ev;
1087
1088         if (!event) {
1089                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1090                 return -1;
1091         }
1092
1093         /* using per thread epoll fd */
1094         if (epfd == RTE_EPOLL_PER_THREAD)
1095                 epfd = rte_intr_tls_epfd();
1096
1097         if (op == EPOLL_CTL_ADD) {
1098                 event->status = RTE_EPOLL_VALID;
1099                 event->fd = fd;  /* ignore fd in event */
1100                 event->epfd = epfd;
1101                 ev.data.ptr = (void *)event;
1102         }
1103
1104         ev.events = event->epdata.event;
1105         if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1106                 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1107                         op, fd, strerror(errno));
1108                 if (op == EPOLL_CTL_ADD)
1109                         /* rollback status when CTL_ADD fail */
1110                         event->status = RTE_EPOLL_INVALID;
1111                 return -1;
1112         }
1113
1114         if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1115                 eal_epoll_data_safe_free(event);
1116
1117         return 0;
1118 }
1119
1120 int
1121 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1122                 int op, unsigned int vec, void *data)
1123 {
1124         struct rte_epoll_event *rev;
1125         struct rte_epoll_data *epdata;
1126         int epfd_op;
1127         unsigned int efd_idx;
1128         int rc = 0;
1129
1130         efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1131                 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1132
1133         if (!intr_handle || intr_handle->nb_efd == 0 ||
1134             efd_idx >= intr_handle->nb_efd) {
1135                 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1136                 return -EPERM;
1137         }
1138
1139         switch (op) {
1140         case RTE_INTR_EVENT_ADD:
1141                 epfd_op = EPOLL_CTL_ADD;
1142                 rev = &intr_handle->elist[efd_idx];
1143                 if (rev->status != RTE_EPOLL_INVALID) {
1144                         RTE_LOG(INFO, EAL, "Event already been added.\n");
1145                         return -EEXIST;
1146                 }
1147
1148                 /* attach to intr vector fd */
1149                 epdata = &rev->epdata;
1150                 epdata->event  = EPOLLIN | EPOLLPRI | EPOLLET;
1151                 epdata->data   = data;
1152                 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1153                 epdata->cb_arg = (void *)intr_handle;
1154                 rc = rte_epoll_ctl(epfd, epfd_op,
1155                                    intr_handle->efds[efd_idx], rev);
1156                 if (!rc)
1157                         RTE_LOG(DEBUG, EAL,
1158                                 "efd %d associated with vec %d added on epfd %d"
1159                                 "\n", rev->fd, vec, epfd);
1160                 else
1161                         rc = -EPERM;
1162                 break;
1163         case RTE_INTR_EVENT_DEL:
1164                 epfd_op = EPOLL_CTL_DEL;
1165                 rev = &intr_handle->elist[efd_idx];
1166                 if (rev->status == RTE_EPOLL_INVALID) {
1167                         RTE_LOG(INFO, EAL, "Event does not exist.\n");
1168                         return -EPERM;
1169                 }
1170
1171                 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1172                 if (rc)
1173                         rc = -EPERM;
1174                 break;
1175         default:
1176                 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1177                 rc = -EPERM;
1178         }
1179
1180         return rc;
1181 }
1182
1183 void
1184 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1185 {
1186         uint32_t i;
1187         struct rte_epoll_event *rev;
1188
1189         for (i = 0; i < intr_handle->nb_efd; i++) {
1190                 rev = &intr_handle->elist[i];
1191                 if (rev->status == RTE_EPOLL_INVALID)
1192                         continue;
1193                 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1194                         /* force free if the entry valid */
1195                         eal_epoll_data_safe_free(rev);
1196                         rev->status = RTE_EPOLL_INVALID;
1197                 }
1198         }
1199 }
1200
1201 int
1202 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1203 {
1204         uint32_t i;
1205         int fd;
1206         uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1207
1208         assert(nb_efd != 0);
1209
1210         if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1211                 for (i = 0; i < n; i++) {
1212                         fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1213                         if (fd < 0) {
1214                                 RTE_LOG(ERR, EAL,
1215                                         "can't setup eventfd, error %i (%s)\n",
1216                                         errno, strerror(errno));
1217                                 return -errno;
1218                         }
1219                         intr_handle->efds[i] = fd;
1220                 }
1221                 intr_handle->nb_efd   = n;
1222                 intr_handle->max_intr = NB_OTHER_INTR + n;
1223         } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1224                 /* only check, initialization would be done in vdev driver.*/
1225                 if (intr_handle->efd_counter_size >
1226                     sizeof(union rte_intr_read_buffer)) {
1227                         RTE_LOG(ERR, EAL, "the efd_counter_size is oversized");
1228                         return -EINVAL;
1229                 }
1230         } else {
1231                 intr_handle->efds[0]  = intr_handle->fd;
1232                 intr_handle->nb_efd   = RTE_MIN(nb_efd, 1U);
1233                 intr_handle->max_intr = NB_OTHER_INTR;
1234         }
1235
1236         return 0;
1237 }
1238
1239 void
1240 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1241 {
1242         uint32_t i;
1243
1244         rte_intr_free_epoll_fd(intr_handle);
1245         if (intr_handle->max_intr > intr_handle->nb_efd) {
1246                 for (i = 0; i < intr_handle->nb_efd; i++)
1247                         close(intr_handle->efds[i]);
1248         }
1249         intr_handle->nb_efd = 0;
1250         intr_handle->max_intr = 0;
1251 }
1252
1253 int
1254 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1255 {
1256         return !(!intr_handle->nb_efd);
1257 }
1258
1259 int
1260 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1261 {
1262         if (!rte_intr_dp_is_en(intr_handle))
1263                 return 1;
1264         else
1265                 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1266 }
1267
1268 int
1269 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1270 {
1271         if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1272                 return 1;
1273
1274         if (intr_handle->type == RTE_INTR_HANDLE_VDEV)
1275                 return 1;
1276
1277         return 0;
1278 }