dc2668a48f6fa60ebea62137d0715940ba16054a
[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 int
365 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
366                         rte_intr_callback_fn cb, void *cb_arg)
367 {
368         int ret, wake_thread;
369         struct rte_intr_source *src;
370         struct rte_intr_callback *callback;
371
372         wake_thread = 0;
373
374         /* first do parameter checking */
375         if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
376                 RTE_LOG(ERR, EAL,
377                         "Registering with invalid input parameter\n");
378                 return -EINVAL;
379         }
380
381         /* allocate a new interrupt callback entity */
382         callback = rte_zmalloc("interrupt callback list",
383                                 sizeof(*callback), 0);
384         if (callback == NULL) {
385                 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
386                 return -ENOMEM;
387         }
388         callback->cb_fn = cb;
389         callback->cb_arg = cb_arg;
390
391         rte_spinlock_lock(&intr_lock);
392
393         /* check if there is at least one callback registered for the fd */
394         TAILQ_FOREACH(src, &intr_sources, next) {
395                 if (src->intr_handle.fd == intr_handle->fd) {
396                         /* we had no interrupts for this */
397                         if TAILQ_EMPTY(&src->callbacks)
398                                 wake_thread = 1;
399
400                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
401                         ret = 0;
402                         break;
403                 }
404         }
405
406         /* no existing callbacks for this - add new source */
407         if (src == NULL) {
408                 if ((src = rte_zmalloc("interrupt source list",
409                                 sizeof(*src), 0)) == NULL) {
410                         RTE_LOG(ERR, EAL, "Can not allocate memory\n");
411                         rte_free(callback);
412                         ret = -ENOMEM;
413                 } else {
414                         src->intr_handle = *intr_handle;
415                         TAILQ_INIT(&src->callbacks);
416                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
417                         TAILQ_INSERT_TAIL(&intr_sources, src, next);
418                         wake_thread = 1;
419                         ret = 0;
420                 }
421         }
422
423         rte_spinlock_unlock(&intr_lock);
424
425         /**
426          * check if need to notify the pipe fd waited by epoll_wait to
427          * rebuild the wait list.
428          */
429         if (wake_thread)
430                 if (write(intr_pipe.writefd, "1", 1) < 0)
431                         return -EPIPE;
432
433         return (ret);
434 }
435
436 int
437 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
438                         rte_intr_callback_fn cb_fn, void *cb_arg)
439 {
440         int ret;
441         struct rte_intr_source *src;
442         struct rte_intr_callback *cb, *next;
443
444         /* do parameter checking first */
445         if (intr_handle == NULL || intr_handle->fd < 0) {
446                 RTE_LOG(ERR, EAL,
447                 "Unregistering with invalid input parameter\n");
448                 return -EINVAL;
449         }
450
451         rte_spinlock_lock(&intr_lock);
452
453         /* check if the insterrupt source for the fd is existent */
454         TAILQ_FOREACH(src, &intr_sources, next)
455                 if (src->intr_handle.fd == intr_handle->fd)
456                         break;
457
458         /* No interrupt source registered for the fd */
459         if (src == NULL) {
460                 ret = -ENOENT;
461
462         /* interrupt source has some active callbacks right now. */
463         } else if (src->active != 0) {
464                 ret = -EAGAIN;
465
466         /* ok to remove. */
467         } else {
468                 ret = 0;
469
470                 /*walk through the callbacks and remove all that match. */
471                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
472
473                         next = TAILQ_NEXT(cb, next);
474
475                         if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
476                                         cb->cb_arg == cb_arg)) {
477                                 TAILQ_REMOVE(&src->callbacks, cb, next);
478                                 rte_free(cb);
479                                 ret++;
480                         }
481                 }
482
483                 /* all callbacks for that source are removed. */
484                 if (TAILQ_EMPTY(&src->callbacks)) {
485                         TAILQ_REMOVE(&intr_sources, src, next);
486                         rte_free(src);
487                 }
488         }
489
490         rte_spinlock_unlock(&intr_lock);
491
492         /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
493         if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
494                 ret = -EPIPE;
495         }
496
497         return (ret);
498 }
499
500 int
501 rte_intr_enable(struct rte_intr_handle *intr_handle)
502 {
503         const int value = 1;
504
505         if (!intr_handle || intr_handle->fd < 0)
506                 return -1;
507
508         switch (intr_handle->type){
509         /* write to the uio fd to enable the interrupt */
510         case RTE_INTR_HANDLE_UIO:
511                 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
512                         RTE_LOG(ERR, EAL,
513                                 "Error enabling interrupts for fd %d\n",
514                                                         intr_handle->fd);
515                         return -1;
516                 }
517                 break;
518         /* not used at this moment */
519         case RTE_INTR_HANDLE_ALARM:
520                 return -1;
521 #ifdef VFIO_PRESENT
522         case RTE_INTR_HANDLE_VFIO_MSIX:
523                 if (vfio_enable_msix(intr_handle))
524                         return -1;
525                 break;
526         case RTE_INTR_HANDLE_VFIO_MSI:
527                 if (vfio_enable_msi(intr_handle))
528                         return -1;
529                 break;
530         case RTE_INTR_HANDLE_VFIO_LEGACY:
531                 if (vfio_enable_intx(intr_handle))
532                         return -1;
533                 break;
534 #endif
535         /* unknown handle type */
536         default:
537                 RTE_LOG(ERR, EAL,
538                         "Unknown handle type of fd %d\n",
539                                         intr_handle->fd);
540                 return -1;
541         }
542
543         return 0;
544 }
545
546 int
547 rte_intr_disable(struct rte_intr_handle *intr_handle)
548 {
549         const int value = 0;
550
551         if (!intr_handle || intr_handle->fd < 0)
552                 return -1;
553
554         switch (intr_handle->type){
555         /* write to the uio fd to disable the interrupt */
556         case RTE_INTR_HANDLE_UIO:
557                 if (write(intr_handle->fd, &value, sizeof(value)) < 0){
558                         RTE_LOG(ERR, EAL,
559                                 "Error disabling interrupts for fd %d\n",
560                                                         intr_handle->fd);
561                         return -1;
562                 }
563                 break;
564         /* not used at this moment */
565         case RTE_INTR_HANDLE_ALARM:
566                 return -1;
567 #ifdef VFIO_PRESENT
568         case RTE_INTR_HANDLE_VFIO_MSIX:
569                 if (vfio_disable_msix(intr_handle))
570                         return -1;
571                 break;
572         case RTE_INTR_HANDLE_VFIO_MSI:
573                 if (vfio_disable_msi(intr_handle))
574                         return -1;
575                 break;
576         case RTE_INTR_HANDLE_VFIO_LEGACY:
577                 if (vfio_disable_intx(intr_handle))
578                         return -1;
579                 break;
580 #endif
581         /* unknown handle type */
582         default:
583                 RTE_LOG(ERR, EAL,
584                         "Unknown handle type of fd %d\n",
585                                         intr_handle->fd);
586                 return -1;
587         }
588
589         return 0;
590 }
591
592 static int
593 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
594 {
595         int n, bytes_read;
596         struct rte_intr_source *src;
597         struct rte_intr_callback *cb;
598         union rte_intr_read_buffer buf;
599         struct rte_intr_callback active_cb;
600
601         for (n = 0; n < nfds; n++) {
602
603                 /**
604                  * if the pipe fd is ready to read, return out to
605                  * rebuild the wait list.
606                  */
607                 if (events[n].data.fd == intr_pipe.readfd){
608                         int r = read(intr_pipe.readfd, buf.charbuf,
609                                         sizeof(buf.charbuf));
610                         RTE_SET_USED(r);
611                         return -1;
612                 }
613                 rte_spinlock_lock(&intr_lock);
614                 TAILQ_FOREACH(src, &intr_sources, next)
615                         if (src->intr_handle.fd ==
616                                         events[n].data.fd)
617                                 break;
618                 if (src == NULL){
619                         rte_spinlock_unlock(&intr_lock);
620                         continue;
621                 }
622
623                 /* mark this interrupt source as active and release the lock. */
624                 src->active = 1;
625                 rte_spinlock_unlock(&intr_lock);
626
627                 /* set the length to be read dor different handle type */
628                 switch (src->intr_handle.type) {
629                 case RTE_INTR_HANDLE_UIO:
630                         bytes_read = sizeof(buf.uio_intr_count);
631                         break;
632                 case RTE_INTR_HANDLE_ALARM:
633                         bytes_read = sizeof(buf.timerfd_num);
634                         break;
635 #ifdef VFIO_PRESENT
636                 case RTE_INTR_HANDLE_VFIO_MSIX:
637                 case RTE_INTR_HANDLE_VFIO_MSI:
638                 case RTE_INTR_HANDLE_VFIO_LEGACY:
639                         bytes_read = sizeof(buf.vfio_intr_count);
640                         break;
641 #endif
642                 default:
643                         bytes_read = 1;
644                         break;
645                 }
646
647                 /**
648                  * read out to clear the ready-to-be-read flag
649                  * for epoll_wait.
650                  */
651                 bytes_read = read(events[n].data.fd, &buf, bytes_read);
652
653                 if (bytes_read < 0)
654                         RTE_LOG(ERR, EAL, "Error reading from file "
655                                 "descriptor %d: %s\n", events[n].data.fd,
656                                                         strerror(errno));
657                 else if (bytes_read == 0)
658                         RTE_LOG(ERR, EAL, "Read nothing from file "
659                                 "descriptor %d\n", events[n].data.fd);
660
661                 /* grab a lock, again to call callbacks and update status. */
662                 rte_spinlock_lock(&intr_lock);
663
664                 if (bytes_read > 0) {
665
666                         /* Finally, call all callbacks. */
667                         TAILQ_FOREACH(cb, &src->callbacks, next) {
668
669                                 /* make a copy and unlock. */
670                                 active_cb = *cb;
671                                 rte_spinlock_unlock(&intr_lock);
672
673                                 /* call the actual callback */
674                                 active_cb.cb_fn(&src->intr_handle,
675                                         active_cb.cb_arg);
676
677                                 /*get the lock back. */
678                                 rte_spinlock_lock(&intr_lock);
679                         }
680                 }
681
682                 /* we done with that interrupt source, release it. */
683                 src->active = 0;
684                 rte_spinlock_unlock(&intr_lock);
685         }
686
687         return 0;
688 }
689
690 /**
691  * It handles all the interrupts.
692  *
693  * @param pfd
694  *  epoll file descriptor.
695  * @param totalfds
696  *  The number of file descriptors added in epoll.
697  *
698  * @return
699  *  void
700  */
701 static void
702 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
703 {
704         struct epoll_event events[totalfds];
705         int nfds = 0;
706
707         for(;;) {
708                 nfds = epoll_wait(pfd, events, totalfds,
709                         EAL_INTR_EPOLL_WAIT_FOREVER);
710                 /* epoll_wait fail */
711                 if (nfds < 0) {
712                         if (errno == EINTR)
713                                 continue;
714                         RTE_LOG(ERR, EAL,
715                                 "epoll_wait returns with fail\n");
716                         return;
717                 }
718                 /* epoll_wait timeout, will never happens here */
719                 else if (nfds == 0)
720                         continue;
721                 /* epoll_wait has at least one fd ready to read */
722                 if (eal_intr_process_interrupts(events, nfds) < 0)
723                         return;
724         }
725 }
726
727 /**
728  * It builds/rebuilds up the epoll file descriptor with all the
729  * file descriptors being waited on. Then handles the interrupts.
730  *
731  * @param arg
732  *  pointer. (unused)
733  *
734  * @return
735  *  never return;
736  */
737 static __attribute__((noreturn)) void *
738 eal_intr_thread_main(__rte_unused void *arg)
739 {
740         struct epoll_event ev;
741
742         /* host thread, never break out */
743         for (;;) {
744                 /* build up the epoll fd with all descriptors we are to
745                  * wait on then pass it to the handle_interrupts function
746                  */
747                 static struct epoll_event pipe_event = {
748                         .events = EPOLLIN | EPOLLPRI,
749                 };
750                 struct rte_intr_source *src;
751                 unsigned numfds = 0;
752
753                 /* create epoll fd */
754                 int pfd = epoll_create(1);
755                 if (pfd < 0)
756                         rte_panic("Cannot create epoll instance\n");
757
758                 pipe_event.data.fd = intr_pipe.readfd;
759                 /**
760                  * add pipe fd into wait list, this pipe is used to
761                  * rebuild the wait list.
762                  */
763                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
764                                                 &pipe_event) < 0) {
765                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
766                                         intr_pipe.readfd, strerror(errno));
767                 }
768                 numfds++;
769
770                 rte_spinlock_lock(&intr_lock);
771
772                 TAILQ_FOREACH(src, &intr_sources, next) {
773                         if (src->callbacks.tqh_first == NULL)
774                                 continue; /* skip those with no callbacks */
775                         ev.events = EPOLLIN | EPOLLPRI;
776                         ev.data.fd = src->intr_handle.fd;
777
778                         /**
779                          * add all the uio device file descriptor
780                          * into wait list.
781                          */
782                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
783                                         src->intr_handle.fd, &ev) < 0){
784                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
785                                         src->intr_handle.fd, strerror(errno));
786                         }
787                         else
788                                 numfds++;
789                 }
790                 rte_spinlock_unlock(&intr_lock);
791                 /* serve the interrupt */
792                 eal_intr_handle_interrupts(pfd, numfds);
793
794                 /**
795                  * when we return, we need to rebuild the
796                  * list of fds to monitor.
797                  */
798                 close(pfd);
799         }
800 }
801
802 int
803 rte_eal_intr_init(void)
804 {
805         int ret = 0;
806
807         /* init the global interrupt source head */
808         TAILQ_INIT(&intr_sources);
809
810         /**
811          * create a pipe which will be waited by epoll and notified to
812          * rebuild the wait list of epoll.
813          */
814         if (pipe(intr_pipe.pipefd) < 0)
815                 return -1;
816
817         /* create the host thread to wait/handle the interrupt */
818         ret = pthread_create(&intr_thread, NULL,
819                         eal_intr_thread_main, NULL);
820         if (ret != 0)
821                 RTE_LOG(ERR, EAL,
822                         "Failed to create thread for interrupt handling\n");
823
824         return -ret;
825 }
826