native.c 44 KB

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