cxllib.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2017 IBM Corp.
  4. */
  5. #include <linux/hugetlb.h>
  6. #include <linux/sched/mm.h>
  7. #include <asm/opal-api.h>
  8. #include <asm/pnv-pci.h>
  9. #include <misc/cxllib.h>
  10. #include "cxl.h"
  11. #define CXL_INVALID_DRA ~0ull
  12. #define CXL_DUMMY_READ_SIZE 128
  13. #define CXL_DUMMY_READ_ALIGN 8
  14. #define CXL_CAPI_WINDOW_START 0x2000000000000ull
  15. #define CXL_CAPI_WINDOW_LOG_SIZE 48
  16. #define CXL_XSL_CONFIG_CURRENT_VERSION CXL_XSL_CONFIG_VERSION1
  17. bool cxllib_slot_is_supported(struct pci_dev *dev, unsigned long flags)
  18. {
  19. int rc;
  20. u32 phb_index;
  21. u64 chip_id, capp_unit_id;
  22. /* No flags currently supported */
  23. if (flags)
  24. return false;
  25. if (!cpu_has_feature(CPU_FTR_HVMODE))
  26. return false;
  27. if (!cxl_is_power9())
  28. return false;
  29. if (cxl_slot_is_switched(dev))
  30. return false;
  31. /* on p9, some pci slots are not connected to a CAPP unit */
  32. rc = cxl_calc_capp_routing(dev, &chip_id, &phb_index, &capp_unit_id);
  33. if (rc)
  34. return false;
  35. return true;
  36. }
  37. EXPORT_SYMBOL_GPL(cxllib_slot_is_supported);
  38. static DEFINE_MUTEX(dra_mutex);
  39. static u64 dummy_read_addr = CXL_INVALID_DRA;
  40. static int allocate_dummy_read_buf(void)
  41. {
  42. u64 buf, vaddr;
  43. size_t buf_size;
  44. /*
  45. * Dummy read buffer is 128-byte long, aligned on a
  46. * 256-byte boundary and we need the physical address.
  47. */
  48. buf_size = CXL_DUMMY_READ_SIZE + (1ull << CXL_DUMMY_READ_ALIGN);
  49. buf = (u64) kzalloc(buf_size, GFP_KERNEL);
  50. if (!buf)
  51. return -ENOMEM;
  52. vaddr = (buf + (1ull << CXL_DUMMY_READ_ALIGN) - 1) &
  53. (~0ull << CXL_DUMMY_READ_ALIGN);
  54. WARN((vaddr + CXL_DUMMY_READ_SIZE) > (buf + buf_size),
  55. "Dummy read buffer alignment issue");
  56. dummy_read_addr = virt_to_phys((void *) vaddr);
  57. return 0;
  58. }
  59. int cxllib_get_xsl_config(struct pci_dev *dev, struct cxllib_xsl_config *cfg)
  60. {
  61. int rc;
  62. u32 phb_index;
  63. u64 chip_id, capp_unit_id;
  64. if (!cpu_has_feature(CPU_FTR_HVMODE))
  65. return -EINVAL;
  66. mutex_lock(&dra_mutex);
  67. if (dummy_read_addr == CXL_INVALID_DRA) {
  68. rc = allocate_dummy_read_buf();
  69. if (rc) {
  70. mutex_unlock(&dra_mutex);
  71. return rc;
  72. }
  73. }
  74. mutex_unlock(&dra_mutex);
  75. rc = cxl_calc_capp_routing(dev, &chip_id, &phb_index, &capp_unit_id);
  76. if (rc)
  77. return rc;
  78. rc = cxl_get_xsl9_dsnctl(dev, capp_unit_id, &cfg->dsnctl);
  79. if (rc)
  80. return rc;
  81. cfg->version = CXL_XSL_CONFIG_CURRENT_VERSION;
  82. cfg->log_bar_size = CXL_CAPI_WINDOW_LOG_SIZE;
  83. cfg->bar_addr = CXL_CAPI_WINDOW_START;
  84. cfg->dra = dummy_read_addr;
  85. return 0;
  86. }
  87. EXPORT_SYMBOL_GPL(cxllib_get_xsl_config);
  88. int cxllib_switch_phb_mode(struct pci_dev *dev, enum cxllib_mode mode,
  89. unsigned long flags)
  90. {
  91. int rc = 0;
  92. if (!cpu_has_feature(CPU_FTR_HVMODE))
  93. return -EINVAL;
  94. switch (mode) {
  95. case CXL_MODE_PCI:
  96. /*
  97. * We currently don't support going back to PCI mode
  98. * However, we'll turn the invalidations off, so that
  99. * the firmware doesn't have to ack them and can do
  100. * things like reset, etc.. with no worries.
  101. * So always return EPERM (can't go back to PCI) or
  102. * EBUSY if we couldn't even turn off snooping
  103. */
  104. rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_SNOOP_OFF);
  105. if (rc)
  106. rc = -EBUSY;
  107. else
  108. rc = -EPERM;
  109. break;
  110. case CXL_MODE_CXL:
  111. /* DMA only supported on TVT1 for the time being */
  112. if (flags != CXL_MODE_DMA_TVT1)
  113. return -EINVAL;
  114. rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_DMA_TVT1);
  115. if (rc)
  116. return rc;
  117. rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_SNOOP_ON);
  118. break;
  119. default:
  120. rc = -EINVAL;
  121. }
  122. return rc;
  123. }
  124. EXPORT_SYMBOL_GPL(cxllib_switch_phb_mode);
  125. /*
  126. * When switching the PHB to capi mode, the TVT#1 entry for
  127. * the Partitionable Endpoint is set in bypass mode, like
  128. * in PCI mode.
  129. * Configure the device dma to use TVT#1, which is done
  130. * by calling dma_set_mask() with a mask large enough.
  131. */
  132. int cxllib_set_device_dma(struct pci_dev *dev, unsigned long flags)
  133. {
  134. int rc;
  135. if (flags)
  136. return -EINVAL;
  137. rc = dma_set_mask(&dev->dev, DMA_BIT_MASK(64));
  138. return rc;
  139. }
  140. EXPORT_SYMBOL_GPL(cxllib_set_device_dma);
  141. int cxllib_get_PE_attributes(struct task_struct *task,
  142. unsigned long translation_mode,
  143. struct cxllib_pe_attributes *attr)
  144. {
  145. if (translation_mode != CXL_TRANSLATED_MODE &&
  146. translation_mode != CXL_REAL_MODE)
  147. return -EINVAL;
  148. attr->sr = cxl_calculate_sr(false,
  149. task == NULL,
  150. translation_mode == CXL_REAL_MODE,
  151. true);
  152. attr->lpid = mfspr(SPRN_LPID);
  153. if (task) {
  154. struct mm_struct *mm = get_task_mm(task);
  155. if (mm == NULL)
  156. return -EINVAL;
  157. /*
  158. * Caller is keeping a reference on mm_users for as long
  159. * as XSL uses the memory context
  160. */
  161. attr->pid = mm->context.id;
  162. mmput(mm);
  163. attr->tid = task->thread.tidr;
  164. } else {
  165. attr->pid = 0;
  166. attr->tid = 0;
  167. }
  168. return 0;
  169. }
  170. EXPORT_SYMBOL_GPL(cxllib_get_PE_attributes);
  171. static int get_vma_info(struct mm_struct *mm, u64 addr,
  172. u64 *vma_start, u64 *vma_end,
  173. unsigned long *page_size)
  174. {
  175. struct vm_area_struct *vma = NULL;
  176. int rc = 0;
  177. mmap_read_lock(mm);
  178. vma = find_vma(mm, addr);
  179. if (!vma) {
  180. rc = -EFAULT;
  181. goto out;
  182. }
  183. *page_size = vma_kernel_pagesize(vma);
  184. *vma_start = vma->vm_start;
  185. *vma_end = vma->vm_end;
  186. out:
  187. mmap_read_unlock(mm);
  188. return rc;
  189. }
  190. int cxllib_handle_fault(struct mm_struct *mm, u64 addr, u64 size, u64 flags)
  191. {
  192. int rc;
  193. u64 dar, vma_start, vma_end;
  194. unsigned long page_size;
  195. if (mm == NULL)
  196. return -EFAULT;
  197. /*
  198. * The buffer we have to process can extend over several pages
  199. * and may also cover several VMAs.
  200. * We iterate over all the pages. The page size could vary
  201. * between VMAs.
  202. */
  203. rc = get_vma_info(mm, addr, &vma_start, &vma_end, &page_size);
  204. if (rc)
  205. return rc;
  206. for (dar = (addr & ~(page_size - 1)); dar < (addr + size);
  207. dar += page_size) {
  208. if (dar < vma_start || dar >= vma_end) {
  209. /*
  210. * We don't hold mm->mmap_lock while iterating, since
  211. * the lock is required by one of the lower-level page
  212. * fault processing functions and it could
  213. * create a deadlock.
  214. *
  215. * It means the VMAs can be altered between 2
  216. * loop iterations and we could theoretically
  217. * miss a page (however unlikely). But that's
  218. * not really a problem, as the driver will
  219. * retry access, get another page fault on the
  220. * missing page and call us again.
  221. */
  222. rc = get_vma_info(mm, dar, &vma_start, &vma_end,
  223. &page_size);
  224. if (rc)
  225. return rc;
  226. }
  227. rc = cxl_handle_mm_fault(mm, flags, dar);
  228. if (rc)
  229. return -EFAULT;
  230. }
  231. return 0;
  232. }
  233. EXPORT_SYMBOL_GPL(cxllib_handle_fault);