nested.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2025 Advanced Micro Devices, Inc.
  4. */
  5. #define dev_fmt(fmt) "AMD-Vi: " fmt
  6. #include <linux/iommu.h>
  7. #include <linux/refcount.h>
  8. #include <uapi/linux/iommufd.h>
  9. #include "amd_iommu.h"
  10. static const struct iommu_domain_ops nested_domain_ops;
  11. static inline struct nested_domain *to_ndomain(struct iommu_domain *dom)
  12. {
  13. return container_of(dom, struct nested_domain, domain);
  14. }
  15. /*
  16. * Validate guest DTE to make sure that configuration for host (v1)
  17. * and guest (v2) page tables are valid when allocating nested domain.
  18. */
  19. static int validate_gdte_nested(struct iommu_hwpt_amd_guest *gdte)
  20. {
  21. u32 gpt_level = FIELD_GET(DTE_GPT_LEVEL_MASK, gdte->dte[2]);
  22. /* Must be zero: Mode, Host-TPR */
  23. if (FIELD_GET(DTE_MODE_MASK, gdte->dte[0]) != 0 ||
  24. FIELD_GET(DTE_HOST_TRP, gdte->dte[0]) != 0)
  25. return -EINVAL;
  26. /* GCR3 TRP must be non-zero if V, GV is set */
  27. if (FIELD_GET(DTE_FLAG_V, gdte->dte[0]) == 1 &&
  28. FIELD_GET(DTE_FLAG_GV, gdte->dte[0]) == 1 &&
  29. FIELD_GET(DTE_GCR3_14_12, gdte->dte[0]) == 0 &&
  30. FIELD_GET(DTE_GCR3_30_15, gdte->dte[1]) == 0 &&
  31. FIELD_GET(DTE_GCR3_51_31, gdte->dte[1]) == 0)
  32. return -EINVAL;
  33. /* Valid Guest Paging Mode values are 0 and 1 */
  34. if (gpt_level != GUEST_PGTABLE_4_LEVEL &&
  35. gpt_level != GUEST_PGTABLE_5_LEVEL)
  36. return -EINVAL;
  37. /* GLX = 3 is reserved */
  38. if (FIELD_GET(DTE_GLX, gdte->dte[0]) == 3)
  39. return -EINVAL;
  40. /*
  41. * We need to check host capability before setting
  42. * the Guest Paging Mode
  43. */
  44. if (gpt_level == GUEST_PGTABLE_5_LEVEL &&
  45. amd_iommu_gpt_level < PAGE_MODE_5_LEVEL)
  46. return -EOPNOTSUPP;
  47. return 0;
  48. }
  49. static void *gdom_info_load_or_alloc_locked(struct xarray *xa, unsigned long index)
  50. {
  51. struct guest_domain_mapping_info *elm, *res;
  52. elm = xa_load(xa, index);
  53. if (elm)
  54. return elm;
  55. xa_unlock(xa);
  56. elm = kzalloc_obj(struct guest_domain_mapping_info);
  57. xa_lock(xa);
  58. if (!elm)
  59. return ERR_PTR(-ENOMEM);
  60. res = __xa_cmpxchg(xa, index, NULL, elm, GFP_KERNEL);
  61. if (xa_is_err(res))
  62. res = ERR_PTR(xa_err(res));
  63. if (res) {
  64. kfree(elm);
  65. return res;
  66. }
  67. refcount_set(&elm->users, 0);
  68. return elm;
  69. }
  70. /*
  71. * This function is assigned to struct iommufd_viommu_ops.alloc_domain_nested()
  72. * during the call to struct iommu_ops.viommu_init().
  73. */
  74. struct iommu_domain *
  75. amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
  76. const struct iommu_user_data *user_data)
  77. {
  78. int ret;
  79. struct nested_domain *ndom;
  80. struct guest_domain_mapping_info *gdom_info;
  81. struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core);
  82. if (user_data->type != IOMMU_HWPT_DATA_AMD_GUEST)
  83. return ERR_PTR(-EOPNOTSUPP);
  84. ndom = kzalloc_obj(*ndom);
  85. if (!ndom)
  86. return ERR_PTR(-ENOMEM);
  87. ret = iommu_copy_struct_from_user(&ndom->gdte, user_data,
  88. IOMMU_HWPT_DATA_AMD_GUEST,
  89. dte);
  90. if (ret)
  91. goto out_err;
  92. ret = validate_gdte_nested(&ndom->gdte);
  93. if (ret)
  94. goto out_err;
  95. ndom->gdom_id = FIELD_GET(DTE_DOMID_MASK, ndom->gdte.dte[1]);
  96. ndom->domain.ops = &nested_domain_ops;
  97. ndom->domain.type = IOMMU_DOMAIN_NESTED;
  98. ndom->viommu = aviommu;
  99. /*
  100. * Normally, when a guest has multiple pass-through devices,
  101. * the IOMMU driver setup DTEs with the same stage-2 table and
  102. * use the same host domain ID (hDomId). In case of nested translation,
  103. * if the guest setup different stage-1 tables with same PASID,
  104. * IOMMU would use the same TLB tag. This will results in TLB
  105. * aliasing issue.
  106. *
  107. * The guest is assigning gDomIDs based on its own algorithm for managing
  108. * cache tags of (DomID, PASID). Within a single viommu, the nest parent domain
  109. * (w/ S2 table) is used by all DTEs. But we need to consistently map the gDomID
  110. * to a single hDomID. This is done using an xarray in the vIOMMU to
  111. * keep track of the gDomID mapping. When the S2 is changed, the INVALIDATE_IOMMU_PAGES
  112. * command must be issued for each hDomID in the xarray.
  113. */
  114. xa_lock(&aviommu->gdomid_array);
  115. gdom_info = gdom_info_load_or_alloc_locked(&aviommu->gdomid_array, ndom->gdom_id);
  116. if (IS_ERR(gdom_info)) {
  117. xa_unlock(&aviommu->gdomid_array);
  118. ret = PTR_ERR(gdom_info);
  119. goto out_err;
  120. }
  121. /* Check if gDomID exist */
  122. if (refcount_inc_not_zero(&gdom_info->users)) {
  123. ndom->gdom_info = gdom_info;
  124. xa_unlock(&aviommu->gdomid_array);
  125. pr_debug("%s: Found gdom_id=%#x, hdom_id=%#x\n",
  126. __func__, ndom->gdom_id, gdom_info->hdom_id);
  127. return &ndom->domain;
  128. }
  129. /* The gDomID does not exist. We allocate new hdom_id */
  130. gdom_info->hdom_id = amd_iommu_pdom_id_alloc();
  131. if (gdom_info->hdom_id <= 0) {
  132. __xa_cmpxchg(&aviommu->gdomid_array,
  133. ndom->gdom_id, gdom_info, NULL, GFP_ATOMIC);
  134. xa_unlock(&aviommu->gdomid_array);
  135. ret = -ENOSPC;
  136. goto out_err_gdom_info;
  137. }
  138. ndom->gdom_info = gdom_info;
  139. refcount_set(&gdom_info->users, 1);
  140. xa_unlock(&aviommu->gdomid_array);
  141. pr_debug("%s: Allocate gdom_id=%#x, hdom_id=%#x\n",
  142. __func__, ndom->gdom_id, gdom_info->hdom_id);
  143. return &ndom->domain;
  144. out_err_gdom_info:
  145. kfree(gdom_info);
  146. out_err:
  147. kfree(ndom);
  148. return ERR_PTR(ret);
  149. }
  150. static void set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *dom,
  151. struct iommu_dev_data *dev_data, struct dev_table_entry *new)
  152. {
  153. struct protection_domain *parent;
  154. struct nested_domain *ndom = to_ndomain(dom);
  155. struct iommu_hwpt_amd_guest *gdte = &ndom->gdte;
  156. struct pt_iommu_amdv1_hw_info pt_info;
  157. /*
  158. * The nest parent domain is attached during the call to the
  159. * struct iommu_ops.viommu_init(), which will be stored as part
  160. * of the struct amd_iommu_viommu.parent.
  161. */
  162. if (WARN_ON(!ndom->viommu || !ndom->viommu->parent))
  163. return;
  164. parent = ndom->viommu->parent;
  165. amd_iommu_make_clear_dte(dev_data, new);
  166. /* Retrieve the current pagetable info via the IOMMU PT API. */
  167. pt_iommu_amdv1_hw_info(&parent->amdv1, &pt_info);
  168. /*
  169. * Use domain ID from nested domain to program DTE.
  170. * See amd_iommu_alloc_domain_nested().
  171. */
  172. amd_iommu_set_dte_v1(dev_data, parent, ndom->gdom_info->hdom_id,
  173. &pt_info, new);
  174. /* GV is required for nested page table */
  175. new->data[0] |= DTE_FLAG_GV;
  176. /* Guest PPR */
  177. new->data[0] |= gdte->dte[0] & DTE_FLAG_PPR;
  178. /* Guest translation stuff */
  179. new->data[0] |= gdte->dte[0] & (DTE_GLX | DTE_FLAG_GIOV);
  180. /* GCR3 table */
  181. new->data[0] |= gdte->dte[0] & DTE_GCR3_14_12;
  182. new->data[1] |= gdte->dte[1] & (DTE_GCR3_30_15 | DTE_GCR3_51_31);
  183. /* Guest paging mode */
  184. new->data[2] |= gdte->dte[2] & DTE_GPT_LEVEL_MASK;
  185. }
  186. static int nested_attach_device(struct iommu_domain *dom, struct device *dev,
  187. struct iommu_domain *old)
  188. {
  189. struct dev_table_entry new = {0};
  190. struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
  191. struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
  192. int ret = 0;
  193. /*
  194. * Needs to make sure PASID is not enabled
  195. * for this attach path.
  196. */
  197. if (WARN_ON(dev_data->pasid_enabled))
  198. return -EINVAL;
  199. mutex_lock(&dev_data->mutex);
  200. set_dte_nested(iommu, dom, dev_data, &new);
  201. amd_iommu_update_dte(iommu, dev_data, &new);
  202. mutex_unlock(&dev_data->mutex);
  203. return ret;
  204. }
  205. static void nested_domain_free(struct iommu_domain *dom)
  206. {
  207. struct guest_domain_mapping_info *curr;
  208. struct nested_domain *ndom = to_ndomain(dom);
  209. struct amd_iommu_viommu *aviommu = ndom->viommu;
  210. xa_lock(&aviommu->gdomid_array);
  211. if (!refcount_dec_and_test(&ndom->gdom_info->users)) {
  212. xa_unlock(&aviommu->gdomid_array);
  213. return;
  214. }
  215. /*
  216. * The refcount for the gdom_id to hdom_id mapping is zero.
  217. * It is now safe to remove the mapping.
  218. */
  219. curr = __xa_cmpxchg(&aviommu->gdomid_array, ndom->gdom_id,
  220. ndom->gdom_info, NULL, GFP_ATOMIC);
  221. xa_unlock(&aviommu->gdomid_array);
  222. if (WARN_ON(!curr || xa_err(curr)))
  223. return;
  224. /* success */
  225. pr_debug("%s: Free gdom_id=%#x, hdom_id=%#x\n",
  226. __func__, ndom->gdom_id, curr->hdom_id);
  227. amd_iommu_pdom_id_free(ndom->gdom_info->hdom_id);
  228. kfree(curr);
  229. kfree(ndom);
  230. }
  231. static const struct iommu_domain_ops nested_domain_ops = {
  232. .attach_dev = nested_attach_device,
  233. .free = nested_domain_free,
  234. };