Home | History | Annotate | Line # | Download | only in linux
io-mapping.h revision 1.4
      1  1.4     njoly /*	$NetBSD: io-mapping.h,v 1.4 2014/10/18 11:39:54 njoly 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.2  riastrad 
     40  1.3  riastrad #include <uvm/uvm_extern.h>
     41  1.3  riastrad 
     42  1.2  riastrad struct io_mapping {
     43  1.2  riastrad 	bus_space_tag_t		diom_bst;
     44  1.2  riastrad 	bus_addr_t		diom_addr;
     45  1.2  riastrad 	bus_size_t		diom_size;
     46  1.3  riastrad 	vaddr_t			diom_va;
     47  1.3  riastrad 	bool			diom_mapped;
     48  1.2  riastrad };
     49  1.2  riastrad 
     50  1.2  riastrad static inline struct io_mapping *
     51  1.2  riastrad bus_space_io_mapping_create_wc(bus_space_tag_t bst, bus_addr_t addr,
     52  1.2  riastrad     bus_size_t size)
     53  1.2  riastrad {
     54  1.3  riastrad 	struct io_mapping *mapping;
     55  1.3  riastrad 	bus_size_t offset;
     56  1.3  riastrad 
     57  1.3  riastrad 	KASSERT(PAGE_SIZE <= size);
     58  1.3  riastrad 	KASSERT(0 == (size & (PAGE_SIZE - 1)));
     59  1.3  riastrad 	KASSERT(__type_fit(off_t, size));
     60  1.3  riastrad 
     61  1.3  riastrad 	/*
     62  1.3  riastrad 	 * XXX For x86: Reserve the region (bus_space_reserve) and set
     63  1.3  riastrad 	 * an MTRR to make it write-combining.  Doesn't matter if we
     64  1.3  riastrad 	 * have PAT and we use pmap_kenter_pa, but matters if we don't
     65  1.3  riastrad 	 * have PAT or if we later make this use direct map.
     66  1.3  riastrad 	 */
     67  1.3  riastrad 
     68  1.3  riastrad 	/* Make sure the region is mappable.  */
     69  1.3  riastrad 	for (offset = 0; offset < size; offset += PAGE_SIZE) {
     70  1.3  riastrad 		if (bus_space_mmap(bst, addr, offset, PROT_READ|PROT_WRITE,
     71  1.3  riastrad 			BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE)
     72  1.3  riastrad 		    == (paddr_t)-1)
     73  1.3  riastrad 			return NULL;
     74  1.3  riastrad 	}
     75  1.3  riastrad 
     76  1.3  riastrad 	/* Create a mapping record.  */
     77  1.3  riastrad 	mapping = kmem_alloc(sizeof(*mapping), KM_SLEEP);
     78  1.2  riastrad 	mapping->diom_bst = bst;
     79  1.2  riastrad 	mapping->diom_addr = addr;
     80  1.2  riastrad 	mapping->diom_size = size;
     81  1.3  riastrad 	mapping->diom_mapped = false;
     82  1.3  riastrad 
     83  1.3  riastrad 	/* Allocate kva for one page.  */
     84  1.3  riastrad 	mapping->diom_va = uvm_km_alloc(kernel_map, PAGE_SIZE, PAGE_SIZE,
     85  1.3  riastrad 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA);
     86  1.3  riastrad 	KASSERT(mapping->diom_va != 0);
     87  1.2  riastrad 
     88  1.2  riastrad 	return mapping;
     89  1.2  riastrad }
     90  1.2  riastrad 
     91  1.2  riastrad static inline void
     92  1.2  riastrad io_mapping_free(struct io_mapping *mapping)
     93  1.2  riastrad {
     94  1.2  riastrad 
     95  1.3  riastrad 	KASSERT(!mapping->diom_mapped);
     96  1.3  riastrad 
     97  1.3  riastrad 	uvm_km_free(kernel_map, mapping->diom_va, PAGE_SIZE, UVM_KMF_VAONLY);
     98  1.2  riastrad 	kmem_free(mapping, sizeof(*mapping));
     99  1.2  riastrad }
    100  1.2  riastrad 
    101  1.2  riastrad static inline void *
    102  1.2  riastrad io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
    103  1.2  riastrad {
    104  1.3  riastrad 	paddr_t cookie;
    105  1.2  riastrad 
    106  1.3  riastrad 	KASSERT(0 == (offset & (PAGE_SIZE - 1)));
    107  1.3  riastrad 	KASSERT(PAGE_SIZE <= mapping->diom_size);
    108  1.3  riastrad 	KASSERT(offset <= (mapping->diom_size - PAGE_SIZE));
    109  1.3  riastrad 	KASSERT(__type_fit(off_t, offset));
    110  1.3  riastrad 	KASSERT(!mapping->diom_mapped);
    111  1.3  riastrad 
    112  1.3  riastrad 	cookie = bus_space_mmap(mapping->diom_bst, mapping->diom_addr, offset,
    113  1.3  riastrad 	    PROT_READ|PROT_WRITE,
    114  1.3  riastrad 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
    115  1.3  riastrad 	KASSERT(cookie != (paddr_t)-1);
    116  1.3  riastrad 
    117  1.3  riastrad 	pmap_kenter_pa(mapping->diom_va, pmap_phys_address(cookie),
    118  1.3  riastrad 	    PROT_READ|PROT_WRITE, pmap_mmap_flags(cookie));
    119  1.3  riastrad 	pmap_update(pmap_kernel());
    120  1.2  riastrad 
    121  1.3  riastrad 	mapping->diom_mapped = true;
    122  1.3  riastrad 	return (void *)mapping->diom_va;
    123  1.2  riastrad }
    124  1.2  riastrad 
    125  1.2  riastrad static inline void
    126  1.3  riastrad io_mapping_unmap(struct io_mapping *mapping, void *ptr __diagused)
    127  1.2  riastrad {
    128  1.2  riastrad 
    129  1.3  riastrad 	KASSERT(mapping->diom_mapped);
    130  1.3  riastrad 	KASSERT(mapping->diom_va == (vaddr_t)ptr);
    131  1.3  riastrad 
    132  1.3  riastrad 	pmap_kremove(mapping->diom_va, PAGE_SIZE);
    133  1.3  riastrad 	pmap_update(pmap_kernel());
    134  1.3  riastrad 
    135  1.3  riastrad 	mapping->diom_mapped = false;
    136  1.2  riastrad }
    137  1.2  riastrad 
    138  1.2  riastrad static inline void *
    139  1.2  riastrad io_mapping_map_atomic_wc(struct io_mapping *mapping, unsigned long offset)
    140  1.2  riastrad {
    141  1.3  riastrad 
    142  1.2  riastrad 	return io_mapping_map_wc(mapping, offset);
    143  1.2  riastrad }
    144  1.2  riastrad 
    145  1.2  riastrad static inline void
    146  1.3  riastrad io_mapping_unmap_atomic(struct io_mapping *mapping, void *ptr)
    147  1.2  riastrad {
    148  1.3  riastrad 
    149  1.4     njoly 	io_mapping_unmap(mapping, ptr);
    150  1.2  riastrad }
    151  1.2  riastrad 
    152  1.2  riastrad #endif  /* _LINUX_IO_MAPPING_H_ */
    153