iommu.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/bug.h>
  19. #include <linux/types.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/errno.h>
  23. #include <linux/iommu.h>
  24. static struct iommu_ops *iommu_ops;
  25. void register_iommu(struct iommu_ops *ops)
  26. {
  27. if (iommu_ops)
  28. BUG();
  29. iommu_ops = ops;
  30. }
  31. bool iommu_found(void)
  32. {
  33. return iommu_ops != NULL;
  34. }
  35. EXPORT_SYMBOL_GPL(iommu_found);
  36. /**
  37. * iommu_set_fault_handler() - set a fault handler for an iommu domain
  38. * @domain: iommu domain
  39. * @handler: fault handler
  40. */
  41. void iommu_set_fault_handler(struct iommu_domain *domain,
  42. iommu_fault_handler_t handler)
  43. {
  44. BUG_ON(!domain);
  45. domain->handler = handler;
  46. }
  47. struct iommu_domain *iommu_domain_alloc(void)
  48. {
  49. struct iommu_domain *domain;
  50. int ret;
  51. domain = kmalloc(sizeof(*domain), GFP_KERNEL);
  52. if (!domain)
  53. return NULL;
  54. ret = iommu_ops->domain_init(domain);
  55. if (ret)
  56. goto out_free;
  57. return domain;
  58. out_free:
  59. kfree(domain);
  60. return NULL;
  61. }
  62. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  63. void iommu_domain_free(struct iommu_domain *domain)
  64. {
  65. iommu_ops->domain_destroy(domain);
  66. kfree(domain);
  67. }
  68. EXPORT_SYMBOL_GPL(iommu_domain_free);
  69. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  70. {
  71. return iommu_ops->attach_dev(domain, dev);
  72. }
  73. EXPORT_SYMBOL_GPL(iommu_attach_device);
  74. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  75. {
  76. iommu_ops->detach_dev(domain, dev);
  77. }
  78. EXPORT_SYMBOL_GPL(iommu_detach_device);
  79. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  80. unsigned long iova)
  81. {
  82. return iommu_ops->iova_to_phys(domain, iova);
  83. }
  84. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  85. int iommu_domain_has_cap(struct iommu_domain *domain,
  86. unsigned long cap)
  87. {
  88. return iommu_ops->domain_has_cap(domain, cap);
  89. }
  90. EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
  91. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  92. phys_addr_t paddr, int gfp_order, int prot)
  93. {
  94. unsigned long invalid_mask;
  95. size_t size;
  96. size = 0x1000UL << gfp_order;
  97. invalid_mask = size - 1;
  98. BUG_ON((iova | paddr) & invalid_mask);
  99. return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
  100. }
  101. EXPORT_SYMBOL_GPL(iommu_map);
  102. int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
  103. {
  104. unsigned long invalid_mask;
  105. size_t size;
  106. size = 0x1000UL << gfp_order;
  107. invalid_mask = size - 1;
  108. BUG_ON(iova & invalid_mask);
  109. return iommu_ops->unmap(domain, iova, gfp_order);
  110. }
  111. EXPORT_SYMBOL_GPL(iommu_unmap);