From patchwork Thu Apr 18 16:32:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tomasz Jeznach X-Patchwork-Id: 13635173 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A0B98C41513 for ; Thu, 18 Apr 2024 17:46:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=8EpGK1UlfcgUu0/gJnXsZ4O/F+6b/PZar16NhFH2hR8=; b=JcsKYLJsQn+Vcm 9Ts8vM5w0qoYvE+qsU+IilI3HJCyaR9U99+Eom+2tLwsRPB7/bCR565oCdJt1i76cOWPSx3ocb8R3 ZIx/uEvNCcCw9r+ZY87DPKj+FwYebjwn/71OW5fc94a7mJZqph1JiNxFn4k+be5VvZ3BF6pX/yqXw Q3tCDiqt81h3ETnZKT948I6Knje1oYohxSDTrPXcMvmHuAPSjSQWLz1KWRauPXEwBGH03xwooRNfO /RjIkz4B3aTzqCAQd1MRwiESrPT5q5AYh27S2zJceuy05/dC1Y4/bmzGYUWtIKFQohkM/9BlUUpwR A+msxXHPz2/YTZ+ETk1A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rxVq3-00000003EwT-2Zg5; Thu, 18 Apr 2024 17:46:23 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rxUgo-000000031Xa-03D2 for linux-riscv@bombadil.infradead.org; Thu, 18 Apr 2024 16:32:46 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:Content-Type :MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:Content-ID:Content-Description; bh=FHKal/vvH8z/ottSej913pkTRK3eYWZvnHgxtvD4TF0=; b=QMiuorILjjnBPF/wEIkhecHEWg iXCV8hTeGZjW15n0BJ1f27wNTM3Xie0TRt4TPf/Ga0shXnLmb8Vn0Kg7Aq8057Wfdh95syb0LeS+6 c6FtteeBXZL8g2vZCux8ktiqI983YPWwmTMPN9pT2n8QwGAZlxvcPy8cTafg0jytCvfJpHxqTACrv +Bpp4M3Fc9xmBTv79OElC3cedR4tJ1JW2z6rFTGzY4Rg2QF7ZRXEVOCv377JXeWppADcPiTlztHor CEUDT1apLygVG6ePlC6VlHM5dvzjkrT1G0gfN2alfhZWDr+yiZ5eHupZWn52/tjfIn9WjWLV6z+qh XQKcEOLQ==; Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rxUgi-0000000C4rp-0RPy for linux-riscv@lists.infradead.org; Thu, 18 Apr 2024 16:32:42 +0000 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-6ee12766586so899267b3a.0 for ; Thu, 18 Apr 2024 09:32:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1713457958; x=1714062758; darn=lists.infradead.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=FHKal/vvH8z/ottSej913pkTRK3eYWZvnHgxtvD4TF0=; b=uUDs0qkJvN1z/Xy6ow2lwhSi59swHFCNi2WljjZVY8hFq/YigSn3xOaGJL+ulISabg KyE4aCb9yBwWSW5d+Cj89q2CKyPmdF4pqPr8LjqlwdjvJy6o6A9YbR9sRhe6zDVYNmAF XX248MGtJRN4gFIRg7PvVeN2/y8G4ePW3g9hi7qF8VBDdnW2nhaQ/MW+fz37pbIcPxxr uXrSkYBAe2tfm1ZpxG3cyftLvTZLwuSHMBqFQ/HKH/boOwqUqGv0TqGDkv8hJWgjxs24 oJrPuzE9ddlzjD6X4BtoKstPyEeT0pIMJJopexRpTJNWD+Yn4R9IlCo4adfQ/6wWPlwQ XSiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713457958; x=1714062758; 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=FHKal/vvH8z/ottSej913pkTRK3eYWZvnHgxtvD4TF0=; b=IjZGYW9X1ZuOFf6qTLE7vu8T176zavU9iBWXO5ScBNrPoXPlFn5xONP71H6D3CzjN1 oYUBEi0/WeHMgxfe3DlEldaAePoi0Kuw2J/KdZZVZyq9eRSXH6dA0Kiqs9sPXItYjMLV nsnBxo0Ygl2yXkkDuCucdH/Vc+i1x4bACwlCNPjIxko4R2Mb2dkuwWTISapteQ7cfYnH OwWopcfAOM30Dn4PzQVidRqAJPx2x3bl7ifn+PExe423dWwDlygyQC8gnh3PArJPpNdE RsQiDuWIHRU0eI1zY97pF7vbI0ikRU/ws/OVvJnQSsMWKvG0Qh+NCX51H1+BgKO0cCEU riJQ== X-Forwarded-Encrypted: i=1; AJvYcCUTDN8hHJHkda9+9zSziY2XvkPnMUDYjMaQIHXoYgwL9V77jUxSZ7rP/Vy4bXAvkR0KSPNI96q2FFHFlro4XP+h0EPmff15qFCBcjpzPuaw X-Gm-Message-State: AOJu0YyIs7NBMmMNEOZdzJo4ZMjNFPPQ/KbvoveGiey6lYPUKHwnOIcT FNsjFyk8v9AlJN/rDXhdGpD0Mi4QsCEBr/UBfwSD2Eth4bA79vCFVo/7KWgvqCk= X-Google-Smtp-Source: AGHT+IGBYbl1wbHIwQ3lWmMx5ZaQ5vB8SSg4htQp5CGoXofTgAD73EDRAgHIKteI0VW5uOioE9zfaQ== X-Received: by 2002:a05:6a00:21d0:b0:6ea:c7bd:90e3 with SMTP id t16-20020a056a0021d000b006eac7bd90e3mr4466325pfj.14.1713457957754; Thu, 18 Apr 2024 09:32:37 -0700 (PDT) Received: from tjeznach.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b19-20020a056a000a9300b006eae3aac040sm1674755pfl.31.2024.04.18.09.32.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 09:32:37 -0700 (PDT) From: Tomasz Jeznach To: Joerg Roedel , Will Deacon , Robin Murphy , Paul Walmsley Subject: [PATCH v2 3/7] iommu/riscv: Add RISC-V IOMMU PCIe device driver Date: Thu, 18 Apr 2024 09:32:21 -0700 Message-Id: <4a068da7a7a984a43d419c52310f8ab7d91da46e.1713456598.git.tjeznach@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240418_173240_837463_278C220C X-CRM114-Status: GOOD ( 24.15 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Anup Patel , devicetree@vger.kernel.org, Conor Dooley , Albert Ou , Tomasz Jeznach , linux@rivosinc.com, linux-kernel@vger.kernel.org, Rob Herring , Sebastien Boeuf , iommu@lists.linux.dev, Palmer Dabbelt , Nick Kossifidis , Krzysztof Kozlowski , linux-riscv@lists.infradead.org Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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. 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 --- MAINTAINERS | 1 + drivers/iommu/riscv/Kconfig | 6 ++ drivers/iommu/riscv/Makefile | 1 + drivers/iommu/riscv/iommu-pci.c | 154 ++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 drivers/iommu/riscv/iommu-pci.c diff --git a/MAINTAINERS b/MAINTAINERS index 051599c76585..4da290d5e9db 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -18975,6 +18975,7 @@ F: Documentation/devicetree/bindings/iommu/riscv,iommu.yaml F: drivers/iommu/riscv/Kconfig F: drivers/iommu/riscv/Makefile F: drivers/iommu/riscv/iommu-bits.h +F: drivers/iommu/riscv/iommu-pci.c F: drivers/iommu/riscv/iommu-platform.c F: drivers/iommu/riscv/iommu.c F: drivers/iommu/riscv/iommu.h diff --git a/drivers/iommu/riscv/Kconfig b/drivers/iommu/riscv/Kconfig index d02326bddb4c..711326992585 100644 --- a/drivers/iommu/riscv/Kconfig +++ b/drivers/iommu/riscv/Kconfig @@ -14,3 +14,9 @@ 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 + depends on RISCV_IOMMU && PCI_MSI + help + Support for the PCI 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..9263c6e475be --- /dev/null +++ b/drivers/iommu/riscv/iommu-pci.c @@ -0,0 +1,154 @@ +// 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 + +#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 = pci_enable_device_mem(pdev); + if (rc) + return rc; + + rc = pci_request_mem_regions(pdev, KBUILD_MODNAME); + if (rc) + goto fail; + + pci_set_master(pdev); + + if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) + goto fail; + + if (pci_resource_len(pdev, 0) < RISCV_IOMMU_REG_SIZE) + goto fail; + + iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL); + if (!iommu) + goto fail; + + iommu->dev = dev; + iommu->reg = pci_iomap(pdev, 0, RISCV_IOMMU_REG_SIZE); + + if (!iommu->reg) + goto fail; + + 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: + dev_err(dev, "unable to use message-signaled interrupts\n"); + rc = -ENODEV; + goto fail_unmap; + } + + /* 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) { + dev_err(dev, "unable to allocate irq vectors\n"); + goto fail_unmap; + } + for (vec = 0; vec < rc; vec++) { + iommu->irqs[vec] = msi_get_virq(dev, vec); + if (!iommu->irqs[vec]) + break; + } + iommu->irqs_count = 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); + } + + rc = riscv_iommu_init(iommu); + if (!rc) + return 0; + +fail_unmap: + iounmap(iommu->reg); + pci_free_irq_vectors(pdev); +fail: + pci_release_regions(pdev); + pci_clear_master(pdev); + pci_disable_device(pdev); + return rc; +} + +static void riscv_iommu_pci_remove(struct pci_dev *pdev) +{ + struct riscv_iommu_device *iommu = dev_get_drvdata(&pdev->dev); + + riscv_iommu_remove(iommu); + iounmap(iommu->reg); + pci_free_irq_vectors(pdev); + pci_release_regions(pdev); + pci_clear_master(pdev); + pci_disable_device(pdev); +} + +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,} +}; + +MODULE_DEVICE_TABLE(pci, riscv_iommu_pci_tbl); + +static const struct of_device_id riscv_iommu_of_match[] = { + {.compatible = "riscv,pci-iommu",}, + {}, +}; + +MODULE_DEVICE_TABLE(of, riscv_iommu_of_match); + +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 = { + .of_match_table = riscv_iommu_of_match, + .suppress_bind_attrs = true, + }, +}; + +module_pci_driver(riscv_iommu_pci_driver);