]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/gpu/drm/nouveau/nv04_fbcon.c
drm/nouveau: fix handling of fbcon colours in 8bpp
[linux-2.6.git] / 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
66         if (info->state != FBINFO_STATE_RUNNING)
67                 return;
68
69         if (!(info->flags & FBINFO_HWACCEL_DISABLED) && RING_SPACE(chan, 7)) {
70                 NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
71                 info->flags |= FBINFO_HWACCEL_DISABLED;
72         }
73
74         if (info->flags & FBINFO_HWACCEL_DISABLED) {
75                 cfb_fillrect(info, rect);
76                 return;
77         }
78
79         BEGIN_RING(chan, NvSubGdiRect, 0x02fc, 1);
80         OUT_RING(chan, (rect->rop != ROP_COPY) ? 1 : 3);
81         BEGIN_RING(chan, NvSubGdiRect, 0x03fc, 1);
82         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
83             info->fix.visual == FB_VISUAL_DIRECTCOLOR)
84                 OUT_RING(chan, ((uint32_t *)info->pseudo_palette)[rect->color]);
85         else
86                 OUT_RING(chan, rect->color);
87         BEGIN_RING(chan, NvSubGdiRect, 0x0400, 2);
88         OUT_RING(chan, (rect->dx << 16) | rect->dy);
89         OUT_RING(chan, (rect->width << 16) | rect->height);
90         FIRE_RING(chan);
91 }
92
93 static void
94 nv04_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
95 {
96         struct nouveau_fbcon_par *par = info->par;
97         struct drm_device *dev = par->dev;
98         struct drm_nouveau_private *dev_priv = dev->dev_private;
99         struct nouveau_channel *chan = dev_priv->channel;
100         uint32_t fg;
101         uint32_t bg;
102         uint32_t dsize;
103         uint32_t width;
104         uint32_t *data = (uint32_t *)image->data;
105
106         if (info->state != FBINFO_STATE_RUNNING)
107                 return;
108
109         if (image->depth != 1) {
110                 cfb_imageblit(info, image);
111                 return;
112         }
113
114         if (!(info->flags & FBINFO_HWACCEL_DISABLED) && RING_SPACE(chan, 8)) {
115                 NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
116                 info->flags |= FBINFO_HWACCEL_DISABLED;
117         }
118
119         if (info->flags & FBINFO_HWACCEL_DISABLED) {
120                 cfb_imageblit(info, image);
121                 return;
122         }
123
124         width = (image->width + 31) & ~31;
125         dsize = (width * image->height) >> 5;
126
127         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
128             info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
129                 fg = ((uint32_t *) info->pseudo_palette)[image->fg_color];
130                 bg = ((uint32_t *) info->pseudo_palette)[image->bg_color];
131         } else {
132                 fg = image->fg_color;
133                 bg = image->bg_color;
134         }
135
136         BEGIN_RING(chan, NvSubGdiRect, 0x0be4, 7);
137         OUT_RING(chan, (image->dy << 16) | (image->dx & 0xffff));
138         OUT_RING(chan, ((image->dy + image->height) << 16) |
139                          ((image->dx + image->width) & 0xffff));
140         OUT_RING(chan, bg);
141         OUT_RING(chan, fg);
142         OUT_RING(chan, (image->height << 16) | image->width);
143         OUT_RING(chan, (image->height << 16) | width);
144         OUT_RING(chan, (image->dy << 16) | (image->dx & 0xffff));
145
146         while (dsize) {
147                 int iter_len = dsize > 128 ? 128 : dsize;
148
149                 if (RING_SPACE(chan, iter_len + 1)) {
150                         NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
151                         info->flags |= FBINFO_HWACCEL_DISABLED;
152                         cfb_imageblit(info, image);
153                         return;
154                 }
155
156                 BEGIN_RING(chan, NvSubGdiRect, 0x0c00, iter_len);
157                 OUT_RINGp(chan, data, iter_len);
158                 data += iter_len;
159                 dsize -= iter_len;
160         }
161
162         FIRE_RING(chan);
163 }
164
165 static int
166 nv04_fbcon_grobj_new(struct drm_device *dev, int class, uint32_t handle)
167 {
168         struct drm_nouveau_private *dev_priv = dev->dev_private;
169         struct nouveau_gpuobj *obj = NULL;
170         int ret;
171
172         ret = nouveau_gpuobj_gr_new(dev_priv->channel, class, &obj);
173         if (ret)
174                 return ret;
175
176         ret = nouveau_gpuobj_ref_add(dev, dev_priv->channel, handle, obj, NULL);
177         if (ret)
178                 return ret;
179
180         return 0;
181 }
182
183 int
184 nv04_fbcon_accel_init(struct fb_info *info)
185 {
186         struct nouveau_fbcon_par *par = info->par;
187         struct drm_device *dev = par->dev;
188         struct drm_nouveau_private *dev_priv = dev->dev_private;
189         struct nouveau_channel *chan = dev_priv->channel;
190         const int sub = NvSubCtxSurf2D;
191         int surface_fmt, pattern_fmt, rect_fmt;
192         int ret;
193
194         switch (info->var.bits_per_pixel) {
195         case 8:
196                 surface_fmt = 1;
197                 pattern_fmt = 3;
198                 rect_fmt = 3;
199                 break;
200         case 16:
201                 surface_fmt = 4;
202                 pattern_fmt = 1;
203                 rect_fmt = 1;
204                 break;
205         case 32:
206                 switch (info->var.transp.length) {
207                 case 0: /* depth 24 */
208                 case 8: /* depth 32 */
209                         break;
210                 default:
211                         return -EINVAL;
212                 }
213
214                 surface_fmt = 6;
215                 pattern_fmt = 3;
216                 rect_fmt = 3;
217                 break;
218         default:
219                 return -EINVAL;
220         }
221
222         ret = nv04_fbcon_grobj_new(dev, dev_priv->card_type >= NV_10 ?
223                                    0x0062 : 0x0042, NvCtxSurf2D);
224         if (ret)
225                 return ret;
226
227         ret = nv04_fbcon_grobj_new(dev, 0x0019, NvClipRect);
228         if (ret)
229                 return ret;
230
231         ret = nv04_fbcon_grobj_new(dev, 0x0043, NvRop);
232         if (ret)
233                 return ret;
234
235         ret = nv04_fbcon_grobj_new(dev, 0x0044, NvImagePatt);
236         if (ret)
237                 return ret;
238
239         ret = nv04_fbcon_grobj_new(dev, 0x004a, NvGdiRect);
240         if (ret)
241                 return ret;
242
243         ret = nv04_fbcon_grobj_new(dev, dev_priv->card_type >= NV_10 ?
244                                    0x009f : 0x005f, NvImageBlit);
245         if (ret)
246                 return ret;
247
248         if (RING_SPACE(chan, 49)) {
249                 NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
250                 info->flags |= FBINFO_HWACCEL_DISABLED;
251                 return 0;
252         }
253
254         BEGIN_RING(chan, sub, 0x0000, 1);
255         OUT_RING(chan, NvCtxSurf2D);
256         BEGIN_RING(chan, sub, 0x0184, 2);
257         OUT_RING(chan, NvDmaFB);
258         OUT_RING(chan, NvDmaFB);
259         BEGIN_RING(chan, sub, 0x0300, 4);
260         OUT_RING(chan, surface_fmt);
261         OUT_RING(chan, info->fix.line_length | (info->fix.line_length << 16));
262         OUT_RING(chan, info->fix.smem_start - dev->mode_config.fb_base);
263         OUT_RING(chan, info->fix.smem_start - dev->mode_config.fb_base);
264
265         BEGIN_RING(chan, sub, 0x0000, 1);
266         OUT_RING(chan, NvRop);
267         BEGIN_RING(chan, sub, 0x0300, 1);
268         OUT_RING(chan, 0x55);
269
270         BEGIN_RING(chan, sub, 0x0000, 1);
271         OUT_RING(chan, NvImagePatt);
272         BEGIN_RING(chan, sub, 0x0300, 8);
273         OUT_RING(chan, pattern_fmt);
274 #ifdef __BIG_ENDIAN
275         OUT_RING(chan, 2);
276 #else
277         OUT_RING(chan, 1);
278 #endif
279         OUT_RING(chan, 0);
280         OUT_RING(chan, 1);
281         OUT_RING(chan, ~0);
282         OUT_RING(chan, ~0);
283         OUT_RING(chan, ~0);
284         OUT_RING(chan, ~0);
285
286         BEGIN_RING(chan, sub, 0x0000, 1);
287         OUT_RING(chan, NvClipRect);
288         BEGIN_RING(chan, sub, 0x0300, 2);
289         OUT_RING(chan, 0);
290         OUT_RING(chan, (info->var.yres_virtual << 16) | info->var.xres_virtual);
291
292         BEGIN_RING(chan, NvSubImageBlit, 0x0000, 1);
293         OUT_RING(chan, NvImageBlit);
294         BEGIN_RING(chan, NvSubImageBlit, 0x019c, 1);
295         OUT_RING(chan, NvCtxSurf2D);
296         BEGIN_RING(chan, NvSubImageBlit, 0x02fc, 1);
297         OUT_RING(chan, 3);
298
299         BEGIN_RING(chan, NvSubGdiRect, 0x0000, 1);
300         OUT_RING(chan, NvGdiRect);
301         BEGIN_RING(chan, NvSubGdiRect, 0x0198, 1);
302         OUT_RING(chan, NvCtxSurf2D);
303         BEGIN_RING(chan, NvSubGdiRect, 0x0188, 2);
304         OUT_RING(chan, NvImagePatt);
305         OUT_RING(chan, NvRop);
306         BEGIN_RING(chan, NvSubGdiRect, 0x0304, 1);
307         OUT_RING(chan, 1);
308         BEGIN_RING(chan, NvSubGdiRect, 0x0300, 1);
309         OUT_RING(chan, rect_fmt);
310         BEGIN_RING(chan, NvSubGdiRect, 0x02fc, 1);
311         OUT_RING(chan, 3);
312
313         FIRE_RING(chan);
314
315         info->fbops->fb_fillrect = nv04_fbcon_fillrect;
316         info->fbops->fb_copyarea = nv04_fbcon_copyarea;
317         info->fbops->fb_imageblit = nv04_fbcon_imageblit;
318         return 0;
319 }
320