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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 AEDAACD3442 for ; Thu, 7 May 2026 07:57:30 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.1302237.1576214 (Exim 4.92) (envelope-from ) id 1wKtbi-0006JA-NP; Thu, 07 May 2026 07:57:18 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1302237.1576214; Thu, 07 May 2026 07:57:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wKtbi-0006J3-KB; Thu, 07 May 2026 07:57:18 +0000 Received: by outflank-mailman (input) for mailman id 1302237; Thu, 07 May 2026 07:57:17 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wKtbg-0006Ix-Vl for xen-devel@lists.xenproject.org; Thu, 07 May 2026 07:57:17 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wKtbg-005Fn6-8u for xen-devel@lists.xenproject.org; Thu, 07 May 2026 09:57:16 +0200 Received: from [10.42.69.8] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69fc45d7-2eae-0a2a0a5409dd-0a2a45088ab0-20 for ; Thu, 07 May 2026 09:57:16 +0200 Received: from [209.85.221.52] (helo=mail-wr1-f52.google.com) by tlsNG-c1860d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.1) (envelope-from ) id 69fc45dc-63b5-0a2a45080019-d155dd34c424-3 for ; Thu, 07 May 2026 09:57:16 +0200 Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-43fe3e22e33so313332f8f.0 for ; Thu, 07 May 2026 00:57:16 -0700 (PDT) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45052a4878fsm17562972f8f.9.2026.05.07.00.57.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 07 May 2026 00:57:15 -0700 (PDT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:In-Reply-To:Autocrypt:From:Content-Language:References:Cc:To:Subject:User-Agent:MIME-Version:Date:Message-ID" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1778140636; x=1778745436; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=3oNtBhy9bRQ0n40pPopFcdMy2NYLPfrJ48jcUQh2stc=; b=SN3hXnj/cECL4a566qpIbXAdkqNPZ5bs+VFGDDkiR9G9+plKUSXyVyXex0ZUMjn80W Z6E5Zdf3Msz04nv+zS/ThjDIAMPYFCuuXpPAEb2RQsLHGZ/TCFxgLTJolLt1s1HuT6XY WdtqLglBfL8tUT8+YoclM46TlOHjzerf+zn2DFYMrJoDEXyv3dWTkKYi39VlBdiYeMJn M8KmP3q2R2wzIZ08hxNnMqY0DWv2uHKiyXbmvdi3perJAfoGzlIaNIhX1WPbI7dyGU0k BO3thCM9uM8GTgyVlktW71195dXO5gB5CcIrmlgwMzgAubZeyfy1ziv9IfwCzt/9pOHX oVaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778140636; x=1778745436; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=3oNtBhy9bRQ0n40pPopFcdMy2NYLPfrJ48jcUQh2stc=; b=MB48LIs8/MfNX3nvZpSw5Bn7nZRf3i++Fhv5NF25qVq23f+ztTUO3Z7MpHZQyHMn7Y 2R/R5rLIG+Hytxi16nBqLrX08ntAZk64qq49JHK2onzR9rIR2vTziy7gOVLPloO596P/ vRFrbwQ3tv5fR+0QRb032PrP0gUhjLgQmWnHxK5tmdcLztajlfSH3F4Qh9e+6IrJfERp 0m8YvMnr4Nw2Sr7BXxGi7G7ZgchXiCcJIr98AvHzqbXEAcnUbMRwd/5v0t/UyUKi/xYN IuNqC9nzMp4YYiXw4QHWcBjCsYCl7yerKUTotbgK3LnrAZHdQJpcoP90n3u3CbcS738r VwZQ== X-Forwarded-Encrypted: i=1; AFNElJ+nb6I2brBkUk6XFLrlTrUDZrbRsEod4tXAiQi5u5suQ6+hZ19s98i8Y9UwBBoKDMIhMnwjf4Jg5xE=@lists.xenproject.org X-Gm-Message-State: AOJu0Yz9dK3OGfSh4RQRnFoGcFxOVloqcdMVCOynVliau17vRAt/03g3 yXEyTTUcmKipD03OK8Mr6+w9Eq6mZZQlkSI2yG9JFaWFE5M/gqXAT8ZB8sfWRvF+fg== X-Gm-Gg: AeBDietyCpOg5FZ7n1RTh8JesLGbPMd+3RMUAqSm1V6hA6dSz/rIKUyIJQUL8OT6yhC qiO6BXbKwq/c+5WVGl0IBMz4jAkphIdEKevP0cOPnPunfBJscPBO3EOkLd60p2oInZEIGCDF0Nd mUa8hVq/PecQDVlh2g1MdR9jBw/e+mEUkWAeOtWzh28VMBs++otp81GVM6dwug5uiZBjgP4/kGF rouTTmBD9nEkGa8r9+yhgwTq8kmCW17cX0f5DSOTn302kvgmqmHwKKi7aV0BrSoOZtjK/0hMufo KmsAu6kDyCRsdK1dNrRyCxyyOH7k4qup8r1QZxoQE4HI6hOMsXzrN6ayX3HEFcTj4Gwuf7XuRfO EcMWJNL+8Qi4gTpxXmo3lOiGGAvX39HgzDJSvhmVdWvlO97xGECmvMshZBbYas3qHsJH4IGcWEK d4HzJlLRPegJf36R2FooE/ce6c7rVXLpl7LpLC651mj7aUPFZe97YPSTRe+k5K21dru8Tclw+z9 PDoDLKzR/OvkNczQplzOVfXU9LgdCOgxpHC X-Received: by 2002:a05:6000:420c:b0:43d:7a97:78a2 with SMTP id ffacd0b85a97d-4515c5752f7mr11716646f8f.28.1778140635646; Thu, 07 May 2026 00:57:15 -0700 (PDT) Message-ID: Date: Thu, 7 May 2026 09:57:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/2] iommu/amd-vi: do not zero IOMMU MMIO region To: Andrew Cooper Cc: Jason Andryuk , Teddy Astie , Roger Pau Monne , xen-devel@lists.xenproject.org References: <20260506135514.47310-1-roger.pau@citrix.com> <20260506135514.47310-3-roger.pau@citrix.com> <5531fe94-6200-4f7c-9c22-e09189438e9c@citrix.com> Content-Language: en-US From: Jan Beulich Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcn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oDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <5531fe94-6200-4f7c-9c22-e09189438e9c@citrix.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-purgate-ID: tlsNG-c1860d/1778140636-BC766DB1-A4744FC5/0/0 X-purgate-type: clean X-purgate-size: 5053 On 06.05.2026 18:18, Andrew Cooper wrote: > On 06/05/2026 2:55 pm, Roger Pau Monne wrote: >> Attempting to memset the whole IOMMU MMIO region to zero is dangerous to >> say the least. We don't know what registers might be there, nor which > > Sorry, one more.  "We don't know which registers might". > >> values might be safe for those registers. On a forthcoming platform doing >> the zeroing of the MMIO region does put the IOMMU in a broken state, which >> is not recoverable by the IOMMU initialization procedure in Xen. >> >> Instead attempt to forcefully disable the IOMMU ahead of enabling it. Fold >> map_iommu_mmio_region() into it's only caller, as the function body is just >> an ioremap() call after the removal of the memset(). >> >> Fixes: 0700c962ac2d ("Add AMD IOMMU support into hypervisor") >> Signed-off-by: Roger Pau Monné >> --- >> Changes since v1: >> - Zero the control register after calling disable_iommu(). >> - Print a warning message if the IOMMU is handed enabled to Xen from >> firmware. >> - Fix commit log grammar issues. >> --- >> xen/drivers/passthrough/amd/iommu_init.c | 31 +++++++++++++----------- >> 1 file changed, 17 insertions(+), 14 deletions(-) >> >> diff --git a/xen/drivers/passthrough/amd/iommu_init.c b/xen/drivers/passthrough/amd/iommu_init.c >> index 76ae78e5ea53..ffc041211fb5 100644 >> --- a/xen/drivers/passthrough/amd/iommu_init.c >> +++ b/xen/drivers/passthrough/amd/iommu_init.c >> @@ -42,18 +42,6 @@ static bool iommu_has_ht_flag(struct amd_iommu *iommu, u8 mask) >> return iommu->ht_flags & mask; >> } >> >> -static int __init map_iommu_mmio_region(struct amd_iommu *iommu) >> -{ >> - iommu->mmio_base = ioremap(iommu->mmio_base_phys, >> - IOMMU_MMIO_REGION_LENGTH); >> - if ( !iommu->mmio_base ) >> - return -ENOMEM; >> - >> - memset(iommu->mmio_base, 0, IOMMU_MMIO_REGION_LENGTH); >> - >> - return 0; >> -} >> - >> static void __init unmap_iommu_mmio_region(struct amd_iommu *iommu) >> { >> if ( iommu->mmio_base ) >> @@ -1367,11 +1355,14 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) >> { >> int rc = alloc_ivrs_mappings(iommu->sbdf.seg); >> >> - if ( !rc ) >> - rc = map_iommu_mmio_region(iommu); >> if ( rc ) >> return rc; >> >> + iommu->mmio_base = ioremap(iommu->mmio_base_phys, >> + IOMMU_MMIO_REGION_LENGTH); >> + if ( !iommu->mmio_base ) >> + return -ENOMEM; >> + >> get_iommu_features(iommu); >> >> /* >> @@ -1381,6 +1372,18 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) >> if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode ) >> return -ERANGE; >> >> + /* Read current control register and forcefully disable the IOMMU. */ >> + iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET); >> + if ( iommu->ctrl.iommu_en ) >> + printk(XENLOG_WARNING >> + "AMD-Vi: IOMMU %pp enabled by firmware (%016lx)\n", >> + &iommu->sbdf, iommu->ctrl.raw); >> + disable_iommu(iommu, true); >> + >> + /* With the IOMMU disabled zero the control register. */ >> + iommu->ctrl.raw = 0; >> + writeq(0, iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET); >> + >> return 0; >> } >> > > I don't think calling disable_iommu() is a good thing here. > > It's just a cascade of clearing one/few bits in ctrl at a time, but is > is added unconditionally so that's 5 UC stores writing 0's to the same > register in the common case. > > I think this logic wants to be: > > @@ -1381,6 +1372,18 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) >      if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode ) >          return -ERANGE; >   > +    /* Check if the IOMMU is active, and disable. */ > +    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET); > +    if ( iommu->ctrl.iommu_en ) > +    { > +        printk(XENLOG_WARNING > +               "AMD-Vi: IOMMU %pp enabled by firmware (ctrl %016lx)\n", > +               &iommu->sbdf, iommu->ctrl.raw); > + > +        iommu->ctrl.raw = 0; > +        writeq(0, iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET); > +    } > + >      return 0; >  } > >   > > which has the advantage that it's closer to the current behaviour, and > therefore arguably a safer backport. > > The only thing that disable_iommu() does which isn't editing the control > register is playing with the PCI MSI enable bit, but that really doesn't > matter when we clear ctrl.int_cap_xt_en.  (And in fact, that path is > buggy because it clears MSI enable without inhibiting interrupt > generation, which architecturally will turn into legacy line interrupt > to deal with.) Except there's no line interrupt associated with this? Jan