Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp533603lqr; Wed, 5 Jun 2024 13:07:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW+8fOYsTqXfMXOfRZVE6/fXdRNN8BN3pLf4v1o1TpfBQEgnd/vzzVi6GoSSLoDYWCjWVhDFaZd3VF4QuF41gfTaFjVjiY50ku+A3CMDw== X-Google-Smtp-Source: AGHT+IGTGyFt0Dwz6PAjWJmvd4fcAjX3VLDdeTHf2qDRwxFF0b3jQT4FqSeB88fevOq1jDPeWZCw X-Received: by 2002:a17:902:ea0b:b0:1f6:73cb:4877 with SMTP id d9443c01a7336-1f6a5a0243cmr43229995ad.6.1717618023940; Wed, 05 Jun 2024 13:07:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717618023; cv=pass; d=google.com; s=arc-20160816; b=Wlx9z5keKi5jM6Zc4fQbvP0s4OH1kVC8jfZ1BVNrGZky7t6wuyBaprdChb9mw1xtpQ u3FeYZbKH881+S5RxjLrL0Z1QKaxwyhEkPr3zX4w5Gm1xpRH80ypUrh0x64h7ycXokGC TCqpH/WYbWyEh5DMQ34JGLBQh9haFhRUVp9UK1HWgFmGETAJartY2SQPha4BWhRzvLqD LaSLrBC3v9Tp8pLMDoGmqKugH61IY/6/cq06JGkBLvGOeIyUc7mPEDIjd14qGVSIsdFM ze7ABgdLGY96qpIv0rXDkdkc/FealvbVQQcWO4mrecECKrYArLmEXAoVyyMgzyU/QsJf fyGQ== 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=dnZlgtmu9J6ju7nZJlkNKtcMm/kqnOJ/uOUWWWiR/Dk=; b=SO0OzSfcH3BlBLyQ4PF3HFAVGCJn9rFOYF3XOTEG6kQ0ji7ZH6y1F9c+gZIpTrYuXf wfcJLTIn97sXtWh4gqyuVQ2CA8xMKPjmlhkIgl8Q2w6dYZovUI1QqUys3T9jIVKPbl0B /cVwoKGEC2vcBOvtUO2x9ZPjLmtlGcE2tFKkkt/Vuvt6RMeHedXzhxazF5QiNFWREhBn 0sZv1yXngUr7VpUFs9tVCzE3/hES4wvNsb9FfZR+XNlDd17mGzY2D1POioPWUOqmxZR6 ArqBqjNDriIswRVqcxP8/nDlBk3BcbLdMxf5x3t0UVIwzuPVZwxAxDRKTuMIIdJirDJk dknw==; 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=GZjsVZRI; 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-203133-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-203133-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 d9443c01a7336-1f63240157dsi6812175ad.477.2024.06.05.13.07.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 13:07:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-203133-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=GZjsVZRI; 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-203133-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-203133-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 AF207B241C5 for ; Wed, 5 Jun 2024 19:58:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 18CF4155750; Wed, 5 Jun 2024 19:58:05 +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="GZjsVZRI" Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 605201553B4 for ; Wed, 5 Jun 2024 19:58:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717617483; cv=none; b=DLUwldu4PRqLrOk93y8iKVDFIYa9TzZI2n/V9WSF0V/sWUIFmG+T2rVo3i+vLFLgCooEONFKVpMkriwPJGNqBBC6H9AHkDq8+N8xk3Li6+9/akiE/1Dox58aKexsC7frbXbOC/NaqppcsK2pK2cnik2pV2lnHoo4jJK8xyMVr9A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717617483; c=relaxed/simple; bh=DcOSw2ew0c3URNrUO3l1Ipvi4bfVqwYuFrqb7c+A5RM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=QXqC7KlgOJZpuD/Ov1vnfNsp4HpfEUbvjGuQPllCt8BIi4yNh54vNryx/khmWMaD+JvRW2i8o1LpNIpYPSQIDp6Cn69mIYhdMCuHffAV8QlXIslr4RHOIfbpn/+afMB4KD0CPDobbkdsvjNUq8f17zj21ZvqfWBiiHGM/YrPXYA= 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=GZjsVZRI; arc=none smtp.client-ip=209.85.210.182 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-f182.google.com with SMTP id d2e1a72fcca58-701b0b0be38so202545b3a.0 for ; Wed, 05 Jun 2024 12:58:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1717617482; x=1718222282; 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=GZjsVZRIkEzoEZ+WbqAV+YBpXDWj4yc9KRamQm5B3wVET4/rE05U05+wULrFN8u2fx IIycWD3uqS2vSvUbnM0O2bxRTKgMtQGtqCWPp4AVFHIU9U5OAeKbQy3J8pb7brhuNOLK FCsdKz5Na2sZ/cARrXiUsOwwCg6MHlOGY3dRH06CQIrv29C8fEfTQMHvVC1MH9/dIuOR Mwkoh7Ck+Olwqjyubxn9ZNd8IYo1NKpMHgI50O7v+lMYdHaOVdv/jHNicJXhHuwvwye5 sI6PilolxgJZbAjU7hhUhV5O9eSexCAVMI1zWXNfb79HGCRJ7EhLUPp2uZ6gs7YR6RnT VeAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717617482; x=1718222282; 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=W9orN3h8ZJ/LFHmqFp3ustzTQR2HvNOXMl8Ru+fr29DoydG0Q/HPbyWkgNacRH0ait we+nZd9FggAIyESwwdJM/OW7E7HK+LF2jJSwEb95AWgfVW3o3T3MSz+XHDkiuybqnGpV nPB0hp/feUeZ4ggr106ouiFcj/mZjzCdgLnGj+T73QkWPUk39cCY73csoD6J8BB/Id7D JF5l8c0LI3HaRXGtvKo2ctPj4bVs6zhMEm8OLrApEyf14yVtEl4wKuWP+4RvWIByzY7e a6g8Ku/eCft11KvzhmmCUeCTJF9NrZI4uaIVMf3oN4xJ093lmIHruz5vpDnft0YJViRX /SeA== X-Forwarded-Encrypted: i=1; AJvYcCVZyrnUztlW9cVaEKdKAszPHPoDs8NcGFKJJn4YmNaKCvAMit1KBjKJGBkwmods1t6LMZm8PmpamkK1/if7TbQmm2cISNcVaKbBVcmI X-Gm-Message-State: AOJu0YynhT3MEeRu6fYGzgsOke6zt8RjRdi3lAz1fHcDjvzrRCqTCCSz AW6OwAbO1vaZfGhyyUudmYTxRIvJNd80pDPTVraQpGPeN0bCc4Ar6cOW6lPoKjo= X-Received: by 2002:a05:6a00:3d01:b0:702:5d6c:3c25 with SMTP id d2e1a72fcca58-703e59b05f2mr3831135b3a.18.1717617481647; Wed, 05 Jun 2024 12:58:01 -0700 (PDT) Received: from tjeznach.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70244900ee9sm8664209b3a.124.2024.06.05.12.58.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 12:58:01 -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 v7 3/7] iommu/riscv: Add RISC-V IOMMU PCIe device driver Date: Wed, 5 Jun 2024 12:57:45 -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