main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. 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_adapter_link_ok(afu->adapter))
  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. struct task_struct *task;
  52. unsigned long flags;
  53. if (!(task = get_pid_task(ctx->pid, PIDTYPE_PID))) {
  54. pr_devel("%s unable to get task %i\n",
  55. __func__, pid_nr(ctx->pid));
  56. return;
  57. }
  58. if (task->mm != mm)
  59. goto out_put;
  60. pr_devel("%s matched mm - card: %i afu: %i pe: %i\n", __func__,
  61. ctx->afu->adapter->adapter_num, ctx->afu->slice, ctx->pe);
  62. spin_lock_irqsave(&ctx->sste_lock, flags);
  63. trace_cxl_slbia(ctx);
  64. memset(ctx->sstp, 0, ctx->sst_size);
  65. spin_unlock_irqrestore(&ctx->sste_lock, flags);
  66. mb();
  67. cxl_afu_slbia(ctx->afu);
  68. out_put:
  69. put_task_struct(task);
  70. }
  71. static inline void cxl_slbia_core(struct mm_struct *mm)
  72. {
  73. struct cxl *adapter;
  74. struct cxl_afu *afu;
  75. struct cxl_context *ctx;
  76. int card, slice, id;
  77. pr_devel("%s called\n", __func__);
  78. spin_lock(&adapter_idr_lock);
  79. idr_for_each_entry(&cxl_adapter_idr, adapter, card) {
  80. /* XXX: Make this lookup faster with link from mm to ctx */
  81. spin_lock(&adapter->afu_list_lock);
  82. for (slice = 0; slice < adapter->slices; slice++) {
  83. afu = adapter->afu[slice];
  84. if (!afu || !afu->enabled)
  85. continue;
  86. rcu_read_lock();
  87. idr_for_each_entry(&afu->contexts_idr, ctx, id)
  88. _cxl_slbia(ctx, mm);
  89. rcu_read_unlock();
  90. }
  91. spin_unlock(&adapter->afu_list_lock);
  92. }
  93. spin_unlock(&adapter_idr_lock);
  94. }
  95. static struct cxl_calls cxl_calls = {
  96. .cxl_slbia = cxl_slbia_core,
  97. .owner = THIS_MODULE,
  98. };
  99. int cxl_alloc_sst(struct cxl_context *ctx)
  100. {
  101. unsigned long vsid;
  102. u64 ea_mask, size, sstp0, sstp1;
  103. sstp0 = 0;
  104. sstp1 = 0;
  105. ctx->sst_size = PAGE_SIZE;
  106. ctx->sst_lru = 0;
  107. ctx->sstp = (struct cxl_sste *)get_zeroed_page(GFP_KERNEL);
  108. if (!ctx->sstp) {
  109. pr_err("cxl_alloc_sst: Unable to allocate segment table\n");
  110. return -ENOMEM;
  111. }
  112. pr_devel("SSTP allocated at 0x%p\n", ctx->sstp);
  113. vsid = get_kernel_vsid((u64)ctx->sstp, mmu_kernel_ssize) << 12;
  114. sstp0 |= (u64)mmu_kernel_ssize << CXL_SSTP0_An_B_SHIFT;
  115. sstp0 |= (SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp) << 50;
  116. size = (((u64)ctx->sst_size >> 8) - 1) << CXL_SSTP0_An_SegTableSize_SHIFT;
  117. if (unlikely(size & ~CXL_SSTP0_An_SegTableSize_MASK)) {
  118. WARN(1, "Impossible segment table size\n");
  119. return -EINVAL;
  120. }
  121. sstp0 |= size;
  122. if (mmu_kernel_ssize == MMU_SEGSIZE_256M)
  123. ea_mask = 0xfffff00ULL;
  124. else
  125. ea_mask = 0xffffffff00ULL;
  126. sstp0 |= vsid >> (50-14); /* Top 14 bits of VSID */
  127. sstp1 |= (vsid << (64-(50-14))) & ~ea_mask;
  128. sstp1 |= (u64)ctx->sstp & ea_mask;
  129. sstp1 |= CXL_SSTP1_An_V;
  130. pr_devel("Looked up %#llx: slbfee. %#llx (ssize: %x, vsid: %#lx), copied to SSTP0: %#llx, SSTP1: %#llx\n",
  131. (u64)ctx->sstp, (u64)ctx->sstp & ESID_MASK, mmu_kernel_ssize, vsid, sstp0, sstp1);
  132. /* Store calculated sstp hardware points for use later */
  133. ctx->sstp0 = sstp0;
  134. ctx->sstp1 = sstp1;
  135. return 0;
  136. }
  137. /* Find a CXL adapter by it's number and increase it's refcount */
  138. struct cxl *get_cxl_adapter(int num)
  139. {
  140. struct cxl *adapter;
  141. spin_lock(&adapter_idr_lock);
  142. if ((adapter = idr_find(&cxl_adapter_idr, num)))
  143. get_device(&adapter->dev);
  144. spin_unlock(&adapter_idr_lock);
  145. return adapter;
  146. }
  147. static int cxl_alloc_adapter_nr(struct cxl *adapter)
  148. {
  149. int i;
  150. idr_preload(GFP_KERNEL);
  151. spin_lock(&adapter_idr_lock);
  152. i = idr_alloc(&cxl_adapter_idr, adapter, 0, 0, GFP_NOWAIT);
  153. spin_unlock(&adapter_idr_lock);
  154. idr_preload_end();
  155. if (i < 0)
  156. return i;
  157. adapter->adapter_num = i;
  158. return 0;
  159. }
  160. void cxl_remove_adapter_nr(struct cxl *adapter)
  161. {
  162. idr_remove(&cxl_adapter_idr, adapter->adapter_num);
  163. }
  164. struct cxl *cxl_alloc_adapter(void)
  165. {
  166. struct cxl *adapter;
  167. if (!(adapter = kzalloc(sizeof(struct cxl), GFP_KERNEL)))
  168. return NULL;
  169. spin_lock_init(&adapter->afu_list_lock);
  170. if (cxl_alloc_adapter_nr(adapter))
  171. goto err1;
  172. if (dev_set_name(&adapter->dev, "card%i", adapter->adapter_num))
  173. goto err2;
  174. return adapter;
  175. err2:
  176. cxl_remove_adapter_nr(adapter);
  177. err1:
  178. kfree(adapter);
  179. return NULL;
  180. }
  181. struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
  182. {
  183. struct cxl_afu *afu;
  184. if (!(afu = kzalloc(sizeof(struct cxl_afu), GFP_KERNEL)))
  185. return NULL;
  186. afu->adapter = adapter;
  187. afu->dev.parent = &adapter->dev;
  188. afu->dev.release = cxl_release_afu;
  189. afu->slice = slice;
  190. idr_init(&afu->contexts_idr);
  191. mutex_init(&afu->contexts_lock);
  192. spin_lock_init(&afu->afu_cntl_lock);
  193. mutex_init(&afu->spa_mutex);
  194. afu->prefault_mode = CXL_PREFAULT_NONE;
  195. afu->irqs_max = afu->adapter->user_irqs;
  196. return afu;
  197. }
  198. int cxl_afu_select_best_mode(struct cxl_afu *afu)
  199. {
  200. if (afu->modes_supported & CXL_MODE_DIRECTED)
  201. return cxl_afu_activate_mode(afu, CXL_MODE_DIRECTED);
  202. if (afu->modes_supported & CXL_MODE_DEDICATED)
  203. return cxl_afu_activate_mode(afu, CXL_MODE_DEDICATED);
  204. dev_warn(&afu->dev, "No supported programming modes available\n");
  205. /* We don't fail this so the user can inspect sysfs */
  206. return 0;
  207. }
  208. static int __init init_cxl(void)
  209. {
  210. int rc = 0;
  211. if (!cpu_has_feature(CPU_FTR_HVMODE))
  212. return -EPERM;
  213. if ((rc = cxl_file_init()))
  214. return rc;
  215. cxl_debugfs_init();
  216. if ((rc = register_cxl_calls(&cxl_calls)))
  217. goto err;
  218. if ((rc = pci_register_driver(&cxl_pci_driver)))
  219. goto err1;
  220. return 0;
  221. err1:
  222. unregister_cxl_calls(&cxl_calls);
  223. err:
  224. cxl_debugfs_exit();
  225. cxl_file_exit();
  226. return rc;
  227. }
  228. static void exit_cxl(void)
  229. {
  230. pci_unregister_driver(&cxl_pci_driver);
  231. cxl_debugfs_exit();
  232. cxl_file_exit();
  233. unregister_cxl_calls(&cxl_calls);
  234. idr_destroy(&cxl_adapter_idr);
  235. }
  236. module_init(init_cxl);
  237. module_exit(exit_cxl);
  238. MODULE_DESCRIPTION("IBM Coherent Accelerator");
  239. MODULE_AUTHOR("Ian Munsie <imunsie@au1.ibm.com>");
  240. MODULE_LICENSE("GPL");