// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Generic System Framebuffers3* Copyright (c) 2012-2013 David Herrmann <[email protected]>4*/56/*7* Simple-Framebuffer support8* Create a platform-device for any available boot framebuffer. The9* simple-framebuffer platform device is already available on DT systems, so10* this module parses the global "screen_info" object and creates a suitable11* platform device compatible with the "simple-framebuffer" DT object. If12* the framebuffer is incompatible, we instead create a legacy13* "vesa-framebuffer", "efi-framebuffer" or "platform-framebuffer" device and14* pass the screen_info as platform_data. This allows legacy drivers15* to pick these devices up without messing with simple-framebuffer drivers.16* The global "screen_info" is still valid at all times.17*18* If CONFIG_SYSFB_SIMPLEFB is not selected, never register "simple-framebuffer"19* platform devices, but only use legacy framebuffer devices for20* backwards compatibility.21*22* TODO: We set the dev_id field of all platform-devices to 0. This allows23* other OF/DT parsers to create such devices, too. However, they must24* start at offset 1 for this to work.25*/2627#include <linux/err.h>28#include <linux/init.h>29#include <linux/kernel.h>30#include <linux/mm.h>31#include <linux/pci.h>32#include <linux/platform_data/simplefb.h>33#include <linux/platform_device.h>34#include <linux/screen_info.h>35#include <linux/sysfb.h>3637static struct platform_device *pd;38static DEFINE_MUTEX(disable_lock);39static bool disabled;4041static struct device *sysfb_parent_dev(const struct screen_info *si);4243static bool sysfb_unregister(void)44{45if (IS_ERR_OR_NULL(pd))46return false;4748platform_device_unregister(pd);49pd = NULL;5051return true;52}5354/**55* sysfb_disable() - disable the Generic System Framebuffers support56* @dev: the device to check if non-NULL57*58* This disables the registration of system framebuffer devices that match the59* generic drivers that make use of the system framebuffer set up by firmware.60*61* It also unregisters a device if this was already registered by sysfb_init().62*63* Context: The function can sleep. A @disable_lock mutex is acquired to serialize64* against sysfb_init(), that registers a system framebuffer device.65*/66void sysfb_disable(struct device *dev)67{68struct screen_info *si = &screen_info;69struct device *parent;7071mutex_lock(&disable_lock);72parent = sysfb_parent_dev(si);73if (!dev || !parent || dev == parent) {74sysfb_unregister();75disabled = true;76}77mutex_unlock(&disable_lock);78}79EXPORT_SYMBOL_GPL(sysfb_disable);8081/**82* sysfb_handles_screen_info() - reports if sysfb handles the global screen_info83*84* Callers can use sysfb_handles_screen_info() to determine whether the Generic85* System Framebuffers (sysfb) can handle the global screen_info data structure86* or not. Drivers might need this information to know if they have to setup the87* system framebuffer, or if they have to delegate this action to sysfb instead.88*89* Returns:90* True if sysfb handles the global screen_info data structure.91*/92bool sysfb_handles_screen_info(void)93{94const struct screen_info *si = &screen_info;9596return !!screen_info_video_type(si);97}98EXPORT_SYMBOL_GPL(sysfb_handles_screen_info);99100#if defined(CONFIG_PCI)101static bool sysfb_pci_dev_is_enabled(struct pci_dev *pdev)102{103/*104* TODO: Try to integrate this code into the PCI subsystem105*/106int ret;107u16 command;108109ret = pci_read_config_word(pdev, PCI_COMMAND, &command);110if (ret != PCIBIOS_SUCCESSFUL)111return false;112if (!(command & PCI_COMMAND_MEMORY))113return false;114return true;115}116#else117static bool sysfb_pci_dev_is_enabled(struct pci_dev *pdev)118{119return false;120}121#endif122123static struct device *sysfb_parent_dev(const struct screen_info *si)124{125struct pci_dev *pdev;126127pdev = screen_info_pci_dev(si);128if (IS_ERR(pdev)) {129return ERR_CAST(pdev);130} else if (pdev) {131if (!sysfb_pci_dev_is_enabled(pdev)) {132pci_dev_put(pdev);133return ERR_PTR(-ENODEV);134}135return &pdev->dev;136}137138return NULL;139}140141static __init int sysfb_init(void)142{143struct screen_info *si = &screen_info;144struct device *parent;145unsigned int type;146struct simplefb_platform_data mode;147const char *name;148bool compatible;149int ret = 0;150151screen_info_apply_fixups();152153mutex_lock(&disable_lock);154if (disabled)155goto unlock_mutex;156157sysfb_apply_efi_quirks();158159parent = sysfb_parent_dev(si);160if (IS_ERR(parent)) {161ret = PTR_ERR(parent);162goto unlock_mutex;163}164165/* try to create a simple-framebuffer device */166compatible = sysfb_parse_mode(si, &mode);167if (compatible) {168pd = sysfb_create_simplefb(si, &mode, parent);169if (!IS_ERR(pd))170goto put_device;171}172173type = screen_info_video_type(si);174175/* if the FB is incompatible, create a legacy framebuffer device */176switch (type) {177case VIDEO_TYPE_EGAC:178name = "ega-framebuffer";179break;180case VIDEO_TYPE_VGAC:181name = "vga-framebuffer";182break;183case VIDEO_TYPE_VLFB:184name = "vesa-framebuffer";185break;186case VIDEO_TYPE_EFI:187name = "efi-framebuffer";188break;189default:190name = "platform-framebuffer";191break;192}193194pd = platform_device_alloc(name, 0);195if (!pd) {196ret = -ENOMEM;197goto put_device;198}199200pd->dev.parent = parent;201202sysfb_set_efifb_fwnode(pd);203204ret = platform_device_add_data(pd, si, sizeof(*si));205if (ret)206goto err;207208ret = platform_device_add(pd);209if (ret)210goto err;211212goto put_device;213err:214platform_device_put(pd);215put_device:216put_device(parent);217unlock_mutex:218mutex_unlock(&disable_lock);219return ret;220}221222/* must execute after PCI subsystem for EFI quirks */223device_initcall(sysfb_init);224225226