blob: 2752c02e3f0e66e44134dd22a1a4d977f1b22614 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yinghai Lu67f241f2009-11-11 22:27:40 -08002#include <linux/init.h>
3#include <linux/pci.h>
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -08004#include <linux/range.h>
Yinghai Lu67f241f2009-11-11 22:27:40 -08005
6#include "bus_numa.h"
7
Yinghai Lud28e5ac2012-04-02 18:31:54 -07008LIST_HEAD(pci_root_infos);
9
10static struct pci_root_info *x86_find_pci_root_info(int bus)
11{
12 struct pci_root_info *info;
13
Yinghai Lud28e5ac2012-04-02 18:31:54 -070014 list_for_each_entry(info, &pci_root_infos, list)
Yinghai Lua10bb122012-05-17 18:51:12 -070015 if (info->busn.start == bus)
Yinghai Lud28e5ac2012-04-02 18:31:54 -070016 return info;
17
18 return NULL;
19}
Yinghai Lu67f241f2009-11-11 22:27:40 -080020
Bjorn Helgaasafcf21c22014-01-24 11:54:36 -070021int x86_pci_root_bus_node(int bus)
22{
23 struct pci_root_info *info = x86_find_pci_root_info(bus);
24
25 if (!info)
26 return NUMA_NO_NODE;
27
28 return info->node;
29}
30
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060031void x86_pci_root_bus_resources(int bus, struct list_head *resources)
Yinghai Lu67f241f2009-11-11 22:27:40 -080032{
Yinghai Lud28e5ac2012-04-02 18:31:54 -070033 struct pci_root_info *info = x86_find_pci_root_info(bus);
34 struct pci_root_res *root_res;
Jiang Liu14d76b62015-02-05 13:44:44 +080035 struct resource_entry *window;
Yinghai Lua10bb122012-05-17 18:51:12 -070036 bool found = false;
Yinghai Lu67f241f2009-11-11 22:27:40 -080037
Yinghai Lud28e5ac2012-04-02 18:31:54 -070038 if (!info)
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060039 goto default_resources;
Yinghai Lu67f241f2009-11-11 22:27:40 -080040
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060041 printk(KERN_DEBUG "PCI: root bus %02x: hardware-probed resources\n",
42 bus);
Yinghai Lu67f241f2009-11-11 22:27:40 -080043
Yinghai Lua10bb122012-05-17 18:51:12 -070044 /* already added by acpi ? */
Jiang Liu14d76b62015-02-05 13:44:44 +080045 resource_list_for_each_entry(window, resources)
Yinghai Lua10bb122012-05-17 18:51:12 -070046 if (window->res->flags & IORESOURCE_BUS) {
47 found = true;
48 break;
49 }
50
51 if (!found)
52 pci_add_resource(resources, &info->busn);
53
Liu Jiang727ae8b2015-11-27 11:12:33 +080054 list_for_each_entry(root_res, &info->resources, list)
55 pci_add_resource(resources, &root_res->res);
Yinghai Lu67f241f2009-11-11 22:27:40 -080056
Bjorn Helgaas2cd69752011-10-28 16:28:14 -060057 return;
58
59default_resources:
60 /*
61 * We don't have any host bridge aperture information from the
62 * "native host bridge drivers," e.g., amd_bus or broadcom_bus,
63 * so fall back to the defaults historically used by pci_create_bus().
64 */
65 printk(KERN_DEBUG "PCI: root bus %02x: using default resources\n", bus);
66 pci_add_resource(resources, &ioport_resource);
67 pci_add_resource(resources, &iomem_resource);
Yinghai Lu67f241f2009-11-11 22:27:40 -080068}
69
Yinghai Lud28e5ac2012-04-02 18:31:54 -070070struct pci_root_info __init *alloc_pci_root_info(int bus_min, int bus_max,
71 int node, int link)
72{
73 struct pci_root_info *info;
74
75 info = kzalloc(sizeof(*info), GFP_KERNEL);
76
77 if (!info)
78 return info;
79
Yinghai Lua10bb122012-05-17 18:51:12 -070080 sprintf(info->name, "PCI Bus #%02x", bus_min);
81
Yinghai Lud28e5ac2012-04-02 18:31:54 -070082 INIT_LIST_HEAD(&info->resources);
Yinghai Lua10bb122012-05-17 18:51:12 -070083 info->busn.name = info->name;
84 info->busn.start = bus_min;
85 info->busn.end = bus_max;
86 info->busn.flags = IORESOURCE_BUS;
Yinghai Lud28e5ac2012-04-02 18:31:54 -070087 info->node = node;
88 info->link = link;
89
90 list_add_tail(&info->list, &pci_root_infos);
91
92 return info;
93}
94
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -080095void update_res(struct pci_root_info *info, resource_size_t start,
96 resource_size_t end, unsigned long flags, int merge)
Yinghai Lu67f241f2009-11-11 22:27:40 -080097{
Yinghai Lu67f241f2009-11-11 22:27:40 -080098 struct resource *res;
Yinghai Lud28e5ac2012-04-02 18:31:54 -070099 struct pci_root_res *root_res;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800100
101 if (start > end)
102 return;
103
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800104 if (start == MAX_RESOURCE)
105 return;
106
Yinghai Lu67f241f2009-11-11 22:27:40 -0800107 if (!merge)
108 goto addit;
109
110 /* try to merge it with old one */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700111 list_for_each_entry(root_res, &info->resources, list) {
Yinghai Lub74fd232010-02-10 01:20:08 -0800112 resource_size_t final_start, final_end;
113 resource_size_t common_start, common_end;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800114
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700115 res = &root_res->res;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800116 if (res->flags != flags)
117 continue;
118
Yinghai Lub74fd232010-02-10 01:20:08 -0800119 common_start = max(res->start, start);
120 common_end = min(res->end, end);
Yinghai Lu67f241f2009-11-11 22:27:40 -0800121 if (common_start > common_end + 1)
122 continue;
123
Yinghai Lub74fd232010-02-10 01:20:08 -0800124 final_start = min(res->start, start);
125 final_end = max(res->end, end);
Yinghai Lu67f241f2009-11-11 22:27:40 -0800126
127 res->start = final_start;
128 res->end = final_end;
129 return;
130 }
131
132addit:
133
134 /* need to add that */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700135 root_res = kzalloc(sizeof(*root_res), GFP_KERNEL);
136 if (!root_res)
Yinghai Lu67f241f2009-11-11 22:27:40 -0800137 return;
138
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700139 res = &root_res->res;
Yinghai Lu67f241f2009-11-11 22:27:40 -0800140 res->name = info->name;
141 res->flags = flags;
142 res->start = start;
143 res->end = end;
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700144
145 list_add_tail(&root_res->list, &info->resources);
Yinghai Lu67f241f2009-11-11 22:27:40 -0800146}