main.c 8.1 KB

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