xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_nv50_fbcon.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nv50_fbcon.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2010 Red Hat Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Ben Skeggs
25  */
26 
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: nouveau_nv50_fbcon.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $");
29 
30 #include "nouveau_drv.h"
31 #include "nouveau_dma.h"
32 #include "nouveau_fbcon.h"
33 #include "nouveau_vmm.h"
34 
35 int
nv50_fbcon_fillrect(struct fb_info * info,const struct fb_fillrect * rect)36 nv50_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
37 {
38 	struct nouveau_fbdev *nfbdev = info->par;
39 	struct nouveau_drm *drm = nouveau_drm(nfbdev->helper.dev);
40 	struct nouveau_channel *chan = drm->channel;
41 	int ret;
42 
43 	ret = RING_SPACE(chan, rect->rop == ROP_COPY ? 7 : 11);
44 	if (ret)
45 		return ret;
46 
47 	if (rect->rop != ROP_COPY) {
48 		BEGIN_NV04(chan, NvSub2D, 0x02ac, 1);
49 		OUT_RING(chan, 1);
50 	}
51 	BEGIN_NV04(chan, NvSub2D, 0x0588, 1);
52 	if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
53 	    info->fix.visual == FB_VISUAL_DIRECTCOLOR)
54 		OUT_RING(chan, ((uint32_t *)info->pseudo_palette)[rect->color]);
55 	else
56 		OUT_RING(chan, rect->color);
57 	BEGIN_NV04(chan, NvSub2D, 0x0600, 4);
58 	OUT_RING(chan, rect->dx);
59 	OUT_RING(chan, rect->dy);
60 	OUT_RING(chan, rect->dx + rect->width);
61 	OUT_RING(chan, rect->dy + rect->height);
62 	if (rect->rop != ROP_COPY) {
63 		BEGIN_NV04(chan, NvSub2D, 0x02ac, 1);
64 		OUT_RING(chan, 3);
65 	}
66 	FIRE_RING(chan);
67 	return 0;
68 }
69 
70 int
nv50_fbcon_copyarea(struct fb_info * info,const struct fb_copyarea * region)71 nv50_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *region)
72 {
73 	struct nouveau_fbdev *nfbdev = info->par;
74 	struct nouveau_drm *drm = nouveau_drm(nfbdev->helper.dev);
75 	struct nouveau_channel *chan = drm->channel;
76 	int ret;
77 
78 	ret = RING_SPACE(chan, 12);
79 	if (ret)
80 		return ret;
81 
82 	BEGIN_NV04(chan, NvSub2D, 0x0110, 1);
83 	OUT_RING(chan, 0);
84 	BEGIN_NV04(chan, NvSub2D, 0x08b0, 4);
85 	OUT_RING(chan, region->dx);
86 	OUT_RING(chan, region->dy);
87 	OUT_RING(chan, region->width);
88 	OUT_RING(chan, region->height);
89 	BEGIN_NV04(chan, NvSub2D, 0x08d0, 4);
90 	OUT_RING(chan, 0);
91 	OUT_RING(chan, region->sx);
92 	OUT_RING(chan, 0);
93 	OUT_RING(chan, region->sy);
94 	FIRE_RING(chan);
95 	return 0;
96 }
97 
98 int
nv50_fbcon_imageblit(struct fb_info * info,const struct fb_image * image)99 nv50_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
100 {
101 	struct nouveau_fbdev *nfbdev = info->par;
102 	struct nouveau_drm *drm = nouveau_drm(nfbdev->helper.dev);
103 	struct nouveau_channel *chan = drm->channel;
104 	uint32_t dwords, *data = (uint32_t *)image->data;
105 	uint32_t mask = ~(~0 >> (32 - info->var.bits_per_pixel));
106 	uint32_t *palette = info->pseudo_palette;
107 	int ret;
108 
109 	if (image->depth != 1)
110 		return -ENODEV;
111 
112 	ret = RING_SPACE(chan, 11);
113 	if (ret)
114 		return ret;
115 
116 	BEGIN_NV04(chan, NvSub2D, 0x0814, 2);
117 	if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
118 	    info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
119 		OUT_RING(chan, palette[image->bg_color] | mask);
120 		OUT_RING(chan, palette[image->fg_color] | mask);
121 	} else {
122 		OUT_RING(chan, image->bg_color);
123 		OUT_RING(chan, image->fg_color);
124 	}
125 	BEGIN_NV04(chan, NvSub2D, 0x0838, 2);
126 	OUT_RING(chan, image->width);
127 	OUT_RING(chan, image->height);
128 	BEGIN_NV04(chan, NvSub2D, 0x0850, 4);
129 	OUT_RING(chan, 0);
130 	OUT_RING(chan, image->dx);
131 	OUT_RING(chan, 0);
132 	OUT_RING(chan, image->dy);
133 
134 	dwords = ALIGN(ALIGN(image->width, 8) * image->height, 32) >> 5;
135 	while (dwords) {
136 		int push = dwords > 2047 ? 2047 : dwords;
137 
138 		ret = RING_SPACE(chan, push + 1);
139 		if (ret)
140 			return ret;
141 
142 		dwords -= push;
143 
144 		BEGIN_NI04(chan, NvSub2D, 0x0860, push);
145 		OUT_RINGp(chan, data, push);
146 		data += push;
147 	}
148 
149 	FIRE_RING(chan);
150 	return 0;
151 }
152 
153 int
nv50_fbcon_accel_init(struct fb_info * info)154 nv50_fbcon_accel_init(struct fb_info *info)
155 {
156 	struct nouveau_fbdev *nfbdev = info->par;
157 	struct nouveau_framebuffer *fb = nouveau_framebuffer(nfbdev->helper.fb);
158 	struct drm_device *dev = nfbdev->helper.dev;
159 	struct nouveau_drm *drm = nouveau_drm(dev);
160 	struct nouveau_channel *chan = drm->channel;
161 	int ret, format;
162 
163 	switch (info->var.bits_per_pixel) {
164 	case 8:
165 		format = 0xf3;
166 		break;
167 	case 15:
168 		format = 0xf8;
169 		break;
170 	case 16:
171 		format = 0xe8;
172 		break;
173 	case 32:
174 		switch (info->var.transp.length) {
175 		case 0: /* depth 24 */
176 		case 8: /* depth 32, just use 24.. */
177 			format = 0xe6;
178 			break;
179 		case 2: /* depth 30 */
180 			format = 0xd1;
181 			break;
182 		default:
183 			return -EINVAL;
184 		}
185 		break;
186 	default:
187 		return -EINVAL;
188 	}
189 
190 	ret = nvif_object_init(&chan->user, 0x502d, 0x502d, NULL, 0,
191 			       &nfbdev->twod);
192 	if (ret)
193 		return ret;
194 
195 	ret = RING_SPACE(chan, 58);
196 	if (ret) {
197 		nouveau_fbcon_gpu_lockup(info);
198 		return ret;
199 	}
200 
201 	BEGIN_NV04(chan, NvSub2D, 0x0000, 1);
202 	OUT_RING(chan, nfbdev->twod.handle);
203 	BEGIN_NV04(chan, NvSub2D, 0x0184, 3);
204 	OUT_RING(chan, chan->vram.handle);
205 	OUT_RING(chan, chan->vram.handle);
206 	OUT_RING(chan, chan->vram.handle);
207 	BEGIN_NV04(chan, NvSub2D, 0x0290, 1);
208 	OUT_RING(chan, 0);
209 	BEGIN_NV04(chan, NvSub2D, 0x0888, 1);
210 	OUT_RING(chan, 1);
211 	BEGIN_NV04(chan, NvSub2D, 0x02ac, 1);
212 	OUT_RING(chan, 3);
213 	BEGIN_NV04(chan, NvSub2D, 0x02a0, 1);
214 	OUT_RING(chan, 0x55);
215 	BEGIN_NV04(chan, NvSub2D, 0x08c0, 4);
216 	OUT_RING(chan, 0);
217 	OUT_RING(chan, 1);
218 	OUT_RING(chan, 0);
219 	OUT_RING(chan, 1);
220 	BEGIN_NV04(chan, NvSub2D, 0x0580, 2);
221 	OUT_RING(chan, 4);
222 	OUT_RING(chan, format);
223 	BEGIN_NV04(chan, NvSub2D, 0x02e8, 2);
224 	OUT_RING(chan, 2);
225 	OUT_RING(chan, 1);
226 	BEGIN_NV04(chan, NvSub2D, 0x0804, 1);
227 	OUT_RING(chan, format);
228 	BEGIN_NV04(chan, NvSub2D, 0x0800, 1);
229 	OUT_RING(chan, 1);
230 	BEGIN_NV04(chan, NvSub2D, 0x0808, 3);
231 	OUT_RING(chan, 0);
232 	OUT_RING(chan, 0);
233 	OUT_RING(chan, 1);
234 	BEGIN_NV04(chan, NvSub2D, 0x081c, 1);
235 	OUT_RING(chan, 1);
236 	BEGIN_NV04(chan, NvSub2D, 0x0840, 4);
237 	OUT_RING(chan, 0);
238 	OUT_RING(chan, 1);
239 	OUT_RING(chan, 0);
240 	OUT_RING(chan, 1);
241 	BEGIN_NV04(chan, NvSub2D, 0x0200, 2);
242 	OUT_RING(chan, format);
243 	OUT_RING(chan, 1);
244 	BEGIN_NV04(chan, NvSub2D, 0x0214, 5);
245 	OUT_RING(chan, info->fix.line_length);
246 	OUT_RING(chan, info->var.xres_virtual);
247 	OUT_RING(chan, info->var.yres_virtual);
248 	OUT_RING(chan, upper_32_bits(fb->vma->addr));
249 	OUT_RING(chan, lower_32_bits(fb->vma->addr));
250 	BEGIN_NV04(chan, NvSub2D, 0x0230, 2);
251 	OUT_RING(chan, format);
252 	OUT_RING(chan, 1);
253 	BEGIN_NV04(chan, NvSub2D, 0x0244, 5);
254 	OUT_RING(chan, info->fix.line_length);
255 	OUT_RING(chan, info->var.xres_virtual);
256 	OUT_RING(chan, info->var.yres_virtual);
257 	OUT_RING(chan, upper_32_bits(fb->vma->addr));
258 	OUT_RING(chan, lower_32_bits(fb->vma->addr));
259 	FIRE_RING(chan);
260 
261 	return 0;
262 }
263 
264