From nobody Fri May  6 16:44:24 2022
X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
	by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4C0451AD5CAE;
	Fri,  6 May 2022 16:44:39 +0000 (UTC)
	(envelope-from br@bsdpad.com)
Received: from mail.bsdpad.com (mail.bsdpad.com [116.202.106.248])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(Client did not present a certificate)
	by mx1.freebsd.org (Postfix) with ESMTPS id 4KvxHV44Xrz4Qsl;
	Fri,  6 May 2022 16:44:38 +0000 (UTC)
	(envelope-from br@bsdpad.com)
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=bsdpad.com;
	s=20201212; h=Subject:To:From;
	bh=YE9E3cKsd+4cbEIEZ+opdg1Lx1nDMzjd3rfq1bKvEvc=; b=ep/EF3S+LFxzlnOK5ugUXRF8FE
	CzpIHNx2auAQkl9kd4BFueEKObFU66lJdVZPWFCn4nvo8DA+m5LXEHqOplaqZniH0aFTZc4maYl9f
	trgyqgwyidRUPd94GrE5pFJ0a5GlCJD17GEzSvFeHUDrDjWv5DxD3gO9lbFUFYq0XkA8dkFS1uc9r
	TmkRunWcctIisgt2CN7z08CfKQ4bab1DHd6Mgw1wfspfEc97zuSnKPtUfUFaMRdVB/LUwSMYhBJhZ
	1hA9RNdmegFP8BRhF/elA2pVLD1J7G+4njE+NNTkGlpeqGJk2GDg9Onl+ISbjUJ8o2sjxhPgLyauG
	6yYGdoVg==;
Received: from localhost ([127.0.0.1] helo=bsdpad.com)
	by mail.bsdpad.com with smtp (Exim 4.94 (FreeBSD))
	(envelope-from 
)
	id 1nn144-000BBy-67; Fri, 06 May 2022 17:44:24 +0100
Received: by bsdpad.com (nbSMTP-1.00) for uid 1001
	br@bsdpad.com; Fri,  6 May 2022 17:44:24 +0100 (BST)
Date: Fri, 6 May 2022 17:44:24 +0100
From: Ruslan Bukin 
To: Ravi Pokala 
Cc: src-committers@freebsd.org, dev-commits-src-all@freebsd.org,
	dev-commits-src-main@freebsd.org
Subject: Re: 0b6bacc7874f - main - Add ofw_bus_iommu_map() that maps PCI
 requester ID to an IOMMU specifier based on "iommu-map" DTS property.
Message-ID: 
References: <202205061551.246FpO6o060060@gitrepo.freebsd.org>
 <450C9313-CB5A-4DCA-97FB-40FC87886F00@panasas.com>
List-Id: Commit messages for all branches of the src repository 
List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all
List-Help: 
List-Post: 
List-Subscribe: 
List-Unsubscribe: 
Sender: owner-dev-commits-src-all@freebsd.org
X-BeenThere: dev-commits-src-all@freebsd.org
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
In-Reply-To: <450C9313-CB5A-4DCA-97FB-40FC87886F00@panasas.com>
X-Rspamd-Queue-Id: 4KvxHV44Xrz4Qsl
X-Spamd-Bar: ---
Authentication-Results: mx1.freebsd.org;
	dkim=pass header.d=bsdpad.com header.s=20201212 header.b="ep/EF3S+";
	dmarc=none;
	spf=pass (mx1.freebsd.org: domain of br@bsdpad.com designates 116.202.106.248 as permitted sender) smtp.mailfrom=br@bsdpad.com
X-Spamd-Result: default: False [-3.20 / 15.00];
	 ARC_NA(0.00)[];
	 NEURAL_HAM_MEDIUM(-1.00)[-1.000];
	 R_DKIM_ALLOW(-0.20)[bsdpad.com:s=20201212];
	 FREEFALL_USER(0.00)[br];
	 FROM_HAS_DN(0.00)[];
	 RCPT_COUNT_THREE(0.00)[4];
	 TO_DN_SOME(0.00)[];
	 TO_MATCH_ENVRCPT_ALL(0.00)[];
	 MIME_GOOD(-0.10)[text/plain];
	 R_SPF_ALLOW(-0.20)[+mx];
	 DMARC_NA(0.00)[freebsd.org];
	 NEURAL_HAM_LONG(-1.00)[-1.000];
	 DKIM_TRACE(0.00)[bsdpad.com:+];
	 NEURAL_HAM_SHORT(-1.00)[-1.000];
	 MLMMJ_DEST(0.00)[dev-commits-src-all,dev-commits-src-main];
	 FORGED_SENDER(0.30)[br@freebsd.org,br@bsdpad.com];
	 MIME_TRACE(0.00)[0:+];
	 RCVD_TLS_LAST(0.00)[];
	 ASN(0.00)[asn:24940, ipnet:116.202.0.0/16, country:DE];
	 FROM_NEQ_ENVFROM(0.00)[br@freebsd.org,br@bsdpad.com];
	 RCVD_COUNT_TWO(0.00)[2]
