From patchwork Wed Feb 1 12:53:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13124422 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 A74B8C636CD for ; Wed, 1 Feb 2023 14:32:54 +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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=m3UGftK1+AaMniz4APewaUuyUvlJmXETBND/POelzgE=; b=IXSUKYTUY2km55 DP1GptEEL9E8pL9UojpvTtpLlivw78RKdclkiDJs4m85RRC3lvy1FmIe7PPlGSwz7Mms5bzkXbbqS ckfn8dQ/sAsWCVOw/RnJpdOUfuRpo85voaSKFQfuLEP4w5ANusNE/Ti/aeN5AeXRCVFlawPLMWMH7 LZTKcALCWyeVelJJxBVFD3/pDD/v/ca+U7IC4SjSzKqvI4B7JZ8DjWK7z2FMOB27n/ZV9zwRdHljF F+oEQfTpiGeFywh4tjWmZWGpkvthGYxn/NyG3q8+EBL1WqJ1qRLxB6k6h+RTa7by3lTKcbFzCM+6H YKkdDCAN5IiOO+U5KSvg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNE9B-00CNOX-DE; Wed, 01 Feb 2023 14:31:39 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNDhr-00CBrE-Rn for linux-arm-kernel@bombadil.infradead.org; Wed, 01 Feb 2023 14:03:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=DAR858KRQvKPg9d8btbK56RFwJ6U3P1FOrADuTdQSiU=; b=C/9plR/am4yMi7SpfiLylPyjPq y0yUB2gCWu3W0w5O1X3xNYaO4I/QBfK6c8prb3q9K3xKl872/bFPaP06x4ML5p6c3UcX77zZc4lHM YSNFE5LIbeGwMTQgENUL8s3yqxsis3te+pfC84pNyWdNmCGH9CTsTpslO9E1iurPiFX4Iq7vMRtuE k5j7F/iq5kt0VszXxmHxeR4qC8KPzj7vd1i6h5OHr8ZGC723h59BL+OdoO068TPwrvPX3NFilusxR oASzCfzwoSQEM/BWFRbxXe1Vk0ETIO2OAXjdTlpuQIxtnhlMxM6Mkyh3i/kGsPOsRoNQzHxY8z0nW +vtyQGRw==; Received: from mail-wm1-f49.google.com ([209.85.128.49]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pNChn-004m5C-2r for linux-arm-kernel@lists.infradead.org; Wed, 01 Feb 2023 12:59:23 +0000 Received: by mail-wm1-f49.google.com with SMTP id q10-20020a1cf30a000000b003db0edfdb74so2388196wmq.1 for ; Wed, 01 Feb 2023 04:59:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=DAR858KRQvKPg9d8btbK56RFwJ6U3P1FOrADuTdQSiU=; b=Ggyb3+Q97jwaq1nLUdEGCN/DmIa9T+TVIlNdQsa9leOlAfSvRNt1uXcyTN1GD+135G R77p3qJrpFcF9f5i8a0xPRilpSW6r6jSZeWNj5JfGc7ve7E9J0dCL9QoWw6rHLjBJhMA l+yjZVQR/gIfvs29AwC7zMLP2kmS34HW6UiaOlED2PTNOSPOcLJa65+ozh4XI3tOYbPh ybxXFnOfIvQ2gh3mm+WWi1Y5RYjUZY9QPb3v90eB/lCy2CilYg/1B9N+AZKXfR+eNB76 cmC8MG7IxBkKv/RY7OILPUPSV/AOHj4cLG9vAlzQqkHcuLxlwSO5PajbdTbxsob4IR/y QfLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=DAR858KRQvKPg9d8btbK56RFwJ6U3P1FOrADuTdQSiU=; b=qgZNgUF2scGFho09SNVLY47Vg6rmmJqb5sjo7KgeK1eEw1FpE+CgLsuT6/g7qViP9S ZsSsQ0vxzi6y/xdTm7NkfiEEMg3+gS2VIo9Iz0pYdzQT3kuW5I/wuEb/CK64TZ+EoadT kcji+o8uLm1sd+ogjb09+G07TBreGYMcnEuVr1dHkUPhmdo8513qX3NeH5esOBJuao7U AbGL/hHh4r9fKN2cG8iIcp/DQGOJg30huq0nWgdCGaRvbmgKB6QblsO32F6+y/A/buUg 9XLfGEiOLqz+Eo0h3J5gI+8uih5Ny5uatP3o8ZFXgCwTrGZ+9KBLO4xx4Uz8YwP3Z9pG aS2Q== X-Gm-Message-State: AO0yUKUJ8oBA9pCgRgoLZiLNUGCu1LjdrPC3v7jlzMdw3UTmFvhc2GbJ rm9AqjD8T3EtfWBXh7/HoO+TfA== X-Google-Smtp-Source: AK7set+iGCoWOT606yNywkv2PHuH9O8/l5rQaiMO8pv3IURPxlu09QCKUnSrv4mv91hqJwJ7knQZYQ== X-Received: by 2002:a05:600c:4f07:b0:3da:fd06:a6f1 with SMTP id l7-20020a05600c4f0700b003dafd06a6f1mr1937037wmq.31.1675256388617; Wed, 01 Feb 2023 04:59:48 -0800 (PST) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id m15-20020a056000024f00b002bfae16ee2fsm17972811wrz.111.2023.02.01.04.59.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 04:59:48 -0800 (PST) From: Jean-Philippe Brucker To: maz@kernel.org, catalin.marinas@arm.com, will@kernel.org, joro@8bytes.org Cc: robin.murphy@arm.com, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, smostafa@google.com, dbrazdil@google.com, ryan.roberts@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, iommu@lists.linux.dev, Jean-Philippe Brucker Subject: [RFC PATCH 37/45] iommu/arm-smmu-v3-kvm: Allocate structures and reset device Date: Wed, 1 Feb 2023 12:53:21 +0000 Message-Id: <20230201125328.2186498-38-jean-philippe@linaro.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230201125328.2186498-1-jean-philippe@linaro.org> References: <20230201125328.2186498-1-jean-philippe@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230201_125916_278883_4BE583F2 X-CRM114-Status: GOOD ( 14.94 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Allocate the structures that will be shared between hypervisor and SMMU: command queue and stream table. Install them in the MMIO registers, along with some configuration bits. After hyp initialization, the host won't have access to those pages anymore. Signed-off-by: Jean-Philippe Brucker --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c index 2cc632f6b256..8808890f4dc0 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c @@ -14,6 +14,7 @@ struct host_arm_smmu_device { struct arm_smmu_device smmu; pkvm_handle_t id; + u32 boot_gbpa; }; #define smmu_to_host(_smmu) \ @@ -76,6 +77,38 @@ static bool kvm_arm_smmu_validate_features(struct arm_smmu_device *smmu) return true; } +static int kvm_arm_smmu_device_reset(struct host_arm_smmu_device *host_smmu) +{ + int ret; + u32 reg; + struct arm_smmu_device *smmu = &host_smmu->smmu; + + reg = readl_relaxed(smmu->base + ARM_SMMU_CR0); + if (reg & CR0_SMMUEN) + dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n"); + + /* Disable bypass */ + host_smmu->boot_gbpa = readl_relaxed(smmu->base + ARM_SMMU_GBPA); + ret = arm_smmu_update_gbpa(smmu, GBPA_ABORT, 0); + if (ret) + return ret; + + ret = arm_smmu_device_disable(smmu); + if (ret) + return ret; + + /* Stream table */ + writeq_relaxed(smmu->strtab_cfg.strtab_base, + smmu->base + ARM_SMMU_STRTAB_BASE); + writel_relaxed(smmu->strtab_cfg.strtab_base_cfg, + smmu->base + ARM_SMMU_STRTAB_BASE_CFG); + + /* Command queue */ + writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE); + + return 0; +} + static int kvm_arm_smmu_probe(struct platform_device *pdev) { int ret; @@ -124,6 +157,20 @@ static int kvm_arm_smmu_probe(struct platform_device *pdev) if (!kvm_arm_smmu_validate_features(smmu)) return -ENODEV; + ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, smmu->base, + ARM_SMMU_CMDQ_PROD, ARM_SMMU_CMDQ_CONS, + CMDQ_ENT_DWORDS, "cmdq"); + if (ret) + return ret; + + ret = arm_smmu_init_strtab(smmu); + if (ret) + return ret; + + ret = kvm_arm_smmu_device_reset(host_smmu); + if (ret) + return ret; + platform_set_drvdata(pdev, host_smmu); /* Hypervisor parameters */ @@ -137,6 +184,15 @@ static int kvm_arm_smmu_probe(struct platform_device *pdev) static int kvm_arm_smmu_remove(struct platform_device *pdev) { + struct host_arm_smmu_device *host_smmu = platform_get_drvdata(pdev); + struct arm_smmu_device *smmu = &host_smmu->smmu; + + /* + * There was an error during hypervisor setup. The hyp driver may + * have already enabled the device, so disable it. + */ + arm_smmu_device_disable(smmu); + arm_smmu_update_gbpa(smmu, host_smmu->boot_gbpa, GBPA_ABORT); return 0; }