162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Generic System Framebuffers 462306a36Sopenharmony_ci * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com> 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci/* 862306a36Sopenharmony_ci * simple-framebuffer probing 962306a36Sopenharmony_ci * Try to convert "screen_info" into a "simple-framebuffer" compatible mode. 1062306a36Sopenharmony_ci * If the mode is incompatible, we return "false" and let the caller create 1162306a36Sopenharmony_ci * legacy nodes instead. 1262306a36Sopenharmony_ci */ 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <linux/err.h> 1562306a36Sopenharmony_ci#include <linux/init.h> 1662306a36Sopenharmony_ci#include <linux/kernel.h> 1762306a36Sopenharmony_ci#include <linux/mm.h> 1862306a36Sopenharmony_ci#include <linux/platform_data/simplefb.h> 1962306a36Sopenharmony_ci#include <linux/platform_device.h> 2062306a36Sopenharmony_ci#include <linux/screen_info.h> 2162306a36Sopenharmony_ci#include <linux/sysfb.h> 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_cistatic const char simplefb_resname[] = "BOOTFB"; 2462306a36Sopenharmony_cistatic const struct simplefb_format formats[] = SIMPLEFB_FORMATS; 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci/* try parsing screen_info into a simple-framebuffer mode struct */ 2762306a36Sopenharmony_ci__init bool sysfb_parse_mode(const struct screen_info *si, 2862306a36Sopenharmony_ci struct simplefb_platform_data *mode) 2962306a36Sopenharmony_ci{ 3062306a36Sopenharmony_ci __u8 type; 3162306a36Sopenharmony_ci u32 bits_per_pixel; 3262306a36Sopenharmony_ci unsigned int i; 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci type = si->orig_video_isVGA; 3562306a36Sopenharmony_ci if (type != VIDEO_TYPE_VLFB && type != VIDEO_TYPE_EFI) 3662306a36Sopenharmony_ci return false; 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci /* 3962306a36Sopenharmony_ci * The meaning of depth and bpp for direct-color formats is 4062306a36Sopenharmony_ci * inconsistent: 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * - DRM format info specifies depth as the number of color 4362306a36Sopenharmony_ci * bits; including alpha, but not including filler bits. 4462306a36Sopenharmony_ci * - Linux' EFI platform code computes lfb_depth from the 4562306a36Sopenharmony_ci * individual color channels, including the reserved bits. 4662306a36Sopenharmony_ci * - VBE 1.1 defines lfb_depth for XRGB1555 as 16, but later 4762306a36Sopenharmony_ci * versions use 15. 4862306a36Sopenharmony_ci * - On the kernel command line, 'bpp' of 32 is usually 4962306a36Sopenharmony_ci * XRGB8888 including the filler bits, but 15 is XRGB1555 5062306a36Sopenharmony_ci * not including the filler bit. 5162306a36Sopenharmony_ci * 5262306a36Sopenharmony_ci * It's not easily possible to fix this in struct screen_info, 5362306a36Sopenharmony_ci * as this could break UAPI. The best solution is to compute 5462306a36Sopenharmony_ci * bits_per_pixel from the color bits, reserved bits and 5562306a36Sopenharmony_ci * reported lfb_depth, whichever is highest. In the loop below, 5662306a36Sopenharmony_ci * ignore simplefb formats with alpha bits, as EFI and VESA 5762306a36Sopenharmony_ci * don't specify alpha channels. 5862306a36Sopenharmony_ci */ 5962306a36Sopenharmony_ci if (si->lfb_depth > 8) { 6062306a36Sopenharmony_ci bits_per_pixel = max(max3(si->red_size + si->red_pos, 6162306a36Sopenharmony_ci si->green_size + si->green_pos, 6262306a36Sopenharmony_ci si->blue_size + si->blue_pos), 6362306a36Sopenharmony_ci si->rsvd_size + si->rsvd_pos); 6462306a36Sopenharmony_ci bits_per_pixel = max_t(u32, bits_per_pixel, si->lfb_depth); 6562306a36Sopenharmony_ci } else { 6662306a36Sopenharmony_ci bits_per_pixel = si->lfb_depth; 6762306a36Sopenharmony_ci } 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(formats); ++i) { 7062306a36Sopenharmony_ci const struct simplefb_format *f = &formats[i]; 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci if (f->transp.length) 7362306a36Sopenharmony_ci continue; /* transparent formats are unsupported by VESA/EFI */ 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci if (bits_per_pixel == f->bits_per_pixel && 7662306a36Sopenharmony_ci si->red_size == f->red.length && 7762306a36Sopenharmony_ci si->red_pos == f->red.offset && 7862306a36Sopenharmony_ci si->green_size == f->green.length && 7962306a36Sopenharmony_ci si->green_pos == f->green.offset && 8062306a36Sopenharmony_ci si->blue_size == f->blue.length && 8162306a36Sopenharmony_ci si->blue_pos == f->blue.offset) { 8262306a36Sopenharmony_ci mode->format = f->name; 8362306a36Sopenharmony_ci mode->width = si->lfb_width; 8462306a36Sopenharmony_ci mode->height = si->lfb_height; 8562306a36Sopenharmony_ci mode->stride = si->lfb_linelength; 8662306a36Sopenharmony_ci return true; 8762306a36Sopenharmony_ci } 8862306a36Sopenharmony_ci } 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci return false; 9162306a36Sopenharmony_ci} 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci__init struct platform_device *sysfb_create_simplefb(const struct screen_info *si, 9462306a36Sopenharmony_ci const struct simplefb_platform_data *mode) 9562306a36Sopenharmony_ci{ 9662306a36Sopenharmony_ci struct platform_device *pd; 9762306a36Sopenharmony_ci struct resource res; 9862306a36Sopenharmony_ci u64 base, size; 9962306a36Sopenharmony_ci u32 length; 10062306a36Sopenharmony_ci int ret; 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci /* 10362306a36Sopenharmony_ci * If the 64BIT_BASE capability is set, ext_lfb_base will contain the 10462306a36Sopenharmony_ci * upper half of the base address. Assemble the address, then make sure 10562306a36Sopenharmony_ci * it is valid and we can actually access it. 10662306a36Sopenharmony_ci */ 10762306a36Sopenharmony_ci base = si->lfb_base; 10862306a36Sopenharmony_ci if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE) 10962306a36Sopenharmony_ci base |= (u64)si->ext_lfb_base << 32; 11062306a36Sopenharmony_ci if (!base || (u64)(resource_size_t)base != base) { 11162306a36Sopenharmony_ci printk(KERN_DEBUG "sysfb: inaccessible VRAM base\n"); 11262306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 11362306a36Sopenharmony_ci } 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci /* 11662306a36Sopenharmony_ci * Don't use lfb_size as IORESOURCE size, since it may contain the 11762306a36Sopenharmony_ci * entire VMEM, and thus require huge mappings. Use just the part we 11862306a36Sopenharmony_ci * need, that is, the part where the framebuffer is located. But verify 11962306a36Sopenharmony_ci * that it does not exceed the advertised VMEM. 12062306a36Sopenharmony_ci * Note that in case of VBE, the lfb_size is shifted by 16 bits for 12162306a36Sopenharmony_ci * historical reasons. 12262306a36Sopenharmony_ci */ 12362306a36Sopenharmony_ci size = si->lfb_size; 12462306a36Sopenharmony_ci if (si->orig_video_isVGA == VIDEO_TYPE_VLFB) 12562306a36Sopenharmony_ci size <<= 16; 12662306a36Sopenharmony_ci length = mode->height * mode->stride; 12762306a36Sopenharmony_ci if (length > size) { 12862306a36Sopenharmony_ci printk(KERN_WARNING "sysfb: VRAM smaller than advertised\n"); 12962306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 13062306a36Sopenharmony_ci } 13162306a36Sopenharmony_ci length = PAGE_ALIGN(length); 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci /* setup IORESOURCE_MEM as framebuffer memory */ 13462306a36Sopenharmony_ci memset(&res, 0, sizeof(res)); 13562306a36Sopenharmony_ci res.flags = IORESOURCE_MEM; 13662306a36Sopenharmony_ci res.name = simplefb_resname; 13762306a36Sopenharmony_ci res.start = base; 13862306a36Sopenharmony_ci res.end = res.start + length - 1; 13962306a36Sopenharmony_ci if (res.end <= res.start) 14062306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci pd = platform_device_alloc("simple-framebuffer", 0); 14362306a36Sopenharmony_ci if (!pd) 14462306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci sysfb_set_efifb_fwnode(pd); 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci ret = platform_device_add_resources(pd, &res, 1); 14962306a36Sopenharmony_ci if (ret) 15062306a36Sopenharmony_ci goto err_put_device; 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci ret = platform_device_add_data(pd, mode, sizeof(*mode)); 15362306a36Sopenharmony_ci if (ret) 15462306a36Sopenharmony_ci goto err_put_device; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci ret = platform_device_add(pd); 15762306a36Sopenharmony_ci if (ret) 15862306a36Sopenharmony_ci goto err_put_device; 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci return pd; 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_cierr_put_device: 16362306a36Sopenharmony_ci platform_device_put(pd); 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci return ERR_PTR(ret); 16662306a36Sopenharmony_ci} 167