Home | History | Annotate | Line # | Download | only in libpci
      1 /*	$NetBSD: rumpdev_bus_space.c,v 1.10 2019/01/27 02:08:48 pgoyette 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 __KERNEL_RCSID(0, "$NetBSD: rumpdev_bus_space.c,v 1.10 2019/01/27 02:08:48 pgoyette Exp $");
     30 
     31 #include <sys/atomic.h>
     32 
     33 #include <sys/param.h>
     34 
     35 #include <dev/pci/pcivar.h>
     36 
     37 #include "pci_user.h"
     38 
     39 #if defined(RUMPCOMP_USERFEATURE_PCI_IOSPACE) \
     40     && (defined(__i386__) || defined(__x86_64__))
     41 #define IOSPACE_SUPPORTED
     42 #endif
     43 
     44 int
     45 bus_space_map(bus_space_tag_t bst, bus_addr_t address, bus_size_t size,
     46 	int flags, bus_space_handle_t *handlep)
     47 {
     48 	int rv;
     49 
     50 	/*
     51 	 * I/O space we just "let it bli" in case someone wants to
     52 	 * map it (e.g. on Xen)
     53  	 *
     54 	 * Memory space needs to be mapped into our guest, so we
     55 	 * make a hypercall to request it.
     56 	 */
     57 	if (bst == 0) {
     58 #ifdef IOSPACE_SUPPORTED
     59 		*handlep = address;
     60 		rv = 0;
     61 #else
     62 		rv = ENOTSUP;
     63 #endif
     64 	} else {
     65 		*handlep = (bus_space_handle_t)rumpcomp_pci_map(address, size);
     66 		rv = *handlep ? 0 : EINVAL;
     67 	}
     68 
     69 	return rv;
     70 }
     71 
     72 uint8_t
     73 bus_space_read_1(bus_space_tag_t bst, bus_space_handle_t bsh,
     74 	bus_size_t offset)
     75 {
     76 	uint8_t rv;
     77 
     78 	if (bst == 0) {
     79 #ifdef IOSPACE_SUPPORTED
     80 		unsigned short addr = bsh + offset;
     81 		__asm__ __volatile__("inb %1, %0" : "=a"(rv) : "d"(addr));
     82 #else
     83 		panic("IO space not supported");
     84 #endif
     85 	} else {
     86 		rv = *(volatile uint8_t *)(bsh + offset);
     87 	}
     88 
     89 	return rv;
     90 }
     91 
     92 uint16_t
     93 bus_space_read_2(bus_space_tag_t bst, bus_space_handle_t bsh,
     94 	bus_size_t offset)
     95 {
     96 	uint16_t rv;
     97 
     98 	if (bst == 0) {
     99 #ifdef IOSPACE_SUPPORTED
    100 		unsigned short addr = bsh + offset;
    101 		__asm__ __volatile__("in %1, %0" : "=a"(rv) : "d"(addr));
    102 #else
    103 		panic("IO space not supported");
    104 #endif
    105 	} else {
    106 		rv = *(volatile uint16_t *)(bsh + offset);
    107 	}
    108 
    109 	return rv;
    110 }
    111 
    112 uint32_t
    113 bus_space_read_4(bus_space_tag_t bst, bus_space_handle_t bsh,
    114 	bus_size_t offset)
    115 {
    116 	uint32_t rv;
    117 
    118 	if (bst == 0) {
    119 #ifdef IOSPACE_SUPPORTED
    120 		unsigned short addr = bsh + offset;
    121 		__asm__ __volatile__("inl %1, %0" : "=a"(rv) : "d"(addr));
    122 #else
    123 		panic("IO space not supported");
    124 #endif
    125 	} else {
    126 		rv = *(volatile uint32_t *)(bsh + offset);
    127 	}
    128 
    129 	return rv;
    130 }
    131 
    132 void
    133 bus_space_read_multi_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    134 	bus_size_t offset, uint8_t *datap, bus_size_t count)
    135 {
    136 
    137 	while (count--) {
    138 		*datap++ = bus_space_read_1(bst, bsh, offset);
    139 		bus_space_barrier(bst, bsh, offset, 1, BUS_SPACE_BARRIER_READ);
    140 	}
    141 }
    142 
    143 void
    144 bus_space_read_multi_2(bus_space_tag_t bst, bus_space_handle_t bsh,
    145 	bus_size_t offset, uint16_t *datap, bus_size_t count)
    146 {
    147 
    148 	while (count--) {
    149 		*datap++ = bus_space_read_2(bst, bsh, offset);
    150 		bus_space_barrier(bst, bsh, offset, 2, BUS_SPACE_BARRIER_READ);
    151 	}
    152 }
    153 
    154 void
    155 bus_space_read_multi_4(bus_space_tag_t bst, bus_space_handle_t bsh,
    156 	bus_size_t offset, uint32_t *datap, bus_size_t count)
    157 {
    158 
    159 	while (count--) {
    160 		*datap++ = bus_space_read_4(bst, bsh, offset);
    161 		bus_space_barrier(bst, bsh, offset, 4, BUS_SPACE_BARRIER_READ);
    162 	}
    163 }
    164 
    165 void
    166 bus_space_write_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    167 	bus_size_t offset, uint8_t v)
    168 {
    169 
    170 	if (bst == 0) {
    171 #ifdef IOSPACE_SUPPORTED
    172 		unsigned short addr = bsh + offset;
    173 		__asm__ __volatile__("outb %0, %1" :: "a"(v), "d"(addr));
    174 #else
    175 		panic("IO space not supported");
    176 #endif
    177 	} else {
    178 		*(volatile uint8_t *)(bsh + offset) = v;
    179 	}
    180 }
    181 
    182 void
    183 bus_space_write_2(bus_space_tag_t bst, bus_space_handle_t bsh,
    184 	bus_size_t offset, uint16_t v)
    185 {
    186 
    187 	if (bst == 0) {
    188 #ifdef IOSPACE_SUPPORTED
    189 		unsigned short addr = bsh + offset;
    190 		__asm__ __volatile__("out %0, %1" :: "a"(v), "d"(addr));
    191 #else
    192 		panic("IO space not supported");
    193 #endif
    194 	} else {
    195 		*(volatile uint16_t *)(bsh + offset) = v;
    196 	}
    197 }
    198 
    199 void
    200 bus_space_write_4(bus_space_tag_t bst, bus_space_handle_t bsh,
    201 	bus_size_t offset, uint32_t v)
    202 {
    203 
    204 	if (bst == 0) {
    205 #ifdef IOSPACE_SUPPORTED
    206 		unsigned short addr = bsh + offset;
    207 		__asm__ __volatile__("outl %0, %1" :: "a"(v), "d"(addr));
    208 #else
    209 		panic("IO space not supported");
    210 #endif
    211 	} else {
    212 		*(volatile uint32_t *)(bsh + offset) = v;
    213 	}
    214 }
    215 
    216 void
    217 bus_space_write_multi_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    218 	bus_size_t offset, const uint8_t *datap, bus_size_t count)
    219 {
    220 
    221 	while (count--) {
    222 		const uint8_t value = *datap++;
    223 
    224 		bus_space_write_1(bst, bsh, offset, value);
    225 		bus_space_barrier(bst, bsh, offset, 1, BUS_SPACE_BARRIER_WRITE);
    226 	}
    227 }
    228 
    229 void
    230 bus_space_write_multi_2(bus_space_tag_t bst, bus_space_handle_t bsh,
    231 	bus_size_t offset, const uint16_t *datap, bus_size_t count)
    232 {
    233 
    234 	while (count--) {
    235 		const uint16_t value = *datap++;
    236 
    237 		bus_space_write_2(bst, bsh, offset, value);
    238 		bus_space_barrier(bst, bsh, offset, 2, BUS_SPACE_BARRIER_WRITE);
    239 	}
    240 }
    241 
    242 void
    243 bus_space_write_multi_4(bus_space_tag_t bst, bus_space_handle_t bsh,
    244 	bus_size_t offset, const uint32_t *datap, bus_size_t count)
    245 {
    246 
    247 	while (count--) {
    248 		const uint32_t value = *datap++;
    249 
    250 		bus_space_write_4(bst, bsh, offset, value);
    251 		bus_space_barrier(bst, bsh, offset, 4, BUS_SPACE_BARRIER_WRITE);
    252 	}
    253 }
    254 
    255 paddr_t
    256 bus_space_mmap(bus_space_tag_t bst, bus_addr_t addr, off_t off,
    257 	int prot, int flags)
    258 {
    259 
    260 	panic("%s: unimplemented", __func__);
    261 }
    262 
    263 int
    264 bus_space_subregion(bus_space_tag_t bst, bus_space_handle_t bsh,
    265 	bus_size_t offset, bus_size_t size, bus_space_handle_t *nhandlep)
    266 {
    267 
    268 	*nhandlep = bsh + offset;
    269 	return 0;
    270 }
    271 
    272 void
    273 bus_space_unmap(bus_space_tag_t bst, bus_space_handle_t bsh,
    274 	bus_size_t size)
    275 {
    276 
    277 	if (bst == 0)
    278 		return;
    279 
    280 	panic("%s: unimplemented", __func__);
    281 }
    282 
    283 void
    284 bus_space_barrier(bus_space_tag_t bst, bus_space_handle_t bsh,
    285 	bus_size_t offset, bus_size_t len, int flags)
    286 {
    287 
    288 	/* weelll ... */
    289 	membar_sync();
    290 }
    291