Home | History | Annotate | Line # | Download | only in nouveau
      1 /*	$NetBSD: nouveau_nvc0_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_nvc0_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
     36 nvc0_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_NVC0(chan, NvSub2D, 0x02ac, 1);
     49 		OUT_RING  (chan, 1);
     50 	}
     51 	BEGIN_NVC0(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_NVC0(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_NVC0(chan, NvSub2D, 0x02ac, 1);
     64 		OUT_RING  (chan, 3);
     65 	}
     66 	FIRE_RING(chan);
     67 	return 0;
     68 }
     69 
     70 int
     71 nvc0_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_NVC0(chan, NvSub2D, 0x0110, 1);
     83 	OUT_RING  (chan, 0);
     84 	BEGIN_NVC0(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_NVC0(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
     99 nvc0_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_NVC0(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_NVC0(chan, NvSub2D, 0x0838, 2);
    126 	OUT_RING  (chan, image->width);
    127 	OUT_RING  (chan, image->height);
    128 	BEGIN_NVC0(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_NIC0(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
    154 nvc0_fbcon_accel_init(struct fb_info *info)
    155 {
    156 	struct nouveau_fbdev *nfbdev = info->par;
    157 	struct drm_device *dev = nfbdev->helper.dev;
    158 	struct nouveau_framebuffer *fb = nouveau_framebuffer(nfbdev->helper.fb);
    159 	struct nouveau_drm *drm = nouveau_drm(dev);
    160 	struct nouveau_channel *chan = drm->channel;
    161 	int ret, format;
    162 
    163 	ret = nvif_object_init(&chan->user, 0x902d, 0x902d, NULL, 0,
    164 			       &nfbdev->twod);
    165 	if (ret)
    166 		return ret;
    167 
    168 	switch (info->var.bits_per_pixel) {
    169 	case 8:
    170 		format = 0xf3;
    171 		break;
    172 	case 15:
    173 		format = 0xf8;
    174 		break;
    175 	case 16:
    176 		format = 0xe8;
    177 		break;
    178 	case 32:
    179 		switch (info->var.transp.length) {
    180 		case 0: /* depth 24 */
    181 		case 8: /* depth 32, just use 24.. */
    182 			format = 0xe6;
    183 			break;
    184 		case 2: /* depth 30 */
    185 			format = 0xd1;
    186 			break;
    187 		default:
    188 			return -EINVAL;
    189 		}
    190 		break;
    191 	default:
    192 		return -EINVAL;
    193 	}
    194 
    195 	ret = RING_SPACE(chan, 58);
    196 	if (ret) {
    197 		WARN_ON(1);
    198 		nouveau_fbcon_gpu_lockup(info);
    199 		return ret;
    200 	}
    201 
    202 	BEGIN_NVC0(chan, NvSub2D, 0x0000, 1);
    203 	OUT_RING  (chan, nfbdev->twod.handle);
    204 	BEGIN_NVC0(chan, NvSub2D, 0x0290, 1);
    205 	OUT_RING  (chan, 0);
    206 	BEGIN_NVC0(chan, NvSub2D, 0x0888, 1);
    207 	OUT_RING  (chan, 1);
    208 	BEGIN_NVC0(chan, NvSub2D, 0x02ac, 1);
    209 	OUT_RING  (chan, 3);
    210 	BEGIN_NVC0(chan, NvSub2D, 0x02a0, 1);
    211 	OUT_RING  (chan, 0x55);
    212 	BEGIN_NVC0(chan, NvSub2D, 0x08c0, 4);
    213 	OUT_RING  (chan, 0);
    214 	OUT_RING  (chan, 1);
    215 	OUT_RING  (chan, 0);
    216 	OUT_RING  (chan, 1);
    217 	BEGIN_NVC0(chan, NvSub2D, 0x0580, 2);
    218 	OUT_RING  (chan, 4);
    219 	OUT_RING  (chan, format);
    220 	BEGIN_NVC0(chan, NvSub2D, 0x02e8, 2);
    221 	OUT_RING  (chan, 2);
    222 	OUT_RING  (chan, 1);
    223 
    224 	BEGIN_NVC0(chan, NvSub2D, 0x0804, 1);
    225 	OUT_RING  (chan, format);
    226 	BEGIN_NVC0(chan, NvSub2D, 0x0800, 1);
    227 	OUT_RING  (chan, 1);
    228 	BEGIN_NVC0(chan, NvSub2D, 0x0808, 3);
    229 	OUT_RING  (chan, 0);
    230 	OUT_RING  (chan, 0);
    231 	OUT_RING  (chan, 1);
    232 	BEGIN_NVC0(chan, NvSub2D, 0x081c, 1);
    233 	OUT_RING  (chan, 1);
    234 	BEGIN_NVC0(chan, NvSub2D, 0x0840, 4);
    235 	OUT_RING  (chan, 0);
    236 	OUT_RING  (chan, 1);
    237 	OUT_RING  (chan, 0);
    238 	OUT_RING  (chan, 1);
    239 	BEGIN_NVC0(chan, NvSub2D, 0x0200, 10);
    240 	OUT_RING  (chan, format);
    241 	OUT_RING  (chan, 1);
    242 	OUT_RING  (chan, 0);
    243 	OUT_RING  (chan, 1);
    244 	OUT_RING  (chan, 0);
    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_NVC0(chan, NvSub2D, 0x0230, 10);
    251 	OUT_RING  (chan, format);
    252 	OUT_RING  (chan, 1);
    253 	OUT_RING  (chan, 0);
    254 	OUT_RING  (chan, 1);
    255 	OUT_RING  (chan, 0);
    256 	OUT_RING  (chan, info->fix.line_length);
    257 	OUT_RING  (chan, info->var.xres_virtual);
    258 	OUT_RING  (chan, info->var.yres_virtual);
    259 	OUT_RING  (chan, upper_32_bits(fb->vma->addr));
    260 	OUT_RING  (chan, lower_32_bits(fb->vma->addr));
    261 	FIRE_RING (chan);
    262 
    263 	return 0;
    264 }
    265 
    266