From nobody Thu Feb 27 14:16:50 2025 X-Original-To: dev-commits-src-all@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 4Z3YLV5cshz5qGJj; Thu, 27 Feb 2025 14:16:50 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Z3YLV3S7Mz3xGR; Thu, 27 Feb 2025 14:16:50 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1740665810; 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=Gtk4Cx5/p8mcrwK33jqA3UkU8UlgLFEpjklLv994960=; b=V5+oRgZfy86HZ8jD9BjIaUATjW4xM6x+UsY3rYQiwwxVIJdx41iPYDwA68c2WBt4UERPc4 gpiwEzZUqb+qhZTguGYRIMOjKIn1GZNwOnu2Hmxu9aJf6IwmJRwgFgHnssDsUcU1158LMm izUp0+1FavE+3Jkh3THrzk+ubLggs1WbP/TTrgmKIZ4PGMIgqO2/yMwqhhaF85tyeCkaTW rwFiutL5CqjAN+/Vo/v0mOeeSU01V4srrLA2HXOs+B8n3PPWOlJjYegAalQfLzR5QD9w3S hze50YlEQlzPpygAOGopgbbY05s0QRFYzcujVWArWqWMv3E6TjJJhrz2Hxxr8w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1740665810; a=rsa-sha256; cv=none; b=NQGKUQadbxd/V2mkIpwCm+U08CmibXODoEP8GsoyWszEi/GbOUMWRHQ3xS0nK3JvhBP/CT p8rUwHyC5wa6RNkoLPj01lM1t4fy+mHQqb24MnvXaK+RasdFZqyZCXKmu0ET/rYnBo93BA 9U9tbLjcq+IXWqhN3vYHmxGYVWhD8E5Z6+fDRocwKIFCCUju2S/NsvZyh65hSR6IO36q0I ohp933UW+Xu9xdN1Q495lS59knrpm5nu5zZUCTOr4pUUvaoIs+o3KRP2zXT9bg9s1v5UBJ 6IWltTOoLlpA334PyCpUBQ+RKgg4ovkCjBROST13QnWM5a9JcAPnOoH1TgXByA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1740665810; 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=Gtk4Cx5/p8mcrwK33jqA3UkU8UlgLFEpjklLv994960=; b=d8DtXVvklXr8udNWWbDwtwbUbkFVvHsZAnbG9vhpwKkpGhmL5mbECSBwJQ5YpM3b65QA20 AUXLZjZZQshnje/3cDeuxJHCiPBNzc6BMTCyEySBZ7SOESLkrupGHp6OKo0l/DdwGpdMs9 ocOuGjeK7KQdxiu00vE1IGeodaiSRT/wF7PjPlS3lDBy3uiWXNz2iR6JfaqaNTaPjXS+T1 kY8dhT1ZBSCn0ofwxm1W3Q9d3OhTkseyMiPM61J33f7xgtPNw+vUtEZ2AcZO6fE0rTGLzE nd1XEn+j78bdzbYfKFtywVGbEbU3ujsxuzmMnnWVQWVOaBJ1Q88WhVct19GDrw== 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 4Z3YLV32cTzmqN; Thu, 27 Feb 2025 14:16:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 51REGosD016233; Thu, 27 Feb 2025 14:16:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 51REGoQT016230; Thu, 27 Feb 2025 14:16:50 GMT (envelope-from git) Date: Thu, 27 Feb 2025 14:16:50 GMT Message-Id: <202502271416.51REGoQT016230@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: 621c8145b81a - stable/14 - pcib: Refine handling of resources allocated from bridge windows List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@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/stable/14 X-Git-Reftype: branch X-Git-Commit: 621c8145b81a42e412256e5b6a4088d8e54042d8 Auto-Submitted: auto-generated The branch stable/14 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=621c8145b81a42e412256e5b6a4088d8e54042d8 commit 621c8145b81a42e412256e5b6a4088d8e54042d8 Author: John Baldwin AuthorDate: 2024-02-09 18:27:45 +0000 Commit: John Baldwin CommitDate: 2025-02-27 13:09:21 +0000 pcib: Refine handling of resources allocated from bridge windows Fix a long-standing layering violation in the original NEW_PCIB code by not passing suballocated resources up to the parent bus for activation and mapping. Instead, handle activation and mapping of sub-allocated resources in this driver. When mapping resources, request a mapping from a suitable sub-region of the resource allocated from the parent bus for the associated bridge window. Note that this does require passing RF_ACTIVE (with RF_UNMAPPED) when allocating bridge window resources from the parent. Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D43690 (cherry picked from commit b377ff8110e3489eb6e6b920b51a2384dfc4eb0b) --- sys/dev/pci/pci_pci.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 126 insertions(+), 4 deletions(-) diff --git a/sys/dev/pci/pci_pci.c b/sys/dev/pci/pci_pci.c index 5286d4e82e53..02fa8cf1fb9e 100644 --- a/sys/dev/pci/pci_pci.c +++ b/sys/dev/pci/pci_pci.c @@ -66,6 +66,10 @@ static bus_alloc_resource_t pcib_alloc_resource; #ifdef NEW_PCIB static bus_adjust_resource_t pcib_adjust_resource; static bus_release_resource_t pcib_release_resource; +static bus_activate_resource_t pcib_activate_resource; +static bus_deactivate_resource_t pcib_deactivate_resource; +static bus_map_resource_t pcib_map_resource; +static bus_unmap_resource_t pcib_unmap_resource; #endif static int pcib_reset_child(device_t dev, device_t child, int flags); @@ -108,12 +112,16 @@ static device_method_t pcib_methods[] = { #ifdef NEW_PCIB DEVMETHOD(bus_adjust_resource, pcib_adjust_resource), DEVMETHOD(bus_release_resource, pcib_release_resource), + DEVMETHOD(bus_activate_resource, pcib_activate_resource), + DEVMETHOD(bus_deactivate_resource, pcib_deactivate_resource), + DEVMETHOD(bus_map_resource, pcib_map_resource), + DEVMETHOD(bus_unmap_resource, pcib_unmap_resource), #else DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), DEVMETHOD(bus_release_resource, bus_generic_release_resource), -#endif DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), +#endif DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), DEVMETHOD(bus_reset_child, pcib_reset_child), @@ -381,7 +389,7 @@ alloc_ranges(rman_res_t start, rman_res_t end, void *arg) device_printf(as->sc->dev, "allocating non-ISA range %#jx-%#jx\n", start, end); as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT, - &rid, start, end, end - start + 1, 0); + &rid, start, end, end - start + 1, RF_ACTIVE | RF_UNMAPPED); if (as->res[as->count] == NULL) as->error = ENXIO; else @@ -454,7 +462,7 @@ pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type, else { rid = w->reg; res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit, - w->limit - w->base + 1, flags); + w->limit - w->base + 1, flags | RF_ACTIVE | RF_UNMAPPED); if (res != NULL) pcib_add_window_resources(w, &res, 1); } @@ -2001,7 +2009,7 @@ pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type, count = roundup2(count, (rman_res_t)1 << w->step); rid = w->reg; res = bus_alloc_resource(sc->dev, type, &rid, start, end, count, - flags & ~RF_ACTIVE); + flags | RF_ACTIVE | RF_UNMAPPED); if (res == NULL) return (ENOSPC); pcib_add_window_resources(w, &res, 1); @@ -2452,6 +2460,120 @@ pcib_release_resource(device_t dev, device_t child, int type, int rid, } return (bus_generic_release_resource(dev, child, type, rid, r)); } + +static int +pcib_activate_resource(device_t dev, device_t child, int type, int rid, + struct resource *r) +{ + struct pcib_softc *sc = device_get_softc(dev); + struct resource_map map; + int error; + + if (!pcib_is_resource_managed(sc, type, r)) + return (bus_generic_activate_resource(dev, child, type, rid, + r)); + + error = rman_activate_resource(r); + if (error != 0) + return (error); + + if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && + (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { + error = BUS_MAP_RESOURCE(dev, child, type, r, NULL, &map); + if (error != 0) { + rman_deactivate_resource(r); + return (error); + } + + rman_set_mapping(r, &map); + } + return (0); +} + +static int +pcib_deactivate_resource(device_t dev, device_t child, int type, int rid, + struct resource *r) +{ + struct pcib_softc *sc = device_get_softc(dev); + struct resource_map map; + int error; + + if (!pcib_is_resource_managed(sc, type, r)) + return (bus_generic_deactivate_resource(dev, child, type, rid, + r)); + + error = rman_deactivate_resource(r); + if (error != 0) + return (error); + + if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && + (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { + rman_get_mapping(r, &map); + BUS_UNMAP_RESOURCE(dev, child, type, r, &map); + } + return (0); +} + +static struct resource * +pcib_find_parent_resource(struct pcib_window *w, struct resource *r) +{ + for (int i = 0; i < w->count; i++) { + if (rman_get_start(w->res[i]) <= rman_get_start(r) && + rman_get_end(w->res[i]) >= rman_get_end(r)) + return (w->res[i]); + } + return (NULL); +} + +static int +pcib_map_resource(device_t dev, device_t child, int type, struct resource *r, + struct resource_map_request *argsp, struct resource_map *map) +{ + struct pcib_softc *sc = device_get_softc(dev); + struct resource_map_request args; + struct pcib_window *w; + struct resource *pres; + rman_res_t length, start; + int error; + + w = pcib_get_resource_window(sc, type, r); + if (w == NULL) + return (bus_generic_map_resource(dev, 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); + + pres = pcib_find_parent_resource(w, r); + if (pres == NULL) + return (ENOENT); + + args.offset = start - rman_get_start(pres); + args.length = length; + return (bus_generic_map_resource(dev, child, type, pres, &args, map)); +} + +static int +pcib_unmap_resource(device_t dev, device_t child, int type, struct resource *r, + struct resource_map *map) +{ + struct pcib_softc *sc = device_get_softc(dev); + struct pcib_window *w; + + w = pcib_get_resource_window(sc, type, r); + if (w != NULL) { + r = pcib_find_parent_resource(w, r); + if (r == NULL) + return (ENOENT); + } + return (bus_generic_unmap_resource(dev, child, type, r, map)); +} #else /* * We have to trap resource allocation requests and ensure that the bridge