2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 * Copyright (C) 2013 Freescale Semiconductor, Inc.
16 * Author: Varun Sethi <varun.sethi@freescale.com>
20 #define pr_fmt(fmt) "fsl-pamu-domain: %s: " fmt, __func__
22 #include <linux/init.h>
23 #include <linux/iommu.h>
24 #include <linux/notifier.h>
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
29 #include <linux/interrupt.h>
30 #include <linux/device.h>
31 #include <linux/of_platform.h>
32 #include <linux/bootmem.h>
33 #include <linux/err.h>
35 #include <asm/bitops.h>
37 #include <asm/pci-bridge.h>
38 #include <sysdev/fsl_pci.h>
40 #include "fsl_pamu_domain.h"
43 * Global spinlock that needs to be held while
46 static DEFINE_SPINLOCK(iommu_lock
);
48 static struct kmem_cache
*fsl_pamu_domain_cache
;
49 static struct kmem_cache
*iommu_devinfo_cache
;
50 static DEFINE_SPINLOCK(device_domain_lock
);
52 static int __init
iommu_init_mempool(void)
55 fsl_pamu_domain_cache
= kmem_cache_create("fsl_pamu_domain",
56 sizeof(struct fsl_dma_domain
),
61 if (!fsl_pamu_domain_cache
) {
62 pr_debug("Couldn't create fsl iommu_domain cache\n");
66 iommu_devinfo_cache
= kmem_cache_create("iommu_devinfo",
67 sizeof(struct device_domain_info
),
71 if (!iommu_devinfo_cache
) {
72 pr_debug("Couldn't create devinfo cache\n");
73 kmem_cache_destroy(fsl_pamu_domain_cache
);
80 static phys_addr_t
get_phys_addr(struct fsl_dma_domain
*dma_domain
, dma_addr_t iova
)
82 u32 win_cnt
= dma_domain
->win_cnt
;
83 struct dma_window
*win_ptr
=
84 &dma_domain
->win_arr
[0];
85 struct iommu_domain_geometry
*geom
;
87 geom
= &dma_domain
->iommu_domain
->geometry
;
89 if (!win_cnt
|| !dma_domain
->geom_size
) {
90 pr_debug("Number of windows/geometry not configured for the domain\n");
96 dma_addr_t subwin_iova
;
99 subwin_size
= dma_domain
->geom_size
>> ilog2(win_cnt
);
100 subwin_iova
= iova
& ~(subwin_size
- 1);
101 wnd
= (subwin_iova
- geom
->aperture_start
) >> ilog2(subwin_size
);
102 win_ptr
= &dma_domain
->win_arr
[wnd
];
106 return (win_ptr
->paddr
+ (iova
& (win_ptr
->size
- 1)));
111 static int map_subwins(int liodn
, struct fsl_dma_domain
*dma_domain
)
113 struct dma_window
*sub_win_ptr
=
114 &dma_domain
->win_arr
[0];
116 unsigned long rpn
, flags
;
118 for (i
= 0; i
< dma_domain
->win_cnt
; i
++) {
119 if (sub_win_ptr
[i
].valid
) {
120 rpn
= sub_win_ptr
[i
].paddr
>>
122 spin_lock_irqsave(&iommu_lock
, flags
);
123 ret
= pamu_config_spaace(liodn
, dma_domain
->win_cnt
, i
,
127 dma_domain
->snoop_id
,
128 dma_domain
->stash_id
,
130 sub_win_ptr
[i
].prot
);
131 spin_unlock_irqrestore(&iommu_lock
, flags
);
133 pr_debug("PAMU SPAACE configuration failed for liodn %d\n",
143 static int map_win(int liodn
, struct fsl_dma_domain
*dma_domain
)
146 struct dma_window
*wnd
= &dma_domain
->win_arr
[0];
147 phys_addr_t wnd_addr
= dma_domain
->iommu_domain
->geometry
.aperture_start
;
150 spin_lock_irqsave(&iommu_lock
, flags
);
151 ret
= pamu_config_ppaace(liodn
, wnd_addr
,
154 wnd
->paddr
>> PAMU_PAGE_SHIFT
,
155 dma_domain
->snoop_id
, dma_domain
->stash_id
,
157 spin_unlock_irqrestore(&iommu_lock
, flags
);
159 pr_debug("PAMU PAACE configuration failed for liodn %d\n",
165 /* Map the DMA window corresponding to the LIODN */
166 static int map_liodn(int liodn
, struct fsl_dma_domain
*dma_domain
)
168 if (dma_domain
->win_cnt
> 1)
169 return map_subwins(liodn
, dma_domain
);
171 return map_win(liodn
, dma_domain
);
175 /* Update window/subwindow mapping for the LIODN */
176 static int update_liodn(int liodn
, struct fsl_dma_domain
*dma_domain
, u32 wnd_nr
)
179 struct dma_window
*wnd
= &dma_domain
->win_arr
[wnd_nr
];
182 spin_lock_irqsave(&iommu_lock
, flags
);
183 if (dma_domain
->win_cnt
> 1) {
184 ret
= pamu_config_spaace(liodn
, dma_domain
->win_cnt
, wnd_nr
,
187 wnd
->paddr
>> PAMU_PAGE_SHIFT
,
188 dma_domain
->snoop_id
,
189 dma_domain
->stash_id
,
190 (wnd_nr
> 0) ? 1 : 0,
193 pr_debug("Subwindow reconfiguration failed for liodn %d\n", liodn
);
195 phys_addr_t wnd_addr
;
197 wnd_addr
= dma_domain
->iommu_domain
->geometry
.aperture_start
;
199 ret
= pamu_config_ppaace(liodn
, wnd_addr
,
202 wnd
->paddr
>> PAMU_PAGE_SHIFT
,
203 dma_domain
->snoop_id
, dma_domain
->stash_id
,
206 pr_debug("Window reconfiguration failed for liodn %d\n", liodn
);
209 spin_unlock_irqrestore(&iommu_lock
, flags
);
214 static int update_liodn_stash(int liodn
, struct fsl_dma_domain
*dma_domain
,
220 spin_lock_irqsave(&iommu_lock
, flags
);
221 if (!dma_domain
->win_arr
) {
222 pr_debug("Windows not configured, stash destination update failed for liodn %d\n", liodn
);
223 spin_unlock_irqrestore(&iommu_lock
, flags
);
227 for (i
= 0; i
< dma_domain
->win_cnt
; i
++) {
228 ret
= pamu_update_paace_stash(liodn
, i
, val
);
230 pr_debug("Failed to update SPAACE %d field for liodn %d\n ", i
, liodn
);
231 spin_unlock_irqrestore(&iommu_lock
, flags
);
236 spin_unlock_irqrestore(&iommu_lock
, flags
);
241 /* Set the geometry parameters for a LIODN */
242 static int pamu_set_liodn(int liodn
, struct device
*dev
,
243 struct fsl_dma_domain
*dma_domain
,
244 struct iommu_domain_geometry
*geom_attr
,
247 phys_addr_t window_addr
, window_size
;
248 phys_addr_t subwin_size
;
250 u32 omi_index
= ~(u32
)0;
254 * Configure the omi_index at the geometry setup time.
255 * This is a static value which depends on the type of
256 * device and would not change thereafter.
258 get_ome_index(&omi_index
, dev
);
260 window_addr
= geom_attr
->aperture_start
;
261 window_size
= dma_domain
->geom_size
;
263 spin_lock_irqsave(&iommu_lock
, flags
);
264 ret
= pamu_disable_liodn(liodn
);
266 ret
= pamu_config_ppaace(liodn
, window_addr
, window_size
, omi_index
,
267 0, dma_domain
->snoop_id
,
268 dma_domain
->stash_id
, win_cnt
, 0);
269 spin_unlock_irqrestore(&iommu_lock
, flags
);
271 pr_debug("PAMU PAACE configuration failed for liodn %d, win_cnt =%d\n", liodn
, win_cnt
);
276 subwin_size
= window_size
>> ilog2(win_cnt
);
277 for (i
= 0; i
< win_cnt
; i
++) {
278 spin_lock_irqsave(&iommu_lock
, flags
);
279 ret
= pamu_disable_spaace(liodn
, i
);
281 ret
= pamu_config_spaace(liodn
, win_cnt
, i
,
282 subwin_size
, omi_index
,
283 0, dma_domain
->snoop_id
,
284 dma_domain
->stash_id
,
286 spin_unlock_irqrestore(&iommu_lock
, flags
);
288 pr_debug("PAMU SPAACE configuration failed for liodn %d\n", liodn
);
297 static int check_size(u64 size
, dma_addr_t iova
)
300 * Size must be a power of two and at least be equal
303 if ((size
& (size
- 1)) || size
< PAMU_PAGE_SIZE
) {
304 pr_debug("%s: size too small or not a power of two\n", __func__
);
308 /* iova must be page size aligned*/
309 if (iova
& (size
- 1)) {
310 pr_debug("%s: address is not aligned with window size\n", __func__
);
317 static struct fsl_dma_domain
*iommu_alloc_dma_domain(void)
319 struct fsl_dma_domain
*domain
;
321 domain
= kmem_cache_zalloc(fsl_pamu_domain_cache
, GFP_KERNEL
);
325 domain
->stash_id
= ~(u32
)0;
326 domain
->snoop_id
= ~(u32
)0;
327 domain
->win_cnt
= pamu_get_max_subwin_cnt();
328 domain
->geom_size
= 0;
330 INIT_LIST_HEAD(&domain
->devices
);
332 spin_lock_init(&domain
->domain_lock
);
337 static void remove_device_ref(struct device_domain_info
*info
, u32 win_cnt
)
341 list_del(&info
->link
);
342 spin_lock_irqsave(&iommu_lock
, flags
);
344 pamu_free_subwins(info
->liodn
);
345 pamu_disable_liodn(info
->liodn
);
346 spin_unlock_irqrestore(&iommu_lock
, flags
);
347 spin_lock_irqsave(&device_domain_lock
, flags
);
348 info
->dev
->archdata
.iommu_domain
= NULL
;
349 kmem_cache_free(iommu_devinfo_cache
, info
);
350 spin_unlock_irqrestore(&device_domain_lock
, flags
);
353 static void detach_device(struct device
*dev
, struct fsl_dma_domain
*dma_domain
)
355 struct device_domain_info
*info
, *tmp
;
358 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
359 /* Remove the device from the domain device list */
360 list_for_each_entry_safe(info
, tmp
, &dma_domain
->devices
, link
) {
361 if (!dev
|| (info
->dev
== dev
))
362 remove_device_ref(info
, dma_domain
->win_cnt
);
364 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
367 static void attach_device(struct fsl_dma_domain
*dma_domain
, int liodn
, struct device
*dev
)
369 struct device_domain_info
*info
, *old_domain_info
;
372 spin_lock_irqsave(&device_domain_lock
, flags
);
374 * Check here if the device is already attached to domain or not.
375 * If the device is already attached to a domain detach it.
377 old_domain_info
= dev
->archdata
.iommu_domain
;
378 if (old_domain_info
&& old_domain_info
->domain
!= dma_domain
) {
379 spin_unlock_irqrestore(&device_domain_lock
, flags
);
380 detach_device(dev
, old_domain_info
->domain
);
381 spin_lock_irqsave(&device_domain_lock
, flags
);
384 info
= kmem_cache_zalloc(iommu_devinfo_cache
, GFP_ATOMIC
);
388 info
->domain
= dma_domain
;
390 list_add(&info
->link
, &dma_domain
->devices
);
392 * In case of devices with multiple LIODNs just store
393 * the info for the first LIODN as all
394 * LIODNs share the same domain
396 if (!dev
->archdata
.iommu_domain
)
397 dev
->archdata
.iommu_domain
= info
;
398 spin_unlock_irqrestore(&device_domain_lock
, flags
);
402 static phys_addr_t
fsl_pamu_iova_to_phys(struct iommu_domain
*domain
,
405 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
407 if ((iova
< domain
->geometry
.aperture_start
) ||
408 iova
> (domain
->geometry
.aperture_end
))
411 return get_phys_addr(dma_domain
, iova
);
414 static int fsl_pamu_domain_has_cap(struct iommu_domain
*domain
,
417 return cap
== IOMMU_CAP_CACHE_COHERENCY
;
420 static void fsl_pamu_domain_destroy(struct iommu_domain
*domain
)
422 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
426 /* remove all the devices from the device list */
427 detach_device(NULL
, dma_domain
);
429 dma_domain
->enabled
= 0;
430 dma_domain
->mapped
= 0;
432 kmem_cache_free(fsl_pamu_domain_cache
, dma_domain
);
435 static int fsl_pamu_domain_init(struct iommu_domain
*domain
)
437 struct fsl_dma_domain
*dma_domain
;
439 dma_domain
= iommu_alloc_dma_domain();
441 pr_debug("dma_domain allocation failed\n");
444 domain
->priv
= dma_domain
;
445 dma_domain
->iommu_domain
= domain
;
446 /* defaul geometry 64 GB i.e. maximum system address */
447 domain
->geometry
.aperture_start
= 0;
448 domain
->geometry
.aperture_end
= (1ULL << 36) - 1;
449 domain
->geometry
.force_aperture
= true;
454 /* Configure geometry settings for all LIODNs associated with domain */
455 static int pamu_set_domain_geometry(struct fsl_dma_domain
*dma_domain
,
456 struct iommu_domain_geometry
*geom_attr
,
459 struct device_domain_info
*info
;
462 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
463 ret
= pamu_set_liodn(info
->liodn
, info
->dev
, dma_domain
,
472 /* Update stash destination for all LIODNs associated with the domain */
473 static int update_domain_stash(struct fsl_dma_domain
*dma_domain
, u32 val
)
475 struct device_domain_info
*info
;
478 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
479 ret
= update_liodn_stash(info
->liodn
, dma_domain
, val
);
487 /* Update domain mappings for all LIODNs associated with the domain */
488 static int update_domain_mapping(struct fsl_dma_domain
*dma_domain
, u32 wnd_nr
)
490 struct device_domain_info
*info
;
493 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
494 ret
= update_liodn(info
->liodn
, dma_domain
, wnd_nr
);
501 static int disable_domain_win(struct fsl_dma_domain
*dma_domain
, u32 wnd_nr
)
503 struct device_domain_info
*info
;
506 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
507 if (dma_domain
->win_cnt
== 1 && dma_domain
->enabled
) {
508 ret
= pamu_disable_liodn(info
->liodn
);
510 dma_domain
->enabled
= 0;
512 ret
= pamu_disable_spaace(info
->liodn
, wnd_nr
);
519 static void fsl_pamu_window_disable(struct iommu_domain
*domain
, u32 wnd_nr
)
521 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
525 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
526 if (!dma_domain
->win_arr
) {
527 pr_debug("Number of windows not configured\n");
528 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
532 if (wnd_nr
>= dma_domain
->win_cnt
) {
533 pr_debug("Invalid window index\n");
534 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
538 if (dma_domain
->win_arr
[wnd_nr
].valid
) {
539 ret
= disable_domain_win(dma_domain
, wnd_nr
);
541 dma_domain
->win_arr
[wnd_nr
].valid
= 0;
542 dma_domain
->mapped
--;
546 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
550 static int fsl_pamu_window_enable(struct iommu_domain
*domain
, u32 wnd_nr
,
551 phys_addr_t paddr
, u64 size
, int prot
)
553 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
554 struct dma_window
*wnd
;
560 if (prot
& IOMMU_READ
)
561 pamu_prot
|= PAACE_AP_PERMS_QUERY
;
562 if (prot
& IOMMU_WRITE
)
563 pamu_prot
|= PAACE_AP_PERMS_UPDATE
;
565 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
566 if (!dma_domain
->win_arr
) {
567 pr_debug("Number of windows not configured\n");
568 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
572 if (wnd_nr
>= dma_domain
->win_cnt
) {
573 pr_debug("Invalid window index\n");
574 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
578 win_size
= dma_domain
->geom_size
>> ilog2(dma_domain
->win_cnt
);
579 if (size
> win_size
) {
580 pr_debug("Invalid window size \n");
581 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
585 if (dma_domain
->win_cnt
== 1) {
586 if (dma_domain
->enabled
) {
587 pr_debug("Disable the window before updating the mapping\n");
588 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
592 ret
= check_size(size
, domain
->geometry
.aperture_start
);
594 pr_debug("Aperture start not aligned to the size\n");
595 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
600 wnd
= &dma_domain
->win_arr
[wnd_nr
];
604 wnd
->prot
= pamu_prot
;
606 ret
= update_domain_mapping(dma_domain
, wnd_nr
);
609 dma_domain
->mapped
++;
612 pr_debug("Disable the window before updating the mapping\n");
616 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
622 * Attach the LIODN to the DMA domain and configure the geometry
623 * and window mappings.
625 static int handle_attach_device(struct fsl_dma_domain
*dma_domain
,
626 struct device
*dev
, const u32
*liodn
,
630 struct iommu_domain
*domain
= dma_domain
->iommu_domain
;
634 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
635 for (i
= 0; i
< num
; i
++) {
637 /* Ensure that LIODN value is valid */
638 if (liodn
[i
] >= PAACE_NUMBER_ENTRIES
) {
639 pr_debug("Invalid liodn %d, attach device failed for %s\n",
640 liodn
[i
], dev
->of_node
->full_name
);
645 attach_device(dma_domain
, liodn
[i
], dev
);
647 * Check if geometry has already been configured
648 * for the domain. If yes, set the geometry for
651 if (dma_domain
->win_arr
) {
652 u32 win_cnt
= dma_domain
->win_cnt
> 1 ? dma_domain
->win_cnt
: 0;
653 ret
= pamu_set_liodn(liodn
[i
], dev
, dma_domain
,
658 if (dma_domain
->mapped
) {
660 * Create window/subwindow mapping for
663 ret
= map_liodn(liodn
[i
], dma_domain
);
669 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
674 static int fsl_pamu_attach_device(struct iommu_domain
*domain
,
677 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
681 struct pci_dev
*pdev
= NULL
;
682 struct pci_controller
*pci_ctl
;
685 * Use LIODN of the PCI controller while attaching a
688 if (dev_is_pci(dev
)) {
689 pdev
= to_pci_dev(dev
);
690 pci_ctl
= pci_bus_to_host(pdev
->bus
);
692 * make dev point to pci controller device
693 * so we can get the LIODN programmed by
696 dev
= pci_ctl
->parent
;
699 liodn
= of_get_property(dev
->of_node
, "fsl,liodn", &len
);
701 liodn_cnt
= len
/ sizeof(u32
);
702 ret
= handle_attach_device(dma_domain
, dev
,
705 pr_debug("missing fsl,liodn property at %s\n",
706 dev
->of_node
->full_name
);
713 static void fsl_pamu_detach_device(struct iommu_domain
*domain
,
716 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
719 struct pci_dev
*pdev
= NULL
;
720 struct pci_controller
*pci_ctl
;
723 * Use LIODN of the PCI controller while detaching a
726 if (dev_is_pci(dev
)) {
727 pdev
= to_pci_dev(dev
);
728 pci_ctl
= pci_bus_to_host(pdev
->bus
);
730 * make dev point to pci controller device
731 * so we can get the LIODN programmed by
734 dev
= pci_ctl
->parent
;
737 prop
= of_get_property(dev
->of_node
, "fsl,liodn", &len
);
739 detach_device(dev
, dma_domain
);
741 pr_debug("missing fsl,liodn property at %s\n",
742 dev
->of_node
->full_name
);
745 static int configure_domain_geometry(struct iommu_domain
*domain
, void *data
)
747 struct iommu_domain_geometry
*geom_attr
= data
;
748 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
749 dma_addr_t geom_size
;
752 geom_size
= geom_attr
->aperture_end
- geom_attr
->aperture_start
+ 1;
754 * Sanity check the geometry size. Also, we do not support
755 * DMA outside of the geometry.
757 if (check_size(geom_size
, geom_attr
->aperture_start
) ||
758 !geom_attr
->force_aperture
) {
759 pr_debug("Invalid PAMU geometry attributes\n");
763 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
764 if (dma_domain
->enabled
) {
765 pr_debug("Can't set geometry attributes as domain is active\n");
766 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
770 /* Copy the domain geometry information */
771 memcpy(&domain
->geometry
, geom_attr
,
772 sizeof(struct iommu_domain_geometry
));
773 dma_domain
->geom_size
= geom_size
;
775 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
780 /* Set the domain stash attribute */
781 static int configure_domain_stash(struct fsl_dma_domain
*dma_domain
, void *data
)
783 struct pamu_stash_attribute
*stash_attr
= data
;
787 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
789 memcpy(&dma_domain
->dma_stash
, stash_attr
,
790 sizeof(struct pamu_stash_attribute
));
792 dma_domain
->stash_id
= get_stash_id(stash_attr
->cache
,
794 if (dma_domain
->stash_id
== ~(u32
)0) {
795 pr_debug("Invalid stash attributes\n");
796 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
800 ret
= update_domain_stash(dma_domain
, dma_domain
->stash_id
);
802 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
807 /* Configure domain dma state i.e. enable/disable DMA*/
808 static int configure_domain_dma_state(struct fsl_dma_domain
*dma_domain
, bool enable
)
810 struct device_domain_info
*info
;
814 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
816 if (enable
&& !dma_domain
->mapped
) {
817 pr_debug("Can't enable DMA domain without valid mapping\n");
818 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
822 dma_domain
->enabled
= enable
;
823 list_for_each_entry(info
, &dma_domain
->devices
,
825 ret
= (enable
) ? pamu_enable_liodn(info
->liodn
) :
826 pamu_disable_liodn(info
->liodn
);
828 pr_debug("Unable to set dma state for liodn %d",
831 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
836 static int fsl_pamu_set_domain_attr(struct iommu_domain
*domain
,
837 enum iommu_attr attr_type
, void *data
)
839 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
844 case DOMAIN_ATTR_GEOMETRY
:
845 ret
= configure_domain_geometry(domain
, data
);
847 case DOMAIN_ATTR_FSL_PAMU_STASH
:
848 ret
= configure_domain_stash(dma_domain
, data
);
850 case DOMAIN_ATTR_FSL_PAMU_ENABLE
:
851 ret
= configure_domain_dma_state(dma_domain
, *(int *)data
);
854 pr_debug("Unsupported attribute type\n");
862 static int fsl_pamu_get_domain_attr(struct iommu_domain
*domain
,
863 enum iommu_attr attr_type
, void *data
)
865 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
870 case DOMAIN_ATTR_FSL_PAMU_STASH
:
871 memcpy((struct pamu_stash_attribute
*) data
, &dma_domain
->dma_stash
,
872 sizeof(struct pamu_stash_attribute
));
874 case DOMAIN_ATTR_FSL_PAMU_ENABLE
:
875 *(int *)data
= dma_domain
->enabled
;
877 case DOMAIN_ATTR_FSL_PAMUV1
:
878 *(int *)data
= DOMAIN_ATTR_FSL_PAMUV1
;
881 pr_debug("Unsupported attribute type\n");
889 static struct iommu_group
*get_device_iommu_group(struct device
*dev
)
891 struct iommu_group
*group
;
893 group
= iommu_group_get(dev
);
895 group
= iommu_group_alloc();
900 static bool check_pci_ctl_endpt_part(struct pci_controller
*pci_ctl
)
904 /* Check the PCI controller version number by readding BRR1 register */
905 version
= in_be32(pci_ctl
->cfg_addr
+ (PCI_FSL_BRR1
>> 2));
906 version
&= PCI_FSL_BRR1_VER
;
907 /* If PCI controller version is >= 0x204 we can partition endpoints*/
908 if (version
>= 0x204)
914 /* Get iommu group information from peer devices or devices on the parent bus */
915 static struct iommu_group
*get_shared_pci_device_group(struct pci_dev
*pdev
)
918 struct iommu_group
*group
;
919 struct pci_bus
*bus
= pdev
->bus
;
922 * Traverese the pci bus device list to get
923 * the shared iommu group.
926 list_for_each_entry(tmp
, &bus
->devices
, bus_list
) {
929 group
= iommu_group_get(&tmp
->dev
);
940 static struct iommu_group
*get_pci_device_group(struct pci_dev
*pdev
)
942 struct pci_controller
*pci_ctl
;
943 bool pci_endpt_partioning
;
944 struct iommu_group
*group
= NULL
;
946 pci_ctl
= pci_bus_to_host(pdev
->bus
);
947 pci_endpt_partioning
= check_pci_ctl_endpt_part(pci_ctl
);
948 /* We can partition PCIe devices so assign device group to the device */
949 if (pci_endpt_partioning
) {
950 group
= iommu_group_get_for_dev(&pdev
->dev
);
953 * PCIe controller is not a paritionable entity
954 * free the controller device iommu_group.
956 if (pci_ctl
->parent
->iommu_group
)
957 iommu_group_remove_device(pci_ctl
->parent
);
960 * All devices connected to the controller will share the
961 * PCI controllers device group. If this is the first
962 * device to be probed for the pci controller, copy the
963 * device group information from the PCI controller device
964 * node and remove the PCI controller iommu group.
965 * For subsequent devices, the iommu group information can
966 * be obtained from sibling devices (i.e. from the bus_devices
969 if (pci_ctl
->parent
->iommu_group
) {
970 group
= get_device_iommu_group(pci_ctl
->parent
);
971 iommu_group_remove_device(pci_ctl
->parent
);
973 group
= get_shared_pci_device_group(pdev
);
977 group
= ERR_PTR(-ENODEV
);
982 static int fsl_pamu_add_device(struct device
*dev
)
984 struct iommu_group
*group
= ERR_PTR(-ENODEV
);
985 struct pci_dev
*pdev
;
990 * For platform devices we allocate a separate group for
991 * each of the devices.
993 if (dev_is_pci(dev
)) {
994 pdev
= to_pci_dev(dev
);
995 /* Don't create device groups for virtual PCI bridges */
996 if (pdev
->subordinate
)
999 group
= get_pci_device_group(pdev
);
1002 prop
= of_get_property(dev
->of_node
, "fsl,liodn", &len
);
1004 group
= get_device_iommu_group(dev
);
1008 return PTR_ERR(group
);
1010 ret
= iommu_group_add_device(group
, dev
);
1012 iommu_group_put(group
);
1016 static void fsl_pamu_remove_device(struct device
*dev
)
1018 iommu_group_remove_device(dev
);
1021 static int fsl_pamu_set_windows(struct iommu_domain
*domain
, u32 w_count
)
1023 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
1024 unsigned long flags
;
1027 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
1028 /* Ensure domain is inactive i.e. DMA should be disabled for the domain */
1029 if (dma_domain
->enabled
) {
1030 pr_debug("Can't set geometry attributes as domain is active\n");
1031 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1035 /* Ensure that the geometry has been set for the domain */
1036 if (!dma_domain
->geom_size
) {
1037 pr_debug("Please configure geometry before setting the number of windows\n");
1038 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1043 * Ensure we have valid window count i.e. it should be less than
1044 * maximum permissible limit and should be a power of two.
1046 if (w_count
> pamu_get_max_subwin_cnt() || !is_power_of_2(w_count
)) {
1047 pr_debug("Invalid window count\n");
1048 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1052 ret
= pamu_set_domain_geometry(dma_domain
, &domain
->geometry
,
1053 ((w_count
> 1) ? w_count
: 0));
1055 kfree(dma_domain
->win_arr
);
1056 dma_domain
->win_arr
= kzalloc(sizeof(struct dma_window
) *
1057 w_count
, GFP_ATOMIC
);
1058 if (!dma_domain
->win_arr
) {
1059 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1062 dma_domain
->win_cnt
= w_count
;
1064 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1069 static u32
fsl_pamu_get_windows(struct iommu_domain
*domain
)
1071 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
1073 return dma_domain
->win_cnt
;
1076 static const struct iommu_ops fsl_pamu_ops
= {
1077 .domain_init
= fsl_pamu_domain_init
,
1078 .domain_destroy
= fsl_pamu_domain_destroy
,
1079 .attach_dev
= fsl_pamu_attach_device
,
1080 .detach_dev
= fsl_pamu_detach_device
,
1081 .domain_window_enable
= fsl_pamu_window_enable
,
1082 .domain_window_disable
= fsl_pamu_window_disable
,
1083 .domain_get_windows
= fsl_pamu_get_windows
,
1084 .domain_set_windows
= fsl_pamu_set_windows
,
1085 .iova_to_phys
= fsl_pamu_iova_to_phys
,
1086 .domain_has_cap
= fsl_pamu_domain_has_cap
,
1087 .domain_set_attr
= fsl_pamu_set_domain_attr
,
1088 .domain_get_attr
= fsl_pamu_get_domain_attr
,
1089 .add_device
= fsl_pamu_add_device
,
1090 .remove_device
= fsl_pamu_remove_device
,
1093 int pamu_domain_init(void)
1097 ret
= iommu_init_mempool();
1101 bus_set_iommu(&platform_bus_type
, &fsl_pamu_ops
);
1102 bus_set_iommu(&pci_bus_type
, &fsl_pamu_ops
);