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