xref: /netbsd-src/sys/rump/dev/lib/libpci/rumpdev_bus_space.c (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1 /*	$NetBSD: rumpdev_bus_space.c,v 1.3 2014/08/22 14:28:58 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/atomic.h>
30 
31 #include <sys/param.h>
32 
33 #include <dev/pci/pcivar.h>
34 
35 #include "pci_user.h"
36 
37 #if defined(RUMP_PCI_IOSPACE) && (defined(__i386__) || defined(__x86_64__))
38 #define IOSPACE_SUPPORTED
39 #endif
40 
41 int
42 bus_space_map(bus_space_tag_t bst, bus_addr_t address, bus_size_t size,
43 	int flags, bus_space_handle_t *handlep)
44 {
45 	int rv;
46 
47 	/*
48 	 * I/O space we just "let it bli" in case someone wants to
49 	 * map it (e.g. on Xen)
50  	 *
51 	 * Memory space needs to be mapped into our guest, so we
52 	 * make a hypercall to request it.
53 	 */
54 	if (bst == 0) {
55 #ifdef IOSPACE_SUPPORTED
56 		*handlep = address;
57 		rv = 0;
58 #else
59 		rv = ENOTSUP;
60 #endif
61 	} else {
62 		*handlep = (bus_space_handle_t)rumpcomp_pci_map(address, size);
63 		rv = *handlep ? 0 : EINVAL;
64 	}
65 
66 	return rv;
67 }
68 
69 uint8_t
70 bus_space_read_1(bus_space_tag_t bst, bus_space_handle_t bsh,
71 	bus_size_t offset)
72 {
73 	uint8_t rv;
74 
75 	if (bst == 0) {
76 #ifdef IOSPACE_SUPPORTED
77 		unsigned short addr = bsh + offset;
78 		__asm__ __volatile__("inb %1, %0" : "=a"(rv) : "d"(addr));
79 #else
80 		panic("IO space not supported");
81 #endif
82 	} else {
83 		rv = *(volatile uint8_t *)(bsh + offset);
84 	}
85 
86 	return rv;
87 }
88 
89 uint16_t
90 bus_space_read_2(bus_space_tag_t bst, bus_space_handle_t bsh,
91 	bus_size_t offset)
92 {
93 	uint16_t rv;
94 
95 	if (bst == 0) {
96 #ifdef IOSPACE_SUPPORTED
97 		unsigned short addr = bsh + offset;
98 		__asm__ __volatile__("in %1, %0" : "=a"(rv) : "d"(addr));
99 #else
100 		panic("IO space not supported");
101 #endif
102 	} else {
103 		rv = *(volatile uint16_t *)(bsh + offset);
104 	}
105 
106 	return rv;
107 }
108 
109 uint32_t
110 bus_space_read_4(bus_space_tag_t bst, bus_space_handle_t bsh,
111 	bus_size_t offset)
112 {
113 	uint32_t rv;
114 
115 	if (bst == 0) {
116 #ifdef IOSPACE_SUPPORTED
117 		unsigned short addr = bsh + offset;
118 		__asm__ __volatile__("inl %1, %0" : "=a"(rv) : "d"(addr));
119 #else
120 		panic("IO space not supported");
121 #endif
122 	} else {
123 		rv = *(volatile uint32_t *)(bsh + offset);
124 	}
125 
126 	return rv;
127 }
128 
129 void
130 bus_space_write_1(bus_space_tag_t bst, bus_space_handle_t bsh,
131 	bus_size_t offset, uint8_t v)
132 {
133 
134 	if (bst == 0) {
135 #ifdef IOSPACE_SUPPORTED
136 		unsigned short addr = bsh + offset;
137 		__asm__ __volatile__("outb %0, %1" :: "a"(v), "d"(addr));
138 #else
139 		panic("IO space not supported");
140 #endif
141 	} else {
142 		*(volatile uint8_t *)(bsh + offset) = v;
143 	}
144 }
145 
146 void
147 bus_space_write_2(bus_space_tag_t bst, bus_space_handle_t bsh,
148 	bus_size_t offset, uint16_t v)
149 {
150 
151 	if (bst == 0) {
152 #ifdef IOSPACE_SUPPORTED
153 		unsigned short addr = bsh + offset;
154 		__asm__ __volatile__("out %0, %1" :: "a"(v), "d"(addr));
155 #else
156 		panic("IO space not supported");
157 #endif
158 	} else {
159 		*(volatile uint16_t *)(bsh + offset) = v;
160 	}
161 }
162 
163 void
164 bus_space_write_4(bus_space_tag_t bst, bus_space_handle_t bsh,
165 	bus_size_t offset, uint32_t v)
166 {
167 
168 	if (bst == 0) {
169 #ifdef IOSPACE_SUPPORTED
170 		unsigned short addr = bsh + offset;
171 		__asm__ __volatile__("outl %0, %1" :: "a"(v), "d"(addr));
172 #else
173 		panic("IO space not supported");
174 #endif
175 	} else {
176 		*(volatile uint32_t *)(bsh + offset) = v;
177 	}
178 }
179 
180 paddr_t
181 bus_space_mmap(bus_space_tag_t bst, bus_addr_t addr, off_t off,
182 	int prot, int flags)
183 {
184 
185 	panic("%s: unimplemented", __func__);
186 }
187 
188 int
189 bus_space_subregion(bus_space_tag_t bst, bus_space_handle_t bsh,
190 	bus_size_t offset, bus_size_t size, bus_space_handle_t *nhandlep)
191 {
192 
193 	panic("%s: unimplemented", __func__);
194 }
195 
196 void
197 bus_space_unmap(bus_space_tag_t bst, bus_space_handle_t bsh,
198 	bus_size_t size)
199 {
200 
201 	panic("%s: unimplemented", __func__);
202 }
203 
204 void
205 bus_space_barrier(bus_space_tag_t bst, bus_space_handle_t bsh,
206 	bus_size_t offset, bus_size_t len, int flags)
207 {
208 
209 	/* weelll ... */
210 	membar_sync();
211 }
212