Commit | Line | Data |
---|---|---|
4e0ee78f HD |
1 | /* |
2 | * OF helpers for IOMMU | |
3 | * | |
4 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms and conditions of the GNU General Public License, | |
8 | * version 2, as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
20 | #include <linux/export.h> | |
7eba1d51 | 21 | #include <linux/iommu.h> |
4e0ee78f HD |
22 | #include <linux/limits.h> |
23 | #include <linux/of.h> | |
cbff5634 | 24 | #include <linux/of_iommu.h> |
4e0ee78f | 25 | |
1cd076bf WD |
26 | static const struct of_device_id __iommu_of_table_sentinel |
27 | __used __section(__iommu_of_table_end); | |
28 | ||
4e0ee78f HD |
29 | /** |
30 | * of_get_dma_window - Parse *dma-window property and returns 0 if found. | |
31 | * | |
32 | * @dn: device node | |
33 | * @prefix: prefix for property name if any | |
34 | * @index: index to start to parse | |
35 | * @busno: Returns busno if supported. Otherwise pass NULL | |
36 | * @addr: Returns address that DMA starts | |
37 | * @size: Returns the range that DMA can handle | |
38 | * | |
39 | * This supports different formats flexibly. "prefix" can be | |
40 | * configured if any. "busno" and "index" are optionally | |
41 | * specified. Set 0(or NULL) if not used. | |
42 | */ | |
43 | int of_get_dma_window(struct device_node *dn, const char *prefix, int index, | |
44 | unsigned long *busno, dma_addr_t *addr, size_t *size) | |
45 | { | |
46 | const __be32 *dma_window, *end; | |
47 | int bytes, cur_index = 0; | |
48 | char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX]; | |
49 | ||
50 | if (!dn || !addr || !size) | |
51 | return -EINVAL; | |
52 | ||
53 | if (!prefix) | |
54 | prefix = ""; | |
55 | ||
56 | snprintf(propname, sizeof(propname), "%sdma-window", prefix); | |
57 | snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix); | |
58 | snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix); | |
59 | ||
60 | dma_window = of_get_property(dn, propname, &bytes); | |
61 | if (!dma_window) | |
62 | return -ENODEV; | |
63 | end = dma_window + bytes / sizeof(*dma_window); | |
64 | ||
65 | while (dma_window < end) { | |
66 | u32 cells; | |
67 | const void *prop; | |
68 | ||
69 | /* busno is one cell if supported */ | |
70 | if (busno) | |
71 | *busno = be32_to_cpup(dma_window++); | |
72 | ||
73 | prop = of_get_property(dn, addrname, NULL); | |
74 | if (!prop) | |
75 | prop = of_get_property(dn, "#address-cells", NULL); | |
76 | ||
77 | cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn); | |
78 | if (!cells) | |
79 | return -EINVAL; | |
80 | *addr = of_read_number(dma_window, cells); | |
81 | dma_window += cells; | |
82 | ||
83 | prop = of_get_property(dn, sizename, NULL); | |
84 | cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn); | |
85 | if (!cells) | |
86 | return -EINVAL; | |
87 | *size = of_read_number(dma_window, cells); | |
88 | dma_window += cells; | |
89 | ||
90 | if (cur_index++ == index) | |
91 | break; | |
92 | } | |
93 | return 0; | |
94 | } | |
95 | EXPORT_SYMBOL_GPL(of_get_dma_window); | |
1cd076bf | 96 | |
7eba1d51 WD |
97 | struct iommu_ops *of_iommu_configure(struct device *dev) |
98 | { | |
99 | struct of_phandle_args iommu_spec; | |
100 | struct device_node *np; | |
101 | struct iommu_ops *ops = NULL; | |
102 | int idx = 0; | |
103 | ||
104 | /* | |
105 | * We don't currently walk up the tree looking for a parent IOMMU. | |
106 | * See the `Notes:' section of | |
107 | * Documentation/devicetree/bindings/iommu/iommu.txt | |
108 | */ | |
109 | while (!of_parse_phandle_with_args(dev->of_node, "iommus", | |
110 | "#iommu-cells", idx, | |
111 | &iommu_spec)) { | |
112 | np = iommu_spec.np; | |
113 | ops = of_iommu_get_ops(np); | |
114 | ||
115 | if (!ops || !ops->of_xlate || ops->of_xlate(dev, &iommu_spec)) | |
116 | goto err_put_node; | |
117 | ||
118 | of_node_put(np); | |
119 | idx++; | |
120 | } | |
121 | ||
122 | return ops; | |
123 | ||
124 | err_put_node: | |
125 | of_node_put(np); | |
126 | return NULL; | |
127 | } | |
128 | ||
1cd076bf WD |
129 | void __init of_iommu_init(void) |
130 | { | |
131 | struct device_node *np; | |
132 | const struct of_device_id *match, *matches = &__iommu_of_table; | |
133 | ||
134 | for_each_matching_node_and_match(np, matches, &match) { | |
135 | const of_iommu_init_fn init_fn = match->data; | |
136 | ||
137 | if (init_fn(np)) | |
138 | pr_err("Failed to initialise IOMMU %s\n", | |
139 | of_node_full_name(np)); | |
140 | } | |
141 | } |