🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: muc.lists.netbsd.tech.kern
4 messages
4 total messages Started by Taylor R Campbell Tue, 27 Oct 2026 14:16
bus_dma without bouncing
#8
Author: Taylor R Campbell
Date: Tue, 27 Oct 2026 14:16
60 lines
4613 bytes
NIC drivers allocate rx buffers for the device to read network data
into through DMA.  In some devices the device's DMA has constraints,
specified through the DMA tag and/or bus_dmamap_create parameters, and
enforced in bus_dmamap_load by bouncing.

To avoid bouncing, or to satisfy alignment requirements imposed by the
device[1], drivers will often allocate rx buffers themselves with
bus_dmamem_alloc/map.

But, given constraints, bus_dmamap_create will _also_ allocate bounce
buffers with bus_dmamem_alloc/map -- even if the driver will never
bounce because it already allocated rx buffers satisfying the
constraint.  This wastes physical pages and kva that will never be
used -- except if there's a driver bug causing every rx buffer to be
bounced unintentionally, which quietly gives bad performance!

Should we have a way to avoid this?

Here are a couple options that come to mind (with 30sec of thought put
into each name, and alternate bike shed paint suggestions welcome):

1. An option BUS_DMA_BOUNCELESS to bus_dmamap_create that causes
   bus_dmamap_load to fail instead of bouncing if constraints are not
   met.

   => This simple API change generally obviates the need to allocate
      bounce buffers in the scenario above.  However, some
      architectural constraints such as confidential computing
      architecture (AMD SEV, Intel TDX) may still require bouncing for
      other reasons (must selectively expose page unencrypted to the
      hypervisor).

2. (a) An option BUS_DMA_ALLOCBUF to bus_dmamap_create that _forces_
       it to allocate a bounce buffer with bus_dmamem_alloc/map
       meeting the constraints.
   (b) Another operation, such as bus_dmamap_bufmem, to get at the
       bounce buffer so a driver can work directly with it instead of
       calling bus_dmamem_alloc/map itself.

   => This would allow us to avoid the duplicate bounce buffer in the
      confidential computing case.  But it's a more complicated change
      to the API, and some bus_dma implementations -- such as sparc
      and sparc64 -- appear not to bounce at all, ever, so they would
      have to be adapted to add logic for allocating bounce bufers.



[1] It seems there is no MI way with bus_dma tags or bus_dmamap_create
    to make bus_dmamap_load enforce alignment by bouncing into an
    aligned bounce buffer if the given address is misaligned.  But
    there are plenty of devices that require aligned addresses for
    DMA, such as Intel wireless cards which require 8- or 12-bit
    alignment.  Am I missing something?  Is there just no way to
    express this constraint in bus_dmamap_create so that
    bus_dmamap_load will _either_ fail noisily or bounce on misaligned
    inputs?

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: bus_dma without bouncing
#9
Author: Mouse
Date: Tue, 27 Oct 2026 14:33
17 lines
2059 bytes
> [1] [...].  But there are plenty of devices that require aligned
>     addresses for DMA, such as Intel wireless cards which require 8-
>     or 12-bit alignment.  [...]

What does 12-bit alignment even _mean_ on an 8-bit-addressing-unit
machine (which IIRC includes everything NetBSD runs on)?  Does it
differ from 3-byte alignment in any practical sense?  Do the interfaces
even support alignment to other than a power of two?

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse@rodents-montreal.org
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: bus_dma without bouncing
#10
Author: Taylor R Campbell
Date: Tue, 27 Oct 2026 14:43
34 lines
3413 bytes
> Date: Tue, 27 Oct 2026 10:33:46 -0400 (EDT)
> From: Mouse <mouse@Rodents-Montreal.ORG>
> 
> > [1] [...].  But there are plenty of devices that require aligned
> >     addresses for DMA, such as Intel wireless cards which require 8-
> >     or 12-bit alignment.  [...]
> 
> What does 12-bit alignment even _mean_ on an 8-bit-addressing-unit
> machine (which IIRC includes everything NetBSD runs on)?  Does it
> differ from 3-byte alignment in any practical sense?  Do the interfaces
> even support alignment to other than a power of two?

