821cd75b86a3c4f2e313bd7448929573ac81faa2
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / gpu / drm / nouveau / core / subdev / fb / base.c
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
25 #include "subdev/fb.h"
26 #include "subdev/bios.h"
27 #include "subdev/bios/bit.h"
28
29 int
30 nouveau_fb_bios_memtype(struct nouveau_bios *bios)
31 {
32         struct bit_entry M;
33         u8 ramcfg;
34
35         ramcfg = (nv_rd32(bios, 0x101000) & 0x0000003c) >> 2;
36         if (!bit_entry(bios, 'M', &M) && M.version == 2 && M.length >= 5) {
37                 u16 table   = nv_ro16(bios, M.offset + 3);
38                 u8  version = nv_ro08(bios, table + 0);
39                 u8  header  = nv_ro08(bios, table + 1);
40                 u8  record  = nv_ro08(bios, table + 2);
41                 u8  entries = nv_ro08(bios, table + 3);
42                 if (table && version == 0x10 && ramcfg < entries) {
43                         u16 entry = table + header + (ramcfg * record);
44                         switch (nv_ro08(bios, entry) & 0x0f) {
45                         case 0: return NV_MEM_TYPE_DDR2;
46                         case 1: return NV_MEM_TYPE_DDR3;
47                         case 2: return NV_MEM_TYPE_GDDR3;
48                         case 3: return NV_MEM_TYPE_GDDR5;
49                         default:
50                                 break;
51                         }
52
53                 }
54         }
55
56         return NV_MEM_TYPE_UNKNOWN;
57 }
58
59 int
60 _nouveau_fb_fini(struct nouveau_object *object, bool suspend)
61 {
62         struct nouveau_fb *pfb = (void *)object;
63         int ret;
64
65         ret = nv_ofuncs(pfb->ram)->fini(nv_object(pfb->ram), suspend);
66         if (ret && suspend)
67                 return ret;
68
69         return nouveau_subdev_fini(&pfb->base, suspend);
70 }
71
72 int
73 _nouveau_fb_init(struct nouveau_object *object)
74 {
75         struct nouveau_fb *pfb = (void *)object;
76         int ret, i;
77
78         ret = nouveau_subdev_init(&pfb->base);
79         if (ret)
80                 return ret;
81
82         ret = nv_ofuncs(pfb->ram)->init(nv_object(pfb->ram));
83         if (ret)
84                 return ret;
85
86         for (i = 0; i < pfb->tile.regions; i++)
87                 pfb->tile.prog(pfb, i, &pfb->tile.region[i]);
88
89         return 0;
90 }
91
92 void
93 _nouveau_fb_dtor(struct nouveau_object *object)
94 {
95         struct nouveau_fb *pfb = (void *)object;
96         int i;
97
98         for (i = 0; i < pfb->tile.regions; i++)
99                 pfb->tile.fini(pfb, i, &pfb->tile.region[i]);
100         nouveau_mm_fini(&pfb->tags);
101         nouveau_mm_fini(&pfb->vram);
102
103         nouveau_object_ref(NULL, (struct nouveau_object **)&pfb->ram);
104         nouveau_subdev_destroy(&pfb->base);
105 }
106
107 int
108 nouveau_fb_create_(struct nouveau_object *parent, struct nouveau_object *engine,
109                    struct nouveau_oclass *oclass, struct nouveau_oclass *ramcls,
110                    int length, void **pobject)
111 {
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 nouveau_object *ram;
126         struct nouveau_fb *pfb;
127         int ret;
128
129         ret = nouveau_subdev_create_(parent, engine, oclass, 0, "PFB", "fb",
130                                      length, pobject);
131         pfb = *pobject;
132         if (ret)
133                 return ret;
134
135         ret = nouveau_object_ctor(nv_object(pfb), nv_object(pfb),
136                                   ramcls, NULL, 0, &ram);
137         if (ret) {
138                 nv_fatal(pfb, "error detecting memory configuration!!\n");
139                 return ret;
140         }
141
142         atomic_dec(&ram->parent->refcount);
143         atomic_dec(&ram->engine->refcount);
144         pfb->ram = (void *)ram;
145
146         if (!nouveau_mm_initialised(&pfb->vram)) {
147                 ret = nouveau_mm_init(&pfb->vram, 0, pfb->ram->size >> 12, 1);
148                 if (ret)
149                         return ret;
150         }
151
152         if (!nouveau_mm_initialised(&pfb->tags)) {
153                 ret = nouveau_mm_init(&pfb->tags, 0, pfb->ram->tags ?
154                                      ++pfb->ram->tags : 0, 1);
155                 if (ret)
156                         return ret;
157         }
158
159         nv_info(pfb, "RAM type: %s\n", name[pfb->ram->type]);
160         nv_info(pfb, "RAM size: %d MiB\n", (int)(pfb->ram->size >> 20));
161         nv_info(pfb, "   ZCOMP: %d tags\n", pfb->ram->tags);
162         return 0;
163 }