setup-bus.c (24a0c654d7d6063301c51361f911369264342b3c) setup-bus.c (1a5767725cecedd80a541799c83c0c97b8b5b624)
1/*
2 * drivers/pci/setup-bus.c
3 *
4 * Extruded from code written by
5 * Dave Rusling (david.rusling@reo.mts.dec.com)
6 * David Mosberger (davidm@cs.arizona.edu)
7 * David Miller (davem@redhat.com)
8 *

--- 1839 unchanged lines hidden (view full) ---

1848 pci_assign_unassigned_root_bus_resources(root_bus);
1849
1850 /* Make sure the root bridge has a companion ACPI device: */
1851 if (ACPI_HANDLE(root_bus->bridge))
1852 acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
1853 }
1854}
1855
1/*
2 * drivers/pci/setup-bus.c
3 *
4 * Extruded from code written by
5 * Dave Rusling (david.rusling@reo.mts.dec.com)
6 * David Mosberger (davidm@cs.arizona.edu)
7 * David Miller (davem@redhat.com)
8 *

--- 1839 unchanged lines hidden (view full) ---

1848 pci_assign_unassigned_root_bus_resources(root_bus);
1849
1850 /* Make sure the root bridge has a companion ACPI device: */
1851 if (ACPI_HANDLE(root_bus->bridge))
1852 acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
1853 }
1854}
1855
1856static void extend_bridge_window(struct pci_dev *bridge, struct resource *res,
1857 struct list_head *add_list, resource_size_t available)
1858{
1859 struct pci_dev_resource *dev_res;
1860
1861 if (res->parent)
1862 return;
1863
1864 if (resource_size(res) >= available)
1865 return;
1866
1867 dev_res = res_to_dev_res(add_list, res);
1868 if (!dev_res)
1869 return;
1870
1871 /* Is there room to extend the window? */
1872 if (available - resource_size(res) <= dev_res->add_size)
1873 return;
1874
1875 dev_res->add_size = available - resource_size(res);
1876 dev_dbg(&bridge->dev, "bridge window %pR extended by %pa\n", res,
1877 &dev_res->add_size);
1878}
1879
1880static void pci_bus_distribute_available_resources(struct pci_bus *bus,
1881 struct list_head *add_list, resource_size_t available_io,
1882 resource_size_t available_mmio, resource_size_t available_mmio_pref)
1883{
1884 resource_size_t remaining_io, remaining_mmio, remaining_mmio_pref;
1885 unsigned int normal_bridges = 0, hotplug_bridges = 0;
1886 struct resource *io_res, *mmio_res, *mmio_pref_res;
1887 struct pci_dev *dev, *bridge = bus->self;
1888
1889 io_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
1890 mmio_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
1891 mmio_pref_res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
1892
1893 /*
1894 * Update additional resource list (add_list) to fill all the
1895 * extra resource space available for this port except the space
1896 * calculated in __pci_bus_size_bridges() which covers all the
1897 * devices currently connected to the port and below.
1898 */
1899 extend_bridge_window(bridge, io_res, add_list, available_io);
1900 extend_bridge_window(bridge, mmio_res, add_list, available_mmio);
1901 extend_bridge_window(bridge, mmio_pref_res, add_list,
1902 available_mmio_pref);
1903
1904 /*
1905 * Calculate the total amount of extra resource space we can
1906 * pass to bridges below this one. This is basically the
1907 * extra space reduced by the minimal required space for the
1908 * non-hotplug bridges.
1909 */
1910 remaining_io = available_io;
1911 remaining_mmio = available_mmio;
1912 remaining_mmio_pref = available_mmio_pref;
1913
1914 /*
1915 * Calculate how many hotplug bridges and normal bridges there
1916 * are on this bus. We will distribute the additional available
1917 * resources between hotplug bridges.
1918 */
1919 for_each_pci_bridge(dev, bus) {
1920 if (dev->is_hotplug_bridge)
1921 hotplug_bridges++;
1922 else
1923 normal_bridges++;
1924 }
1925
1926 for_each_pci_bridge(dev, bus) {
1927 const struct resource *res;
1928
1929 if (dev->is_hotplug_bridge)
1930 continue;
1931
1932 /*
1933 * Reduce the available resource space by what the
1934 * bridge and devices below it occupy.
1935 */
1936 res = &dev->resource[PCI_BRIDGE_RESOURCES + 0];
1937 if (!res->parent && available_io > resource_size(res))
1938 remaining_io -= resource_size(res);
1939
1940 res = &dev->resource[PCI_BRIDGE_RESOURCES + 1];
1941 if (!res->parent && available_mmio > resource_size(res))
1942 remaining_mmio -= resource_size(res);
1943
1944 res = &dev->resource[PCI_BRIDGE_RESOURCES + 2];
1945 if (!res->parent && available_mmio_pref > resource_size(res))
1946 remaining_mmio_pref -= resource_size(res);
1947 }
1948
1949 /*
1950 * Go over devices on this bus and distribute the remaining
1951 * resource space between hotplug bridges.
1952 */
1953 for_each_pci_bridge(dev, bus) {
1954 struct pci_bus *b;
1955
1956 b = dev->subordinate;
1957 if (!b)
1958 continue;
1959
1960 if (!hotplug_bridges && normal_bridges == 1) {
1961 /*
1962 * There is only one bridge on the bus (upstream
1963 * port) so it gets all available resources
1964 * which it can then distribute to the possible
1965 * hotplug bridges below.
1966 */
1967 pci_bus_distribute_available_resources(b, add_list,
1968 available_io, available_mmio,
1969 available_mmio_pref);
1970 } else if (dev->is_hotplug_bridge) {
1971 resource_size_t align, io, mmio, mmio_pref;
1972
1973 /*
1974 * Distribute available extra resources equally
1975 * between hotplug-capable downstream ports
1976 * taking alignment into account.
1977 *
1978 * Here hotplug_bridges is always != 0.
1979 */
1980 align = pci_resource_alignment(bridge, io_res);
1981 io = div64_ul(available_io, hotplug_bridges);
1982 io = min(ALIGN(io, align), remaining_io);
1983 remaining_io -= io;
1984
1985 align = pci_resource_alignment(bridge, mmio_res);
1986 mmio = div64_ul(available_mmio, hotplug_bridges);
1987 mmio = min(ALIGN(mmio, align), remaining_mmio);
1988 remaining_mmio -= mmio;
1989
1990 align = pci_resource_alignment(bridge, mmio_pref_res);
1991 mmio_pref = div64_ul(available_mmio_pref,
1992 hotplug_bridges);
1993 mmio_pref = min(ALIGN(mmio_pref, align),
1994 remaining_mmio_pref);
1995 remaining_mmio_pref -= mmio_pref;
1996
1997 pci_bus_distribute_available_resources(b, add_list, io,
1998 mmio, mmio_pref);
1999 }
2000 }
2001}
2002
2003static void
2004pci_bridge_distribute_available_resources(struct pci_dev *bridge,
2005 struct list_head *add_list)
2006{
2007 resource_size_t available_io, available_mmio, available_mmio_pref;
2008 const struct resource *res;
2009
2010 if (!bridge->is_hotplug_bridge)
2011 return;
2012
2013 /* Take the initial extra resources from the hotplug port */
2014 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 0];
2015 available_io = resource_size(res);
2016 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 1];
2017 available_mmio = resource_size(res);
2018 res = &bridge->resource[PCI_BRIDGE_RESOURCES + 2];
2019 available_mmio_pref = resource_size(res);
2020
2021 pci_bus_distribute_available_resources(bridge->subordinate,
2022 add_list, available_io, available_mmio, available_mmio_pref);
2023}
2024
1856void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1857{
1858 struct pci_bus *parent = bridge->subordinate;
1859 LIST_HEAD(add_list); /* list of resources that
1860 want additional resources */
1861 int tried_times = 0;
1862 LIST_HEAD(fail_head);
1863 struct pci_dev_resource *fail_res;
1864 int retval;
1865 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1866 IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
1867
1868again:
1869 __pci_bus_size_bridges(parent, &add_list);
2025void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
2026{
2027 struct pci_bus *parent = bridge->subordinate;
2028 LIST_HEAD(add_list); /* list of resources that
2029 want additional resources */
2030 int tried_times = 0;
2031 LIST_HEAD(fail_head);
2032 struct pci_dev_resource *fail_res;
2033 int retval;
2034 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
2035 IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
2036
2037again:
2038 __pci_bus_size_bridges(parent, &add_list);
2039
2040 /*
2041 * Distribute remaining resources (if any) equally between
2042 * hotplug bridges below. This makes it possible to extend the
2043 * hierarchy later without running out of resources.
2044 */
2045 pci_bridge_distribute_available_resources(bridge, &add_list);
2046
1870 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1871 BUG_ON(!list_empty(&add_list));
1872 tried_times++;
1873
1874 if (list_empty(&fail_head))
1875 goto enable_all;
1876
1877 if (tried_times >= 2) {

--- 54 unchanged lines hidden ---
2047 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
2048 BUG_ON(!list_empty(&add_list));
2049 tried_times++;
2050
2051 if (list_empty(&fail_head))
2052 goto enable_all;
2053
2054 if (tried_times >= 2) {

--- 54 unchanged lines hidden ---