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