public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] LoongArch: Align pci memory base address with page size
@ 2023-06-02  3:07 Bibo Mao
  2023-06-02  4:11 ` Huacai Chen
  0 siblings, 1 reply; 11+ messages in thread
From: Bibo Mao @ 2023-06-02  3:07 UTC (permalink / raw)
  To: Huacai Chen, WANG Xuerui, Jianmin Lv
  Cc: loongarch, linux-kernel, loongson-kernel

LoongArch linux kernel uses 16K page size by default, some pci devices have
only 4K memory size, it is normal in general architectures. However memory
space of different pci devices will share one physical page address space.
This is not safe for mmu protection, also UIO and VFIO requires base
address of pci memory space page aligned.

This patch adds check with function pcibios_align_resource, and set base
address of resource page aligned.

Signed-off-by: Bibo Mao <maobibo@loongson.cn>
---
 arch/loongarch/pci/pci.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/arch/loongarch/pci/pci.c b/arch/loongarch/pci/pci.c
index 2726639150bc..1380f3672ba2 100644
--- a/arch/loongarch/pci/pci.c
+++ b/arch/loongarch/pci/pci.c
@@ -83,6 +83,29 @@ int pcibios_alloc_irq(struct pci_dev *dev)
 	return acpi_pci_irq_enable(dev);
 }
 
+/*
+ * memory space size of some pci cards is 4K, it is separated with
+ * different pages for generic architectures, so that mmu protection can
+ * work with different pci cards. However page size for LoongArch system
+ * is 16K, memory space of different pci cards may share the same page
+ * on LoongArch, it is not safe here.
+ * Also uio drivers and vfio drivers sugguests that base address of memory
+ * space should page aligned. This function aligns base address with page size
+ */
+resource_size_t pcibios_align_resource(void *data, const struct resource *res,
+		resource_size_t size, resource_size_t align)
+{
+	resource_size_t start = res->start;
+
+	if (res->flags & IORESOURCE_MEM) {
+		if (align & (PAGE_SIZE - 1)) {
+			align = PAGE_SIZE;
+			start = ALIGN(start, align);
+		}
+	}
+	return start;
+}
+
 static void pci_fixup_vgadev(struct pci_dev *pdev)
 {
 	struct pci_dev *devp = NULL;
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-06-05  0:50 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-02  3:07 [PATCH] LoongArch: Align pci memory base address with page size Bibo Mao
2023-06-02  4:11 ` Huacai Chen
2023-06-02  6:48   ` bibo, mao
2023-06-02  6:55     ` Huacai Chen
2023-06-02  7:35       ` bibo, mao
2023-06-02  8:00         ` Huacai Chen
2023-06-02  9:39           ` bibo, mao
2023-06-04 12:33             ` Huacai Chen
2023-06-05  0:50               ` 毛碧波
2023-06-03  3:37           ` bibo, mao
2023-06-02 19:19   ` Bjorn Helgaas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox