native.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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/sched.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched.h>
  13. #include <linux/mutex.h>
  14. #include <linux/mm.h>
  15. #include <linux/uaccess.h>
  16. #include <asm/synch.h>
  17. #include <misc/cxl-base.h>
  18. #include "cxl.h"
  19. #include "trace.h"
  20. static int afu_control(struct cxl_afu *afu, u64 command,
  21. u64 result, u64 mask, bool enabled)
  22. {
  23. u64 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  24. unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
  25. int rc = 0;
  26. spin_lock(&afu->afu_cntl_lock);
  27. pr_devel("AFU command starting: %llx\n", command);
  28. trace_cxl_afu_ctrl(afu, command);
  29. cxl_p2n_write(afu, CXL_AFU_Cntl_An, AFU_Cntl | command);
  30. AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  31. while ((AFU_Cntl & mask) != result) {
  32. if (time_after_eq(jiffies, timeout)) {
  33. dev_warn(&afu->dev, "WARNING: AFU control timed out!\n");
  34. rc = -EBUSY;
  35. goto out;
  36. }
  37. pr_devel_ratelimited("AFU control... (0x%.16llx)\n",
  38. AFU_Cntl | command);
  39. cpu_relax();
  40. AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  41. };
  42. pr_devel("AFU command complete: %llx\n", command);
  43. afu->enabled = enabled;
  44. out:
  45. trace_cxl_afu_ctrl_done(afu, command, rc);
  46. spin_unlock(&afu->afu_cntl_lock);
  47. return rc;
  48. }
  49. static int afu_enable(struct cxl_afu *afu)
  50. {
  51. pr_devel("AFU enable request\n");
  52. return afu_control(afu, CXL_AFU_Cntl_An_E,
  53. CXL_AFU_Cntl_An_ES_Enabled,
  54. CXL_AFU_Cntl_An_ES_MASK, true);
  55. }
  56. int cxl_afu_disable(struct cxl_afu *afu)
  57. {
  58. pr_devel("AFU disable request\n");
  59. return afu_control(afu, 0, CXL_AFU_Cntl_An_ES_Disabled,
  60. CXL_AFU_Cntl_An_ES_MASK, false);
  61. }
  62. /* This will disable as well as reset */
  63. int __cxl_afu_reset(struct cxl_afu *afu)
  64. {
  65. pr_devel("AFU reset request\n");
  66. return afu_control(afu, CXL_AFU_Cntl_An_RA,
  67. CXL_AFU_Cntl_An_RS_Complete | CXL_AFU_Cntl_An_ES_Disabled,
  68. CXL_AFU_Cntl_An_RS_MASK | CXL_AFU_Cntl_An_ES_MASK,
  69. false);
  70. }
  71. int cxl_afu_check_and_enable(struct cxl_afu *afu)
  72. {
  73. if (afu->enabled)
  74. return 0;
  75. return afu_enable(afu);
  76. }
  77. int cxl_psl_purge(struct cxl_afu *afu)
  78. {
  79. u64 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
  80. u64 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  81. u64 dsisr, dar;
  82. u64 start, end;
  83. unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
  84. int rc = 0;
  85. trace_cxl_psl_ctrl(afu, CXL_PSL_SCNTL_An_Pc);
  86. pr_devel("PSL purge request\n");
  87. if ((AFU_Cntl & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
  88. WARN(1, "psl_purge request while AFU not disabled!\n");
  89. cxl_afu_disable(afu);
  90. }
  91. cxl_p1n_write(afu, CXL_PSL_SCNTL_An,
  92. PSL_CNTL | CXL_PSL_SCNTL_An_Pc);
  93. start = local_clock();
  94. PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
  95. while ((PSL_CNTL & CXL_PSL_SCNTL_An_Ps_MASK)
  96. == CXL_PSL_SCNTL_An_Ps_Pending) {
  97. if (time_after_eq(jiffies, timeout)) {
  98. dev_warn(&afu->dev, "WARNING: PSL Purge timed out!\n");
  99. rc = -EBUSY;
  100. goto out;
  101. }
  102. dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  103. pr_devel_ratelimited("PSL purging... PSL_CNTL: 0x%.16llx PSL_DSISR: 0x%.16llx\n", PSL_CNTL, dsisr);
  104. if (dsisr & CXL_PSL_DSISR_TRANS) {
  105. dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
  106. dev_notice(&afu->dev, "PSL purge terminating pending translation, DSISR: 0x%.16llx, DAR: 0x%.16llx\n", dsisr, dar);
  107. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  108. } else if (dsisr) {
  109. dev_notice(&afu->dev, "PSL purge acknowledging pending non-translation fault, DSISR: 0x%.16llx\n", dsisr);
  110. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  111. } else {
  112. cpu_relax();
  113. }
  114. PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
  115. };
  116. end = local_clock();
  117. pr_devel("PSL purged in %lld ns\n", end - start);
  118. cxl_p1n_write(afu, CXL_PSL_SCNTL_An,
  119. PSL_CNTL & ~CXL_PSL_SCNTL_An_Pc);
  120. out:
  121. trace_cxl_psl_ctrl_done(afu, CXL_PSL_SCNTL_An_Pc, rc);
  122. return rc;
  123. }
  124. static int spa_max_procs(int spa_size)
  125. {
  126. /*
  127. * From the CAIA:
  128. * end_of_SPA_area = SPA_Base + ((n+4) * 128) + (( ((n*8) + 127) >> 7) * 128) + 255
  129. * Most of that junk is really just an overly-complicated way of saying
  130. * the last 256 bytes are __aligned(128), so it's really:
  131. * end_of_SPA_area = end_of_PSL_queue_area + __aligned(128) 255
  132. * and
  133. * end_of_PSL_queue_area = SPA_Base + ((n+4) * 128) + (n*8) - 1
  134. * so
  135. * sizeof(SPA) = ((n+4) * 128) + (n*8) + __aligned(128) 256
  136. * Ignore the alignment (which is safe in this case as long as we are
  137. * careful with our rounding) and solve for n:
  138. */
  139. return ((spa_size / 8) - 96) / 17;
  140. }
  141. static int alloc_spa(struct cxl_afu *afu)
  142. {
  143. u64 spap;
  144. /* Work out how many pages to allocate */
  145. afu->spa_order = 0;
  146. do {
  147. afu->spa_order++;
  148. afu->spa_size = (1 << afu->spa_order) * PAGE_SIZE;
  149. afu->spa_max_procs = spa_max_procs(afu->spa_size);
  150. } while (afu->spa_max_procs < afu->num_procs);
  151. WARN_ON(afu->spa_size > 0x100000); /* Max size supported by the hardware */
  152. if (!(afu->spa = (struct cxl_process_element *)
  153. __get_free_pages(GFP_KERNEL | __GFP_ZERO, afu->spa_order))) {
  154. pr_err("cxl_alloc_spa: Unable to allocate scheduled process area\n");
  155. return -ENOMEM;
  156. }
  157. pr_devel("spa pages: %i afu->spa_max_procs: %i afu->num_procs: %i\n",
  158. 1<<afu->spa_order, afu->spa_max_procs, afu->num_procs);
  159. afu->sw_command_status = (__be64 *)((char *)afu->spa +
  160. ((afu->spa_max_procs + 3) * 128));
  161. spap = virt_to_phys(afu->spa) & CXL_PSL_SPAP_Addr;
  162. spap |= ((afu->spa_size >> (12 - CXL_PSL_SPAP_Size_Shift)) - 1) & CXL_PSL_SPAP_Size;
  163. spap |= CXL_PSL_SPAP_V;
  164. pr_devel("cxl: SPA allocated at 0x%p. Max processes: %i, sw_command_status: 0x%p CXL_PSL_SPAP_An=0x%016llx\n", afu->spa, afu->spa_max_procs, afu->sw_command_status, spap);
  165. cxl_p1n_write(afu, CXL_PSL_SPAP_An, spap);
  166. return 0;
  167. }
  168. static void release_spa(struct cxl_afu *afu)
  169. {
  170. cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0);
  171. free_pages((unsigned long) afu->spa, afu->spa_order);
  172. }
  173. int cxl_tlb_slb_invalidate(struct cxl *adapter)
  174. {
  175. unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
  176. pr_devel("CXL adapter wide TLBIA & SLBIA\n");
  177. cxl_p1_write(adapter, CXL_PSL_AFUSEL, CXL_PSL_AFUSEL_A);
  178. cxl_p1_write(adapter, CXL_PSL_TLBIA, CXL_TLB_SLB_IQ_ALL);
  179. while (cxl_p1_read(adapter, CXL_PSL_TLBIA) & CXL_TLB_SLB_P) {
  180. if (time_after_eq(jiffies, timeout)) {
  181. dev_warn(&adapter->dev, "WARNING: CXL adapter wide TLBIA timed out!\n");
  182. return -EBUSY;
  183. }
  184. cpu_relax();
  185. }
  186. cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_ALL);
  187. while (cxl_p1_read(adapter, CXL_PSL_SLBIA) & CXL_TLB_SLB_P) {
  188. if (time_after_eq(jiffies, timeout)) {
  189. dev_warn(&adapter->dev, "WARNING: CXL adapter wide SLBIA timed out!\n");
  190. return -EBUSY;
  191. }
  192. cpu_relax();
  193. }
  194. return 0;
  195. }
  196. int cxl_afu_slbia(struct cxl_afu *afu)
  197. {
  198. unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
  199. pr_devel("cxl_afu_slbia issuing SLBIA command\n");
  200. cxl_p2n_write(afu, CXL_SLBIA_An, CXL_TLB_SLB_IQ_ALL);
  201. while (cxl_p2n_read(afu, CXL_SLBIA_An) & CXL_TLB_SLB_P) {
  202. if (time_after_eq(jiffies, timeout)) {
  203. dev_warn(&afu->dev, "WARNING: CXL AFU SLBIA timed out!\n");
  204. return -EBUSY;
  205. }
  206. cpu_relax();
  207. }
  208. return 0;
  209. }
  210. static int cxl_write_sstp(struct cxl_afu *afu, u64 sstp0, u64 sstp1)
  211. {
  212. int rc;
  213. /* 1. Disable SSTP by writing 0 to SSTP1[V] */
  214. cxl_p2n_write(afu, CXL_SSTP1_An, 0);
  215. /* 2. Invalidate all SLB entries */
  216. if ((rc = cxl_afu_slbia(afu)))
  217. return rc;
  218. /* 3. Set SSTP0_An */
  219. cxl_p2n_write(afu, CXL_SSTP0_An, sstp0);
  220. /* 4. Set SSTP1_An */
  221. cxl_p2n_write(afu, CXL_SSTP1_An, sstp1);
  222. return 0;
  223. }
  224. /* Using per slice version may improve performance here. (ie. SLBIA_An) */
  225. static void slb_invalid(struct cxl_context *ctx)
  226. {
  227. struct cxl *adapter = ctx->afu->adapter;
  228. u64 slbia;
  229. WARN_ON(!mutex_is_locked(&ctx->afu->spa_mutex));
  230. cxl_p1_write(adapter, CXL_PSL_LBISEL,
  231. ((u64)be32_to_cpu(ctx->elem->common.pid) << 32) |
  232. be32_to_cpu(ctx->elem->lpid));
  233. cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_LPIDPID);
  234. while (1) {
  235. slbia = cxl_p1_read(adapter, CXL_PSL_SLBIA);
  236. if (!(slbia & CXL_TLB_SLB_P))
  237. break;
  238. cpu_relax();
  239. }
  240. }
  241. static int do_process_element_cmd(struct cxl_context *ctx,
  242. u64 cmd, u64 pe_state)
  243. {
  244. u64 state;
  245. unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
  246. int rc = 0;
  247. trace_cxl_llcmd(ctx, cmd);
  248. WARN_ON(!ctx->afu->enabled);
  249. ctx->elem->software_state = cpu_to_be32(pe_state);
  250. smp_wmb();
  251. *(ctx->afu->sw_command_status) = cpu_to_be64(cmd | 0 | ctx->pe);
  252. smp_mb();
  253. cxl_p1n_write(ctx->afu, CXL_PSL_LLCMD_An, cmd | ctx->pe);
  254. while (1) {
  255. if (time_after_eq(jiffies, timeout)) {
  256. dev_warn(&ctx->afu->dev, "WARNING: Process Element Command timed out!\n");
  257. rc = -EBUSY;
  258. goto out;
  259. }
  260. state = be64_to_cpup(ctx->afu->sw_command_status);
  261. if (state == ~0ULL) {
  262. pr_err("cxl: Error adding process element to AFU\n");
  263. rc = -1;
  264. goto out;
  265. }
  266. if ((state & (CXL_SPA_SW_CMD_MASK | CXL_SPA_SW_STATE_MASK | CXL_SPA_SW_LINK_MASK)) ==
  267. (cmd | (cmd >> 16) | ctx->pe))
  268. break;
  269. /*
  270. * The command won't finish in the PSL if there are
  271. * outstanding DSIs. Hence we need to yield here in
  272. * case there are outstanding DSIs that we need to
  273. * service. Tuning possiblity: we could wait for a
  274. * while before sched
  275. */
  276. schedule();
  277. }
  278. out:
  279. trace_cxl_llcmd_done(ctx, cmd, rc);
  280. return rc;
  281. }
  282. static int add_process_element(struct cxl_context *ctx)
  283. {
  284. int rc = 0;
  285. mutex_lock(&ctx->afu->spa_mutex);
  286. pr_devel("%s Adding pe: %i started\n", __func__, ctx->pe);
  287. if (!(rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_ADD, CXL_PE_SOFTWARE_STATE_V)))
  288. ctx->pe_inserted = true;
  289. pr_devel("%s Adding pe: %i finished\n", __func__, ctx->pe);
  290. mutex_unlock(&ctx->afu->spa_mutex);
  291. return rc;
  292. }
  293. static int terminate_process_element(struct cxl_context *ctx)
  294. {
  295. int rc = 0;
  296. /* fast path terminate if it's already invalid */
  297. if (!(ctx->elem->software_state & cpu_to_be32(CXL_PE_SOFTWARE_STATE_V)))
  298. return rc;
  299. mutex_lock(&ctx->afu->spa_mutex);
  300. pr_devel("%s Terminate pe: %i started\n", __func__, ctx->pe);
  301. rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_TERMINATE,
  302. CXL_PE_SOFTWARE_STATE_V | CXL_PE_SOFTWARE_STATE_T);
  303. ctx->elem->software_state = 0; /* Remove Valid bit */
  304. pr_devel("%s Terminate pe: %i finished\n", __func__, ctx->pe);
  305. mutex_unlock(&ctx->afu->spa_mutex);
  306. return rc;
  307. }
  308. static int remove_process_element(struct cxl_context *ctx)
  309. {
  310. int rc = 0;
  311. mutex_lock(&ctx->afu->spa_mutex);
  312. pr_devel("%s Remove pe: %i started\n", __func__, ctx->pe);
  313. if (!(rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_REMOVE, 0)))
  314. ctx->pe_inserted = false;
  315. slb_invalid(ctx);
  316. pr_devel("%s Remove pe: %i finished\n", __func__, ctx->pe);
  317. mutex_unlock(&ctx->afu->spa_mutex);
  318. return rc;
  319. }
  320. void cxl_assign_psn_space(struct cxl_context *ctx)
  321. {
  322. if (!ctx->afu->pp_size || ctx->master) {
  323. ctx->psn_phys = ctx->afu->psn_phys;
  324. ctx->psn_size = ctx->afu->adapter->ps_size;
  325. } else {
  326. ctx->psn_phys = ctx->afu->psn_phys +
  327. (ctx->afu->pp_offset + ctx->afu->pp_size * ctx->pe);
  328. ctx->psn_size = ctx->afu->pp_size;
  329. }
  330. }
  331. static int activate_afu_directed(struct cxl_afu *afu)
  332. {
  333. int rc;
  334. dev_info(&afu->dev, "Activating AFU directed mode\n");
  335. if (alloc_spa(afu))
  336. return -ENOMEM;
  337. cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_AFU);
  338. cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
  339. cxl_p1n_write(afu, CXL_PSL_ID_An, CXL_PSL_ID_An_F | CXL_PSL_ID_An_L);
  340. afu->current_mode = CXL_MODE_DIRECTED;
  341. afu->num_procs = afu->max_procs_virtualised;
  342. if ((rc = cxl_chardev_m_afu_add(afu)))
  343. return rc;
  344. if ((rc = cxl_sysfs_afu_m_add(afu)))
  345. goto err;
  346. if ((rc = cxl_chardev_s_afu_add(afu)))
  347. goto err1;
  348. return 0;
  349. err1:
  350. cxl_sysfs_afu_m_remove(afu);
  351. err:
  352. cxl_chardev_afu_remove(afu);
  353. return rc;
  354. }
  355. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  356. #define set_endian(sr) ((sr) |= CXL_PSL_SR_An_LE)
  357. #else
  358. #define set_endian(sr) ((sr) &= ~(CXL_PSL_SR_An_LE))
  359. #endif
  360. static u64 calculate_sr(struct cxl_context *ctx)
  361. {
  362. u64 sr = 0;
  363. if (ctx->master)
  364. sr |= CXL_PSL_SR_An_MP;
  365. if (mfspr(SPRN_LPCR) & LPCR_TC)
  366. sr |= CXL_PSL_SR_An_TC;
  367. if (ctx->kernel) {
  368. sr |= CXL_PSL_SR_An_R | (mfmsr() & MSR_SF);
  369. sr |= CXL_PSL_SR_An_HV;
  370. } else {
  371. sr |= CXL_PSL_SR_An_PR | CXL_PSL_SR_An_R;
  372. set_endian(sr);
  373. sr &= ~(CXL_PSL_SR_An_HV);
  374. if (!test_tsk_thread_flag(current, TIF_32BIT))
  375. sr |= CXL_PSL_SR_An_SF;
  376. }
  377. return sr;
  378. }
  379. static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr)
  380. {
  381. u32 pid;
  382. int r, result;
  383. cxl_assign_psn_space(ctx);
  384. ctx->elem->ctxtime = 0; /* disable */
  385. ctx->elem->lpid = cpu_to_be32(mfspr(SPRN_LPID));
  386. ctx->elem->haurp = 0; /* disable */
  387. ctx->elem->sdr = cpu_to_be64(mfspr(SPRN_SDR1));
  388. pid = current->pid;
  389. if (ctx->kernel)
  390. pid = 0;
  391. ctx->elem->common.tid = 0;
  392. ctx->elem->common.pid = cpu_to_be32(pid);
  393. ctx->elem->sr = cpu_to_be64(calculate_sr(ctx));
  394. ctx->elem->common.csrp = 0; /* disable */
  395. ctx->elem->common.aurp0 = 0; /* disable */
  396. ctx->elem->common.aurp1 = 0; /* disable */
  397. cxl_prefault(ctx, wed);
  398. ctx->elem->common.sstp0 = cpu_to_be64(ctx->sstp0);
  399. ctx->elem->common.sstp1 = cpu_to_be64(ctx->sstp1);
  400. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  401. ctx->elem->ivte_offsets[r] = cpu_to_be16(ctx->irqs.offset[r]);
  402. ctx->elem->ivte_ranges[r] = cpu_to_be16(ctx->irqs.range[r]);
  403. }
  404. ctx->elem->common.amr = cpu_to_be64(amr);
  405. ctx->elem->common.wed = cpu_to_be64(wed);
  406. /* first guy needs to enable */
  407. if ((result = cxl_afu_check_and_enable(ctx->afu)))
  408. return result;
  409. add_process_element(ctx);
  410. return 0;
  411. }
  412. static int deactivate_afu_directed(struct cxl_afu *afu)
  413. {
  414. dev_info(&afu->dev, "Deactivating AFU directed mode\n");
  415. afu->current_mode = 0;
  416. afu->num_procs = 0;
  417. cxl_sysfs_afu_m_remove(afu);
  418. cxl_chardev_afu_remove(afu);
  419. __cxl_afu_reset(afu);
  420. cxl_afu_disable(afu);
  421. cxl_psl_purge(afu);
  422. release_spa(afu);
  423. return 0;
  424. }
  425. static int activate_dedicated_process(struct cxl_afu *afu)
  426. {
  427. dev_info(&afu->dev, "Activating dedicated process mode\n");
  428. cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_Process);
  429. cxl_p1n_write(afu, CXL_PSL_CtxTime_An, 0); /* disable */
  430. cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0); /* disable */
  431. cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
  432. cxl_p1n_write(afu, CXL_PSL_LPID_An, mfspr(SPRN_LPID));
  433. cxl_p1n_write(afu, CXL_HAURP_An, 0); /* disable */
  434. cxl_p1n_write(afu, CXL_PSL_SDR_An, mfspr(SPRN_SDR1));
  435. cxl_p2n_write(afu, CXL_CSRP_An, 0); /* disable */
  436. cxl_p2n_write(afu, CXL_AURP0_An, 0); /* disable */
  437. cxl_p2n_write(afu, CXL_AURP1_An, 0); /* disable */
  438. afu->current_mode = CXL_MODE_DEDICATED;
  439. afu->num_procs = 1;
  440. return cxl_chardev_d_afu_add(afu);
  441. }
  442. static int attach_dedicated(struct cxl_context *ctx, u64 wed, u64 amr)
  443. {
  444. struct cxl_afu *afu = ctx->afu;
  445. u64 pid;
  446. int rc;
  447. pid = (u64)current->pid << 32;
  448. if (ctx->kernel)
  449. pid = 0;
  450. cxl_p2n_write(afu, CXL_PSL_PID_TID_An, pid);
  451. cxl_p1n_write(afu, CXL_PSL_SR_An, calculate_sr(ctx));
  452. if ((rc = cxl_write_sstp(afu, ctx->sstp0, ctx->sstp1)))
  453. return rc;
  454. cxl_prefault(ctx, wed);
  455. cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An,
  456. (((u64)ctx->irqs.offset[0] & 0xffff) << 48) |
  457. (((u64)ctx->irqs.offset[1] & 0xffff) << 32) |
  458. (((u64)ctx->irqs.offset[2] & 0xffff) << 16) |
  459. ((u64)ctx->irqs.offset[3] & 0xffff));
  460. cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, (u64)
  461. (((u64)ctx->irqs.range[0] & 0xffff) << 48) |
  462. (((u64)ctx->irqs.range[1] & 0xffff) << 32) |
  463. (((u64)ctx->irqs.range[2] & 0xffff) << 16) |
  464. ((u64)ctx->irqs.range[3] & 0xffff));
  465. cxl_p2n_write(afu, CXL_PSL_AMR_An, amr);
  466. /* master only context for dedicated */
  467. cxl_assign_psn_space(ctx);
  468. if ((rc = __cxl_afu_reset(afu)))
  469. return rc;
  470. cxl_p2n_write(afu, CXL_PSL_WED_An, wed);
  471. return afu_enable(afu);
  472. }
  473. static int deactivate_dedicated_process(struct cxl_afu *afu)
  474. {
  475. dev_info(&afu->dev, "Deactivating dedicated process mode\n");
  476. afu->current_mode = 0;
  477. afu->num_procs = 0;
  478. cxl_chardev_afu_remove(afu);
  479. return 0;
  480. }
  481. int _cxl_afu_deactivate_mode(struct cxl_afu *afu, int mode)
  482. {
  483. if (mode == CXL_MODE_DIRECTED)
  484. return deactivate_afu_directed(afu);
  485. if (mode == CXL_MODE_DEDICATED)
  486. return deactivate_dedicated_process(afu);
  487. return 0;
  488. }
  489. int cxl_afu_deactivate_mode(struct cxl_afu *afu)
  490. {
  491. return _cxl_afu_deactivate_mode(afu, afu->current_mode);
  492. }
  493. int cxl_afu_activate_mode(struct cxl_afu *afu, int mode)
  494. {
  495. if (!mode)
  496. return 0;
  497. if (!(mode & afu->modes_supported))
  498. return -EINVAL;
  499. if (mode == CXL_MODE_DIRECTED)
  500. return activate_afu_directed(afu);
  501. if (mode == CXL_MODE_DEDICATED)
  502. return activate_dedicated_process(afu);
  503. return -EINVAL;
  504. }
  505. int cxl_attach_process(struct cxl_context *ctx, bool kernel, u64 wed, u64 amr)
  506. {
  507. ctx->kernel = kernel;
  508. if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
  509. return attach_afu_directed(ctx, wed, amr);
  510. if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
  511. return attach_dedicated(ctx, wed, amr);
  512. return -EINVAL;
  513. }
  514. static inline int detach_process_native_dedicated(struct cxl_context *ctx)
  515. {
  516. __cxl_afu_reset(ctx->afu);
  517. cxl_afu_disable(ctx->afu);
  518. cxl_psl_purge(ctx->afu);
  519. return 0;
  520. }
  521. static inline int detach_process_native_afu_directed(struct cxl_context *ctx)
  522. {
  523. if (!ctx->pe_inserted)
  524. return 0;
  525. if (terminate_process_element(ctx))
  526. return -1;
  527. if (remove_process_element(ctx))
  528. return -1;
  529. return 0;
  530. }
  531. int cxl_detach_process(struct cxl_context *ctx)
  532. {
  533. trace_cxl_detach(ctx);
  534. if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
  535. return detach_process_native_dedicated(ctx);
  536. return detach_process_native_afu_directed(ctx);
  537. }
  538. int cxl_get_irq(struct cxl_afu *afu, struct cxl_irq_info *info)
  539. {
  540. u64 pidtid;
  541. info->dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  542. info->dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
  543. info->dsr = cxl_p2n_read(afu, CXL_PSL_DSR_An);
  544. pidtid = cxl_p2n_read(afu, CXL_PSL_PID_TID_An);
  545. info->pid = pidtid >> 32;
  546. info->tid = pidtid & 0xffffffff;
  547. info->afu_err = cxl_p2n_read(afu, CXL_AFU_ERR_An);
  548. info->errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
  549. return 0;
  550. }
  551. static void recover_psl_err(struct cxl_afu *afu, u64 errstat)
  552. {
  553. u64 dsisr;
  554. pr_devel("RECOVERING FROM PSL ERROR... (0x%.16llx)\n", errstat);
  555. /* Clear PSL_DSISR[PE] */
  556. dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  557. cxl_p2n_write(afu, CXL_PSL_DSISR_An, dsisr & ~CXL_PSL_DSISR_An_PE);
  558. /* Write 1s to clear error status bits */
  559. cxl_p2n_write(afu, CXL_PSL_ErrStat_An, errstat);
  560. }
  561. int cxl_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask)
  562. {
  563. trace_cxl_psl_irq_ack(ctx, tfc);
  564. if (tfc)
  565. cxl_p2n_write(ctx->afu, CXL_PSL_TFC_An, tfc);
  566. if (psl_reset_mask)
  567. recover_psl_err(ctx->afu, psl_reset_mask);
  568. return 0;
  569. }
  570. int cxl_check_error(struct cxl_afu *afu)
  571. {
  572. return (cxl_p1n_read(afu, CXL_PSL_SCNTL_An) == ~0ULL);
  573. }