]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
blk: Add bounce buffer support to read/write operations
authorMarek Vasut <marek.vasut+renesas@mailbox.org>
Sun, 13 Aug 2023 23:49:59 +0000 (01:49 +0200)
committerTom Rini <trini@konsulko.com>
Tue, 22 Aug 2023 19:17:53 +0000 (15:17 -0400)
Some devices have limited DMA capabilities and require that the
buffers passed to them fit specific properties. Add new optional
callback which can be used at driver level to indicate whether a
buffer alignment is suitable for the device DMA or not, and
trigger use of generic bounce buffer implementation to help use
of unsuitable buffers at the expense of performance degradation.

Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
drivers/block/blk-uclass.c
include/blk.h

index 6aac92d99627dd003c586a975e68f84ddf75f21d..885513893f6f4003d4c567900ec4273b02a7acac 100644 (file)
@@ -446,6 +446,26 @@ int blk_get_device(int uclass_id, int devnum, struct udevice **devp)
        return device_probe(*devp);
 }
 
+struct blk_bounce_buffer {
+       struct udevice          *dev;
+       struct bounce_buffer    state;
+};
+
+static int blk_buffer_aligned(struct bounce_buffer *state)
+{
+#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
+       struct blk_bounce_buffer *bbstate =
+               container_of(state, struct blk_bounce_buffer, state);
+       struct udevice *dev = bbstate->dev;
+       const struct blk_ops *ops = blk_get_ops(dev);
+
+       if (ops->buffer_aligned)
+               return ops->buffer_aligned(dev, state);
+#endif /* CONFIG_BOUNCE_BUFFER */
+
+       return 1;       /* Default, any buffer is OK */
+}
+
 long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *buf)
 {
        struct blk_desc *desc = dev_get_uclass_plat(dev);
@@ -458,7 +478,25 @@ long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *buf)
        if (blkcache_read(desc->uclass_id, desc->devnum,
                          start, blkcnt, desc->blksz, buf))
                return blkcnt;
-       blks_read = ops->read(dev, start, blkcnt, buf);
+
+       if (IS_ENABLED(CONFIG_BOUNCE_BUFFER)) {
+               struct blk_bounce_buffer bbstate = { .dev = dev };
+               int ret;
+
+               ret = bounce_buffer_start_extalign(&bbstate.state, buf,
+                                                  blkcnt * desc->blksz,
+                                                  GEN_BB_WRITE, desc->blksz,
+                                                  blk_buffer_aligned);
+               if (ret)
+                       return ret;
+
+               blks_read = ops->read(dev, start, blkcnt, bbstate.state.bounce_buffer);
+
+               bounce_buffer_stop(&bbstate.state);
+       } else {
+               blks_read = ops->read(dev, start, blkcnt, buf);
+       }
+
        if (blks_read == blkcnt)
                blkcache_fill(desc->uclass_id, desc->devnum, start, blkcnt,
                              desc->blksz, buf);
@@ -471,13 +509,33 @@ long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
 {
        struct blk_desc *desc = dev_get_uclass_plat(dev);
        const struct blk_ops *ops = blk_get_ops(dev);
+       long blks_written;
 
        if (!ops->write)
                return -ENOSYS;
 
        blkcache_invalidate(desc->uclass_id, desc->devnum);
 
-       return ops->write(dev, start, blkcnt, buf);
+       if (IS_ENABLED(CONFIG_BOUNCE_BUFFER)) {
+               struct blk_bounce_buffer bbstate = { .dev = dev };
+               int ret;
+
+               ret = bounce_buffer_start_extalign(&bbstate.state, (void *)buf,
+                                                  blkcnt * desc->blksz,
+                                                  GEN_BB_READ, desc->blksz,
+                                                  blk_buffer_aligned);
+               if (ret)
+                       return ret;
+
+               blks_written = ops->write(dev, start, blkcnt,
+                                         bbstate.state.bounce_buffer);
+
+               bounce_buffer_stop(&bbstate.state);
+       } else {
+               blks_written = ops->write(dev, start, blkcnt, buf);
+       }
+
+       return blks_written;
 }
 
 long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt)
index 8986e953e5a41e01a9f68cab2b35b5e9a496fe52..b819f97c2f1e06d11a3c9f7f6dac116c99f0da0d 100644 (file)
@@ -7,6 +7,7 @@
 #ifndef BLK_H
 #define BLK_H
 
+#include <bouncebuf.h>
 #include <dm/uclass-id.h>
 #include <efi.h>
 
@@ -260,6 +261,24 @@ struct blk_ops {
         * @return 0 if OK, -ve on error
         */
        int (*select_hwpart)(struct udevice *dev, int hwpart);
+
+#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
+       /**
+        * buffer_aligned() - test memory alignment of block operation buffer
+        *
+        * Some devices have limited DMA capabilities and require that the
+        * buffers passed to them fit specific properties. This optional
+        * callback can be used to indicate whether a buffer alignment is
+        * suitable for the device DMA or not, and trigger use of generic
+        * bounce buffer implementation to help use of unsuitable buffers
+        * at the expense of performance degradation.
+        *
+        * @dev:        Block device associated with the request
+        * @state:      Bounce buffer state
+        * @return 1 if OK, 0 if unaligned
+        */
+       int (*buffer_aligned)(struct udevice *dev, struct bounce_buffer *state);
+#endif /* CONFIG_BOUNCE_BUFFER */
 };
 
 /*