Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1892110lqm; Fri, 3 May 2024 09:15:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUA8xLhw5RDjS+cuWe34W6V1+//G30stvPfBShvGgXz3caSoQ68muGegQ129eqMKqfO3dYI3TfAOk44SH8M7iPdTsyMGq4rqQE6FQnV6Q== X-Google-Smtp-Source: AGHT+IHvj4ntEyU3zaeDoIX0rvhYgWQcBPNJc9+BOKyrqAdqyx0SfV0U8BsTV3pxsOkKn5ZOXaka X-Received: by 2002:a17:902:7409:b0:1e0:a22a:623e with SMTP id g9-20020a170902740900b001e0a22a623emr2760076pll.21.1714752957916; Fri, 03 May 2024 09:15:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714752957; cv=pass; d=google.com; s=arc-20160816; b=Zy3TREA1SfxYkchHc3s+39Du40+9AdWcKZaZNLktoEzU4mR9g9tFduagAbZHKNq2zV c60JWeinJiRmdNiACB8at7hrNcSYozJ9VpaiiHdS6FRblYg14r2Z6DX50Alefdcpcphz 7yNl3/52Rt91d4BcmHcT3Z4U/fuka8T21U73w0HYOmyX4spmYtAEMuEUjUBfcib4ZUb3 3x4+1XphyrwVp8LawFQ3VLanto05kK33OEaqFK3OlW1qn90P0QUX+QqVIwjTEv99+mKC U1OQ5WPsPvonj2a/eP7164iI/mUQPYxQRmwhoetEafcszz0cieN9N5bV8ppUfH9qilH5 DIsA== 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=TYFk5/b+aOUw3mGtrPvaq+gBSGRl/cp0Y63BjuVf1cE=; b=ny/PbOCv//2yhsMGjKVY1xP4YDB2BxyEJxRbKo+KOtY5IhulqdqEdLczMIxGzTDf/H fMCIolOkP/fJnWZA+KuevqSAxJH+wg4Vvs50/Y7VCcOHASeX1Ss2PR7ib2e1qNgBkz/d m/U0A5G653kIkGNaT0sbTbgv2NCGiRJHvLxdlvO8QHI9mIQ5dRKd1o+kXV0EFhAc/Hlm jdBVufny8YiqoHvLIOPMHkzUEE9ncZTz9EauKEYyaD85lR3+o/65+mkzYpw/odSVOVZx 5F7VEdCdcgtnh5jn5gTsHe9yCB/t0qVGRrmxt0EDpllKIr/OhAsSn4AJ/qYcmroDtIWF Szmw==; 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=P9T8VE5Z; 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-167881-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-167881-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id k12-20020a170902c40c00b001eadb402587si3347287plk.339.2024.05.03.09.15.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 09:15:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-167881-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=P9T8VE5Z; 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-167881-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-167881-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 2C34AB23534 for ; Fri, 3 May 2024 16:13:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E6CF315533F; Fri, 3 May 2024 16:12:57 +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="P9T8VE5Z" Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 3C48A15625B for ; Fri, 3 May 2024 16:12:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714752776; cv=none; b=HJsdODgn3XfqwtHdIeTYpj8ZNl+20r4ESlmEPYHe0CSvzQT9lL6u8LhHN/S4Bg0uluM/f4fiLDea2Vqy47qMACGqTkVbYf7e6Fr+IlJ1Jv9VsH3IT/8i7c0ofb/I2rEeJag/ZYqiKKo+IacS69ts25K1LEIwPoDPTBnBXTtfAfo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714752776; 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=Uuv+8W/2YQneV+Xr1rZGRzwEOA2eOhjV4tVw1pPixdMTpby+AF7ai07lYsJWKe47W0TsbzD8ayzsnKjM5mO9BiPBsTvUzuyM5aScTWrBB4t9oO1RsaLdIZGmFpF7qv0Qs6UyyZpyoOlh82zD+Ut5QCv1g8GoALHggjHgyTG22P0= 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=P9T8VE5Z; arc=none smtp.client-ip=209.85.214.170 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-pl1-f170.google.com with SMTP id d9443c01a7336-1ed0abbf706so14253495ad.2 for ; Fri, 03 May 2024 09:12:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1714752774; x=1715357574; 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=P9T8VE5Z03zA7oYBOZAMgC9r0V111O0L9wVr8vqg2xBmZfxefbJ7RROFKvKSiR5RKW Fio4iDdBeaPyZfS8mSiIF1TZCE8Vbng5yTMet2BTRs7VFhRVYoKdP92ELUfxhW/cFhmr x6TOoJlJU6ihDIZe08XFk3etjy0laOOT9YaWBZ//LzLkSMmfd2smw5OprdRabmjYy9bj R4KuYyjV8KIhvRtx3oKuOFCt4032wOFkA4bLXVBw7E8fMut0RbwWvCpyh47ZgefAHW1B iOeKuIooH3Si5PkNRB9A3Ps8l0gSLmoO2DPtBpxrLNpZb8mQ2stn+iTUiKTy42SaZJSn uV3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714752774; x=1715357574; 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=VCqa2F2iQCUNAUV61hKj2lsryadSjQ+Ma8QyBmk2uppML4yjd1Ts9Lg6rTsLnadb1+ 1zF+2RHkK/jv9ScLWvLGowvawaD76NnLXKvpB8YQ66R/Tps5v/Vjpkjo9FF6kGTzWx4b POkOVlEgl/CH8DyjsHXzsx/kvPDkuJao8mzHkZdwulm2pKT4Q3MMkZ0k6NuWe6ydiyC7 ixhPC3ANUNEGtiJDb4Bmv/doxPwg1QxCD6k3zKQZnQfxbb5u3o22Bwzy6SFL43kfketA RYiO7lQX/karAKAIbhDpZyrj4L7zEfpNpPvjeHYMGoSk4aviHfgN4Hz9yySTmrvheQty 98xg== X-Forwarded-Encrypted: i=1; AJvYcCWQ8riGzPnexb5+33znFYLLL9OIXA7JQaFmYROghkaQI4ClSpmed4JZIpxPTQrHtmrdCthrAev4dsxXW7vqlEUvwFWkG0pItyryeEOD X-Gm-Message-State: AOJu0YzVWgiokc0UQNzxtLGZgYSi0wWApiiJ1Cw6vyqFjW8sBMjcWcc5 fqsnsxXtk7yQbWIhujOC3lCGN25XTd9JwqQCPna3wwXIgIQKrX9bNpQtXwCW2u0= X-Received: by 2002:a17:902:dac1:b0:1e2:3720:e9b4 with SMTP id q1-20020a170902dac100b001e23720e9b4mr3567048plx.54.1714752774561; Fri, 03 May 2024 09:12:54 -0700 (PDT) Received: from tjeznach.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id x3-20020a170902ec8300b001e2086fddecsm3424746plg.139.2024.05.03.09.12.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 09:12:54 -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 v4 3/7] iommu/riscv: Add RISC-V IOMMU PCIe device driver Date: Fri, 3 May 2024 09:12:36 -0700 Message-Id: <5d554e03d4fe1f4e94ef63554fbfef4608629b85.1714752293.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