main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. .cxl_cx4_setup_msi_irqs = _cxl_cx4_setup_msi_irqs,
  102. .cxl_cx4_teardown_msi_irqs = _cxl_cx4_teardown_msi_irqs,
  103. .owner = THIS_MODULE,
  104. };
  105. int cxl_alloc_sst(struct cxl_context *ctx)
  106. {
  107. unsigned long vsid;
  108. u64 ea_mask, size, sstp0, sstp1;
  109. sstp0 = 0;
  110. sstp1 = 0;
  111. ctx->sst_size = PAGE_SIZE;
  112. ctx->sst_lru = 0;
  113. ctx->sstp = (struct cxl_sste *)get_zeroed_page(GFP_KERNEL);
  114. if (!ctx->sstp) {
  115. pr_err("cxl_alloc_sst: Unable to allocate segment table\n");
  116. return -ENOMEM;
  117. }
  118. pr_devel("SSTP allocated at 0x%p\n", ctx->sstp);
  119. vsid = get_kernel_vsid((u64)ctx->sstp, mmu_kernel_ssize) << 12;
  120. sstp0 |= (u64)mmu_kernel_ssize << CXL_SSTP0_An_B_SHIFT;
  121. sstp0 |= (SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp) << 50;
  122. size = (((u64)ctx->sst_size >> 8) - 1) << CXL_SSTP0_An_SegTableSize_SHIFT;
  123. if (unlikely(size & ~CXL_SSTP0_An_SegTableSize_MASK)) {
  124. WARN(1, "Impossible segment table size\n");
  125. return -EINVAL;
  126. }
  127. sstp0 |= size;
  128. if (mmu_kernel_ssize == MMU_SEGSIZE_256M)
  129. ea_mask = 0xfffff00ULL;
  130. else
  131. ea_mask = 0xffffffff00ULL;
  132. sstp0 |= vsid >> (50-14); /* Top 14 bits of VSID */
  133. sstp1 |= (vsid << (64-(50-14))) & ~ea_mask;
  134. sstp1 |= (u64)ctx->sstp & ea_mask;
  135. sstp1 |= CXL_SSTP1_An_V;
  136. pr_devel("Looked up %#llx: slbfee. %#llx (ssize: %x, vsid: %#lx), copied to SSTP0: %#llx, SSTP1: %#llx\n",
  137. (u64)ctx->sstp, (u64)ctx->sstp & ESID_MASK, mmu_kernel_ssize, vsid, sstp0, sstp1);
  138. /* Store calculated sstp hardware points for use later */
  139. ctx->sstp0 = sstp0;
  140. ctx->sstp1 = sstp1;
  141. return 0;
  142. }
  143. /* print buffer content as integers when debugging */
  144. void cxl_dump_debug_buffer(void *buf, size_t buf_len)
  145. {
  146. #ifdef DEBUG
  147. int i, *ptr;
  148. /*
  149. * We want to regroup up to 4 integers per line, which means they
  150. * need to be in the same pr_devel() statement
  151. */
  152. ptr = (int *) buf;
  153. for (i = 0; i * 4 < buf_len; i += 4) {
  154. if ((i + 3) * 4 < buf_len)
  155. pr_devel("%.8x %.8x %.8x %.8x\n", ptr[i], ptr[i + 1],
  156. ptr[i + 2], ptr[i + 3]);
  157. else if ((i + 2) * 4 < buf_len)
  158. pr_devel("%.8x %.8x %.8x\n", ptr[i], ptr[i + 1],
  159. ptr[i + 2]);
  160. else if ((i + 1) * 4 < buf_len)
  161. pr_devel("%.8x %.8x\n", ptr[i], ptr[i + 1]);
  162. else
  163. pr_devel("%.8x\n", ptr[i]);
  164. }
  165. #endif /* DEBUG */
  166. }
  167. /* Find a CXL adapter by it's number and increase it's refcount */
  168. struct cxl *get_cxl_adapter(int num)
  169. {
  170. struct cxl *adapter;
  171. spin_lock(&adapter_idr_lock);
  172. if ((adapter = idr_find(&cxl_adapter_idr, num)))
  173. get_device(&adapter->dev);
  174. spin_unlock(&adapter_idr_lock);
  175. return adapter;
  176. }
  177. static int cxl_alloc_adapter_nr(struct cxl *adapter)
  178. {
  179. int i;
  180. idr_preload(GFP_KERNEL);
  181. spin_lock(&adapter_idr_lock);
  182. i = idr_alloc(&cxl_adapter_idr, adapter, 0, 0, GFP_NOWAIT);
  183. spin_unlock(&adapter_idr_lock);
  184. idr_preload_end();
  185. if (i < 0)
  186. return i;
  187. adapter->adapter_num = i;
  188. return 0;
  189. }
  190. void cxl_remove_adapter_nr(struct cxl *adapter)
  191. {
  192. idr_remove(&cxl_adapter_idr, adapter->adapter_num);
  193. }
  194. struct cxl *cxl_alloc_adapter(void)
  195. {
  196. struct cxl *adapter;
  197. if (!(adapter = kzalloc(sizeof(struct cxl), GFP_KERNEL)))
  198. return NULL;
  199. spin_lock_init(&adapter->afu_list_lock);
  200. if (cxl_alloc_adapter_nr(adapter))
  201. goto err1;
  202. if (dev_set_name(&adapter->dev, "card%i", adapter->adapter_num))
  203. goto err2;
  204. /* start with context lock taken */
  205. atomic_set(&adapter->contexts_num, -1);
  206. return adapter;
  207. err2:
  208. cxl_remove_adapter_nr(adapter);
  209. err1:
  210. kfree(adapter);
  211. return NULL;
  212. }
  213. struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
  214. {
  215. struct cxl_afu *afu;
  216. if (!(afu = kzalloc(sizeof(struct cxl_afu), GFP_KERNEL)))
  217. return NULL;
  218. afu->adapter = adapter;
  219. afu->dev.parent = &adapter->dev;
  220. afu->dev.release = cxl_ops->release_afu;
  221. afu->slice = slice;
  222. idr_init(&afu->contexts_idr);
  223. mutex_init(&afu->contexts_lock);
  224. spin_lock_init(&afu->afu_cntl_lock);
  225. atomic_set(&afu->configured_state, -1);
  226. afu->prefault_mode = CXL_PREFAULT_NONE;
  227. afu->irqs_max = afu->adapter->user_irqs;
  228. return afu;
  229. }
  230. int cxl_afu_select_best_mode(struct cxl_afu *afu)
  231. {
  232. if (afu->modes_supported & CXL_MODE_DIRECTED)
  233. return cxl_ops->afu_activate_mode(afu, CXL_MODE_DIRECTED);
  234. if (afu->modes_supported & CXL_MODE_DEDICATED)
  235. return cxl_ops->afu_activate_mode(afu, CXL_MODE_DEDICATED);
  236. dev_warn(&afu->dev, "No supported programming modes available\n");
  237. /* We don't fail this so the user can inspect sysfs */
  238. return 0;
  239. }
  240. int cxl_adapter_context_get(struct cxl *adapter)
  241. {
  242. int rc;
  243. rc = atomic_inc_unless_negative(&adapter->contexts_num);
  244. return rc >= 0 ? 0 : -EBUSY;
  245. }
  246. void cxl_adapter_context_put(struct cxl *adapter)
  247. {
  248. atomic_dec_if_positive(&adapter->contexts_num);
  249. }
  250. int cxl_adapter_context_lock(struct cxl *adapter)
  251. {
  252. int rc;
  253. /* no active contexts -> contexts_num == 0 */
  254. rc = atomic_cmpxchg(&adapter->contexts_num, 0, -1);
  255. return rc ? -EBUSY : 0;
  256. }
  257. void cxl_adapter_context_unlock(struct cxl *adapter)
  258. {
  259. int val = atomic_cmpxchg(&adapter->contexts_num, -1, 0);
  260. /*
  261. * contexts lock taken -> contexts_num == -1
  262. * If not true then show a warning and force reset the lock.
  263. * This will happen when context_unlock was requested without
  264. * doing a context_lock.
  265. */
  266. if (val != -1) {
  267. atomic_set(&adapter->contexts_num, 0);
  268. WARN(1, "Adapter context unlocked with %d active contexts",
  269. val);
  270. }
  271. }
  272. static int __init init_cxl(void)
  273. {
  274. int rc = 0;
  275. if ((rc = cxl_file_init()))
  276. return rc;
  277. cxl_debugfs_init();
  278. if ((rc = register_cxl_calls(&cxl_calls)))
  279. goto err;
  280. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  281. cxl_ops = &cxl_native_ops;
  282. rc = pci_register_driver(&cxl_pci_driver);
  283. }
  284. #ifdef CONFIG_PPC_PSERIES
  285. else {
  286. cxl_ops = &cxl_guest_ops;
  287. rc = platform_driver_register(&cxl_of_driver);
  288. }
  289. #endif
  290. if (rc)
  291. goto err1;
  292. return 0;
  293. err1:
  294. unregister_cxl_calls(&cxl_calls);
  295. err:
  296. cxl_debugfs_exit();
  297. cxl_file_exit();
  298. return rc;
  299. }
  300. static void exit_cxl(void)
  301. {
  302. if (cpu_has_feature(CPU_FTR_HVMODE))
  303. pci_unregister_driver(&cxl_pci_driver);
  304. #ifdef CONFIG_PPC_PSERIES
  305. else
  306. platform_driver_unregister(&cxl_of_driver);
  307. #endif
  308. cxl_debugfs_exit();
  309. cxl_file_exit();
  310. unregister_cxl_calls(&cxl_calls);
  311. idr_destroy(&cxl_adapter_idr);
  312. }
  313. module_init(init_cxl);
  314. module_exit(exit_cxl);
  315. MODULE_DESCRIPTION("IBM Coherent Accelerator");
  316. MODULE_AUTHOR("Ian Munsie <imunsie@au1.ibm.com>");
  317. MODULE_LICENSE("GPL");