Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp770507lqb; Fri, 24 May 2024 12:35:50 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUBM47LB0eaQfv1LnmOMZn5e4u7wwnSgxTj1RR4yoI7+1+V6GJ1CDulYV94ulrFH4wjbfdcupMWJ1B4TzpfjUKewPQuw/31Zi6uaPf3DQ== X-Google-Smtp-Source: AGHT+IH2WHh94LP43aeqvjC8y3G9I2pbv+MHyijE5ns7KrjzayvOHLzJLB/Vs8GfSiVnt0cN6neJ X-Received: by 2002:a17:902:cecd:b0:1f3:4225:bb79 with SMTP id d9443c01a7336-1f4486c5624mr49356095ad.2.1716579350523; Fri, 24 May 2024 12:35:50 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716579350; cv=pass; d=google.com; s=arc-20160816; b=J/YInVx8IfVLNVdrrQ7djp4c3CnRfIyrUyh0yeMSEUN3L5dYoZ7zQmpLPoRiAUjZtI 0wZLxHxDBDIhIRdmP24ievoZ587BSW/nLdyPPJWEOHQ8mr/l5VaVISQIhWHjSbZpjDzx GBHTvm1YEsnoHkGtd0rH2F+4EiwoPqumX/ghIWBaWM6VJIV+8YG1xB6Oc1VkiZBlh3FL VN9cUyxSILlPgbJZMeou0RG0rnCvDB3hZjwMWr/hCX+oxJbW+R+TuRxDdaAvTpEWKdLy QihlI0l1v0J+dxAuHlQPjTVOb+0gu3hCiQASQx6yvhkks6wURxHjYDZANdH8NlGIyEI0 UIgQ== 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=Gxi9dSsEt2UtDDs3loOHfKu3ofWZQsX1GUiGzyjfiYM=; fh=HGVkfh9iRWsbUAu4OH50ZenUWMh7sYr8pf5A32+xKjQ=; b=grSXo+4qb0u8TGWTqAItoLjc0xdVQ6eckHqvtvQO5RHs4jZAiDahBu6hYau9nk59eZ k/ZJwym5YkOYYA7QmGFqoMRjUxocglu2FR+df4hSW7CM0YzJ6bwFXZdKDVTM157xqEgo wLPniw+XIbBNJ6SWOhRQ5uCG2eOJy5LcbJ/nU0Mjmey3uE+NTr/vON1EaQfXwVvieBd0 aa9+AngdxqHParXqnNgYWvQLq5EgtkXLiRqF8eqV+ihfHluz5RuOnPm+mvPL7P8huwBb rrmktVdWFbNGCFZbrZV3+tamVmYIoZ2FT0DjxUWgcbuB3O9bb3a3BgiZDvOl64kfOpwi lwpw==; 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=DQHXzLMJ; 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-189052-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189052-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 d9443c01a7336-1f45dc0628bsi7429705ad.552.2024.05.24.12.35.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 12:35:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-189052-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=DQHXzLMJ; 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-189052-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189052-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 29E43281046 for ; Fri, 24 May 2024 19:35:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A34C78593E; Fri, 24 May 2024 19:35:10 +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="DQHXzLMJ" Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 46FB97D417 for ; Fri, 24 May 2024 19:35:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716579309; cv=none; b=nXaDd+nqMeNIdVlac5VldAEjysYW1TT0SoU1jDUsfKmUUs4CxfxUXBKw9k01SODS6sikEnfCX0j2UuwcLpccgCWZeGANYPOxFiDVsmQWEHYY/dT+GKwnwXHxdK0GsgaZrsxkQlrgKQpO4WEHFs787mHqAxdLuucO+ILPFLl7vrg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716579309; c=relaxed/simple; bh=oKJItKnsBP2MoFoXF4GEbrlNHgVit/cPojwa15Gmbkk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BZ6NtislaAfDb+2rzqFgh+S/aoAkS76/qcZ0IfWN4NF/7kOHzUd9B2Rt5C1ZcrT+7+P/koa0IDnEb3QgKZHniJ38AjaaqBegLM+/Aqn6IIMUETlVKyHLIpCYOa0atRASu2KNHPfLXt2ysAd5x+zqicuCJiNT97wn5dx8eND9h18= 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=DQHXzLMJ; arc=none smtp.client-ip=209.85.210.171 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-f171.google.com with SMTP id d2e1a72fcca58-6f8edddf756so1071327b3a.3 for ; Fri, 24 May 2024 12:35:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716579308; x=1717184108; 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=Gxi9dSsEt2UtDDs3loOHfKu3ofWZQsX1GUiGzyjfiYM=; b=DQHXzLMJYdmoS88LYBvmcb3/liFNJBZ3RrUeu1vQNsaXolAgN4/EbM/AwEiWtUlxZV dJkobvBLnX3ogDJDY1YnrvT6MYbdxxYhnbPZE+CStbjG8461sM7JrRy8K+p0owOYeMip YB44FY8gsMV75op1YmV0DTs5RBt7CrfbESKY64ZJfQbRVjxmxQcDjQAZYrcF7HbR6QaJ R5Ms0qf0uNwGqJIK+KFGjDYrC50jlHmClRJFwIATo4ZYEV+21nl3Ty85nxJXe+q7AdL6 7zzIkGy/JNqE4KHjdvUPXlHMgiHiA34k4J7g8grgHizGtvfd7MMHBP1cJd8LTrPuP2PY 0wGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716579308; x=1717184108; 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=Gxi9dSsEt2UtDDs3loOHfKu3ofWZQsX1GUiGzyjfiYM=; b=A0qJ/kaLz14dBsA/u2E9Wk+vP1lvMBOPxR8QrNP3fq5wdY27kP6i+o8b/Jbt9/WfzO mkvHkZZD/q+Qq1dgD2bDWoJ6Oj5oTKg6rXRioP5x8Oa7AGi0kF83HrfDn9Z56pRuv9RU w8MeY0mmjZ5/CmpTzSSNiRzrWhpNm8hvzZvJcXEsBfQVHuKFrMb6WjjZywm8Mbc1xriz 0BlYp0MxpWzRWVlc/bba3w3m3Ky2BYUaXdG03UqsNl9UdJ2zaWx97qsHWUhbBGn7Q7jZ oodzPimKusm3uhNAESRRHT6S89hh2hTQ91cFpZUY/oYoYYlwfL1WY8bEPXUWj9+WWAp/ o6QA== X-Forwarded-Encrypted: i=1; AJvYcCUf1VqEb6L3PsBjbuI6qatyR8OKiB0Qmw0jGtPIfJRcj4knv0rf7wnqEBblO+u0GJqJ1Ggyf4AUqMpJE39rvBTmiPiocLMCdKPjxmdd X-Gm-Message-State: AOJu0YxDmk8UQJz9pQOKUvflqXjkTjhMOLVIelYrs+Za2NxXFw06N8aC ja2a5pZxA/LFG8lR6gF7ZHEY/11HELpTzoQ235H175zwGiovzPI8a3SeVrdkI4o= X-Received: by 2002:a05:6a00:2a0f:b0:6f8:b1dc:d980 with SMTP id d2e1a72fcca58-6f8f3f9074fmr3718630b3a.31.1716579307705; Fri, 24 May 2024 12:35:07 -0700 (PDT) Received: from tjeznach.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f8fd4d878esm1441684b3a.190.2024.05.24.12.35.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 12:35:07 -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 , Zong Li Subject: [PATCH v6 4/7] iommu/riscv: Enable IOMMU registration and device probe. Date: Fri, 24 May 2024 12:34:44 -0700 Message-Id: <0a2b572a22247cf3af3a790dc9277cb48ed37430.1716578450.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-Transfer-Encoding: 8bit Advertise IOMMU device and its core API. Only minimal implementation for single identity domain type, without per-group domain protection. Reviewed-by: Lu Baolu Reviewed-by: Zong Li Signed-off-by: Tomasz Jeznach --- drivers/iommu/riscv/iommu.c | 66 +++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c index 3c5a6b49669d..b8e0e4b62585 100644 --- a/drivers/iommu/riscv/iommu.c +++ b/drivers/iommu/riscv/iommu.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "iommu-bits.h" #include "iommu.h" @@ -36,6 +37,60 @@ static void riscv_iommu_disable(struct riscv_iommu_device *iommu) riscv_iommu_writel(iommu, RISCV_IOMMU_REG_PQCSR, 0); } +static int riscv_iommu_attach_identity_domain(struct iommu_domain *iommu_domain, + struct device *dev) +{ + /* Global pass-through already enabled, do nothing for now. */ + return 0; +} + +static struct iommu_domain riscv_iommu_identity_domain = { + .type = IOMMU_DOMAIN_IDENTITY, + .ops = &(const struct iommu_domain_ops) { + .attach_dev = riscv_iommu_attach_identity_domain, + } +}; + +static int riscv_iommu_device_domain_type(struct device *dev) +{ + return IOMMU_DOMAIN_IDENTITY; +} + +static struct iommu_group *riscv_iommu_device_group(struct device *dev) +{ + if (dev_is_pci(dev)) + return pci_device_group(dev); + return generic_device_group(dev); +} + +static int riscv_iommu_of_xlate(struct device *dev, const struct of_phandle_args *args) +{ + return iommu_fwspec_add_ids(dev, args->args, 1); +} + +static struct iommu_device *riscv_iommu_probe_device(struct device *dev) +{ + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); + struct riscv_iommu_device *iommu; + + if (!fwspec || !fwspec->iommu_fwnode->dev || !fwspec->num_ids) + return ERR_PTR(-ENODEV); + + iommu = dev_get_drvdata(fwspec->iommu_fwnode->dev); + if (!iommu) + return ERR_PTR(-ENODEV); + + return &iommu->iommu; +} + +static const struct iommu_ops riscv_iommu_ops = { + .of_xlate = riscv_iommu_of_xlate, + .identity_domain = &riscv_iommu_identity_domain, + .def_domain_type = riscv_iommu_device_domain_type, + .device_group = riscv_iommu_device_group, + .probe_device = riscv_iommu_probe_device, +}; + static int riscv_iommu_init_check(struct riscv_iommu_device *iommu) { u64 ddtp; @@ -71,6 +126,7 @@ static int riscv_iommu_init_check(struct riscv_iommu_device *iommu) void riscv_iommu_remove(struct riscv_iommu_device *iommu) { + iommu_device_unregister(&iommu->iommu); iommu_device_sysfs_remove(&iommu->iommu); } @@ -95,5 +151,15 @@ int riscv_iommu_init(struct riscv_iommu_device *iommu) return dev_err_probe(iommu->dev, rc, "cannot register sysfs interface\n"); + rc = iommu_device_register(&iommu->iommu, &riscv_iommu_ops, iommu->dev); + if (rc) { + dev_err_probe(iommu->dev, rc, "cannot register iommu interface\n"); + goto err_remove_sysfs; + } + return 0; + +err_remove_sysfs: + iommu_device_sysfs_remove(&iommu->iommu); + return rc; } -- 2.34.1