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