native.c 44 KB

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