native.c 42 KB

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