intel-pasid.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * intel-pasid.c - PASID idr, table and entry manipulation
  4. *
  5. * Copyright (C) 2018 Intel Corporation
  6. *
  7. * Author: Lu Baolu <baolu.lu@linux.intel.com>
  8. */
  9. #define pr_fmt(fmt) "DMAR: " fmt
  10. #include <linux/dmar.h>
  11. #include <linux/intel-iommu.h>
  12. #include <linux/iommu.h>
  13. #include <linux/memory.h>
  14. #include <linux/pci.h>
  15. #include <linux/pci-ats.h>
  16. #include <linux/spinlock.h>
  17. #include "intel-pasid.h"
  18. /*
  19. * Intel IOMMU system wide PASID name space:
  20. */
  21. static DEFINE_SPINLOCK(pasid_lock);
  22. u32 intel_pasid_max_id = PASID_MAX;
  23. static DEFINE_IDR(pasid_idr);
  24. int intel_pasid_alloc_id(void *ptr, int start, int end, gfp_t gfp)
  25. {
  26. int ret, min, max;
  27. min = max_t(int, start, PASID_MIN);
  28. max = min_t(int, end, intel_pasid_max_id);
  29. WARN_ON(in_interrupt());
  30. idr_preload(gfp);
  31. spin_lock(&pasid_lock);
  32. ret = idr_alloc(&pasid_idr, ptr, min, max, GFP_ATOMIC);
  33. spin_unlock(&pasid_lock);
  34. idr_preload_end();
  35. return ret;
  36. }
  37. void intel_pasid_free_id(int pasid)
  38. {
  39. spin_lock(&pasid_lock);
  40. idr_remove(&pasid_idr, pasid);
  41. spin_unlock(&pasid_lock);
  42. }
  43. void *intel_pasid_lookup_id(int pasid)
  44. {
  45. void *p;
  46. spin_lock(&pasid_lock);
  47. p = idr_find(&pasid_idr, pasid);
  48. spin_unlock(&pasid_lock);
  49. return p;
  50. }
  51. /*
  52. * Per device pasid table management:
  53. */
  54. static inline void
  55. device_attach_pasid_table(struct device_domain_info *info,
  56. struct pasid_table *pasid_table)
  57. {
  58. info->pasid_table = pasid_table;
  59. list_add(&info->table, &pasid_table->dev);
  60. }
  61. static inline void
  62. device_detach_pasid_table(struct device_domain_info *info,
  63. struct pasid_table *pasid_table)
  64. {
  65. info->pasid_table = NULL;
  66. list_del(&info->table);
  67. }
  68. struct pasid_table_opaque {
  69. struct pasid_table **pasid_table;
  70. int segment;
  71. int bus;
  72. int devfn;
  73. };
  74. static int search_pasid_table(struct device_domain_info *info, void *opaque)
  75. {
  76. struct pasid_table_opaque *data = opaque;
  77. if (info->iommu->segment == data->segment &&
  78. info->bus == data->bus &&
  79. info->devfn == data->devfn &&
  80. info->pasid_table) {
  81. *data->pasid_table = info->pasid_table;
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. static int get_alias_pasid_table(struct pci_dev *pdev, u16 alias, void *opaque)
  87. {
  88. struct pasid_table_opaque *data = opaque;
  89. data->segment = pci_domain_nr(pdev->bus);
  90. data->bus = PCI_BUS_NUM(alias);
  91. data->devfn = alias & 0xff;
  92. return for_each_device_domain(&search_pasid_table, data);
  93. }
  94. /*
  95. * Allocate a pasid table for @dev. It should be called in a
  96. * single-thread context.
  97. */
  98. int intel_pasid_alloc_table(struct device *dev)
  99. {
  100. struct device_domain_info *info;
  101. struct pasid_table *pasid_table;
  102. struct pasid_table_opaque data;
  103. struct page *pages;
  104. size_t size, count;
  105. int ret, order;
  106. info = dev->archdata.iommu;
  107. if (WARN_ON(!info || !dev_is_pci(dev) ||
  108. !info->pasid_supported || info->pasid_table))
  109. return -EINVAL;
  110. /* DMA alias device already has a pasid table, use it: */
  111. data.pasid_table = &pasid_table;
  112. ret = pci_for_each_dma_alias(to_pci_dev(dev),
  113. &get_alias_pasid_table, &data);
  114. if (ret)
  115. goto attach_out;
  116. pasid_table = kzalloc(sizeof(*pasid_table), GFP_ATOMIC);
  117. if (!pasid_table)
  118. return -ENOMEM;
  119. INIT_LIST_HEAD(&pasid_table->dev);
  120. size = sizeof(struct pasid_entry);
  121. count = min_t(int, pci_max_pasids(to_pci_dev(dev)), intel_pasid_max_id);
  122. order = get_order(size * count);
  123. pages = alloc_pages_node(info->iommu->node,
  124. GFP_ATOMIC | __GFP_ZERO,
  125. order);
  126. if (!pages)
  127. return -ENOMEM;
  128. pasid_table->table = page_address(pages);
  129. pasid_table->order = order;
  130. pasid_table->max_pasid = count;
  131. attach_out:
  132. device_attach_pasid_table(info, pasid_table);
  133. return 0;
  134. }
  135. void intel_pasid_free_table(struct device *dev)
  136. {
  137. struct device_domain_info *info;
  138. struct pasid_table *pasid_table;
  139. info = dev->archdata.iommu;
  140. if (!info || !dev_is_pci(dev) ||
  141. !info->pasid_supported || !info->pasid_table)
  142. return;
  143. pasid_table = info->pasid_table;
  144. device_detach_pasid_table(info, pasid_table);
  145. if (!list_empty(&pasid_table->dev))
  146. return;
  147. free_pages((unsigned long)pasid_table->table, pasid_table->order);
  148. kfree(pasid_table);
  149. }
  150. struct pasid_table *intel_pasid_get_table(struct device *dev)
  151. {
  152. struct device_domain_info *info;
  153. info = dev->archdata.iommu;
  154. if (!info)
  155. return NULL;
  156. return info->pasid_table;
  157. }
  158. int intel_pasid_get_dev_max_id(struct device *dev)
  159. {
  160. struct device_domain_info *info;
  161. info = dev->archdata.iommu;
  162. if (!info || !info->pasid_table)
  163. return 0;
  164. return info->pasid_table->max_pasid;
  165. }
  166. struct pasid_entry *intel_pasid_get_entry(struct device *dev, int pasid)
  167. {
  168. struct pasid_table *pasid_table;
  169. struct pasid_entry *entries;
  170. pasid_table = intel_pasid_get_table(dev);
  171. if (WARN_ON(!pasid_table || pasid < 0 ||
  172. pasid >= intel_pasid_get_dev_max_id(dev)))
  173. return NULL;
  174. entries = pasid_table->table;
  175. return &entries[pasid];
  176. }
  177. /*
  178. * Interfaces for PASID table entry manipulation:
  179. */
  180. static inline void pasid_clear_entry(struct pasid_entry *pe)
  181. {
  182. WRITE_ONCE(pe->val, 0);
  183. }
  184. void intel_pasid_clear_entry(struct device *dev, int pasid)
  185. {
  186. struct pasid_entry *pe;
  187. pe = intel_pasid_get_entry(dev, pasid);
  188. if (WARN_ON(!pe))
  189. return;
  190. pasid_clear_entry(pe);
  191. }