1 /* $NetBSD: nouveau_nvkm_subdev_fb_ram.c,v 1.4 2021/12/19 10:51:58 riastradh Exp $ */
2
3 /*
4 * Copyright 2015 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs <bskeggs@redhat.com>
25 */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_fb_ram.c,v 1.4 2021/12/19 10:51:58 riastradh Exp $");
28
29 #define nvkm_vram(p) container_of((p), struct nvkm_vram, memory)
30 #include "ram.h"
31
32 #include <core/memory.h>
33 #include <subdev/mmu.h>
34
35 #include <linux/nbsd-namespace.h>
36
37 struct nvkm_vram {
38 struct nvkm_memory memory;
39 struct nvkm_ram *ram;
40 u8 page;
41 struct nvkm_mm_node *mn;
42 };
43
44 static int
nvkm_vram_map(struct nvkm_memory * memory,u64 offset,struct nvkm_vmm * vmm,struct nvkm_vma * vma,void * argv,u32 argc)45 nvkm_vram_map(struct nvkm_memory *memory, u64 offset, struct nvkm_vmm *vmm,
46 struct nvkm_vma *vma, void *argv, u32 argc)
47 {
48 struct nvkm_vram *vram = nvkm_vram(memory);
49 struct nvkm_vmm_map map = {
50 .memory = &vram->memory,
51 .offset = offset,
52 .mem = vram->mn,
53 };
54
55 return nvkm_vmm_map(vmm, vma, argv, argc, &map);
56 }
57
58 static u64
nvkm_vram_size(struct nvkm_memory * memory)59 nvkm_vram_size(struct nvkm_memory *memory)
60 {
61 return (u64)nvkm_mm_size(nvkm_vram(memory)->mn) << NVKM_RAM_MM_SHIFT;
62 }
63
64 static u64
nvkm_vram_addr(struct nvkm_memory * memory)65 nvkm_vram_addr(struct nvkm_memory *memory)
66 {
67 struct nvkm_vram *vram = nvkm_vram(memory);
68 if (!nvkm_mm_contiguous(vram->mn))
69 return ~0ULL;
70 return (u64)nvkm_mm_addr(vram->mn) << NVKM_RAM_MM_SHIFT;
71 }
72
73 static u8
nvkm_vram_page(struct nvkm_memory * memory)74 nvkm_vram_page(struct nvkm_memory *memory)
75 {
76 return nvkm_vram(memory)->page;
77 }
78
79 static enum nvkm_memory_target
nvkm_vram_target(struct nvkm_memory * memory)80 nvkm_vram_target(struct nvkm_memory *memory)
81 {
82 return NVKM_MEM_TARGET_VRAM;
83 }
84
85 static void *
nvkm_vram_dtor(struct nvkm_memory * memory)86 nvkm_vram_dtor(struct nvkm_memory *memory)
87 {
88 struct nvkm_vram *vram = nvkm_vram(memory);
89 struct nvkm_mm_node *next = vram->mn;
90 struct nvkm_mm_node *node;
91 mutex_lock(&vram->ram->fb->subdev.mutex);
92 while ((node = next)) {
93 next = node->next;
94 nvkm_mm_free(&vram->ram->vram, &node);
95 }
96 mutex_unlock(&vram->ram->fb->subdev.mutex);
97 return vram;
98 }
99
100 static const struct nvkm_memory_func
101 nvkm_vram = {
102 .dtor = nvkm_vram_dtor,
103 .target = nvkm_vram_target,
104 .page = nvkm_vram_page,
105 .addr = nvkm_vram_addr,
106 .size = nvkm_vram_size,
107 .map = nvkm_vram_map,
108 };
109
110 int
nvkm_ram_get(struct nvkm_device * device,u8 heap,u8 type,u8 rpage,u64 size,bool contig,bool back,struct nvkm_memory ** pmemory)111 nvkm_ram_get(struct nvkm_device *device, u8 heap, u8 type, u8 rpage, u64 size,
112 bool contig, bool back, struct nvkm_memory **pmemory)
113 {
114 struct nvkm_ram *ram;
115 struct nvkm_mm *mm;
116 struct nvkm_mm_node **node, *r;
117 struct nvkm_vram *vram;
118 u8 page = max(rpage, (u8)NVKM_RAM_MM_SHIFT);
119 u32 align = (1 << page) >> NVKM_RAM_MM_SHIFT;
120 u32 max = ALIGN(size, 1 << page) >> NVKM_RAM_MM_SHIFT;
121 u32 min = contig ? max : align;
122 int ret;
123
124 if (!device->fb || !(ram = device->fb->ram))
125 return -ENODEV;
126 ram = device->fb->ram;
127 mm = &ram->vram;
128
129 if (!(vram = kzalloc(sizeof(*vram), GFP_KERNEL)))
130 return -ENOMEM;
131 nvkm_memory_ctor(&nvkm_vram, &vram->memory);
132 vram->ram = ram;
133 vram->page = page;
134 *pmemory = &vram->memory;
135
136 mutex_lock(&ram->fb->subdev.mutex);
137 node = &vram->mn;
138 do {
139 if (back)
140 ret = nvkm_mm_tail(mm, heap, type, max, min, align, &r);
141 else
142 ret = nvkm_mm_head(mm, heap, type, max, min, align, &r);
143 if (ret) {
144 mutex_unlock(&ram->fb->subdev.mutex);
145 nvkm_memory_unref(pmemory);
146 return ret;
147 }
148
149 *node = r;
150 node = &r->next;
151 max -= r->length;
152 } while (max);
153 mutex_unlock(&ram->fb->subdev.mutex);
154 return 0;
155 }
156
157 int
nvkm_ram_init(struct nvkm_ram * ram)158 nvkm_ram_init(struct nvkm_ram *ram)
159 {
160 if (ram->func->init)
161 return ram->func->init(ram);
162 return 0;
163 }
164
165 void
nvkm_ram_del(struct nvkm_ram ** pram)166 nvkm_ram_del(struct nvkm_ram **pram)
167 {
168 struct nvkm_ram *ram = *pram;
169 if (ram && !WARN_ON(!ram->func)) {
170 if (ram->func->dtor)
171 *pram = ram->func->dtor(ram);
172 nvkm_mm_fini(&ram->vram);
173 kfree(*pram);
174 *pram = NULL;
175 }
176 }
177
178 int
nvkm_ram_ctor(const struct nvkm_ram_func * func,struct nvkm_fb * fb,enum nvkm_ram_type type,u64 size,struct nvkm_ram * ram)179 nvkm_ram_ctor(const struct nvkm_ram_func *func, struct nvkm_fb *fb,
180 enum nvkm_ram_type type, u64 size, struct nvkm_ram *ram)
181 {
182 static const char *name[] = {
183 [NVKM_RAM_TYPE_UNKNOWN] = "of unknown memory type",
184 [NVKM_RAM_TYPE_STOLEN ] = "stolen system memory",
185 [NVKM_RAM_TYPE_SGRAM ] = "SGRAM",
186 [NVKM_RAM_TYPE_SDRAM ] = "SDRAM",
187 [NVKM_RAM_TYPE_DDR1 ] = "DDR1",
188 [NVKM_RAM_TYPE_DDR2 ] = "DDR2",
189 [NVKM_RAM_TYPE_DDR3 ] = "DDR3",
190 [NVKM_RAM_TYPE_GDDR2 ] = "GDDR2",
191 [NVKM_RAM_TYPE_GDDR3 ] = "GDDR3",
192 [NVKM_RAM_TYPE_GDDR4 ] = "GDDR4",
193 [NVKM_RAM_TYPE_GDDR5 ] = "GDDR5",
194 [NVKM_RAM_TYPE_GDDR5X ] = "GDDR5X",
195 [NVKM_RAM_TYPE_GDDR6 ] = "GDDR6",
196 [NVKM_RAM_TYPE_HBM2 ] = "HBM2",
197 };
198 struct nvkm_subdev *subdev = &fb->subdev;
199 int ret;
200
201 nvkm_info(subdev, "%d MiB %s\n", (int)(size >> 20), name[type]);
202 ram->func = func;
203 ram->fb = fb;
204 ram->type = type;
205 ram->size = size;
206
207 if (!nvkm_mm_initialised(&ram->vram)) {
208 ret = nvkm_mm_init(&ram->vram, NVKM_RAM_MM_NORMAL, 0,
209 size >> NVKM_RAM_MM_SHIFT, 1);
210 if (ret)
211 return ret;
212 }
213
214 return 0;
215 }
216
217 int
nvkm_ram_new_(const struct nvkm_ram_func * func,struct nvkm_fb * fb,enum nvkm_ram_type type,u64 size,struct nvkm_ram ** pram)218 nvkm_ram_new_(const struct nvkm_ram_func *func, struct nvkm_fb *fb,
219 enum nvkm_ram_type type, u64 size, struct nvkm_ram **pram)
220 {
221 if (!(*pram = kzalloc(sizeof(**pram), GFP_KERNEL)))
222 return -ENOMEM;
223 return nvkm_ram_ctor(func, fb, type, size, *pram);
224 }
225