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