b5bcfc59afb87d4c88bb812842fa4be0119f03c6
[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         /* not used at this moment */
563         case RTE_INTR_HANDLE_DEV_EVENT:
564                 return -1;
565         /* unknown handle type */
566         default:
567                 RTE_LOG(ERR, EAL,
568                         "Unknown handle type of fd %d\n",
569                                         intr_handle->fd);
570                 return -1;
571         }
572
573         return 0;
574 }
575
576 int
577 rte_intr_disable(const struct rte_intr_handle *intr_handle)
578 {
579         if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
580                 return 0;
581
582         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
583                 return -1;
584
585         switch (intr_handle->type){
586         /* write to the uio fd to disable the interrupt */
587         case RTE_INTR_HANDLE_UIO:
588                 if (uio_intr_disable(intr_handle))
589                         return -1;
590                 break;
591         case RTE_INTR_HANDLE_UIO_INTX:
592                 if (uio_intx_intr_disable(intr_handle))
593                         return -1;
594                 break;
595         /* not used at this moment */
596         case RTE_INTR_HANDLE_ALARM:
597                 return -1;
598 #ifdef VFIO_PRESENT
599         case RTE_INTR_HANDLE_VFIO_MSIX:
600                 if (vfio_disable_msix(intr_handle))
601                         return -1;
602                 break;
603         case RTE_INTR_HANDLE_VFIO_MSI:
604                 if (vfio_disable_msi(intr_handle))
605                         return -1;
606                 break;
607         case RTE_INTR_HANDLE_VFIO_LEGACY:
608                 if (vfio_disable_intx(intr_handle))
609                         return -1;
610                 break;
611 #endif
612         /* not used at this moment */
613         case RTE_INTR_HANDLE_DEV_EVENT:
614                 return -1;
615         /* unknown handle type */
616         default:
617                 RTE_LOG(ERR, EAL,
618                         "Unknown handle type of fd %d\n",
619                                         intr_handle->fd);
620                 return -1;
621         }
622
623         return 0;
624 }
625
626 static int
627 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
628 {
629         bool call = false;
630         int n, bytes_read;
631         struct rte_intr_source *src;
632         struct rte_intr_callback *cb;
633         union rte_intr_read_buffer buf;
634         struct rte_intr_callback active_cb;
635
636         for (n = 0; n < nfds; n++) {
637
638                 /**
639                  * if the pipe fd is ready to read, return out to
640                  * rebuild the wait list.
641                  */
642                 if (events[n].data.fd == intr_pipe.readfd){
643                         int r = read(intr_pipe.readfd, buf.charbuf,
644                                         sizeof(buf.charbuf));
645                         RTE_SET_USED(r);
646                         return -1;
647                 }
648                 rte_spinlock_lock(&intr_lock);
649                 TAILQ_FOREACH(src, &intr_sources, next)
650                         if (src->intr_handle.fd ==
651                                         events[n].data.fd)
652                                 break;
653                 if (src == NULL){
654                         rte_spinlock_unlock(&intr_lock);
655                         continue;
656                 }
657
658                 /* mark this interrupt source as active and release the lock. */
659                 src->active = 1;
660                 rte_spinlock_unlock(&intr_lock);
661
662                 /* set the length to be read dor different handle type */
663                 switch (src->intr_handle.type) {
664                 case RTE_INTR_HANDLE_UIO:
665                 case RTE_INTR_HANDLE_UIO_INTX:
666                         bytes_read = sizeof(buf.uio_intr_count);
667                         break;
668                 case RTE_INTR_HANDLE_ALARM:
669                         bytes_read = sizeof(buf.timerfd_num);
670                         break;
671 #ifdef VFIO_PRESENT
672                 case RTE_INTR_HANDLE_VFIO_MSIX:
673                 case RTE_INTR_HANDLE_VFIO_MSI:
674                 case RTE_INTR_HANDLE_VFIO_LEGACY:
675                         bytes_read = sizeof(buf.vfio_intr_count);
676                         break;
677 #endif
678                 case RTE_INTR_HANDLE_VDEV:
679                 case RTE_INTR_HANDLE_EXT:
680                         bytes_read = 0;
681                         call = true;
682                         break;
683                 case RTE_INTR_HANDLE_DEV_EVENT:
684                         bytes_read = 0;
685                         call = true;
686                         break;
687                 default:
688                         bytes_read = 1;
689                         break;
690                 }
691
692                 if (bytes_read > 0) {
693                         /**
694                          * read out to clear the ready-to-be-read flag
695                          * for epoll_wait.
696                          */
697                         bytes_read = read(events[n].data.fd, &buf, bytes_read);
698                         if (bytes_read < 0) {
699                                 if (errno == EINTR || errno == EWOULDBLOCK)
700                                         continue;
701
702                                 RTE_LOG(ERR, EAL, "Error reading from file "
703                                         "descriptor %d: %s\n",
704                                         events[n].data.fd,
705                                         strerror(errno));
706                         } else if (bytes_read == 0)
707                                 RTE_LOG(ERR, EAL, "Read nothing from file "
708                                         "descriptor %d\n", events[n].data.fd);
709                         else
710                                 call = true;
711                 }
712
713                 /* grab a lock, again to call callbacks and update status. */
714                 rte_spinlock_lock(&intr_lock);
715
716                 if (call) {
717
718                         /* Finally, call all callbacks. */
719                         TAILQ_FOREACH(cb, &src->callbacks, next) {
720
721                                 /* make a copy and unlock. */
722                                 active_cb = *cb;
723                                 rte_spinlock_unlock(&intr_lock);
724
725                                 /* call the actual callback */
726                                 active_cb.cb_fn(active_cb.cb_arg);
727
728                                 /*get the lock back. */
729                                 rte_spinlock_lock(&intr_lock);
730                         }
731                 }
732
733                 /* we done with that interrupt source, release it. */
734                 src->active = 0;
735                 rte_spinlock_unlock(&intr_lock);
736         }
737
738         return 0;
739 }
740
741 /**
742  * It handles all the interrupts.
743  *
744  * @param pfd
745  *  epoll file descriptor.
746  * @param totalfds
747  *  The number of file descriptors added in epoll.
748  *
749  * @return
750  *  void
751  */
752 static void
753 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
754 {
755         struct epoll_event events[totalfds];
756         int nfds = 0;
757
758         for(;;) {
759                 nfds = epoll_wait(pfd, events, totalfds,
760                         EAL_INTR_EPOLL_WAIT_FOREVER);
761                 /* epoll_wait fail */
762                 if (nfds < 0) {
763                         if (errno == EINTR)
764                                 continue;
765                         RTE_LOG(ERR, EAL,
766                                 "epoll_wait returns with fail\n");
767                         return;
768                 }
769                 /* epoll_wait timeout, will never happens here */
770                 else if (nfds == 0)
771                         continue;
772                 /* epoll_wait has at least one fd ready to read */
773                 if (eal_intr_process_interrupts(events, nfds) < 0)
774                         return;
775         }
776 }
777
778 /**
779  * It builds/rebuilds up the epoll file descriptor with all the
780  * file descriptors being waited on. Then handles the interrupts.
781  *
782  * @param arg
783  *  pointer. (unused)
784  *
785  * @return
786  *  never return;
787  */
788 static __attribute__((noreturn)) void *
789 eal_intr_thread_main(__rte_unused void *arg)
790 {
791         struct epoll_event ev;
792
793         /* host thread, never break out */
794         for (;;) {
795                 /* build up the epoll fd with all descriptors we are to
796                  * wait on then pass it to the handle_interrupts function
797                  */
798                 static struct epoll_event pipe_event = {
799                         .events = EPOLLIN | EPOLLPRI,
800                 };
801                 struct rte_intr_source *src;
802                 unsigned numfds = 0;
803
804                 /* create epoll fd */
805                 int pfd = epoll_create(1);
806                 if (pfd < 0)
807                         rte_panic("Cannot create epoll instance\n");
808
809                 pipe_event.data.fd = intr_pipe.readfd;
810                 /**
811                  * add pipe fd into wait list, this pipe is used to
812                  * rebuild the wait list.
813                  */
814                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
815                                                 &pipe_event) < 0) {
816                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
817                                         intr_pipe.readfd, strerror(errno));
818                 }
819                 numfds++;
820
821                 rte_spinlock_lock(&intr_lock);
822
823                 TAILQ_FOREACH(src, &intr_sources, next) {
824                         if (src->callbacks.tqh_first == NULL)
825                                 continue; /* skip those with no callbacks */
826                         ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
827                         ev.data.fd = src->intr_handle.fd;
828
829                         /**
830                          * add all the uio device file descriptor
831                          * into wait list.
832                          */
833                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
834                                         src->intr_handle.fd, &ev) < 0){
835                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
836                                         src->intr_handle.fd, strerror(errno));
837                         }
838                         else
839                                 numfds++;
840                 }
841                 rte_spinlock_unlock(&intr_lock);
842                 /* serve the interrupt */
843                 eal_intr_handle_interrupts(pfd, numfds);
844
845                 /**
846                  * when we return, we need to rebuild the
847                  * list of fds to monitor.
848                  */
849                 close(pfd);
850         }
851 }
852
853 int
854 rte_eal_intr_init(void)
855 {
856         int ret = 0, ret_1 = 0;
857         char thread_name[RTE_MAX_THREAD_NAME_LEN];
858
859         /* init the global interrupt source head */
860         TAILQ_INIT(&intr_sources);
861
862         /**
863          * create a pipe which will be waited by epoll and notified to
864          * rebuild the wait list of epoll.
865          */
866         if (pipe(intr_pipe.pipefd) < 0) {
867                 rte_errno = errno;
868                 return -1;
869         }
870
871         /* create the host thread to wait/handle the interrupt */
872         ret = pthread_create(&intr_thread, NULL,
873                         eal_intr_thread_main, NULL);
874         if (ret != 0) {
875                 rte_errno = ret;
876                 RTE_LOG(ERR, EAL,
877                         "Failed to create thread for interrupt handling\n");
878         } else {
879                 /* Set thread_name for aid in debugging. */
880                 snprintf(thread_name, sizeof(thread_name),
881                         "eal-intr-thread");
882                 ret_1 = rte_thread_setname(intr_thread, thread_name);
883                 if (ret_1 != 0)
884                         RTE_LOG(DEBUG, EAL,
885                         "Failed to set thread name for interrupt handling\n");
886         }
887
888         return -ret;
889 }
890
891 static void
892 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
893 {
894         union rte_intr_read_buffer buf;
895         int bytes_read = 0;
896         int nbytes;
897
898         switch (intr_handle->type) {
899         case RTE_INTR_HANDLE_UIO:
900         case RTE_INTR_HANDLE_UIO_INTX:
901                 bytes_read = sizeof(buf.uio_intr_count);
902                 break;
903 #ifdef VFIO_PRESENT
904         case RTE_INTR_HANDLE_VFIO_MSIX:
905         case RTE_INTR_HANDLE_VFIO_MSI:
906         case RTE_INTR_HANDLE_VFIO_LEGACY:
907                 bytes_read = sizeof(buf.vfio_intr_count);
908                 break;
909 #endif
910         case RTE_INTR_HANDLE_VDEV:
911                 bytes_read = intr_handle->efd_counter_size;
912                 /* For vdev, number of bytes to read is set by driver */
913                 break;
914         case RTE_INTR_HANDLE_EXT:
915                 return;
916         default:
917                 bytes_read = 1;
918                 RTE_LOG(INFO, EAL, "unexpected intr type\n");
919                 break;
920         }
921
922         /**
923          * read out to clear the ready-to-be-read flag
924          * for epoll_wait.
925          */
926         if (bytes_read == 0)
927                 return;
928         do {
929                 nbytes = read(fd, &buf, bytes_read);
930                 if (nbytes < 0) {
931                         if (errno == EINTR || errno == EWOULDBLOCK ||
932                             errno == EAGAIN)
933                                 continue;
934                         RTE_LOG(ERR, EAL,
935                                 "Error reading from fd %d: %s\n",
936                                 fd, strerror(errno));
937                 } else if (nbytes == 0)
938                         RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
939                 return;
940         } while (1);
941 }
942
943 static int
944 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
945                         struct rte_epoll_event *events)
946 {
947         unsigned int i, count = 0;
948         struct rte_epoll_event *rev;
949
950         for (i = 0; i < n; i++) {
951                 rev = evs[i].data.ptr;
952                 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
953                                                  RTE_EPOLL_EXEC))
954                         continue;
955
956                 events[count].status        = RTE_EPOLL_VALID;
957                 events[count].fd            = rev->fd;
958                 events[count].epfd          = rev->epfd;
959                 events[count].epdata.event  = rev->epdata.event;
960                 events[count].epdata.data   = rev->epdata.data;
961                 if (rev->epdata.cb_fun)
962                         rev->epdata.cb_fun(rev->fd,
963                                            rev->epdata.cb_arg);
964
965                 rte_compiler_barrier();
966                 rev->status = RTE_EPOLL_VALID;
967                 count++;
968         }
969         return count;
970 }
971
972 static inline int
973 eal_init_tls_epfd(void)
974 {
975         int pfd = epoll_create(255);
976
977         if (pfd < 0) {
978                 RTE_LOG(ERR, EAL,
979                         "Cannot create epoll instance\n");
980                 return -1;
981         }
982         return pfd;
983 }
984
985 int
986 rte_intr_tls_epfd(void)
987 {
988         if (RTE_PER_LCORE(_epfd) == -1)
989                 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
990
991         return RTE_PER_LCORE(_epfd);
992 }
993
994 int
995 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
996                int maxevents, int timeout)
997 {
998         struct epoll_event evs[maxevents];
999         int rc;
1000
1001         if (!events) {
1002                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1003                 return -1;
1004         }
1005
1006         /* using per thread epoll fd */
1007         if (epfd == RTE_EPOLL_PER_THREAD)
1008                 epfd = rte_intr_tls_epfd();
1009
1010         while (1) {
1011                 rc = epoll_wait(epfd, evs, maxevents, timeout);
1012                 if (likely(rc > 0)) {
1013                         /* epoll_wait has at least one fd ready to read */
1014                         rc = eal_epoll_process_event(evs, rc, events);
1015                         break;
1016                 } else if (rc < 0) {
1017                         if (errno == EINTR)
1018                                 continue;
1019                         /* epoll_wait fail */
1020                         RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1021                                 strerror(errno));
1022                         rc = -1;
1023                         break;
1024                 } else {
1025                         /* rc == 0, epoll_wait timed out */
1026                         break;
1027                 }
1028         }
1029
1030         return rc;
1031 }
1032
1033 static inline void
1034 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1035 {
1036         while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1037                                     RTE_EPOLL_INVALID))
1038                 while (ev->status != RTE_EPOLL_VALID)
1039                         rte_pause();
1040         memset(&ev->epdata, 0, sizeof(ev->epdata));
1041         ev->fd = -1;
1042         ev->epfd = -1;
1043 }
1044
1045 int
1046 rte_epoll_ctl(int epfd, int op, int fd,
1047               struct rte_epoll_event *event)
1048 {
1049         struct epoll_event ev;
1050
1051         if (!event) {
1052                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1053                 return -1;
1054         }
1055
1056         /* using per thread epoll fd */
1057         if (epfd == RTE_EPOLL_PER_THREAD)
1058                 epfd = rte_intr_tls_epfd();
1059
1060         if (op == EPOLL_CTL_ADD) {
1061                 event->status = RTE_EPOLL_VALID;
1062                 event->fd = fd;  /* ignore fd in event */
1063                 event->epfd = epfd;
1064                 ev.data.ptr = (void *)event;
1065         }
1066
1067         ev.events = event->epdata.event;
1068         if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1069                 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1070                         op, fd, strerror(errno));
1071                 if (op == EPOLL_CTL_ADD)
1072                         /* rollback status when CTL_ADD fail */
1073                         event->status = RTE_EPOLL_INVALID;
1074                 return -1;
1075         }
1076
1077         if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1078                 eal_epoll_data_safe_free(event);
1079
1080         return 0;
1081 }
1082
1083 int
1084 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1085                 int op, unsigned int vec, void *data)
1086 {
1087         struct rte_epoll_event *rev;
1088         struct rte_epoll_data *epdata;
1089         int epfd_op;
1090         unsigned int efd_idx;
1091         int rc = 0;
1092
1093         efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1094                 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1095
1096         if (!intr_handle || intr_handle->nb_efd == 0 ||
1097             efd_idx >= intr_handle->nb_efd) {
1098                 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1099                 return -EPERM;
1100         }
1101
1102         switch (op) {
1103         case RTE_INTR_EVENT_ADD:
1104                 epfd_op = EPOLL_CTL_ADD;
1105                 rev = &intr_handle->elist[efd_idx];
1106                 if (rev->status != RTE_EPOLL_INVALID) {
1107                         RTE_LOG(INFO, EAL, "Event already been added.\n");
1108                         return -EEXIST;
1109                 }
1110
1111                 /* attach to intr vector fd */
1112                 epdata = &rev->epdata;
1113                 epdata->event  = EPOLLIN | EPOLLPRI | EPOLLET;
1114                 epdata->data   = data;
1115                 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1116                 epdata->cb_arg = (void *)intr_handle;
1117                 rc = rte_epoll_ctl(epfd, epfd_op,
1118                                    intr_handle->efds[efd_idx], rev);
1119                 if (!rc)
1120                         RTE_LOG(DEBUG, EAL,
1121                                 "efd %d associated with vec %d added on epfd %d"
1122                                 "\n", rev->fd, vec, epfd);
1123                 else
1124                         rc = -EPERM;
1125                 break;
1126         case RTE_INTR_EVENT_DEL:
1127                 epfd_op = EPOLL_CTL_DEL;
1128                 rev = &intr_handle->elist[efd_idx];
1129                 if (rev->status == RTE_EPOLL_INVALID) {
1130                         RTE_LOG(INFO, EAL, "Event does not exist.\n");
1131                         return -EPERM;
1132                 }
1133
1134                 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1135                 if (rc)
1136                         rc = -EPERM;
1137                 break;
1138         default:
1139                 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1140                 rc = -EPERM;
1141         }
1142
1143         return rc;
1144 }
1145
1146 void
1147 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1148 {
1149         uint32_t i;
1150         struct rte_epoll_event *rev;
1151
1152         for (i = 0; i < intr_handle->nb_efd; i++) {
1153                 rev = &intr_handle->elist[i];
1154                 if (rev->status == RTE_EPOLL_INVALID)
1155                         continue;
1156                 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1157                         /* force free if the entry valid */
1158                         eal_epoll_data_safe_free(rev);
1159                         rev->status = RTE_EPOLL_INVALID;
1160                 }
1161         }
1162 }
1163
1164 int
1165 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1166 {
1167         uint32_t i;
1168         int fd;
1169         uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1170
1171         assert(nb_efd != 0);
1172
1173         if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1174                 for (i = 0; i < n; i++) {
1175                         fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1176                         if (fd < 0) {
1177                                 RTE_LOG(ERR, EAL,
1178                                         "can't setup eventfd, error %i (%s)\n",
1179                                         errno, strerror(errno));
1180                                 return -errno;
1181                         }
1182                         intr_handle->efds[i] = fd;
1183                 }
1184                 intr_handle->nb_efd   = n;
1185                 intr_handle->max_intr = NB_OTHER_INTR + n;
1186         } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1187                 /* only check, initialization would be done in vdev driver.*/
1188                 if (intr_handle->efd_counter_size >
1189                     sizeof(union rte_intr_read_buffer)) {
1190                         RTE_LOG(ERR, EAL, "the efd_counter_size is oversized");
1191                         return -EINVAL;
1192                 }
1193         } else {
1194                 intr_handle->efds[0]  = intr_handle->fd;
1195                 intr_handle->nb_efd   = RTE_MIN(nb_efd, 1U);
1196                 intr_handle->max_intr = NB_OTHER_INTR;
1197         }
1198
1199         return 0;
1200 }
1201
1202 void
1203 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1204 {
1205         uint32_t i;
1206
1207         rte_intr_free_epoll_fd(intr_handle);
1208         if (intr_handle->max_intr > intr_handle->nb_efd) {
1209                 for (i = 0; i < intr_handle->nb_efd; i++)
1210                         close(intr_handle->efds[i]);
1211         }
1212         intr_handle->nb_efd = 0;
1213         intr_handle->max_intr = 0;
1214 }
1215
1216 int
1217 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1218 {
1219         return !(!intr_handle->nb_efd);
1220 }
1221
1222 int
1223 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1224 {
1225         if (!rte_intr_dp_is_en(intr_handle))
1226                 return 1;
1227         else
1228                 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1229 }
1230
1231 int
1232 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1233 {
1234         if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1235                 return 1;
1236
1237         if (intr_handle->type == RTE_INTR_HANDLE_VDEV)
1238                 return 1;
1239
1240         return 0;
1241 }