regmap: flat: Add flat cache type
[cascardo/linux.git] / drivers / gpu / drm / nouveau / core / subdev / fb / nvc0.c
1 /*
2  * Copyright 2012 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/fb.h>
26 #include <subdev/bios.h>
27
28 struct nvc0_fb_priv {
29         struct nouveau_fb base;
30         struct page *r100c10_page;
31         dma_addr_t r100c10;
32 };
33
34 /* 0 = unsupported
35  * 1 = non-compressed
36  * 3 = compressed
37  */
38 static const u8 types[256] = {
39         1, 1, 3, 3, 3, 3, 0, 3, 3, 3, 3, 0, 0, 0, 0, 0,
40         0, 1, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, 0,
41         0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
42         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3,
43         3, 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44         0, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
47         0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 1, 1, 0,
48         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49         0, 0, 0, 3, 3, 3, 3, 1, 1, 1, 1, 0, 0, 0, 0, 0,
50         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
51         3, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3,
52         3, 3, 0, 0, 0, 0, 0, 0, 3, 0, 0, 3, 0, 3, 0, 3,
53         3, 0, 3, 3, 3, 3, 3, 0, 0, 3, 0, 3, 0, 3, 3, 0,
54         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 1, 1, 0
55 };
56
57 static bool
58 nvc0_fb_memtype_valid(struct nouveau_fb *pfb, u32 tile_flags)
59 {
60         u8 memtype = (tile_flags & 0x0000ff00) >> 8;
61         return likely((types[memtype] == 1));
62 }
63
64 static int
65 nvc0_fb_vram_init(struct nouveau_fb *pfb)
66 {
67         struct nouveau_bios *bios = nouveau_bios(pfb);
68         const u32 rsvd_head = ( 256 * 1024) >> 12; /* vga memory */
69         const u32 rsvd_tail = (1024 * 1024) >> 12; /* vbios etc */
70         u32 parts = nv_rd32(pfb, 0x022438);
71         u32 pmask = nv_rd32(pfb, 0x022554);
72         u32 bsize = nv_rd32(pfb, 0x10f20c);
73         u32 offset, length;
74         bool uniform = true;
75         int ret, part;
76
77         nv_debug(pfb, "0x100800: 0x%08x\n", nv_rd32(pfb, 0x100800));
78         nv_debug(pfb, "parts 0x%08x mask 0x%08x\n", parts, pmask);
79
80         pfb->ram.type = nouveau_fb_bios_memtype(bios);
81         pfb->ram.ranks = (nv_rd32(pfb, 0x10f200) & 0x00000004) ? 2 : 1;
82
83         /* read amount of vram attached to each memory controller */
84         for (part = 0; part < parts; part++) {
85                 if (!(pmask & (1 << part))) {
86                         u32 psize = nv_rd32(pfb, 0x11020c + (part * 0x1000));
87                         if (psize != bsize) {
88                                 if (psize < bsize)
89                                         bsize = psize;
90                                 uniform = false;
91                         }
92
93                         nv_debug(pfb, "%d: mem_amount 0x%08x\n", part, psize);
94                         pfb->ram.size += (u64)psize << 20;
95                 }
96         }
97
98         /* if all controllers have the same amount attached, there's no holes */
99         if (uniform) {
100                 offset = rsvd_head;
101                 length = (pfb->ram.size >> 12) - rsvd_head - rsvd_tail;
102                 return nouveau_mm_init(&pfb->vram, offset, length, 1);
103         }
104
105         /* otherwise, address lowest common amount from 0GiB */
106         ret = nouveau_mm_init(&pfb->vram, rsvd_head, (bsize << 8) * parts, 1);
107         if (ret)
108                 return ret;
109
110         /* and the rest starting from (8GiB + common_size) */
111         offset = (0x0200000000ULL >> 12) + (bsize << 8);
112         length = (pfb->ram.size >> 12) - (bsize << 8) - rsvd_tail;
113
114         ret = nouveau_mm_init(&pfb->vram, offset, length, 0);
115         if (ret) {
116                 nouveau_mm_fini(&pfb->vram);
117                 return ret;
118         }
119
120         return 0;
121 }
122
123 static int
124 nvc0_fb_vram_new(struct nouveau_fb *pfb, u64 size, u32 align, u32 ncmin,
125                  u32 memtype, struct nouveau_mem **pmem)
126 {
127         struct nouveau_mm *mm = &pfb->vram;
128         struct nouveau_mm_node *r;
129         struct nouveau_mem *mem;
130         int type = (memtype & 0x0ff);
131         int back = (memtype & 0x800);
132         int ret;
133
134         size  >>= 12;
135         align >>= 12;
136         ncmin >>= 12;
137         if (!ncmin)
138                 ncmin = size;
139
140         mem = kzalloc(sizeof(*mem), GFP_KERNEL);
141         if (!mem)
142                 return -ENOMEM;
143
144         INIT_LIST_HEAD(&mem->regions);
145         mem->memtype = type;
146         mem->size = size;
147
148         mutex_lock(&mm->mutex);
149         do {
150                 if (back)
151                         ret = nouveau_mm_tail(mm, 1, size, ncmin, align, &r);
152                 else
153                         ret = nouveau_mm_head(mm, 1, size, ncmin, align, &r);
154                 if (ret) {
155                         mutex_unlock(&mm->mutex);
156                         pfb->ram.put(pfb, &mem);
157                         return ret;
158                 }
159
160                 list_add_tail(&r->rl_entry, &mem->regions);
161                 size -= r->length;
162         } while (size);
163         mutex_unlock(&mm->mutex);
164
165         r = list_first_entry(&mem->regions, struct nouveau_mm_node, rl_entry);
166         mem->offset = (u64)r->offset << 12;
167         *pmem = mem;
168         return 0;
169 }
170
171 static int
172 nvc0_fb_init(struct nouveau_object *object)
173 {
174         struct nvc0_fb_priv *priv = (void *)object;
175         int ret;
176
177         ret = nouveau_fb_init(&priv->base);
178         if (ret)
179                 return ret;
180
181         nv_wr32(priv, 0x100c10, priv->r100c10 >> 8);
182         return 0;
183 }
184
185 static void
186 nvc0_fb_dtor(struct nouveau_object *object)
187 {
188         struct nouveau_device *device = nv_device(object);
189         struct nvc0_fb_priv *priv = (void *)object;
190
191         if (priv->r100c10_page) {
192                 pci_unmap_page(device->pdev, priv->r100c10, PAGE_SIZE,
193                                PCI_DMA_BIDIRECTIONAL);
194                 __free_page(priv->r100c10_page);
195         }
196
197         nouveau_fb_destroy(&priv->base);
198 }
199
200 static int
201 nvc0_fb_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
202              struct nouveau_oclass *oclass, void *data, u32 size,
203              struct nouveau_object **pobject)
204 {
205         struct nouveau_device *device = nv_device(parent);
206         struct nvc0_fb_priv *priv;
207         int ret;
208
209         ret = nouveau_fb_create(parent, engine, oclass, &priv);
210         *pobject = nv_object(priv);
211         if (ret)
212                 return ret;
213
214         priv->base.memtype_valid = nvc0_fb_memtype_valid;
215         priv->base.ram.init = nvc0_fb_vram_init;
216         priv->base.ram.get = nvc0_fb_vram_new;
217         priv->base.ram.put = nv50_fb_vram_del;
218
219         priv->r100c10_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
220         if (!priv->r100c10_page)
221                 return -ENOMEM;
222
223         priv->r100c10 = pci_map_page(device->pdev, priv->r100c10_page, 0,
224                                      PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
225         if (pci_dma_mapping_error(device->pdev, priv->r100c10))
226                 return -EFAULT;
227
228         return nouveau_fb_preinit(&priv->base);
229 }
230
231
232 struct nouveau_oclass
233 nvc0_fb_oclass = {
234         .handle = NV_SUBDEV(FB, 0xc0),
235         .ofuncs = &(struct nouveau_ofuncs) {
236                 .ctor = nvc0_fb_ctor,
237                 .dtor = nvc0_fb_dtor,
238                 .init = nvc0_fb_init,
239                 .fini = _nouveau_fb_fini,
240         },
241 };