@@ -1467,14 +1467,15 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client,
}
EXPORT_SYMBOL_IF_KUNIT(calc_map_type_and_dist);
-static unsigned int pci_p2pdma_client_tlp_flags(struct pci_dev *client,
- unsigned int tlp_flags)
+VISIBLE_IF_KUNIT unsigned int
+pci_p2pdma_client_tlp_flags(struct pci_dev *client, unsigned int tlp_flags)
{
if (client->ats_enabled)
return tlp_flags | PCI_P2PDMA_TLP_TRANSLATED;
return tlp_flags & ~PCI_P2PDMA_TLP_TRANSLATED;
}
+EXPORT_SYMBOL_IF_KUNIT(pci_p2pdma_client_tlp_flags);
/*
* A client that translates every address it is handed cannot be handed a bus
@@ -1486,7 +1487,7 @@ static unsigned int pci_p2pdma_client_tlp_flags(struct pci_dev *client,
* Take the Address Type from @tlp_flags rather than reading the ATS Enable
* bit again, so the policy and the class it applies to always agree.
*/
-static enum pci_p2pdma_map_type
+VISIBLE_IF_KUNIT enum pci_p2pdma_map_type
pci_p2pdma_client_map_type(struct pci_dev *provider, struct pci_dev *client,
bool per_mapping, unsigned int tlp_flags,
enum pci_p2pdma_map_type type)
@@ -1505,6 +1506,7 @@ pci_p2pdma_client_map_type(struct pci_dev *provider, struct pci_dev *client,
return PCI_P2PDMA_MAP_NOT_SUPPORTED;
}
+EXPORT_SYMBOL_IF_KUNIT(pci_p2pdma_client_map_type);
/* Whether the driver of @client called pcim_p2pdma_set_ats_per_mapping(). */
static bool pci_p2pdma_client_per_mapping(struct pci_dev *client)
@@ -1148,6 +1148,12 @@ enum pci_p2pdma_map_type calc_map_type_and_dist(struct pci_dev *provider,
int *dist,
unsigned int tlp_flags,
bool verbose);
+unsigned int pci_p2pdma_client_tlp_flags(struct pci_dev *client,
+ unsigned int tlp_flags);
+enum pci_p2pdma_map_type
+pci_p2pdma_client_map_type(struct pci_dev *provider, struct pci_dev *client,
+ bool per_mapping, unsigned int tlp_flags,
+ enum pci_p2pdma_map_type type);
#endif
#ifdef CONFIG_PCI_QUIRKS
@@ -1228,6 +1228,125 @@ static void acs_walk_unreadable_control_test(struct kunit *test)
PCI_P2PDMA_MAP_NOT_SUPPORTED);
}
+/*
+ * Route the way pci_p2pdma_map_type_tlp() does once it has a client: the
+ * client decides which flags apply, and the answer is then checked against
+ * what the client can be handed.
+ */
+static enum pci_p2pdma_map_type acs_client_map(struct acs_fabric *f,
+ bool per_mapping,
+ unsigned int tlp_flags)
+{
+ enum pci_p2pdma_map_type type;
+ int dist;
+
+ if (!per_mapping)
+ tlp_flags = pci_p2pdma_client_tlp_flags(f->client, tlp_flags);
+ type = calc_map_type_and_dist(f->provider, f->client, &dist, tlp_flags,
+ false);
+ return pci_p2pdma_client_map_type(f->provider, f->client, per_mapping,
+ tlp_flags, type);
+}
+
+/*
+ * A client with ATS enabled for the whole device translates whatever it is
+ * handed, so it never gets bus addresses even on a direct route, and
+ * Translation Blocking rejects it even when its caller asks for the default
+ * class.
+ */
+static void acs_client_ats_test(struct kunit *test)
+{
+ struct acs_fabric f = {};
+
+ acs_build_fabric(test, &f);
+ f.client->ats_enabled = 1;
+
+ KUNIT_EXPECT_EQ(test, acs_client_map(&f, false, 0),
+ PCI_P2PDMA_MAP_THRU_HOST_BRIDGE);
+
+ f.client_leaf->acs_cap = 0x100;
+ f.client_leaf->acs_capabilities = PCI_ACS_TB;
+ f.client_leaf_cfg.ctrl = PCI_ACS_TB;
+
+ KUNIT_EXPECT_EQ(test, acs_client_map(&f, false, 0),
+ PCI_P2PDMA_MAP_NOT_SUPPORTED);
+}
+
+/*
+ * The cache holds what the topology allows, filled when the client first
+ * asked. A client whose ATS was enabled after that must still be kept off the
+ * bus addresses the cache recorded.
+ */
+static void acs_client_ats_cached_test(struct kunit *test)
+{
+ enum pci_p2pdma_map_type type[PCI_P2PDMA_TLP_CLASSES], hit;
+ struct acs_fabric f = {};
+ unsigned long cached;
+ unsigned int flags;
+ int dist;
+
+ acs_build_fabric(test, &f);
+ for (flags = 0; flags < PCI_P2PDMA_TLP_CLASSES; flags++)
+ type[flags] = calc_map_type_and_dist(f.provider, f.client,
+ &dist, flags, false);
+ cached = pci_p2pdma_map_types_pack(type);
+
+ f.client->ats_enabled = 1;
+ flags = pci_p2pdma_client_tlp_flags(f.client, 0);
+ hit = pci_p2pdma_map_types_unpack(cached, flags);
+
+ KUNIT_EXPECT_EQ(test,
+ pci_p2pdma_client_map_type(f.provider, f.client, false,
+ flags, hit),
+ PCI_P2PDMA_MAP_THRU_HOST_BRIDGE);
+}
+
+/*
+ * A client that declared per-mapping ATS picks the Address Type per mapping,
+ * so it keeps the per-class answers even with ATS enabled.
+ */
+static void acs_client_ats_per_mapping_test(struct kunit *test)
+{
+ struct acs_fabric f = {};
+
+ acs_build_fabric(test, &f);
+ f.client->ats_enabled = 1;
+
+ KUNIT_EXPECT_EQ(test, acs_client_map(&f, true, 0),
+ PCI_P2PDMA_MAP_BUS_ADDR);
+ KUNIT_EXPECT_EQ(test,
+ acs_client_map(&f, true, PCI_P2PDMA_TLP_TRANSLATED),
+ PCI_P2PDMA_MAP_BUS_ADDR);
+
+ f.client_leaf->acs_cap = 0x100;
+ f.client_leaf->acs_capabilities = PCI_ACS_TB;
+ f.client_leaf_cfg.ctrl = PCI_ACS_TB;
+
+ KUNIT_EXPECT_EQ(test, acs_client_map(&f, true, 0),
+ PCI_P2PDMA_MAP_BUS_ADDR);
+ KUNIT_EXPECT_EQ(test,
+ acs_client_map(&f, true, PCI_P2PDMA_TLP_TRANSLATED),
+ PCI_P2PDMA_MAP_NOT_SUPPORTED);
+}
+
+/*
+ * A client with ATS disabled cannot issue Translated Requests, so a caller
+ * asking about them gets the answer for the Untranslated ones it will issue.
+ */
+static void acs_client_no_ats_test(struct kunit *test)
+{
+ struct acs_fabric f = {};
+
+ acs_build_fabric(test, &f);
+ f.dn1->acs_cap = 0x100;
+ f.dn1->acs_capabilities = PCI_ACS_RR | PCI_ACS_DT;
+ f.dn1_cfg.ctrl = PCI_ACS_RR | PCI_ACS_DT;
+
+ KUNIT_EXPECT_EQ(test,
+ acs_client_map(&f, false, PCI_P2PDMA_TLP_TRANSLATED),
+ PCI_P2PDMA_MAP_THRU_HOST_BRIDGE);
+}
+
static struct kunit_case pci_acs_test_cases[] = {
KUNIT_CASE_PARAM(pci_acs_p2pdma_request_test,
acs_request_gen_params),
@@ -1272,6 +1391,10 @@ static struct kunit_case pci_acs_test_cases[] = {
acs_intel_tb_gen_params),
KUNIT_CASE(acs_walk_unreadable_control_test),
KUNIT_CASE(acs_map_types_pack_test),
+ KUNIT_CASE(acs_client_ats_test),
+ KUNIT_CASE(acs_client_ats_cached_test),
+ KUNIT_CASE(acs_client_ats_per_mapping_test),
+ KUNIT_CASE(acs_client_no_ats_test),
{}
};