Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp1217662lqs; Fri, 14 Jun 2024 22:28:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWNCoUpPDcMU/2REZ2YsEnIDFO7bKzVjbC+7BXHO9zO21Ly6bxG0RYaBmmUfzPnvyJ2pWysZZoufrvjoZm/TuBgAwnVpLf5k7fqdm/a9g== X-Google-Smtp-Source: AGHT+IHNHe7UKKyOENC92pZT9M2liqvkQ4uiVO7Zwq6c944tFHUU6foLIOuiG0cedi7jvejTXkLF X-Received: by 2002:a05:622a:311:b0:440:5809:a545 with SMTP id d75a77b69052e-44216b1db75mr53407741cf.46.1718429323842; Fri, 14 Jun 2024 22:28:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718429323; cv=pass; d=google.com; s=arc-20160816; b=HF/IQZinKiIiIGmHKsksnUbYjUW4Hvs/u102LVfo7HgweRzjdOlu9qBE400VMmL8no 2+zZiy+XRogsxrK+dReqyWiLhfu0dx++E/4/LAkjQEAREWC0uO8WJRcVQ771if9aRwv9 MIDG0raZQHDzCaVFubpQ8umeJ6CXZDZDkkJwzVfIPpARPtVCdbcPgwdwf3ZTK8yjB9T4 GLKmLdh2hKYPTW5Eg2XoFyo7j4U0tTqkeRSohGpo7FFN7eRI3G6Ly1fErIBlaVpCZPhc z4rAk/lYqpx3YWgcrdtRIECSzCP1i/d1KcuMXAR2ngBQfHNVTt/OdN5FSLG1DslGiIeu OCng== 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=bZ/AEFSFwdT9H/GgMZYFdqgb9wNJbl1T6o8nBqHR0q8=; fh=JeE1mnlPjeK0SsSBRg+rRuXOyeyL0xtSrc7NR3KxGqY=; b=NCplbQx0OI6lWCjwIPaLQjpHYy3pqjMohMZtxcphl86Lp1pyF0A4K1KmV8kweqexW8 DgenKiIsVIF1ptZxaz1h2ZoiiKCB7XXAtUlk0AusynIAiXH7ZPGyaS68uCgyPPZ8NIK7 uwN0pVexOSx+szV/MOzJbOyaSbIl2bInOgT2X+JN1YbcVMaSKAzmDUiHmbGH5rLWdWpR m7BOlM6tQA2SpS0AsclK2TLlkRTXfnfkZwPZcCz8OM5wL6oq8aMUiVemAyL6r3i/3IYV tHyARk9JNxojJC7cGm2TZHWpGq8+JeF+trY86S0/H6mETBarDH32hUhy94B/II32xalL hEAA==; 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=czYgHKte; 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-215709-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-215709-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-441f2ea832bsi57604301cf.238.2024.06.14.22.28.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 22:28:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-215709-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=czYgHKte; 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-215709-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-215709-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id BF5EE1C20308 for ; Sat, 15 Jun 2024 05:28:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 06AE118637; Sat, 15 Jun 2024 05:27:59 +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="czYgHKte" Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) (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 C62DF175AB for ; Sat, 15 Jun 2024 05:27:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718429277; cv=none; b=XxuvBcwKt4sMghXTNHLwIyTL4iQKzYvm0MlX4hUDj1yDv7rQial2ImoJfOW7Fu8lHE2fogOb1iXdegIrvwRxlHXqxyBhUWVQ3/W9qjFNZp401sOimaDzzUERHQyyg13ML8/Xa7BexAMwVJTuJp7a8M3QDpod9a4wyGFsfjgE8GY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718429277; c=relaxed/simple; bh=DcOSw2ew0c3URNrUO3l1Ipvi4bfVqwYuFrqb7c+A5RM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=aevXMtv/0ZDLsQjWmrG4QSC1spSx0hFSM7P2Nxzff93MtHpKZajhMR/4ybH71qELfJm964UjIiEOth2Ow5bvyJZC9yz8+qmjnQf/+dyq3/6W/kDzyERoh9CrIIZ1NAgK7/EKBV387Cw3MjylF+OtEWj2gCIBq2LxCZbF14DeWW8= 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=czYgHKte; arc=none smtp.client-ip=209.85.160.49 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-oa1-f49.google.com with SMTP id 586e51a60fabf-25545664f37so1400250fac.1 for ; Fri, 14 Jun 2024 22:27:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1718429274; x=1719034074; 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=bZ/AEFSFwdT9H/GgMZYFdqgb9wNJbl1T6o8nBqHR0q8=; b=czYgHKtelKDFTAP7IoHNZOsFUhpr1/BRhrpU6WYyUMJBgdF08+ifWwp4YxeK5PyZ38 ngG8UaYJSRXz4imlAvPEjgHIgljmhv4eEfSTGycofzdPSO0HryVF+r5z6gY0GRzcdqUz B0i4C5FIVSd+QcbxWSlOWNkVRcsXmgMV913alyvIjoSAYqmVIa/Hm9qxbkGVDbMu+Uft mlyocoDmH/CvOOxObTPXEj8Ev7vYZjBM1ZLYmFSbIRPmlcvHWepE3FJKgfRt7UDbIIYA BZm8aPqfqdbumqhxzuAR4EDMMqoJyFhL6Eih9ObNUTg/+7Oj8nuDaBuyhumdv3FHzDud Y/AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718429274; x=1719034074; 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=bZ/AEFSFwdT9H/GgMZYFdqgb9wNJbl1T6o8nBqHR0q8=; b=AhBR23oV2sfxcn1RL+AGzXhTkgaCQfJuw3MreQNaUV+7curIvApqp2LACPA84Sl4O/ 7xM1gPFU5mSM69OYEaKM6lwcZkxA0QfeDtkBZDagma7hNVOekwLn7SIO/C5p0bcY445w rJK/kc4KK+eNs4Eha0Z1ngfcDFhOuXmWOcGvqBn+gluPtMRkQ3MwiPuQlqZ34xQn2sg3 bgNId88faUaJnN09iD7lowrIZBoZc62W2fh5sh6tBdU89+Rhn9u6Dz50Sm5QC5gqln/N 5cctCTilSyQWM88CVmLU8hHnjlKWN2QPumbi4Q5BK4WtbazoTRTD7jLjWALuuJMi/uQg U7hA== X-Forwarded-Encrypted: i=1; AJvYcCV1Whc0LH0YKdRZKwzDKu0y6ffUfzEjscNT8eBbrv5RqXDOiX6aoxHXqF8oDf6WWYKCdyqDUnjZ671PiJLNJ4pwu87191ia9onFxe6V X-Gm-Message-State: AOJu0YzMPBmOU4kuyTO4+B3NSS6elL0L5DKPJYxYho5skmNQkWX7upyE Gqd+NSm0nyJSqS8hXUZvV34dqON/ZkOaTUZJPqNkQrNju1TtH9UwpMgXTdFHTps= X-Received: by 2002:a05:6871:828:b0:254:a753:d1c0 with SMTP id 586e51a60fabf-25842b7b406mr5663474fac.48.1718429273890; Fri, 14 Jun 2024 22:27:53 -0700 (PDT) Received: from tjeznach.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-705ccb6b99bsm4081578b3a.143.2024.06.14.22.27.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 22:27:52 -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 v8 3/7] iommu/riscv: Add RISC-V IOMMU PCIe device driver Date: Fri, 14 Jun 2024 22:27:33 -0700 Message-Id: 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..e675acceb290 --- /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_CAPABILITIES); + 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_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 = 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