iommu.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. struct iommu_domain *iommu_domain_alloc(void)
  37. {
  38. struct iommu_domain *domain;
  39. int ret;
  40. domain = kmalloc(sizeof(*domain), GFP_KERNEL);
  41. if (!domain)
  42. return NULL;
  43. ret = iommu_ops->domain_init(domain);
  44. if (ret)
  45. goto out_free;
  46. return domain;
  47. out_free:
  48. kfree(domain);
  49. return NULL;
  50. }
  51. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  52. void iommu_domain_free(struct iommu_domain *domain)
  53. {
  54. iommu_ops->domain_destroy(domain);
  55. kfree(domain);
  56. }
  57. EXPORT_SYMBOL_GPL(iommu_domain_free);
  58. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  59. {
  60. return iommu_ops->attach_dev(domain, dev);
  61. }
  62. EXPORT_SYMBOL_GPL(iommu_attach_device);
  63. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  64. {
  65. iommu_ops->detach_dev(domain, dev);
  66. }
  67. EXPORT_SYMBOL_GPL(iommu_detach_device);
  68. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  69. unsigned long iova)
  70. {
  71. return iommu_ops->iova_to_phys(domain, iova);
  72. }
  73. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  74. int iommu_domain_has_cap(struct iommu_domain *domain,
  75. unsigned long cap)
  76. {
  77. return iommu_ops->domain_has_cap(domain, cap);
  78. }
  79. EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
  80. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  81. phys_addr_t paddr, int gfp_order, int prot)
  82. {
  83. unsigned long invalid_mask;
  84. size_t size;
  85. size = 0x1000UL << gfp_order;
  86. invalid_mask = size - 1;
  87. BUG_ON((iova | paddr) & invalid_mask);
  88. return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
  89. }
  90. EXPORT_SYMBOL_GPL(iommu_map);
  91. int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
  92. {
  93. unsigned long invalid_mask;
  94. size_t size;
  95. size = 0x1000UL << gfp_order;
  96. invalid_mask = size - 1;
  97. BUG_ON(iova & invalid_mask);
  98. return iommu_ops->unmap(domain, iova, gfp_order);
  99. }
  100. EXPORT_SYMBOL_GPL(iommu_unmap);