1 /* 2 * Copyright (c) 2006 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 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 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. 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 * 3. Neither the name of The DragonFly Project 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 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/types.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/systimer.h> 39 #include <sys/sysctl.h> 40 #include <sys/signal.h> 41 #include <sys/interrupt.h> 42 #include <sys/bus.h> 43 #include <sys/time.h> 44 #include <sys/event.h> 45 #include <machine/cpu.h> 46 #include <machine/globaldata.h> 47 #include <machine/md_var.h> 48 49 #include <unistd.h> 50 #include <signal.h> 51 #include <stdlib.h> 52 #include <fcntl.h> 53 54 struct kqueue_info { 55 void (*func)(void *, struct intrframe *); 56 void *data; 57 int fd; 58 }; 59 60 static int KQueueFd = -1; 61 62 /* 63 * Initialize kqueue based I/O 64 * 65 * Use SIGIO to get an immediate event when the kqueue has something waiting 66 * for us. Setup the SIGIO signal as a mailbox signal for efficiency. 67 * 68 * Currently only read events are supported. 69 */ 70 void 71 init_kqueue(void) 72 { 73 #if 0 74 struct sigaction sa; 75 bzero(&sa, sizeof(sa)); 76 /*sa.sa_mailbox = &mdcpu->gd_mailbox;*/ 77 sa.sa_flags = 0; 78 sa.sa_handler = kqueuesig; 79 sigemptyset(&sa.sa_mask); 80 sigaction(SIGIO, &sa, NULL); 81 #endif 82 KQueueFd = kqueue(); 83 if (fcntl(KQueueFd, F_SETOWN, getpid()) < 0) 84 panic("Cannot configure kqueue for SIGIO, update your kernel"); 85 if (fcntl(KQueueFd, F_SETFL, O_ASYNC) < 0) 86 panic("Cannot configure kqueue for SIGIO, update your kernel"); 87 } 88 89 #if 0 90 /* 91 * Signal handler dispatches interrupt thread. Use interrupt #1 92 */ 93 static void 94 kqueuesig(int signo) 95 { 96 signalintr(1); 97 } 98 99 #endif 100 101 /* 102 * Generic I/O event support 103 */ 104 struct kqueue_info * 105 kqueue_add(int fd, void (*func)(void *, struct intrframe *), void *data) 106 { 107 struct timespec ts = { 0, 0 }; 108 struct kqueue_info *info; 109 struct kevent kev; 110 111 info = kmalloc(sizeof(*info), M_DEVBUF, M_ZERO|M_INTWAIT); 112 info->func = func; 113 info->data = data; 114 info->fd = fd; 115 EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE|EV_CLEAR, 0, 0, info); 116 if (kevent(KQueueFd, &kev, 1, NULL, 0, &ts) < 0) 117 panic("kqueue: kevent() call could not add descriptor"); 118 return(info); 119 } 120 121 #if 0 122 /* 123 * Medium resolution timer support 124 */ 125 struct kqueue_info * 126 kqueue_add_timer(void (*func)(void *, struct intrframe *), void *data) 127 { 128 struct kqueue_info *info; 129 130 info = kmalloc(sizeof(*info), M_DEVBUF, M_ZERO|M_INTWAIT); 131 info->func = func; 132 info->data = data; 133 info->fd = (uintptr_t)info; 134 135 return(info); 136 } 137 138 void 139 kqueue_reload_timer(struct kqueue_info *info, int ms) 140 { 141 struct timespec ts = { 0, 0 }; 142 struct kevent kev; 143 144 KKASSERT(ms > 0); 145 146 EV_SET(&kev, info->fd, EVFILT_TIMER, 147 EV_ADD|EV_ENABLE|EV_ONESHOT|EV_CLEAR, 0, (uintptr_t)ms, info); 148 if (kevent(KQueueFd, &kev, 1, NULL, 0, &ts) < 0) 149 panic("kqueue_reload_timer: Failed"); 150 } 151 #endif 152 153 /* 154 * Destroy a previously added kqueue event 155 */ 156 void 157 kqueue_del(struct kqueue_info *info) 158 { 159 struct timespec ts = { 0, 0 }; 160 struct kevent kev; 161 162 KKASSERT(info->fd >= 0); 163 EV_SET(&kev, info->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); 164 if (kevent(KQueueFd, &kev, 1, NULL, 0, &ts) < 0) 165 panic("kevent: failed to delete descriptor %d", info->fd); 166 info->fd = -1; 167 kfree(info, M_DEVBUF); 168 } 169 170 /* 171 * Safely called via DragonFly's normal interrupt handling mechanism. 172 * 173 * Calleld with the MP lock held. Note that this is still an interrupt 174 * thread context. 175 */ 176 void 177 kqueue_intr(struct intrframe *frame) 178 { 179 struct timespec ts; 180 struct kevent kevary[8]; 181 int n; 182 int i; 183 184 ts.tv_sec = 0; 185 ts.tv_nsec = 0; 186 do { 187 n = kevent(KQueueFd, NULL, 0, kevary, 8, &ts); 188 for (i = 0; i < n; ++i) { 189 struct kevent *kev = &kevary[i]; 190 struct kqueue_info *info = (void *)kev->udata; 191 192 info->func(info->data, frame); 193 } 194 } while (n == 8); 195 } 196