native.c 44 KB

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