main.c 5.4 KB

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