blob: ea7c2b6dfc56191876e56b7602934b1c11e963e9 [file] [log] [blame]
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05301#include <linux/kernel.h>
Paul Gortmaker97759132011-05-27 17:06:52 -04002#include <linux/export.h>
Grant Likelya6422852011-07-23 23:52:48 -06003#include <linux/of.h>
Liviu Dudaucbe40972014-09-29 15:29:28 +01004#include <linux/of_address.h>
Murali Karicheric49b8fc2015-03-03 12:52:12 -05005#include <linux/of_device.h>
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05306#include <linux/of_pci.h>
Liviu Dudaucbe40972014-09-29 15:29:28 +01007#include <linux/slab.h>
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05308
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10009static inline int __of_pci_pci_compare(struct device_node *node,
Thierry Reding45ab9702013-05-16 17:55:18 +020010 unsigned int data)
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +053011{
Thierry Reding45ab9702013-05-16 17:55:18 +020012 int devfn;
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +053013
Thierry Reding45ab9702013-05-16 17:55:18 +020014 devfn = of_pci_get_devfn(node);
15 if (devfn < 0)
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +100016 return 0;
Thierry Reding45ab9702013-05-16 17:55:18 +020017
18 return devfn == data;
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +053019}
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +100020
21struct device_node *of_pci_find_child_device(struct device_node *parent,
22 unsigned int devfn)
23{
24 struct device_node *node, *node2;
25
26 for_each_child_of_node(parent, node) {
27 if (__of_pci_pci_compare(node, devfn))
28 return node;
29 /*
30 * Some OFs create a parent node "multifunc-device" as
31 * a fake root for all functions of a multi-function
32 * device we go down them as well.
33 */
34 if (!strcmp(node->name, "multifunc-device")) {
35 for_each_child_of_node(node, node2) {
36 if (__of_pci_pci_compare(node2, devfn)) {
37 of_node_put(node);
38 return node2;
39 }
40 }
41 }
42 }
43 return NULL;
44}
45EXPORT_SYMBOL_GPL(of_pci_find_child_device);
Thierry Reding45ab9702013-05-16 17:55:18 +020046
47/**
48 * of_pci_get_devfn() - Get device and function numbers for a device node
49 * @np: device node
50 *
51 * Parses a standard 5-cell PCI resource and returns an 8-bit value that can
52 * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device
53 * and function numbers respectively. On error a negative error code is
54 * returned.
55 */
56int of_pci_get_devfn(struct device_node *np)
57{
58 unsigned int size;
59 const __be32 *reg;
60
61 reg = of_get_property(np, "reg", &size);
62
63 if (!reg || size < 5 * sizeof(__be32))
64 return -EINVAL;
65
66 return (be32_to_cpup(reg) >> 8) & 0xff;
67}
68EXPORT_SYMBOL_GPL(of_pci_get_devfn);
Thierry Reding4e23d3f2013-05-16 17:55:19 +020069
70/**
71 * of_pci_parse_bus_range() - parse the bus-range property of a PCI device
72 * @node: device node
73 * @res: address to a struct resource to return the bus-range
74 *
75 * Returns 0 on success or a negative error-code on failure.
76 */
77int of_pci_parse_bus_range(struct device_node *node, struct resource *res)
78{
79 const __be32 *values;
80 int len;
81
82 values = of_get_property(node, "bus-range", &len);
83 if (!values || len < sizeof(*values) * 2)
84 return -EINVAL;
85
86 res->name = node->name;
87 res->start = be32_to_cpup(values++);
88 res->end = be32_to_cpup(values);
89 res->flags = IORESOURCE_BUS;
90
91 return 0;
92}
93EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
Thomas Petazzoni0d5a6db2013-08-09 22:27:09 +020094
Liviu Dudau41e5c0f2014-09-29 15:29:27 +010095/**
96 * This function will try to obtain the host bridge domain number by
97 * finding a property called "linux,pci-domain" of the given device node.
98 *
99 * @node: device tree node with the domain information
100 *
101 * Returns the associated domain number from DT in the range [0-0xffff], or
102 * a negative value if the required property is not found.
103 */
104int of_get_pci_domain_nr(struct device_node *node)
105{
106 const __be32 *value;
107 int len;
108 u16 domain;
109
110 value = of_get_property(node, "linux,pci-domain", &len);
111 if (!value || len < sizeof(*value))
112 return -EINVAL;
113
114 domain = (u16)be32_to_cpup(value);
115
116 return domain;
117}
118EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
119
Murali Karicheric49b8fc2015-03-03 12:52:12 -0500120/**
121 * of_pci_dma_configure - Setup DMA configuration
122 * @dev: ptr to pci_dev struct of the PCI device
123 *
124 * Function to update PCI devices's DMA configuration using the same
125 * info from the OF node of host bridge's parent (if any).
126 */
127void of_pci_dma_configure(struct pci_dev *pci_dev)
128{
129 struct device *dev = &pci_dev->dev;
130 struct device *bridge = pci_get_host_bridge_device(pci_dev);
131
132 if (!bridge->parent)
133 return;
134
135 of_dma_configure(dev, bridge->parent->of_node);
136 pci_put_host_bridge_device(bridge);
137}
138EXPORT_SYMBOL_GPL(of_pci_dma_configure);
139
Liviu Dudaucbe40972014-09-29 15:29:28 +0100140#if defined(CONFIG_OF_ADDRESS)
141/**
142 * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT
143 * @dev: device node of the host bridge having the range property
144 * @busno: bus number associated with the bridge root bus
145 * @bus_max: maximum number of buses for this bridge
146 * @resources: list where the range of resources will be added after DT parsing
147 * @io_base: pointer to a variable that will contain on return the physical
148 * address for the start of the I/O range. Can be NULL if the caller doesn't
149 * expect IO ranges to be present in the device tree.
150 *
151 * It is the caller's job to free the @resources list.
152 *
153 * This function will parse the "ranges" property of a PCI host bridge device
154 * node and setup the resource mapping based on its content. It is expected
155 * that the property conforms with the Power ePAPR document.
156 *
157 * It returns zero if the range parsing has been successful or a standard error
158 * value if it failed.
159 */
160int of_pci_get_host_bridge_resources(struct device_node *dev,
161 unsigned char busno, unsigned char bus_max,
162 struct list_head *resources, resource_size_t *io_base)
163{
Rafael J. Wysocki5c493df2015-02-09 15:40:30 +0100164 struct resource_entry *window;
Liviu Dudaucbe40972014-09-29 15:29:28 +0100165 struct resource *res;
166 struct resource *bus_range;
167 struct of_pci_range range;
168 struct of_pci_range_parser parser;
169 char range_type[4];
170 int err;
171
172 if (io_base)
173 *io_base = (resource_size_t)OF_BAD_ADDR;
174
175 bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL);
176 if (!bus_range)
177 return -ENOMEM;
178
179 pr_info("PCI host bridge %s ranges:\n", dev->full_name);
180
181 err = of_pci_parse_bus_range(dev, bus_range);
182 if (err) {
183 bus_range->start = busno;
184 bus_range->end = bus_max;
185 bus_range->flags = IORESOURCE_BUS;
186 pr_info(" No bus range found for %s, using %pR\n",
187 dev->full_name, bus_range);
188 } else {
189 if (bus_range->end > bus_range->start + bus_max)
190 bus_range->end = bus_range->start + bus_max;
191 }
192 pci_add_resource(resources, bus_range);
193
194 /* Check for ranges property */
195 err = of_pci_range_parser_init(&parser, dev);
196 if (err)
197 goto parse_failed;
198
199 pr_debug("Parsing ranges property...\n");
200 for_each_of_pci_range(&parser, &range) {
201 /* Read next ranges element */
202 if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
203 snprintf(range_type, 4, " IO");
204 else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
205 snprintf(range_type, 4, "MEM");
206 else
207 snprintf(range_type, 4, "err");
208 pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type,
209 range.cpu_addr, range.cpu_addr + range.size - 1,
210 range.pci_addr);
211
212 /*
213 * If we failed translation or got a zero-sized region
214 * then skip this range
215 */
216 if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
217 continue;
218
219 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
220 if (!res) {
221 err = -ENOMEM;
222 goto parse_failed;
223 }
224
225 err = of_pci_range_to_resource(&range, dev, res);
Pavel Fedinf134f252015-10-08 10:24:26 +0300226 if (err) {
227 kfree(res);
228 continue;
229 }
Liviu Dudaucbe40972014-09-29 15:29:28 +0100230
231 if (resource_type(res) == IORESOURCE_IO) {
232 if (!io_base) {
233 pr_err("I/O range found for %s. Please provide an io_base pointer to save CPU base address\n",
234 dev->full_name);
235 err = -EINVAL;
236 goto conversion_failed;
237 }
238 if (*io_base != (resource_size_t)OF_BAD_ADDR)
239 pr_warn("More than one I/O resource converted for %s. CPU base address for old range lost!\n",
240 dev->full_name);
241 *io_base = range.cpu_addr;
242 }
243
244 pci_add_resource_offset(resources, res, res->start - range.pci_addr);
245 }
246
247 return 0;
248
249conversion_failed:
250 kfree(res);
251parse_failed:
Rafael J. Wysocki5c493df2015-02-09 15:40:30 +0100252 resource_list_for_each_entry(window, resources)
Lorenzo Pieralisid2be00c2015-01-27 18:01:45 +0000253 kfree(window->res);
Liviu Dudaucbe40972014-09-29 15:29:28 +0100254 pci_free_resource_list(resources);
255 return err;
256}
257EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
258#endif /* CONFIG_OF_ADDRESS */
259
Thomas Petazzoni0d5a6db2013-08-09 22:27:09 +0200260#ifdef CONFIG_PCI_MSI
261
262static LIST_HEAD(of_pci_msi_chip_list);
263static DEFINE_MUTEX(of_pci_msi_chip_mutex);
264
Yijing Wangc2791b82014-11-11 17:45:45 -0700265int of_pci_msi_chip_add(struct msi_controller *chip)
Thomas Petazzoni0d5a6db2013-08-09 22:27:09 +0200266{
267 if (!of_property_read_bool(chip->of_node, "msi-controller"))
268 return -EINVAL;
269
270 mutex_lock(&of_pci_msi_chip_mutex);
271 list_add(&chip->list, &of_pci_msi_chip_list);
272 mutex_unlock(&of_pci_msi_chip_mutex);
273
274 return 0;
275}
276EXPORT_SYMBOL_GPL(of_pci_msi_chip_add);
277
Yijing Wangc2791b82014-11-11 17:45:45 -0700278void of_pci_msi_chip_remove(struct msi_controller *chip)
Thomas Petazzoni0d5a6db2013-08-09 22:27:09 +0200279{
280 mutex_lock(&of_pci_msi_chip_mutex);
281 list_del(&chip->list);
282 mutex_unlock(&of_pci_msi_chip_mutex);
283}
284EXPORT_SYMBOL_GPL(of_pci_msi_chip_remove);
285
Yijing Wangc2791b82014-11-11 17:45:45 -0700286struct msi_controller *of_pci_find_msi_chip_by_node(struct device_node *of_node)
Thomas Petazzoni0d5a6db2013-08-09 22:27:09 +0200287{
Yijing Wangc2791b82014-11-11 17:45:45 -0700288 struct msi_controller *c;
Thomas Petazzoni0d5a6db2013-08-09 22:27:09 +0200289
290 mutex_lock(&of_pci_msi_chip_mutex);
291 list_for_each_entry(c, &of_pci_msi_chip_list, list) {
292 if (c->of_node == of_node) {
293 mutex_unlock(&of_pci_msi_chip_mutex);
294 return c;
295 }
296 }
297 mutex_unlock(&of_pci_msi_chip_mutex);
298
299 return NULL;
300}
301EXPORT_SYMBOL_GPL(of_pci_find_msi_chip_by_node);
302
303#endif /* CONFIG_PCI_MSI */