diff options
author | Joerg Roedel <jroedel@suse.de> | 2014-09-25 09:34:23 -0400 |
---|---|---|
committer | Joerg Roedel <jroedel@suse.de> | 2014-09-25 09:34:23 -0400 |
commit | dedd9431484a826f85667967ef0c5954b12c3537 (patch) | |
tree | a8be2d419ace3eaab153185f9fab1952f3875db7 | |
parent | 0f33be009b89d2268e94194dc4fd01a7851b6d51 (diff) | |
parent | ccd359f219bee914501a8892b148e2a1315066d3 (diff) |
Merge branch 'for-joerg/arm-smmu/updates' of git://git.kernel.org/pub/scm/linux/kernel/git/will/linux into arm/smmu
-rw-r--r-- | Documentation/devicetree/bindings/iommu/arm,smmu.txt | 1 | ||||
-rw-r--r-- | drivers/iommu/arm-smmu.c | 201 |
2 files changed, 121 insertions, 81 deletions
diff --git a/Documentation/devicetree/bindings/iommu/arm,smmu.txt b/Documentation/devicetree/bindings/iommu/arm,smmu.txt index 2d0f7cd867ea..06760503a819 100644 --- a/Documentation/devicetree/bindings/iommu/arm,smmu.txt +++ b/Documentation/devicetree/bindings/iommu/arm,smmu.txt | |||
@@ -14,6 +14,7 @@ conditions. | |||
14 | "arm,smmu-v1" | 14 | "arm,smmu-v1" |
15 | "arm,smmu-v2" | 15 | "arm,smmu-v2" |
16 | "arm,mmu-400" | 16 | "arm,mmu-400" |
17 | "arm,mmu-401" | ||
17 | "arm,mmu-500" | 18 | "arm,mmu-500" |
18 | 19 | ||
19 | depending on the particular implementation and/or the | 20 | depending on the particular implementation and/or the |
diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c index a83cc2a2a2ca..37dc3dd0df96 100644 --- a/drivers/iommu/arm-smmu.c +++ b/drivers/iommu/arm-smmu.c | |||
@@ -24,7 +24,7 @@ | |||
24 | * - v7/v8 long-descriptor format | 24 | * - v7/v8 long-descriptor format |
25 | * - Non-secure access to the SMMU | 25 | * - Non-secure access to the SMMU |
26 | * - 4k and 64k pages, with contiguous pte hints. | 26 | * - 4k and 64k pages, with contiguous pte hints. |
27 | * - Up to 42-bit addressing (dependent on VA_BITS) | 27 | * - Up to 48-bit addressing (dependent on VA_BITS) |
28 | * - Context fault reporting | 28 | * - Context fault reporting |
29 | */ | 29 | */ |
30 | 30 | ||
@@ -59,7 +59,7 @@ | |||
59 | 59 | ||
60 | /* SMMU global address space */ | 60 | /* SMMU global address space */ |
61 | #define ARM_SMMU_GR0(smmu) ((smmu)->base) | 61 | #define ARM_SMMU_GR0(smmu) ((smmu)->base) |
62 | #define ARM_SMMU_GR1(smmu) ((smmu)->base + (smmu)->pagesize) | 62 | #define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift)) |
63 | 63 | ||
64 | /* | 64 | /* |
65 | * SMMU global address space with conditional offset to access secure | 65 | * SMMU global address space with conditional offset to access secure |
@@ -224,7 +224,7 @@ | |||
224 | 224 | ||
225 | /* Translation context bank */ | 225 | /* Translation context bank */ |
226 | #define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1)) | 226 | #define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1)) |
227 | #define ARM_SMMU_CB(smmu, n) ((n) * (smmu)->pagesize) | 227 | #define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift)) |
228 | 228 | ||
229 | #define ARM_SMMU_CB_SCTLR 0x0 | 229 | #define ARM_SMMU_CB_SCTLR 0x0 |
230 | #define ARM_SMMU_CB_RESUME 0x8 | 230 | #define ARM_SMMU_CB_RESUME 0x8 |
@@ -326,6 +326,16 @@ | |||
326 | 326 | ||
327 | #define FSYNR0_WNR (1 << 4) | 327 | #define FSYNR0_WNR (1 << 4) |
328 | 328 | ||
329 | static int force_stage; | ||
330 | module_param_named(force_stage, force_stage, int, S_IRUGO | S_IWUSR); | ||
331 | MODULE_PARM_DESC(force_stage, | ||
332 | "Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation."); | ||
333 | |||
334 | enum arm_smmu_arch_version { | ||
335 | ARM_SMMU_V1 = 1, | ||
336 | ARM_SMMU_V2, | ||
337 | }; | ||
338 | |||
329 | struct arm_smmu_smr { | 339 | struct arm_smmu_smr { |
330 | u8 idx; | 340 | u8 idx; |
331 | u16 mask; | 341 | u16 mask; |
@@ -349,7 +359,7 @@ struct arm_smmu_device { | |||
349 | 359 | ||
350 | void __iomem *base; | 360 | void __iomem *base; |
351 | unsigned long size; | 361 | unsigned long size; |
352 | unsigned long pagesize; | 362 | unsigned long pgshift; |
353 | 363 | ||
354 | #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0) | 364 | #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0) |
355 | #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1) | 365 | #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1) |
@@ -360,7 +370,7 @@ struct arm_smmu_device { | |||
360 | 370 | ||
361 | #define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0) | 371 | #define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0) |
362 | u32 options; | 372 | u32 options; |
363 | int version; | 373 | enum arm_smmu_arch_version version; |
364 | 374 | ||
365 | u32 num_context_banks; | 375 | u32 num_context_banks; |
366 | u32 num_s2_context_banks; | 376 | u32 num_s2_context_banks; |
@@ -370,8 +380,9 @@ struct arm_smmu_device { | |||
370 | u32 num_mapping_groups; | 380 | u32 num_mapping_groups; |
371 | DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS); | 381 | DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS); |
372 | 382 | ||
373 | unsigned long input_size; | 383 | unsigned long s1_input_size; |
374 | unsigned long s1_output_size; | 384 | unsigned long s1_output_size; |
385 | unsigned long s2_input_size; | ||
375 | unsigned long s2_output_size; | 386 | unsigned long s2_output_size; |
376 | 387 | ||
377 | u32 num_global_irqs; | 388 | u32 num_global_irqs; |
@@ -426,17 +437,17 @@ static void parse_driver_options(struct arm_smmu_device *smmu) | |||
426 | } while (arm_smmu_options[++i].opt); | 437 | } while (arm_smmu_options[++i].opt); |
427 | } | 438 | } |
428 | 439 | ||
429 | static struct device *dev_get_master_dev(struct device *dev) | 440 | static struct device_node *dev_get_dev_node(struct device *dev) |
430 | { | 441 | { |
431 | if (dev_is_pci(dev)) { | 442 | if (dev_is_pci(dev)) { |
432 | struct pci_bus *bus = to_pci_dev(dev)->bus; | 443 | struct pci_bus *bus = to_pci_dev(dev)->bus; |
433 | 444 | ||
434 | while (!pci_is_root_bus(bus)) | 445 | while (!pci_is_root_bus(bus)) |
435 | bus = bus->parent; | 446 | bus = bus->parent; |
436 | return bus->bridge->parent; | 447 | return bus->bridge->parent->of_node; |
437 | } | 448 | } |
438 | 449 | ||
439 | return dev; | 450 | return dev->of_node; |
440 | } | 451 | } |
441 | 452 | ||
442 | static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu, | 453 | static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu, |
@@ -461,15 +472,17 @@ static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu, | |||
461 | } | 472 | } |
462 | 473 | ||
463 | static struct arm_smmu_master_cfg * | 474 | static struct arm_smmu_master_cfg * |
464 | find_smmu_master_cfg(struct arm_smmu_device *smmu, struct device *dev) | 475 | find_smmu_master_cfg(struct device *dev) |
465 | { | 476 | { |
466 | struct arm_smmu_master *master; | 477 | struct arm_smmu_master_cfg *cfg = NULL; |
478 | struct iommu_group *group = iommu_group_get(dev); | ||
467 | 479 | ||
468 | if (dev_is_pci(dev)) | 480 | if (group) { |
469 | return dev->archdata.iommu; | 481 | cfg = iommu_group_get_iommudata(group); |
482 | iommu_group_put(group); | ||
483 | } | ||
470 | 484 | ||
471 | master = find_smmu_master(smmu, dev->of_node); | 485 | return cfg; |
472 | return master ? &master->cfg : NULL; | ||
473 | } | 486 | } |
474 | 487 | ||
475 | static int insert_smmu_master(struct arm_smmu_device *smmu, | 488 | static int insert_smmu_master(struct arm_smmu_device *smmu, |
@@ -545,7 +558,7 @@ static struct arm_smmu_device *find_smmu_for_device(struct device *dev) | |||
545 | { | 558 | { |
546 | struct arm_smmu_device *smmu; | 559 | struct arm_smmu_device *smmu; |
547 | struct arm_smmu_master *master = NULL; | 560 | struct arm_smmu_master *master = NULL; |
548 | struct device_node *dev_node = dev_get_master_dev(dev)->of_node; | 561 | struct device_node *dev_node = dev_get_dev_node(dev); |
549 | 562 | ||
550 | spin_lock(&arm_smmu_devices_lock); | 563 | spin_lock(&arm_smmu_devices_lock); |
551 | list_for_each_entry(smmu, &arm_smmu_devices, list) { | 564 | list_for_each_entry(smmu, &arm_smmu_devices, list) { |
@@ -729,7 +742,7 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain) | |||
729 | 742 | ||
730 | /* CBAR */ | 743 | /* CBAR */ |
731 | reg = cfg->cbar; | 744 | reg = cfg->cbar; |
732 | if (smmu->version == 1) | 745 | if (smmu->version == ARM_SMMU_V1) |
733 | reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT; | 746 | reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT; |
734 | 747 | ||
735 | /* | 748 | /* |
@@ -744,7 +757,7 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain) | |||
744 | } | 757 | } |
745 | writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx)); | 758 | writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx)); |
746 | 759 | ||
747 | if (smmu->version > 1) { | 760 | if (smmu->version > ARM_SMMU_V1) { |
748 | /* CBA2R */ | 761 | /* CBA2R */ |
749 | #ifdef CONFIG_64BIT | 762 | #ifdef CONFIG_64BIT |
750 | reg = CBA2R_RW64_64BIT; | 763 | reg = CBA2R_RW64_64BIT; |
@@ -755,7 +768,7 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain) | |||
755 | gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx)); | 768 | gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx)); |
756 | 769 | ||
757 | /* TTBCR2 */ | 770 | /* TTBCR2 */ |
758 | switch (smmu->input_size) { | 771 | switch (smmu->s1_input_size) { |
759 | case 32: | 772 | case 32: |
760 | reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT); | 773 | reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT); |
761 | break; | 774 | break; |
@@ -817,14 +830,14 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain) | |||
817 | * TTBCR | 830 | * TTBCR |
818 | * We use long descriptor, with inner-shareable WBWA tables in TTBR0. | 831 | * We use long descriptor, with inner-shareable WBWA tables in TTBR0. |
819 | */ | 832 | */ |
820 | if (smmu->version > 1) { | 833 | if (smmu->version > ARM_SMMU_V1) { |
821 | if (PAGE_SIZE == SZ_4K) | 834 | if (PAGE_SIZE == SZ_4K) |
822 | reg = TTBCR_TG0_4K; | 835 | reg = TTBCR_TG0_4K; |
823 | else | 836 | else |
824 | reg = TTBCR_TG0_64K; | 837 | reg = TTBCR_TG0_64K; |
825 | 838 | ||
826 | if (!stage1) { | 839 | if (!stage1) { |
827 | reg |= (64 - smmu->s1_output_size) << TTBCR_T0SZ_SHIFT; | 840 | reg |= (64 - smmu->s2_input_size) << TTBCR_T0SZ_SHIFT; |
828 | 841 | ||
829 | switch (smmu->s2_output_size) { | 842 | switch (smmu->s2_output_size) { |
830 | case 32: | 843 | case 32: |
@@ -847,7 +860,7 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain) | |||
847 | break; | 860 | break; |
848 | } | 861 | } |
849 | } else { | 862 | } else { |
850 | reg |= (64 - smmu->input_size) << TTBCR_T0SZ_SHIFT; | 863 | reg |= (64 - smmu->s1_input_size) << TTBCR_T0SZ_SHIFT; |
851 | } | 864 | } |
852 | } else { | 865 | } else { |
853 | reg = 0; | 866 | reg = 0; |
@@ -914,7 +927,7 @@ static int arm_smmu_init_domain_context(struct iommu_domain *domain, | |||
914 | goto out_unlock; | 927 | goto out_unlock; |
915 | 928 | ||
916 | cfg->cbndx = ret; | 929 | cfg->cbndx = ret; |
917 | if (smmu->version == 1) { | 930 | if (smmu->version == ARM_SMMU_V1) { |
918 | cfg->irptndx = atomic_inc_return(&smmu->irptndx); | 931 | cfg->irptndx = atomic_inc_return(&smmu->irptndx); |
919 | cfg->irptndx %= smmu->num_context_irqs; | 932 | cfg->irptndx %= smmu->num_context_irqs; |
920 | } else { | 933 | } else { |
@@ -1151,9 +1164,10 @@ static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain, | |||
1151 | struct arm_smmu_device *smmu = smmu_domain->smmu; | 1164 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
1152 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); | 1165 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
1153 | 1166 | ||
1167 | /* Devices in an IOMMU group may already be configured */ | ||
1154 | ret = arm_smmu_master_configure_smrs(smmu, cfg); | 1168 | ret = arm_smmu_master_configure_smrs(smmu, cfg); |
1155 | if (ret) | 1169 | if (ret) |
1156 | return ret; | 1170 | return ret == -EEXIST ? 0 : ret; |
1157 | 1171 | ||
1158 | for (i = 0; i < cfg->num_streamids; ++i) { | 1172 | for (i = 0; i < cfg->num_streamids; ++i) { |
1159 | u32 idx, s2cr; | 1173 | u32 idx, s2cr; |
@@ -1174,6 +1188,10 @@ static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain, | |||
1174 | struct arm_smmu_device *smmu = smmu_domain->smmu; | 1188 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
1175 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); | 1189 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
1176 | 1190 | ||
1191 | /* An IOMMU group is torn down by the first device to be removed */ | ||
1192 | if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs) | ||
1193 | return; | ||
1194 | |||
1177 | /* | 1195 | /* |
1178 | * We *must* clear the S2CR first, because freeing the SMR means | 1196 | * We *must* clear the S2CR first, because freeing the SMR means |
1179 | * that it can be re-allocated immediately. | 1197 | * that it can be re-allocated immediately. |
@@ -1195,12 +1213,17 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) | |||
1195 | struct arm_smmu_device *smmu, *dom_smmu; | 1213 | struct arm_smmu_device *smmu, *dom_smmu; |
1196 | struct arm_smmu_master_cfg *cfg; | 1214 | struct arm_smmu_master_cfg *cfg; |
1197 | 1215 | ||
1198 | smmu = dev_get_master_dev(dev)->archdata.iommu; | 1216 | smmu = find_smmu_for_device(dev); |
1199 | if (!smmu) { | 1217 | if (!smmu) { |
1200 | dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n"); | 1218 | dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n"); |
1201 | return -ENXIO; | 1219 | return -ENXIO; |
1202 | } | 1220 | } |
1203 | 1221 | ||
1222 | if (dev->archdata.iommu) { | ||
1223 | dev_err(dev, "already attached to IOMMU domain\n"); | ||
1224 | return -EEXIST; | ||
1225 | } | ||
1226 | |||
1204 | /* | 1227 | /* |
1205 | * Sanity check the domain. We don't support domains across | 1228 | * Sanity check the domain. We don't support domains across |
1206 | * different SMMUs. | 1229 | * different SMMUs. |
@@ -1223,11 +1246,14 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) | |||
1223 | } | 1246 | } |
1224 | 1247 | ||
1225 | /* Looks ok, so add the device to the domain */ | 1248 | /* Looks ok, so add the device to the domain */ |
1226 | cfg = find_smmu_master_cfg(smmu_domain->smmu, dev); | 1249 | cfg = find_smmu_master_cfg(dev); |
1227 | if (!cfg) | 1250 | if (!cfg) |
1228 | return -ENODEV; | 1251 | return -ENODEV; |
1229 | 1252 | ||
1230 | return arm_smmu_domain_add_master(smmu_domain, cfg); | 1253 | ret = arm_smmu_domain_add_master(smmu_domain, cfg); |
1254 | if (!ret) | ||
1255 | dev->archdata.iommu = domain; | ||
1256 | return ret; | ||
1231 | } | 1257 | } |
1232 | 1258 | ||
1233 | static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev) | 1259 | static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev) |
@@ -1235,9 +1261,12 @@ static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev) | |||
1235 | struct arm_smmu_domain *smmu_domain = domain->priv; | 1261 | struct arm_smmu_domain *smmu_domain = domain->priv; |
1236 | struct arm_smmu_master_cfg *cfg; | 1262 | struct arm_smmu_master_cfg *cfg; |
1237 | 1263 | ||
1238 | cfg = find_smmu_master_cfg(smmu_domain->smmu, dev); | 1264 | cfg = find_smmu_master_cfg(dev); |
1239 | if (cfg) | 1265 | if (!cfg) |
1240 | arm_smmu_domain_remove_master(smmu_domain, cfg); | 1266 | return; |
1267 | |||
1268 | dev->archdata.iommu = NULL; | ||
1269 | arm_smmu_domain_remove_master(smmu_domain, cfg); | ||
1241 | } | 1270 | } |
1242 | 1271 | ||
1243 | static bool arm_smmu_pte_is_contiguous_range(unsigned long addr, | 1272 | static bool arm_smmu_pte_is_contiguous_range(unsigned long addr, |
@@ -1379,6 +1408,7 @@ static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud, | |||
1379 | ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, next, pfn, | 1408 | ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, next, pfn, |
1380 | prot, stage); | 1409 | prot, stage); |
1381 | phys += next - addr; | 1410 | phys += next - addr; |
1411 | pfn = __phys_to_pfn(phys); | ||
1382 | } while (pmd++, addr = next, addr < end); | 1412 | } while (pmd++, addr = next, addr < end); |
1383 | 1413 | ||
1384 | return ret; | 1414 | return ret; |
@@ -1431,9 +1461,11 @@ static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain, | |||
1431 | 1461 | ||
1432 | if (cfg->cbar == CBAR_TYPE_S2_TRANS) { | 1462 | if (cfg->cbar == CBAR_TYPE_S2_TRANS) { |
1433 | stage = 2; | 1463 | stage = 2; |
1464 | input_mask = (1ULL << smmu->s2_input_size) - 1; | ||
1434 | output_mask = (1ULL << smmu->s2_output_size) - 1; | 1465 | output_mask = (1ULL << smmu->s2_output_size) - 1; |
1435 | } else { | 1466 | } else { |
1436 | stage = 1; | 1467 | stage = 1; |
1468 | input_mask = (1ULL << smmu->s1_input_size) - 1; | ||
1437 | output_mask = (1ULL << smmu->s1_output_size) - 1; | 1469 | output_mask = (1ULL << smmu->s1_output_size) - 1; |
1438 | } | 1470 | } |
1439 | 1471 | ||
@@ -1443,7 +1475,6 @@ static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain, | |||
1443 | if (size & ~PAGE_MASK) | 1475 | if (size & ~PAGE_MASK) |
1444 | return -EINVAL; | 1476 | return -EINVAL; |
1445 | 1477 | ||
1446 | input_mask = (1ULL << smmu->input_size) - 1; | ||
1447 | if ((phys_addr_t)iova & ~input_mask) | 1478 | if ((phys_addr_t)iova & ~input_mask) |
1448 | return -ERANGE; | 1479 | return -ERANGE; |
1449 | 1480 | ||
@@ -1549,17 +1580,19 @@ static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data) | |||
1549 | return 0; /* Continue walking */ | 1580 | return 0; /* Continue walking */ |
1550 | } | 1581 | } |
1551 | 1582 | ||
1583 | static void __arm_smmu_release_pci_iommudata(void *data) | ||
1584 | { | ||
1585 | kfree(data); | ||
1586 | } | ||
1587 | |||
1552 | static int arm_smmu_add_device(struct device *dev) | 1588 | static int arm_smmu_add_device(struct device *dev) |
1553 | { | 1589 | { |
1554 | struct arm_smmu_device *smmu; | 1590 | struct arm_smmu_device *smmu; |
1591 | struct arm_smmu_master_cfg *cfg; | ||
1555 | struct iommu_group *group; | 1592 | struct iommu_group *group; |
1593 | void (*releasefn)(void *) = NULL; | ||
1556 | int ret; | 1594 | int ret; |
1557 | 1595 | ||
1558 | if (dev->archdata.iommu) { | ||
1559 | dev_warn(dev, "IOMMU driver already assigned to device\n"); | ||
1560 | return -EINVAL; | ||
1561 | } | ||
1562 | |||
1563 | smmu = find_smmu_for_device(dev); | 1596 | smmu = find_smmu_for_device(dev); |
1564 | if (!smmu) | 1597 | if (!smmu) |
1565 | return -ENODEV; | 1598 | return -ENODEV; |
@@ -1571,7 +1604,6 @@ static int arm_smmu_add_device(struct device *dev) | |||
1571 | } | 1604 | } |
1572 | 1605 | ||
1573 | if (dev_is_pci(dev)) { | 1606 | if (dev_is_pci(dev)) { |
1574 | struct arm_smmu_master_cfg *cfg; | ||
1575 | struct pci_dev *pdev = to_pci_dev(dev); | 1607 | struct pci_dev *pdev = to_pci_dev(dev); |
1576 | 1608 | ||
1577 | cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); | 1609 | cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); |
@@ -1587,11 +1619,20 @@ static int arm_smmu_add_device(struct device *dev) | |||
1587 | */ | 1619 | */ |
1588 | pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, | 1620 | pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, |
1589 | &cfg->streamids[0]); | 1621 | &cfg->streamids[0]); |
1590 | dev->archdata.iommu = cfg; | 1622 | releasefn = __arm_smmu_release_pci_iommudata; |
1591 | } else { | 1623 | } else { |
1592 | dev->archdata.iommu = smmu; | 1624 | struct arm_smmu_master *master; |
1625 | |||
1626 | master = find_smmu_master(smmu, dev->of_node); | ||
1627 | if (!master) { | ||
1628 | ret = -ENODEV; | ||
1629 | goto out_put_group; | ||
1630 | } | ||
1631 | |||
1632 | cfg = &master->cfg; | ||
1593 | } | 1633 | } |
1594 | 1634 | ||
1635 | iommu_group_set_iommudata(group, cfg, releasefn); | ||
1595 | ret = iommu_group_add_device(group, dev); | 1636 | ret = iommu_group_add_device(group, dev); |
1596 | 1637 | ||
1597 | out_put_group: | 1638 | out_put_group: |
@@ -1601,10 +1642,6 @@ out_put_group: | |||
1601 | 1642 | ||
1602 | static void arm_smmu_remove_device(struct device *dev) | 1643 | static void arm_smmu_remove_device(struct device *dev) |
1603 | { | 1644 | { |
1604 | if (dev_is_pci(dev)) | ||
1605 | kfree(dev->archdata.iommu); | ||
1606 | |||
1607 | dev->archdata.iommu = NULL; | ||
1608 | iommu_group_remove_device(dev); | 1645 | iommu_group_remove_device(dev); |
1609 | } | 1646 | } |
1610 | 1647 | ||
@@ -1702,10 +1739,6 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu) | |||
1702 | u32 id; | 1739 | u32 id; |
1703 | 1740 | ||
1704 | dev_notice(smmu->dev, "probing hardware configuration...\n"); | 1741 | dev_notice(smmu->dev, "probing hardware configuration...\n"); |
1705 | |||
1706 | /* Primecell ID */ | ||
1707 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2); | ||
1708 | smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1; | ||
1709 | dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version); | 1742 | dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version); |
1710 | 1743 | ||
1711 | /* ID0 */ | 1744 | /* ID0 */ |
@@ -1716,6 +1749,13 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu) | |||
1716 | return -ENODEV; | 1749 | return -ENODEV; |
1717 | } | 1750 | } |
1718 | #endif | 1751 | #endif |
1752 | |||
1753 | /* Restrict available stages based on module parameter */ | ||
1754 | if (force_stage == 1) | ||
1755 | id &= ~(ID0_S2TS | ID0_NTS); | ||
1756 | else if (force_stage == 2) | ||
1757 | id &= ~(ID0_S1TS | ID0_NTS); | ||
1758 | |||
1719 | if (id & ID0_S1TS) { | 1759 | if (id & ID0_S1TS) { |
1720 | smmu->features |= ARM_SMMU_FEAT_TRANS_S1; | 1760 | smmu->features |= ARM_SMMU_FEAT_TRANS_S1; |
1721 | dev_notice(smmu->dev, "\tstage 1 translation\n"); | 1761 | dev_notice(smmu->dev, "\tstage 1 translation\n"); |
@@ -1732,8 +1772,7 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu) | |||
1732 | } | 1772 | } |
1733 | 1773 | ||
1734 | if (!(smmu->features & | 1774 | if (!(smmu->features & |
1735 | (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2 | | 1775 | (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) { |
1736 | ARM_SMMU_FEAT_TRANS_NESTED))) { | ||
1737 | dev_err(smmu->dev, "\tno translation support!\n"); | 1776 | dev_err(smmu->dev, "\tno translation support!\n"); |
1738 | return -ENODEV; | 1777 | return -ENODEV; |
1739 | } | 1778 | } |
@@ -1779,12 +1818,12 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu) | |||
1779 | 1818 | ||
1780 | /* ID1 */ | 1819 | /* ID1 */ |
1781 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1); | 1820 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1); |
1782 | smmu->pagesize = (id & ID1_PAGESIZE) ? SZ_64K : SZ_4K; | 1821 | smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12; |
1783 | 1822 | ||
1784 | /* Check for size mismatch of SMMU address space from mapped region */ | 1823 | /* Check for size mismatch of SMMU address space from mapped region */ |
1785 | size = 1 << | 1824 | size = 1 << |
1786 | (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1); | 1825 | (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1); |
1787 | size *= (smmu->pagesize << 1); | 1826 | size *= 2 << smmu->pgshift; |
1788 | if (smmu->size != size) | 1827 | if (smmu->size != size) |
1789 | dev_warn(smmu->dev, | 1828 | dev_warn(smmu->dev, |
1790 | "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n", | 1829 | "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n", |
@@ -1803,28 +1842,21 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu) | |||
1803 | /* ID2 */ | 1842 | /* ID2 */ |
1804 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2); | 1843 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2); |
1805 | size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK); | 1844 | size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK); |
1845 | smmu->s1_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size); | ||
1806 | 1846 | ||
1807 | /* | 1847 | /* Stage-2 input size limited due to pgd allocation (PTRS_PER_PGD) */ |
1808 | * Stage-1 output limited by stage-2 input size due to pgd | ||
1809 | * allocation (PTRS_PER_PGD). | ||
1810 | */ | ||
1811 | if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) { | ||
1812 | #ifdef CONFIG_64BIT | 1848 | #ifdef CONFIG_64BIT |
1813 | smmu->s1_output_size = min_t(unsigned long, VA_BITS, size); | 1849 | smmu->s2_input_size = min_t(unsigned long, VA_BITS, size); |
1814 | #else | 1850 | #else |
1815 | smmu->s1_output_size = min(32UL, size); | 1851 | smmu->s2_input_size = min(32UL, size); |
1816 | #endif | 1852 | #endif |
1817 | } else { | ||
1818 | smmu->s1_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, | ||
1819 | size); | ||
1820 | } | ||
1821 | 1853 | ||
1822 | /* The stage-2 output mask is also applied for bypass */ | 1854 | /* The stage-2 output mask is also applied for bypass */ |
1823 | size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK); | 1855 | size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK); |
1824 | smmu->s2_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size); | 1856 | smmu->s2_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size); |
1825 | 1857 | ||
1826 | if (smmu->version == 1) { | 1858 | if (smmu->version == ARM_SMMU_V1) { |
1827 | smmu->input_size = 32; | 1859 | smmu->s1_input_size = 32; |
1828 | } else { | 1860 | } else { |
1829 | #ifdef CONFIG_64BIT | 1861 | #ifdef CONFIG_64BIT |
1830 | size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK; | 1862 | size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK; |
@@ -1832,7 +1864,7 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu) | |||
1832 | #else | 1864 | #else |
1833 | size = 32; | 1865 | size = 32; |
1834 | #endif | 1866 | #endif |
1835 | smmu->input_size = size; | 1867 | smmu->s1_input_size = size; |
1836 | 1868 | ||
1837 | if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) || | 1869 | if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) || |
1838 | (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) || | 1870 | (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) || |
@@ -1843,15 +1875,30 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu) | |||
1843 | } | 1875 | } |
1844 | } | 1876 | } |
1845 | 1877 | ||
1846 | dev_notice(smmu->dev, | 1878 | if (smmu->features & ARM_SMMU_FEAT_TRANS_S1) |
1847 | "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n", | 1879 | dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n", |
1848 | smmu->input_size, smmu->s1_output_size, | 1880 | smmu->s1_input_size, smmu->s1_output_size); |
1849 | smmu->s2_output_size); | 1881 | |
1882 | if (smmu->features & ARM_SMMU_FEAT_TRANS_S2) | ||
1883 | dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n", | ||
1884 | smmu->s2_input_size, smmu->s2_output_size); | ||
1885 | |||
1850 | return 0; | 1886 | return 0; |
1851 | } | 1887 | } |
1852 | 1888 | ||
1889 | static struct of_device_id arm_smmu_of_match[] = { | ||
1890 | { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 }, | ||
1891 | { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 }, | ||
1892 | { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 }, | ||
1893 | { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 }, | ||
1894 | { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 }, | ||
1895 | { }, | ||
1896 | }; | ||
1897 | MODULE_DEVICE_TABLE(of, arm_smmu_of_match); | ||
1898 | |||
1853 | static int arm_smmu_device_dt_probe(struct platform_device *pdev) | 1899 | static int arm_smmu_device_dt_probe(struct platform_device *pdev) |
1854 | { | 1900 | { |
1901 | const struct of_device_id *of_id; | ||
1855 | struct resource *res; | 1902 | struct resource *res; |
1856 | struct arm_smmu_device *smmu; | 1903 | struct arm_smmu_device *smmu; |
1857 | struct device *dev = &pdev->dev; | 1904 | struct device *dev = &pdev->dev; |
@@ -1866,6 +1913,9 @@ static int arm_smmu_device_dt_probe(struct platform_device *pdev) | |||
1866 | } | 1913 | } |
1867 | smmu->dev = dev; | 1914 | smmu->dev = dev; |
1868 | 1915 | ||
1916 | of_id = of_match_node(arm_smmu_of_match, dev->of_node); | ||
1917 | smmu->version = (enum arm_smmu_arch_version)of_id->data; | ||
1918 | |||
1869 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 1919 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
1870 | smmu->base = devm_ioremap_resource(dev, res); | 1920 | smmu->base = devm_ioremap_resource(dev, res); |
1871 | if (IS_ERR(smmu->base)) | 1921 | if (IS_ERR(smmu->base)) |
@@ -1930,7 +1980,7 @@ static int arm_smmu_device_dt_probe(struct platform_device *pdev) | |||
1930 | 1980 | ||
1931 | parse_driver_options(smmu); | 1981 | parse_driver_options(smmu); |
1932 | 1982 | ||
1933 | if (smmu->version > 1 && | 1983 | if (smmu->version > ARM_SMMU_V1 && |
1934 | smmu->num_context_banks != smmu->num_context_irqs) { | 1984 | smmu->num_context_banks != smmu->num_context_irqs) { |
1935 | dev_err(dev, | 1985 | dev_err(dev, |
1936 | "found only %d context interrupt(s) but %d required\n", | 1986 | "found only %d context interrupt(s) but %d required\n", |
@@ -2011,17 +2061,6 @@ static int arm_smmu_device_remove(struct platform_device *pdev) | |||
2011 | return 0; | 2061 | return 0; |
2012 | } | 2062 | } |
2013 | 2063 | ||
2014 | #ifdef CONFIG_OF | ||
2015 | static struct of_device_id arm_smmu_of_match[] = { | ||
2016 | { .compatible = "arm,smmu-v1", }, | ||
2017 | { .compatible = "arm,smmu-v2", }, | ||
2018 | { .compatible = "arm,mmu-400", }, | ||
2019 | { .compatible = "arm,mmu-500", }, | ||
2020 | { }, | ||
2021 | }; | ||
2022 | MODULE_DEVICE_TABLE(of, arm_smmu_of_match); | ||
2023 | #endif | ||
2024 | |||
2025 | static struct platform_driver arm_smmu_driver = { | 2064 | static struct platform_driver arm_smmu_driver = { |
2026 | .driver = { | 2065 | .driver = { |
2027 | .owner = THIS_MODULE, | 2066 | .owner = THIS_MODULE, |