grant-dma-iommu.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Stub IOMMU driver which does nothing.
  4. * The main purpose of it being present is to reuse generic IOMMU device tree
  5. * bindings by Xen grant DMA-mapping layer.
  6. *
  7. * Copyright (C) 2022 EPAM Systems Inc.
  8. */
  9. #include <linux/iommu.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. struct grant_dma_iommu_device {
  13. struct device *dev;
  14. struct iommu_device iommu;
  15. };
  16. static struct iommu_device *grant_dma_iommu_probe_device(struct device *dev)
  17. {
  18. return ERR_PTR(-ENODEV);
  19. }
  20. /* Nothing is really needed here except a dummy probe_device callback */
  21. static const struct iommu_ops grant_dma_iommu_ops = {
  22. .probe_device = grant_dma_iommu_probe_device,
  23. };
  24. static const struct of_device_id grant_dma_iommu_of_match[] = {
  25. { .compatible = "xen,grant-dma" },
  26. { },
  27. };
  28. static int grant_dma_iommu_probe(struct platform_device *pdev)
  29. {
  30. struct grant_dma_iommu_device *mmu;
  31. int ret;
  32. mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
  33. if (!mmu)
  34. return -ENOMEM;
  35. mmu->dev = &pdev->dev;
  36. ret = iommu_device_register(&mmu->iommu, &grant_dma_iommu_ops, &pdev->dev);
  37. if (ret)
  38. return ret;
  39. platform_set_drvdata(pdev, mmu);
  40. return 0;
  41. }
  42. static int grant_dma_iommu_remove(struct platform_device *pdev)
  43. {
  44. struct grant_dma_iommu_device *mmu = platform_get_drvdata(pdev);
  45. platform_set_drvdata(pdev, NULL);
  46. iommu_device_unregister(&mmu->iommu);
  47. return 0;
  48. }
  49. static struct platform_driver grant_dma_iommu_driver = {
  50. .driver = {
  51. .name = "grant-dma-iommu",
  52. .of_match_table = grant_dma_iommu_of_match,
  53. },
  54. .probe = grant_dma_iommu_probe,
  55. .remove = grant_dma_iommu_remove,
  56. };
  57. static int __init grant_dma_iommu_init(void)
  58. {
  59. struct device_node *iommu_np;
  60. iommu_np = of_find_matching_node(NULL, grant_dma_iommu_of_match);
  61. if (!iommu_np)
  62. return 0;
  63. of_node_put(iommu_np);
  64. return platform_driver_register(&grant_dma_iommu_driver);
  65. }
  66. subsys_initcall(grant_dma_iommu_init);