From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8F300C433F5 for ; Thu, 21 Apr 2022 16:13:19 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8F24183EB5; Thu, 21 Apr 2022 18:12:10 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="VDMQzX9e"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AFE0E83E70; Thu, 21 Apr 2022 18:11:45 +0200 (CEST) Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id DDEDE83E60 for ; Thu, 21 Apr 2022 18:11:41 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=3PYJhYgYKBuACUEWNNIQQING.EQOW-DQQVNKUVU.FGPZ.FG@flex--ascull.bounces.google.com Received: by mail-wr1-x44a.google.com with SMTP id t17-20020adfa2d1000000b0020ac519c222so222269wra.4 for ; Thu, 21 Apr 2022 09:11:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=HhxB00FPJaToBVp5hqDkt/gYVp5N6tZ/0YIqQdHDRF0=; b=VDMQzX9ePGu/Hv7odJZsE0Hm4bNPT4/RnchgAQw7B/wFgqfGeFDoeBx7K+407mAT4R +tt0jofDxe+qO71qJkopU0uoMJTjYSLDIAZW6m4tVJyUeDaeDcL4+CX/Q56YLtUNrMh6 3ssRhzQjuoz+Qhlnu19Z2pWcVeZxboY9b0oeBVjuPn94bwu+FiplBzLgYUYH4P4QyUaq PSwno01XPEFkrGAdb0YR3kp/gKrJnU03OP/+D5cZ0TAH1PFlqoyr7H3Bz3WhMrAiKQb6 WfQvuRw15z8kD22cXqSl8cgQoyriA4prvGC0K8OFufMDCx2sUQkr1lCjf/WfKLRNWXwY ItMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=HhxB00FPJaToBVp5hqDkt/gYVp5N6tZ/0YIqQdHDRF0=; b=5qpQCSqQPyT2CvvgVaiqcds0KYjKtfdDVCHpuwQ0tAuNrFKhgDpaYQIp+wNNwv+Bw8 PWRC1BanbuDPFt0cLQxQJVBSTxTRNnCrPEGUTTqXUG25nL2KYHogs6rShnz5mllYJZNc zhmuUhfnlyuGn2xky91rOKGUuvXEq9VDvaM/rAQefl2IkiBrswJNRjr63+K8xeWxVaUB xpOMTiamJrHbY6O+sODpXT6XL3ukLL8SO/Seee4fPmleIQsyzWFYhX855+29Lw9DTwNz 30gDBhlsmvWWicpyBCey0/Kc15JtHS92tjDjoK7QCtf5YD1sFpUfs/GN+LePEUBcdecP oeOw== X-Gm-Message-State: AOAM530YrgThmaJS9jxco667oV8kTWTvzrkQ6fXhdUr3hAxZybO7QdiB tuQ1mHSuST9OYwuvhMZEhX/yzmjCuw3Cc3b/2jgb/izsCsNKcwJ7KKC6hWv75bDzRKM5+bTMvxM 538o9JjGbnPxj23fH7l4hNcFRcLs3tfHIpOCm8V+SvZ//EV6SnSdPc1gdmYY= X-Google-Smtp-Source: ABdhPJzPXbitAQKVbnOfhFF9n/T3y0Dt/e1eI2P08qgycwACpQ4lACBZjfPx8djFaQSJ0B723mWsFpK7JwU= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a05:600c:4e01:b0:392:ab42:48 with SMTP id b1-20020a05600c4e0100b00392ab420048mr75328wmq.169.1650557501276; Thu, 21 Apr 2022 09:11:41 -0700 (PDT) Date: Thu, 21 Apr 2022 16:11:08 +0000 In-Reply-To: <20220421161116.1202023-1-ascull@google.com> Message-Id: <20220421161116.1202023-11-ascull@google.com> Mime-Version: 1.0 References: <20220421161116.1202023-1-ascull@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v3 10/18] pci: Range check address conversions From: Andrew Scull To: u-boot@lists.denx.de Cc: sjg@chromium.org, bmeng.cn@gmail.com, trini@konsulko.com, Andrew Scull Content-Type: text/plain; charset="UTF-8" X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean When converting between PCI bus and physical addresses, include a length parameter that can be used to check that the entire range fits within one of the PCI regions. This prevents an address being returned that might be only partially valid for the range it is going to be used for. Where the range check is not wanted, passing a length of 0 will have the same behaviour as before this change. Signed-off-by: Andrew Scull Reviewed-by: Bin Meng --- drivers/pci/pci-uclass.c | 57 ++++++++++++++++++++++++---------------- include/pci.h | 20 +++++++------- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/drivers/pci/pci-uclass.c b/drivers/pci/pci-uclass.c index 0424cd339e..997ca0d5e4 100644 --- a/drivers/pci/pci-uclass.c +++ b/drivers/pci/pci-uclass.c @@ -1390,12 +1390,13 @@ void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr) dm_pci_write_config32(dev, bar, addr); } -static int _dm_pci_bus_to_phys(struct udevice *ctlr, - pci_addr_t bus_addr, unsigned long flags, +static int _dm_pci_bus_to_phys(struct udevice *ctlr, pci_addr_t bus_addr, + size_t len, unsigned long flags, unsigned long skip_mask, phys_addr_t *pa) { struct pci_controller *hose = dev_get_uclass_priv(ctlr); struct pci_region *res; + pci_addr_t offset; int i; if (hose->region_count == 0) { @@ -1412,18 +1413,25 @@ static int _dm_pci_bus_to_phys(struct udevice *ctlr, if (res->flags & skip_mask) continue; - if (bus_addr >= res->bus_start && - (bus_addr - res->bus_start) < res->size) { - *pa = (bus_addr - res->bus_start + res->phys_start); - return 0; - } + if (bus_addr < res->bus_start) + continue; + + offset = bus_addr - res->bus_start; + if (offset >= res->size) + continue; + + if (len > res->size - offset) + continue; + + *pa = res->phys_start + offset; + return 0; } return 1; } phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr, - unsigned long flags) + size_t len, unsigned long flags) { phys_addr_t phys_addr = 0; struct udevice *ctlr; @@ -1437,14 +1445,14 @@ phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr, * on matches that don't have PCI_REGION_SYS_MEMORY set */ if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) { - ret = _dm_pci_bus_to_phys(ctlr, bus_addr, + ret = _dm_pci_bus_to_phys(ctlr, bus_addr, len, flags, PCI_REGION_SYS_MEMORY, &phys_addr); if (!ret) return phys_addr; } - ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr); + ret = _dm_pci_bus_to_phys(ctlr, bus_addr, len, flags, 0, &phys_addr); if (ret) puts("pci_hose_bus_to_phys: invalid physical address\n"); @@ -1453,12 +1461,12 @@ phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr, } static int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, - unsigned long flags, unsigned long skip_mask, - pci_addr_t *ba) + size_t len, unsigned long flags, + unsigned long skip_mask, pci_addr_t *ba) { struct pci_region *res; struct udevice *ctlr; - pci_addr_t bus_addr; + phys_addr_t offset; int i; struct pci_controller *hose; @@ -1480,20 +1488,25 @@ static int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, if (res->flags & skip_mask) continue; - bus_addr = phys_addr - res->phys_start + res->bus_start; + if (phys_addr < res->phys_start) + continue; - if (bus_addr >= res->bus_start && - (bus_addr - res->bus_start) < res->size) { - *ba = bus_addr; - return 0; - } + offset = phys_addr - res->phys_start; + if (offset >= res->size) + continue; + + if (len > res->size - offset) + continue; + + *ba = res->bus_start + offset; + return 0; } return 1; } pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, - unsigned long flags) + size_t len, unsigned long flags) { pci_addr_t bus_addr = 0; int ret; @@ -1503,13 +1516,13 @@ pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, * on matches that don't have PCI_REGION_SYS_MEMORY set */ if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) { - ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, + ret = _dm_pci_phys_to_bus(dev, phys_addr, len, flags, PCI_REGION_SYS_MEMORY, &bus_addr); if (!ret) return bus_addr; } - ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr); + ret = _dm_pci_phys_to_bus(dev, phys_addr, len, flags, 0, &bus_addr); if (ret) puts("pci_hose_phys_to_bus: invalid physical address\n"); diff --git a/include/pci.h b/include/pci.h index 3d0356b8fd..966b84c148 100644 --- a/include/pci.h +++ b/include/pci.h @@ -1313,14 +1313,15 @@ void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr); u32 dm_pci_read_bar32(const struct udevice *dev, int barnum); /** - * dm_pci_bus_to_phys() - convert a PCI bus address to a physical address + * dm_pci_bus_to_phys() - convert a PCI bus address range to a physical address * * @dev: Device containing the PCI address * @addr: PCI address to convert + * @len: Length of the address range * @flags: Flags for the region type (PCI_REGION_...) * Return: physical address corresponding to that PCI bus address */ -phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t addr, +phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t addr, size_t len, unsigned long flags); /** @@ -1328,10 +1329,11 @@ phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t addr, * * @dev: Device containing the bus address * @addr: Physical address to convert + * @len: Length of the address range * @flags: Flags for the region type (PCI_REGION_...) * Return: PCI bus address corresponding to that physical address */ -pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t addr, +pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t addr, size_t len, unsigned long flags); /** @@ -1453,19 +1455,19 @@ int dm_pci_find_ext_capability(struct udevice *dev, int cap); int dm_pci_flr(struct udevice *dev); #define dm_pci_virt_to_bus(dev, addr, flags) \ - dm_pci_phys_to_bus(dev, (virt_to_phys(addr)), (flags)) + dm_pci_phys_to_bus(dev, (virt_to_phys(addr)), 0, (flags)) #define dm_pci_bus_to_virt(dev, addr, flags, len, map_flags) \ - map_physmem(dm_pci_bus_to_phys(dev, (addr), (flags)), \ + map_physmem(dm_pci_bus_to_phys(dev, (addr), (len), (flags)), \ (len), (map_flags)) #define dm_pci_phys_to_mem(dev, addr) \ - dm_pci_phys_to_bus((dev), (addr), PCI_REGION_MEM) + dm_pci_phys_to_bus((dev), (addr), 0, PCI_REGION_MEM) #define dm_pci_mem_to_phys(dev, addr) \ - dm_pci_bus_to_phys((dev), (addr), PCI_REGION_MEM) + dm_pci_bus_to_phys((dev), (addr), 0, PCI_REGION_MEM) #define dm_pci_phys_to_io(dev, addr) \ - dm_pci_phys_to_bus((dev), (addr), PCI_REGION_IO) + dm_pci_phys_to_bus((dev), (addr), 0, PCI_REGION_IO) #define dm_pci_io_to_phys(dev, addr) \ - dm_pci_bus_to_phys((dev), (addr), PCI_REGION_IO) + dm_pci_bus_to_phys((dev), (addr), 0, PCI_REGION_IO) #define dm_pci_virt_to_mem(dev, addr) \ dm_pci_virt_to_bus((dev), (addr), PCI_REGION_MEM) -- 2.36.0.rc2.479.g8af0fa9b8e-goog