native.c 36 KB

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