]>
Commit | Line | Data |
---|---|---|
606ad42a RH |
1 | #define pr_fmt(fmt) "OF: PCI: " fmt |
2 | ||
04bea68b | 3 | #include <linux/kernel.h> |
9775913f | 4 | #include <linux/export.h> |
a6422850 | 5 | #include <linux/of.h> |
cbe4097f | 6 | #include <linux/of_address.h> |
c49b8fc2 | 7 | #include <linux/of_device.h> |
04bea68b | 8 | #include <linux/of_pci.h> |
cbe4097f | 9 | #include <linux/slab.h> |
04bea68b | 10 | |
98d9f30c | 11 | static inline int __of_pci_pci_compare(struct device_node *node, |
45ab9702 | 12 | unsigned int data) |
04bea68b | 13 | { |
45ab9702 | 14 | int devfn; |
04bea68b | 15 | |
45ab9702 TR |
16 | devfn = of_pci_get_devfn(node); |
17 | if (devfn < 0) | |
98d9f30c | 18 | return 0; |
45ab9702 TR |
19 | |
20 | return devfn == data; | |
98d9f30c | 21 | } |
04bea68b | 22 | |
98d9f30c BH |
23 | struct device_node *of_pci_find_child_device(struct device_node *parent, |
24 | unsigned int devfn) | |
25 | { | |
26 | struct device_node *node, *node2; | |
27 | ||
28 | for_each_child_of_node(parent, node) { | |
29 | if (__of_pci_pci_compare(node, devfn)) | |
30 | return node; | |
31 | /* | |
32 | * Some OFs create a parent node "multifunc-device" as | |
33 | * a fake root for all functions of a multi-function | |
34 | * device we go down them as well. | |
04bea68b | 35 | */ |
98d9f30c BH |
36 | if (!strcmp(node->name, "multifunc-device")) { |
37 | for_each_child_of_node(node, node2) { | |
38 | if (__of_pci_pci_compare(node2, devfn)) { | |
39 | of_node_put(node); | |
40 | return node2; | |
41 | } | |
42 | } | |
43 | } | |
04bea68b | 44 | } |
98d9f30c | 45 | return NULL; |
04bea68b | 46 | } |
98d9f30c | 47 | EXPORT_SYMBOL_GPL(of_pci_find_child_device); |
45ab9702 TR |
48 | |
49 | /** | |
50 | * of_pci_get_devfn() - Get device and function numbers for a device node | |
51 | * @np: device node | |
52 | * | |
53 | * Parses a standard 5-cell PCI resource and returns an 8-bit value that can | |
54 | * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device | |
55 | * and function numbers respectively. On error a negative error code is | |
56 | * returned. | |
57 | */ | |
58 | int of_pci_get_devfn(struct device_node *np) | |
59 | { | |
60 | unsigned int size; | |
61 | const __be32 *reg; | |
62 | ||
63 | reg = of_get_property(np, "reg", &size); | |
64 | ||
65 | if (!reg || size < 5 * sizeof(__be32)) | |
66 | return -EINVAL; | |
67 | ||
68 | return (be32_to_cpup(reg) >> 8) & 0xff; | |
69 | } | |
70 | EXPORT_SYMBOL_GPL(of_pci_get_devfn); | |
4e23d3f5 TR |
71 | |
72 | /** | |
73 | * of_pci_parse_bus_range() - parse the bus-range property of a PCI device | |
74 | * @node: device node | |
75 | * @res: address to a struct resource to return the bus-range | |
76 | * | |
77 | * Returns 0 on success or a negative error-code on failure. | |
78 | */ | |
79 | int of_pci_parse_bus_range(struct device_node *node, struct resource *res) | |
80 | { | |
81 | const __be32 *values; | |
82 | int len; | |
83 | ||
84 | values = of_get_property(node, "bus-range", &len); | |
85 | if (!values || len < sizeof(*values) * 2) | |
86 | return -EINVAL; | |
87 | ||
88 | res->name = node->name; | |
89 | res->start = be32_to_cpup(values++); | |
90 | res->end = be32_to_cpup(values); | |
91 | res->flags = IORESOURCE_BUS; | |
92 | ||
93 | return 0; | |
94 | } | |
95 | EXPORT_SYMBOL_GPL(of_pci_parse_bus_range); | |
0d5a6db3 | 96 | |
41e5c0f8 LD |
97 | /** |
98 | * This function will try to obtain the host bridge domain number by | |
99 | * finding a property called "linux,pci-domain" of the given device node. | |
100 | * | |
101 | * @node: device tree node with the domain information | |
102 | * | |
103 | * Returns the associated domain number from DT in the range [0-0xffff], or | |
104 | * a negative value if the required property is not found. | |
105 | */ | |
106 | int of_get_pci_domain_nr(struct device_node *node) | |
107 | { | |
108 | const __be32 *value; | |
109 | int len; | |
110 | u16 domain; | |
111 | ||
112 | value = of_get_property(node, "linux,pci-domain", &len); | |
113 | if (!value || len < sizeof(*value)) | |
114 | return -EINVAL; | |
115 | ||
116 | domain = (u16)be32_to_cpup(value); | |
117 | ||
118 | return domain; | |
119 | } | |
120 | EXPORT_SYMBOL_GPL(of_get_pci_domain_nr); | |
121 | ||
f81c11af MZ |
122 | /** |
123 | * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only | |
124 | * is present and valid | |
125 | */ | |
126 | void of_pci_check_probe_only(void) | |
127 | { | |
128 | u32 val; | |
129 | int ret; | |
130 | ||
131 | ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val); | |
132 | if (ret) { | |
133 | if (ret == -ENODATA || ret == -EOVERFLOW) | |
134 | pr_warn("linux,pci-probe-only without valid value, ignoring\n"); | |
135 | return; | |
136 | } | |
137 | ||
138 | if (val) | |
139 | pci_add_flags(PCI_PROBE_ONLY); | |
140 | else | |
141 | pci_clear_flags(PCI_PROBE_ONLY); | |
142 | ||
606ad42a | 143 | pr_info("PROBE_ONLY %sabled\n", val ? "en" : "dis"); |
f81c11af MZ |
144 | } |
145 | EXPORT_SYMBOL_GPL(of_pci_check_probe_only); | |
146 | ||
cbe4097f LD |
147 | #if defined(CONFIG_OF_ADDRESS) |
148 | /** | |
149 | * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT | |
150 | * @dev: device node of the host bridge having the range property | |
151 | * @busno: bus number associated with the bridge root bus | |
152 | * @bus_max: maximum number of buses for this bridge | |
153 | * @resources: list where the range of resources will be added after DT parsing | |
154 | * @io_base: pointer to a variable that will contain on return the physical | |
155 | * address for the start of the I/O range. Can be NULL if the caller doesn't | |
156 | * expect IO ranges to be present in the device tree. | |
157 | * | |
158 | * It is the caller's job to free the @resources list. | |
159 | * | |
160 | * This function will parse the "ranges" property of a PCI host bridge device | |
161 | * node and setup the resource mapping based on its content. It is expected | |
162 | * that the property conforms with the Power ePAPR document. | |
163 | * | |
164 | * It returns zero if the range parsing has been successful or a standard error | |
165 | * value if it failed. | |
166 | */ | |
167 | int of_pci_get_host_bridge_resources(struct device_node *dev, | |
168 | unsigned char busno, unsigned char bus_max, | |
169 | struct list_head *resources, resource_size_t *io_base) | |
170 | { | |
5c493df2 | 171 | struct resource_entry *window; |
cbe4097f LD |
172 | struct resource *res; |
173 | struct resource *bus_range; | |
174 | struct of_pci_range range; | |
175 | struct of_pci_range_parser parser; | |
176 | char range_type[4]; | |
177 | int err; | |
178 | ||
179 | if (io_base) | |
180 | *io_base = (resource_size_t)OF_BAD_ADDR; | |
181 | ||
182 | bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL); | |
183 | if (!bus_range) | |
184 | return -ENOMEM; | |
185 | ||
606ad42a | 186 | pr_info("host bridge %s ranges:\n", dev->full_name); |
cbe4097f LD |
187 | |
188 | err = of_pci_parse_bus_range(dev, bus_range); | |
189 | if (err) { | |
190 | bus_range->start = busno; | |
191 | bus_range->end = bus_max; | |
192 | bus_range->flags = IORESOURCE_BUS; | |
193 | pr_info(" No bus range found for %s, using %pR\n", | |
194 | dev->full_name, bus_range); | |
195 | } else { | |
196 | if (bus_range->end > bus_range->start + bus_max) | |
197 | bus_range->end = bus_range->start + bus_max; | |
198 | } | |
199 | pci_add_resource(resources, bus_range); | |
200 | ||
201 | /* Check for ranges property */ | |
202 | err = of_pci_range_parser_init(&parser, dev); | |
203 | if (err) | |
204 | goto parse_failed; | |
205 | ||
206 | pr_debug("Parsing ranges property...\n"); | |
207 | for_each_of_pci_range(&parser, &range) { | |
208 | /* Read next ranges element */ | |
209 | if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO) | |
210 | snprintf(range_type, 4, " IO"); | |
211 | else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM) | |
212 | snprintf(range_type, 4, "MEM"); | |
213 | else | |
214 | snprintf(range_type, 4, "err"); | |
215 | pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type, | |
216 | range.cpu_addr, range.cpu_addr + range.size - 1, | |
217 | range.pci_addr); | |
218 | ||
219 | /* | |
220 | * If we failed translation or got a zero-sized region | |
221 | * then skip this range | |
222 | */ | |
223 | if (range.cpu_addr == OF_BAD_ADDR || range.size == 0) | |
224 | continue; | |
225 | ||
226 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); | |
227 | if (!res) { | |
228 | err = -ENOMEM; | |
229 | goto parse_failed; | |
230 | } | |
231 | ||
232 | err = of_pci_range_to_resource(&range, dev, res); | |
f134f251 PF |
233 | if (err) { |
234 | kfree(res); | |
235 | continue; | |
236 | } | |
cbe4097f LD |
237 | |
238 | if (resource_type(res) == IORESOURCE_IO) { | |
239 | if (!io_base) { | |
240 | pr_err("I/O range found for %s. Please provide an io_base pointer to save CPU base address\n", | |
241 | dev->full_name); | |
242 | err = -EINVAL; | |
243 | goto conversion_failed; | |
244 | } | |
245 | if (*io_base != (resource_size_t)OF_BAD_ADDR) | |
246 | pr_warn("More than one I/O resource converted for %s. CPU base address for old range lost!\n", | |
247 | dev->full_name); | |
248 | *io_base = range.cpu_addr; | |
249 | } | |
250 | ||
251 | pci_add_resource_offset(resources, res, res->start - range.pci_addr); | |
252 | } | |
253 | ||
254 | return 0; | |
255 | ||
256 | conversion_failed: | |
257 | kfree(res); | |
258 | parse_failed: | |
5c493df2 | 259 | resource_list_for_each_entry(window, resources) |
d2be00c0 | 260 | kfree(window->res); |
cbe4097f LD |
261 | pci_free_resource_list(resources); |
262 | return err; | |
263 | } | |
264 | EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources); | |
265 | #endif /* CONFIG_OF_ADDRESS */ | |
266 | ||
0d5a6db3 TP |
267 | #ifdef CONFIG_PCI_MSI |
268 | ||
269 | static LIST_HEAD(of_pci_msi_chip_list); | |
270 | static DEFINE_MUTEX(of_pci_msi_chip_mutex); | |
271 | ||
c2791b80 | 272 | int of_pci_msi_chip_add(struct msi_controller *chip) |
0d5a6db3 TP |
273 | { |
274 | if (!of_property_read_bool(chip->of_node, "msi-controller")) | |
275 | return -EINVAL; | |
276 | ||
277 | mutex_lock(&of_pci_msi_chip_mutex); | |
278 | list_add(&chip->list, &of_pci_msi_chip_list); | |
279 | mutex_unlock(&of_pci_msi_chip_mutex); | |
280 | ||
281 | return 0; | |
282 | } | |
283 | EXPORT_SYMBOL_GPL(of_pci_msi_chip_add); | |
284 | ||
c2791b80 | 285 | void of_pci_msi_chip_remove(struct msi_controller *chip) |
0d5a6db3 TP |
286 | { |
287 | mutex_lock(&of_pci_msi_chip_mutex); | |
288 | list_del(&chip->list); | |
289 | mutex_unlock(&of_pci_msi_chip_mutex); | |
290 | } | |
291 | EXPORT_SYMBOL_GPL(of_pci_msi_chip_remove); | |
292 | ||
c2791b80 | 293 | struct msi_controller *of_pci_find_msi_chip_by_node(struct device_node *of_node) |
0d5a6db3 | 294 | { |
c2791b80 | 295 | struct msi_controller *c; |
0d5a6db3 TP |
296 | |
297 | mutex_lock(&of_pci_msi_chip_mutex); | |
298 | list_for_each_entry(c, &of_pci_msi_chip_list, list) { | |
299 | if (c->of_node == of_node) { | |
300 | mutex_unlock(&of_pci_msi_chip_mutex); | |
301 | return c; | |
302 | } | |
303 | } | |
304 | mutex_unlock(&of_pci_msi_chip_mutex); | |
305 | ||
306 | return NULL; | |
307 | } | |
308 | EXPORT_SYMBOL_GPL(of_pci_find_msi_chip_by_node); | |
309 | ||
310 | #endif /* CONFIG_PCI_MSI */ | |
987068fc RM |
311 | |
312 | /** | |
313 | * of_pci_map_rid - Translate a requester ID through a downstream mapping. | |
314 | * @np: root complex device node. | |
315 | * @rid: PCI requester ID to map. | |
316 | * @map_name: property name of the map to use. | |
317 | * @map_mask_name: optional property name of the mask to use. | |
318 | * @target: optional pointer to a target device node. | |
319 | * @id_out: optional pointer to receive the translated ID. | |
320 | * | |
321 | * Given a PCI requester ID, look up the appropriate implementation-defined | |
322 | * platform ID and/or the target device which receives transactions on that | |
323 | * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or | |
324 | * @id_out may be NULL if only the other is required. If @target points to | |
325 | * a non-NULL device node pointer, only entries targeting that node will be | |
326 | * matched; if it points to a NULL value, it will receive the device node of | |
327 | * the first matching target phandle, with a reference held. | |
328 | * | |
329 | * Return: 0 on success or a standard error code on failure. | |
330 | */ | |
331 | int of_pci_map_rid(struct device_node *np, u32 rid, | |
332 | const char *map_name, const char *map_mask_name, | |
333 | struct device_node **target, u32 *id_out) | |
334 | { | |
335 | u32 map_mask, masked_rid; | |
336 | int map_len; | |
337 | const __be32 *map = NULL; | |
338 | ||
339 | if (!np || !map_name || (!target && !id_out)) | |
340 | return -EINVAL; | |
341 | ||
342 | map = of_get_property(np, map_name, &map_len); | |
343 | if (!map) { | |
344 | if (target) | |
345 | return -ENODEV; | |
346 | /* Otherwise, no map implies no translation */ | |
347 | *id_out = rid; | |
348 | return 0; | |
349 | } | |
350 | ||
351 | if (!map_len || map_len % (4 * sizeof(*map))) { | |
352 | pr_err("%s: Error: Bad %s length: %d\n", np->full_name, | |
353 | map_name, map_len); | |
354 | return -EINVAL; | |
355 | } | |
356 | ||
357 | /* The default is to select all bits. */ | |
358 | map_mask = 0xffffffff; | |
359 | ||
360 | /* | |
361 | * Can be overridden by "{iommu,msi}-map-mask" property. | |
362 | * If of_property_read_u32() fails, the default is used. | |
363 | */ | |
364 | if (map_mask_name) | |
365 | of_property_read_u32(np, map_mask_name, &map_mask); | |
366 | ||
367 | masked_rid = map_mask & rid; | |
368 | for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) { | |
369 | struct device_node *phandle_node; | |
370 | u32 rid_base = be32_to_cpup(map + 0); | |
371 | u32 phandle = be32_to_cpup(map + 1); | |
372 | u32 out_base = be32_to_cpup(map + 2); | |
373 | u32 rid_len = be32_to_cpup(map + 3); | |
374 | ||
375 | if (rid_base & ~map_mask) { | |
376 | pr_err("%s: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n", | |
377 | np->full_name, map_name, map_name, | |
378 | map_mask, rid_base); | |
379 | return -EFAULT; | |
380 | } | |
381 | ||
382 | if (masked_rid < rid_base || masked_rid >= rid_base + rid_len) | |
383 | continue; | |
384 | ||
385 | phandle_node = of_find_node_by_phandle(phandle); | |
386 | if (!phandle_node) | |
387 | return -ENODEV; | |
388 | ||
389 | if (target) { | |
390 | if (*target) | |
391 | of_node_put(phandle_node); | |
392 | else | |
393 | *target = phandle_node; | |
394 | ||
395 | if (*target != phandle_node) | |
396 | continue; | |
397 | } | |
398 | ||
399 | if (id_out) | |
400 | *id_out = masked_rid - rid_base + out_base; | |
401 | ||
402 | pr_debug("%s: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n", | |
403 | np->full_name, map_name, map_mask, rid_base, out_base, | |
404 | rid_len, rid, *id_out); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | pr_err("%s: Invalid %s translation - no match for rid 0x%x on %s\n", | |
409 | np->full_name, map_name, rid, | |
410 | target && *target ? (*target)->full_name : "any target"); | |
411 | return -EFAULT; | |
412 | } |