cxl.h 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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. #ifndef _CXL_H_
  10. #define _CXL_H_
  11. #include <linux/interrupt.h>
  12. #include <linux/semaphore.h>
  13. #include <linux/device.h>
  14. #include <linux/types.h>
  15. #include <linux/cdev.h>
  16. #include <linux/pid.h>
  17. #include <linux/io.h>
  18. #include <linux/pci.h>
  19. #include <linux/fs.h>
  20. #include <asm/cputable.h>
  21. #include <asm/mmu.h>
  22. #include <asm/reg.h>
  23. #include <misc/cxl-base.h>
  24. #include <misc/cxl.h>
  25. #include <uapi/misc/cxl.h>
  26. extern uint cxl_verbose;
  27. #define CXL_TIMEOUT 5
  28. /*
  29. * Bump version each time a user API change is made, whether it is
  30. * backwards compatible ot not.
  31. */
  32. #define CXL_API_VERSION 3
  33. #define CXL_API_VERSION_COMPATIBLE 1
  34. /*
  35. * Opaque types to avoid accidentally passing registers for the wrong MMIO
  36. *
  37. * At the end of the day, I'm not married to using typedef here, but it might
  38. * (and has!) help avoid bugs like mixing up CXL_PSL_CtxTime and
  39. * CXL_PSL_CtxTime_An, or calling cxl_p1n_write instead of cxl_p1_write.
  40. *
  41. * I'm quite happy if these are changed back to #defines before upstreaming, it
  42. * should be little more than a regexp search+replace operation in this file.
  43. */
  44. typedef struct {
  45. const int x;
  46. } cxl_p1_reg_t;
  47. typedef struct {
  48. const int x;
  49. } cxl_p1n_reg_t;
  50. typedef struct {
  51. const int x;
  52. } cxl_p2n_reg_t;
  53. #define cxl_reg_off(reg) \
  54. (reg.x)
  55. /* Memory maps. Ref CXL Appendix A */
  56. /* PSL Privilege 1 Memory Map */
  57. /* Configuration and Control area - CAIA 1&2 */
  58. static const cxl_p1_reg_t CXL_PSL_CtxTime = {0x0000};
  59. static const cxl_p1_reg_t CXL_PSL_ErrIVTE = {0x0008};
  60. static const cxl_p1_reg_t CXL_PSL_KEY1 = {0x0010};
  61. static const cxl_p1_reg_t CXL_PSL_KEY2 = {0x0018};
  62. static const cxl_p1_reg_t CXL_PSL_Control = {0x0020};
  63. /* Downloading */
  64. static const cxl_p1_reg_t CXL_PSL_DLCNTL = {0x0060};
  65. static const cxl_p1_reg_t CXL_PSL_DLADDR = {0x0068};
  66. /* PSL Lookaside Buffer Management Area - CAIA 1 */
  67. static const cxl_p1_reg_t CXL_PSL_LBISEL = {0x0080};
  68. static const cxl_p1_reg_t CXL_PSL_SLBIE = {0x0088};
  69. static const cxl_p1_reg_t CXL_PSL_SLBIA = {0x0090};
  70. static const cxl_p1_reg_t CXL_PSL_TLBIE = {0x00A0};
  71. static const cxl_p1_reg_t CXL_PSL_TLBIA = {0x00A8};
  72. static const cxl_p1_reg_t CXL_PSL_AFUSEL = {0x00B0};
  73. /* 0x00C0:7EFF Implementation dependent area */
  74. /* PSL registers - CAIA 1 */
  75. static const cxl_p1_reg_t CXL_PSL_FIR1 = {0x0100};
  76. static const cxl_p1_reg_t CXL_PSL_FIR2 = {0x0108};
  77. static const cxl_p1_reg_t CXL_PSL_Timebase = {0x0110};
  78. static const cxl_p1_reg_t CXL_PSL_VERSION = {0x0118};
  79. static const cxl_p1_reg_t CXL_PSL_RESLCKTO = {0x0128};
  80. static const cxl_p1_reg_t CXL_PSL_TB_CTLSTAT = {0x0140};
  81. static const cxl_p1_reg_t CXL_PSL_FIR_CNTL = {0x0148};
  82. static const cxl_p1_reg_t CXL_PSL_DSNDCTL = {0x0150};
  83. static const cxl_p1_reg_t CXL_PSL_SNWRALLOC = {0x0158};
  84. static const cxl_p1_reg_t CXL_PSL_TRACE = {0x0170};
  85. /* PSL registers - CAIA 2 */
  86. static const cxl_p1_reg_t CXL_PSL9_CONTROL = {0x0020};
  87. static const cxl_p1_reg_t CXL_XSL9_INV = {0x0110};
  88. static const cxl_p1_reg_t CXL_XSL9_DBG = {0x0130};
  89. static const cxl_p1_reg_t CXL_XSL9_DEF = {0x0140};
  90. static const cxl_p1_reg_t CXL_XSL9_DSNCTL = {0x0168};
  91. static const cxl_p1_reg_t CXL_PSL9_FIR1 = {0x0300};
  92. static const cxl_p1_reg_t CXL_PSL9_FIR_MASK = {0x0308};
  93. static const cxl_p1_reg_t CXL_PSL9_Timebase = {0x0310};
  94. static const cxl_p1_reg_t CXL_PSL9_DEBUG = {0x0320};
  95. static const cxl_p1_reg_t CXL_PSL9_FIR_CNTL = {0x0348};
  96. static const cxl_p1_reg_t CXL_PSL9_DSNDCTL = {0x0350};
  97. static const cxl_p1_reg_t CXL_PSL9_TB_CTLSTAT = {0x0340};
  98. static const cxl_p1_reg_t CXL_PSL9_TRACECFG = {0x0368};
  99. static const cxl_p1_reg_t CXL_PSL9_APCDEDALLOC = {0x0378};
  100. static const cxl_p1_reg_t CXL_PSL9_APCDEDTYPE = {0x0380};
  101. static const cxl_p1_reg_t CXL_PSL9_TNR_ADDR = {0x0388};
  102. static const cxl_p1_reg_t CXL_PSL9_CTCCFG = {0x0390};
  103. static const cxl_p1_reg_t CXL_PSL9_GP_CT = {0x0398};
  104. static const cxl_p1_reg_t CXL_XSL9_IERAT = {0x0588};
  105. static const cxl_p1_reg_t CXL_XSL9_ILPP = {0x0590};
  106. /* 0x7F00:7FFF Reserved PCIe MSI-X Pending Bit Array area */
  107. /* 0x8000:FFFF Reserved PCIe MSI-X Table Area */
  108. /* PSL Slice Privilege 1 Memory Map */
  109. /* Configuration Area - CAIA 1&2 */
  110. static const cxl_p1n_reg_t CXL_PSL_SR_An = {0x00};
  111. static const cxl_p1n_reg_t CXL_PSL_LPID_An = {0x08};
  112. static const cxl_p1n_reg_t CXL_PSL_AMBAR_An = {0x10};
  113. static const cxl_p1n_reg_t CXL_PSL_SPOffset_An = {0x18};
  114. static const cxl_p1n_reg_t CXL_PSL_ID_An = {0x20};
  115. static const cxl_p1n_reg_t CXL_PSL_SERR_An = {0x28};
  116. /* Memory Management and Lookaside Buffer Management - CAIA 1*/
  117. static const cxl_p1n_reg_t CXL_PSL_SDR_An = {0x30};
  118. /* Memory Management and Lookaside Buffer Management - CAIA 1&2 */
  119. static const cxl_p1n_reg_t CXL_PSL_AMOR_An = {0x38};
  120. /* Pointer Area - CAIA 1&2 */
  121. static const cxl_p1n_reg_t CXL_HAURP_An = {0x80};
  122. static const cxl_p1n_reg_t CXL_PSL_SPAP_An = {0x88};
  123. static const cxl_p1n_reg_t CXL_PSL_LLCMD_An = {0x90};
  124. /* Control Area - CAIA 1&2 */
  125. static const cxl_p1n_reg_t CXL_PSL_SCNTL_An = {0xA0};
  126. static const cxl_p1n_reg_t CXL_PSL_CtxTime_An = {0xA8};
  127. static const cxl_p1n_reg_t CXL_PSL_IVTE_Offset_An = {0xB0};
  128. static const cxl_p1n_reg_t CXL_PSL_IVTE_Limit_An = {0xB8};
  129. /* 0xC0:FF Implementation Dependent Area - CAIA 1&2 */
  130. static const cxl_p1n_reg_t CXL_PSL_FIR_SLICE_An = {0xC0};
  131. static const cxl_p1n_reg_t CXL_AFU_DEBUG_An = {0xC8};
  132. /* 0xC0:FF Implementation Dependent Area - CAIA 1 */
  133. static const cxl_p1n_reg_t CXL_PSL_APCALLOC_A = {0xD0};
  134. static const cxl_p1n_reg_t CXL_PSL_COALLOC_A = {0xD8};
  135. static const cxl_p1n_reg_t CXL_PSL_RXCTL_A = {0xE0};
  136. static const cxl_p1n_reg_t CXL_PSL_SLICE_TRACE = {0xE8};
  137. /* PSL Slice Privilege 2 Memory Map */
  138. /* Configuration and Control Area - CAIA 1&2 */
  139. static const cxl_p2n_reg_t CXL_PSL_PID_TID_An = {0x000};
  140. static const cxl_p2n_reg_t CXL_CSRP_An = {0x008};
  141. /* Configuration and Control Area - CAIA 1 */
  142. static const cxl_p2n_reg_t CXL_AURP0_An = {0x010};
  143. static const cxl_p2n_reg_t CXL_AURP1_An = {0x018};
  144. static const cxl_p2n_reg_t CXL_SSTP0_An = {0x020};
  145. static const cxl_p2n_reg_t CXL_SSTP1_An = {0x028};
  146. /* Configuration and Control Area - CAIA 1 */
  147. static const cxl_p2n_reg_t CXL_PSL_AMR_An = {0x030};
  148. /* Segment Lookaside Buffer Management - CAIA 1 */
  149. static const cxl_p2n_reg_t CXL_SLBIE_An = {0x040};
  150. static const cxl_p2n_reg_t CXL_SLBIA_An = {0x048};
  151. static const cxl_p2n_reg_t CXL_SLBI_Select_An = {0x050};
  152. /* Interrupt Registers - CAIA 1&2 */
  153. static const cxl_p2n_reg_t CXL_PSL_DSISR_An = {0x060};
  154. static const cxl_p2n_reg_t CXL_PSL_DAR_An = {0x068};
  155. static const cxl_p2n_reg_t CXL_PSL_DSR_An = {0x070};
  156. static const cxl_p2n_reg_t CXL_PSL_TFC_An = {0x078};
  157. static const cxl_p2n_reg_t CXL_PSL_PEHandle_An = {0x080};
  158. static const cxl_p2n_reg_t CXL_PSL_ErrStat_An = {0x088};
  159. /* AFU Registers - CAIA 1&2 */
  160. static const cxl_p2n_reg_t CXL_AFU_Cntl_An = {0x090};
  161. static const cxl_p2n_reg_t CXL_AFU_ERR_An = {0x098};
  162. /* Work Element Descriptor - CAIA 1&2 */
  163. static const cxl_p2n_reg_t CXL_PSL_WED_An = {0x0A0};
  164. /* 0x0C0:FFF Implementation Dependent Area */
  165. #define CXL_PSL_SPAP_Addr 0x0ffffffffffff000ULL
  166. #define CXL_PSL_SPAP_Size 0x0000000000000ff0ULL
  167. #define CXL_PSL_SPAP_Size_Shift 4
  168. #define CXL_PSL_SPAP_V 0x0000000000000001ULL
  169. /****** CXL_PSL_Control ****************************************************/
  170. #define CXL_PSL_Control_tb (0x1ull << (63-63))
  171. #define CXL_PSL_Control_Fr (0x1ull << (63-31))
  172. #define CXL_PSL_Control_Fs_MASK (0x3ull << (63-29))
  173. #define CXL_PSL_Control_Fs_Complete (0x3ull << (63-29))
  174. /****** CXL_PSL_DLCNTL *****************************************************/
  175. #define CXL_PSL_DLCNTL_D (0x1ull << (63-28))
  176. #define CXL_PSL_DLCNTL_C (0x1ull << (63-29))
  177. #define CXL_PSL_DLCNTL_E (0x1ull << (63-30))
  178. #define CXL_PSL_DLCNTL_S (0x1ull << (63-31))
  179. #define CXL_PSL_DLCNTL_CE (CXL_PSL_DLCNTL_C | CXL_PSL_DLCNTL_E)
  180. #define CXL_PSL_DLCNTL_DCES (CXL_PSL_DLCNTL_D | CXL_PSL_DLCNTL_CE | CXL_PSL_DLCNTL_S)
  181. /****** CXL_PSL_SR_An ******************************************************/
  182. #define CXL_PSL_SR_An_SF MSR_SF /* 64bit */
  183. #define CXL_PSL_SR_An_TA (1ull << (63-1)) /* Tags active, GA1: 0 */
  184. #define CXL_PSL_SR_An_HV MSR_HV /* Hypervisor, GA1: 0 */
  185. #define CXL_PSL_SR_An_XLAT_hpt (0ull << (63-6))/* Hashed page table (HPT) mode */
  186. #define CXL_PSL_SR_An_XLAT_roh (2ull << (63-6))/* Radix on HPT mode */
  187. #define CXL_PSL_SR_An_XLAT_ror (3ull << (63-6))/* Radix on Radix mode */
  188. #define CXL_PSL_SR_An_BOT (1ull << (63-10)) /* Use the in-memory segment table */
  189. #define CXL_PSL_SR_An_PR MSR_PR /* Problem state, GA1: 1 */
  190. #define CXL_PSL_SR_An_ISL (1ull << (63-53)) /* Ignore Segment Large Page */
  191. #define CXL_PSL_SR_An_TC (1ull << (63-54)) /* Page Table secondary hash */
  192. #define CXL_PSL_SR_An_US (1ull << (63-56)) /* User state, GA1: X */
  193. #define CXL_PSL_SR_An_SC (1ull << (63-58)) /* Segment Table secondary hash */
  194. #define CXL_PSL_SR_An_R MSR_DR /* Relocate, GA1: 1 */
  195. #define CXL_PSL_SR_An_MP (1ull << (63-62)) /* Master Process */
  196. #define CXL_PSL_SR_An_LE (1ull << (63-63)) /* Little Endian */
  197. /****** CXL_PSL_ID_An ****************************************************/
  198. #define CXL_PSL_ID_An_F (1ull << (63-31))
  199. #define CXL_PSL_ID_An_L (1ull << (63-30))
  200. /****** CXL_PSL_SERR_An ****************************************************/
  201. #define CXL_PSL_SERR_An_afuto (1ull << (63-0))
  202. #define CXL_PSL_SERR_An_afudis (1ull << (63-1))
  203. #define CXL_PSL_SERR_An_afuov (1ull << (63-2))
  204. #define CXL_PSL_SERR_An_badsrc (1ull << (63-3))
  205. #define CXL_PSL_SERR_An_badctx (1ull << (63-4))
  206. #define CXL_PSL_SERR_An_llcmdis (1ull << (63-5))
  207. #define CXL_PSL_SERR_An_llcmdto (1ull << (63-6))
  208. #define CXL_PSL_SERR_An_afupar (1ull << (63-7))
  209. #define CXL_PSL_SERR_An_afudup (1ull << (63-8))
  210. #define CXL_PSL_SERR_An_IRQS ( \
  211. CXL_PSL_SERR_An_afuto | CXL_PSL_SERR_An_afudis | CXL_PSL_SERR_An_afuov | \
  212. CXL_PSL_SERR_An_badsrc | CXL_PSL_SERR_An_badctx | CXL_PSL_SERR_An_llcmdis | \
  213. CXL_PSL_SERR_An_llcmdto | CXL_PSL_SERR_An_afupar | CXL_PSL_SERR_An_afudup)
  214. #define CXL_PSL_SERR_An_afuto_mask (1ull << (63-32))
  215. #define CXL_PSL_SERR_An_afudis_mask (1ull << (63-33))
  216. #define CXL_PSL_SERR_An_afuov_mask (1ull << (63-34))
  217. #define CXL_PSL_SERR_An_badsrc_mask (1ull << (63-35))
  218. #define CXL_PSL_SERR_An_badctx_mask (1ull << (63-36))
  219. #define CXL_PSL_SERR_An_llcmdis_mask (1ull << (63-37))
  220. #define CXL_PSL_SERR_An_llcmdto_mask (1ull << (63-38))
  221. #define CXL_PSL_SERR_An_afupar_mask (1ull << (63-39))
  222. #define CXL_PSL_SERR_An_afudup_mask (1ull << (63-40))
  223. #define CXL_PSL_SERR_An_IRQ_MASKS ( \
  224. CXL_PSL_SERR_An_afuto_mask | CXL_PSL_SERR_An_afudis_mask | CXL_PSL_SERR_An_afuov_mask | \
  225. CXL_PSL_SERR_An_badsrc_mask | CXL_PSL_SERR_An_badctx_mask | CXL_PSL_SERR_An_llcmdis_mask | \
  226. CXL_PSL_SERR_An_llcmdto_mask | CXL_PSL_SERR_An_afupar_mask | CXL_PSL_SERR_An_afudup_mask)
  227. #define CXL_PSL_SERR_An_AE (1ull << (63-30))
  228. /****** CXL_PSL_SCNTL_An ****************************************************/
  229. #define CXL_PSL_SCNTL_An_CR (0x1ull << (63-15))
  230. /* Programming Modes: */
  231. #define CXL_PSL_SCNTL_An_PM_MASK (0xffffull << (63-31))
  232. #define CXL_PSL_SCNTL_An_PM_Shared (0x0000ull << (63-31))
  233. #define CXL_PSL_SCNTL_An_PM_OS (0x0001ull << (63-31))
  234. #define CXL_PSL_SCNTL_An_PM_Process (0x0002ull << (63-31))
  235. #define CXL_PSL_SCNTL_An_PM_AFU (0x0004ull << (63-31))
  236. #define CXL_PSL_SCNTL_An_PM_AFU_PBT (0x0104ull << (63-31))
  237. /* Purge Status (ro) */
  238. #define CXL_PSL_SCNTL_An_Ps_MASK (0x3ull << (63-39))
  239. #define CXL_PSL_SCNTL_An_Ps_Pending (0x1ull << (63-39))
  240. #define CXL_PSL_SCNTL_An_Ps_Complete (0x3ull << (63-39))
  241. /* Purge */
  242. #define CXL_PSL_SCNTL_An_Pc (0x1ull << (63-48))
  243. /* Suspend Status (ro) */
  244. #define CXL_PSL_SCNTL_An_Ss_MASK (0x3ull << (63-55))
  245. #define CXL_PSL_SCNTL_An_Ss_Pending (0x1ull << (63-55))
  246. #define CXL_PSL_SCNTL_An_Ss_Complete (0x3ull << (63-55))
  247. /* Suspend Control */
  248. #define CXL_PSL_SCNTL_An_Sc (0x1ull << (63-63))
  249. /* AFU Slice Enable Status (ro) */
  250. #define CXL_AFU_Cntl_An_ES_MASK (0x7ull << (63-2))
  251. #define CXL_AFU_Cntl_An_ES_Disabled (0x0ull << (63-2))
  252. #define CXL_AFU_Cntl_An_ES_Enabled (0x4ull << (63-2))
  253. /* AFU Slice Enable */
  254. #define CXL_AFU_Cntl_An_E (0x1ull << (63-3))
  255. /* AFU Slice Reset status (ro) */
  256. #define CXL_AFU_Cntl_An_RS_MASK (0x3ull << (63-5))
  257. #define CXL_AFU_Cntl_An_RS_Pending (0x1ull << (63-5))
  258. #define CXL_AFU_Cntl_An_RS_Complete (0x2ull << (63-5))
  259. /* AFU Slice Reset */
  260. #define CXL_AFU_Cntl_An_RA (0x1ull << (63-7))
  261. /****** CXL_SSTP0/1_An ******************************************************/
  262. /* These top bits are for the segment that CONTAINS the segment table */
  263. #define CXL_SSTP0_An_B_SHIFT SLB_VSID_SSIZE_SHIFT
  264. #define CXL_SSTP0_An_KS (1ull << (63-2))
  265. #define CXL_SSTP0_An_KP (1ull << (63-3))
  266. #define CXL_SSTP0_An_N (1ull << (63-4))
  267. #define CXL_SSTP0_An_L (1ull << (63-5))
  268. #define CXL_SSTP0_An_C (1ull << (63-6))
  269. #define CXL_SSTP0_An_TA (1ull << (63-7))
  270. #define CXL_SSTP0_An_LP_SHIFT (63-9) /* 2 Bits */
  271. /* And finally, the virtual address & size of the segment table: */
  272. #define CXL_SSTP0_An_SegTableSize_SHIFT (63-31) /* 12 Bits */
  273. #define CXL_SSTP0_An_SegTableSize_MASK \
  274. (((1ull << 12) - 1) << CXL_SSTP0_An_SegTableSize_SHIFT)
  275. #define CXL_SSTP0_An_STVA_U_MASK ((1ull << (63-49))-1)
  276. #define CXL_SSTP1_An_STVA_L_MASK (~((1ull << (63-55))-1))
  277. #define CXL_SSTP1_An_V (1ull << (63-63))
  278. /****** CXL_PSL_SLBIE_[An] - CAIA 1 **************************************************/
  279. /* write: */
  280. #define CXL_SLBIE_C PPC_BIT(36) /* Class */
  281. #define CXL_SLBIE_SS PPC_BITMASK(37, 38) /* Segment Size */
  282. #define CXL_SLBIE_SS_SHIFT PPC_BITLSHIFT(38)
  283. #define CXL_SLBIE_TA PPC_BIT(38) /* Tags Active */
  284. /* read: */
  285. #define CXL_SLBIE_MAX PPC_BITMASK(24, 31)
  286. #define CXL_SLBIE_PENDING PPC_BITMASK(56, 63)
  287. /****** Common to all CXL_TLBIA/SLBIA_[An] - CAIA 1 **********************************/
  288. #define CXL_TLB_SLB_P (1ull) /* Pending (read) */
  289. /****** Common to all CXL_TLB/SLB_IA/IE_[An] registers - CAIA 1 **********************/
  290. #define CXL_TLB_SLB_IQ_ALL (0ull) /* Inv qualifier */
  291. #define CXL_TLB_SLB_IQ_LPID (1ull) /* Inv qualifier */
  292. #define CXL_TLB_SLB_IQ_LPIDPID (3ull) /* Inv qualifier */
  293. /****** CXL_PSL_AFUSEL ******************************************************/
  294. #define CXL_PSL_AFUSEL_A (1ull << (63-55)) /* Adapter wide invalidates affect all AFUs */
  295. /****** CXL_PSL_DSISR_An - CAIA 1 ****************************************************/
  296. #define CXL_PSL_DSISR_An_DS (1ull << (63-0)) /* Segment not found */
  297. #define CXL_PSL_DSISR_An_DM (1ull << (63-1)) /* PTE not found (See also: M) or protection fault */
  298. #define CXL_PSL_DSISR_An_ST (1ull << (63-2)) /* Segment Table PTE not found */
  299. #define CXL_PSL_DSISR_An_UR (1ull << (63-3)) /* AURP PTE not found */
  300. #define CXL_PSL_DSISR_TRANS (CXL_PSL_DSISR_An_DS | CXL_PSL_DSISR_An_DM | CXL_PSL_DSISR_An_ST | CXL_PSL_DSISR_An_UR)
  301. #define CXL_PSL_DSISR_An_PE (1ull << (63-4)) /* PSL Error (implementation specific) */
  302. #define CXL_PSL_DSISR_An_AE (1ull << (63-5)) /* AFU Error */
  303. #define CXL_PSL_DSISR_An_OC (1ull << (63-6)) /* OS Context Warning */
  304. #define CXL_PSL_DSISR_PENDING (CXL_PSL_DSISR_TRANS | CXL_PSL_DSISR_An_PE | CXL_PSL_DSISR_An_AE | CXL_PSL_DSISR_An_OC)
  305. /* NOTE: Bits 32:63 are undefined if DSISR[DS] = 1 */
  306. #define CXL_PSL_DSISR_An_M DSISR_NOHPTE /* PTE not found */
  307. #define CXL_PSL_DSISR_An_P DSISR_PROTFAULT /* Storage protection violation */
  308. #define CXL_PSL_DSISR_An_A (1ull << (63-37)) /* AFU lock access to write through or cache inhibited storage */
  309. #define CXL_PSL_DSISR_An_S DSISR_ISSTORE /* Access was afu_wr or afu_zero */
  310. #define CXL_PSL_DSISR_An_K DSISR_KEYFAULT /* Access not permitted by virtual page class key protection */
  311. /****** CXL_PSL_DSISR_An - CAIA 2 ****************************************************/
  312. #define CXL_PSL9_DSISR_An_TF (1ull << (63-3)) /* Translation fault */
  313. #define CXL_PSL9_DSISR_An_PE (1ull << (63-4)) /* PSL Error (implementation specific) */
  314. #define CXL_PSL9_DSISR_An_AE (1ull << (63-5)) /* AFU Error */
  315. #define CXL_PSL9_DSISR_An_OC (1ull << (63-6)) /* OS Context Warning */
  316. #define CXL_PSL9_DSISR_An_S (1ull << (63-38)) /* TF for a write operation */
  317. #define CXL_PSL9_DSISR_PENDING (CXL_PSL9_DSISR_An_TF | CXL_PSL9_DSISR_An_PE | CXL_PSL9_DSISR_An_AE | CXL_PSL9_DSISR_An_OC)
  318. /*
  319. * NOTE: Bits 56:63 (Checkout Response Status) are valid when DSISR_An[TF] = 1
  320. * Status (0:7) Encoding
  321. */
  322. #define CXL_PSL9_DSISR_An_CO_MASK 0x00000000000000ffULL
  323. #define CXL_PSL9_DSISR_An_SF 0x0000000000000080ULL /* Segment Fault 0b10000000 */
  324. #define CXL_PSL9_DSISR_An_PF_SLR 0x0000000000000088ULL /* PTE not found (Single Level Radix) 0b10001000 */
  325. #define CXL_PSL9_DSISR_An_PF_RGC 0x000000000000008CULL /* PTE not found (Radix Guest (child)) 0b10001100 */
  326. #define CXL_PSL9_DSISR_An_PF_RGP 0x0000000000000090ULL /* PTE not found (Radix Guest (parent)) 0b10010000 */
  327. #define CXL_PSL9_DSISR_An_PF_HRH 0x0000000000000094ULL /* PTE not found (HPT/Radix Host) 0b10010100 */
  328. #define CXL_PSL9_DSISR_An_PF_STEG 0x000000000000009CULL /* PTE not found (STEG VA) 0b10011100 */
  329. #define CXL_PSL9_DSISR_An_URTCH 0x00000000000000B4ULL /* Unsupported Radix Tree Configuration 0b10110100 */
  330. /****** CXL_PSL_TFC_An ******************************************************/
  331. #define CXL_PSL_TFC_An_A (1ull << (63-28)) /* Acknowledge non-translation fault */
  332. #define CXL_PSL_TFC_An_C (1ull << (63-29)) /* Continue (abort transaction) */
  333. #define CXL_PSL_TFC_An_AE (1ull << (63-30)) /* Restart PSL with address error */
  334. #define CXL_PSL_TFC_An_R (1ull << (63-31)) /* Restart PSL transaction */
  335. /****** CXL_PSL_DEBUG *****************************************************/
  336. #define CXL_PSL_DEBUG_CDC (1ull << (63-27)) /* Coherent Data cache support */
  337. /****** CXL_XSL9_IERAT_ERAT - CAIA 2 **********************************/
  338. #define CXL_XSL9_IERAT_MLPID (1ull << (63-0)) /* Match LPID */
  339. #define CXL_XSL9_IERAT_MPID (1ull << (63-1)) /* Match PID */
  340. #define CXL_XSL9_IERAT_PRS (1ull << (63-4)) /* PRS bit for Radix invalidations */
  341. #define CXL_XSL9_IERAT_INVR (1ull << (63-3)) /* Invalidate Radix */
  342. #define CXL_XSL9_IERAT_IALL (1ull << (63-8)) /* Invalidate All */
  343. #define CXL_XSL9_IERAT_IINPROG (1ull << (63-63)) /* Invalidate in progress */
  344. /* cxl_process_element->software_status */
  345. #define CXL_PE_SOFTWARE_STATE_V (1ul << (31 - 0)) /* Valid */
  346. #define CXL_PE_SOFTWARE_STATE_C (1ul << (31 - 29)) /* Complete */
  347. #define CXL_PE_SOFTWARE_STATE_S (1ul << (31 - 30)) /* Suspend */
  348. #define CXL_PE_SOFTWARE_STATE_T (1ul << (31 - 31)) /* Terminate */
  349. /****** CXL_PSL_RXCTL_An (Implementation Specific) **************************
  350. * Controls AFU Hang Pulse, which sets the timeout for the AFU to respond to
  351. * the PSL for any response (except MMIO). Timeouts will occur between 1x to 2x
  352. * of the hang pulse frequency.
  353. */
  354. #define CXL_PSL_RXCTL_AFUHP_4S 0x7000000000000000ULL
  355. /* SPA->sw_command_status */
  356. #define CXL_SPA_SW_CMD_MASK 0xffff000000000000ULL
  357. #define CXL_SPA_SW_CMD_TERMINATE 0x0001000000000000ULL
  358. #define CXL_SPA_SW_CMD_REMOVE 0x0002000000000000ULL
  359. #define CXL_SPA_SW_CMD_SUSPEND 0x0003000000000000ULL
  360. #define CXL_SPA_SW_CMD_RESUME 0x0004000000000000ULL
  361. #define CXL_SPA_SW_CMD_ADD 0x0005000000000000ULL
  362. #define CXL_SPA_SW_CMD_UPDATE 0x0006000000000000ULL
  363. #define CXL_SPA_SW_STATE_MASK 0x0000ffff00000000ULL
  364. #define CXL_SPA_SW_STATE_TERMINATED 0x0000000100000000ULL
  365. #define CXL_SPA_SW_STATE_REMOVED 0x0000000200000000ULL
  366. #define CXL_SPA_SW_STATE_SUSPENDED 0x0000000300000000ULL
  367. #define CXL_SPA_SW_STATE_RESUMED 0x0000000400000000ULL
  368. #define CXL_SPA_SW_STATE_ADDED 0x0000000500000000ULL
  369. #define CXL_SPA_SW_STATE_UPDATED 0x0000000600000000ULL
  370. #define CXL_SPA_SW_PSL_ID_MASK 0x00000000ffff0000ULL
  371. #define CXL_SPA_SW_LINK_MASK 0x000000000000ffffULL
  372. #define CXL_MAX_SLICES 4
  373. #define MAX_AFU_MMIO_REGS 3
  374. #define CXL_MODE_TIME_SLICED 0x4
  375. #define CXL_SUPPORTED_MODES (CXL_MODE_DEDICATED | CXL_MODE_DIRECTED)
  376. #define CXL_DEV_MINORS 13 /* 1 control + 4 AFUs * 3 (dedicated/master/shared) */
  377. #define CXL_CARD_MINOR(adapter) (adapter->adapter_num * CXL_DEV_MINORS)
  378. #define CXL_DEVT_ADAPTER(dev) (MINOR(dev) / CXL_DEV_MINORS)
  379. #define CXL_PSL9_TRACEID_MAX 0xAU
  380. #define CXL_PSL9_TRACESTATE_FIN 0x3U
  381. enum cxl_context_status {
  382. CLOSED,
  383. OPENED,
  384. STARTED
  385. };
  386. enum prefault_modes {
  387. CXL_PREFAULT_NONE,
  388. CXL_PREFAULT_WED,
  389. CXL_PREFAULT_ALL,
  390. };
  391. enum cxl_attrs {
  392. CXL_ADAPTER_ATTRS,
  393. CXL_AFU_MASTER_ATTRS,
  394. CXL_AFU_ATTRS,
  395. };
  396. struct cxl_sste {
  397. __be64 esid_data;
  398. __be64 vsid_data;
  399. };
  400. #define to_cxl_adapter(d) container_of(d, struct cxl, dev)
  401. #define to_cxl_afu(d) container_of(d, struct cxl_afu, dev)
  402. struct cxl_afu_native {
  403. void __iomem *p1n_mmio;
  404. void __iomem *afu_desc_mmio;
  405. irq_hw_number_t psl_hwirq;
  406. unsigned int psl_virq;
  407. struct mutex spa_mutex;
  408. /*
  409. * Only the first part of the SPA is used for the process element
  410. * linked list. The only other part that software needs to worry about
  411. * is sw_command_status, which we store a separate pointer to.
  412. * Everything else in the SPA is only used by hardware
  413. */
  414. struct cxl_process_element *spa;
  415. __be64 *sw_command_status;
  416. unsigned int spa_size;
  417. int spa_order;
  418. int spa_max_procs;
  419. u64 pp_offset;
  420. };
  421. struct cxl_afu_guest {
  422. struct cxl_afu *parent;
  423. u64 handle;
  424. phys_addr_t p2n_phys;
  425. u64 p2n_size;
  426. int max_ints;
  427. bool handle_err;
  428. struct delayed_work work_err;
  429. int previous_state;
  430. };
  431. struct cxl_afu {
  432. struct cxl_afu_native *native;
  433. struct cxl_afu_guest *guest;
  434. irq_hw_number_t serr_hwirq;
  435. unsigned int serr_virq;
  436. char *psl_irq_name;
  437. char *err_irq_name;
  438. void __iomem *p2n_mmio;
  439. phys_addr_t psn_phys;
  440. u64 pp_size;
  441. struct cxl *adapter;
  442. struct device dev;
  443. struct cdev afu_cdev_s, afu_cdev_m, afu_cdev_d;
  444. struct device *chardev_s, *chardev_m, *chardev_d;
  445. struct idr contexts_idr;
  446. struct dentry *debugfs;
  447. struct mutex contexts_lock;
  448. spinlock_t afu_cntl_lock;
  449. /* -1: AFU deconfigured/locked, >= 0: number of readers */
  450. atomic_t configured_state;
  451. /* AFU error buffer fields and bin attribute for sysfs */
  452. u64 eb_len, eb_offset;
  453. struct bin_attribute attr_eb;
  454. /* pointer to the vphb */
  455. struct pci_controller *phb;
  456. int pp_irqs;
  457. int irqs_max;
  458. int num_procs;
  459. int max_procs_virtualised;
  460. int slice;
  461. int modes_supported;
  462. int current_mode;
  463. int crs_num;
  464. u64 crs_len;
  465. u64 crs_offset;
  466. struct list_head crs;
  467. enum prefault_modes prefault_mode;
  468. bool psa;
  469. bool pp_psa;
  470. bool enabled;
  471. };
  472. struct cxl_irq_name {
  473. struct list_head list;
  474. char *name;
  475. };
  476. struct irq_avail {
  477. irq_hw_number_t offset;
  478. irq_hw_number_t range;
  479. unsigned long *bitmap;
  480. };
  481. /*
  482. * This is a cxl context. If the PSL is in dedicated mode, there will be one
  483. * of these per AFU. If in AFU directed there can be lots of these.
  484. */
  485. struct cxl_context {
  486. struct cxl_afu *afu;
  487. /* Problem state MMIO */
  488. phys_addr_t psn_phys;
  489. u64 psn_size;
  490. /* Used to unmap any mmaps when force detaching */
  491. struct address_space *mapping;
  492. struct mutex mapping_lock;
  493. struct page *ff_page;
  494. bool mmio_err_ff;
  495. bool kernelapi;
  496. spinlock_t sste_lock; /* Protects segment table entries */
  497. struct cxl_sste *sstp;
  498. u64 sstp0, sstp1;
  499. unsigned int sst_size, sst_lru;
  500. wait_queue_head_t wq;
  501. /* use mm context associated with this pid for ds faults */
  502. struct pid *pid;
  503. spinlock_t lock; /* Protects pending_irq_mask, pending_fault and fault_addr */
  504. /* Only used in PR mode */
  505. u64 process_token;
  506. /* driver private data */
  507. void *priv;
  508. unsigned long *irq_bitmap; /* Accessed from IRQ context */
  509. struct cxl_irq_ranges irqs;
  510. struct list_head irq_names;
  511. u64 fault_addr;
  512. u64 fault_dsisr;
  513. u64 afu_err;
  514. /*
  515. * This status and it's lock pretects start and detach context
  516. * from racing. It also prevents detach from racing with
  517. * itself
  518. */
  519. enum cxl_context_status status;
  520. struct mutex status_mutex;
  521. /* XXX: Is it possible to need multiple work items at once? */
  522. struct work_struct fault_work;
  523. u64 dsisr;
  524. u64 dar;
  525. struct cxl_process_element *elem;
  526. /*
  527. * pe is the process element handle, assigned by this driver when the
  528. * context is initialized.
  529. *
  530. * external_pe is the PE shown outside of cxl.
  531. * On bare-metal, pe=external_pe, because we decide what the handle is.
  532. * In a guest, we only find out about the pe used by pHyp when the
  533. * context is attached, and that's the value we want to report outside
  534. * of cxl.
  535. */
  536. int pe;
  537. int external_pe;
  538. u32 irq_count;
  539. bool pe_inserted;
  540. bool master;
  541. bool kernel;
  542. bool pending_irq;
  543. bool pending_fault;
  544. bool pending_afu_err;
  545. /* Used by AFU drivers for driver specific event delivery */
  546. struct cxl_afu_driver_ops *afu_driver_ops;
  547. atomic_t afu_driver_events;
  548. struct rcu_head rcu;
  549. struct mm_struct *mm;
  550. u16 tidr;
  551. bool assign_tidr;
  552. };
  553. struct cxl_irq_info;
  554. struct cxl_service_layer_ops {
  555. int (*adapter_regs_init)(struct cxl *adapter, struct pci_dev *dev);
  556. int (*invalidate_all)(struct cxl *adapter);
  557. int (*afu_regs_init)(struct cxl_afu *afu);
  558. int (*sanitise_afu_regs)(struct cxl_afu *afu);
  559. int (*register_serr_irq)(struct cxl_afu *afu);
  560. void (*release_serr_irq)(struct cxl_afu *afu);
  561. irqreturn_t (*handle_interrupt)(int irq, struct cxl_context *ctx, struct cxl_irq_info *irq_info);
  562. irqreturn_t (*fail_irq)(struct cxl_afu *afu, struct cxl_irq_info *irq_info);
  563. int (*activate_dedicated_process)(struct cxl_afu *afu);
  564. int (*attach_afu_directed)(struct cxl_context *ctx, u64 wed, u64 amr);
  565. int (*attach_dedicated_process)(struct cxl_context *ctx, u64 wed, u64 amr);
  566. void (*update_dedicated_ivtes)(struct cxl_context *ctx);
  567. void (*debugfs_add_adapter_regs)(struct cxl *adapter, struct dentry *dir);
  568. void (*debugfs_add_afu_regs)(struct cxl_afu *afu, struct dentry *dir);
  569. void (*psl_irq_dump_registers)(struct cxl_context *ctx);
  570. void (*err_irq_dump_registers)(struct cxl *adapter);
  571. void (*debugfs_stop_trace)(struct cxl *adapter);
  572. void (*write_timebase_ctrl)(struct cxl *adapter);
  573. u64 (*timebase_read)(struct cxl *adapter);
  574. int capi_mode;
  575. bool needs_reset_before_disable;
  576. };
  577. struct cxl_native {
  578. u64 afu_desc_off;
  579. u64 afu_desc_size;
  580. void __iomem *p1_mmio;
  581. void __iomem *p2_mmio;
  582. irq_hw_number_t err_hwirq;
  583. unsigned int err_virq;
  584. u64 ps_off;
  585. bool no_data_cache; /* set if no data cache on the card */
  586. const struct cxl_service_layer_ops *sl_ops;
  587. };
  588. struct cxl_guest {
  589. struct platform_device *pdev;
  590. int irq_nranges;
  591. struct cdev cdev;
  592. irq_hw_number_t irq_base_offset;
  593. struct irq_avail *irq_avail;
  594. spinlock_t irq_alloc_lock;
  595. u64 handle;
  596. char *status;
  597. u16 vendor;
  598. u16 device;
  599. u16 subsystem_vendor;
  600. u16 subsystem;
  601. };
  602. struct cxl {
  603. struct cxl_native *native;
  604. struct cxl_guest *guest;
  605. spinlock_t afu_list_lock;
  606. struct cxl_afu *afu[CXL_MAX_SLICES];
  607. struct device dev;
  608. struct dentry *trace;
  609. struct dentry *psl_err_chk;
  610. struct dentry *debugfs;
  611. char *irq_name;
  612. struct bin_attribute cxl_attr;
  613. int adapter_num;
  614. int user_irqs;
  615. u64 ps_size;
  616. u16 psl_rev;
  617. u16 base_image;
  618. u8 vsec_status;
  619. u8 caia_major;
  620. u8 caia_minor;
  621. u8 slices;
  622. bool user_image_loaded;
  623. bool perst_loads_image;
  624. bool perst_select_user;
  625. bool perst_same_image;
  626. bool psl_timebase_synced;
  627. bool tunneled_ops_supported;
  628. /*
  629. * number of contexts mapped on to this card. Possible values are:
  630. * >0: Number of contexts mapped and new one can be mapped.
  631. * 0: No active contexts and new ones can be mapped.
  632. * -1: No contexts mapped and new ones cannot be mapped.
  633. */
  634. atomic_t contexts_num;
  635. };
  636. int cxl_pci_alloc_one_irq(struct cxl *adapter);
  637. void cxl_pci_release_one_irq(struct cxl *adapter, int hwirq);
  638. int cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter, unsigned int num);
  639. void cxl_pci_release_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter);
  640. int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq, unsigned int virq);
  641. int cxl_update_image_control(struct cxl *adapter);
  642. int cxl_pci_reset(struct cxl *adapter);
  643. void cxl_pci_release_afu(struct device *dev);
  644. ssize_t cxl_pci_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len);
  645. /* common == phyp + powernv - CAIA 1&2 */
  646. struct cxl_process_element_common {
  647. __be32 tid;
  648. __be32 pid;
  649. __be64 csrp;
  650. union {
  651. struct {
  652. __be64 aurp0;
  653. __be64 aurp1;
  654. __be64 sstp0;
  655. __be64 sstp1;
  656. } psl8; /* CAIA 1 */
  657. struct {
  658. u8 reserved2[8];
  659. u8 reserved3[8];
  660. u8 reserved4[8];
  661. u8 reserved5[8];
  662. } psl9; /* CAIA 2 */
  663. } u;
  664. __be64 amr;
  665. u8 reserved6[4];
  666. __be64 wed;
  667. } __packed;
  668. /* just powernv - CAIA 1&2 */
  669. struct cxl_process_element {
  670. __be64 sr;
  671. __be64 SPOffset;
  672. union {
  673. __be64 sdr; /* CAIA 1 */
  674. u8 reserved1[8]; /* CAIA 2 */
  675. } u;
  676. __be64 haurp;
  677. __be32 ctxtime;
  678. __be16 ivte_offsets[4];
  679. __be16 ivte_ranges[4];
  680. __be32 lpid;
  681. struct cxl_process_element_common common;
  682. __be32 software_state;
  683. } __packed;
  684. static inline bool cxl_adapter_link_ok(struct cxl *cxl, struct cxl_afu *afu)
  685. {
  686. struct pci_dev *pdev;
  687. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  688. pdev = to_pci_dev(cxl->dev.parent);
  689. return !pci_channel_offline(pdev);
  690. }
  691. return true;
  692. }
  693. static inline void __iomem *_cxl_p1_addr(struct cxl *cxl, cxl_p1_reg_t reg)
  694. {
  695. WARN_ON(!cpu_has_feature(CPU_FTR_HVMODE));
  696. return cxl->native->p1_mmio + cxl_reg_off(reg);
  697. }
  698. static inline void cxl_p1_write(struct cxl *cxl, cxl_p1_reg_t reg, u64 val)
  699. {
  700. if (likely(cxl_adapter_link_ok(cxl, NULL)))
  701. out_be64(_cxl_p1_addr(cxl, reg), val);
  702. }
  703. static inline u64 cxl_p1_read(struct cxl *cxl, cxl_p1_reg_t reg)
  704. {
  705. if (likely(cxl_adapter_link_ok(cxl, NULL)))
  706. return in_be64(_cxl_p1_addr(cxl, reg));
  707. else
  708. return ~0ULL;
  709. }
  710. static inline void __iomem *_cxl_p1n_addr(struct cxl_afu *afu, cxl_p1n_reg_t reg)
  711. {
  712. WARN_ON(!cpu_has_feature(CPU_FTR_HVMODE));
  713. return afu->native->p1n_mmio + cxl_reg_off(reg);
  714. }
  715. static inline void cxl_p1n_write(struct cxl_afu *afu, cxl_p1n_reg_t reg, u64 val)
  716. {
  717. if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
  718. out_be64(_cxl_p1n_addr(afu, reg), val);
  719. }
  720. static inline u64 cxl_p1n_read(struct cxl_afu *afu, cxl_p1n_reg_t reg)
  721. {
  722. if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
  723. return in_be64(_cxl_p1n_addr(afu, reg));
  724. else
  725. return ~0ULL;
  726. }
  727. static inline void __iomem *_cxl_p2n_addr(struct cxl_afu *afu, cxl_p2n_reg_t reg)
  728. {
  729. return afu->p2n_mmio + cxl_reg_off(reg);
  730. }
  731. static inline void cxl_p2n_write(struct cxl_afu *afu, cxl_p2n_reg_t reg, u64 val)
  732. {
  733. if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
  734. out_be64(_cxl_p2n_addr(afu, reg), val);
  735. }
  736. static inline u64 cxl_p2n_read(struct cxl_afu *afu, cxl_p2n_reg_t reg)
  737. {
  738. if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
  739. return in_be64(_cxl_p2n_addr(afu, reg));
  740. else
  741. return ~0ULL;
  742. }
  743. static inline bool cxl_is_power8(void)
  744. {
  745. if ((pvr_version_is(PVR_POWER8E)) ||
  746. (pvr_version_is(PVR_POWER8NVL)) ||
  747. (pvr_version_is(PVR_POWER8)))
  748. return true;
  749. return false;
  750. }
  751. static inline bool cxl_is_power9(void)
  752. {
  753. if (pvr_version_is(PVR_POWER9))
  754. return true;
  755. return false;
  756. }
  757. ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
  758. loff_t off, size_t count);
  759. struct cxl_calls {
  760. void (*cxl_slbia)(struct mm_struct *mm);
  761. struct module *owner;
  762. };
  763. int register_cxl_calls(struct cxl_calls *calls);
  764. void unregister_cxl_calls(struct cxl_calls *calls);
  765. int cxl_update_properties(struct device_node *dn, struct property *new_prop);
  766. void cxl_remove_adapter_nr(struct cxl *adapter);
  767. void cxl_release_spa(struct cxl_afu *afu);
  768. dev_t cxl_get_dev(void);
  769. int cxl_file_init(void);
  770. void cxl_file_exit(void);
  771. int cxl_register_adapter(struct cxl *adapter);
  772. int cxl_register_afu(struct cxl_afu *afu);
  773. int cxl_chardev_d_afu_add(struct cxl_afu *afu);
  774. int cxl_chardev_m_afu_add(struct cxl_afu *afu);
  775. int cxl_chardev_s_afu_add(struct cxl_afu *afu);
  776. void cxl_chardev_afu_remove(struct cxl_afu *afu);
  777. void cxl_context_detach_all(struct cxl_afu *afu);
  778. void cxl_context_free(struct cxl_context *ctx);
  779. void cxl_context_detach(struct cxl_context *ctx);
  780. int cxl_sysfs_adapter_add(struct cxl *adapter);
  781. void cxl_sysfs_adapter_remove(struct cxl *adapter);
  782. int cxl_sysfs_afu_add(struct cxl_afu *afu);
  783. void cxl_sysfs_afu_remove(struct cxl_afu *afu);
  784. int cxl_sysfs_afu_m_add(struct cxl_afu *afu);
  785. void cxl_sysfs_afu_m_remove(struct cxl_afu *afu);
  786. struct cxl *cxl_alloc_adapter(void);
  787. struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice);
  788. int cxl_afu_select_best_mode(struct cxl_afu *afu);
  789. int cxl_native_register_psl_irq(struct cxl_afu *afu);
  790. void cxl_native_release_psl_irq(struct cxl_afu *afu);
  791. int cxl_native_register_psl_err_irq(struct cxl *adapter);
  792. void cxl_native_release_psl_err_irq(struct cxl *adapter);
  793. int cxl_native_register_serr_irq(struct cxl_afu *afu);
  794. void cxl_native_release_serr_irq(struct cxl_afu *afu);
  795. int afu_register_irqs(struct cxl_context *ctx, u32 count);
  796. void afu_release_irqs(struct cxl_context *ctx, void *cookie);
  797. void afu_irq_name_free(struct cxl_context *ctx);
  798. int cxl_attach_afu_directed_psl9(struct cxl_context *ctx, u64 wed, u64 amr);
  799. int cxl_attach_afu_directed_psl8(struct cxl_context *ctx, u64 wed, u64 amr);
  800. int cxl_activate_dedicated_process_psl9(struct cxl_afu *afu);
  801. int cxl_activate_dedicated_process_psl8(struct cxl_afu *afu);
  802. int cxl_attach_dedicated_process_psl9(struct cxl_context *ctx, u64 wed, u64 amr);
  803. int cxl_attach_dedicated_process_psl8(struct cxl_context *ctx, u64 wed, u64 amr);
  804. void cxl_update_dedicated_ivtes_psl9(struct cxl_context *ctx);
  805. void cxl_update_dedicated_ivtes_psl8(struct cxl_context *ctx);
  806. #ifdef CONFIG_DEBUG_FS
  807. int cxl_debugfs_init(void);
  808. void cxl_debugfs_exit(void);
  809. int cxl_debugfs_adapter_add(struct cxl *adapter);
  810. void cxl_debugfs_adapter_remove(struct cxl *adapter);
  811. int cxl_debugfs_afu_add(struct cxl_afu *afu);
  812. void cxl_debugfs_afu_remove(struct cxl_afu *afu);
  813. void cxl_debugfs_add_adapter_regs_psl9(struct cxl *adapter, struct dentry *dir);
  814. void cxl_debugfs_add_adapter_regs_psl8(struct cxl *adapter, struct dentry *dir);
  815. void cxl_debugfs_add_afu_regs_psl9(struct cxl_afu *afu, struct dentry *dir);
  816. void cxl_debugfs_add_afu_regs_psl8(struct cxl_afu *afu, struct dentry *dir);
  817. #else /* CONFIG_DEBUG_FS */
  818. static inline int __init cxl_debugfs_init(void)
  819. {
  820. return 0;
  821. }
  822. static inline void cxl_debugfs_exit(void)
  823. {
  824. }
  825. static inline int cxl_debugfs_adapter_add(struct cxl *adapter)
  826. {
  827. return 0;
  828. }
  829. static inline void cxl_debugfs_adapter_remove(struct cxl *adapter)
  830. {
  831. }
  832. static inline int cxl_debugfs_afu_add(struct cxl_afu *afu)
  833. {
  834. return 0;
  835. }
  836. static inline void cxl_debugfs_afu_remove(struct cxl_afu *afu)
  837. {
  838. }
  839. static inline void cxl_debugfs_add_adapter_regs_psl9(struct cxl *adapter,
  840. struct dentry *dir)
  841. {
  842. }
  843. static inline void cxl_debugfs_add_adapter_regs_psl8(struct cxl *adapter,
  844. struct dentry *dir)
  845. {
  846. }
  847. static inline void cxl_debugfs_add_afu_regs_psl9(struct cxl_afu *afu, struct dentry *dir)
  848. {
  849. }
  850. static inline void cxl_debugfs_add_afu_regs_psl8(struct cxl_afu *afu, struct dentry *dir)
  851. {
  852. }
  853. #endif /* CONFIG_DEBUG_FS */
  854. void cxl_handle_fault(struct work_struct *work);
  855. void cxl_prefault(struct cxl_context *ctx, u64 wed);
  856. int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar);
  857. struct cxl *get_cxl_adapter(int num);
  858. int cxl_alloc_sst(struct cxl_context *ctx);
  859. void cxl_dump_debug_buffer(void *addr, size_t size);
  860. void init_cxl_native(void);
  861. struct cxl_context *cxl_context_alloc(void);
  862. int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master);
  863. void cxl_context_set_mapping(struct cxl_context *ctx,
  864. struct address_space *mapping);
  865. void cxl_context_free(struct cxl_context *ctx);
  866. int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma);
  867. unsigned int cxl_map_irq(struct cxl *adapter, irq_hw_number_t hwirq,
  868. irq_handler_t handler, void *cookie, const char *name);
  869. void cxl_unmap_irq(unsigned int virq, void *cookie);
  870. int __detach_context(struct cxl_context *ctx);
  871. /*
  872. * This must match the layout of the H_COLLECT_CA_INT_INFO retbuf defined
  873. * in PAPR.
  874. * Field pid_tid is now 'reserved' because it's no more used on bare-metal.
  875. * On a guest environment, PSL_PID_An is located on the upper 32 bits and
  876. * PSL_TID_An register in the lower 32 bits.
  877. */
  878. struct cxl_irq_info {
  879. u64 dsisr;
  880. u64 dar;
  881. u64 dsr;
  882. u64 reserved;
  883. u64 afu_err;
  884. u64 errstat;
  885. u64 proc_handle;
  886. u64 padding[2]; /* to match the expected retbuf size for plpar_hcall9 */
  887. };
  888. void cxl_assign_psn_space(struct cxl_context *ctx);
  889. int cxl_invalidate_all_psl9(struct cxl *adapter);
  890. int cxl_invalidate_all_psl8(struct cxl *adapter);
  891. irqreturn_t cxl_irq_psl9(int irq, struct cxl_context *ctx, struct cxl_irq_info *irq_info);
  892. irqreturn_t cxl_irq_psl8(int irq, struct cxl_context *ctx, struct cxl_irq_info *irq_info);
  893. irqreturn_t cxl_fail_irq_psl(struct cxl_afu *afu, struct cxl_irq_info *irq_info);
  894. int cxl_register_one_irq(struct cxl *adapter, irq_handler_t handler,
  895. void *cookie, irq_hw_number_t *dest_hwirq,
  896. unsigned int *dest_virq, const char *name);
  897. int cxl_check_error(struct cxl_afu *afu);
  898. int cxl_afu_slbia(struct cxl_afu *afu);
  899. int cxl_data_cache_flush(struct cxl *adapter);
  900. int cxl_afu_disable(struct cxl_afu *afu);
  901. int cxl_psl_purge(struct cxl_afu *afu);
  902. int cxl_calc_capp_routing(struct pci_dev *dev, u64 *chipid,
  903. u32 *phb_index, u64 *capp_unit_id);
  904. int cxl_slot_is_switched(struct pci_dev *dev);
  905. int cxl_get_xsl9_dsnctl(struct pci_dev *dev, u64 capp_unit_id, u64 *reg);
  906. u64 cxl_calculate_sr(bool master, bool kernel, bool real_mode, bool p9);
  907. void cxl_native_irq_dump_regs_psl9(struct cxl_context *ctx);
  908. void cxl_native_irq_dump_regs_psl8(struct cxl_context *ctx);
  909. void cxl_native_err_irq_dump_regs_psl8(struct cxl *adapter);
  910. void cxl_native_err_irq_dump_regs_psl9(struct cxl *adapter);
  911. int cxl_pci_vphb_add(struct cxl_afu *afu);
  912. void cxl_pci_vphb_remove(struct cxl_afu *afu);
  913. void cxl_release_mapping(struct cxl_context *ctx);
  914. extern struct pci_driver cxl_pci_driver;
  915. extern struct platform_driver cxl_of_driver;
  916. int afu_allocate_irqs(struct cxl_context *ctx, u32 count);
  917. int afu_open(struct inode *inode, struct file *file);
  918. int afu_release(struct inode *inode, struct file *file);
  919. long afu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  920. int afu_mmap(struct file *file, struct vm_area_struct *vm);
  921. __poll_t afu_poll(struct file *file, struct poll_table_struct *poll);
  922. ssize_t afu_read(struct file *file, char __user *buf, size_t count, loff_t *off);
  923. extern const struct file_operations afu_fops;
  924. struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_device *dev);
  925. void cxl_guest_remove_adapter(struct cxl *adapter);
  926. int cxl_of_read_adapter_handle(struct cxl *adapter, struct device_node *np);
  927. int cxl_of_read_adapter_properties(struct cxl *adapter, struct device_node *np);
  928. ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len);
  929. ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len);
  930. int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np);
  931. void cxl_guest_remove_afu(struct cxl_afu *afu);
  932. int cxl_of_read_afu_handle(struct cxl_afu *afu, struct device_node *afu_np);
  933. int cxl_of_read_afu_properties(struct cxl_afu *afu, struct device_node *afu_np);
  934. int cxl_guest_add_chardev(struct cxl *adapter);
  935. void cxl_guest_remove_chardev(struct cxl *adapter);
  936. void cxl_guest_reload_module(struct cxl *adapter);
  937. int cxl_of_probe(struct platform_device *pdev);
  938. struct cxl_backend_ops {
  939. struct module *module;
  940. int (*adapter_reset)(struct cxl *adapter);
  941. int (*alloc_one_irq)(struct cxl *adapter);
  942. void (*release_one_irq)(struct cxl *adapter, int hwirq);
  943. int (*alloc_irq_ranges)(struct cxl_irq_ranges *irqs,
  944. struct cxl *adapter, unsigned int num);
  945. void (*release_irq_ranges)(struct cxl_irq_ranges *irqs,
  946. struct cxl *adapter);
  947. int (*setup_irq)(struct cxl *adapter, unsigned int hwirq,
  948. unsigned int virq);
  949. irqreturn_t (*handle_psl_slice_error)(struct cxl_context *ctx,
  950. u64 dsisr, u64 errstat);
  951. irqreturn_t (*psl_interrupt)(int irq, void *data);
  952. int (*ack_irq)(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask);
  953. void (*irq_wait)(struct cxl_context *ctx);
  954. int (*attach_process)(struct cxl_context *ctx, bool kernel,
  955. u64 wed, u64 amr);
  956. int (*detach_process)(struct cxl_context *ctx);
  957. void (*update_ivtes)(struct cxl_context *ctx);
  958. bool (*support_attributes)(const char *attr_name, enum cxl_attrs type);
  959. bool (*link_ok)(struct cxl *cxl, struct cxl_afu *afu);
  960. void (*release_afu)(struct device *dev);
  961. ssize_t (*afu_read_err_buffer)(struct cxl_afu *afu, char *buf,
  962. loff_t off, size_t count);
  963. int (*afu_check_and_enable)(struct cxl_afu *afu);
  964. int (*afu_activate_mode)(struct cxl_afu *afu, int mode);
  965. int (*afu_deactivate_mode)(struct cxl_afu *afu, int mode);
  966. int (*afu_reset)(struct cxl_afu *afu);
  967. int (*afu_cr_read8)(struct cxl_afu *afu, int cr_idx, u64 offset, u8 *val);
  968. int (*afu_cr_read16)(struct cxl_afu *afu, int cr_idx, u64 offset, u16 *val);
  969. int (*afu_cr_read32)(struct cxl_afu *afu, int cr_idx, u64 offset, u32 *val);
  970. int (*afu_cr_read64)(struct cxl_afu *afu, int cr_idx, u64 offset, u64 *val);
  971. int (*afu_cr_write8)(struct cxl_afu *afu, int cr_idx, u64 offset, u8 val);
  972. int (*afu_cr_write16)(struct cxl_afu *afu, int cr_idx, u64 offset, u16 val);
  973. int (*afu_cr_write32)(struct cxl_afu *afu, int cr_idx, u64 offset, u32 val);
  974. ssize_t (*read_adapter_vpd)(struct cxl *adapter, void *buf, size_t count);
  975. };
  976. extern const struct cxl_backend_ops cxl_native_ops;
  977. extern const struct cxl_backend_ops cxl_guest_ops;
  978. extern const struct cxl_backend_ops *cxl_ops;
  979. /* check if the given pci_dev is on the the cxl vphb bus */
  980. bool cxl_pci_is_vphb_device(struct pci_dev *dev);
  981. /* decode AFU error bits in the PSL register PSL_SERR_An */
  982. void cxl_afu_decode_psl_serr(struct cxl_afu *afu, u64 serr);
  983. /*
  984. * Increments the number of attached contexts on an adapter.
  985. * In case an adapter_context_lock is taken the return -EBUSY.
  986. */
  987. int cxl_adapter_context_get(struct cxl *adapter);
  988. /* Decrements the number of attached contexts on an adapter */
  989. void cxl_adapter_context_put(struct cxl *adapter);
  990. /* If no active contexts then prevents contexts from being attached */
  991. int cxl_adapter_context_lock(struct cxl *adapter);
  992. /* Unlock the contexts-lock if taken. Warn and force unlock otherwise */
  993. void cxl_adapter_context_unlock(struct cxl *adapter);
  994. /* Increases the reference count to "struct mm_struct" */
  995. void cxl_context_mm_count_get(struct cxl_context *ctx);
  996. /* Decrements the reference count to "struct mm_struct" */
  997. void cxl_context_mm_count_put(struct cxl_context *ctx);
  998. #endif