Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp770437lqb; Fri, 24 May 2024 12:35:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUkRv4FnAKxlHY8dtLNgfkcTDIXubkaPvVxhhcgSNKfMjRGWLtxmvQrMcEelxImVaeq/hb93sulPyhjrE0xvfybN555yCmk54anrG4rmQ== X-Google-Smtp-Source: AGHT+IFnG5EaVuDAOFMdkoafMLG5xp/Nl7kwOeB8lbi4f45kyolpCx7QTGZRhk+FNmoIeYPBfuJs X-Received: by 2002:a17:902:ec87:b0:1e4:17e4:3a30 with SMTP id d9443c01a7336-1f4489400bfmr38901625ad.31.1716579341070; Fri, 24 May 2024 12:35:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716579341; cv=pass; d=google.com; s=arc-20160816; b=u3G/OLdL7hs4tmNeW5l+7SXnKvnqnOLRoSFxEzI0PT1CYqXQ3vXqb+sAMi06r6PcJg rd9UQK+hvWsF6H4epX081/jJ6B3h2fIZDPLh0Wuwv0bbR9giIghs76/Yk0/v6yFwmTmC ztvywa7JHtKvTURCP8G+SPLj/CiY6fi+JmjiloZT11KA6OYNdZU8j2HMwRorR94WMu44 eYgYprNbk5kSBJvWu2aJdQ6Us/H7/LgdzYIWedh4Fc65iksTtYzPXKv/Za4uFzRZnSdO djXAM+aEUBTVEWizfGhd+nyv6k2Cl7Tr3FBOhWHZNiCccakq6rW3rbMeF3/vU1x7/5JC va7g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=sgmIcC/shr4A/zWz9MZMwn+W/sUs7tQkcY5o3763qNA=; fh=jdHqJfIZ6eS1bv+/l2ju+Iu/9du55OMEkklPKC4ZLKM=; b=Nfb46T6iq7phRdISJ2pyc61ND4F+obFwTrW77opfDDtItfN/HrqV22EEyt6unb71fx rvTvjjcts4og/UrKfAUWLzoe9GcAH2xuqWFNcQTrkpqVZt0e0R2ajNXkHvTuF2BN/H0z UQyxwebYgcJ3bPf1mbwhMSBumWp7ZWdlaFV8eafdYGdngVqcXHQAYbVpbR+b2jZT+anZ kyN+UEncQYo4r4U08to7V0e2bDNsUhewWYz/GPiwbTwBDOY6qmvNsfF8Eq/DDLguGM18 Xes7+pl83xxqjv/vhZRs7L5N+mbJ946xvW2OoGWlckqey8R7Y5Q9LeFlDDp/3kkXJxbg 8R5g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=aMPRiiiA; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-189051-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189051-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f44c789007si18307845ad.15.2024.05.24.12.35.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 12:35:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-189051-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=aMPRiiiA; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-189051-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189051-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id B279428145A for ; Fri, 24 May 2024 19:35:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B582A84D05; Fri, 24 May 2024 19:35:09 +0000 (UTC) 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="aMPRiiiA" Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 946597691F for ; Fri, 24 May 2024 19:35:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716579308; cv=none; b=BCbzNvLSEZuciiF+NeQxPJhHZWE8rki7IIsF9KeuknQ7+fUjOOKqbjCYg76iZH9rrUoTSE/qZ5d/YiwIgLQ61WwTknT/lKA2G9M/IbmvdXwrWTQ89g62uDflhGcJbF3P8Z3QPUL9cghjea2iBN52erhyIz+eRsgNDYWXDLncB2k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716579308; c=relaxed/simple; bh=HoFwp2NvrZ/kgOo1bSjj3elxDpYa/qQz+6YWpjmIG1I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=mUsMghddERz6VxfW1phW9pmmqSJvvAZqUh2DyJwnV9Jq3114VLREBW6+HVTShxBPethKxJlpCUEws86Yu2M3ptEjutWhXOdBbBmy/lkir+BkV9qrwxDpUiI+xdrx14Da9SVIK1H8btRLN2tkTvNr3bzH5jW8Nal3+zRmmsNZyIc= 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=aMPRiiiA; arc=none smtp.client-ip=209.85.210.175 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 Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-6f8eba8f25eso1072926b3a.3 for ; Fri, 24 May 2024 12:35:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716579306; x=1717184106; 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=sgmIcC/shr4A/zWz9MZMwn+W/sUs7tQkcY5o3763qNA=; b=aMPRiiiAGmV4dHWBE69ePzBzMD0W1hlDho0jJTslxzBz6LBzLFkEO01/TYQODqYqcM a+i4rf72FJ9be8ALZZSb4TygvezY9W0oHRLvFDrh14/ZGAhd2mgMtGDlx/nGLdAfpIz6 bRz9RUFIymH/auy5ymkvvgS5fH1Chxn/r2Ql+X04Ki0vjBRAz8z/Pf6bdT6OEMK5x1cP h4o5qSiRTrReQ1iv9Yaewc6oVoeiHBIj/FiXpsEG3jKu/u9OhkaLpO4VN6tzuqiUI+B7 36EuQNVKA0bWjaQaq+gGHqjfWV7ozpm8FBJwSe9SU53FtnkDdBRC7HTjb1XikhLtgrHy rmjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716579306; x=1717184106; 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=sgmIcC/shr4A/zWz9MZMwn+W/sUs7tQkcY5o3763qNA=; b=dhLNCsBX1tji5sHamYZtAz+3jB63OKxGShpYFoozOKH38EcQazmRtl3wWRnmnIGN8s CMl141PFUxClhRG/hFdv1QGfSTHeJ+WwdvvRksXfo9JGJXqJDzkm3IBn9obNmgfZ2VID mp7k3KKqlELMrFNTE13O/BRsjcTshjgTysRucnF8NnkeO4YHB1ZnW00272alCfVf9t09 vtzx95egNDToE9DR9zyH9rdP5YJBT4Jjc7Tyr0HjN1CWBY5bZkn8RM4k/Ei6aU7e57c5 oLDG0pHibmS81POJ+aZw83yQ6IXVjODGZ6kqwwYsssU/1sbaTnrwj1eeboHrXojCVSmx 7Tsg== X-Forwarded-Encrypted: i=1; AJvYcCWwiVcCZXu67f7aPwyZugZ+ttEmbin218iYcANBCwvcaldhrMnbFoyjrc7S87G/u/YbneKqzG7mx6qPZzTXOPXCch43tVqRgMB1RZ2B X-Gm-Message-State: AOJu0YyMrqvVkvbeSfHIWEmfmDdUmiPFaZwezfwgViXzC4DlI6Dh7kDr 61RDcRRgwU7G9AMBhGbhY823jsX8+UycHkbaIoBDegH/zXW7zfjEArJM0aL3cRo= X-Received: by 2002:a05:6a21:191:b0:1af:ac8a:19b1 with SMTP id adf61e73a8af0-1b212f0e98dmr3620037637.50.1716579305770; Fri, 24 May 2024 12:35:05 -0700 (PDT) Received: from tjeznach.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f8fd4d878esm1441684b3a.190.2024.05.24.12.35.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 12:35:04 -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 v6 3/7] iommu/riscv: Add RISC-V IOMMU PCIe device driver Date: Fri, 24 May 2024 12:34:43 -0700 Message-Id: <73c0fd157ac0d2d7dedf9c7fbe1a042b2490b6ba.1716578450.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-Type: text/plain; charset=UTF8 Content-Transfer-Encoding: 8bit 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@ventanamicro.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 | 119 ++++++++++++++++++++++++++++++++ 3 files changed, 125 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 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) += iommu.o iommu-platform.o +obj-$(CONFIG_RISCV_IOMMU_PCI) += iommu-pci.o diff --git a/drivers/iommu/riscv/iommu-pci.c b/drivers/iommu/riscv/iommu-pci.c new file mode 100644 index 000000000000..0a60e068fdc9 --- /dev/null +++ b/drivers/iommu/riscv/iommu-pci.c @@ -0,0 +1,119 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright © 2022-2024 Rivos Inc. + * Copyright © 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" + +/* Rivos Inc. assigned PCI Vendor and Device IDs */ +#ifndef PCI_VENDOR_ID_RIVOS +#define PCI_VENDOR_ID_RIVOS 0x1efd +#endif + +#ifndef PCI_DEVICE_ID_RIVOS_IOMMU +#define PCI_DEVICE_ID_RIVOS_IOMMU 0xedf1 +#endif + +static int riscv_iommu_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) +{ + struct device *dev = &pdev->dev; + struct riscv_iommu_device *iommu; + int rc, vec; + + rc = 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 = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev)); + if (rc) + return dev_err_probe(dev, rc, "pcim_iomap_regions failed\n"); + + iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL); + if (!iommu) + return -ENOMEM; + + iommu->dev = dev; + iommu->reg = pcim_iomap_table(pdev)[0]; + + pci_set_master(pdev); + dev_set_drvdata(dev, iommu); + + /* Check device reported capabilities / features. */ + iommu->caps = riscv_iommu_readq(iommu, RISCV_IOMMU_REG_CAP); + iommu->fctl = 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_CAP_IGS, iommu->caps)) { + case RISCV_IOMMU_CAP_IGS_MSI: + case RISCV_IOMMU_CAP_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 = pci_alloc_irq_vectors(pdev, 1, RISCV_IOMMU_INTR_COUNT, + PCI_IRQ_MSIX | PCI_IRQ_MSI); + if (rc <= 0) + return dev_err_probe(dev, -ENODEV, + "unable to allocate irq vectors\n"); + + iommu->irqs_count = rc; + for (vec = 0; vec < iommu->irqs_count; vec++) + iommu->irqs[vec] = msi_get_virq(dev, vec); + + /* Enable message-signaled interrupts, fctl.WSI */ + if (iommu->fctl & RISCV_IOMMU_FCTL_WSI) { + iommu->fctl ^= 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 = dev_get_drvdata(&pdev->dev); + + riscv_iommu_remove(iommu); +} + +static const struct pci_device_id riscv_iommu_pci_tbl[] = { + {PCI_VENDOR_ID_RIVOS, PCI_DEVICE_ID_RIVOS_IOMMU, + PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, + {0,} +}; + +static struct pci_driver riscv_iommu_pci_driver = { + .name = KBUILD_MODNAME, + .id_table = riscv_iommu_pci_tbl, + .probe = riscv_iommu_pci_probe, + .remove = riscv_iommu_pci_remove, + .driver = { + .suppress_bind_attrs = true, + }, +}; + +builtin_pci_driver(riscv_iommu_pci_driver); -- 2.34.1