blob: 470d8ce1f6a5379937b5c490384841fa43626774 [file] [log] [blame]
Stephen Boyd55742b72012-08-08 11:40:26 -07001/* Copyright (c) 2010-2012, 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
30#include <mach/iommu_hw-8xxx.h>
31#include <mach/iommu.h>
32
Stephen Boyd55742b72012-08-08 11:40:26 -070033static DEFINE_MUTEX(iommu_list_lock);
34static LIST_HEAD(iommu_list);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070035
Stephen Boyd55742b72012-08-08 11:40:26 -070036void msm_iommu_add_drv(struct msm_iommu_drvdata *drv)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070037{
Stephen Boyd55742b72012-08-08 11:40:26 -070038 mutex_lock(&iommu_list_lock);
39 list_add(&drv->list, &iommu_list);
40 mutex_unlock(&iommu_list_lock);
41}
42
43void msm_iommu_remove_drv(struct msm_iommu_drvdata *drv)
44{
45 mutex_lock(&iommu_list_lock);
46 list_del(&drv->list);
47 mutex_unlock(&iommu_list_lock);
48}
49
50static int find_iommu_ctx(struct device *dev, void *data)
51{
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080052 struct msm_iommu_ctx_drvdata *c;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070053
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080054 c = dev_get_drvdata(dev);
Stephen Boyd55742b72012-08-08 11:40:26 -070055 if (!c || !c->name)
56 return 0;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070057
Stephen Boyd55742b72012-08-08 11:40:26 -070058 return !strcmp(data, c->name);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070059}
60
Stephen Boyd55742b72012-08-08 11:40:26 -070061static struct device *find_context(struct device *dev, const char *name)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070062{
Stephen Boyd55742b72012-08-08 11:40:26 -070063 return device_find_child(dev, (void *)name, find_iommu_ctx);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070064}
65
66struct device *msm_iommu_get_ctx(const char *ctx_name)
67{
Stephen Boyd55742b72012-08-08 11:40:26 -070068 struct msm_iommu_drvdata *drv;
69 struct device *dev = NULL;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070070
Stephen Boyd55742b72012-08-08 11:40:26 -070071 mutex_lock(&iommu_list_lock);
72 list_for_each_entry(drv, &iommu_list, list) {
73 dev = find_context(drv->dev, ctx_name);
74 if (dev)
75 break;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070076 }
Stephen Boyd55742b72012-08-08 11:40:26 -070077 mutex_unlock(&iommu_list_lock);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070078
Stephen Boyd55742b72012-08-08 11:40:26 -070079 if (!dev || !dev_get_drvdata(dev))
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070080 pr_err("Could not find context <%s>\n", ctx_name);
Stephen Boyd55742b72012-08-08 11:40:26 -070081 put_device(dev);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070082
Stephen Boyd55742b72012-08-08 11:40:26 -070083 return dev;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070084}
85EXPORT_SYMBOL(msm_iommu_get_ctx);
86
Olav Haugan95d24162012-12-05 14:47:47 -080087static void msm_iommu_reset(void __iomem *base, void __iomem *glb_base, int ncb)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070088{
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080089 int ctx;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070090
Olav Haugan95d24162012-12-05 14:47:47 -080091 SET_RPUE(glb_base, 0);
92 SET_RPUEIE(glb_base, 0);
93 SET_ESRRESTORE(glb_base, 0);
94 SET_TBE(glb_base, 0);
95 SET_CR(glb_base, 0);
96 SET_SPDMBE(glb_base, 0);
97 SET_TESTBUSCR(glb_base, 0);
98 SET_TLBRSW(glb_base, 0);
99 SET_GLOBAL_TLBIALL(glb_base, 0);
100 SET_RPU_ACR(glb_base, 0);
101 SET_TLBLKCRWE(glb_base, 1);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700102
103 for (ctx = 0; ctx < ncb; ctx++) {
Olav Haugan95d24162012-12-05 14:47:47 -0800104 SET_BPRCOSH(glb_base, ctx, 0);
105 SET_BPRCISH(glb_base, ctx, 0);
106 SET_BPRCNSH(glb_base, ctx, 0);
107 SET_BPSHCFG(glb_base, ctx, 0);
108 SET_BPMTCFG(glb_base, ctx, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700109 SET_ACTLR(base, ctx, 0);
110 SET_SCTLR(base, ctx, 0);
111 SET_FSRRESTORE(base, ctx, 0);
112 SET_TTBR0(base, ctx, 0);
113 SET_TTBR1(base, ctx, 0);
114 SET_TTBCR(base, ctx, 0);
115 SET_BFBCR(base, ctx, 0);
116 SET_PAR(base, ctx, 0);
117 SET_FAR(base, ctx, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700118 SET_TLBFLPTER(base, ctx, 0);
119 SET_TLBSLPTER(base, ctx, 0);
120 SET_TLBLKCR(base, ctx, 0);
Stepan Moskovchenko0272fd62011-09-26 15:06:40 -0700121 SET_CTX_TLBIALL(base, ctx, 0);
122 SET_TLBIVA(base, ctx, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700123 SET_PRRR(base, ctx, 0);
124 SET_NMRR(base, ctx, 0);
125 SET_CONTEXTIDR(base, ctx, 0);
126 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127 mb();
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700128}
129
Olav Haugan95d24162012-12-05 14:47:47 -0800130static int msm_iommu_parse_dt(struct platform_device *pdev,
131 struct msm_iommu_drvdata *drvdata)
132{
133#ifdef CONFIG_OF_DEVICE
134 struct device_node *child;
135 struct resource *r;
136 u32 glb_offset = 0;
137
138 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
139 if (!r) {
140 pr_err("%s: Missing property reg\n", __func__);
141 return -EINVAL;
142 }
143 drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
144 if (!drvdata->base) {
145 pr_err("%s: Unable to ioremap address %x size %x\n", __func__,
146 r->start, resource_size(r));
147 return -ENOMEM;
148 }
149 drvdata->glb_base = drvdata->base;
150
151 if (!of_property_read_u32(pdev->dev.of_node, "qcom,glb-offset",
152 &glb_offset)) {
153 drvdata->glb_base += glb_offset;
154 } else {
155 pr_err("%s: Missing property qcom,glb-offset\n", __func__);
156 return -EINVAL;
157 }
158
159 for_each_child_of_node(pdev->dev.of_node, child) {
160 drvdata->ncb++;
161 if (!of_platform_device_create(child, NULL, &pdev->dev))
162 pr_err("Failed to create %s device\n", child->name);
163 }
164
165 drvdata->name = dev_name(&pdev->dev);
166 drvdata->sec_id = -1;
167 drvdata->ttbr_split = 0;
168#endif
169 return 0;
170}
171
172static int __get_clocks(struct platform_device *pdev,
173 struct msm_iommu_drvdata *drvdata)
174{
175 int ret = 0;
176
177 drvdata->pclk = clk_get(&pdev->dev, "iface_clk");
178 if (IS_ERR(drvdata->pclk)) {
179 ret = PTR_ERR(drvdata->pclk);
180 drvdata->pclk = NULL;
181 pr_err("Unable to get %s clock for %s IOMMU device\n",
182 dev_name(&pdev->dev), drvdata->name);
183 goto fail;
184 }
185
186 drvdata->clk = clk_get(&pdev->dev, "core_clk");
187
188 if (!IS_ERR(drvdata->clk)) {
189 if (clk_get_rate(drvdata->clk) == 0) {
190 ret = clk_round_rate(drvdata->clk, 1000);
191 clk_set_rate(drvdata->clk, ret);
192 }
193 } else {
194 drvdata->clk = NULL;
195 }
196 return 0;
197fail:
198 return ret;
199}
200
201static void __put_clocks(struct msm_iommu_drvdata *drvdata)
202{
203 if (drvdata->clk)
204 clk_put(drvdata->clk);
205 clk_put(drvdata->pclk);
206}
207
208static int __enable_clocks(struct msm_iommu_drvdata *drvdata)
209{
210 int ret;
211
212 ret = clk_prepare_enable(drvdata->pclk);
213 if (ret)
214 goto fail;
215
216 if (drvdata->clk) {
217 ret = clk_prepare_enable(drvdata->clk);
218 if (ret)
219 clk_disable_unprepare(drvdata->pclk);
220 }
221fail:
222 return ret;
223}
224
225static void __disable_clocks(struct msm_iommu_drvdata *drvdata)
226{
227 if (drvdata->clk)
228 clk_disable_unprepare(drvdata->clk);
229 clk_disable_unprepare(drvdata->pclk);
230}
231
232/*
233 * Do a basic check of the IOMMU by performing an ATS operation
234 * on context bank 0.
235 */
236static int iommu_sanity_check(struct msm_iommu_drvdata *drvdata)
237{
238 int par;
239 int ret = 0;
240
241 SET_M(drvdata->base, 0, 1);
242 SET_PAR(drvdata->base, 0, 0);
243 SET_V2PCFG(drvdata->base, 0, 1);
244 SET_V2PPR(drvdata->base, 0, 0);
245 mb();
246 par = GET_PAR(drvdata->base, 0);
247 SET_V2PCFG(drvdata->base, 0, 0);
248 SET_M(drvdata->base, 0, 0);
249 mb();
250
251 if (!par) {
252 pr_err("%s: Invalid PAR value detected\n", drvdata->name);
253 ret = -ENODEV;
254 }
255 return ret;
256}
257
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700258static int msm_iommu_probe(struct platform_device *pdev)
259{
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700260 struct msm_iommu_drvdata *drvdata;
261 struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data;
Olav Haugan95d24162012-12-05 14:47:47 -0800262 int ret;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700263
Olav Haugan95d24162012-12-05 14:47:47 -0800264 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700265
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800266 if (!drvdata) {
267 ret = -ENOMEM;
268 goto fail;
269 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700270
Olav Haugan95d24162012-12-05 14:47:47 -0800271 if (pdev->dev.of_node) {
272 ret = msm_iommu_parse_dt(pdev, drvdata);
273 if (ret)
274 goto fail;
275 } else if (pdev->dev.platform_data) {
276 struct resource *r, *r2;
277 resource_size_t len;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700278
Olav Haugan95d24162012-12-05 14:47:47 -0800279 r = platform_get_resource_byname(pdev, IORESOURCE_MEM,
280 "physbase");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700281
Olav Haugan95d24162012-12-05 14:47:47 -0800282 if (!r) {
283 ret = -ENODEV;
284 goto fail;
Matt Wagantall99858222011-11-08 14:31:27 -0800285 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700286
Olav Haugan95d24162012-12-05 14:47:47 -0800287 len = resource_size(r);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700288
Olav Haugan95d24162012-12-05 14:47:47 -0800289 r2 = request_mem_region(r->start, len, r->name);
290 if (!r2) {
291 pr_err("Could not request memory region: start=%p, len=%d\n",
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800292 (void *) r->start, len);
Olav Haugan95d24162012-12-05 14:47:47 -0800293 ret = -EBUSY;
294 goto fail;
295 }
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800296
Olav Haugan95d24162012-12-05 14:47:47 -0800297 drvdata->base = devm_ioremap(&pdev->dev, r2->start, len);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800298
Olav Haugan95d24162012-12-05 14:47:47 -0800299 if (!drvdata->base) {
300 pr_err("Could not ioremap: start=%p, len=%d\n",
301 (void *) r2->start, len);
302 ret = -EBUSY;
303 goto fail;
304 }
305 /*
306 * Global register space offset for legacy IOMMUv1 hardware
307 * is always 0xFF000
308 */
309 drvdata->glb_base = drvdata->base + 0xFF000;
310 drvdata->name = iommu_dev->name;
311 drvdata->dev = &pdev->dev;
312 drvdata->ncb = iommu_dev->ncb;
313 drvdata->ttbr_split = iommu_dev->ttbr_split;
314 } else {
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800315 ret = -ENODEV;
Olav Haugan95d24162012-12-05 14:47:47 -0800316 goto fail;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800317 }
318
Stephen Boyd55742b72012-08-08 11:40:26 -0700319 drvdata->dev = &pdev->dev;
320
Olav Haugan95d24162012-12-05 14:47:47 -0800321 ret = __get_clocks(pdev, drvdata);
322
323 if (ret)
324 goto fail;
325
326 __enable_clocks(drvdata);
327
328 msm_iommu_reset(drvdata->base, drvdata->glb_base, drvdata->ncb);
329
330 ret = iommu_sanity_check(drvdata);
331 if (ret)
332 goto fail_clk;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800333
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700334 pr_info("device %s mapped at %p, with %d ctx banks\n",
Olav Haugan95d24162012-12-05 14:47:47 -0800335 drvdata->name, drvdata->base, drvdata->ncb);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700336
Olav Haugan95d24162012-12-05 14:47:47 -0800337 msm_iommu_add_drv(drvdata);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800338 platform_set_drvdata(pdev, drvdata);
339
Olav Haugan95d24162012-12-05 14:47:47 -0800340 __disable_clocks(drvdata);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700341
342 return 0;
Olav Haugan95d24162012-12-05 14:47:47 -0800343
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800344fail_clk:
Olav Haugan95d24162012-12-05 14:47:47 -0800345 __disable_clocks(drvdata);
346 __put_clocks(drvdata);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700347fail:
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700348 return ret;
349}
350
351static int msm_iommu_remove(struct platform_device *pdev)
352{
353 struct msm_iommu_drvdata *drv = NULL;
354
355 drv = platform_get_drvdata(pdev);
356 if (drv) {
Stephen Boyd55742b72012-08-08 11:40:26 -0700357 msm_iommu_remove_drv(drv);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800358 if (drv->clk)
359 clk_put(drv->clk);
360 clk_put(drv->pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700361 platform_set_drvdata(pdev, NULL);
362 }
363 return 0;
364}
365
Olav Haugan95d24162012-12-05 14:47:47 -0800366static int msm_iommu_ctx_parse_dt(struct platform_device *pdev,
367 struct msm_iommu_ctx_drvdata *ctx_drvdata)
368{
369 struct resource *r, rp;
370 int irq, ret;
371 u32 nmid_array_size;
372 u32 nmid;
373
374 irq = platform_get_irq(pdev, 0);
375 if (irq > 0) {
376 ret = request_threaded_irq(irq, NULL,
377 msm_iommu_fault_handler,
378 IRQF_ONESHOT | IRQF_SHARED,
379 "msm_iommu_nonsecure_irq", pdev);
380 if (ret) {
381 pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
382 return ret;
383 }
384 }
385
386 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
387 if (!r) {
388 pr_err("Could not find reg property for context bank\n");
389 return -EINVAL;
390 }
391
392 ret = of_address_to_resource(pdev->dev.parent->of_node, 0, &rp);
393 if (ret) {
394 pr_err("of_address_to_resource failed\n");
395 return -EINVAL;
396 }
397
398 /* Calculate the context bank number using the base addresses. CB0
399 * starts at the base address.
400 */
401 ctx_drvdata->num = ((r->start - rp.start) >> CTX_SHIFT);
402
403 if (of_property_read_string(pdev->dev.of_node, "label",
404 &ctx_drvdata->name)) {
405 pr_err("Could not find label property\n");
406 return -EINVAL;
407 }
408
409 if (!of_get_property(pdev->dev.of_node, "qcom,iommu-ctx-mids",
410 &nmid_array_size)) {
411 pr_err("Could not find iommu-ctx-mids property\n");
412 return -EINVAL;
413 }
414 if (nmid_array_size >= sizeof(ctx_drvdata->sids)) {
415 pr_err("Too many mids defined - array size: %u, mids size: %u\n",
416 nmid_array_size, sizeof(ctx_drvdata->sids));
417 return -EINVAL;
418 }
419 nmid = nmid_array_size / sizeof(*ctx_drvdata->sids);
420
421 if (of_property_read_u32_array(pdev->dev.of_node, "qcom,iommu-ctx-mids",
422 ctx_drvdata->sids, nmid)) {
423 pr_err("Could not find iommu-ctx-mids property\n");
424 return -EINVAL;
425 }
426 ctx_drvdata->nsid = nmid;
427
428 return 0;
429}
430
431static void __program_m2v_tables(struct msm_iommu_drvdata *drvdata,
432 struct msm_iommu_ctx_drvdata *ctx_drvdata)
433{
434 int i;
435
436 /* Program the M2V tables for this context */
437 for (i = 0; i < ctx_drvdata->nsid; i++) {
438 int sid = ctx_drvdata->sids[i];
439 int num = ctx_drvdata->num;
440
441 SET_M2VCBR_N(drvdata->glb_base, sid, 0);
442 SET_CBACR_N(drvdata->glb_base, num, 0);
443
444 /* Route page faults to the non-secure interrupt */
445 SET_IRPTNDX(drvdata->glb_base, num, 1);
446
447 /* Set VMID = 0 */
448 SET_VMID(drvdata->glb_base, sid, 0);
449
450 /* Set the context number for that SID to this context */
451 SET_CBNDX(drvdata->glb_base, sid, num);
452
453 /* Set SID associated with this context bank to 0 */
454 SET_CBVMID(drvdata->glb_base, num, 0);
455
456 /* Set the ASID for TLB tagging for this context to 0 */
457 SET_CONTEXTIDR_ASID(drvdata->base, num, 0);
458
459 /* Set security bit override to be Non-secure */
460 SET_NSCFG(drvdata->glb_base, sid, 3);
461 }
462 mb();
463}
464
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700465static int msm_iommu_ctx_probe(struct platform_device *pdev)
466{
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700467 struct msm_iommu_drvdata *drvdata;
468 struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700469 int i, ret, irq;
Olav Haugan95d24162012-12-05 14:47:47 -0800470 if (!pdev->dev.parent) {
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700471 ret = -EINVAL;
472 goto fail;
473 }
474
475 drvdata = dev_get_drvdata(pdev->dev.parent);
476
477 if (!drvdata) {
478 ret = -ENODEV;
479 goto fail;
480 }
481
Olav Haugan95d24162012-12-05 14:47:47 -0800482 ctx_drvdata = devm_kzalloc(&pdev->dev, sizeof(*ctx_drvdata),
483 GFP_KERNEL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700484 if (!ctx_drvdata) {
485 ret = -ENOMEM;
486 goto fail;
487 }
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700488
Olav Haugan95d24162012-12-05 14:47:47 -0800489 ctx_drvdata->pdev = pdev;
490 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
491 platform_set_drvdata(pdev, ctx_drvdata);
Olav Haugane99ee7e2012-12-11 15:02:02 -0800492 ctx_drvdata->attach_count = 0;
Olav Haugan95d24162012-12-05 14:47:47 -0800493
494 if (pdev->dev.of_node) {
495 ret = msm_iommu_ctx_parse_dt(pdev, ctx_drvdata);
496 if (ret)
497 goto fail;
498 } else if (pdev->dev.platform_data) {
499 struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
500
501 ctx_drvdata->num = c->num;
502 ctx_drvdata->name = c->name;
503
504 for (i = 0; i < MAX_NUM_MIDS; ++i) {
505 if (c->mids[i] == -1) {
506 ctx_drvdata->nsid = i;
507 break;
508 }
509 ctx_drvdata->sids[i] = c->mids[i];
510 }
511 irq = platform_get_irq_byname(
512 to_platform_device(pdev->dev.parent),
513 "nonsecure_irq");
514 if (irq < 0) {
515 ret = -ENODEV;
516 goto fail;
517 }
518
519 ret = request_threaded_irq(irq, NULL, msm_iommu_fault_handler,
520 IRQF_ONESHOT | IRQF_SHARED,
521 "msm_iommu_nonsecure_irq", ctx_drvdata);
522
523 if (ret) {
524 pr_err("request_threaded_irq %d failed: %d\n", irq,
525 ret);
526 goto fail;
527 }
528 } else {
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700529 ret = -ENODEV;
530 goto fail;
531 }
532
Olav Haugan95d24162012-12-05 14:47:47 -0800533 __enable_clocks(drvdata);
534 __program_m2v_tables(drvdata, ctx_drvdata);
535 __disable_clocks(drvdata);
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700536
Olav Haugan95d24162012-12-05 14:47:47 -0800537 dev_info(&pdev->dev, "context %s using bank %d\n", ctx_drvdata->name,
538 ctx_drvdata->num);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700539 return 0;
540fail:
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700541 return ret;
542}
543
Olav Haugan95d24162012-12-05 14:47:47 -0800544static int __devexit msm_iommu_ctx_remove(struct platform_device *pdev)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700545{
Olav Haugan95d24162012-12-05 14:47:47 -0800546 platform_set_drvdata(pdev, NULL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700547 return 0;
548}
549
Olav Haugan95d24162012-12-05 14:47:47 -0800550
551static struct of_device_id msm_iommu_match_table[] = {
552 { .compatible = "qcom,msm-smmu-v1", },
553 {}
554};
555
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700556static struct platform_driver msm_iommu_driver = {
557 .driver = {
558 .name = "msm_iommu",
Olav Haugan95d24162012-12-05 14:47:47 -0800559 .of_match_table = msm_iommu_match_table,
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700560 },
561 .probe = msm_iommu_probe,
Olav Haugan95d24162012-12-05 14:47:47 -0800562 .remove = __devexit_p(msm_iommu_remove),
563};
564
565static struct of_device_id msm_iommu_ctx_match_table[] = {
566 { .name = "qcom,iommu-ctx", },
567 {}
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700568};
569
570static struct platform_driver msm_iommu_ctx_driver = {
571 .driver = {
572 .name = "msm_iommu_ctx",
Olav Haugan95d24162012-12-05 14:47:47 -0800573 .of_match_table = msm_iommu_ctx_match_table,
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700574 },
575 .probe = msm_iommu_ctx_probe,
Olav Haugan95d24162012-12-05 14:47:47 -0800576 .remove = __devexit_p(msm_iommu_ctx_remove),
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700577};
578
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800579static int __init msm_iommu_driver_init(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700580{
581 int ret;
582 ret = platform_driver_register(&msm_iommu_driver);
583 if (ret != 0) {
584 pr_err("Failed to register IOMMU driver\n");
585 goto error;
586 }
587
588 ret = platform_driver_register(&msm_iommu_ctx_driver);
589 if (ret != 0) {
590 pr_err("Failed to register IOMMU context driver\n");
591 goto error;
592 }
593
594error:
595 return ret;
596}
597
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800598static void __exit msm_iommu_driver_exit(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700599{
600 platform_driver_unregister(&msm_iommu_ctx_driver);
601 platform_driver_unregister(&msm_iommu_driver);
602}
603
604subsys_initcall(msm_iommu_driver_init);
605module_exit(msm_iommu_driver_exit);
606
607MODULE_LICENSE("GPL v2");
608MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");