ppr.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2023 Advanced Micro Devices, Inc.
  4. */
  5. #define pr_fmt(fmt) "AMD-Vi: " fmt
  6. #define dev_fmt(fmt) pr_fmt(fmt)
  7. #include <linux/amd-iommu.h>
  8. #include <linux/delay.h>
  9. #include <linux/mmu_notifier.h>
  10. #include <asm/iommu.h>
  11. #include "amd_iommu.h"
  12. #include "amd_iommu_types.h"
  13. #include "../iommu-pages.h"
  14. int __init amd_iommu_alloc_ppr_log(struct amd_iommu *iommu)
  15. {
  16. iommu->ppr_log = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
  17. PPR_LOG_SIZE);
  18. return iommu->ppr_log ? 0 : -ENOMEM;
  19. }
  20. void amd_iommu_enable_ppr_log(struct amd_iommu *iommu)
  21. {
  22. u64 entry;
  23. if (iommu->ppr_log == NULL)
  24. return;
  25. iommu_feature_enable(iommu, CONTROL_PPR_EN);
  26. entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
  27. memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
  28. &entry, sizeof(entry));
  29. /* set head and tail to zero manually */
  30. writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  31. writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  32. iommu_feature_enable(iommu, CONTROL_PPRINT_EN);
  33. iommu_feature_enable(iommu, CONTROL_PPRLOG_EN);
  34. }
  35. void __init amd_iommu_free_ppr_log(struct amd_iommu *iommu)
  36. {
  37. iommu_free_pages(iommu->ppr_log);
  38. }
  39. /*
  40. * This function restarts ppr logging in case the IOMMU experienced
  41. * PPR log overflow.
  42. */
  43. void amd_iommu_restart_ppr_log(struct amd_iommu *iommu)
  44. {
  45. amd_iommu_restart_log(iommu, "PPR", CONTROL_PPRINT_EN,
  46. CONTROL_PPRLOG_EN, MMIO_STATUS_PPR_RUN_MASK,
  47. MMIO_STATUS_PPR_OVERFLOW_MASK);
  48. }
  49. static inline u32 ppr_flag_to_fault_perm(u16 flag)
  50. {
  51. int perm = 0;
  52. if (flag & PPR_FLAG_READ)
  53. perm |= IOMMU_FAULT_PERM_READ;
  54. if (flag & PPR_FLAG_WRITE)
  55. perm |= IOMMU_FAULT_PERM_WRITE;
  56. if (flag & PPR_FLAG_EXEC)
  57. perm |= IOMMU_FAULT_PERM_EXEC;
  58. if (!(flag & PPR_FLAG_US))
  59. perm |= IOMMU_FAULT_PERM_PRIV;
  60. return perm;
  61. }
  62. static bool ppr_is_valid(struct amd_iommu *iommu, u64 *raw)
  63. {
  64. struct device *dev = iommu->iommu.dev;
  65. u16 devid = PPR_DEVID(raw[0]);
  66. if (!(PPR_FLAGS(raw[0]) & PPR_FLAG_GN)) {
  67. dev_dbg(dev, "PPR logged [Request ignored due to GN=0 (device=%04x:%02x:%02x.%x "
  68. "pasid=0x%05llx address=0x%llx flags=0x%04llx tag=0x%03llx]\n",
  69. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  70. PPR_PASID(raw[0]), raw[1], PPR_FLAGS(raw[0]), PPR_TAG(raw[0]));
  71. return false;
  72. }
  73. if (PPR_FLAGS(raw[0]) & PPR_FLAG_RVSD) {
  74. dev_dbg(dev, "PPR logged [Invalid request format (device=%04x:%02x:%02x.%x "
  75. "pasid=0x%05llx address=0x%llx flags=0x%04llx tag=0x%03llx]\n",
  76. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  77. PPR_PASID(raw[0]), raw[1], PPR_FLAGS(raw[0]), PPR_TAG(raw[0]));
  78. return false;
  79. }
  80. return true;
  81. }
  82. static void iommu_call_iopf_notifier(struct amd_iommu *iommu, u64 *raw)
  83. {
  84. struct iommu_dev_data *dev_data;
  85. struct iopf_fault event;
  86. struct pci_dev *pdev;
  87. u16 devid = PPR_DEVID(raw[0]);
  88. if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
  89. pr_info_ratelimited("Unknown PPR request received\n");
  90. return;
  91. }
  92. pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id,
  93. PCI_BUS_NUM(devid), devid & 0xff);
  94. if (!pdev)
  95. return;
  96. if (!ppr_is_valid(iommu, raw))
  97. goto out;
  98. memset(&event, 0, sizeof(struct iopf_fault));
  99. event.fault.type = IOMMU_FAULT_PAGE_REQ;
  100. event.fault.prm.perm = ppr_flag_to_fault_perm(PPR_FLAGS(raw[0]));
  101. event.fault.prm.addr = (u64)(raw[1] & PAGE_MASK);
  102. event.fault.prm.pasid = PPR_PASID(raw[0]);
  103. event.fault.prm.grpid = PPR_TAG(raw[0]) & 0x1FF;
  104. /*
  105. * PASID zero is used for requests from the I/O device without
  106. * a PASID
  107. */
  108. dev_data = dev_iommu_priv_get(&pdev->dev);
  109. if (event.fault.prm.pasid == 0 ||
  110. event.fault.prm.pasid >= dev_data->max_pasids) {
  111. pr_info_ratelimited("Invalid PASID : 0x%x, device : 0x%x\n",
  112. event.fault.prm.pasid, pdev->dev.id);
  113. goto out;
  114. }
  115. event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
  116. event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
  117. if (PPR_TAG(raw[0]) & 0x200)
  118. event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
  119. /* Submit event */
  120. iommu_report_device_fault(&pdev->dev, &event);
  121. return;
  122. out:
  123. /* Nobody cared, abort */
  124. amd_iommu_complete_ppr(&pdev->dev, PPR_PASID(raw[0]),
  125. IOMMU_PAGE_RESP_FAILURE,
  126. PPR_TAG(raw[0]) & 0x1FF);
  127. }
  128. void amd_iommu_poll_ppr_log(struct amd_iommu *iommu)
  129. {
  130. u32 head, tail;
  131. if (iommu->ppr_log == NULL)
  132. return;
  133. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  134. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  135. while (head != tail) {
  136. volatile u64 *raw;
  137. u64 entry[2];
  138. int i;
  139. raw = (u64 *)(iommu->ppr_log + head);
  140. /*
  141. * Hardware bug: Interrupt may arrive before the entry is
  142. * written to memory. If this happens we need to wait for the
  143. * entry to arrive.
  144. */
  145. for (i = 0; i < LOOP_TIMEOUT; ++i) {
  146. if (PPR_REQ_TYPE(raw[0]) != 0)
  147. break;
  148. udelay(1);
  149. }
  150. /* Avoid memcpy function-call overhead */
  151. entry[0] = raw[0];
  152. entry[1] = raw[1];
  153. /*
  154. * To detect the hardware errata 733 we need to clear the
  155. * entry back to zero. This issue does not exist on SNP
  156. * enabled system. Also this buffer is not writeable on
  157. * SNP enabled system.
  158. */
  159. if (!amd_iommu_snp_en)
  160. raw[0] = raw[1] = 0UL;
  161. /* Update head pointer of hardware ring-buffer */
  162. head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
  163. writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  164. /* Handle PPR entry */
  165. iommu_call_iopf_notifier(iommu, entry);
  166. }
  167. }
  168. /**************************************************************
  169. *
  170. * IOPF handling stuff
  171. */
  172. /* Setup per-IOMMU IOPF queue if not exist. */
  173. int amd_iommu_iopf_init(struct amd_iommu *iommu)
  174. {
  175. int ret = 0;
  176. if (iommu->iopf_queue)
  177. return ret;
  178. snprintf(iommu->iopfq_name, sizeof(iommu->iopfq_name), "amdvi-%#x",
  179. PCI_SEG_DEVID_TO_SBDF(iommu->pci_seg->id, iommu->devid));
  180. iommu->iopf_queue = iopf_queue_alloc(iommu->iopfq_name);
  181. if (!iommu->iopf_queue)
  182. ret = -ENOMEM;
  183. return ret;
  184. }
  185. /* Destroy per-IOMMU IOPF queue if no longer needed. */
  186. void amd_iommu_iopf_uninit(struct amd_iommu *iommu)
  187. {
  188. iopf_queue_free(iommu->iopf_queue);
  189. iommu->iopf_queue = NULL;
  190. }
  191. void amd_iommu_page_response(struct device *dev, struct iopf_fault *evt,
  192. struct iommu_page_response *resp)
  193. {
  194. amd_iommu_complete_ppr(dev, resp->pasid, resp->code, resp->grpid);
  195. }
  196. int amd_iommu_iopf_add_device(struct amd_iommu *iommu,
  197. struct iommu_dev_data *dev_data)
  198. {
  199. int ret = 0;
  200. if (!dev_data->pri_enabled)
  201. return ret;
  202. if (!iommu->iopf_queue)
  203. return -EINVAL;
  204. ret = iopf_queue_add_device(iommu->iopf_queue, dev_data->dev);
  205. if (ret)
  206. return ret;
  207. dev_data->ppr = true;
  208. return 0;
  209. }
  210. /* Its assumed that caller has verified that device was added to iopf queue */
  211. void amd_iommu_iopf_remove_device(struct amd_iommu *iommu,
  212. struct iommu_dev_data *dev_data)
  213. {
  214. iopf_queue_remove_device(iommu->iopf_queue, dev_data->dev);
  215. dev_data->ppr = false;
  216. }