main.c 8.8 KB

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