Linux IOMMU Development
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Jim Quinlan <james.quinlan@broadcom.com>
Cc: Rich Felker <dalias@libc.org>,
	"open list:SUPERH" <linux-sh@vger.kernel.org>,
	David Airlie <airlied@linux.ie>,
	linux-pci@vger.kernel.org, Hanjun Guo <guohanjun@huawei.com>,
	"open list:REMOTE PROCESSOR \(REMOTEPROC\) SUBSYSTEM"
	<linux-remoteproc@vger.kernel.org>,
	"open list:DRM DRIVERS FOR ALLWINNER A10"
	<dri-devel@lists.freedesktop.org>,
	Julien Grall <julien.grall@arm.com>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>, Will Deacon <will@kernel.org>,
	Christoph Hellwig <hch@lst.de>,
	"open list:STAGING SUBSYSTEM" <devel@driverdev.osuosl.org>,
	Jean-Philippe Brucker <jean-philippe@linaro.org>,
	Yoshinori Sato <ysato@users.sourceforge.jp>,
	Frank Rowand <frowand.list@gmail.com>,
	"maintainer:X86 ARCHITECTURE \(32-BIT AND 64-BIT\)"
	<x86@kernel.org>, Russell King <linux@armlinux.org.uk>,
	"open list:ACPI FOR ARM64 \(ACPI/arm64\)"
	<linux-acpi@vger.kernel.org>, Chen-Yu Tsai <wens@csie.org>,
	Ingo Molnar <mingo@redhat.com>,
	bcm-kernel-feedback-list@broadcom.com,
	Alan Stern <stern@rowland.harvard.edu>,
	Len Brown <lenb@kernel.org>, Ohad Ben-Cohen <ohad@wizery.com>,
	"open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE"
	<devicetree@vger.kernel.org>, Arnd Bergmann <arnd@arndb.de>,
	Maxime Ripard <mripard@kernel.org>,
	Rob Herring <robh+dt@kernel.org>, Borislav Petkov <bp@alien8.de>,
	Yong Deng <yong.deng@magewell.com>,
	Santosh Shilimkar <ssantosh@kernel.org>,
	Bjorn Helgaas <bhelgaas@google.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	"moderated list:ARM PORT" <linux-arm-kernel@lists.infradead.org>,
	Saravana Kannan <saravanak@google.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"open list:USB SUBSYSTEM" <linux-usb@vger.kernel.org>,
	"Rafael J. Wysocki" <rjw@rjwysocki.net>,
	open list <linux-kernel@vger.kernel.org>,
	Paul Kocialkowski <paul.kocialkowski@bootlin.com>,
	"open list:IOMMU DRIVERS" <iommu@lists.linux-foundation.org>,
	Stefano Stabellini <sstabellini@kernel.org>,
	Daniel Vetter <daniel@ffwll.ch>,
	Sudeep Holla <sudeep.holla@arm.com>,
	"open list:ALLWINNER A10 CSI DRIVER"
	<linux-media@vger.kernel.org>,
	Robin Murphy <robin.murphy@arm.com>
Subject: Re: [PATCH RESEND v10 07/11] device-mapping: Introduce DMA range map, supplanting dma_pfn_offset
Date: Tue, 18 Aug 2020 11:12:25 +0300	[thread overview]
Message-ID: <20200818081225.GA1891694@smile.fi.intel.com> (raw)
In-Reply-To: <20200817215326.30912-8-james.quinlan@broadcom.com>

On Mon, Aug 17, 2020 at 05:53:09PM -0400, Jim Quinlan wrote:
> The new field 'dma_range_map' in struct device is used to facilitate the
> use of single or multiple offsets between mapping regions of cpu addrs and
> dma addrs.  It subsumes the role of "dev->dma_pfn_offset" which was only
> capable of holding a single uniform offset and had no region bounds
> checking.
> 
> The function of_dma_get_range() has been modified so that it takes a single
> argument -- the device node -- and returns a map, NULL, or an error code.
> The map is an array that holds the information regarding the DMA regions.
> Each range entry contains the address offset, the cpu_start address, the
> dma_start address, and the size of the region.
> 
> of_dma_configure() is the typical manner to set range offsets but there are
> a number of ad hoc assignments to "dev->dma_pfn_offset" in the kernel
> driver code.  These cases now invoke the function
> dma_attach_offset_range(dev, cpu_addr, dma_addr, size).

...

> +	if (dev) {
> +		phys_addr_t paddr = PFN_PHYS(pfn);
> +

> +		pfn -= (dma_offset_from_phys_addr(dev, paddr) >> PAGE_SHIFT);

PFN_DOWN() ?

> +	}

...

> +		pfn += (dma_offset_from_dma_addr(dev, addr) >> PAGE_SHIFT);

Ditto.


...

