native.c 43 KB

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