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