Home | History | Annotate | Line # | Download | only in linux
io-mapping.h revision 1.2.8.2
      1  1.2.8.2    martin /*	$NetBSD: io-mapping.h,v 1.2.8.2 2014/11/10 19:45:53 martin 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.2.8.1       snj #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.2.8.1       snj #include <uvm/uvm_extern.h>
     41  1.2.8.1       snj 
     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.2.8.1       snj 	vaddr_t			diom_va;
     47  1.2.8.1       snj 	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.2.8.1       snj 	struct io_mapping *mapping;
     55  1.2.8.1       snj 	bus_size_t offset;
     56  1.2.8.1       snj 
     57  1.2.8.1       snj 	KASSERT(PAGE_SIZE <= size);
     58  1.2.8.1       snj 	KASSERT(0 == (size & (PAGE_SIZE - 1)));
     59  1.2.8.1       snj 	KASSERT(__type_fit(off_t, size));
     60  1.2.8.1       snj 
     61  1.2.8.1       snj 	/*
     62  1.2.8.1       snj 	 * XXX For x86: Reserve the region (bus_space_reserve) and set
     63  1.2.8.1       snj 	 * an MTRR to make it write-combining.  Doesn't matter if we
     64  1.2.8.1       snj 	 * have PAT and we use pmap_kenter_pa, but matters if we don't
     65  1.2.8.1       snj 	 * have PAT or if we later make this use direct map.
     66  1.2.8.1       snj 	 */
     67  1.2.8.1       snj 
     68  1.2.8.1       snj 	/* Make sure the region is mappable.  */
     69  1.2.8.1       snj 	for (offset = 0; offset < size; offset += PAGE_SIZE) {
     70  1.2.8.1       snj 		if (bus_space_mmap(bst, addr, offset, PROT_READ|PROT_WRITE,
     71  1.2.8.1       snj 			BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE)
     72  1.2.8.1       snj 		    == (paddr_t)-1)
     73  1.2.8.1       snj 			return NULL;
     74  1.2.8.1       snj 	}
     75  1.2.8.1       snj 
     76  1.2.8.1       snj 	/* Create a mapping record.  */
     77  1.2.8.1       snj 	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.2.8.1       snj 	mapping->diom_mapped = false;
     82  1.2.8.1       snj 
     83  1.2.8.1       snj 	/* Allocate kva for one page.  */
     84  1.2.8.1       snj 	mapping->diom_va = uvm_km_alloc(kernel_map, PAGE_SIZE, PAGE_SIZE,
     85  1.2.8.1       snj 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA);
     86  1.2.8.1       snj 	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.2.8.1       snj 	KASSERT(!mapping->diom_mapped);
     96  1.2.8.1       snj 
     97  1.2.8.1       snj 	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.2.8.1       snj 	paddr_t cookie;
    105      1.2  riastrad 
    106  1.2.8.1       snj 	KASSERT(0 == (offset & (PAGE_SIZE - 1)));
    107  1.2.8.1       snj 	KASSERT(PAGE_SIZE <= mapping->diom_size);
    108  1.2.8.1       snj 	KASSERT(offset <= (mapping->diom_size - PAGE_SIZE));
    109  1.2.8.1       snj 	KASSERT(__type_fit(off_t, offset));
    110  1.2.8.1       snj 	KASSERT(!mapping->diom_mapped);
    111  1.2.8.1       snj 
    112  1.2.8.1       snj 	cookie = bus_space_mmap(mapping->diom_bst, mapping->diom_addr, offset,
    113  1.2.8.1       snj 	    PROT_READ|PROT_WRITE,
    114  1.2.8.1       snj 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
    115  1.2.8.1       snj 	KASSERT(cookie != (paddr_t)-1);
    116  1.2.8.1       snj 
    117  1.2.8.1       snj 	pmap_kenter_pa(mapping->diom_va, pmap_phys_address(cookie),
    118  1.2.8.1       snj 	    PROT_READ|PROT_WRITE, pmap_mmap_flags(cookie));
    119  1.2.8.1       snj 	pmap_update(pmap_kernel());
    120      1.2  riastrad 
    121  1.2.8.1       snj 	mapping->diom_mapped = true;
    122  1.2.8.1       snj 	return (void *)mapping->diom_va;
    123      1.2  riastrad }
    124      1.2  riastrad 
    125      1.2  riastrad static inline void
    126  1.2.8.1       snj io_mapping_unmap(struct io_mapping *mapping, void *ptr __diagused)
    127      1.2  riastrad {
    128      1.2  riastrad 
    129  1.2.8.1       snj 	KASSERT(mapping->diom_mapped);
    130  1.2.8.1       snj 	KASSERT(mapping->diom_va == (vaddr_t)ptr);
    131  1.2.8.1       snj 
    132  1.2.8.1       snj 	pmap_kremove(mapping->diom_va, PAGE_SIZE);
    133  1.2.8.1       snj 	pmap_update(pmap_kernel());
    134  1.2.8.1       snj 
    135  1.2.8.1       snj 	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.2.8.1       snj 
    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.2.8.1       snj io_mapping_unmap_atomic(struct io_mapping *mapping, void *ptr)
    147      1.2  riastrad {
    148  1.2.8.1       snj 
    149  1.2.8.2    martin 	io_mapping_unmap(mapping, ptr);
    150      1.2  riastrad }
    151      1.2  riastrad 
    152      1.2  riastrad #endif  /* _LINUX_IO_MAPPING_H_ */
    153