Merge tag 'nfs-for-6.12-1' of git://git.linux-nfs.org/projects/anna/linux-nfs
[linux-2.6-block.git] / drivers / firmware / sysfb_simplefb.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
e3263ab3 2/*
8633ef82 3 * Generic System Framebuffers
e3263ab3 4 * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
e3263ab3
DH
5 */
6
7/*
8 * simple-framebuffer probing
9 * Try to convert "screen_info" into a "simple-framebuffer" compatible mode.
10 * If the mode is incompatible, we return "false" and let the caller create
11 * legacy nodes instead.
12 */
13
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/mm.h>
18#include <linux/platform_data/simplefb.h>
19#include <linux/platform_device.h>
20#include <linux/screen_info.h>
d391c582 21#include <linux/sysfb.h>
e3263ab3
DH
22
23static const char simplefb_resname[] = "BOOTFB";
24static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
25
8633ef82
JMC
26/* try parsing screen_info into a simple-framebuffer mode struct */
27__init bool sysfb_parse_mode(const struct screen_info *si,
28 struct simplefb_platform_data *mode)
e3263ab3 29{
e3263ab3 30 __u8 type;
f35cd3fa 31 u32 bits_per_pixel;
e3263ab3
DH
32 unsigned int i;
33
34 type = si->orig_video_isVGA;
35 if (type != VIDEO_TYPE_VLFB && type != VIDEO_TYPE_EFI)
36 return false;
37
f35cd3fa
TZ
38 /*
39 * The meaning of depth and bpp for direct-color formats is
40 * inconsistent:
41 *
42 * - DRM format info specifies depth as the number of color
43 * bits; including alpha, but not including filler bits.
44 * - Linux' EFI platform code computes lfb_depth from the
45 * individual color channels, including the reserved bits.
46 * - VBE 1.1 defines lfb_depth for XRGB1555 as 16, but later
47 * versions use 15.
48 * - On the kernel command line, 'bpp' of 32 is usually
49 * XRGB8888 including the filler bits, but 15 is XRGB1555
50 * not including the filler bit.
51 *
52 * It's not easily possible to fix this in struct screen_info,
53 * as this could break UAPI. The best solution is to compute
1b617bc9
PA
54 * bits_per_pixel from the color bits, reserved bits and
55 * reported lfb_depth, whichever is highest. In the loop below,
f35cd3fa
TZ
56 * ignore simplefb formats with alpha bits, as EFI and VESA
57 * don't specify alpha channels.
58 */
59 if (si->lfb_depth > 8) {
60 bits_per_pixel = max(max3(si->red_size + si->red_pos,
61 si->green_size + si->green_pos,
62 si->blue_size + si->blue_pos),
63 si->rsvd_size + si->rsvd_pos);
1b617bc9 64 bits_per_pixel = max_t(u32, bits_per_pixel, si->lfb_depth);
f35cd3fa
TZ
65 } else {
66 bits_per_pixel = si->lfb_depth;
67 }
68
e3263ab3 69 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
f35cd3fa
TZ
70 const struct simplefb_format *f = &formats[i];
71
72 if (f->transp.length)
73 continue; /* transparent formats are unsupported by VESA/EFI */
74
75 if (bits_per_pixel == f->bits_per_pixel &&
e3263ab3
DH
76 si->red_size == f->red.length &&
77 si->red_pos == f->red.offset &&
78 si->green_size == f->green.length &&
79 si->green_pos == f->green.offset &&
80 si->blue_size == f->blue.length &&
f35cd3fa 81 si->blue_pos == f->blue.offset) {
e3263ab3
DH
82 mode->format = f->name;
83 mode->width = si->lfb_width;
84 mode->height = si->lfb_height;
85 mode->stride = si->lfb_linelength;
86 return true;
87 }
88 }
89
90 return false;
91}
92
0949ee75 93__init struct platform_device *sysfb_create_simplefb(const struct screen_info *si,
9eac534d
TZ
94 const struct simplefb_platform_data *mode,
95 struct device *parent)
e3263ab3
DH
96{
97 struct platform_device *pd;
98 struct resource res;
f96acec8
DH
99 u64 base, size;
100 u32 length;
8633ef82 101 int ret;
9164b4ce
DH
102
103 /*
104 * If the 64BIT_BASE capability is set, ext_lfb_base will contain the
105 * upper half of the base address. Assemble the address, then make sure
106 * it is valid and we can actually access it.
107 */
108 base = si->lfb_base;
109 if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE)
110 base |= (u64)si->ext_lfb_base << 32;
111 if (!base || (u64)(resource_size_t)base != base) {
112 printk(KERN_DEBUG "sysfb: inaccessible VRAM base\n");
0949ee75 113 return ERR_PTR(-EINVAL);
9164b4ce 114 }
e3263ab3 115
f96acec8
DH
116 /*
117 * Don't use lfb_size as IORESOURCE size, since it may contain the
118 * entire VMEM, and thus require huge mappings. Use just the part we
119 * need, that is, the part where the framebuffer is located. But verify
120 * that it does not exceed the advertised VMEM.
121 * Note that in case of VBE, the lfb_size is shifted by 16 bits for
122 * historical reasons.
123 */
124 size = si->lfb_size;
125 if (si->orig_video_isVGA == VIDEO_TYPE_VLFB)
126 size <<= 16;
127 length = mode->height * mode->stride;
f96acec8 128 if (length > size) {
e3263ab3 129 printk(KERN_WARNING "sysfb: VRAM smaller than advertised\n");
0949ee75 130 return ERR_PTR(-EINVAL);
e3263ab3 131 }
dacc9092 132 length = PAGE_ALIGN(length);
e3263ab3
DH
133
134 /* setup IORESOURCE_MEM as framebuffer memory */
135 memset(&res, 0, sizeof(res));
c9689834 136 res.flags = IORESOURCE_MEM;
e3263ab3 137 res.name = simplefb_resname;
9164b4ce 138 res.start = base;
f96acec8 139 res.end = res.start + length - 1;
e3263ab3 140 if (res.end <= res.start)
0949ee75 141 return ERR_PTR(-EINVAL);
e3263ab3 142
8633ef82
JMC
143 pd = platform_device_alloc("simple-framebuffer", 0);
144 if (!pd)
0949ee75 145 return ERR_PTR(-ENOMEM);
8633ef82 146
9eac534d
TZ
147 pd->dev.parent = parent;
148
3615c786 149 sysfb_set_efifb_fwnode(pd);
8633ef82
JMC
150
151 ret = platform_device_add_resources(pd, &res, 1);
202c0891
JH
152 if (ret)
153 goto err_put_device;
8633ef82
JMC
154
155 ret = platform_device_add_data(pd, mode, sizeof(*mode));
202c0891
JH
156 if (ret)
157 goto err_put_device;
158
159 ret = platform_device_add(pd);
160 if (ret)
161 goto err_put_device;
162
0949ee75 163 return pd;
202c0891
JH
164
165err_put_device:
166 platform_device_put(pd);
8633ef82 167
0949ee75 168 return ERR_PTR(ret);
e3263ab3 169}