native.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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 <linux/delay.h>
  17. #include <asm/synch.h>
  18. #include <misc/cxl-base.h>
  19. #include "cxl.h"
  20. #include "trace.h"
  21. static int afu_control(struct cxl_afu *afu, u64 command, u64 clear,
  22. u64 result, u64 mask, bool enabled)
  23. {
  24. u64 AFU_Cntl;
  25. unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
  26. int rc = 0;
  27. spin_lock(&afu->afu_cntl_lock);
  28. pr_devel("AFU command starting: %llx\n", command);
  29. trace_cxl_afu_ctrl(afu, command);
  30. AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  31. cxl_p2n_write(afu, CXL_AFU_Cntl_An, (AFU_Cntl & ~clear) | command);
  32. AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  33. while ((AFU_Cntl & mask) != result) {
  34. if (time_after_eq(jiffies, timeout)) {
  35. dev_warn(&afu->dev, "WARNING: AFU control timed out!\n");
  36. rc = -EBUSY;
  37. goto out;
  38. }
  39. if (!cxl_ops->link_ok(afu->adapter, afu)) {
  40. afu->enabled = enabled;
  41. rc = -EIO;
  42. goto out;
  43. }
  44. pr_devel_ratelimited("AFU control... (0x%016llx)\n",
  45. AFU_Cntl | command);
  46. cpu_relax();
  47. AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  48. };
  49. if (AFU_Cntl & CXL_AFU_Cntl_An_RA) {
  50. /*
  51. * Workaround for a bug in the XSL used in the Mellanox CX4
  52. * that fails to clear the RA bit after an AFU reset,
  53. * preventing subsequent AFU resets from working.
  54. */
  55. cxl_p2n_write(afu, CXL_AFU_Cntl_An, AFU_Cntl & ~CXL_AFU_Cntl_An_RA);
  56. }
  57. pr_devel("AFU command complete: %llx\n", command);
  58. afu->enabled = enabled;
  59. out:
  60. trace_cxl_afu_ctrl_done(afu, command, rc);
  61. spin_unlock(&afu->afu_cntl_lock);
  62. return rc;
  63. }
  64. static int afu_enable(struct cxl_afu *afu)
  65. {
  66. pr_devel("AFU enable request\n");
  67. return afu_control(afu, CXL_AFU_Cntl_An_E, 0,
  68. CXL_AFU_Cntl_An_ES_Enabled,
  69. CXL_AFU_Cntl_An_ES_MASK, true);
  70. }
  71. int cxl_afu_disable(struct cxl_afu *afu)
  72. {
  73. pr_devel("AFU disable request\n");
  74. return afu_control(afu, 0, CXL_AFU_Cntl_An_E,
  75. CXL_AFU_Cntl_An_ES_Disabled,
  76. CXL_AFU_Cntl_An_ES_MASK, false);
  77. }
  78. /* This will disable as well as reset */
  79. static int native_afu_reset(struct cxl_afu *afu)
  80. {
  81. pr_devel("AFU reset request\n");
  82. return afu_control(afu, CXL_AFU_Cntl_An_RA, 0,
  83. CXL_AFU_Cntl_An_RS_Complete | CXL_AFU_Cntl_An_ES_Disabled,
  84. CXL_AFU_Cntl_An_RS_MASK | CXL_AFU_Cntl_An_ES_MASK,
  85. false);
  86. }
  87. static int native_afu_check_and_enable(struct cxl_afu *afu)
  88. {
  89. if (!cxl_ops->link_ok(afu->adapter, afu)) {
  90. WARN(1, "Refusing to enable afu while link down!\n");
  91. return -EIO;
  92. }
  93. if (afu->enabled)
  94. return 0;
  95. return afu_enable(afu);
  96. }
  97. int cxl_psl_purge(struct cxl_afu *afu)
  98. {
  99. u64 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
  100. u64 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  101. u64 dsisr, dar;
  102. u64 start, end;
  103. unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
  104. int rc = 0;
  105. trace_cxl_psl_ctrl(afu, CXL_PSL_SCNTL_An_Pc);
  106. pr_devel("PSL purge request\n");
  107. if (!cxl_ops->link_ok(afu->adapter, afu)) {
  108. dev_warn(&afu->dev, "PSL Purge called with link down, ignoring\n");
  109. rc = -EIO;
  110. goto out;
  111. }
  112. if ((AFU_Cntl & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
  113. WARN(1, "psl_purge request while AFU not disabled!\n");
  114. cxl_afu_disable(afu);
  115. }
  116. cxl_p1n_write(afu, CXL_PSL_SCNTL_An,
  117. PSL_CNTL | CXL_PSL_SCNTL_An_Pc);
  118. start = local_clock();
  119. PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
  120. while ((PSL_CNTL & CXL_PSL_SCNTL_An_Ps_MASK)
  121. == CXL_PSL_SCNTL_An_Ps_Pending) {
  122. if (time_after_eq(jiffies, timeout)) {
  123. dev_warn(&afu->dev, "WARNING: PSL Purge timed out!\n");
  124. rc = -EBUSY;
  125. goto out;
  126. }
  127. if (!cxl_ops->link_ok(afu->adapter, afu)) {
  128. rc = -EIO;
  129. goto out;
  130. }
  131. dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  132. pr_devel_ratelimited("PSL purging... PSL_CNTL: 0x%016llx PSL_DSISR: 0x%016llx\n", PSL_CNTL, dsisr);
  133. if (dsisr & CXL_PSL_DSISR_TRANS) {
  134. dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
  135. dev_notice(&afu->dev, "PSL purge terminating pending translation, DSISR: 0x%016llx, DAR: 0x%016llx\n", dsisr, dar);
  136. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  137. } else if (dsisr) {
  138. dev_notice(&afu->dev, "PSL purge acknowledging pending non-translation fault, DSISR: 0x%016llx\n", dsisr);
  139. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  140. } else {
  141. cpu_relax();
  142. }
  143. PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
  144. };
  145. end = local_clock();
  146. pr_devel("PSL purged in %lld ns\n", end - start);
  147. cxl_p1n_write(afu, CXL_PSL_SCNTL_An,
  148. PSL_CNTL & ~CXL_PSL_SCNTL_An_Pc);
  149. out:
  150. trace_cxl_psl_ctrl_done(afu, CXL_PSL_SCNTL_An_Pc, rc);
  151. return rc;
  152. }
  153. static int spa_max_procs(int spa_size)
  154. {
  155. /*
  156. * From the CAIA:
  157. * end_of_SPA_area = SPA_Base + ((n+4) * 128) + (( ((n*8) + 127) >> 7) * 128) + 255
  158. * Most of that junk is really just an overly-complicated way of saying
  159. * the last 256 bytes are __aligned(128), so it's really:
  160. * end_of_SPA_area = end_of_PSL_queue_area + __aligned(128) 255
  161. * and
  162. * end_of_PSL_queue_area = SPA_Base + ((n+4) * 128) + (n*8) - 1
  163. * so
  164. * sizeof(SPA) = ((n+4) * 128) + (n*8) + __aligned(128) 256
  165. * Ignore the alignment (which is safe in this case as long as we are
  166. * careful with our rounding) and solve for n:
  167. */
  168. return ((spa_size / 8) - 96) / 17;
  169. }
  170. int cxl_alloc_spa(struct cxl_afu *afu)
  171. {
  172. unsigned spa_size;
  173. /* Work out how many pages to allocate */
  174. afu->native->spa_order = -1;
  175. do {
  176. afu->native->spa_order++;
  177. spa_size = (1 << afu->native->spa_order) * PAGE_SIZE;
  178. if (spa_size > 0x100000) {
  179. dev_warn(&afu->dev, "num_of_processes too large for the SPA, limiting to %i (0x%x)\n",
  180. afu->native->spa_max_procs, afu->native->spa_size);
  181. afu->num_procs = afu->native->spa_max_procs;
  182. break;
  183. }
  184. afu->native->spa_size = spa_size;
  185. afu->native->spa_max_procs = spa_max_procs(afu->native->spa_size);
  186. } while (afu->native->spa_max_procs < afu->num_procs);
  187. if (!(afu->native->spa = (struct cxl_process_element *)
  188. __get_free_pages(GFP_KERNEL | __GFP_ZERO, afu->native->spa_order))) {
  189. pr_err("cxl_alloc_spa: Unable to allocate scheduled process area\n");
  190. return -ENOMEM;
  191. }
  192. pr_devel("spa pages: %i afu->spa_max_procs: %i afu->num_procs: %i\n",
  193. 1<<afu->native->spa_order, afu->native->spa_max_procs, afu->num_procs);
  194. return 0;
  195. }
  196. static void attach_spa(struct cxl_afu *afu)
  197. {
  198. u64 spap;
  199. afu->native->sw_command_status = (__be64 *)((char *)afu->native->spa +
  200. ((afu->native->spa_max_procs + 3) * 128));
  201. spap = virt_to_phys(afu->native->spa) & CXL_PSL_SPAP_Addr;
  202. spap |= ((afu->native->spa_size >> (12 - CXL_PSL_SPAP_Size_Shift)) - 1) & CXL_PSL_SPAP_Size;
  203. spap |= CXL_PSL_SPAP_V;
  204. pr_devel("cxl: SPA allocated at 0x%p. Max processes: %i, sw_command_status: 0x%p CXL_PSL_SPAP_An=0x%016llx\n",
  205. afu->native->spa, afu->native->spa_max_procs,
  206. afu->native->sw_command_status, spap);
  207. cxl_p1n_write(afu, CXL_PSL_SPAP_An, spap);
  208. }
  209. static inline void detach_spa(struct cxl_afu *afu)
  210. {
  211. cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0);
  212. }
  213. void cxl_release_spa(struct cxl_afu *afu)
  214. {
  215. if (afu->native->spa) {
  216. free_pages((unsigned long) afu->native->spa,
  217. afu->native->spa_order);
  218. afu->native->spa = NULL;
  219. }
  220. }
  221. int cxl_tlb_slb_invalidate(struct cxl *adapter)
  222. {
  223. unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
  224. pr_devel("CXL adapter wide TLBIA & SLBIA\n");
  225. cxl_p1_write(adapter, CXL_PSL_AFUSEL, CXL_PSL_AFUSEL_A);
  226. cxl_p1_write(adapter, CXL_PSL_TLBIA, CXL_TLB_SLB_IQ_ALL);
  227. while (cxl_p1_read(adapter, CXL_PSL_TLBIA) & CXL_TLB_SLB_P) {
  228. if (time_after_eq(jiffies, timeout)) {
  229. dev_warn(&adapter->dev, "WARNING: CXL adapter wide TLBIA timed out!\n");
  230. return -EBUSY;
  231. }
  232. if (!cxl_ops->link_ok(adapter, NULL))
  233. return -EIO;
  234. cpu_relax();
  235. }
  236. cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_ALL);
  237. while (cxl_p1_read(adapter, CXL_PSL_SLBIA) & CXL_TLB_SLB_P) {
  238. if (time_after_eq(jiffies, timeout)) {
  239. dev_warn(&adapter->dev, "WARNING: CXL adapter wide SLBIA timed out!\n");
  240. return -EBUSY;
  241. }
  242. if (!cxl_ops->link_ok(adapter, NULL))
  243. return -EIO;
  244. cpu_relax();
  245. }
  246. return 0;
  247. }
  248. static int cxl_write_sstp(struct cxl_afu *afu, u64 sstp0, u64 sstp1)
  249. {
  250. int rc;
  251. /* 1. Disable SSTP by writing 0 to SSTP1[V] */
  252. cxl_p2n_write(afu, CXL_SSTP1_An, 0);
  253. /* 2. Invalidate all SLB entries */
  254. if ((rc = cxl_afu_slbia(afu)))
  255. return rc;
  256. /* 3. Set SSTP0_An */
  257. cxl_p2n_write(afu, CXL_SSTP0_An, sstp0);
  258. /* 4. Set SSTP1_An */
  259. cxl_p2n_write(afu, CXL_SSTP1_An, sstp1);
  260. return 0;
  261. }
  262. /* Using per slice version may improve performance here. (ie. SLBIA_An) */
  263. static void slb_invalid(struct cxl_context *ctx)
  264. {
  265. struct cxl *adapter = ctx->afu->adapter;
  266. u64 slbia;
  267. WARN_ON(!mutex_is_locked(&ctx->afu->native->spa_mutex));
  268. cxl_p1_write(adapter, CXL_PSL_LBISEL,
  269. ((u64)be32_to_cpu(ctx->elem->common.pid) << 32) |
  270. be32_to_cpu(ctx->elem->lpid));
  271. cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_LPIDPID);
  272. while (1) {
  273. if (!cxl_ops->link_ok(adapter, NULL))
  274. break;
  275. slbia = cxl_p1_read(adapter, CXL_PSL_SLBIA);
  276. if (!(slbia & CXL_TLB_SLB_P))
  277. break;
  278. cpu_relax();
  279. }
  280. }
  281. static int do_process_element_cmd(struct cxl_context *ctx,
  282. u64 cmd, u64 pe_state)
  283. {
  284. u64 state;
  285. unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
  286. int rc = 0;
  287. trace_cxl_llcmd(ctx, cmd);
  288. WARN_ON(!ctx->afu->enabled);
  289. ctx->elem->software_state = cpu_to_be32(pe_state);
  290. smp_wmb();
  291. *(ctx->afu->native->sw_command_status) = cpu_to_be64(cmd | 0 | ctx->pe);
  292. smp_mb();
  293. cxl_p1n_write(ctx->afu, CXL_PSL_LLCMD_An, cmd | ctx->pe);
  294. while (1) {
  295. if (time_after_eq(jiffies, timeout)) {
  296. dev_warn(&ctx->afu->dev, "WARNING: Process Element Command timed out!\n");
  297. rc = -EBUSY;
  298. goto out;
  299. }
  300. if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
  301. dev_warn(&ctx->afu->dev, "WARNING: Device link down, aborting Process Element Command!\n");
  302. rc = -EIO;
  303. goto out;
  304. }
  305. state = be64_to_cpup(ctx->afu->native->sw_command_status);
  306. if (state == ~0ULL) {
  307. pr_err("cxl: Error adding process element to AFU\n");
  308. rc = -1;
  309. goto out;
  310. }
  311. if ((state & (CXL_SPA_SW_CMD_MASK | CXL_SPA_SW_STATE_MASK | CXL_SPA_SW_LINK_MASK)) ==
  312. (cmd | (cmd >> 16) | ctx->pe))
  313. break;
  314. /*
  315. * The command won't finish in the PSL if there are
  316. * outstanding DSIs. Hence we need to yield here in
  317. * case there are outstanding DSIs that we need to
  318. * service. Tuning possiblity: we could wait for a
  319. * while before sched
  320. */
  321. schedule();
  322. }
  323. out:
  324. trace_cxl_llcmd_done(ctx, cmd, rc);
  325. return rc;
  326. }
  327. static int add_process_element(struct cxl_context *ctx)
  328. {
  329. int rc = 0;
  330. mutex_lock(&ctx->afu->native->spa_mutex);
  331. pr_devel("%s Adding pe: %i started\n", __func__, ctx->pe);
  332. if (!(rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_ADD, CXL_PE_SOFTWARE_STATE_V)))
  333. ctx->pe_inserted = true;
  334. pr_devel("%s Adding pe: %i finished\n", __func__, ctx->pe);
  335. mutex_unlock(&ctx->afu->native->spa_mutex);
  336. return rc;
  337. }
  338. static int terminate_process_element(struct cxl_context *ctx)
  339. {
  340. int rc = 0;
  341. /* fast path terminate if it's already invalid */
  342. if (!(ctx->elem->software_state & cpu_to_be32(CXL_PE_SOFTWARE_STATE_V)))
  343. return rc;
  344. mutex_lock(&ctx->afu->native->spa_mutex);
  345. pr_devel("%s Terminate pe: %i started\n", __func__, ctx->pe);
  346. /* We could be asked to terminate when the hw is down. That
  347. * should always succeed: it's not running if the hw has gone
  348. * away and is being reset.
  349. */
  350. if (cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
  351. rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_TERMINATE,
  352. CXL_PE_SOFTWARE_STATE_V | CXL_PE_SOFTWARE_STATE_T);
  353. ctx->elem->software_state = 0; /* Remove Valid bit */
  354. pr_devel("%s Terminate pe: %i finished\n", __func__, ctx->pe);
  355. mutex_unlock(&ctx->afu->native->spa_mutex);
  356. return rc;
  357. }
  358. static int remove_process_element(struct cxl_context *ctx)
  359. {
  360. int rc = 0;
  361. mutex_lock(&ctx->afu->native->spa_mutex);
  362. pr_devel("%s Remove pe: %i started\n", __func__, ctx->pe);
  363. /* We could be asked to remove when the hw is down. Again, if
  364. * the hw is down, the PE is gone, so we succeed.
  365. */
  366. if (cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
  367. rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_REMOVE, 0);
  368. if (!rc)
  369. ctx->pe_inserted = false;
  370. slb_invalid(ctx);
  371. pr_devel("%s Remove pe: %i finished\n", __func__, ctx->pe);
  372. mutex_unlock(&ctx->afu->native->spa_mutex);
  373. return rc;
  374. }
  375. void cxl_assign_psn_space(struct cxl_context *ctx)
  376. {
  377. if (!ctx->afu->pp_size || ctx->master) {
  378. ctx->psn_phys = ctx->afu->psn_phys;
  379. ctx->psn_size = ctx->afu->adapter->ps_size;
  380. } else {
  381. ctx->psn_phys = ctx->afu->psn_phys +
  382. (ctx->afu->native->pp_offset + ctx->afu->pp_size * ctx->pe);
  383. ctx->psn_size = ctx->afu->pp_size;
  384. }
  385. }
  386. static int activate_afu_directed(struct cxl_afu *afu)
  387. {
  388. int rc;
  389. dev_info(&afu->dev, "Activating AFU directed mode\n");
  390. afu->num_procs = afu->max_procs_virtualised;
  391. if (afu->native->spa == NULL) {
  392. if (cxl_alloc_spa(afu))
  393. return -ENOMEM;
  394. }
  395. attach_spa(afu);
  396. cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_AFU);
  397. cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
  398. cxl_p1n_write(afu, CXL_PSL_ID_An, CXL_PSL_ID_An_F | CXL_PSL_ID_An_L);
  399. afu->current_mode = CXL_MODE_DIRECTED;
  400. if ((rc = cxl_chardev_m_afu_add(afu)))
  401. return rc;
  402. if ((rc = cxl_sysfs_afu_m_add(afu)))
  403. goto err;
  404. if ((rc = cxl_chardev_s_afu_add(afu)))
  405. goto err1;
  406. return 0;
  407. err1:
  408. cxl_sysfs_afu_m_remove(afu);
  409. err:
  410. cxl_chardev_afu_remove(afu);
  411. return rc;
  412. }
  413. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  414. #define set_endian(sr) ((sr) |= CXL_PSL_SR_An_LE)
  415. #else
  416. #define set_endian(sr) ((sr) &= ~(CXL_PSL_SR_An_LE))
  417. #endif
  418. static u64 calculate_sr(struct cxl_context *ctx)
  419. {
  420. u64 sr = 0;
  421. set_endian(sr);
  422. if (ctx->master)
  423. sr |= CXL_PSL_SR_An_MP;
  424. if (mfspr(SPRN_LPCR) & LPCR_TC)
  425. sr |= CXL_PSL_SR_An_TC;
  426. if (ctx->kernel) {
  427. if (!ctx->real_mode)
  428. sr |= CXL_PSL_SR_An_R;
  429. sr |= (mfmsr() & MSR_SF) | CXL_PSL_SR_An_HV;
  430. } else {
  431. sr |= CXL_PSL_SR_An_PR | CXL_PSL_SR_An_R;
  432. sr &= ~(CXL_PSL_SR_An_HV);
  433. if (!test_tsk_thread_flag(current, TIF_32BIT))
  434. sr |= CXL_PSL_SR_An_SF;
  435. }
  436. return sr;
  437. }
  438. static void update_ivtes_directed(struct cxl_context *ctx)
  439. {
  440. bool need_update = (ctx->status == STARTED);
  441. int r;
  442. if (need_update) {
  443. WARN_ON(terminate_process_element(ctx));
  444. WARN_ON(remove_process_element(ctx));
  445. }
  446. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  447. ctx->elem->ivte_offsets[r] = cpu_to_be16(ctx->irqs.offset[r]);
  448. ctx->elem->ivte_ranges[r] = cpu_to_be16(ctx->irqs.range[r]);
  449. }
  450. /*
  451. * Theoretically we could use the update llcmd, instead of a
  452. * terminate/remove/add (or if an atomic update was required we could
  453. * do a suspend/update/resume), however it seems there might be issues
  454. * with the update llcmd on some cards (including those using an XSL on
  455. * an ASIC) so for now it's safest to go with the commands that are
  456. * known to work. In the future if we come across a situation where the
  457. * card may be performing transactions using the same PE while we are
  458. * doing this update we might need to revisit this.
  459. */
  460. if (need_update)
  461. WARN_ON(add_process_element(ctx));
  462. }
  463. static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr)
  464. {
  465. u32 pid;
  466. int result;
  467. cxl_assign_psn_space(ctx);
  468. ctx->elem->ctxtime = 0; /* disable */
  469. ctx->elem->lpid = cpu_to_be32(mfspr(SPRN_LPID));
  470. ctx->elem->haurp = 0; /* disable */
  471. ctx->elem->sdr = cpu_to_be64(mfspr(SPRN_SDR1));
  472. pid = current->pid;
  473. if (ctx->kernel)
  474. pid = 0;
  475. ctx->elem->common.tid = 0;
  476. ctx->elem->common.pid = cpu_to_be32(pid);
  477. ctx->elem->sr = cpu_to_be64(calculate_sr(ctx));
  478. ctx->elem->common.csrp = 0; /* disable */
  479. ctx->elem->common.aurp0 = 0; /* disable */
  480. ctx->elem->common.aurp1 = 0; /* disable */
  481. cxl_prefault(ctx, wed);
  482. ctx->elem->common.sstp0 = cpu_to_be64(ctx->sstp0);
  483. ctx->elem->common.sstp1 = cpu_to_be64(ctx->sstp1);
  484. /*
  485. * Ensure we have the multiplexed PSL interrupt set up to take faults
  486. * for kernel contexts that may not have allocated any AFU IRQs at all:
  487. */
  488. if (ctx->irqs.range[0] == 0) {
  489. ctx->irqs.offset[0] = ctx->afu->native->psl_hwirq;
  490. ctx->irqs.range[0] = 1;
  491. }
  492. update_ivtes_directed(ctx);
  493. ctx->elem->common.amr = cpu_to_be64(amr);
  494. ctx->elem->common.wed = cpu_to_be64(wed);
  495. /* first guy needs to enable */
  496. if ((result = cxl_ops->afu_check_and_enable(ctx->afu)))
  497. return result;
  498. return add_process_element(ctx);
  499. }
  500. static int deactivate_afu_directed(struct cxl_afu *afu)
  501. {
  502. dev_info(&afu->dev, "Deactivating AFU directed mode\n");
  503. afu->current_mode = 0;
  504. afu->num_procs = 0;
  505. cxl_sysfs_afu_m_remove(afu);
  506. cxl_chardev_afu_remove(afu);
  507. /*
  508. * The CAIA section 2.2.1 indicates that the procedure for starting and
  509. * stopping an AFU in AFU directed mode is AFU specific, which is not
  510. * ideal since this code is generic and with one exception has no
  511. * knowledge of the AFU. This is in contrast to the procedure for
  512. * disabling a dedicated process AFU, which is documented to just
  513. * require a reset. The architecture does indicate that both an AFU
  514. * reset and an AFU disable should result in the AFU being disabled and
  515. * we do both followed by a PSL purge for safety.
  516. *
  517. * Notably we used to have some issues with the disable sequence on PSL
  518. * cards, which is why we ended up using this heavy weight procedure in
  519. * the first place, however a bug was discovered that had rendered the
  520. * disable operation ineffective, so it is conceivable that was the
  521. * sole explanation for those difficulties. Careful regression testing
  522. * is recommended if anyone attempts to remove or reorder these
  523. * operations.
  524. *
  525. * The XSL on the Mellanox CX4 behaves a little differently from the
  526. * PSL based cards and will time out an AFU reset if the AFU is still
  527. * enabled. That card is special in that we do have a means to identify
  528. * it from this code, so in that case we skip the reset and just use a
  529. * disable/purge to avoid the timeout and corresponding noise in the
  530. * kernel log.
  531. */
  532. if (afu->adapter->native->sl_ops->needs_reset_before_disable)
  533. cxl_ops->afu_reset(afu);
  534. cxl_afu_disable(afu);
  535. cxl_psl_purge(afu);
  536. return 0;
  537. }
  538. static int activate_dedicated_process(struct cxl_afu *afu)
  539. {
  540. dev_info(&afu->dev, "Activating dedicated process mode\n");
  541. cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_Process);
  542. cxl_p1n_write(afu, CXL_PSL_CtxTime_An, 0); /* disable */
  543. cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0); /* disable */
  544. cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
  545. cxl_p1n_write(afu, CXL_PSL_LPID_An, mfspr(SPRN_LPID));
  546. cxl_p1n_write(afu, CXL_HAURP_An, 0); /* disable */
  547. cxl_p1n_write(afu, CXL_PSL_SDR_An, mfspr(SPRN_SDR1));
  548. cxl_p2n_write(afu, CXL_CSRP_An, 0); /* disable */
  549. cxl_p2n_write(afu, CXL_AURP0_An, 0); /* disable */
  550. cxl_p2n_write(afu, CXL_AURP1_An, 0); /* disable */
  551. afu->current_mode = CXL_MODE_DEDICATED;
  552. afu->num_procs = 1;
  553. return cxl_chardev_d_afu_add(afu);
  554. }
  555. static void update_ivtes_dedicated(struct cxl_context *ctx)
  556. {
  557. struct cxl_afu *afu = ctx->afu;
  558. cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An,
  559. (((u64)ctx->irqs.offset[0] & 0xffff) << 48) |
  560. (((u64)ctx->irqs.offset[1] & 0xffff) << 32) |
  561. (((u64)ctx->irqs.offset[2] & 0xffff) << 16) |
  562. ((u64)ctx->irqs.offset[3] & 0xffff));
  563. cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, (u64)
  564. (((u64)ctx->irqs.range[0] & 0xffff) << 48) |
  565. (((u64)ctx->irqs.range[1] & 0xffff) << 32) |
  566. (((u64)ctx->irqs.range[2] & 0xffff) << 16) |
  567. ((u64)ctx->irqs.range[3] & 0xffff));
  568. }
  569. static int attach_dedicated(struct cxl_context *ctx, u64 wed, u64 amr)
  570. {
  571. struct cxl_afu *afu = ctx->afu;
  572. u64 pid;
  573. int rc;
  574. pid = (u64)current->pid << 32;
  575. if (ctx->kernel)
  576. pid = 0;
  577. cxl_p2n_write(afu, CXL_PSL_PID_TID_An, pid);
  578. cxl_p1n_write(afu, CXL_PSL_SR_An, calculate_sr(ctx));
  579. if ((rc = cxl_write_sstp(afu, ctx->sstp0, ctx->sstp1)))
  580. return rc;
  581. cxl_prefault(ctx, wed);
  582. update_ivtes_dedicated(ctx);
  583. cxl_p2n_write(afu, CXL_PSL_AMR_An, amr);
  584. /* master only context for dedicated */
  585. cxl_assign_psn_space(ctx);
  586. if ((rc = cxl_ops->afu_reset(afu)))
  587. return rc;
  588. cxl_p2n_write(afu, CXL_PSL_WED_An, wed);
  589. return afu_enable(afu);
  590. }
  591. static int deactivate_dedicated_process(struct cxl_afu *afu)
  592. {
  593. dev_info(&afu->dev, "Deactivating dedicated process mode\n");
  594. afu->current_mode = 0;
  595. afu->num_procs = 0;
  596. cxl_chardev_afu_remove(afu);
  597. return 0;
  598. }
  599. static int native_afu_deactivate_mode(struct cxl_afu *afu, int mode)
  600. {
  601. if (mode == CXL_MODE_DIRECTED)
  602. return deactivate_afu_directed(afu);
  603. if (mode == CXL_MODE_DEDICATED)
  604. return deactivate_dedicated_process(afu);
  605. return 0;
  606. }
  607. static int native_afu_activate_mode(struct cxl_afu *afu, int mode)
  608. {
  609. if (!mode)
  610. return 0;
  611. if (!(mode & afu->modes_supported))
  612. return -EINVAL;
  613. if (!cxl_ops->link_ok(afu->adapter, afu)) {
  614. WARN(1, "Device link is down, refusing to activate!\n");
  615. return -EIO;
  616. }
  617. if (mode == CXL_MODE_DIRECTED)
  618. return activate_afu_directed(afu);
  619. if (mode == CXL_MODE_DEDICATED)
  620. return activate_dedicated_process(afu);
  621. return -EINVAL;
  622. }
  623. static int native_attach_process(struct cxl_context *ctx, bool kernel,
  624. u64 wed, u64 amr)
  625. {
  626. if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
  627. WARN(1, "Device link is down, refusing to attach process!\n");
  628. return -EIO;
  629. }
  630. ctx->kernel = kernel;
  631. if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
  632. return attach_afu_directed(ctx, wed, amr);
  633. if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
  634. return attach_dedicated(ctx, wed, amr);
  635. return -EINVAL;
  636. }
  637. static inline int detach_process_native_dedicated(struct cxl_context *ctx)
  638. {
  639. /*
  640. * The CAIA section 2.1.1 indicates that we need to do an AFU reset to
  641. * stop the AFU in dedicated mode (we therefore do not make that
  642. * optional like we do in the afu directed path). It does not indicate
  643. * that we need to do an explicit disable (which should occur
  644. * implicitly as part of the reset) or purge, but we do these as well
  645. * to be on the safe side.
  646. *
  647. * Notably we used to have some issues with the disable sequence
  648. * (before the sequence was spelled out in the architecture) which is
  649. * why we were so heavy weight in the first place, however a bug was
  650. * discovered that had rendered the disable operation ineffective, so
  651. * it is conceivable that was the sole explanation for those
  652. * difficulties. Point is, we should be careful and do some regression
  653. * testing if we ever attempt to remove any part of this procedure.
  654. */
  655. cxl_ops->afu_reset(ctx->afu);
  656. cxl_afu_disable(ctx->afu);
  657. cxl_psl_purge(ctx->afu);
  658. return 0;
  659. }
  660. static void native_update_ivtes(struct cxl_context *ctx)
  661. {
  662. if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
  663. return update_ivtes_directed(ctx);
  664. if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
  665. return update_ivtes_dedicated(ctx);
  666. WARN(1, "native_update_ivtes: Bad mode\n");
  667. }
  668. static inline int detach_process_native_afu_directed(struct cxl_context *ctx)
  669. {
  670. if (!ctx->pe_inserted)
  671. return 0;
  672. if (terminate_process_element(ctx))
  673. return -1;
  674. if (remove_process_element(ctx))
  675. return -1;
  676. return 0;
  677. }
  678. static int native_detach_process(struct cxl_context *ctx)
  679. {
  680. trace_cxl_detach(ctx);
  681. if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
  682. return detach_process_native_dedicated(ctx);
  683. return detach_process_native_afu_directed(ctx);
  684. }
  685. static int native_get_irq_info(struct cxl_afu *afu, struct cxl_irq_info *info)
  686. {
  687. u64 pidtid;
  688. /* If the adapter has gone away, we can't get any meaningful
  689. * information.
  690. */
  691. if (!cxl_ops->link_ok(afu->adapter, afu))
  692. return -EIO;
  693. info->dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  694. info->dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
  695. info->dsr = cxl_p2n_read(afu, CXL_PSL_DSR_An);
  696. pidtid = cxl_p2n_read(afu, CXL_PSL_PID_TID_An);
  697. info->pid = pidtid >> 32;
  698. info->tid = pidtid & 0xffffffff;
  699. info->afu_err = cxl_p2n_read(afu, CXL_AFU_ERR_An);
  700. info->errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
  701. info->proc_handle = 0;
  702. return 0;
  703. }
  704. void cxl_native_psl_irq_dump_regs(struct cxl_context *ctx)
  705. {
  706. u64 fir1, fir2, fir_slice, serr, afu_debug;
  707. fir1 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR1);
  708. fir2 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR2);
  709. fir_slice = cxl_p1n_read(ctx->afu, CXL_PSL_FIR_SLICE_An);
  710. afu_debug = cxl_p1n_read(ctx->afu, CXL_AFU_DEBUG_An);
  711. dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%016llx\n", fir1);
  712. dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%016llx\n", fir2);
  713. if (ctx->afu->adapter->native->sl_ops->register_serr_irq) {
  714. serr = cxl_p1n_read(ctx->afu, CXL_PSL_SERR_An);
  715. cxl_afu_decode_psl_serr(ctx->afu, serr);
  716. }
  717. dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
  718. dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
  719. }
  720. static irqreturn_t native_handle_psl_slice_error(struct cxl_context *ctx,
  721. u64 dsisr, u64 errstat)
  722. {
  723. dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%016llx\n", errstat);
  724. if (ctx->afu->adapter->native->sl_ops->psl_irq_dump_registers)
  725. ctx->afu->adapter->native->sl_ops->psl_irq_dump_registers(ctx);
  726. if (ctx->afu->adapter->native->sl_ops->debugfs_stop_trace) {
  727. dev_crit(&ctx->afu->dev, "STOPPING CXL TRACE\n");
  728. ctx->afu->adapter->native->sl_ops->debugfs_stop_trace(ctx->afu->adapter);
  729. }
  730. return cxl_ops->ack_irq(ctx, 0, errstat);
  731. }
  732. static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct cxl_irq_info *irq_info)
  733. {
  734. if (irq_info->dsisr & CXL_PSL_DSISR_TRANS)
  735. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  736. else
  737. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  738. return IRQ_HANDLED;
  739. }
  740. static irqreturn_t native_irq_multiplexed(int irq, void *data)
  741. {
  742. struct cxl_afu *afu = data;
  743. struct cxl_context *ctx;
  744. struct cxl_irq_info irq_info;
  745. int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff;
  746. int ret;
  747. if ((ret = native_get_irq_info(afu, &irq_info))) {
  748. WARN(1, "Unable to get CXL IRQ Info: %i\n", ret);
  749. return fail_psl_irq(afu, &irq_info);
  750. }
  751. rcu_read_lock();
  752. ctx = idr_find(&afu->contexts_idr, ph);
  753. if (ctx) {
  754. ret = cxl_irq(irq, ctx, &irq_info);
  755. rcu_read_unlock();
  756. return ret;
  757. }
  758. rcu_read_unlock();
  759. WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %016llx DAR"
  760. " %016llx\n(Possible AFU HW issue - was a term/remove acked"
  761. " with outstanding transactions?)\n", ph, irq_info.dsisr,
  762. irq_info.dar);
  763. return fail_psl_irq(afu, &irq_info);
  764. }
  765. static void native_irq_wait(struct cxl_context *ctx)
  766. {
  767. u64 dsisr;
  768. int timeout = 1000;
  769. int ph;
  770. /*
  771. * Wait until no further interrupts are presented by the PSL
  772. * for this context.
  773. */
  774. while (timeout--) {
  775. ph = cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) & 0xffff;
  776. if (ph != ctx->pe)
  777. return;
  778. dsisr = cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An);
  779. if ((dsisr & CXL_PSL_DSISR_PENDING) == 0)
  780. return;
  781. /*
  782. * We are waiting for the workqueue to process our
  783. * irq, so need to let that run here.
  784. */
  785. msleep(1);
  786. }
  787. dev_warn(&ctx->afu->dev, "WARNING: waiting on DSI for PE %i"
  788. " DSISR %016llx!\n", ph, dsisr);
  789. return;
  790. }
  791. static irqreturn_t native_slice_irq_err(int irq, void *data)
  792. {
  793. struct cxl_afu *afu = data;
  794. u64 fir_slice, errstat, serr, afu_debug, afu_error, dsisr;
  795. /*
  796. * slice err interrupt is only used with full PSL (no XSL)
  797. */
  798. serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  799. fir_slice = cxl_p1n_read(afu, CXL_PSL_FIR_SLICE_An);
  800. errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
  801. afu_debug = cxl_p1n_read(afu, CXL_AFU_DEBUG_An);
  802. afu_error = cxl_p2n_read(afu, CXL_AFU_ERR_An);
  803. dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  804. cxl_afu_decode_psl_serr(afu, serr);
  805. dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
  806. dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%016llx\n", errstat);
  807. dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
  808. dev_crit(&afu->dev, "AFU_ERR_An: 0x%.16llx\n", afu_error);
  809. dev_crit(&afu->dev, "PSL_DSISR_An: 0x%.16llx\n", dsisr);
  810. cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
  811. return IRQ_HANDLED;
  812. }
  813. void cxl_native_err_irq_dump_regs(struct cxl *adapter)
  814. {
  815. u64 fir1, fir2;
  816. fir1 = cxl_p1_read(adapter, CXL_PSL_FIR1);
  817. fir2 = cxl_p1_read(adapter, CXL_PSL_FIR2);
  818. dev_crit(&adapter->dev, "PSL_FIR1: 0x%016llx\nPSL_FIR2: 0x%016llx\n", fir1, fir2);
  819. }
  820. static irqreturn_t native_irq_err(int irq, void *data)
  821. {
  822. struct cxl *adapter = data;
  823. u64 err_ivte;
  824. WARN(1, "CXL ERROR interrupt %i\n", irq);
  825. err_ivte = cxl_p1_read(adapter, CXL_PSL_ErrIVTE);
  826. dev_crit(&adapter->dev, "PSL_ErrIVTE: 0x%016llx\n", err_ivte);
  827. if (adapter->native->sl_ops->debugfs_stop_trace) {
  828. dev_crit(&adapter->dev, "STOPPING CXL TRACE\n");
  829. adapter->native->sl_ops->debugfs_stop_trace(adapter);
  830. }
  831. if (adapter->native->sl_ops->err_irq_dump_registers)
  832. adapter->native->sl_ops->err_irq_dump_registers(adapter);
  833. return IRQ_HANDLED;
  834. }
  835. int cxl_native_register_psl_err_irq(struct cxl *adapter)
  836. {
  837. int rc;
  838. adapter->irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
  839. dev_name(&adapter->dev));
  840. if (!adapter->irq_name)
  841. return -ENOMEM;
  842. if ((rc = cxl_register_one_irq(adapter, native_irq_err, adapter,
  843. &adapter->native->err_hwirq,
  844. &adapter->native->err_virq,
  845. adapter->irq_name))) {
  846. kfree(adapter->irq_name);
  847. adapter->irq_name = NULL;
  848. return rc;
  849. }
  850. cxl_p1_write(adapter, CXL_PSL_ErrIVTE, adapter->native->err_hwirq & 0xffff);
  851. return 0;
  852. }
  853. void cxl_native_release_psl_err_irq(struct cxl *adapter)
  854. {
  855. if (adapter->native->err_virq != irq_find_mapping(NULL, adapter->native->err_hwirq))
  856. return;
  857. cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
  858. cxl_unmap_irq(adapter->native->err_virq, adapter);
  859. cxl_ops->release_one_irq(adapter, adapter->native->err_hwirq);
  860. kfree(adapter->irq_name);
  861. }
  862. int cxl_native_register_serr_irq(struct cxl_afu *afu)
  863. {
  864. u64 serr;
  865. int rc;
  866. afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
  867. dev_name(&afu->dev));
  868. if (!afu->err_irq_name)
  869. return -ENOMEM;
  870. if ((rc = cxl_register_one_irq(afu->adapter, native_slice_irq_err, afu,
  871. &afu->serr_hwirq,
  872. &afu->serr_virq, afu->err_irq_name))) {
  873. kfree(afu->err_irq_name);
  874. afu->err_irq_name = NULL;
  875. return rc;
  876. }
  877. serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  878. serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff);
  879. cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
  880. return 0;
  881. }
  882. void cxl_native_release_serr_irq(struct cxl_afu *afu)
  883. {
  884. if (afu->serr_virq != irq_find_mapping(NULL, afu->serr_hwirq))
  885. return;
  886. cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
  887. cxl_unmap_irq(afu->serr_virq, afu);
  888. cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq);
  889. kfree(afu->err_irq_name);
  890. }
  891. int cxl_native_register_psl_irq(struct cxl_afu *afu)
  892. {
  893. int rc;
  894. afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s",
  895. dev_name(&afu->dev));
  896. if (!afu->psl_irq_name)
  897. return -ENOMEM;
  898. if ((rc = cxl_register_one_irq(afu->adapter, native_irq_multiplexed,
  899. afu, &afu->native->psl_hwirq, &afu->native->psl_virq,
  900. afu->psl_irq_name))) {
  901. kfree(afu->psl_irq_name);
  902. afu->psl_irq_name = NULL;
  903. }
  904. return rc;
  905. }
  906. void cxl_native_release_psl_irq(struct cxl_afu *afu)
  907. {
  908. if (afu->native->psl_virq != irq_find_mapping(NULL, afu->native->psl_hwirq))
  909. return;
  910. cxl_unmap_irq(afu->native->psl_virq, afu);
  911. cxl_ops->release_one_irq(afu->adapter, afu->native->psl_hwirq);
  912. kfree(afu->psl_irq_name);
  913. }
  914. static void recover_psl_err(struct cxl_afu *afu, u64 errstat)
  915. {
  916. u64 dsisr;
  917. pr_devel("RECOVERING FROM PSL ERROR... (0x%016llx)\n", errstat);
  918. /* Clear PSL_DSISR[PE] */
  919. dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  920. cxl_p2n_write(afu, CXL_PSL_DSISR_An, dsisr & ~CXL_PSL_DSISR_An_PE);
  921. /* Write 1s to clear error status bits */
  922. cxl_p2n_write(afu, CXL_PSL_ErrStat_An, errstat);
  923. }
  924. static int native_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask)
  925. {
  926. trace_cxl_psl_irq_ack(ctx, tfc);
  927. if (tfc)
  928. cxl_p2n_write(ctx->afu, CXL_PSL_TFC_An, tfc);
  929. if (psl_reset_mask)
  930. recover_psl_err(ctx->afu, psl_reset_mask);
  931. return 0;
  932. }
  933. int cxl_check_error(struct cxl_afu *afu)
  934. {
  935. return (cxl_p1n_read(afu, CXL_PSL_SCNTL_An) == ~0ULL);
  936. }
  937. static bool native_support_attributes(const char *attr_name,
  938. enum cxl_attrs type)
  939. {
  940. return true;
  941. }
  942. static int native_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off, u64 *out)
  943. {
  944. if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
  945. return -EIO;
  946. if (unlikely(off >= afu->crs_len))
  947. return -ERANGE;
  948. *out = in_le64(afu->native->afu_desc_mmio + afu->crs_offset +
  949. (cr * afu->crs_len) + off);
  950. return 0;
  951. }
  952. static int native_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off, u32 *out)
  953. {
  954. if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
  955. return -EIO;
  956. if (unlikely(off >= afu->crs_len))
  957. return -ERANGE;
  958. *out = in_le32(afu->native->afu_desc_mmio + afu->crs_offset +
  959. (cr * afu->crs_len) + off);
  960. return 0;
  961. }
  962. static int native_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off, u16 *out)
  963. {
  964. u64 aligned_off = off & ~0x3L;
  965. u32 val;
  966. int rc;
  967. rc = native_afu_cr_read32(afu, cr, aligned_off, &val);
  968. if (!rc)
  969. *out = (val >> ((off & 0x3) * 8)) & 0xffff;
  970. return rc;
  971. }
  972. static int native_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off, u8 *out)
  973. {
  974. u64 aligned_off = off & ~0x3L;
  975. u32 val;
  976. int rc;
  977. rc = native_afu_cr_read32(afu, cr, aligned_off, &val);
  978. if (!rc)
  979. *out = (val >> ((off & 0x3) * 8)) & 0xff;
  980. return rc;
  981. }
  982. static int native_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in)
  983. {
  984. if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
  985. return -EIO;
  986. if (unlikely(off >= afu->crs_len))
  987. return -ERANGE;
  988. out_le32(afu->native->afu_desc_mmio + afu->crs_offset +
  989. (cr * afu->crs_len) + off, in);
  990. return 0;
  991. }
  992. static int native_afu_cr_write16(struct cxl_afu *afu, int cr, u64 off, u16 in)
  993. {
  994. u64 aligned_off = off & ~0x3L;
  995. u32 val32, mask, shift;
  996. int rc;
  997. rc = native_afu_cr_read32(afu, cr, aligned_off, &val32);
  998. if (rc)
  999. return rc;
  1000. shift = (off & 0x3) * 8;
  1001. WARN_ON(shift == 24);
  1002. mask = 0xffff << shift;
  1003. val32 = (val32 & ~mask) | (in << shift);
  1004. rc = native_afu_cr_write32(afu, cr, aligned_off, val32);
  1005. return rc;
  1006. }
  1007. static int native_afu_cr_write8(struct cxl_afu *afu, int cr, u64 off, u8 in)
  1008. {
  1009. u64 aligned_off = off & ~0x3L;
  1010. u32 val32, mask, shift;
  1011. int rc;
  1012. rc = native_afu_cr_read32(afu, cr, aligned_off, &val32);
  1013. if (rc)
  1014. return rc;
  1015. shift = (off & 0x3) * 8;
  1016. mask = 0xff << shift;
  1017. val32 = (val32 & ~mask) | (in << shift);
  1018. rc = native_afu_cr_write32(afu, cr, aligned_off, val32);
  1019. return rc;
  1020. }
  1021. const struct cxl_backend_ops cxl_native_ops = {
  1022. .module = THIS_MODULE,
  1023. .adapter_reset = cxl_pci_reset,
  1024. .alloc_one_irq = cxl_pci_alloc_one_irq,
  1025. .release_one_irq = cxl_pci_release_one_irq,
  1026. .alloc_irq_ranges = cxl_pci_alloc_irq_ranges,
  1027. .release_irq_ranges = cxl_pci_release_irq_ranges,
  1028. .setup_irq = cxl_pci_setup_irq,
  1029. .handle_psl_slice_error = native_handle_psl_slice_error,
  1030. .psl_interrupt = NULL,
  1031. .ack_irq = native_ack_irq,
  1032. .irq_wait = native_irq_wait,
  1033. .attach_process = native_attach_process,
  1034. .detach_process = native_detach_process,
  1035. .update_ivtes = native_update_ivtes,
  1036. .support_attributes = native_support_attributes,
  1037. .link_ok = cxl_adapter_link_ok,
  1038. .release_afu = cxl_pci_release_afu,
  1039. .afu_read_err_buffer = cxl_pci_afu_read_err_buffer,
  1040. .afu_check_and_enable = native_afu_check_and_enable,
  1041. .afu_activate_mode = native_afu_activate_mode,
  1042. .afu_deactivate_mode = native_afu_deactivate_mode,
  1043. .afu_reset = native_afu_reset,
  1044. .afu_cr_read8 = native_afu_cr_read8,
  1045. .afu_cr_read16 = native_afu_cr_read16,
  1046. .afu_cr_read32 = native_afu_cr_read32,
  1047. .afu_cr_read64 = native_afu_cr_read64,
  1048. .afu_cr_write8 = native_afu_cr_write8,
  1049. .afu_cr_write16 = native_afu_cr_write16,
  1050. .afu_cr_write32 = native_afu_cr_write32,
  1051. .read_adapter_vpd = cxl_pci_read_adapter_vpd,
  1052. };