196 lines · c
1// SPDX-License-Identifier: GPL-2.0-or-later2/*3 * Generic System Framebuffers4 * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>5 */6 7/*8 * Simple-Framebuffer support9 * Create a platform-device for any available boot framebuffer. The10 * simple-framebuffer platform device is already available on DT systems, so11 * this module parses the global "screen_info" object and creates a suitable12 * platform device compatible with the "simple-framebuffer" DT object. If13 * the framebuffer is incompatible, we instead create a legacy14 * "vesa-framebuffer", "efi-framebuffer" or "platform-framebuffer" device and15 * pass the screen_info as platform_data. This allows legacy drivers16 * to pick these devices up without messing with simple-framebuffer drivers.17 * The global "screen_info" is still valid at all times.18 *19 * If CONFIG_SYSFB_SIMPLEFB is not selected, never register "simple-framebuffer"20 * platform devices, but only use legacy framebuffer devices for21 * backwards compatibility.22 *23 * TODO: We set the dev_id field of all platform-devices to 0. This allows24 * other OF/DT parsers to create such devices, too. However, they must25 * start at offset 1 for this to work.26 */27 28#include <linux/err.h>29#include <linux/init.h>30#include <linux/kernel.h>31#include <linux/mm.h>32#include <linux/pci.h>33#include <linux/platform_data/simplefb.h>34#include <linux/platform_device.h>35#include <linux/screen_info.h>36#include <linux/sysfb.h>37 38static struct platform_device *pd;39static DEFINE_MUTEX(disable_lock);40static bool disabled;41 42static struct device *sysfb_parent_dev(const struct screen_info *si);43 44static bool sysfb_unregister(void)45{46 if (IS_ERR_OR_NULL(pd))47 return false;48 49 platform_device_unregister(pd);50 pd = NULL;51 52 return true;53}54 55/**56 * sysfb_disable() - disable the Generic System Framebuffers support57 * @dev: the device to check if non-NULL58 *59 * This disables the registration of system framebuffer devices that match the60 * generic drivers that make use of the system framebuffer set up by firmware.61 *62 * It also unregisters a device if this was already registered by sysfb_init().63 *64 * Context: The function can sleep. A @disable_lock mutex is acquired to serialize65 * against sysfb_init(), that registers a system framebuffer device.66 */67void sysfb_disable(struct device *dev)68{69 struct screen_info *si = &screen_info;70 struct device *parent;71 72 mutex_lock(&disable_lock);73 parent = sysfb_parent_dev(si);74 if (!dev || !parent || dev == parent) {75 sysfb_unregister();76 disabled = true;77 }78 mutex_unlock(&disable_lock);79}80EXPORT_SYMBOL_GPL(sysfb_disable);81 82#if defined(CONFIG_PCI)83static bool sysfb_pci_dev_is_enabled(struct pci_dev *pdev)84{85 /*86 * TODO: Try to integrate this code into the PCI subsystem87 */88 int ret;89 u16 command;90 91 ret = pci_read_config_word(pdev, PCI_COMMAND, &command);92 if (ret != PCIBIOS_SUCCESSFUL)93 return false;94 if (!(command & PCI_COMMAND_MEMORY))95 return false;96 return true;97}98#else99static bool sysfb_pci_dev_is_enabled(struct pci_dev *pdev)100{101 return false;102}103#endif104 105static struct device *sysfb_parent_dev(const struct screen_info *si)106{107 struct pci_dev *pdev;108 109 pdev = screen_info_pci_dev(si);110 if (IS_ERR(pdev)) {111 return ERR_CAST(pdev);112 } else if (pdev) {113 if (!sysfb_pci_dev_is_enabled(pdev)) {114 pci_dev_put(pdev);115 return ERR_PTR(-ENODEV);116 }117 return &pdev->dev;118 }119 120 return NULL;121}122 123static __init int sysfb_init(void)124{125 struct screen_info *si = &screen_info;126 struct device *parent;127 struct simplefb_platform_data mode;128 const char *name;129 bool compatible;130 int ret = 0;131 132 screen_info_apply_fixups();133 134 mutex_lock(&disable_lock);135 if (disabled)136 goto unlock_mutex;137 138 sysfb_apply_efi_quirks();139 140 parent = sysfb_parent_dev(si);141 if (IS_ERR(parent)) {142 ret = PTR_ERR(parent);143 goto unlock_mutex;144 }145 146 /* try to create a simple-framebuffer device */147 compatible = sysfb_parse_mode(si, &mode);148 if (compatible) {149 pd = sysfb_create_simplefb(si, &mode, parent);150 if (!IS_ERR(pd))151 goto put_device;152 }153 154 /* if the FB is incompatible, create a legacy framebuffer device */155 if (si->orig_video_isVGA == VIDEO_TYPE_EFI)156 name = "efi-framebuffer";157 else if (si->orig_video_isVGA == VIDEO_TYPE_VLFB)158 name = "vesa-framebuffer";159 else if (si->orig_video_isVGA == VIDEO_TYPE_VGAC)160 name = "vga-framebuffer";161 else if (si->orig_video_isVGA == VIDEO_TYPE_EGAC)162 name = "ega-framebuffer";163 else164 name = "platform-framebuffer";165 166 pd = platform_device_alloc(name, 0);167 if (!pd) {168 ret = -ENOMEM;169 goto put_device;170 }171 172 pd->dev.parent = parent;173 174 sysfb_set_efifb_fwnode(pd);175 176 ret = platform_device_add_data(pd, si, sizeof(*si));177 if (ret)178 goto err;179 180 ret = platform_device_add(pd);181 if (ret)182 goto err;183 184 goto put_device;185err:186 platform_device_put(pd);187put_device:188 put_device(parent);189unlock_mutex:190 mutex_unlock(&disable_lock);191 return ret;192}193 194/* must execute after PCI subsystem for EFI quirks */195device_initcall(sysfb_init);196