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