public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Justin Weiss <justin@justinweiss.com>
Cc: "Alex Lanzano" <lanzano.alex@gmail.com>,
	"Lars-Peter Clausen" <lars@metafoo.de>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Derek J . Clark" <derekjohn.clark@gmail.com>,
	"Philip Müller" <philm@manjaro.org>
Subject: Re: [PATCH 2/3] iio: imu: Add triggered buffer for Bosch BMI270 IMU
Date: Mon, 14 Oct 2024 20:01:13 +0100	[thread overview]
Message-ID: <20241014200113.2a7b2a35@jic23-huawei> (raw)
In-Reply-To: <871q0joh9v.fsf@justinweiss.com>

On Sun, 13 Oct 2024 13:54:36 -0700
Justin Weiss <justin@justinweiss.com> wrote:

> Jonathan Cameron <jic23@kernel.org> writes:
> 
> > On Sat, 12 Oct 2024 19:43:19 -0700
> > Justin Weiss <justin@justinweiss.com> wrote:
> >  
> >> Jonathan Cameron <jic23@kernel.org> writes:
> >>   
> >> > On Fri, 11 Oct 2024 08:37:48 -0700
> >> > Justin Weiss <justin@justinweiss.com> wrote:
> >> >    
> >> >> Set up a triggered buffer for the accel and angl_vel values.
> >> >> 
> >> >> Signed-off-by: Justin Weiss <justin@justinweiss.com>    
> >> > Hi Justin
> >> >
> >> > A few suggestions inline. Other than the DMA safe buffer thing, looks good
> >> > but you might want to consider using a single bulk read.
> >> >
> >> > My cynical view is that if someone paid for an IMU they probably want all
> >> > the channels, so optimizing for that case is a good plan.
> >> >    
> >> >> ...
> >> >> 
> >> >> +	__le16 sample;
> >> >> +
> >> >> +	for_each_set_bit(i, indio_dev->active_scan_mask, indio_dev->masklength) {
> >> >> +		ret = regmap_bulk_read(bmi270_device->regmap,
> >> >> +				       base + i * sizeof(sample),
> >> >> +				       &sample, sizeof(sample));    
> >> >
> >> > This is always a fun corner.
> >> > regmap doesn't guarantee to bounce buffer the data used by the underlying
> >> > transport. In the case of SPI that means we need a DMA safe buffer for bulk
> >> > accesses.  In practice it may well bounce the data today but there are optmizations
> >> > that would make it zero copy that might get applied in future.
> >> >
> >> > Easiest way to do that is put your sample variable in the iio_priv structure
> >> > at the end and mark it __aligned(IIO_DMA_MINALIGN)
> >> >
> >> > Given you are reading a bunch of contiguous registers here it may well make
> >> > sense to do a single bulk read directly into buf and then use
> >> > the available_scan_masks to let the IIO core know it always gets a full set
> >> > of samples. Then if the user selects a subset the IIO core will reorganize
> >> > the data that they get presented with.    
> >> 
> >> That's convenient :-)
> >> 
> >> It should make this much simpler. To clarify, I'll use regmap_bulk_read
> >> to read all of the registers at once into a stack-allocated buffer, and
> >> then push that buffer. Then I can remove bmi270_device->buf entirely,
> >> and avoid the DMA problem that way.  
> >
> > Given this supports SPI. The target buffer can't be on the stack.
> > You still need the __aligned(IIO_DMA_MINALIGN) element in your iio_priv()
> > structure.
> >  
> 
> Got it. I see that the BMI323 driver does the regmap_read into the
> DMA-aligned buffer, and then copies it to the timestamp-aligned buffer,
> which it then sends to iio_push_to_buffers_with_timestamp. Is that a
> good way to handle this?

Yes.  We don't have a zero copy path so that's the best we can do.

> 
> I think the timestamp-aligned buffer could still be stack-allocated in
> that case.

No it can't.  __aligned doesn't do anything useful for us on the stack
as we don't control what comes after it.  You could in theory force alignment
and pad - as long as compilers now respect __aligned  for this case (they
didn't use to!)  It's potentially a few hundred bytes on the stack, so
better on the heap.

> Or maybe a second buffer isn't even necessary, if
> DMA_MINALIGN is at least the correct alignment for
> iio_push_to_buffers_with_timestamp and I could pass the DMA-aligned
> buffer in.

Common trick is to just DMA into almost what you already have in bmi270_data.
But now the timestamp is in a fixed place you can use a structure to handle
the alignment for you.
	/*
+	 * Where IIO_DMA_MINALIGN is larger than 8 bytes, align to that
+	 * to ensure a DMA safe buffer.
+	 */
+	//__le16 buf[12] __aligned(IIO_DMA_MINALIGN);
	struct {
		__le16 channels[6];
		aligned_s64 timestamp; //type is only in iio tree currently.
	} data __aligned(IIO_DMA_MINALIGN);

The aligned_s64 is needed for x86_32 where s64 is only aligned to 4 bytes
whereas IIO assume natural alignment of everything (so 8 bytes).
 };

Then pass that to the push_to_buffers call just as you currently do.

Jonathan


 


  reply	other threads:[~2024-10-14 19:01 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-11 15:37 [PATCH 0/3] Add i2c driver for Bosch BMI260 IMU Justin Weiss
2024-10-11 15:37 ` [PATCH 1/3] iio: imu: Add i2c driver for bmi260 imu Justin Weiss
2024-10-12 11:08   ` Jonathan Cameron
2024-10-13  2:41     ` Justin Weiss
2024-10-13 15:14       ` Jonathan Cameron
2024-10-13 20:36         ` Justin Weiss
2024-10-14 18:50           ` Jonathan Cameron
2024-10-11 15:37 ` [PATCH 2/3] iio: imu: Add triggered buffer for Bosch BMI270 IMU Justin Weiss
2024-10-12 11:18   ` Jonathan Cameron
2024-10-13  2:43     ` Justin Weiss
2024-10-13 15:17       ` Jonathan Cameron
2024-10-13 20:54         ` Justin Weiss
2024-10-14 19:01           ` Jonathan Cameron [this message]
2024-10-11 15:37 ` [PATCH 3/3] iio: imu: Add scale and sampling frequency to " Justin Weiss
2024-10-12 11:35   ` Jonathan Cameron
2024-10-13  2:45     ` Justin Weiss
2024-10-13 15:40       ` Jonathan Cameron
2024-10-13 20:55         ` Justin Weiss
2024-10-14 19:11           ` Jonathan Cameron
2024-10-16  1:20             ` Justin Weiss
2024-10-18 18:02               ` Jonathan Cameron
2024-10-12 10:57 ` [PATCH 0/3] Add i2c driver for Bosch BMI260 IMU Jonathan Cameron
2024-10-13  2:36   ` Justin Weiss

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=20241014200113.2a7b2a35@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=derekjohn.clark@gmail.com \
    --cc=justin@justinweiss.com \
    --cc=lanzano.alex@gmail.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=philm@manjaro.org \
    /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