xref: /linux/drivers/video/screen_info_pci.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/pci.h>
4 #include <linux/printk.h>
5 #include <linux/screen_info.h>
6 #include <linux/string.h>
7 #include <linux/sysfb.h>
8 
9 static struct pci_dev *screen_info_lfb_pdev;
10 static size_t screen_info_lfb_bar;
11 static resource_size_t screen_info_lfb_res_start; // original start of resource
12 static resource_size_t screen_info_lfb_offset; // framebuffer offset within resource
13 
14 static bool __screen_info_relocation_is_valid(const struct screen_info *si, struct resource *pr)
15 {
16 	u64 size = __screen_info_lfb_size(si, screen_info_video_type(si));
17 
18 	if (screen_info_lfb_offset > resource_size(pr))
19 		return false;
20 	if (size > resource_size(pr))
21 		return false;
22 	if (resource_size(pr) - size < screen_info_lfb_offset)
23 		return false;
24 
25 	return true;
26 }
27 
28 void screen_info_apply_fixups(void)
29 {
30 	struct screen_info *si = &sysfb_primary_display.screen;
31 
32 	if (screen_info_lfb_pdev) {
33 		struct resource *pr = &screen_info_lfb_pdev->resource[screen_info_lfb_bar];
34 
35 		if (pr->start != screen_info_lfb_res_start) {
36 			if (__screen_info_relocation_is_valid(si, pr)) {
37 				/*
38 				 * Only update base if we have an actual
39 				 * relocation to a valid I/O range.
40 				 */
41 				__screen_info_set_lfb_base(si, pr->start + screen_info_lfb_offset);
42 				pr_info("Relocating firmware framebuffer to offset %pa[d] within %pr\n",
43 					&screen_info_lfb_offset, pr);
44 			} else {
45 				pr_warn("Invalid relocating, disabling firmware framebuffer\n");
46 			}
47 		}
48 	}
49 }
50 
51 static int __screen_info_lfb_pci_bus_region(const struct screen_info *si, unsigned int type,
52 					    struct pci_bus_region *r)
53 {
54 	u64 base, size;
55 
56 	base = __screen_info_lfb_base(si);
57 	if (!base)
58 		return -EINVAL;
59 
60 	size = __screen_info_lfb_size(si, type);
61 	if (!size)
62 		return -EINVAL;
63 
64 	r->start = base;
65 	r->end = base + size - 1;
66 
67 	return 0;
68 }
69 
70 static void screen_info_fixup_lfb(struct pci_dev *pdev)
71 {
72 	unsigned int type;
73 	struct pci_bus_region bus_region;
74 	int ret;
75 	struct resource r = {
76 		.flags = IORESOURCE_MEM,
77 	};
78 	const struct resource *pr;
79 	const struct screen_info *si = &sysfb_primary_display.screen;
80 
81 	if (screen_info_lfb_pdev)
82 		return; // already found
83 
84 	type = screen_info_video_type(si);
85 	if (!__screen_info_has_lfb(type))
86 		return; // only applies to EFI; maybe VESA
87 
88 	ret = __screen_info_lfb_pci_bus_region(si, type, &bus_region);
89 	if (ret < 0)
90 		return;
91 
92 	/*
93 	 * Translate the PCI bus address to resource. Account
94 	 * for an offset if the framebuffer is behind a PCI host
95 	 * bridge.
96 	 */
97 	pcibios_bus_to_resource(pdev->bus, &r, &bus_region);
98 
99 	pr = pci_find_resource(pdev, &r);
100 	if (!pr)
101 		return;
102 
103 	/*
104 	 * We've found a PCI device with the framebuffer
105 	 * resource. Store away the parameters to track
106 	 * relocation of the framebuffer aperture.
107 	 */
108 	screen_info_lfb_pdev = pdev;
109 	screen_info_lfb_bar = pr - pdev->resource;
110 	screen_info_lfb_offset = r.start - pr->start;
111 	screen_info_lfb_res_start = bus_region.start;
112 }
113 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY, 16,
114 			       screen_info_fixup_lfb);
115 
116 static struct pci_dev *__screen_info_pci_dev(struct resource *res)
117 {
118 	struct pci_dev *pdev = NULL;
119 	const struct resource *r = NULL;
120 
121 	if (!(res->flags & IORESOURCE_MEM))
122 		return NULL;
123 
124 	while (!r && (pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
125 		r = pci_find_resource(pdev, res);
126 	}
127 
128 	return pdev;
129 }
130 
131 /**
132  * screen_info_pci_dev() - Return PCI parent device that contains screen_info's framebuffer
133  * @si: the screen_info
134  *
135  * Returns:
136  * The screen_info's parent device or NULL on success, or a pointer-encoded
137  * errno value otherwise. The value NULL is not an error. It signals that no
138  * PCI device has been found.
139  */
140 struct pci_dev *screen_info_pci_dev(const struct screen_info *si)
141 {
142 	struct resource res[SCREEN_INFO_MAX_RESOURCES];
143 	ssize_t i, numres;
144 
145 	numres = screen_info_resources(si, res, ARRAY_SIZE(res));
146 	if (numres < 0)
147 		return ERR_PTR(numres);
148 
149 	for (i = 0; i < numres; ++i) {
150 		struct pci_dev *pdev = __screen_info_pci_dev(&res[i]);
151 
152 		if (pdev)
153 			return pdev;
154 	}
155 
156 	return NULL;
157 }
158 EXPORT_SYMBOL(screen_info_pci_dev);
159