1cacade19SGerd Hoffmann // SPDX-License-Identifier: GPL-2.0 2cacade19SGerd Hoffmann /* 3cacade19SGerd Hoffmann * Framebuffer driver for mdpy (mediated virtual pci display device). 4cacade19SGerd Hoffmann * 5cacade19SGerd Hoffmann * See mdpy-defs.h for device specs 6cacade19SGerd Hoffmann * 7cacade19SGerd Hoffmann * (c) Gerd Hoffmann <kraxel@redhat.com> 8cacade19SGerd Hoffmann * 9cacade19SGerd Hoffmann * Using some code snippets from simplefb and cirrusfb. 10cacade19SGerd Hoffmann * 11cacade19SGerd Hoffmann * This program is free software; you can redistribute it and/or modify it 12cacade19SGerd Hoffmann * under the terms and conditions of the GNU General Public License, 13cacade19SGerd Hoffmann * version 2, as published by the Free Software Foundation. 14cacade19SGerd Hoffmann * 15cacade19SGerd Hoffmann * This program is distributed in the hope it will be useful, but WITHOUT 16cacade19SGerd Hoffmann * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 17cacade19SGerd Hoffmann * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 18cacade19SGerd Hoffmann * more details. 19cacade19SGerd Hoffmann */ 20cacade19SGerd Hoffmann #include <linux/errno.h> 21cacade19SGerd Hoffmann #include <linux/fb.h> 22cacade19SGerd Hoffmann #include <linux/io.h> 23cacade19SGerd Hoffmann #include <linux/pci.h> 24cacade19SGerd Hoffmann #include <linux/module.h> 25cacade19SGerd Hoffmann #include <drm/drm_fourcc.h> 26cacade19SGerd Hoffmann #include "mdpy-defs.h" 27cacade19SGerd Hoffmann 28cacade19SGerd Hoffmann static const struct fb_fix_screeninfo mdpy_fb_fix = { 29cacade19SGerd Hoffmann .id = "mdpy-fb", 30cacade19SGerd Hoffmann .type = FB_TYPE_PACKED_PIXELS, 31cacade19SGerd Hoffmann .visual = FB_VISUAL_TRUECOLOR, 32cacade19SGerd Hoffmann .accel = FB_ACCEL_NONE, 33cacade19SGerd Hoffmann }; 34cacade19SGerd Hoffmann 35cacade19SGerd Hoffmann static const struct fb_var_screeninfo mdpy_fb_var = { 36cacade19SGerd Hoffmann .height = -1, 37cacade19SGerd Hoffmann .width = -1, 38cacade19SGerd Hoffmann .activate = FB_ACTIVATE_NOW, 39cacade19SGerd Hoffmann .vmode = FB_VMODE_NONINTERLACED, 40cacade19SGerd Hoffmann 41cacade19SGerd Hoffmann .bits_per_pixel = 32, 42cacade19SGerd Hoffmann .transp.offset = 24, 43cacade19SGerd Hoffmann .red.offset = 16, 44cacade19SGerd Hoffmann .green.offset = 8, 45cacade19SGerd Hoffmann .blue.offset = 0, 46cacade19SGerd Hoffmann .transp.length = 8, 47cacade19SGerd Hoffmann .red.length = 8, 48cacade19SGerd Hoffmann .green.length = 8, 49cacade19SGerd Hoffmann .blue.length = 8, 50cacade19SGerd Hoffmann }; 51cacade19SGerd Hoffmann 52cacade19SGerd Hoffmann #define PSEUDO_PALETTE_SIZE 16 53cacade19SGerd Hoffmann 54cacade19SGerd Hoffmann struct mdpy_fb_par { 55cacade19SGerd Hoffmann u32 palette[PSEUDO_PALETTE_SIZE]; 56cacade19SGerd Hoffmann }; 57cacade19SGerd Hoffmann 58cacade19SGerd Hoffmann static int mdpy_fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue, 59cacade19SGerd Hoffmann u_int transp, struct fb_info *info) 60cacade19SGerd Hoffmann { 61cacade19SGerd Hoffmann u32 *pal = info->pseudo_palette; 62cacade19SGerd Hoffmann u32 cr = red >> (16 - info->var.red.length); 63cacade19SGerd Hoffmann u32 cg = green >> (16 - info->var.green.length); 64cacade19SGerd Hoffmann u32 cb = blue >> (16 - info->var.blue.length); 65cacade19SGerd Hoffmann u32 value, mask; 66cacade19SGerd Hoffmann 67cacade19SGerd Hoffmann if (regno >= PSEUDO_PALETTE_SIZE) 68cacade19SGerd Hoffmann return -EINVAL; 69cacade19SGerd Hoffmann 70cacade19SGerd Hoffmann value = (cr << info->var.red.offset) | 71cacade19SGerd Hoffmann (cg << info->var.green.offset) | 72cacade19SGerd Hoffmann (cb << info->var.blue.offset); 73cacade19SGerd Hoffmann if (info->var.transp.length > 0) { 74cacade19SGerd Hoffmann mask = (1 << info->var.transp.length) - 1; 75cacade19SGerd Hoffmann mask <<= info->var.transp.offset; 76cacade19SGerd Hoffmann value |= mask; 77cacade19SGerd Hoffmann } 78cacade19SGerd Hoffmann pal[regno] = value; 79cacade19SGerd Hoffmann 80cacade19SGerd Hoffmann return 0; 81cacade19SGerd Hoffmann } 82cacade19SGerd Hoffmann 83cacade19SGerd Hoffmann static void mdpy_fb_destroy(struct fb_info *info) 84cacade19SGerd Hoffmann { 85cacade19SGerd Hoffmann if (info->screen_base) 86cacade19SGerd Hoffmann iounmap(info->screen_base); 87cacade19SGerd Hoffmann } 88cacade19SGerd Hoffmann 89*b0077e52SJani Nikula static const struct fb_ops mdpy_fb_ops = { 90cacade19SGerd Hoffmann .owner = THIS_MODULE, 91cacade19SGerd Hoffmann .fb_destroy = mdpy_fb_destroy, 92cacade19SGerd Hoffmann .fb_setcolreg = mdpy_fb_setcolreg, 93cacade19SGerd Hoffmann .fb_fillrect = cfb_fillrect, 94cacade19SGerd Hoffmann .fb_copyarea = cfb_copyarea, 95cacade19SGerd Hoffmann .fb_imageblit = cfb_imageblit, 96cacade19SGerd Hoffmann }; 97cacade19SGerd Hoffmann 98cacade19SGerd Hoffmann static int mdpy_fb_probe(struct pci_dev *pdev, 99cacade19SGerd Hoffmann const struct pci_device_id *ent) 100cacade19SGerd Hoffmann { 101cacade19SGerd Hoffmann struct fb_info *info; 102cacade19SGerd Hoffmann struct mdpy_fb_par *par; 103cacade19SGerd Hoffmann u32 format, width, height; 104cacade19SGerd Hoffmann int ret; 105cacade19SGerd Hoffmann 106cacade19SGerd Hoffmann ret = pci_enable_device(pdev); 107cacade19SGerd Hoffmann if (ret < 0) 108cacade19SGerd Hoffmann return ret; 109cacade19SGerd Hoffmann 110cacade19SGerd Hoffmann ret = pci_request_regions(pdev, "mdpy-fb"); 111cacade19SGerd Hoffmann if (ret < 0) 112cacade19SGerd Hoffmann return ret; 113cacade19SGerd Hoffmann 114cacade19SGerd Hoffmann pci_read_config_dword(pdev, MDPY_FORMAT_OFFSET, &format); 115cacade19SGerd Hoffmann pci_read_config_dword(pdev, MDPY_WIDTH_OFFSET, &width); 116cacade19SGerd Hoffmann pci_read_config_dword(pdev, MDPY_HEIGHT_OFFSET, &height); 117cacade19SGerd Hoffmann if (format != DRM_FORMAT_XRGB8888) { 118cacade19SGerd Hoffmann pci_err(pdev, "format mismatch (0x%x != 0x%x)\n", 119cacade19SGerd Hoffmann format, DRM_FORMAT_XRGB8888); 120cacade19SGerd Hoffmann return -EINVAL; 121cacade19SGerd Hoffmann } 122cacade19SGerd Hoffmann if (width < 100 || width > 10000) { 123cacade19SGerd Hoffmann pci_err(pdev, "width (%d) out of range\n", width); 124cacade19SGerd Hoffmann return -EINVAL; 125cacade19SGerd Hoffmann } 126cacade19SGerd Hoffmann if (height < 100 || height > 10000) { 127cacade19SGerd Hoffmann pci_err(pdev, "height (%d) out of range\n", height); 128cacade19SGerd Hoffmann return -EINVAL; 129cacade19SGerd Hoffmann } 130cacade19SGerd Hoffmann pci_info(pdev, "mdpy found: %dx%d framebuffer\n", 131cacade19SGerd Hoffmann width, height); 132cacade19SGerd Hoffmann 133cacade19SGerd Hoffmann info = framebuffer_alloc(sizeof(struct mdpy_fb_par), &pdev->dev); 134cacade19SGerd Hoffmann if (!info) 135cacade19SGerd Hoffmann goto err_release_regions; 136cacade19SGerd Hoffmann pci_set_drvdata(pdev, info); 137cacade19SGerd Hoffmann par = info->par; 138cacade19SGerd Hoffmann 139cacade19SGerd Hoffmann info->fix = mdpy_fb_fix; 140cacade19SGerd Hoffmann info->fix.smem_start = pci_resource_start(pdev, 0); 141cacade19SGerd Hoffmann info->fix.smem_len = pci_resource_len(pdev, 0); 142cacade19SGerd Hoffmann info->fix.line_length = width * 4; 143cacade19SGerd Hoffmann 144cacade19SGerd Hoffmann info->var = mdpy_fb_var; 145cacade19SGerd Hoffmann info->var.xres = width; 146cacade19SGerd Hoffmann info->var.yres = height; 147cacade19SGerd Hoffmann info->var.xres_virtual = width; 148cacade19SGerd Hoffmann info->var.yres_virtual = height; 149cacade19SGerd Hoffmann 150cacade19SGerd Hoffmann info->screen_size = info->fix.smem_len; 151cacade19SGerd Hoffmann info->screen_base = ioremap(info->fix.smem_start, 152cacade19SGerd Hoffmann info->screen_size); 153cacade19SGerd Hoffmann if (!info->screen_base) { 154cacade19SGerd Hoffmann pci_err(pdev, "ioremap(pcibar) failed\n"); 155cacade19SGerd Hoffmann ret = -EIO; 156cacade19SGerd Hoffmann goto err_release_fb; 157cacade19SGerd Hoffmann } 158cacade19SGerd Hoffmann 159cacade19SGerd Hoffmann info->apertures = alloc_apertures(1); 160cacade19SGerd Hoffmann if (!info->apertures) { 161cacade19SGerd Hoffmann ret = -ENOMEM; 162cacade19SGerd Hoffmann goto err_unmap; 163cacade19SGerd Hoffmann } 164cacade19SGerd Hoffmann info->apertures->ranges[0].base = info->fix.smem_start; 165cacade19SGerd Hoffmann info->apertures->ranges[0].size = info->fix.smem_len; 166cacade19SGerd Hoffmann 167cacade19SGerd Hoffmann info->fbops = &mdpy_fb_ops; 168cacade19SGerd Hoffmann info->flags = FBINFO_DEFAULT; 169cacade19SGerd Hoffmann info->pseudo_palette = par->palette; 170cacade19SGerd Hoffmann 171cacade19SGerd Hoffmann ret = register_framebuffer(info); 172cacade19SGerd Hoffmann if (ret < 0) { 173cacade19SGerd Hoffmann pci_err(pdev, "mdpy-fb device register failed: %d\n", ret); 174cacade19SGerd Hoffmann goto err_unmap; 175cacade19SGerd Hoffmann } 176cacade19SGerd Hoffmann 177cacade19SGerd Hoffmann pci_info(pdev, "fb%d registered\n", info->node); 178cacade19SGerd Hoffmann return 0; 179cacade19SGerd Hoffmann 180cacade19SGerd Hoffmann err_unmap: 181cacade19SGerd Hoffmann iounmap(info->screen_base); 182cacade19SGerd Hoffmann 183cacade19SGerd Hoffmann err_release_fb: 184cacade19SGerd Hoffmann framebuffer_release(info); 185cacade19SGerd Hoffmann 186cacade19SGerd Hoffmann err_release_regions: 187cacade19SGerd Hoffmann pci_release_regions(pdev); 188cacade19SGerd Hoffmann 189cacade19SGerd Hoffmann return ret; 190cacade19SGerd Hoffmann } 191cacade19SGerd Hoffmann 192cacade19SGerd Hoffmann static void mdpy_fb_remove(struct pci_dev *pdev) 193cacade19SGerd Hoffmann { 194cacade19SGerd Hoffmann struct fb_info *info = pci_get_drvdata(pdev); 195cacade19SGerd Hoffmann 196cacade19SGerd Hoffmann unregister_framebuffer(info); 197cacade19SGerd Hoffmann framebuffer_release(info); 198cacade19SGerd Hoffmann } 199cacade19SGerd Hoffmann 200cacade19SGerd Hoffmann static struct pci_device_id mdpy_fb_pci_table[] = { 201cacade19SGerd Hoffmann { 202cacade19SGerd Hoffmann .vendor = MDPY_PCI_VENDOR_ID, 203cacade19SGerd Hoffmann .device = MDPY_PCI_DEVICE_ID, 204cacade19SGerd Hoffmann .subvendor = MDPY_PCI_SUBVENDOR_ID, 205cacade19SGerd Hoffmann .subdevice = MDPY_PCI_SUBDEVICE_ID, 206cacade19SGerd Hoffmann }, { 207cacade19SGerd Hoffmann /* end of list */ 208cacade19SGerd Hoffmann } 209cacade19SGerd Hoffmann }; 210cacade19SGerd Hoffmann 211cacade19SGerd Hoffmann static struct pci_driver mdpy_fb_pci_driver = { 212cacade19SGerd Hoffmann .name = "mdpy-fb", 213cacade19SGerd Hoffmann .id_table = mdpy_fb_pci_table, 214cacade19SGerd Hoffmann .probe = mdpy_fb_probe, 215cacade19SGerd Hoffmann .remove = mdpy_fb_remove, 216cacade19SGerd Hoffmann }; 217cacade19SGerd Hoffmann 218cacade19SGerd Hoffmann static int __init mdpy_fb_init(void) 219cacade19SGerd Hoffmann { 220cacade19SGerd Hoffmann int ret; 221cacade19SGerd Hoffmann 222cacade19SGerd Hoffmann ret = pci_register_driver(&mdpy_fb_pci_driver); 223cacade19SGerd Hoffmann if (ret) 224cacade19SGerd Hoffmann return ret; 225cacade19SGerd Hoffmann 226cacade19SGerd Hoffmann return 0; 227cacade19SGerd Hoffmann } 228cacade19SGerd Hoffmann 229cacade19SGerd Hoffmann module_init(mdpy_fb_init); 230cacade19SGerd Hoffmann 231cacade19SGerd Hoffmann MODULE_DEVICE_TABLE(pci, mdpy_fb_pci_table); 232cacade19SGerd Hoffmann MODULE_LICENSE("GPL v2"); 233