]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/iommu/of_iommu.c
iommu: provide early initialisation hook for IOMMU drivers
[karo-tx-linux.git] / drivers / iommu / of_iommu.c
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>
21 #include <linux/limits.h>
22 #include <linux/of.h>
23 #include <linux/of_iommu.h>
24
25 static const struct of_device_id __iommu_of_table_sentinel
26         __used __section(__iommu_of_table_end);
27
28 /**
29  * of_get_dma_window - Parse *dma-window property and returns 0 if found.
30  *
31  * @dn: device node
32  * @prefix: prefix for property name if any
33  * @index: index to start to parse
34  * @busno: Returns busno if supported. Otherwise pass NULL
35  * @addr: Returns address that DMA starts
36  * @size: Returns the range that DMA can handle
37  *
38  * This supports different formats flexibly. "prefix" can be
39  * configured if any. "busno" and "index" are optionally
40  * specified. Set 0(or NULL) if not used.
41  */
42 int of_get_dma_window(struct device_node *dn, const char *prefix, int index,
43                       unsigned long *busno, dma_addr_t *addr, size_t *size)
44 {
45         const __be32 *dma_window, *end;
46         int bytes, cur_index = 0;
47         char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX];
48
49         if (!dn || !addr || !size)
50                 return -EINVAL;
51
52         if (!prefix)
53                 prefix = "";
54
55         snprintf(propname, sizeof(propname), "%sdma-window", prefix);
56         snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix);
57         snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix);
58
59         dma_window = of_get_property(dn, propname, &bytes);
60         if (!dma_window)
61                 return -ENODEV;
62         end = dma_window + bytes / sizeof(*dma_window);
63
64         while (dma_window < end) {
65                 u32 cells;
66                 const void *prop;
67
68                 /* busno is one cell if supported */
69                 if (busno)
70                         *busno = be32_to_cpup(dma_window++);
71
72                 prop = of_get_property(dn, addrname, NULL);
73                 if (!prop)
74                         prop = of_get_property(dn, "#address-cells", NULL);
75
76                 cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn);
77                 if (!cells)
78                         return -EINVAL;
79                 *addr = of_read_number(dma_window, cells);
80                 dma_window += cells;
81
82                 prop = of_get_property(dn, sizename, NULL);
83                 cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn);
84                 if (!cells)
85                         return -EINVAL;
86                 *size = of_read_number(dma_window, cells);
87                 dma_window += cells;
88
89                 if (cur_index++ == index)
90                         break;
91         }
92         return 0;
93 }
94 EXPORT_SYMBOL_GPL(of_get_dma_window);
95
96 void __init of_iommu_init(void)
97 {
98         struct device_node *np;
99         const struct of_device_id *match, *matches = &__iommu_of_table;
100
101         for_each_matching_node_and_match(np, matches, &match) {
102                 const of_iommu_init_fn init_fn = match->data;
103
104                 if (init_fn(np))
105                         pr_err("Failed to initialise IOMMU %s\n",
106                                 of_node_full_name(np));
107         }
108 }