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"
44 * Global spinlock that needs to be held while
47 static DEFINE_SPINLOCK(iommu_lock
);
49 static struct kmem_cache
*fsl_pamu_domain_cache
;
50 static struct kmem_cache
*iommu_devinfo_cache
;
51 static DEFINE_SPINLOCK(device_domain_lock
);
53 static int __init
iommu_init_mempool(void)
56 fsl_pamu_domain_cache
= kmem_cache_create("fsl_pamu_domain",
57 sizeof(struct fsl_dma_domain
),
62 if (!fsl_pamu_domain_cache
) {
63 pr_debug("Couldn't create fsl iommu_domain cache\n");
67 iommu_devinfo_cache
= kmem_cache_create("iommu_devinfo",
68 sizeof(struct device_domain_info
),
72 if (!iommu_devinfo_cache
) {
73 pr_debug("Couldn't create devinfo cache\n");
74 kmem_cache_destroy(fsl_pamu_domain_cache
);
81 static phys_addr_t
get_phys_addr(struct fsl_dma_domain
*dma_domain
, dma_addr_t iova
)
83 u32 win_cnt
= dma_domain
->win_cnt
;
84 struct dma_window
*win_ptr
=
85 &dma_domain
->win_arr
[0];
86 struct iommu_domain_geometry
*geom
;
88 geom
= &dma_domain
->iommu_domain
->geometry
;
90 if (!win_cnt
|| !dma_domain
->geom_size
) {
91 pr_debug("Number of windows/geometry not configured for the domain\n");
97 dma_addr_t subwin_iova
;
100 subwin_size
= dma_domain
->geom_size
>> ilog2(win_cnt
);
101 subwin_iova
= iova
& ~(subwin_size
- 1);
102 wnd
= (subwin_iova
- geom
->aperture_start
) >> ilog2(subwin_size
);
103 win_ptr
= &dma_domain
->win_arr
[wnd
];
107 return (win_ptr
->paddr
+ (iova
& (win_ptr
->size
- 1)));
112 static int map_subwins(int liodn
, struct fsl_dma_domain
*dma_domain
)
114 struct dma_window
*sub_win_ptr
=
115 &dma_domain
->win_arr
[0];
117 unsigned long rpn
, flags
;
119 for (i
= 0; i
< dma_domain
->win_cnt
; i
++) {
120 if (sub_win_ptr
[i
].valid
) {
121 rpn
= sub_win_ptr
[i
].paddr
>>
123 spin_lock_irqsave(&iommu_lock
, flags
);
124 ret
= pamu_config_spaace(liodn
, dma_domain
->win_cnt
, i
,
128 dma_domain
->snoop_id
,
129 dma_domain
->stash_id
,
131 sub_win_ptr
[i
].prot
);
132 spin_unlock_irqrestore(&iommu_lock
, flags
);
134 pr_debug("PAMU SPAACE configuration failed for liodn %d\n",
144 static int map_win(int liodn
, struct fsl_dma_domain
*dma_domain
)
147 struct dma_window
*wnd
= &dma_domain
->win_arr
[0];
148 phys_addr_t wnd_addr
= dma_domain
->iommu_domain
->geometry
.aperture_start
;
151 spin_lock_irqsave(&iommu_lock
, flags
);
152 ret
= pamu_config_ppaace(liodn
, wnd_addr
,
155 wnd
->paddr
>> PAMU_PAGE_SHIFT
,
156 dma_domain
->snoop_id
, dma_domain
->stash_id
,
158 spin_unlock_irqrestore(&iommu_lock
, flags
);
160 pr_debug("PAMU PAACE configuration failed for liodn %d\n",
166 /* Map the DMA window corresponding to the LIODN */
167 static int map_liodn(int liodn
, struct fsl_dma_domain
*dma_domain
)
169 if (dma_domain
->win_cnt
> 1)
170 return map_subwins(liodn
, dma_domain
);
172 return map_win(liodn
, dma_domain
);
176 /* Update window/subwindow mapping for the LIODN */
177 static int update_liodn(int liodn
, struct fsl_dma_domain
*dma_domain
, u32 wnd_nr
)
180 struct dma_window
*wnd
= &dma_domain
->win_arr
[wnd_nr
];
183 spin_lock_irqsave(&iommu_lock
, flags
);
184 if (dma_domain
->win_cnt
> 1) {
185 ret
= pamu_config_spaace(liodn
, dma_domain
->win_cnt
, wnd_nr
,
188 wnd
->paddr
>> PAMU_PAGE_SHIFT
,
189 dma_domain
->snoop_id
,
190 dma_domain
->stash_id
,
191 (wnd_nr
> 0) ? 1 : 0,
194 pr_debug("Subwindow reconfiguration failed for liodn %d\n", liodn
);
196 phys_addr_t wnd_addr
;
198 wnd_addr
= dma_domain
->iommu_domain
->geometry
.aperture_start
;
200 ret
= pamu_config_ppaace(liodn
, wnd_addr
,
203 wnd
->paddr
>> PAMU_PAGE_SHIFT
,
204 dma_domain
->snoop_id
, dma_domain
->stash_id
,
207 pr_debug("Window reconfiguration failed for liodn %d\n", liodn
);
210 spin_unlock_irqrestore(&iommu_lock
, flags
);
215 static int update_liodn_stash(int liodn
, struct fsl_dma_domain
*dma_domain
,
221 spin_lock_irqsave(&iommu_lock
, flags
);
222 if (!dma_domain
->win_arr
) {
223 pr_debug("Windows not configured, stash destination update failed for liodn %d\n", liodn
);
224 spin_unlock_irqrestore(&iommu_lock
, flags
);
228 for (i
= 0; i
< dma_domain
->win_cnt
; i
++) {
229 ret
= pamu_update_paace_stash(liodn
, i
, val
);
231 pr_debug("Failed to update SPAACE %d field for liodn %d\n ", i
, liodn
);
232 spin_unlock_irqrestore(&iommu_lock
, flags
);
237 spin_unlock_irqrestore(&iommu_lock
, flags
);
242 /* Set the geometry parameters for a LIODN */
243 static int pamu_set_liodn(int liodn
, struct device
*dev
,
244 struct fsl_dma_domain
*dma_domain
,
245 struct iommu_domain_geometry
*geom_attr
,
248 phys_addr_t window_addr
, window_size
;
249 phys_addr_t subwin_size
;
251 u32 omi_index
= ~(u32
)0;
255 * Configure the omi_index at the geometry setup time.
256 * This is a static value which depends on the type of
257 * device and would not change thereafter.
259 get_ome_index(&omi_index
, dev
);
261 window_addr
= geom_attr
->aperture_start
;
262 window_size
= dma_domain
->geom_size
;
264 spin_lock_irqsave(&iommu_lock
, flags
);
265 ret
= pamu_disable_liodn(liodn
);
267 ret
= pamu_config_ppaace(liodn
, window_addr
, window_size
, omi_index
,
268 0, dma_domain
->snoop_id
,
269 dma_domain
->stash_id
, win_cnt
, 0);
270 spin_unlock_irqrestore(&iommu_lock
, flags
);
272 pr_debug("PAMU PAACE configuration failed for liodn %d, win_cnt =%d\n", liodn
, win_cnt
);
277 subwin_size
= window_size
>> ilog2(win_cnt
);
278 for (i
= 0; i
< win_cnt
; i
++) {
279 spin_lock_irqsave(&iommu_lock
, flags
);
280 ret
= pamu_disable_spaace(liodn
, i
);
282 ret
= pamu_config_spaace(liodn
, win_cnt
, i
,
283 subwin_size
, omi_index
,
284 0, dma_domain
->snoop_id
,
285 dma_domain
->stash_id
,
287 spin_unlock_irqrestore(&iommu_lock
, flags
);
289 pr_debug("PAMU SPAACE configuration failed for liodn %d\n", liodn
);
298 static int check_size(u64 size
, dma_addr_t iova
)
301 * Size must be a power of two and at least be equal
304 if ((size
& (size
- 1)) || size
< PAMU_PAGE_SIZE
) {
305 pr_debug("%s: size too small or not a power of two\n", __func__
);
309 /* iova must be page size aligned*/
310 if (iova
& (size
- 1)) {
311 pr_debug("%s: address is not aligned with window size\n", __func__
);
318 static struct fsl_dma_domain
*iommu_alloc_dma_domain(void)
320 struct fsl_dma_domain
*domain
;
322 domain
= kmem_cache_zalloc(fsl_pamu_domain_cache
, GFP_KERNEL
);
326 domain
->stash_id
= ~(u32
)0;
327 domain
->snoop_id
= ~(u32
)0;
328 domain
->win_cnt
= pamu_get_max_subwin_cnt();
329 domain
->geom_size
= 0;
331 INIT_LIST_HEAD(&domain
->devices
);
333 spin_lock_init(&domain
->domain_lock
);
338 static void remove_device_ref(struct device_domain_info
*info
, u32 win_cnt
)
342 list_del(&info
->link
);
343 spin_lock_irqsave(&iommu_lock
, flags
);
345 pamu_free_subwins(info
->liodn
);
346 pamu_disable_liodn(info
->liodn
);
347 spin_unlock_irqrestore(&iommu_lock
, flags
);
348 spin_lock_irqsave(&device_domain_lock
, flags
);
349 info
->dev
->archdata
.iommu_domain
= NULL
;
350 kmem_cache_free(iommu_devinfo_cache
, info
);
351 spin_unlock_irqrestore(&device_domain_lock
, flags
);
354 static void detach_device(struct device
*dev
, struct fsl_dma_domain
*dma_domain
)
356 struct device_domain_info
*info
, *tmp
;
359 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
360 /* Remove the device from the domain device list */
361 list_for_each_entry_safe(info
, tmp
, &dma_domain
->devices
, link
) {
362 if (!dev
|| (info
->dev
== dev
))
363 remove_device_ref(info
, dma_domain
->win_cnt
);
365 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
368 static void attach_device(struct fsl_dma_domain
*dma_domain
, int liodn
, struct device
*dev
)
370 struct device_domain_info
*info
, *old_domain_info
;
373 spin_lock_irqsave(&device_domain_lock
, flags
);
375 * Check here if the device is already attached to domain or not.
376 * If the device is already attached to a domain detach it.
378 old_domain_info
= dev
->archdata
.iommu_domain
;
379 if (old_domain_info
&& old_domain_info
->domain
!= dma_domain
) {
380 spin_unlock_irqrestore(&device_domain_lock
, flags
);
381 detach_device(dev
, old_domain_info
->domain
);
382 spin_lock_irqsave(&device_domain_lock
, flags
);
385 info
= kmem_cache_zalloc(iommu_devinfo_cache
, GFP_ATOMIC
);
389 info
->domain
= dma_domain
;
391 list_add(&info
->link
, &dma_domain
->devices
);
393 * In case of devices with multiple LIODNs just store
394 * the info for the first LIODN as all
395 * LIODNs share the same domain
397 if (!dev
->archdata
.iommu_domain
)
398 dev
->archdata
.iommu_domain
= info
;
399 spin_unlock_irqrestore(&device_domain_lock
, flags
);
403 static phys_addr_t
fsl_pamu_iova_to_phys(struct iommu_domain
*domain
,
406 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
408 if ((iova
< domain
->geometry
.aperture_start
) ||
409 iova
> (domain
->geometry
.aperture_end
))
412 return get_phys_addr(dma_domain
, iova
);
415 static int fsl_pamu_domain_has_cap(struct iommu_domain
*domain
,
418 return cap
== IOMMU_CAP_CACHE_COHERENCY
;
421 static void fsl_pamu_domain_destroy(struct iommu_domain
*domain
)
423 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
427 /* remove all the devices from the device list */
428 detach_device(NULL
, dma_domain
);
430 dma_domain
->enabled
= 0;
431 dma_domain
->mapped
= 0;
433 kmem_cache_free(fsl_pamu_domain_cache
, dma_domain
);
436 static int fsl_pamu_domain_init(struct iommu_domain
*domain
)
438 struct fsl_dma_domain
*dma_domain
;
440 dma_domain
= iommu_alloc_dma_domain();
442 pr_debug("dma_domain allocation failed\n");
445 domain
->priv
= dma_domain
;
446 dma_domain
->iommu_domain
= domain
;
447 /* defaul geometry 64 GB i.e. maximum system address */
448 domain
->geometry
.aperture_start
= 0;
449 domain
->geometry
.aperture_end
= (1ULL << 36) - 1;
450 domain
->geometry
.force_aperture
= true;
455 /* Configure geometry settings for all LIODNs associated with domain */
456 static int pamu_set_domain_geometry(struct fsl_dma_domain
*dma_domain
,
457 struct iommu_domain_geometry
*geom_attr
,
460 struct device_domain_info
*info
;
463 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
464 ret
= pamu_set_liodn(info
->liodn
, info
->dev
, dma_domain
,
473 /* Update stash destination for all LIODNs associated with the domain */
474 static int update_domain_stash(struct fsl_dma_domain
*dma_domain
, u32 val
)
476 struct device_domain_info
*info
;
479 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
480 ret
= update_liodn_stash(info
->liodn
, dma_domain
, val
);
488 /* Update domain mappings for all LIODNs associated with the domain */
489 static int update_domain_mapping(struct fsl_dma_domain
*dma_domain
, u32 wnd_nr
)
491 struct device_domain_info
*info
;
494 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
495 ret
= update_liodn(info
->liodn
, dma_domain
, wnd_nr
);
502 static int disable_domain_win(struct fsl_dma_domain
*dma_domain
, u32 wnd_nr
)
504 struct device_domain_info
*info
;
507 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
508 if (dma_domain
->win_cnt
== 1 && dma_domain
->enabled
) {
509 ret
= pamu_disable_liodn(info
->liodn
);
511 dma_domain
->enabled
= 0;
513 ret
= pamu_disable_spaace(info
->liodn
, wnd_nr
);
520 static void fsl_pamu_window_disable(struct iommu_domain
*domain
, u32 wnd_nr
)
522 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
526 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
527 if (!dma_domain
->win_arr
) {
528 pr_debug("Number of windows not configured\n");
529 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
533 if (wnd_nr
>= dma_domain
->win_cnt
) {
534 pr_debug("Invalid window index\n");
535 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
539 if (dma_domain
->win_arr
[wnd_nr
].valid
) {
540 ret
= disable_domain_win(dma_domain
, wnd_nr
);
542 dma_domain
->win_arr
[wnd_nr
].valid
= 0;
543 dma_domain
->mapped
--;
547 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
551 static int fsl_pamu_window_enable(struct iommu_domain
*domain
, u32 wnd_nr
,
552 phys_addr_t paddr
, u64 size
, int prot
)
554 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
555 struct dma_window
*wnd
;
561 if (prot
& IOMMU_READ
)
562 pamu_prot
|= PAACE_AP_PERMS_QUERY
;
563 if (prot
& IOMMU_WRITE
)
564 pamu_prot
|= PAACE_AP_PERMS_UPDATE
;
566 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
567 if (!dma_domain
->win_arr
) {
568 pr_debug("Number of windows not configured\n");
569 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
573 if (wnd_nr
>= dma_domain
->win_cnt
) {
574 pr_debug("Invalid window index\n");
575 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
579 win_size
= dma_domain
->geom_size
>> ilog2(dma_domain
->win_cnt
);
580 if (size
> win_size
) {
581 pr_debug("Invalid window size \n");
582 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
586 if (dma_domain
->win_cnt
== 1) {
587 if (dma_domain
->enabled
) {
588 pr_debug("Disable the window before updating the mapping\n");
589 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
593 ret
= check_size(size
, domain
->geometry
.aperture_start
);
595 pr_debug("Aperture start not aligned to the size\n");
596 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
601 wnd
= &dma_domain
->win_arr
[wnd_nr
];
605 wnd
->prot
= pamu_prot
;
607 ret
= update_domain_mapping(dma_domain
, wnd_nr
);
610 dma_domain
->mapped
++;
613 pr_debug("Disable the window before updating the mapping\n");
617 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
623 * Attach the LIODN to the DMA domain and configure the geometry
624 * and window mappings.
626 static int handle_attach_device(struct fsl_dma_domain
*dma_domain
,
627 struct device
*dev
, const u32
*liodn
,
631 struct iommu_domain
*domain
= dma_domain
->iommu_domain
;
635 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
636 for (i
= 0; i
< num
; i
++) {
638 /* Ensure that LIODN value is valid */
639 if (liodn
[i
] >= PAACE_NUMBER_ENTRIES
) {
640 pr_debug("Invalid liodn %d, attach device failed for %s\n",
641 liodn
[i
], dev
->of_node
->full_name
);
646 attach_device(dma_domain
, liodn
[i
], dev
);
648 * Check if geometry has already been configured
649 * for the domain. If yes, set the geometry for
652 if (dma_domain
->win_arr
) {
653 u32 win_cnt
= dma_domain
->win_cnt
> 1 ? dma_domain
->win_cnt
: 0;
654 ret
= pamu_set_liodn(liodn
[i
], dev
, dma_domain
,
659 if (dma_domain
->mapped
) {
661 * Create window/subwindow mapping for
664 ret
= map_liodn(liodn
[i
], dma_domain
);
670 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
675 static int fsl_pamu_attach_device(struct iommu_domain
*domain
,
678 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
682 struct pci_dev
*pdev
= NULL
;
683 struct pci_controller
*pci_ctl
;
686 * Use LIODN of the PCI controller while attaching a
689 if (dev_is_pci(dev
)) {
690 pdev
= to_pci_dev(dev
);
691 pci_ctl
= pci_bus_to_host(pdev
->bus
);
693 * make dev point to pci controller device
694 * so we can get the LIODN programmed by
697 dev
= pci_ctl
->parent
;
700 liodn
= of_get_property(dev
->of_node
, "fsl,liodn", &len
);
702 liodn_cnt
= len
/ sizeof(u32
);
703 ret
= handle_attach_device(dma_domain
, dev
,
706 pr_debug("missing fsl,liodn property at %s\n",
707 dev
->of_node
->full_name
);
714 static void fsl_pamu_detach_device(struct iommu_domain
*domain
,
717 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
720 struct pci_dev
*pdev
= NULL
;
721 struct pci_controller
*pci_ctl
;
724 * Use LIODN of the PCI controller while detaching a
727 if (dev_is_pci(dev
)) {
728 pdev
= to_pci_dev(dev
);
729 pci_ctl
= pci_bus_to_host(pdev
->bus
);
731 * make dev point to pci controller device
732 * so we can get the LIODN programmed by
735 dev
= pci_ctl
->parent
;
738 prop
= of_get_property(dev
->of_node
, "fsl,liodn", &len
);
740 detach_device(dev
, dma_domain
);
742 pr_debug("missing fsl,liodn property at %s\n",
743 dev
->of_node
->full_name
);
746 static int configure_domain_geometry(struct iommu_domain
*domain
, void *data
)
748 struct iommu_domain_geometry
*geom_attr
= data
;
749 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
750 dma_addr_t geom_size
;
753 geom_size
= geom_attr
->aperture_end
- geom_attr
->aperture_start
+ 1;
755 * Sanity check the geometry size. Also, we do not support
756 * DMA outside of the geometry.
758 if (check_size(geom_size
, geom_attr
->aperture_start
) ||
759 !geom_attr
->force_aperture
) {
760 pr_debug("Invalid PAMU geometry attributes\n");
764 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
765 if (dma_domain
->enabled
) {
766 pr_debug("Can't set geometry attributes as domain is active\n");
767 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
771 /* Copy the domain geometry information */
772 memcpy(&domain
->geometry
, geom_attr
,
773 sizeof(struct iommu_domain_geometry
));
774 dma_domain
->geom_size
= geom_size
;
776 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
781 /* Set the domain stash attribute */
782 static int configure_domain_stash(struct fsl_dma_domain
*dma_domain
, void *data
)
784 struct pamu_stash_attribute
*stash_attr
= data
;
788 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
790 memcpy(&dma_domain
->dma_stash
, stash_attr
,
791 sizeof(struct pamu_stash_attribute
));
793 dma_domain
->stash_id
= get_stash_id(stash_attr
->cache
,
795 if (dma_domain
->stash_id
== ~(u32
)0) {
796 pr_debug("Invalid stash attributes\n");
797 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
801 ret
= update_domain_stash(dma_domain
, dma_domain
->stash_id
);
803 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
808 /* Configure domain dma state i.e. enable/disable DMA*/
809 static int configure_domain_dma_state(struct fsl_dma_domain
*dma_domain
, bool enable
)
811 struct device_domain_info
*info
;
815 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
817 if (enable
&& !dma_domain
->mapped
) {
818 pr_debug("Can't enable DMA domain without valid mapping\n");
819 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
823 dma_domain
->enabled
= enable
;
824 list_for_each_entry(info
, &dma_domain
->devices
,
826 ret
= (enable
) ? pamu_enable_liodn(info
->liodn
) :
827 pamu_disable_liodn(info
->liodn
);
829 pr_debug("Unable to set dma state for liodn %d",
832 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
837 static int fsl_pamu_set_domain_attr(struct iommu_domain
*domain
,
838 enum iommu_attr attr_type
, void *data
)
840 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
845 case DOMAIN_ATTR_GEOMETRY
:
846 ret
= configure_domain_geometry(domain
, data
);
848 case DOMAIN_ATTR_FSL_PAMU_STASH
:
849 ret
= configure_domain_stash(dma_domain
, data
);
851 case DOMAIN_ATTR_FSL_PAMU_ENABLE
:
852 ret
= configure_domain_dma_state(dma_domain
, *(int *)data
);
855 pr_debug("Unsupported attribute type\n");
863 static int fsl_pamu_get_domain_attr(struct iommu_domain
*domain
,
864 enum iommu_attr attr_type
, void *data
)
866 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
871 case DOMAIN_ATTR_FSL_PAMU_STASH
:
872 memcpy((struct pamu_stash_attribute
*) data
, &dma_domain
->dma_stash
,
873 sizeof(struct pamu_stash_attribute
));
875 case DOMAIN_ATTR_FSL_PAMU_ENABLE
:
876 *(int *)data
= dma_domain
->enabled
;
878 case DOMAIN_ATTR_FSL_PAMUV1
:
879 *(int *)data
= DOMAIN_ATTR_FSL_PAMUV1
;
882 pr_debug("Unsupported attribute type\n");
890 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
892 static struct iommu_group
*get_device_iommu_group(struct device
*dev
)
894 struct iommu_group
*group
;
896 group
= iommu_group_get(dev
);
898 group
= iommu_group_alloc();
903 static bool check_pci_ctl_endpt_part(struct pci_controller
*pci_ctl
)
907 /* Check the PCI controller version number by readding BRR1 register */
908 version
= in_be32(pci_ctl
->cfg_addr
+ (PCI_FSL_BRR1
>> 2));
909 version
&= PCI_FSL_BRR1_VER
;
910 /* If PCI controller version is >= 0x204 we can partition endpoints*/
911 if (version
>= 0x204)
917 /* Get iommu group information from peer devices or devices on the parent bus */
918 static struct iommu_group
*get_shared_pci_device_group(struct pci_dev
*pdev
)
921 struct iommu_group
*group
;
922 struct pci_bus
*bus
= pdev
->bus
;
925 * Traverese the pci bus device list to get
926 * the shared iommu group.
929 list_for_each_entry(tmp
, &bus
->devices
, bus_list
) {
932 group
= iommu_group_get(&tmp
->dev
);
943 static struct iommu_group
*get_pci_device_group(struct pci_dev
*pdev
)
945 struct pci_controller
*pci_ctl
;
946 bool pci_endpt_partioning
;
947 struct iommu_group
*group
= NULL
;
948 struct pci_dev
*bridge
, *dma_pdev
= NULL
;
950 pci_ctl
= pci_bus_to_host(pdev
->bus
);
951 pci_endpt_partioning
= check_pci_ctl_endpt_part(pci_ctl
);
952 /* We can partition PCIe devices so assign device group to the device */
953 if (pci_endpt_partioning
) {
954 bridge
= pci_find_upstream_pcie_bridge(pdev
);
956 if (pci_is_pcie(bridge
))
957 dma_pdev
= pci_get_domain_bus_and_slot(
958 pci_domain_nr(pdev
->bus
),
959 bridge
->subordinate
->number
, 0);
961 dma_pdev
= pci_dev_get(bridge
);
963 dma_pdev
= pci_dev_get(pdev
);
965 /* Account for quirked devices */
966 swap_pci_ref(&dma_pdev
, pci_get_dma_source(dma_pdev
));
969 * If it's a multifunction device that does not support our
970 * required ACS flags, add to the same group as lowest numbered
971 * function that also does not suport the required ACS flags.
973 if (dma_pdev
->multifunction
&&
974 !pci_acs_enabled(dma_pdev
, REQ_ACS_FLAGS
)) {
975 u8 i
, slot
= PCI_SLOT(dma_pdev
->devfn
);
977 for (i
= 0; i
< 8; i
++) {
980 tmp
= pci_get_slot(dma_pdev
->bus
, PCI_DEVFN(slot
, i
));
984 if (!pci_acs_enabled(tmp
, REQ_ACS_FLAGS
)) {
985 swap_pci_ref(&dma_pdev
, tmp
);
993 * Devices on the root bus go through the iommu. If that's not us,
994 * find the next upstream device and test ACS up to the root bus.
995 * Finding the next device may require skipping virtual buses.
997 while (!pci_is_root_bus(dma_pdev
->bus
)) {
998 struct pci_bus
*bus
= dma_pdev
->bus
;
1000 while (!bus
->self
) {
1001 if (!pci_is_root_bus(bus
))
1007 if (pci_acs_path_enabled(bus
->self
, NULL
, REQ_ACS_FLAGS
))
1010 swap_pci_ref(&dma_pdev
, pci_dev_get(bus
->self
));
1014 group
= get_device_iommu_group(&dma_pdev
->dev
);
1015 pci_dev_put(dma_pdev
);
1017 * PCIe controller is not a paritionable entity
1018 * free the controller device iommu_group.
1020 if (pci_ctl
->parent
->iommu_group
)
1021 iommu_group_remove_device(pci_ctl
->parent
);
1024 * All devices connected to the controller will share the
1025 * PCI controllers device group. If this is the first
1026 * device to be probed for the pci controller, copy the
1027 * device group information from the PCI controller device
1028 * node and remove the PCI controller iommu group.
1029 * For subsequent devices, the iommu group information can
1030 * be obtained from sibling devices (i.e. from the bus_devices
1033 if (pci_ctl
->parent
->iommu_group
) {
1034 group
= get_device_iommu_group(pci_ctl
->parent
);
1035 iommu_group_remove_device(pci_ctl
->parent
);
1037 group
= get_shared_pci_device_group(pdev
);
1041 group
= ERR_PTR(-ENODEV
);
1046 static int fsl_pamu_add_device(struct device
*dev
)
1048 struct iommu_group
*group
= ERR_PTR(-ENODEV
);
1049 struct pci_dev
*pdev
;
1054 * For platform devices we allocate a separate group for
1055 * each of the devices.
1057 if (dev_is_pci(dev
)) {
1058 pdev
= to_pci_dev(dev
);
1059 /* Don't create device groups for virtual PCI bridges */
1060 if (pdev
->subordinate
)
1063 group
= get_pci_device_group(pdev
);
1066 prop
= of_get_property(dev
->of_node
, "fsl,liodn", &len
);
1068 group
= get_device_iommu_group(dev
);
1072 return PTR_ERR(group
);
1075 * Check if device has already been added to an iommu group.
1076 * Group could have already been created for a PCI device in
1077 * the iommu_group_get_for_dev path.
1079 if (!dev
->iommu_group
)
1080 ret
= iommu_group_add_device(group
, dev
);
1082 iommu_group_put(group
);
1086 static void fsl_pamu_remove_device(struct device
*dev
)
1088 iommu_group_remove_device(dev
);
1091 static int fsl_pamu_set_windows(struct iommu_domain
*domain
, u32 w_count
)
1093 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
1094 unsigned long flags
;
1097 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
1098 /* Ensure domain is inactive i.e. DMA should be disabled for the domain */
1099 if (dma_domain
->enabled
) {
1100 pr_debug("Can't set geometry attributes as domain is active\n");
1101 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1105 /* Ensure that the geometry has been set for the domain */
1106 if (!dma_domain
->geom_size
) {
1107 pr_debug("Please configure geometry before setting the number of windows\n");
1108 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1113 * Ensure we have valid window count i.e. it should be less than
1114 * maximum permissible limit and should be a power of two.
1116 if (w_count
> pamu_get_max_subwin_cnt() || !is_power_of_2(w_count
)) {
1117 pr_debug("Invalid window count\n");
1118 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1122 ret
= pamu_set_domain_geometry(dma_domain
, &domain
->geometry
,
1123 ((w_count
> 1) ? w_count
: 0));
1125 if (dma_domain
->win_arr
)
1126 kfree(dma_domain
->win_arr
);
1127 dma_domain
->win_arr
= kzalloc(sizeof(struct dma_window
) *
1128 w_count
, GFP_ATOMIC
);
1129 if (!dma_domain
->win_arr
) {
1130 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1133 dma_domain
->win_cnt
= w_count
;
1135 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1140 static u32
fsl_pamu_get_windows(struct iommu_domain
*domain
)
1142 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
1144 return dma_domain
->win_cnt
;
1147 static struct iommu_ops fsl_pamu_ops
= {
1148 .domain_init
= fsl_pamu_domain_init
,
1149 .domain_destroy
= fsl_pamu_domain_destroy
,
1150 .attach_dev
= fsl_pamu_attach_device
,
1151 .detach_dev
= fsl_pamu_detach_device
,
1152 .domain_window_enable
= fsl_pamu_window_enable
,
1153 .domain_window_disable
= fsl_pamu_window_disable
,
1154 .domain_get_windows
= fsl_pamu_get_windows
,
1155 .domain_set_windows
= fsl_pamu_set_windows
,
1156 .iova_to_phys
= fsl_pamu_iova_to_phys
,
1157 .domain_has_cap
= fsl_pamu_domain_has_cap
,
1158 .domain_set_attr
= fsl_pamu_set_domain_attr
,
1159 .domain_get_attr
= fsl_pamu_get_domain_attr
,
1160 .add_device
= fsl_pamu_add_device
,
1161 .remove_device
= fsl_pamu_remove_device
,
1164 int pamu_domain_init()
1168 ret
= iommu_init_mempool();
1172 bus_set_iommu(&platform_bus_type
, &fsl_pamu_ops
);
1173 bus_set_iommu(&pci_bus_type
, &fsl_pamu_ops
);