diff options
| author | Joshua Bakita <bakitajoshua@gmail.com> | 2023-06-28 18:24:25 -0400 |
|---|---|---|
| committer | Joshua Bakita <bakitajoshua@gmail.com> | 2023-06-28 18:24:25 -0400 |
| commit | 01e6fac4d61fdd7fff5433942ec93fc2ea1e4df1 (patch) | |
| tree | 4ef34501728a087be24f4ba0af90f91486bf780b /include/os/linux/linux-dma.c | |
| parent | 306a03d18b305e4e573be3b2931978fa10679eb9 (diff) | |
Include nvgpu headers
These are needed to build on NVIDIA's Jetson boards for the time
being. Only a couple structs are required, so it should be fairly
easy to remove this dependency at some point in the future.
Diffstat (limited to 'include/os/linux/linux-dma.c')
| -rw-r--r-- | include/os/linux/linux-dma.c | 534 |
1 files changed, 534 insertions, 0 deletions
diff --git a/include/os/linux/linux-dma.c b/include/os/linux/linux-dma.c new file mode 100644 index 0000000..d704b2a --- /dev/null +++ b/include/os/linux/linux-dma.c | |||
| @@ -0,0 +1,534 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved. | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify it | ||
| 5 | * under the terms and conditions of the GNU General Public License, | ||
| 6 | * version 2, as published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT | ||
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 11 | * more details. | ||
| 12 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include <linux/dma-mapping.h> | ||
| 18 | #include <linux/version.h> | ||
| 19 | |||
| 20 | #include <nvgpu/log.h> | ||
| 21 | #include <nvgpu/dma.h> | ||
| 22 | #include <nvgpu/lock.h> | ||
| 23 | #include <nvgpu/bug.h> | ||
| 24 | #include <nvgpu/gmmu.h> | ||
| 25 | #include <nvgpu/kmem.h> | ||
| 26 | #include <nvgpu/enabled.h> | ||
| 27 | #include <nvgpu/vidmem.h> | ||
| 28 | #include <nvgpu/gk20a.h> | ||
| 29 | |||
| 30 | #include <nvgpu/linux/dma.h> | ||
| 31 | |||
| 32 | #include "platform_gk20a.h" | ||
| 33 | #include "os_linux.h" | ||
| 34 | #include "dmabuf_vidmem.h" | ||
| 35 | |||
| 36 | #ifdef __DMA_ATTRS_LONGS | ||
| 37 | #define NVGPU_DEFINE_DMA_ATTRS(x) \ | ||
| 38 | struct dma_attrs x = { \ | ||
| 39 | .flags = { [0 ... __DMA_ATTRS_LONGS-1] = 0 }, \ | ||
| 40 | } | ||
| 41 | #define NVGPU_DMA_ATTR(attrs) &attrs | ||
| 42 | #else | ||
| 43 | #define NVGPU_DEFINE_DMA_ATTRS(attrs) unsigned long attrs = 0 | ||
| 44 | #define NVGPU_DMA_ATTR(attrs) attrs | ||
| 45 | #endif | ||
| 46 | |||
| 47 | /* | ||
| 48 | * Enough to hold all the possible flags in string form. When a new flag is | ||
| 49 | * added it must be added here as well!! | ||
| 50 | */ | ||
| 51 | #define NVGPU_DMA_STR_SIZE \ | ||
| 52 | sizeof("NO_KERNEL_MAPPING FORCE_CONTIGUOUS") | ||
| 53 | |||
| 54 | /* | ||
| 55 | * The returned string is kmalloc()ed here but must be freed by the caller. | ||
| 56 | */ | ||
| 57 | static char *nvgpu_dma_flags_to_str(struct gk20a *g, unsigned long flags) | ||
| 58 | { | ||
| 59 | char *buf = nvgpu_kzalloc(g, NVGPU_DMA_STR_SIZE); | ||
| 60 | int bytes_available = NVGPU_DMA_STR_SIZE; | ||
| 61 | |||
| 62 | /* | ||
| 63 | * Return the empty buffer if there's no flags. Makes it easier on the | ||
| 64 | * calling code to just print it instead of any if (NULL) type logic. | ||
| 65 | */ | ||
| 66 | if (!flags) | ||
| 67 | return buf; | ||
| 68 | |||
| 69 | #define APPEND_FLAG(flag, str_flag) \ | ||
| 70 | do { \ | ||
| 71 | if (flags & flag) { \ | ||
| 72 | strncat(buf, str_flag, bytes_available); \ | ||
| 73 | bytes_available -= strlen(str_flag); \ | ||
| 74 | } \ | ||
| 75 | } while (0) | ||
| 76 | |||
| 77 | APPEND_FLAG(NVGPU_DMA_NO_KERNEL_MAPPING, "NO_KERNEL_MAPPING "); | ||
| 78 | APPEND_FLAG(NVGPU_DMA_FORCE_CONTIGUOUS, "FORCE_CONTIGUOUS "); | ||
| 79 | #undef APPEND_FLAG | ||
| 80 | |||
| 81 | return buf; | ||
| 82 | } | ||
| 83 | |||
| 84 | /** | ||
| 85 | * __dma_dbg - Debug print for DMA allocs and frees. | ||
| 86 | * | ||
| 87 | * @g - The GPU. | ||
| 88 | * @size - The requested size of the alloc (size_t). | ||
| 89 | * @flags - The flags (unsigned long). | ||
| 90 | * @type - A string describing the type (i.e: sysmem or vidmem). | ||
| 91 | * @what - A string with 'alloc' or 'free'. | ||
| 92 | * | ||
| 93 | * @flags is the DMA flags. If there are none or it doesn't make sense to print | ||
| 94 | * flags just pass 0. | ||
| 95 | * | ||
| 96 | * Please use dma_dbg_alloc() and dma_dbg_free() instead of this function. | ||
| 97 | */ | ||
| 98 | static void __dma_dbg(struct gk20a *g, size_t size, unsigned long flags, | ||
| 99 | const char *type, const char *what, | ||
| 100 | const char *func, int line) | ||
| 101 | { | ||
| 102 | char *flags_str = NULL; | ||
| 103 | |||
| 104 | /* | ||
| 105 | * Don't bother making the flags_str if debugging is | ||
| 106 | * not enabled. This saves a malloc and a free. | ||
| 107 | */ | ||
| 108 | if (!nvgpu_log_mask_enabled(g, gpu_dbg_dma)) | ||
| 109 | return; | ||
| 110 | |||
| 111 | flags_str = nvgpu_dma_flags_to_str(g, flags); | ||
| 112 | |||
| 113 | __nvgpu_log_dbg(g, gpu_dbg_dma, | ||
| 114 | func, line, | ||
| 115 | "DMA %s: [%s] size=%-7zu " | ||
| 116 | "aligned=%-7zu total=%-10llukB %s", | ||
| 117 | what, type, | ||
| 118 | size, PAGE_ALIGN(size), | ||
| 119 | g->dma_memory_used >> 10, | ||
| 120 | flags_str); | ||
| 121 | |||
| 122 | if (flags_str) | ||
| 123 | nvgpu_kfree(g, flags_str); | ||
| 124 | } | ||
| 125 | |||
| 126 | #define dma_dbg_alloc(g, size, flags, type) \ | ||
| 127 | __dma_dbg(g, size, flags, type, "alloc", __func__, __LINE__) | ||
| 128 | #define dma_dbg_free(g, size, flags, type) \ | ||
| 129 | __dma_dbg(g, size, flags, type, "free", __func__, __LINE__) | ||
| 130 | |||
| 131 | /* | ||
| 132 | * For after the DMA alloc is done. | ||
| 133 | */ | ||
| 134 | #define __dma_dbg_done(g, size, type, what) \ | ||
| 135 | nvgpu_log(g, gpu_dbg_dma, \ | ||
| 136 | "DMA %s: [%s] size=%-7zu Done!", \ | ||
| 137 | what, type, size); \ | ||
| 138 | |||
| 139 | #define dma_dbg_alloc_done(g, size, type) \ | ||
| 140 | __dma_dbg_done(g, size, type, "alloc") | ||
| 141 | #define dma_dbg_free_done(g, size, type) \ | ||
| 142 | __dma_dbg_done(g, size, type, "free") | ||
| 143 | |||
| 144 | #if defined(CONFIG_GK20A_VIDMEM) | ||
| 145 | static u64 __nvgpu_dma_alloc(struct nvgpu_allocator *allocator, u64 at, | ||
| 146 | size_t size) | ||
| 147 | { | ||
| 148 | u64 addr = 0; | ||
| 149 | |||
| 150 | if (at) | ||
| 151 | addr = nvgpu_alloc_fixed(allocator, at, size, 0); | ||
| 152 | else | ||
| 153 | addr = nvgpu_alloc(allocator, size); | ||
| 154 | |||
| 155 | return addr; | ||
| 156 | } | ||
| 157 | #endif | ||
| 158 | |||
| 159 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0) | ||
| 160 | static void nvgpu_dma_flags_to_attrs(unsigned long *attrs, | ||
| 161 | unsigned long flags) | ||
| 162 | #define ATTR_ARG(x) *x | ||
| 163 | #else | ||
| 164 | static void nvgpu_dma_flags_to_attrs(struct dma_attrs *attrs, | ||
| 165 | unsigned long flags) | ||
| 166 | #define ATTR_ARG(x) x | ||
| 167 | #endif | ||
| 168 | { | ||
| 169 | if (flags & NVGPU_DMA_NO_KERNEL_MAPPING) | ||
| 170 | dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, ATTR_ARG(attrs)); | ||
| 171 | if (flags & NVGPU_DMA_FORCE_CONTIGUOUS) | ||
| 172 | dma_set_attr(DMA_ATTR_FORCE_CONTIGUOUS, ATTR_ARG(attrs)); | ||
| 173 | #undef ATTR_ARG | ||
| 174 | } | ||
| 175 | |||
| 176 | int nvgpu_dma_alloc_flags_sys(struct gk20a *g, unsigned long flags, | ||
| 177 | size_t size, struct nvgpu_mem *mem) | ||
| 178 | { | ||
| 179 | struct device *d = dev_from_gk20a(g); | ||
| 180 | int err; | ||
| 181 | dma_addr_t iova; | ||
| 182 | NVGPU_DEFINE_DMA_ATTRS(dma_attrs); | ||
| 183 | void *alloc_ret; | ||
| 184 | |||
| 185 | if (nvgpu_mem_is_valid(mem)) { | ||
| 186 | nvgpu_warn(g, "memory leak !!"); | ||
| 187 | WARN_ON(1); | ||
| 188 | } | ||
| 189 | |||
| 190 | /* | ||
| 191 | * WAR for IO coherent chips: the DMA API does not seem to generate | ||
| 192 | * mappings that work correctly. Unclear why - Bug ID: 2040115. | ||
| 193 | * | ||
| 194 | * Basically we just tell the DMA API not to map with NO_KERNEL_MAPPING | ||
| 195 | * and then make a vmap() ourselves. | ||
| 196 | */ | ||
| 197 | if (nvgpu_is_enabled(g, NVGPU_USE_COHERENT_SYSMEM)) | ||
| 198 | flags |= NVGPU_DMA_NO_KERNEL_MAPPING; | ||
| 199 | |||
| 200 | /* | ||
| 201 | * Before the debug print so we see this in the total. But during | ||
| 202 | * cleanup in the fail path this has to be subtracted. | ||
| 203 | */ | ||
| 204 | g->dma_memory_used += PAGE_ALIGN(size); | ||
| 205 | |||
| 206 | dma_dbg_alloc(g, size, flags, "sysmem"); | ||
| 207 | |||
| 208 | /* | ||
| 209 | * Save the old size but for actual allocation purposes the size is | ||
| 210 | * going to be page aligned. | ||
| 211 | */ | ||
| 212 | mem->size = size; | ||
| 213 | size = PAGE_ALIGN(size); | ||
| 214 | |||
