io-mapping.h revision 1.9 1 /* $NetBSD: io-mapping.h,v 1.9 2021/12/19 11:57:43 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 #include <sys/mman.h>
40
41 #include <uvm/uvm_extern.h>
42
43 struct io_mapping {
44 bus_space_tag_t diom_bst;
45 bus_addr_t base; /* Linux API */
46 bus_size_t size; /* Linux API */
47 vaddr_t diom_va;
48 bool diom_mapped;
49 };
50
51 static inline bool
52 bus_space_io_mapping_init_wc(bus_space_tag_t bst, struct io_mapping *mapping,
53 bus_addr_t addr, bus_size_t size)
54 {
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 false;
74 }
75
76 /* Initialize the mapping record. */
77 mapping->diom_bst = bst;
78 mapping->base = addr;
79 mapping->size = size;
80 mapping->diom_mapped = false;
81
82 /* Allocate kva for one page. */
83 mapping->diom_va = uvm_km_alloc(kernel_map, PAGE_SIZE, PAGE_SIZE,
84 UVM_KMF_VAONLY | UVM_KMF_WAITVA);
85 KASSERT(mapping->diom_va != 0);
86
87 return true;
88 }
89
90 static inline void
91 io_mapping_fini(struct io_mapping *mapping)
92 {
93
94 KASSERT(!mapping->diom_mapped);
95
96 uvm_km_free(kernel_map, mapping->diom_va, PAGE_SIZE, UVM_KMF_VAONLY);
97 mapping->diom_va = 0; /* paranoia */
98 }
99
100 static inline struct io_mapping *
101 bus_space_io_mapping_create_wc(bus_space_tag_t bst, bus_addr_t addr,
102 bus_size_t size)
103 {
104 struct io_mapping *mapping;
105
106 mapping = kmem_alloc(sizeof(*mapping), KM_SLEEP);
107 if (!bus_space_io_mapping_init_wc(bst, mapping, addr, size)) {
108 kmem_free(mapping, sizeof(*mapping));
109 return NULL;
110 }
111
112 return mapping;
113 }
114
115 static inline void
116 io_mapping_free(struct io_mapping *mapping)
117 {
118
119 io_mapping_fini(mapping);
120 kmem_free(mapping, sizeof(*mapping));
121 }
122
123 static inline void *
124 io_mapping_map_wc(struct io_mapping *mapping, bus_addr_t offset,
125 bus_size_t size)
126 {
127 paddr_t cookie;
128
129 KASSERT(size == PAGE_SIZE);
130 KASSERT(0 == (offset & (PAGE_SIZE - 1)));
131 KASSERT(PAGE_SIZE <= mapping->size);
132 KASSERT(offset <= (mapping->size - PAGE_SIZE));
133 KASSERT(__type_fit(off_t, offset));
134 KASSERT(!mapping->diom_mapped);
135
136 cookie = bus_space_mmap(mapping->diom_bst, mapping->base, offset,
137 PROT_READ|PROT_WRITE,
138 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
139 KASSERT(cookie != (paddr_t)-1);
140
141 pmap_kenter_pa(mapping->diom_va, pmap_phys_address(cookie),
142 PROT_READ|PROT_WRITE, pmap_mmap_flags(cookie));
143 pmap_update(pmap_kernel());
144
145 mapping->diom_mapped = true;
146 return (void *)mapping->diom_va;
147 }
148
149 static inline void
150 io_mapping_unmap(struct io_mapping *mapping, void *ptr __diagused)
151 {
152
153 KASSERT(mapping->diom_mapped);
154 KASSERT(mapping->diom_va == (vaddr_t)ptr);
155
156 pmap_kremove(mapping->diom_va, PAGE_SIZE);
157 pmap_update(pmap_kernel());
158
159 mapping->diom_mapped = false;
160 }
161
162 static inline void *
163 io_mapping_map_atomic_wc(struct io_mapping *mapping, unsigned long offset)
164 {
165
166 return io_mapping_map_wc(mapping, offset, PAGE_SIZE);
167 }
168
169 static inline void
170 io_mapping_unmap_atomic(struct io_mapping *mapping, void *ptr)
171 {
172
173 io_mapping_unmap(mapping, ptr);
174 }
175
176 #endif /* _LINUX_IO_MAPPING_H_ */
177