hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/acpi/arm64/iort.c
....@@ -1,15 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2016, Semihalf
34 * Author: Tomasz Nowicki <tn@semihalf.com>
4
- *
5
- * This program is free software; you can redistribute it and/or modify it
6
- * under the terms and conditions of the GNU General Public License,
7
- * version 2, as published by the Free Software Foundation.
8
- *
9
- * This program is distributed in the hope it will be useful, but WITHOUT
10
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
- * more details.
135 *
146 * This file implements early detection/parsing of I/O mapping
157 * reported to OS through firmware via I/O Remapping Table (IORT)
....@@ -19,12 +11,14 @@
1911 #define pr_fmt(fmt) "ACPI: IORT: " fmt
2012
2113 #include <linux/acpi_iort.h>
14
+#include <linux/bitfield.h>
2215 #include <linux/iommu.h>
2316 #include <linux/kernel.h>
2417 #include <linux/list.h>
2518 #include <linux/pci.h>
2619 #include <linux/platform_device.h>
2720 #include <linux/slab.h>
21
+#include <linux/dma-map-ops.h>
2822
2923 #define IORT_TYPE_MASK(type) (1 << (type))
3024 #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
....@@ -50,7 +44,7 @@
5044 * iort_set_fwnode() - Create iort_fwnode and use it to register
5145 * iommu data in the iort_fwnode_list
5246 *
53
- * @node: IORT table node associated with the IOMMU
47
+ * @iort_node: IORT table node associated with the IOMMU
5448 * @fwnode: fwnode associated with the IORT node
5549 *
5650 * Returns: 0 on success
....@@ -271,15 +265,31 @@
271265
272266 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
273267 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
274
- struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
268
+ struct acpi_device *adev;
275269 struct acpi_iort_named_component *ncomp;
270
+ struct device *nc_dev = dev;
271
+
272
+ /*
273
+ * Walk the device tree to find a device with an
274
+ * ACPI companion; there is no point in scanning
275
+ * IORT for a device matching a named component if
276
+ * the device does not have an ACPI companion to
277
+ * start with.
278
+ */
279
+ do {
280
+ adev = ACPI_COMPANION(nc_dev);
281
+ if (adev)
282
+ break;
283
+
284
+ nc_dev = nc_dev->parent;
285
+ } while (nc_dev);
276286
277287 if (!adev)
278288 goto out;
279289
280290 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
281291 if (ACPI_FAILURE(status)) {
282
- dev_warn(dev, "Can't get device full path name\n");
292
+ dev_warn(nc_dev, "Can't get device full path name\n");
283293 goto out;
284294 }
285295
....@@ -307,7 +317,7 @@
307317 }
308318
309319 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
310
- u32 *rid_out)
320
+ u32 *rid_out, bool check_overlap)
311321 {
312322 /* Single mapping does not care for input id */
313323 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
....@@ -323,10 +333,36 @@
323333 }
324334
325335 if (rid_in < map->input_base ||
326
- (rid_in >= map->input_base + map->id_count))
336
+ (rid_in > map->input_base + map->id_count))
327337 return -ENXIO;
328338
339
+ if (check_overlap) {
340
+ /*
341
+ * We already found a mapping for this input ID at the end of
342
+ * another region. If it coincides with the start of this
343
+ * region, we assume the prior match was due to the off-by-1
344
+ * issue mentioned below, and allow it to be superseded.
345
+ * Otherwise, things are *really* broken, and we just disregard
346
+ * duplicate matches entirely to retain compatibility.
347
+ */
348
+ pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n",
349
+ map, rid_in);
350
+ if (rid_in != map->input_base)
351
+ return -ENXIO;
352
+
353
+ pr_err(FW_BUG "applying workaround.\n");
354
+ }
355
+
329356 *rid_out = map->output_base + (rid_in - map->input_base);
357
+
358
+ /*
359
+ * Due to confusion regarding the meaning of the id_count field (which
360
+ * carries the number of IDs *minus 1*), we may have to disregard this
361
+ * match if it is at the end of the range, and overlaps with the start
362
+ * of another one.
363
+ */
364
+ if (map->id_count > 0 && rid_in == map->input_base + map->id_count)
365
+ return -EAGAIN;
330366 return 0;
331367 }
332368
....@@ -356,7 +392,8 @@
356392 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
357393 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
358394 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
359
- node->type == ACPI_IORT_NODE_SMMU_V3) {
395
+ node->type == ACPI_IORT_NODE_SMMU_V3 ||
396
+ node->type == ACPI_IORT_NODE_PMCG) {
360397 *id_out = map->output_base;
361398 return parent;
362399 }
....@@ -368,6 +405,7 @@
368405 static int iort_get_id_mapping_index(struct acpi_iort_node *node)
369406 {
370407 struct acpi_iort_smmu_v3 *smmu;
408
+ struct acpi_iort_pmcg *pmcg;
371409
372410 switch (node->type) {
373411 case ACPI_IORT_NODE_SMMU_V3:
....@@ -394,6 +432,12 @@
394432 }
395433
396434 return smmu->id_mapping_index;
435
+ case ACPI_IORT_NODE_PMCG:
436
+ pmcg = (struct acpi_iort_pmcg *)node->node_data;
437
+ if (pmcg->overflow_gsiv || node->mapping_count == 0)
438
+ return -EINVAL;
439
+
440
+ return 0;
397441 default:
398442 return -EINVAL;
399443 }
....@@ -408,7 +452,8 @@
408452 /* Parse the ID mapping tree to find specified node type */
409453 while (node) {
410454 struct acpi_iort_id_mapping *map;
411
- int i, index;
455
+ int i, index, rc = 0;
456
+ u32 out_ref = 0, map_id = id;
412457
413458 if (IORT_TYPE_MASK(node->type) & type_mask) {
414459 if (id_out)
....@@ -442,15 +487,18 @@
442487 if (i == index)
443488 continue;
444489
445
- if (!iort_id_map(map, node->type, id, &id))
490
+ rc = iort_id_map(map, node->type, map_id, &id, out_ref);
491
+ if (!rc)
446492 break;
493
+ if (rc == -EAGAIN)
494
+ out_ref = map->output_reference;
447495 }
448496
449
- if (i == node->mapping_count)
497
+ if (i == node->mapping_count && !out_ref)
450498 goto fail_map;
451499
452500 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
453
- map->output_reference);
501
+ rc ? out_ref : map->output_reference);
454502 }
455503
456504 fail_map:
....@@ -503,7 +551,6 @@
503551 node = iort_get_iort_node(dev->fwnode);
504552 if (node)
505553 return node;
506
-
507554 /*
508555 * if not, then it should be a platform device defined in
509556 * DSDT/SSDT (with Named Component node in IORT)
....@@ -512,32 +559,29 @@
512559 iort_match_node_callback, dev);
513560 }
514561
515
- /* Find a PCI root bus */
516562 pbus = to_pci_dev(dev)->bus;
517
- while (!pci_is_root_bus(pbus))
518
- pbus = pbus->parent;
519563
520564 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
521565 iort_match_node_callback, &pbus->dev);
522566 }
523567
524568 /**
525
- * iort_msi_map_rid() - Map a MSI requester ID for a device
569
+ * iort_msi_map_id() - Map a MSI input ID for a device
526570 * @dev: The device for which the mapping is to be done.
527
- * @req_id: The device requester ID.
571
+ * @input_id: The device input ID.
528572 *
529
- * Returns: mapped MSI RID on success, input requester ID otherwise
573
+ * Returns: mapped MSI ID on success, input ID otherwise
530574 */
531
-u32 iort_msi_map_rid(struct device *dev, u32 req_id)
575
+u32 iort_msi_map_id(struct device *dev, u32 input_id)
532576 {
533577 struct acpi_iort_node *node;
534578 u32 dev_id;
535579
536580 node = iort_find_dev_node(dev);
537581 if (!node)
538
- return req_id;
582
+ return input_id;
539583
540
- iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
584
+ iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE);
541585 return dev_id;
542586 }
543587
....@@ -594,13 +638,13 @@
594638 /**
595639 * iort_dev_find_its_id() - Find the ITS identifier for a device
596640 * @dev: The device.
597
- * @req_id: Device's requester ID
641
+ * @id: Device's ID
598642 * @idx: Index of the ITS identifier list.
599643 * @its_id: ITS identifier.
600644 *
601645 * Returns: 0 on success, appropriate error value otherwise
602646 */
603
-static int iort_dev_find_its_id(struct device *dev, u32 req_id,
647
+static int iort_dev_find_its_id(struct device *dev, u32 id,
604648 unsigned int idx, int *its_id)
605649 {
606650 struct acpi_iort_its_group *its;
....@@ -610,7 +654,7 @@
610654 if (!node)
611655 return -ENXIO;
612656
613
- node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
657
+ node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE);
614658 if (!node)
615659 return -ENXIO;
616660
....@@ -629,23 +673,25 @@
629673 /**
630674 * iort_get_device_domain() - Find MSI domain related to a device
631675 * @dev: The device.
632
- * @req_id: Requester ID for the device.
676
+ * @id: Requester ID for the device.
677
+ * @bus_token: irq domain bus token.
633678 *
634679 * Returns: the MSI domain for this device, NULL otherwise
635680 */
636
-struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
681
+struct irq_domain *iort_get_device_domain(struct device *dev, u32 id,
682
+ enum irq_domain_bus_token bus_token)
637683 {
638684 struct fwnode_handle *handle;
639685 int its_id;
640686
641
- if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
687
+ if (iort_dev_find_its_id(dev, id, 0, &its_id))
642688 return NULL;
643689
644690 handle = iort_find_domain_token(its_id);
645691 if (!handle)
646692 return NULL;
647693
648
- return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
694
+ return irq_find_matching_fwnode(handle, bus_token);
649695 }
650696
651697 static void iort_set_device_domain(struct device *dev,
....@@ -741,45 +787,11 @@
741787 dev_set_msi_domain(dev, msi_domain);
742788 }
743789
744
-static int __maybe_unused __get_pci_rid(struct pci_dev *pdev, u16 alias,
745
- void *data)
746
-{
747
- u32 *rid = data;
748
-
749
- *rid = alias;
750
- return 0;
751
-}
752
-
753
-static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
754
- struct fwnode_handle *fwnode,
755
- const struct iommu_ops *ops)
756
-{
757
- int ret = iommu_fwspec_init(dev, fwnode, ops);
758
-
759
- if (!ret)
760
- ret = iommu_fwspec_add_ids(dev, &streamid, 1);
761
-
762
- return ret;
763
-}
764
-
765
-static inline bool iort_iommu_driver_enabled(u8 type)
766
-{
767
- switch (type) {
768
- case ACPI_IORT_NODE_SMMU_V3:
769
- return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
770
- case ACPI_IORT_NODE_SMMU:
771
- return IS_BUILTIN(CONFIG_ARM_SMMU);
772
- default:
773
- pr_warn("IORT node type %u does not describe an SMMU\n", type);
774
- return false;
775
- }
776
-}
777
-
778790 #ifdef CONFIG_IOMMU_API
779791 static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
780792 {
781793 struct acpi_iort_node *iommu;
782
- struct iommu_fwspec *fwspec = dev->iommu_fwspec;
794
+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
783795
784796 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
785797
....@@ -794,19 +806,19 @@
794806 return NULL;
795807 }
796808
797
-static inline const struct iommu_ops *iort_fwspec_iommu_ops(
798
- struct iommu_fwspec *fwspec)
809
+static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
799810 {
811
+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
812
+
800813 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
801814 }
802815
803
-static inline int iort_add_device_replay(const struct iommu_ops *ops,
804
- struct device *dev)
816
+static inline int iort_add_device_replay(struct device *dev)
805817 {
806818 int err = 0;
807819
808
- if (ops->add_device && dev->bus && !dev->iommu_group)
809
- err = ops->add_device(dev);
820
+ if (dev->bus && !device_iommu_mapped(dev))
821
+ err = iommu_probe_device(dev);
810822
811823 return err;
812824 }
....@@ -824,6 +836,7 @@
824836 */
825837 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
826838 {
839
+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
827840 struct acpi_iort_its_group *its;
828841 struct acpi_iort_node *iommu_node, *its_node = NULL;
829842 int i, resv = 0;
....@@ -841,9 +854,9 @@
841854 * a given PCI or named component may map IDs to.
842855 */
843856
844
- for (i = 0; i < dev->iommu_fwspec->num_ids; i++) {
857
+ for (i = 0; i < fwspec->num_ids; i++) {
845858 its_node = iort_node_map_id(iommu_node,
846
- dev->iommu_fwspec->ids[i],
859
+ fwspec->ids[i],
847860 NULL, IORT_MSI_TYPE);
848861 if (its_node)
849862 break;
....@@ -873,16 +886,39 @@
873886
874887 return (resv == its->its_count) ? resv : -ENODEV;
875888 }
876
-#else
877
-static inline const struct iommu_ops *iort_fwspec_iommu_ops(
878
- struct iommu_fwspec *fwspec)
879
-{ return NULL; }
880
-static inline int iort_add_device_replay(const struct iommu_ops *ops,
881
- struct device *dev)
882
-{ return 0; }
883
-int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
884
-{ return 0; }
885
-#endif
889
+
890
+static inline bool iort_iommu_driver_enabled(u8 type)
891
+{
892
+ switch (type) {
893
+ case ACPI_IORT_NODE_SMMU_V3:
894
+ return IS_ENABLED(CONFIG_ARM_SMMU_V3);
895
+ case ACPI_IORT_NODE_SMMU:
896
+ return IS_ENABLED(CONFIG_ARM_SMMU);
897
+ default:
898
+ pr_warn("IORT node type %u does not describe an SMMU\n", type);
899
+ return false;
900
+ }
901
+}
902
+
903
+static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
904
+ struct fwnode_handle *fwnode,
905
+ const struct iommu_ops *ops)
906
+{
907
+ int ret = iommu_fwspec_init(dev, fwnode, ops);
908
+
909
+ if (!ret)
910
+ ret = iommu_fwspec_add_ids(dev, &streamid, 1);
911
+
912
+ return ret;
913
+}
914
+
915
+static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
916
+{
917
+ struct acpi_iort_root_complex *pci_rc;
918
+
919
+ pci_rc = (struct acpi_iort_root_complex *)node->node_data;
920
+ return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
921
+}
886922
887923 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
888924 u32 streamid)
....@@ -929,6 +965,136 @@
929965 return iort_iommu_xlate(info->dev, parent, streamid);
930966 }
931967
968
+static void iort_named_component_init(struct device *dev,
969
+ struct acpi_iort_node *node)
970
+{
971
+ struct acpi_iort_named_component *nc;
972
+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
973
+
974
+ if (!fwspec)
975
+ return;
976
+
977
+ nc = (struct acpi_iort_named_component *)node->node_data;
978
+ fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS,
979
+ nc->node_flags);
980
+}
981
+
982
+static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node)
983
+{
984
+ struct acpi_iort_node *parent;
985
+ int err = -ENODEV, i = 0;
986
+ u32 streamid = 0;
987
+
988
+ do {
989
+
990
+ parent = iort_node_map_platform_id(node, &streamid,
991
+ IORT_IOMMU_TYPE,
992
+ i++);
993
+
994
+ if (parent)
995
+ err = iort_iommu_xlate(dev, parent, streamid);
996
+ } while (parent && !err);
997
+
998
+ return err;
999
+}
1000
+
1001
+static int iort_nc_iommu_map_id(struct device *dev,
1002
+ struct acpi_iort_node *node,
1003
+ const u32 *in_id)
1004
+{
1005
+ struct acpi_iort_node *parent;
1006
+ u32 streamid;
1007
+
1008
+ parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE);
1009
+ if (parent)
1010
+ return iort_iommu_xlate(dev, parent, streamid);
1011
+
1012
+ return -ENODEV;
1013
+}
1014
+
1015
+
1016
+/**
1017
+ * iort_iommu_configure_id - Set-up IOMMU configuration for a device.
1018
+ *
1019
+ * @dev: device to configure
1020
+ * @id_in: optional input id const value pointer
1021
+ *
1022
+ * Returns: iommu_ops pointer on configuration success
1023
+ * NULL on configuration failure
1024
+ */
1025
+const struct iommu_ops *iort_iommu_configure_id(struct device *dev,
1026
+ const u32 *id_in)
1027
+{
1028
+ struct acpi_iort_node *node;
1029
+ const struct iommu_ops *ops;
1030
+ int err = -ENODEV;
1031
+
1032
+ /*
1033
+ * If we already translated the fwspec there
1034
+ * is nothing left to do, return the iommu_ops.
1035
+ */
1036
+ ops = iort_fwspec_iommu_ops(dev);
1037
+ if (ops)
1038
+ return ops;
1039
+
1040
+ if (dev_is_pci(dev)) {
1041
+ struct iommu_fwspec *fwspec;
1042
+ struct pci_bus *bus = to_pci_dev(dev)->bus;
1043
+ struct iort_pci_alias_info info = { .dev = dev };
1044
+
1045
+ node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1046
+ iort_match_node_callback, &bus->dev);
1047
+ if (!node)
1048
+ return NULL;
1049
+
1050
+ info.node = node;
1051
+ err = pci_for_each_dma_alias(to_pci_dev(dev),
1052
+ iort_pci_iommu_init, &info);
1053
+
1054
+ fwspec = dev_iommu_fwspec_get(dev);
1055
+ if (fwspec && iort_pci_rc_supports_ats(node))
1056
+ fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
1057
+ } else {
1058
+ node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1059
+ iort_match_node_callback, dev);
1060
+ if (!node)
1061
+ return NULL;
1062
+
1063
+ err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) :
1064
+ iort_nc_iommu_map(dev, node);
1065
+
1066
+ if (!err)
1067
+ iort_named_component_init(dev, node);
1068
+ }
1069
+
1070
+ /*
1071
+ * If we have reason to believe the IOMMU driver missed the initial
1072
+ * add_device callback for dev, replay it to get things in order.
1073
+ */
1074
+ if (!err) {
1075
+ ops = iort_fwspec_iommu_ops(dev);
1076
+ err = iort_add_device_replay(dev);
1077
+ }
1078
+
1079
+ /* Ignore all other errors apart from EPROBE_DEFER */
1080
+ if (err == -EPROBE_DEFER) {
1081
+ ops = ERR_PTR(err);
1082
+ } else if (err) {
1083
+ dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1084
+ ops = NULL;
1085
+ }
1086
+
1087
+ return ops;
1088
+}
1089
+
1090
+#else
1091
+int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
1092
+{ return 0; }
1093
+const struct iommu_ops *iort_iommu_configure_id(struct device *dev,
1094
+ const u32 *input_id)
1095
+{ return NULL; }
1096
+#endif
1097
+
9321098 static int nc_dma_get_range(struct device *dev, u64 *size)
9331099 {
9341100 struct acpi_iort_node *node;
....@@ -940,6 +1106,11 @@
9401106 return -ENODEV;
9411107
9421108 ncomp = (struct acpi_iort_named_component *)node->node_data;
1109
+
1110
+ if (!ncomp->memory_address_limit) {
1111
+ pr_warn(FW_BUG "Named component missing memory address limit\n");
1112
+ return -EINVAL;
1113
+ }
9431114
9441115 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
9451116 1ULL<<ncomp->memory_address_limit;
....@@ -960,6 +1131,11 @@
9601131
9611132 rc = (struct acpi_iort_root_complex *)node->node_data;
9621133
1134
+ if (!rc->memory_address_limit) {
1135
+ pr_warn(FW_BUG "Root complex missing memory address limit\n");
1136
+ return -EINVAL;
1137
+ }
1138
+
9631139 *size = rc->memory_address_limit >= 64 ? U64_MAX :
9641140 1ULL<<rc->memory_address_limit;
9651141
....@@ -971,12 +1147,12 @@
9711147 *
9721148 * @dev: device to configure
9731149 * @dma_addr: device DMA address result pointer
974
- * @size: DMA range size result pointer
1150
+ * @dma_size: DMA range size result pointer
9751151 */
9761152 void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
9771153 {
978
- u64 mask, dmaaddr = 0, size = 0, offset = 0;
979
- int ret, msb;
1154
+ u64 end, mask, dmaaddr = 0, size = 0, offset = 0;
1155
+ int ret;
9801156
9811157 /*
9821158 * If @dev is expected to be DMA-capable then the bus code that created
....@@ -994,110 +1170,29 @@
9941170 else
9951171 size = 1ULL << 32;
9961172
997
- if (dev_is_pci(dev)) {
998
- ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
999
- if (ret == -ENODEV)
1000
- ret = rc_dma_get_range(dev, &size);
1001
- } else {
1002
- ret = nc_dma_get_range(dev, &size);
1003
- }
1173
+ ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
1174
+ if (ret == -ENODEV)
1175
+ ret = dev_is_pci(dev) ? rc_dma_get_range(dev, &size)
1176
+ : nc_dma_get_range(dev, &size);
10041177
10051178 if (!ret) {
1006
- msb = fls64(dmaaddr + size - 1);
10071179 /*
1008
- * Round-up to the power-of-two mask or set
1009
- * the mask to the whole 64-bit address space
1010
- * in case the DMA region covers the full
1011
- * memory window.
1180
+ * Limit coherent and dma mask based on size retrieved from
1181
+ * firmware.
10121182 */
1013
- mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1;
1014
- /*
1015
- * Limit coherent and dma mask based on size
1016
- * retrieved from firmware.
1017
- */
1018
- dev->bus_dma_mask = mask;
1019
- dev->coherent_dma_mask = mask;
1020
- *dev->dma_mask = mask;
1183
+ end = dmaaddr + size - 1;
1184
+ mask = DMA_BIT_MASK(ilog2(end) + 1);
1185
+ dev->bus_dma_limit = end;
1186
+ dev->coherent_dma_mask = min(dev->coherent_dma_mask, mask);
1187
+ *dev->dma_mask = min(*dev->dma_mask, mask);
10211188 }
10221189
10231190 *dma_addr = dmaaddr;
10241191 *dma_size = size;
10251192
1026
- dev->dma_pfn_offset = PFN_DOWN(offset);
1027
- dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
1028
-}
1193
+ ret = dma_direct_set_offset(dev, dmaaddr + offset, dmaaddr, size);
10291194
1030
-/**
1031
- * iort_iommu_configure - Set-up IOMMU configuration for a device.
1032
- *
1033
- * @dev: device to configure
1034
- *
1035
- * Returns: iommu_ops pointer on configuration success
1036
- * NULL on configuration failure
1037
- */
1038
-const struct iommu_ops *iort_iommu_configure(struct device *dev)
1039
-{
1040
- struct acpi_iort_node *node, *parent;
1041
- const struct iommu_ops *ops;
1042
- u32 streamid = 0;
1043
- int err = -ENODEV;
1044
-
1045
- /*
1046
- * If we already translated the fwspec there
1047
- * is nothing left to do, return the iommu_ops.
1048
- */
1049
- ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
1050
- if (ops)
1051
- return ops;
1052
-
1053
- if (dev_is_pci(dev)) {
1054
- struct pci_bus *bus = to_pci_dev(dev)->bus;
1055
- struct iort_pci_alias_info info = { .dev = dev };
1056
-
1057
- node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1058
- iort_match_node_callback, &bus->dev);
1059
- if (!node)
1060
- return NULL;
1061
-
1062
- info.node = node;
1063
- err = pci_for_each_dma_alias(to_pci_dev(dev),
1064
- iort_pci_iommu_init, &info);
1065
- } else {
1066
- int i = 0;
1067
-
1068
- node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1069
- iort_match_node_callback, dev);
1070
- if (!node)
1071
- return NULL;
1072
-
1073
- do {
1074
- parent = iort_node_map_platform_id(node, &streamid,
1075
- IORT_IOMMU_TYPE,
1076
- i++);
1077
-
1078
- if (parent)
1079
- err = iort_iommu_xlate(dev, parent, streamid);
1080
- } while (parent && !err);
1081
- }
1082
-
1083
- /*
1084
- * If we have reason to believe the IOMMU driver missed the initial
1085
- * add_device callback for dev, replay it to get things in order.
1086
- */
1087
- if (!err) {
1088
- ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
1089
- err = iort_add_device_replay(ops, dev);
1090
- }
1091
-
1092
- /* Ignore all other errors apart from EPROBE_DEFER */
1093
- if (err == -EPROBE_DEFER) {
1094
- ops = ERR_PTR(err);
1095
- } else if (err) {
1096
- dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1097
- ops = NULL;
1098
- }
1099
-
1100
- return ops;
1195
+ dev_dbg(dev, "dma_offset(%#08llx)%s\n", offset, ret ? " failed!" : "");
11011196 }
11021197
11031198 static void __init acpi_iort_register_irq(int hwirq, const char *name,
....@@ -1217,14 +1312,23 @@
12171312 }
12181313 }
12191314
1220
-static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
1315
+static void __init arm_smmu_v3_dma_configure(struct device *dev,
1316
+ struct acpi_iort_node *node)
12211317 {
12221318 struct acpi_iort_smmu_v3 *smmu;
1319
+ enum dev_dma_attr attr;
12231320
12241321 /* Retrieve SMMUv3 specific data */
12251322 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
12261323
1227
- return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
1324
+ attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1325
+ DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1326
+
1327
+ /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1328
+ dev->dma_mask = &dev->coherent_dma_mask;
1329
+
1330
+ /* Configure DMA for the page table walker */
1331
+ acpi_dma_configure(dev, attr);
12281332 }
12291333
12301334 #if defined(CONFIG_ACPI_NUMA)
....@@ -1238,12 +1342,12 @@
12381342
12391343 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
12401344 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1241
- int node = acpi_map_pxm_to_node(smmu->pxm);
1345
+ int dev_node = pxm_to_node(smmu->pxm);
12421346
1243
- if (node != NUMA_NO_NODE && !node_online(node))
1347
+ if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
12441348 return -EINVAL;
12451349
1246
- set_dev_node(dev, node);
1350
+ set_dev_node(dev, dev_node);
12471351 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
12481352 smmu->base_address,
12491353 smmu->pxm);
....@@ -1306,30 +1410,107 @@
13061410 }
13071411 }
13081412
1309
-static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
1413
+static void __init arm_smmu_dma_configure(struct device *dev,
1414
+ struct acpi_iort_node *node)
13101415 {
13111416 struct acpi_iort_smmu *smmu;
1417
+ enum dev_dma_attr attr;
13121418
13131419 /* Retrieve SMMU specific data */
13141420 smmu = (struct acpi_iort_smmu *)node->node_data;
13151421
1316
- return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
1422
+ attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1423
+ DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1424
+
1425
+ /* We expect the dma masks to be equivalent for SMMU set-ups */
1426
+ dev->dma_mask = &dev->coherent_dma_mask;
1427
+
1428
+ /* Configure DMA for the page table walker */
1429
+ acpi_dma_configure(dev, attr);
1430
+}
1431
+
1432
+static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1433
+{
1434
+ struct acpi_iort_pmcg *pmcg;
1435
+
1436
+ /* Retrieve PMCG specific data */
1437
+ pmcg = (struct acpi_iort_pmcg *)node->node_data;
1438
+
1439
+ /*
1440
+ * There are always 2 memory resources.
1441
+ * If the overflow_gsiv is present then add that for a total of 3.
1442
+ */
1443
+ return pmcg->overflow_gsiv ? 3 : 2;
1444
+}
1445
+
1446
+static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1447
+ struct acpi_iort_node *node)
1448
+{
1449
+ struct acpi_iort_pmcg *pmcg;
1450
+
1451
+ /* Retrieve PMCG specific data */
1452
+ pmcg = (struct acpi_iort_pmcg *)node->node_data;
1453
+
1454
+ res[0].start = pmcg->page0_base_address;
1455
+ res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1456
+ res[0].flags = IORESOURCE_MEM;
1457
+ /*
1458
+ * The initial version in DEN0049C lacked a way to describe register
1459
+ * page 1, which makes it broken for most PMCG implementations; in
1460
+ * that case, just let the driver fail gracefully if it expects to
1461
+ * find a second memory resource.
1462
+ */
1463
+ if (node->revision > 0) {
1464
+ res[1].start = pmcg->page1_base_address;
1465
+ res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1466
+ res[1].flags = IORESOURCE_MEM;
1467
+ }
1468
+
1469
+ if (pmcg->overflow_gsiv)
1470
+ acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1471
+ ACPI_EDGE_SENSITIVE, &res[2]);
1472
+}
1473
+
1474
+static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1475
+ /* HiSilicon Hip08 Platform */
1476
+ {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1477
+ "Erratum #162001800, Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP08},
1478
+ /* HiSilicon Hip09 Platform */
1479
+ {"HISI ", "HIP09 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1480
+ "Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP09},
1481
+ { }
1482
+};
1483
+
1484
+static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1485
+{
1486
+ u32 model;
1487
+ int idx;
1488
+
1489
+ idx = acpi_match_platform_list(pmcg_plat_info);
1490
+ if (idx >= 0)
1491
+ model = pmcg_plat_info[idx].data;
1492
+ else
1493
+ model = IORT_SMMU_V3_PMCG_GENERIC;
1494
+
1495
+ return platform_device_add_data(pdev, &model, sizeof(model));
13171496 }
13181497
13191498 struct iort_dev_config {
13201499 const char *name;
13211500 int (*dev_init)(struct acpi_iort_node *node);
1322
- bool (*dev_is_coherent)(struct acpi_iort_node *node);
1501
+ void (*dev_dma_configure)(struct device *dev,
1502
+ struct acpi_iort_node *node);
13231503 int (*dev_count_resources)(struct acpi_iort_node *node);
13241504 void (*dev_init_resources)(struct resource *res,
13251505 struct acpi_iort_node *node);
13261506 int (*dev_set_proximity)(struct device *dev,
13271507 struct acpi_iort_node *node);
1508
+ int (*dev_add_platdata)(struct platform_device *pdev);
13281509 };
13291510
13301511 static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
13311512 .name = "arm-smmu-v3",
1332
- .dev_is_coherent = arm_smmu_v3_is_coherent,
1513
+ .dev_dma_configure = arm_smmu_v3_dma_configure,
13331514 .dev_count_resources = arm_smmu_v3_count_resources,
13341515 .dev_init_resources = arm_smmu_v3_init_resources,
13351516 .dev_set_proximity = arm_smmu_v3_set_proximity,
....@@ -1337,9 +1518,16 @@
13371518
13381519 static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
13391520 .name = "arm-smmu",
1340
- .dev_is_coherent = arm_smmu_is_coherent,
1521
+ .dev_dma_configure = arm_smmu_dma_configure,
13411522 .dev_count_resources = arm_smmu_count_resources,
1342
- .dev_init_resources = arm_smmu_init_resources
1523
+ .dev_init_resources = arm_smmu_init_resources,
1524
+};
1525
+
1526
+static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1527
+ .name = "arm-smmu-v3-pmcg",
1528
+ .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1529
+ .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1530
+ .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
13431531 };
13441532
13451533 static __init const struct iort_dev_config *iort_get_dev_cfg(
....@@ -1350,6 +1538,8 @@
13501538 return &iort_arm_smmu_v3_cfg;
13511539 case ACPI_IORT_NODE_SMMU:
13521540 return &iort_arm_smmu_cfg;
1541
+ case ACPI_IORT_NODE_PMCG:
1542
+ return &iort_arm_smmu_v3_pmcg_cfg;
13531543 default:
13541544 return NULL;
13551545 }
....@@ -1358,6 +1548,7 @@
13581548 /**
13591549 * iort_add_platform_device() - Allocate a platform device for IORT node
13601550 * @node: Pointer to device ACPI IORT node
1551
+ * @ops: Pointer to IORT device config struct
13611552 *
13621553 * Returns: 0 on success, <0 failure
13631554 */
....@@ -1367,7 +1558,6 @@
13671558 struct fwnode_handle *fwnode;
13681559 struct platform_device *pdev;
13691560 struct resource *r;
1370
- enum dev_dma_attr attr;
13711561 int ret, count;
13721562
13731563 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
....@@ -1401,18 +1591,18 @@
14011591 goto dev_put;
14021592
14031593 /*
1404
- * Add a copy of IORT node pointer to platform_data to
1405
- * be used to retrieve IORT data information.
1594
+ * Platform devices based on PMCG nodes uses platform_data to
1595
+ * pass the hardware model info to the driver. For others, add
1596
+ * a copy of IORT node pointer to platform_data to be used to
1597
+ * retrieve IORT data information.
14061598 */
1407
- ret = platform_device_add_data(pdev, &node, sizeof(node));
1599
+ if (ops->dev_add_platdata)
1600
+ ret = ops->dev_add_platdata(pdev);
1601
+ else
1602
+ ret = platform_device_add_data(pdev, &node, sizeof(node));
1603
+
14081604 if (ret)
14091605 goto dev_put;
1410
-
1411
- /*
1412
- * We expect the dma masks to be equivalent for
1413
- * all SMMUs set-ups
1414
- */
1415
- pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
14161606
14171607 fwnode = iort_get_fwnode(node);
14181608
....@@ -1423,11 +1613,8 @@
14231613
14241614 pdev->dev.fwnode = fwnode;
14251615
1426
- attr = ops->dev_is_coherent && ops->dev_is_coherent(node) ?
1427
- DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1428
-
1429
- /* Configure DMA for the page table walker */
1430
- acpi_dma_configure(&pdev->dev, attr);
1616
+ if (ops->dev_dma_configure)
1617
+ ops->dev_dma_configure(&pdev->dev, node);
14311618
14321619 iort_set_device_domain(&pdev->dev, node);
14331620
....@@ -1438,15 +1625,21 @@
14381625 return 0;
14391626
14401627 dma_deconfigure:
1441
- acpi_dma_deconfigure(&pdev->dev);
1628
+ arch_teardown_dma_ops(&pdev->dev);
14421629 dev_put:
14431630 platform_device_put(pdev);
14441631
14451632 return ret;
14461633 }
14471634
1448
-static bool __init iort_enable_acs(struct acpi_iort_node *iort_node)
1635
+#ifdef CONFIG_PCI
1636
+static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
14491637 {
1638
+ static bool acs_enabled __initdata;
1639
+
1640
+ if (acs_enabled)
1641
+ return;
1642
+
14501643 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
14511644 struct acpi_iort_node *parent;
14521645 struct acpi_iort_id_mapping *map;
....@@ -1468,13 +1661,15 @@
14681661 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
14691662 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
14701663 pci_request_acs();
1471
- return true;
1664
+ acs_enabled = true;
1665
+ return;
14721666 }
14731667 }
14741668 }
1475
-
1476
- return false;
14771669 }
1670
+#else
1671
+static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1672
+#endif
14781673
14791674 static void __init iort_init_platform_devices(void)
14801675 {
....@@ -1482,7 +1677,6 @@
14821677 struct acpi_table_iort *iort;
14831678 struct fwnode_handle *fwnode;
14841679 int i, ret;
1485
- bool acs_enabled = false;
14861680 const struct iort_dev_config *ops;
14871681
14881682 /*
....@@ -1503,8 +1697,7 @@
15031697 return;
15041698 }
15051699
1506
- if (!acs_enabled)
1507
- acs_enabled = iort_enable_acs(iort_node);
1700
+ iort_enable_acs(iort_node);
15081701
15091702 ops = iort_get_dev_cfg(iort_node);
15101703 if (ops) {
....@@ -1531,6 +1724,10 @@
15311724 {
15321725 acpi_status status;
15331726
1727
+ /* iort_table will be used at runtime after the iort init,
1728
+ * so we don't need to call acpi_put_table() to release
1729
+ * the IORT table mapping.
1730
+ */
15341731 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
15351732 if (ACPI_FAILURE(status)) {
15361733 if (status != AE_NOT_FOUND) {
....@@ -1544,3 +1741,58 @@
15441741
15451742 iort_init_platform_devices();
15461743 }
1744
+
1745
+#ifdef CONFIG_ZONE_DMA
1746
+/*
1747
+ * Extract the highest CPU physical address accessible to all DMA masters in
1748
+ * the system. PHYS_ADDR_MAX is returned when no constrained device is found.
1749
+ */
1750
+phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
1751
+{
1752
+ phys_addr_t limit = PHYS_ADDR_MAX;
1753
+ struct acpi_iort_node *node, *end;
1754
+ struct acpi_table_iort *iort;
1755
+ acpi_status status;
1756
+ int i;
1757
+
1758
+ if (acpi_disabled)
1759
+ return limit;
1760
+
1761
+ status = acpi_get_table(ACPI_SIG_IORT, 0,
1762
+ (struct acpi_table_header **)&iort);
1763
+ if (ACPI_FAILURE(status))
1764
+ return limit;
1765
+
1766
+ node = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->node_offset);
1767
+ end = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->header.length);
1768
+
1769
+ for (i = 0; i < iort->node_count; i++) {
1770
+ if (node >= end)
1771
+ break;
1772
+
1773
+ switch (node->type) {
1774
+ struct acpi_iort_named_component *ncomp;
1775
+ struct acpi_iort_root_complex *rc;
1776
+ phys_addr_t local_limit;
1777
+
1778
+ case ACPI_IORT_NODE_NAMED_COMPONENT:
1779
+ ncomp = (struct acpi_iort_named_component *)node->node_data;
1780
+ local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
1781
+ limit = min_not_zero(limit, local_limit);
1782
+ break;
1783
+
1784
+ case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
1785
+ if (node->revision < 1)
1786
+ break;
1787
+
1788
+ rc = (struct acpi_iort_root_complex *)node->node_data;
1789
+ local_limit = DMA_BIT_MASK(rc->memory_address_limit);
1790
+ limit = min_not_zero(limit, local_limit);
1791
+ break;
1792
+ }
1793
+ node = ACPI_ADD_PTR(struct acpi_iort_node, node, node->length);
1794
+ }
1795
+ acpi_put_table(&iort->header);
1796
+ return limit;
1797
+}
1798
+#endif