blob: 4c69c8cf1949bcefe43bf168a04d15e7331b3c6d [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2012 The Linux Foundation. All rights reserved.
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -08002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/io.h>
19#include <linux/clk.h>
20#include <linux/iommu.h>
21#include <linux/interrupt.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/atomic.h>
25#include <linux/of.h>
26#include <linux/of_address.h>
27#include <linux/of_device.h>
28
29#include <mach/iommu_hw-v2.h>
30#include <mach/iommu.h>
31
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080032static int msm_iommu_parse_dt(struct platform_device *pdev,
33 struct msm_iommu_drvdata *drvdata)
34{
35 struct device_node *child;
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070036 int ret = 0;
37 u32 nsmr;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080038
39 ret = device_move(&pdev->dev, &msm_iommu_root_dev->dev, DPM_ORDER_NONE);
40 if (ret)
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070041 goto fail;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080042
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070043 ret = of_property_read_u32(pdev->dev.of_node, "qcom,iommu-smt-size",
44 &nsmr);
45 if (ret)
46 goto fail;
47
48 if (nsmr > MAX_NUM_SMR) {
49 pr_err("Invalid SMT size: %d\n", nsmr);
50 ret = -EINVAL;
51 goto fail;
52 }
53
54 drvdata->nsmr = nsmr;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080055 for_each_child_of_node(pdev->dev.of_node, child) {
56 drvdata->ncb++;
57 if (!of_platform_device_create(child, NULL, &pdev->dev))
58 pr_err("Failed to create %s device\n", child->name);
59 }
60
61 drvdata->name = dev_name(&pdev->dev);
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070062fail:
63 return ret;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080064}
65
66static atomic_t msm_iommu_next_id = ATOMIC_INIT(-1);
67
68static int __devinit msm_iommu_probe(struct platform_device *pdev)
69{
70 struct msm_iommu_drvdata *drvdata;
71 struct resource *r;
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -070072 int ret, needs_alt_core_clk;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080073
74 if (msm_iommu_root_dev == pdev)
75 return 0;
76
77 if (pdev->id == -1)
78 pdev->id = atomic_inc_return(&msm_iommu_next_id) - 1;
79
80 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
81 if (!drvdata)
82 return -ENOMEM;
83
84 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85 if (!r)
86 return -EINVAL;
87
88 drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
89 if (!drvdata->base)
90 return -ENOMEM;
91
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -070092 drvdata->gdsc = devm_regulator_get(&pdev->dev, "vdd");
93 if (IS_ERR(drvdata->gdsc))
94 return -EINVAL;
95
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -070096 drvdata->pclk = devm_clk_get(&pdev->dev, "iface_clk");
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080097 if (IS_ERR(drvdata->pclk))
98 return PTR_ERR(drvdata->pclk);
99
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700100 drvdata->clk = devm_clk_get(&pdev->dev, "core_clk");
101 if (IS_ERR(drvdata->clk))
102 return PTR_ERR(drvdata->clk);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800103
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700104 needs_alt_core_clk = of_property_read_bool(pdev->dev.of_node,
105 "qcom,needs-alt-core-clk");
106 if (needs_alt_core_clk) {
107 drvdata->aclk = devm_clk_get(&pdev->dev, "alt_core_clk");
108 if (IS_ERR(drvdata->aclk))
109 return PTR_ERR(drvdata->aclk);
110 }
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800111
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700112 if (clk_get_rate(drvdata->clk) == 0) {
113 ret = clk_round_rate(drvdata->clk, 1);
114 clk_set_rate(drvdata->clk, ret);
115 }
116
117 if (drvdata->aclk && clk_get_rate(drvdata->aclk) == 0) {
118 ret = clk_round_rate(drvdata->aclk, 1);
119 clk_set_rate(drvdata->aclk, ret);
120 }
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800121
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800122 ret = msm_iommu_parse_dt(pdev, drvdata);
123 if (ret)
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700124 return ret;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800125
126 pr_info("device %s mapped at %p, with %d ctx banks\n",
127 drvdata->name, drvdata->base, drvdata->ncb);
128
129 platform_set_drvdata(pdev, drvdata);
130
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800131 return 0;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800132}
133
134static int __devexit msm_iommu_remove(struct platform_device *pdev)
135{
136 struct msm_iommu_drvdata *drv = NULL;
137
138 drv = platform_get_drvdata(pdev);
139 if (drv) {
140 if (drv->clk)
141 clk_put(drv->clk);
142 clk_put(drv->pclk);
143 platform_set_drvdata(pdev, NULL);
144 }
145 return 0;
146}
147
148static int msm_iommu_ctx_parse_dt(struct platform_device *pdev,
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800149 struct msm_iommu_ctx_drvdata *ctx_drvdata)
150{
151 struct resource *r, rp;
Sathish Ambleycf045e62012-06-07 12:56:50 -0700152 int irq, ret;
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700153 u32 nsid;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800154
155 irq = platform_get_irq(pdev, 0);
156 if (irq > 0) {
157 ret = request_threaded_irq(irq, NULL,
158 msm_iommu_fault_handler_v2,
159 IRQF_ONESHOT | IRQF_SHARED,
160 "msm_iommu_nonsecure_irq", pdev);
161 if (ret) {
162 pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
163 return ret;
164 }
165 }
166
167 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
168 if (!r)
169 return -EINVAL;
170
171 ret = of_address_to_resource(pdev->dev.parent->of_node, 0, &rp);
172 if (ret)
173 return -EINVAL;
174
175 /* Calculate the context bank number using the base addresses. The
176 * first 8 pages belong to the global address space which is followed
177 * by the context banks, hence subtract by 8 to get the context bank
178 * number.
179 */
180 ctx_drvdata->num = ((r->start - rp.start) >> CTX_SHIFT) - 8;
181
Stepan Moskovchenkoce78fc22012-07-11 17:20:27 -0700182 if (of_property_read_string(pdev->dev.of_node, "label",
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800183 &ctx_drvdata->name))
184 ctx_drvdata->name = dev_name(&pdev->dev);
185
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700186 if (!of_get_property(pdev->dev.of_node, "qcom,iommu-ctx-sids", &nsid))
187 return -EINVAL;
188
189 if (nsid >= sizeof(ctx_drvdata->sids))
190 return -EINVAL;
191
192 if (of_property_read_u32_array(pdev->dev.of_node, "qcom,iommu-ctx-sids",
193 ctx_drvdata->sids,
194 nsid / sizeof(*ctx_drvdata->sids))) {
195 return -EINVAL;
196 }
197 ctx_drvdata->nsid = nsid;
198
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800199 return 0;
200}
201
202static int __devinit msm_iommu_ctx_probe(struct platform_device *pdev)
203{
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800204 struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
205 int ret;
206
207 if (!pdev->dev.parent)
208 return -EINVAL;
209
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800210 ctx_drvdata = devm_kzalloc(&pdev->dev, sizeof(*ctx_drvdata),
211 GFP_KERNEL);
212 if (!ctx_drvdata)
213 return -ENOMEM;
214
215 ctx_drvdata->pdev = pdev;
216 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
217 platform_set_drvdata(pdev, ctx_drvdata);
218
Sathish Ambleycf045e62012-06-07 12:56:50 -0700219 ret = msm_iommu_ctx_parse_dt(pdev, ctx_drvdata);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800220 if (!ret)
221 dev_info(&pdev->dev, "context %s using bank %d\n",
Stepan Moskovchenkoe14ca5c2012-07-11 12:40:51 -0700222 ctx_drvdata->name, ctx_drvdata->num);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800223
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800224 return ret;
225}
226
227static int __devexit msm_iommu_ctx_remove(struct platform_device *pdev)
228{
229 platform_set_drvdata(pdev, NULL);
230 return 0;
231}
232
233static struct of_device_id msm_iommu_match_table[] = {
234 { .compatible = "qcom,msm-smmu-v2", },
235 {}
236};
237
238static struct platform_driver msm_iommu_driver = {
239 .driver = {
240 .name = "msm_iommu_v2",
241 .of_match_table = msm_iommu_match_table,
242 },
243 .probe = msm_iommu_probe,
244 .remove = __devexit_p(msm_iommu_remove),
245};
246
247static struct of_device_id msm_iommu_ctx_match_table[] = {
248 { .name = "qcom,iommu-ctx", },
249 {}
250};
251
252static struct platform_driver msm_iommu_ctx_driver = {
253 .driver = {
254 .name = "msm_iommu_ctx_v2",
255 .of_match_table = msm_iommu_ctx_match_table,
256 },
257 .probe = msm_iommu_ctx_probe,
258 .remove = __devexit_p(msm_iommu_ctx_remove),
259};
260
261static int __init msm_iommu_driver_init(void)
262{
263 struct device_node *node;
264 int ret;
265
266 node = of_find_compatible_node(NULL, NULL, "qcom,msm-smmu-v2");
267 if (!node)
268 return -ENODEV;
269
270 of_node_put(node);
271
272 msm_iommu_root_dev = platform_device_register_simple(
273 "msm_iommu", -1, 0, 0);
274 if (!msm_iommu_root_dev) {
275 pr_err("Failed to create root IOMMU device\n");
276 ret = -ENODEV;
277 goto error;
278 }
279
280 atomic_inc(&msm_iommu_next_id);
281
282 ret = platform_driver_register(&msm_iommu_driver);
283 if (ret != 0) {
284 pr_err("Failed to register IOMMU driver\n");
285 goto error;
286 }
287
288 ret = platform_driver_register(&msm_iommu_ctx_driver);
289 if (ret != 0) {
290 pr_err("Failed to register IOMMU context driver\n");
291 goto error;
292 }
293
294error:
295 return ret;
296}
297
298static void __exit msm_iommu_driver_exit(void)
299{
300 platform_driver_unregister(&msm_iommu_ctx_driver);
301 platform_driver_unregister(&msm_iommu_driver);
302 platform_device_unregister(msm_iommu_root_dev);
303}
304
305subsys_initcall(msm_iommu_driver_init);
306module_exit(msm_iommu_driver_exit);
307
308MODULE_LICENSE("GPL v2");