1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <stdio.h>
30 #include <string.h>
31 
32 #include "inout.h"
33 #include "log.h"
34 SET_DECLARE(inout_port_set, struct inout_port);
35 
36 #define	MAX_IOPORTS	(1 << 16)
37 
38 #define	VERIFY_IOPORT(port, size) \
39 	((port) >= 0 && (size) > 0 && ((port) + (size)) <= MAX_IOPORTS)
40 
41 static struct {
42 	const char	*name;
43 	int		flags;
44 	inout_func_t	handler;
45 	void		*arg;
46 } inout_handlers[MAX_IOPORTS];
47 
48 static int
default_inout(struct vmctx * ctx,int vcpu,int in,int port,int bytes,uint32_t * eax,void * arg)49 default_inout(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
50 	      uint32_t *eax, void *arg)
51 {
52 	if (in) {
53 		switch (bytes) {
54 		case 4:
55 			*eax = 0xffffffff;
56 			break;
57 		case 2:
58 			*eax = 0xffff;
59 			break;
60 		case 1:
61 			*eax = 0xff;
62 			break;
63 		}
64 	}
65 
66 	return 0;
67 }
68 
69 static void
register_default_iohandler(int start,int size)70 register_default_iohandler(int start, int size)
71 {
72 	struct inout_port iop;
73 
74 	if (!VERIFY_IOPORT(start, size)) {
75 		pr_err("invalid input: port:0x%x, size:%d", start, size);
76 		return;
77 	}
78 
79 	bzero(&iop, sizeof(iop));
80 	iop.name = "default";
81 	iop.port = start;
82 	iop.size = size;
83 	iop.flags = IOPORT_F_INOUT | IOPORT_F_DEFAULT;
84 	iop.handler = default_inout;
85 
86 	register_inout(&iop);
87 }
88 
89 int
emulate_inout(struct vmctx * ctx,int * pvcpu,struct acrn_pio_request * pio_request)90 emulate_inout(struct vmctx *ctx, int *pvcpu, struct acrn_pio_request *pio_request)
91 {
92 	int bytes, flags, in, port;
93 	inout_func_t handler;
94 	void *arg;
95 	int retval;
96 
97 	bytes = pio_request->size;
98 	in = (pio_request->direction == ACRN_IOREQ_DIR_READ);
99 	port = pio_request->address;
100 
101 	if ((port + bytes - 1 >= MAX_IOPORTS) ||
102 		((bytes != 1) && (bytes != 2) && (bytes != 4)))
103 		return -1;
104 
105 	handler = inout_handlers[port].handler;
106 	flags = inout_handlers[port].flags;
107 	arg = inout_handlers[port].arg;
108 
109 	if (pio_request->direction == ACRN_IOREQ_DIR_READ) {
110 		if (!(flags & IOPORT_F_IN))
111 			return -1;
112 	} else {
113 		if (!(flags & IOPORT_F_OUT))
114 			return -1;
115 	}
116 	retval = handler(ctx, *pvcpu, in, port, bytes,
117 		(uint32_t *)&(pio_request->value), arg);
118 	return retval;
119 }
120 
121 void
init_inout(void)122 init_inout(void)
123 {
124 	struct inout_port **iopp, *iop;
125 
126 	/*
127 	 * Set up the default handler for all ports
128 	 */
129 	register_default_iohandler(0, MAX_IOPORTS);
130 
131 	/*
132 	 * Overwrite with specified handlers
133 	 */
134 	SET_FOREACH(iopp, inout_port_set) {
135 		iop = *iopp;
136 		if (iop->port >= MAX_IOPORTS) {
137 			pr_err("%s: invalid port:0x%x", __func__, iop->port);
138 			continue;
139 		}
140 
141 		inout_handlers[iop->port].name = iop->name;
142 		inout_handlers[iop->port].flags = iop->flags;
143 		inout_handlers[iop->port].handler = iop->handler;
144 		inout_handlers[iop->port].arg = NULL;
145 	}
146 }
147 
148 int
register_inout(struct inout_port * iop)149 register_inout(struct inout_port *iop)
150 {
151 	int i;
152 
153 	if (!VERIFY_IOPORT(iop->port, iop->size)) {
154 		pr_err("invalid input: port:0x%x, size:%d",
155 				iop->port, iop->size);
156 		return -1;
157 	}
158 
159 	/*
160 	 * Verify that the new registration is not overwriting an already
161 	 * allocated i/o range.
162 	 */
163 	if ((iop->flags & IOPORT_F_DEFAULT) == 0) {
164 		for (i = iop->port; i < iop->port + iop->size; i++) {
165 			if ((inout_handlers[i].flags & IOPORT_F_DEFAULT) == 0)
166 				return -1;
167 		}
168 	}
169 
170 	for (i = iop->port; i < iop->port + iop->size; i++) {
171 		inout_handlers[i].name = iop->name;
172 		inout_handlers[i].flags = iop->flags;
173 		inout_handlers[i].handler = iop->handler;
174 		inout_handlers[i].arg = iop->arg;
175 	}
176 
177 	return 0;
178 }
179 
180 int
unregister_inout(struct inout_port * iop)181 unregister_inout(struct inout_port *iop)
182 {
183 
184 	if (!VERIFY_IOPORT(iop->port, iop->size)) {
185 		pr_err("invalid input: port:0x%x, size:%d",
186 				iop->port, iop->size);
187 		return -1;
188 	}
189 
190 	register_default_iohandler(iop->port, iop->size);
191 
192 	return 0;
193 }
194