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