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