]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/gpu/drm/nouveau/core/subdev/fb/ramnvc0.c
Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-beck.git] / drivers / gpu / drm / nouveau / core / subdev / fb / ramnvc0.c
1 /*
2  * Copyright 2013 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #include <subdev/bios.h>
26 #include <subdev/ltcg.h>
27
28 #include "priv.h"
29
30 extern const u8 nvc0_pte_storage_type_map[256];
31
32 void
33 nvc0_ram_put(struct nouveau_fb *pfb, struct nouveau_mem **pmem)
34 {
35         struct nouveau_ltcg *ltcg = nouveau_ltcg(pfb);
36         struct nouveau_mem *mem = *pmem;
37
38         *pmem = NULL;
39         if (unlikely(mem == NULL))
40                 return;
41
42         mutex_lock(&pfb->base.mutex);
43         if (mem->tag)
44                 ltcg->tags_free(ltcg, &mem->tag);
45         __nv50_ram_put(pfb, mem);
46         mutex_unlock(&pfb->base.mutex);
47
48         kfree(mem);
49 }
50
51 int
52 nvc0_ram_get(struct nouveau_fb *pfb, u64 size, u32 align, u32 ncmin,
53              u32 memtype, struct nouveau_mem **pmem)
54 {
55         struct nouveau_mm *mm = &pfb->vram;
56         struct nouveau_mm_node *r;
57         struct nouveau_mem *mem;
58         int type = (memtype & 0x0ff);
59         int back = (memtype & 0x800);
60         const bool comp = nvc0_pte_storage_type_map[type] != type;
61         int ret;
62
63         size  >>= 12;
64         align >>= 12;
65         ncmin >>= 12;
66         if (!ncmin)
67                 ncmin = size;
68
69         mem = kzalloc(sizeof(*mem), GFP_KERNEL);
70         if (!mem)
71                 return -ENOMEM;
72
73         INIT_LIST_HEAD(&mem->regions);
74         mem->size = size;
75
76         mutex_lock(&pfb->base.mutex);
77         if (comp) {
78                 struct nouveau_ltcg *ltcg = nouveau_ltcg(pfb);
79
80                 /* compression only works with lpages */
81                 if (align == (1 << (17 - 12))) {
82                         int n = size >> 5;
83                         ltcg->tags_alloc(ltcg, n, &mem->tag);
84                 }
85
86                 if (unlikely(!mem->tag))
87                         type = nvc0_pte_storage_type_map[type];
88         }
89         mem->memtype = type;
90
91         do {
92                 if (back)
93                         ret = nouveau_mm_tail(mm, 1, size, ncmin, align, &r);
94                 else
95                         ret = nouveau_mm_head(mm, 1, size, ncmin, align, &r);
96                 if (ret) {
97                         mutex_unlock(&pfb->base.mutex);
98                         pfb->ram->put(pfb, &mem);
99                         return ret;
100                 }
101
102                 list_add_tail(&r->rl_entry, &mem->regions);
103                 size -= r->length;
104         } while (size);
105         mutex_unlock(&pfb->base.mutex);
106
107         r = list_first_entry(&mem->regions, struct nouveau_mm_node, rl_entry);
108         mem->offset = (u64)r->offset << 12;
109         *pmem = mem;
110         return 0;
111 }
112
113 static int
114 nvc0_ram_create(struct nouveau_object *parent, struct nouveau_object *engine,
115                 struct nouveau_oclass *oclass, void *data, u32 size,
116                 struct nouveau_object **pobject)
117 {
118         struct nouveau_fb *pfb = nouveau_fb(parent);
119         struct nouveau_bios *bios = nouveau_bios(pfb);
120         struct nouveau_ram *ram;
121         const u32 rsvd_head = ( 256 * 1024) >> 12; /* vga memory */
122         const u32 rsvd_tail = (1024 * 1024) >> 12; /* vbios etc */
123         u32 parts = nv_rd32(pfb, 0x022438);
124         u32 pmask = nv_rd32(pfb, 0x022554);
125         u32 bsize = nv_rd32(pfb, 0x10f20c);
126         u32 offset, length;
127         bool uniform = true;
128         int ret, part;
129
130         ret = nouveau_ram_create(parent, engine, oclass, &ram);
131         *pobject = nv_object(ram);
132         if (ret)
133                 return ret;
134
135         nv_debug(pfb, "0x100800: 0x%08x\n", nv_rd32(pfb, 0x100800));
136         nv_debug(pfb, "parts 0x%08x mask 0x%08x\n", parts, pmask);
137
138         ram->type = nouveau_fb_bios_memtype(bios);
139         ram->ranks = (nv_rd32(pfb, 0x10f200) & 0x00000004) ? 2 : 1;
140
141         /* read amount of vram attached to each memory controller */
142         for (part = 0; part < parts; part++) {
143                 if (!(pmask & (1 << part))) {
144                         u32 psize = nv_rd32(pfb, 0x11020c + (part * 0x1000));
145                         if (psize != bsize) {
146                                 if (psize < bsize)
147                                         bsize = psize;
148                                 uniform = false;
149                         }
150
151                         nv_debug(pfb, "%d: mem_amount 0x%08x\n", part, psize);
152                         ram->size += (u64)psize << 20;
153                 }
154         }
155
156         /* if all controllers have the same amount attached, there's no holes */
157         if (uniform) {
158                 offset = rsvd_head;
159                 length = (ram->size >> 12) - rsvd_head - rsvd_tail;
160                 ret = nouveau_mm_init(&pfb->vram, offset, length, 1);
161         } else {
162                 /* otherwise, address lowest common amount from 0GiB */
163                 ret = nouveau_mm_init(&pfb->vram, rsvd_head,
164                                       (bsize << 8) * parts, 1);
165                 if (ret)
166                         return ret;
167
168                 /* and the rest starting from (8GiB + common_size) */
169                 offset = (0x0200000000ULL >> 12) + (bsize << 8);
170                 length = (ram->size >> 12) - (bsize << 8) - rsvd_tail;
171
172                 ret = nouveau_mm_init(&pfb->vram, offset, length, 0);
173                 if (ret)
174                         nouveau_mm_fini(&pfb->vram);
175         }
176
177         if (ret)
178                 return ret;
179
180         ram->get = nvc0_ram_get;
181         ram->put = nvc0_ram_put;
182         return 0;
183 }
184
185 struct nouveau_oclass
186 nvc0_ram_oclass = {
187         .handle = 0,
188         .ofuncs = &(struct nouveau_ofuncs) {
189                 .ctor = nvc0_ram_create,
190                 .dtor = _nouveau_ram_dtor,
191                 .init = _nouveau_ram_init,
192                 .fini = _nouveau_ram_fini,
193         }
194 };