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