main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2014 IBM Corp.
  4. */
  5. #include <linux/spinlock.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/device.h>
  9. #include <linux/mutex.h>
  10. #include <linux/init.h>
  11. #include <linux/list.h>
  12. #include <linux/mm.h>
  13. #include <linux/of.h>
  14. #include <linux/slab.h>
  15. #include <linux/idr.h>
  16. #include <linux/pci.h>
  17. #include <linux/sched/task.h>
  18. #include <asm/cputable.h>
  19. #include <asm/mmu.h>
  20. #include <misc/cxl-base.h>
  21. #include "cxl.h"
  22. #include "trace.h"
  23. static DEFINE_SPINLOCK(adapter_idr_lock);
  24. static DEFINE_IDR(cxl_adapter_idr);
  25. uint cxl_verbose;
  26. module_param_named(verbose, cxl_verbose, uint, 0600);
  27. MODULE_PARM_DESC(verbose, "Enable verbose dmesg output");
  28. const struct cxl_backend_ops *cxl_ops;
  29. int cxl_afu_slbia(struct cxl_afu *afu)
  30. {
  31. unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
  32. pr_devel("cxl_afu_slbia issuing SLBIA command\n");
  33. cxl_p2n_write(afu, CXL_SLBIA_An, CXL_TLB_SLB_IQ_ALL);
  34. while (cxl_p2n_read(afu, CXL_SLBIA_An) & CXL_TLB_SLB_P) {
  35. if (time_after_eq(jiffies, timeout)) {
  36. dev_warn(&afu->dev, "WARNING: CXL AFU SLBIA timed out!\n");
  37. return -EBUSY;
  38. }
  39. /* If the adapter has gone down, we can assume that we
  40. * will PERST it and that will invalidate everything.
  41. */
  42. if (!cxl_ops->link_ok(afu->adapter, afu))
  43. return -EIO;
  44. cpu_relax();
  45. }
  46. return 0;
  47. }
  48. static inline void _cxl_slbia(struct cxl_context *ctx, struct mm_struct *mm)
  49. {
  50. unsigned long flags;
  51. if (ctx->mm != mm)
  52. return;
  53. pr_devel("%s matched mm - card: %i afu: %i pe: %i\n", __func__,
  54. ctx->afu->adapter->adapter_num, ctx->afu->slice, ctx->pe);
  55. spin_lock_irqsave(&ctx->sste_lock, flags);
  56. trace_cxl_slbia(ctx);
  57. memset(ctx->sstp, 0, ctx->sst_size);
  58. spin_unlock_irqrestore(&ctx->sste_lock, flags);
  59. mb();
  60. cxl_afu_slbia(ctx->afu);
  61. }
  62. static inline void cxl_slbia_core(struct mm_struct *mm)
  63. {
  64. struct cxl *adapter;
  65. struct cxl_afu *afu;
  66. struct cxl_context *ctx;
  67. int card, slice, id;
  68. pr_devel("%s called\n", __func__);
  69. spin_lock(&adapter_idr_lock);
  70. idr_for_each_entry(&cxl_adapter_idr, adapter, card) {
  71. /* XXX: Make this lookup faster with link from mm to ctx */
  72. spin_lock(&adapter->afu_list_lock);
  73. for (slice = 0; slice < adapter->slices; slice++) {
  74. afu = adapter->afu[slice];
  75. if (!afu || !afu->enabled)
  76. continue;
  77. rcu_read_lock();
  78. idr_for_each_entry(&afu->contexts_idr, ctx, id)
  79. _cxl_slbia(ctx, mm);
  80. rcu_read_unlock();
  81. }
  82. spin_unlock(&adapter->afu_list_lock);
  83. }
  84. spin_unlock(&adapter_idr_lock);
  85. }
  86. static struct cxl_calls cxl_calls = {
  87. .cxl_slbia = cxl_slbia_core,
  88. .owner = THIS_MODULE,
  89. };
  90. int cxl_alloc_sst(struct cxl_context *ctx)
  91. {
  92. unsigned long vsid;
  93. u64 ea_mask, size, sstp0, sstp1;
  94. sstp0 = 0;
  95. sstp1 = 0;
  96. ctx->sst_size = PAGE_SIZE;
  97. ctx->sst_lru = 0;
  98. ctx->sstp = (struct cxl_sste *)get_zeroed_page(GFP_KERNEL);
  99. if (!ctx->sstp) {
  100. pr_err("cxl_alloc_sst: Unable to allocate segment table\n");
  101. return -ENOMEM;
  102. }
  103. pr_devel("SSTP allocated at 0x%p\n", ctx->sstp);
  104. vsid = get_kernel_vsid((u64)ctx->sstp, mmu_kernel_ssize) << 12;
  105. sstp0 |= (u64)mmu_kernel_ssize << CXL_SSTP0_An_B_SHIFT;
  106. sstp0 |= (SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp) << 50;
  107. size = (((u64)ctx->sst_size >> 8) - 1) << CXL_SSTP0_An_SegTableSize_SHIFT;
  108. if (unlikely(size & ~CXL_SSTP0_An_SegTableSize_MASK)) {
  109. WARN(1, "Impossible segment table size\n");
  110. return -EINVAL;
  111. }
  112. sstp0 |= size;
  113. if (mmu_kernel_ssize == MMU_SEGSIZE_256M)
  114. ea_mask = 0xfffff00ULL;
  115. else
  116. ea_mask = 0xffffffff00ULL;
  117. sstp0 |= vsid >> (50-14); /* Top 14 bits of VSID */
  118. sstp1 |= (vsid << (64-(50-14))) & ~ea_mask;
  119. sstp1 |= (u64)ctx->sstp & ea_mask;
  120. sstp1 |= CXL_SSTP1_An_V;
  121. pr_devel("Looked up %#llx: slbfee. %#llx (ssize: %x, vsid: %#lx), copied to SSTP0: %#llx, SSTP1: %#llx\n",
  122. (u64)ctx->sstp, (u64)ctx->sstp & ESID_MASK, mmu_kernel_ssize, vsid, sstp0, sstp1);
  123. /* Store calculated sstp hardware points for use later */
  124. ctx->sstp0 = sstp0;
  125. ctx->sstp1 = sstp1;
  126. return 0;
  127. }
  128. /* print buffer content as integers when debugging */
  129. void cxl_dump_debug_buffer(void *buf, size_t buf_len)
  130. {
  131. #ifdef DEBUG
  132. int i, *ptr;
  133. /*
  134. * We want to regroup up to 4 integers per line, which means they
  135. * need to be in the same pr_devel() statement
  136. */
  137. ptr = (int *) buf;
  138. for (i = 0; i * 4 < buf_len; i += 4) {
  139. if ((i + 3) * 4 < buf_len)
  140. pr_devel("%.8x %.8x %.8x %.8x\n", ptr[i], ptr[i + 1],
  141. ptr[i + 2], ptr[i + 3]);
  142. else if ((i + 2) * 4 < buf_len)
  143. pr_devel("%.8x %.8x %.8x\n", ptr[i], ptr[i + 1],
  144. ptr[i + 2]);
  145. else if ((i + 1) * 4 < buf_len)
  146. pr_devel("%.8x %.8x\n", ptr[i], ptr[i + 1]);
  147. else
  148. pr_devel("%.8x\n", ptr[i]);
  149. }
  150. #endif /* DEBUG */
  151. }
  152. /* Find a CXL adapter by it's number and increase it's refcount */
  153. struct cxl *get_cxl_adapter(int num)
  154. {
  155. struct cxl *adapter;
  156. spin_lock(&adapter_idr_lock);
  157. if ((adapter = idr_find(&cxl_adapter_idr, num)))
  158. get_device(&adapter->dev);
  159. spin_unlock(&adapter_idr_lock);
  160. return adapter;
  161. }
  162. static int cxl_alloc_adapter_nr(struct cxl *adapter)
  163. {
  164. int i;
  165. idr_preload(GFP_KERNEL);
  166. spin_lock(&adapter_idr_lock);
  167. i = idr_alloc(&cxl_adapter_idr, adapter, 0, 0, GFP_NOWAIT);
  168. spin_unlock(&adapter_idr_lock);
  169. idr_preload_end();
  170. if (i < 0)
  171. return i;
  172. adapter->adapter_num = i;
  173. return 0;
  174. }
  175. void cxl_remove_adapter_nr(struct cxl *adapter)
  176. {
  177. idr_remove(&cxl_adapter_idr, adapter->adapter_num);
  178. }
  179. struct cxl *cxl_alloc_adapter(void)
  180. {
  181. struct cxl *adapter;
  182. if (!(adapter = kzalloc(sizeof(struct cxl), GFP_KERNEL)))
  183. return NULL;
  184. spin_lock_init(&adapter->afu_list_lock);
  185. if (cxl_alloc_adapter_nr(adapter))
  186. goto err1;
  187. if (dev_set_name(&adapter->dev, "card%i", adapter->adapter_num))
  188. goto err2;
  189. /* start with context lock taken */
  190. atomic_set(&adapter->contexts_num, -1);
  191. return adapter;
  192. err2:
  193. cxl_remove_adapter_nr(adapter);
  194. err1:
  195. kfree(adapter);
  196. return NULL;
  197. }
  198. struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
  199. {
  200. struct cxl_afu *afu;
  201. if (!(afu = kzalloc(sizeof(struct cxl_afu), GFP_KERNEL)))
  202. return NULL;
  203. afu->adapter = adapter;
  204. afu->dev.parent = &adapter->dev;
  205. afu->dev.release = cxl_ops->release_afu;
  206. afu->slice = slice;
  207. idr_init(&afu->contexts_idr);
  208. mutex_init(&afu->contexts_lock);
  209. spin_lock_init(&afu->afu_cntl_lock);
  210. atomic_set(&afu->configured_state, -1);
  211. afu->prefault_mode = CXL_PREFAULT_NONE;
  212. afu->irqs_max = afu->adapter->user_irqs;
  213. return afu;
  214. }
  215. int cxl_afu_select_best_mode(struct cxl_afu *afu)
  216. {
  217. if (afu->modes_supported & CXL_MODE_DIRECTED)
  218. return cxl_ops->afu_activate_mode(afu, CXL_MODE_DIRECTED);
  219. if (afu->modes_supported & CXL_MODE_DEDICATED)
  220. return cxl_ops->afu_activate_mode(afu, CXL_MODE_DEDICATED);
  221. dev_warn(&afu->dev, "No supported programming modes available\n");
  222. /* We don't fail this so the user can inspect sysfs */
  223. return 0;
  224. }
  225. int cxl_adapter_context_get(struct cxl *adapter)
  226. {
  227. int rc;
  228. rc = atomic_inc_unless_negative(&adapter->contexts_num);
  229. return rc ? 0 : -EBUSY;
  230. }
  231. void cxl_adapter_context_put(struct cxl *adapter)
  232. {
  233. atomic_dec_if_positive(&adapter->contexts_num);
  234. }
  235. int cxl_adapter_context_lock(struct cxl *adapter)
  236. {
  237. int rc;
  238. /* no active contexts -> contexts_num == 0 */
  239. rc = atomic_cmpxchg(&adapter->contexts_num, 0, -1);
  240. return rc ? -EBUSY : 0;
  241. }
  242. void cxl_adapter_context_unlock(struct cxl *adapter)
  243. {
  244. int val = atomic_cmpxchg(&adapter->contexts_num, -1, 0);
  245. /*
  246. * contexts lock taken -> contexts_num == -1
  247. * If not true then show a warning and force reset the lock.
  248. * This will happen when context_unlock was requested without
  249. * doing a context_lock.
  250. */
  251. if (val != -1) {
  252. atomic_set(&adapter->contexts_num, 0);
  253. WARN(1, "Adapter context unlocked with %d active contexts",
  254. val);
  255. }
  256. }
  257. static int __init init_cxl(void)
  258. {
  259. int rc = 0;
  260. if (!tlbie_capable)
  261. return -EINVAL;
  262. if ((rc = cxl_file_init()))
  263. return rc;
  264. cxl_debugfs_init();
  265. /*
  266. * we don't register the callback on P9. slb callack is only
  267. * used for the PSL8 MMU and CX4.
  268. */
  269. if (cxl_is_power8()) {
  270. rc = register_cxl_calls(&cxl_calls);
  271. if (rc)
  272. goto err;
  273. }
  274. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  275. cxl_ops = &cxl_native_ops;
  276. rc = pci_register_driver(&cxl_pci_driver);
  277. }
  278. #ifdef CONFIG_PPC_PSERIES
  279. else {
  280. cxl_ops = &cxl_guest_ops;
  281. rc = platform_driver_register(&cxl_of_driver);
  282. }
  283. #endif
  284. if (rc)
  285. goto err1;
  286. return 0;
  287. err1:
  288. if (cxl_is_power8())
  289. unregister_cxl_calls(&cxl_calls);
  290. err:
  291. cxl_debugfs_exit();
  292. cxl_file_exit();
  293. return rc;
  294. }
  295. static void exit_cxl(void)
  296. {
  297. if (cpu_has_feature(CPU_FTR_HVMODE))
  298. pci_unregister_driver(&cxl_pci_driver);
  299. #ifdef CONFIG_PPC_PSERIES
  300. else
  301. platform_driver_unregister(&cxl_of_driver);
  302. #endif
  303. cxl_debugfs_exit();
  304. cxl_file_exit();
  305. if (cxl_is_power8())
  306. unregister_cxl_calls(&cxl_calls);
  307. idr_destroy(&cxl_adapter_idr);
  308. }
  309. module_init(init_cxl);
  310. module_exit(exit_cxl);
  311. MODULE_DESCRIPTION("IBM Coherent Accelerator");
  312. MODULE_AUTHOR("Ian Munsie <imunsie@au1.ibm.com>");
  313. MODULE_LICENSE("GPL");