Re: [RFC PATCH 1/3] PCI: do not compare CPU address with PCI address

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Nov 18, 2013 at 10:36 PM, Guo Chao <yan@xxxxxxxxxxxxxxxxxx> wrote:
> In resource assignment code, limits are exerted to restrict allocated
> resource range. However these limits are PCI address but compared to
> CPU address in the end. Translated them before comparing.
>
> We can't just use pcibios_bus_to_resource because the limits may not
> included in the host bridge window. Introduce a help function to
> do this translation, if address missed, return an approximate one.
>
> Signed-off-by: Guo Chao <yan@xxxxxxxxxxxxxxxxxx>
> ---
>  drivers/pci/bus.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 63 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
> index fc1b740..532c0a4 100644
> --- a/drivers/pci/bus.c
> +++ b/drivers/pci/bus.c
> @@ -99,6 +99,64 @@ void pci_bus_remove_resources(struct pci_bus *bus)
>  }
>
>  /**
> + * pci_bus_to_resource
> + *
> + * Much like pcibios_bus_to_resource() except it takes a single address
> + * and returns an approximate one if target address is not included
> + * in the bridge window. The approximate address is smaller than required
> + * one is 'bound' is 1, larger than required one if 'bound' is 0.
> + */
> +static resource_size_t pci_bus_to_resource(struct pci_bus *bus, int flags,
> +                                   int mask, resource_size_t addr, int bound)
> +{
> +       struct pci_host_bridge *bridge;
> +       struct pci_host_bridge_window *window, *match = NULL;
> +       resource_size_t max = 0, min = -1;
> +       resource_size_t offset = -1, start, end;
> +
> +       while (bus->parent)
> +               bus = bus->parent;
> +
> +       bridge = to_pci_host_bridge(bus->bridge);
> +
> +       list_for_each_entry(window, &bridge->windows, list) {
> +               if ((flags ^ window->res->flags) & mask)
> +                       continue;
> +
> +               start = window->res->start - window->offset;
> +               end = window->res->end - window->offset;
> +
> +               if (addr >= start && addr <= end) {
> +                       offset = window->offset;
> +                       break;
> +               }
> +
> +               if (bound && addr > end && end > max) {
> +                       max = end;
> +                       match = window;
> +               } else if (!bound && addr < start && start < min) {
> +                       min = start;
> +                       match = window;
> +               }
> +       }
> +
> +       if (offset == -1) {
> +               /*
> +                * Not even found the matched type. This may happen,
> +                * for example, if try to translate IO address in a HB
> +                * without IO window. Just return the original address,
> +                * it will fail later anyway.
> +                */
> +               if (match == NULL)
> +                       return addr;
> +
> +               return (bound ? max : min) + match->offset;
> +       }
> +
> +       return addr + offset;
> +}

that is confusing.

Can we use http://lkml.indiana.edu/hypermail/linux/kernel/1206.0/00437/pcibus_addr_converting_bus.patch
instead?

or as attached:
Subject: [PATCH] PCI: pcibus address to resource converting take bus directly

For allocating resource under bus path, we do have dev pass along, and we
could just use bus instead.

Signed-off-by: Yinghai Lu <yinghai@xxxxxxxxxx>

---
 drivers/pci/host-bridge.c |   34 +++++++++++++++++++++-------------
 include/linux/pci.h       |    3 +++
 2 files changed, 24 insertions(+), 13 deletions(-)

Index: linux-2.6/drivers/pci/host-bridge.c
===================================================================
--- linux-2.6.orig/drivers/pci/host-bridge.c
+++ linux-2.6/drivers/pci/host-bridge.c
@@ -9,22 +9,19 @@
 
 #include "pci.h"
 
-static struct pci_bus *find_pci_root_bus(struct pci_dev *dev)
+static struct pci_bus *find_pci_root_bus(struct pci_bus *bus)
 {
-	struct pci_bus *bus;
-
-	bus = dev->bus;
 	while (bus->parent)
 		bus = bus->parent;
 
 	return bus;
 }
 
