diff options
Diffstat (limited to 'drivers/gpu/nvgpu/common/mm/nvgpu_mem.c')
-rw-r--r-- | drivers/gpu/nvgpu/common/mm/nvgpu_mem.c | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/drivers/gpu/nvgpu/common/mm/nvgpu_mem.c b/drivers/gpu/nvgpu/common/mm/nvgpu_mem.c new file mode 100644 index 00000000..b4e718b4 --- /dev/null +++ b/drivers/gpu/nvgpu/common/mm/nvgpu_mem.c | |||
@@ -0,0 +1,119 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/kmem.h> | ||
24 | #include <nvgpu/nvgpu_mem.h> | ||
25 | #include <nvgpu/dma.h> | ||
26 | #include <nvgpu/vidmem.h> | ||
27 | |||
28 | #include "gk20a/gk20a.h" | ||
29 | |||
30 | void *nvgpu_sgt_get_next(struct nvgpu_sgt *sgt, void *sgl) | ||
31 | { | ||
32 | return sgt->ops->sgl_next(sgl); | ||
33 | } | ||
34 | |||
35 | u64 nvgpu_sgt_get_phys(struct nvgpu_sgt *sgt, void *sgl) | ||
36 | { | ||
37 | return sgt->ops->sgl_phys(sgl); | ||
38 | } | ||
39 | |||
40 | u64 nvgpu_sgt_get_dma(struct nvgpu_sgt *sgt, void *sgl) | ||
41 | { | ||
42 | return sgt->ops->sgl_dma(sgl); | ||
43 | } | ||
44 | |||
45 | u64 nvgpu_sgt_get_length(struct nvgpu_sgt *sgt, void *sgl) | ||
46 | { | ||
47 | return sgt->ops->sgl_length(sgl); | ||
48 | } | ||
49 | |||
50 | u64 nvgpu_sgt_get_gpu_addr(struct gk20a *g, struct nvgpu_sgt *sgt, void *sgl, | ||
51 | struct nvgpu_gmmu_attrs *attrs) | ||
52 | { | ||
53 | return sgt->ops->sgl_gpu_addr(g, sgl, attrs); | ||
54 | } | ||
55 | |||
56 | bool nvgpu_sgt_iommuable(struct gk20a *g, struct nvgpu_sgt *sgt) | ||
57 | { | ||
58 | if (sgt->ops->sgt_iommuable) | ||
59 | return sgt->ops->sgt_iommuable(g, sgt); | ||
60 | return false; | ||
61 | } | ||
62 | |||
63 | void nvgpu_sgt_free(struct gk20a *g, struct nvgpu_sgt *sgt) | ||
64 | { | ||
65 | if (sgt && sgt->ops->sgt_free) | ||
66 | sgt->ops->sgt_free(g, sgt); | ||
67 | } | ||
68 | |||
69 | u64 nvgpu_mem_iommu_translate(struct gk20a *g, u64 phys) | ||
70 | { | ||
71 | /* ensure it is not vidmem allocation */ | ||
72 | WARN_ON(nvgpu_addr_is_vidmem_page_alloc(phys)); | ||
73 | |||
74 | if (nvgpu_iommuable(g) && g->ops.mm.get_iommu_bit) | ||
75 | return phys | 1ULL << g->ops.mm.get_iommu_bit(g); | ||
76 | |||
77 | return phys; | ||
78 | } | ||
79 | |||
80 | /* | ||
81 | * Determine alignment for a passed buffer. Necessary since the buffer may | ||
82 | * appear big enough to map with large pages but the SGL may have chunks that | ||
83 | * are not aligned on a 64/128kB large page boundary. There's also the | ||
84 | * possibility chunks are odd sizes which will necessitate small page mappings | ||
85 | * to correctly glue them together into a contiguous virtual mapping. | ||
86 | */ | ||
87 | u64 nvgpu_sgt_alignment(struct gk20a *g, struct nvgpu_sgt *sgt) | ||
88 | { | ||
89 | u64 align = 0, chunk_align = 0; | ||
90 | void *sgl; | ||
91 | |||
92 | /* | ||
93 | * If this SGT is iommuable and we want to use the IOMMU address then | ||
94 | * the SGT's first entry has the IOMMU address. We will align on this | ||
95 | * and double check length of buffer later. Also, since there's an | ||
96 | * IOMMU we know that this DMA address is contiguous. | ||
97 | */ | ||
98 | if (!g->mm.bypass_smmu && | ||
99 | nvgpu_sgt_iommuable(g, sgt) && | ||
100 | nvgpu_sgt_get_dma(sgt, sgt->sgl)) | ||
101 | return 1ULL << __ffs(nvgpu_sgt_get_dma(sgt, sgt->sgl)); | ||
102 | |||
103 | /* | ||
104 | * Otherwise the buffer is not iommuable (VIDMEM, for example) or we are | ||
105 | * bypassing the IOMMU and need to use the underlying physical entries | ||
106 | * of the SGT. | ||
107 | */ | ||
108 | nvgpu_sgt_for_each_sgl(sgl, sgt) { | ||
109 | chunk_align = 1ULL << __ffs(nvgpu_sgt_get_phys(sgt, sgl) | | ||
110 | nvgpu_sgt_get_length(sgt, sgl)); | ||
111 | |||
112 | if (align) | ||
113 | align = min(align, chunk_align); | ||
114 | else | ||
115 | align = chunk_align; | ||
116 | } | ||
117 | |||
118 | return align; | ||
119 | } | ||