1 /*
2  * Copyright 2015 Red Hat Inc.
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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs <bskeggs@redhat.com>
23  */
24 #include "ram.h"
25 
26 int
27 nvkm_ram_init(struct nvkm_ram *ram)
28 {
29 	if (ram->func->init)
30 		return ram->func->init(ram);
31 	return 0;
32 }
33 
34 void
35 nvkm_ram_del(struct nvkm_ram **pram)
36 {
37 	struct nvkm_ram *ram = *pram;
38 	if (ram && !WARN_ON(!ram->func)) {
39 		if (ram->func->dtor)
40 			*pram = ram->func->dtor(ram);
41 		nvkm_mm_fini(&ram->tags);
42 		nvkm_mm_fini(&ram->vram);
43 		kfree(*pram);
44 		*pram = NULL;
45 	}
46 }
47 
48 int
49 nvkm_ram_ctor(const struct nvkm_ram_func *func, struct nvkm_fb *fb,
50 	      enum nvkm_ram_type type, u64 size, u32 tags,
51 	      struct nvkm_ram *ram)
52 {
53 	static const char *name[] = {
54 		[NVKM_RAM_TYPE_UNKNOWN] = "of unknown memory type",
55 		[NVKM_RAM_TYPE_STOLEN ] = "stolen system memory",
56 		[NVKM_RAM_TYPE_SGRAM  ] = "SGRAM",
57 		[NVKM_RAM_TYPE_SDRAM  ] = "SDRAM",
58 		[NVKM_RAM_TYPE_DDR1   ] = "DDR1",
59 		[NVKM_RAM_TYPE_DDR2   ] = "DDR2",
60 		[NVKM_RAM_TYPE_DDR3   ] = "DDR3",
61 		[NVKM_RAM_TYPE_GDDR2  ] = "GDDR2",
62 		[NVKM_RAM_TYPE_GDDR3  ] = "GDDR3",
63 		[NVKM_RAM_TYPE_GDDR4  ] = "GDDR4",
64 		[NVKM_RAM_TYPE_GDDR5  ] = "GDDR5",
65 	};
66 	struct nvkm_subdev *subdev = &fb->subdev;
67 	int ret;
68 
69 	nvkm_info(subdev, "%d MiB %s\n", (int)(size >> 20), name[type]);
70 	ram->func = func;
71 	ram->fb = fb;
72 	ram->type = type;
73 	ram->size = size;
74 
75 	if (!nvkm_mm_initialised(&ram->vram)) {
76 		ret = nvkm_mm_init(&ram->vram, 0, size >> NVKM_RAM_MM_SHIFT, 1);
77 		if (ret)
78 			return ret;
79 	}
80 
81 	if (!nvkm_mm_initialised(&ram->tags)) {
82 		ret = nvkm_mm_init(&ram->tags, 0, tags ? ++tags : 0, 1);
83 		if (ret)
84 			return ret;
85 
86 		nvkm_debug(subdev, "%d compression tags\n", tags);
87 	}
88 
89 	return 0;
90 }
91 
92 int
93 nvkm_ram_new_(const struct nvkm_ram_func *func, struct nvkm_fb *fb,
94 	      enum nvkm_ram_type type, u64 size, u32 tags,
95 	      struct nvkm_ram **pram)
96 {
97 	if (!(*pram = kzalloc(sizeof(**pram), GFP_KERNEL)))
98 		return -ENOMEM;
99 	return nvkm_ram_ctor(func, fb, type, size, tags, *pram);
100 }
101