git: 67f40afb696e - stable/13 - linuxkpi: Add <linux/dma-buf-map.h>

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 08:54:00 UTC
The branch stable/13 has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=67f40afb696ea31bb340ea48b2ce185f076b0179

commit 67f40afb696ea31bb340ea48b2ce185f076b0179
Author:     Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
AuthorDate: 2022-11-11 17:40:57 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2023-01-24 09:08:00 +0000

    linuxkpi: Add <linux/dma-buf-map.h>
    
    I took the implementation from OpenBSD, commit
    d55ef580b1748517027c3eabdb715316ca5b1442.
    
    The only difference is the addition of `dma_buf_map_is_equal()`.
    
    Reviewed by:    manu
    Approved by:    manu
    Differential Revision:  https://reviews.freebsd.org/D36963
    
    (cherry picked from commit 0e5569a08cf02e036774235e54e1008a26167b36)
---
 .../linuxkpi/common/include/linux/dma-buf-map.h    | 91 ++++++++++++++++++++++
 1 file changed, 91 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/linux/dma-buf-map.h b/sys/compat/linuxkpi/common/include/linux/dma-buf-map.h
new file mode 100644
index 000000000000..567ce3b072b3
--- /dev/null
+++ b/sys/compat/linuxkpi/common/include/linux/dma-buf-map.h
@@ -0,0 +1,91 @@
+/* Public domain. */
+
+#ifndef _LINUX_DMA_BUF_MAP_H
+#define _LINUX_DMA_BUF_MAP_H
+
+#include <linux/io.h>
+#include <linux/string.h>
+
+struct dma_buf_map {
+	union {
+		void *vaddr_iomem;
+		void *vaddr;
+	};
+	bool is_iomem;
+};
+
+static inline void
+dma_buf_map_incr(struct dma_buf_map *dbm, size_t n)
+{
+	if (dbm->is_iomem)
+		dbm->vaddr_iomem += n;
+	else
+		dbm->vaddr += n;
+}
+
+static inline void
+dma_buf_map_memcpy_to(struct dma_buf_map *dbm, const void *src, size_t len)
+{
+	if (dbm->is_iomem)
+		memcpy_toio(dbm->vaddr_iomem, src, len);
+	else
+		memcpy(dbm->vaddr, src, len);
+}
+
+static inline bool
+dma_buf_map_is_null(const struct dma_buf_map *dbm)
+{
+	if (dbm->is_iomem)
+		return (dbm->vaddr_iomem == NULL);
+	else
+		return (dbm->vaddr == NULL);
+}
+
+static inline bool
+dma_buf_map_is_set(const struct dma_buf_map *dbm)
+{
+	if (dbm->is_iomem)
+		return (dbm->vaddr_iomem != NULL);
+	else
+		return (dbm->vaddr != NULL);
+}
+
+static inline bool
+dma_buf_map_is_equal(
+    const struct dma_buf_map *dbm_a, const struct dma_buf_map *dbm_b)
+{
+	if (dbm_a->is_iomem != dbm_b->is_iomem)
+		return (false);
+
+	if (dbm_a->is_iomem)
+		return (dbm_a->vaddr_iomem == dbm_b->vaddr_iomem);
+	else
+		return (dbm_a->vaddr == dbm_b->vaddr);
+}
+
+static inline void
+dma_buf_map_clear(struct dma_buf_map *dbm)
+{
+	if (dbm->is_iomem) {
+		dbm->vaddr_iomem = NULL;
+		dbm->is_iomem = false;
+	} else {
+		dbm->vaddr = NULL;
+	}
+}
+
+static inline void
+dma_buf_map_set_vaddr_iomem(struct dma_buf_map *dbm, void *addr)
+{
+	dbm->vaddr_iomem = addr;
+	dbm->is_iomem = true;
+}
+
+static inline void
+dma_buf_map_set_vaddr(struct dma_buf_map *dbm, void *addr)
+{
+	dbm->vaddr = addr;
+	dbm->is_iomem = false;
+}
+
+#endif