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 "fsl_pamu_domain.h"
24 #include <sysdev/fsl_pci.h>
27 * Global spinlock that needs to be held while
30 static DEFINE_SPINLOCK(iommu_lock
);
32 static struct kmem_cache
*fsl_pamu_domain_cache
;
33 static struct kmem_cache
*iommu_devinfo_cache
;
34 static DEFINE_SPINLOCK(device_domain_lock
);
36 static int __init
iommu_init_mempool(void)
38 fsl_pamu_domain_cache
= kmem_cache_create("fsl_pamu_domain",
39 sizeof(struct fsl_dma_domain
),
43 if (!fsl_pamu_domain_cache
) {
44 pr_debug("Couldn't create fsl iommu_domain cache\n");
48 iommu_devinfo_cache
= kmem_cache_create("iommu_devinfo",
49 sizeof(struct device_domain_info
),
53 if (!iommu_devinfo_cache
) {
54 pr_debug("Couldn't create devinfo cache\n");
55 kmem_cache_destroy(fsl_pamu_domain_cache
);
62 static phys_addr_t
get_phys_addr(struct fsl_dma_domain
*dma_domain
, dma_addr_t iova
)
64 u32 win_cnt
= dma_domain
->win_cnt
;
65 struct dma_window
*win_ptr
= &dma_domain
->win_arr
[0];
66 struct iommu_domain_geometry
*geom
;
68 geom
= &dma_domain
->iommu_domain
->geometry
;
70 if (!win_cnt
|| !dma_domain
->geom_size
) {
71 pr_debug("Number of windows/geometry not configured for the domain\n");
77 dma_addr_t subwin_iova
;
80 subwin_size
= dma_domain
->geom_size
>> ilog2(win_cnt
);
81 subwin_iova
= iova
& ~(subwin_size
- 1);
82 wnd
= (subwin_iova
- geom
->aperture_start
) >> ilog2(subwin_size
);
83 win_ptr
= &dma_domain
->win_arr
[wnd
];
87 return win_ptr
->paddr
+ (iova
& (win_ptr
->size
- 1));
92 static int map_subwins(int liodn
, struct fsl_dma_domain
*dma_domain
)
94 struct dma_window
*sub_win_ptr
= &dma_domain
->win_arr
[0];
96 unsigned long rpn
, flags
;
98 for (i
= 0; i
< dma_domain
->win_cnt
; i
++) {
99 if (sub_win_ptr
[i
].valid
) {
100 rpn
= sub_win_ptr
[i
].paddr
>> PAMU_PAGE_SHIFT
;
101 spin_lock_irqsave(&iommu_lock
, flags
);
102 ret
= pamu_config_spaace(liodn
, dma_domain
->win_cnt
, i
,
106 dma_domain
->snoop_id
,
107 dma_domain
->stash_id
,
109 sub_win_ptr
[i
].prot
);
110 spin_unlock_irqrestore(&iommu_lock
, flags
);
112 pr_debug("SPAACE configuration failed for liodn %d\n",
122 static int map_win(int liodn
, struct fsl_dma_domain
*dma_domain
)
125 struct dma_window
*wnd
= &dma_domain
->win_arr
[0];
126 phys_addr_t wnd_addr
= dma_domain
->iommu_domain
->geometry
.aperture_start
;
129 spin_lock_irqsave(&iommu_lock
, flags
);
130 ret
= pamu_config_ppaace(liodn
, wnd_addr
,
133 wnd
->paddr
>> PAMU_PAGE_SHIFT
,
134 dma_domain
->snoop_id
, dma_domain
->stash_id
,
136 spin_unlock_irqrestore(&iommu_lock
, flags
);
138 pr_debug("PAACE configuration failed for liodn %d\n", liodn
);
143 /* Map the DMA window corresponding to the LIODN */
144 static int map_liodn(int liodn
, struct fsl_dma_domain
*dma_domain
)
146 if (dma_domain
->win_cnt
> 1)
147 return map_subwins(liodn
, dma_domain
);
149 return map_win(liodn
, dma_domain
);
152 /* Update window/subwindow mapping for the LIODN */
153 static int update_liodn(int liodn
, struct fsl_dma_domain
*dma_domain
, u32 wnd_nr
)
156 struct dma_window
*wnd
= &dma_domain
->win_arr
[wnd_nr
];
159 spin_lock_irqsave(&iommu_lock
, flags
);
160 if (dma_domain
->win_cnt
> 1) {
161 ret
= pamu_config_spaace(liodn
, dma_domain
->win_cnt
, wnd_nr
,
164 wnd
->paddr
>> PAMU_PAGE_SHIFT
,
165 dma_domain
->snoop_id
,
166 dma_domain
->stash_id
,
167 (wnd_nr
> 0) ? 1 : 0,
170 pr_debug("Subwindow reconfiguration failed for liodn %d\n",
173 phys_addr_t wnd_addr
;
175 wnd_addr
= dma_domain
->iommu_domain
->geometry
.aperture_start
;
177 ret
= pamu_config_ppaace(liodn
, wnd_addr
,
180 wnd
->paddr
>> PAMU_PAGE_SHIFT
,
181 dma_domain
->snoop_id
, dma_domain
->stash_id
,
184 pr_debug("Window reconfiguration failed for liodn %d\n",
188 spin_unlock_irqrestore(&iommu_lock
, flags
);
193 static int update_liodn_stash(int liodn
, struct fsl_dma_domain
*dma_domain
,
199 spin_lock_irqsave(&iommu_lock
, flags
);
200 if (!dma_domain
->win_arr
) {
201 pr_debug("Windows not configured, stash destination update failed for liodn %d\n",
203 spin_unlock_irqrestore(&iommu_lock
, flags
);
207 for (i
= 0; i
< dma_domain
->win_cnt
; i
++) {
208 ret
= pamu_update_paace_stash(liodn
, i
, val
);
210 pr_debug("Failed to update SPAACE %d field for liodn %d\n ",
212 spin_unlock_irqrestore(&iommu_lock
, flags
);
217 spin_unlock_irqrestore(&iommu_lock
, flags
);
222 /* Set the geometry parameters for a LIODN */
223 static int pamu_set_liodn(int liodn
, struct device
*dev
,
224 struct fsl_dma_domain
*dma_domain
,
225 struct iommu_domain_geometry
*geom_attr
,
228 phys_addr_t window_addr
, window_size
;
229 phys_addr_t subwin_size
;
231 u32 omi_index
= ~(u32
)0;
235 * Configure the omi_index at the geometry setup time.
236 * This is a static value which depends on the type of
237 * device and would not change thereafter.
239 get_ome_index(&omi_index
, dev
);
241 window_addr
= geom_attr
->aperture_start
;
242 window_size
= dma_domain
->geom_size
;
244 spin_lock_irqsave(&iommu_lock
, flags
);
245 ret
= pamu_disable_liodn(liodn
);
247 ret
= pamu_config_ppaace(liodn
, window_addr
, window_size
, omi_index
,
248 0, dma_domain
->snoop_id
,
249 dma_domain
->stash_id
, win_cnt
, 0);
250 spin_unlock_irqrestore(&iommu_lock
, flags
);
252 pr_debug("PAACE configuration failed for liodn %d, win_cnt =%d\n",
258 subwin_size
= window_size
>> ilog2(win_cnt
);
259 for (i
= 0; i
< win_cnt
; i
++) {
260 spin_lock_irqsave(&iommu_lock
, flags
);
261 ret
= pamu_disable_spaace(liodn
, i
);
263 ret
= pamu_config_spaace(liodn
, win_cnt
, i
,
264 subwin_size
, omi_index
,
265 0, dma_domain
->snoop_id
,
266 dma_domain
->stash_id
,
268 spin_unlock_irqrestore(&iommu_lock
, flags
);
270 pr_debug("SPAACE configuration failed for liodn %d\n",
280 static int check_size(u64 size
, dma_addr_t iova
)
283 * Size must be a power of two and at least be equal
286 if ((size
& (size
- 1)) || size
< PAMU_PAGE_SIZE
) {
287 pr_debug("Size too small or not a power of two\n");
291 /* iova must be page size aligned */
292 if (iova
& (size
- 1)) {
293 pr_debug("Address is not aligned with window size\n");
300 static struct fsl_dma_domain
*iommu_alloc_dma_domain(void)
302 struct fsl_dma_domain
*domain
;
304 domain
= kmem_cache_zalloc(fsl_pamu_domain_cache
, GFP_KERNEL
);
308 domain
->stash_id
= ~(u32
)0;
309 domain
->snoop_id
= ~(u32
)0;
310 domain
->win_cnt
= pamu_get_max_subwin_cnt();
311 domain
->geom_size
= 0;
313 INIT_LIST_HEAD(&domain
->devices
);
315 spin_lock_init(&domain
->domain_lock
);
320 static void remove_device_ref(struct device_domain_info
*info
, u32 win_cnt
)
324 list_del(&info
->link
);
325 spin_lock_irqsave(&iommu_lock
, flags
);
327 pamu_free_subwins(info
->liodn
);
328 pamu_disable_liodn(info
->liodn
);
329 spin_unlock_irqrestore(&iommu_lock
, flags
);
330 spin_lock_irqsave(&device_domain_lock
, flags
);
331 info
->dev
->archdata
.iommu_domain
= NULL
;
332 kmem_cache_free(iommu_devinfo_cache
, info
);
333 spin_unlock_irqrestore(&device_domain_lock
, flags
);
336 static void detach_device(struct device
*dev
, struct fsl_dma_domain
*dma_domain
)
338 struct device_domain_info
*info
, *tmp
;
341 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
342 /* Remove the device from the domain device list */
343 list_for_each_entry_safe(info
, tmp
, &dma_domain
->devices
, link
) {
344 if (!dev
|| (info
->dev
== dev
))
345 remove_device_ref(info
, dma_domain
->win_cnt
);
347 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
350 static void attach_device(struct fsl_dma_domain
*dma_domain
, int liodn
, struct device
*dev
)
352 struct device_domain_info
*info
, *old_domain_info
;
355 spin_lock_irqsave(&device_domain_lock
, flags
);
357 * Check here if the device is already attached to domain or not.
358 * If the device is already attached to a domain detach it.
360 old_domain_info
= dev
->archdata
.iommu_domain
;
361 if (old_domain_info
&& old_domain_info
->domain
!= dma_domain
) {
362 spin_unlock_irqrestore(&device_domain_lock
, flags
);
363 detach_device(dev
, old_domain_info
->domain
);
364 spin_lock_irqsave(&device_domain_lock
, flags
);
367 info
= kmem_cache_zalloc(iommu_devinfo_cache
, GFP_ATOMIC
);
371 info
->domain
= dma_domain
;
373 list_add(&info
->link
, &dma_domain
->devices
);
375 * In case of devices with multiple LIODNs just store
376 * the info for the first LIODN as all
377 * LIODNs share the same domain
379 if (!dev
->archdata
.iommu_domain
)
380 dev
->archdata
.iommu_domain
= info
;
381 spin_unlock_irqrestore(&device_domain_lock
, flags
);
384 static phys_addr_t
fsl_pamu_iova_to_phys(struct iommu_domain
*domain
,
387 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
389 if (iova
< domain
->geometry
.aperture_start
||
390 iova
> domain
->geometry
.aperture_end
)
393 return get_phys_addr(dma_domain
, iova
);
396 static bool fsl_pamu_capable(enum iommu_cap cap
)
398 return cap
== IOMMU_CAP_CACHE_COHERENCY
;
401 static void fsl_pamu_domain_destroy(struct iommu_domain
*domain
)
403 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
407 /* remove all the devices from the device list */
408 detach_device(NULL
, dma_domain
);
410 dma_domain
->enabled
= 0;
411 dma_domain
->mapped
= 0;
413 kmem_cache_free(fsl_pamu_domain_cache
, dma_domain
);
416 static int fsl_pamu_domain_init(struct iommu_domain
*domain
)
418 struct fsl_dma_domain
*dma_domain
;
420 dma_domain
= iommu_alloc_dma_domain();
422 pr_debug("dma_domain allocation failed\n");
425 domain
->priv
= dma_domain
;
426 dma_domain
->iommu_domain
= domain
;
427 /* defaul geometry 64 GB i.e. maximum system address */
428 domain
->geometry
.aperture_start
= 0;
429 domain
->geometry
.aperture_end
= (1ULL << 36) - 1;
430 domain
->geometry
.force_aperture
= true;
435 /* Configure geometry settings for all LIODNs associated with domain */
436 static int pamu_set_domain_geometry(struct fsl_dma_domain
*dma_domain
,
437 struct iommu_domain_geometry
*geom_attr
,
440 struct device_domain_info
*info
;
443 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
444 ret
= pamu_set_liodn(info
->liodn
, info
->dev
, dma_domain
,
453 /* Update stash destination for all LIODNs associated with the domain */
454 static int update_domain_stash(struct fsl_dma_domain
*dma_domain
, u32 val
)
456 struct device_domain_info
*info
;
459 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
460 ret
= update_liodn_stash(info
->liodn
, dma_domain
, val
);
468 /* Update domain mappings for all LIODNs associated with the domain */
469 static int update_domain_mapping(struct fsl_dma_domain
*dma_domain
, u32 wnd_nr
)
471 struct device_domain_info
*info
;
474 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
475 ret
= update_liodn(info
->liodn
, dma_domain
, wnd_nr
);
482 static int disable_domain_win(struct fsl_dma_domain
*dma_domain
, u32 wnd_nr
)
484 struct device_domain_info
*info
;
487 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
488 if (dma_domain
->win_cnt
== 1 && dma_domain
->enabled
) {
489 ret
= pamu_disable_liodn(info
->liodn
);
491 dma_domain
->enabled
= 0;
493 ret
= pamu_disable_spaace(info
->liodn
, wnd_nr
);
500 static void fsl_pamu_window_disable(struct iommu_domain
*domain
, u32 wnd_nr
)
502 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
506 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
507 if (!dma_domain
->win_arr
) {
508 pr_debug("Number of windows not configured\n");
509 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
513 if (wnd_nr
>= dma_domain
->win_cnt
) {
514 pr_debug("Invalid window index\n");
515 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
519 if (dma_domain
->win_arr
[wnd_nr
].valid
) {
520 ret
= disable_domain_win(dma_domain
, wnd_nr
);
522 dma_domain
->win_arr
[wnd_nr
].valid
= 0;
523 dma_domain
->mapped
--;
527 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
530 static int fsl_pamu_window_enable(struct iommu_domain
*domain
, u32 wnd_nr
,
531 phys_addr_t paddr
, u64 size
, int prot
)
533 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
534 struct dma_window
*wnd
;
540 if (prot
& IOMMU_READ
)
541 pamu_prot
|= PAACE_AP_PERMS_QUERY
;
542 if (prot
& IOMMU_WRITE
)
543 pamu_prot
|= PAACE_AP_PERMS_UPDATE
;
545 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
546 if (!dma_domain
->win_arr
) {
547 pr_debug("Number of windows not configured\n");
548 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
552 if (wnd_nr
>= dma_domain
->win_cnt
) {
553 pr_debug("Invalid window index\n");
554 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
558 win_size
= dma_domain
->geom_size
>> ilog2(dma_domain
->win_cnt
);
559 if (size
> win_size
) {
560 pr_debug("Invalid window size\n");
561 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
565 if (dma_domain
->win_cnt
== 1) {
566 if (dma_domain
->enabled
) {
567 pr_debug("Disable the window before updating the mapping\n");
568 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
572 ret
= check_size(size
, domain
->geometry
.aperture_start
);
574 pr_debug("Aperture start not aligned to the size\n");
575 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
580 wnd
= &dma_domain
->win_arr
[wnd_nr
];
584 wnd
->prot
= pamu_prot
;
586 ret
= update_domain_mapping(dma_domain
, wnd_nr
);
589 dma_domain
->mapped
++;
592 pr_debug("Disable the window before updating the mapping\n");
596 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
602 * Attach the LIODN to the DMA domain and configure the geometry
603 * and window mappings.
605 static int handle_attach_device(struct fsl_dma_domain
*dma_domain
,
606 struct device
*dev
, const u32
*liodn
,
610 struct iommu_domain
*domain
= dma_domain
->iommu_domain
;
614 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
615 for (i
= 0; i
< num
; i
++) {
616 /* Ensure that LIODN value is valid */
617 if (liodn
[i
] >= PAACE_NUMBER_ENTRIES
) {
618 pr_debug("Invalid liodn %d, attach device failed for %s\n",
619 liodn
[i
], dev
->of_node
->full_name
);
624 attach_device(dma_domain
, liodn
[i
], dev
);
626 * Check if geometry has already been configured
627 * for the domain. If yes, set the geometry for
630 if (dma_domain
->win_arr
) {
631 u32 win_cnt
= dma_domain
->win_cnt
> 1 ? dma_domain
->win_cnt
: 0;
633 ret
= pamu_set_liodn(liodn
[i
], dev
, dma_domain
,
634 &domain
->geometry
, win_cnt
);
637 if (dma_domain
->mapped
) {
639 * Create window/subwindow mapping for
642 ret
= map_liodn(liodn
[i
], dma_domain
);
648 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
653 static int fsl_pamu_attach_device(struct iommu_domain
*domain
,
656 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
660 struct pci_dev
*pdev
= NULL
;
661 struct pci_controller
*pci_ctl
;
664 * Use LIODN of the PCI controller while attaching a
667 if (dev_is_pci(dev
)) {
668 pdev
= to_pci_dev(dev
);
669 pci_ctl
= pci_bus_to_host(pdev
->bus
);
671 * make dev point to pci controller device
672 * so we can get the LIODN programmed by
675 dev
= pci_ctl
->parent
;
678 liodn
= of_get_property(dev
->of_node
, "fsl,liodn", &len
);
680 liodn_cnt
= len
/ sizeof(u32
);
681 ret
= handle_attach_device(dma_domain
, dev
, liodn
, liodn_cnt
);
683 pr_debug("missing fsl,liodn property at %s\n",
684 dev
->of_node
->full_name
);
691 static void fsl_pamu_detach_device(struct iommu_domain
*domain
,
694 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
697 struct pci_dev
*pdev
= NULL
;
698 struct pci_controller
*pci_ctl
;
701 * Use LIODN of the PCI controller while detaching a
704 if (dev_is_pci(dev
)) {
705 pdev
= to_pci_dev(dev
);
706 pci_ctl
= pci_bus_to_host(pdev
->bus
);
708 * make dev point to pci controller device
709 * so we can get the LIODN programmed by
712 dev
= pci_ctl
->parent
;
715 prop
= of_get_property(dev
->of_node
, "fsl,liodn", &len
);
717 detach_device(dev
, dma_domain
);
719 pr_debug("missing fsl,liodn property at %s\n",
720 dev
->of_node
->full_name
);
723 static int configure_domain_geometry(struct iommu_domain
*domain
, void *data
)
725 struct iommu_domain_geometry
*geom_attr
= data
;
726 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
727 dma_addr_t geom_size
;
730 geom_size
= geom_attr
->aperture_end
- geom_attr
->aperture_start
+ 1;
732 * Sanity check the geometry size. Also, we do not support
733 * DMA outside of the geometry.
735 if (check_size(geom_size
, geom_attr
->aperture_start
) ||
736 !geom_attr
->force_aperture
) {
737 pr_debug("Invalid PAMU geometry attributes\n");
741 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
742 if (dma_domain
->enabled
) {
743 pr_debug("Can't set geometry attributes as domain is active\n");
744 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
748 /* Copy the domain geometry information */
749 memcpy(&domain
->geometry
, geom_attr
,
750 sizeof(struct iommu_domain_geometry
));
751 dma_domain
->geom_size
= geom_size
;
753 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
758 /* Set the domain stash attribute */
759 static int configure_domain_stash(struct fsl_dma_domain
*dma_domain
, void *data
)
761 struct pamu_stash_attribute
*stash_attr
= data
;
765 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
767 memcpy(&dma_domain
->dma_stash
, stash_attr
,
768 sizeof(struct pamu_stash_attribute
));
770 dma_domain
->stash_id
= get_stash_id(stash_attr
->cache
,
772 if (dma_domain
->stash_id
== ~(u32
)0) {
773 pr_debug("Invalid stash attributes\n");
774 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
778 ret
= update_domain_stash(dma_domain
, dma_domain
->stash_id
);
780 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
785 /* Configure domain dma state i.e. enable/disable DMA */
786 static int configure_domain_dma_state(struct fsl_dma_domain
*dma_domain
, bool enable
)
788 struct device_domain_info
*info
;
792 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
794 if (enable
&& !dma_domain
->mapped
) {
795 pr_debug("Can't enable DMA domain without valid mapping\n");
796 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
800 dma_domain
->enabled
= enable
;
801 list_for_each_entry(info
, &dma_domain
->devices
, link
) {
802 ret
= (enable
) ? pamu_enable_liodn(info
->liodn
) :
803 pamu_disable_liodn(info
->liodn
);
805 pr_debug("Unable to set dma state for liodn %d",
808 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
813 static int fsl_pamu_set_domain_attr(struct iommu_domain
*domain
,
814 enum iommu_attr attr_type
, void *data
)
816 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
820 case DOMAIN_ATTR_GEOMETRY
:
821 ret
= configure_domain_geometry(domain
, data
);
823 case DOMAIN_ATTR_FSL_PAMU_STASH
:
824 ret
= configure_domain_stash(dma_domain
, data
);
826 case DOMAIN_ATTR_FSL_PAMU_ENABLE
:
827 ret
= configure_domain_dma_state(dma_domain
, *(int *)data
);
830 pr_debug("Unsupported attribute type\n");
838 static int fsl_pamu_get_domain_attr(struct iommu_domain
*domain
,
839 enum iommu_attr attr_type
, void *data
)
841 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
845 case DOMAIN_ATTR_FSL_PAMU_STASH
:
846 memcpy(data
, &dma_domain
->dma_stash
,
847 sizeof(struct pamu_stash_attribute
));
849 case DOMAIN_ATTR_FSL_PAMU_ENABLE
:
850 *(int *)data
= dma_domain
->enabled
;
852 case DOMAIN_ATTR_FSL_PAMUV1
:
853 *(int *)data
= DOMAIN_ATTR_FSL_PAMUV1
;
856 pr_debug("Unsupported attribute type\n");
864 static struct iommu_group
*get_device_iommu_group(struct device
*dev
)
866 struct iommu_group
*group
;
868 group
= iommu_group_get(dev
);
870 group
= iommu_group_alloc();
875 static bool check_pci_ctl_endpt_part(struct pci_controller
*pci_ctl
)
879 /* Check the PCI controller version number by readding BRR1 register */
880 version
= in_be32(pci_ctl
->cfg_addr
+ (PCI_FSL_BRR1
>> 2));
881 version
&= PCI_FSL_BRR1_VER
;
882 /* If PCI controller version is >= 0x204 we can partition endpoints */
883 return version
>= 0x204;
886 /* Get iommu group information from peer devices or devices on the parent bus */
887 static struct iommu_group
*get_shared_pci_device_group(struct pci_dev
*pdev
)
890 struct iommu_group
*group
;
891 struct pci_bus
*bus
= pdev
->bus
;
894 * Traverese the pci bus device list to get
895 * the shared iommu group.
898 list_for_each_entry(tmp
, &bus
->devices
, bus_list
) {
901 group
= iommu_group_get(&tmp
->dev
);
912 static struct iommu_group
*get_pci_device_group(struct pci_dev
*pdev
)
914 struct pci_controller
*pci_ctl
;
915 bool pci_endpt_partioning
;
916 struct iommu_group
*group
= NULL
;
918 pci_ctl
= pci_bus_to_host(pdev
->bus
);
919 pci_endpt_partioning
= check_pci_ctl_endpt_part(pci_ctl
);
920 /* We can partition PCIe devices so assign device group to the device */
921 if (pci_endpt_partioning
) {
922 group
= iommu_group_get_for_dev(&pdev
->dev
);
925 * PCIe controller is not a paritionable entity
926 * free the controller device iommu_group.
928 if (pci_ctl
->parent
->iommu_group
)
929 iommu_group_remove_device(pci_ctl
->parent
);
932 * All devices connected to the controller will share the
933 * PCI controllers device group. If this is the first
934 * device to be probed for the pci controller, copy the
935 * device group information from the PCI controller device
936 * node and remove the PCI controller iommu group.
937 * For subsequent devices, the iommu group information can
938 * be obtained from sibling devices (i.e. from the bus_devices
941 if (pci_ctl
->parent
->iommu_group
) {
942 group
= get_device_iommu_group(pci_ctl
->parent
);
943 iommu_group_remove_device(pci_ctl
->parent
);
945 group
= get_shared_pci_device_group(pdev
);
950 group
= ERR_PTR(-ENODEV
);
955 static int fsl_pamu_add_device(struct device
*dev
)
957 struct iommu_group
*group
= ERR_PTR(-ENODEV
);
958 struct pci_dev
*pdev
;
963 * For platform devices we allocate a separate group for
964 * each of the devices.
966 if (dev_is_pci(dev
)) {
967 pdev
= to_pci_dev(dev
);
968 /* Don't create device groups for virtual PCI bridges */
969 if (pdev
->subordinate
)
972 group
= get_pci_device_group(pdev
);
975 prop
= of_get_property(dev
->of_node
, "fsl,liodn", &len
);
977 group
= get_device_iommu_group(dev
);
981 return PTR_ERR(group
);
984 * Check if device has already been added to an iommu group.
985 * Group could have already been created for a PCI device in
986 * the iommu_group_get_for_dev path.
988 if (!dev
->iommu_group
)
989 ret
= iommu_group_add_device(group
, dev
);
991 iommu_group_put(group
);
995 static void fsl_pamu_remove_device(struct device
*dev
)
997 iommu_group_remove_device(dev
);
1000 static int fsl_pamu_set_windows(struct iommu_domain
*domain
, u32 w_count
)
1002 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
1003 unsigned long flags
;
1006 spin_lock_irqsave(&dma_domain
->domain_lock
, flags
);
1007 /* Ensure domain is inactive i.e. DMA should be disabled for the domain */
1008 if (dma_domain
->enabled
) {
1009 pr_debug("Can't set geometry attributes as domain is active\n");
1010 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1014 /* Ensure that the geometry has been set for the domain */
1015 if (!dma_domain
->geom_size
) {
1016 pr_debug("Please configure geometry before setting the number of windows\n");
1017 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1022 * Ensure we have valid window count i.e. it should be less than
1023 * maximum permissible limit and should be a power of two.
1025 if (w_count
> pamu_get_max_subwin_cnt() || !is_power_of_2(w_count
)) {
1026 pr_debug("Invalid window count\n");
1027 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1031 ret
= pamu_set_domain_geometry(dma_domain
, &domain
->geometry
,
1032 w_count
> 1 ? w_count
: 0);
1034 kfree(dma_domain
->win_arr
);
1035 dma_domain
->win_arr
= kcalloc(w_count
,
1036 sizeof(*dma_domain
->win_arr
),
1038 if (!dma_domain
->win_arr
) {
1039 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1042 dma_domain
->win_cnt
= w_count
;
1044 spin_unlock_irqrestore(&dma_domain
->domain_lock
, flags
);
1049 static u32
fsl_pamu_get_windows(struct iommu_domain
*domain
)
1051 struct fsl_dma_domain
*dma_domain
= domain
->priv
;
1053 return dma_domain
->win_cnt
;
1056 static const struct iommu_ops fsl_pamu_ops
= {
1057 .capable
= fsl_pamu_capable
,
1058 .domain_init
= fsl_pamu_domain_init
,
1059 .domain_destroy
= fsl_pamu_domain_destroy
,
1060 .attach_dev
= fsl_pamu_attach_device
,
1061 .detach_dev
= fsl_pamu_detach_device
,
1062 .domain_window_enable
= fsl_pamu_window_enable
,
1063 .domain_window_disable
= fsl_pamu_window_disable
,
1064 .domain_get_windows
= fsl_pamu_get_windows
,
1065 .domain_set_windows
= fsl_pamu_set_windows
,
1066 .iova_to_phys
= fsl_pamu_iova_to_phys
,
1067 .domain_set_attr
= fsl_pamu_set_domain_attr
,
1068 .domain_get_attr
= fsl_pamu_get_domain_attr
,
1069 .add_device
= fsl_pamu_add_device
,
1070 .remove_device
= fsl_pamu_remove_device
,
1073 int __init
pamu_domain_init(void)
1077 ret
= iommu_init_mempool();
1081 bus_set_iommu(&platform_bus_type
, &fsl_pamu_ops
);
1082 bus_set_iommu(&pci_bus_type
, &fsl_pamu_ops
);