8c5b834f9118417ab8ffd6ec1add153e158b592a
[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_tailq.h>
54 #include <rte_eal.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_ring.h>
60 #include <rte_debug.h>
61 #include <rte_log.h>
62 #include <rte_mempool.h>
63 #include <rte_pci.h>
64 #include <rte_malloc.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
67
68 #include "eal_private.h"
69 #include "eal_vfio.h"
70
71 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
72
73 /**
74  * union for pipe fds.
75  */
76 union intr_pipefds{
77         struct {
78                 int pipefd[2];
79         };
80         struct {
81                 int readfd;
82                 int writefd;
83         };
84 };
85
86 /**
87  * union buffer for reading on different devices
88  */
89 union rte_intr_read_buffer {
90         int uio_intr_count;              /* for uio device */
91 #ifdef VFIO_PRESENT
92         uint64_t vfio_intr_count;        /* for vfio device */
93 #endif
94         uint64_t timerfd_num;            /* for timerfd */
95         char charbuf[16];                /* for others */
96 };
97
98 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
99 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
100
101 struct rte_intr_callback {
102         TAILQ_ENTRY(rte_intr_callback) next;
103         rte_intr_callback_fn cb_fn;  /**< callback address */
104         void *cb_arg;                /**< parameter for callback */
105 };
106
107 struct rte_intr_source {
108         TAILQ_ENTRY(rte_intr_source) next;
109         struct rte_intr_handle intr_handle; /**< interrupt handle */
110         struct rte_intr_cb_list callbacks;  /**< user callbacks */
111         uint32_t active;
112 };
113
114 /* global spinlock for interrupt data operation */
115 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
116
117 /* union buffer for pipe read/write */
118 static union intr_pipefds intr_pipe;
119
120 /* interrupt sources list */
121 static struct rte_intr_source_list intr_sources;
122
123 /* interrupt handling thread */
124 static pthread_t intr_thread;
125
126 /* VFIO interrupts */
127 #ifdef VFIO_PRESENT
128
129 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
130
131 /* enable legacy (INTx) interrupts */
132 static int
133 vfio_enable_intx(struct rte_intr_handle *intr_handle) {
134         struct vfio_irq_set *irq_set;
135         char irq_set_buf[IRQ_SET_BUF_LEN];
136         int len, ret;
137         int *fd_ptr;
138
139         len = sizeof(irq_set_buf);
140
141         /* enable INTx */
142         irq_set = (struct vfio_irq_set *) irq_set_buf;
143         irq_set->argsz = len;
144         irq_set->count = 1;
145         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
146         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
147         irq_set->start = 0;
148         fd_ptr = (int *) &irq_set->data;
149         *fd_ptr = intr_handle->fd;
150
151         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
152
153         if (ret) {
154                 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
155                                                 intr_handle->fd);
156                 return -1;
157         }
158
159         /* unmask INTx after enabling */
160         memset(irq_set, 0, len);
161         len = sizeof(struct vfio_irq_set);
162         irq_set->argsz = len;
163         irq_set->count = 1;
164         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
165         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
166         irq_set->start = 0;
167
168         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
169
170         if (ret) {
171                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
172                                                 intr_handle->fd);
173                 return -1;
174         }
175         return 0;
176 }
177
178 /* disable legacy (INTx) interrupts */
179 static int
180 vfio_disable_intx(struct rte_intr_handle *intr_handle) {
181         struct vfio_irq_set *irq_set;
182         char irq_set_buf[IRQ_SET_BUF_LEN];
183         int len, ret;
184
185         len = sizeof(struct vfio_irq_set);
186
187         /* mask interrupts before disabling */
188         irq_set = (struct vfio_irq_set *) irq_set_buf;
189         irq_set->argsz = len;
190         irq_set->count = 1;
191         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
192         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
193         irq_set->start = 0;
194
195         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
196
197         if (ret) {
198                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
199                                                 intr_handle->fd);
200                 return -1;
201         }
202
203         /* disable INTx*/
204         memset(irq_set, 0, len);
205         irq_set->argsz = len;
206         irq_set->count = 0;
207         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
208         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
209         irq_set->start = 0;
210
211         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
212
213         if (ret) {
214                 RTE_LOG(ERR, EAL,
215                         "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
216                 return -1;
217         }
218         return 0;
219 }
220
221 /* enable MSI-X interrupts */
222 static int
223 vfio_enable_msi(struct rte_intr_handle *intr_handle) {
224         int len, ret;
225         char irq_set_buf[IRQ_SET_BUF_LEN];
226         struct vfio_irq_set *irq_set;
227         int *fd_ptr;
228
229         len = sizeof(irq_set_buf);
230
231         irq_set = (struct vfio_irq_set *) irq_set_buf;
232         irq_set->argsz = len;
233         irq_set->count = 1;
234         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
235         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
236         irq_set->start = 0;
237         fd_ptr = (int *) &irq_set->data;
238         *fd_ptr = intr_handle->fd;
239
240         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
241
242         if (ret) {
243                 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
244                                                 intr_handle->fd);
245                 return -1;
246         }
247
248         /* manually trigger interrupt to enable it */
249         memset(irq_set, 0, len);
250         len = sizeof(struct vfio_irq_set);
251         irq_set->argsz = len;
252         irq_set->count = 1;
253         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
254         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
255         irq_set->start = 0;
256
257         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
258
259         if (ret) {
260                 RTE_LOG(ERR, EAL, "Error triggering MSI interrupts for fd %d\n",
261                                                 intr_handle->fd);
262                 return -1;
263         }
264         return 0;
265 }
266
267 /* disable MSI-X interrupts */
268 static int
269 vfio_disable_msi(struct rte_intr_handle *intr_handle) {
270         struct vfio_irq_set *irq_set;
271         char irq_set_buf[IRQ_SET_BUF_LEN];
272         int len, ret;
273
274         len = sizeof(struct vfio_irq_set);
275
276         irq_set = (struct vfio_irq_set *) irq_set_buf;
277         irq_set->argsz = len;
278         irq_set->count = 0;
279         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
280         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
281         irq_set->start = 0;
282
283         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
284
285         if (ret)
286                 RTE_LOG(ERR, EAL,
287                         "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
288
289         return ret;
290 }
291
292 /* enable MSI-X interrupts */
293 static int
294 vfio_enable_msix(struct rte_intr_handle *intr_handle) {
295         int len, ret;
296         char irq_set_buf[IRQ_SET_BUF_LEN];
297         struct vfio_irq_set *irq_set;
298         int *fd_ptr;
299
300         len = sizeof(irq_set_buf);
301
302         irq_set = (struct vfio_irq_set *) irq_set_buf;
303         irq_set->argsz = len;
304         irq_set->count = 1;
305         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
306         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
307         irq_set->start = 0;
308         fd_ptr = (int *) &irq_set->data;
309         *fd_ptr = intr_handle->fd;
310
311         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
312
313         if (ret) {
314                 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
315                                                 intr_handle->fd);
316                 return -1;
317         }
318
319         /* manually trigger interrupt to enable it */
320         memset(irq_set, 0, len);
321         len = sizeof(struct vfio_irq_set);
322         irq_set->argsz = len;
323         irq_set->count = 1;
324         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
325         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
326         irq_set->start = 0;
327
328         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
329
330         if (ret) {
331                 RTE_LOG(ERR, EAL, "Error triggering MSI-X interrupts for fd %d\n",
332                                                 intr_handle->fd);
333                 return -1;
334         }
335         return 0;
336 }
337
338 /* disable MSI-X interrupts */
339 static int
340 vfio_disable_msix(struct rte_intr_handle *intr_handle) {
341         struct vfio_irq_set *irq_set;
342         char irq_set_buf[IRQ_SET_BUF_LEN];
343         int len, ret;
344
345         len = sizeof(struct vfio_irq_set);
346
347         irq_set = (struct vfio_irq_set *) irq_set_buf;
348         irq_set->argsz = len;
349         irq_set->count = 0;
350         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
351         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
352         irq_set->start = 0;
353
354         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
355
356         if (ret)
357                 RTE_LOG(ERR, EAL,
358                         "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
359
360         return ret;
361 }
362 #endif
363
364 static int
365 uio_intr_disable(struct rte_intr_handle *intr_handle)
366 {
367         unsigned char command_high;
368
369         /* use UIO config file descriptor for uio_pci_generic */
370         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
371                 RTE_LOG(ERR, EAL,
372                         "Error reading interrupts status for fd %d\n",
373                         intr_handle->uio_cfg_fd);
374                 return -1;
375         }
376         /* disable interrupts */
377         command_high |= 0x4;
378         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
379                 RTE_LOG(ERR, EAL,
380                         "Error disabling interrupts for fd %d\n",
381                         intr_handle->uio_cfg_fd);
382                 return -1;
383         }
384
385         return 0;
386 }
387
388 static int
389 uio_intr_enable(struct rte_intr_handle *intr_handle)
390 {
391         unsigned char command_high;
392
393         /* use UIO config file descriptor for uio_pci_generic */
394         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
395                 RTE_LOG(ERR, EAL,
396                         "Error reading interrupts status for fd %d\n",
397                         intr_handle->uio_cfg_fd);
398                 return -1;
399         }
400         /* enable interrupts */
401         command_high &= ~0x4;
402         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
403                 RTE_LOG(ERR, EAL,
404                         "Error enabling interrupts for fd %d\n",
405                         intr_handle->uio_cfg_fd);
406                 return -1;
407         }
408
409         return 0;
410 }
411
412 int
413 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
414                         rte_intr_callback_fn cb, void *cb_arg)
415 {
416         int ret, wake_thread;
417         struct rte_intr_source *src;
418         struct rte_intr_callback *callback;
419
420         wake_thread = 0;
421
422         /* first do parameter checking */
423         if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
424                 RTE_LOG(ERR, EAL,
425                         "Registering with invalid input parameter\n");
426                 return -EINVAL;
427         }
428
429         /* allocate a new interrupt callback entity */
430         callback = rte_zmalloc("interrupt callback list",
431                                 sizeof(*callback), 0);
432         if (callback == NULL) {
433                 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
434                 return -ENOMEM;
435         }
436         callback->cb_fn = cb;
437         callback->cb_arg = cb_arg;
438
439         rte_spinlock_lock(&intr_lock);
440
441         /* check if there is at least one callback registered for the fd */
442         TAILQ_FOREACH(src, &intr_sources, next) {
443                 if (src->intr_handle.fd == intr_handle->fd) {
444                         /* we had no interrupts for this */
445                         if TAILQ_EMPTY(&src->callbacks)
446                                 wake_thread = 1;
447
448                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
449                         ret = 0;
450                         break;
451                 }
452         }
453
454         /* no existing callbacks for this - add new source */
455         if (src == NULL) {
456                 if ((src = rte_zmalloc("interrupt source list",
457                                 sizeof(*src), 0)) == NULL) {
458                         RTE_LOG(ERR, EAL, "Can not allocate memory\n");
459                         rte_free(callback);
460                         ret = -ENOMEM;
461                 } else {
462                         src->intr_handle = *intr_handle;
463                         TAILQ_INIT(&src->callbacks);
464                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
465                         TAILQ_INSERT_TAIL(&intr_sources, src, next);
466                         wake_thread = 1;
467                         ret = 0;
468                 }
469         }
470
471         rte_spinlock_unlock(&intr_lock);
472
473         /**
474          * check if need to notify the pipe fd waited by epoll_wait to
475          * rebuild the wait list.
476          */
477         if (wake_thread)
478                 if (write(intr_pipe.writefd, "1", 1) < 0)
479                         return -EPIPE;
480
481         return (ret);
482 }
483
484 int
485 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
486                         rte_intr_callback_fn cb_fn, void *cb_arg)
487 {
488         int ret;
489         struct rte_intr_source *src;
490         struct rte_intr_callback *cb, *next;
491
492         /* do parameter checking first */
493         if (intr_handle == NULL || intr_handle->fd < 0) {
494                 RTE_LOG(ERR, EAL,
495                 "Unregistering with invalid input parameter\n");
496                 return -EINVAL;
497         }
498
499         rte_spinlock_lock(&intr_lock);
500
501         /* check if the insterrupt source for the fd is existent */
502         TAILQ_FOREACH(src, &intr_sources, next)
503                 if (src->intr_handle.fd == intr_handle->fd)
504                         break;
505
506         /* No interrupt source registered for the fd */
507         if (src == NULL) {
508                 ret = -ENOENT;
509
510         /* interrupt source has some active callbacks right now. */
511         } else if (src->active != 0) {
512                 ret = -EAGAIN;
513
514         /* ok to remove. */
515         } else {
516                 ret = 0;
517
518                 /*walk through the callbacks and remove all that match. */
519                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
520
521                         next = TAILQ_NEXT(cb, next);
522
523                         if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
524                                         cb->cb_arg == cb_arg)) {
525                                 TAILQ_REMOVE(&src->callbacks, cb, next);
526                                 rte_free(cb);
527                                 ret++;
528                         }
529                 }
530
531                 /* all callbacks for that source are removed. */
532                 if (TAILQ_EMPTY(&src->callbacks)) {
533                         TAILQ_REMOVE(&intr_sources, src, next);
534                         rte_free(src);
535                 }
536         }
537
538         rte_spinlock_unlock(&intr_lock);
539
540         /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
541         if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
542                 ret = -EPIPE;
543         }
544
545         return (ret);
546 }
547
548 int
549 rte_intr_enable(struct rte_intr_handle *intr_handle)
550 {
551         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
552                 return -1;
553
554         switch (intr_handle->type){
555         /* write to the uio fd to enable the interrupt */
556         case RTE_INTR_HANDLE_UIO:
557                 if (uio_intr_enable(intr_handle))
558                         return -1;
559                 break;
560         /* not used at this moment */
561         case RTE_INTR_HANDLE_ALARM:
562                 return -1;
563 #ifdef VFIO_PRESENT
564         case RTE_INTR_HANDLE_VFIO_MSIX:
565                 if (vfio_enable_msix(intr_handle))
566                         return -1;
567                 break;
568         case RTE_INTR_HANDLE_VFIO_MSI:
569                 if (vfio_enable_msi(intr_handle))
570                         return -1;
571                 break;
572         case RTE_INTR_HANDLE_VFIO_LEGACY:
573                 if (vfio_enable_intx(intr_handle))
574                         return -1;
575                 break;
576 #endif
577         /* unknown handle type */
578         default:
579                 RTE_LOG(ERR, EAL,
580                         "Unknown handle type of fd %d\n",
581                                         intr_handle->fd);
582                 return -1;
583         }
584
585         return 0;
586 }
587
588 int
589 rte_intr_disable(struct rte_intr_handle *intr_handle)
590 {
591         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
592                 return -1;
593
594         switch (intr_handle->type){
595         /* write to the uio fd to disable the interrupt */
596         case RTE_INTR_HANDLE_UIO:
597                 if (uio_intr_disable(intr_handle))
598                         return -1;
599                 break;
600         /* not used at this moment */
601         case RTE_INTR_HANDLE_ALARM:
602                 return -1;
603 #ifdef VFIO_PRESENT
604         case RTE_INTR_HANDLE_VFIO_MSIX:
605                 if (vfio_disable_msix(intr_handle))
606                         return -1;
607                 break;
608         case RTE_INTR_HANDLE_VFIO_MSI:
609                 if (vfio_disable_msi(intr_handle))
610                         return -1;
611                 break;
612         case RTE_INTR_HANDLE_VFIO_LEGACY:
613                 if (vfio_disable_intx(intr_handle))
614                         return -1;
615                 break;
616 #endif
617         /* unknown handle type */
618         default:
619                 RTE_LOG(ERR, EAL,
620                         "Unknown handle type of fd %d\n",
621                                         intr_handle->fd);
622                 return -1;
623         }
624
625         return 0;
626 }
627
628 static int
629 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
630 {
631         int n, bytes_read;
632         struct rte_intr_source *src;
633         struct rte_intr_callback *cb;
634         union rte_intr_read_buffer buf;
635         struct rte_intr_callback active_cb;
636
637         for (n = 0; n < nfds; n++) {
638
639                 /**
640                  * if the pipe fd is ready to read, return out to
641                  * rebuild the wait list.
642                  */
643                 if (events[n].data.fd == intr_pipe.readfd){
644                         int r = read(intr_pipe.readfd, buf.charbuf,
645                                         sizeof(buf.charbuf));
646                         RTE_SET_USED(r);
647                         return -1;
648                 }
649                 rte_spinlock_lock(&intr_lock);
650                 TAILQ_FOREACH(src, &intr_sources, next)
651                         if (src->intr_handle.fd ==
652                                         events[n].data.fd)
653                                 break;
654                 if (src == NULL){
655                         rte_spinlock_unlock(&intr_lock);
656                         continue;
657                 }
658
659                 /* mark this interrupt source as active and release the lock. */
660                 src->active = 1;
661                 rte_spinlock_unlock(&intr_lock);
662
663                 /* set the length to be read dor different handle type */
664                 switch (src->intr_handle.type) {
665                 case RTE_INTR_HANDLE_UIO:
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                 default:
679                         bytes_read = 1;
680                         break;
681                 }
682
683                 /**
684                  * read out to clear the ready-to-be-read flag
685                  * for epoll_wait.
686                  */
687                 bytes_read = read(events[n].data.fd, &buf, bytes_read);
688
689                 if (bytes_read < 0)
690                         RTE_LOG(ERR, EAL, "Error reading from file "
691                                 "descriptor %d: %s\n", events[n].data.fd,
692                                                         strerror(errno));
693                 else if (bytes_read == 0)
694                         RTE_LOG(ERR, EAL, "Read nothing from file "
695                                 "descriptor %d\n", events[n].data.fd);
696
697                 /* grab a lock, again to call callbacks and update status. */
698                 rte_spinlock_lock(&intr_lock);
699
700                 if (bytes_read > 0) {
701
702                         /* Finally, call all callbacks. */
703                         TAILQ_FOREACH(cb, &src->callbacks, next) {
704
705                                 /* make a copy and unlock. */
706                                 active_cb = *cb;
707                                 rte_spinlock_unlock(&intr_lock);
708
709                                 /* call the actual callback */
710                                 active_cb.cb_fn(&src->intr_handle,
711                                         active_cb.cb_arg);
712
713                                 /*get the lock back. */
714                                 rte_spinlock_lock(&intr_lock);
715                         }
716                 }
717
718                 /* we done with that interrupt source, release it. */
719                 src->active = 0;
720                 rte_spinlock_unlock(&intr_lock);
721         }
722
723         return 0;
724 }
725
726 /**
727  * It handles all the interrupts.
728  *
729  * @param pfd
730  *  epoll file descriptor.
731  * @param totalfds
732  *  The number of file descriptors added in epoll.
733  *
734  * @return
735  *  void
736  */
737 static void
738 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
739 {
740         struct epoll_event events[totalfds];
741         int nfds = 0;
742
743         for(;;) {
744                 nfds = epoll_wait(pfd, events, totalfds,
745                         EAL_INTR_EPOLL_WAIT_FOREVER);
746                 /* epoll_wait fail */
747                 if (nfds < 0) {
748                         if (errno == EINTR)
749                                 continue;
750                         RTE_LOG(ERR, EAL,
751                                 "epoll_wait returns with fail\n");
752                         return;
753                 }
754                 /* epoll_wait timeout, will never happens here */
755                 else if (nfds == 0)
756                         continue;
757                 /* epoll_wait has at least one fd ready to read */
758                 if (eal_intr_process_interrupts(events, nfds) < 0)
759                         return;
760         }
761 }
762
763 /**
764  * It builds/rebuilds up the epoll file descriptor with all the
765  * file descriptors being waited on. Then handles the interrupts.
766  *
767  * @param arg
768  *  pointer. (unused)
769  *
770  * @return
771  *  never return;
772  */
773 static __attribute__((noreturn)) void *
774 eal_intr_thread_main(__rte_unused void *arg)
775 {
776         struct epoll_event ev;
777
778         /* host thread, never break out */
779         for (;;) {
780                 /* build up the epoll fd with all descriptors we are to
781                  * wait on then pass it to the handle_interrupts function
782                  */
783                 static struct epoll_event pipe_event = {
784                         .events = EPOLLIN | EPOLLPRI,
785                 };
786                 struct rte_intr_source *src;
787                 unsigned numfds = 0;
788
789                 /* create epoll fd */
790                 int pfd = epoll_create(1);
791                 if (pfd < 0)
792                         rte_panic("Cannot create epoll instance\n");
793
794                 pipe_event.data.fd = intr_pipe.readfd;
795                 /**
796                  * add pipe fd into wait list, this pipe is used to
797                  * rebuild the wait list.
798                  */
799                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
800                                                 &pipe_event) < 0) {
801                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
802                                         intr_pipe.readfd, strerror(errno));
803                 }
804                 numfds++;
805
806                 rte_spinlock_lock(&intr_lock);
807
808                 TAILQ_FOREACH(src, &intr_sources, next) {
809                         if (src->callbacks.tqh_first == NULL)
810                                 continue; /* skip those with no callbacks */
811                         ev.events = EPOLLIN | EPOLLPRI;
812                         ev.data.fd = src->intr_handle.fd;
813
814                         /**
815                          * add all the uio device file descriptor
816                          * into wait list.
817                          */
818                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
819                                         src->intr_handle.fd, &ev) < 0){
820                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
821                                         src->intr_handle.fd, strerror(errno));
822                         }
823                         else
824                                 numfds++;
825                 }
826                 rte_spinlock_unlock(&intr_lock);
827                 /* serve the interrupt */
828                 eal_intr_handle_interrupts(pfd, numfds);
829
830                 /**
831                  * when we return, we need to rebuild the
832                  * list of fds to monitor.
833                  */
834                 close(pfd);
835         }
836 }
837
838 int
839 rte_eal_intr_init(void)
840 {
841         int ret = 0;
842
843         /* init the global interrupt source head */
844         TAILQ_INIT(&intr_sources);
845
846         /**
847          * create a pipe which will be waited by epoll and notified to
848          * rebuild the wait list of epoll.
849          */
850         if (pipe(intr_pipe.pipefd) < 0)
851                 return -1;
852
853         /* create the host thread to wait/handle the interrupt */
854         ret = pthread_create(&intr_thread, NULL,
855                         eal_intr_thread_main, NULL);
856         if (ret != 0)
857                 RTE_LOG(ERR, EAL,
858                         "Failed to create thread for interrupt handling\n");
859
860         return -ret;
861 }
862