native.c 20 KB

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