Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 2 | /* |
Javier Martinez Canillas | 8633ef8 | 2021-06-25 15:13:59 +0200 | [diff] [blame] | 3 | * Generic System Framebuffers |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 4 | * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com> |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 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> |
Javier Martinez Canillas | d391c58 | 2021-06-25 15:09:46 +0200 | [diff] [blame] | 21 | #include <linux/sysfb.h> |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 22 | |
| 23 | static const char simplefb_resname[] = "BOOTFB"; |
| 24 | static const struct simplefb_format formats[] = SIMPLEFB_FORMATS; |
| 25 | |
Javier Martinez Canillas | 8633ef8 | 2021-06-25 15:13:59 +0200 | [diff] [blame] | 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) |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 29 | { |
| 30 | const struct simplefb_format *f; |
| 31 | __u8 type; |
| 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 | |
| 38 | for (i = 0; i < ARRAY_SIZE(formats); ++i) { |
| 39 | f = &formats[i]; |
| 40 | if (si->lfb_depth == f->bits_per_pixel && |
| 41 | si->red_size == f->red.length && |
| 42 | si->red_pos == f->red.offset && |
| 43 | si->green_size == f->green.length && |
| 44 | si->green_pos == f->green.offset && |
| 45 | si->blue_size == f->blue.length && |
| 46 | si->blue_pos == f->blue.offset && |
| 47 | si->rsvd_size == f->transp.length && |
| 48 | si->rsvd_pos == f->transp.offset) { |
| 49 | mode->format = f->name; |
| 50 | mode->width = si->lfb_width; |
| 51 | mode->height = si->lfb_height; |
| 52 | mode->stride = si->lfb_linelength; |
| 53 | return true; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return false; |
| 58 | } |
| 59 | |
Javier Martinez Canillas | 8633ef8 | 2021-06-25 15:13:59 +0200 | [diff] [blame] | 60 | __init int sysfb_create_simplefb(const struct screen_info *si, |
| 61 | const struct simplefb_platform_data *mode) |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 62 | { |
| 63 | struct platform_device *pd; |
| 64 | struct resource res; |
David Herrmann | f96acec | 2016-11-15 13:01:58 +0100 | [diff] [blame] | 65 | u64 base, size; |
| 66 | u32 length; |
Javier Martinez Canillas | 8633ef8 | 2021-06-25 15:13:59 +0200 | [diff] [blame] | 67 | int ret; |
David Herrmann | 9164b4c | 2016-11-15 13:01:57 +0100 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * If the 64BIT_BASE capability is set, ext_lfb_base will contain the |
| 71 | * upper half of the base address. Assemble the address, then make sure |
| 72 | * it is valid and we can actually access it. |
| 73 | */ |
| 74 | base = si->lfb_base; |
| 75 | if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE) |
| 76 | base |= (u64)si->ext_lfb_base << 32; |
| 77 | if (!base || (u64)(resource_size_t)base != base) { |
| 78 | printk(KERN_DEBUG "sysfb: inaccessible VRAM base\n"); |
| 79 | return -EINVAL; |
| 80 | } |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 81 | |
David Herrmann | f96acec | 2016-11-15 13:01:58 +0100 | [diff] [blame] | 82 | /* |
| 83 | * Don't use lfb_size as IORESOURCE size, since it may contain the |
| 84 | * entire VMEM, and thus require huge mappings. Use just the part we |
| 85 | * need, that is, the part where the framebuffer is located. But verify |
| 86 | * that it does not exceed the advertised VMEM. |
| 87 | * Note that in case of VBE, the lfb_size is shifted by 16 bits for |
| 88 | * historical reasons. |
| 89 | */ |
| 90 | size = si->lfb_size; |
| 91 | if (si->orig_video_isVGA == VIDEO_TYPE_VLFB) |
| 92 | size <<= 16; |
| 93 | length = mode->height * mode->stride; |
David Herrmann | f96acec | 2016-11-15 13:01:58 +0100 | [diff] [blame] | 94 | if (length > size) { |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 95 | printk(KERN_WARNING "sysfb: VRAM smaller than advertised\n"); |
| 96 | return -EINVAL; |
| 97 | } |
Arvind Sankar | dacc909 | 2020-01-07 18:04:10 -0500 | [diff] [blame] | 98 | length = PAGE_ALIGN(length); |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 99 | |
| 100 | /* setup IORESOURCE_MEM as framebuffer memory */ |
| 101 | memset(&res, 0, sizeof(res)); |
David Herrmann | 29d274b | 2013-10-02 16:41:04 +0200 | [diff] [blame] | 102 | res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 103 | res.name = simplefb_resname; |
David Herrmann | 9164b4c | 2016-11-15 13:01:57 +0100 | [diff] [blame] | 104 | res.start = base; |
David Herrmann | f96acec | 2016-11-15 13:01:58 +0100 | [diff] [blame] | 105 | res.end = res.start + length - 1; |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 106 | if (res.end <= res.start) |
| 107 | return -EINVAL; |
| 108 | |
Javier Martinez Canillas | 8633ef8 | 2021-06-25 15:13:59 +0200 | [diff] [blame] | 109 | pd = platform_device_alloc("simple-framebuffer", 0); |
| 110 | if (!pd) |
| 111 | return -ENOMEM; |
| 112 | |
| 113 | sysfb_apply_efi_quirks(pd); |
| 114 | |
| 115 | ret = platform_device_add_resources(pd, &res, 1); |
Miaoqian Lin | 0589e88 | 2021-12-31 08:04:25 +0000 | [diff] [blame] | 116 | if (ret) { |
| 117 | platform_device_put(pd); |
Javier Martinez Canillas | 8633ef8 | 2021-06-25 15:13:59 +0200 | [diff] [blame] | 118 | return ret; |
Miaoqian Lin | 0589e88 | 2021-12-31 08:04:25 +0000 | [diff] [blame] | 119 | } |
Javier Martinez Canillas | 8633ef8 | 2021-06-25 15:13:59 +0200 | [diff] [blame] | 120 | |
| 121 | ret = platform_device_add_data(pd, mode, sizeof(*mode)); |
Miaoqian Lin | 0589e88 | 2021-12-31 08:04:25 +0000 | [diff] [blame] | 122 | if (ret) { |
| 123 | platform_device_put(pd); |
Javier Martinez Canillas | 8633ef8 | 2021-06-25 15:13:59 +0200 | [diff] [blame] | 124 | return ret; |
Miaoqian Lin | 0589e88 | 2021-12-31 08:04:25 +0000 | [diff] [blame] | 125 | } |
Javier Martinez Canillas | 8633ef8 | 2021-06-25 15:13:59 +0200 | [diff] [blame] | 126 | |
| 127 | return platform_device_add(pd); |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 128 | } |