4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <sys/queue.h>
46 #include <sys/epoll.h>
47 #include <sys/signalfd.h>
49 #include <rte_common.h>
50 #include <rte_interrupts.h>
51 #include <rte_memory.h>
52 #include <rte_memzone.h>
53 #include <rte_launch.h>
54 #include <rte_tailq.h>
56 #include <rte_per_lcore.h>
57 #include <rte_lcore.h>
58 #include <rte_atomic.h>
59 #include <rte_branch_prediction.h>
61 #include <rte_debug.h>
63 #include <rte_mempool.h>
65 #include <rte_malloc.h>
66 #include <rte_errno.h>
67 #include <rte_spinlock.h>
69 #include "eal_private.h"
71 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
87 * union buffer for reading on different devices
89 union rte_intr_read_buffer {
90 int uio_intr_count; /* for uio device */
91 uint64_t timerfd_num; /* for timerfd */
92 char charbuf[16]; /* for others */
95 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
96 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
98 struct rte_intr_callback {
99 TAILQ_ENTRY(rte_intr_callback) next;
100 rte_intr_callback_fn cb_fn; /**< callback address */
101 void *cb_arg; /**< parameter for callback */
104 struct rte_intr_source {
105 TAILQ_ENTRY(rte_intr_source) next;
106 struct rte_intr_handle intr_handle; /**< interrupt handle */
107 struct rte_intr_cb_list callbacks; /**< user callbacks */
111 /* global spinlock for interrupt data operation */
112 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
114 /* union buffer for pipe read/write */
115 static union intr_pipefds intr_pipe;
117 /* interrupt sources list */
118 static struct rte_intr_source_list intr_sources;
120 /* interrupt handling thread */
121 static pthread_t intr_thread;
124 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
125 rte_intr_callback_fn cb, void *cb_arg)
127 int ret, wake_thread;
128 struct rte_intr_source *src;
129 struct rte_intr_callback *callback;
133 /* first do parameter checking */
134 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
136 "Registering with invalid input parameter\n");
140 /* allocate a new interrupt callback entity */
141 callback = rte_zmalloc("interrupt callback list",
142 sizeof(*callback), 0);
143 if (callback == NULL) {
144 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
147 callback->cb_fn = cb;
148 callback->cb_arg = cb_arg;
150 rte_spinlock_lock(&intr_lock);
152 /* check if there is at least one callback registered for the fd */
153 TAILQ_FOREACH(src, &intr_sources, next) {
154 if (src->intr_handle.fd == intr_handle->fd) {
155 /* we had no interrupts for this */
156 if TAILQ_EMPTY(&src->callbacks)
159 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
165 /* no existing callbacks for this - add new source */
167 if ((src = rte_zmalloc("interrupt source list",
168 sizeof(*src), 0)) == NULL) {
169 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
173 src->intr_handle = *intr_handle;
174 TAILQ_INIT(&src->callbacks);
175 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
176 TAILQ_INSERT_TAIL(&intr_sources, src, next);
182 rte_spinlock_unlock(&intr_lock);
185 * check if need to notify the pipe fd waited by epoll_wait to
186 * rebuild the wait list.
189 if (write(intr_pipe.writefd, "1", 1) < 0)
196 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
197 rte_intr_callback_fn cb_fn, void *cb_arg)
200 struct rte_intr_source *src;
201 struct rte_intr_callback *cb, *next;
203 /* do parameter checking first */
204 if (intr_handle == NULL || intr_handle->fd < 0) {
206 "Unregistering with invalid input parameter\n");
210 rte_spinlock_lock(&intr_lock);
212 /* check if the insterrupt source for the fd is existent */
213 TAILQ_FOREACH(src, &intr_sources, next)
214 if (src->intr_handle.fd == intr_handle->fd)
217 /* No interrupt source registered for the fd */
221 /* interrupt source has some active callbacks right now. */
222 } else if (src->active != 0) {
229 /*walk through the callbacks and remove all that match. */
230 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
232 next = TAILQ_NEXT(cb, next);
234 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
235 cb->cb_arg == cb_arg)) {
236 TAILQ_REMOVE(&src->callbacks, cb, next);
242 /* all callbacks for that source are removed. */
243 if (TAILQ_EMPTY(&src->callbacks)) {
244 TAILQ_REMOVE(&intr_sources, src, next);
249 rte_spinlock_unlock(&intr_lock);
251 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
252 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
260 rte_intr_enable(struct rte_intr_handle *intr_handle)
264 if (!intr_handle || intr_handle->fd < 0)
267 switch (intr_handle->type){
268 /* write to the uio fd to enable the interrupt */
269 case RTE_INTR_HANDLE_UIO:
270 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
272 "Error enabling interrupts for fd %d\n",
277 /* not used at this moment */
278 case RTE_INTR_HANDLE_ALARM:
280 /* unkown handle type */
283 "Unknown handle type of fd %d\n",
292 rte_intr_disable(struct rte_intr_handle *intr_handle)
296 if (!intr_handle || intr_handle->fd < 0)
299 switch (intr_handle->type){
300 /* write to the uio fd to disable the interrupt */
301 case RTE_INTR_HANDLE_UIO:
302 if (write(intr_handle->fd, &value, sizeof(value)) < 0){
304 "Error enabling interrupts for fd %d\n",
309 /* not used at this moment */
310 case RTE_INTR_HANDLE_ALARM:
312 /* unkown handle type */
315 "Unknown handle type of fd %d\n",
324 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
327 struct rte_intr_source *src;
328 struct rte_intr_callback *cb;
329 union rte_intr_read_buffer buf;
330 struct rte_intr_callback active_cb;
332 for (n = 0; n < nfds; n++) {
335 * if the pipe fd is ready to read, return out to
336 * rebuild the wait list.
338 if (events[n].data.fd == intr_pipe.readfd){
339 int r = read(intr_pipe.readfd, buf.charbuf,
340 sizeof(buf.charbuf));
344 rte_spinlock_lock(&intr_lock);
345 TAILQ_FOREACH(src, &intr_sources, next)
346 if (src->intr_handle.fd ==
350 rte_spinlock_unlock(&intr_lock);
354 /* mark this interrupt source as active and release the lock. */
356 rte_spinlock_unlock(&intr_lock);
358 /* set the length to be read dor different handle type */
359 switch (src->intr_handle.type) {
360 case RTE_INTR_HANDLE_UIO:
363 case RTE_INTR_HANDLE_ALARM:
364 bytes_read = sizeof(uint64_t);
372 * read out to clear the ready-to-be-read flag
375 bytes_read = read(events[n].data.fd, &buf, bytes_read);
377 if (bytes_read < 0) {
378 RTE_LOG(ERR, EAL, "Error reading from file descriptor"
379 " %d, error: %d\n", events[n].data.fd, errno);
381 else if (bytes_read == 0) {
383 "Read nothing from file descriptor %d.\n",
387 /* grab a lock, again to call callbacks and update status. */
388 rte_spinlock_lock(&intr_lock);
390 if (bytes_read > 0) {
392 /* Finally, call all callbacks. */
393 TAILQ_FOREACH(cb, &src->callbacks, next) {
395 /* make a copy and unlock. */
397 rte_spinlock_unlock(&intr_lock);
399 /* call the actual callback */
400 active_cb.cb_fn(&src->intr_handle,
403 /*get the lcok back. */
404 rte_spinlock_lock(&intr_lock);
408 /* we done with that interrupt source, release it. */
410 rte_spinlock_unlock(&intr_lock);
417 * It handles all the interrupts.
420 * epoll file descriptor.
422 * The number of file descriptors added in epoll.
428 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
430 struct epoll_event events[totalfds];
434 nfds = epoll_wait(pfd, events, totalfds,
435 EAL_INTR_EPOLL_WAIT_FOREVER);
436 /* epoll_wait fail */
441 "epoll_wait returns with fail\n");
444 /* epoll_wait timeout, will never happens here */
447 /* epoll_wait has at least one fd ready to read */
448 if (eal_intr_process_interrupts(events, nfds) < 0)
454 * It builds/rebuilds up the epoll file descriptor with all the
455 * file descriptors being waited on. Then handles the interrupts.
463 static __attribute__((noreturn)) void *
464 eal_intr_thread_main(__rte_unused void *arg)
466 struct epoll_event ev;
468 /* host thread, never break out */
470 /* build up the epoll fd with all descriptors we are to
471 * wait on then pass it to the handle_interrupts function
473 static struct epoll_event pipe_event = {
474 .events = EPOLLIN | EPOLLPRI,
476 struct rte_intr_source *src;
479 /* create epoll fd */
480 int pfd = epoll_create(1);
482 rte_panic("Cannot create epoll instance\n");
484 pipe_event.data.fd = intr_pipe.readfd;
486 * add pipe fd into wait list, this pipe is used to
487 * rebuild the wait list.
489 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
491 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
492 intr_pipe.readfd, strerror(errno));
496 rte_spinlock_lock(&intr_lock);
498 TAILQ_FOREACH(src, &intr_sources, next) {
499 if (src->callbacks.tqh_first == NULL)
500 continue; /* skip those with no callbacks */
501 ev.events = EPOLLIN | EPOLLPRI;
502 ev.data.fd = src->intr_handle.fd;
505 * add all the uio device file descriptor
508 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
509 src->intr_handle.fd, &ev) < 0){
510 rte_panic("Error adding fd %d epoll_ctl, %s\n",
511 src->intr_handle.fd, strerror(errno));
516 rte_spinlock_unlock(&intr_lock);
517 /* serve the interrupt */
518 eal_intr_handle_interrupts(pfd, numfds);
521 * when we return, we need to rebuild the
522 * list of fds to monitor.
529 rte_eal_intr_init(void)
533 /* init the global interrupt source head */
534 TAILQ_INIT(&intr_sources);
537 * create a pipe which will be waited by epoll and notified to
538 * rebuild the wait list of epoll.
540 if (pipe(intr_pipe.pipefd) < 0)
543 /* create the host thread to wait/handle the interrupt */
544 ret = pthread_create(&intr_thread, NULL,
545 eal_intr_thread_main, NULL);
548 "Failed to create thread for interrupt handling\n");