By `k-bit alignment' I meant that the k low-order bits of the (byte)
addresses must be zero.  I.e., an address is 8-bit-aligned if it is an
integral multiple of 256, and an address is 12-bit-aligned if it is an
integral multiple of 4096, under this definition.

This constraint arises because the DMA registers don't even have space
for the low 8 (or 4 or 12 or whatever) bits:

   2046 	/* Set physical address of RX ring (256-byte aligned). */
   2047 	IWM_WRITE(sc,
   2048 	    IWM_FH_RSCSR_CHNL0_RBDCB_BASE_REG, sc->rxq.desc_dma.paddr >> 8);
   2049 
   2050 	/* Set physical address of RX status (16-byte aligned). */
   2051 	IWM_WRITE(sc,
   2052 	    IWM_FH_RSCSR_CHNL0_STTS_WPTR_REG, sc->rxq.stat_dma.paddr >> 4);

https://nxr.NetBSD.org/xref/src/sys/dev/pci/if_iwm.c?r=1.94#2046

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: bus_dma without bouncing
#11
Author: Yuri Honegger
Date: Tue, 27 Oct 2026 14:51
88 lines
5788 bytes

> Am 27.10.2026 um 15:16 schrieb Taylor R Campbell <riastradh@NetBSD.org>:
> 
> NIC drivers allocate rx buffers for the device to read network data
> into through DMA.  In some devices the device's DMA has constraints,
> specified through the DMA tag and/or bus_dmamap_create parameters, and
> enforced in bus_dmamap_load by bouncing.
> 
> To avoid bouncing, or to satisfy alignment requirements imposed by the
> device[1], drivers will often allocate rx buffers themselves with
> bus_dmamem_alloc/map.

Another case that I currently don’t know how to represent is a device that support multisegment dma, but all segment sizes need to be a multiple of 64 bytes. This is the case for the DMA controller on some TI SoCs (am335x, am1808).

> 
> But, given constraints, bus_dmamap_create will _also_ allocate bounce
> buffers with bus_dmamem_alloc/map -- even if the driver will never
> bounce because it already allocated rx buffers satisfying the
> constraint.  This wastes physical pages and kva that will never be
> used -- except if there's a driver bug causing every rx buffer to be
> bounced unintentionally, which quietly gives bad performance!
> 
> Should we have a way to avoid this?

Yes, but that is treating the symptoms. I’ve been wondering what if we allow drivers to express more constraints for how their DMA memory looks like so they no longer need to manage bounce buffer themselves?

> 
> Here are a couple options that come to mind (with 30sec of thought put
> into each name, and alternate bike shed paint suggestions welcome):
> 
> 1. An option BUS_DMA_BOUNCELESS to bus_dmamap_create that causes
>   bus_dmamap_load to fail instead of bouncing if constraints are not
>   met.

BUS_DMA_NOBOUNCE would be a bit more in line with the other constants, but I don’t particularly care.

> 
>   => This simple API change generally obviates the need to allocate
>      bounce buffers in the scenario above.  However, some
>      architectural constraints such as confidential computing
>      architecture (AMD SEV, Intel TDX) may still require bouncing for
>      other reasons (must selectively expose page unencrypted to the
>      hypervisor).
> 
> 2. (a) An option BUS_DMA_ALLOCBUF to bus_dmamap_create that _forces_
>       it to allocate a bounce buffer with bus_dmamem_alloc/map
>       meeting the constraints.

How does this differ from BUS_DMA_ALLOCNOW?

>   (b) Another operation, such as bus_dmamap_bufmem, to get at the
>       bounce buffer so a driver can work directly with it instead of
>       calling bus_dmamem_alloc/map itself.
> 
>   => This would allow us to avoid the duplicate bounce buffer in the
>      confidential computing case.  But it's a more complicated change
>      to the API, and some bus_dma implementations -- such as sparc
>      and sparc64 -- appear not to bounce at all, ever, so they would
>      have to be adapted to add logic for allocating bounce bufers.

To me, the complexity doesn’t seem worth it compared to adding a BOUNCELESS flag.

> 
> 
> 
> [1] It seems there is no MI way with bus_dma tags or bus_dmamap_create
>    to make bus_dmamap_load enforce alignment by bouncing into an
>    aligned bounce buffer if the given address is misaligned.  But
>    there are plenty of devices that require aligned addresses for
>    DMA, such as Intel wireless cards which require 8- or 12-bit
>    alignment.  Am I missing something?  Is there just no way to
>    express this constraint in bus_dmamap_create so that
>    bus_dmamap_load will _either_ fail noisily or bounce on misaligned
>    inputs?


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads