drm/nouveau: Allocate a per-channel instance of NV_SW.
[safe/jmp/linux-2.6] / drivers / gpu / drm / nouveau / nv04_fbcon.c
1 /*
2  * Copyright 2009 Ben Skeggs
3  * Copyright 2008 Stuart Bennett
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "drmP.h"
26 #include "nouveau_drv.h"
27 #include "nouveau_dma.h"
28 #include "nouveau_fbcon.h"
29
30 static void
31 nv04_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *region)
32 {
33         struct nouveau_fbcon_par *par = info->par;
34         struct drm_device *dev = par->dev;
35         struct drm_nouveau_private *dev_priv = dev->dev_private;
36         struct nouveau_channel *chan = dev_priv->channel;
37
38         if (info->state != FBINFO_STATE_RUNNING)
39                 return;
40
41         if (!(info->flags & FBINFO_HWACCEL_DISABLED) && RING_SPACE(chan, 4)) {
42                 NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
43                 info->flags |= FBINFO_HWACCEL_DISABLED;
44         }
45
46         if (info->flags & FBINFO_HWACCEL_DISABLED) {
47                 cfb_copyarea(info, region);
48                 return;
49         }
50
51         BEGIN_RING(chan, NvSubImageBlit, 0x0300, 3);
52         OUT_RING(chan, (region->sy << 16) | region->sx);
53         OUT_RING(chan, (region->dy << 16) | region->dx);
54         OUT_RING(chan, (region->height << 16) | region->width);
55         FIRE_RING(chan);
56 }
57
58 static void
59 nv04_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
60 {
61         struct nouveau_fbcon_par *par = info->par;
62         struct drm_device *dev = par->dev;
63         struct drm_nouveau_private *dev_priv = dev->dev_private;
64         struct nouveau_channel *chan = dev_priv->channel;
65         uint32_t color = ((uint32_t *) info->pseudo_palette)[rect->color];
66
67         if (info->state != FBINFO_STATE_RUNNING)
68                 return;
69
70         if (!(info->flags & FBINFO_HWACCEL_DISABLED) && RING_SPACE(chan, 7)) {
71                 NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
72                 info->flags |= FBINFO_HWACCEL_DISABLED;
73         }
74
75         if (info->flags & FBINFO_HWACCEL_DISABLED) {
76                 cfb_fillrect(info, rect);
77                 return;
78         }
79
80         BEGIN_RING(chan, NvSubGdiRect, 0x02fc, 1);
81         OUT_RING(chan, (rect->rop != ROP_COPY) ? 1 : 3);
82         BEGIN_RING(chan, NvSubGdiRect, 0x03fc, 1);
83         OUT_RING(chan, color);
84         BEGIN_RING(chan, NvSubGdiRect, 0x0400, 2);
85         OUT_RING(chan, (rect->dx << 16) | rect->dy);
86         OUT_RING(chan, (rect->width << 16) | rect->height);
87         FIRE_RING(chan);
88 }
89
90 static void
91 nv04_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
92 {
93         struct nouveau_fbcon_par *par = info->par;
94         struct drm_device *dev = par->dev;
95         struct drm_nouveau_private *dev_priv = dev->dev_private;
96         struct nouveau_channel *chan = dev_priv->channel;
97         uint32_t fg;
98         uint32_t bg;
99         uint32_t dsize;
100         uint32_t width;
101         uint32_t *data = (uint32_t *)image->data;
102
103         if (info->state != FBINFO_STATE_RUNNING)
104                 return;
105
106         if (image->depth != 1) {
107                 cfb_imageblit(info, image);
108                 return;
109         }
110
111         if (!(info->flags & FBINFO_HWACCEL_DISABLED) && RING_SPACE(chan, 8)) {
112                 NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
113                 info->flags |= FBINFO_HWACCEL_DISABLED;
114         }
115
116         if (info->flags & FBINFO_HWACCEL_DISABLED) {
117                 cfb_imageblit(info, image);
118                 return;
119         }
120
121         width = (image->width + 31) & ~31;
122         dsize = (width * image->height) >> 5;
123
124         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
125             info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
126                 fg = ((uint32_t *) info->pseudo_palette)[image->fg_color];
127                 bg = ((uint32_t *) info->pseudo_palette)[image->bg_color];
128         } else {
129                 fg = image->fg_color;
130                 bg = image->bg_color;
131         }
132
133         BEGIN_RING(chan, NvSubGdiRect, 0x0be4, 7);
134         OUT_RING(chan, (image->dy << 16) | (image->dx & 0xffff));
135         OUT_RING(chan, ((image->dy + image->height) << 16) |
136                          ((image->dx + image->width) & 0xffff));
137         OUT_RING(chan, bg);
138         OUT_RING(chan, fg);
139         OUT_RING(chan, (image->height << 16) | image->width);
140         OUT_RING(chan, (image->height << 16) | width);
141         OUT_RING(chan, (image->dy << 16) | (image->dx & 0xffff));
142
143         while (dsize) {
144                 int iter_len = dsize > 128 ? 128 : dsize;
145
146                 if (RING_SPACE(chan, iter_len + 1)) {
147                         NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
148                         info->flags |= FBINFO_HWACCEL_DISABLED;
149                         cfb_imageblit(info, image);
150                         return;
151                 }
152
153                 BEGIN_RING(chan, NvSubGdiRect, 0x0c00, iter_len);
154                 OUT_RINGp(chan, data, iter_len);
155                 data += iter_len;
156                 dsize -= iter_len;
157         }
158
159         FIRE_RING(chan);
160 }
161
162 static int
163 nv04_fbcon_grobj_new(struct drm_device *dev, int class, uint32_t handle)
164 {
165         struct drm_nouveau_private *dev_priv = dev->dev_private;
166         struct nouveau_gpuobj *obj = NULL;
167         int ret;
168
169         ret = nouveau_gpuobj_gr_new(dev_priv->channel, class, &obj);
170         if (ret)
171                 return ret;
172
173         ret = nouveau_gpuobj_ref_add(dev, dev_priv->channel, handle, obj, NULL);
174         if (ret)
175                 return ret;
176
177         return 0;
178 }
179
180 int
181 nv04_fbcon_accel_init(struct fb_info *info)
182 {
183         struct nouveau_fbcon_par *par = info->par;
184         struct drm_device *dev = par->dev;
185         struct drm_nouveau_private *dev_priv = dev->dev_private;
186         struct nouveau_channel *chan = dev_priv->channel;
187         const int sub = NvSubCtxSurf2D;
188         int surface_fmt, pattern_fmt, rect_fmt;
189         int ret;
190
191         switch (info->var.bits_per_pixel) {
192         case 8:
193                 surface_fmt = 1;
194                 pattern_fmt = 3;
195                 rect_fmt = 3;
196                 break;
197         case 16:
198                 surface_fmt = 4;
199                 pattern_fmt = 1;
200                 rect_fmt = 1;
201                 break;
202         case 32:
203                 switch (info->var.transp.length) {
204                 case 0: /* depth 24 */
205                 case 8: /* depth 32 */
206                         break;
207                 default:
208                         return -EINVAL;
209                 }
210
211                 surface_fmt = 6;
212                 pattern_fmt = 3;
213                 rect_fmt = 3;
214                 break;
215         default:
216                 return -EINVAL;
217         }
218
219         ret = nv04_fbcon_grobj_new(dev, dev_priv->card_type >= NV_10 ?
220                                    0x0062 : 0x0042, NvCtxSurf2D);
221         if (ret)
222                 return ret;
223
224         ret = nv04_fbcon_grobj_new(dev, 0x0019, NvClipRect);
225         if (ret)
226                 return ret;
227
228         ret = nv04_fbcon_grobj_new(dev, 0x0043, NvRop);
229         if (ret)
230                 return ret;
231
232         ret = nv04_fbcon_grobj_new(dev, 0x0044, NvImagePatt);
233         if (ret)
234                 return ret;
235
236         ret = nv04_fbcon_grobj_new(dev, 0x004a, NvGdiRect);
237         if (ret)
238                 return ret;
239
240         ret = nv04_fbcon_grobj_new(dev, dev_priv->card_type >= NV_10 ?
241                                    0x009f : 0x005f, NvImageBlit);
242         if (ret)
243                 return ret;
244
245         if (RING_SPACE(chan, 49)) {
246                 NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
247                 info->flags |= FBINFO_HWACCEL_DISABLED;
248                 return 0;
249         }
250
251         BEGIN_RING(chan, sub, 0x0000, 1);
252         OUT_RING(chan, NvCtxSurf2D);
253         BEGIN_RING(chan, sub, 0x0184, 2);
254         OUT_RING(chan, NvDmaFB);
255         OUT_RING(chan, NvDmaFB);
256         BEGIN_RING(chan, sub, 0x0300, 4);
257         OUT_RING(chan, surface_fmt);
258         OUT_RING(chan, info->fix.line_length | (info->fix.line_length << 16));
259         OUT_RING(chan, info->fix.smem_start - dev->mode_config.fb_base);
260         OUT_RING(chan, info->fix.smem_start - dev->mode_config.fb_base);
261
262         BEGIN_RING(chan, sub, 0x0000, 1);
263         OUT_RING(chan, NvRop);
264         BEGIN_RING(chan, sub, 0x0300, 1);
265         OUT_RING(chan, 0x55);
266
267         BEGIN_RING(chan, sub, 0x0000, 1);
268         OUT_RING(chan, NvImagePatt);
269         BEGIN_RING(chan, sub, 0x0300, 8);
270         OUT_RING(chan, pattern_fmt);
271 #ifdef __BIG_ENDIAN
272         OUT_RING(chan, 2);
273 #else
274         OUT_RING(chan, 1);
275 #endif
276         OUT_RING(chan, 0);
277         OUT_RING(chan, 1);
278         OUT_RING(chan, ~0);
279         OUT_RING(chan, ~0);
280         OUT_RING(chan, ~0);
281         OUT_RING(chan, ~0);
282
283         BEGIN_RING(chan, sub, 0x0000, 1);
284         OUT_RING(chan, NvClipRect);
285         BEGIN_RING(chan, sub, 0x0300, 2);
286         OUT_RING(chan, 0);
287         OUT_RING(chan, (info->var.yres_virtual << 16) | info->var.xres_virtual);
288
289         BEGIN_RING(chan, NvSubImageBlit, 0x0000, 1);
290         OUT_RING(chan, NvImageBlit);
291         BEGIN_RING(chan, NvSubImageBlit, 0x019c, 1);
292         OUT_RING(chan, NvCtxSurf2D);
293         BEGIN_RING(chan, NvSubImageBlit, 0x02fc, 1);
294         OUT_RING(chan, 3);
295
296         BEGIN_RING(chan, NvSubGdiRect, 0x0000, 1);
297         OUT_RING(chan, NvGdiRect);
298         BEGIN_RING(chan, NvSubGdiRect, 0x0198, 1);
299         OUT_RING(chan, NvCtxSurf2D);
300         BEGIN_RING(chan, NvSubGdiRect, 0x0188, 2);
301         OUT_RING(chan, NvImagePatt);
302         OUT_RING(chan, NvRop);
303         BEGIN_RING(chan, NvSubGdiRect, 0x0304, 1);
304         OUT_RING(chan, 1);
305         BEGIN_RING(chan, NvSubGdiRect, 0x0300, 1);
306         OUT_RING(chan, rect_fmt);
307         BEGIN_RING(chan, NvSubGdiRect, 0x02fc, 1);
308         OUT_RING(chan, 3);
309
310         FIRE_RING(chan);
311
312         info->fbops->fb_fillrect = nv04_fbcon_fillrect;
313         info->fbops->fb_copyarea = nv04_fbcon_copyarea;
314         info->fbops->fb_imageblit = nv04_fbcon_imageblit;
315         return 0;
316 }
317