David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Generic System Framebuffers on x86 |
| 3 | * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by the Free |
| 7 | * Software Foundation; either version 2 of the License, or (at your option) |
| 8 | * any later version. |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * simple-framebuffer probing |
| 13 | * Try to convert "screen_info" into a "simple-framebuffer" compatible mode. |
| 14 | * If the mode is incompatible, we return "false" and let the caller create |
| 15 | * legacy nodes instead. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/platform_data/simplefb.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/screen_info.h> |
| 25 | #include <asm/sysfb.h> |
| 26 | |
| 27 | static const char simplefb_resname[] = "BOOTFB"; |
| 28 | static const struct simplefb_format formats[] = SIMPLEFB_FORMATS; |
| 29 | |
| 30 | /* try parsing x86 screen_info into a simple-framebuffer mode struct */ |
| 31 | __init bool parse_mode(const struct screen_info *si, |
| 32 | struct simplefb_platform_data *mode) |
| 33 | { |
| 34 | const struct simplefb_format *f; |
| 35 | __u8 type; |
| 36 | unsigned int i; |
| 37 | |
| 38 | type = si->orig_video_isVGA; |
| 39 | if (type != VIDEO_TYPE_VLFB && type != VIDEO_TYPE_EFI) |
| 40 | return false; |
| 41 | |
| 42 | for (i = 0; i < ARRAY_SIZE(formats); ++i) { |
| 43 | f = &formats[i]; |
| 44 | if (si->lfb_depth == f->bits_per_pixel && |
| 45 | si->red_size == f->red.length && |
| 46 | si->red_pos == f->red.offset && |
| 47 | si->green_size == f->green.length && |
| 48 | si->green_pos == f->green.offset && |
| 49 | si->blue_size == f->blue.length && |
| 50 | si->blue_pos == f->blue.offset && |
| 51 | si->rsvd_size == f->transp.length && |
| 52 | si->rsvd_pos == f->transp.offset) { |
| 53 | mode->format = f->name; |
| 54 | mode->width = si->lfb_width; |
| 55 | mode->height = si->lfb_height; |
| 56 | mode->stride = si->lfb_linelength; |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | __init int create_simplefb(const struct screen_info *si, |
| 65 | const struct simplefb_platform_data *mode) |
| 66 | { |
| 67 | struct platform_device *pd; |
| 68 | struct resource res; |
David Herrmann | f96acec | 2016-11-15 13:01:58 +0100 | [diff] [blame] | 69 | u64 base, size; |
| 70 | u32 length; |
David Herrmann | 9164b4c | 2016-11-15 13:01:57 +0100 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * If the 64BIT_BASE capability is set, ext_lfb_base will contain the |
| 74 | * upper half of the base address. Assemble the address, then make sure |
| 75 | * it is valid and we can actually access it. |
| 76 | */ |
| 77 | base = si->lfb_base; |
| 78 | if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE) |
| 79 | base |= (u64)si->ext_lfb_base << 32; |
| 80 | if (!base || (u64)(resource_size_t)base != base) { |
| 81 | printk(KERN_DEBUG "sysfb: inaccessible VRAM base\n"); |
| 82 | return -EINVAL; |
| 83 | } |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 84 | |
David Herrmann | f96acec | 2016-11-15 13:01:58 +0100 | [diff] [blame] | 85 | /* |
| 86 | * Don't use lfb_size as IORESOURCE size, since it may contain the |
| 87 | * entire VMEM, and thus require huge mappings. Use just the part we |
| 88 | * need, that is, the part where the framebuffer is located. But verify |
| 89 | * that it does not exceed the advertised VMEM. |
| 90 | * Note that in case of VBE, the lfb_size is shifted by 16 bits for |
| 91 | * historical reasons. |
| 92 | */ |
| 93 | size = si->lfb_size; |
| 94 | if (si->orig_video_isVGA == VIDEO_TYPE_VLFB) |
| 95 | size <<= 16; |
| 96 | length = mode->height * mode->stride; |
David Herrmann | f96acec | 2016-11-15 13:01:58 +0100 | [diff] [blame] | 97 | if (length > size) { |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 98 | printk(KERN_WARNING "sysfb: VRAM smaller than advertised\n"); |
| 99 | return -EINVAL; |
| 100 | } |
Arvind Sankar | a5f3092 | 2020-01-07 18:04:10 -0500 | [diff] [blame] | 101 | length = PAGE_ALIGN(length); |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 102 | |
| 103 | /* setup IORESOURCE_MEM as framebuffer memory */ |
| 104 | memset(&res, 0, sizeof(res)); |
David Herrmann | 29d274b | 2013-10-02 16:41:04 +0200 | [diff] [blame] | 105 | res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 106 | res.name = simplefb_resname; |
David Herrmann | 9164b4c | 2016-11-15 13:01:57 +0100 | [diff] [blame] | 107 | res.start = base; |
David Herrmann | f96acec | 2016-11-15 13:01:58 +0100 | [diff] [blame] | 108 | res.end = res.start + length - 1; |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 109 | if (res.end <= res.start) |
| 110 | return -EINVAL; |
| 111 | |
| 112 | pd = platform_device_register_resndata(NULL, "simple-framebuffer", 0, |
| 113 | &res, 1, mode, sizeof(*mode)); |
Fabian Frederick | 76ef0db | 2014-10-17 22:01:05 +0200 | [diff] [blame] | 114 | return PTR_ERR_OR_ZERO(pd); |
David Herrmann | e3263ab | 2013-08-02 14:05:22 +0200 | [diff] [blame] | 115 | } |