]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
PCI: dwc: ep: Add 'address' alignment to 'size' check in dw_pcie_prog_ep_inbound_atu()
authorNiklas Cassel <cassel@kernel.org>
Fri, 13 Dec 2024 14:33:04 +0000 (15:33 +0100)
committerKrzysztof Wilczyński <kwilczynski@kernel.org>
Wed, 18 Dec 2024 21:51:30 +0000 (21:51 +0000)
dw_pcie_prog_ep_inbound_atu() is used to program an inbound iATU in
"BAR Match Mode".

A memory address returned by e.g. kmalloc() is guaranteed to have natural
alignment (aligned to the size of the allocation). It is however not
guaranteed that pci_epc_set_bar() (and thus dw_pcie_prog_ep_inbound_atu())
is supplied an address that has natural alignment. (An EPF driver can send
in an arbitrary physical address to pci_epc_set_bar().)

The DWC Databook description for the LWR_TARGET_RW and LWR_TARGET_HW fields
in the IATU_LWR_TARGET_ADDR_OFF_INBOUND_i registers state that:
"Field size depends on log2(BAR_MASK+1) in BAR match mode."

I.e. only the upper bits are writable, and the number of writable bits is
dependent on the configured BAR_MASK.

Add a check to ensure that the physical address programmed in the iATU is
aligned to the size of the BAR (BAR_MASK+1), as without this, we can get
hard to debug errors, as we could write to bits that are read-only (without
getting a write error), which could cause the iATU to end up redirecting to
a physical address that is different from the address that we intended.

Link: https://lore.kernel.org/r/20241213143301.4158431-11-cassel@kernel.org
Signed-off-by: Niklas Cassel <cassel@kernel.org>
Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org>
Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
drivers/pci/controller/dwc/pcie-designware-ep.c
drivers/pci/controller/dwc/pcie-designware.c
drivers/pci/controller/dwc/pcie-designware.h

index 44a617d54b15f1e197d53ac0613f8db46d958b83..8e07d432e74f206085a75c76d9e95f8a3ae61305 100644 (file)
@@ -128,7 +128,8 @@ static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
 }
 
 static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, u8 func_no, int type,
-                                 dma_addr_t cpu_addr, enum pci_barno bar)
+                                 dma_addr_t cpu_addr, enum pci_barno bar,
+                                 size_t size)
 {
        int ret;
        u32 free_win;
@@ -145,7 +146,7 @@ static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, u8 func_no, int type,
        }
 
        ret = dw_pcie_prog_ep_inbound_atu(pci, func_no, free_win, type,
-                                         cpu_addr, bar);
+                                         cpu_addr, bar, size);
        if (ret < 0) {
                dev_err(pci->dev, "Failed to program IB window\n");
                return ret;
@@ -265,7 +266,8 @@ config_atu:
        else
                type = PCIE_ATU_TYPE_IO;
 
-       ret = dw_pcie_ep_inbound_atu(ep, func_no, type, epf_bar->phys_addr, bar);
+       ret = dw_pcie_ep_inbound_atu(ep, func_no, type, epf_bar->phys_addr, bar,
+                                    size);
        if (ret)
                return ret;
 
index 6d6cbc8b5b2c67b111d7e08006a833abc34d46b7..3c683b6119c39950dde45a6f7af3381533a50776 100644 (file)
@@ -597,11 +597,12 @@ int dw_pcie_prog_inbound_atu(struct dw_pcie *pci, int index, int type,
 }
 
 int dw_pcie_prog_ep_inbound_atu(struct dw_pcie *pci, u8 func_no, int index,
-                               int type, u64 cpu_addr, u8 bar)
+                               int type, u64 cpu_addr, u8 bar, size_t size)
 {
        u32 retries, val;
 
-       if (!IS_ALIGNED(cpu_addr, pci->region_align))
+       if (!IS_ALIGNED(cpu_addr, pci->region_align) ||
+           !IS_ALIGNED(cpu_addr, size))
                return -EINVAL;
 
        dw_pcie_writel_atu_ib(pci, index, PCIE_ATU_LOWER_TARGET,
index 347ab74ac35aa4f061bc9d1f27c97e5fb9749107..fc08727116725071b49445e9587f61027d09d6f4 100644 (file)
@@ -491,7 +491,7 @@ int dw_pcie_prog_outbound_atu(struct dw_pcie *pci,
 int dw_pcie_prog_inbound_atu(struct dw_pcie *pci, int index, int type,
                             u64 cpu_addr, u64 pci_addr, u64 size);
 int dw_pcie_prog_ep_inbound_atu(struct dw_pcie *pci, u8 func_no, int index,
-                               int type, u64 cpu_addr, u8 bar);
+                               int type, u64 cpu_addr, u8 bar, size_t size);
 void dw_pcie_disable_atu(struct dw_pcie *pci, u32 dir, int index);
 void dw_pcie_setup(struct dw_pcie *pci);
 void dw_pcie_iatu_detect(struct dw_pcie *pci);