1 /*
2  * Copyright 2012 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
23  */
24 #include "priv.h"
25 
26 #include <subdev/bios.h>
27 #include <subdev/bios/M0203.h>
28 
29 int
30 nvkm_fb_bios_memtype(struct nvkm_bios *bios)
31 {
32 	struct nvkm_device *device = bios->subdev.device;
33 	const u8 ramcfg = (nvkm_rd32(device, 0x101000) & 0x0000003c) >> 2;
34 	struct nvbios_M0203E M0203E;
35 	u8 ver, hdr;
36 
37 	if (nvbios_M0203Em(bios, ramcfg, &ver, &hdr, &M0203E)) {
38 		switch (M0203E.type) {
39 		case M0203E_TYPE_DDR2 : return NV_MEM_TYPE_DDR2;
40 		case M0203E_TYPE_DDR3 : return NV_MEM_TYPE_DDR3;
41 		case M0203E_TYPE_GDDR3: return NV_MEM_TYPE_GDDR3;
42 		case M0203E_TYPE_GDDR5: return NV_MEM_TYPE_GDDR5;
43 		default:
44 			nv_warn(bios, "M0203E type %02x\n", M0203E.type);
45 			return NV_MEM_TYPE_UNKNOWN;
46 		}
47 	}
48 
49 	nv_warn(bios, "M0203E not matched!\n");
50 	return NV_MEM_TYPE_UNKNOWN;
51 }
52 
53 int
54 _nvkm_fb_fini(struct nvkm_object *object, bool suspend)
55 {
56 	struct nvkm_fb *fb = (void *)object;
57 	int ret;
58 
59 	if (fb->ram) {
60 		ret = nv_ofuncs(fb->ram)->fini(nv_object(fb->ram), suspend);
61 		if (ret && suspend)
62 			return ret;
63 	}
64 
65 	return nvkm_subdev_fini(&fb->subdev, suspend);
66 }
67 
68 int
69 _nvkm_fb_init(struct nvkm_object *object)
70 {
71 	struct nvkm_fb *fb = (void *)object;
72 	int ret, i;
73 
74 	ret = nvkm_subdev_init(&fb->subdev);
75 	if (ret)
76 		return ret;
77 
78 	if (fb->ram) {
79 		ret = nv_ofuncs(fb->ram)->init(nv_object(fb->ram));
80 		if (ret)
81 			return ret;
82 	}
83 
84 	for (i = 0; i < fb->tile.regions; i++)
85 		fb->tile.prog(fb, i, &fb->tile.region[i]);
86 
87 	return 0;
88 }
89 
90 void
91 _nvkm_fb_dtor(struct nvkm_object *object)
92 {
93 	struct nvkm_fb *fb = (void *)object;
94 	int i;
95 
96 	for (i = 0; i < fb->tile.regions; i++)
97 		fb->tile.fini(fb, i, &fb->tile.region[i]);
98 	nvkm_mm_fini(&fb->tags);
99 
100 	if (fb->ram) {
101 		nvkm_mm_fini(&fb->vram);
102 		nvkm_object_ref(NULL, (struct nvkm_object **)&fb->ram);
103 	}
104 
105 	nvkm_subdev_destroy(&fb->subdev);
106 }
107 
108 int
109 nvkm_fb_create_(struct nvkm_object *parent, struct nvkm_object *engine,
110 		struct nvkm_oclass *oclass, int length, void **pobject)
111 {
112 	struct nvkm_fb_impl *impl = (void *)oclass;
113 	static const char *name[] = {
114 		[NV_MEM_TYPE_UNKNOWN] = "unknown",
115 		[NV_MEM_TYPE_STOLEN ] = "stolen system memory",
116 		[NV_MEM_TYPE_SGRAM  ] = "SGRAM",
117 		[NV_MEM_TYPE_SDRAM  ] = "SDRAM",
118 		[NV_MEM_TYPE_DDR1   ] = "DDR1",
119 		[NV_MEM_TYPE_DDR2   ] = "DDR2",
120 		[NV_MEM_TYPE_DDR3   ] = "DDR3",
121 		[NV_MEM_TYPE_GDDR2  ] = "GDDR2",
122 		[NV_MEM_TYPE_GDDR3  ] = "GDDR3",
123 		[NV_MEM_TYPE_GDDR4  ] = "GDDR4",
124 		[NV_MEM_TYPE_GDDR5  ] = "GDDR5",
125 	};
126 	struct nvkm_object *ram;
127 	struct nvkm_fb *fb;
128 	int ret;
129 
130 	ret = nvkm_subdev_create_(parent, engine, oclass, 0, "PFB", "fb",
131 				  length, pobject);
132 	fb = *pobject;
133 	if (ret)
134 		return ret;
135 
136 	fb->memtype_valid = impl->memtype;
137 
138 	if (!impl->ram)
139 		return 0;
140 
141 	ret = nvkm_object_ctor(nv_object(fb), NULL, impl->ram, NULL, 0, &ram);
142 	if (ret) {
143 		nv_fatal(fb, "error detecting memory configuration!!\n");
144 		return ret;
145 	}
146 
147 	fb->ram = (void *)ram;
148 
149 	if (!nvkm_mm_initialised(&fb->vram)) {
150 		ret = nvkm_mm_init(&fb->vram, 0, fb->ram->size >> 12, 1);
151 		if (ret)
152 			return ret;
153 	}
154 
155 	if (!nvkm_mm_initialised(&fb->tags)) {
156 		ret = nvkm_mm_init(&fb->tags, 0, fb->ram->tags ?
157 				   ++fb->ram->tags : 0, 1);
158 		if (ret)
159 			return ret;
160 	}
161 
162 	nv_info(fb, "RAM type: %s\n", name[fb->ram->type]);
163 	nv_info(fb, "RAM size: %d MiB\n", (int)(fb->ram->size >> 20));
164 	nv_info(fb, "   ZCOMP: %d tags\n", fb->ram->tags);
165 	return 0;
166 }
167