> +static inline u64 dma_offset_from_dma_addr(struct device *dev, dma_addr_t dma_addr)
> +{
> +	const struct bus_dma_region *m = dev->dma_range_map;
> +
> +	if (!m)
> +		return 0;
> +	for (; m->size; m++)
> +		if (dma_addr >= m->dma_start && dma_addr - m->dma_start < m->size)
> +			return m->offset;
> +	return 0;
> +}
> +
> +static inline u64 dma_offset_from_phys_addr(struct device *dev, phys_addr_t paddr)
> +{
> +	const struct bus_dma_region *m = dev->dma_range_map;
> +
> +	if (!m)
> +		return 0;
> +	for (; m->size; m++)
> +		if (paddr >= m->cpu_start && paddr - m->cpu_start < m->size)
> +			return m->offset;
> +	return 0;
> +}

Perhaps for these the form with one return 0 is easier to read

	if (m) {
		for (; m->size; m++)
			if (paddr >= m->cpu_start && paddr - m->cpu_start < m->size)
				return m->offset;
	}
	return 0;

?

...

> +	if (mem->use_dev_dma_pfn_offset) {
> +		u64 base_addr = (u64)mem->pfn_base << PAGE_SHIFT;

PFN_PHYS() ?

> +
> +		return base_addr - dma_offset_from_phys_addr(dev, base_addr);
> +	}

...

> + * It returns -ENOMEM if out of memory, 0 otherwise.

This doesn't describe cases dev->dma_range_map != NULL and offset == 0.

> +int dma_set_offset_range(struct device *dev, phys_addr_t cpu_start,
> +			 dma_addr_t dma_start, u64 size)
> +{
> +	struct bus_dma_region *map;
> +	u64 offset = (u64)cpu_start - (u64)dma_start;
> +
> +	if (!offset)
> +		return 0;
> +
> +	if (dev->dma_range_map) {
> +		dev_err(dev, "attempt to add DMA range to existing map\n");
> +		return -EINVAL;
> +	}
> +
> +	map = kcalloc(2, sizeof(*map), GFP_KERNEL);
> +	if (!map)
> +		return -ENOMEM;
> +	map[0].cpu_start = cpu_start;
> +	map[0].dma_start = dma_start;
> +	map[0].offset = offset;
> +	map[0].size = size;
> +	dev->dma_range_map = map;
> +
> +	return 0;
> +}

...

> +void *dma_copy_dma_range_map(const struct bus_dma_region *map)
> +{
> +	int num_ranges;
> +	struct bus_dma_region *new_map;
> +	const struct bus_dma_region *r = map;
> +
> +	for (num_ranges = 0; r->size; num_ranges++)
> +		r++;

> +	new_map = kcalloc(num_ranges + 1, sizeof(*map), GFP_KERNEL);
> +	if (new_map)
> +		memcpy(new_map, map, sizeof(*map) * num_ranges);

Looks like krealloc() on the first glance...

> +
> +	return new_map;
> +}

-- 
With Best Regards,
Andy Shevchenko


_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

  reply	other threads:[~2020-08-18  8:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-17 21:53 [PATCH RESEND v10 00/11] PCI: brcmstb: enable PCIe for STB chips Jim Quinlan via iommu
2020-08-17 21:53 ` [PATCH RESEND v10 07/11] device-mapping: Introduce DMA range map, supplanting dma_pfn_offset Jim Quinlan via iommu
2020-08-18  8:12   ` Andy Shevchenko [this message]
2020-08-20 13:37     ` Jim Quinlan via iommu
2020-08-21  8:02       ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200818081225.GA1891694@smile.fi.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=airlied@linux.ie \
    --cc=arnd@arndb.de \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=bhelgaas@google.com \
    --cc=bp@alien8.de \
    --cc=dalias@libc.org \
    --cc=daniel@ffwll.ch \
    --cc=devel@driverdev.osuosl.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=frowand.list@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=guohanjun@huawei.com \
    --cc=hch@lst.de \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=iommu@lists.linux-foundation.org \
    --cc=james.quinlan@broadcom.com \
    --cc=jean-philippe@linaro.org \
    --cc=julien.grall@arm.com \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-remoteproc@vger.kernel.org \
    --cc=linux-sh@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=mchehab@kernel.org \
    --cc=mingo@redhat.com \
    --cc=mripard@kernel.org \
    --cc=ohad@wizery.com \
    --cc=paul.kocialkowski@bootlin.com \
    --cc=rjw@rjwysocki.net \
    --cc=robh+dt@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=saravanak@google.com \
    --cc=ssantosh@kernel.org \
    --cc=sstabellini@kernel.org \
    --cc=stern@rowland.harvard.edu \
    --cc=sudeep.holla@arm.com \
    --cc=tglx@linutronix.de \
    --cc=wens@csie.org \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    --cc=yong.deng@magewell.com \
    --cc=ysato@users.sourceforge.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox