Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6929929rwn; Tue, 13 Sep 2022 11:01:43 -0700 (PDT) X-Google-Smtp-Source: AA6agR4bBlIQKXsB27bYwiBHQX6iX0IJTaEPznedibfohCLC0zhQ2cLCa+7722I2RhJa/miIg8sq X-Received: by 2002:a17:906:fe09:b0:77a:52b3:da5d with SMTP id wy9-20020a170906fe0900b0077a52b3da5dmr13825861ejb.57.1663092101575; Tue, 13 Sep 2022 11:01:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663092101; cv=none; d=google.com; s=arc-20160816; b=PjXhHjG3Gx9wZqmUs3rDpuWLvSGQ/2dHRfjQvXWq8IkGPE88odVnWCmSXgx2zWZDVx zZw4BmxaI10NORktWQZcV41k1PAi1V8yIktjd+liqfETbXlYMQa9Ojlr4RAp8nhSYLgs DgfKCY/pNBjZLyKvbxjmohrM2tSewTNGCUHgaYpZevDE7pth1DcsEmGONsqjPwQDItE2 b1nej7pMI0VSzn9JWbO5NAUsfQnHjsGBpv3ahNUx5Q0vFelkraOmAOE4Iezl6ji8JSN4 IJ6+JA2K/ZO1kRvPpHXs4YLDdSvNGgY63/VrJzq/ZVdYByuUiw2Y14JJL72lh1RiGg8q uE1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UElDd+kNW0oQcqksI3bjiyKvkg3mFJ4B64gDiQ1T0fY=; b=iI5hTpl/u9NJ/MY0QxBEEe6kFWYC0zO+HHbBPozt9JGBTN5+zzWH0x/e/Uc51kR5qO HG0RquoFUlS6b1FCqhO6wa4cwms3JzbBhRjaLzA22A38UPJFKJ0CqSrRrQZ1woHJ6GEk dlUMBRSHz3FYGQ/WgSyWl+7iwjzeLnqMzGcv5kp1tJkxzIWNJ+ztc0B0ppVpeB8f8npi b/Qg8wmpuKnuuB0DVCHQmzQZDiVmXqrO/+hB4e2UEmZne0pFrtKQx9QhyuBigyfM0fZ2 sQF4Lhg1SHW+dI80XBeWH/dq3MjlsQ4ISndJ1xv/GMNTdpfYwbjyUOdEbbIe/e5bE7M8 FHQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="bk1deY/Z"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cs12-20020a170906dc8c00b0073da2f559aesi10200913ejc.480.2022.09.13.11.01.10; Tue, 13 Sep 2022 11:01:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="bk1deY/Z"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232381AbiIMRwG (ORCPT + 99 others); Tue, 13 Sep 2022 13:52:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230208AbiIMRvk (ORCPT ); Tue, 13 Sep 2022 13:51:40 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3CA389CC2; Tue, 13 Sep 2022 09:50:20 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id D5DEAB80EFC; Tue, 13 Sep 2022 14:21:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3AC90C433B5; Tue, 13 Sep 2022 14:21:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1663078878; bh=AKyBCjV9hs/yvvLPFK6IzsfqGsSmMNIgLy3DPd3o5ms=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bk1deY/ZepgzvmqwvQciHUBSR2joH0SVU2qm7EiimtK0fLMEDtmBIgsDfHsaK8t8M KWWB6d4Gav38EaqQJ5cHKR3tYO/7v+2UTUO44p6ZEDA3BulESn0slYCi3ivsTXfc+X lYUp3GANSkOjOxsIARJuLonPPiE7NGzDtZB2pK9o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lu Baolu , Joerg Roedel Subject: [PATCH 5.15 117/121] iommu/vt-d: Correctly calculate sagaw value of IOMMU Date: Tue, 13 Sep 2022 16:05:08 +0200 Message-Id: <20220913140402.394224802@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913140357.323297659@linuxfoundation.org> References: <20220913140357.323297659@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lu Baolu commit 53fc7ad6edf210b497230ce74b61b322a202470c upstream. The Intel IOMMU driver possibly selects between the first-level and the second-level translation tables for DMA address translation. However, the levels of page-table walks for the 4KB base page size are calculated from the SAGAW field of the capability register, which is only valid for the second-level page table. This causes the IOMMU driver to stop working if the hardware (or the emulated IOMMU) advertises only first-level translation capability and reports the SAGAW field as 0. This solves the above problem by considering both the first level and the second level when calculating the supported page table levels. Fixes: b802d070a52a1 ("iommu/vt-d: Use iova over first level") Cc: stable@vger.kernel.org Signed-off-by: Lu Baolu Link: https://lore.kernel.org/r/20220817023558.3253263-1-baolu.lu@linux.intel.com Signed-off-by: Joerg Roedel Signed-off-by: Greg Kroah-Hartman --- drivers/iommu/intel/iommu.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -542,14 +542,36 @@ static inline int domain_pfn_supported(s return !(addr_width < BITS_PER_LONG && pfn >> addr_width); } +/* + * Calculate the Supported Adjusted Guest Address Widths of an IOMMU. + * Refer to 11.4.2 of the VT-d spec for the encoding of each bit of + * the returned SAGAW. + */ +static unsigned long __iommu_calculate_sagaw(struct intel_iommu *iommu) +{ + unsigned long fl_sagaw, sl_sagaw; + + fl_sagaw = BIT(2) | (cap_fl1gp_support(iommu->cap) ? BIT(3) : 0); + sl_sagaw = cap_sagaw(iommu->cap); + + /* Second level only. */ + if (!sm_supported(iommu) || !ecap_flts(iommu->ecap)) + return sl_sagaw; + + /* First level only. */ + if (!ecap_slts(iommu->ecap)) + return fl_sagaw; + + return fl_sagaw & sl_sagaw; +} + static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw) { unsigned long sagaw; int agaw; - sagaw = cap_sagaw(iommu->cap); - for (agaw = width_to_agaw(max_gaw); - agaw >= 0; agaw--) { + sagaw = __iommu_calculate_sagaw(iommu); + for (agaw = width_to_agaw(max_gaw); agaw >= 0; agaw--) { if (test_bit(agaw, &sagaw)) break; }