From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) (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 D59803D47A3 for ; Fri, 14 Aug 2026 01:56:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786672616; cv=none; b=q5rOUT87VEdn2gNo3nhVGGiEA5iQvLOPT1Hx5LBYfgPoDhktYx6eJAkj53R7Vuf/Zy3VSP7cr5mmo2mog0g0RjA3+T7W54OtJo0mtTWXxRLeu+emTZ5vwSMs+CYRMmKJ+XQ5WUKf+NFQPfVkNd99nt5QNjHiNdEoIB1rsmkHO2M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786672616; c=relaxed/simple; bh=1redyhJC+IGh+e+S6ytViOC1oWenhApj7OjFxxKaJwM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IqVYWp4q0xfKlK3f6TB1AFQnYio3RV0bYVioEk4uJFGCSWr7M7tH6I3uDSvezlxu7gkz9W0iKXfrku2wmFE4Gl90DfyDeBjvqe4ipuCR/oDN9fDW/y4e/3OzLdarPKHMlkGKai3qDFT2z5mgpeco+O5u6NHH46WogBzJ33JvqHs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--praan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HB/0SxTo; arc=none smtp.client-ip=209.85.210.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--praan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HB/0SxTo" Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-84859a64079so622496b3a.3 for ; Thu, 13 Aug 2026 18:56:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786672613; x=1787277413; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=S7AGEyzljGhXcSI0kwm3voWfb0/wbSArpC0SZwch5AQ=; b=HB/0SxToSqMLfqIZ2lsn6agNYAqivAQqsB8O8TPn7AxnkQnE2DWgiiieHoYMdhYfhY eAYd0wN3nXCfWVzFr4/PFahFMLQTILohfo54NQXh8e0zz/i3EKyBpPPWqWBrbtIRq3Nn SnrCP5VTS8L5q8jbKCGibUgMespDfVaRB4+rRf9TUZNNPEMozhf124f+Xc2ou1sjARsH OAPTcKrug/gJe9hZr3NAjSqSVZKRDS7Ap1QwHmQPGasPRjqxmeWkEl4sGxSb2Dwu3uEu z7fYHt+yG6EmTaVAbUtTsny6qJARvokdFcA+DSm96G/5fhc0UZcOZJ/S0310z31QxXxK W75A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786672613; x=1787277413; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=S7AGEyzljGhXcSI0kwm3voWfb0/wbSArpC0SZwch5AQ=; b=ZVjUN6ozQqkONZBoT+zMB/wizTwnhj9dFxweVyI6gRsg7PV+fa3sTkn+9mqb5eG+qA e8EonvI5vQ3/ImMjOfbjaySAAn4mwctpFcm9UZSBR2Jqfc5/uVPRSkRtLDlD5FIdVeei hB84Dkp1TJECZvtySacUBOm9lhHiqqHPZ671vqMOkkYk9dTwLVnJvtFowkoLMHQVJyhy jVYp27WX9Bz87/D7REq2xYpbqbF1Ovwk1Ten0/fnod93d9MgJM0ETGZgW5I1SdKcurE4 3zRBAB03JpLvMwrSAx6Fy+rGXzbcilH6pqZgvciARELBDlHZ8NnYhOHM4Ni3YgvQOMoX eWhQ== X-Forwarded-Encrypted: i=1; AHgh+RoG/0UPAsaZ2qeNtLJLmzAaKjPHXWyn+mOg0J4gTukQl0KF4TbQJHwzy8OF8h7F50pETDDPlZd0Ukc=@vger.kernel.org X-Gm-Message-State: AOJu0YxmZFYXEeipwR5HBZp5kcuCa2ShLg8tJ7WbRTkEVqG5UHAR47ix 166iPpW+AddZlBGein7QykevqiaR5V5qUjGGdqmaXFo2vd3dQv4NbxN6R9ZKIXTf8reYX26xEMD 7ZQ== X-Received: from pfbmy11-n1.prod.google.com ([2002:a05:6a00:6d4b:10b0:84f:d0db:d487]) (user=praan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:240d:b0:848:411f:3969 with SMTP id d2e1a72fcca58-84fde20e899mr2349449b3a.20.1786672612845; Thu, 13 Aug 2026 18:56:52 -0700 (PDT) Date: Fri, 14 Aug 2026 01:56:43 +0000 In-Reply-To: <20260814015647.3370124-1-praan@google.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260814015647.3370124-1-praan@google.com> X-Mailer: git-send-email 2.55.0.691.gc56d675ccc-goog Message-ID: <20260814015647.3370124-2-praan@google.com> Subject: [PATCH v2 1/5] iommu/amd: Refactor device probe and capability initialization From: Pranjal Shrivastava To: iommu@lists.linux.dev, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Joerg Roedel , Suravee Suthikulpanit , Vasant Hegde , Ankit Soni , Jason Gunthorpe , Bjorn Helgaas , Samiullah Khawaja , Pranjal Shrivastava Content-Type: text/plain; charset="UTF-8" Restructure the device probe path to improve readability and prepare for cleaner error handling. Refactor check_device() into iommu_lookup_device to explicitly validate and return the amd_iommu ptr & devid. Refactor iommu_init_device() to return the allocated dev_data. Consolidate all PCI cap inits (MSI domains, PASID, ATS) into a new helper: iommu_init_device_caps(). Suggested-by: Vasant Hegde Signed-off-by: Pranjal Shrivastava --- drivers/iommu/amd/iommu.c | 114 +++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 58 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 4c31294fabc5..9b8ad131ba79 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -675,7 +675,8 @@ static void pdev_disable_caps(struct pci_dev *pdev) * This function checks if the driver got a valid device from the caller to * avoid dereferencing invalid pointers. */ -static bool check_device(struct device *dev) +static bool iommu_lookup_device(struct device *dev, + struct amd_iommu **iommu_out, u16 *devid_out) { struct amd_iommu_pci_seg *pci_seg; struct amd_iommu *iommu; @@ -690,7 +691,7 @@ static bool check_device(struct device *dev) devid = PCI_SBDF_TO_DEVID(sbdf); iommu = rlookup_amd_iommu(dev); - if (!iommu) + if (!iommu || !iommu->iommu.ops) return false; /* Out of our scope? */ @@ -698,47 +699,33 @@ static bool check_device(struct device *dev) if (devid > pci_seg->last_bdf) return false; + *iommu_out = iommu; + *devid_out = devid; return true; } -static int iommu_init_device(struct amd_iommu *iommu, struct device *dev) +static struct iommu_dev_data *iommu_init_device(struct amd_iommu *iommu, + struct device *dev, u16 devid) { struct iommu_dev_data *dev_data; - int devid, sbdf; - - if (dev_iommu_priv_get(dev)) - return 0; - sbdf = get_device_sbdf_id(dev); - if (sbdf < 0) - return sbdf; - - devid = PCI_SBDF_TO_DEVID(sbdf); dev_data = find_dev_data(iommu, devid); if (!dev_data) - return -ENOMEM; + return ERR_PTR(-ENOMEM); dev_data->dev = dev; /* - * The dev_iommu_priv_set() needes to be called before setup_aliases. + * The dev_iommu_priv_set() needs to be called before setup_aliases. * Otherwise, subsequent call to dev_iommu_priv_get() will fail. */ dev_iommu_priv_set(dev, dev_data); setup_aliases(iommu, dev); - /* - * By default we use passthrough mode for IOMMUv2 capable device. - * But if amd_iommu=force_isolation is set (e.g. to debug DMA to - * invalid address), we ignore the capability for the device so - * it'll be forced to go into translation mode. - */ - if ((iommu_default_passthrough() || !amd_iommu_force_isolation) && - dev_is_pci(dev) && amd_iommu_gt_ppr_supported()) { - dev_data->flags = pdev_get_caps(to_pci_dev(dev)); - } + /* Wait for DTE updates to go through */ + iommu_completion_wait(iommu); - return 0; + return dev_data; } static void iommu_ignore_device(struct amd_iommu *iommu, struct device *dev) @@ -2477,49 +2464,70 @@ static void detach_device(struct device *dev) mutex_unlock(&dev_data->mutex); } +static void iommu_init_device_caps(struct iommu_dev_data *dev_data, + struct device *dev, + struct amd_iommu *iommu) +{ + if (FEATURE_NUM_INT_REMAP_SUP_2K(amd_iommu_efr2)) + dev_data->max_irqs = MAX_IRQS_PER_TABLE_2K; + else + dev_data->max_irqs = MAX_IRQS_PER_TABLE_512; + + amd_iommu_set_pci_msi_domain(dev, iommu); + + if (!dev_is_pci(dev)) + return; + + /* + * By default we use passthrough mode for IOMMUv2 capable device. + * But if amd_iommu=force_isolation is set (e.g. to debug DMA to + * invalid address), we ignore the capability for the device so + * it'll be forced to go into translation mode. + */ + if ((iommu_default_passthrough() || !amd_iommu_force_isolation) && + amd_iommu_gt_ppr_supported()) { + dev_data->flags = pdev_get_caps(to_pci_dev(dev)); + } + + /* + * If IOMMU and device supports PASID then it will contain max + * supported PASIDs, else it will be zero. + */ + if (amd_iommu_pasid_supported() && + pdev_pasid_supported(dev_data)) { + dev_data->max_pasids = min_t(u32, iommu->iommu.max_pasids, + pci_max_pasids(to_pci_dev(dev))); + } + + pci_prepare_ats(to_pci_dev(dev), PAGE_SHIFT); +} + static struct iommu_device *amd_iommu_probe_device(struct device *dev) { struct iommu_device *iommu_dev; struct amd_iommu *iommu; struct iommu_dev_data *dev_data; int ret; + u16 devid; - if (!check_device(dev)) - return ERR_PTR(-ENODEV); - - iommu = rlookup_amd_iommu(dev); - if (!iommu) - return ERR_PTR(-ENODEV); - - /* Not registered yet? */ - if (!iommu->iommu.ops) + if (!iommu_lookup_device(dev, &iommu, &devid)) return ERR_PTR(-ENODEV); if (dev_iommu_priv_get(dev)) return &iommu->iommu; - ret = iommu_init_device(iommu, dev); - if (ret) { + dev_data = iommu_init_device(iommu, dev, devid); + if (IS_ERR(dev_data)) { + ret = PTR_ERR(dev_data); dev_err(dev, "Failed to initialize - trying to proceed anyway\n"); iommu_dev = ERR_PTR(ret); iommu_ignore_device(iommu, dev); goto out_err; } - amd_iommu_set_pci_msi_domain(dev, iommu); + iommu_init_device_caps(dev_data, dev, iommu); iommu_dev = &iommu->iommu; - /* - * If IOMMU and device supports PASID then it will contain max - * supported PASIDs, else it will be zero. - */ - dev_data = dev_iommu_priv_get(dev); - if (amd_iommu_pasid_supported() && dev_is_pci(dev) && - pdev_pasid_supported(dev_data)) { - dev_data->max_pasids = min_t(u32, iommu->iommu.max_pasids, - pci_max_pasids(to_pci_dev(dev))); - } - if (amd_iommu_pgtable == PD_MODE_NONE) { pr_warn_once("%s: DMA translation not supported by iommu.\n", __func__); @@ -2527,16 +2535,6 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev) goto out_err; } - iommu_completion_wait(iommu); - - if (FEATURE_NUM_INT_REMAP_SUP_2K(amd_iommu_efr2)) - dev_data->max_irqs = MAX_IRQS_PER_TABLE_2K; - else - dev_data->max_irqs = MAX_IRQS_PER_TABLE_512; - - if (dev_is_pci(dev)) - pci_prepare_ats(to_pci_dev(dev), PAGE_SHIFT); - out_err: return iommu_dev; } -- 2.55.0.691.gc56d675ccc-goog