drm/nouveau: disallow fbcon accel if running in interrupt context
[cascardo/linux.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_ramht.h"
29 #include "nouveau_fbcon.h"
30
31 int
32 nv04_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *region)
33 {
34         struct nouveau_fbdev *nfbdev = info->par;
35         struct drm_device *dev = nfbdev->dev;
36         struct drm_nouveau_private *dev_priv = dev->dev_private;
37         struct nouveau_channel *chan = dev_priv->channel;
38         int ret;
39
40         ret = RING_SPACE(chan, 4);
41         if (ret)
42                 return ret;
43
44         BEGIN_RING(chan, NvSubImageBlit, 0x0300, 3);
45         OUT_RING(chan, (region->sy << 16) | region->sx);
46         OUT_RING(chan, (region->dy << 16) | region->dx);
47         OUT_RING(chan, (region->height << 16) | region->width);
48         FIRE_RING(chan);
49         return 0;
50 }
51
52 int
53 nv04_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
54 {
55         struct nouveau_fbdev *nfbdev = info->par;
56         struct drm_device *dev = nfbdev->dev;
57         struct drm_nouveau_private *dev_priv = dev->dev_private;
58         struct nouveau_channel *chan = dev_priv->channel;
59         int ret;
60
61         ret = RING_SPACE(chan, 7);
62         if (ret)
63                 return ret;
64
65         BEGIN_RING(chan, NvSubGdiRect, 0x02fc, 1);
66         OUT_RING(chan, (rect->rop != ROP_COPY) ? 1 : 3);
67         BEGIN_RING(chan, NvSubGdiRect, 0x03fc, 1);
68         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
69             info->fix.visual == FB_VISUAL_DIRECTCOLOR)
70                 OUT_RING(chan, ((uint32_t *)info->pseudo_palette)[rect->color]);
71         else
72                 OUT_RING(chan, rect->color);
73         BEGIN_RING(chan, NvSubGdiRect, 0x0400, 2);
74         OUT_RING(chan, (rect->dx << 16) | rect->dy);
75         OUT_RING(chan, (rect->width << 16) | rect->height);
76         FIRE_RING(chan);
77         return 0;
78 }
79
80 int
81 nv04_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
82 {
83         struct nouveau_fbdev *nfbdev = info->par;
84         struct drm_device *dev = nfbdev->dev;
85         struct drm_nouveau_private *dev_priv = dev->dev_private;
86         struct nouveau_channel *chan = dev_priv->channel;
87         uint32_t fg;
88         uint32_t bg;
89         uint32_t dsize;
90         uint32_t width;
91         uint32_t *data = (uint32_t *)image->data;
92         int ret;
93
94         if (image->depth != 1)
95                 return -ENODEV;
96
97         ret = RING_SPACE(chan, 8);
98         if (ret)
99                 return ret;
100
101         width = ALIGN(image->width, 8);
102         dsize = ALIGN(width * image->height, 32) >> 5;
103
104         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
105             info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
106                 fg = ((uint32_t *) info->pseudo_palette)[image->fg_color];
107                 bg = ((uint32_t *) info->pseudo_palette)[image->bg_color];
108         } else {
109                 fg = image->fg_color;
110                 bg = image->bg_color;
111         }
112
113         BEGIN_RING(chan, NvSubGdiRect, 0x0be4, 7);
114         OUT_RING(chan, (image->dy << 16) | (image->dx & 0xffff));
115         OUT_RING(chan, ((image->dy + image->height) << 16) |
116                          ((image->dx + image->width) & 0xffff));
117         OUT_RING(chan, bg);
118         OUT_RING(chan, fg);
119         OUT_RING(chan, (image->height << 16) | width);
120         OUT_RING(chan, (image->height << 16) | image->width);
121         OUT_RING(chan, (image->dy << 16) | (image->dx & 0xffff));
122
123         while (dsize) {
124                 int iter_len = dsize > 128 ? 128 : dsize;
125
126                 ret = RING_SPACE(chan, iter_len + 1);
127                 if (ret)
128                         return ret;
129
130                 BEGIN_RING(chan, NvSubGdiRect, 0x0c00, iter_len);
131                 OUT_RINGp(chan, data, iter_len);
132                 data += iter_len;
133                 dsize -= iter_len;
134         }
135
136         FIRE_RING(chan);
137         return 0;
138 }
139
140 static int
141 nv04_fbcon_grobj_new(struct drm_device *dev, int class, uint32_t handle)
142 {
143         struct drm_nouveau_private *dev_priv = dev->dev_private;
144         struct nouveau_gpuobj *obj = NULL;
145         int ret;
146
147         ret = nouveau_gpuobj_gr_new(dev_priv->channel, class, &obj);
148         if (ret)
149                 return ret;
150
151         ret = nouveau_ramht_insert(dev_priv->channel, handle, obj);
152         nouveau_gpuobj_ref(NULL, &obj);
153         return ret;
154 }
155
156 int
157 nv04_fbcon_accel_init(struct fb_info *info)
158 {
159         struct nouveau_fbdev *nfbdev = info->par;
160         struct drm_device *dev = nfbdev->dev;
161         struct drm_nouveau_private *dev_priv = dev->dev_private;
162         struct nouveau_channel *chan = dev_priv->channel;
163         const int sub = NvSubCtxSurf2D;
164         int surface_fmt, pattern_fmt, rect_fmt;
165         int ret;
166
167         switch (info->var.bits_per_pixel) {
168         case 8:
169                 surface_fmt = 1;
170                 pattern_fmt = 3;
171                 rect_fmt = 3;
172                 break;
173         case 16:
174                 surface_fmt = 4;
175                 pattern_fmt = 1;
176                 rect_fmt = 1;
177                 break;
178         case 32:
179                 switch (info->var.transp.length) {
180                 case 0: /* depth 24 */
181                 case 8: /* depth 32 */
182                         break;
183                 default:
184                         return -EINVAL;
185                 }
186
187                 surface_fmt = 6;
188                 pattern_fmt = 3;
189                 rect_fmt = 3;
190                 break;
191         default:
192                 return -EINVAL;
193         }
194
195         ret = nv04_fbcon_grobj_new(dev, dev_priv->card_type >= NV_10 ?
196                                    0x0062 : 0x0042, NvCtxSurf2D);
197         if (ret)
198                 return ret;
199
200         ret = nv04_fbcon_grobj_new(dev, 0x0019, NvClipRect);
201         if (ret)
202                 return ret;
203
204         ret = nv04_fbcon_grobj_new(dev, 0x0043, NvRop);
205         if (ret)
206                 return ret;
207
208         ret = nv04_fbcon_grobj_new(dev, 0x0044, NvImagePatt);
209         if (ret)
210                 return ret;
211
212         ret = nv04_fbcon_grobj_new(dev, 0x004a, NvGdiRect);
213         if (ret)
214                 return ret;
215
216         ret = nv04_fbcon_grobj_new(dev, dev_priv->chipset >= 0x11 ?
217                                    0x009f : 0x005f, NvImageBlit);
218         if (ret)
219                 return ret;
220
221         if (RING_SPACE(chan, 49)) {
222                 nouveau_fbcon_gpu_lockup(info);
223                 return 0;
224         }
225
226         BEGIN_RING(chan, sub, 0x0000, 1);
227         OUT_RING(chan, NvCtxSurf2D);
228         BEGIN_RING(chan, sub, 0x0184, 2);
229         OUT_RING(chan, NvDmaFB);
230         OUT_RING(chan, NvDmaFB);
231         BEGIN_RING(chan, sub, 0x0300, 4);
232         OUT_RING(chan, surface_fmt);
233         OUT_RING(chan, info->fix.line_length | (info->fix.line_length << 16));
234         OUT_RING(chan, info->fix.smem_start - dev->mode_config.fb_base);
235         OUT_RING(chan, info->fix.smem_start - dev->mode_config.fb_base);
236
237         BEGIN_RING(chan, sub, 0x0000, 1);
238         OUT_RING(chan, NvRop);
239         BEGIN_RING(chan, sub, 0x0300, 1);
240         OUT_RING(chan, 0x55);
241
242         BEGIN_RING(chan, sub, 0x0000, 1);
243         OUT_RING(chan, NvImagePatt);
244         BEGIN_RING(chan, sub, 0x0300, 8);
245         OUT_RING(chan, pattern_fmt);
246 #ifdef __BIG_ENDIAN
247         OUT_RING(chan, 2);
248 #else
249         OUT_RING(chan, 1);
250 #endif
251         OUT_RING(chan, 0);
252         OUT_RING(chan, 1);
253         OUT_RING(chan, ~0);
254         OUT_RING(chan, ~0);
255         OUT_RING(chan, ~0);
256         OUT_RING(chan, ~0);
257
258         BEGIN_RING(chan, sub, 0x0000, 1);
259         OUT_RING(chan, NvClipRect);
260         BEGIN_RING(chan, sub, 0x0300, 2);
261         OUT_RING(chan, 0);
262         OUT_RING(chan, (info->var.yres_virtual << 16) | info->var.xres_virtual);
263
264         BEGIN_RING(chan, NvSubImageBlit, 0x0000, 1);
265         OUT_RING(chan, NvImageBlit);
266         BEGIN_RING(chan, NvSubImageBlit, 0x019c, 1);
267         OUT_RING(chan, NvCtxSurf2D);
268         BEGIN_RING(chan, NvSubImageBlit, 0x02fc, 1);
269         OUT_RING(chan, 3);
270
271         BEGIN_RING(chan, NvSubGdiRect, 0x0000, 1);
272         OUT_RING(chan, NvGdiRect);
273         BEGIN_RING(chan, NvSubGdiRect, 0x0198, 1);
274         OUT_RING(chan, NvCtxSurf2D);
275         BEGIN_RING(chan, NvSubGdiRect, 0x0188, 2);
276         OUT_RING(chan, NvImagePatt);
277         OUT_RING(chan, NvRop);
278         BEGIN_RING(chan, NvSubGdiRect, 0x0304, 1);
279         OUT_RING(chan, 1);
280         BEGIN_RING(chan, NvSubGdiRect, 0x0300, 1);
281         OUT_RING(chan, rect_fmt);
282         BEGIN_RING(chan, NvSubGdiRect, 0x02fc, 1);
283         OUT_RING(chan, 3);
284
285         FIRE_RING(chan);
286
287         return 0;
288 }
289