iommu.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/device.h>
  19. #include <linux/kernel.h>
  20. #include <linux/bug.h>
  21. #include <linux/types.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/errno.h>
  25. #include <linux/iommu.h>
  26. static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
  27. {
  28. }
  29. /**
  30. * bus_set_iommu - set iommu-callbacks for the bus
  31. * @bus: bus.
  32. * @ops: the callbacks provided by the iommu-driver
  33. *
  34. * This function is called by an iommu driver to set the iommu methods
  35. * used for a particular bus. Drivers for devices on that bus can use
  36. * the iommu-api after these ops are registered.
  37. * This special function is needed because IOMMUs are usually devices on
  38. * the bus itself, so the iommu drivers are not initialized when the bus
  39. * is set up. With this function the iommu-driver can set the iommu-ops
  40. * afterwards.
  41. */
  42. int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
  43. {
  44. if (bus->iommu_ops != NULL)
  45. return -EBUSY;
  46. bus->iommu_ops = ops;
  47. /* Do IOMMU specific setup for this bus-type */
  48. iommu_bus_init(bus, ops);
  49. return 0;
  50. }
  51. EXPORT_SYMBOL_GPL(bus_set_iommu);
  52. bool iommu_present(struct bus_type *bus)
  53. {
  54. return bus->iommu_ops != NULL;
  55. }
  56. EXPORT_SYMBOL_GPL(iommu_present);
  57. struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  58. {
  59. struct iommu_domain *domain;
  60. int ret;
  61. if (bus == NULL || bus->iommu_ops == NULL)
  62. return NULL;
  63. domain = kmalloc(sizeof(*domain), GFP_KERNEL);
  64. if (!domain)
  65. return NULL;
  66. domain->ops = bus->iommu_ops;
  67. ret = domain->ops->domain_init(domain);
  68. if (ret)
  69. goto out_free;
  70. return domain;
  71. out_free:
  72. kfree(domain);
  73. return NULL;
  74. }
  75. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  76. void iommu_domain_free(struct iommu_domain *domain)
  77. {
  78. if (likely(domain->ops->domain_destroy != NULL))
  79. domain->ops->domain_destroy(domain);
  80. kfree(domain);
  81. }
  82. EXPORT_SYMBOL_GPL(iommu_domain_free);
  83. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  84. {
  85. if (unlikely(domain->ops->attach_dev == NULL))
  86. return -ENODEV;
  87. return domain->ops->attach_dev(domain, dev);
  88. }
  89. EXPORT_SYMBOL_GPL(iommu_attach_device);
  90. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  91. {
  92. if (unlikely(domain->ops->detach_dev == NULL))
  93. return;
  94. domain->ops->detach_dev(domain, dev);
  95. }
  96. EXPORT_SYMBOL_GPL(iommu_detach_device);
  97. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  98. unsigned long iova)
  99. {
  100. if (unlikely(domain->ops->iova_to_phys == NULL))
  101. return 0;
  102. return domain->ops->iova_to_phys(domain, iova);
  103. }
  104. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  105. int iommu_domain_has_cap(struct iommu_domain *domain,
  106. unsigned long cap)
  107. {
  108. if (unlikely(domain->ops->domain_has_cap == NULL))
  109. return 0;
  110. return domain->ops->domain_has_cap(domain, cap);
  111. }
  112. EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
  113. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  114. phys_addr_t paddr, int gfp_order, int prot)
  115. {
  116. size_t size;
  117. if (unlikely(domain->ops->map == NULL))
  118. return -ENODEV;
  119. size = PAGE_SIZE << gfp_order;
  120. BUG_ON(!IS_ALIGNED(iova | paddr, size));
  121. return domain->ops->map(domain, iova, paddr, gfp_order, prot);
  122. }
  123. EXPORT_SYMBOL_GPL(iommu_map);
  124. int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
  125. {
  126. size_t size;
  127. if (unlikely(domain->ops->unmap == NULL))
  128. return -ENODEV;
  129. size = PAGE_SIZE << gfp_order;
  130. BUG_ON(!IS_ALIGNED(iova, size));
  131. return domain->ops->unmap(domain, iova, gfp_order);
  132. }
  133. EXPORT_SYMBOL_GPL(iommu_unmap);