Home | History | Annotate | Line # | Download | only in linux
io-mapping.h revision 1.5
      1  1.5  riastrad /*	$NetBSD: io-mapping.h,v 1.5 2015/02/25 14:02:43 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.2  riastrad /*-
      4  1.2  riastrad  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.2  riastrad  * All rights reserved.
      6  1.2  riastrad  *
      7  1.2  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  riastrad  * by Taylor R. Campbell.
      9  1.2  riastrad  *
     10  1.2  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.2  riastrad  * modification, are permitted provided that the following conditions
     12  1.2  riastrad  * are met:
     13  1.2  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.2  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.2  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.2  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.2  riastrad  *
     19  1.2  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2  riastrad  */
     31  1.2  riastrad 
     32  1.2  riastrad #ifndef _LINUX_IO_MAPPING_H_
     33  1.2  riastrad #define _LINUX_IO_MAPPING_H_
     34  1.2  riastrad 
     35  1.3  riastrad #include <sys/param.h>
     36  1.2  riastrad #include <sys/bus.h>
     37  1.2  riastrad #include <sys/kmem.h>
     38  1.2  riastrad #include <sys/systm.h>
     39  1.5  riastrad #include <sys/mman.h>
     40  1.2  riastrad 
     41  1.3  riastrad #include <uvm/uvm_extern.h>
     42  1.3  riastrad 
     43  1.2  riastrad struct io_mapping {
     44  1.2  riastrad 	bus_space_tag_t		diom_bst;
     45  1.2  riastrad 	bus_addr_t		diom_addr;
     46  1.2  riastrad 	bus_size_t		diom_size;
     47  1.3  riastrad 	vaddr_t			diom_va;
     48  1.3  riastrad 	bool			diom_mapped;
     49  1.2  riastrad };
     50  1.2  riastrad 
     51  1.2  riastrad static inline struct io_mapping *
     52  1.2  riastrad bus_space_io_mapping_create_wc(bus_space_tag_t bst, bus_addr_t addr,
     53  1.2  riastrad     bus_size_t size)
     54  1.2  riastrad {
     55  1.3  riastrad 	struct io_mapping *mapping;
     56  1.3  riastrad 	bus_size_t offset;
     57  1.3  riastrad 
     58  1.3  riastrad 	KASSERT(PAGE_SIZE <= size);
     59  1.3  riastrad 	KASSERT(0 == (size & (PAGE_SIZE - 1)));
     60  1.3  riastrad 	KASSERT(__type_fit(off_t, size));
     61  1.3  riastrad 
     62  1.3  riastrad 	/*
     63  1.3  riastrad 	 * XXX For x86: Reserve the region (bus_space_reserve) and set
     64  1.3  riastrad 	 * an MTRR to make it write-combining.  Doesn't matter if we
     65  1.3  riastrad 	 * have PAT and we use pmap_kenter_pa, but matters if we don't
     66  1.3  riastrad 	 * have PAT or if we later make this use direct map.
     67  1.3  riastrad 	 */
     68  1.3  riastrad 
     69  1.3  riastrad 	/* Make sure the region is mappable.  */
     70  1.3  riastrad 	for (offset = 0; offset < size; offset += PAGE_SIZE) {
     71  1.3  riastrad 		if (bus_space_mmap(bst, addr, offset, PROT_READ|PROT_WRITE,
     72  1.3  riastrad 			BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE)
     73  1.3  riastrad 		    == (paddr_t)-1)
     74  1.3  riastrad 			return NULL;
     75  1.3  riastrad 	}
     76  1.3  riastrad 
     77  1.3  riastrad 	/* Create a mapping record.  */
     78  1.3  riastrad 	mapping = kmem_alloc(sizeof(*mapping), KM_SLEEP);
     79  1.2  riastrad 	mapping->diom_bst = bst;
     80  1.2  riastrad 	mapping->diom_addr = addr;
     81  1.2  riastrad 	mapping->diom_size = size;
     82  1.3  riastrad 	mapping->diom_mapped = false;
     83  1.3  riastrad 
     84  1.3  riastrad 	/* Allocate kva for one page.  */
     85  1.3  riastrad 	mapping->diom_va = uvm_km_alloc(kernel_map, PAGE_SIZE, PAGE_SIZE,
     86  1.3  riastrad 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA);
     87  1.3  riastrad 	KASSERT(mapping->diom_va != 0);
     88  1.2  riastrad 
     89  1.2  riastrad 	return mapping;
     90  1.2  riastrad }
     91  1.2  riastrad 
     92  1.2  riastrad static inline void
     93  1.2  riastrad io_mapping_free(struct io_mapping *mapping)
     94  1.2  riastrad {
     95  1.2  riastrad 
     96  1.3  riastrad 	KASSERT(!mapping->diom_mapped);
     97  1.3  riastrad 
     98  1.3  riastrad 	uvm_km_free(kernel_map, mapping->diom_va, PAGE_SIZE, UVM_KMF_VAONLY);
     99  1.2  riastrad 	kmem_free(mapping, sizeof(*mapping));
    100  1.2  riastrad }
    101  1.2  riastrad 
    102  1.2  riastrad static inline void *
    103  1.2  riastrad io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
    104  1.2  riastrad {
    105  1.3  riastrad 	paddr_t cookie;
    106  1.2  riastrad 
    107  1.3  riastrad 	KASSERT(0 == (offset & (PAGE_SIZE - 1)));
    108  1.3  riastrad 	KASSERT(PAGE_SIZE <= mapping->diom_size);
    109  1.3  riastrad 	KASSERT(offset <= (mapping->diom_size - PAGE_SIZE));
    110  1.3  riastrad 	KASSERT(__type_fit(off_t, offset));
    111  1.3  riastrad 	KASSERT(!mapping->diom_mapped);
    112  1.3  riastrad 
    113  1.3  riastrad 	cookie = bus_space_mmap(mapping->diom_bst, mapping->diom_addr, offset,
    114  1.3  riastrad 	    PROT_READ|PROT_WRITE,
    115  1.3  riastrad 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
    116  1.3  riastrad 	KASSERT(cookie != (paddr_t)-1);
    117  1.3  riastrad 
    118  1.3  riastrad 	pmap_kenter_pa(mapping->diom_va, pmap_phys_address(cookie),
    119  1.3  riastrad 	    PROT_READ|PROT_WRITE, pmap_mmap_flags(cookie));
    120  1.3  riastrad 	pmap_update(pmap_kernel());
    121  1.2  riastrad 
    122  1.3  riastrad 	mapping->diom_mapped = true;
    123  1.3  riastrad 	return (void *)mapping->diom_va;
    124  1.2  riastrad }
    125  1.2  riastrad 
    126  1.2  riastrad static inline void
    127  1.3  riastrad io_mapping_unmap(struct io_mapping *mapping, void *ptr __diagused)
    128  1.2  riastrad {
    129  1.2  riastrad 
    130  1.3  riastrad 	KASSERT(mapping->diom_mapped);
    131  1.3  riastrad 	KASSERT(mapping->diom_va == (vaddr_t)ptr);
    132  1.3  riastrad 
    133  1.3  riastrad 	pmap_kremove(mapping->diom_va, PAGE_SIZE);
    134  1.3  riastrad 	pmap_update(pmap_kernel());
    135  1.3  riastrad 
    136  1.3  riastrad 	mapping->diom_mapped = false;
    137  1.2  riastrad }
    138  1.2  riastrad 
    139  1.2  riastrad static inline void *
    140  1.2  riastrad io_mapping_map_atomic_wc(struct io_mapping *mapping, unsigned long offset)
    141  1.2  riastrad {
    142  1.3  riastrad 
    143  1.2  riastrad 	return io_mapping_map_wc(mapping, offset);
    144  1.2  riastrad }
    145  1.2  riastrad 
    146  1.2  riastrad static inline void
    147  1.3  riastrad io_mapping_unmap_atomic(struct io_mapping *mapping, void *ptr)
    148  1.2  riastrad {
    149  1.3  riastrad 
    150  1.4     njoly 	io_mapping_unmap(mapping, ptr);
    151  1.2  riastrad }
    152  1.2  riastrad 
    153  1.2  riastrad #endif  /* _LINUX_IO_MAPPING_H_ */
    154