io-mapping.h revision 1.2.10.3 1 1.2.10.2 tls /* $NetBSD: io-mapping.h,v 1.2.10.3 2017/12/03 11:37:59 jdolecek Exp $ */
2 1.2.10.2 tls
3 1.2.10.2 tls /*-
4 1.2.10.2 tls * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2.10.2 tls * All rights reserved.
6 1.2.10.2 tls *
7 1.2.10.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.2.10.2 tls * by Taylor R. Campbell.
9 1.2.10.2 tls *
10 1.2.10.2 tls * Redistribution and use in source and binary forms, with or without
11 1.2.10.2 tls * modification, are permitted provided that the following conditions
12 1.2.10.2 tls * are met:
13 1.2.10.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.2.10.2 tls * notice, this list of conditions and the following disclaimer.
15 1.2.10.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.10.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.2.10.2 tls * documentation and/or other materials provided with the distribution.
18 1.2.10.2 tls *
19 1.2.10.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.10.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.10.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.10.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.10.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.10.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.10.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.10.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.10.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.10.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.10.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.2.10.2 tls */
31 1.2.10.2 tls
32 1.2.10.2 tls #ifndef _LINUX_IO_MAPPING_H_
33 1.2.10.2 tls #define _LINUX_IO_MAPPING_H_
34 1.2.10.2 tls
35 1.2.10.3 jdolecek #include <sys/param.h>
36 1.2.10.2 tls #include <sys/bus.h>
37 1.2.10.2 tls #include <sys/kmem.h>
38 1.2.10.2 tls #include <sys/systm.h>
39 1.2.10.3 jdolecek #include <sys/mman.h>
40 1.2.10.3 jdolecek
41 1.2.10.3 jdolecek #include <uvm/uvm_extern.h>
42 1.2.10.2 tls
43 1.2.10.2 tls struct io_mapping {
44 1.2.10.2 tls bus_space_tag_t diom_bst;
45 1.2.10.2 tls bus_addr_t diom_addr;
46 1.2.10.2 tls bus_size_t diom_size;
47 1.2.10.3 jdolecek vaddr_t diom_va;
48 1.2.10.3 jdolecek bool diom_mapped;
49 1.2.10.2 tls };
50 1.2.10.2 tls
51 1.2.10.2 tls static inline struct io_mapping *
52 1.2.10.2 tls bus_space_io_mapping_create_wc(bus_space_tag_t bst, bus_addr_t addr,
53 1.2.10.2 tls bus_size_t size)
54 1.2.10.2 tls {
55 1.2.10.3 jdolecek struct io_mapping *mapping;
56 1.2.10.3 jdolecek bus_size_t offset;
57 1.2.10.3 jdolecek
58 1.2.10.3 jdolecek KASSERT(PAGE_SIZE <= size);
59 1.2.10.3 jdolecek KASSERT(0 == (size & (PAGE_SIZE - 1)));
60 1.2.10.3 jdolecek KASSERT(__type_fit(off_t, size));
61 1.2.10.3 jdolecek
62 1.2.10.3 jdolecek /*
63 1.2.10.3 jdolecek * XXX For x86: Reserve the region (bus_space_reserve) and set
64 1.2.10.3 jdolecek * an MTRR to make it write-combining. Doesn't matter if we
65 1.2.10.3 jdolecek * have PAT and we use pmap_kenter_pa, but matters if we don't
66 1.2.10.3 jdolecek * have PAT or if we later make this use direct map.
67 1.2.10.3 jdolecek */
68 1.2.10.3 jdolecek
69 1.2.10.3 jdolecek /* Make sure the region is mappable. */
70 1.2.10.3 jdolecek for (offset = 0; offset < size; offset += PAGE_SIZE) {
71 1.2.10.3 jdolecek if (bus_space_mmap(bst, addr, offset, PROT_READ|PROT_WRITE,
72 1.2.10.3 jdolecek BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE)
73 1.2.10.3 jdolecek == (paddr_t)-1)
74 1.2.10.3 jdolecek return NULL;
75 1.2.10.3 jdolecek }
76 1.2.10.3 jdolecek
77 1.2.10.3 jdolecek /* Create a mapping record. */
78 1.2.10.3 jdolecek mapping = kmem_alloc(sizeof(*mapping), KM_SLEEP);
79 1.2.10.2 tls mapping->diom_bst = bst;
80 1.2.10.2 tls mapping->diom_addr = addr;
81 1.2.10.2 tls mapping->diom_size = size;
82 1.2.10.3 jdolecek mapping->diom_mapped = false;
83 1.2.10.3 jdolecek
84 1.2.10.3 jdolecek /* Allocate kva for one page. */
85 1.2.10.3 jdolecek mapping->diom_va = uvm_km_alloc(kernel_map, PAGE_SIZE, PAGE_SIZE,
86 1.2.10.3 jdolecek UVM_KMF_VAONLY | UVM_KMF_WAITVA);
87 1.2.10.3 jdolecek KASSERT(mapping->diom_va != 0);
88 1.2.10.2 tls
89 1.2.10.2 tls return mapping;
90 1.2.10.2 tls }
91 1.2.10.2 tls
92 1.2.10.2 tls static inline void
93 1.2.10.2 tls io_mapping_free(struct io_mapping *mapping)
94 1.2.10.2 tls {
95 1.2.10.2 tls
96 1.2.10.3 jdolecek KASSERT(!mapping->diom_mapped);
97 1.2.10.3 jdolecek
98 1.2.10.3 jdolecek uvm_km_free(kernel_map, mapping->diom_va, PAGE_SIZE, UVM_KMF_VAONLY);
99 1.2.10.2 tls kmem_free(mapping, sizeof(*mapping));
100 1.2.10.2 tls }
101 1.2.10.2 tls
102 1.2.10.2 tls static inline void *
103 1.2.10.2 tls io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
104 1.2.10.2 tls {
105 1.2.10.3 jdolecek paddr_t cookie;
106 1.2.10.2 tls
107 1.2.10.3 jdolecek KASSERT(0 == (offset & (PAGE_SIZE - 1)));
108 1.2.10.3 jdolecek KASSERT(PAGE_SIZE <= mapping->diom_size);
109 1.2.10.3 jdolecek KASSERT(offset <= (mapping->diom_size - PAGE_SIZE));
110 1.2.10.3 jdolecek KASSERT(__type_fit(off_t, offset));
111 1.2.10.3 jdolecek KASSERT(!mapping->diom_mapped);
112 1.2.10.3 jdolecek
113 1.2.10.3 jdolecek cookie = bus_space_mmap(mapping->diom_bst, mapping->diom_addr, offset,
114 1.2.10.3 jdolecek PROT_READ|PROT_WRITE,
115 1.2.10.3 jdolecek BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
116 1.2.10.3 jdolecek KASSERT(cookie != (paddr_t)-1);
117 1.2.10.3 jdolecek
118 1.2.10.3 jdolecek pmap_kenter_pa(mapping->diom_va, pmap_phys_address(cookie),
119 1.2.10.3 jdolecek PROT_READ|PROT_WRITE, pmap_mmap_flags(cookie));
120 1.2.10.3 jdolecek pmap_update(pmap_kernel());
121 1.2.10.2 tls
122 1.2.10.3 jdolecek mapping->diom_mapped = true;
123 1.2.10.3 jdolecek return (void *)mapping->diom_va;
124 1.2.10.2 tls }
125 1.2.10.2 tls
126 1.2.10.2 tls static inline void
127 1.2.10.3 jdolecek io_mapping_unmap(struct io_mapping *mapping, void *ptr __diagused)
128 1.2.10.2 tls {
129 1.2.10.2 tls
130 1.2.10.3 jdolecek KASSERT(mapping->diom_mapped);
131 1.2.10.3 jdolecek KASSERT(mapping->diom_va == (vaddr_t)ptr);
132 1.2.10.3 jdolecek
133 1.2.10.3 jdolecek pmap_kremove(mapping->diom_va, PAGE_SIZE);
134 1.2.10.3 jdolecek pmap_update(pmap_kernel());
135 1.2.10.3 jdolecek
136 1.2.10.3 jdolecek mapping->diom_mapped = false;
137 1.2.10.2 tls }
138 1.2.10.2 tls
139 1.2.10.2 tls static inline void *
140 1.2.10.2 tls io_mapping_map_atomic_wc(struct io_mapping *mapping, unsigned long offset)
141 1.2.10.2 tls {
142 1.2.10.3 jdolecek
143 1.2.10.2 tls return io_mapping_map_wc(mapping, offset);
144 1.2.10.2 tls }
145 1.2.10.2 tls
146 1.2.10.2 tls static inline void
147 1.2.10.3 jdolecek io_mapping_unmap_atomic(struct io_mapping *mapping, void *ptr)
148 1.2.10.2 tls {
149 1.2.10.3 jdolecek
150 1.2.10.3 jdolecek io_mapping_unmap(mapping, ptr);
151 1.2.10.2 tls }
152 1.2.10.2 tls
153 1.2.10.2 tls #endif /* _LINUX_IO_MAPPING_H_ */
154