blob: 79612801d43ecddf10d41d3fd51f5cec14172338 [file] [log] [blame]
Stephen Boyd55742b72012-08-08 11:40:26 -07001/* 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>
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080024#include <linux/of.h>
25#include <linux/of_address.h>
26#include <linux/of_device.h>
27
28#include <mach/iommu_hw-v2.h>
29#include <mach/iommu.h>
30
Stepan Moskovchenko880a3182012-10-01 12:35:24 -070031static int msm_iommu_parse_bfb_settings(struct platform_device *pdev,
32 struct msm_iommu_drvdata *drvdata)
33{
34 struct msm_iommu_bfb_settings *bfb_settings;
35 u32 nreg, nval;
36 int ret, i;
37
38 /*
39 * It is not valid for a device to have the qcom,iommu-bfb-regs
40 * property but not the qcom,iommu-bfb-data property, and vice versa.
41 */
42 if (!of_get_property(pdev->dev.of_node, "qcom,iommu-bfb-regs", &nreg)) {
43 if (of_get_property(pdev->dev.of_node, "qcom,iommu-bfb-data",
44 &nval))
45 return -EINVAL;
46 return 0;
47 }
48
49 if (!of_get_property(pdev->dev.of_node, "qcom,iommu-bfb-data", &nval))
50 return -EINVAL;
51
52 if (nreg >= sizeof(bfb_settings->regs))
53 return -EINVAL;
54
55 if (nval >= sizeof(bfb_settings->data))
56 return -EINVAL;
57
58 if (nval != nreg)
59 return -EINVAL;
60
61 bfb_settings = devm_kzalloc(&pdev->dev, sizeof(*bfb_settings),
62 GFP_KERNEL);
63 if (!bfb_settings)
64 return -ENOMEM;
65
66 ret = of_property_read_u32_array(pdev->dev.of_node,
67 "qcom,iommu-bfb-regs",
68 bfb_settings->regs,
69 nreg / sizeof(*bfb_settings->regs));
70 if (ret)
71 return ret;
72
73 ret = of_property_read_u32_array(pdev->dev.of_node,
74 "qcom,iommu-bfb-data",
75 bfb_settings->data,
76 nval / sizeof(*bfb_settings->data));
77 if (ret)
78 return ret;
79
80 bfb_settings->length = nreg / sizeof(*bfb_settings->regs);
81
82 for (i = 0; i < bfb_settings->length; i++)
83 if (bfb_settings->regs[i] < IMPLDEF_OFFSET ||
84 bfb_settings->regs[i] >= IMPLDEF_OFFSET + IMPLDEF_LENGTH)
85 return -EINVAL;
86
87 drvdata->bfb_settings = bfb_settings;
88 return 0;
89}
90
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080091static int msm_iommu_parse_dt(struct platform_device *pdev,
92 struct msm_iommu_drvdata *drvdata)
93{
94 struct device_node *child;
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070095 int ret = 0;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080096
Stephen Boyd55742b72012-08-08 11:40:26 -070097 drvdata->dev = &pdev->dev;
98 msm_iommu_add_drv(drvdata);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080099
Stepan Moskovchenko880a3182012-10-01 12:35:24 -0700100 ret = msm_iommu_parse_bfb_settings(pdev, drvdata);
101 if (ret)
102 goto fail;
103
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800104 for_each_child_of_node(pdev->dev.of_node, child) {
105 drvdata->ncb++;
106 if (!of_platform_device_create(child, NULL, &pdev->dev))
107 pr_err("Failed to create %s device\n", child->name);
108 }
109
110 drvdata->name = dev_name(&pdev->dev);
Laura Abbott0d135652012-10-04 12:59:03 -0700111 drvdata->sec_id = -1;
112 of_property_read_u32(pdev->dev.of_node, "qcom,iommu-secure-id",
113 &drvdata->sec_id);
114 return 0;
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700115fail:
116 return ret;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800117}
118
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800119static int __devinit msm_iommu_probe(struct platform_device *pdev)
120{
121 struct msm_iommu_drvdata *drvdata;
122 struct resource *r;
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700123 int ret, needs_alt_core_clk;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800124
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800125 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
126 if (!drvdata)
127 return -ENOMEM;
128
129 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
130 if (!r)
131 return -EINVAL;
132
133 drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
134 if (!drvdata->base)
135 return -ENOMEM;
136
Olav Haugan95d24162012-12-05 14:47:47 -0800137 drvdata->glb_base = drvdata->base;
138
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700139 drvdata->gdsc = devm_regulator_get(&pdev->dev, "vdd");
140 if (IS_ERR(drvdata->gdsc))
141 return -EINVAL;
142
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700143 drvdata->pclk = devm_clk_get(&pdev->dev, "iface_clk");
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800144 if (IS_ERR(drvdata->pclk))
145 return PTR_ERR(drvdata->pclk);
146
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700147 drvdata->clk = devm_clk_get(&pdev->dev, "core_clk");
148 if (IS_ERR(drvdata->clk))
149 return PTR_ERR(drvdata->clk);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800150
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700151 needs_alt_core_clk = of_property_read_bool(pdev->dev.of_node,
152 "qcom,needs-alt-core-clk");
153 if (needs_alt_core_clk) {
154 drvdata->aclk = devm_clk_get(&pdev->dev, "alt_core_clk");
155 if (IS_ERR(drvdata->aclk))
156 return PTR_ERR(drvdata->aclk);
157 }
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800158
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700159 if (clk_get_rate(drvdata->clk) == 0) {
160 ret = clk_round_rate(drvdata->clk, 1);
161 clk_set_rate(drvdata->clk, ret);
162 }
163
164 if (drvdata->aclk && clk_get_rate(drvdata->aclk) == 0) {
165 ret = clk_round_rate(drvdata->aclk, 1);
166 clk_set_rate(drvdata->aclk, ret);
167 }
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800168
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800169 ret = msm_iommu_parse_dt(pdev, drvdata);
170 if (ret)
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700171 return ret;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800172
173 pr_info("device %s mapped at %p, with %d ctx banks\n",
174 drvdata->name, drvdata->base, drvdata->ncb);
175
176 platform_set_drvdata(pdev, drvdata);
177
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800178 return 0;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800179}
180
181static int __devexit msm_iommu_remove(struct platform_device *pdev)
182{
183 struct msm_iommu_drvdata *drv = NULL;
184
185 drv = platform_get_drvdata(pdev);
186 if (drv) {
Stephen Boyd55742b72012-08-08 11:40:26 -0700187 msm_iommu_remove_drv(drv);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800188 if (drv->clk)
189 clk_put(drv->clk);
190 clk_put(drv->pclk);
191 platform_set_drvdata(pdev, NULL);
192 }
193 return 0;
194}
195
196static int msm_iommu_ctx_parse_dt(struct platform_device *pdev,
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800197 struct msm_iommu_ctx_drvdata *ctx_drvdata)
198{
199 struct resource *r, rp;
Sathish Ambleycf045e62012-06-07 12:56:50 -0700200 int irq, ret;
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700201 u32 nsid;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800202
203 irq = platform_get_irq(pdev, 0);
204 if (irq > 0) {
205 ret = request_threaded_irq(irq, NULL,
206 msm_iommu_fault_handler_v2,
207 IRQF_ONESHOT | IRQF_SHARED,
208 "msm_iommu_nonsecure_irq", pdev);
209 if (ret) {
210 pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
211 return ret;
212 }
213 }
214
215 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
216 if (!r)
217 return -EINVAL;
218
219 ret = of_address_to_resource(pdev->dev.parent->of_node, 0, &rp);
220 if (ret)
221 return -EINVAL;
222
223 /* Calculate the context bank number using the base addresses. The
224 * first 8 pages belong to the global address space which is followed
225 * by the context banks, hence subtract by 8 to get the context bank
226 * number.
227 */
228 ctx_drvdata->num = ((r->start - rp.start) >> CTX_SHIFT) - 8;
229
Stepan Moskovchenkoce78fc22012-07-11 17:20:27 -0700230 if (of_property_read_string(pdev->dev.of_node, "label",
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800231 &ctx_drvdata->name))
232 ctx_drvdata->name = dev_name(&pdev->dev);
233
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700234 if (!of_get_property(pdev->dev.of_node, "qcom,iommu-ctx-sids", &nsid))
235 return -EINVAL;
236
237 if (nsid >= sizeof(ctx_drvdata->sids))
238 return -EINVAL;
239
240 if (of_property_read_u32_array(pdev->dev.of_node, "qcom,iommu-ctx-sids",
241 ctx_drvdata->sids,
242 nsid / sizeof(*ctx_drvdata->sids))) {
243 return -EINVAL;
244 }
245 ctx_drvdata->nsid = nsid;
246
Olav Haugan26ddd432012-12-07 11:39:21 -0800247 ctx_drvdata->secure_context = of_property_read_bool(pdev->dev.of_node,
248 "qcom,secure-context");
249 ctx_drvdata->asid = -1;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800250 return 0;
251}
252
253static int __devinit msm_iommu_ctx_probe(struct platform_device *pdev)
254{
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800255 struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
256 int ret;
257
258 if (!pdev->dev.parent)
259 return -EINVAL;
260
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800261 ctx_drvdata = devm_kzalloc(&pdev->dev, sizeof(*ctx_drvdata),
262 GFP_KERNEL);
263 if (!ctx_drvdata)
264 return -ENOMEM;
265
266 ctx_drvdata->pdev = pdev;
267 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
268 platform_set_drvdata(pdev, ctx_drvdata);
269
Sathish Ambleycf045e62012-06-07 12:56:50 -0700270 ret = msm_iommu_ctx_parse_dt(pdev, ctx_drvdata);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800271 if (!ret)
272 dev_info(&pdev->dev, "context %s using bank %d\n",
Stepan Moskovchenkoe14ca5c2012-07-11 12:40:51 -0700273 ctx_drvdata->name, ctx_drvdata->num);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800274
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800275 return ret;
276}
277
278static int __devexit msm_iommu_ctx_remove(struct platform_device *pdev)
279{
280 platform_set_drvdata(pdev, NULL);
281 return 0;
282}
283
284static struct of_device_id msm_iommu_match_table[] = {
285 { .compatible = "qcom,msm-smmu-v2", },
286 {}
287};
288
289static struct platform_driver msm_iommu_driver = {
290 .driver = {
291 .name = "msm_iommu_v2",
292 .of_match_table = msm_iommu_match_table,
293 },
294 .probe = msm_iommu_probe,
295 .remove = __devexit_p(msm_iommu_remove),
296};
297
298static struct of_device_id msm_iommu_ctx_match_table[] = {
299 { .name = "qcom,iommu-ctx", },
300 {}
301};
302
303static struct platform_driver msm_iommu_ctx_driver = {
304 .driver = {
305 .name = "msm_iommu_ctx_v2",
306 .of_match_table = msm_iommu_ctx_match_table,
307 },
308 .probe = msm_iommu_ctx_probe,
309 .remove = __devexit_p(msm_iommu_ctx_remove),
310};
311
312static int __init msm_iommu_driver_init(void)
313{
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800314 int ret;
315
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800316 ret = platform_driver_register(&msm_iommu_driver);
317 if (ret != 0) {
318 pr_err("Failed to register IOMMU driver\n");
319 goto error;
320 }
321
322 ret = platform_driver_register(&msm_iommu_ctx_driver);
323 if (ret != 0) {
324 pr_err("Failed to register IOMMU context driver\n");
325 goto error;
326 }
327
328error:
329 return ret;
330}
331
332static void __exit msm_iommu_driver_exit(void)
333{
334 platform_driver_unregister(&msm_iommu_ctx_driver);
335 platform_driver_unregister(&msm_iommu_driver);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800336}
337
338subsys_initcall(msm_iommu_driver_init);
339module_exit(msm_iommu_driver_exit);
340
341MODULE_LICENSE("GPL v2");