--- //depot/vendor/freebsd/src/sys/dev/acpica/acpi.c 2007/09/13 01:46:22 +++ //depot/user/jhb/acpipci/dev/acpica/acpi.c 2007/10/08 18:46:03 @@ -153,6 +153,8 @@ char *buf, size_t buflen); static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, char *buf, size_t buflen); +static void acpi_hint_device_unit(device_t acdev, device_t child, + const char *name, int *unitp); static device_method_t acpi_methods[] = { /* Device interface */ @@ -183,6 +185,7 @@ DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), + DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit), /* ACPI bus */ DEVMETHOD(acpi_id_probe, acpi_device_id_probe), @@ -936,6 +939,89 @@ return (&ad->ad_rl); } +static int +acpi_match_resource_hint(device_t dev, int type, long value) +{ + struct acpi_device *ad = device_get_ivars(dev); + struct resource_list *rl = &ad->ad_rl; + struct resource_list_entry *rle; + + STAILQ_FOREACH(rle, rl, link) { + if (rle->type != type) + continue; + if (rle->start <= value && rle->end >= value) + return (1); + } + return (0); +} + +/* + * Wire device unit numbers based on resource matches in hints. + */ +static void +acpi_hint_device_unit(device_t acdev, device_t child, const char *name, + int *unitp) +{ + const char *s; + long value; + int line, matches, unit; + + /* + * Iterate over all the hints for the devices with the specified + * name to see if one's resources are a subset of this device. + */ + line = 0; + for (;;) { + if (resource_find_dev(&line, name, &unit, "at", NULL) != 0) + break; + + /* Must have an "at" for acpi or isa. */ + resource_string_value(name, unit, "at", &s); + if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 || + strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0)) + continue; + + /* + * Check for matching resources. We must have at least one, + * and all resources specified have to match. + * + * XXX: We may want to revisit this to be more lenient and wire + * as long as it gets one match. + */ + matches = 0; + if (resource_long_value(name, unit, "port", &value) == 0) { + if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value)) + matches++; + else + continue; + } + if (resource_long_value(name, unit, "maddr", &value) == 0) { + if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value)) + matches++; + else + continue; + } + if (resource_long_value(name, unit, "irq", &value) == 0) { + if (acpi_match_resource_hint(child, SYS_RES_IRQ, value)) + matches++; + else + continue; + } + if (resource_long_value(name, unit, "drq", &value) == 0) { + if (acpi_match_resource_hint(child, SYS_RES_DRQ, value)) + matches++; + else + continue; + } + + if (matches > 0) { + /* We have a winner! */ + *unitp = unit; + break; + } + } +} + /* * Pre-allocate/manage all memory and IO resources. Since rman can't handle * duplicates, we merge any in the sysresource attach routine. --- //depot/vendor/freebsd/src/sys/dev/sio/sio_pci.c 2007/03/29 04:32:10 +++ //depot/user/jhb/acpipci/dev/sio/sio_pci.c 2007/09/24 17:40:11 @@ -43,7 +43,6 @@ #include static int sio_pci_attach(device_t dev); -static void sio_pci_kludge_unit(device_t dev); static int sio_pci_probe(device_t dev); static device_method_t sio_pci_methods[] = { @@ -101,39 +100,9 @@ id++; if (id->desc == NULL) return (ENXIO); - sio_pci_kludge_unit(dev); return (sioattach(dev, id->rid, 0UL)); } -/* - * Don't cut and paste this to other drivers. It is a horrible kludge - * which will fail to work and also be unnecessary in future versions. - */ -static void -sio_pci_kludge_unit(dev) - device_t dev; -{ - devclass_t dc; - int err; - int start; - int unit; - - unit = 0; - start = 0; - while (resource_int_value("sio", unit, "port", &start) == 0 && - start > 0) - unit++; - if (device_get_unit(dev) < unit) { - dc = device_get_devclass(dev); - while (devclass_get_device(dc, unit)) - unit++; - device_printf(dev, "moving to sio%d\n", unit); - err = device_set_unit(dev, unit); /* EVIL DO NOT COPY */ - if (err) - device_printf(dev, "error moving device %d\n", err); - } -} - static int sio_pci_probe(dev) device_t dev; --- //depot/vendor/freebsd/src/sys/kern/bus_if.m 2007/02/23 12:24:01 +++ //depot/user/jhb/acpipci/kern/bus_if.m 2007/09/24 17:40:11 @@ -527,7 +527,7 @@ */ METHOD void hinted_child { device_t _dev; - const char * _dname; + const char *_dname; int _dunit; }; @@ -541,3 +541,19 @@ device_t _dev; device_t _child; } DEFAULT bus_generic_get_dma_tag; + +/** + * @brief Allow the bus to determine the unit number of a device. + * + * @param _dev the parent device of @p _child + * @param _child the device whose unit is to be wired + * @param _name the name of the device's new devclass + * @param _unitp a pointer to the device's new unit value + */ +METHOD void hint_device_unit { + device_t _dev; + device_t _child; + const char *_name; + int *_unitp; +}; + --- //depot/vendor/freebsd/src/sys/kern/subr_bus.c 2007/07/27 12:03:05 +++ //depot/user/jhb/acpipci/kern/subr_bus.c 2007/09/24 17:40:11 @@ -1293,12 +1293,18 @@ * @retval ENOMEM memory allocation failure */ static int -devclass_alloc_unit(devclass_t dc, int *unitp) +devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp) { + const char *s; int unit = *unitp; PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); + /* Ask the parent bus if it wants to wire this device. */ + if (unit == -1) + BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name, + &unit); + /* If we were given a wired unit number, check for existing device */ /* XXX imp XXX */ if (unit != -1) { @@ -1312,8 +1318,18 @@ } else { /* Unwired device, find the next available slot for it */ unit = 0; - while (unit < dc->maxunit && dc->devices[unit] != NULL) - unit++; + for (unit = 0;; unit++) { + /* If there is an "at" hint for a unit then skip it. */ + if (resource_string_value(dc->name, unit, "at", &s) == + 0) + continue; + + /* If this device slot is already in use, skip it. */ + if (unit < dc->maxunit && dc->devices[unit] != NULL) + continue; + + break; + } } /* @@ -1373,7 +1389,7 @@ if (!dev->nameunit) return (ENOMEM); - if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { + if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) { free(dev->nameunit, M_BUS); dev->nameunit = NULL; return (error);