1 /*-
2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3 * Copyright (c) 2017 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * Portions of this software were developed by Landon Fuller
7 * under sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17 * redistribution must be conditioned upon including a substantially
18 * similar Disclaimer requirement for further binary redistribution.
19 *
20 * NO WARRANTY
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGES.
32 */
33
34 #include <sys/cdefs.h>
35 /*
36 * Abstract BHND Bridge Device Driver
37 *
38 * Provides generic support for bridging from a parent bus (such as PCI) to
39 * a BHND-compatible bus (e.g. bcma or siba).
40 */
41
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/bus.h>
45 #include <sys/module.h>
46 #include <sys/sbuf.h>
47 #include <sys/systm.h>
48
49 #include <machine/bus.h>
50 #include <sys/rman.h>
51 #include <machine/resource.h>
52
53 #include <dev/bhnd/bhndvar.h>
54 #include <dev/bhnd/bhndreg.h>
55
56 #include <dev/bhnd/bhnd_erom.h>
57
58 #include <dev/bhnd/cores/chipc/chipcreg.h>
59 #include <dev/bhnd/nvram/bhnd_nvram.h>
60
61 #include "bhnd_chipc_if.h"
62 #include "bhnd_nvram_if.h"
63
64 #include "bhndbvar.h"
65 #include "bhndb_bus_if.h"
66 #include "bhndb_hwdata.h"
67 #include "bhndb_private.h"
68
69 /* Debugging flags */
70 static u_long bhndb_debug = 0;
71 TUNABLE_ULONG("hw.bhndb.debug", &bhndb_debug);
72
73 enum {
74 BHNDB_DEBUG_PRIO = 1 << 0,
75 };
76
77 #define BHNDB_DEBUG(_type) (BHNDB_DEBUG_ ## _type & bhndb_debug)
78
79 static bool bhndb_hw_matches(struct bhndb_softc *sc,
80 struct bhnd_core_info *cores, u_int ncores,
81 const struct bhndb_hw *hw);
82
83 static int bhndb_init_region_cfg(struct bhndb_softc *sc,
84 bhnd_erom_t *erom,
85 struct bhndb_resources *r,
86 struct bhnd_core_info *cores, u_int ncores,
87 const struct bhndb_hw_priority *table);
88
89 static int bhndb_find_hwspec(struct bhndb_softc *sc,
90 struct bhnd_core_info *cores, u_int ncores,
91 const struct bhndb_hw **hw);
92
93 bhndb_addrspace bhndb_get_addrspace(struct bhndb_softc *sc,
94 device_t child);
95
96 static struct rman *bhndb_get_rman(struct bhndb_softc *sc,
97 device_t child, int type);
98
99 static int bhndb_init_child_resource(struct resource *r,
100 struct resource *parent,
101 bhnd_size_t offset,
102 bhnd_size_t size);
103
104 static int bhndb_activate_static_region(
105 struct bhndb_softc *sc,
106 struct bhndb_region *region,
107 device_t child, int type, int rid,
108 struct resource *r);
109
110 static int bhndb_try_activate_resource(
111 struct bhndb_softc *sc, device_t child,
112 int type, int rid, struct resource *r,
113 bool *indirect);
114
115 static inline struct bhndb_dw_alloc *bhndb_io_resource(struct bhndb_softc *sc,
116 bus_addr_t addr, bus_size_t size,
117 bus_size_t *offset, bool *stolen,
118 bus_addr_t *restore);
119
120 /**
121 * Default bhndb(4) implementation of DEVICE_PROBE().
122 *
123 * This function provides the default bhndb implementation of DEVICE_PROBE(),
124 * and is compatible with bhndb(4) bridges attached via bhndb_attach_bridge().
125 */
126 int
bhndb_generic_probe(device_t dev)127 bhndb_generic_probe(device_t dev)
128 {
129 return (BUS_PROBE_NOWILDCARD);
130 }
131
132 static void
bhndb_probe_nomatch(device_t dev,device_t child)133 bhndb_probe_nomatch(device_t dev, device_t child)
134 {
135 const char *name;
136
137 name = device_get_name(child);
138 if (name == NULL)
139 name = "unknown device";
140
141 device_printf(dev, "<%s> (no driver attached)\n", name);
142 }
143
144 static int
bhndb_print_child(device_t dev,device_t child)145 bhndb_print_child(device_t dev, device_t child)
146 {
147 struct resource_list *rl;
148 int retval = 0;
149
150 retval += bus_print_child_header(dev, child);
151
152 rl = BUS_GET_RESOURCE_LIST(dev, child);
153 if (rl != NULL) {
154 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY,
155 "%#jx");
156 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ,
157 "%jd");
158 }
159
160 retval += bus_print_child_domain(dev, child);
161 retval += bus_print_child_footer(dev, child);
162
163 return (retval);
164 }
165
166 static int
bhndb_child_location(device_t dev,device_t child,struct sbuf * sb)167 bhndb_child_location(device_t dev, device_t child, struct sbuf *sb)
168 {
169 struct bhndb_softc *sc;
170
171 sc = device_get_softc(dev);
172
173 sbuf_printf(sb, "base=0x%llx",
174 (unsigned long long) sc->chipid.enum_addr);
175 return (0);
176 }
177
178 /**
179 * Return true if @p cores matches the @p hw specification.
180 *
181 * @param sc BHNDB device state.
182 * @param cores A device table to match against.
183 * @param ncores The number of cores in @p cores.
184 * @param hw The hardware description to be matched against.
185 */
186 static bool
bhndb_hw_matches(struct bhndb_softc * sc,struct bhnd_core_info * cores,u_int ncores,const struct bhndb_hw * hw)187 bhndb_hw_matches(struct bhndb_softc *sc, struct bhnd_core_info *cores,
188 u_int ncores, const struct bhndb_hw *hw)
189 {
190 for (u_int i = 0; i < hw->num_hw_reqs; i++) {
191 const struct bhnd_core_match *match;
192 bool found;
193
194 match = &hw->hw_reqs[i];
195 found = false;
196
197 for (u_int d = 0; d < ncores; d++) {
198 struct bhnd_core_info *core = &cores[d];
199
200 if (BHNDB_IS_CORE_DISABLED(sc->dev, sc->bus_dev, core))
201 continue;
202
203 if (!bhnd_core_matches(core, match))
204 continue;
205
206 found = true;
207 break;
208 }
209
210 if (!found)
211 return (false);
212 }
213
214 return (true);
215 }
216
217 /**
218 * Initialize the region maps and priority configuration in @p br using
219 * the priority @p table and the set of cores enumerated by @p erom.
220 *
221 * @param sc The bhndb device state.
222 * @param br The resource state to be configured.
223 * @param erom EROM parser used to enumerate @p cores.
224 * @param cores All cores enumerated on the bridged bhnd bus.
225 * @param ncores The length of @p cores.
226 * @param table Hardware priority table to be used to determine the relative
227 * priorities of per-core port resources.
228 */
229 static int
bhndb_init_region_cfg(struct bhndb_softc * sc,bhnd_erom_t * erom,struct bhndb_resources * br,struct bhnd_core_info * cores,u_int ncores,const struct bhndb_hw_priority * table)230 bhndb_init_region_cfg(struct bhndb_softc *sc, bhnd_erom_t *erom,
231 struct bhndb_resources *br, struct bhnd_core_info *cores, u_int ncores,
232 const struct bhndb_hw_priority *table)
233 {
234 const struct bhndb_hw_priority *hp;
235 bhnd_addr_t addr;
236 bhnd_size_t size;
237 size_t prio_low, prio_default, prio_high;
238 int error;
239
240 /* The number of port regions per priority band that must be accessible
241 * via dynamic register windows */
242 prio_low = 0;
243 prio_default = 0;
244 prio_high = 0;
245
246 /*
247 * Register bridge regions covering all statically mapped ports.
248 */
249 for (u_int i = 0; i < ncores; i++) {
250 const struct bhndb_regwin *regw;
251 struct bhnd_core_info *core;
252 struct bhnd_core_match md;
253
254 core = &cores[i];
255 md = bhnd_core_get_match_desc(core);
256
257 for (regw = br->cfg->register_windows;
258 regw->win_type != BHNDB_REGWIN_T_INVALID; regw++)
259 {
260 const struct bhndb_port_priority *pp;
261 uint32_t alloc_flags;
262
263 /* Only core windows are supported */
264 if (regw->win_type != BHNDB_REGWIN_T_CORE)
265 continue;
266
267 /* Skip non-matching cores. */
268 if (!bhndb_regwin_match_core(regw, core))
269 continue;
270
271 /* Fetch the base address of the mapped port */
272 error = bhnd_erom_lookup_core_addr(erom, &md,
273 regw->d.core.port_type,
274 regw->d.core.port,
275 regw->d.core.region,
276 NULL,
277 &addr,
278 &size);
279 if (error) {
280 /* Skip non-applicable register windows */
281 if (error == ENOENT)
282 continue;
283
284 return (error);
285 }
286
287 /*
288 * Apply the register window's region offset, if any.
289 */
290 if (regw->d.core.offset > size) {
291 device_printf(sc->dev, "invalid register "
292 "window offset %#jx for region %#jx+%#jx\n",
293 regw->d.core.offset, addr, size);
294 return (EINVAL);
295 }
296
297 addr += regw->d.core.offset;
298
299 /*
300 * Always defer to the register window's size.
301 *
302 * If the port size is smaller than the window size,
303 * this ensures that we fully utilize register windows
304 * larger than the referenced port.
305 *
306 * If the port size is larger than the window size, this
307 * ensures that we do not directly map the allocations
308 * within the region to a too-small window.
309 */
310 size = regw->win_size;
311
312 /* Fetch allocation flags from the corresponding port
313 * priority entry, if any */
314 pp = bhndb_hw_priorty_find_port(table, core,
315 regw->d.core.port_type, regw->d.core.port,
316 regw->d.core.region);
317 if (pp != NULL) {
318 alloc_flags = pp->alloc_flags;
319 } else {
320 alloc_flags = 0;
321 }
322
323 /*
324 * Add to the bus region list.
325 *
326 * The window priority for a statically mapped region is
327 * always HIGH.
328 */
329 error = bhndb_add_resource_region(br, addr, size,
330 BHNDB_PRIORITY_HIGH, alloc_flags, regw);
331 if (error)
332 return (error);
333 }
334 }
335
336 /*
337 * Perform priority accounting and register bridge regions for all
338 * ports defined in the priority table
339 */
340 for (u_int i = 0; i < ncores; i++) {
341 struct bhnd_core_info *core;
342 struct bhnd_core_match md;
343
344 core = &cores[i];
345 md = bhnd_core_get_match_desc(core);
346
347 /*
348 * Skip priority accounting for cores that ...
349 */
350
351 /* ... do not require bridge resources */
352 if (BHNDB_IS_CORE_DISABLED(sc->dev, sc->bus_dev, core))
353 continue;
354
355 /* ... do not have a priority table entry */
356 hp = bhndb_hw_priority_find_core(table, core);
357 if (hp == NULL)
358 continue;
359
360 /* ... are explicitly disabled in the priority table. */
361 if (hp->priority == BHNDB_PRIORITY_NONE)
362 continue;
363
364 /* Determine the number of dynamic windows required and
365 * register their bus_region entries. */
366 for (u_int i = 0; i < hp->num_ports; i++) {
367 const struct bhndb_port_priority *pp;
368
369 pp = &hp->ports[i];
370
371 /* Fetch the address+size of the mapped port. */
372 error = bhnd_erom_lookup_core_addr(erom, &md,
373 pp->type, pp->port, pp->region,
374 NULL, &addr, &size);
375 if (error) {
376 /* Skip ports not defined on this device */
377 if (error == ENOENT)
378 continue;
379
380 return (error);
381 }
382
383 /* Skip ports with an existing static mapping */
384 if (bhndb_has_static_region_mapping(br, addr, size))
385 continue;
386
387 /* Define a dynamic region for this port */
388 error = bhndb_add_resource_region(br, addr, size,
389 pp->priority, pp->alloc_flags, NULL);
390 if (error)
391 return (error);
392
393 /* Update port mapping counts */
394 switch (pp->priority) {
395 case BHNDB_PRIORITY_NONE:
396 break;
397 case BHNDB_PRIORITY_LOW:
398 prio_low++;
399 break;
400 case BHNDB_PRIORITY_DEFAULT:
401 prio_default++;
402 break;
403 case BHNDB_PRIORITY_HIGH:
404 prio_high++;
405 break;
406 }
407 }
408 }
409
410 /* Determine the minimum priority at which we'll allocate direct
411 * register windows from our dynamic pool */
412 size_t prio_total = prio_low + prio_default + prio_high;
413 if (prio_total <= br->dwa_count) {
414 /* low+default+high priority regions get windows */
415 br->min_prio = BHNDB_PRIORITY_LOW;
416
417 } else if (prio_default + prio_high <= br->dwa_count) {
418 /* default+high priority regions get windows */
419 br->min_prio = BHNDB_PRIORITY_DEFAULT;
420
421 } else {
422 /* high priority regions get windows */
423 br->min_prio = BHNDB_PRIORITY_HIGH;
424 }
425
426 if (BHNDB_DEBUG(PRIO)) {
427 struct bhndb_region *region;
428 const char *direct_msg, *type_msg;
429 bhndb_priority_t prio, prio_min;
430 uint32_t flags;
431
432 prio_min = br->min_prio;
433 device_printf(sc->dev, "min_prio: %d\n", prio_min);
434
435 STAILQ_FOREACH(region, &br->bus_regions, link) {
436 prio = region->priority;
437 flags = region->alloc_flags;
438
439 direct_msg = prio >= prio_min ? "direct" : "indirect";
440 type_msg = region->static_regwin ? "static" : "dynamic";
441
442 device_printf(sc->dev, "region 0x%llx+0x%llx priority "
443 "%u %s/%s",
444 (unsigned long long) region->addr,
445 (unsigned long long) region->size,
446 region->priority,
447 direct_msg, type_msg);
448
449 if (flags & BHNDB_ALLOC_FULFILL_ON_OVERCOMMIT)
450 printf(" [overcommit]\n");
451 else
452 printf("\n");
453 }
454 }
455
456 return (0);
457 }
458
459 /**
460 * Find a hardware specification for @p dev.
461 *
462 * @param sc The bhndb device state.
463 * @param cores All cores enumerated on the bridged bhnd bus.
464 * @param ncores The length of @p cores.
465 * @param[out] hw On success, the matched hardware specification.
466 * with @p dev.
467 *
468 * @retval 0 success
469 * @retval non-zero if an error occurs fetching device info for comparison.
470 */
471 static int
bhndb_find_hwspec(struct bhndb_softc * sc,struct bhnd_core_info * cores,u_int ncores,const struct bhndb_hw ** hw)472 bhndb_find_hwspec(struct bhndb_softc *sc, struct bhnd_core_info *cores,
473 u_int ncores, const struct bhndb_hw **hw)
474 {
475 const struct bhndb_hw *next, *hw_table;
476
477 /* Search for the first matching hardware config. */
478 hw_table = BHNDB_BUS_GET_HARDWARE_TABLE(sc->parent_dev, sc->dev);
479 for (next = hw_table; next->hw_reqs != NULL; next++) {
480 if (!bhndb_hw_matches(sc, cores, ncores, next))
481 continue;
482
483 /* Found */
484 *hw = next;
485 return (0);
486 }
487
488 return (ENOENT);
489 }
490
491 /**
492 * Helper function that must be called by subclass bhndb(4) drivers
493 * when implementing DEVICE_ATTACH() before calling any bhnd(4) or bhndb(4)
494 * APIs on the bridge device.
495 *
496 * This function will add a bridged bhnd(4) child device with a device order of
497 * BHND_PROBE_BUS. Any subclass bhndb(4) driver may use the BHND_PROBE_*
498 * priority bands to add additional devices that will be attached in
499 * their preferred order relative to the bridged bhnd(4) bus.
500 *
501 * @param dev The bridge device to attach.
502 * @param cid The bridged device's chip identification.
503 * @param cores The bridged device's core table.
504 * @param ncores The number of cores in @p cores.
505 * @param bridge_core Core info for the bhnd(4) core serving as the host
506 * bridge.
507 * @param erom_class An erom parser class that may be used to parse
508 * the bridged device's device enumeration table.
509 */
510 int
bhndb_attach(device_t dev,struct bhnd_chipid * cid,struct bhnd_core_info * cores,u_int ncores,struct bhnd_core_info * bridge_core,bhnd_erom_class_t * erom_class)511 bhndb_attach(device_t dev, struct bhnd_chipid *cid,
512 struct bhnd_core_info *cores, u_int ncores,
513 struct bhnd_core_info *bridge_core, bhnd_erom_class_t *erom_class)
514 {
515 struct bhndb_devinfo *dinfo;
516 struct bhndb_softc *sc;
517 const struct bhndb_hw *hw;
518 const struct bhndb_hwcfg *hwcfg;
519 const struct bhndb_hw_priority *hwprio;
520 struct bhnd_erom_io *eio;
521 bhnd_erom_t *erom;
522 int error;
523
524 sc = device_get_softc(dev);
525 sc->dev = dev;
526 sc->parent_dev = device_get_parent(dev);
527 sc->bridge_core = *bridge_core;
528 sc->chipid = *cid;
529
530 if ((error = bhnd_service_registry_init(&sc->services)))
531 return (error);
532
533 BHNDB_LOCK_INIT(sc);
534
535 erom = NULL;
536
537 /* Find a matching bridge hardware configuration */
538 if ((error = bhndb_find_hwspec(sc, cores, ncores, &hw))) {
539 device_printf(sc->dev, "unable to identify device, "
540 " using generic bridge resource definitions\n");
541
542 hwcfg = BHNDB_BUS_GET_GENERIC_HWCFG(sc->parent_dev, dev);
543 hw = NULL;
544 } else {
545 hwcfg = hw->cfg;
546 }
547
548 if (hw != NULL && (bootverbose || BHNDB_DEBUG(PRIO))) {
549 device_printf(sc->dev, "%s resource configuration\n", hw->name);
550 }
551
552 /* Allocate bridge resource state using the discovered hardware
553 * configuration */
554 sc->bus_res = bhndb_alloc_resources(sc->dev, sc->parent_dev, hwcfg);
555 if (sc->bus_res == NULL) {
556 device_printf(sc->dev, "failed to allocate bridge resource "
557 "state\n");
558 error = ENOMEM;
559 goto failed;
560 }
561
562 /* Add our bridged bus device */
563 sc->bus_dev = BUS_ADD_CHILD(dev, BHND_PROBE_BUS, "bhnd", -1);
564 if (sc->bus_dev == NULL) {
565 error = ENXIO;
566 goto failed;
567 }
568
569 dinfo = device_get_ivars(sc->bus_dev);
570 dinfo->addrspace = BHNDB_ADDRSPACE_BRIDGED;
571
572 /* We can now use bhndb to perform bridging of SYS_RES_MEMORY resources;
573 * we use this to instantiate an erom parser instance */
574 eio = bhnd_erom_iores_new(sc->bus_dev, 0);
575 if ((erom = bhnd_erom_alloc(erom_class, cid, eio)) == NULL) {
576 bhnd_erom_io_fini(eio);
577 error = ENXIO;
578 goto failed;
579 }
580
581 /* Populate our resource priority configuration */
582 hwprio = BHNDB_BUS_GET_HARDWARE_PRIO(sc->parent_dev, sc->dev);
583 error = bhndb_init_region_cfg(sc, erom, sc->bus_res, cores, ncores,
584 hwprio);
585 if (error) {
586 device_printf(sc->dev, "failed to initialize resource "
587 "priority configuration: %d\n", error);
588 goto failed;
589 }
590
591 /* Free our erom instance */
592 bhnd_erom_free(erom);
593 erom = NULL;
594
595 return (0);
596
597 failed:
598 BHNDB_LOCK_DESTROY(sc);
599
600 if (sc->bus_res != NULL)
601 bhndb_free_resources(sc->bus_res);
602
603 if (erom != NULL)
604 bhnd_erom_free(erom);
605
606 bhnd_service_registry_fini(&sc->services);
607
608 return (error);
609 }
610
611 /**
612 * Default bhndb(4) implementation of DEVICE_DETACH().
613 *
614 * This function detaches any child devices, and if successful, releases all
615 * resources held by the bridge device.
616 */
617 int
bhndb_generic_detach(device_t dev)618 bhndb_generic_detach(device_t dev)
619 {
620 struct bhndb_softc *sc;
621 int error;
622
623 sc = device_get_softc(dev);
624
625 /* Detach children */
626 if ((error = bus_generic_detach(dev)))
627 return (error);
628
629 /* Delete children */
630 if ((error = device_delete_children(dev)))
631 return (error);
632
633 /* Clean up our service registry */
634 if ((error = bhnd_service_registry_fini(&sc->services)))
635 return (error);
636
637 /* Clean up our driver state. */
638 bhndb_free_resources(sc->bus_res);
639
640 BHNDB_LOCK_DESTROY(sc);
641
642 return (0);
643 }
644
645 /**
646 * Default bhndb(4) implementation of DEVICE_SUSPEND().
647 *
648 * This function calls bus_generic_suspend() (or implements equivalent
649 * behavior).
650 */
651 int
bhndb_generic_suspend(device_t dev)652 bhndb_generic_suspend(device_t dev)
653 {
654 return (bus_generic_suspend(dev));
655 }
656
657 /**
658 * Default bhndb(4) implementation of DEVICE_RESUME().
659 *
660 * This function calls bus_generic_resume() (or implements equivalent
661 * behavior).
662 */
663 int
bhndb_generic_resume(device_t dev)664 bhndb_generic_resume(device_t dev)
665 {
666 struct bhndb_softc *sc;
667 struct bhndb_resources *bus_res;
668 struct bhndb_dw_alloc *dwa;
669 int error;
670
671 sc = device_get_softc(dev);
672 bus_res = sc->bus_res;
673
674 /* Guarantee that all in-use dynamic register windows are mapped to
675 * their previously configured target address. */
676 BHNDB_LOCK(sc);
677 error = 0;
678 for (size_t i = 0; i < bus_res->dwa_count; i++) {
679 dwa = &bus_res->dw_alloc[i];
680
681 /* Skip regions that were not previously used */
682 if (bhndb_dw_is_free(bus_res, dwa) && dwa->target == 0x0)
683 continue;
684
685 /* Otherwise, ensure the register window is correct before
686 * any children attempt MMIO */
687 error = BHNDB_SET_WINDOW_ADDR(dev, dwa->win, dwa->target);
688 if (error)
689 break;
690 }
691 BHNDB_UNLOCK(sc);
692
693 /* Error restoring hardware state; children cannot be safely resumed */
694 if (error) {
695 device_printf(dev, "Unable to restore hardware configuration; "
696 "cannot resume: %d\n", error);
697 return (error);
698 }
699
700 return (bus_generic_resume(dev));
701 }
702
703 /**
704 * Default implementation of BHNDB_SUSPEND_RESOURCE.
705 */
706 static void
bhndb_suspend_resource(device_t dev,device_t child,int type,struct resource * r)707 bhndb_suspend_resource(device_t dev, device_t child, int type,
708 struct resource *r)
709 {
710 struct bhndb_softc *sc;
711 struct bhndb_dw_alloc *dwa;
712
713 sc = device_get_softc(dev);
714
715 /* Non-MMIO resources (e.g. IRQs) are handled solely by our parent */
716 if (type != SYS_RES_MEMORY)
717 return;
718
719 BHNDB_LOCK(sc);
720 dwa = bhndb_dw_find_resource(sc->bus_res, r);
721 if (dwa == NULL) {
722 BHNDB_UNLOCK(sc);
723 return;
724 }
725
726 if (BHNDB_DEBUG(PRIO))
727 device_printf(child, "suspend resource type=%d 0x%jx+0x%jx\n",
728 type, rman_get_start(r), rman_get_size(r));
729
730 /* Release the resource's window reference */
731 bhndb_dw_release(sc->bus_res, dwa, r);
732 BHNDB_UNLOCK(sc);
733 }
734
735 /**
736 * Default implementation of BHNDB_RESUME_RESOURCE.
737 */
738 static int
bhndb_resume_resource(device_t dev,device_t child,int type,struct resource * r)739 bhndb_resume_resource(device_t dev, device_t child, int type,
740 struct resource *r)
741 {
742 struct bhndb_softc *sc;
743
744 sc = device_get_softc(dev);
745
746 /* Non-MMIO resources (e.g. IRQs) are handled solely by our parent */
747 if (type != SYS_RES_MEMORY)
748 return (0);
749
750 /* Inactive resources don't require reallocation of bridge resources */
751 if (!(rman_get_flags(r) & RF_ACTIVE))
752 return (0);
753
754 if (BHNDB_DEBUG(PRIO))
755 device_printf(child, "resume resource type=%d 0x%jx+0x%jx\n",
756 type, rman_get_start(r), rman_get_size(r));
757
758 return (bhndb_try_activate_resource(sc, rman_get_device(r), type,
759 rman_get_rid(r), r, NULL));
760 }
761
762 /**
763 * Default bhndb(4) implementation of BUS_READ_IVAR().
764 */
765 static int
bhndb_read_ivar(device_t dev,device_t child,int index,uintptr_t * result)766 bhndb_read_ivar(device_t dev, device_t child, int index,
767 uintptr_t *result)
768 {
769 return (ENOENT);
770 }
771
772 /**
773 * Default bhndb(4) implementation of BUS_WRITE_IVAR().
774 */
775 static int
bhndb_write_ivar(device_t dev,device_t child,int index,uintptr_t value)776 bhndb_write_ivar(device_t dev, device_t child, int index,
777 uintptr_t value)
778 {
779 return (ENOENT);
780 }
781
782 /**
783 * Return the address space for the given @p child device.
784 */
785 bhndb_addrspace
bhndb_get_addrspace(struct bhndb_softc * sc,device_t child)786 bhndb_get_addrspace(struct bhndb_softc *sc, device_t child)
787 {
788 struct bhndb_devinfo *dinfo;
789 device_t imd_dev;
790
791 /* Find the directly attached parent of the requesting device */
792 imd_dev = child;
793 while (imd_dev != NULL && device_get_parent(imd_dev) != sc->dev)
794 imd_dev = device_get_parent(imd_dev);
795
796 if (imd_dev == NULL)
797 panic("bhndb address space request for non-child device %s\n",
798 device_get_nameunit(child));
799
800 dinfo = device_get_ivars(imd_dev);
801 return (dinfo->addrspace);
802 }
803
804 /**
805 * Return the rman instance for a given resource @p type, if any.
806 *
807 * @param sc The bhndb device state.
808 * @param child The requesting child.
809 * @param type The resource type (e.g. SYS_RES_MEMORY, SYS_RES_IRQ, ...)
810 */
811 static struct rman *
bhndb_get_rman(struct bhndb_softc * sc,device_t child,int type)812 bhndb_get_rman(struct bhndb_softc *sc, device_t child, int type)
813 {
814 switch (bhndb_get_addrspace(sc, child)) {
815 case BHNDB_ADDRSPACE_NATIVE:
816 switch (type) {
817 case SYS_RES_MEMORY:
818 return (&sc->bus_res->ht_mem_rman);
819 case SYS_RES_IRQ:
820 return (NULL);
821 default:
822 return (NULL);
823 }
824
825 case BHNDB_ADDRSPACE_BRIDGED:
826 switch (type) {
827 case SYS_RES_MEMORY:
828 return (&sc->bus_res->br_mem_rman);
829 case SYS_RES_IRQ:
830 return (&sc->bus_res->br_irq_rman);
831 default:
832 return (NULL);
833 }
834 }
835
836 /* Quieten gcc */
837 return (NULL);
838 }
839
840 /**
841 * Default implementation of BUS_ADD_CHILD()
842 */
843 static device_t
bhndb_add_child(device_t dev,u_int order,const char * name,int unit)844 bhndb_add_child(device_t dev, u_int order, const char *name, int unit)
845 {
846 struct bhndb_devinfo *dinfo;
847 device_t child;
848
849 child = device_add_child_ordered(dev, order, name, unit);
850 if (child == NULL)
851 return (NULL);
852
853 dinfo = malloc(sizeof(struct bhndb_devinfo), M_BHND, M_NOWAIT);
854 if (dinfo == NULL) {
855 device_delete_child(dev, child);
856 return (NULL);
857 }
858
859 dinfo->addrspace = BHNDB_ADDRSPACE_NATIVE;
860 resource_list_init(&dinfo->resources);
861
862 device_set_ivars(child, dinfo);
863
864 return (child);
865 }
866
867 /**
868 * Default implementation of BUS_CHILD_DELETED().
869 */
870 static void
bhndb_child_deleted(device_t dev,device_t child)871 bhndb_child_deleted(device_t dev, device_t child)
872 {
873 struct bhndb_devinfo *dinfo = device_get_ivars(child);
874 if (dinfo != NULL) {
875 resource_list_free(&dinfo->resources);
876 free(dinfo, M_BHND);
877 }
878
879 device_set_ivars(child, NULL);
880 }
881
882 /**
883 * Default implementation of BHNDB_GET_CHIPID().
884 */
885 static const struct bhnd_chipid *
bhndb_get_chipid(device_t dev,device_t child)886 bhndb_get_chipid(device_t dev, device_t child)
887 {
888 struct bhndb_softc *sc = device_get_softc(dev);
889 return (&sc->chipid);
890 }
891
892 /**
893 * Default implementation of BHNDB_IS_CORE_DISABLED().
894 */
895 static bool
bhndb_is_core_disabled(device_t dev,device_t child,struct bhnd_core_info * core)896 bhndb_is_core_disabled(device_t dev, device_t child,
897 struct bhnd_core_info *core)
898 {
899 struct bhndb_softc *sc;
900
901 sc = device_get_softc(dev);
902
903 /* Try to defer to the bhndb bus parent */
904 if (BHNDB_BUS_IS_CORE_DISABLED(sc->parent_dev, dev, core))
905 return (true);
906
907 /* Otherwise, we treat bridge-capable cores as unpopulated if they're
908 * not the configured host bridge */
909 if (BHND_DEVCLASS_SUPPORTS_HOSTB(bhnd_core_class(core)))
910 return (!bhnd_cores_equal(core, &sc->bridge_core));
911
912 /* Assume the core is populated */
913 return (false);
914 }
915
916 /**
917 * Default bhndb(4) implementation of BHNDB_GET_HOSTB_CORE().
918 *
919 * This function uses a heuristic valid on all known PCI/PCIe/PCMCIA-bridged
920 * bhnd(4) devices.
921 */
922 static int
bhndb_get_hostb_core(device_t dev,device_t child,struct bhnd_core_info * core)923 bhndb_get_hostb_core(device_t dev, device_t child, struct bhnd_core_info *core)
924 {
925 struct bhndb_softc *sc = device_get_softc(dev);
926
927 *core = sc->bridge_core;
928 return (0);
929 }
930
931 /**
932 * Default bhndb(4) implementation of BHND_BUS_GET_SERVICE_REGISTRY().
933 */
934 static struct bhnd_service_registry *
bhndb_get_service_registry(device_t dev,device_t child)935 bhndb_get_service_registry(device_t dev, device_t child)
936 {
937 struct bhndb_softc *sc = device_get_softc(dev);
938
939 return (&sc->services);
940 }
941
942 /**
943 * Default bhndb(4) implementation of BUS_ALLOC_RESOURCE().
944 */
945 static struct resource *
bhndb_alloc_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)946 bhndb_alloc_resource(device_t dev, device_t child, int type,
947 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
948 {
949 struct bhndb_softc *sc;
950 struct resource_list_entry *rle;
951 struct resource *rv;
952 struct rman *rm;
953 int error;
954 bool passthrough, isdefault;
955
956 sc = device_get_softc(dev);
957 passthrough = (device_get_parent(child) != dev);
958 isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
959 rle = NULL;
960
961 /* Fetch the resource manager */
962 rm = bhndb_get_rman(sc, child, type);
963 if (rm == NULL) {
964 /* Delegate to our parent device's bus; the requested
965 * resource type isn't handled locally. */
966 return (BUS_ALLOC_RESOURCE(device_get_parent(sc->parent_dev),
967 child, type, rid, start, end, count, flags));
968 }
969
970 /* Populate defaults */
971 if (!passthrough && isdefault) {
972 /* Fetch the resource list entry. */
973 rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child),
974 type, *rid);
975 if (rle == NULL) {
976 device_printf(dev,
977 "default resource %#x type %d for child %s "
978 "not found\n", *rid, type,
979 device_get_nameunit(child));
980
981 return (NULL);
982 }
983
984 if (rle->res != NULL) {
985 device_printf(dev,
986 "resource entry %#x type %d for child %s is busy\n",
987 *rid, type, device_get_nameunit(child));
988
989 return (NULL);
990 }
991
992 start = rle->start;
993 end = rle->end;
994 count = ulmax(count, rle->count);
995 }
996
997 /* Validate resource addresses */
998 if (start > end || count > ((end - start) + 1))
999 return (NULL);
1000
1001 /* Make our reservation */
1002 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1003 child);
1004 if (rv == NULL)
1005 return (NULL);
1006
1007 rman_set_rid(rv, *rid);
1008 rman_set_type(rv, type);
1009
1010 /* Activate */
1011 if (flags & RF_ACTIVE) {
1012 error = bus_activate_resource(child, type, *rid, rv);
1013 if (error) {
1014 device_printf(dev,
1015 "failed to activate entry %#x type %d for "
1016 "child %s: %d\n",
1017 *rid, type, device_get_nameunit(child), error);
1018
1019 rman_release_resource(rv);
1020
1021 return (NULL);
1022 }
1023 }
1024
1025 /* Update child's resource list entry */
1026 if (rle != NULL) {
1027 rle->res = rv;
1028 rle->start = rman_get_start(rv);
1029 rle->end = rman_get_end(rv);
1030 rle->count = rman_get_size(rv);
1031 }
1032
1033 return (rv);
1034 }
1035
1036 /**
1037 * Default bhndb(4) implementation of BUS_RELEASE_RESOURCE().
1038 */
1039 static int
bhndb_release_resource(device_t dev,device_t child,int type,int rid,struct resource * r)1040 bhndb_release_resource(device_t dev, device_t child, int type, int rid,
1041 struct resource *r)
1042 {
1043 struct bhndb_softc *sc;
1044 struct resource_list_entry *rle;
1045 bool passthrough;
1046 int error;
1047
1048 sc = device_get_softc(dev);
1049 passthrough = (device_get_parent(child) != dev);
1050
1051 /* Delegate to our parent device's bus if the requested resource type
1052 * isn't handled locally. */
1053 if (bhndb_get_rman(sc, child, type) == NULL) {
1054 return (BUS_RELEASE_RESOURCE(device_get_parent(sc->parent_dev),
1055 child, type, rid, r));
1056 }
1057
1058 /* Deactivate resources */
1059 if (rman_get_flags(r) & RF_ACTIVE) {
1060 error = BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, r);
1061 if (error)
1062 return (error);
1063 }
1064
1065 if ((error = rman_release_resource(r)))
1066 return (error);
1067
1068 if (!passthrough) {
1069 /* Clean resource list entry */
1070 rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child),
1071 type, rid);
1072 if (rle != NULL)
1073 rle->res = NULL;
1074 }
1075
1076 return (0);
1077 }
1078
1079 /**
1080 * Default bhndb(4) implementation of BUS_ADJUST_RESOURCE().
1081 */
1082 static int
bhndb_adjust_resource(device_t dev,device_t child,int type,struct resource * r,rman_res_t start,rman_res_t end)1083 bhndb_adjust_resource(device_t dev, device_t child, int type,
1084 struct resource *r, rman_res_t start, rman_res_t end)
1085 {
1086 struct bhndb_softc *sc;
1087 struct rman *rm;
1088 rman_res_t mstart, mend;
1089 int error;
1090
1091 sc = device_get_softc(dev);
1092 error = 0;
1093
1094 /* Delegate to our parent device's bus if the requested resource type
1095 * isn't handled locally. */
1096 rm = bhndb_get_rman(sc, child, type);
1097 if (rm == NULL) {
1098 return (BUS_ADJUST_RESOURCE(device_get_parent(sc->parent_dev),
1099 child, type, r, start, end));
1100 }
1101
1102 /* Verify basic constraints */
1103 if (end <= start)
1104 return (EINVAL);
1105
1106 if (!rman_is_region_manager(r, rm))
1107 return (ENXIO);
1108
1109 BHNDB_LOCK(sc);
1110
1111 /* If not active, allow any range permitted by the resource manager */
1112 if (!(rman_get_flags(r) & RF_ACTIVE))
1113 goto done;
1114
1115 /* Otherwise, the range is limited by the bridged resource mapping */
1116 error = bhndb_find_resource_limits(sc->bus_res, r, &mstart,
1117 &mend);
1118 if (error)
1119 goto done;
1120
1121 if (start < mstart || end > mend) {
1122 error = EINVAL;
1123 goto done;
1124 }
1125
1126 /* Fall through */
1127 done:
1128 if (!error)
1129 error = rman_adjust_resource(r, start, end);
1130
1131 BHNDB_UNLOCK(sc);
1132 return (error);
1133 }
1134
1135 /**
1136 * Initialize child resource @p r with a virtual address, tag, and handle
1137 * copied from @p parent, adjusted to contain only the range defined by
1138 * @p offsize and @p size.
1139 *
1140 * @param r The register to be initialized.
1141 * @param parent The parent bus resource that fully contains the subregion.
1142 * @param offset The subregion offset within @p parent.
1143 * @param size The subregion size.
1144 * @p r.
1145 */
1146 static int
bhndb_init_child_resource(struct resource * r,struct resource * parent,bhnd_size_t offset,bhnd_size_t size)1147 bhndb_init_child_resource(struct resource *r,
1148 struct resource *parent, bhnd_size_t offset, bhnd_size_t size)
1149 {
1150 bus_space_handle_t bh, child_bh;
1151 bus_space_tag_t bt;
1152 uintptr_t vaddr;
1153 int error;
1154
1155 /* Fetch the parent resource's real bus values */
1156 vaddr = (uintptr_t) rman_get_virtual(parent);
1157 bt = rman_get_bustag(parent);
1158 bh = rman_get_bushandle(parent);
1159
1160 /* Configure child resource with window-adjusted real bus values */
1161 vaddr += offset;
1162 error = bus_space_subregion(bt, bh, offset, size, &child_bh);
1163 if (error)
1164 return (error);
1165
1166 rman_set_virtual(r, (void *) vaddr);
1167 rman_set_bustag(r, bt);
1168 rman_set_bushandle(r, child_bh);
1169
1170 return (0);
1171 }
1172
1173 /**
1174 * Attempt activation of a fixed register window mapping for @p child.
1175 *
1176 * @param sc BHNDB device state.
1177 * @param region The static region definition capable of mapping @p r.
1178 * @param child A child requesting resource activation.
1179 * @param type Resource type.
1180 * @param rid Resource identifier.
1181 * @param r Resource to be activated.
1182 *
1183 * @retval 0 if @p r was activated successfully
1184 * @retval ENOENT if no fixed register window was found.
1185 * @retval non-zero if @p r could not be activated.
1186 */
1187 static int
bhndb_activate_static_region(struct bhndb_softc * sc,struct bhndb_region * region,device_t child,int type,int rid,struct resource * r)1188 bhndb_activate_static_region(struct bhndb_softc *sc,
1189 struct bhndb_region *region, device_t child, int type, int rid,
1190 struct resource *r)
1191 {
1192 struct resource *bridge_res;
1193 const struct bhndb_regwin *win;
1194 bhnd_size_t parent_offset;
1195 rman_res_t r_start, r_size;
1196 int error;
1197
1198 win = region->static_regwin;
1199
1200 KASSERT(win != NULL && BHNDB_REGWIN_T_IS_STATIC(win->win_type),
1201 ("can't activate non-static region"));
1202
1203 r_start = rman_get_start(r);
1204 r_size = rman_get_size(r);
1205
1206 /* Find the corresponding bridge resource */
1207 bridge_res = bhndb_host_resource_for_regwin(sc->bus_res->res, win);
1208 if (bridge_res == NULL)
1209 return (ENXIO);
1210
1211 /* Calculate subregion offset within the parent resource */
1212 parent_offset = r_start - region->addr;
1213 parent_offset += win->win_offset;
1214
1215 /* Configure resource with its real bus values. */
1216 error = bhndb_init_child_resource(r, bridge_res, parent_offset, r_size);
1217 if (error)
1218 return (error);
1219
1220 /* Mark active */
1221 if ((error = rman_activate_resource(r)))
1222 return (error);
1223
1224 return (0);
1225 }
1226
1227 /**
1228 * Attempt to allocate/retain a dynamic register window for @p r, returning
1229 * the retained window.
1230 *
1231 * @param sc The bhndb driver state.
1232 * @param r The resource for which a window will be retained.
1233 */
1234 static struct bhndb_dw_alloc *
bhndb_retain_dynamic_window(struct bhndb_softc * sc,struct resource * r)1235 bhndb_retain_dynamic_window(struct bhndb_softc *sc, struct resource *r)
1236 {
1237 struct bhndb_dw_alloc *dwa;
1238 rman_res_t r_start, r_size;
1239 int error;
1240
1241 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1242
1243 r_start = rman_get_start(r);
1244 r_size = rman_get_size(r);
1245
1246 /* Look for an existing dynamic window we can reference */
1247 dwa = bhndb_dw_find_mapping(sc->bus_res, r_start, r_size);
1248 if (dwa != NULL) {
1249 if (bhndb_dw_retain(sc->bus_res, dwa, r) == 0)
1250 return (dwa);
1251
1252 return (NULL);
1253 }
1254
1255 /* Otherwise, try to reserve a free window */
1256 dwa = bhndb_dw_next_free(sc->bus_res);
1257 if (dwa == NULL) {
1258 /* No free windows */
1259 return (NULL);
1260 }
1261
1262 /* Window must be large enough to map the entire resource */
1263 if (dwa->win->win_size < rman_get_size(r))
1264 return (NULL);
1265
1266 /* Set the window target */
1267 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, rman_get_start(r),
1268 rman_get_size(r));
1269 if (error) {
1270 device_printf(sc->dev, "dynamic window initialization "
1271 "for 0x%llx-0x%llx failed: %d\n",
1272 (unsigned long long) r_start,
1273 (unsigned long long) r_start + r_size - 1,
1274 error);
1275 return (NULL);
1276 }
1277
1278 /* Add our reservation */
1279 if (bhndb_dw_retain(sc->bus_res, dwa, r))
1280 return (NULL);
1281
1282 return (dwa);
1283 }
1284
1285 /**
1286 * Activate a resource using any viable static or dynamic register window.
1287 *
1288 * @param sc The bhndb driver state.
1289 * @param child The child holding ownership of @p r.
1290 * @param type The type of the resource to be activated.
1291 * @param rid The resource ID of @p r.
1292 * @param r The resource to be activated
1293 * @param[out] indirect On error and if not NULL, will be set to 'true' if
1294 * the caller should instead use an indirect resource mapping.
1295 *
1296 * @retval 0 success
1297 * @retval non-zero activation failed.
1298 */
1299 static int
bhndb_try_activate_resource(struct bhndb_softc * sc,device_t child,int type,int rid,struct resource * r,bool * indirect)1300 bhndb_try_activate_resource(struct bhndb_softc *sc, device_t child, int type,
1301 int rid, struct resource *r, bool *indirect)
1302 {
1303 struct bhndb_region *region;
1304 struct bhndb_dw_alloc *dwa;
1305 bhndb_priority_t dw_priority;
1306 rman_res_t r_start, r_size;
1307 rman_res_t parent_offset;
1308 int error;
1309
1310 BHNDB_LOCK_ASSERT(sc, MA_NOTOWNED);
1311
1312 if (indirect != NULL)
1313 *indirect = false;
1314
1315 switch (type) {
1316 case SYS_RES_IRQ:
1317 /* IRQ resources are always directly mapped */
1318 return (rman_activate_resource(r));
1319
1320 case SYS_RES_MEMORY:
1321 /* Handled below */
1322 break;
1323
1324 default:
1325 device_printf(sc->dev, "unsupported resource type %d\n", type);
1326 return (ENXIO);
1327 }
1328
1329 /* Only MMIO resources can be mapped via register windows */
1330 KASSERT(type == SYS_RES_MEMORY, ("invalid type: %d", type));
1331
1332 r_start = rman_get_start(r);
1333 r_size = rman_get_size(r);
1334
1335 /* Activate native addrspace resources using the host address space */
1336 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_NATIVE) {
1337 struct resource *parent;
1338
1339 /* Find the bridge resource referenced by the child */
1340 parent = bhndb_host_resource_for_range(sc->bus_res->res,
1341 type, r_start, r_size);
1342 if (parent == NULL) {
1343 device_printf(sc->dev, "host resource not found "
1344 "for 0x%llx-0x%llx\n",
1345 (unsigned long long) r_start,
1346 (unsigned long long) r_start + r_size - 1);
1347 return (ENOENT);
1348 }
1349
1350 /* Initialize child resource with the real bus values */
1351 error = bhndb_init_child_resource(r, parent,
1352 r_start - rman_get_start(parent), r_size);
1353 if (error)
1354 return (error);
1355
1356 /* Try to activate child resource */
1357 return (rman_activate_resource(r));
1358 }
1359
1360 /* Default to low priority */
1361 dw_priority = BHNDB_PRIORITY_LOW;
1362
1363 /* Look for a bus region matching the resource's address range */
1364 region = bhndb_find_resource_region(sc->bus_res, r_start, r_size);
1365 if (region != NULL)
1366 dw_priority = region->priority;
1367
1368 /* Prefer static mappings over consuming a dynamic windows. */
1369 if (region && region->static_regwin) {
1370 error = bhndb_activate_static_region(sc, region, child, type,
1371 rid, r);
1372 if (error)
1373 device_printf(sc->dev, "static window allocation "
1374 "for 0x%llx-0x%llx failed\n",
1375 (unsigned long long) r_start,
1376 (unsigned long long) r_start + r_size - 1);
1377 return (error);
1378 }
1379
1380 /* A dynamic window will be required; is this resource high enough
1381 * priority to be reserved a dynamic window? */
1382 if (dw_priority < sc->bus_res->min_prio) {
1383 if (indirect)
1384 *indirect = true;
1385
1386 return (ENOMEM);
1387 }
1388
1389 /* Find and retain a usable window */
1390 BHNDB_LOCK(sc); {
1391 dwa = bhndb_retain_dynamic_window(sc, r);
1392 } BHNDB_UNLOCK(sc);
1393
1394 if (dwa == NULL) {
1395 if (indirect)
1396 *indirect = true;
1397 return (ENOMEM);
1398 }
1399
1400 /* Configure resource with its real bus values. */
1401 parent_offset = dwa->win->win_offset;
1402 parent_offset += r_start - dwa->target;
1403
1404 error = bhndb_init_child_resource(r, dwa->parent_res, parent_offset,
1405 dwa->win->win_size);
1406 if (error)
1407 goto failed;
1408
1409 /* Mark active */
1410 if ((error = rman_activate_resource(r)))
1411 goto failed;
1412
1413 return (0);
1414
1415 failed:
1416 /* Release our region allocation. */
1417 BHNDB_LOCK(sc);
1418 bhndb_dw_release(sc->bus_res, dwa, r);
1419 BHNDB_UNLOCK(sc);
1420
1421 return (error);
1422 }
1423
1424 /**
1425 * Default bhndb(4) implementation of BUS_ACTIVATE_RESOURCE().
1426 */
1427 static int
bhndb_activate_resource(device_t dev,device_t child,int type,int rid,struct resource * r)1428 bhndb_activate_resource(device_t dev, device_t child, int type, int rid,
1429 struct resource *r)
1430 {
1431 struct bhndb_softc *sc = device_get_softc(dev);
1432
1433 /* Delegate directly to our parent device's bus if the requested
1434 * resource type isn't handled locally. */
1435 if (bhndb_get_rman(sc, child, type) == NULL) {
1436 return (BUS_ACTIVATE_RESOURCE(device_get_parent(sc->parent_dev),
1437 child, type, rid, r));
1438 }
1439
1440 return (bhndb_try_activate_resource(sc, child, type, rid, r, NULL));
1441 }
1442
1443 /**
1444 * Default bhndb(4) implementation of BUS_DEACTIVATE_RESOURCE().
1445 */
1446 static int
bhndb_deactivate_resource(device_t dev,device_t child,int type,int rid,struct resource * r)1447 bhndb_deactivate_resource(device_t dev, device_t child, int type,
1448 int rid, struct resource *r)
1449 {
1450 struct bhndb_dw_alloc *dwa;
1451 struct bhndb_softc *sc;
1452 struct rman *rm;
1453 int error;
1454
1455 sc = device_get_softc(dev);
1456
1457 /* Delegate directly to our parent device's bus if the requested
1458 * resource type isn't handled locally. */
1459 rm = bhndb_get_rman(sc, child, type);
1460 if (rm == NULL) {
1461 return (BUS_DEACTIVATE_RESOURCE(
1462 device_get_parent(sc->parent_dev), child, type, rid, r));
1463 }
1464
1465 /* Mark inactive */
1466 if ((error = rman_deactivate_resource(r)))
1467 return (error);
1468
1469 switch (type) {
1470 case SYS_RES_IRQ:
1471 /* No bridge-level state to be freed */
1472 return (0);
1473
1474 case SYS_RES_MEMORY:
1475 /* Free any dynamic window allocation. */
1476 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) {
1477 BHNDB_LOCK(sc);
1478 dwa = bhndb_dw_find_resource(sc->bus_res, r);
1479 if (dwa != NULL)
1480 bhndb_dw_release(sc->bus_res, dwa, r);
1481 BHNDB_UNLOCK(sc);
1482 }
1483
1484 return (0);
1485
1486 default:
1487 device_printf(dev, "unsupported resource type %d\n", type);
1488 return (ENXIO);
1489 }
1490 }
1491
1492 /**
1493 * Default bhndb(4) implementation of BUS_GET_RESOURCE_LIST().
1494 */
1495 static struct resource_list *
bhndb_get_resource_list(device_t dev,device_t child)1496 bhndb_get_resource_list(device_t dev, device_t child)
1497 {
1498 struct bhndb_devinfo *dinfo = device_get_ivars(child);
1499 return (&dinfo->resources);
1500 }
1501
1502 /**
1503 * Default bhndb(4) implementation of BHND_BUS_ACTIVATE_RESOURCE().
1504 *
1505 * For BHNDB_ADDRSPACE_NATIVE children, all resources are activated as direct
1506 * resources via BUS_ACTIVATE_RESOURCE().
1507 *
1508 * For BHNDB_ADDRSPACE_BRIDGED children, the resource priority is determined,
1509 * and if possible, the resource is activated as a direct resource. For example,
1510 * depending on resource priority and bridge resource availability, this
1511 * function will attempt to activate SYS_RES_MEMORY resources using either a
1512 * static register window, a dynamic register window, or it will configure @p r
1513 * as an indirect resource -- in that order.
1514 */
1515 static int
bhndb_activate_bhnd_resource(device_t dev,device_t child,int type,int rid,struct bhnd_resource * r)1516 bhndb_activate_bhnd_resource(device_t dev, device_t child,
1517 int type, int rid, struct bhnd_resource *r)
1518 {
1519 struct bhndb_softc *sc;
1520 struct bhndb_region *region;
1521 bhndb_priority_t r_prio;
1522 rman_res_t r_start, r_size;
1523 int error;
1524 bool indirect;
1525
1526 KASSERT(!r->direct,
1527 ("direct flag set on inactive resource"));
1528
1529 KASSERT(!(rman_get_flags(r->res) & RF_ACTIVE),
1530 ("RF_ACTIVE set on inactive resource"));
1531
1532 sc = device_get_softc(dev);
1533
1534 /* Delegate directly to BUS_ACTIVATE_RESOURCE() if the requested
1535 * resource type isn't handled locally. */
1536 if (bhndb_get_rman(sc, child, type) == NULL) {
1537 error = BUS_ACTIVATE_RESOURCE(dev, child, type, rid, r->res);
1538 if (error == 0)
1539 r->direct = true;
1540 return (error);
1541 }
1542
1543 r_start = rman_get_start(r->res);
1544 r_size = rman_get_size(r->res);
1545
1546 /* Determine the resource priority of bridged resources, and skip direct
1547 * allocation if the priority is too low. */
1548 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) {
1549 switch (type) {
1550 case SYS_RES_IRQ:
1551 /* IRQ resources are always direct */
1552 break;
1553
1554 case SYS_RES_MEMORY:
1555 region = bhndb_find_resource_region(sc->bus_res,
1556 r_start, r_size);
1557 if (region != NULL)
1558 r_prio = region->priority;
1559 else
1560 r_prio = BHNDB_PRIORITY_NONE;
1561
1562 /* If less than the minimum dynamic window priority,
1563 * this resource should always be indirect. */
1564 if (r_prio < sc->bus_res->min_prio)
1565 return (0);
1566
1567 break;
1568
1569 default:
1570 device_printf(dev, "unsupported resource type %d\n",
1571 type);
1572 return (ENXIO);
1573 }
1574 }
1575
1576 /* Attempt direct activation */
1577 error = bhndb_try_activate_resource(sc, child, type, rid, r->res,
1578 &indirect);
1579 if (!error) {
1580 r->direct = true;
1581 } else if (indirect) {
1582 /* The request was valid, but no viable register window is
1583 * available; indirection must be employed. */
1584 error = 0;
1585 r->direct = false;
1586 }
1587
1588 if (BHNDB_DEBUG(PRIO) &&
1589 bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED)
1590 {
1591 device_printf(child, "activated 0x%llx-0x%llx as %s "
1592 "resource\n",
1593 (unsigned long long) r_start,
1594 (unsigned long long) r_start + r_size - 1,
1595 r->direct ? "direct" : "indirect");
1596 }
1597
1598 return (error);
1599 }
1600
1601 /**
1602 * Default bhndb(4) implementation of BHND_BUS_DEACTIVATE_RESOURCE().
1603 */
1604 static int
bhndb_deactivate_bhnd_resource(device_t dev,device_t child,int type,int rid,struct bhnd_resource * r)1605 bhndb_deactivate_bhnd_resource(device_t dev, device_t child,
1606 int type, int rid, struct bhnd_resource *r)
1607 {
1608 int error;
1609
1610 /* Indirect resources don't require activation */
1611 if (!r->direct)
1612 return (0);
1613
1614 KASSERT(rman_get_flags(r->res) & RF_ACTIVE,
1615 ("RF_ACTIVE not set on direct resource"));
1616
1617 /* Perform deactivation */
1618 error = BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, r->res);
1619 if (!error)
1620 r->direct = false;
1621
1622 return (error);
1623 }
1624
1625 /**
1626 * Find the best available bridge resource allocation record capable of handling
1627 * bus I/O requests of @p size at @p addr.
1628 *
1629 * In order of preference, this function will either:
1630 *
1631 * - Configure and return a free allocation record
1632 * - Return an existing allocation record mapping the requested space, or
1633 * - Steal, configure, and return an in-use allocation record.
1634 *
1635 * Will panic if a usable record cannot be found.
1636 *
1637 * @param sc Bridge driver state.
1638 * @param addr The I/O target address.
1639 * @param size The size of the I/O operation to be performed at @p addr.
1640 * @param[out] borrowed Set to true if the allocation record was borrowed to
1641 * fulfill this request; the borrowed record maps the target address range,
1642 * and must not be modified.
1643 * @param[out] stolen Set to true if the allocation record was stolen to fulfill
1644 * this request. If a stolen allocation record is returned,
1645 * bhndb_io_resource_restore() must be called upon completion of the bus I/O
1646 * request.
1647 * @param[out] restore If the allocation record was stolen, this will be set
1648 * to the target that must be restored.
1649 */
1650 static struct bhndb_dw_alloc *
bhndb_io_resource_get_window(struct bhndb_softc * sc,bus_addr_t addr,bus_size_t size,bool * borrowed,bool * stolen,bus_addr_t * restore)1651 bhndb_io_resource_get_window(struct bhndb_softc *sc, bus_addr_t addr,
1652 bus_size_t size, bool *borrowed, bool *stolen, bus_addr_t *restore)
1653 {
1654 struct bhndb_resources *br;
1655 struct bhndb_dw_alloc *dwa;
1656 struct bhndb_region *region;
1657
1658 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1659
1660 br = sc->bus_res;
1661 *borrowed = false;
1662 *stolen = false;
1663
1664 /* Try to fetch a free window */
1665 if ((dwa = bhndb_dw_next_free(br)) != NULL)
1666 return (dwa);
1667
1668 /* Search for an existing dynamic mapping of this address range.
1669 * Static regions are not searched, as a statically mapped
1670 * region would never be allocated as an indirect resource. */
1671 for (size_t i = 0; i < br->dwa_count; i++) {
1672 const struct bhndb_regwin *win;
1673
1674 dwa = &br->dw_alloc[i];
1675 win = dwa->win;
1676
1677 KASSERT(win->win_type == BHNDB_REGWIN_T_DYN,
1678 ("invalid register window type"));
1679
1680 /* Verify the range */
1681 if (addr < dwa->target)
1682 continue;
1683
1684 if (addr + size > dwa->target + win->win_size)
1685 continue;
1686
1687 /* Found */
1688 *borrowed = true;
1689 return (dwa);
1690 }
1691
1692 /* Try to steal a window; this should only be required on very early
1693 * PCI_V0 (BCM4318, etc) Wi-Fi chipsets */
1694 region = bhndb_find_resource_region(br, addr, size);
1695 if (region == NULL)
1696 return (NULL);
1697
1698 if ((region->alloc_flags & BHNDB_ALLOC_FULFILL_ON_OVERCOMMIT) == 0)
1699 return (NULL);
1700
1701 /* Steal a window. This acquires our backing spinlock, disabling
1702 * interrupts; the spinlock will be released by
1703 * bhndb_dw_return_stolen() */
1704 if ((dwa = bhndb_dw_steal(br, restore)) != NULL) {
1705 *stolen = true;
1706 return (dwa);
1707 }
1708
1709 panic("register windows exhausted attempting to map 0x%llx-0x%llx\n",
1710 (unsigned long long) addr, (unsigned long long) addr+size-1);
1711 }
1712
1713 /**
1714 * Return a borrowed reference to a bridge resource allocation record capable
1715 * of handling bus I/O requests of @p size at @p addr.
1716 *
1717 * This will either return a reference to an existing allocation record mapping
1718 * the requested space, or will configure and return a free allocation record.
1719 *
1720 * Will panic if a usable record cannot be found.
1721 *
1722 * @param sc Bridge driver state.
1723 * @param addr The I/O target address.
1724 * @param size The size of the I/O operation to be performed at @p addr.
1725 * @param[out] offset The offset within the returned resource at which
1726 * to perform the I/O request.
1727 * @param[out] stolen Set to true if the allocation record was stolen to fulfill
1728 * this request. If a stolen allocation record is returned,
1729 * bhndb_io_resource_restore() must be called upon completion of the bus I/O
1730 * request.
1731 * @param[out] restore If the allocation record was stolen, this will be set
1732 * to the target that must be restored.
1733 */
1734 static inline struct bhndb_dw_alloc *
bhndb_io_resource(struct bhndb_softc * sc,bus_addr_t addr,bus_size_t size,bus_size_t * offset,bool * stolen,bus_addr_t * restore)1735 bhndb_io_resource(struct bhndb_softc *sc, bus_addr_t addr, bus_size_t size,
1736 bus_size_t *offset, bool *stolen, bus_addr_t *restore)
1737 {
1738 struct bhndb_dw_alloc *dwa;
1739 bool borrowed;
1740 int error;
1741
1742 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1743
1744 dwa = bhndb_io_resource_get_window(sc, addr, size, &borrowed, stolen,
1745 restore);
1746
1747 /* Adjust the window if the I/O request won't fit in the current
1748 * target range. */
1749 if (addr < dwa->target ||
1750 addr > dwa->target + dwa->win->win_size ||
1751 (dwa->target + dwa->win->win_size) - addr < size)
1752 {
1753 /* Cannot modify target of borrowed windows */
1754 if (borrowed) {
1755 panic("borrowed register window does not map expected "
1756 "range 0x%llx-0x%llx\n",
1757 (unsigned long long) addr,
1758 (unsigned long long) addr+size-1);
1759 }
1760
1761 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, addr,
1762 size);
1763 if (error) {
1764 panic("failed to set register window target mapping "
1765 "0x%llx-0x%llx\n",
1766 (unsigned long long) addr,
1767 (unsigned long long) addr+size-1);
1768 }
1769 }
1770
1771 /* Calculate the offset and return */
1772 *offset = (addr - dwa->target) + dwa->win->win_offset;
1773 return (dwa);
1774 }
1775
1776 /*
1777 * BHND_BUS_(READ|WRITE_* implementations
1778 */
1779
1780 /* bhndb_bus_(read|write) common implementation */
1781 #define BHNDB_IO_COMMON_SETUP(_io_size) \
1782 struct bhndb_softc *sc; \
1783 struct bhndb_dw_alloc *dwa; \
1784 struct resource *io_res; \
1785 bus_size_t io_offset; \
1786 bus_addr_t restore; \
1787 bool stolen; \
1788 \
1789 sc = device_get_softc(dev); \
1790 \
1791 BHNDB_LOCK(sc); \
1792 dwa = bhndb_io_resource(sc, rman_get_start(r->res) + \
1793 offset, _io_size, &io_offset, &stolen, &restore); \
1794 io_res = dwa->parent_res; \
1795 \
1796 KASSERT(!r->direct, \
1797 ("bhnd_bus slow path used for direct resource")); \
1798 \
1799 KASSERT(rman_get_flags(io_res) & RF_ACTIVE, \
1800 ("i/o resource is not active"));
1801
1802 #define BHNDB_IO_COMMON_TEARDOWN() \
1803 if (stolen) { \
1804 bhndb_dw_return_stolen(sc->dev, sc->bus_res, \
1805 dwa, restore); \
1806 } \
1807 BHNDB_UNLOCK(sc);
1808
1809 /* Defines a bhndb_bus_read_* method implementation */
1810 #define BHNDB_IO_READ(_type, _name) \
1811 static _type \
1812 bhndb_bus_read_ ## _name (device_t dev, device_t child, \
1813 struct bhnd_resource *r, bus_size_t offset) \
1814 { \
1815 _type v; \
1816 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \
1817 v = bus_read_ ## _name (io_res, io_offset); \
1818 BHNDB_IO_COMMON_TEARDOWN(); \
1819 \
1820 return (v); \
1821 }
1822
1823 /* Defines a bhndb_bus_write_* method implementation */
1824 #define BHNDB_IO_WRITE(_type, _name) \
1825 static void \
1826 bhndb_bus_write_ ## _name (device_t dev, device_t child, \
1827 struct bhnd_resource *r, bus_size_t offset, _type value) \
1828 { \
1829 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \
1830 bus_write_ ## _name (io_res, io_offset, value); \
1831 BHNDB_IO_COMMON_TEARDOWN(); \
1832 }
1833
1834 /* Defines a bhndb_bus_(read|write|set)_(multi|region)_* method */
1835 #define BHNDB_IO_MISC(_type, _ptr, _op, _size) \
1836 static void \
1837 bhndb_bus_ ## _op ## _ ## _size (device_t dev, \
1838 device_t child, struct bhnd_resource *r, bus_size_t offset, \
1839 _type _ptr datap, bus_size_t count) \
1840 { \
1841 BHNDB_IO_COMMON_SETUP(sizeof(_type) * count); \
1842 bus_ ## _op ## _ ## _size (io_res, io_offset, \
1843 datap, count); \
1844 BHNDB_IO_COMMON_TEARDOWN(); \
1845 }
1846
1847 /* Defines a complete set of read/write methods */
1848 #define BHNDB_IO_METHODS(_type, _size) \
1849 BHNDB_IO_READ(_type, _size) \
1850 BHNDB_IO_WRITE(_type, _size) \
1851 \
1852 BHNDB_IO_READ(_type, stream_ ## _size) \
1853 BHNDB_IO_WRITE(_type, stream_ ## _size) \
1854 \
1855 BHNDB_IO_MISC(_type, *, read_multi, _size) \
1856 BHNDB_IO_MISC(_type, *, write_multi, _size) \
1857 \
1858 BHNDB_IO_MISC(_type, *, read_multi_stream, _size) \
1859 BHNDB_IO_MISC(_type, *, write_multi_stream, _size) \
1860 \
1861 BHNDB_IO_MISC(_type, , set_multi, _size) \
1862 BHNDB_IO_MISC(_type, , set_region, _size) \
1863 BHNDB_IO_MISC(_type, *, read_region, _size) \
1864 BHNDB_IO_MISC(_type, *, write_region, _size) \
1865 \
1866 BHNDB_IO_MISC(_type, *, read_region_stream, _size) \
1867 BHNDB_IO_MISC(_type, *, write_region_stream, _size)
1868
1869 BHNDB_IO_METHODS(uint8_t, 1);
1870 BHNDB_IO_METHODS(uint16_t, 2);
1871 BHNDB_IO_METHODS(uint32_t, 4);
1872
1873 /**
1874 * Default bhndb(4) implementation of BHND_BUS_BARRIER().
1875 */
1876 static void
bhndb_bus_barrier(device_t dev,device_t child,struct bhnd_resource * r,bus_size_t offset,bus_size_t length,int flags)1877 bhndb_bus_barrier(device_t dev, device_t child, struct bhnd_resource *r,
1878 bus_size_t offset, bus_size_t length, int flags)
1879 {
1880 BHNDB_IO_COMMON_SETUP(length);
1881
1882 bus_barrier(io_res, io_offset + offset, length, flags);
1883
1884 BHNDB_IO_COMMON_TEARDOWN();
1885 }
1886
1887 /**
1888 * Default bhndb(4) implementation of BHND_MAP_INTR().
1889 */
1890 static int
bhndb_bhnd_map_intr(device_t dev,device_t child,u_int intr,rman_res_t * irq)1891 bhndb_bhnd_map_intr(device_t dev, device_t child, u_int intr, rman_res_t *irq)
1892 {
1893 u_int ivec;
1894 int error;
1895
1896 /* Is the intr valid? */
1897 if (intr >= bhnd_get_intr_count(child))
1898 return (EINVAL);
1899
1900 /* Fetch the interrupt vector */
1901 if ((error = bhnd_get_intr_ivec(child, intr, &ivec)))
1902 return (error);
1903
1904 /* Map directly to the actual backplane interrupt vector */
1905 *irq = ivec;
1906
1907 return (0);
1908 }
1909
1910 /**
1911 * Default bhndb(4) implementation of BHND_UNMAP_INTR().
1912 */
1913 static void
bhndb_bhnd_unmap_intr(device_t dev,device_t child,rman_res_t irq)1914 bhndb_bhnd_unmap_intr(device_t dev, device_t child, rman_res_t irq)
1915 {
1916 /* No state to clean up */
1917 }
1918
1919 /**
1920 * Default bhndb(4) implementation of BUS_SETUP_INTR().
1921 */
1922 static int
bhndb_setup_intr(device_t dev,device_t child,struct resource * r,int flags,driver_filter_t filter,driver_intr_t handler,void * arg,void ** cookiep)1923 bhndb_setup_intr(device_t dev, device_t child, struct resource *r,
1924 int flags, driver_filter_t filter, driver_intr_t handler, void *arg,
1925 void **cookiep)
1926 {
1927 struct bhndb_softc *sc;
1928 struct bhndb_intr_isrc *isrc;
1929 struct bhndb_intr_handler *ih;
1930 int error;
1931
1932 sc = device_get_softc(dev);
1933
1934 /* Fetch the isrc */
1935 if ((error = BHNDB_MAP_INTR_ISRC(dev, r, &isrc))) {
1936 device_printf(dev, "failed to fetch isrc: %d\n", error);
1937 return (error);
1938 }
1939
1940 /* Allocate new ihandler entry */
1941 ih = bhndb_alloc_intr_handler(child, r, isrc);
1942 if (ih == NULL)
1943 return (ENOMEM);
1944
1945 /* Perform actual interrupt setup via the host isrc */
1946 error = bus_setup_intr(isrc->is_owner, isrc->is_res, flags, filter,
1947 handler, arg, &ih->ih_cookiep);
1948 if (error) {
1949 bhndb_free_intr_handler(ih);
1950 return (error);
1951 }
1952
1953 /* Add to our interrupt handler list */
1954 BHNDB_LOCK(sc);
1955 bhndb_register_intr_handler(sc->bus_res, ih);
1956 BHNDB_UNLOCK(sc);
1957
1958 /* Provide the interrupt handler entry as our cookiep value */
1959 *cookiep = ih;
1960 return (0);
1961 }
1962
1963 /**
1964 * Default bhndb(4) implementation of BUS_TEARDOWN_INTR().
1965 */
1966 static int
bhndb_teardown_intr(device_t dev,device_t child,struct resource * r,void * cookiep)1967 bhndb_teardown_intr(device_t dev, device_t child, struct resource *r,
1968 void *cookiep)
1969 {
1970 struct bhndb_softc *sc;
1971 struct bhndb_intr_handler *ih;
1972 struct bhndb_intr_isrc *isrc;
1973 int error;
1974
1975 sc = device_get_softc(dev);
1976
1977 /* Locate and claim ownership of the interrupt handler entry */
1978 BHNDB_LOCK(sc);
1979
1980 ih = bhndb_find_intr_handler(sc->bus_res, cookiep);
1981 if (ih == NULL) {
1982 panic("%s requested teardown of invalid cookiep %p",
1983 device_get_nameunit(child), cookiep);
1984 }
1985
1986 bhndb_deregister_intr_handler(sc->bus_res, ih);
1987
1988 BHNDB_UNLOCK(sc);
1989
1990 /* Perform actual interrupt teardown via the host isrc */
1991 isrc = ih->ih_isrc;
1992 error = bus_teardown_intr(isrc->is_owner, isrc->is_res, ih->ih_cookiep);
1993 if (error) {
1994 /* If teardown fails, we need to reinsert the handler entry
1995 * to allow later teardown */
1996 BHNDB_LOCK(sc);
1997 bhndb_register_intr_handler(sc->bus_res, ih);
1998 BHNDB_UNLOCK(sc);
1999
2000 return (error);
2001 }
2002
2003 /* Free the entry */
2004 bhndb_free_intr_handler(ih);
2005 return (0);
2006 }
2007
2008 /**
2009 * Default bhndb(4) implementation of BUS_BIND_INTR().
2010 */
2011 static int
bhndb_bind_intr(device_t dev,device_t child,struct resource * irq,int cpu)2012 bhndb_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
2013 {
2014 struct bhndb_softc *sc;
2015 struct bhndb_intr_handler *ih;
2016 struct bhndb_intr_isrc *isrc;
2017
2018 sc = device_get_softc(dev);
2019 isrc = NULL;
2020
2021 /* Fetch the isrc corresponding to the child IRQ resource */
2022 BHNDB_LOCK(sc);
2023 STAILQ_FOREACH(ih, &sc->bus_res->bus_intrs, ih_link) {
2024 if (ih->ih_res == irq) {
2025 isrc = ih->ih_isrc;
2026 break;
2027 }
2028 }
2029 BHNDB_UNLOCK(sc);
2030
2031 if (isrc == NULL) {
2032 panic("%s requested bind of invalid irq %#jx-%#jx",
2033 device_get_nameunit(child), rman_get_start(irq),
2034 rman_get_end(irq));
2035 }
2036
2037 /* Perform actual bind via the host isrc */
2038 return (bus_bind_intr(isrc->is_owner, isrc->is_res, cpu));
2039 }
2040
2041 /**
2042 * Default bhndb(4) implementation of BUS_DESCRIBE_INTR().
2043 */
2044 static int
bhndb_describe_intr(device_t dev,device_t child,struct resource * irq,void * cookie,const char * descr)2045 bhndb_describe_intr(device_t dev, device_t child, struct resource *irq,
2046 void *cookie, const char *descr)
2047 {
2048 struct bhndb_softc *sc;
2049 struct bhndb_intr_handler *ih;
2050 struct bhndb_intr_isrc *isrc;
2051
2052 sc = device_get_softc(dev);
2053
2054 /* Locate the interrupt handler entry; the caller owns the handler
2055 * reference, and thus our entry is guaranteed to remain valid after
2056 * we drop out lock below. */
2057 BHNDB_LOCK(sc);
2058
2059 ih = bhndb_find_intr_handler(sc->bus_res, cookie);
2060 if (ih == NULL) {
2061 panic("%s requested invalid cookiep %p",
2062 device_get_nameunit(child), cookie);
2063 }
2064
2065 isrc = ih->ih_isrc;
2066
2067 BHNDB_UNLOCK(sc);
2068
2069 /* Perform the actual request via the host isrc */
2070 return (BUS_DESCRIBE_INTR(device_get_parent(isrc->is_owner),
2071 isrc->is_owner, isrc->is_res, ih->ih_cookiep, descr));
2072 }
2073
2074 /**
2075 * Default bhndb(4) implementation of BUS_CONFIG_INTR().
2076 */
2077 static int
bhndb_config_intr(device_t dev,int irq,enum intr_trigger trig,enum intr_polarity pol)2078 bhndb_config_intr(device_t dev, int irq, enum intr_trigger trig,
2079 enum intr_polarity pol)
2080 {
2081 /* Unsupported */
2082 return (ENXIO);
2083 }
2084
2085 /**
2086 * Default bhndb(4) implementation of BUS_REMAP_INTR().
2087 */
2088 static int
bhndb_remap_intr(device_t dev,device_t child,u_int irq)2089 bhndb_remap_intr(device_t dev, device_t child, u_int irq)
2090 {
2091 /* Unsupported */
2092 return (ENXIO);
2093 }
2094
2095 /**
2096 * Default bhndb(4) implementation of BHND_BUS_GET_DMA_TRANSLATION().
2097 */
2098 static inline int
bhndb_get_dma_translation(device_t dev,device_t child,u_int width,uint32_t flags,bus_dma_tag_t * dmat,struct bhnd_dma_translation * translation)2099 bhndb_get_dma_translation(device_t dev, device_t child, u_int width,
2100 uint32_t flags, bus_dma_tag_t *dmat,
2101 struct bhnd_dma_translation *translation)
2102 {
2103 struct bhndb_softc *sc;
2104 const struct bhndb_hwcfg *hwcfg;
2105 const struct bhnd_dma_translation *match;
2106 bus_dma_tag_t match_dmat;
2107 bhnd_addr_t addr_mask, match_addr_mask;
2108
2109 sc = device_get_softc(dev);
2110 hwcfg = sc->bus_res->cfg;
2111
2112 /* Is DMA supported? */
2113 if (sc->bus_res->res->dma_tags == NULL)
2114 return (ENODEV);
2115
2116 /* Is the requested width supported? */
2117 if (width > BHND_DMA_ADDR_32BIT) {
2118 /* Backplane must support 64-bit addressing */
2119 if (!(sc->chipid.chip_caps & BHND_CAP_BP64))
2120 width = BHND_DMA_ADDR_32BIT;
2121 }
2122
2123 /* Find the best matching descriptor for the requested width */
2124 addr_mask = BHND_DMA_ADDR_BITMASK(width);
2125
2126 match = NULL;
2127 match_addr_mask = 0x0;
2128 match_dmat = NULL;
2129
2130 for (size_t i = 0; i < sc->bus_res->res->num_dma_tags; i++) {
2131 const struct bhnd_dma_translation *dwin;
2132 bhnd_addr_t masked;
2133
2134 dwin = &hwcfg->dma_translations[i];
2135
2136 /* The base address must be device addressable */
2137 if ((dwin->base_addr & addr_mask) != dwin->base_addr)
2138 continue;
2139
2140 /* The flags must match */
2141 if ((dwin->flags & flags) != flags)
2142 continue;
2143
2144 /* The window must cover at least part of our addressable
2145 * range */
2146 masked = (dwin->addr_mask | dwin->addrext_mask) & addr_mask;
2147 if (masked == 0)
2148 continue;
2149
2150 /* Is this a better match? */
2151 if (match == NULL || masked > match_addr_mask) {
2152 match = dwin;
2153 match_addr_mask = masked;
2154 match_dmat = sc->bus_res->res->dma_tags[i];
2155 }
2156 }
2157
2158 if (match == NULL || match_addr_mask == 0)
2159 return (ENOENT);
2160
2161 if (dmat != NULL)
2162 *dmat = match_dmat;
2163
2164 if (translation != NULL)
2165 *translation = *match;
2166
2167 return (0);
2168 }
2169
2170 /**
2171 * Default bhndb(4) implementation of BUS_GET_DMA_TAG().
2172 */
2173 static bus_dma_tag_t
bhndb_get_dma_tag(device_t dev,device_t child)2174 bhndb_get_dma_tag(device_t dev, device_t child)
2175 {
2176 struct bhndb_softc *sc = device_get_softc(dev);
2177
2178 /*
2179 * A bridge may have multiple DMA translation descriptors, each with
2180 * their own incompatible restrictions; drivers should in general call
2181 * BHND_BUS_GET_DMA_TRANSLATION() to fetch both the best available DMA
2182 * translation, and its corresponding DMA tag.
2183 *
2184 * Child drivers that do not use BHND_BUS_GET_DMA_TRANSLATION() are
2185 * responsible for creating their own restricted DMA tag; since we
2186 * cannot do this for them in BUS_GET_DMA_TAG(), we simply return the
2187 * bridge parent's DMA tag directly;
2188 */
2189 return (bus_get_dma_tag(sc->parent_dev));
2190 }
2191
2192 static device_method_t bhndb_methods[] = {
2193 /* Device interface */ \
2194 DEVMETHOD(device_probe, bhndb_generic_probe),
2195 DEVMETHOD(device_detach, bhndb_generic_detach),
2196 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2197 DEVMETHOD(device_suspend, bhndb_generic_suspend),
2198 DEVMETHOD(device_resume, bhndb_generic_resume),
2199
2200 /* Bus interface */
2201 DEVMETHOD(bus_probe_nomatch, bhndb_probe_nomatch),
2202 DEVMETHOD(bus_print_child, bhndb_print_child),
2203 DEVMETHOD(bus_child_location, bhndb_child_location),
2204 DEVMETHOD(bus_add_child, bhndb_add_child),
2205 DEVMETHOD(bus_child_deleted, bhndb_child_deleted),
2206
2207 DEVMETHOD(bus_alloc_resource, bhndb_alloc_resource),
2208 DEVMETHOD(bus_release_resource, bhndb_release_resource),
2209 DEVMETHOD(bus_activate_resource, bhndb_activate_resource),
2210 DEVMETHOD(bus_deactivate_resource, bhndb_deactivate_resource),
2211
2212 DEVMETHOD(bus_setup_intr, bhndb_setup_intr),
2213 DEVMETHOD(bus_teardown_intr, bhndb_teardown_intr),
2214 DEVMETHOD(bus_config_intr, bhndb_config_intr),
2215 DEVMETHOD(bus_bind_intr, bhndb_bind_intr),
2216 DEVMETHOD(bus_describe_intr, bhndb_describe_intr),
2217 DEVMETHOD(bus_remap_intr, bhndb_remap_intr),
2218
2219 DEVMETHOD(bus_get_dma_tag, bhndb_get_dma_tag),
2220
2221 DEVMETHOD(bus_adjust_resource, bhndb_adjust_resource),
2222 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
2223 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
2224 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
2225 DEVMETHOD(bus_get_resource_list, bhndb_get_resource_list),
2226
2227 DEVMETHOD(bus_read_ivar, bhndb_read_ivar),
2228 DEVMETHOD(bus_write_ivar, bhndb_write_ivar),
2229
2230 /* BHNDB interface */
2231 DEVMETHOD(bhndb_get_chipid, bhndb_get_chipid),
2232 DEVMETHOD(bhndb_is_core_disabled, bhndb_is_core_disabled),
2233 DEVMETHOD(bhndb_get_hostb_core, bhndb_get_hostb_core),
2234 DEVMETHOD(bhndb_suspend_resource, bhndb_suspend_resource),
2235 DEVMETHOD(bhndb_resume_resource, bhndb_resume_resource),
2236
2237 /* BHND interface */
2238 DEVMETHOD(bhnd_bus_get_chipid, bhndb_get_chipid),
2239 DEVMETHOD(bhnd_bus_activate_resource, bhndb_activate_bhnd_resource),
2240 DEVMETHOD(bhnd_bus_deactivate_resource, bhndb_deactivate_bhnd_resource),
2241 DEVMETHOD(bhnd_bus_get_nvram_var, bhnd_bus_generic_get_nvram_var),
2242 DEVMETHOD(bhnd_bus_map_intr, bhndb_bhnd_map_intr),
2243 DEVMETHOD(bhnd_bus_unmap_intr, bhndb_bhnd_unmap_intr),
2244 DEVMETHOD(bhnd_bus_get_dma_translation, bhndb_get_dma_translation),
2245
2246 DEVMETHOD(bhnd_bus_get_service_registry,bhndb_get_service_registry),
2247 DEVMETHOD(bhnd_bus_register_provider, bhnd_bus_generic_sr_register_provider),
2248 DEVMETHOD(bhnd_bus_deregister_provider, bhnd_bus_generic_sr_deregister_provider),
2249 DEVMETHOD(bhnd_bus_retain_provider, bhnd_bus_generic_sr_retain_provider),
2250 DEVMETHOD(bhnd_bus_release_provider, bhnd_bus_generic_sr_release_provider),
2251
2252 DEVMETHOD(bhnd_bus_read_1, bhndb_bus_read_1),
2253 DEVMETHOD(bhnd_bus_read_2, bhndb_bus_read_2),
2254 DEVMETHOD(bhnd_bus_read_4, bhndb_bus_read_4),
2255 DEVMETHOD(bhnd_bus_write_1, bhndb_bus_write_1),
2256 DEVMETHOD(bhnd_bus_write_2, bhndb_bus_write_2),
2257 DEVMETHOD(bhnd_bus_write_4, bhndb_bus_write_4),
2258
2259 DEVMETHOD(bhnd_bus_read_stream_1, bhndb_bus_read_stream_1),
2260 DEVMETHOD(bhnd_bus_read_stream_2, bhndb_bus_read_stream_2),
2261 DEVMETHOD(bhnd_bus_read_stream_4, bhndb_bus_read_stream_4),
2262 DEVMETHOD(bhnd_bus_write_stream_1, bhndb_bus_write_stream_1),
2263 DEVMETHOD(bhnd_bus_write_stream_2, bhndb_bus_write_stream_2),
2264 DEVMETHOD(bhnd_bus_write_stream_4, bhndb_bus_write_stream_4),
2265
2266 DEVMETHOD(bhnd_bus_read_multi_1, bhndb_bus_read_multi_1),
2267 DEVMETHOD(bhnd_bus_read_multi_2, bhndb_bus_read_multi_2),
2268 DEVMETHOD(bhnd_bus_read_multi_4, bhndb_bus_read_multi_4),
2269 DEVMETHOD(bhnd_bus_write_multi_1, bhndb_bus_write_multi_1),
2270 DEVMETHOD(bhnd_bus_write_multi_2, bhndb_bus_write_multi_2),
2271 DEVMETHOD(bhnd_bus_write_multi_4, bhndb_bus_write_multi_4),
2272
2273 DEVMETHOD(bhnd_bus_read_multi_stream_1, bhndb_bus_read_multi_stream_1),
2274 DEVMETHOD(bhnd_bus_read_multi_stream_2, bhndb_bus_read_multi_stream_2),
2275 DEVMETHOD(bhnd_bus_read_multi_stream_4, bhndb_bus_read_multi_stream_4),
2276 DEVMETHOD(bhnd_bus_write_multi_stream_1,bhndb_bus_write_multi_stream_1),
2277 DEVMETHOD(bhnd_bus_write_multi_stream_2,bhndb_bus_write_multi_stream_2),
2278 DEVMETHOD(bhnd_bus_write_multi_stream_4,bhndb_bus_write_multi_stream_4),
2279
2280 DEVMETHOD(bhnd_bus_set_multi_1, bhndb_bus_set_multi_1),
2281 DEVMETHOD(bhnd_bus_set_multi_2, bhndb_bus_set_multi_2),
2282 DEVMETHOD(bhnd_bus_set_multi_4, bhndb_bus_set_multi_4),
2283 DEVMETHOD(bhnd_bus_set_region_1, bhndb_bus_set_region_1),
2284 DEVMETHOD(bhnd_bus_set_region_2, bhndb_bus_set_region_2),
2285 DEVMETHOD(bhnd_bus_set_region_4, bhndb_bus_set_region_4),
2286
2287 DEVMETHOD(bhnd_bus_read_region_1, bhndb_bus_read_region_1),
2288 DEVMETHOD(bhnd_bus_read_region_2, bhndb_bus_read_region_2),
2289 DEVMETHOD(bhnd_bus_read_region_4, bhndb_bus_read_region_4),
2290 DEVMETHOD(bhnd_bus_write_region_1, bhndb_bus_write_region_1),
2291 DEVMETHOD(bhnd_bus_write_region_2, bhndb_bus_write_region_2),
2292 DEVMETHOD(bhnd_bus_write_region_4, bhndb_bus_write_region_4),
2293
2294 DEVMETHOD(bhnd_bus_read_region_stream_1,bhndb_bus_read_region_stream_1),
2295 DEVMETHOD(bhnd_bus_read_region_stream_2,bhndb_bus_read_region_stream_2),
2296 DEVMETHOD(bhnd_bus_read_region_stream_4,bhndb_bus_read_region_stream_4),
2297 DEVMETHOD(bhnd_bus_write_region_stream_1,bhndb_bus_write_region_stream_1),
2298 DEVMETHOD(bhnd_bus_write_region_stream_2,bhndb_bus_write_region_stream_2),
2299 DEVMETHOD(bhnd_bus_write_region_stream_4,bhndb_bus_write_region_stream_4),
2300
2301 DEVMETHOD(bhnd_bus_barrier, bhndb_bus_barrier),
2302
2303 DEVMETHOD_END
2304 };
2305
2306 DEFINE_CLASS_0(bhndb, bhndb_driver, bhndb_methods, sizeof(struct bhndb_softc));
2307
2308 MODULE_VERSION(bhndb, 1);
2309 MODULE_DEPEND(bhndb, bhnd, 1, 1, 1);
2310