main.c 5.4 KB

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