drm/nouveau/core: add support for reverse mm allocations
[linux-2.6-block.git] / drivers / gpu / drm / nouveau / core / subdev / fb / nv50_vram.c
1 /*
2  * Copyright 2010 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 "drmP.h"
26 #include "nouveau_drv.h"
27 #include <core/mm.h>
28
29 static int types[0x80] = {
30         1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31         1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0,
32         1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2, 0,
33         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34         1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 0, 0,
35         0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36         1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 2, 2, 2, 2,
37         1, 0, 2, 0, 1, 0, 2, 0, 1, 1, 2, 2, 1, 1, 0, 0
38 };
39
40 bool
41 nv50_vram_flags_valid(struct drm_device *dev, u32 tile_flags)
42 {
43         int type = (tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK) >> 8;
44
45         if (likely(type < ARRAY_SIZE(types) && types[type]))
46                 return true;
47         return false;
48 }
49
50 void
51 nv50_vram_del(struct drm_device *dev, struct nouveau_mem **pmem)
52 {
53         struct drm_nouveau_private *dev_priv = dev->dev_private;
54         struct nouveau_mm *mm = &dev_priv->engine.vram.mm;
55         struct nouveau_mm_node *this;
56         struct nouveau_mem *mem;
57
58         mem = *pmem;
59         *pmem = NULL;
60         if (unlikely(mem == NULL))
61                 return;
62
63         mutex_lock(&mm->mutex);
64         while (!list_empty(&mem->regions)) {
65                 this = list_first_entry(&mem->regions, struct nouveau_mm_node, rl_entry);
66
67                 list_del(&this->rl_entry);
68                 nouveau_mm_free(mm, &this);
69         }
70
71         if (mem->tag) {
72                 drm_mm_put_block(mem->tag);
73                 mem->tag = NULL;
74         }
75         mutex_unlock(&mm->mutex);
76
77         kfree(mem);
78 }
79
80 int
81 nv50_vram_new(struct drm_device *dev, u64 size, u32 align, u32 ncmin,
82               u32 memtype, struct nouveau_mem **pmem)
83 {
84         struct drm_nouveau_private *dev_priv = dev->dev_private;
85         struct nouveau_mm *mm = &dev_priv->engine.vram.mm;
86         struct nouveau_mm_node *r;
87         struct nouveau_mem *mem;
88         int comp = (memtype & 0x300) >> 8;
89         int type = (memtype & 0x07f);
90         int back = (memtype & 0x800);
91         int ret;
92
93         size >>= 12;
94         align >>= 12;
95         ncmin >>= 12;
96         if (!ncmin)
97                 ncmin = size;
98
99         mem = kzalloc(sizeof(*mem), GFP_KERNEL);
100         if (!mem)
101                 return -ENOMEM;
102
103         mutex_lock(&mm->mutex);
104         if (comp) {
105                 if (align == 16) {
106                         struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
107                         int n = (size >> 4) * comp;
108
109                         mem->tag = drm_mm_search_free(&pfb->tag_heap, n, 0, 0);
110                         if (mem->tag)
111                                 mem->tag = drm_mm_get_block(mem->tag, n, 0);
112                 }
113
114                 if (unlikely(!mem->tag))
115                         comp = 0;
116         }
117
118         INIT_LIST_HEAD(&mem->regions);
119         mem->dev = dev_priv->dev;
120         mem->memtype = (comp << 7) | type;
121         mem->size = size;
122
123         type = types[type];
124         do {
125                 if (back)
126                         ret = nouveau_mm_tail(mm, type, size, ncmin, align, &r);
127                 else
128                         ret = nouveau_mm_head(mm, type, size, ncmin, align, &r);
129
130                 if (ret) {
131                         mutex_unlock(&mm->mutex);
132                         nv50_vram_del(dev, &mem);
133                         return ret;
134                 }
135
136                 list_add_tail(&r->rl_entry, &mem->regions);
137                 size -= r->length;
138         } while (size);
139         mutex_unlock(&mm->mutex);
140
141         r = list_first_entry(&mem->regions, struct nouveau_mm_node, rl_entry);
142         mem->offset = (u64)r->offset << 12;
143         *pmem = mem;
144         return 0;
145 }
146
147 static u32
148 nv50_vram_rblock(struct drm_device *dev)
149 {
150         struct drm_nouveau_private *dev_priv = dev->dev_private;
151         int i, parts, colbits, rowbitsa, rowbitsb, banks;
152         u64 rowsize, predicted;
153         u32 r0, r4, rt, ru, rblock_size;
154
155         r0 = nv_rd32(dev, 0x100200);
156         r4 = nv_rd32(dev, 0x100204);
157         rt = nv_rd32(dev, 0x100250);
158         ru = nv_rd32(dev, 0x001540);
159         NV_DEBUG(dev, "memcfg 0x%08x 0x%08x 0x%08x 0x%08x\n", r0, r4, rt, ru);
160
161         for (i = 0, parts = 0; i < 8; i++) {
162                 if (ru & (0x00010000 << i))
163                         parts++;
164         }
165
166         colbits  =  (r4 & 0x0000f000) >> 12;
167         rowbitsa = ((r4 & 0x000f0000) >> 16) + 8;
168         rowbitsb = ((r4 & 0x00f00000) >> 20) + 8;
169         banks    = 1 << (((r4 & 0x03000000) >> 24) + 2);
170
171         rowsize = parts * banks * (1 << colbits) * 8;
172         predicted = rowsize << rowbitsa;
173         if (r0 & 0x00000004)
174                 predicted += rowsize << rowbitsb;
175
176         if (predicted != dev_priv->vram_size) {
177                 NV_WARN(dev, "memory controller reports %dMiB VRAM\n",
178                         (u32)(dev_priv->vram_size >> 20));
179                 NV_WARN(dev, "we calculated %dMiB VRAM\n",
180                         (u32)(predicted >> 20));
181         }
182
183         rblock_size = rowsize;
184         if (rt & 1)
185                 rblock_size *= 3;
186
187         NV_DEBUG(dev, "rblock %d bytes\n", rblock_size);
188         return rblock_size;
189 }
190
191 int
192 nv50_vram_init(struct drm_device *dev)
193 {
194         struct drm_nouveau_private *dev_priv = dev->dev_private;
195         struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
196         const u32 rsvd_head = ( 256 * 1024) >> 12; /* vga memory */
197         const u32 rsvd_tail = (1024 * 1024) >> 12; /* vbios etc */
198         u32 pfb714 = nv_rd32(dev, 0x100714);
199         u32 rblock, length;
200
201         switch (pfb714 & 0x00000007) {
202         case 0: dev_priv->vram_type = NV_MEM_TYPE_DDR1; break;
203         case 1:
204                 if (nouveau_mem_vbios_type(dev) == NV_MEM_TYPE_DDR3)
205                         dev_priv->vram_type = NV_MEM_TYPE_DDR3;
206                 else
207                         dev_priv->vram_type = NV_MEM_TYPE_DDR2;
208                 break;
209         case 2: dev_priv->vram_type = NV_MEM_TYPE_GDDR3; break;
210         case 3: dev_priv->vram_type = NV_MEM_TYPE_GDDR4; break;
211         case 4: dev_priv->vram_type = NV_MEM_TYPE_GDDR5; break;
212         default:
213                 break;
214         }
215
216         dev_priv->vram_rank_B = !!(nv_rd32(dev, 0x100200) & 0x4);
217         dev_priv->vram_size  = nv_rd32(dev, 0x10020c);
218         dev_priv->vram_size |= (dev_priv->vram_size & 0xff) << 32;
219         dev_priv->vram_size &= 0xffffffff00ULL;
220
221         /* IGPs, no funky reordering happens here, they don't have VRAM */
222         if (dev_priv->chipset == 0xaa ||
223             dev_priv->chipset == 0xac ||
224             dev_priv->chipset == 0xaf) {
225                 dev_priv->vram_sys_base = (u64)nv_rd32(dev, 0x100e10) << 12;
226                 rblock = 4096 >> 12;
227         } else {
228                 rblock = nv50_vram_rblock(dev) >> 12;
229         }
230
231         length = (dev_priv->vram_size >> 12) - rsvd_head - rsvd_tail;
232
233         return nouveau_mm_init(&vram->mm, rsvd_head, length, rblock);
234 }
235
236 void
237 nv50_vram_fini(struct drm_device *dev)
238 {
239         struct drm_nouveau_private *dev_priv = dev->dev_private;
240         struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
241
242         nouveau_mm_fini(&vram->mm);
243 }