Home | History | Annotate | Line # | Download | only in libpci
rumpdev_bus_space.c revision 1.1
      1  1.1  pooka /*	$NetBSD: rumpdev_bus_space.c,v 1.1 2014/04/04 12:53:59 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.1  pooka 
     30  1.1  pooka #include <sys/param.h>
     31  1.1  pooka 
     32  1.1  pooka #include <dev/pci/pcivar.h>
     33  1.1  pooka 
     34  1.1  pooka #include "pci_user.h"
     35  1.1  pooka 
     36  1.1  pooka int
     37  1.1  pooka bus_space_map(bus_space_tag_t bst, bus_addr_t address, bus_size_t size,
     38  1.1  pooka 	int flags, bus_space_handle_t *handlep)
     39  1.1  pooka {
     40  1.1  pooka 	int rv;
     41  1.1  pooka 
     42  1.1  pooka 	/*
     43  1.1  pooka 	 * I/O space we just "let it bli" in case someone wants to
     44  1.1  pooka 	 * map it (e.g. on Xen)
     45  1.1  pooka  	 *
     46  1.1  pooka 	 * Memory space needs to be mapped into our guest, so we
     47  1.1  pooka 	 * make a hypercall to request it.
     48  1.1  pooka 	 */
     49  1.1  pooka 	if (bst == 0) {
     50  1.1  pooka 		*handlep = address;
     51  1.1  pooka 		rv = 0;
     52  1.1  pooka 	} else {
     53  1.1  pooka 		*handlep = (bus_space_handle_t)rumpcomp_pci_map(address, size);
     54  1.1  pooka 		rv = *handlep ? 0 : EINVAL;
     55  1.1  pooka 	}
     56  1.1  pooka 
     57  1.1  pooka 	return rv;
     58  1.1  pooka }
     59  1.1  pooka 
     60  1.1  pooka uint8_t
     61  1.1  pooka bus_space_read_1(bus_space_tag_t bst, bus_space_handle_t bsh,
     62  1.1  pooka 	bus_size_t offset)
     63  1.1  pooka {
     64  1.1  pooka 	uint8_t rv;
     65  1.1  pooka 
     66  1.1  pooka 	if (bst == 0) {
     67  1.1  pooka 		panic("8bit IO space not supported");
     68  1.1  pooka 	} else {
     69  1.1  pooka 		rv = *(volatile uint8_t *)(bsh + offset);
     70  1.1  pooka 	}
     71  1.1  pooka 
     72  1.1  pooka 	return rv;
     73  1.1  pooka }
     74  1.1  pooka 
     75  1.1  pooka uint16_t
     76  1.1  pooka bus_space_read_2(bus_space_tag_t bst, bus_space_handle_t bsh,
     77  1.1  pooka 	bus_size_t offset)
     78  1.1  pooka {
     79  1.1  pooka 	uint16_t rv;
     80  1.1  pooka 
     81  1.1  pooka 	if (bst == 0) {
     82  1.1  pooka 		panic("16bit IO space not supported");
     83  1.1  pooka 	} else {
     84  1.1  pooka 		rv = *(volatile uint16_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 uint32_t
     91  1.1  pooka bus_space_read_4(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 	uint32_t rv;
     95  1.1  pooka 
     96  1.1  pooka 	if (bst == 0) {
     97  1.1  pooka #if 1
     98  1.1  pooka 		panic("IO space not supported in this build");
     99  1.1  pooka #else
    100  1.1  pooka 		unsigned short addr = bsh + offset;
    101  1.1  pooka 		__asm__ __volatile__("inl %1, %0" : "=a"(rv) : "d"(addr));
    102  1.1  pooka #endif
    103  1.1  pooka 	} else {
    104  1.1  pooka 		rv = *(volatile uint32_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 void
    111  1.1  pooka bus_space_write_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    112  1.1  pooka 	bus_size_t offset, uint8_t v)
    113  1.1  pooka {
    114  1.1  pooka 
    115  1.1  pooka 	if (bst == 0) {
    116  1.1  pooka #if 1
    117  1.1  pooka 		panic("IO space not supported in this build");
    118  1.1  pooka #endif
    119  1.1  pooka 	} else {
    120  1.1  pooka 		*(volatile uint8_t *)(bsh + offset) = v;
    121  1.1  pooka 	}
    122  1.1  pooka }
    123  1.1  pooka 
    124  1.1  pooka void
    125  1.1  pooka bus_space_write_2(bus_space_tag_t bst, bus_space_handle_t bsh,
    126  1.1  pooka 	bus_size_t offset, uint16_t v)
    127  1.1  pooka {
    128  1.1  pooka 
    129  1.1  pooka 	if (bst == 0) {
    130  1.1  pooka #if 1
    131  1.1  pooka 		panic("IO space not supported in this build");
    132  1.1  pooka #endif
    133  1.1  pooka 	} else {
    134  1.1  pooka 		*(volatile uint16_t *)(bsh + offset) = v;
    135  1.1  pooka 	}
    136  1.1  pooka }
    137  1.1  pooka 
    138  1.1  pooka void
    139  1.1  pooka bus_space_write_4(bus_space_tag_t bst, bus_space_handle_t bsh,
    140  1.1  pooka 	bus_size_t offset, uint32_t v)
    141  1.1  pooka {
    142  1.1  pooka 
    143  1.1  pooka 	if (bst == 0) {
    144  1.1  pooka #if 1
    145  1.1  pooka 		panic("IO space not supported in this build");
    146  1.1  pooka #else
    147  1.1  pooka 		unsigned short addr = bsh + offset;
    148  1.1  pooka 		__asm__ __volatile__("outl %0, %1" :: "a"(v), "d"(addr));
    149  1.1  pooka #endif
    150  1.1  pooka 	} else {
    151  1.1  pooka 		*(volatile uint32_t *)(bsh + offset) = v;
    152  1.1  pooka 	}
    153  1.1  pooka }
    154  1.1  pooka 
    155  1.1  pooka paddr_t
    156  1.1  pooka bus_space_mmap(bus_space_tag_t bst, bus_addr_t addr, off_t off,
    157  1.1  pooka 	int prot, int flags)
    158  1.1  pooka {
    159  1.1  pooka 
    160  1.1  pooka 	panic("%s: unimplemented", __func__);
    161  1.1  pooka }
    162  1.1  pooka 
    163  1.1  pooka int
    164  1.1  pooka bus_space_subregion(bus_space_tag_t bst, bus_space_handle_t bsh,
    165  1.1  pooka 	bus_size_t offset, bus_size_t size, bus_space_handle_t *nhandlep)
    166  1.1  pooka {
    167  1.1  pooka 
    168  1.1  pooka 	panic("%s: unimplemented", __func__);
    169  1.1  pooka }
    170  1.1  pooka 
    171  1.1  pooka void
    172  1.1  pooka bus_space_unmap(bus_space_tag_t bst, bus_space_handle_t bsh,
    173  1.1  pooka 	bus_size_t size)
    174  1.1  pooka {
    175  1.1  pooka 
    176  1.1  pooka 	panic("%s: unimplemented", __func__);
    177  1.1  pooka }
    178