From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4781B48662F for ; Thu, 20 Aug 2026 21:42:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787262122; cv=none; b=Wpbzf1c5cJNOmy1wwK0OyYG9Fiyo0MFjAd06GTjo3CIfv7+6vetycDOrV+7fc8aKDSdaaopRN+uIxXcUtPF6wxOjIuZaSueWQYZCHHUJ3qk/RECTFUVQkt4L74K/+pza1C3BPKt9OMr+MalmYyugSB4Jz3WG7NTUpgX/TdTNsj4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787262122; c=relaxed/simple; bh=20/oMPU96Nis5oh5Xnu9M/9yRMdjxw1341Lef5oF/DU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Qjny3UE1oZ25Ev4QNxyDL4EXjkc53RMWnYGfSBuoQevv6zkiisQvP9RmqH2UfeNJNYCWtHKGZT+7fJJdR7t+ygeM0QySNaOXw+vYFPIbeSg32P8tGwsCtVdu6ejLWd+bS/Z/bHJNhu9AVFPQ/Xa+zMXnCBYYOG7te2B/iwyVscM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=bndIb7HQ; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=UZOBDvNT; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="bndIb7HQ"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="UZOBDvNT" Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67KK7kZe807455 for ; Thu, 20 Aug 2026 21:42:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=qcppdkim1; bh=+m1BHDhHK9T Y2fxWAkv5d/udjXarPp2DOE+7KtvToDU=; b=bndIb7HQYHDGpUUyyq6mwwz72Iv 6cu2le0Sl7jvB7UpNryAj3+yX5zOjE2C9CdA4tjAcPhFpg+UulXaLI8x9G3ue7Wu lUwf5y8uxtvs6iIquyS9+9ik557SfXj5sX9e8ynTwjxgx00xlQfJil1mDF3hpenr Swnd4zNBAj1gY7jX/T9qV51e6Tet6uH/2zmXQGAmT4uTig3fVdwAFPvh0xByv7+i hA+Ue2kQ3sF0xUVsmsB76GwMZJpuh1A2ib8o2kCSLvlbYljQBr1WBhIWiApiVa8b Qi6CKYogIfsBUnfJoXRGVPr8MVEWbCO/LIPeXzDduzBz7zi0iRiEiuEp5jA== Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4g5xgcbbt9-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Thu, 20 Aug 2026 21:42:00 +0000 (GMT) Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-92e82060977so30194085a.1 for ; Thu, 20 Aug 2026 14:42:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1787262119; x=1787866919; 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:content-type; bh=+m1BHDhHK9TY2fxWAkv5d/udjXarPp2DOE+7KtvToDU=; b=UZOBDvNTDKFJdd9N7K/RX8y9OJGF/DwwiOKa7efvBDuqzXm+zNthv0DqXRv+mNMxt6 fXjV3EyqUAelZvsCvMdrjz7yENH2fytY19jsC2oH8mUngHhSzZls7FC74RPcFbRLoPD9 DNdeU6FKReRjwcT18bfIm8Exf5F5pflSsjdli25MfAsBBFmv3+DG59I1PC0HKK8sdIXx 7LsfdZZqqIi/X7a1IvN7ouUKsFFWVMqgbRDa9RzBXbuObekMDaTr/+w07Oun9q9dsjWx gr9MDHJ24psTnaGNaf+PkMj4iCwiuMlFluMk0gWiIqsEScl05x3LYnfTocLuvKptdRTf ehDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787262119; x=1787866919; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=+m1BHDhHK9TY2fxWAkv5d/udjXarPp2DOE+7KtvToDU=; b=hPkpZQ5lPendRChd8xQnpYteEH/hQqhz1zZ045Z+ir86hIvE2q3P6AhPGKAcfccQSy 8NJOIzTSdlYlKmH/wdyudz3NX2b+kmvIKs69W1vvxOoy3WWtYP6ateP4Ul2tdhdr3w0i QzGdbyTR9xM06x8vKAO7s0vdQ9DOTJ6yesaAkoM36d9wcMEQph5E30GPZo5Jf3VCDwbA lMw8WKDacsHid68ZtWLyzzgekLSHcuB6AFPDIzE9N0fA17ue6XQ5lEVllGmNhNvvkdsz Q+m76h3fCt8Qf7CVM/3rV7nj3JQtEeUv4HREQqp14sur8ewy/pcx0Wd5+v29CKow4m0P KKbg== X-Gm-Message-State: AOJu0YwFH4NjkMgtavGm3zpvF/wk0uRKYicDbSiklcz1FelxnuY0K2dn +3ToE/Y1ewgT8OZ4rkk27MF2NBDtzJvYVqrZyGsECyM89L59/gIdOPOWT8nCpHF3uSlahHKWJ6w tmxFg8LjFjxsoG4GV+0bHpSnKxmiVRic7TDyUptrhEC4w+dbPdAWaBrWqc8RdokOpv+M= X-Gm-Gg: AR+sD13O/tB+7AlMKXVVZe1RiV8GLoqJ1Y9iSi9g/OfUN2x2ne4RfORH5XuBLHg+BoX zOS7cw3zyzPK/+tszr8bQDMBNzG/PD6V0xqHMKRXCL60Pz2x0pfgMoSeeuSJCL3J/H5TqrwbXi9 5y6O7XoJ9LIdjXGSxaG5FlMDSfSjn+8lgeAknQsf5D0BwB8jIpfobhwybos9ZmmWQYIRfALeZ6E zYO7Vf1wj64wS3ATqNlXsyyed06DL3aGgCvr1VyTRXZmUB6TbQo1LIBboz6N4KrN/5XiHtvVODJ KoNdPeqS9aXKMiKY0IsXYhtIh0VUQ5lyKwC3v34aa+++WtI+MA2DFrSqnU6h0UuiLTmvGo/n/N8 bxtUEzkPx X-Received: by 2002:a05:620a:63c8:b0:937:17f9:da2 with SMTP id af79cd13be357-937283b5de9mr979111885a.6.1787262119608; Thu, 20 Aug 2026 14:41:59 -0700 (PDT) X-Received: by 2002:a05:620a:63c8:b0:937:17f9:da2 with SMTP id af79cd13be357-937283b5de9mr979108785a.6.1787262119202; Thu, 20 Aug 2026 14:41:59 -0700 (PDT) Received: from localhost ([193.165.40.195]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-482b14c3775sm13092981f8f.26.2026.08.20.14.41.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Aug 2026 14:41:57 -0700 (PDT) From: Andrew Jones To: linux-riscv@lists.infradead.org, iommu@lists.linux.dev Cc: linux-kernel@vger.kernel.org, tomasz.jeznach@linux.dev, tjeznach@rivosinc.com, jgg@ziepe.ca, jgg@nvidia.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, pjw@kernel.org, palmer@dabbelt.com, anup@brainfault.org, tglx@kernel.org, kevin.tian@intel.com, fangyu.yu@linux.alibaba.com Subject: [PATCH v4 03/21] iommu/dma: Add iommu_dma_sw_map_msi() Date: Thu, 20 Aug 2026 23:41:32 +0200 Message-ID: <20260820214150.545737-4-andrew.jones@oss.qualcomm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260820214150.545737-1-andrew.jones@oss.qualcomm.com> References: <20260820214150.545737-1-andrew.jones@oss.qualcomm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Authority-Analysis: v=2.4 cv=GPM41ONK c=1 sm=1 tr=0 ts=6a8774a8 cx=c_pps a=hnmNkyzTK/kJ09Xio7VxxA==:117 a=CzrpMg+lqiTzknPDBj2tlw==:17 a=Sv0fKeRqtYgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=Um2Pa8k9VHT-vaBCBUpS:22 a=EUspDBNiAAAA:8 a=B9hF7e9JpKM1iJhnVVUA:9 a=PEH46H7Ffwr30OY-TuGO:22 X-Proofpoint-ORIG-GUID: zQX9UcPZXqnBonLR2aZigYhNrZJkdcD3 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODIwMDE2MiBTYWx0ZWRfX/fG6aVMHF28Z 2EF8qs+uZp72/WkyuXCHy1NfA1I4IEVI/0noEHCe8qEKMuqyKvcbYp+yxmbDG8ZJX22wnxLFBM8 ZFKtldNDUe9K/gdR5QKD4y7ys87GkVCZnjTHeD4JGZ1olZC2adC74ei6DGFcfEugM3tnRE2MnZ+ VN18fA97FU841aeOC9TWqG1nff7h5Rqd6oEywMF9+negBEhK3ezrdzEwtJeI+F7S5sYhxhhU2hV 0/TpPToa4N+wz8h/jnJmErQ6Ip0/10DBV7rJgtQl0ZyfBeA6ky9uglhLK/NyNM3Sj9uPOJrpgVr r7isJZgFP9SdlTxaMD9Hg2achRmfcaO/ynbjzHrfxMr22UlZuZvZIQ0XC60D5mf0ypK6mDO+nL2 zm0wveTuU0mpiwBjbvzMkWSQQm1ROU5WMsGTJ/wRzQOP3x7HcUdcuqHu48Q2wcHR3RLSd9DL0B1 GM2wiA/uZozSQAJYqWQ== X-Proofpoint-GUID: zQX9UcPZXqnBonLR2aZigYhNrZJkdcD3 X-Proofpoint-Spam-Info: AW1haW4tMjYwODIwMDE2MiBTYWx0ZWRfX8ChZSvEr8Yfz AY70dmjyY+DInHAF2ecqW7m+EluTYXuxq2MnJgkZEisg+5BVNCtx9wKXcimUy7pVH0nGQUIYuTv bGUJ7moWMvEJmdiCFaxnPTSxgeWCcB4= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-20_02,2026-08-20_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 priorityscore=1501 spamscore=0 clxscore=1015 impostorscore=0 lowpriorityscore=0 suspectscore=0 phishscore=0 malwarescore=0 adultscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608200162 Add a descriptor-free counterpart to iommu_dma_sw_msi(). The existing function is tied to a struct msi_desc and stores the result in the descriptor. This variant returns the IOVA directly so callers can pre-map MSI targets before any descriptor has been allocated. Callers may pass a required mapping size so MSI doorbells that must not share a larger IOMMU leaf, such as RISC-V IMSIC files, can fail before a mapping is installed. Signed-off-by: Andrew Jones --- drivers/iommu/dma-iommu.c | 48 ++++++++++++++++++++++++++++++++------- drivers/iommu/dma-iommu.h | 12 ++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 9a07eb39336e..3ce9f4878d92 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -2247,24 +2247,56 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev, return NULL; } -int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc, - phys_addr_t msi_addr) +/* + * Descriptor-free counterpart to iommu_dma_sw_msi(). Maps an MSI physical + * page into the domain and returns the IOVA and mapping granule. Used for + * pre-mapping MSI targets before any MSI descriptor has been set. + * + * The caller must pass a device attached to @domain and hold @dev's IOMMU + * group mutex. If @required_size is non-zero then it must exactly match the + * domain's MSI mapping granule. @msi_iova and @msi_shift must be non-NULL. + */ +int iommu_dma_sw_map_msi(struct iommu_domain *domain, + struct device *dev, phys_addr_t msi_addr, + size_t required_size, dma_addr_t *msi_iova, + unsigned int *msi_shift) { - struct device *dev = msi_desc_to_dev(desc); const struct iommu_dma_msi_page *msi_page; + size_t size; - if (!has_msi_cookie(domain)) { - msi_desc_set_iommu_msi_iova(desc, 0, 0); + *msi_iova = 0; + *msi_shift = 0; + + if (!has_msi_cookie(domain)) return 0; - } + + size = cookie_msi_granule(domain); + if (required_size && size != required_size) + return -EOPNOTSUPP; iommu_group_mutex_assert(dev); msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain); if (!msi_page) return -ENOMEM; - msi_desc_set_iommu_msi_iova(desc, msi_page->iova, - ilog2(cookie_msi_granule(domain))); + *msi_iova = msi_page->iova; + *msi_shift = ilog2(size); + return 0; +} + +int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc, + phys_addr_t msi_addr) +{ + struct device *dev = msi_desc_to_dev(desc); + dma_addr_t msi_iova; + unsigned int msi_shift; + int ret; + + ret = iommu_dma_sw_map_msi(domain, dev, msi_addr, 0, &msi_iova, &msi_shift); + if (ret) + return ret; + + msi_desc_set_iommu_msi_iova(desc, msi_iova, msi_shift); return 0; } diff --git a/drivers/iommu/dma-iommu.h b/drivers/iommu/dma-iommu.h index 040d00252563..3e1adeafdcac 100644 --- a/drivers/iommu/dma-iommu.h +++ b/drivers/iommu/dma-iommu.h @@ -19,6 +19,10 @@ int iommu_dma_init_fq(struct iommu_domain *domain); void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list); +int iommu_dma_sw_map_msi(struct iommu_domain *domain, + struct device *dev, phys_addr_t msi_addr, + size_t required_size, dma_addr_t *msi_iova, + unsigned int *msi_shift); int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc, phys_addr_t msi_addr); @@ -53,6 +57,14 @@ static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_he { } +static inline int iommu_dma_sw_map_msi(struct iommu_domain *domain, + struct device *dev, phys_addr_t msi_addr, + size_t required_size, dma_addr_t *msi_iova, + unsigned int *msi_shift) +{ + return -ENODEV; +} + static inline int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc, phys_addr_t msi_addr) { -- 2.43.0