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
mdpy_fb_setcolreg(u_int regno,u_int red,u_int green,u_int blue,u_int transp,struct fb_info * info)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
mdpy_fb_destroy(struct fb_info * info)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
89b0077e52SJani Nikula static const struct fb_ops mdpy_fb_ops = {
90cacade19SGerd Hoffmann .owner = THIS_MODULE,
91e4ef4e5dSThomas Zimmermann FB_DEFAULT_IOMEM_OPS,
92cacade19SGerd Hoffmann .fb_destroy = mdpy_fb_destroy,
93cacade19SGerd Hoffmann .fb_setcolreg = mdpy_fb_setcolreg,
94cacade19SGerd Hoffmann };
95cacade19SGerd Hoffmann
mdpy_fb_probe(struct pci_dev * pdev,const struct pci_device_id * ent)96cacade19SGerd Hoffmann static int mdpy_fb_probe(struct pci_dev *pdev,
97cacade19SGerd Hoffmann const struct pci_device_id *ent)
98cacade19SGerd Hoffmann {
99cacade19SGerd Hoffmann struct fb_info *info;
100cacade19SGerd Hoffmann struct mdpy_fb_par *par;
101cacade19SGerd Hoffmann u32 format, width, height;
102cacade19SGerd Hoffmann int ret;
103cacade19SGerd Hoffmann
104cacade19SGerd Hoffmann ret = pci_enable_device(pdev);
105cacade19SGerd Hoffmann if (ret < 0)
106cacade19SGerd Hoffmann return ret;
107cacade19SGerd Hoffmann
108cacade19SGerd Hoffmann ret = pci_request_regions(pdev, "mdpy-fb");
109cacade19SGerd Hoffmann if (ret < 0)
110d1f0f50fSShang XiaoJing goto err_disable_dev;
111cacade19SGerd Hoffmann
112cacade19SGerd Hoffmann pci_read_config_dword(pdev, MDPY_FORMAT_OFFSET, &format);
113cacade19SGerd Hoffmann pci_read_config_dword(pdev, MDPY_WIDTH_OFFSET, &width);
114cacade19SGerd Hoffmann pci_read_config_dword(pdev, MDPY_HEIGHT_OFFSET, &height);
115cacade19SGerd Hoffmann if (format != DRM_FORMAT_XRGB8888) {
116cacade19SGerd Hoffmann pci_err(pdev, "format mismatch (0x%x != 0x%x)\n",
117cacade19SGerd Hoffmann format, DRM_FORMAT_XRGB8888);
118752774ceSWei Yongjun ret = -EINVAL;
119752774ceSWei Yongjun goto err_release_regions;
120cacade19SGerd Hoffmann }
121cacade19SGerd Hoffmann if (width < 100 || width > 10000) {
122cacade19SGerd Hoffmann pci_err(pdev, "width (%d) out of range\n", width);
123752774ceSWei Yongjun ret = -EINVAL;
124752774ceSWei Yongjun goto err_release_regions;
125cacade19SGerd Hoffmann }
126cacade19SGerd Hoffmann if (height < 100 || height > 10000) {
127cacade19SGerd Hoffmann pci_err(pdev, "height (%d) out of range\n", height);
128752774ceSWei Yongjun ret = -EINVAL;
129752774ceSWei Yongjun goto err_release_regions;
130cacade19SGerd Hoffmann }
131cacade19SGerd Hoffmann pci_info(pdev, "mdpy found: %dx%d framebuffer\n",
132cacade19SGerd Hoffmann width, height);
133cacade19SGerd Hoffmann
134cacade19SGerd Hoffmann info = framebuffer_alloc(sizeof(struct mdpy_fb_par), &pdev->dev);
135752774ceSWei Yongjun if (!info) {
136752774ceSWei Yongjun ret = -ENOMEM;
137cacade19SGerd Hoffmann goto err_release_regions;
138752774ceSWei Yongjun }
139cacade19SGerd Hoffmann pci_set_drvdata(pdev, info);
140cacade19SGerd Hoffmann par = info->par;
141cacade19SGerd Hoffmann
142cacade19SGerd Hoffmann info->fix = mdpy_fb_fix;
143cacade19SGerd Hoffmann info->fix.smem_start = pci_resource_start(pdev, 0);
144cacade19SGerd Hoffmann info->fix.smem_len = pci_resource_len(pdev, 0);
145cacade19SGerd Hoffmann info->fix.line_length = width * 4;
146cacade19SGerd Hoffmann
147cacade19SGerd Hoffmann info->var = mdpy_fb_var;
148cacade19SGerd Hoffmann info->var.xres = width;
149cacade19SGerd Hoffmann info->var.yres = height;
150cacade19SGerd Hoffmann info->var.xres_virtual = width;
151cacade19SGerd Hoffmann info->var.yres_virtual = height;
152cacade19SGerd Hoffmann
153cacade19SGerd Hoffmann info->screen_size = info->fix.smem_len;
154cacade19SGerd Hoffmann info->screen_base = ioremap(info->fix.smem_start,
155cacade19SGerd Hoffmann info->screen_size);
156cacade19SGerd Hoffmann if (!info->screen_base) {
157cacade19SGerd Hoffmann pci_err(pdev, "ioremap(pcibar) failed\n");
158cacade19SGerd Hoffmann ret = -EIO;
159cacade19SGerd Hoffmann goto err_release_fb;
160cacade19SGerd Hoffmann }
161cacade19SGerd Hoffmann
162cacade19SGerd Hoffmann info->fbops = &mdpy_fb_ops;
163cacade19SGerd Hoffmann info->pseudo_palette = par->palette;
164cacade19SGerd Hoffmann
165cacade19SGerd Hoffmann ret = register_framebuffer(info);
166cacade19SGerd Hoffmann if (ret < 0) {
167cacade19SGerd Hoffmann pci_err(pdev, "mdpy-fb device register failed: %d\n", ret);
168cacade19SGerd Hoffmann goto err_unmap;
169cacade19SGerd Hoffmann }
170cacade19SGerd Hoffmann
171cacade19SGerd Hoffmann pci_info(pdev, "fb%d registered\n", info->node);
172cacade19SGerd Hoffmann return 0;
173cacade19SGerd Hoffmann
174cacade19SGerd Hoffmann err_unmap:
175cacade19SGerd Hoffmann iounmap(info->screen_base);
176cacade19SGerd Hoffmann
177cacade19SGerd Hoffmann err_release_fb:
178cacade19SGerd Hoffmann framebuffer_release(info);
179cacade19SGerd Hoffmann
180cacade19SGerd Hoffmann err_release_regions:
181cacade19SGerd Hoffmann pci_release_regions(pdev);
182cacade19SGerd Hoffmann
183d1f0f50fSShang XiaoJing err_disable_dev:
184d1f0f50fSShang XiaoJing pci_disable_device(pdev);
185d1f0f50fSShang XiaoJing
186cacade19SGerd Hoffmann return ret;
187cacade19SGerd Hoffmann }
188cacade19SGerd Hoffmann
mdpy_fb_remove(struct pci_dev * pdev)189cacade19SGerd Hoffmann static void mdpy_fb_remove(struct pci_dev *pdev)
190cacade19SGerd Hoffmann {
191cacade19SGerd Hoffmann struct fb_info *info = pci_get_drvdata(pdev);
192cacade19SGerd Hoffmann
193cacade19SGerd Hoffmann unregister_framebuffer(info);
194d1f0f50fSShang XiaoJing iounmap(info->screen_base);
195cacade19SGerd Hoffmann framebuffer_release(info);
196d1f0f50fSShang XiaoJing pci_release_regions(pdev);
197d1f0f50fSShang XiaoJing pci_disable_device(pdev);
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
mdpy_fb_init(void)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);
232*0756bec2SJeff Johnson MODULE_DESCRIPTION("Framebuffer driver for mdpy (mediated virtual pci display device)");
233cacade19SGerd Hoffmann MODULE_LICENSE("GPL v2");
234