iommu.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /**
  58. * iommu_set_fault_handler() - set a fault handler for an iommu domain
  59. * @domain: iommu domain
  60. * @handler: fault handler
  61. *
  62. * This function should be used by IOMMU users which want to be notified
  63. * whenever an IOMMU fault happens.
  64. *
  65. * The fault handler itself should return 0 on success, and an appropriate
  66. * error code otherwise.
  67. */
  68. void iommu_set_fault_handler(struct iommu_domain *domain,
  69. iommu_fault_handler_t handler)
  70. {
  71. BUG_ON(!domain);
  72. domain->handler = handler;
  73. }
  74. EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
  75. struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  76. {
  77. struct iommu_domain *domain;
  78. int ret;
  79. if (bus == NULL || bus->iommu_ops == NULL)
  80. return NULL;
  81. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  82. if (!domain)
  83. return NULL;
  84. domain->ops = bus->iommu_ops;
  85. ret = domain->ops->domain_init(domain);
  86. if (ret)
  87. goto out_free;
  88. return domain;
  89. out_free:
  90. kfree(domain);
  91. return NULL;
  92. }
  93. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  94. void iommu_domain_free(struct iommu_domain *domain)
  95. {
  96. if (likely(domain->ops->domain_destroy != NULL))
  97. domain->ops->domain_destroy(domain);
  98. kfree(domain);
  99. }
  100. EXPORT_SYMBOL_GPL(iommu_domain_free);
  101. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  102. {
  103. if (unlikely(domain->ops->attach_dev == NULL))
  104. return -ENODEV;
  105. return domain->ops->attach_dev(domain, dev);
  106. }
  107. EXPORT_SYMBOL_GPL(iommu_attach_device);
  108. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  109. {
  110. if (unlikely(domain->ops->detach_dev == NULL))
  111. return;
  112. domain->ops->detach_dev(domain, dev);
  113. }
  114. EXPORT_SYMBOL_GPL(iommu_detach_device);
  115. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  116. unsigned long iova)
  117. {
  118. if (unlikely(domain->ops->iova_to_phys == NULL))
  119. return 0;
  120. return domain->ops->iova_to_phys(domain, iova);
  121. }
  122. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  123. int iommu_domain_has_cap(struct iommu_domain *domain,
  124. unsigned long cap)
  125. {
  126. if (unlikely(domain->ops->domain_has_cap == NULL))
  127. return 0;
  128. return domain->ops->domain_has_cap(domain, cap);
  129. }
  130. EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
  131. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  132. phys_addr_t paddr, int gfp_order, int prot)
  133. {
  134. size_t size;
  135. if (unlikely(domain->ops->map == NULL))
  136. return -ENODEV;
  137. size = PAGE_SIZE << gfp_order;
  138. BUG_ON(!IS_ALIGNED(iova | paddr, size));
  139. return domain->ops->map(domain, iova, paddr, gfp_order, prot);
  140. }
  141. EXPORT_SYMBOL_GPL(iommu_map);
  142. int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
  143. {
  144. size_t size;
  145. if (unlikely(domain->ops->unmap == NULL))
  146. return -ENODEV;
  147. size = PAGE_SIZE << gfp_order;
  148. BUG_ON(!IS_ALIGNED(iova, size));
  149. return domain->ops->unmap(domain, iova, gfp_order);
  150. }
  151. EXPORT_SYMBOL_GPL(iommu_unmap);