2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3 * Copyright (c) 2017 The FreeBSD Foundation
6 * Portions of this software were developed by Landon Fuller
7 * under sponsorship from the FreeBSD Foundation.
9 * Portions of this software were developed by Landon Fuller
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
20 * redistribution must be conditioned upon including a substantially
21 * similar Disclaimer requirement for further binary redistribution.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34 * THE POSSIBILITY OF SUCH DAMAGES.
39 #ifndef _BHND_BHNDB_PRIVATE_H_
40 #define _BHND_BHNDB_PRIVATE_H_
42 #include <sys/param.h>
43 #include <sys/bitstring.h>
45 #include <sys/systm.h>
47 #include <machine/bus.h>
49 #include <machine/resource.h>
54 * Private bhndb(4) driver definitions.
57 struct bhndb_dw_alloc;
58 struct bhndb_intr_handler;
60 struct bhndb_resources;
62 struct bhndb_resources *bhndb_alloc_resources(device_t dev,
64 const struct bhndb_hwcfg *cfg);
66 void bhndb_free_resources(
67 struct bhndb_resources *br);
69 int bhndb_add_resource_region(
70 struct bhndb_resources *br,
71 bhnd_addr_t addr, bhnd_size_t size,
72 bhndb_priority_t priority,
74 const struct bhndb_regwin *static_regwin);
76 int bhndb_find_resource_limits(
77 struct bhndb_resources *br, int type,
78 struct resource *r, rman_res_t *start,
81 struct bhndb_intr_handler *bhndb_alloc_intr_handler(device_t owner,
83 struct bhndb_intr_isrc *isrc);
84 void bhndb_free_intr_handler(
85 struct bhndb_intr_handler *ih);
87 void bhndb_register_intr_handler(
88 struct bhndb_resources *br,
89 struct bhndb_intr_handler *ih);
90 void bhndb_deregister_intr_handler(
91 struct bhndb_resources *br,
92 struct bhndb_intr_handler *ih);
93 struct bhndb_intr_handler *bhndb_find_intr_handler(
94 struct bhndb_resources *br,
97 bool bhndb_has_static_region_mapping(
98 struct bhndb_resources *br,
99 bhnd_addr_t addr, bhnd_size_t size);
101 struct bhndb_region *bhndb_find_resource_region(
102 struct bhndb_resources *br,
103 bhnd_addr_t addr, bhnd_size_t size);
105 struct bhndb_dw_alloc *bhndb_dw_find_resource(
106 struct bhndb_resources *dr,
109 struct bhndb_dw_alloc *bhndb_dw_find_mapping(
110 struct bhndb_resources *br,
111 bhnd_addr_t addr, bhnd_size_t size);
114 struct bhndb_resources *br,
115 struct bhndb_dw_alloc *dwa,
116 struct resource *res);
118 void bhndb_dw_release(
119 struct bhndb_resources *br,
120 struct bhndb_dw_alloc *dwa,
121 struct resource *res);
123 int bhndb_dw_set_addr(device_t dev,
124 struct bhndb_resources *br,
125 struct bhndb_dw_alloc *dwa,
126 bus_addr_t addr, bus_size_t size);
128 struct bhndb_dw_alloc *bhndb_dw_steal(struct bhndb_resources *br,
131 void bhndb_dw_return_stolen(device_t dev,
132 struct bhndb_resources *br,
133 struct bhndb_dw_alloc *dwa,
136 const struct bhndb_hw_priority *bhndb_hw_priority_find_core(
137 const struct bhndb_hw_priority *table,
138 struct bhnd_core_info *core);
140 const struct bhndb_port_priority *bhndb_hw_priorty_find_port(
141 const struct bhndb_hw_priority *table,
142 struct bhnd_core_info *core,
143 bhnd_port_type port_type, u_int port,
148 * Dynamic register window allocation reference.
150 struct bhndb_dw_rentry {
151 struct resource *dw_res; /**< child resource */
152 LIST_ENTRY(bhndb_dw_rentry) dw_link;
156 * A dynamic register window allocation record.
158 struct bhndb_dw_alloc {
159 const struct bhndb_regwin *win; /**< window definition */
160 struct resource *parent_res; /**< enclosing resource */
161 u_int rnid; /**< region identifier */
162 rman_res_t target; /**< the current window address, or 0x0 if unknown */
164 LIST_HEAD(, bhndb_dw_rentry) refs; /**< references */
168 * A bus address region description.
170 struct bhndb_region {
171 bhnd_addr_t addr; /**< start of mapped range */
172 bhnd_size_t size; /**< size of mapped range */
173 bhndb_priority_t priority; /**< direct resource allocation priority */
174 uint32_t alloc_flags; /**< resource allocation flags (@see bhndb_alloc_flags) */
175 const struct bhndb_regwin *static_regwin; /**< fixed mapping regwin, if any */
177 STAILQ_ENTRY(bhndb_region) link;
181 * Attached interrupt handler state
183 struct bhndb_intr_handler {
184 device_t ih_owner; /**< child device */
185 struct resource *ih_res; /**< child resource */
186 void *ih_cookiep; /**< hostb-assigned cookiep, or NULL if bus_setup_intr() incomplete. */
187 struct bhndb_intr_isrc *ih_isrc; /**< host interrupt source routing the child's interrupt */
188 bool ih_active; /**< handler has been registered via bhndb_register_intr_handler */
190 STAILQ_ENTRY(bhndb_intr_handler) ih_link;
194 * BHNDB resource allocation state.
196 struct bhndb_resources {
197 device_t dev; /**< bridge device */
198 const struct bhndb_hwcfg *cfg; /**< hardware configuration */
200 struct bhndb_host_resources *res; /**< host resources, or NULL if not allocated */
202 struct rman ht_mem_rman; /**< host memory manager */
203 struct rman br_mem_rman; /**< bridged memory manager */
204 struct rman br_irq_rman; /**< bridged irq manager */
206 STAILQ_HEAD(, bhndb_region) bus_regions; /**< bus region descriptors */
208 struct mtx dw_steal_mtx; /**< spinlock must be held when stealing a dynamic window allocation */
209 struct bhndb_dw_alloc *dw_alloc; /**< dynamic window allocation records */
210 size_t dwa_count; /**< number of dynamic windows available. */
211 bitstr_t *dwa_freelist; /**< dynamic window free list */
212 bhndb_priority_t min_prio; /**< minimum resource priority required to
213 allocate a dynamic window */
215 STAILQ_HEAD(,bhndb_intr_handler) bus_intrs; /**< attached child interrupt handlers */
219 * Returns true if the all dynamic windows are marked free, false
222 * @param br The resource state to check.
225 bhndb_dw_all_free(struct bhndb_resources *br)
228 bit_ffs(br->dwa_freelist, br->dwa_count, &bit);
233 * Find the next free dynamic window region in @p br.
235 * @param br The resource state to search.
237 static inline struct bhndb_dw_alloc *
238 bhndb_dw_next_free(struct bhndb_resources *br)
240 struct bhndb_dw_alloc *dw_free;
243 bit_ffc(br->dwa_freelist, br->dwa_count, &bit);
247 dw_free = &br->dw_alloc[bit];
249 KASSERT(LIST_EMPTY(&dw_free->refs),
250 ("free list out of sync with refs"));
256 * Returns true if a dynamic window allocation is marked as free.
258 * @param br The resource state owning @p dwa.
259 * @param dwa The dynamic window allocation record to be checked.
262 bhndb_dw_is_free(struct bhndb_resources *br, struct bhndb_dw_alloc *dwa)
264 bool is_free = LIST_EMPTY(&dwa->refs);
266 KASSERT(is_free == !bit_test(br->dwa_freelist, dwa->rnid),
267 ("refs out of sync with free list"));
273 #define BHNDB_LOCK_INIT(sc) \
274 mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), \
275 "bhndb resource allocator lock", MTX_DEF)
276 #define BHNDB_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
277 #define BHNDB_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
278 #define BHNDB_LOCK_ASSERT(sc, what) mtx_assert(&(sc)->sc_mtx, what)
279 #define BHNDB_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
281 #endif /* _BHND_BHNDB_PRIVATE_H_ */