Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp72117lqm; Tue, 30 Apr 2024 13:08:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXoETPQx6955T/K7N7OVAi629UVuUFoe8XAu+SS4dU7QC+LDhbddShPHH6132il6P/ppbKeb0qPCoCWdAKoF0gnSS4VoAAQ1ZX9eOSBmw== X-Google-Smtp-Source: AGHT+IH1BoulkefTzxus2zvp4GuO2fUvJ124W3uzNUiLon2vCbzDkXYRlCQG5W4736/lOEv+Xj4j X-Received: by 2002:a05:6a20:a12a:b0:1af:3857:e652 with SMTP id q42-20020a056a20a12a00b001af3857e652mr1193534pzk.12.1714507726782; Tue, 30 Apr 2024 13:08:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714507726; cv=pass; d=google.com; s=arc-20160816; b=Td8AdQwaSGcNdSX/uhGnAdZeWuwK3o3ocJaS/wU1KrvDHMfF8JNtHtsKiUCIsmRqGE 4unadov0JxdCL4ZuUdmH6V9uaYChAS9V07PPoAyn29mOPj50YG6XXwpDMqLauh98j9bS w21hJrt8I3cIkDzS4SMuXDUDtaf1M9eE3B2PX+VuqTzIRXAU3SJqJFNrOdAL023qSyFa fdrgORZQ3DT0643nCds02zvZi+9CUFC8Kijy1wEmSPF1pendg6k1pAlFunYf/WF5NtWB QeJ8VIZ7Au8dw/TFaewqXNF+u8ajm/aMYfjiiBkAlOHUJ5N+N/guNNt3MDDCZ2/eOgFu 5z8A== 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=1HG+I/Huo0JwwBHVVG3HrcQh/fpO8X/Q3uFltOztKy0=; fh=CzdYfUcvhwXkZN7+EtPycbG7CTUAomTFrA3Ityu98WI=; b=q8hGkR2atG3mDPKjMu6SMcC+5ccfP2HEHOlcUFTuwWiPzk7j2WFqJ0QWauLmHvYFXF 0MILipVnI1otRItxc1JfdkZNW9AFm4i7l9rTRzbFWjSW/gngNL0uOe4JKPAZmL7l/eah S/PNgvHnd3+bwLSE71LaqDB0gnJ8tHVqNNkksT7gvooRzCzr59HOFtu1k0wWmnv+aZOy 5EsQwtPQ9qlaGppNETzc/bKCczrGgd+1daofIW9iUuCjtwEpNwWBRbKQ6gLkFp3kp2IQ 4EMGY966dm63VX6yBl45o91019OuaeKtYhuvWfmStqPfBu1Mbc7WzNNt6ir0F6cdusjr X1PA==; 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=afFXWvBZ; 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-164683-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-164683-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 r17-20020a638f51000000b0060a08f24c4fsi11117825pgn.491.2024.04.30.13.08.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 13:08:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-164683-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=afFXWvBZ; 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-164683-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-164683-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 2CAAE283AE3 for ; Tue, 30 Apr 2024 20:03:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9D1611BED6D; Tue, 30 Apr 2024 20:02:47 +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="afFXWvBZ" 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 E94841A0B17 for ; Tue, 30 Apr 2024 20:02:44 +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=1714507366; cv=none; b=DVI+UAG1oxT+2tXEonZOX+51DWWFf5INNRTyNuI+snN4J0C4YksT2CvWosXf/IVnKbxMf4Dp6BrVBKNfj/nKgYFNjamQAtf9UK7EC3ovZlngNqVRsvFe7fXFgWmInLY4VJlgjuCyMUMaBp8nes0yK9PpgOJiRwcW1sYTCKe6SXI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714507366; c=relaxed/simple; bh=Gpr24MQX+gy8NsJj3SH41pQY+ZPfFqLd1KvwE+z56Ng=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=rkvSfn1Gcyvp7d2YNZ+ITDmpx5xKCoFHp7pz4urxwg0W2anI++KiZPA6ebtoeg+suCsu2+owz7f3isKs1YrSsKljmw3LKx08GaYXq1Qc3SLrWb9ZjWdDmxJfsKtI9rOBImfYeDlKd+CGryNjYI3R1xMY4AyCVxg5N9kSSwMCkMo= 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=afFXWvBZ; 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-1e86d56b3bcso56391545ad.1 for ; Tue, 30 Apr 2024 13:02:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1714507364; x=1715112164; 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=1HG+I/Huo0JwwBHVVG3HrcQh/fpO8X/Q3uFltOztKy0=; b=afFXWvBZLHLJkuS7vS81Dv3FsmTDUM9RglIwEWDdTplMo+CLdVMVcqQ1V959IGKFr2 oIfbK2kyI4RtsxhaIRjC5hr4pCP/DJFXXFngNDSJ55a+W6rMp773atQDklS+zszddQIF 6zEdX1nbKu5iz+zGgnEfWrTz1Xma4JCj3FEkM0LnC1GhA9lkfZMVKtl0o58WSM2qx+hw sI8iCq2nxk82PELefpbeWf08OHOMtbnnTGlAXVIOsmuKBJ9CcvZhnwN9N3rWNnnOG+sC bi2h6VDPrMP+7OKw181vdB0+3GfxKKRzoPPytpx1h+DfRKJsMWqnrxweQwPQx+oEFkIm bD3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714507364; x=1715112164; 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=1HG+I/Huo0JwwBHVVG3HrcQh/fpO8X/Q3uFltOztKy0=; b=QIV2pnPDFEFrTROPFzdN5TsY6qODjuuhVZlnVRTGGXkdZoA1166RdBx5gtYnbCoDNJ vuvqLW0jt2DwD2uWqb/GhA3sM2NqY6KoUWwCgD8TA6LTV3v4hJ+xWZJZfAtotvvRpK6O YOaDCgFceuC5mp8hNQNiuoUYU4xx5LWvvdYeKPelr5kBZyXoP63moBl7+TFlU1Dz2+Pp +14pVlWfC+JMGUWdtGoFDlFnIFGY932IAp6HztPefGVuLTnSSG8LKo3E7Fh9mXwcYDh8 wJ3pMCMKQrlU33y38mHuky28gHftHmmQ5DCoCuC2Om0+6YMFVhHb/6pZly24iOqnFgth fTNQ== X-Forwarded-Encrypted: i=1; AJvYcCWX+PNYHHYgzPJgjGTjhU9YTTOAzrSrqNp8EznPkxq6ZjBjS5OoH/7gJfwOAHhymlhHVURv1RiUZyT/YeYJ3MrnYEV/n64NdabA50k2 X-Gm-Message-State: AOJu0YwafjZCSkIvxjEAXxcFgt3YSB7Q8o+2gQX0Bnnhy8PYYoFsr2qL KpXL3Lyj1XQLcCN7RZS8T/uPYcBHNFhXZWfNeJx5Bj2ZTGnejAbBzuEMK51MDPU= X-Received: by 2002:a17:902:6e02:b0:1e4:2d13:cf68 with SMTP id u2-20020a1709026e0200b001e42d13cf68mr447303plk.17.1714507364266; Tue, 30 Apr 2024 13:02:44 -0700 (PDT) Received: from tjeznach.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id w19-20020a1709027b9300b001e435fa2521sm22809820pll.249.2024.04.30.13.02.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 13:02:43 -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 Subject: [PATCH v3 3/7] iommu/riscv: Add RISC-V IOMMU PCIe device driver Date: Tue, 30 Apr 2024 13:01:53 -0700 Message-Id: <3a04bd180fd510cb90e8d8dba5fb60f207e5e83f.1714494653.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 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