xref: /netbsd-src/sys/rump/dev/lib/libpci/rumpdev_bus_space.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: rumpdev_bus_space.c,v 1.2 2014/04/13 15:43:26 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 int
38 bus_space_map(bus_space_tag_t bst, bus_addr_t address, bus_size_t size,
39 	int flags, bus_space_handle_t *handlep)
40 {
41 	int rv;
42 
43 	/*
44 	 * I/O space we just "let it bli" in case someone wants to
45 	 * map it (e.g. on Xen)
46  	 *
47 	 * Memory space needs to be mapped into our guest, so we
48 	 * make a hypercall to request it.
49 	 */
50 	if (bst == 0) {
51 		*handlep = address;
52 		rv = 0;
53 	} else {
54 		*handlep = (bus_space_handle_t)rumpcomp_pci_map(address, size);
55 		rv = *handlep ? 0 : EINVAL;
56 	}
57 
58 	return rv;
59 }
60 
61 uint8_t
62 bus_space_read_1(bus_space_tag_t bst, bus_space_handle_t bsh,
63 	bus_size_t offset)
64 {
65 	uint8_t rv;
66 
67 	if (bst == 0) {
68 		panic("8bit IO space not supported");
69 	} else {
70 		rv = *(volatile uint8_t *)(bsh + offset);
71 	}
72 
73 	return rv;
74 }
75 
76 uint16_t
77 bus_space_read_2(bus_space_tag_t bst, bus_space_handle_t bsh,
78 	bus_size_t offset)
79 {
80 	uint16_t rv;
81 
82 	if (bst == 0) {
83 		panic("16bit IO space not supported");
84 	} else {
85 		rv = *(volatile uint16_t *)(bsh + offset);
86 	}
87 
88 	return rv;
89 }
90 
91 uint32_t
92 bus_space_read_4(bus_space_tag_t bst, bus_space_handle_t bsh,
93 	bus_size_t offset)
94 {
95 	uint32_t rv;
96 
97 	if (bst == 0) {
98 #if 1
99 		panic("IO space not supported in this build");
100 #else
101 		unsigned short addr = bsh + offset;
102 		__asm__ __volatile__("inl %1, %0" : "=a"(rv) : "d"(addr));
103 #endif
104 	} else {
105 		rv = *(volatile uint32_t *)(bsh + offset);
106 	}
107 
108 	return rv;
109 }
110 
111 void
112 bus_space_write_1(bus_space_tag_t bst, bus_space_handle_t bsh,
113 	bus_size_t offset, uint8_t v)
114 {
115 
116 	if (bst == 0) {
117 #if 1
118 		panic("IO space not supported in this build");
119 #endif
120 	} else {
121 		*(volatile uint8_t *)(bsh + offset) = v;
122 	}
123 }
124 
125 void
126 bus_space_write_2(bus_space_tag_t bst, bus_space_handle_t bsh,
127 	bus_size_t offset, uint16_t v)
128 {
129 
130 	if (bst == 0) {
131 #if 1
132 		panic("IO space not supported in this build");
133 #endif
134 	} else {
135 		*(volatile uint16_t *)(bsh + offset) = v;
136 	}
137 }
138 
139 void
140 bus_space_write_4(bus_space_tag_t bst, bus_space_handle_t bsh,
141 	bus_size_t offset, uint32_t v)
142 {
143 
144 	if (bst == 0) {
145 #if 1
146 		panic("IO space not supported in this build");
147 #else
148 		unsigned short addr = bsh + offset;
149 		__asm__ __volatile__("outl %0, %1" :: "a"(v), "d"(addr));
150 #endif
151 	} else {
152 		*(volatile uint32_t *)(bsh + offset) = v;
153 	}
154 }
155 
156 paddr_t
157 bus_space_mmap(bus_space_tag_t bst, bus_addr_t addr, off_t off,
158 	int prot, int flags)
159 {
160 
161 	panic("%s: unimplemented", __func__);
162 }
163 
164 int
165 bus_space_subregion(bus_space_tag_t bst, bus_space_handle_t bsh,
166 	bus_size_t offset, bus_size_t size, bus_space_handle_t *nhandlep)
167 {
168 
169 	panic("%s: unimplemented", __func__);
170 }
171 
172 void
173 bus_space_unmap(bus_space_tag_t bst, bus_space_handle_t bsh,
174 	bus_size_t size)
175 {
176 
177 	panic("%s: unimplemented", __func__);
178 }
179 
180 void
181 bus_space_barrier(bus_space_tag_t bst, bus_space_handle_t bsh,
182 	bus_size_t offset, bus_size_t len, int flags)
183 {
184 
185 	/* weelll ... */
186 	membar_sync();
187 }
188