Linux 5.2
[linux-2.6/linux-2.6-arm.git] / drivers / iommu / omap-iommu.c
blob62f9c61338a585d91f8af3cd1b3310c05784faeb
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * omap iommu: tlb and pagetable primitives
5 * Copyright (C) 2008-2010 Nokia Corporation
6 * Copyright (C) 2013-2017 Texas Instruments Incorporated - http://www.ti.com/
8 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
9 * Paul Mundt and Toshihiro Kobayashi
12 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/platform_device.h>
18 #include <linux/iommu.h>
19 #include <linux/omap-iommu.h>
20 #include <linux/mutex.h>
21 #include <linux/spinlock.h>
22 #include <linux/io.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/of.h>
25 #include <linux/of_iommu.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_platform.h>
28 #include <linux/regmap.h>
29 #include <linux/mfd/syscon.h>
31 #include <linux/platform_data/iommu-omap.h>
33 #include "omap-iopgtable.h"
34 #include "omap-iommu.h"
36 static const struct iommu_ops omap_iommu_ops;
38 #define to_iommu(dev) \
39 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
41 /* bitmap of the page sizes currently supported */
42 #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
44 #define MMU_LOCK_BASE_SHIFT 10
45 #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
46 #define MMU_LOCK_BASE(x) \
47 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
49 #define MMU_LOCK_VICT_SHIFT 4
50 #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
51 #define MMU_LOCK_VICT(x) \
52 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
54 static struct platform_driver omap_iommu_driver;
55 static struct kmem_cache *iopte_cachep;
57 /**
58 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
59 * @dom: generic iommu domain handle
60 **/
61 static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
63 return container_of(dom, struct omap_iommu_domain, domain);
66 /**
67 * omap_iommu_save_ctx - Save registers for pm off-mode support
68 * @dev: client device
69 **/
70 void omap_iommu_save_ctx(struct device *dev)
72 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
73 struct omap_iommu *obj;
74 u32 *p;
75 int i;
77 if (!arch_data)
78 return;
80 while (arch_data->iommu_dev) {
81 obj = arch_data->iommu_dev;
82 p = obj->ctx;
83 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
84 p[i] = iommu_read_reg(obj, i * sizeof(u32));
85 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
86 p[i]);
88 arch_data++;
91 EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
93 /**
94 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
95 * @dev: client device
96 **/
97 void omap_iommu_restore_ctx(struct device *dev)
99 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
100 struct omap_iommu *obj;
101 u32 *p;
102 int i;
104 if (!arch_data)
105 return;
107 while (arch_data->iommu_dev) {
108 obj = arch_data->iommu_dev;
109 p = obj->ctx;
110 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
111 iommu_write_reg(obj, p[i], i * sizeof(u32));
112 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
113 p[i]);
115 arch_data++;
118 EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
120 static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
122 u32 val, mask;
124 if (!obj->syscfg)
125 return;
127 mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
128 val = enable ? mask : 0;
129 regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
132 static void __iommu_set_twl(struct omap_iommu *obj, bool on)
134 u32 l = iommu_read_reg(obj, MMU_CNTL);
136 if (on)
137 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
138 else
139 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
141 l &= ~MMU_CNTL_MASK;
142 if (on)
143 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
144 else
145 l |= (MMU_CNTL_MMU_EN);
147 iommu_write_reg(obj, l, MMU_CNTL);
150 static int omap2_iommu_enable(struct omap_iommu *obj)
152 u32 l, pa;
154 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
155 return -EINVAL;
157 pa = virt_to_phys(obj->iopgd);
158 if (!IS_ALIGNED(pa, SZ_16K))
159 return -EINVAL;
161 l = iommu_read_reg(obj, MMU_REVISION);
162 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
163 (l >> 4) & 0xf, l & 0xf);
165 iommu_write_reg(obj, pa, MMU_TTB);
167 dra7_cfg_dspsys_mmu(obj, true);
169 if (obj->has_bus_err_back)
170 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
172 __iommu_set_twl(obj, true);
174 return 0;
177 static void omap2_iommu_disable(struct omap_iommu *obj)
179 u32 l = iommu_read_reg(obj, MMU_CNTL);
181 l &= ~MMU_CNTL_MASK;
182 iommu_write_reg(obj, l, MMU_CNTL);
183 dra7_cfg_dspsys_mmu(obj, false);
185 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
188 static int iommu_enable(struct omap_iommu *obj)
190 int err;
191 struct platform_device *pdev = to_platform_device(obj->dev);
192 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
194 if (pdata && pdata->deassert_reset) {
195 err = pdata->deassert_reset(pdev, pdata->reset_name);
196 if (err) {
197 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
198 return err;
202 pm_runtime_get_sync(obj->dev);
204 err = omap2_iommu_enable(obj);
206 return err;
209 static void iommu_disable(struct omap_iommu *obj)
211 struct platform_device *pdev = to_platform_device(obj->dev);
212 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
214 omap2_iommu_disable(obj);
216 pm_runtime_put_sync(obj->dev);
218 if (pdata && pdata->assert_reset)
219 pdata->assert_reset(pdev, pdata->reset_name);
223 * TLB operations
225 static u32 iotlb_cr_to_virt(struct cr_regs *cr)
227 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
228 u32 mask = get_cam_va_mask(cr->cam & page_size);
230 return cr->cam & mask;
233 static u32 get_iopte_attr(struct iotlb_entry *e)
235 u32 attr;
237 attr = e->mixed << 5;
238 attr |= e->endian;
239 attr |= e->elsz >> 3;
240 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
241 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
242 return attr;
245 static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
247 u32 status, fault_addr;
249 status = iommu_read_reg(obj, MMU_IRQSTATUS);
250 status &= MMU_IRQ_MASK;
251 if (!status) {
252 *da = 0;
253 return 0;
256 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
257 *da = fault_addr;
259 iommu_write_reg(obj, status, MMU_IRQSTATUS);
261 return status;
264 void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
266 u32 val;
268 val = iommu_read_reg(obj, MMU_LOCK);
270 l->base = MMU_LOCK_BASE(val);
271 l->vict = MMU_LOCK_VICT(val);
274 void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
276 u32 val;
278 val = (l->base << MMU_LOCK_BASE_SHIFT);
279 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
281 iommu_write_reg(obj, val, MMU_LOCK);
284 static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
286 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
287 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
290 static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
292 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
293 iommu_write_reg(obj, cr->ram, MMU_RAM);
295 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
296 iommu_write_reg(obj, 1, MMU_LD_TLB);
299 /* only used in iotlb iteration for-loop */
300 struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
302 struct cr_regs cr;
303 struct iotlb_lock l;
305 iotlb_lock_get(obj, &l);
306 l.vict = n;
307 iotlb_lock_set(obj, &l);
308 iotlb_read_cr(obj, &cr);
310 return cr;
313 #ifdef PREFETCH_IOTLB
314 static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
315 struct iotlb_entry *e)
317 struct cr_regs *cr;
319 if (!e)
320 return NULL;
322 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
323 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
324 e->da);
325 return ERR_PTR(-EINVAL);
328 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
329 if (!cr)
330 return ERR_PTR(-ENOMEM);
332 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
333 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
335 return cr;
339 * load_iotlb_entry - Set an iommu tlb entry
340 * @obj: target iommu
341 * @e: an iommu tlb entry info
343 static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
345 int err = 0;
346 struct iotlb_lock l;
347 struct cr_regs *cr;
349 if (!obj || !obj->nr_tlb_entries || !e)
350 return -EINVAL;
352 pm_runtime_get_sync(obj->dev);
354 iotlb_lock_get(obj, &l);
355 if (l.base == obj->nr_tlb_entries) {
356 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
357 err = -EBUSY;
358 goto out;
360 if (!e->prsvd) {
361 int i;
362 struct cr_regs tmp;
364 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
365 if (!iotlb_cr_valid(&tmp))
366 break;
368 if (i == obj->nr_tlb_entries) {
369 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
370 err = -EBUSY;
371 goto out;
374 iotlb_lock_get(obj, &l);
375 } else {
376 l.vict = l.base;
377 iotlb_lock_set(obj, &l);
380 cr = iotlb_alloc_cr(obj, e);
381 if (IS_ERR(cr)) {
382 pm_runtime_put_sync(obj->dev);
383 return PTR_ERR(cr);
386 iotlb_load_cr(obj, cr);
387 kfree(cr);
389 if (e->prsvd)
390 l.base++;
391 /* increment victim for next tlb load */
392 if (++l.vict == obj->nr_tlb_entries)
393 l.vict = l.base;
394 iotlb_lock_set(obj, &l);
395 out:
396 pm_runtime_put_sync(obj->dev);
397 return err;
400 #else /* !PREFETCH_IOTLB */
402 static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
404 return 0;
407 #endif /* !PREFETCH_IOTLB */
409 static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
411 return load_iotlb_entry(obj, e);
415 * flush_iotlb_page - Clear an iommu tlb entry
416 * @obj: target iommu
417 * @da: iommu device virtual address
419 * Clear an iommu tlb entry which includes 'da' address.
421 static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
423 int i;
424 struct cr_regs cr;
426 pm_runtime_get_sync(obj->dev);
428 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
429 u32 start;
430 size_t bytes;
432 if (!iotlb_cr_valid(&cr))
433 continue;
435 start = iotlb_cr_to_virt(&cr);
436 bytes = iopgsz_to_bytes(cr.cam & 3);
438 if ((start <= da) && (da < start + bytes)) {
439 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
440 __func__, start, da, bytes);
441 iotlb_load_cr(obj, &cr);
442 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
443 break;
446 pm_runtime_put_sync(obj->dev);
448 if (i == obj->nr_tlb_entries)
449 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
453 * flush_iotlb_all - Clear all iommu tlb entries
454 * @obj: target iommu
456 static void flush_iotlb_all(struct omap_iommu *obj)
458 struct iotlb_lock l;
460 pm_runtime_get_sync(obj->dev);
462 l.base = 0;
463 l.vict = 0;
464 iotlb_lock_set(obj, &l);
466 iommu_write_reg(obj, 1, MMU_GFLUSH);
468 pm_runtime_put_sync(obj->dev);
472 * H/W pagetable operations
474 static void flush_iopte_range(struct device *dev, dma_addr_t dma,
475 unsigned long offset, int num_entries)
477 size_t size = num_entries * sizeof(u32);
479 dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE);
482 static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid)
484 dma_addr_t pt_dma;
486 /* Note: freed iopte's must be clean ready for re-use */
487 if (iopte) {
488 if (dma_valid) {
489 pt_dma = virt_to_phys(iopte);
490 dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE,
491 DMA_TO_DEVICE);
494 kmem_cache_free(iopte_cachep, iopte);
498 static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd,
499 dma_addr_t *pt_dma, u32 da)
501 u32 *iopte;
502 unsigned long offset = iopgd_index(da) * sizeof(da);
504 /* a table has already existed */
505 if (*iopgd)
506 goto pte_ready;
509 * do the allocation outside the page table lock
511 spin_unlock(&obj->page_table_lock);
512 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
513 spin_lock(&obj->page_table_lock);
515 if (!*iopgd) {
516 if (!iopte)
517 return ERR_PTR(-ENOMEM);
519 *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE,
520 DMA_TO_DEVICE);
521 if (dma_mapping_error(obj->dev, *pt_dma)) {
522 dev_err(obj->dev, "DMA map error for L2 table\n");
523 iopte_free(obj, iopte, false);
524 return ERR_PTR(-ENOMEM);
528 * we rely on dma address and the physical address to be
529 * the same for mapping the L2 table
531 if (WARN_ON(*pt_dma != virt_to_phys(iopte))) {
532 dev_err(obj->dev, "DMA translation error for L2 table\n");
533 dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE,
534 DMA_TO_DEVICE);
535 iopte_free(obj, iopte, false);
536 return ERR_PTR(-ENOMEM);
539 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
541 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
542 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
543 } else {
544 /* We raced, free the reduniovant table */
545 iopte_free(obj, iopte, false);
548 pte_ready:
549 iopte = iopte_offset(iopgd, da);
550 *pt_dma = iopgd_page_paddr(iopgd);
551 dev_vdbg(obj->dev,
552 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
553 __func__, da, iopgd, *iopgd, iopte, *iopte);
555 return iopte;
558 static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
560 u32 *iopgd = iopgd_offset(obj, da);
561 unsigned long offset = iopgd_index(da) * sizeof(da);
563 if ((da | pa) & ~IOSECTION_MASK) {
564 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
565 __func__, da, pa, IOSECTION_SIZE);
566 return -EINVAL;
569 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
570 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
571 return 0;
574 static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
576 u32 *iopgd = iopgd_offset(obj, da);
577 unsigned long offset = iopgd_index(da) * sizeof(da);
578 int i;
580 if ((da | pa) & ~IOSUPER_MASK) {
581 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
582 __func__, da, pa, IOSUPER_SIZE);
583 return -EINVAL;
586 for (i = 0; i < 16; i++)
587 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
588 flush_iopte_range(obj->dev, obj->pd_dma, offset, 16);
589 return 0;
592 static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
594 u32 *iopgd = iopgd_offset(obj, da);
595 dma_addr_t pt_dma;
596 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
597 unsigned long offset = iopte_index(da) * sizeof(da);
599 if (IS_ERR(iopte))
600 return PTR_ERR(iopte);
602 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
603 flush_iopte_range(obj->dev, pt_dma, offset, 1);
605 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
606 __func__, da, pa, iopte, *iopte);
608 return 0;
611 static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
613 u32 *iopgd = iopgd_offset(obj, da);
614 dma_addr_t pt_dma;
615 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
616 unsigned long offset = iopte_index(da) * sizeof(da);
617 int i;
619 if ((da | pa) & ~IOLARGE_MASK) {
620 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
621 __func__, da, pa, IOLARGE_SIZE);
622 return -EINVAL;
625 if (IS_ERR(iopte))
626 return PTR_ERR(iopte);
628 for (i = 0; i < 16; i++)
629 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
630 flush_iopte_range(obj->dev, pt_dma, offset, 16);
631 return 0;
634 static int
635 iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
637 int (*fn)(struct omap_iommu *, u32, u32, u32);
638 u32 prot;
639 int err;
641 if (!obj || !e)
642 return -EINVAL;
644 switch (e->pgsz) {
645 case MMU_CAM_PGSZ_16M:
646 fn = iopgd_alloc_super;
647 break;
648 case MMU_CAM_PGSZ_1M:
649 fn = iopgd_alloc_section;
650 break;
651 case MMU_CAM_PGSZ_64K:
652 fn = iopte_alloc_large;
653 break;
654 case MMU_CAM_PGSZ_4K:
655 fn = iopte_alloc_page;
656 break;
657 default:
658 fn = NULL;
659 break;
662 if (WARN_ON(!fn))
663 return -EINVAL;
665 prot = get_iopte_attr(e);
667 spin_lock(&obj->page_table_lock);
668 err = fn(obj, e->da, e->pa, prot);
669 spin_unlock(&obj->page_table_lock);
671 return err;
675 * omap_iopgtable_store_entry - Make an iommu pte entry
676 * @obj: target iommu
677 * @e: an iommu tlb entry info
679 static int
680 omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
682 int err;
684 flush_iotlb_page(obj, e->da);
685 err = iopgtable_store_entry_core(obj, e);
686 if (!err)
687 prefetch_iotlb_entry(obj, e);
688 return err;
692 * iopgtable_lookup_entry - Lookup an iommu pte entry
693 * @obj: target iommu
694 * @da: iommu device virtual address
695 * @ppgd: iommu pgd entry pointer to be returned
696 * @ppte: iommu pte entry pointer to be returned
698 static void
699 iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
701 u32 *iopgd, *iopte = NULL;
703 iopgd = iopgd_offset(obj, da);
704 if (!*iopgd)
705 goto out;
707 if (iopgd_is_table(*iopgd))
708 iopte = iopte_offset(iopgd, da);
709 out:
710 *ppgd = iopgd;
711 *ppte = iopte;
714 static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
716 size_t bytes;
717 u32 *iopgd = iopgd_offset(obj, da);
718 int nent = 1;
719 dma_addr_t pt_dma;
720 unsigned long pd_offset = iopgd_index(da) * sizeof(da);
721 unsigned long pt_offset = iopte_index(da) * sizeof(da);
723 if (!*iopgd)
724 return 0;
726 if (iopgd_is_table(*iopgd)) {
727 int i;
728 u32 *iopte = iopte_offset(iopgd, da);
730 bytes = IOPTE_SIZE;
731 if (*iopte & IOPTE_LARGE) {
732 nent *= 16;
733 /* rewind to the 1st entry */
734 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
736 bytes *= nent;
737 memset(iopte, 0, nent * sizeof(*iopte));
738 pt_dma = iopgd_page_paddr(iopgd);
739 flush_iopte_range(obj->dev, pt_dma, pt_offset, nent);
742 * do table walk to check if this table is necessary or not
744 iopte = iopte_offset(iopgd, 0);
745 for (i = 0; i < PTRS_PER_IOPTE; i++)
746 if (iopte[i])
747 goto out;
749 iopte_free(obj, iopte, true);
750 nent = 1; /* for the next L1 entry */
751 } else {
752 bytes = IOPGD_SIZE;
753 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
754 nent *= 16;
755 /* rewind to the 1st entry */
756 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
758 bytes *= nent;
760 memset(iopgd, 0, nent * sizeof(*iopgd));
761 flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent);
762 out:
763 return bytes;
767 * iopgtable_clear_entry - Remove an iommu pte entry
768 * @obj: target iommu
769 * @da: iommu device virtual address
771 static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
773 size_t bytes;
775 spin_lock(&obj->page_table_lock);
777 bytes = iopgtable_clear_entry_core(obj, da);
778 flush_iotlb_page(obj, da);
780 spin_unlock(&obj->page_table_lock);
782 return bytes;
785 static void iopgtable_clear_entry_all(struct omap_iommu *obj)
787 unsigned long offset;
788 int i;
790 spin_lock(&obj->page_table_lock);
792 for (i = 0; i < PTRS_PER_IOPGD; i++) {
793 u32 da;
794 u32 *iopgd;
796 da = i << IOPGD_SHIFT;
797 iopgd = iopgd_offset(obj, da);
798 offset = iopgd_index(da) * sizeof(da);
800 if (!*iopgd)
801 continue;
803 if (iopgd_is_table(*iopgd))
804 iopte_free(obj, iopte_offset(iopgd, 0), true);
806 *iopgd = 0;
807 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
810 flush_iotlb_all(obj);
812 spin_unlock(&obj->page_table_lock);
816 * Device IOMMU generic operations
818 static irqreturn_t iommu_fault_handler(int irq, void *data)
820 u32 da, errs;
821 u32 *iopgd, *iopte;
822 struct omap_iommu *obj = data;
823 struct iommu_domain *domain = obj->domain;
824 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
826 if (!omap_domain->dev)
827 return IRQ_NONE;
829 errs = iommu_report_fault(obj, &da);
830 if (errs == 0)
831 return IRQ_HANDLED;
833 /* Fault callback or TLB/PTE Dynamic loading */
834 if (!report_iommu_fault(domain, obj->dev, da, 0))
835 return IRQ_HANDLED;
837 iommu_write_reg(obj, 0, MMU_IRQENABLE);
839 iopgd = iopgd_offset(obj, da);
841 if (!iopgd_is_table(*iopgd)) {
842 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
843 obj->name, errs, da, iopgd, *iopgd);
844 return IRQ_NONE;
847 iopte = iopte_offset(iopgd, da);
849 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
850 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
852 return IRQ_NONE;
856 * omap_iommu_attach() - attach iommu device to an iommu domain
857 * @obj: target omap iommu device
858 * @iopgd: page table
860 static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
862 int err;
864 spin_lock(&obj->iommu_lock);
866 obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE,
867 DMA_TO_DEVICE);
868 if (dma_mapping_error(obj->dev, obj->pd_dma)) {
869 dev_err(obj->dev, "DMA map error for L1 table\n");
870 err = -ENOMEM;
871 goto out_err;
874 obj->iopgd = iopgd;
875 err = iommu_enable(obj);
876 if (err)
877 goto out_err;
878 flush_iotlb_all(obj);
880 spin_unlock(&obj->iommu_lock);
882 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
884 return 0;
886 out_err:
887 spin_unlock(&obj->iommu_lock);
889 return err;
893 * omap_iommu_detach - release iommu device
894 * @obj: target iommu
896 static void omap_iommu_detach(struct omap_iommu *obj)
898 if (!obj || IS_ERR(obj))
899 return;
901 spin_lock(&obj->iommu_lock);
903 dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE,
904 DMA_TO_DEVICE);
905 iommu_disable(obj);
906 obj->pd_dma = 0;
907 obj->iopgd = NULL;
909 spin_unlock(&obj->iommu_lock);
911 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
914 static bool omap_iommu_can_register(struct platform_device *pdev)
916 struct device_node *np = pdev->dev.of_node;
918 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
919 return true;
922 * restrict IOMMU core registration only for processor-port MDMA MMUs
923 * on DRA7 DSPs
925 if ((!strcmp(dev_name(&pdev->dev), "40d01000.mmu")) ||
926 (!strcmp(dev_name(&pdev->dev), "41501000.mmu")))
927 return true;
929 return false;
932 static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
933 struct omap_iommu *obj)
935 struct device_node *np = pdev->dev.of_node;
936 int ret;
938 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
939 return 0;
941 if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
942 dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
943 return -EINVAL;
946 obj->syscfg =
947 syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
948 if (IS_ERR(obj->syscfg)) {
949 /* can fail with -EPROBE_DEFER */
950 ret = PTR_ERR(obj->syscfg);
951 return ret;
954 if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
955 &obj->id)) {
956 dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
957 return -EINVAL;
960 if (obj->id != 0 && obj->id != 1) {
961 dev_err(&pdev->dev, "invalid IOMMU instance id\n");
962 return -EINVAL;
965 return 0;
969 * OMAP Device MMU(IOMMU) detection
971 static int omap_iommu_probe(struct platform_device *pdev)
973 int err = -ENODEV;
974 int irq;
975 struct omap_iommu *obj;
976 struct resource *res;
977 struct device_node *of = pdev->dev.of_node;
979 if (!of) {
980 pr_err("%s: only DT-based devices are supported\n", __func__);
981 return -ENODEV;
984 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
985 if (!obj)
986 return -ENOMEM;
988 obj->name = dev_name(&pdev->dev);
989 obj->nr_tlb_entries = 32;
990 err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
991 if (err && err != -EINVAL)
992 return err;
993 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
994 return -EINVAL;
995 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
996 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
998 obj->dev = &pdev->dev;
999 obj->ctx = (void *)obj + sizeof(*obj);
1001 spin_lock_init(&obj->iommu_lock);
1002 spin_lock_init(&obj->page_table_lock);
1004 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1005 obj->regbase = devm_ioremap_resource(obj->dev, res);
1006 if (IS_ERR(obj->regbase))
1007 return PTR_ERR(obj->regbase);
1009 err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
1010 if (err)
1011 return err;
1013 irq = platform_get_irq(pdev, 0);
1014 if (irq < 0)
1015 return -ENODEV;
1017 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
1018 dev_name(obj->dev), obj);
1019 if (err < 0)
1020 return err;
1021 platform_set_drvdata(pdev, obj);
1023 if (omap_iommu_can_register(pdev)) {
1024 obj->group = iommu_group_alloc();
1025 if (IS_ERR(obj->group))
1026 return PTR_ERR(obj->group);
1028 err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL,
1029 obj->name);
1030 if (err)
1031 goto out_group;
1033 iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
1035 err = iommu_device_register(&obj->iommu);
1036 if (err)
1037 goto out_sysfs;
1040 pm_runtime_irq_safe(obj->dev);
1041 pm_runtime_enable(obj->dev);
1043 omap_iommu_debugfs_add(obj);
1045 dev_info(&pdev->dev, "%s registered\n", obj->name);
1047 return 0;
1049 out_sysfs:
1050 iommu_device_sysfs_remove(&obj->iommu);
1051 out_group:
1052 iommu_group_put(obj->group);
1053 return err;
1056 static int omap_iommu_remove(struct platform_device *pdev)
1058 struct omap_iommu *obj = platform_get_drvdata(pdev);
1060 if (obj->group) {
1061 iommu_group_put(obj->group);
1062 obj->group = NULL;
1064 iommu_device_sysfs_remove(&obj->iommu);
1065 iommu_device_unregister(&obj->iommu);
1068 omap_iommu_debugfs_remove(obj);
1070 pm_runtime_disable(obj->dev);
1072 dev_info(&pdev->dev, "%s removed\n", obj->name);
1073 return 0;
1076 static const struct of_device_id omap_iommu_of_match[] = {
1077 { .compatible = "ti,omap2-iommu" },
1078 { .compatible = "ti,omap4-iommu" },
1079 { .compatible = "ti,dra7-iommu" },
1080 { .compatible = "ti,dra7-dsp-iommu" },
1084 static struct platform_driver omap_iommu_driver = {
1085 .probe = omap_iommu_probe,
1086 .remove = omap_iommu_remove,
1087 .driver = {
1088 .name = "omap-iommu",
1089 .of_match_table = of_match_ptr(omap_iommu_of_match),
1093 static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
1095 memset(e, 0, sizeof(*e));
1097 e->da = da;
1098 e->pa = pa;
1099 e->valid = MMU_CAM_V;
1100 e->pgsz = pgsz;
1101 e->endian = MMU_RAM_ENDIAN_LITTLE;
1102 e->elsz = MMU_RAM_ELSZ_8;
1103 e->mixed = 0;
1105 return iopgsz_to_bytes(e->pgsz);
1108 static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
1109 phys_addr_t pa, size_t bytes, int prot)
1111 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1112 struct device *dev = omap_domain->dev;
1113 struct omap_iommu_device *iommu;
1114 struct omap_iommu *oiommu;
1115 struct iotlb_entry e;
1116 int omap_pgsz;
1117 u32 ret = -EINVAL;
1118 int i;
1120 omap_pgsz = bytes_to_iopgsz(bytes);
1121 if (omap_pgsz < 0) {
1122 dev_err(dev, "invalid size to map: %d\n", bytes);
1123 return -EINVAL;
1126 dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%x\n", da, &pa, bytes);
1128 iotlb_init_entry(&e, da, pa, omap_pgsz);
1130 iommu = omap_domain->iommus;
1131 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1132 oiommu = iommu->iommu_dev;
1133 ret = omap_iopgtable_store_entry(oiommu, &e);
1134 if (ret) {
1135 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n",
1136 ret);
1137 break;
1141 if (ret) {
1142 while (i--) {
1143 iommu--;
1144 oiommu = iommu->iommu_dev;
1145 iopgtable_clear_entry(oiommu, da);
1149 return ret;
1152 static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1153 size_t size)
1155 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1156 struct device *dev = omap_domain->dev;
1157 struct omap_iommu_device *iommu;
1158 struct omap_iommu *oiommu;
1159 bool error = false;
1160 size_t bytes = 0;
1161 int i;
1163 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
1165 iommu = omap_domain->iommus;
1166 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1167 oiommu = iommu->iommu_dev;
1168 bytes = iopgtable_clear_entry(oiommu, da);
1169 if (!bytes)
1170 error = true;
1174 * simplify return - we are only checking if any of the iommus
1175 * reported an error, but not if all of them are unmapping the
1176 * same number of entries. This should not occur due to the
1177 * mirror programming.
1179 return error ? 0 : bytes;
1182 static int omap_iommu_count(struct device *dev)
1184 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1185 int count = 0;
1187 while (arch_data->iommu_dev) {
1188 count++;
1189 arch_data++;
1192 return count;
1195 /* caller should call cleanup if this function fails */
1196 static int omap_iommu_attach_init(struct device *dev,
1197 struct omap_iommu_domain *odomain)
1199 struct omap_iommu_device *iommu;
1200 int i;
1202 odomain->num_iommus = omap_iommu_count(dev);
1203 if (!odomain->num_iommus)
1204 return -EINVAL;
1206 odomain->iommus = kcalloc(odomain->num_iommus, sizeof(*iommu),
1207 GFP_ATOMIC);
1208 if (!odomain->iommus)
1209 return -ENOMEM;
1211 iommu = odomain->iommus;
1212 for (i = 0; i < odomain->num_iommus; i++, iommu++) {
1213 iommu->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_ATOMIC);
1214 if (!iommu->pgtable)
1215 return -ENOMEM;
1218 * should never fail, but please keep this around to ensure
1219 * we keep the hardware happy
1221 if (WARN_ON(!IS_ALIGNED((long)iommu->pgtable,
1222 IOPGD_TABLE_SIZE)))
1223 return -EINVAL;
1226 return 0;
1229 static void omap_iommu_detach_fini(struct omap_iommu_domain *odomain)
1231 int i;
1232 struct omap_iommu_device *iommu = odomain->iommus;
1234 for (i = 0; iommu && i < odomain->num_iommus; i++, iommu++)
1235 kfree(iommu->pgtable);
1237 kfree(odomain->iommus);
1238 odomain->num_iommus = 0;
1239 odomain->iommus = NULL;
1242 static int
1243 omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1245 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1246 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1247 struct omap_iommu_device *iommu;
1248 struct omap_iommu *oiommu;
1249 int ret = 0;
1250 int i;
1252 if (!arch_data || !arch_data->iommu_dev) {
1253 dev_err(dev, "device doesn't have an associated iommu\n");
1254 return -EINVAL;
1257 spin_lock(&omap_domain->lock);
1259 /* only a single client device can be attached to a domain */
1260 if (omap_domain->dev) {
1261 dev_err(dev, "iommu domain is already attached\n");
1262 ret = -EBUSY;
1263 goto out;
1266 ret = omap_iommu_attach_init(dev, omap_domain);
1267 if (ret) {
1268 dev_err(dev, "failed to allocate required iommu data %d\n",
1269 ret);
1270 goto init_fail;
1273 iommu = omap_domain->iommus;
1274 for (i = 0; i < omap_domain->num_iommus; i++, iommu++, arch_data++) {
1275 /* configure and enable the omap iommu */
1276 oiommu = arch_data->iommu_dev;
1277 ret = omap_iommu_attach(oiommu, iommu->pgtable);
1278 if (ret) {
1279 dev_err(dev, "can't get omap iommu: %d\n", ret);
1280 goto attach_fail;
1283 oiommu->domain = domain;
1284 iommu->iommu_dev = oiommu;
1287 omap_domain->dev = dev;
1289 goto out;
1291 attach_fail:
1292 while (i--) {
1293 iommu--;
1294 arch_data--;
1295 oiommu = iommu->iommu_dev;
1296 omap_iommu_detach(oiommu);
1297 iommu->iommu_dev = NULL;
1298 oiommu->domain = NULL;
1300 init_fail:
1301 omap_iommu_detach_fini(omap_domain);
1302 out:
1303 spin_unlock(&omap_domain->lock);
1304 return ret;
1307 static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1308 struct device *dev)
1310 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1311 struct omap_iommu_device *iommu = omap_domain->iommus;
1312 struct omap_iommu *oiommu;
1313 int i;
1315 if (!omap_domain->dev) {
1316 dev_err(dev, "domain has no attached device\n");
1317 return;
1320 /* only a single device is supported per domain for now */
1321 if (omap_domain->dev != dev) {
1322 dev_err(dev, "invalid attached device\n");
1323 return;
1327 * cleanup in the reverse order of attachment - this addresses
1328 * any h/w dependencies between multiple instances, if any
1330 iommu += (omap_domain->num_iommus - 1);
1331 arch_data += (omap_domain->num_iommus - 1);
1332 for (i = 0; i < omap_domain->num_iommus; i++, iommu--, arch_data--) {
1333 oiommu = iommu->iommu_dev;
1334 iopgtable_clear_entry_all(oiommu);
1336 omap_iommu_detach(oiommu);
1337 iommu->iommu_dev = NULL;
1338 oiommu->domain = NULL;
1341 omap_iommu_detach_fini(omap_domain);
1343 omap_domain->dev = NULL;
1346 static void omap_iommu_detach_dev(struct iommu_domain *domain,
1347 struct device *dev)
1349 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1351 spin_lock(&omap_domain->lock);
1352 _omap_iommu_detach_dev(omap_domain, dev);
1353 spin_unlock(&omap_domain->lock);
1356 static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
1358 struct omap_iommu_domain *omap_domain;
1360 if (type != IOMMU_DOMAIN_UNMANAGED)
1361 return NULL;
1363 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1364 if (!omap_domain)
1365 return NULL;
1367 spin_lock_init(&omap_domain->lock);
1369 omap_domain->domain.geometry.aperture_start = 0;
1370 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1371 omap_domain->domain.geometry.force_aperture = true;
1373 return &omap_domain->domain;
1376 static void omap_iommu_domain_free(struct iommu_domain *domain)
1378 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1381 * An iommu device is still attached
1382 * (currently, only one device can be attached) ?
1384 if (omap_domain->dev)
1385 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1387 kfree(omap_domain);
1390 static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1391 dma_addr_t da)
1393 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1394 struct omap_iommu_device *iommu = omap_domain->iommus;
1395 struct omap_iommu *oiommu = iommu->iommu_dev;
1396 struct device *dev = oiommu->dev;
1397 u32 *pgd, *pte;
1398 phys_addr_t ret = 0;
1401 * all the iommus within the domain will have identical programming,
1402 * so perform the lookup using just the first iommu
1404 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1406 if (pte) {
1407 if (iopte_is_small(*pte))
1408 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1409 else if (iopte_is_large(*pte))
1410 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1411 else
1412 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1413 (unsigned long long)da);
1414 } else {
1415 if (iopgd_is_section(*pgd))
1416 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1417 else if (iopgd_is_super(*pgd))
1418 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1419 else
1420 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1421 (unsigned long long)da);
1424 return ret;
1427 static int omap_iommu_add_device(struct device *dev)
1429 struct omap_iommu_arch_data *arch_data, *tmp;
1430 struct omap_iommu *oiommu;
1431 struct iommu_group *group;
1432 struct device_node *np;
1433 struct platform_device *pdev;
1434 int num_iommus, i;
1435 int ret;
1438 * Allocate the archdata iommu structure for DT-based devices.
1440 * TODO: Simplify this when removing non-DT support completely from the
1441 * IOMMU users.
1443 if (!dev->of_node)
1444 return 0;
1447 * retrieve the count of IOMMU nodes using phandle size as element size
1448 * since #iommu-cells = 0 for OMAP
1450 num_iommus = of_property_count_elems_of_size(dev->of_node, "iommus",
1451 sizeof(phandle));
1452 if (num_iommus < 0)
1453 return 0;
1455 arch_data = kcalloc(num_iommus + 1, sizeof(*arch_data), GFP_KERNEL);
1456 if (!arch_data)
1457 return -ENOMEM;
1459 for (i = 0, tmp = arch_data; i < num_iommus; i++, tmp++) {
1460 np = of_parse_phandle(dev->of_node, "iommus", i);
1461 if (!np) {
1462 kfree(arch_data);
1463 return -EINVAL;
1466 pdev = of_find_device_by_node(np);
1467 if (WARN_ON(!pdev)) {
1468 of_node_put(np);
1469 kfree(arch_data);
1470 return -EINVAL;
1473 oiommu = platform_get_drvdata(pdev);
1474 if (!oiommu) {
1475 of_node_put(np);
1476 kfree(arch_data);
1477 return -EINVAL;
1480 tmp->iommu_dev = oiommu;
1482 of_node_put(np);
1486 * use the first IOMMU alone for the sysfs device linking.
1487 * TODO: Evaluate if a single iommu_group needs to be
1488 * maintained for both IOMMUs
1490 oiommu = arch_data->iommu_dev;
1491 ret = iommu_device_link(&oiommu->iommu, dev);
1492 if (ret) {
1493 kfree(arch_data);
1494 return ret;
1497 dev->archdata.iommu = arch_data;
1500 * IOMMU group initialization calls into omap_iommu_device_group, which
1501 * needs a valid dev->archdata.iommu pointer
1503 group = iommu_group_get_for_dev(dev);
1504 if (IS_ERR(group)) {
1505 iommu_device_unlink(&oiommu->iommu, dev);
1506 dev->archdata.iommu = NULL;
1507 kfree(arch_data);
1508 return PTR_ERR(group);
1510 iommu_group_put(group);
1512 return 0;
1515 static void omap_iommu_remove_device(struct device *dev)
1517 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1519 if (!dev->of_node || !arch_data)
1520 return;
1522 iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
1523 iommu_group_remove_device(dev);
1525 dev->archdata.iommu = NULL;
1526 kfree(arch_data);
1530 static struct iommu_group *omap_iommu_device_group(struct device *dev)
1532 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1533 struct iommu_group *group = ERR_PTR(-EINVAL);
1535 if (arch_data->iommu_dev)
1536 group = iommu_group_ref_get(arch_data->iommu_dev->group);
1538 return group;
1541 static const struct iommu_ops omap_iommu_ops = {
1542 .domain_alloc = omap_iommu_domain_alloc,
1543 .domain_free = omap_iommu_domain_free,
1544 .attach_dev = omap_iommu_attach_dev,
1545 .detach_dev = omap_iommu_detach_dev,
1546 .map = omap_iommu_map,
1547 .unmap = omap_iommu_unmap,
1548 .iova_to_phys = omap_iommu_iova_to_phys,
1549 .add_device = omap_iommu_add_device,
1550 .remove_device = omap_iommu_remove_device,
1551 .device_group = omap_iommu_device_group,
1552 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
1555 static int __init omap_iommu_init(void)
1557 struct kmem_cache *p;
1558 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1559 size_t align = 1 << 10; /* L2 pagetable alignement */
1560 struct device_node *np;
1561 int ret;
1563 np = of_find_matching_node(NULL, omap_iommu_of_match);
1564 if (!np)
1565 return 0;
1567 of_node_put(np);
1569 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1570 NULL);
1571 if (!p)
1572 return -ENOMEM;
1573 iopte_cachep = p;
1575 omap_iommu_debugfs_init();
1577 ret = platform_driver_register(&omap_iommu_driver);
1578 if (ret) {
1579 pr_err("%s: failed to register driver\n", __func__);
1580 goto fail_driver;
1583 ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1584 if (ret)
1585 goto fail_bus;
1587 return 0;
1589 fail_bus:
1590 platform_driver_unregister(&omap_iommu_driver);
1591 fail_driver:
1592 kmem_cache_destroy(iopte_cachep);
1593 return ret;
1595 subsys_initcall(omap_iommu_init);
1596 /* must be ready before omap3isp is probed */