aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iommu
diff options
context:
space:
mode:
authorCho KyongHo <pullip.cho@samsung.com>2014-05-12 02:15:04 -0400
committerJoerg Roedel <jroedel@suse.de>2014-05-13 13:13:00 -0400
commit66a7ed84b345d676d7f60f0d397705ccb1c5c7e2 (patch)
tree89c5e40a15e75673ca308bd5f14de02fc4f47c17 /drivers/iommu
parenteeb5184bb7a149b539fe725ee8e9cd89b4bf2840 (diff)
iommu/exynos: Apply workaround of caching fault page table entries
This patch contains 2 workaround for the System MMU v3.x. System MMU v3.2 and v3.3 has FLPD cache that caches first level page table entries to reduce page table walking latency. However, the FLPD cache is filled with a first level page table entry even though it is not accessed by a master H/W because System MMU v3.3 speculatively prefetches page table entries that may be accessed in the near future by the master H/W. The prefetched FLPD cache entries are not invalidated by iommu_unmap() because iommu_unmap() only unmaps and invalidates the page table entries that is mapped. Because exynos-iommu driver discards a second level page table when it needs to be replaced with another second level page table or a first level page table entry with 1MB mapping, It is required to invalidate FLPD cache that may contain the first level page table entry that points to the second level page table. Another workaround of System MMU v3.3 is initializing the first level page table entries with the second level page table which is filled with all zeros. This prevents System MMU prefetches 'fault' first level page table entry which may lead page fault on access to 16MiB wide. System MMU 3.x fetches consecutive page table entries by a page table walking to maximize bus utilization and to minimize TLB miss panelty. Unfortunately, functional problem is raised with the fetching behavior because it fetches 'fault' page table entries that specifies no translation information and that a valid translation information will be written to in the near future. The logic in the System MMU generates page fault with the cached fault entries that is no longer coherent with the page table which is updated. There is another workaround that must be implemented by I/O virtual memory manager: any two consecutive I/O virtual memory area must have a hole between the two that is larger than or equal to 128KiB. Also, next I/O virtual memory area must be started from the next 128KiB boundary. 0 128K 256K 384K 512K |-------------|---------------|-----------------|----------------| |area1---------------->|.........hole...........|<--- area2 ----- The constraint is depicted above. The size is selected by the calculation followed: - System MMU can fetch consecutive 64 page table entries at once 64 * 4KiB = 256KiB. This is the size between 128K ~ 384K of the above picture. This style of fetching is 'block fetch'. It fetches the page table entries predefined consecutive page table entries including the entry that is the reason of the page table walking. - System MMU can prefetch upto consecutive 32 page table entries. This is the size between 256K ~ 384K. Signed-off-by: Cho KyongHo <pullip.cho@samsung.com> Signed-off-by: Shaik Ameer Basha <shaik.ameer@samsung.com> Signed-off-by: Joerg Roedel <jroedel@suse.de>
Diffstat (limited to 'drivers/iommu')
-rw-r--r--drivers/iommu/exynos-iommu.c163
1 files changed, 146 insertions, 17 deletions
diff --git a/drivers/iommu/exynos-iommu.c b/drivers/iommu/exynos-iommu.c
index 26fb4d777ae2..82aecd0ce74d 100644
--- a/drivers/iommu/exynos-iommu.c
+++ b/drivers/iommu/exynos-iommu.c
@@ -45,8 +45,12 @@ typedef u32 sysmmu_pte_t;
45#define LPAGE_MASK (~(LPAGE_SIZE - 1)) 45#define LPAGE_MASK (~(LPAGE_SIZE - 1))
46#define SPAGE_MASK (~(SPAGE_SIZE - 1)) 46#define SPAGE_MASK (~(SPAGE_SIZE - 1))
47 47
48#define lv1ent_fault(sent) (((*(sent) & 3) == 0) || ((*(sent) & 3) == 3)) 48#define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \
49#define lv1ent_page(sent) ((*(sent) & 3) == 1) 49 ((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
50#define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK)
51#define lv1ent_page_zero(sent) ((*(sent) & 3) == 1)
52#define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \
53 ((*(sent) & 3) == 1))
50#define lv1ent_section(sent) ((*(sent) & 3) == 2) 54#define lv1ent_section(sent) ((*(sent) & 3) == 2)
51 55
52#define lv2ent_fault(pent) ((*(pent) & 3) == 0) 56#define lv2ent_fault(pent) ((*(pent) & 3) == 0)
@@ -130,6 +134,8 @@ static u32 lv2ent_offset(sysmmu_iova_t iova)
130#define has_sysmmu(dev) (dev->archdata.iommu != NULL) 134#define has_sysmmu(dev) (dev->archdata.iommu != NULL)
131 135
132static struct kmem_cache *lv2table_kmem_cache; 136static struct kmem_cache *lv2table_kmem_cache;
137static sysmmu_pte_t *zero_lv2_table;
138#define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
133 139
134static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova) 140static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
135{ 141{
@@ -515,6 +521,32 @@ static bool exynos_sysmmu_disable(struct device *dev)
515 return disabled; 521 return disabled;
516} 522}
517 523
524static void __sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
525 sysmmu_iova_t iova)
526{
527 if (__raw_sysmmu_version(data) == MAKE_MMU_VER(3, 3))
528 __raw_writel(iova | 0x1, data->sfrbase + REG_MMU_FLUSH_ENTRY);
529}
530
531static void sysmmu_tlb_invalidate_flpdcache(struct device *dev,
532 sysmmu_iova_t iova)
533{
534 unsigned long flags;
535 struct exynos_iommu_owner *owner = dev->archdata.iommu;
536 struct sysmmu_drvdata *data = dev_get_drvdata(owner->sysmmu);
537
538 if (!IS_ERR(data->clk_master))
539 clk_enable(data->clk_master);
540
541 spin_lock_irqsave(&data->lock, flags);
542 if (is_sysmmu_active(data))
543 __sysmmu_tlb_invalidate_flpdcache(data, iova);
544 spin_unlock_irqrestore(&data->lock, flags);
545
546 if (!IS_ERR(data->clk_master))
547 clk_disable(data->clk_master);
548}
549
518static void sysmmu_tlb_invalidate_entry(struct device *dev, sysmmu_iova_t iova, 550static void sysmmu_tlb_invalidate_entry(struct device *dev, sysmmu_iova_t iova,
519 size_t size) 551 size_t size)
520{ 552{
@@ -667,21 +699,32 @@ static inline void pgtable_flush(void *vastart, void *vaend)
667static int exynos_iommu_domain_init(struct iommu_domain *domain) 699static int exynos_iommu_domain_init(struct iommu_domain *domain)
668{ 700{
669 struct exynos_iommu_domain *priv; 701 struct exynos_iommu_domain *priv;
702 int i;
670 703
671 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 704 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
672 if (!priv) 705 if (!priv)
673 return -ENOMEM; 706 return -ENOMEM;
674 707
675 priv->pgtable = (sysmmu_pte_t *)__get_free_pages( 708 priv->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
676 GFP_KERNEL | __GFP_ZERO, 2);
677 if (!priv->pgtable) 709 if (!priv->pgtable)
678 goto err_pgtable; 710 goto err_pgtable;
679 711
680 priv->lv2entcnt = (short *)__get_free_pages( 712 priv->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
681 GFP_KERNEL | __GFP_ZERO, 1);
682 if (!priv->lv2entcnt) 713 if (!priv->lv2entcnt)
683 goto err_counter; 714 goto err_counter;
684 715
716 /* w/a of System MMU v3.3 to prevent caching 1MiB mapping */
717 for (i = 0; i < NUM_LV1ENTRIES; i += 8) {
718 priv->pgtable[i + 0] = ZERO_LV2LINK;
719 priv->pgtable[i + 1] = ZERO_LV2LINK;
720 priv->pgtable[i + 2] = ZERO_LV2LINK;
721 priv->pgtable[i + 3] = ZERO_LV2LINK;
722 priv->pgtable[i + 4] = ZERO_LV2LINK;
723 priv->pgtable[i + 5] = ZERO_LV2LINK;
724 priv->pgtable[i + 6] = ZERO_LV2LINK;
725 priv->pgtable[i + 7] = ZERO_LV2LINK;
726 }
727
685 pgtable_flush(priv->pgtable, priv->pgtable + NUM_LV1ENTRIES); 728 pgtable_flush(priv->pgtable, priv->pgtable + NUM_LV1ENTRIES);
686 729
687 spin_lock_init(&priv->lock); 730 spin_lock_init(&priv->lock);
@@ -794,8 +837,8 @@ static void exynos_iommu_detach_device(struct iommu_domain *domain,
794 dev_err(dev, "%s: No IOMMU is attached\n", __func__); 837 dev_err(dev, "%s: No IOMMU is attached\n", __func__);
795} 838}
796 839
797static sysmmu_pte_t *alloc_lv2entry(sysmmu_pte_t *sent, sysmmu_iova_t iova, 840static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *priv,
798 short *pgcounter) 841 sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
799{ 842{
800 if (lv1ent_section(sent)) { 843 if (lv1ent_section(sent)) {
801 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova); 844 WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
@@ -804,6 +847,7 @@ static sysmmu_pte_t *alloc_lv2entry(sysmmu_pte_t *sent, sysmmu_iova_t iova,
804 847
805 if (lv1ent_fault(sent)) { 848 if (lv1ent_fault(sent)) {
806 sysmmu_pte_t *pent; 849 sysmmu_pte_t *pent;
850 bool need_flush_flpd_cache = lv1ent_zero(sent);
807 851
808 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC); 852 pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
809 BUG_ON((unsigned int)pent & (LV2TABLE_SIZE - 1)); 853 BUG_ON((unsigned int)pent & (LV2TABLE_SIZE - 1));
@@ -814,12 +858,39 @@ static sysmmu_pte_t *alloc_lv2entry(sysmmu_pte_t *sent, sysmmu_iova_t iova,
814 *pgcounter = NUM_LV2ENTRIES; 858 *pgcounter = NUM_LV2ENTRIES;
815 pgtable_flush(pent, pent + NUM_LV2ENTRIES); 859 pgtable_flush(pent, pent + NUM_LV2ENTRIES);
816 pgtable_flush(sent, sent + 1); 860 pgtable_flush(sent, sent + 1);
861
862 /*
863 * If pretched SLPD is a fault SLPD in zero_l2_table, FLPD cache
864 * may caches the address of zero_l2_table. This function
865 * replaces the zero_l2_table with new L2 page table to write
866 * valid mappings.
867 * Accessing the valid area may cause page fault since FLPD
868 * cache may still caches zero_l2_table for the valid area
869 * instead of new L2 page table that have the mapping
870 * information of the valid area
871 * Thus any replacement of zero_l2_table with other valid L2
872 * page table must involve FLPD cache invalidation for System
873 * MMU v3.3.
874 * FLPD cache invalidation is performed with TLB invalidation
875 * by VPN without blocking. It is safe to invalidate TLB without
876 * blocking because the target address of TLB invalidation is
877 * not currently mapped.
878 */
879 if (need_flush_flpd_cache) {
880 struct exynos_iommu_owner *owner;
881 spin_lock(&priv->lock);
882 list_for_each_entry(owner, &priv->clients, client)
883 sysmmu_tlb_invalidate_flpdcache(
884 owner->dev, iova);
885 spin_unlock(&priv->lock);
886 }
817 } 887 }
818 888
819 return page_entry(sent, iova); 889 return page_entry(sent, iova);
820} 890}
821 891
822static int lv1set_section(sysmmu_pte_t *sent, sysmmu_iova_t iova, 892static int lv1set_section(struct exynos_iommu_domain *priv,
893 sysmmu_pte_t *sent, sysmmu_iova_t iova,
823 phys_addr_t paddr, short *pgcnt) 894 phys_addr_t paddr, short *pgcnt)
824{ 895{
825 if (lv1ent_section(sent)) { 896 if (lv1ent_section(sent)) {
@@ -843,6 +914,18 @@ static int lv1set_section(sysmmu_pte_t *sent, sysmmu_iova_t iova,
843 914
844 pgtable_flush(sent, sent + 1); 915 pgtable_flush(sent, sent + 1);
845 916
917 spin_lock(&priv->lock);
918 if (lv1ent_page_zero(sent)) {
919 struct exynos_iommu_owner *owner;
920 /*
921 * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
922 * entry by speculative prefetch of SLPD which has no mapping.
923 */
924 list_for_each_entry(owner, &priv->clients, client)
925 sysmmu_tlb_invalidate_flpdcache(owner->dev, iova);
926 }
927 spin_unlock(&priv->lock);
928
846 return 0; 929 return 0;
847} 930}
848 931
@@ -874,6 +957,32 @@ static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
874 return 0; 957 return 0;
875} 958}
876 959
960/*
961 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
962 *
963 * System MMU v3.x have an advanced logic to improve address translation
964 * performance with caching more page table entries by a page table walk.
965 * However, the logic has a bug that caching fault page table entries and System
966 * MMU reports page fault if the cached fault entry is hit even though the fault
967 * entry is updated to a valid entry after the entry is cached.
968 * To prevent caching fault page table entries which may be updated to valid
969 * entries later, the virtual memory manager should care about the w/a about the
970 * problem. The followings describe w/a.
971 *
972 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
973 * in maximum to prevent misbehavior of System MMU 3.x. (w/a of h/w bug)
974 *
975 * Precisely, any start address of I/O virtual region must be aligned by
976 * the following sizes for System MMU v3.1 and v3.2.
977 * System MMU v3.1: 128KiB
978 * System MMU v3.2: 256KiB
979 *
980 * Because System MMU v3.3 caches page table entries more aggressively, it needs
981 * more w/a.
982 * - Any two consecutive I/O virtual regions must be have a hole of larger size
983 * than or equal size to 128KiB.
984 * - Start address of an I/O virtual region must be aligned by 128KiB.
985 */
877static int exynos_iommu_map(struct iommu_domain *domain, unsigned long l_iova, 986static int exynos_iommu_map(struct iommu_domain *domain, unsigned long l_iova,
878 phys_addr_t paddr, size_t size, int prot) 987 phys_addr_t paddr, size_t size, int prot)
879{ 988{
@@ -890,12 +999,12 @@ static int exynos_iommu_map(struct iommu_domain *domain, unsigned long l_iova,
890 entry = section_entry(priv->pgtable, iova); 999 entry = section_entry(priv->pgtable, iova);
891 1000
892 if (size == SECT_SIZE) { 1001 if (size == SECT_SIZE) {
893 ret = lv1set_section(entry, iova, paddr, 1002 ret = lv1set_section(priv, entry, iova, paddr,
894 &priv->lv2entcnt[lv1ent_offset(iova)]); 1003 &priv->lv2entcnt[lv1ent_offset(iova)]);
895 } else { 1004 } else {
896 sysmmu_pte_t *pent; 1005 sysmmu_pte_t *pent;
897 1006
898 pent = alloc_lv2entry(entry, iova, 1007 pent = alloc_lv2entry(priv, entry, iova,
899 &priv->lv2entcnt[lv1ent_offset(iova)]); 1008 &priv->lv2entcnt[lv1ent_offset(iova)]);
900 1009
901 if (IS_ERR(pent)) 1010 if (IS_ERR(pent))
@@ -914,11 +1023,24 @@ static int exynos_iommu_map(struct iommu_domain *domain, unsigned long l_iova,
914 return ret; 1023 return ret;
915} 1024}
916 1025
1026static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *priv,
1027 sysmmu_iova_t iova, size_t size)
1028{
1029 struct exynos_iommu_owner *owner;
1030 unsigned long flags;
1031
1032 spin_lock_irqsave(&priv->lock, flags);
1033
1034 list_for_each_entry(owner, &priv->clients, client)
1035 sysmmu_tlb_invalidate_entry(owner->dev, iova, size);
1036
1037 spin_unlock_irqrestore(&priv->lock, flags);
1038}
1039
917static size_t exynos_iommu_unmap(struct iommu_domain *domain, 1040static size_t exynos_iommu_unmap(struct iommu_domain *domain,
918 unsigned long l_iova, size_t size) 1041 unsigned long l_iova, size_t size)
919{ 1042{
920 struct exynos_iommu_domain *priv = domain->priv; 1043 struct exynos_iommu_domain *priv = domain->priv;
921 struct exynos_iommu_owner *owner;
922 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova; 1044 sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
923 sysmmu_pte_t *ent; 1045 sysmmu_pte_t *ent;
924 size_t err_pgsize; 1046 size_t err_pgsize;
@@ -936,7 +1058,7 @@ static size_t exynos_iommu_unmap(struct iommu_domain *domain,
936 goto err; 1058 goto err;
937 } 1059 }
938 1060
939 *ent = 0; 1061 *ent = ZERO_LV2LINK; /* w/a for h/w bug in Sysmem MMU v3.3 */
940 pgtable_flush(ent, ent + 1); 1062 pgtable_flush(ent, ent + 1);
941 size = SECT_SIZE; 1063 size = SECT_SIZE;
942 goto done; 1064 goto done;
@@ -979,10 +1101,7 @@ static size_t exynos_iommu_unmap(struct iommu_domain *domain,
979done: 1101done:
980 spin_unlock_irqrestore(&priv->pgtablelock, flags); 1102 spin_unlock_irqrestore(&priv->pgtablelock, flags);
981 1103
982 spin_lock_irqsave(&priv->lock, flags); 1104 exynos_iommu_tlb_invalidate_entry(priv, iova, size);
983 list_for_each_entry(owner, &priv->clients, client)
984 sysmmu_tlb_invalidate_entry(owner->dev, iova, size);
985 spin_unlock_irqrestore(&priv->lock, flags);
986 1105
987 return size; 1106 return size;
988err: 1107err:
@@ -1078,6 +1197,14 @@ static int __init exynos_iommu_init(void)
1078 goto err_reg_driver; 1197 goto err_reg_driver;
1079 } 1198 }
1080 1199
1200 zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
1201 if (zero_lv2_table == NULL) {
1202 pr_err("%s: Failed to allocate zero level2 page table\n",
1203 __func__);
1204 ret = -ENOMEM;
1205 goto err_zero_lv2;
1206 }
1207
1081 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops); 1208 ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
1082 if (ret) { 1209 if (ret) {
1083 pr_err("%s: Failed to register exynos-iommu driver.\n", 1210 pr_err("%s: Failed to register exynos-iommu driver.\n",
@@ -1087,6 +1214,8 @@ static int __init exynos_iommu_init(void)
1087 1214
1088 return 0; 1215 return 0;
1089err_set_iommu: 1216err_set_iommu:
1217 kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
1218err_zero_lv2:
1090 platform_driver_unregister(&exynos_sysmmu_driver); 1219 platform_driver_unregister(&exynos_sysmmu_driver);
1091err_reg_driver: 1220err_reg_driver:
1092 kmem_cache_destroy(lv2table_kmem_cache); 1221 kmem_cache_destroy(lv2table_kmem_cache);