native.c 42 KB

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