VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
97870c34b4532 (Alex Dewar    2019-08-25 10:49:18 +0100   1) // SPDX-License-Identifier: GPL-2.0
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800   2) /*
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000   3)  * Copyright (C) 2017 - Cambridge Greys Ltd
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000   4)  * Copyright (C) 2011 - 2014 Cisco Systems Inc
fee64d3c153f1 (Jeff Dike     2008-02-04 22:31:02 -0800   5)  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800   6)  */
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800   7) 
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800   8) #include <stdlib.h>
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800   9) #include <errno.h>
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  10) #include <sys/epoll.h>
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  11) #include <signal.h>
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  12) #include <string.h>
37185b3324087 (Al Viro       2012-10-08 03:27:32 +0100  13) #include <irq_user.h>
37185b3324087 (Al Viro       2012-10-08 03:27:32 +0100  14) #include <os.h>
37185b3324087 (Al Viro       2012-10-08 03:27:32 +0100  15) #include <um_malloc.h>
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  16) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  17) /* Epoll support */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  18) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  19) static int epollfd = -1;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  20) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  21) #define MAX_EPOLL_EVENTS 64
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  22) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  23) static struct epoll_event epoll_events[MAX_EPOLL_EVENTS];
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  24) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  25) /* Helper to return an Epoll data pointer from an epoll event structure.
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  26)  * We need to keep this one on the userspace side to keep includes separate
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  27)  */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  28) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  29) void *os_epoll_get_data_pointer(int index)
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  30) {
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  31) 	return epoll_events[index].data.ptr;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  32) }
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  33) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  34) /* Helper to compare events versus the events in the epoll structure.
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  35)  * Same as above - needs to be on the userspace side
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  36)  */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  37) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  38) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  39) int os_epoll_triggered(int index, int events)
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  40) {
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  41) 	return epoll_events[index].events & events;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  42) }
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  43) /* Helper to set the event mask.
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  44)  * The event mask is opaque to the kernel side, because it does not have
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  45)  * access to the right includes/defines for EPOLL constants.
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  46)  */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  47) 
2fccfcc0c7426 (Johannes Berg 2020-12-02 12:59:55 +0100  48) int os_event_mask(enum um_irq_type irq_type)
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  49) {
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  50) 	if (irq_type == IRQ_READ)
e3a01cbee9c5f (Anton Ivanov  2020-12-07 17:19:38 +0000  51) 		return EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP | EPOLLRDHUP;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  52) 	if (irq_type == IRQ_WRITE)
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  53) 		return EPOLLOUT;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  54) 	return 0;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  55) }
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  56) 
f2e62992a2325 (Jeff Dike     2007-02-10 01:44:23 -0800  57) /*
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  58)  * Initial Epoll Setup
f2e62992a2325 (Jeff Dike     2007-02-10 01:44:23 -0800  59)  */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  60) int os_setup_epoll(void)
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  61) {
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  62) 	epollfd = epoll_create(MAX_EPOLL_EVENTS);
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  63) 	return epollfd;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  64) }
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  65) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  66) /*
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  67)  * Helper to run the actual epoll_wait
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  68)  */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  69) int os_waiting_for_events_epoll(void)
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  70) {
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  71) 	int n, err;
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  72) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  73) 	n = epoll_wait(epollfd,
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  74) 		(struct epoll_event *) &epoll_events, MAX_EPOLL_EVENTS, 0);
191ef966ac164 (Jesper Juhl   2006-05-01 12:15:57 -0700  75) 	if (n < 0) {
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  76) 		err = -errno;
191ef966ac164 (Jesper Juhl   2006-05-01 12:15:57 -0700  77) 		if (errno != EINTR)
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  78) 			printk(
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  79) 				UM_KERN_ERR "os_waiting_for_events:"
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  80) 				" epoll returned %d, error = %s\n", n,
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  81) 				strerror(errno)
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  82) 			);
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  83) 		return err;
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  84) 	}
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  85) 	return n;
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  86) }
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  87) 
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  88) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  89) /*
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  90)  * Helper to add a fd to epoll
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  91)  */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  92) int os_add_epoll_fd(int events, int fd, void *data)
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800  93) {
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  94) 	struct epoll_event event;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  95) 	int result;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  96) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  97) 	event.data.ptr = data;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  98) 	event.events = events | EPOLLET;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000  99) 	result = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 100) 	if ((result) && (errno == EEXIST))
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 101) 		result = os_mod_epoll_fd(events, fd, data);
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 102) 	if (result)
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 103) 		printk("epollctl add err fd %d, %s\n", fd, strerror(errno));
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 104) 	return result;
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 105) }
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 106) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 107) /*
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 108)  * Helper to mod the fd event mask and/or data backreference
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 109)  */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 110) int os_mod_epoll_fd(int events, int fd, void *data)
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 111) {
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 112) 	struct epoll_event event;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 113) 	int result;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 114) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 115) 	event.data.ptr = data;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 116) 	event.events = events;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 117) 	result = epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &event);
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 118) 	if (result)
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 119) 		printk(UM_KERN_ERR
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 120) 			"epollctl mod err fd %d, %s\n", fd, strerror(errno));
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 121) 	return result;
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 122) }
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 123) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 124) /*
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 125)  * Helper to delete the epoll fd
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 126)  */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 127) int os_del_epoll_fd(int fd)
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 128) {
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 129) 	struct epoll_event event;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 130) 	int result;
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 131) 	/* This is quiet as we use this as IO ON/OFF - so it is often
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 132) 	 * invoked on a non-existent fd
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 133) 	 */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 134) 	result = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &event);
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 135) 	return result;
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 136) }
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 137) 
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 138) void os_set_ioignore(void)
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 139) {
4b84c69b5f6c0 (Jeff Dike     2006-09-25 23:33:04 -0700 140) 	signal(SIGIO, SIG_IGN);
63ae2a94d98dd (Jeff Dike     2006-03-27 01:14:30 -0800 141) }
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 142) 
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 143) void os_close_epoll_fd(void)
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 144) {
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 145) 	/* Needed so we do not leak an fd when rebooting */
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 146) 	os_close_file(epollfd);
ff6a17989c08b (Anton Ivanov  2017-11-20 21:17:58 +0000 147) }