drm/nouveau: remove some useless GETPARAMs
[cascardo/linux.git] / drivers / gpu / drm / nouveau / nv50_evo.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
27 #include "nouveau_drv.h"
28 #include "nouveau_dma.h"
29 #include "nouveau_ramht.h"
30
31 static void
32 nv50_evo_channel_del(struct nouveau_channel **pevo)
33 {
34         struct drm_nouveau_private *dev_priv;
35         struct nouveau_channel *evo = *pevo;
36
37         if (!evo)
38                 return;
39         *pevo = NULL;
40
41         dev_priv = evo->dev->dev_private;
42         dev_priv->evo_alloc &= ~(1 << evo->id);
43
44         nouveau_gpuobj_channel_takedown(evo);
45         nouveau_bo_unmap(evo->pushbuf_bo);
46         nouveau_bo_ref(NULL, &evo->pushbuf_bo);
47
48         if (evo->user)
49                 iounmap(evo->user);
50
51         kfree(evo);
52 }
53
54 int
55 nv50_evo_dmaobj_new(struct nouveau_channel *evo, u32 class, u32 name,
56                     u32 tile_flags, u32 magic_flags, u32 offset, u32 limit)
57 {
58         struct drm_nouveau_private *dev_priv = evo->dev->dev_private;
59         struct drm_device *dev = evo->dev;
60         struct nouveau_gpuobj *obj = NULL;
61         int ret;
62
63         ret = nouveau_gpuobj_new(dev, dev_priv->evo, 6*4, 32, 0, &obj);
64         if (ret)
65                 return ret;
66         obj->engine = NVOBJ_ENGINE_DISPLAY;
67
68         nv_wo32(obj,  0, (tile_flags << 22) | (magic_flags << 16) | class);
69         nv_wo32(obj,  4, limit);
70         nv_wo32(obj,  8, offset);
71         nv_wo32(obj, 12, 0x00000000);
72         nv_wo32(obj, 16, 0x00000000);
73         if (dev_priv->card_type < NV_C0)
74                 nv_wo32(obj, 20, 0x00010000);
75         else
76                 nv_wo32(obj, 20, 0x00020000);
77         dev_priv->engine.instmem.flush(dev);
78
79         ret = nouveau_ramht_insert(evo, name, obj);
80         nouveau_gpuobj_ref(NULL, &obj);
81         if (ret) {
82                 return ret;
83         }
84
85         return 0;
86 }
87
88 static int
89 nv50_evo_channel_new(struct drm_device *dev, struct nouveau_channel **pevo)
90 {
91         struct drm_nouveau_private *dev_priv = dev->dev_private;
92         struct nouveau_channel *evo;
93         int ret;
94
95         evo = kzalloc(sizeof(struct nouveau_channel), GFP_KERNEL);
96         if (!evo)
97                 return -ENOMEM;
98         *pevo = evo;
99
100         for (evo->id = 0; evo->id < 5; evo->id++) {
101                 if (dev_priv->evo_alloc & (1 << evo->id))
102                         continue;
103
104                 dev_priv->evo_alloc |= (1 << evo->id);
105                 break;
106         }
107
108         if (evo->id == 5) {
109                 kfree(evo);
110                 return -ENODEV;
111         }
112
113         evo->dev = dev;
114         evo->user_get = 4;
115         evo->user_put = 0;
116
117         ret = nouveau_bo_new(dev, NULL, 4096, 0, TTM_PL_FLAG_VRAM, 0, 0,
118                              false, true, &evo->pushbuf_bo);
119         if (ret == 0)
120                 ret = nouveau_bo_pin(evo->pushbuf_bo, TTM_PL_FLAG_VRAM);
121         if (ret) {
122                 NV_ERROR(dev, "Error creating EVO DMA push buffer: %d\n", ret);
123                 nv50_evo_channel_del(pevo);
124                 return ret;
125         }
126
127         ret = nouveau_bo_map(evo->pushbuf_bo);
128         if (ret) {
129                 NV_ERROR(dev, "Error mapping EVO DMA push buffer: %d\n", ret);
130                 nv50_evo_channel_del(pevo);
131                 return ret;
132         }
133
134         evo->user = ioremap(pci_resource_start(dev->pdev, 0) +
135                             NV50_PDISPLAY_USER(evo->id), PAGE_SIZE);
136         if (!evo->user) {
137                 NV_ERROR(dev, "Error mapping EVO control regs.\n");
138                 nv50_evo_channel_del(pevo);
139                 return -ENOMEM;
140         }
141
142         /* bind primary evo channel's ramht to the channel */
143         if (dev_priv->evo && evo != dev_priv->evo)
144                 nouveau_ramht_ref(dev_priv->evo->ramht, &evo->ramht, NULL);
145
146         return 0;
147 }
148
149 static int
150 nv50_evo_channel_init(struct nouveau_channel *evo)
151 {
152         struct drm_device *dev = evo->dev;
153         int id = evo->id, ret, i;
154         u64 pushbuf = evo->pushbuf_bo->bo.mem.start << PAGE_SHIFT;
155         u32 tmp;
156
157         tmp = nv_rd32(dev, NV50_PDISPLAY_EVO_CTRL(id));
158         if ((tmp & 0x009f0000) == 0x00020000)
159                 nv_wr32(dev, NV50_PDISPLAY_EVO_CTRL(id), tmp | 0x00800000);
160
161         tmp = nv_rd32(dev, NV50_PDISPLAY_EVO_CTRL(id));
162         if ((tmp & 0x003f0000) == 0x00030000)
163                 nv_wr32(dev, NV50_PDISPLAY_EVO_CTRL(id), tmp | 0x00600000);
164
165         /* initialise fifo */
166         nv_wr32(dev, NV50_PDISPLAY_EVO_DMA_CB(id), pushbuf >> 8 |
167                      NV50_PDISPLAY_EVO_DMA_CB_LOCATION_VRAM |
168                      NV50_PDISPLAY_EVO_DMA_CB_VALID);
169         nv_wr32(dev, NV50_PDISPLAY_EVO_UNK2(id), 0x00010000);
170         nv_wr32(dev, NV50_PDISPLAY_EVO_HASH_TAG(id), id);
171         nv_mask(dev, NV50_PDISPLAY_EVO_CTRL(id), NV50_PDISPLAY_EVO_CTRL_DMA,
172                      NV50_PDISPLAY_EVO_CTRL_DMA_ENABLED);
173
174         nv_wr32(dev, NV50_PDISPLAY_USER_PUT(id), 0x00000000);
175         nv_wr32(dev, NV50_PDISPLAY_EVO_CTRL(id), 0x01000003 |
176                      NV50_PDISPLAY_EVO_CTRL_DMA_ENABLED);
177         if (!nv_wait(dev, NV50_PDISPLAY_EVO_CTRL(id), 0x80000000, 0x00000000)) {
178                 NV_ERROR(dev, "EvoCh %d init timeout: 0x%08x\n", id,
179                          nv_rd32(dev, NV50_PDISPLAY_EVO_CTRL(id)));
180                 return -EBUSY;
181         }
182
183         /* enable error reporting on the channel */
184         nv_mask(dev, 0x610028, 0x00000000, 0x00010001 << id);
185
186         evo->dma.max = (4096/4) - 2;
187         evo->dma.put = 0;
188         evo->dma.cur = evo->dma.put;
189         evo->dma.free = evo->dma.max - evo->dma.cur;
190
191         ret = RING_SPACE(evo, NOUVEAU_DMA_SKIPS);
192         if (ret)
193                 return ret;
194
195         for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
196                 OUT_RING(evo, 0);
197
198         return 0;
199 }
200
201 static void
202 nv50_evo_channel_fini(struct nouveau_channel *evo)
203 {
204         struct drm_device *dev = evo->dev;
205         int id = evo->id;
206
207         nv_mask(dev, 0x610028, 0x00010001 << id, 0x00000000);
208         nv_mask(dev, NV50_PDISPLAY_EVO_CTRL(id), 0x00001010, 0x00001000);
209         nv_wr32(dev, NV50_PDISPLAY_INTR_0, (1 << id));
210         nv_mask(dev, NV50_PDISPLAY_EVO_CTRL(id), 0x00000003, 0x00000000);
211         if (!nv_wait(dev, NV50_PDISPLAY_EVO_CTRL(id), 0x001e0000, 0x00000000)) {
212                 NV_ERROR(dev, "EvoCh %d takedown timeout: 0x%08x\n", id,
213                          nv_rd32(dev, NV50_PDISPLAY_EVO_CTRL(id)));
214         }
215 }
216
217 static int
218 nv50_evo_create(struct drm_device *dev)
219 {
220         struct drm_nouveau_private *dev_priv = dev->dev_private;
221         struct nouveau_gpuobj *ramht = NULL;
222         struct nouveau_channel *evo;
223         int ret;
224
225         /* create primary evo channel, the one we use for modesetting
226          * purporses
227          */
228         ret = nv50_evo_channel_new(dev, &dev_priv->evo);
229         if (ret)
230                 return ret;
231         evo = dev_priv->evo;
232
233         /* setup object management on it, any other evo channel will
234          * use this also as there's no per-channel support on the
235          * hardware
236          */
237         ret = nouveau_gpuobj_new(dev, NULL, 32768, 65536,
238                                  NVOBJ_FLAG_ZERO_ALLOC, &evo->ramin);
239         if (ret) {
240                 NV_ERROR(dev, "Error allocating EVO channel memory: %d\n", ret);
241                 nv50_evo_channel_del(&dev_priv->evo);
242                 return ret;
243         }
244
245         ret = drm_mm_init(&evo->ramin_heap, 0, 32768);
246         if (ret) {
247                 NV_ERROR(dev, "Error initialising EVO PRAMIN heap: %d\n", ret);
248                 nv50_evo_channel_del(&dev_priv->evo);
249                 return ret;
250         }
251
252         ret = nouveau_gpuobj_new(dev, evo, 4096, 16, 0, &ramht);
253         if (ret) {
254                 NV_ERROR(dev, "Unable to allocate EVO RAMHT: %d\n", ret);
255                 nv50_evo_channel_del(&dev_priv->evo);
256                 return ret;
257         }
258
259         ret = nouveau_ramht_new(dev, ramht, &evo->ramht);
260         nouveau_gpuobj_ref(NULL, &ramht);
261         if (ret) {
262                 nv50_evo_channel_del(&dev_priv->evo);
263                 return ret;
264         }
265
266         /* create some default objects for the scanout memtypes we support */
267         if (dev_priv->chipset != 0x50) {
268                 ret = nv50_evo_dmaobj_new(evo, 0x3d, NvEvoFB16, 0x70, 0x19,
269                                           0, 0xffffffff);
270                 if (ret) {
271                         nv50_evo_channel_del(&dev_priv->evo);
272                         return ret;
273                 }
274
275
276                 ret = nv50_evo_dmaobj_new(evo, 0x3d, NvEvoFB32, 0x7a, 0x19,
277                                           0, 0xffffffff);
278                 if (ret) {
279                         nv50_evo_channel_del(&dev_priv->evo);
280                         return ret;
281                 }
282         }
283
284         ret = nv50_evo_dmaobj_new(evo, 0x3d, NvEvoVRAM, 0, 0x19,
285                                   0, dev_priv->vram_size);
286         if (ret) {
287                 nv50_evo_channel_del(&dev_priv->evo);
288                 return ret;
289         }
290
291         return 0;
292 }
293
294 int
295 nv50_evo_init(struct drm_device *dev)
296 {
297         struct drm_nouveau_private *dev_priv = dev->dev_private;
298         int ret;
299
300         if (!dev_priv->evo) {
301                 ret = nv50_evo_create(dev);
302                 if (ret)
303                         return ret;
304         }
305
306         return nv50_evo_channel_init(dev_priv->evo);
307 }
308
309 void
310 nv50_evo_fini(struct drm_device *dev)
311 {
312         struct drm_nouveau_private *dev_priv = dev->dev_private;
313
314         if (dev_priv->evo) {
315                 nv50_evo_channel_fini(dev_priv->evo);
316                 nv50_evo_channel_del(&dev_priv->evo);
317         }
318 }