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 43CF1C433F5 for ; Sun, 20 Mar 2022 11:44:57 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3CD0E83B2D; Sun, 20 Mar 2022 12:43:47 +0100 (CET) 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="YbAO212k"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4EAD5839A8; Sun, 20 Mar 2022 12:42:10 +0100 (CET) Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) (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 4DF6B838F0 for ; Sun, 20 Mar 2022 12:42:07 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=3DhM3YgYKBikFXHZQQLTTLQJ.HTRZ-GTTYQNXYX.IJSc.IJ@flex--ascull.bounces.google.com Received: by mail-wm1-x349.google.com with SMTP id m35-20020a05600c3b2300b0038c90ef2dceso2961219wms.4 for ; Sun, 20 Mar 2022 04:42:07 -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=rg5dlqhPUAX30qoX/L95KFJjdYMEh21iN724ib1AVPU=; b=YbAO212kkTgbFfu8qRG2eXi7S4cOL2q2bVMRQRRYlToYhDYcFcGy33WueG04Cup0Ot pUxVDl+uT604Co1cBoUvAKPCvCpXfTMqkvPA3k5eqTcPJbXk1+81D7Eoubb15VyRww4g zyh0wer6Ph96Qr7qXmAdz+FyZ5To9QAGhN1Vr70/PTs7zBr2+TU2xgA8q19W1jF7lUwK N/Jaq6Vem0X0XIg5jeDQqE1JY678qHWtman7ZsDMN6LYu1BT4bGexrcpdyg13P1lQ6fu Y7SKQ1mxfppjH2t2SCSeGgMYIuoXOvJ5stPBl4qHaWwLShyeTiRgwZPgxCFrMwc3aH1r WLBA== 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=rg5dlqhPUAX30qoX/L95KFJjdYMEh21iN724ib1AVPU=; b=deYtOR2bRcDgnC//2QGohKcYMYwz9ZUaovmKL4wGbw7jCbkcEOvKCysFNLY4I+v5su RXYi56LOKHMgZYvCAv4BausP8pv9BHIPtCh18Y6SrsH2Opgvzj8b1MsycQX9tLmu4O3o LDSIZPojZp//p4TbrNc4WR7i2t+rnGmXuszyMcZp4mgBjSjh2+AtEzN1e+EsmHomy1xS z7E5PezNvYeMqRfjxaX7LA1v4o/4IVKjO18DRkn3kzKiyho4DHzLl/OV6tcrh3uhaIKm lBs19MBV5N4Ex3s4OD6hTW+hFYDqZsE3MQTbE/O2PW9HUvkUkR8REcEFMyxenw9pr267 oKHQ== X-Gm-Message-State: AOAM533ulkD81ZZSE7+Uhy6B1K6VnqMC2CoBX1GtiIplqEJj877nvr+w APZjPqHYxZLF6+kshRYBkeolJxDEs3/907bjWzoYmGHzPEHl2YIgUduhVPWzpDqGvKPfqmi5Ast x1pq4TBCzxMPkhvqp6JYlVBU8ayRuf/JNxYbaZ43OToJcW1TzNxluDet3Btc= X-Google-Smtp-Source: ABdhPJw1K61qoTbQFnYkr6oxv5XfVR6kNyjU9uga5ndMOVSHqjbTExB2wuzX2aXBg9id6obqNeRW35URPoc= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a1c:f617:0:b0:383:5aab:9c51 with SMTP id w23-20020a1cf617000000b003835aab9c51mr22830368wmc.79.1647776526817; Sun, 20 Mar 2022 04:42:06 -0700 (PDT) Date: Sun, 20 Mar 2022 11:41:16 +0000 In-Reply-To: <20220320114118.2237795-1-ascull@google.com> Message-Id: <20220320114118.2237795-10-ascull@google.com> Mime-Version: 1.0 References: <20220320114118.2237795-1-ascull@google.com> X-Mailer: git-send-email 2.35.1.894.gb6a874cedc-goog Subject: [PATCH 09/11] pci: Add function to validate PCI address range From: Andrew Scull To: u-boot@lists.denx.de Cc: sjg@chromium.org, bmeng.cn@gmail.com, adelva@google.com, keirf@google.com, ptosi@google.com, Andrew Scull Content-Type: text/plain; charset="UTF-8" X-Mailman-Approved-At: Sun, 20 Mar 2022 12:43:23 +0100 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 Add a function to convert a PCI address range to a physical address range. The address range is validated to ensure it is contained within one of the PCI address regions and that the whole range can be indexed from the resulting physical address. Signed-off-by: Andrew Scull --- drivers/pci/pci-uclass.c | 30 ++++++++++++++++++++++++++++++ include/pci.h | 16 ++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/drivers/pci/pci-uclass.c b/drivers/pci/pci-uclass.c index 54a05d7567..efab8916e2 100644 --- a/drivers/pci/pci-uclass.c +++ b/drivers/pci/pci-uclass.c @@ -1452,6 +1452,36 @@ phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr, return phys_addr; } +phys_addr_t dm_pci_bus_range_to_phys(struct udevice *dev, pci_addr_t bus_addr, + size_t size, unsigned long mask, + unsigned long flags) +{ + struct udevice *ctlr = pci_get_controller(dev); + struct pci_controller *hose = dev_get_uclass_priv(ctlr); + struct pci_region *res; + size_t offset; + int i; + + for (i = 0; i < hose->region_count; i++) { + res = &hose->regions[i]; + + if ((res->flags & mask) != flags) + continue; + + if (bus_addr < res->bus_start) + continue; + + offset = bus_addr - res->bus_start; + if (offset >= res->size || size > res->size - offset) + continue; + + return res->phys_start + offset; + } + + puts("pci_bus_range_to_phys: invalid address range\n"); + return 0; +} + 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) diff --git a/include/pci.h b/include/pci.h index 673c95c6bb..15b3031c1f 100644 --- a/include/pci.h +++ b/include/pci.h @@ -1446,6 +1446,22 @@ u32 dm_pci_read_bar32(const struct udevice *dev, int barnum); phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t addr, unsigned long flags); +/** + * dm_pci_bus_range_to_phys() - convert a PCI bus address range to a physical + * address. + * + * @dev: Device containing the PCI address + * @addr: PCI address to convert + * @size: Size of the addressrange + * @mask: Mask of flags to match + * @flags: Flags for the region type (PCI_REGION_...) + * Return: physical address corresponding to that PCI bus address range or 0 if + * the range could not be converted + */ +phys_addr_t dm_pci_bus_range_to_phys(struct udevice *dev, pci_addr_t bus_addr, + size_t size, unsigned long mask, + unsigned long flags); + /** * dm_pci_phys_to_bus() - convert a physical address to a PCI bus address * -- 2.35.1.894.gb6a874cedc-goog