Skip to contentSkip to conversation

efi/fb: Convert PCI bus address to resource if translated by the bridge

RFC PATCH In review Sinan Kaya 3 participants 6 messages May 16, 2018 – May 17, 2018

Subsystems (4)
lore
Share & tools
lore

Sinan Kaya

author
#0 lore raw

RFC PATCH 1/1 efi/fb: Convert PCI bus address to resource if translated by the bridge

A host bridge is allowed to remap BAR addresses using _TRA attribute in _CRS windows.

pci_bus 0000:00: root bus resource [mem 0x80100100000-0x8011fffffff window] (bus address [0x00100000-0x1fffffff]) pci 0000:02:00.0: reg 0x10: [mem 0x8011e000000-0x8011effffff]

When a VGA device is behind such a host bridge and the resource is translated efifb driver is trying to do ioremap against bus address rather than the resource address and is failing to probe.

efifb driver is having difficulty locating the base address from BAR address when

efifb: probing for efifb
efifb: cannot reserve video memory at 0x1e000000
efifb: framebuffer at 0x1e000000, using 1920k, total 1875k
efifb: mode is 800x600x32, linelength=3200, pages=1
efifb: scrolling: redraw
efifb: Truecolor: size=8:8:8:8, shift=24:16:8:0

Use the host bridge offset information to convert bus address to resource address in the fixup.

---
 drivers/video/fbdev/efifb.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/video/fbdev/efifb.c +23 -0
index 46a4484..feef02b 100644
@@ -428,6 +428,8 @@ static void efifb_fixup_resources(struct pci_dev *dev)
 {
 	u64 base = screen_info.lfb_base;
 	u64 size = screen_info.lfb_size;
+	struct pci_host_bridge *bridge;
+	struct resource_entry *window;
 	int i;
 
 	if (efifb_pci_dev || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
@@ -439,6 +441,27 @@ static void efifb_fixup_resources(struct pci_dev *dev)
 	if (!base)
 		return;
 
+	bridge = pci_find_host_bridge(dev->bus);
+	if (!bridge)
+		return;
+
+	resource_list_for_each_entry(window, &bridge->windows) {
+		phys_addr_t win_start;
+		phys_addr_t win_end;
+		size_t win_size;
+
+		if (resource_type(window->res) != IORESOURCE_MEM)
+			continue;
+
+		win_start = window->res->start - window->offset;
+		win_end = window->res->end - window->offset;
+		win_size = window->res->end - window->res->start + 1;
+		if (win_start <= base && win_end >= base + win_size - 1) {
+			base += window->offset;
+			break;
+		}
+	}
+
 	for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
 		struct resource *res = &dev->resource[i];
 
Signature · 1 line
2.7.4
author
#1 lore raw

drivers/video/fbdev/efifb.c static void efifb_fixup_resources(struct pci_dev *dev) · line 462

Quoted 4 lines · Sinan Kaya
drivers/video/fbdev/efifb.c +4 -0
+		if (win_start <= base && win_end >= base + win_size - 1) {
+			base += window->offset;
+			break;
+		}

I should probably add window->offset!=0 to the if statement in order not to break non-translating case.

Any other comments?

Signature · 3 lines
Sinan Kaya
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
#2 lore raw
In reply to Sinan Kaya

drivers/video/fbdev/efifb.c static void efifb_fixup_resources(struct pci_dev *dev) · line 456

Quoted 1 line · Sinan Kaya
drivers/video/fbdev/efifb.c +1 -0
+		win_start = window->res->start - window->offset;

Can you guarantee that window->res->start is always >= window->offset?

drivers/video/fbdev/efifb.c static void efifb_fixup_resources(struct pci_dev *dev) · line 458

Quoted 1 line
drivers/video/fbdev/efifb.c +1 -0
+		win_size = window->res->end - window->res->start + 1;

Use resource_size() instead.

Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
author
#3 lore raw
Quoted 5 lines · Timur Tabi
Quoted 1 line · Sinan Kaya

+        win_start = window->res->start - window->offset;

Can you guarantee that window->res->start is always >= window->offset?

Resource offset is generally 0 when not translating or a positive number that you subtract from the resource start to reach to the translating window.

Having a bigger offset than resource start should be considered a BIOS bug.

Quoted 4 lines
Quoted 1 line

+        win_size = window->res->end - window->res->start + 1;

Use resource_size() instead.

I'll fix it.

Signature · 3 lines
Sinan Kaya
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
#4 lore raw
In reply to Sinan Kaya
Quoted 56 lines · Sinan Kaya

A host bridge is allowed to remap BAR addresses using _TRA attribute in _CRS windows.

pci_bus 0000:00: root bus resource [mem 0x80100100000-0x8011fffffff window] (bus address [0x00100000-0x1fffffff]) pci 0000:02:00.0: reg 0x10: [mem 0x8011e000000-0x8011effffff]

When a VGA device is behind such a host bridge and the resource is translated efifb driver is trying to do ioremap against bus address rather than the resource address and is failing to probe.

efifb driver is having difficulty locating the base address from BAR address when

efifb: probing for efifb
efifb: cannot reserve video memory at 0x1e000000
efifb: framebuffer at 0x1e000000, using 1920k, total 1875k
efifb: mode is 800x600x32, linelength=3200, pages=1
efifb: scrolling: redraw
efifb: Truecolor: size=8:8:8:8, shift=24:16:8:0

Use the host bridge offset information to convert bus address to resource address in the fixup.

---
  drivers/video/fbdev/efifb.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
drivers/video/fbdev/efifb.c +13 -0
index 46a4484..feef02b 100644
@@ -428,6 +428,8 @@ static void efifb_fixup_resources(struct pci_dev *dev)
  {
  	u64 base = screen_info.lfb_base;
  	u64 size = screen_info.lfb_size;
+	struct pci_host_bridge *bridge;
+	struct resource_entry *window;
  	int i;
 
  	if (efifb_pci_dev || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
@@ -439,6 +441,27 @@ static void efifb_fixup_resources(struct pci_dev *dev)
  	if (!base)
  		return;
 
+	bridge = pci_find_host_bridge(dev->bus);
+	if (!bridge)
+		return;
+
+	resource_list_for_each_entry(window, &bridge->windows) {
+		phys_addr_t win_start;
+		phys_addr_t win_end;
+		size_t win_size;
+
+		if (resource_type(window->res) != IORESOURCE_MEM)
+			continue;

drivers/video/fbdev/efifb.c static void efifb_fixup_resources(struct pci_dev *dev) · line 464

Quoted 10 lines
drivers/video/fbdev/efifb.c +10 -0
+
+		win_start = window->res->start - window->offset;
+		win_end = window->res->end - window->offset;
+		win_size = window->res->end - window->res->start + 1;
+		if (win_start <= base && win_end >= base + win_size - 1) {
+			base += window->offset;
+			break;
+		}
+	}
+

Is this not pretty much just pcibios_bus_to_resource()?

Robin.

Quoted 4 lines
drivers/video/fbdev/efifb.c +0 -0
  	for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
  		struct resource *res = &dev->resource[i];
 
author
#5 lore raw
Quoted 5 lines · Robin Murphy
Quoted 2 lines

+    }
+

Is this not pretty much just pcibios_bus_to_resource()?

Agreed, let me convert the code to use pcibios_bus_to_resource() API. I wasn't aware of its existence.

Quoted 1 line

Robin.

Signature · 3 lines
Sinan Kaya
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.