1/* $NetBSD: nouveau_subdev_fb_base.c,v 1.1.1.1 2014/08/06 12:36:30 riastradh Exp $ */
2
3/*
4 * Copyright 2012 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
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_fb_base.c,v 1.1.1.1 2014/08/06 12:36:30 riastradh Exp $");
29
30#include <subdev/bios.h>
31#include <subdev/bios/bit.h>
32
33#include "priv.h"
34
35int
36nouveau_fb_bios_memtype(struct nouveau_bios *bios)
37{
38 struct bit_entry M;
39 u8 ramcfg;
40
41 ramcfg = (nv_rd32(bios, 0x101000) & 0x0000003c) >> 2;
42 if (!bit_entry(bios, 'M', &M) && M.version == 2 && M.length >= 5) {
43 u16 table = nv_ro16(bios, M.offset + 3);
44 u8 version = nv_ro08(bios, table + 0);
45 u8 header = nv_ro08(bios, table + 1);
46 u8 record = nv_ro08(bios, table + 2);
47 u8 entries = nv_ro08(bios, table + 3);
48 if (table && version == 0x10 && ramcfg < entries) {
49 u16 entry = table + header + (ramcfg * record);
50 switch (nv_ro08(bios, entry) & 0x0f) {
51 case 0: return NV_MEM_TYPE_DDR2;
52 case 1: return NV_MEM_TYPE_DDR3;
53 case 2: return NV_MEM_TYPE_GDDR3;
54 case 3: return NV_MEM_TYPE_GDDR5;
55 default:
56 break;
57 }
58
59 }
60 }
61
62 return NV_MEM_TYPE_UNKNOWN;
63}
64
65int
66_nouveau_fb_fini(struct nouveau_object *object, bool suspend)
67{
68 struct nouveau_fb *pfb = (void *)object;
69 int ret;
70
71 ret = nv_ofuncs(pfb->ram)->fini(nv_object(pfb->ram), suspend);
72 if (ret && suspend)
73 return ret;
74
75 return nouveau_subdev_fini(&pfb->base, suspend);
76}
77
78int
79_nouveau_fb_init(struct nouveau_object *object)
80{
81 struct nouveau_fb *pfb = (void *)object;
82 int ret, i;
83
84 ret = nouveau_subdev_init(&pfb->base);
85 if (ret)
86 return ret;
87
88 ret = nv_ofuncs(pfb->ram)->init(nv_object(pfb->ram));
89 if (ret)
90 return ret;
91
92 for (i = 0; i < pfb->tile.regions; i++)
93 pfb->tile.prog(pfb, i, &pfb->tile.region[i]);
94
95 return 0;
96}
97
98void
99_nouveau_fb_dtor(struct nouveau_object *object)
100{
101 struct nouveau_fb *pfb = (void *)object;
102 int i;
103
104 for (i = 0; i < pfb->tile.regions; i++)
105 pfb->tile.fini(pfb, i, &pfb->tile.region[i]);
106 nouveau_mm_fini(&pfb->tags);
107 nouveau_mm_fini(&pfb->vram);
108
109 nouveau_object_ref(NULL, (struct nouveau_object **)&pfb->ram);
110 nouveau_subdev_destroy(&pfb->base);
111}
112
113int
114nouveau_fb_create_(struct nouveau_object *parent, struct nouveau_object *engine,
115 struct nouveau_oclass *oclass, int length, void **pobject)
116{
117 struct nouveau_fb_impl *impl = (void *)oclass;
118 static const char *name[] = {
119 [NV_MEM_TYPE_UNKNOWN] = "unknown",
120 [NV_MEM_TYPE_STOLEN ] = "stolen system memory",
121 [NV_MEM_TYPE_SGRAM ] = "SGRAM",
122 [NV_MEM_TYPE_SDRAM ] = "SDRAM",
123 [NV_MEM_TYPE_DDR1 ] = "DDR1",
124 [NV_MEM_TYPE_DDR2 ] = "DDR2",
125 [NV_MEM_TYPE_DDR3 ] = "DDR3",
126 [NV_MEM_TYPE_GDDR2 ] = "GDDR2",
127 [NV_MEM_TYPE_GDDR3 ] = "GDDR3",
128 [NV_MEM_TYPE_GDDR4 ] = "GDDR4",
129 [NV_MEM_TYPE_GDDR5 ] = "GDDR5",
130 };
131 struct nouveau_object *ram;
132 struct nouveau_fb *pfb;
133 int ret;
134
135 ret = nouveau_subdev_create_(parent, engine, oclass, 0, "PFB", "fb",
136 length, pobject);
137 pfb = *pobject;
138 if (ret)
139 return ret;
140
141 pfb->memtype_valid = impl->memtype;
142
143 ret = nouveau_object_ctor(nv_object(pfb), nv_object(pfb),
144 impl->ram, NULL, 0, &ram);
145 if (ret) {
146 nv_fatal(pfb, "error detecting memory configuration!!\n");
147 return ret;
148 }
149
150 atomic_dec(&ram->parent->refcount);
151 atomic_dec(&ram->engine->refcount);
152 pfb->ram = (void *)ram;
153
154 if (!nouveau_mm_initialised(&pfb->vram)) {
155 ret = nouveau_mm_init(&pfb->vram, 0, pfb->ram->size >> 12, 1);
156 if (ret)
157 return ret;
158 }
159
160 if (!nouveau_mm_initialised(&pfb->tags)) {
161 ret = nouveau_mm_init(&pfb->tags, 0, pfb->ram->tags ?
162 ++pfb->ram->tags : 0, 1);
163 if (ret)
164 return ret;
165 }
166
167 nv_info(pfb, "RAM type: %s\n", name[pfb->ram->type]);
168 nv_info(pfb, "RAM size: %d MiB\n", (int)(pfb->ram->size >> 20));
169 nv_info(pfb, " ZCOMP: %d tags\n", pfb->ram->tags);
170 return 0;
171}
172