Re: [PATCH kernel v2 1/2] dma: Allow mixing bypass and normal IOMMU operation

From: Christoph Hellwig
Date: Tue Oct 27 2020 - 12:50:43 EST


> +static inline bool dma_handle_direct(struct device *dev, dma_addr_t dma_handle)
> +{
> + return dma_handle >= dev->archdata.dma_offset;
> +}

This won't compile except for powerpc, and directly accesing arch members
in common code is a bad idea. Maybe both your helpers need to be
supplied by arch code to better abstract this out.

> if (dma_map_direct(dev, ops))
> addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
> +#ifdef CONFIG_DMA_OPS_BYPASS_BUS_LIMIT
> + else if (dev->bus_dma_limit &&
> + can_map_direct(dev, (phys_addr_t) page_to_phys(page) + offset + size))
> + addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
> +#endif

I don't think page_to_phys needs a phys_addr_t on the return value.
I'd also much prefer if we make this a little more beautiful, here
are a few suggestions:

- hide the bus_dma_limit check inside can_map_direct, and provide a
stub so that we can avoid the ifdef
- use a better name for can_map_direct, and maybe also a better calling
convention by passing the page (the sg code also has the page), and
maybe even hide the dma_map_direct inside it.

if (dma_map_direct(dev, ops) ||
arch_dma_map_page_direct(dev, page, offset, size))
addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);

> BUG_ON(!valid_dma_direction(dir));
> if (dma_map_direct(dev, ops))
> dma_direct_unmap_page(dev, addr, size, dir, attrs);
> +#ifdef CONFIG_DMA_OPS_BYPASS_BUS_LIMIT
> + else if (dev->bus_dma_limit && dma_handle_direct(dev, addr + size))
> + dma_direct_unmap_page(dev, addr, size, dir, attrs);
> +#endif

Same here.

> if (dma_map_direct(dev, ops))
> ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
> +#ifdef CONFIG_DMA_OPS_BYPASS_BUS_LIMIT
> + else if (dev->bus_dma_limit) {
> + struct scatterlist *s;
> + bool direct = true;
> + int i;
> +
> + for_each_sg(sg, s, nents, i) {
> + direct = can_map_direct(dev, sg_phys(s) + s->offset + s->length);
> + if (!direct)
> + break;
> + }
> + if (direct)
> + ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
> + else
> + ents = ops->map_sg(dev, sg, nents, dir, attrs);
> + }
> +#endif

This needs to go into a helper as well. I think the same style as
above would work pretty nicely as well:

if (dma_map_direct(dev, ops) ||
arch_dma_map_sg_direct(dev, sg, nents))
ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
else
ents = ops->map_sg(dev, sg, nents, dir, attrs);

> +#ifdef CONFIG_DMA_OPS_BYPASS_BUS_LIMIT
> + if (dev->bus_dma_limit) {
> + struct scatterlist *s;
> + bool direct = true;
> + int i;
> +
> + for_each_sg(sg, s, nents, i) {
> + direct = dma_handle_direct(dev, s->dma_address + s->length);
> + if (!direct)
> + break;
> + }
> + if (direct) {
> + dma_direct_unmap_sg(dev, sg, nents, dir, attrs);
> + return;
> + }
> + }
> +#endif

One more time here..