Home | History | Annotate | Line # | Download | only in linux
io-mapping.h revision 1.2.10.2
      1 /*	$NetBSD: io-mapping.h,v 1.2.10.2 2014/08/20 00:04:21 tls Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _LINUX_IO_MAPPING_H_
     33 #define _LINUX_IO_MAPPING_H_
     34 
     35 #include <sys/bus.h>
     36 #include <sys/kmem.h>
     37 #include <sys/systm.h>
     38 
     39 struct io_mapping {
     40 	bus_space_tag_t		diom_bst;
     41 	bus_addr_t		diom_addr;
     42 	bus_size_t		diom_size;
     43 	int			diom_flags;
     44 	bus_space_handle_t	diom_bsh;
     45 	void			*diom_vaddr;
     46 };
     47 
     48 static inline struct io_mapping *
     49 bus_space_io_mapping_create_wc(bus_space_tag_t bst, bus_addr_t addr,
     50     bus_size_t size)
     51 {
     52 	struct io_mapping *const mapping = kmem_alloc(sizeof(*mapping),
     53 	    KM_SLEEP);
     54 	mapping->diom_bst = bst;
     55 	mapping->diom_addr = addr;
     56 	mapping->diom_size = size;
     57 	mapping->diom_flags = 0;
     58 	mapping->diom_flags |= BUS_SPACE_MAP_LINEAR;
     59 	mapping->diom_flags |= BUS_SPACE_MAP_PREFETCHABLE;
     60 	mapping->diom_vaddr = NULL;
     61 
     62 	bus_space_handle_t bsh;
     63 	if (bus_space_map(mapping->diom_bst, addr, PAGE_SIZE,
     64 		mapping->diom_flags, &bsh)) {
     65 		kmem_free(mapping, sizeof(*mapping));
     66 		return NULL;
     67 	}
     68 	bus_space_unmap(mapping->diom_bst, bsh, PAGE_SIZE);
     69 
     70 	return mapping;
     71 }
     72 
     73 static inline void
     74 io_mapping_free(struct io_mapping *mapping)
     75 {
     76 
     77 	KASSERT(mapping->diom_vaddr == NULL);
     78 	kmem_free(mapping, sizeof(*mapping));
     79 }
     80 
     81 static inline void *
     82 io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
     83 {
     84 
     85 	KASSERT(mapping->diom_vaddr == NULL);
     86 	KASSERT(ISSET(mapping->diom_flags, BUS_SPACE_MAP_LINEAR));
     87 	if (bus_space_map(mapping->diom_bst, (mapping->diom_addr + offset),
     88 		PAGE_SIZE, mapping->diom_flags, &mapping->diom_bsh))
     89 		panic("Unable to make I/O mapping!"); /* XXX */
     90 	mapping->diom_vaddr = bus_space_vaddr(mapping->diom_bst,
     91 	    mapping->diom_bsh);
     92 
     93 	return mapping->diom_vaddr;
     94 }
     95 
     96 static inline void
     97 io_mapping_unmap(struct io_mapping *mapping, void *vaddr __unused)
     98 {
     99 
    100 	KASSERT(mapping->diom_vaddr == vaddr);
    101 	bus_space_unmap(mapping->diom_bst, mapping->diom_bsh, PAGE_SIZE);
    102 	mapping->diom_vaddr = NULL;
    103 }
    104 
    105 static inline void *
    106 io_mapping_map_atomic_wc(struct io_mapping *mapping, unsigned long offset)
    107 {
    108 	return io_mapping_map_wc(mapping, offset);
    109 }
    110 
    111 static inline void
    112 io_mapping_unmap_atomic(struct io_mapping *mapping, void *vaddr __unused)
    113 {
    114 	return io_mapping_unmap(mapping, vaddr);
    115 }
    116 
    117 #endif  /* _LINUX_IO_MAPPING_H_ */
    118