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