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