-static struct pci_host_bridge *find_pci_host_bridge(struct pci_dev *dev)
+static struct pci_host_bridge *find_pci_host_bridge(struct pci_bus *bus)
 {
-	struct pci_bus *bus = find_pci_root_bus(dev);
+	struct pci_bus *root_bus = find_pci_root_bus(bus);
 
-	return to_pci_host_bridge(bus->bridge);
+	return to_pci_host_bridge(root_bus->bridge);
 }
 
 void pci_set_host_bridge_release(struct pci_host_bridge *bridge,
@@ -40,10 +37,11 @@ static bool resource_contains(struct res
 	return res1->start <= res2->start && res1->end >= res2->end;
 }
 
-void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
-			     struct resource *res)
+void __pcibios_resource_to_bus(struct pci_bus *bus,
+				      struct pci_bus_region *region,
+				      struct resource *res)
 {
-	struct pci_host_bridge *bridge = find_pci_host_bridge(dev);
+	struct pci_host_bridge *bridge = find_pci_host_bridge(bus);
 	struct pci_host_bridge_window *window;
 	resource_size_t offset = 0;
 
@@ -60,6 +58,11 @@ void pcibios_resource_to_bus(struct pci_
 	region->start = res->start - offset;
 	region->end = res->end - offset;
 }
+void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
+			     struct resource *res)
+{
+	__pcibios_resource_to_bus(dev->bus, region, res);
+}
 EXPORT_SYMBOL(pcibios_resource_to_bus);
 
 static bool region_contains(struct pci_bus_region *region1,
@@ -68,10 +71,10 @@ static bool region_contains(struct pci_b
 	return region1->start <= region2->start && region1->end >= region2->end;
 }
 
-void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
-			     struct pci_bus_region *region)
+static void __pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
+				      struct pci_bus_region *region)
 {
-	struct pci_host_bridge *bridge = find_pci_host_bridge(dev);
+	struct pci_host_bridge *bridge = find_pci_host_bridge(bus);
 	struct pci_host_bridge_window *window;
 	resource_size_t offset = 0;
 
@@ -93,4 +96,9 @@ void pcibios_bus_to_resource(struct pci_
 	res->start = region->start + offset;
 	res->end = region->end + offset;
 }
+void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
+			     struct pci_bus_region *region)
+{
+	__pcibios_bus_to_resource(dev->bus, res, region);
+}
 EXPORT_SYMBOL(pcibios_bus_to_resource);
Index: linux-2.6/include/linux/pci.h
===================================================================
--- linux-2.6.orig/include/linux/pci.h
+++ linux-2.6/include/linux/pci.h
@@ -792,6 +792,9 @@ void pci_fixup_cardbus(struct pci_bus *)
 
 /* Generic PCI functions used internally */
 
+void __pcibios_resource_to_bus(struct pci_bus *bus,
+			       struct pci_bus_region *region,
+			       struct resource *res);
 void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
 			     struct resource *res);
 void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
Subject: [PATCH] PCI: Add pcibios_bus_addr_to_res()

it takes addr and return converted address only.

Signed-off-by: Yinghai Lu <yinghai@xxxxxxxxxx>

---
 drivers/pci/host-bridge.c |   14 ++++++++++++++
 include/linux/pci.h       |    2 ++
 2 files changed, 16 insertions(+)

Index: linux-2.6/drivers/pci/host-bridge.c
===================================================================
--- linux-2.6.orig/drivers/pci/host-bridge.c
+++ linux-2.6/drivers/pci/host-bridge.c
@@ -102,3 +102,17 @@ void pcibios_bus_to_resource(struct pci_
 	__pcibios_bus_to_resource(dev->bus, res, region);
 }
 EXPORT_SYMBOL(pcibios_bus_to_resource);
+
+resource_size_t pcibios_bus_addr_to_res(struct pci_bus *bus, int flags,
+					resource_size_t addr)
+{
+	struct pci_bus_region region;
+	struct resource r;
+
+	r.flags = flags;
+	region.start = addr;
+	region.end = addr;
+	__pcibios_bus_to_resource(bus, &r, &region);
+
+	return r.end;
+}
Index: linux-2.6/include/linux/pci.h
===================================================================
--- linux-2.6.orig/include/linux/pci.h
+++ linux-2.6/include/linux/pci.h
@@ -799,6 +799,8 @@ void pcibios_resource_to_bus(struct pci_
 			     struct resource *res);
 void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
 			     struct pci_bus_region *region);
+resource_size_t pcibios_bus_addr_to_res(struct pci_bus *bus, int flags,
+					resource_size_t addr);
 void pcibios_scan_specific_bus(int busn);
 struct pci_bus *pci_find_bus(int domain, int busnr);
 void pci_bus_add_devices(const struct pci_bus *bus);

[Index of Archives]     [DMA Engine]     [Linux Coverity]     [Linux USB]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Greybus]

  Powered by Linux