Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[linux-drm-fsl-dcu.git] / drivers / gpu / drm / nouveau / core / subdev / fb / ramnvc0.c
1 /*
2  * Copyright 2013 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/bios.h>
26 #include <subdev/bios/bit.h>
27 #include <subdev/bios/pll.h>
28 #include <subdev/bios/rammap.h>
29 #include <subdev/bios/timing.h>
30 #include <subdev/ltcg.h>
31
32 #include <subdev/clock.h>
33 #include <subdev/clock/pll.h>
34
35 #include <core/option.h>
36
37 #include "ramfuc.h"
38
39 #include "nvc0.h"
40
41 struct nvc0_ramfuc {
42         struct ramfuc base;
43
44         struct ramfuc_reg r_0x10fe20;
45         struct ramfuc_reg r_0x10fe24;
46         struct ramfuc_reg r_0x137320;
47         struct ramfuc_reg r_0x137330;
48
49         struct ramfuc_reg r_0x132000;
50         struct ramfuc_reg r_0x132004;
51         struct ramfuc_reg r_0x132100;
52
53         struct ramfuc_reg r_0x137390;
54
55         struct ramfuc_reg r_0x10f290;
56         struct ramfuc_reg r_0x10f294;
57         struct ramfuc_reg r_0x10f298;
58         struct ramfuc_reg r_0x10f29c;
59         struct ramfuc_reg r_0x10f2a0;
60
61         struct ramfuc_reg r_0x10f300;
62         struct ramfuc_reg r_0x10f338;
63         struct ramfuc_reg r_0x10f340;
64         struct ramfuc_reg r_0x10f344;
65         struct ramfuc_reg r_0x10f348;
66
67         struct ramfuc_reg r_0x10f910;
68         struct ramfuc_reg r_0x10f914;
69
70         struct ramfuc_reg r_0x100b0c;
71         struct ramfuc_reg r_0x10f050;
72         struct ramfuc_reg r_0x10f090;
73         struct ramfuc_reg r_0x10f200;
74         struct ramfuc_reg r_0x10f210;
75         struct ramfuc_reg r_0x10f310;
76         struct ramfuc_reg r_0x10f314;
77         struct ramfuc_reg r_0x10f610;
78         struct ramfuc_reg r_0x10f614;
79         struct ramfuc_reg r_0x10f800;
80         struct ramfuc_reg r_0x10f808;
81         struct ramfuc_reg r_0x10f824;
82         struct ramfuc_reg r_0x10f830;
83         struct ramfuc_reg r_0x10f988;
84         struct ramfuc_reg r_0x10f98c;
85         struct ramfuc_reg r_0x10f990;
86         struct ramfuc_reg r_0x10f998;
87         struct ramfuc_reg r_0x10f9b0;
88         struct ramfuc_reg r_0x10f9b4;
89         struct ramfuc_reg r_0x10fb04;
90         struct ramfuc_reg r_0x10fb08;
91         struct ramfuc_reg r_0x137300;
92         struct ramfuc_reg r_0x137310;
93         struct ramfuc_reg r_0x137360;
94         struct ramfuc_reg r_0x1373ec;
95         struct ramfuc_reg r_0x1373f0;
96         struct ramfuc_reg r_0x1373f8;
97
98         struct ramfuc_reg r_0x61c140;
99         struct ramfuc_reg r_0x611200;
100
101         struct ramfuc_reg r_0x13d8f4;
102 };
103
104 struct nvc0_ram {
105         struct nouveau_ram base;
106         struct nvc0_ramfuc fuc;
107         struct nvbios_pll refpll;
108         struct nvbios_pll mempll;
109 };
110
111 static void
112 nvc0_ram_train(struct nvc0_ramfuc *fuc, u32 magic)
113 {
114         struct nvc0_ram *ram = container_of(fuc, typeof(*ram), fuc);
115         struct nouveau_fb *pfb = nouveau_fb(ram);
116         u32 part = nv_rd32(pfb, 0x022438), i;
117         u32 mask = nv_rd32(pfb, 0x022554);
118         u32 addr = 0x110974;
119
120         ram_wr32(fuc, 0x10f910, magic);
121         ram_wr32(fuc, 0x10f914, magic);
122
123         for (i = 0; (magic & 0x80000000) && i < part; addr += 0x1000, i++) {
124                 if (mask & (1 << i))
125                         continue;
126                 ram_wait(fuc, addr, 0x0000000f, 0x00000000, 500000);
127         }
128 }
129
130 static int
131 nvc0_ram_calc(struct nouveau_fb *pfb, u32 freq)
132 {
133         struct nouveau_clock *clk = nouveau_clock(pfb);
134         struct nouveau_bios *bios = nouveau_bios(pfb);
135         struct nvc0_ram *ram = (void *)pfb->ram;
136         struct nvc0_ramfuc *fuc = &ram->fuc;
137         struct bit_entry M;
138         u8  ver, cnt, strap;
139         u32 data;
140         struct {
141                 u32 data;
142                 u8  size;
143         } rammap, ramcfg, timing;
144         int ref, div, out;
145         int from, mode;
146         int N1, M1, P;
147         int ret;
148
149         /* lookup memory config data relevant to the target frequency */
150         rammap.data = nvbios_rammap_match(bios, freq / 1000, &ver, &rammap.size,
151                                          &cnt, &ramcfg.size);
152         if (!rammap.data || ver != 0x10 || rammap.size < 0x0e) {
153                 nv_error(pfb, "invalid/missing rammap entry\n");
154                 return -EINVAL;
155         }
156
157         /* locate specific data set for the attached memory */
158         if (bit_entry(bios, 'M', &M) || M.version != 2 || M.length < 3) {
159                 nv_error(pfb, "invalid/missing memory table\n");
160                 return -EINVAL;
161         }
162
163         strap = (nv_rd32(pfb, 0x101000) & 0x0000003c) >> 2;
164         data = nv_ro16(bios, M.offset + 1);
165         if (data)
166                 strap = nv_ro08(bios, data + strap);
167
168         if (strap >= cnt) {
169                 nv_error(pfb, "invalid ramcfg strap\n");
170                 return -EINVAL;
171         }
172
173         ramcfg.data = rammap.data + rammap.size + (strap * ramcfg.size);
174         if (!ramcfg.data || ver != 0x10 || ramcfg.size < 0x0e) {
175                 nv_error(pfb, "invalid/missing ramcfg entry\n");
176                 return -EINVAL;
177         }
178
179         /* lookup memory timings, if bios says they're present */
180         strap = nv_ro08(bios, ramcfg.data + 0x01);
181         if (strap != 0xff) {
182                 timing.data = nvbios_timing_entry(bios, strap, &ver,
183                                                  &timing.size);
184                 if (!timing.data || ver != 0x10 || timing.size < 0x19) {
185                         nv_error(pfb, "invalid/missing timing entry\n");
186                         return -EINVAL;
187                 }
188         } else {
189                 timing.data = 0;
190         }
191
192         ret = ram_init(fuc, pfb);
193         if (ret)
194                 return ret;
195
196         /* determine current mclk configuration */
197         from = !!(ram_rd32(fuc, 0x1373f0) & 0x00000002); /*XXX: ok? */
198
199         /* determine target mclk configuration */
200         if (!(ram_rd32(fuc, 0x137300) & 0x00000100))
201                 ref = clk->read(clk, nv_clk_src_sppll0);
202         else
203                 ref = clk->read(clk, nv_clk_src_sppll1);
204         div = max(min((ref * 2) / freq, (u32)65), (u32)2) - 2;
205         out = (ref * 2) / (div + 2);
206         mode = freq != out;
207
208         ram_mask(fuc, 0x137360, 0x00000002, 0x00000000);
209
210         if ((ram_rd32(fuc, 0x132000) & 0x00000002) || 0 /*XXX*/) {
211                 ram_nuke(fuc, 0x132000);
212                 ram_mask(fuc, 0x132000, 0x00000002, 0x00000002);
213                 ram_mask(fuc, 0x132000, 0x00000002, 0x00000000);
214         }
215
216         if (mode == 1) {
217                 ram_nuke(fuc, 0x10fe20);
218                 ram_mask(fuc, 0x10fe20, 0x00000002, 0x00000002);
219                 ram_mask(fuc, 0x10fe20, 0x00000002, 0x00000000);
220         }
221
222 // 0x00020034 // 0x0000000a
223         ram_wr32(fuc, 0x132100, 0x00000001);
224
225         if (mode == 1 && from == 0) {
226                 /* calculate refpll */
227                 ret = nva3_pll_calc(nv_subdev(pfb), &ram->refpll,
228                                     ram->mempll.refclk, &N1, NULL, &M1, &P);
229                 if (ret <= 0) {
230                         nv_error(pfb, "unable to calc refpll\n");
231                         return ret ? ret : -ERANGE;
232                 }
233
234                 ram_wr32(fuc, 0x10fe20, 0x20010000);
235                 ram_wr32(fuc, 0x137320, 0x00000003);
236                 ram_wr32(fuc, 0x137330, 0x81200006);
237                 ram_wr32(fuc, 0x10fe24, (P << 16) | (N1 << 8) | M1);
238                 ram_wr32(fuc, 0x10fe20, 0x20010001);
239                 ram_wait(fuc, 0x137390, 0x00020000, 0x00020000, 64000);
240
241                 /* calculate mempll */
242                 ret = nva3_pll_calc(nv_subdev(pfb), &ram->mempll, freq,
243                                    &N1, NULL, &M1, &P);
244                 if (ret <= 0) {
245                         nv_error(pfb, "unable to calc refpll\n");
246                         return ret ? ret : -ERANGE;
247                 }
248
249                 ram_wr32(fuc, 0x10fe20, 0x20010005);
250                 ram_wr32(fuc, 0x132004, (P << 16) | (N1 << 8) | M1);
251                 ram_wr32(fuc, 0x132000, 0x18010101);
252                 ram_wait(fuc, 0x137390, 0x00000002, 0x00000002, 64000);
253         } else
254         if (mode == 0) {
255                 ram_wr32(fuc, 0x137300, 0x00000003);
256         }
257
258         if (from == 0) {
259                 ram_nuke(fuc, 0x10fb04);
260                 ram_mask(fuc, 0x10fb04, 0x0000ffff, 0x00000000);
261                 ram_nuke(fuc, 0x10fb08);
262                 ram_mask(fuc, 0x10fb08, 0x0000ffff, 0x00000000);
263                 ram_wr32(fuc, 0x10f988, 0x2004ff00);
264                 ram_wr32(fuc, 0x10f98c, 0x003fc040);
265                 ram_wr32(fuc, 0x10f990, 0x20012001);
266                 ram_wr32(fuc, 0x10f998, 0x00011a00);
267                 ram_wr32(fuc, 0x13d8f4, 0x00000000);
268         } else {
269                 ram_wr32(fuc, 0x10f988, 0x20010000);
270                 ram_wr32(fuc, 0x10f98c, 0x00000000);
271                 ram_wr32(fuc, 0x10f990, 0x20012001);
272                 ram_wr32(fuc, 0x10f998, 0x00010a00);
273         }
274
275         if (from == 0) {
276 // 0x00020039 // 0x000000ba
277         }
278
279 // 0x0002003a // 0x00000002
280         ram_wr32(fuc, 0x100b0c, 0x00080012);
281 // 0x00030014 // 0x00000000 // 0x02b5f070
282 // 0x00030014 // 0x00010000 // 0x02b5f070
283         ram_wr32(fuc, 0x611200, 0x00003300);
284 // 0x00020034 // 0x0000000a
285 // 0x00030020 // 0x00000001 // 0x00000000
286
287         ram_mask(fuc, 0x10f200, 0x00000800, 0x00000000);
288         ram_wr32(fuc, 0x10f210, 0x00000000);
289         ram_nsec(fuc, 1000);
290         if (mode == 0)
291                 nvc0_ram_train(fuc, 0x000c1001);
292         ram_wr32(fuc, 0x10f310, 0x00000001);
293         ram_nsec(fuc, 1000);
294         ram_wr32(fuc, 0x10f090, 0x00000061);
295         ram_wr32(fuc, 0x10f090, 0xc000007f);
296         ram_nsec(fuc, 1000);
297
298         if (from == 0) {
299                 ram_wr32(fuc, 0x10f824, 0x00007fd4);
300         } else {
301                 ram_wr32(fuc, 0x1373ec, 0x00020404);
302         }
303
304         if (mode == 0) {
305                 ram_mask(fuc, 0x10f808, 0x00080000, 0x00000000);
306                 ram_mask(fuc, 0x10f200, 0x00008000, 0x00008000);
307                 ram_wr32(fuc, 0x10f830, 0x41500010);
308                 ram_mask(fuc, 0x10f830, 0x01000000, 0x00000000);
309                 ram_mask(fuc, 0x132100, 0x00000100, 0x00000100);
310                 ram_wr32(fuc, 0x10f050, 0xff000090);
311                 ram_wr32(fuc, 0x1373ec, 0x00020f0f);
312                 ram_wr32(fuc, 0x1373f0, 0x00000003);
313                 ram_wr32(fuc, 0x137310, 0x81201616);
314                 ram_wr32(fuc, 0x132100, 0x00000001);
315 // 0x00020039 // 0x000000ba
316                 ram_wr32(fuc, 0x10f830, 0x00300017);
317                 ram_wr32(fuc, 0x1373f0, 0x00000001);
318                 ram_wr32(fuc, 0x10f824, 0x00007e77);
319                 ram_wr32(fuc, 0x132000, 0x18030001);
320                 ram_wr32(fuc, 0x10f090, 0x4000007e);
321                 ram_nsec(fuc, 2000);
322                 ram_wr32(fuc, 0x10f314, 0x00000001);
323                 ram_wr32(fuc, 0x10f210, 0x80000000);
324                 ram_wr32(fuc, 0x10f338, 0x00300220);
325                 ram_wr32(fuc, 0x10f300, 0x0000011d);
326                 ram_nsec(fuc, 1000);
327                 ram_wr32(fuc, 0x10f290, 0x02060505);
328                 ram_wr32(fuc, 0x10f294, 0x34208288);
329                 ram_wr32(fuc, 0x10f298, 0x44050411);
330                 ram_wr32(fuc, 0x10f29c, 0x0000114c);
331                 ram_wr32(fuc, 0x10f2a0, 0x42e10069);
332                 ram_wr32(fuc, 0x10f614, 0x40044f77);
333                 ram_wr32(fuc, 0x10f610, 0x40044f77);
334                 ram_wr32(fuc, 0x10f344, 0x00600009);
335                 ram_nsec(fuc, 1000);
336                 ram_wr32(fuc, 0x10f348, 0x00700008);
337                 ram_wr32(fuc, 0x61c140, 0x19240000);
338                 ram_wr32(fuc, 0x10f830, 0x00300017);
339                 nvc0_ram_train(fuc, 0x80021001);
340                 nvc0_ram_train(fuc, 0x80081001);
341                 ram_wr32(fuc, 0x10f340, 0x00500004);
342                 ram_nsec(fuc, 1000);
343                 ram_wr32(fuc, 0x10f830, 0x01300017);
344                 ram_wr32(fuc, 0x10f830, 0x00300017);
345 // 0x00030020 // 0x00000000 // 0x00000000
346 // 0x00020034 // 0x0000000b
347                 ram_wr32(fuc, 0x100b0c, 0x00080028);
348                 ram_wr32(fuc, 0x611200, 0x00003330);
349         } else {
350                 ram_wr32(fuc, 0x10f800, 0x00001800);
351                 ram_wr32(fuc, 0x13d8f4, 0x00000000);
352                 ram_wr32(fuc, 0x1373ec, 0x00020404);
353                 ram_wr32(fuc, 0x1373f0, 0x00000003);
354                 ram_wr32(fuc, 0x10f830, 0x40700010);
355                 ram_wr32(fuc, 0x10f830, 0x40500010);
356                 ram_wr32(fuc, 0x13d8f4, 0x00000000);
357                 ram_wr32(fuc, 0x1373f8, 0x00000000);
358                 ram_wr32(fuc, 0x132100, 0x00000101);
359                 ram_wr32(fuc, 0x137310, 0x89201616);
360                 ram_wr32(fuc, 0x10f050, 0xff000090);
361                 ram_wr32(fuc, 0x1373ec, 0x00030404);
362                 ram_wr32(fuc, 0x1373f0, 0x00000002);
363         // 0x00020039 // 0x00000011
364                 ram_wr32(fuc, 0x132100, 0x00000001);
365                 ram_wr32(fuc, 0x1373f8, 0x00002000);
366                 ram_nsec(fuc, 2000);
367                 ram_wr32(fuc, 0x10f808, 0x7aaa0050);
368                 ram_wr32(fuc, 0x10f830, 0x00500010);
369                 ram_wr32(fuc, 0x10f200, 0x00ce1000);
370                 ram_wr32(fuc, 0x10f090, 0x4000007e);
371                 ram_nsec(fuc, 2000);
372                 ram_wr32(fuc, 0x10f314, 0x00000001);
373                 ram_wr32(fuc, 0x10f210, 0x80000000);
374                 ram_wr32(fuc, 0x10f338, 0x00300200);
375                 ram_wr32(fuc, 0x10f300, 0x0000084d);
376                 ram_nsec(fuc, 1000);
377                 ram_wr32(fuc, 0x10f290, 0x0b343825);
378                 ram_wr32(fuc, 0x10f294, 0x3483028e);
379                 ram_wr32(fuc, 0x10f298, 0x440c0600);
380                 ram_wr32(fuc, 0x10f29c, 0x0000214c);
381                 ram_wr32(fuc, 0x10f2a0, 0x42e20069);
382                 ram_wr32(fuc, 0x10f200, 0x00ce0000);
383                 ram_wr32(fuc, 0x10f614, 0x60044e77);
384                 ram_wr32(fuc, 0x10f610, 0x60044e77);
385                 ram_wr32(fuc, 0x10f340, 0x00500000);
386                 ram_nsec(fuc, 1000);
387                 ram_wr32(fuc, 0x10f344, 0x00600228);
388                 ram_nsec(fuc, 1000);
389                 ram_wr32(fuc, 0x10f348, 0x00700000);
390                 ram_wr32(fuc, 0x13d8f4, 0x00000000);
391                 ram_wr32(fuc, 0x61c140, 0x09a40000);
392
393                 nvc0_ram_train(fuc, 0x800e1008);
394
395                 ram_nsec(fuc, 1000);
396                 ram_wr32(fuc, 0x10f800, 0x00001804);
397         // 0x00030020 // 0x00000000 // 0x00000000
398         // 0x00020034 // 0x0000000b
399                 ram_wr32(fuc, 0x13d8f4, 0x00000000);
400                 ram_wr32(fuc, 0x100b0c, 0x00080028);
401                 ram_wr32(fuc, 0x611200, 0x00003330);
402                 ram_nsec(fuc, 100000);
403                 ram_wr32(fuc, 0x10f9b0, 0x05313f41);
404                 ram_wr32(fuc, 0x10f9b4, 0x00002f50);
405
406                 nvc0_ram_train(fuc, 0x010c1001);
407         }
408
409         ram_mask(fuc, 0x10f200, 0x00000800, 0x00000800);
410 // 0x00020016 // 0x00000000
411
412         if (mode == 0)
413                 ram_mask(fuc, 0x132000, 0x00000001, 0x00000000);
414         return 0;
415 }
416
417 static int
418 nvc0_ram_prog(struct nouveau_fb *pfb)
419 {
420         struct nouveau_device *device = nv_device(pfb);
421         struct nvc0_ram *ram = (void *)pfb->ram;
422         struct nvc0_ramfuc *fuc = &ram->fuc;
423         ram_exec(fuc, nouveau_boolopt(device->cfgopt, "NvMemExec", false));
424         return 0;
425 }
426
427 static void
428 nvc0_ram_tidy(struct nouveau_fb *pfb)
429 {
430         struct nvc0_ram *ram = (void *)pfb->ram;
431         struct nvc0_ramfuc *fuc = &ram->fuc;
432         ram_exec(fuc, false);
433 }
434
435 extern const u8 nvc0_pte_storage_type_map[256];
436
437 void
438 nvc0_ram_put(struct nouveau_fb *pfb, struct nouveau_mem **pmem)
439 {
440         struct nouveau_ltcg *ltcg = nouveau_ltcg(pfb);
441         struct nouveau_mem *mem = *pmem;
442
443         *pmem = NULL;
444         if (unlikely(mem == NULL))
445                 return;
446
447         mutex_lock(&pfb->base.mutex);
448         if (mem->tag)
449                 ltcg->tags_free(ltcg, &mem->tag);
450         __nv50_ram_put(pfb, mem);
451         mutex_unlock(&pfb->base.mutex);
452
453         kfree(mem);
454 }
455
456 int
457 nvc0_ram_get(struct nouveau_fb *pfb, u64 size, u32 align, u32 ncmin,
458              u32 memtype, struct nouveau_mem **pmem)
459 {
460         struct nouveau_mm *mm = &pfb->vram;
461         struct nouveau_mm_node *r;
462         struct nouveau_mem *mem;
463         int type = (memtype & 0x0ff);
464         int back = (memtype & 0x800);
465         const bool comp = nvc0_pte_storage_type_map[type] != type;
466         int ret;
467
468         size  >>= 12;
469         align >>= 12;
470         ncmin >>= 12;
471         if (!ncmin)
472                 ncmin = size;
473
474         mem = kzalloc(sizeof(*mem), GFP_KERNEL);
475         if (!mem)
476                 return -ENOMEM;
477
478         INIT_LIST_HEAD(&mem->regions);
479         mem->size = size;
480
481         mutex_lock(&pfb->base.mutex);
482         if (comp) {
483                 struct nouveau_ltcg *ltcg = nouveau_ltcg(pfb);
484
485                 /* compression only works with lpages */
486                 if (align == (1 << (17 - 12))) {
487                         int n = size >> 5;
488                         ltcg->tags_alloc(ltcg, n, &mem->tag);
489                 }
490
491                 if (unlikely(!mem->tag))
492                         type = nvc0_pte_storage_type_map[type];
493         }
494         mem->memtype = type;
495
496         do {
497                 if (back)
498                         ret = nouveau_mm_tail(mm, 1, size, ncmin, align, &r);
499                 else
500                         ret = nouveau_mm_head(mm, 1, size, ncmin, align, &r);
501                 if (ret) {
502                         mutex_unlock(&pfb->base.mutex);
503                         pfb->ram->put(pfb, &mem);
504                         return ret;
505                 }
506
507                 list_add_tail(&r->rl_entry, &mem->regions);
508                 size -= r->length;
509         } while (size);
510         mutex_unlock(&pfb->base.mutex);
511
512         r = list_first_entry(&mem->regions, struct nouveau_mm_node, rl_entry);
513         mem->offset = (u64)r->offset << 12;
514         *pmem = mem;
515         return 0;
516 }
517
518 int
519 nvc0_ram_create_(struct nouveau_object *parent, struct nouveau_object *engine,
520                  struct nouveau_oclass *oclass, int size, void **pobject)
521 {
522         struct nouveau_fb *pfb = nouveau_fb(parent);
523         struct nouveau_bios *bios = nouveau_bios(pfb);
524         struct nouveau_ram *ram;
525         const u32 rsvd_head = ( 256 * 1024) >> 12; /* vga memory */
526         const u32 rsvd_tail = (1024 * 1024) >> 12; /* vbios etc */
527         u32 parts = nv_rd32(pfb, 0x022438);
528         u32 pmask = nv_rd32(pfb, 0x022554);
529         u32 bsize = nv_rd32(pfb, 0x10f20c);
530         u32 offset, length;
531         bool uniform = true;
532         int ret, part;
533
534         ret = nouveau_ram_create_(parent, engine, oclass, size, pobject);
535         ram = *pobject;
536         if (ret)
537                 return ret;
538
539         nv_debug(pfb, "0x100800: 0x%08x\n", nv_rd32(pfb, 0x100800));
540         nv_debug(pfb, "parts 0x%08x mask 0x%08x\n", parts, pmask);
541
542         ram->type = nouveau_fb_bios_memtype(bios);
543         ram->ranks = (nv_rd32(pfb, 0x10f200) & 0x00000004) ? 2 : 1;
544
545         /* read amount of vram attached to each memory controller */
546         for (part = 0; part < parts; part++) {
547                 if (!(pmask & (1 << part))) {
548                         u32 psize = nv_rd32(pfb, 0x11020c + (part * 0x1000));
549                         if (psize != bsize) {
550                                 if (psize < bsize)
551                                         bsize = psize;
552                                 uniform = false;
553                         }
554
555                         nv_debug(pfb, "%d: mem_amount 0x%08x\n", part, psize);
556                         ram->size += (u64)psize << 20;
557                 }
558         }
559
560         /* if all controllers have the same amount attached, there's no holes */
561         if (uniform) {
562                 offset = rsvd_head;
563                 length = (ram->size >> 12) - rsvd_head - rsvd_tail;
564                 ret = nouveau_mm_init(&pfb->vram, offset, length, 1);
565         } else {
566                 /* otherwise, address lowest common amount from 0GiB */
567                 ret = nouveau_mm_init(&pfb->vram, rsvd_head,
568                                       (bsize << 8) * parts, 1);
569                 if (ret)
570                         return ret;
571
572                 /* and the rest starting from (8GiB + common_size) */
573                 offset = (0x0200000000ULL >> 12) + (bsize << 8);
574                 length = (ram->size >> 12) - (bsize << 8) - rsvd_tail;
575
576                 ret = nouveau_mm_init(&pfb->vram, offset, length, 0);
577                 if (ret)
578                         nouveau_mm_fini(&pfb->vram);
579         }
580
581         if (ret)
582                 return ret;
583
584         ram->get = nvc0_ram_get;
585         ram->put = nvc0_ram_put;
586         return 0;
587 }
588
589 static int
590 nvc0_ram_init(struct nouveau_object *object)
591 {
592         struct nouveau_fb *pfb = (void *)object->parent;
593         struct nvc0_ram   *ram = (void *)object;
594         int ret, i;
595
596         ret = nouveau_ram_init(&ram->base);
597         if (ret)
598                 return ret;
599
600         /* prepare for ddr link training, and load training patterns */
601         switch (ram->base.type) {
602         case NV_MEM_TYPE_GDDR5: {
603                 static const u8  train0[] = {
604                         0x00, 0xff, 0x55, 0xaa, 0x33, 0xcc,
605                         0x00, 0xff, 0xff, 0x00, 0xff, 0x00,
606                 };
607                 static const u32 train1[] = {
608                         0x00000000, 0xffffffff,
609                         0x55555555, 0xaaaaaaaa,
610                         0x33333333, 0xcccccccc,
611                         0xf0f0f0f0, 0x0f0f0f0f,
612                         0x00ff00ff, 0xff00ff00,
613                         0x0000ffff, 0xffff0000,
614                 };
615
616                 for (i = 0; i < 0x30; i++) {
617                         nv_wr32(pfb, 0x10f968, 0x00000000 | (i << 8));
618                         nv_wr32(pfb, 0x10f96c, 0x00000000 | (i << 8));
619                         nv_wr32(pfb, 0x10f920, 0x00000100 | train0[i % 12]);
620                         nv_wr32(pfb, 0x10f924, 0x00000100 | train0[i % 12]);
621                         nv_wr32(pfb, 0x10f918,              train1[i % 12]);
622                         nv_wr32(pfb, 0x10f91c,              train1[i % 12]);
623                         nv_wr32(pfb, 0x10f920, 0x00000000 | train0[i % 12]);
624                         nv_wr32(pfb, 0x10f924, 0x00000000 | train0[i % 12]);
625                         nv_wr32(pfb, 0x10f918,              train1[i % 12]);
626                         nv_wr32(pfb, 0x10f91c,              train1[i % 12]);
627                 }
628         }       break;
629         default:
630                 break;
631         }
632
633         return 0;
634 }
635
636 static int
637 nvc0_ram_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
638               struct nouveau_oclass *oclass, void *data, u32 size,
639               struct nouveau_object **pobject)
640 {
641         struct nouveau_bios *bios = nouveau_bios(parent);
642         struct nvc0_ram *ram;
643         int ret;
644
645         ret = nvc0_ram_create(parent, engine, oclass, &ram);
646         *pobject = nv_object(ram);
647         if (ret)
648                 return ret;
649
650         ret = nvbios_pll_parse(bios, 0x0c, &ram->refpll);
651         if (ret) {
652                 nv_error(ram, "mclk refpll data not found\n");
653                 return ret;
654         }
655
656         ret = nvbios_pll_parse(bios, 0x04, &ram->mempll);
657         if (ret) {
658                 nv_error(ram, "mclk pll data not found\n");
659                 return ret;
660         }
661
662         switch (ram->base.type) {
663         case NV_MEM_TYPE_GDDR5:
664                 ram->base.calc = nvc0_ram_calc;
665                 ram->base.prog = nvc0_ram_prog;
666                 ram->base.tidy = nvc0_ram_tidy;
667                 break;
668         default:
669                 nv_warn(ram, "reclocking of this ram type unsupported\n");
670                 return 0;
671         }
672
673         ram->fuc.r_0x10fe20 = ramfuc_reg(0x10fe20);
674         ram->fuc.r_0x10fe24 = ramfuc_reg(0x10fe24);
675         ram->fuc.r_0x137320 = ramfuc_reg(0x137320);
676         ram->fuc.r_0x137330 = ramfuc_reg(0x137330);
677
678         ram->fuc.r_0x132000 = ramfuc_reg(0x132000);
679         ram->fuc.r_0x132004 = ramfuc_reg(0x132004);
680         ram->fuc.r_0x132100 = ramfuc_reg(0x132100);
681
682         ram->fuc.r_0x137390 = ramfuc_reg(0x137390);
683
684         ram->fuc.r_0x10f290 = ramfuc_reg(0x10f290);
685         ram->fuc.r_0x10f294 = ramfuc_reg(0x10f294);
686         ram->fuc.r_0x10f298 = ramfuc_reg(0x10f298);
687         ram->fuc.r_0x10f29c = ramfuc_reg(0x10f29c);
688         ram->fuc.r_0x10f2a0 = ramfuc_reg(0x10f2a0);
689
690         ram->fuc.r_0x10f300 = ramfuc_reg(0x10f300);
691         ram->fuc.r_0x10f338 = ramfuc_reg(0x10f338);
692         ram->fuc.r_0x10f340 = ramfuc_reg(0x10f340);
693         ram->fuc.r_0x10f344 = ramfuc_reg(0x10f344);
694         ram->fuc.r_0x10f348 = ramfuc_reg(0x10f348);
695
696         ram->fuc.r_0x10f910 = ramfuc_reg(0x10f910);
697         ram->fuc.r_0x10f914 = ramfuc_reg(0x10f914);
698
699         ram->fuc.r_0x100b0c = ramfuc_reg(0x100b0c);
700         ram->fuc.r_0x10f050 = ramfuc_reg(0x10f050);
701         ram->fuc.r_0x10f090 = ramfuc_reg(0x10f090);
702         ram->fuc.r_0x10f200 = ramfuc_reg(0x10f200);
703         ram->fuc.r_0x10f210 = ramfuc_reg(0x10f210);
704         ram->fuc.r_0x10f310 = ramfuc_reg(0x10f310);
705         ram->fuc.r_0x10f314 = ramfuc_reg(0x10f314);
706         ram->fuc.r_0x10f610 = ramfuc_reg(0x10f610);
707         ram->fuc.r_0x10f614 = ramfuc_reg(0x10f614);
708         ram->fuc.r_0x10f800 = ramfuc_reg(0x10f800);
709         ram->fuc.r_0x10f808 = ramfuc_reg(0x10f808);
710         ram->fuc.r_0x10f824 = ramfuc_reg(0x10f824);
711         ram->fuc.r_0x10f830 = ramfuc_reg(0x10f830);
712         ram->fuc.r_0x10f988 = ramfuc_reg(0x10f988);
713         ram->fuc.r_0x10f98c = ramfuc_reg(0x10f98c);
714         ram->fuc.r_0x10f990 = ramfuc_reg(0x10f990);
715         ram->fuc.r_0x10f998 = ramfuc_reg(0x10f998);
716         ram->fuc.r_0x10f9b0 = ramfuc_reg(0x10f9b0);
717         ram->fuc.r_0x10f9b4 = ramfuc_reg(0x10f9b4);
718         ram->fuc.r_0x10fb04 = ramfuc_reg(0x10fb04);
719         ram->fuc.r_0x10fb08 = ramfuc_reg(0x10fb08);
720         ram->fuc.r_0x137310 = ramfuc_reg(0x137300);
721         ram->fuc.r_0x137310 = ramfuc_reg(0x137310);
722         ram->fuc.r_0x137360 = ramfuc_reg(0x137360);
723         ram->fuc.r_0x1373ec = ramfuc_reg(0x1373ec);
724         ram->fuc.r_0x1373f0 = ramfuc_reg(0x1373f0);
725         ram->fuc.r_0x1373f8 = ramfuc_reg(0x1373f8);
726
727         ram->fuc.r_0x61c140 = ramfuc_reg(0x61c140);
728         ram->fuc.r_0x611200 = ramfuc_reg(0x611200);
729
730         ram->fuc.r_0x13d8f4 = ramfuc_reg(0x13d8f4);
731         return 0;
732 }
733
734 struct nouveau_oclass
735 nvc0_ram_oclass = {
736         .handle = 0,
737         .ofuncs = &(struct nouveau_ofuncs) {
738                 .ctor = nvc0_ram_ctor,
739                 .dtor = _nouveau_ram_dtor,
740                 .init = nvc0_ram_init,
741                 .fini = _nouveau_ram_fini,
742         }
743 };