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