X-ThisMailContainsUnwantedMimeParts: N
On Fri, May 06, 2022 at 09:25:40AM -0700, Ravi Pokala wrote:
> -----Original Message-----
> From:  on behalf of Ruslan Bukin 
> Date: 2022-05-06, Friday at 08:51
> To: , , 
> Subject: git: 0b6bacc7874f - main - Add ofw_bus_iommu_map() that maps PCI requester ID to an IOMMU specifier based on "iommu-map" DTS property.
> 
>     The branch main has been updated by br:
> 
>     URL: https://cgit.FreeBSD.org/src/commit/?id=0b6bacc7874f9c68ad35b06f1a7614171eb9ab10
> 
>     commit 0b6bacc7874f9c68ad35b06f1a7614171eb9ab10
>     Author:     Ruslan Bukin 
>     AuthorDate: 2022-05-06 15:41:11 +0000
>     Commit:     Ruslan Bukin 
>     CommitDate: 2022-05-06 15:48:04 +0000
> 
>         Add ofw_bus_iommu_map() that maps PCI requester ID to an IOMMU
>         specifier based on "iommu-map" DTS property.
> 
>         Sponsored by: UKRI
>     ---
>      sys/dev/ofw/ofw_bus_subr.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
>      sys/dev/ofw/ofw_bus_subr.h |  3 ++-
>      2 files changed, 46 insertions(+), 1 deletion(-)
> 
>     diff --git a/sys/dev/ofw/ofw_bus_subr.c b/sys/dev/ofw/ofw_bus_subr.c
>     index ea57d1086779..408d554b3c7f 100644
>     --- a/sys/dev/ofw/ofw_bus_subr.c
>     +++ b/sys/dev/ofw/ofw_bus_subr.c
>     @@ -491,6 +491,50 @@ ofw_bus_msimap(phandle_t node, uint16_t pci_rid, phandle_t *msi_parent,
>      	return (err);
>      }
> 
>     +int
>     +ofw_bus_iommu_map(phandle_t node, uint16_t pci_rid, phandle_t *iommu_parent,
>     +    uint32_t *iommu_rid)
>     +{
>     +	pcell_t mask, iommu_base, rid_base, rid_length;
>     +	uint32_t masked_rid;
>     +	pcell_t map[4];
>     +	ssize_t len;
>     +	int err, i;
>     +
>     +	len = OF_getproplen(node, "iommu-map");
>     +	if (len <= 0)
>     +		return (ENOENT);
>     +        if (len > sizeof(map))
>     +                return (ENOMEM);
>     +
>     +	len = OF_getencprop(node, "iommu-map", map, 16);
>     +
>     +	err = ENOENT;
>     +	mask = 0xffffffff;
>     +	OF_getencprop(node, "iommu-map-mask", &mask, sizeof(mask));
>     +
>     +	masked_rid = pci_rid & mask;
>     +	for (i = 0; i < len; i += 4) {
>     +		rid_base = map[i + 0];
>     +		rid_length = map[i + 3];
> 
> Hi Ruslan,
> 
> I'm confused by this. 'map' is declared as a 4-element array, which means 'map[i + 0]' and 'map[i + 3]' are both only valid when 'i' is 0. But 'i' is the loop iterator; doesn't that mean you're accessing outside the array after the 0th time through the loop?
> 
> Thanks,
Thanks, I'll fix this. I did not want to allocate memory here but it looks like we have to, since there is no limit on amount of entires in "iommu-map" property.
> 
> Ravi (rpokala@)
> 
>     +		if (masked_rid < rid_base ||
>     +		    masked_rid >= (rid_base + rid_length))
>     +			continue;
>     +
>     +		iommu_base = map[i + 2];
>     +
>     +		if (iommu_parent != NULL)
>     +			*iommu_parent = map[i + 1];
>     +		if (iommu_rid != NULL)
>     +			*iommu_rid = masked_rid - rid_base + iommu_base;
>     +		err = 0;
>     +		break;
>     +	}
>     +
>     +	return (err);
>     +}
>     +
>      static int
>      ofw_bus_reg_to_rl_helper(device_t dev, phandle_t node, pcell_t acells, pcell_t scells,
>          struct resource_list *rl, const char *reg_source)
>     diff --git a/sys/dev/ofw/ofw_bus_subr.h b/sys/dev/ofw/ofw_bus_subr.h
>     index 45709c92c1f7..89f291f92373 100644
>     --- a/sys/dev/ofw/ofw_bus_subr.h
>     +++ b/sys/dev/ofw/ofw_bus_subr.h
>     @@ -92,8 +92,9 @@ int	ofw_bus_lookup_imap(phandle_t, struct ofw_bus_iinfo *, void *, int,
>      int	ofw_bus_search_intrmap(void *, int, void *, int, void *, int, void *,
>      	    void *, void *, int, phandle_t *);
> 
>     -/* Routines for processing msi maps */
>     +/* Routines for processing msi and iommu maps. */
>      int ofw_bus_msimap(phandle_t, uint16_t, phandle_t *, uint32_t *);
>     +int ofw_bus_iommu_map(phandle_t, uint16_t, phandle_t *, uint32_t *);
> 
>      /* Routines for parsing device-tree data into resource lists. */
>      int ofw_bus_reg_to_rl(device_t, phandle_t, pcell_t, pcell_t,
> 
>