drm/gk104-/fb/ram: perform certain steps only when bios data differs
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / core / subdev / fb / ramnve0.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/gpio.h>
26
27 #include <subdev/bios.h>
28 #include <subdev/bios/pll.h>
29 #include <subdev/bios/init.h>
30 #include <subdev/bios/rammap.h>
31 #include <subdev/bios/timing.h>
32 #include <subdev/bios/M0205.h>
33 #include <subdev/bios/M0209.h>
34
35 #include <subdev/clock.h>
36 #include <subdev/clock/pll.h>
37
38 #include <subdev/timer.h>
39
40 #include <core/option.h>
41
42 #include "nvc0.h"
43
44 #include "ramfuc.h"
45
46 struct nve0_ramfuc {
47         struct ramfuc base;
48
49         struct nvbios_pll refpll;
50         struct nvbios_pll mempll;
51
52         struct ramfuc_reg r_gpioMV;
53         u32 r_funcMV[2];
54         struct ramfuc_reg r_gpio2E;
55         u32 r_func2E[2];
56         struct ramfuc_reg r_gpiotrig;
57
58         struct ramfuc_reg r_0x132020;
59         struct ramfuc_reg r_0x132028;
60         struct ramfuc_reg r_0x132024;
61         struct ramfuc_reg r_0x132030;
62         struct ramfuc_reg r_0x132034;
63         struct ramfuc_reg r_0x132000;
64         struct ramfuc_reg r_0x132004;
65         struct ramfuc_reg r_0x132040;
66
67         struct ramfuc_reg r_0x10f248;
68         struct ramfuc_reg r_0x10f290;
69         struct ramfuc_reg r_0x10f294;
70         struct ramfuc_reg r_0x10f298;
71         struct ramfuc_reg r_0x10f29c;
72         struct ramfuc_reg r_0x10f2a0;
73         struct ramfuc_reg r_0x10f2a4;
74         struct ramfuc_reg r_0x10f2a8;
75         struct ramfuc_reg r_0x10f2ac;
76         struct ramfuc_reg r_0x10f2cc;
77         struct ramfuc_reg r_0x10f2e8;
78         struct ramfuc_reg r_0x10f250;
79         struct ramfuc_reg r_0x10f24c;
80         struct ramfuc_reg r_0x10fec4;
81         struct ramfuc_reg r_0x10fec8;
82         struct ramfuc_reg r_0x10f604;
83         struct ramfuc_reg r_0x10f614;
84         struct ramfuc_reg r_0x10f610;
85         struct ramfuc_reg r_0x100770;
86         struct ramfuc_reg r_0x100778;
87         struct ramfuc_reg r_0x10f224;
88
89         struct ramfuc_reg r_0x10f870;
90         struct ramfuc_reg r_0x10f698;
91         struct ramfuc_reg r_0x10f694;
92         struct ramfuc_reg r_0x10f6b8;
93         struct ramfuc_reg r_0x10f808;
94         struct ramfuc_reg r_0x10f670;
95         struct ramfuc_reg r_0x10f60c;
96         struct ramfuc_reg r_0x10f830;
97         struct ramfuc_reg r_0x1373ec;
98         struct ramfuc_reg r_0x10f800;
99         struct ramfuc_reg r_0x10f82c;
100
101         struct ramfuc_reg r_0x10f978;
102         struct ramfuc_reg r_0x10f910;
103         struct ramfuc_reg r_0x10f914;
104
105         struct ramfuc_reg r_mr[16]; /* MR0 - MR8, MR15 */
106
107         struct ramfuc_reg r_0x62c000;
108
109         struct ramfuc_reg r_0x10f200;
110
111         struct ramfuc_reg r_0x10f210;
112         struct ramfuc_reg r_0x10f310;
113         struct ramfuc_reg r_0x10f314;
114         struct ramfuc_reg r_0x10f318;
115         struct ramfuc_reg r_0x10f090;
116         struct ramfuc_reg r_0x10f69c;
117         struct ramfuc_reg r_0x10f824;
118         struct ramfuc_reg r_0x1373f0;
119         struct ramfuc_reg r_0x1373f4;
120         struct ramfuc_reg r_0x137320;
121         struct ramfuc_reg r_0x10f65c;
122         struct ramfuc_reg r_0x10f6bc;
123         struct ramfuc_reg r_0x100710;
124         struct ramfuc_reg r_0x100750;
125 };
126
127 struct nve0_ram {
128         struct nouveau_ram base;
129         struct nve0_ramfuc fuc;
130
131         struct list_head cfg;
132         u32 parts;
133         u32 pmask;
134         u32 pnuts;
135
136         struct nvbios_ramcfg diff;
137         int from;
138         int mode;
139         int N1, fN1, M1, P1;
140         int N2, M2, P2;
141 };
142
143 /*******************************************************************************
144  * GDDR5
145  ******************************************************************************/
146 static void
147 nve0_ram_train(struct nve0_ramfuc *fuc, u32 mask, u32 data)
148 {
149         struct nve0_ram *ram = container_of(fuc, typeof(*ram), fuc);
150         u32 addr = 0x110974, i;
151
152         ram_mask(fuc, 0x10f910, mask, data);
153         ram_mask(fuc, 0x10f914, mask, data);
154
155         for (i = 0; (data & 0x80000000) && i < ram->parts; addr += 0x1000, i++) {
156                 if (ram->pmask & (1 << i))
157                         continue;
158                 ram_wait(fuc, addr, 0x0000000f, 0x00000000, 500000);
159         }
160 }
161
162 static void
163 r1373f4_init(struct nve0_ramfuc *fuc)
164 {
165         struct nve0_ram *ram = container_of(fuc, typeof(*ram), fuc);
166         const u32 mcoef = ((--ram->P2 << 28) | (ram->N2 << 8) | ram->M2);
167         const u32 rcoef = ((  ram->P1 << 16) | (ram->N1 << 8) | ram->M1);
168         const u32 runk0 = ram->fN1 << 16;
169         const u32 runk1 = ram->fN1;
170
171         if (ram->from == 2) {
172                 ram_mask(fuc, 0x1373f4, 0x00000000, 0x00001100);
173                 ram_mask(fuc, 0x1373f4, 0x00000000, 0x00000010);
174         } else {
175                 ram_mask(fuc, 0x1373f4, 0x00000000, 0x00010010);
176         }
177
178         ram_mask(fuc, 0x1373f4, 0x00000003, 0x00000000);
179         ram_mask(fuc, 0x1373f4, 0x00000010, 0x00000000);
180
181         /* (re)program refpll, if required */
182         if ((ram_rd32(fuc, 0x132024) & 0xffffffff) != rcoef ||
183             (ram_rd32(fuc, 0x132034) & 0x0000ffff) != runk1) {
184                 ram_mask(fuc, 0x132000, 0x00000001, 0x00000000);
185                 ram_mask(fuc, 0x132020, 0x00000001, 0x00000000);
186                 ram_wr32(fuc, 0x137320, 0x00000000);
187                 ram_mask(fuc, 0x132030, 0xffff0000, runk0);
188                 ram_mask(fuc, 0x132034, 0x0000ffff, runk1);
189                 ram_wr32(fuc, 0x132024, rcoef);
190                 ram_mask(fuc, 0x132028, 0x00080000, 0x00080000);
191                 ram_mask(fuc, 0x132020, 0x00000001, 0x00000001);
192                 ram_wait(fuc, 0x137390, 0x00020000, 0x00020000, 64000);
193                 ram_mask(fuc, 0x132028, 0x00080000, 0x00000000);
194         }
195
196         /* (re)program mempll, if required */
197         if (ram->mode == 2) {
198                 ram_mask(fuc, 0x1373f4, 0x00010000, 0x00000000);
199                 ram_mask(fuc, 0x132000, 0x80000000, 0x80000000);
200                 ram_mask(fuc, 0x132000, 0x00000001, 0x00000000);
201                 ram_mask(fuc, 0x132004, 0x103fffff, mcoef);
202                 ram_mask(fuc, 0x132000, 0x00000001, 0x00000001);
203                 ram_wait(fuc, 0x137390, 0x00000002, 0x00000002, 64000);
204                 ram_mask(fuc, 0x1373f4, 0x00000000, 0x00001100);
205         } else {
206                 ram_mask(fuc, 0x1373f4, 0x00000000, 0x00010100);
207         }
208
209         ram_mask(fuc, 0x1373f4, 0x00000000, 0x00000010);
210 }
211
212 static void
213 r1373f4_fini(struct nve0_ramfuc *fuc)
214 {
215         struct nve0_ram *ram = container_of(fuc, typeof(*ram), fuc);
216         struct nouveau_ram_data *next = ram->base.next;
217         u8 v0 = next->bios.ramcfg_11_03_c0;
218         u8 v1 = next->bios.ramcfg_11_03_30;
219         u32 tmp;
220
221         tmp = ram_rd32(fuc, 0x1373ec) & ~0x00030000;
222         ram_wr32(fuc, 0x1373ec, tmp | (v1 << 16));
223         ram_mask(fuc, 0x1373f0, (~ram->mode & 3), 0x00000000);
224         if (ram->mode == 2) {
225                 ram_mask(fuc, 0x1373f4, 0x00000003, 0x000000002);
226                 ram_mask(fuc, 0x1373f4, 0x00001100, 0x000000000);
227         } else {
228                 ram_mask(fuc, 0x1373f4, 0x00000003, 0x000000001);
229                 ram_mask(fuc, 0x1373f4, 0x00010000, 0x000000000);
230         }
231         ram_mask(fuc, 0x10f800, 0x00000030, (v0 ^ v1) << 4);
232 }
233
234 static void
235 nve0_ram_nuts(struct nve0_ram *ram, struct ramfuc_reg *reg,
236               u32 _mask, u32 _data, u32 _copy)
237 {
238         struct nve0_fb_priv *priv = (void *)nouveau_fb(ram);
239         struct ramfuc *fuc = &ram->fuc.base;
240         u32 addr = 0x110000 + (reg->addr[0] & 0xfff);
241         u32 mask = _mask | _copy;
242         u32 data = (_data & _mask) | (reg->data & _copy);
243         u32 i;
244
245         for (i = 0; i < 16; i++, addr += 0x1000) {
246                 if (ram->pnuts & (1 << i)) {
247                         u32 prev = nv_rd32(priv, addr);
248                         u32 next = (prev & ~mask) | data;
249                         nouveau_memx_wr32(fuc->memx, addr, next);
250                 }
251         }
252 }
253 #define ram_nuts(s,r,m,d,c)                                                    \
254         nve0_ram_nuts((s), &(s)->fuc.r_##r, (m), (d), (c))
255
256 static int
257 nve0_ram_calc_gddr5(struct nouveau_fb *pfb, u32 freq)
258 {
259         struct nve0_ram *ram = (void *)pfb->ram;
260         struct nve0_ramfuc *fuc = &ram->fuc;
261         struct nouveau_ram_data *next = ram->base.next;
262         int vc = !next->bios.ramcfg_11_02_08;
263         int mv = !next->bios.ramcfg_11_02_04;
264         u32 mask, data;
265
266         ram_mask(fuc, 0x10f808, 0x40000000, 0x40000000);
267         ram_wr32(fuc, 0x62c000, 0x0f0f0000);
268
269         /* MR1: turn termination on early, for some reason.. */
270         if ((ram->base.mr[1] & 0x03c) != 0x030) {
271                 ram_mask(fuc, mr[1], 0x03c, ram->base.mr[1] & 0x03c);
272                 ram_nuts(ram, mr[1], 0x03c, ram->base.mr1_nuts & 0x03c, 0x000);
273         }
274
275         if (vc == 1 && ram_have(fuc, gpio2E)) {
276                 u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[1]);
277                 if (temp != ram_rd32(fuc, gpio2E)) {
278                         ram_wr32(fuc, gpiotrig, 1);
279                         ram_nsec(fuc, 20000);
280                 }
281         }
282
283         ram_mask(fuc, 0x10f200, 0x00000800, 0x00000000);
284
285         nve0_ram_train(fuc, 0x01020000, 0x000c0000);
286
287         ram_wr32(fuc, 0x10f210, 0x00000000); /* REFRESH_AUTO = 0 */
288         ram_nsec(fuc, 1000);
289         ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
290         ram_nsec(fuc, 1000);
291
292         ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
293         ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
294         ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
295         ram_wr32(fuc, 0x10f090, 0x00000061);
296         ram_wr32(fuc, 0x10f090, 0xc000007f);
297         ram_nsec(fuc, 1000);
298
299         ram_wr32(fuc, 0x10f698, 0x00000000);
300         ram_wr32(fuc, 0x10f69c, 0x00000000);
301
302         /*XXX: there does appear to be some kind of condition here, simply
303          *     modifying these bits in the vbios from the default pl0
304          *     entries shows no change.  however, the data does appear to
305          *     be correct and may be required for the transition back
306          */
307         mask = 0x800f07e0;
308         data = 0x00030000;
309         if (ram_rd32(fuc, 0x10f978) & 0x00800000)
310                 data |= 0x00040000;
311
312         if (1) {
313                 data |= 0x800807e0;
314                 switch (next->bios.ramcfg_11_03_c0) {
315                 case 3: data &= ~0x00000040; break;
316                 case 2: data &= ~0x00000100; break;
317                 case 1: data &= ~0x80000000; break;
318                 case 0: data &= ~0x00000400; break;
319                 }
320
321                 switch (next->bios.ramcfg_11_03_30) {
322                 case 3: data &= ~0x00000020; break;
323                 case 2: data &= ~0x00000080; break;
324                 case 1: data &= ~0x00080000; break;
325                 case 0: data &= ~0x00000200; break;
326                 }
327         }
328
329         if (next->bios.ramcfg_11_02_80)
330                 mask |= 0x03000000;
331         if (next->bios.ramcfg_11_02_40)
332                 mask |= 0x00002000;
333         if (next->bios.ramcfg_11_07_10)
334                 mask |= 0x00004000;
335         if (next->bios.ramcfg_11_07_08)
336                 mask |= 0x00000003;
337         else {
338                 mask |= 0x34000000;
339                 if (ram_rd32(fuc, 0x10f978) & 0x00800000)
340                         mask |= 0x40000000;
341         }
342         ram_mask(fuc, 0x10f824, mask, data);
343
344         ram_mask(fuc, 0x132040, 0x00010000, 0x00000000);
345
346         if (ram->from == 2 && ram->mode != 2) {
347                 ram_mask(fuc, 0x10f808, 0x00080000, 0x00000000);
348                 ram_mask(fuc, 0x10f200, 0x18008000, 0x00008000);
349                 ram_mask(fuc, 0x10f800, 0x00000000, 0x00000004);
350                 ram_mask(fuc, 0x10f830, 0x00008000, 0x01040010);
351                 ram_mask(fuc, 0x10f830, 0x01000000, 0x00000000);
352                 r1373f4_init(fuc);
353                 ram_mask(fuc, 0x1373f0, 0x00000002, 0x00000001);
354                 r1373f4_fini(fuc);
355                 ram_mask(fuc, 0x10f830, 0x00c00000, 0x00240001);
356         } else
357         if (ram->from != 2 && ram->mode != 2) {
358                 r1373f4_init(fuc);
359                 r1373f4_fini(fuc);
360         }
361
362         if (ram_have(fuc, gpioMV)) {
363                 u32 temp  = ram_mask(fuc, gpioMV, 0x3000, fuc->r_funcMV[mv]);
364                 if (temp != ram_rd32(fuc, gpioMV)) {
365                         ram_wr32(fuc, gpiotrig, 1);
366                         ram_nsec(fuc, 64000);
367                 }
368         }
369
370         if (next->bios.ramcfg_11_02_40 ||
371             next->bios.ramcfg_11_07_10) {
372                 ram_mask(fuc, 0x132040, 0x00010000, 0x00010000);
373                 ram_nsec(fuc, 20000);
374         }
375
376         if (ram->from != 2 && ram->mode == 2) {
377                 if (0 /*XXX: Titan */)
378                         ram_mask(fuc, 0x10f200, 0x18000000, 0x18000000);
379                 ram_mask(fuc, 0x10f800, 0x00000004, 0x00000000);
380                 ram_mask(fuc, 0x1373f0, 0x00000000, 0x00000002);
381                 ram_mask(fuc, 0x10f830, 0x00800001, 0x00408010);
382                 r1373f4_init(fuc);
383                 r1373f4_fini(fuc);
384                 ram_mask(fuc, 0x10f808, 0x00000000, 0x00080000);
385                 ram_mask(fuc, 0x10f200, 0x00808000, 0x00800000);
386         } else
387         if (ram->from == 2 && ram->mode == 2) {
388                 ram_mask(fuc, 0x10f800, 0x00000004, 0x00000000);
389                 r1373f4_init(fuc);
390                 r1373f4_fini(fuc);
391         }
392
393         if (ram->mode != 2) /*XXX*/ {
394                 if (next->bios.ramcfg_11_07_40)
395                         ram_mask(fuc, 0x10f670, 0x80000000, 0x80000000);
396         }
397
398         ram_wr32(fuc, 0x10f65c, 0x00000011 * next->bios.rammap_11_11_0c);
399         ram_wr32(fuc, 0x10f6b8, 0x01010101 * next->bios.ramcfg_11_09);
400         ram_wr32(fuc, 0x10f6bc, 0x01010101 * next->bios.ramcfg_11_09);
401
402         if (!next->bios.ramcfg_11_07_08 && !next->bios.ramcfg_11_07_04) {
403                 ram_wr32(fuc, 0x10f698, 0x01010101 * next->bios.ramcfg_11_04);
404                 ram_wr32(fuc, 0x10f69c, 0x01010101 * next->bios.ramcfg_11_04);
405         } else
406         if (!next->bios.ramcfg_11_07_08) {
407                 ram_wr32(fuc, 0x10f698, 0x00000000);
408                 ram_wr32(fuc, 0x10f69c, 0x00000000);
409         }
410
411         if (ram->mode != 2) {
412                 u32 data = 0x01000100 * next->bios.ramcfg_11_04;
413                 ram_nuke(fuc, 0x10f694);
414                 ram_mask(fuc, 0x10f694, 0xff00ff00, data);
415         }
416
417         if (ram->mode == 2 && next->bios.ramcfg_11_08_10)
418                 data = 0x00000080;
419         else
420                 data = 0x00000000;
421         ram_mask(fuc, 0x10f60c, 0x00000080, data);
422
423         mask = 0x00070000;
424         data = 0x00000000;
425         if (!next->bios.ramcfg_11_02_80)
426                 data |= 0x03000000;
427         if (!next->bios.ramcfg_11_02_40)
428                 data |= 0x00002000;
429         if (!next->bios.ramcfg_11_07_10)
430                 data |= 0x00004000;
431         if (!next->bios.ramcfg_11_07_08)
432                 data |= 0x00000003;
433         else
434                 data |= 0x74000000;
435         ram_mask(fuc, 0x10f824, mask, data);
436
437         if (next->bios.ramcfg_11_01_08)
438                 data = 0x00000000;
439         else
440                 data = 0x00001000;
441         ram_mask(fuc, 0x10f200, 0x00001000, data);
442
443         if (ram_rd32(fuc, 0x10f670) & 0x80000000) {
444                 ram_nsec(fuc, 10000);
445                 ram_mask(fuc, 0x10f670, 0x80000000, 0x00000000);
446         }
447
448         if (next->bios.ramcfg_11_08_01)
449                 data = 0x00100000;
450         else
451                 data = 0x00000000;
452         ram_mask(fuc, 0x10f82c, 0x00100000, data);
453
454         data = 0x00000000;
455         if (next->bios.ramcfg_11_08_08)
456                 data |= 0x00002000;
457         if (next->bios.ramcfg_11_08_04)
458                 data |= 0x00001000;
459         if (next->bios.ramcfg_11_08_02)
460                 data |= 0x00004000;
461         ram_mask(fuc, 0x10f830, 0x00007000, data);
462
463         /* PFB timing */
464         ram_mask(fuc, 0x10f248, 0xffffffff, next->bios.timing[10]);
465         ram_mask(fuc, 0x10f290, 0xffffffff, next->bios.timing[0]);
466         ram_mask(fuc, 0x10f294, 0xffffffff, next->bios.timing[1]);
467         ram_mask(fuc, 0x10f298, 0xffffffff, next->bios.timing[2]);
468         ram_mask(fuc, 0x10f29c, 0xffffffff, next->bios.timing[3]);
469         ram_mask(fuc, 0x10f2a0, 0xffffffff, next->bios.timing[4]);
470         ram_mask(fuc, 0x10f2a4, 0xffffffff, next->bios.timing[5]);
471         ram_mask(fuc, 0x10f2a8, 0xffffffff, next->bios.timing[6]);
472         ram_mask(fuc, 0x10f2ac, 0xffffffff, next->bios.timing[7]);
473         ram_mask(fuc, 0x10f2cc, 0xffffffff, next->bios.timing[8]);
474         ram_mask(fuc, 0x10f2e8, 0xffffffff, next->bios.timing[9]);
475
476         data = mask = 0x00000000;
477         if (ram->diff.ramcfg_11_08_20) {
478                 if (next->bios.ramcfg_11_08_20)
479                         data |= 0x01000000;
480                 mask |= 0x01000000;
481         }
482         ram_mask(fuc, 0x10f200, mask, data);
483
484         data = mask = 0x00000000;
485         if (ram->diff.ramcfg_11_02_03) {
486                 data |= next->bios.ramcfg_11_02_03 << 8;
487                 mask |= 0x00000300;
488         }
489         if (ram->diff.ramcfg_11_01_10) {
490                 if (next->bios.ramcfg_11_01_10)
491                         data |= 0x70000000;
492                 mask |= 0x70000000;
493         }
494         ram_mask(fuc, 0x10f604, mask, data);
495
496         data = mask = 0x00000000;
497         if (ram->diff.timing_20_30_07) {
498                 data |= next->bios.timing_20_30_07 << 28;
499                 mask |= 0x70000000;
500         }
501         if (ram->diff.ramcfg_11_01_01) {
502                 if (next->bios.ramcfg_11_01_01)
503                         data |= 0x00000100;
504                 mask |= 0x00000100;
505         }
506         ram_mask(fuc, 0x10f614, mask, data);
507
508         data = mask = 0x00000000;
509         if (ram->diff.timing_20_30_07) {
510                 data |= next->bios.timing_20_30_07 << 28;
511                 mask |= 0x70000000;
512         }
513         if (ram->diff.ramcfg_11_01_02) {
514                 if (next->bios.ramcfg_11_01_02)
515                         data |= 0x00000100;
516                 mask |= 0x00000100;
517         }
518         ram_mask(fuc, 0x10f610, mask, data);
519
520         mask = 0x33f00000;
521         data = 0x00000000;
522         if (!next->bios.ramcfg_11_01_04)
523                 data |= 0x20200000;
524         if (!next->bios.ramcfg_11_07_80)
525                 data |= 0x12800000;
526         /*XXX: see note above about there probably being some condition
527          *     for the 10f824 stuff that uses ramcfg 3...
528          */
529         if (next->bios.ramcfg_11_03_f0) {
530                 if (next->bios.rammap_11_08_0c) {
531                         if (!next->bios.ramcfg_11_07_80)
532                                 mask |= 0x00000020;
533                         else
534                                 data |= 0x00000020;
535                         mask |= 0x00000004;
536                 }
537         } else {
538                 mask |= 0x40000020;
539                 data |= 0x00000004;
540         }
541
542         ram_mask(fuc, 0x10f808, mask, data);
543
544         ram_wr32(fuc, 0x10f870, 0x11111111 * next->bios.ramcfg_11_03_0f);
545
546         data = mask = 0x00000000;
547         if (ram->diff.ramcfg_11_02_03) {
548                 data |= next->bios.ramcfg_11_02_03;
549                 mask |= 0x00000003;
550         }
551         if (ram->diff.ramcfg_11_01_10) {
552                 if (next->bios.ramcfg_11_01_10)
553                         data |= 0x00000004;
554                 mask |= 0x00000004;
555         }
556
557         if ((ram_mask(fuc, 0x100770, mask, data) & mask & 4) != (data & 4)) {
558                 ram_mask(fuc, 0x100750, 0x00000008, 0x00000008);
559                 ram_wr32(fuc, 0x100710, 0x00000000);
560                 ram_wait(fuc, 0x100710, 0x80000000, 0x80000000, 200000);
561         }
562
563         data = next->bios.timing_20_30_07 << 8;
564         if (next->bios.ramcfg_11_01_01)
565                 data |= 0x80000000;
566         ram_mask(fuc, 0x100778, 0x00000700, data);
567
568         ram_mask(fuc, 0x10f250, 0x000003f0, next->bios.timing_20_2c_003f << 4);
569         data = (next->bios.timing[10] & 0x7f000000) >> 24;
570         if (data < next->bios.timing_20_2c_1fc0)
571                 data = next->bios.timing_20_2c_1fc0;
572         ram_mask(fuc, 0x10f24c, 0x7f000000, data << 24);
573         ram_mask(fuc, 0x10f224, 0x001f0000, next->bios.timing_20_30_f8 << 16);
574
575         ram_mask(fuc, 0x10fec4, 0x041e0f07, next->bios.timing_20_31_0800 << 26 |
576                                             next->bios.timing_20_31_0780 << 17 |
577                                             next->bios.timing_20_31_0078 << 8 |
578                                             next->bios.timing_20_31_0007);
579         ram_mask(fuc, 0x10fec8, 0x00000027, next->bios.timing_20_31_8000 << 5 |
580                                             next->bios.timing_20_31_7000);
581
582         ram_wr32(fuc, 0x10f090, 0x4000007e);
583         ram_nsec(fuc, 2000);
584         ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
585         ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
586         ram_wr32(fuc, 0x10f210, 0x80000000); /* REFRESH_AUTO = 1 */
587
588         if (next->bios.ramcfg_11_08_10 && (ram->mode == 2) /*XXX*/) {
589                 u32 temp = ram_mask(fuc, 0x10f294, 0xff000000, 0x24000000);
590                 nve0_ram_train(fuc, 0xbc0e0000, 0xa4010000); /*XXX*/
591                 ram_nsec(fuc, 1000);
592                 ram_wr32(fuc, 0x10f294, temp);
593         }
594
595         ram_mask(fuc, mr[3], 0xfff, ram->base.mr[3]);
596         ram_wr32(fuc, mr[0], ram->base.mr[0]);
597         ram_mask(fuc, mr[8], 0xfff, ram->base.mr[8]);
598         ram_nsec(fuc, 1000);
599         ram_mask(fuc, mr[1], 0xfff, ram->base.mr[1]);
600         ram_mask(fuc, mr[5], 0xfff, ram->base.mr[5] & ~0x004); /* LP3 later */
601         ram_mask(fuc, mr[6], 0xfff, ram->base.mr[6]);
602         ram_mask(fuc, mr[7], 0xfff, ram->base.mr[7]);
603
604         if (vc == 0 && ram_have(fuc, gpio2E)) {
605                 u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[0]);
606                 if (temp != ram_rd32(fuc, gpio2E)) {
607                         ram_wr32(fuc, gpiotrig, 1);
608                         ram_nsec(fuc, 20000);
609                 }
610         }
611
612         ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
613         ram_wr32(fuc, 0x10f318, 0x00000001); /* NOP? */
614         ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
615         ram_nsec(fuc, 1000);
616         ram_nuts(ram, 0x10f200, 0x18808800, 0x00000000, 0x18808800);
617
618         data  = ram_rd32(fuc, 0x10f978);
619         data &= ~0x00046144;
620         data |=  0x0000000b;
621         if (!next->bios.ramcfg_11_07_08) {
622                 if (!next->bios.ramcfg_11_07_04)
623                         data |= 0x0000200c;
624                 else
625                         data |= 0x00000000;
626         } else {
627                 data |= 0x00040044;
628         }
629         ram_wr32(fuc, 0x10f978, data);
630
631         if (ram->mode == 1) {
632                 data = ram_rd32(fuc, 0x10f830) | 0x00000001;
633                 ram_wr32(fuc, 0x10f830, data);
634         }
635
636         if (!next->bios.ramcfg_11_07_08) {
637                 data = 0x88020000;
638                 if ( next->bios.ramcfg_11_07_04)
639                         data |= 0x10000000;
640                 if (!next->bios.rammap_11_08_10)
641                         data |= 0x00080000;
642         } else {
643                 data = 0xa40e0000;
644         }
645         nve0_ram_train(fuc, 0xbc0f0000, data);
646         if (1) /* XXX: not always? */
647                 ram_nsec(fuc, 1000);
648
649         if (ram->mode == 2) { /*XXX*/
650                 ram_mask(fuc, 0x10f800, 0x00000004, 0x00000004);
651         }
652
653         /* LP3 */
654         if (ram_mask(fuc, mr[5], 0x004, ram->base.mr[5]) != ram->base.mr[5])
655                 ram_nsec(fuc, 1000);
656
657         if (ram->mode != 2) {
658                 ram_mask(fuc, 0x10f830, 0x01000000, 0x01000000);
659                 ram_mask(fuc, 0x10f830, 0x01000000, 0x00000000);
660         }
661
662         if (next->bios.ramcfg_11_07_02)
663                 nve0_ram_train(fuc, 0x80020000, 0x01000000);
664
665         ram_wr32(fuc, 0x62c000, 0x0f0f0f00);
666
667         if (next->bios.rammap_11_08_01)
668                 data = 0x00000800;
669         else
670                 data = 0x00000000;
671         ram_mask(fuc, 0x10f200, 0x00000800, data);
672         ram_nuts(ram, 0x10f200, 0x18808800, data, 0x18808800);
673         return 0;
674 }
675
676 /*******************************************************************************
677  * DDR3
678  ******************************************************************************/
679
680 static int
681 nve0_ram_calc_sddr3(struct nouveau_fb *pfb, u32 freq)
682 {
683         struct nve0_ram *ram = (void *)pfb->ram;
684         struct nve0_ramfuc *fuc = &ram->fuc;
685         const u32 rcoef = ((  ram->P1 << 16) | (ram->N1 << 8) | ram->M1);
686         const u32 runk0 = ram->fN1 << 16;
687         const u32 runk1 = ram->fN1;
688         struct nouveau_ram_data *next = ram->base.next;
689         int vc = !next->bios.ramcfg_11_02_08;
690         int mv = !next->bios.ramcfg_11_02_04;
691         u32 mask, data;
692
693         ram_mask(fuc, 0x10f808, 0x40000000, 0x40000000);
694         ram_wr32(fuc, 0x62c000, 0x0f0f0000);
695
696         if (vc == 1 && ram_have(fuc, gpio2E)) {
697                 u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[1]);
698                 if (temp != ram_rd32(fuc, gpio2E)) {
699                         ram_wr32(fuc, gpiotrig, 1);
700                         ram_nsec(fuc, 20000);
701                 }
702         }
703
704         ram_mask(fuc, 0x10f200, 0x00000800, 0x00000000);
705         if (next->bios.ramcfg_11_03_f0)
706                 ram_mask(fuc, 0x10f808, 0x04000000, 0x04000000);
707
708         ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
709         ram_wr32(fuc, 0x10f210, 0x00000000); /* REFRESH_AUTO = 0 */
710         ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
711         ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
712         ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
713         ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
714         ram_nsec(fuc, 1000);
715
716         ram_wr32(fuc, 0x10f090, 0x00000060);
717         ram_wr32(fuc, 0x10f090, 0xc000007e);
718
719         /*XXX: there does appear to be some kind of condition here, simply
720          *     modifying these bits in the vbios from the default pl0
721          *     entries shows no change.  however, the data does appear to
722          *     be correct and may be required for the transition back
723          */
724         mask = 0x00010000;
725         data = 0x00010000;
726
727         if (1) {
728                 mask |= 0x800807e0;
729                 data |= 0x800807e0;
730                 switch (next->bios.ramcfg_11_03_c0) {
731                 case 3: data &= ~0x00000040; break;
732                 case 2: data &= ~0x00000100; break;
733                 case 1: data &= ~0x80000000; break;
734                 case 0: data &= ~0x00000400; break;
735                 }
736
737                 switch (next->bios.ramcfg_11_03_30) {
738                 case 3: data &= ~0x00000020; break;
739                 case 2: data &= ~0x00000080; break;
740                 case 1: data &= ~0x00080000; break;
741                 case 0: data &= ~0x00000200; break;
742                 }
743         }
744
745         if (next->bios.ramcfg_11_02_80)
746                 mask |= 0x03000000;
747         if (next->bios.ramcfg_11_02_40)
748                 mask |= 0x00002000;
749         if (next->bios.ramcfg_11_07_10)
750                 mask |= 0x00004000;
751         if (next->bios.ramcfg_11_07_08)
752                 mask |= 0x00000003;
753         else
754                 mask |= 0x14000000;
755         ram_mask(fuc, 0x10f824, mask, data);
756
757         ram_mask(fuc, 0x132040, 0x00010000, 0x00000000);
758
759         ram_mask(fuc, 0x1373f4, 0x00000000, 0x00010010);
760         data  = ram_rd32(fuc, 0x1373ec) & ~0x00030000;
761         data |= next->bios.ramcfg_11_03_30 << 16;
762         ram_wr32(fuc, 0x1373ec, data);
763         ram_mask(fuc, 0x1373f4, 0x00000003, 0x00000000);
764         ram_mask(fuc, 0x1373f4, 0x00000010, 0x00000000);
765
766         /* (re)program refpll, if required */
767         if ((ram_rd32(fuc, 0x132024) & 0xffffffff) != rcoef ||
768             (ram_rd32(fuc, 0x132034) & 0x0000ffff) != runk1) {
769                 ram_mask(fuc, 0x132000, 0x00000001, 0x00000000);
770                 ram_mask(fuc, 0x132020, 0x00000001, 0x00000000);
771                 ram_wr32(fuc, 0x137320, 0x00000000);
772                 ram_mask(fuc, 0x132030, 0xffff0000, runk0);
773                 ram_mask(fuc, 0x132034, 0x0000ffff, runk1);
774                 ram_wr32(fuc, 0x132024, rcoef);
775                 ram_mask(fuc, 0x132028, 0x00080000, 0x00080000);
776                 ram_mask(fuc, 0x132020, 0x00000001, 0x00000001);
777                 ram_wait(fuc, 0x137390, 0x00020000, 0x00020000, 64000);
778                 ram_mask(fuc, 0x132028, 0x00080000, 0x00000000);
779         }
780
781         ram_mask(fuc, 0x1373f4, 0x00000010, 0x00000010);
782         ram_mask(fuc, 0x1373f4, 0x00000003, 0x00000001);
783         ram_mask(fuc, 0x1373f4, 0x00010000, 0x00000000);
784
785         if (ram_have(fuc, gpioMV)) {
786                 u32 temp  = ram_mask(fuc, gpioMV, 0x3000, fuc->r_funcMV[mv]);
787                 if (temp != ram_rd32(fuc, gpioMV)) {
788                         ram_wr32(fuc, gpiotrig, 1);
789                         ram_nsec(fuc, 64000);
790                 }
791         }
792
793         if (next->bios.ramcfg_11_02_40 ||
794             next->bios.ramcfg_11_07_10) {
795                 ram_mask(fuc, 0x132040, 0x00010000, 0x00010000);
796                 ram_nsec(fuc, 20000);
797         }
798
799         if (ram->mode != 2) /*XXX*/ {
800                 if (next->bios.ramcfg_11_07_40)
801                         ram_mask(fuc, 0x10f670, 0x80000000, 0x80000000);
802         }
803
804         ram_wr32(fuc, 0x10f65c, 0x00000011 * next->bios.rammap_11_11_0c);
805         ram_wr32(fuc, 0x10f6b8, 0x01010101 * next->bios.ramcfg_11_09);
806         ram_wr32(fuc, 0x10f6bc, 0x01010101 * next->bios.ramcfg_11_09);
807
808         mask = 0x00010000;
809         data = 0x00000000;
810         if (!next->bios.ramcfg_11_02_80)
811                 data |= 0x03000000;
812         if (!next->bios.ramcfg_11_02_40)
813                 data |= 0x00002000;
814         if (!next->bios.ramcfg_11_07_10)
815                 data |= 0x00004000;
816         if (!next->bios.ramcfg_11_07_08)
817                 data |= 0x00000003;
818         else
819                 data |= 0x14000000;
820         ram_mask(fuc, 0x10f824, mask, data);
821         ram_nsec(fuc, 1000);
822
823         if (next->bios.ramcfg_11_08_01)
824                 data = 0x00100000;
825         else
826                 data = 0x00000000;
827         ram_mask(fuc, 0x10f82c, 0x00100000, data);
828
829         /* PFB timing */
830         ram_mask(fuc, 0x10f248, 0xffffffff, next->bios.timing[10]);
831         ram_mask(fuc, 0x10f290, 0xffffffff, next->bios.timing[0]);
832         ram_mask(fuc, 0x10f294, 0xffffffff, next->bios.timing[1]);
833         ram_mask(fuc, 0x10f298, 0xffffffff, next->bios.timing[2]);
834         ram_mask(fuc, 0x10f29c, 0xffffffff, next->bios.timing[3]);
835         ram_mask(fuc, 0x10f2a0, 0xffffffff, next->bios.timing[4]);
836         ram_mask(fuc, 0x10f2a4, 0xffffffff, next->bios.timing[5]);
837         ram_mask(fuc, 0x10f2a8, 0xffffffff, next->bios.timing[6]);
838         ram_mask(fuc, 0x10f2ac, 0xffffffff, next->bios.timing[7]);
839         ram_mask(fuc, 0x10f2cc, 0xffffffff, next->bios.timing[8]);
840         ram_mask(fuc, 0x10f2e8, 0xffffffff, next->bios.timing[9]);
841
842         mask = 0x33f00000;
843         data = 0x00000000;
844         if (!next->bios.ramcfg_11_01_04)
845                 data |= 0x20200000;
846         if (!next->bios.ramcfg_11_07_80)
847                 data |= 0x12800000;
848         /*XXX: see note above about there probably being some condition
849          *     for the 10f824 stuff that uses ramcfg 3...
850          */
851         if (next->bios.ramcfg_11_03_f0) {
852                 if (next->bios.rammap_11_08_0c) {
853                         if (!next->bios.ramcfg_11_07_80)
854                                 mask |= 0x00000020;
855                         else
856                                 data |= 0x00000020;
857                         mask |= 0x08000004;
858                 }
859                 data |= 0x04000000;
860         } else {
861                 mask |= 0x44000020;
862                 data |= 0x08000004;
863         }
864
865         ram_mask(fuc, 0x10f808, mask, data);
866
867         ram_wr32(fuc, 0x10f870, 0x11111111 * next->bios.ramcfg_11_03_0f);
868
869         ram_mask(fuc, 0x10f250, 0x000003f0, next->bios.timing_20_2c_003f << 4);
870
871         data = (next->bios.timing[10] & 0x7f000000) >> 24;
872         if (data < next->bios.timing_20_2c_1fc0)
873                 data = next->bios.timing_20_2c_1fc0;
874         ram_mask(fuc, 0x10f24c, 0x7f000000, data << 24);
875
876         ram_mask(fuc, 0x10f224, 0x001f0000, next->bios.timing_20_30_f8 << 16);
877
878         ram_wr32(fuc, 0x10f090, 0x4000007f);
879         ram_nsec(fuc, 1000);
880
881         ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
882         ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
883         ram_wr32(fuc, 0x10f210, 0x80000000); /* REFRESH_AUTO = 1 */
884         ram_nsec(fuc, 1000);
885
886         ram_nuke(fuc, mr[0]);
887         ram_mask(fuc, mr[0], 0x100, 0x100);
888         ram_mask(fuc, mr[0], 0x100, 0x000);
889
890         ram_mask(fuc, mr[2], 0xfff, ram->base.mr[2]);
891         ram_wr32(fuc, mr[0], ram->base.mr[0]);
892         ram_nsec(fuc, 1000);
893
894         ram_nuke(fuc, mr[0]);
895         ram_mask(fuc, mr[0], 0x100, 0x100);
896         ram_mask(fuc, mr[0], 0x100, 0x000);
897
898         if (vc == 0 && ram_have(fuc, gpio2E)) {
899                 u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[0]);
900                 if (temp != ram_rd32(fuc, gpio2E)) {
901                         ram_wr32(fuc, gpiotrig, 1);
902                         ram_nsec(fuc, 20000);
903                 }
904         }
905
906         if (ram->mode != 2) {
907                 ram_mask(fuc, 0x10f830, 0x01000000, 0x01000000);
908                 ram_mask(fuc, 0x10f830, 0x01000000, 0x00000000);
909         }
910
911         ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
912         ram_wr32(fuc, 0x10f318, 0x00000001); /* NOP? */
913         ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
914         ram_nsec(fuc, 1000);
915
916         ram_wr32(fuc, 0x62c000, 0x0f0f0f00);
917
918         if (next->bios.rammap_11_08_01)
919                 data = 0x00000800;
920         else
921                 data = 0x00000000;
922         ram_mask(fuc, 0x10f200, 0x00000800, data);
923         return 0;
924 }
925
926 /*******************************************************************************
927  * main hooks
928  ******************************************************************************/
929
930 static int
931 nve0_ram_calc_data(struct nouveau_fb *pfb, u32 khz,
932                    struct nouveau_ram_data *data)
933 {
934         struct nve0_ram *ram = (void *)pfb->ram;
935         struct nouveau_ram_data *cfg;
936         u32 mhz = khz / 1000;
937
938         list_for_each_entry(cfg, &ram->cfg, head) {
939                 if (mhz >= cfg->bios.rammap_min &&
940                     mhz <= cfg->bios.rammap_max) {
941                         *data = *cfg;
942                         data->freq = khz;
943                         return 0;
944                 }
945         }
946
947         nv_error(ram, "ramcfg data for %dMHz not found\n", mhz);
948         return -EINVAL;
949 }
950
951 static int
952 nve0_ram_calc_xits(struct nouveau_fb *pfb, struct nouveau_ram_data *next)
953 {
954         struct nve0_ram *ram = (void *)pfb->ram;
955         struct nve0_ramfuc *fuc = &ram->fuc;
956         int refclk, i;
957         int ret;
958
959         ret = ram_init(fuc, pfb);
960         if (ret)
961                 return ret;
962
963         ram_fb_disable(fuc);
964
965         ram->mode = (next->freq > fuc->refpll.vco1.max_freq) ? 2 : 1;
966         ram->from = ram_rd32(fuc, 0x1373f4) & 0x0000000f;
967
968         /* XXX: this is *not* what nvidia do.  on fermi nvidia generally
969          * select, based on some unknown condition, one of the two possible
970          * reference frequencies listed in the vbios table for mempll and
971          * program refpll to that frequency.
972          *
973          * so far, i've seen very weird values being chosen by nvidia on
974          * kepler boards, no idea how/why they're chosen.
975          */
976         refclk = next->freq;
977         if (ram->mode == 2)
978                 refclk = fuc->mempll.refclk;
979
980         /* calculate refpll coefficients */
981         ret = nva3_pll_calc(nv_subdev(pfb), &fuc->refpll, refclk, &ram->N1,
982                            &ram->fN1, &ram->M1, &ram->P1);
983         fuc->mempll.refclk = ret;
984         if (ret <= 0) {
985                 nv_error(pfb, "unable to calc refpll\n");
986                 return -EINVAL;
987         }
988
989         /* calculate mempll coefficients, if we're using it */
990         if (ram->mode == 2) {
991                 /* post-divider doesn't work... the reg takes the values but
992                  * appears to completely ignore it.  there *is* a bit at
993                  * bit 28 that appears to divide the clock by 2 if set.
994                  */
995                 fuc->mempll.min_p = 1;
996                 fuc->mempll.max_p = 2;
997
998                 ret = nva3_pll_calc(nv_subdev(pfb), &fuc->mempll, next->freq,
999                                    &ram->N2, NULL, &ram->M2, &ram->P2);
1000                 if (ret <= 0) {
1001                         nv_error(pfb, "unable to calc mempll\n");
1002                         return -EINVAL;
1003                 }
1004         }
1005
1006         for (i = 0; i < ARRAY_SIZE(fuc->r_mr); i++) {
1007                 if (ram_have(fuc, mr[i]))
1008                         ram->base.mr[i] = ram_rd32(fuc, mr[i]);
1009         }
1010         ram->base.freq = next->freq;
1011
1012         switch (ram->base.type) {
1013         case NV_MEM_TYPE_DDR3:
1014                 ret = nouveau_sddr3_calc(&ram->base);
1015                 if (ret == 0)
1016                         ret = nve0_ram_calc_sddr3(pfb, next->freq);
1017                 break;
1018         case NV_MEM_TYPE_GDDR5:
1019                 ret = nouveau_gddr5_calc(&ram->base, ram->pnuts != 0);
1020                 if (ret == 0)
1021                         ret = nve0_ram_calc_gddr5(pfb, next->freq);
1022                 break;
1023         default:
1024                 ret = -ENOSYS;
1025                 break;
1026         }
1027
1028         if (!ret)
1029                 ram_fb_enable(fuc);
1030
1031         return ret;
1032 }
1033
1034 static int
1035 nve0_ram_calc(struct nouveau_fb *pfb, u32 freq)
1036 {
1037         struct nouveau_clock *clk = nouveau_clock(pfb);
1038         struct nve0_ram *ram = (void *)pfb->ram;
1039         struct nouveau_ram_data *xits = &ram->base.xition;
1040         struct nouveau_ram_data *copy;
1041         int ret;
1042
1043         if (ram->base.next == NULL) {
1044                 ret = nve0_ram_calc_data(pfb, clk->read(clk, nv_clk_src_mem),
1045                                         &ram->base.former);
1046                 if (ret)
1047                         return ret;
1048
1049                 ret = nve0_ram_calc_data(pfb, freq, &ram->base.target);
1050                 if (ret)
1051                         return ret;
1052
1053                 if (ram->base.target.freq < ram->base.former.freq) {
1054                         *xits = ram->base.target;
1055                         copy = &ram->base.former;
1056                 } else {
1057                         *xits = ram->base.former;
1058                         copy = &ram->base.target;
1059                 }
1060
1061                 xits->bios.ramcfg_11_02_04 = copy->bios.ramcfg_11_02_04;
1062                 xits->bios.ramcfg_11_02_03 = copy->bios.ramcfg_11_02_03;
1063                 xits->bios.timing_20_30_07 = copy->bios.timing_20_30_07;
1064
1065                 ram->base.next = &ram->base.target;
1066                 if (memcmp(xits, &ram->base.former, sizeof(xits->bios)))
1067                         ram->base.next = &ram->base.xition;
1068         } else {
1069                 BUG_ON(ram->base.next != &ram->base.xition);
1070                 ram->base.next = &ram->base.target;
1071         }
1072
1073         return nve0_ram_calc_xits(pfb, ram->base.next);
1074 }
1075
1076 static int
1077 nve0_ram_prog(struct nouveau_fb *pfb)
1078 {
1079         struct nouveau_device *device = nv_device(pfb);
1080         struct nve0_ram *ram = (void *)pfb->ram;
1081         struct nve0_ramfuc *fuc = &ram->fuc;
1082         ram_exec(fuc, nouveau_boolopt(device->cfgopt, "NvMemExec", true));
1083         return (ram->base.next == &ram->base.xition);
1084 }
1085
1086 static void
1087 nve0_ram_tidy(struct nouveau_fb *pfb)
1088 {
1089         struct nve0_ram *ram = (void *)pfb->ram;
1090         struct nve0_ramfuc *fuc = &ram->fuc;
1091         ram->base.next = NULL;
1092         ram_exec(fuc, false);
1093 }
1094
1095 struct nve0_ram_train {
1096         u16 mask;
1097         struct nvbios_M0209S remap;
1098         struct nvbios_M0209S type00;
1099         struct nvbios_M0209S type01;
1100         struct nvbios_M0209S type04;
1101         struct nvbios_M0209S type06;
1102         struct nvbios_M0209S type07;
1103         struct nvbios_M0209S type08;
1104         struct nvbios_M0209S type09;
1105 };
1106
1107 static int
1108 nve0_ram_train_type(struct nouveau_fb *pfb, int i, u8 ramcfg,
1109                     struct nve0_ram_train *train)
1110 {
1111         struct nouveau_bios *bios = nouveau_bios(pfb);
1112         struct nvbios_M0205E M0205E;
1113         struct nvbios_M0205S M0205S;
1114         struct nvbios_M0209E M0209E;
1115         struct nvbios_M0209S *remap = &train->remap;
1116         struct nvbios_M0209S *value;
1117         u8  ver, hdr, cnt, len;
1118         u32 data;
1119
1120         /* determine type of data for this index */
1121         if (!(data = nvbios_M0205Ep(bios, i, &ver, &hdr, &cnt, &len, &M0205E)))
1122                 return -ENOENT;
1123
1124         switch (M0205E.type) {
1125         case 0x00: value = &train->type00; break;
1126         case 0x01: value = &train->type01; break;
1127         case 0x04: value = &train->type04; break;
1128         case 0x06: value = &train->type06; break;
1129         case 0x07: value = &train->type07; break;
1130         case 0x08: value = &train->type08; break;
1131         case 0x09: value = &train->type09; break;
1132         default:
1133                 return 0;
1134         }
1135
1136         /* training data index determined by ramcfg strap */
1137         if (!(data = nvbios_M0205Sp(bios, i, ramcfg, &ver, &hdr, &M0205S)))
1138                 return -EINVAL;
1139         i = M0205S.data;
1140
1141         /* training data format information */
1142         if (!(data = nvbios_M0209Ep(bios, i, &ver, &hdr, &cnt, &len, &M0209E)))
1143                 return -EINVAL;
1144
1145         /* ... and the raw data */
1146         if (!(data = nvbios_M0209Sp(bios, i, 0, &ver, &hdr, value)))
1147                 return -EINVAL;
1148
1149         if (M0209E.v02_07 == 2) {
1150                 /* of course! why wouldn't we have a pointer to another entry
1151                  * in the same table, and use the first one as an array of
1152                  * remap indices...
1153                  */
1154                 if (!(data = nvbios_M0209Sp(bios, M0209E.v03, 0, &ver, &hdr,
1155                                             remap)))
1156                         return -EINVAL;
1157
1158                 for (i = 0; i < ARRAY_SIZE(value->data); i++)
1159                         value->data[i] = remap->data[value->data[i]];
1160         } else
1161         if (M0209E.v02_07 != 1)
1162                 return -EINVAL;
1163
1164         train->mask |= 1 << M0205E.type;
1165         return 0;
1166 }
1167
1168 static int
1169 nve0_ram_train_init_0(struct nouveau_fb *pfb, struct nve0_ram_train *train)
1170 {
1171         int i, j;
1172
1173         if ((train->mask & 0x03d3) != 0x03d3) {
1174                 nv_warn(pfb, "missing link training data\n");
1175                 return -EINVAL;
1176         }
1177
1178         for (i = 0; i < 0x30; i++) {
1179                 for (j = 0; j < 8; j += 4) {
1180                         nv_wr32(pfb, 0x10f968 + j, 0x00000000 | (i << 8));
1181                         nv_wr32(pfb, 0x10f920 + j, 0x00000000 |
1182                                                    train->type08.data[i] << 4 |
1183                                                    train->type06.data[i]);
1184                         nv_wr32(pfb, 0x10f918 + j, train->type00.data[i]);
1185                         nv_wr32(pfb, 0x10f920 + j, 0x00000100 |
1186                                                    train->type09.data[i] << 4 |
1187                                                    train->type07.data[i]);
1188                         nv_wr32(pfb, 0x10f918 + j, train->type01.data[i]);
1189                 }
1190         }
1191
1192         for (j = 0; j < 8; j += 4) {
1193                 for (i = 0; i < 0x100; i++) {
1194                         nv_wr32(pfb, 0x10f968 + j, i);
1195                         nv_wr32(pfb, 0x10f900 + j, train->type04.data[i]);
1196                 }
1197         }
1198
1199         return 0;
1200 }
1201
1202 static int
1203 nve0_ram_train_init(struct nouveau_fb *pfb)
1204 {
1205         u8 ramcfg = nvbios_ramcfg_index(nv_subdev(pfb));
1206         struct nve0_ram_train *train;
1207         int ret = -ENOMEM, i;
1208
1209         if ((train = kzalloc(sizeof(*train), GFP_KERNEL))) {
1210                 for (i = 0; i < 0x100; i++) {
1211                         ret = nve0_ram_train_type(pfb, i, ramcfg, train);
1212                         if (ret && ret != -ENOENT)
1213                                 break;
1214                 }
1215         }
1216
1217         switch (pfb->ram->type) {
1218         case NV_MEM_TYPE_GDDR5:
1219                 ret = nve0_ram_train_init_0(pfb, train);
1220                 break;
1221         default:
1222                 ret = 0;
1223                 break;
1224         }
1225
1226         kfree(train);
1227         return ret;
1228 }
1229
1230 int
1231 nve0_ram_init(struct nouveau_object *object)
1232 {
1233         struct nouveau_fb *pfb = (void *)object->parent;
1234         struct nve0_ram *ram   = (void *)object;
1235         struct nouveau_bios *bios = nouveau_bios(pfb);
1236         u8  ver, hdr, cnt, len, snr, ssz;
1237         u32 data, save;
1238         int ret, i;
1239
1240         ret = nouveau_ram_init(&ram->base);
1241         if (ret)
1242                 return ret;
1243
1244         /* run a bunch of tables from rammap table.  there's actually
1245          * individual pointers for each rammap entry too, but, nvidia
1246          * seem to just run the last two entries' scripts early on in
1247          * their init, and never again.. we'll just run 'em all once
1248          * for now.
1249          *
1250          * i strongly suspect that each script is for a separate mode
1251          * (likely selected by 0x10f65c's lower bits?), and the
1252          * binary driver skips the one that's already been setup by
1253          * the init tables.
1254          */
1255         data = nvbios_rammapTe(bios, &ver, &hdr, &cnt, &len, &snr, &ssz);
1256         if (!data || hdr < 0x15)
1257                 return -EINVAL;
1258
1259         cnt  = nv_ro08(bios, data + 0x14); /* guess at count */
1260         data = nv_ro32(bios, data + 0x10); /* guess u32... */
1261         save = nv_rd32(pfb, 0x10f65c) & 0x000000f0;
1262         for (i = 0; i < cnt; i++, data += 4) {
1263                 if (i != save >> 4) {
1264                         nv_mask(pfb, 0x10f65c, 0x000000f0, i << 4);
1265                         nvbios_exec(&(struct nvbios_init) {
1266                                         .subdev = nv_subdev(pfb),
1267                                         .bios = bios,
1268                                         .offset = nv_ro32(bios, data),
1269                                         .execute = 1,
1270                                     });
1271                 }
1272         }
1273         nv_mask(pfb, 0x10f65c, 0x000000f0, save);
1274         nv_mask(pfb, 0x10f584, 0x11000000, 0x00000000);
1275         nv_wr32(pfb, 0x10ecc0, 0xffffffff);
1276         nv_mask(pfb, 0x10f160, 0x00000010, 0x00000010);
1277
1278         return nve0_ram_train_init(pfb);
1279 }
1280
1281 static int
1282 nve0_ram_ctor_data(struct nve0_ram *ram, u8 ramcfg, int i)
1283 {
1284         struct nouveau_fb *pfb = (void *)nv_object(ram)->parent;
1285         struct nouveau_bios *bios = nouveau_bios(pfb);
1286         struct nouveau_ram_data *cfg;
1287         struct nvbios_ramcfg *d = &ram->diff;
1288         struct nvbios_ramcfg *p, *n;
1289         u8  ver, hdr, cnt, len;
1290         u32 data;
1291         int ret;
1292
1293         if (!(cfg = kmalloc(sizeof(*cfg), GFP_KERNEL)))
1294                 return -ENOMEM;
1295         p = &list_last_entry(&ram->cfg, typeof(*cfg), head)->bios;
1296         n = &cfg->bios;
1297
1298         /* memory config data for a range of target frequencies */
1299         data = nvbios_rammapEp(bios, i, &ver, &hdr, &cnt, &len, &cfg->bios);
1300         if (ret = -ENOENT, !data)
1301                 goto done;
1302         if (ret = -ENOSYS, ver != 0x11 || hdr < 0x12)
1303                 goto done;
1304
1305         /* ... and a portion specific to the attached memory */
1306         data = nvbios_rammapSp(bios, data, ver, hdr, cnt, len, ramcfg,
1307                                &ver, &hdr, &cfg->bios);
1308         if (ret = -EINVAL, !data)
1309                 goto done;
1310         if (ret = -ENOSYS, ver != 0x11 || hdr < 0x0a)
1311                 goto done;
1312
1313         /* lookup memory timings, if bios says they're present */
1314         if (cfg->bios.ramcfg_timing != 0xff) {
1315                 data = nvbios_timingEp(bios, cfg->bios.ramcfg_timing,
1316                                        &ver, &hdr, &cnt, &len,
1317                                        &cfg->bios);
1318                 if (ret = -EINVAL, !data)
1319                         goto done;
1320                 if (ret = -ENOSYS, ver != 0x20 || hdr < 0x33)
1321                         goto done;
1322         }
1323
1324         list_add_tail(&cfg->head, &ram->cfg);
1325         if (ret = 0, i == 0)
1326                 goto done;
1327
1328         d->ramcfg_11_01_01 |= p->ramcfg_11_01_01 != n->ramcfg_11_01_01;
1329         d->ramcfg_11_01_02 |= p->ramcfg_11_01_02 != n->ramcfg_11_01_02;
1330         d->ramcfg_11_01_10 |= p->ramcfg_11_01_10 != n->ramcfg_11_01_10;
1331         d->ramcfg_11_02_03 |= p->ramcfg_11_02_03 != n->ramcfg_11_02_03;
1332         d->ramcfg_11_08_20 |= p->ramcfg_11_08_20 != n->ramcfg_11_08_20;
1333         d->timing_20_30_07 |= p->timing_20_30_07 != n->timing_20_30_07;
1334 done:
1335         if (ret)
1336                 kfree(cfg);
1337         return ret;
1338 }
1339
1340 static void
1341 nve0_ram_dtor(struct nouveau_object *object)
1342 {
1343         struct nve0_ram *ram = (void *)object;
1344         struct nouveau_ram_data *cfg, *tmp;
1345
1346         list_for_each_entry_safe(cfg, tmp, &ram->cfg, head) {
1347                 kfree(cfg);
1348         }
1349
1350         nouveau_ram_destroy(&ram->base);
1351 }
1352
1353 static int
1354 nve0_ram_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
1355               struct nouveau_oclass *oclass, void *data, u32 size,
1356               struct nouveau_object **pobject)
1357 {
1358         struct nouveau_fb *pfb = nouveau_fb(parent);
1359         struct nouveau_bios *bios = nouveau_bios(pfb);
1360         struct nouveau_gpio *gpio = nouveau_gpio(pfb);
1361         struct dcb_gpio_func func;
1362         struct nve0_ram *ram;
1363         int ret, i;
1364         u8  ramcfg = nvbios_ramcfg_index(nv_subdev(pfb));
1365         u32 tmp;
1366
1367         ret = nvc0_ram_create(parent, engine, oclass, 0x022554, &ram);
1368         *pobject = nv_object(ram);
1369         if (ret)
1370                 return ret;
1371
1372         INIT_LIST_HEAD(&ram->cfg);
1373
1374         switch (ram->base.type) {
1375         case NV_MEM_TYPE_DDR3:
1376         case NV_MEM_TYPE_GDDR5:
1377                 ram->base.calc = nve0_ram_calc;
1378                 ram->base.prog = nve0_ram_prog;
1379                 ram->base.tidy = nve0_ram_tidy;
1380                 break;
1381         default:
1382                 nv_warn(pfb, "reclocking of this RAM type is unsupported\n");
1383                 break;
1384         }
1385
1386         /* calculate a mask of differently configured memory partitions,
1387          * because, of course reclocking wasn't complicated enough
1388          * already without having to treat some of them differently to
1389          * the others....
1390          */
1391         ram->parts = nv_rd32(pfb, 0x022438);
1392         ram->pmask = nv_rd32(pfb, 0x022554);
1393         ram->pnuts = 0;
1394         for (i = 0, tmp = 0; i < ram->parts; i++) {
1395                 if (!(ram->pmask & (1 << i))) {
1396                         u32 cfg1 = nv_rd32(pfb, 0x110204 + (i * 0x1000));
1397                         if (tmp && tmp != cfg1) {
1398                                 ram->pnuts |= (1 << i);
1399                                 continue;
1400                         }
1401                         tmp = cfg1;
1402                 }
1403         }
1404
1405         /* parse bios data for all rammap table entries up-front, and
1406          * build information on whether certain fields differ between
1407          * any of the entries.
1408          *
1409          * the binary driver appears to completely ignore some fields
1410          * when all entries contain the same value.  at first, it was
1411          * hoped that these were mere optimisations and the bios init
1412          * tables had configured as per the values here, but there is
1413          * evidence now to suggest that this isn't the case and we do
1414          * need to treat this condition as a "don't touch" indicator.
1415          */
1416         for (i = 0; !ret; i++) {
1417                 ret = nve0_ram_ctor_data(ram, ramcfg, i);
1418                 if (ret && ret != -ENOENT) {
1419                         nv_error(pfb, "failed to parse ramcfg data\n");
1420                         return ret;
1421                 }
1422         }
1423
1424         /* parse bios data for both pll's */
1425         ret = nvbios_pll_parse(bios, 0x0c, &ram->fuc.refpll);
1426         if (ret) {
1427                 nv_error(pfb, "mclk refpll data not found\n");
1428                 return ret;
1429         }
1430
1431         ret = nvbios_pll_parse(bios, 0x04, &ram->fuc.mempll);
1432         if (ret) {
1433                 nv_error(pfb, "mclk pll data not found\n");
1434                 return ret;
1435         }
1436
1437         /* lookup memory voltage gpios */
1438         ret = gpio->find(gpio, 0, 0x18, DCB_GPIO_UNUSED, &func);
1439         if (ret == 0) {
1440                 ram->fuc.r_gpioMV = ramfuc_reg(0x00d610 + (func.line * 0x04));
1441                 ram->fuc.r_funcMV[0] = (func.log[0] ^ 2) << 12;
1442                 ram->fuc.r_funcMV[1] = (func.log[1] ^ 2) << 12;
1443         }
1444
1445         ret = gpio->find(gpio, 0, 0x2e, DCB_GPIO_UNUSED, &func);
1446         if (ret == 0) {
1447                 ram->fuc.r_gpio2E = ramfuc_reg(0x00d610 + (func.line * 0x04));
1448                 ram->fuc.r_func2E[0] = (func.log[0] ^ 2) << 12;
1449                 ram->fuc.r_func2E[1] = (func.log[1] ^ 2) << 12;
1450         }
1451
1452         ram->fuc.r_gpiotrig = ramfuc_reg(0x00d604);
1453
1454         ram->fuc.r_0x132020 = ramfuc_reg(0x132020);
1455         ram->fuc.r_0x132028 = ramfuc_reg(0x132028);
1456         ram->fuc.r_0x132024 = ramfuc_reg(0x132024);
1457         ram->fuc.r_0x132030 = ramfuc_reg(0x132030);
1458         ram->fuc.r_0x132034 = ramfuc_reg(0x132034);
1459         ram->fuc.r_0x132000 = ramfuc_reg(0x132000);
1460         ram->fuc.r_0x132004 = ramfuc_reg(0x132004);
1461         ram->fuc.r_0x132040 = ramfuc_reg(0x132040);
1462
1463         ram->fuc.r_0x10f248 = ramfuc_reg(0x10f248);
1464         ram->fuc.r_0x10f290 = ramfuc_reg(0x10f290);
1465         ram->fuc.r_0x10f294 = ramfuc_reg(0x10f294);
1466         ram->fuc.r_0x10f298 = ramfuc_reg(0x10f298);
1467         ram->fuc.r_0x10f29c = ramfuc_reg(0x10f29c);
1468         ram->fuc.r_0x10f2a0 = ramfuc_reg(0x10f2a0);
1469         ram->fuc.r_0x10f2a4 = ramfuc_reg(0x10f2a4);
1470         ram->fuc.r_0x10f2a8 = ramfuc_reg(0x10f2a8);
1471         ram->fuc.r_0x10f2ac = ramfuc_reg(0x10f2ac);
1472         ram->fuc.r_0x10f2cc = ramfuc_reg(0x10f2cc);
1473         ram->fuc.r_0x10f2e8 = ramfuc_reg(0x10f2e8);
1474         ram->fuc.r_0x10f250 = ramfuc_reg(0x10f250);
1475         ram->fuc.r_0x10f24c = ramfuc_reg(0x10f24c);
1476         ram->fuc.r_0x10fec4 = ramfuc_reg(0x10fec4);
1477         ram->fuc.r_0x10fec8 = ramfuc_reg(0x10fec8);
1478         ram->fuc.r_0x10f604 = ramfuc_reg(0x10f604);
1479         ram->fuc.r_0x10f614 = ramfuc_reg(0x10f614);
1480         ram->fuc.r_0x10f610 = ramfuc_reg(0x10f610);
1481         ram->fuc.r_0x100770 = ramfuc_reg(0x100770);
1482         ram->fuc.r_0x100778 = ramfuc_reg(0x100778);
1483         ram->fuc.r_0x10f224 = ramfuc_reg(0x10f224);
1484
1485         ram->fuc.r_0x10f870 = ramfuc_reg(0x10f870);
1486         ram->fuc.r_0x10f698 = ramfuc_reg(0x10f698);
1487         ram->fuc.r_0x10f694 = ramfuc_reg(0x10f694);
1488         ram->fuc.r_0x10f6b8 = ramfuc_reg(0x10f6b8);
1489         ram->fuc.r_0x10f808 = ramfuc_reg(0x10f808);
1490         ram->fuc.r_0x10f670 = ramfuc_reg(0x10f670);
1491         ram->fuc.r_0x10f60c = ramfuc_reg(0x10f60c);
1492         ram->fuc.r_0x10f830 = ramfuc_reg(0x10f830);
1493         ram->fuc.r_0x1373ec = ramfuc_reg(0x1373ec);
1494         ram->fuc.r_0x10f800 = ramfuc_reg(0x10f800);
1495         ram->fuc.r_0x10f82c = ramfuc_reg(0x10f82c);
1496
1497         ram->fuc.r_0x10f978 = ramfuc_reg(0x10f978);
1498         ram->fuc.r_0x10f910 = ramfuc_reg(0x10f910);
1499         ram->fuc.r_0x10f914 = ramfuc_reg(0x10f914);
1500
1501         switch (ram->base.type) {
1502         case NV_MEM_TYPE_GDDR5:
1503                 ram->fuc.r_mr[0] = ramfuc_reg(0x10f300);
1504                 ram->fuc.r_mr[1] = ramfuc_reg(0x10f330);
1505                 ram->fuc.r_mr[2] = ramfuc_reg(0x10f334);
1506                 ram->fuc.r_mr[3] = ramfuc_reg(0x10f338);
1507                 ram->fuc.r_mr[4] = ramfuc_reg(0x10f33c);
1508                 ram->fuc.r_mr[5] = ramfuc_reg(0x10f340);
1509                 ram->fuc.r_mr[6] = ramfuc_reg(0x10f344);
1510                 ram->fuc.r_mr[7] = ramfuc_reg(0x10f348);
1511                 ram->fuc.r_mr[8] = ramfuc_reg(0x10f354);
1512                 ram->fuc.r_mr[15] = ramfuc_reg(0x10f34c);
1513                 break;
1514         case NV_MEM_TYPE_DDR3:
1515                 ram->fuc.r_mr[0] = ramfuc_reg(0x10f300);
1516                 ram->fuc.r_mr[2] = ramfuc_reg(0x10f320);
1517                 break;
1518         default:
1519                 break;
1520         }
1521
1522         ram->fuc.r_0x62c000 = ramfuc_reg(0x62c000);
1523         ram->fuc.r_0x10f200 = ramfuc_reg(0x10f200);
1524         ram->fuc.r_0x10f210 = ramfuc_reg(0x10f210);
1525         ram->fuc.r_0x10f310 = ramfuc_reg(0x10f310);
1526         ram->fuc.r_0x10f314 = ramfuc_reg(0x10f314);
1527         ram->fuc.r_0x10f318 = ramfuc_reg(0x10f318);
1528         ram->fuc.r_0x10f090 = ramfuc_reg(0x10f090);
1529         ram->fuc.r_0x10f69c = ramfuc_reg(0x10f69c);
1530         ram->fuc.r_0x10f824 = ramfuc_reg(0x10f824);
1531         ram->fuc.r_0x1373f0 = ramfuc_reg(0x1373f0);
1532         ram->fuc.r_0x1373f4 = ramfuc_reg(0x1373f4);
1533         ram->fuc.r_0x137320 = ramfuc_reg(0x137320);
1534         ram->fuc.r_0x10f65c = ramfuc_reg(0x10f65c);
1535         ram->fuc.r_0x10f6bc = ramfuc_reg(0x10f6bc);
1536         ram->fuc.r_0x100710 = ramfuc_reg(0x100710);
1537         ram->fuc.r_0x100750 = ramfuc_reg(0x100750);
1538         return 0;
1539 }
1540
1541 struct nouveau_oclass
1542 nve0_ram_oclass = {
1543         .handle = 0,
1544         .ofuncs = &(struct nouveau_ofuncs) {
1545                 .ctor = nve0_ram_ctor,
1546                 .dtor = nve0_ram_dtor,
1547                 .init = nve0_ram_init,
1548                 .fini = _nouveau_ram_fini,
1549         }
1550 };