native.c 36 KB

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