blob: 2d0fba279a2d714854d59da4cea9bdf66e48e910 [file] [log] [blame]
Olav Haugane6d01ef2013-01-25 16:55:44 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -07002 *
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.
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070011 */
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>
Stephen Boyd55742b72012-08-08 11:40:26 -070024#include <linux/list.h>
25#include <linux/mutex.h>
Olav Haugan95d24162012-12-05 14:47:47 -080026#include <linux/of.h>
27#include <linux/of_address.h>
28#include <linux/of_device.h>
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070029
Olav Hauganc5993142013-02-04 13:59:39 -080030#include <mach/iommu_perfmon.h>
Olav Haugane6d01ef2013-01-25 16:55:44 -080031#include <mach/iommu_hw-v0.h>
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070032#include <mach/iommu.h>
Olav Haugan236970a2013-05-14 17:00:02 -070033#include <mach/msm_bus.h>
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070034
Olav Haugan336bf412013-05-10 16:22:21 -070035static struct of_device_id msm_iommu_v0_ctx_match_table[];
Olav Haugan0858ae02013-06-04 16:51:50 -070036static struct iommu_access_ops *msm_iommu_access_ops;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070037
Olav Haugan95d24162012-12-05 14:47:47 -080038static void msm_iommu_reset(void __iomem *base, void __iomem *glb_base, int ncb)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070039{
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080040 int ctx;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070041
Olav Haugan95d24162012-12-05 14:47:47 -080042 SET_RPUE(glb_base, 0);
43 SET_RPUEIE(glb_base, 0);
44 SET_ESRRESTORE(glb_base, 0);
45 SET_TBE(glb_base, 0);
46 SET_CR(glb_base, 0);
47 SET_SPDMBE(glb_base, 0);
48 SET_TESTBUSCR(glb_base, 0);
49 SET_TLBRSW(glb_base, 0);
50 SET_GLOBAL_TLBIALL(glb_base, 0);
51 SET_RPU_ACR(glb_base, 0);
52 SET_TLBLKCRWE(glb_base, 1);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070053
54 for (ctx = 0; ctx < ncb; ctx++) {
Olav Haugan95d24162012-12-05 14:47:47 -080055 SET_BPRCOSH(glb_base, ctx, 0);
56 SET_BPRCISH(glb_base, ctx, 0);
57 SET_BPRCNSH(glb_base, ctx, 0);
58 SET_BPSHCFG(glb_base, ctx, 0);
59 SET_BPMTCFG(glb_base, ctx, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070060 SET_ACTLR(base, ctx, 0);
61 SET_SCTLR(base, ctx, 0);
62 SET_FSRRESTORE(base, ctx, 0);
63 SET_TTBR0(base, ctx, 0);
64 SET_TTBR1(base, ctx, 0);
65 SET_TTBCR(base, ctx, 0);
66 SET_BFBCR(base, ctx, 0);
67 SET_PAR(base, ctx, 0);
68 SET_FAR(base, ctx, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070069 SET_TLBFLPTER(base, ctx, 0);
70 SET_TLBSLPTER(base, ctx, 0);
71 SET_TLBLKCR(base, ctx, 0);
Stepan Moskovchenko0272fd62011-09-26 15:06:40 -070072 SET_CTX_TLBIALL(base, ctx, 0);
73 SET_TLBIVA(base, ctx, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070074 SET_PRRR(base, ctx, 0);
75 SET_NMRR(base, ctx, 0);
76 SET_CONTEXTIDR(base, ctx, 0);
77 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078 mb();
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070079}
80
Olav Haugan08fb8ac2013-05-15 15:30:07 -070081static int __get_clocks(struct platform_device *pdev,
82 struct msm_iommu_drvdata *drvdata,
83 int needs_alt_core_clk)
84{
85 int ret = 0;
86
87 drvdata->pclk = devm_clk_get(&pdev->dev, "iface_clk");
88 if (IS_ERR(drvdata->pclk)) {
89 ret = PTR_ERR(drvdata->pclk);
90 drvdata->pclk = NULL;
91 if (ret != -EPROBE_DEFER) {
92 pr_err("Unable to get %s clock for %s IOMMU device\n",
93 dev_name(&pdev->dev), drvdata->name);
94 }
95 goto fail;
96 }
97
98 drvdata->clk = devm_clk_get(&pdev->dev, "core_clk");
99
100 if (!IS_ERR(drvdata->clk)) {
101 if (clk_get_rate(drvdata->clk) == 0) {
102 ret = clk_round_rate(drvdata->clk, 1000);
103 clk_set_rate(drvdata->clk, ret);
104 }
105 } else {
106 drvdata->clk = NULL;
107 }
108
109 if (needs_alt_core_clk) {
110 drvdata->aclk = devm_clk_get(&pdev->dev, "alt_core_clk");
111 if (IS_ERR(drvdata->aclk)) {
112 ret = PTR_ERR(drvdata->aclk);
113 goto fail;
114 }
115 }
116
117 if (drvdata->aclk && clk_get_rate(drvdata->aclk) == 0) {
118 ret = clk_round_rate(drvdata->aclk, 1000);
119 clk_set_rate(drvdata->aclk, ret);
120 }
121
122 return 0;
123fail:
124 return ret;
125}
126
Olav Haugan336bf412013-05-10 16:22:21 -0700127#ifdef CONFIG_OF_DEVICE
Olav Haugan236970a2013-05-14 17:00:02 -0700128
129static int __get_bus_vote_client(struct platform_device *pdev,
130 struct msm_iommu_drvdata *drvdata)
131{
132 int ret = 0;
133 struct msm_bus_scale_pdata *bs_table;
134 const char *dummy;
135
136 /* Check whether bus scaling has been specified for this node */
137 ret = of_property_read_string(pdev->dev.of_node, "qcom,msm-bus,name",
138 &dummy);
139 if (ret)
140 return 0;
141
142 bs_table = msm_bus_cl_get_pdata(pdev);
143
144 if (bs_table) {
145 drvdata->bus_client = msm_bus_scale_register_client(bs_table);
146 if (IS_ERR(&drvdata->bus_client)) {
147 pr_err("%s(): Bus client register failed.\n", __func__);
148 ret = -EINVAL;
149 }
150 }
151 return ret;
152}
153
154static void __put_bus_vote_client(struct msm_iommu_drvdata *drvdata)
155{
156 msm_bus_scale_unregister_client(drvdata->bus_client);
157 drvdata->bus_client = 0;
158}
159
Olav Haugan95d24162012-12-05 14:47:47 -0800160static int msm_iommu_parse_dt(struct platform_device *pdev,
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700161 struct msm_iommu_drvdata *drvdata)
Olav Haugan95d24162012-12-05 14:47:47 -0800162{
Olav Haugan95d24162012-12-05 14:47:47 -0800163 struct device_node *child;
164 struct resource *r;
165 u32 glb_offset = 0;
Olav Haugan236970a2013-05-14 17:00:02 -0700166 int ret = 0;
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700167 int needs_alt_core_clk;
Olav Haugan236970a2013-05-14 17:00:02 -0700168
169 ret = __get_bus_vote_client(pdev, drvdata);
170
171 if (ret)
172 goto fail;
Olav Haugan95d24162012-12-05 14:47:47 -0800173
174 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
175 if (!r) {
176 pr_err("%s: Missing property reg\n", __func__);
Olav Haugan236970a2013-05-14 17:00:02 -0700177 ret = -EINVAL;
178 goto fail;
Olav Haugan95d24162012-12-05 14:47:47 -0800179 }
180 drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
181 if (!drvdata->base) {
Stepan Moskovchenko61fa3db2012-08-10 17:51:18 +0100182 pr_err("%s: Unable to ioremap %pr\n", __func__, r);
Olav Haugan236970a2013-05-14 17:00:02 -0700183 ret = -ENOMEM;
184 goto fail;
Olav Haugan95d24162012-12-05 14:47:47 -0800185 }
186 drvdata->glb_base = drvdata->base;
187
188 if (!of_property_read_u32(pdev->dev.of_node, "qcom,glb-offset",
189 &glb_offset)) {
190 drvdata->glb_base += glb_offset;
191 } else {
192 pr_err("%s: Missing property qcom,glb-offset\n", __func__);
Olav Haugan236970a2013-05-14 17:00:02 -0700193 ret = -EINVAL;
194 goto fail;
Olav Haugan95d24162012-12-05 14:47:47 -0800195 }
196
Olav Haugan336bf412013-05-10 16:22:21 -0700197 for_each_child_of_node(pdev->dev.of_node, child)
Olav Haugan95d24162012-12-05 14:47:47 -0800198 drvdata->ncb++;
Olav Haugan95d24162012-12-05 14:47:47 -0800199
Olav Haugan7a2f99c2013-02-04 14:43:26 -0800200 ret = of_property_read_string(pdev->dev.of_node, "label",
201 &drvdata->name);
202 if (ret) {
203 pr_err("%s: Missing property label\n", __func__);
Olav Haugan236970a2013-05-14 17:00:02 -0700204 ret = -EINVAL;
205 goto fail;
Olav Haugan7a2f99c2013-02-04 14:43:26 -0800206 }
Olav Haugan97ce7aa2013-04-30 13:59:41 -0700207
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700208 needs_alt_core_clk = of_property_read_bool(pdev->dev.of_node,
Olav Haugan97ce7aa2013-04-30 13:59:41 -0700209 "qcom,needs-alt-core-clk");
210
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700211 ret = __get_clocks(pdev, drvdata, needs_alt_core_clk);
212
213 if (ret)
214 goto fail;
215
Olav Haugan95d24162012-12-05 14:47:47 -0800216 drvdata->sec_id = -1;
217 drvdata->ttbr_split = 0;
Olav Haugan336bf412013-05-10 16:22:21 -0700218
219 ret = of_platform_populate(pdev->dev.of_node,
220 msm_iommu_v0_ctx_match_table,
221 NULL, &pdev->dev);
Olav Haugan236970a2013-05-14 17:00:02 -0700222 if (ret) {
Olav Haugan336bf412013-05-10 16:22:21 -0700223 pr_err("Failed to create iommu context device\n");
Olav Haugan236970a2013-05-14 17:00:02 -0700224 goto fail;
225 }
Olav Haugan336bf412013-05-10 16:22:21 -0700226
227 return ret;
Olav Haugan236970a2013-05-14 17:00:02 -0700228
229fail:
230 __put_bus_vote_client(drvdata);
231 return ret;
Olav Haugan336bf412013-05-10 16:22:21 -0700232}
233
234#else
235static int msm_iommu_parse_dt(struct platform_device *pdev,
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700236 struct msm_iommu_drvdata *drvdata)
Olav Haugan336bf412013-05-10 16:22:21 -0700237{
Olav Haugan95d24162012-12-05 14:47:47 -0800238 return 0;
239}
Olav Haugan236970a2013-05-14 17:00:02 -0700240
241static void __put_bus_vote_client(struct msm_iommu_drvdata *drvdata)
242{
243
244}
245
Olav Haugan336bf412013-05-10 16:22:21 -0700246#endif
Olav Haugan95d24162012-12-05 14:47:47 -0800247
Olav Haugan95d24162012-12-05 14:47:47 -0800248/*
249 * Do a basic check of the IOMMU by performing an ATS operation
250 * on context bank 0.
251 */
252static int iommu_sanity_check(struct msm_iommu_drvdata *drvdata)
253{
254 int par;
255 int ret = 0;
256
257 SET_M(drvdata->base, 0, 1);
258 SET_PAR(drvdata->base, 0, 0);
259 SET_V2PCFG(drvdata->base, 0, 1);
260 SET_V2PPR(drvdata->base, 0, 0);
261 mb();
262 par = GET_PAR(drvdata->base, 0);
263 SET_V2PCFG(drvdata->base, 0, 0);
264 SET_M(drvdata->base, 0, 0);
265 mb();
266
267 if (!par) {
268 pr_err("%s: Invalid PAR value detected\n", drvdata->name);
269 ret = -ENODEV;
270 }
271 return ret;
272}
273
Olav Hauganc5993142013-02-04 13:59:39 -0800274static int msm_iommu_pmon_parse_dt(struct platform_device *pdev,
275 struct iommu_pmon *pmon_info)
276{
277 int ret = 0;
278 int irq = platform_get_irq(pdev, 0);
279 unsigned int cls_prop_size;
280
281 if (irq > 0) {
282 pmon_info->iommu.evt_irq = platform_get_irq(pdev, 0);
283
284 ret = of_property_read_u32(pdev->dev.of_node,
285 "qcom,iommu-pmu-ngroups",
286 &pmon_info->num_groups);
287 if (ret) {
288 pr_err("Error reading qcom,iommu-pmu-ngroups\n");
289 goto fail;
290 }
291 ret = of_property_read_u32(pdev->dev.of_node,
292 "qcom,iommu-pmu-ncounters",
293 &pmon_info->num_counters);
294 if (ret) {
295 pr_err("Error reading qcom,iommu-pmu-ncounters\n");
296 goto fail;
297 }
298
299 if (!of_get_property(pdev->dev.of_node,
300 "qcom,iommu-pmu-event-classes",
301 &cls_prop_size)) {
302 pr_err("Error reading qcom,iommu-pmu-event-classes\n");
303 return -EINVAL;
304 }
305
306 pmon_info->event_cls_supported =
307 devm_kzalloc(&pdev->dev, cls_prop_size, GFP_KERNEL);
308
309 if (!pmon_info->event_cls_supported) {
310 pr_err("Unable to get memory for event class array\n");
311 return -ENOMEM;
312 }
313
314 pmon_info->nevent_cls_supported = cls_prop_size / sizeof(u32);
315
316 ret = of_property_read_u32_array(pdev->dev.of_node,
317 "qcom,iommu-pmu-event-classes",
318 pmon_info->event_cls_supported,
319 pmon_info->nevent_cls_supported);
320 if (ret) {
321 pr_err("Error reading qcom,iommu-pmu-event-classes\n");
322 return ret;
323 }
324 } else {
325 pmon_info->iommu.evt_irq = -1;
326 ret = irq;
327 }
328
329fail:
330 return ret;
331}
332
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700333static int msm_iommu_probe(struct platform_device *pdev)
334{
Olav Hauganc5993142013-02-04 13:59:39 -0800335 struct iommu_pmon *pmon_info;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700336 struct msm_iommu_drvdata *drvdata;
337 struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data;
Olav Haugan95d24162012-12-05 14:47:47 -0800338 int ret;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700339
Olav Haugan95d24162012-12-05 14:47:47 -0800340 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700341
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800342 if (!drvdata) {
343 ret = -ENOMEM;
Olav Haugan236970a2013-05-14 17:00:02 -0700344 goto fail_mem;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800345 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700346
Olav Haugan95d24162012-12-05 14:47:47 -0800347 if (pdev->dev.of_node) {
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700348 ret = msm_iommu_parse_dt(pdev, drvdata);
Olav Haugan95d24162012-12-05 14:47:47 -0800349 if (ret)
350 goto fail;
351 } else if (pdev->dev.platform_data) {
352 struct resource *r, *r2;
353 resource_size_t len;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700354
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700355 ret = __get_clocks(pdev, drvdata, 0);
356
357 if (ret)
358 goto fail;
359
Olav Haugan95d24162012-12-05 14:47:47 -0800360 r = platform_get_resource_byname(pdev, IORESOURCE_MEM,
361 "physbase");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700362
Olav Haugan95d24162012-12-05 14:47:47 -0800363 if (!r) {
364 ret = -ENODEV;
365 goto fail;
Matt Wagantall99858222011-11-08 14:31:27 -0800366 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700367
Olav Haugan95d24162012-12-05 14:47:47 -0800368 len = resource_size(r);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700369
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700370 r2 = devm_request_mem_region(&pdev->dev, r->start,
371 len, r->name);
Olav Haugan95d24162012-12-05 14:47:47 -0800372 if (!r2) {
Stepan Moskovchenko61fa3db2012-08-10 17:51:18 +0100373 pr_err("Could not request memory region: %pr\n", r);
Olav Haugan95d24162012-12-05 14:47:47 -0800374 ret = -EBUSY;
375 goto fail;
376 }
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800377
Olav Haugan95d24162012-12-05 14:47:47 -0800378 drvdata->base = devm_ioremap(&pdev->dev, r2->start, len);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800379
Olav Haugan95d24162012-12-05 14:47:47 -0800380 if (!drvdata->base) {
Stepan Moskovchenko61fa3db2012-08-10 17:51:18 +0100381 pr_err("Could not ioremap: %pr\n", r);
Olav Haugan95d24162012-12-05 14:47:47 -0800382 ret = -EBUSY;
383 goto fail;
384 }
385 /*
386 * Global register space offset for legacy IOMMUv1 hardware
387 * is always 0xFF000
388 */
389 drvdata->glb_base = drvdata->base + 0xFF000;
390 drvdata->name = iommu_dev->name;
391 drvdata->dev = &pdev->dev;
392 drvdata->ncb = iommu_dev->ncb;
393 drvdata->ttbr_split = iommu_dev->ttbr_split;
394 } else {
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800395 ret = -ENODEV;
Olav Haugan95d24162012-12-05 14:47:47 -0800396 goto fail;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800397 }
398
Stephen Boyd55742b72012-08-08 11:40:26 -0700399 drvdata->dev = &pdev->dev;
400
Olav Haugan0858ae02013-06-04 16:51:50 -0700401 msm_iommu_access_ops->iommu_clk_on(drvdata);
Olav Haugan95d24162012-12-05 14:47:47 -0800402
403 msm_iommu_reset(drvdata->base, drvdata->glb_base, drvdata->ncb);
404
405 ret = iommu_sanity_check(drvdata);
406 if (ret)
407 goto fail_clk;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800408
Olav Haugan0858ae02013-06-04 16:51:50 -0700409 msm_iommu_access_ops->iommu_clk_off(drvdata);
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700410
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700411 pr_info("device %s mapped at %p, with %d ctx banks\n",
Olav Haugan95d24162012-12-05 14:47:47 -0800412 drvdata->name, drvdata->base, drvdata->ncb);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700413
Olav Haugan95d24162012-12-05 14:47:47 -0800414 msm_iommu_add_drv(drvdata);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800415 platform_set_drvdata(pdev, drvdata);
416
Olav Hauganc5993142013-02-04 13:59:39 -0800417 pmon_info = msm_iommu_pm_alloc(&pdev->dev);
418 if (pmon_info != NULL) {
419 ret = msm_iommu_pmon_parse_dt(pdev, pmon_info);
420 if (ret) {
421 msm_iommu_pm_free(&pdev->dev);
422 pr_info("%s: pmon not available.\n", drvdata->name);
423 } else {
424 pmon_info->iommu.base = drvdata->base;
Olav Haugan0858ae02013-06-04 16:51:50 -0700425 pmon_info->iommu.ops = msm_iommu_access_ops;
Olav Hauganc5993142013-02-04 13:59:39 -0800426 pmon_info->iommu.hw_ops = iommu_pm_get_hw_ops_v0();
427 pmon_info->iommu.iommu_name = drvdata->name;
Olav Haugan463e6402013-04-15 10:53:32 -0700428 pmon_info->iommu.always_on = 1;
Olav Hauganc5993142013-02-04 13:59:39 -0800429 ret = msm_iommu_pm_iommu_register(pmon_info);
430 if (ret) {
431 pr_err("%s iommu register fail\n",
432 drvdata->name);
433 msm_iommu_pm_free(&pdev->dev);
434 } else {
435 pr_debug("%s iommu registered for pmon\n",
436 pmon_info->iommu.iommu_name);
437 }
438 }
439 }
440
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700441 return 0;
Olav Haugan95d24162012-12-05 14:47:47 -0800442
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800443fail_clk:
Olav Haugan0858ae02013-06-04 16:51:50 -0700444 msm_iommu_access_ops->iommu_clk_off(drvdata);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700445fail:
Olav Haugan236970a2013-05-14 17:00:02 -0700446 __put_bus_vote_client(drvdata);
447fail_mem:
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700448 return ret;
449}
450
451static int msm_iommu_remove(struct platform_device *pdev)
452{
453 struct msm_iommu_drvdata *drv = NULL;
454
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700455 msm_iommu_pm_iommu_unregister(&pdev->dev);
456 msm_iommu_pm_free(&pdev->dev);
457
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700458 drv = platform_get_drvdata(pdev);
459 if (drv) {
Olav Haugan236970a2013-05-14 17:00:02 -0700460 __put_bus_vote_client(drv);
Stephen Boyd55742b72012-08-08 11:40:26 -0700461 msm_iommu_remove_drv(drv);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700462 platform_set_drvdata(pdev, NULL);
463 }
464 return 0;
465}
466
Olav Haugan95d24162012-12-05 14:47:47 -0800467static int msm_iommu_ctx_parse_dt(struct platform_device *pdev,
468 struct msm_iommu_ctx_drvdata *ctx_drvdata)
469{
470 struct resource *r, rp;
471 int irq, ret;
472 u32 nmid_array_size;
473 u32 nmid;
474
475 irq = platform_get_irq(pdev, 0);
476 if (irq > 0) {
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700477 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
Olav Haugan95d24162012-12-05 14:47:47 -0800478 msm_iommu_fault_handler,
479 IRQF_ONESHOT | IRQF_SHARED,
Olav Haugan3b9d8542013-02-28 17:49:00 -0800480 "msm_iommu_nonsecure_irq", ctx_drvdata);
Olav Haugan95d24162012-12-05 14:47:47 -0800481 if (ret) {
482 pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700483 goto out;
Olav Haugan95d24162012-12-05 14:47:47 -0800484 }
485 }
486
487 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
488 if (!r) {
489 pr_err("Could not find reg property for context bank\n");
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700490 ret = -EINVAL;
491 goto out;
Olav Haugan95d24162012-12-05 14:47:47 -0800492 }
493
494 ret = of_address_to_resource(pdev->dev.parent->of_node, 0, &rp);
495 if (ret) {
496 pr_err("of_address_to_resource failed\n");
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700497 ret = -EINVAL;
498 goto out;
Olav Haugan95d24162012-12-05 14:47:47 -0800499 }
500
501 /* Calculate the context bank number using the base addresses. CB0
502 * starts at the base address.
503 */
504 ctx_drvdata->num = ((r->start - rp.start) >> CTX_SHIFT);
505
506 if (of_property_read_string(pdev->dev.of_node, "label",
507 &ctx_drvdata->name)) {
508 pr_err("Could not find label property\n");
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700509 ret = -EINVAL;
510 goto out;
Olav Haugan95d24162012-12-05 14:47:47 -0800511 }
512
513 if (!of_get_property(pdev->dev.of_node, "qcom,iommu-ctx-mids",
514 &nmid_array_size)) {
515 pr_err("Could not find iommu-ctx-mids property\n");
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700516 ret = -EINVAL;
517 goto out;
Olav Haugan95d24162012-12-05 14:47:47 -0800518 }
519 if (nmid_array_size >= sizeof(ctx_drvdata->sids)) {
520 pr_err("Too many mids defined - array size: %u, mids size: %u\n",
521 nmid_array_size, sizeof(ctx_drvdata->sids));
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700522 ret = -EINVAL;
523 goto out;
Olav Haugan95d24162012-12-05 14:47:47 -0800524 }
525 nmid = nmid_array_size / sizeof(*ctx_drvdata->sids);
526
527 if (of_property_read_u32_array(pdev->dev.of_node, "qcom,iommu-ctx-mids",
528 ctx_drvdata->sids, nmid)) {
529 pr_err("Could not find iommu-ctx-mids property\n");
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700530 ret = -EINVAL;
531 goto out;
Olav Haugan95d24162012-12-05 14:47:47 -0800532 }
533 ctx_drvdata->nsid = nmid;
534
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700535out:
536 return ret;
Olav Haugan95d24162012-12-05 14:47:47 -0800537}
538
539static void __program_m2v_tables(struct msm_iommu_drvdata *drvdata,
540 struct msm_iommu_ctx_drvdata *ctx_drvdata)
541{
542 int i;
543
544 /* Program the M2V tables for this context */
545 for (i = 0; i < ctx_drvdata->nsid; i++) {
546 int sid = ctx_drvdata->sids[i];
547 int num = ctx_drvdata->num;
548
549 SET_M2VCBR_N(drvdata->glb_base, sid, 0);
550 SET_CBACR_N(drvdata->glb_base, num, 0);
551
552 /* Route page faults to the non-secure interrupt */
553 SET_IRPTNDX(drvdata->glb_base, num, 1);
554
555 /* Set VMID = 0 */
556 SET_VMID(drvdata->glb_base, sid, 0);
557
558 /* Set the context number for that SID to this context */
559 SET_CBNDX(drvdata->glb_base, sid, num);
560
561 /* Set SID associated with this context bank to 0 */
562 SET_CBVMID(drvdata->glb_base, num, 0);
563
564 /* Set the ASID for TLB tagging for this context to 0 */
565 SET_CONTEXTIDR_ASID(drvdata->base, num, 0);
566
567 /* Set security bit override to be Non-secure */
568 SET_NSCFG(drvdata->glb_base, sid, 3);
569 }
570 mb();
571}
572
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700573static int msm_iommu_ctx_probe(struct platform_device *pdev)
574{
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700575 struct msm_iommu_drvdata *drvdata;
576 struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700577 int i, ret, irq;
Olav Haugan95d24162012-12-05 14:47:47 -0800578 if (!pdev->dev.parent) {
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700579 ret = -EINVAL;
580 goto fail;
581 }
582
583 drvdata = dev_get_drvdata(pdev->dev.parent);
584
585 if (!drvdata) {
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700586 ret = -EPROBE_DEFER;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700587 goto fail;
588 }
589
Olav Haugan95d24162012-12-05 14:47:47 -0800590 ctx_drvdata = devm_kzalloc(&pdev->dev, sizeof(*ctx_drvdata),
591 GFP_KERNEL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700592 if (!ctx_drvdata) {
593 ret = -ENOMEM;
594 goto fail;
595 }
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700596
Olav Haugan95d24162012-12-05 14:47:47 -0800597 ctx_drvdata->pdev = pdev;
598 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
599 platform_set_drvdata(pdev, ctx_drvdata);
Olav Haugane99ee7e2012-12-11 15:02:02 -0800600 ctx_drvdata->attach_count = 0;
Olav Haugan95d24162012-12-05 14:47:47 -0800601
602 if (pdev->dev.of_node) {
603 ret = msm_iommu_ctx_parse_dt(pdev, ctx_drvdata);
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700604 if (ret) {
605 platform_set_drvdata(pdev, NULL);
Olav Haugan95d24162012-12-05 14:47:47 -0800606 goto fail;
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700607 }
Olav Haugan95d24162012-12-05 14:47:47 -0800608 } else if (pdev->dev.platform_data) {
609 struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
610
611 ctx_drvdata->num = c->num;
612 ctx_drvdata->name = c->name;
613
614 for (i = 0; i < MAX_NUM_MIDS; ++i) {
615 if (c->mids[i] == -1) {
616 ctx_drvdata->nsid = i;
617 break;
618 }
619 ctx_drvdata->sids[i] = c->mids[i];
620 }
621 irq = platform_get_irq_byname(
622 to_platform_device(pdev->dev.parent),
623 "nonsecure_irq");
624 if (irq < 0) {
625 ret = -ENODEV;
626 goto fail;
627 }
628
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700629 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
630 msm_iommu_fault_handler,
Olav Haugan95d24162012-12-05 14:47:47 -0800631 IRQF_ONESHOT | IRQF_SHARED,
632 "msm_iommu_nonsecure_irq", ctx_drvdata);
633
634 if (ret) {
635 pr_err("request_threaded_irq %d failed: %d\n", irq,
636 ret);
637 goto fail;
638 }
639 } else {
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700640 ret = -ENODEV;
641 goto fail;
642 }
643
Olav Haugan0858ae02013-06-04 16:51:50 -0700644 msm_iommu_access_ops->iommu_clk_on(drvdata);
Olav Haugan95d24162012-12-05 14:47:47 -0800645 __program_m2v_tables(drvdata, ctx_drvdata);
Olav Haugan0858ae02013-06-04 16:51:50 -0700646 msm_iommu_access_ops->iommu_clk_off(drvdata);
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700647
Olav Haugan95d24162012-12-05 14:47:47 -0800648 dev_info(&pdev->dev, "context %s using bank %d\n", ctx_drvdata->name,
649 ctx_drvdata->num);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700650 return 0;
651fail:
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700652 return ret;
653}
654
Olav Haugan95d24162012-12-05 14:47:47 -0800655static int __devexit msm_iommu_ctx_remove(struct platform_device *pdev)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700656{
Olav Haugan95d24162012-12-05 14:47:47 -0800657 platform_set_drvdata(pdev, NULL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700658 return 0;
659}
660
Olav Haugan95d24162012-12-05 14:47:47 -0800661
662static struct of_device_id msm_iommu_match_table[] = {
Olav Haugan0e22c482013-01-28 17:39:36 -0800663 { .compatible = "qcom,msm-smmu-v0", },
Olav Haugan95d24162012-12-05 14:47:47 -0800664 {}
665};
666
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700667static struct platform_driver msm_iommu_driver = {
668 .driver = {
Olav Haugan0e22c482013-01-28 17:39:36 -0800669 .name = "msm_iommu-v0",
Olav Haugan95d24162012-12-05 14:47:47 -0800670 .of_match_table = msm_iommu_match_table,
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700671 },
672 .probe = msm_iommu_probe,
Olav Haugan95d24162012-12-05 14:47:47 -0800673 .remove = __devexit_p(msm_iommu_remove),
674};
675
Olav Haugan336bf412013-05-10 16:22:21 -0700676static struct of_device_id msm_iommu_v0_ctx_match_table[] = {
677 { .compatible = "qcom,msm-smmu-v0-ctx", },
Olav Haugan95d24162012-12-05 14:47:47 -0800678 {}
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700679};
680
681static struct platform_driver msm_iommu_ctx_driver = {
682 .driver = {
683 .name = "msm_iommu_ctx",
Olav Haugan336bf412013-05-10 16:22:21 -0700684 .of_match_table = msm_iommu_v0_ctx_match_table,
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700685 },
686 .probe = msm_iommu_ctx_probe,
Olav Haugan95d24162012-12-05 14:47:47 -0800687 .remove = __devexit_p(msm_iommu_ctx_remove),
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700688};
689
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800690static int __init msm_iommu_driver_init(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700691{
692 int ret;
Olav Haugan0858ae02013-06-04 16:51:50 -0700693
694 if (msm_soc_version_supports_iommu_v0()) {
695 msm_set_iommu_access_ops(&iommu_access_ops_v0);
696 msm_iommu_access_ops = msm_get_iommu_access_ops();
697 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700698 ret = platform_driver_register(&msm_iommu_driver);
699 if (ret != 0) {
700 pr_err("Failed to register IOMMU driver\n");
701 goto error;
702 }
703
704 ret = platform_driver_register(&msm_iommu_ctx_driver);
705 if (ret != 0) {
706 pr_err("Failed to register IOMMU context driver\n");
707 goto error;
708 }
709
710error:
711 return ret;
712}
713
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800714static void __exit msm_iommu_driver_exit(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700715{
716 platform_driver_unregister(&msm_iommu_ctx_driver);
717 platform_driver_unregister(&msm_iommu_driver);
718}
719
720subsys_initcall(msm_iommu_driver_init);
721module_exit(msm_iommu_driver_exit);
722
723MODULE_LICENSE("GPL v2");
724MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");