From patchwork Fri Aug 9 17:57:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Vasut X-Patchwork-Id: 11087433 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7F9021850 for ; Fri, 9 Aug 2019 17:58:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6DFD720602 for ; Fri, 9 Aug 2019 17:58:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5E22E20415; Fri, 9 Aug 2019 17:58:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A3511205F7 for ; Fri, 9 Aug 2019 17:58:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2436928AbfHIR6M (ORCPT ); Fri, 9 Aug 2019 13:58:12 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:34985 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726463AbfHIR6L (ORCPT ); Fri, 9 Aug 2019 13:58:11 -0400 Received: by mail-wr1-f67.google.com with SMTP id k2so13161379wrq.2; Fri, 09 Aug 2019 10:58:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=tUZWuc1WhcjzBsiR4EKC4YEhfzXYVanXFSYVyIayfbs=; b=e8UitYY0PXOWAO8UGePPUxI2EvvYBfJ2yGdXi3D5+3htjZKD/ou0ZhTJR0259vo7+M HSBqKcbRRV0brb8Zssl7yiABHpfnW3LGFl67QSMcC4YCTpez7BO5fYh4R4dw9UGHtjjA 9y9ZaR7zov1TMZwKf8Bbj5SPERFN8gLdFSInO7gudsCCWpzfqDsiMH7rXbk9dyz1e7UN K/h7/0BdUlMjHpDy6HG3B/9FS3ZywXP2euM06OsvshGto3mxF2PgGUJnslXQkYcyn+4l HffwqATnuKqkJbpQtSmRL+QxGD9tnwzjgx7Pola6JgaKqxQjGW5WKtozKKaqO47DKL8I AmQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=tUZWuc1WhcjzBsiR4EKC4YEhfzXYVanXFSYVyIayfbs=; b=lKtnEsyavsqD1UTLTNDOkhvnHnx1LHxxB5Ytz4wlXRsY6LNpMvivpGr7rUm+cjhFzX Y/vW6UIMJ9MPyTTc6wbjnqSCbS2+4EBfnu4p2CqM47vC4UdRNDMlJAMMsmQ6XQSqhqwi pE6c6TT9r2J0eQhG8lGnIM5wVxoDXbl05v/st+INA0ru2QRCKR/K8CeCfgcGJOFx9YA8 /at+qNMsEY4Q0IrjZsPh/7jTgxgbiChLhIXSJlu6tFy80G0KM3gCJYyfoEU0oQweYXYi fpPuL9GtQ5NT3Fl0q4XCXMKog1Z/Cl+/oMUie9q4yiskHTFJIsZzMWbRiEQP4+LFjxYm odoQ== X-Gm-Message-State: APjAAAWi0hD9xrMs6txvQCPfw7pD+xETpUotLH1kw7ewsAdcz5dAPw0t bmzmsWjYXkLs8MrHnhi2bzUs7SBL X-Google-Smtp-Source: APXvYqzm/ed/27C6KnmquFScFATPGI9e5ZZfh1xd6v4c05S4CeSP+9fAJzNNbQo6CGC1t69Swj56ww== X-Received: by 2002:adf:eac5:: with SMTP id o5mr12778849wrn.140.1565373489148; Fri, 09 Aug 2019 10:58:09 -0700 (PDT) Received: from chi.lan (ip-86-49-35-8.net.upcbroadband.cz. [86.49.35.8]) by smtp.gmail.com with ESMTPSA id z6sm4078920wre.76.2019.08.09.10.58.08 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 09 Aug 2019 10:58:08 -0700 (PDT) From: marek.vasut@gmail.com To: linux-pci@vger.kernel.org Cc: Marek Vasut , Geert Uytterhoeven , Lorenzo Pieralisi , Wolfram Sang , linux-renesas-soc@vger.kernel.org Subject: [PATCH V3 1/3] PCI: rcar: Move the inbound index check Date: Fri, 9 Aug 2019 19:57:39 +0200 Message-Id: <20190809175741.7066-1-marek.vasut@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Marek Vasut Since the $idx variable value is stored across multiple calls to rcar_pcie_inbound_ranges() function, and the $idx value is used to index registers which are written, subsequent calls might cause the $idx value to be high enough to trigger writes into nonexistent registers. Fix this by moving the $idx value check to the beginning of the loop. Signed-off-by: Marek Vasut Cc: Geert Uytterhoeven Cc: Lorenzo Pieralisi Cc: Wolfram Sang Cc: linux-renesas-soc@vger.kernel.org To: linux-pci@vger.kernel.org Reviewed-by: Andrew Murray --- V2: New patch V3: Adjust the check to idx >= MAX_NR_INBOUND_MAPS - 1 --- drivers/pci/controller/pcie-rcar.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/drivers/pci/controller/pcie-rcar.c b/drivers/pci/controller/pcie-rcar.c index f6a669a9af41..56a6433eb70b 100644 --- a/drivers/pci/controller/pcie-rcar.c +++ b/drivers/pci/controller/pcie-rcar.c @@ -1048,6 +1048,10 @@ static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie, mask &= ~0xf; while (cpu_addr < cpu_end) { + if (idx >= MAX_NR_INBOUND_MAPS - 1) { + dev_err(pcie->dev, "Failed to map inbound regions!\n"); + return -EINVAL; + } /* * Set up 64-bit inbound regions as the range parser doesn't * distinguish between 32 and 64-bit types. @@ -1067,11 +1071,6 @@ static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie, pci_addr += size; cpu_addr += size; idx += 2; - - if (idx > MAX_NR_INBOUND_MAPS) { - dev_err(pcie->dev, "Failed to map inbound regions!\n"); - return -EINVAL; - } } *index = idx; From patchwork Fri Aug 9 17:57:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Vasut X-Patchwork-Id: 11087431 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7278A13B1 for ; Fri, 9 Aug 2019 17:58:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 674D1205F7 for ; Fri, 9 Aug 2019 17:58:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 57BFE1FF29; Fri, 9 Aug 2019 17:58:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 02BD71FF0B for ; Fri, 9 Aug 2019 17:58:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2437009AbfHIR6N (ORCPT ); Fri, 9 Aug 2019 13:58:13 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:39780 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2436827AbfHIR6N (ORCPT ); Fri, 9 Aug 2019 13:58:13 -0400 Received: by mail-wr1-f68.google.com with SMTP id t16so8892641wra.6; Fri, 09 Aug 2019 10:58:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yNK81rW078tKNHD8lBCI2aUIuH2iKKE88d8/v/IOfyM=; b=l82p4N8N17B5IyWLMWnnadHZgMe+xJMQcWPZEeJ4beUymYWmc+aP0vConoaTI+4nhP HaLoOGy5J6HIsk5B7gMEYIG4QAIDlhCcfYWxrTlv8Bh8m+NwBPCq5mJHEeVXEtEMnm2J B+vZ+9LZ8crEzKrtXnzHUb0BOs6WJHhfUUuuKC3WkH8kFLpjnYclWMdqqEtwHYobVUHz JLLI1m6RKB9nbTjMS8AHtWneMHQrachJjvOillNsbLhFjWek82NnVZL4dvFeXxceF/UY /8zIbD8qfmnVKxRYLD+3YbGVAYtux2FY+5fOZlh3DJNYupOnQsq7JA+uou4Vfl7aRTt3 KZ3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yNK81rW078tKNHD8lBCI2aUIuH2iKKE88d8/v/IOfyM=; b=GtsJMQItHsFkP5In/+nFOv5lib3NdCLl0JdSTtlIDTjG9a9QV3jl6/UXFLwdR/hCdR Xy8NIHBkGD0tQJb8Z8s7c55wz+lPSB6xXxnulQebaZ6jwOPA0pihwXg98D3YITti1Td3 1zqFZzIVPFWxN7w6znTs0LJLntyDQVn22XThmIW34dx11mhZUqaTqLFTDp1kcv/snFpx pxfmXIAbkDhJxNMVQ9ix5xsPT/AsWNz5fmgGjj+Rlb+6plFq/LZ1LOHRsKyWy4Y+as+q t3tjj1EeMrEkcNJXK0lb+ShG3mb8gbV3m7E+WywpOfGT0rHh6VAfk7N7zbj/X20ewRk4 QrQg== X-Gm-Message-State: APjAAAVumuEnAGgWy5sXiqvnj/czO2Zkh15G46qK+1X7OhTa7l8qWoZH aXY9dAFnb6ULuY7N/uCpHakSvTrm X-Google-Smtp-Source: APXvYqxV/5xxcIvNmDMUNB+ShJLsh4VdHdOTqqymXu2cPy251iQMjm9EhNDyJADnhV1TukokUc2zTg== X-Received: by 2002:a5d:4d81:: with SMTP id b1mr26768399wru.27.1565373490741; Fri, 09 Aug 2019 10:58:10 -0700 (PDT) Received: from chi.lan (ip-86-49-35-8.net.upcbroadband.cz. [86.49.35.8]) by smtp.gmail.com with ESMTPSA id z6sm4078920wre.76.2019.08.09.10.58.09 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 09 Aug 2019 10:58:09 -0700 (PDT) From: marek.vasut@gmail.com To: linux-pci@vger.kernel.org Cc: Marek Vasut , Geert Uytterhoeven , Lorenzo Pieralisi , Wolfram Sang , linux-renesas-soc@vger.kernel.org Subject: [PATCH V3 2/3] PCI: rcar: Do not abort on too many inbound dma-ranges Date: Fri, 9 Aug 2019 19:57:40 +0200 Message-Id: <20190809175741.7066-2-marek.vasut@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190809175741.7066-1-marek.vasut@gmail.com> References: <20190809175741.7066-1-marek.vasut@gmail.com> MIME-Version: 1.0 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Marek Vasut In case the "dma-ranges" DT property contains either too many ranges or the range start address is unaligned in such a way that populating the range into the controller requires multiple entries, a situation may occur where all ranges cannot be loaded into the controller. Currently, the driver refuses to probe in such a situation. Relax this behavior, load as many ranges as possible and warn if some ranges do not fit anymore. Signed-off-by: Marek Vasut Cc: Geert Uytterhoeven Cc: Lorenzo Pieralisi Cc: Wolfram Sang Cc: linux-renesas-soc@vger.kernel.org To: linux-pci@vger.kernel.org Reviewed-by: Simon Horman --- V2: Update on top of 1/3 V3: No change --- drivers/pci/controller/pcie-rcar.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/pci/controller/pcie-rcar.c b/drivers/pci/controller/pcie-rcar.c index 56a6433eb70b..e2735005ffd3 100644 --- a/drivers/pci/controller/pcie-rcar.c +++ b/drivers/pci/controller/pcie-rcar.c @@ -1049,8 +1049,9 @@ static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie, while (cpu_addr < cpu_end) { if (idx >= MAX_NR_INBOUND_MAPS - 1) { - dev_err(pcie->dev, "Failed to map inbound regions!\n"); - return -EINVAL; + dev_warn(pcie->dev, + "Too many inbound regions, not all are mapped.\n"); + break; } /* * Set up 64-bit inbound regions as the range parser doesn't From patchwork Fri Aug 9 17:57:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Vasut X-Patchwork-Id: 11087437 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C42C21709 for ; Fri, 9 Aug 2019 17:58:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B804E1FF0B for ; Fri, 9 Aug 2019 17:58:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B5CF91FF14; Fri, 9 Aug 2019 17:58:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F21231FF29 for ; Fri, 9 Aug 2019 17:58:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2437013AbfHIR6O (ORCPT ); Fri, 9 Aug 2019 13:58:14 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:40903 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726463AbfHIR6O (ORCPT ); Fri, 9 Aug 2019 13:58:14 -0400 Received: by mail-wr1-f68.google.com with SMTP id r1so98972674wrl.7; Fri, 09 Aug 2019 10:58:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yQWTJJ6S7RxirtJD9R73eQy6LDLBWJ0MPzTFIc93ZNU=; b=Ft70k0pWg0W4FbRaq6gXE4QluAB+shuXiJWgX0CDVAwZnzDU/B/r9KvOeiDtVd1LNK T234jOUilPYn6ARiCAnpjh6hVXulpa+Exh2AVy/BeZVDNIaRYgcE6oEb5GTiLMwvxTvC kwAUJ+sQeIygag3B4tkyqquQyComqtlFEK3XiA22OMVbEplk7oouBX9KUjitUfj/Xzwy gCRhTqcwfEo1nCofsEFc+dZ/AoTo0lfHQLHJcDwVFC3HP7G0RhRBndTbEAIBEyh2N12Q onvzGnCcQGM6mC99GP2wA+KxcYuImYpzllgqrauE2PQbuFt/cDJ5D8+1KIVLJaLwOiFS iArA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yQWTJJ6S7RxirtJD9R73eQy6LDLBWJ0MPzTFIc93ZNU=; b=FPrHo2yl/YHIREgsN0sQe1Zmt2qUTALpqZfxgiCTmpCclQIexcGvO9tA4lbJPaFAQV 03QZCjYsqmD79EseLq0cjM21rWwa5Wzn6NKUZHiZqCJZf1xPUXOZZLvG+TrRXOzXeRng fi2eE29vmYjChSzZFJ9Y094jvW8Dl5jK5QbTMZqHTAAynly3OdPKT0KtrHvLD8Nb7J/3 6EvbaSugE7fuF5U0bVK36hisTC7qw5xZdBx+1k8Qn34s2iCA4X4FTdpxovKNriaSpjnj iC6y/VFZRgq//es6XRxs6bnz1r+YKTPC5aPpOtQ8OO5DLQxUJjnF6HVScYhjw6fx02c1 SiyQ== X-Gm-Message-State: APjAAAV4vAEsPub1DYFg8tJBV49u/2P/BInIUM/QX2ZSyPykB/PvPTtm Yr6UGEG1Rp4jeIbv6trHvNfjfwqq X-Google-Smtp-Source: APXvYqzQbSQuIpjecKlDRj7fQfRBQoSChICMzbl7EKHQo8257NMpTfD/sMmbqmagMc/M7tarKe6Ssg== X-Received: by 2002:a5d:6749:: with SMTP id l9mr19935099wrw.73.1565373491844; Fri, 09 Aug 2019 10:58:11 -0700 (PDT) Received: from chi.lan (ip-86-49-35-8.net.upcbroadband.cz. [86.49.35.8]) by smtp.gmail.com with ESMTPSA id z6sm4078920wre.76.2019.08.09.10.58.10 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 09 Aug 2019 10:58:11 -0700 (PDT) From: marek.vasut@gmail.com To: linux-pci@vger.kernel.org Cc: Marek Vasut , Geert Uytterhoeven , Lorenzo Pieralisi , Wolfram Sang , linux-renesas-soc@vger.kernel.org, Simon Horman Subject: [PATCH V3 3/3] PCI: rcar: Recalculate inbound range alignment for each controller entry Date: Fri, 9 Aug 2019 19:57:41 +0200 Message-Id: <20190809175741.7066-3-marek.vasut@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190809175741.7066-1-marek.vasut@gmail.com> References: <20190809175741.7066-1-marek.vasut@gmail.com> MIME-Version: 1.0 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Marek Vasut Due to hardware constraints, the size of each inbound range entry populated into the controller cannot be larger than the alignment of the entry's start address. Currently, the alignment for each "dma-ranges" inbound range is calculated only once for each range and the increment for programming the controller is also derived from it only once. Thus, a "dma-ranges" entry describing a memory at 0x48000000 and size 0x38000000 would lead to multiple controller entries, each 0x08000000 long. This is inefficient, especially considering that by adding the size to the start address, the alignment increases. This patch moves the alignment calculation into the loop populating the controller entries, thus updating the alignment for each controller entry. Signed-off-by: Marek Vasut Cc: Geert Uytterhoeven Cc: Lorenzo Pieralisi Cc: Wolfram Sang Cc: linux-renesas-soc@vger.kernel.org To: linux-pci@vger.kernel.org Reviewed-by: Simon Horman Reviewed-by: Andrew Murray --- V2: Update on top of 1/3 V3: No change --- drivers/pci/controller/pcie-rcar.c | 37 +++++++++++++++--------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/drivers/pci/controller/pcie-rcar.c b/drivers/pci/controller/pcie-rcar.c index e2735005ffd3..d820aa64d0b7 100644 --- a/drivers/pci/controller/pcie-rcar.c +++ b/drivers/pci/controller/pcie-rcar.c @@ -1029,30 +1029,31 @@ static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie, if (restype & IORESOURCE_PREFETCH) flags |= LAM_PREFETCH; - /* - * If the size of the range is larger than the alignment of the start - * address, we have to use multiple entries to perform the mapping. - */ - if (cpu_addr > 0) { - unsigned long nr_zeros = __ffs64(cpu_addr); - u64 alignment = 1ULL << nr_zeros; - - size = min(range->size, alignment); - } else { - size = range->size; - } - /* Hardware supports max 4GiB inbound region */ - size = min(size, 1ULL << 32); - - mask = roundup_pow_of_two(size) - 1; - mask &= ~0xf; - while (cpu_addr < cpu_end) { if (idx >= MAX_NR_INBOUND_MAPS - 1) { dev_warn(pcie->dev, "Too many inbound regions, not all are mapped.\n"); break; } + /* + * If the size of the range is larger than the alignment of + * the start address, we have to use multiple entries to + * perform the mapping. + */ + if (cpu_addr > 0) { + unsigned long nr_zeros = __ffs64(cpu_addr); + u64 alignment = 1ULL << nr_zeros; + + size = min(range->size, alignment); + } else { + size = range->size; + } + /* Hardware supports max 4GiB inbound region */ + size = min(size, 1ULL << 32); + + mask = roundup_pow_of_two(size) - 1; + mask &= ~0xf; + /* * Set up 64-bit inbound regions as the range parser doesn't * distinguish between 32 and 64-bit types.