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