From nobody Fri Feb 09 18:44:25 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TWjST6XV6z5B89s; Fri, 9 Feb 2024 18:44:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TWjST39kcz4g6w; Fri, 9 Feb 2024 18:44:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1707504265; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NoaOJlHKBFHwwXF1z9uYJT/QLEfZTjEA6t7ge01BRIc=; b=yynXfHg1AuSgXzbrxzBzRII1Ts9NUDED+WXr1nL2PrDkU5EmH4BGOpo8gP7Hxxpz4veLd8 YwFKlGiAxvbVY1Yr3jxhAjKTbPrsgxJ1pS86hm70+NGN7w3n8+CctzY8dPeO7QJ9lsWV8L TBciQNunlpiXMLsI82i/JGuaqy3DSnkS739N0TQ1it6SROQpmxATDobK99M9l/dxujQQIm OYuvIw0NP/sIlQ87zMzKYkqbqSQCTWSN+1n06yhTwaFSoXwLDazY0dKUdbqQBSp+3OtUVp JcICY9kGJCDeu+mxkChBqpHnCoqWXOPwUMnOkUrFUsUCdP4LA9+MKL63m75HBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1707504265; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NoaOJlHKBFHwwXF1z9uYJT/QLEfZTjEA6t7ge01BRIc=; b=o/EaJOH4TZXDQ48wTgqTMKCqzYMyPqur1uEgOWe1sOR+/DTPmp53VbApcAPiUIPd1P1iIl VPk+bshCoL/SPD/bBAj5v4vcSmZXnoU9edcPEIzwnV32EvkazsNMStyGoTdbqu+wnwtwiK gsmsJB8jP+a72TPeJ34LxItQS4a4wMZWOF/My/S8oGYtdaOgeY7izKUZrFjUd3SUoTUkyd +1+qeP3T40fS3PXA1uwQcwY+kDeC2vq4sFDDc1wAqWSPaxCeNEJ4IHlhTLq6sRzK/GRmg1 xnJ878rc2aYwUybUe3lcHsEYrGrWwJQroM/c6v3iqHZLjty919XPGxnAeS0PKg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1707504265; a=rsa-sha256; cv=none; b=ugHABkcRONXuLMqnpj+lYn1tcI0ch0OQQD398MBXJMfGrkKtypsd3uceLN4J6MzPIGUZw+ zY9hvTlBTEwyiCsSC4new90olZutc2L+NcVQJids8p4gH32ci3YBm6ZnSWqvvNunEUFPTB Rj8tdiulxvkI6nE8wEzUHAEZI1y7OHx11HK2HLHVV46OmlQwpw/hD5cbhdTLDpEyydX+as ajLdL0760Guwkw8iVncgK6AraG3JaSApdYfBtcmqmmJym7rdDHH1HSNhGYTLbOMQ5bvkik q5tMCLlzcSWCzSPxFDypApWT7CApT3pmbERrBiFuIRb3tafm/NsHla+CoNdYrw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TWjST2DPjzkwH; Fri, 9 Feb 2024 18:44:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 419IiPG4054824; Fri, 9 Feb 2024 18:44:25 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 419IiPcQ054821; Fri, 9 Feb 2024 18:44:25 GMT (envelope-from git) Date: Fri, 9 Feb 2024 18:44:25 GMT Message-Id: <202402091844.419IiPcQ054821@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: 0e1246e33461 - main - acpi: Cleanup handling of suballocated resources List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 0e1246e3346107b56b52d605a10f763c307e0889 Auto-Submitted: auto-generated The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=0e1246e3346107b56b52d605a10f763c307e0889 commit 0e1246e3346107b56b52d605a10f763c307e0889 Author: John Baldwin AuthorDate: 2024-02-09 18:27:45 +0000 Commit: John Baldwin CommitDate: 2024-02-09 18:27:45 +0000 acpi: Cleanup handling of suballocated resources For resources suballocated from the system resource rmans, handle those in the ACPI bus driver without passing them up to the parent. This means using bus_generic_rman_* for several bus methods for operations on suballocated resources. For bus_map/unmap_resource, find the system resource allocated from the parent bus (nexus) that contains the range being mapped and request a mapping of that parent resource. This avoids a layering violation where nexus drivers were previously asked to manage the activation and mapping of resources created belonging to the ACPI resource managers. Note that this does require passing RF_ACTIVE (with RF_UNMAPPED) when allocating system resources from the parent. While here, don't assume that the parent bus (nexus) provides a resource list that sysres resources are placed on. Instead, create a dedicated resource_list in the ACPI bus driver's softc to hold sysres resources. Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D43687 --- sys/dev/acpica/acpi.c | 198 +++++++++++++++++++++++++---------------- sys/dev/acpica/acpi_resource.c | 8 +- sys/dev/acpica/acpivar.h | 6 +- 3 files changed, 130 insertions(+), 82 deletions(-) diff --git a/sys/dev/acpica/acpi.c b/sys/dev/acpica/acpi.c index 2899929c8e6b..9cd809761b8e 100644 --- a/sys/dev/acpica/acpi.c +++ b/sys/dev/acpica/acpi.c @@ -126,11 +126,16 @@ static bus_child_deleted_t acpi_child_deleted; static bus_read_ivar_t acpi_read_ivar; static bus_write_ivar_t acpi_write_ivar; static bus_get_resource_list_t acpi_get_rlist; +static bus_get_rman_t acpi_get_rman; static bus_set_resource_t acpi_set_resource; static bus_alloc_resource_t acpi_alloc_resource; static bus_adjust_resource_t acpi_adjust_resource; static bus_release_resource_t acpi_release_resource; static bus_delete_resource_t acpi_delete_resource; +static bus_activate_resource_t acpi_activate_resource; +static bus_deactivate_resource_t acpi_deactivate_resource; +static bus_map_resource_t acpi_map_resource; +static bus_unmap_resource_t acpi_unmap_resource; static bus_child_pnpinfo_t acpi_child_pnpinfo_method; static bus_child_location_t acpi_child_location_method; static bus_hint_device_unit_t acpi_hint_device_unit; @@ -196,16 +201,19 @@ static device_method_t acpi_methods[] = { DEVMETHOD(bus_read_ivar, acpi_read_ivar), DEVMETHOD(bus_write_ivar, acpi_write_ivar), DEVMETHOD(bus_get_resource_list, acpi_get_rlist), + DEVMETHOD(bus_get_rman, acpi_get_rman), DEVMETHOD(bus_set_resource, acpi_set_resource), DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), DEVMETHOD(bus_adjust_resource, acpi_adjust_resource), DEVMETHOD(bus_release_resource, acpi_release_resource), DEVMETHOD(bus_delete_resource, acpi_delete_resource), + DEVMETHOD(bus_activate_resource, acpi_activate_resource), + DEVMETHOD(bus_deactivate_resource, acpi_deactivate_resource), + DEVMETHOD(bus_map_resource, acpi_map_resource), + DEVMETHOD(bus_unmap_resource, acpi_unmap_resource), DEVMETHOD(bus_child_pnpinfo, acpi_child_pnpinfo_method), DEVMETHOD(bus_child_location, acpi_child_location_method), - DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), - 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), @@ -479,6 +487,8 @@ acpi_attach(device_t dev) if (rman_init(&acpi_rman_mem) != 0) panic("acpi rman_init memory failed"); + resource_list_init(&sc->sysres_rl); + /* Initialise the ACPI mutex */ mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); @@ -1294,6 +1304,20 @@ acpi_get_domain(device_t dev, device_t child, int *domain) return (bus_generic_get_domain(dev, child, domain)); } +static struct rman * +acpi_get_rman(device_t bus, int type, u_int flags) +{ + /* Only memory and IO resources are managed. */ + switch (type) { + case SYS_RES_IOPORT: + return (&acpi_rman_io); + case SYS_RES_MEMORY: + return (&acpi_rman_mem); + default: + return (NULL); + } +} + /* * Pre-allocate/manage all memory and IO resources. Since rman can't handle * duplicates, we merge any in the sysresource attach routine. @@ -1301,8 +1325,8 @@ acpi_get_domain(device_t dev, device_t child, int *domain) static int acpi_sysres_alloc(device_t dev) { + struct acpi_softc *sc = device_get_softc(dev); struct resource *res; - struct resource_list *rl; struct resource_list_entry *rle; struct rman *rm; device_t *children; @@ -1320,28 +1344,21 @@ acpi_sysres_alloc(device_t dev) } free(children, M_TEMP); - rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); - STAILQ_FOREACH(rle, rl, link) { + STAILQ_FOREACH(rle, &sc->sysres_rl, link) { if (rle->res != NULL) { device_printf(dev, "duplicate resource for %jx\n", rle->start); continue; } /* Only memory and IO resources are valid here. */ - switch (rle->type) { - case SYS_RES_IOPORT: - rm = &acpi_rman_io; - break; - case SYS_RES_MEMORY: - rm = &acpi_rman_mem; - break; - default: + rm = acpi_get_rman(dev, rle->type, 0); + if (rm == NULL) continue; - } /* Pre-allocate resource and add to our rman pool. */ - res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, - &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); + res = bus_alloc_resource(dev, rle->type, + &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, + RF_ACTIVE | RF_UNMAPPED); if (res != NULL) { rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); rle->res = res; @@ -1542,63 +1559,39 @@ acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, * from our system resource regions. */ if (res == NULL && start + count - 1 == end) - res = acpi_alloc_sysres(child, type, rid, start, end, count, flags); + res = bus_generic_rman_alloc_resource(bus, child, type, rid, start, end, + count, flags); return (res); } -/* - * Attempt to allocate a specific resource range from the system - * resource ranges. Note that we only handle memory and I/O port - * system resources. - */ -struct resource * -acpi_alloc_sysres(device_t child, int type, int *rid, rman_res_t start, - rman_res_t end, rman_res_t count, u_int flags) +static bool +acpi_is_resource_managed(device_t bus, int type, struct resource *r) { - struct rman *rm; - struct resource *res; - - switch (type) { - case SYS_RES_IOPORT: - rm = &acpi_rman_io; - break; - case SYS_RES_MEMORY: - rm = &acpi_rman_mem; - break; - default: - return (NULL); - } + struct rman *rm; - KASSERT(start + count - 1 == end, ("wildcard resource range")); - res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, - child); - if (res == NULL) - return (NULL); - - rman_set_rid(res, *rid); - - /* If requested, activate the resource using the parent's method. */ - if (flags & RF_ACTIVE) - if (bus_activate_resource(child, type, *rid, res) != 0) { - rman_release_resource(res); - return (NULL); - } - - return (res); + rm = acpi_get_rman(bus, type, 0); + if (rm == NULL) + return (false); + return (rman_is_region_manager(r, rm)); } -static int -acpi_is_resource_managed(int type, struct resource *r) +static struct resource * +acpi_managed_resource(device_t bus, int type, struct resource *r) { + struct acpi_softc *sc = device_get_softc(bus); + struct resource_list_entry *rle; - /* We only handle memory and IO resources through rman. */ - switch (type) { - case SYS_RES_IOPORT: - return (rman_is_region_manager(r, &acpi_rman_io)); - case SYS_RES_MEMORY: - return (rman_is_region_manager(r, &acpi_rman_mem)); - } - return (0); + KASSERT(acpi_is_resource_managed(bus, type, r), + ("resource %p is not suballocated", r)); + + STAILQ_FOREACH(rle, &sc->sysres_rl, link) { + if (rle->type != type || rle->res == NULL) + continue; + if (rman_get_start(r) >= rman_get_start(rle->res) && + rman_get_end(r) <= rman_get_end(rle->res)) + return (rle->res); + } + return (NULL); } static int @@ -1606,7 +1599,7 @@ acpi_adjust_resource(device_t bus, device_t child, int type, struct resource *r, rman_res_t start, rman_res_t end) { - if (acpi_is_resource_managed(type, r)) + if (acpi_is_resource_managed(bus, type, r)) return (rman_adjust_resource(r, start, end)); return (bus_generic_adjust_resource(bus, child, type, r, start, end)); } @@ -1615,20 +1608,12 @@ static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) { - int ret; - /* * If this resource belongs to one of our internal managers, * deactivate it and release it to the local pool. */ - if (acpi_is_resource_managed(type, r)) { - if (rman_get_flags(r) & RF_ACTIVE) { - ret = bus_deactivate_resource(child, type, rid, r); - if (ret != 0) - return (ret); - } - return (rman_release_resource(r)); - } + if (acpi_is_resource_managed(bus, type, r)) + return (bus_generic_rman_release_resource(bus, child, type, rid, r)); return (bus_generic_rl_release_resource(bus, child, type, rid, r)); } @@ -1648,6 +1633,69 @@ acpi_delete_resource(device_t bus, device_t child, int type, int rid) resource_list_delete(rl, type, rid); } +static int +acpi_activate_resource(device_t bus, device_t child, int type, int rid, + struct resource *r) +{ + if (acpi_is_resource_managed(bus, type, r)) + return (bus_generic_rman_activate_resource(bus, child, type, + rid, r)); + return (bus_generic_activate_resource(bus, child, type, rid, r)); +} + +static int +acpi_deactivate_resource(device_t bus, device_t child, int type, int rid, + struct resource *r) +{ + if (acpi_is_resource_managed(bus, type, r)) + return (bus_generic_rman_deactivate_resource(bus, child, type, + rid, r)); + return (bus_generic_deactivate_resource(bus, child, type, rid, r)); +} + +static int +acpi_map_resource(device_t bus, device_t child, int type, struct resource *r, + struct resource_map_request *argsp, struct resource_map *map) +{ + struct resource_map_request args; + struct resource *sysres; + rman_res_t length, start; + int error; + + if (!acpi_is_resource_managed(bus, type, r)) + return (bus_generic_map_resource(bus, child, type, r, argsp, + map)); + + /* Resources must be active to be mapped. */ + if (!(rman_get_flags(r) & RF_ACTIVE)) + return (ENXIO); + + resource_init_map_request(&args); + error = resource_validate_map_request(r, argsp, &args, &start, &length); + if (error) + return (error); + + sysres = acpi_managed_resource(bus, type, r); + if (sysres == NULL) + return (ENOENT); + + args.offset = start - rman_get_start(sysres); + args.length = length; + return (bus_generic_map_resource(bus, child, type, sysres, &args, map)); +} + +static int +acpi_unmap_resource(device_t bus, device_t child, int type, struct resource *r, + struct resource_map *map) +{ + if (acpi_is_resource_managed(bus, type, r)) { + r = acpi_managed_resource(bus, type, r); + if (r == NULL) + return (ENOENT); + } + return (bus_generic_unmap_resource(bus, child, type, r, map)); +} + /* Allocate an IO port or memory resource, given its GAS. */ int acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, diff --git a/sys/dev/acpica/acpi_resource.c b/sys/dev/acpica/acpi_resource.c index b845fd146f67..6b77e74d95f1 100644 --- a/sys/dev/acpica/acpi_resource.c +++ b/sys/dev/acpica/acpi_resource.c @@ -737,8 +737,6 @@ acpi_res_set_end_dependent(device_t dev, void *context) * private rman. */ -static int acpi_sysres_rid = 100; - static int acpi_sysres_probe(device_t dev); static int acpi_sysres_attach(device_t dev); @@ -780,6 +778,7 @@ static int acpi_sysres_attach(device_t dev) { device_t bus; + struct acpi_softc *bus_sc; struct resource_list_entry *bus_rle, *dev_rle; struct resource_list *bus_rl, *dev_rl; int done, type; @@ -794,7 +793,8 @@ acpi_sysres_attach(device_t dev) */ bus = device_get_parent(dev); dev_rl = BUS_GET_RESOURCE_LIST(bus, dev); - bus_rl = BUS_GET_RESOURCE_LIST(device_get_parent(bus), bus); + bus_sc = acpi_device_get_parent_softc(dev); + bus_rl = &bus_sc->sysres_rl; STAILQ_FOREACH(dev_rle, dev_rl, link) { if (dev_rle->type != SYS_RES_IOPORT && dev_rle->type != SYS_RES_MEMORY) continue; @@ -834,7 +834,7 @@ acpi_sysres_attach(device_t dev) /* If we didn't merge with anything, add this resource. */ if (bus_rle == NULL) - bus_set_resource(bus, type, acpi_sysres_rid++, start, count); + resource_list_add_next(bus_rl, type, start, end, count); } /* After merging/moving resources to the parent, free the list. */ diff --git a/sys/dev/acpica/acpivar.h b/sys/dev/acpica/acpivar.h index bb969821b945..2322ab96014b 100644 --- a/sys/dev/acpica/acpivar.h +++ b/sys/dev/acpica/acpivar.h @@ -78,6 +78,9 @@ struct acpi_softc { struct apm_clone_data *acpi_clone; /* Pseudo-dev for devd(8). */ STAILQ_HEAD(,apm_clone_data) apm_cdevs; /* All apm/apmctl/acpi cdevs. */ struct callout susp_force_to; /* Force suspend if no acks. */ + + /* System Resources */ + struct resource_list sysres_rl; }; struct acpi_device { @@ -438,9 +441,6 @@ ACPI_STATUS acpi_lookup_irq_resource(device_t dev, int rid, struct resource *res, ACPI_RESOURCE *acpi_res); ACPI_STATUS acpi_parse_resources(device_t dev, ACPI_HANDLE handle, struct acpi_parse_resource_set *set, void *arg); -struct resource *acpi_alloc_sysres(device_t child, int type, int *rid, - rman_res_t start, rman_res_t end, rman_res_t count, - u_int flags); /* ACPI event handling */ UINT32 acpi_event_power_button_sleep(void *context);