Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[cascardo/linux.git] / drivers / gpu / drm / nouveau / core / subdev / fb / ramnv50.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/perf.h>
29 #include <subdev/bios/timing.h>
30 #include <subdev/clock/pll.h>
31 #include <subdev/fb.h>
32
33 #include <core/option.h>
34 #include <core/mm.h>
35
36 #include "ramseq.h"
37
38 #include "nv50.h"
39
40 struct nv50_ramseq {
41         struct hwsq base;
42         struct hwsq_reg r_0x002504;
43         struct hwsq_reg r_0x004008;
44         struct hwsq_reg r_0x00400c;
45         struct hwsq_reg r_0x00c040;
46         struct hwsq_reg r_0x100210;
47         struct hwsq_reg r_0x1002d0;
48         struct hwsq_reg r_0x1002d4;
49         struct hwsq_reg r_0x1002dc;
50         struct hwsq_reg r_0x100da0[8];
51         struct hwsq_reg r_0x100e20;
52         struct hwsq_reg r_0x100e24;
53         struct hwsq_reg r_0x611200;
54         struct hwsq_reg r_timing[9];
55         struct hwsq_reg r_mr[4];
56 };
57
58 struct nv50_ram {
59         struct nouveau_ram base;
60         struct nv50_ramseq hwsq;
61 };
62
63 #define QFX5800NVA0 1
64
65 static int
66 nv50_ram_calc(struct nouveau_fb *pfb, u32 freq)
67 {
68         struct nouveau_bios *bios = nouveau_bios(pfb);
69         struct nv50_ram *ram = (void *)pfb->ram;
70         struct nv50_ramseq *hwsq = &ram->hwsq;
71         struct nvbios_perfE perfE;
72         struct nvbios_pll mpll;
73         struct bit_entry M;
74         struct {
75                 u32 data;
76                 u8  size;
77         } ramcfg, timing;
78         u8  ver, hdr, cnt, strap;
79         u32 data;
80         int N1, M1, N2, M2, P;
81         int ret, i;
82
83         /* lookup closest matching performance table entry for frequency */
84         i = 0;
85         do {
86                 ramcfg.data = nvbios_perfEp(bios, i++, &ver, &hdr, &cnt,
87                                            &ramcfg.size, &perfE);
88                 if (!ramcfg.data || (ver < 0x25 || ver >= 0x40) ||
89                     (ramcfg.size < 2)) {
90                         nv_error(pfb, "invalid/missing perftab entry\n");
91                         return -EINVAL;
92                 }
93         } while (perfE.memory < freq);
94
95         /* locate specific data set for the attached memory */
96         if (bit_entry(bios, 'M', &M) || M.version != 1 || M.length < 5) {
97                 nv_error(pfb, "invalid/missing memory table\n");
98                 return -EINVAL;
99         }
100
101         strap = (nv_rd32(pfb, 0x101000) & 0x0000003c) >> 2;
102         data = nv_ro16(bios, M.offset + 3);
103         if (data)
104                 strap = nv_ro08(bios, data + strap);
105
106         if (strap >= cnt) {
107                 nv_error(pfb, "invalid ramcfg strap\n");
108                 return -EINVAL;
109         }
110
111         ramcfg.data += hdr + (strap * ramcfg.size);
112
113         /* lookup memory timings, if bios says they're present */
114         strap = nv_ro08(bios, ramcfg.data + 0x01);
115         if (strap != 0xff) {
116                 timing.data = nvbios_timing_entry(bios, strap, &ver, &hdr);
117                 if (!timing.data || ver != 0x10 || hdr < 0x12) {
118                         nv_error(pfb, "invalid/missing timing entry "
119                                  "%02x %04x %02x %02x\n",
120                                  strap, timing.data, ver, hdr);
121                         return -EINVAL;
122                 }
123         } else {
124                 timing.data = 0;
125         }
126
127         ret = ram_init(hwsq, nv_subdev(pfb));
128         if (ret)
129                 return ret;
130
131         ram_wait(hwsq, 0x01, 0x00); /* wait for !vblank */
132         ram_wait(hwsq, 0x01, 0x01); /* wait for vblank */
133         ram_wr32(hwsq, 0x611200, 0x00003300);
134         ram_wr32(hwsq, 0x002504, 0x00000001); /* block fifo */
135         ram_nsec(hwsq, 8000);
136         ram_setf(hwsq, 0x10, 0x00); /* disable fb */
137         ram_wait(hwsq, 0x00, 0x01); /* wait for fb disabled */
138
139         ram_wr32(hwsq, 0x1002d4, 0x00000001); /* precharge */
140         ram_wr32(hwsq, 0x1002d0, 0x00000001); /* refresh */
141         ram_wr32(hwsq, 0x1002d0, 0x00000001); /* refresh */
142         ram_wr32(hwsq, 0x100210, 0x00000000); /* disable auto-refresh */
143         ram_wr32(hwsq, 0x1002dc, 0x00000001); /* enable self-refresh */
144
145         ret = nvbios_pll_parse(bios, 0x004008, &mpll);
146         mpll.vco2.max_freq = 0;
147         if (ret == 0) {
148                 ret = nv04_pll_calc(nv_subdev(pfb), &mpll, freq,
149                                    &N1, &M1, &N2, &M2, &P);
150                 if (ret == 0)
151                         ret = -EINVAL;
152         }
153
154         if (ret < 0)
155                 return ret;
156
157         ram_mask(hwsq, 0x00c040, 0xc000c000, 0x0000c000);
158         ram_mask(hwsq, 0x004008, 0x00000200, 0x00000200);
159         ram_mask(hwsq, 0x00400c, 0x0000ffff, (N1 << 8) | M1);
160         ram_mask(hwsq, 0x004008, 0x81ff0000, 0x80000000 | (mpll.bias_p << 19) |
161                                              (P << 22) | (P << 16));
162 #if QFX5800NVA0
163         for (i = 0; i < 8; i++)
164                 ram_mask(hwsq, 0x100da0[i], 0x00000000, 0x00000000); /*XXX*/
165 #endif
166         ram_nsec(hwsq, 96000); /*XXX*/
167         ram_mask(hwsq, 0x004008, 0x00002200, 0x00002000);
168
169         ram_wr32(hwsq, 0x1002dc, 0x00000000); /* disable self-refresh */
170         ram_wr32(hwsq, 0x100210, 0x80000000); /* enable auto-refresh */
171
172         ram_nsec(hwsq, 12000);
173
174         switch (ram->base.type) {
175         case NV_MEM_TYPE_DDR2:
176                 ram_nuke(hwsq, mr[0]); /* force update */
177                 ram_mask(hwsq, mr[0], 0x000, 0x000);
178                 break;
179         case NV_MEM_TYPE_GDDR3:
180                 ram_mask(hwsq, mr[2], 0x000, 0x000);
181                 ram_nuke(hwsq, mr[0]); /* force update */
182                 ram_mask(hwsq, mr[0], 0x000, 0x000);
183                 break;
184         default:
185                 break;
186         }
187
188         ram_mask(hwsq, timing[3], 0x00000000, 0x00000000); /*XXX*/
189         ram_mask(hwsq, timing[1], 0x00000000, 0x00000000); /*XXX*/
190         ram_mask(hwsq, timing[6], 0x00000000, 0x00000000); /*XXX*/
191         ram_mask(hwsq, timing[7], 0x00000000, 0x00000000); /*XXX*/
192         ram_mask(hwsq, timing[8], 0x00000000, 0x00000000); /*XXX*/
193         ram_mask(hwsq, timing[0], 0x00000000, 0x00000000); /*XXX*/
194         ram_mask(hwsq, timing[2], 0x00000000, 0x00000000); /*XXX*/
195         ram_mask(hwsq, timing[4], 0x00000000, 0x00000000); /*XXX*/
196         ram_mask(hwsq, timing[5], 0x00000000, 0x00000000); /*XXX*/
197
198         ram_mask(hwsq, timing[0], 0x00000000, 0x00000000); /*XXX*/
199
200 #if QFX5800NVA0
201         ram_nuke(hwsq, 0x100e24);
202         ram_mask(hwsq, 0x100e24, 0x00000000, 0x00000000);
203         ram_nuke(hwsq, 0x100e20);
204         ram_mask(hwsq, 0x100e20, 0x00000000, 0x00000000);
205 #endif
206
207         ram_mask(hwsq, mr[0], 0x100, 0x100);
208         ram_mask(hwsq, mr[0], 0x100, 0x000);
209
210         ram_setf(hwsq, 0x10, 0x01); /* enable fb */
211         ram_wait(hwsq, 0x00, 0x00); /* wait for fb enabled */
212         ram_wr32(hwsq, 0x611200, 0x00003330);
213         ram_wr32(hwsq, 0x002504, 0x00000000); /* un-block fifo */
214         return 0;
215 }
216
217 static int
218 nv50_ram_prog(struct nouveau_fb *pfb)
219 {
220         struct nouveau_device *device = nv_device(pfb);
221         struct nv50_ram *ram = (void *)pfb->ram;
222         struct nv50_ramseq *hwsq = &ram->hwsq;
223
224         ram_exec(hwsq, nouveau_boolopt(device->cfgopt, "NvMemExec", false));
225         return 0;
226 }
227
228 static void
229 nv50_ram_tidy(struct nouveau_fb *pfb)
230 {
231         struct nv50_ram *ram = (void *)pfb->ram;
232         struct nv50_ramseq *hwsq = &ram->hwsq;
233         ram_exec(hwsq, false);
234 }
235
236 void
237 __nv50_ram_put(struct nouveau_fb *pfb, struct nouveau_mem *mem)
238 {
239         struct nouveau_mm_node *this;
240
241         while (!list_empty(&mem->regions)) {
242                 this = list_first_entry(&mem->regions, typeof(*this), rl_entry);
243
244                 list_del(&this->rl_entry);
245                 nouveau_mm_free(&pfb->vram, &this);
246         }
247
248         nouveau_mm_free(&pfb->tags, &mem->tag);
249 }
250
251 void
252 nv50_ram_put(struct nouveau_fb *pfb, struct nouveau_mem **pmem)
253 {
254         struct nouveau_mem *mem = *pmem;
255
256         *pmem = NULL;
257         if (unlikely(mem == NULL))
258                 return;
259
260         mutex_lock(&pfb->base.mutex);
261         __nv50_ram_put(pfb, mem);
262         mutex_unlock(&pfb->base.mutex);
263
264         kfree(mem);
265 }
266
267 int
268 nv50_ram_get(struct nouveau_fb *pfb, u64 size, u32 align, u32 ncmin,
269              u32 memtype, struct nouveau_mem **pmem)
270 {
271         struct nouveau_mm *heap = &pfb->vram;
272         struct nouveau_mm *tags = &pfb->tags;
273         struct nouveau_mm_node *r;
274         struct nouveau_mem *mem;
275         int comp = (memtype & 0x300) >> 8;
276         int type = (memtype & 0x07f);
277         int back = (memtype & 0x800);
278         int min, max, ret;
279
280         max = (size >> 12);
281         min = ncmin ? (ncmin >> 12) : max;
282         align >>= 12;
283
284         mem = kzalloc(sizeof(*mem), GFP_KERNEL);
285         if (!mem)
286                 return -ENOMEM;
287
288         mutex_lock(&pfb->base.mutex);
289         if (comp) {
290                 if (align == 16) {
291                         int n = (max >> 4) * comp;
292
293                         ret = nouveau_mm_head(tags, 1, n, n, 1, &mem->tag);
294                         if (ret)
295                                 mem->tag = NULL;
296                 }
297
298                 if (unlikely(!mem->tag))
299                         comp = 0;
300         }
301
302         INIT_LIST_HEAD(&mem->regions);
303         mem->memtype = (comp << 7) | type;
304         mem->size = max;
305
306         type = nv50_fb_memtype[type];
307         do {
308                 if (back)
309                         ret = nouveau_mm_tail(heap, type, max, min, align, &r);
310                 else
311                         ret = nouveau_mm_head(heap, type, max, min, align, &r);
312                 if (ret) {
313                         mutex_unlock(&pfb->base.mutex);
314                         pfb->ram->put(pfb, &mem);
315                         return ret;
316                 }
317
318                 list_add_tail(&r->rl_entry, &mem->regions);
319                 max -= r->length;
320         } while (max);
321         mutex_unlock(&pfb->base.mutex);
322
323         r = list_first_entry(&mem->regions, struct nouveau_mm_node, rl_entry);
324         mem->offset = (u64)r->offset << 12;
325         *pmem = mem;
326         return 0;
327 }
328
329 static u32
330 nv50_fb_vram_rblock(struct nouveau_fb *pfb, struct nouveau_ram *ram)
331 {
332         int i, parts, colbits, rowbitsa, rowbitsb, banks;
333         u64 rowsize, predicted;
334         u32 r0, r4, rt, ru, rblock_size;
335
336         r0 = nv_rd32(pfb, 0x100200);
337         r4 = nv_rd32(pfb, 0x100204);
338         rt = nv_rd32(pfb, 0x100250);
339         ru = nv_rd32(pfb, 0x001540);
340         nv_debug(pfb, "memcfg 0x%08x 0x%08x 0x%08x 0x%08x\n", r0, r4, rt, ru);
341
342         for (i = 0, parts = 0; i < 8; i++) {
343                 if (ru & (0x00010000 << i))
344                         parts++;
345         }
346
347         colbits  =  (r4 & 0x0000f000) >> 12;
348         rowbitsa = ((r4 & 0x000f0000) >> 16) + 8;
349         rowbitsb = ((r4 & 0x00f00000) >> 20) + 8;
350         banks    = 1 << (((r4 & 0x03000000) >> 24) + 2);
351
352         rowsize = parts * banks * (1 << colbits) * 8;
353         predicted = rowsize << rowbitsa;
354         if (r0 & 0x00000004)
355                 predicted += rowsize << rowbitsb;
356
357         if (predicted != ram->size) {
358                 nv_warn(pfb, "memory controller reports %d MiB VRAM\n",
359                         (u32)(ram->size >> 20));
360         }
361
362         rblock_size = rowsize;
363         if (rt & 1)
364                 rblock_size *= 3;
365
366         nv_debug(pfb, "rblock %d bytes\n", rblock_size);
367         return rblock_size;
368 }
369
370 int
371 nv50_ram_create_(struct nouveau_object *parent, struct nouveau_object *engine,
372                  struct nouveau_oclass *oclass, int length, void **pobject)
373 {
374         const u32 rsvd_head = ( 256 * 1024) >> 12; /* vga memory */
375         const u32 rsvd_tail = (1024 * 1024) >> 12; /* vbios etc */
376         struct nouveau_bios *bios = nouveau_bios(parent);
377         struct nouveau_fb *pfb = nouveau_fb(parent);
378         struct nouveau_ram *ram;
379         int ret;
380
381         ret = nouveau_ram_create_(parent, engine, oclass, length, pobject);
382         ram = *pobject;
383         if (ret)
384                 return ret;
385
386         ram->size = nv_rd32(pfb, 0x10020c);
387         ram->size = (ram->size & 0xffffff00) | ((ram->size & 0x000000ff) << 32);
388
389         switch (nv_rd32(pfb, 0x100714) & 0x00000007) {
390         case 0: ram->type = NV_MEM_TYPE_DDR1; break;
391         case 1:
392                 if (nouveau_fb_bios_memtype(bios) == NV_MEM_TYPE_DDR3)
393                         ram->type = NV_MEM_TYPE_DDR3;
394                 else
395                         ram->type = NV_MEM_TYPE_DDR2;
396                 break;
397         case 2: ram->type = NV_MEM_TYPE_GDDR3; break;
398         case 3: ram->type = NV_MEM_TYPE_GDDR4; break;
399         case 4: ram->type = NV_MEM_TYPE_GDDR5; break;
400         default:
401                 break;
402         }
403
404         ret = nouveau_mm_init(&pfb->vram, rsvd_head, (ram->size >> 12) -
405                               (rsvd_head + rsvd_tail),
406                               nv50_fb_vram_rblock(pfb, ram) >> 12);
407         if (ret)
408                 return ret;
409
410         ram->ranks = (nv_rd32(pfb, 0x100200) & 0x4) ? 2 : 1;
411         ram->tags  =  nv_rd32(pfb, 0x100320);
412         ram->get = nv50_ram_get;
413         ram->put = nv50_ram_put;
414         return 0;
415 }
416
417 static int
418 nv50_ram_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
419               struct nouveau_oclass *oclass, void *data, u32 datasize,
420               struct nouveau_object **pobject)
421 {
422         struct nv50_ram *ram;
423         int ret, i;
424
425         ret = nv50_ram_create(parent, engine, oclass, &ram);
426         *pobject = nv_object(ram);
427         if (ret)
428                 return ret;
429
430         switch (ram->base.type) {
431         case NV_MEM_TYPE_DDR2:
432         case NV_MEM_TYPE_GDDR3:
433                 ram->base.calc = nv50_ram_calc;
434                 ram->base.prog = nv50_ram_prog;
435                 ram->base.tidy = nv50_ram_tidy;
436                 break;
437         default:
438                 nv_warn(ram, "reclocking of this ram type unsupported\n");
439                 return 0;
440         }
441
442         ram->hwsq.r_0x002504 = hwsq_reg(0x002504);
443         ram->hwsq.r_0x00c040 = hwsq_reg(0x00c040);
444         ram->hwsq.r_0x004008 = hwsq_reg(0x004008);
445         ram->hwsq.r_0x00400c = hwsq_reg(0x00400c);
446         ram->hwsq.r_0x100210 = hwsq_reg(0x100210);
447         ram->hwsq.r_0x1002d0 = hwsq_reg(0x1002d0);
448         ram->hwsq.r_0x1002d4 = hwsq_reg(0x1002d4);
449         ram->hwsq.r_0x1002dc = hwsq_reg(0x1002dc);
450         for (i = 0; i < 8; i++)
451                 ram->hwsq.r_0x100da0[i] = hwsq_reg(0x100da0 + (i * 0x04));
452         ram->hwsq.r_0x100e20 = hwsq_reg(0x100e20);
453         ram->hwsq.r_0x100e24 = hwsq_reg(0x100e24);
454         ram->hwsq.r_0x611200 = hwsq_reg(0x611200);
455
456         for (i = 0; i < 9; i++)
457                 ram->hwsq.r_timing[i] = hwsq_reg(0x100220 + (i * 0x04));
458
459         if (ram->base.ranks > 1) {
460                 ram->hwsq.r_mr[0] = hwsq_reg2(0x1002c0, 0x1002c8);
461                 ram->hwsq.r_mr[1] = hwsq_reg2(0x1002c4, 0x1002cc);
462                 ram->hwsq.r_mr[2] = hwsq_reg2(0x1002e0, 0x1002e8);
463                 ram->hwsq.r_mr[3] = hwsq_reg2(0x1002e4, 0x1002ec);
464         } else {
465                 ram->hwsq.r_mr[0] = hwsq_reg(0x1002c0);
466                 ram->hwsq.r_mr[1] = hwsq_reg(0x1002c4);
467                 ram->hwsq.r_mr[2] = hwsq_reg(0x1002e0);
468                 ram->hwsq.r_mr[3] = hwsq_reg(0x1002e4);
469         }
470
471         return 0;
472 }
473
474 struct nouveau_oclass
475 nv50_ram_oclass = {
476         .ofuncs = &(struct nouveau_ofuncs) {
477                 .ctor = nv50_ram_ctor,
478                 .dtor = _nouveau_ram_dtor,
479                 .init = _nouveau_ram_init,
480                 .fini = _nouveau_ram_fini,
481         }
482 };