270 lines
7.3 KiB
C
270 lines
7.3 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* Temporary AP DMA mapping qualification for the IPA v2.6L BAM port.
|
|
*
|
|
* The old platina DT omits the AP IOMMU stream. A generated overlay adds
|
|
* one temporary test device using the vendor AP stream (ANOC2, 0x19c0).
|
|
* The platform/IOMMU core attaches its default DMA domain. We do not
|
|
* manufacture physical DMA addresses or attach an identity domain.
|
|
*
|
|
* No IPA/BAM MMIO, clock change, IRQ, DMA submission, reset, QMI or netdev.
|
|
* All allocations and the overlay are removed before successful init.
|
|
*/
|
|
#include <linux/device.h>
|
|
#include <linux/dma-mapping.h>
|
|
#include <linux/firmware.h>
|
|
#include <linux/init.h>
|
|
#include <linux/iommu.h>
|
|
#include <linux/module.h>
|
|
#include <linux/of.h>
|
|
#include <linux/of_platform.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/scatterlist.h>
|
|
#include <linux/sysfs.h>
|
|
|
|
#include "ipa_v2_dma.h"
|
|
|
|
#define AP_STREAM_ID 0x19c0
|
|
#define ANOC2_PATH "/soc@0/iommu@16c0000"
|
|
#define TEST_PATH "/soc@0/platina-ipa-dma-test@147c0000"
|
|
#define TEST_COMPAT "xiaomi,platina-ipa-v2-dma-test"
|
|
#define TEST_BUFFERS 3
|
|
|
|
static char *overlay_firmware = "platina/ipa-v2-dma-test.dtbo";
|
|
module_param(overlay_firmware, charp, 0400);
|
|
MODULE_PARM_DESC(overlay_firmware, "Overlay generated from this boot's live DT");
|
|
|
|
static char snapshot[512];
|
|
static bool probe_seen;
|
|
static int probe_result = -ENODEV;
|
|
static int overlay_id;
|
|
|
|
static int snapshot_get(char *buffer, const struct kernel_param *kp)
|
|
{
|
|
return sysfs_emit(buffer, "%s\n", snapshot);
|
|
}
|
|
|
|
static const struct kernel_param_ops snapshot_ops = {
|
|
.get = snapshot_get,
|
|
};
|
|
module_param_cb(snapshot, &snapshot_ops, NULL, 0400);
|
|
MODULE_PARM_DESC(snapshot, "Mapping test result; no hardware DMA was submitted");
|
|
|
|
static int count_device(struct device *dev, void *data)
|
|
{
|
|
unsigned int *count = data;
|
|
|
|
(*count)++;
|
|
return 0;
|
|
}
|
|
|
|
static bool node_at_path(struct device_node *node, const char *path)
|
|
{
|
|
struct device_node *expected = of_find_node_by_path(path);
|
|
bool match = expected && node == expected;
|
|
|
|
of_node_put(expected);
|
|
return match;
|
|
}
|
|
|
|
/* Reject another declaration of the AP stream before adding a test device. */
|
|
static int ap_stream_unused(void)
|
|
{
|
|
struct of_phandle_args args;
|
|
struct device_node *node;
|
|
int i, ret;
|
|
|
|
for_each_node_with_property(node, "iommus") {
|
|
for (i = 0; ; i++) {
|
|
bool conflict;
|
|
|
|
ret = of_parse_phandle_with_args(node, "iommus",
|
|
"#iommu-cells", i, &args);
|
|
if (ret == -ENOENT)
|
|
break;
|
|
if (ret) {
|
|
of_node_put(node);
|
|
return ret;
|
|
}
|
|
conflict = node_at_path(args.np, ANOC2_PATH) &&
|
|
args.args_count && args.args[0] == AP_STREAM_ID;
|
|
|
|
of_node_put(args.np);
|
|
if (conflict) {
|
|
pr_err("%s: AP stream already declared by %pOF\n",
|
|
KBUILD_MODNAME, node);
|
|
of_node_put(node);
|
|
return -EBUSY;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int dma_test_probe(struct platform_device *pdev)
|
|
{
|
|
struct device *dev = &pdev->dev;
|
|
struct of_phandle_args args;
|
|
struct iommu_domain *domain;
|
|
struct iommu_group *group;
|
|
struct resource *res;
|
|
void *cpu[TEST_BUFFERS] = { };
|
|
dma_addr_t dma[TEST_BUFFERS] = { };
|
|
unsigned int count = 0;
|
|
int ret, i, group_id;
|
|
|
|
probe_seen = true;
|
|
ret = -EINVAL;
|
|
if (!node_at_path(dev->of_node, TEST_PATH))
|
|
goto out_result;
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
if (!res || res->start != 0x147c0000 || resource_size(res) != 0x1000)
|
|
goto out_result;
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
|
|
if (!res || res->start != 0x14784000 || resource_size(res) != 0x26934)
|
|
goto out_result;
|
|
ret = of_parse_phandle_with_args(dev->of_node, "iommus", "#iommu-cells",
|
|
0, &args);
|
|
if (ret)
|
|
goto out_result;
|
|
ret = args.args_count == 1 && args.args[0] == AP_STREAM_ID &&
|
|
node_at_path(args.np, ANOC2_PATH) ? 0 : -EINVAL;
|
|
of_node_put(args.np);
|
|
if (ret)
|
|
goto out_result;
|
|
|
|
domain = iommu_get_domain_for_dev(dev);
|
|
if (!domain || domain->type != IOMMU_DOMAIN_DMA) {
|
|
ret = -EOPNOTSUPP;
|
|
dev_err(dev, "strict translated DMA domain required\n");
|
|
goto out_result;
|
|
}
|
|
group = iommu_group_get(dev);
|
|
if (!group) {
|
|
ret = -ENODEV;
|
|
goto out_result;
|
|
}
|
|
ret = iommu_group_for_each_dev(group, &count, count_device);
|
|
group_id = iommu_group_id(group);
|
|
iommu_group_put(group);
|
|
if (ret || count != 1) {
|
|
ret = ret ?: -EBUSY;
|
|
goto out_result;
|
|
}
|
|
ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
|
|
if (ret)
|
|
goto out_result;
|
|
for (i = 0; i < TEST_BUFFERS; i++) {
|
|
cpu[i] = dma_alloc_coherent(dev, PAGE_SIZE, &dma[i], GFP_KERNEL);
|
|
if (!cpu[i]) {
|
|
ret = -ENOMEM;
|
|
goto out_free;
|
|
}
|
|
ret = ipa_v2_verify_mapping(dev, domain, cpu[i], dma[i]);
|
|
if (ret)
|
|
goto out_free;
|
|
}
|
|
scnprintf(snapshot, sizeof(snapshot),
|
|
"{\"datapath_ready\":false,\"mapping_verified\":true,"
|
|
"\"hardware_dma_verified\":false,\"stream_id\":%u,"
|
|
"\"iommu_group\":%d,\"domain_type\":%u,\"dma_bits\":32,"
|
|
"\"page_size\":%lu,\"buffers\":%u,"
|
|
"\"iovas\":[%llu,%llu,%llu]}",
|
|
AP_STREAM_ID, group_id, domain->type, PAGE_SIZE, TEST_BUFFERS,
|
|
(unsigned long long)dma[0], (unsigned long long)dma[1],
|
|
(unsigned long long)dma[2]);
|
|
dev_info(dev, "mapping verified: %s\n", snapshot);
|
|
out_free:
|
|
for (i = TEST_BUFFERS - 1; i >= 0; i--)
|
|
if (cpu[i])
|
|
dma_free_coherent(dev, PAGE_SIZE, cpu[i], dma[i]);
|
|
out_result:
|
|
probe_result = ret;
|
|
if (ret)
|
|
dev_err(dev, "DMA mapping test failed: %d\n", ret);
|
|
return ret;
|
|
}
|
|
|
|
static const struct of_device_id dma_test_match[] = {
|
|
{ .compatible = TEST_COMPAT },
|
|
{ }
|
|
};
|
|
|
|
/* No MODULE_DEVICE_TABLE: explicit loading only, never an automatic service. */
|
|
static struct platform_driver dma_test_driver = {
|
|
.probe = dma_test_probe,
|
|
.driver = {
|
|
.name = "platina-ipa-v2-dma-test",
|
|
.of_match_table = dma_test_match,
|
|
.probe_type = PROBE_FORCE_SYNCHRONOUS,
|
|
.suppress_bind_attrs = true,
|
|
},
|
|
};
|
|
|
|
static int __init dma_test_init(void)
|
|
{
|
|
const struct firmware *firmware;
|
|
struct platform_device *original;
|
|
struct device_node *node;
|
|
int ret, remove_ret;
|
|
|
|
if (!of_machine_is_compatible("xiaomi,platina"))
|
|
return -ENODEV;
|
|
node = of_find_compatible_node(NULL, NULL, "qcom,sdm660-ipa");
|
|
if (!node)
|
|
return -ENODEV;
|
|
original = of_find_device_by_node(node);
|
|
of_node_put(node);
|
|
if (!original)
|
|
return -ENODEV;
|
|
|
|
device_lock(&original->dev);
|
|
if (original->dev.driver || iommu_get_domain_for_dev(&original->dev)) {
|
|
ret = -EBUSY;
|
|
goto out_device;
|
|
}
|
|
ret = ap_stream_unused();
|
|
if (ret)
|
|
goto out_device;
|
|
ret = request_firmware_direct(&firmware, overlay_firmware, &original->dev);
|
|
if (ret)
|
|
goto out_device;
|
|
if (!firmware->size || firmware->size > 4096) {
|
|
ret = -EINVAL;
|
|
goto out_firmware;
|
|
}
|
|
ret = platform_driver_register(&dma_test_driver);
|
|
if (ret)
|
|
goto out_firmware;
|
|
ret = of_overlay_fdt_apply(firmware->data, firmware->size, &overlay_id, NULL);
|
|
if (!ret)
|
|
ret = probe_seen ? probe_result : -ENODEV;
|
|
platform_driver_unregister(&dma_test_driver);
|
|
if (overlay_id > 0) {
|
|
remove_ret = of_overlay_remove(&overlay_id);
|
|
if (remove_ret) {
|
|
pr_err("%s: overlay cleanup failed: %d\n",
|
|
KBUILD_MODNAME, remove_ret);
|
|
ret = remove_ret;
|
|
}
|
|
}
|
|
if (!ret)
|
|
pr_info("%s: buffers freed, test device and overlay removed\n",
|
|
KBUILD_MODNAME);
|
|
out_firmware:
|
|
release_firmware(firmware);
|
|
out_device:
|
|
device_unlock(&original->dev);
|
|
put_device(&original->dev);
|
|
return ret;
|
|
}
|
|
|
|
static void __exit dma_test_exit(void)
|
|
{
|
|
}
|
|
|
|
module_init(dma_test_init);
|
|
module_exit(dma_test_exit);
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_DESCRIPTION("Platina IPA v2.6L temporary strict IOMMU mapping test");
|