iommu.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/kernel.h>
  19. #include <linux/bug.h>
  20. #include <linux/types.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/errno.h>
  24. #include <linux/iommu.h>
  25. static struct iommu_ops *iommu_ops;
  26. void register_iommu(struct iommu_ops *ops)
  27. {
  28. if (iommu_ops)
  29. BUG();
  30. iommu_ops = ops;
  31. }
  32. bool iommu_found(void)
  33. {
  34. return iommu_ops != NULL;
  35. }
  36. EXPORT_SYMBOL_GPL(iommu_found);
  37. struct iommu_domain *iommu_domain_alloc(void)
  38. {
  39. struct iommu_domain *domain;
  40. int ret;
  41. domain = kmalloc(sizeof(*domain), GFP_KERNEL);
  42. if (!domain)
  43. return NULL;
  44. ret = iommu_ops->domain_init(domain);
  45. if (ret)
  46. goto out_free;
  47. return domain;
  48. out_free:
  49. kfree(domain);
  50. return NULL;
  51. }
  52. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  53. void iommu_domain_free(struct iommu_domain *domain)
  54. {
  55. iommu_ops->domain_destroy(domain);
  56. kfree(domain);
  57. }
  58. EXPORT_SYMBOL_GPL(iommu_domain_free);
  59. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  60. {
  61. return iommu_ops->attach_dev(domain, dev);
  62. }
  63. EXPORT_SYMBOL_GPL(iommu_attach_device);
  64. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  65. {
  66. iommu_ops->detach_dev(domain, dev);
  67. }
  68. EXPORT_SYMBOL_GPL(iommu_detach_device);
  69. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  70. unsigned long iova)
  71. {
  72. return iommu_ops->iova_to_phys(domain, iova);
  73. }
  74. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  75. int iommu_domain_has_cap(struct iommu_domain *domain,
  76. unsigned long cap)
  77. {
  78. return iommu_ops->domain_has_cap(domain, cap);
  79. }
  80. EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
  81. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  82. phys_addr_t paddr, int gfp_order, int prot)
  83. {
  84. size_t size;
  85. size = PAGE_SIZE << gfp_order;
  86. BUG_ON(!IS_ALIGNED(iova | paddr, size));
  87. return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
  88. }
  89. EXPORT_SYMBOL_GPL(iommu_map);
  90. int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
  91. {
  92. size_t size;
  93. size = PAGE_SIZE << gfp_order;
  94. BUG_ON(!IS_ALIGNED(iova, size));
  95. return iommu_ops->unmap(domain, iova, gfp_order);
  96. }
  97. EXPORT_SYMBOL_GPL(iommu_unmap);