From nobody Tue Nov 26 22:32:19 2024 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E4C2E199221 for ; Wed, 16 Oct 2024 06:52:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729061554; cv=none; b=jwfTZ4pYDAaesVWkZllk97NBMJ64LIGodleCtfYEuHzEUtxb9aNCzo11wl5T2Ff2+Ao2zO+GszVEFIULTaRIrDNkU4eC7Gi5gyLHfbZEe1PPs2fugfe5AEnrsxqAslm2s/1UZm8+zYxlJKt77ifkOrJV2JLrnevkkAbepZzwbvo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729061554; c=relaxed/simple; bh=Te/Q1kP4+1VYDUEulqGDVjmArtl7oE8N40xpPsWG7CI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=dXhSzJtIylqpushqPx9cbVhzF4kyrohvGCKkInCxI4rWwgEiYf54MaATxSIwou4tbHGk25nGwsq8WmQ7LxdWhw9lQeMVVKiwouIaHNuvfoIpBDdGLFLK1V0VMtWRKmozQDJTiiUUqDhiVnyc2dBHapWPnQdvUhXOzMXDDnexi80= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=105a1fLm; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="105a1fLm" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-71e52582cf8so2545525b3a.2 for ; Tue, 15 Oct 2024 23:52:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1729061552; x=1729666352; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vNvjcSMD1XB1l6NOS6ExlEo9cUT+I176k7DgI2zQ1iA=; b=105a1fLmuFn1vs4oa1y06SJFsRydgsfYVD1Rk/cFU7gRzuCh/NVLHfUjrjbq1nQieW 5fxiKxx4PVVN7DNPfnOP0A2XxFti1gOqKg5i70oIGagmlpjpykXNO/bJ55bgKbPo+4Zs rlxKAi9lojTV0L/sw/VG9sOCXN6PuDnStCrobbe+ps8HVt1qXgJxF553htPYbPg6Yg+W eDa6pQM/fnQJXWjY5WDVwHxY2Rvy29yOrmpWHQcSejfRs4L5BFE0cTsHT97g/sEwXkRw hCskDqH8kO5w/P/goJcjwqiPAjUISOKtremsi0tKbFziWLQRNJknj6tymVi10KIQX35u sXUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729061552; x=1729666352; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vNvjcSMD1XB1l6NOS6ExlEo9cUT+I176k7DgI2zQ1iA=; b=TNNsZKFAc3xQABwE69Zt7hu2YSsr1M43NLb02qtQ7+sz9cc4+1Rkd7v0Pb/wDUei3w ZsPMpJBvFOrWs42F7k3ZLKUmZ4F29t3lnyX99gXszz8vY975KAkVUY9PG4ANTe5ozGzQ UnnXESND7OKF++Frl2bv+nuHlJ2gdHN6kYQm2DVPp+1RUcJcohzlo79tiUhNTp1M27AT 2DYJDjLCyx2S910N6FtWEPWG/BCta+pdvWJu5RKCQ7kyBLfR3QwbslHWMrVs37qcqypO wlf8Bi1TIembF4zqyxWhXROR03vgUXsTDrsY9+TDIU+lStNVMqXmllfWC30FQfwfEOp+ YgWA== X-Forwarded-Encrypted: i=1; AJvYcCV6NOpgGJK1WMcs+MPGEZnQjrqfE8yYR/23KigIB8g3kRSYL9oeLDuhGFGTzwwNdI6UKa3kX/ssPGvInZU=@vger.kernel.org X-Gm-Message-State: AOJu0YznGFFKUMvF+ST52GlHt/Awlwgc8mMzCIUpf1r5voJsZCWuW9MT QL1QVVJJwPcPbcxmAdE9mX4zt0CGSapeS+PDGXLzKTXWGj0SEZVpaJ3JuE5XuK0= X-Google-Smtp-Source: AGHT+IHLEg/i40WT7t1JozAQJt5Umm33sJaAcjn4J78u5wRH1PZ4Sx7uaYZeshi+nm7rC8MxQbxbxA== X-Received: by 2002:a05:6a21:2d84:b0:1cf:2bb8:f38b with SMTP id adf61e73a8af0-1d8c9699b93mr21793045637.38.1729061552323; Tue, 15 Oct 2024 23:52:32 -0700 (PDT) Received: from tjeznach.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71e77370a83sm2410762b3a.5.2024.10.15.23.52.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 23:52:31 -0700 (PDT) From: Tomasz Jeznach To: Joerg Roedel , Will Deacon , Robin Murphy , Paul Walmsley Cc: Palmer Dabbelt , Albert Ou , Anup Patel , Sunil V L , Nick Kossifidis , Sebastien Boeuf , Rob Herring , Krzysztof Kozlowski , Conor Dooley , devicetree@vger.kernel.org, iommu@lists.linux.dev, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux@rivosinc.com, Tomasz Jeznach , Lu Baolu Subject: [PATCH v10 3/7] iommu/riscv: Add RISC-V IOMMU PCIe device driver Date: Tue, 15 Oct 2024 23:52:15 -0700 Message-Id: <12f3bdbe519ebb7ca482191e7334d38b25b8ae8f.1729059707.git.tjeznach@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce device driver for PCIe implementation of RISC-V IOMMU architected hardware. IOMMU hardware and system support for MSI or MSI-X is required by this implementation. Vendor and device identifiers used in this patch matches QEMU implementation of the RISC-V IOMMU PCIe device, from Rivos VID (0x1efd) range allocated by the PCI-SIG. MAINTAINERS | added iommu-pci.c already covered by matching pattern. Link: https://lore.kernel.org/qemu-devel/20240307160319.675044-1-dbarboza@v= entanamicro.com/ Co-developed-by: Nick Kossifidis Signed-off-by: Nick Kossifidis Reviewed-by: Lu Baolu Signed-off-by: Tomasz Jeznach --- drivers/iommu/riscv/Kconfig | 5 ++ drivers/iommu/riscv/Makefile | 1 + drivers/iommu/riscv/iommu-pci.c | 120 ++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 drivers/iommu/riscv/iommu-pci.c diff --git a/drivers/iommu/riscv/Kconfig b/drivers/iommu/riscv/Kconfig index 5dcc5c45aa50..c071816f59a6 100644 --- a/drivers/iommu/riscv/Kconfig +++ b/drivers/iommu/riscv/Kconfig @@ -13,3 +13,8 @@ config RISCV_IOMMU =20 Say Y here if your SoC includes an IOMMU device implementing the RISC-V IOMMU architecture. + +config RISCV_IOMMU_PCI + def_bool y if RISCV_IOMMU && PCI_MSI + help + Support for the PCIe implementation of RISC-V IOMMU architecture. diff --git a/drivers/iommu/riscv/Makefile b/drivers/iommu/riscv/Makefile index e4c189de58d3..f54c9ed17d41 100644 --- a/drivers/iommu/riscv/Makefile +++ b/drivers/iommu/riscv/Makefile @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_RISCV_IOMMU) +=3D iommu.o iommu-platform.o +obj-$(CONFIG_RISCV_IOMMU_PCI) +=3D iommu-pci.o diff --git a/drivers/iommu/riscv/iommu-pci.c b/drivers/iommu/riscv/iommu-pc= i.c new file mode 100644 index 000000000000..c7a89143014c --- /dev/null +++ b/drivers/iommu/riscv/iommu-pci.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright =C2=A9 2022-2024 Rivos Inc. + * Copyright =C2=A9 2023 FORTH-ICS/CARV + * + * RISCV IOMMU as a PCIe device + * + * Authors + * Tomasz Jeznach + * Nick Kossifidis + */ + +#include +#include +#include +#include +#include + +#include "iommu-bits.h" +#include "iommu.h" + +/* QEMU RISC-V IOMMU implementation */ +#define PCI_DEVICE_ID_REDHAT_RISCV_IOMMU 0x0014 + +/* Rivos Inc. assigned PCI Vendor and Device IDs */ +#ifndef PCI_VENDOR_ID_RIVOS +#define PCI_VENDOR_ID_RIVOS 0x1efd +#endif + +#define PCI_DEVICE_ID_RIVOS_RISCV_IOMMU_GA 0x0008 + +static int riscv_iommu_pci_probe(struct pci_dev *pdev, const struct pci_de= vice_id *ent) +{ + struct device *dev =3D &pdev->dev; + struct riscv_iommu_device *iommu; + int rc, vec; + + rc =3D pcim_enable_device(pdev); + if (rc) + return rc; + + if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) + return -ENODEV; + + if (pci_resource_len(pdev, 0) < RISCV_IOMMU_REG_SIZE) + return -ENODEV; + + rc =3D pcim_iomap_regions(pdev, BIT(0), pci_name(pdev)); + if (rc) + return dev_err_probe(dev, rc, "pcim_iomap_regions failed\n"); + + iommu =3D devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL); + if (!iommu) + return -ENOMEM; + + iommu->dev =3D dev; + iommu->reg =3D pcim_iomap_table(pdev)[0]; + + pci_set_master(pdev); + dev_set_drvdata(dev, iommu); + + /* Check device reported capabilities / features. */ + iommu->caps =3D riscv_iommu_readq(iommu, RISCV_IOMMU_REG_CAPABILITIES); + iommu->fctl =3D riscv_iommu_readl(iommu, RISCV_IOMMU_REG_FCTL); + + /* The PCI driver only uses MSIs, make sure the IOMMU supports this */ + switch (FIELD_GET(RISCV_IOMMU_CAPABILITIES_IGS, iommu->caps)) { + case RISCV_IOMMU_CAPABILITIES_IGS_MSI: + case RISCV_IOMMU_CAPABILITIES_IGS_BOTH: + break; + default: + return dev_err_probe(dev, -ENODEV, + "unable to use message-signaled interrupts\n"); + } + + /* Allocate and assign IRQ vectors for the various events */ + rc =3D pci_alloc_irq_vectors(pdev, 1, RISCV_IOMMU_INTR_COUNT, + PCI_IRQ_MSIX | PCI_IRQ_MSI); + if (rc <=3D 0) + return dev_err_probe(dev, -ENODEV, + "unable to allocate irq vectors\n"); + + iommu->irqs_count =3D rc; + for (vec =3D 0; vec < iommu->irqs_count; vec++) + iommu->irqs[vec] =3D msi_get_virq(dev, vec); + + /* Enable message-signaled interrupts, fctl.WSI */ + if (iommu->fctl & RISCV_IOMMU_FCTL_WSI) { + iommu->fctl ^=3D RISCV_IOMMU_FCTL_WSI; + riscv_iommu_writel(iommu, RISCV_IOMMU_REG_FCTL, iommu->fctl); + } + + return riscv_iommu_init(iommu); +} + +static void riscv_iommu_pci_remove(struct pci_dev *pdev) +{ + struct riscv_iommu_device *iommu =3D dev_get_drvdata(&pdev->dev); + + riscv_iommu_remove(iommu); +} + +static const struct pci_device_id riscv_iommu_pci_tbl[] =3D { + {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_RISCV_IOMMU), 0}, + {PCI_VDEVICE(RIVOS, PCI_DEVICE_ID_RIVOS_RISCV_IOMMU_GA), 0}, + {0,} +}; + +static struct pci_driver riscv_iommu_pci_driver =3D { + .name =3D KBUILD_MODNAME, + .id_table =3D riscv_iommu_pci_tbl, + .probe =3D riscv_iommu_pci_probe, + .remove =3D riscv_iommu_pci_remove, + .driver =3D { + .suppress_bind_attrs =3D true, + }, +}; + +builtin_pci_driver(riscv_iommu_pci_driver); --=20 2.34.1