pci.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  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/pci_regs.h>
  10. #include <linux/pci_ids.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/sort.h>
  16. #include <linux/pci.h>
  17. #include <linux/of.h>
  18. #include <linux/delay.h>
  19. #include <asm/opal.h>
  20. #include <asm/msi_bitmap.h>
  21. #include <asm/pnv-pci.h>
  22. #include <asm/io.h>
  23. #include <asm/reg.h>
  24. #include "cxl.h"
  25. #include <misc/cxl.h>
  26. #define CXL_PCI_VSEC_ID 0x1280
  27. #define CXL_VSEC_MIN_SIZE 0x80
  28. #define CXL_READ_VSEC_LENGTH(dev, vsec, dest) \
  29. { \
  30. pci_read_config_word(dev, vsec + 0x6, dest); \
  31. *dest >>= 4; \
  32. }
  33. #define CXL_READ_VSEC_NAFUS(dev, vsec, dest) \
  34. pci_read_config_byte(dev, vsec + 0x8, dest)
  35. #define CXL_READ_VSEC_STATUS(dev, vsec, dest) \
  36. pci_read_config_byte(dev, vsec + 0x9, dest)
  37. #define CXL_STATUS_SECOND_PORT 0x80
  38. #define CXL_STATUS_MSI_X_FULL 0x40
  39. #define CXL_STATUS_MSI_X_SINGLE 0x20
  40. #define CXL_STATUS_FLASH_RW 0x08
  41. #define CXL_STATUS_FLASH_RO 0x04
  42. #define CXL_STATUS_LOADABLE_AFU 0x02
  43. #define CXL_STATUS_LOADABLE_PSL 0x01
  44. /* If we see these features we won't try to use the card */
  45. #define CXL_UNSUPPORTED_FEATURES \
  46. (CXL_STATUS_MSI_X_FULL | CXL_STATUS_MSI_X_SINGLE)
  47. #define CXL_READ_VSEC_MODE_CONTROL(dev, vsec, dest) \
  48. pci_read_config_byte(dev, vsec + 0xa, dest)
  49. #define CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val) \
  50. pci_write_config_byte(dev, vsec + 0xa, val)
  51. #define CXL_VSEC_PROTOCOL_MASK 0xe0
  52. #define CXL_VSEC_PROTOCOL_1024TB 0x80
  53. #define CXL_VSEC_PROTOCOL_512TB 0x40
  54. #define CXL_VSEC_PROTOCOL_256TB 0x20 /* Power 8/9 uses this */
  55. #define CXL_VSEC_PROTOCOL_ENABLE 0x01
  56. #define CXL_READ_VSEC_PSL_REVISION(dev, vsec, dest) \
  57. pci_read_config_word(dev, vsec + 0xc, dest)
  58. #define CXL_READ_VSEC_CAIA_MINOR(dev, vsec, dest) \
  59. pci_read_config_byte(dev, vsec + 0xe, dest)
  60. #define CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, dest) \
  61. pci_read_config_byte(dev, vsec + 0xf, dest)
  62. #define CXL_READ_VSEC_BASE_IMAGE(dev, vsec, dest) \
  63. pci_read_config_word(dev, vsec + 0x10, dest)
  64. #define CXL_READ_VSEC_IMAGE_STATE(dev, vsec, dest) \
  65. pci_read_config_byte(dev, vsec + 0x13, dest)
  66. #define CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, val) \
  67. pci_write_config_byte(dev, vsec + 0x13, val)
  68. #define CXL_VSEC_USER_IMAGE_LOADED 0x80 /* RO */
  69. #define CXL_VSEC_PERST_LOADS_IMAGE 0x20 /* RW */
  70. #define CXL_VSEC_PERST_SELECT_USER 0x10 /* RW */
  71. #define CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, dest) \
  72. pci_read_config_dword(dev, vsec + 0x20, dest)
  73. #define CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, dest) \
  74. pci_read_config_dword(dev, vsec + 0x24, dest)
  75. #define CXL_READ_VSEC_PS_OFF(dev, vsec, dest) \
  76. pci_read_config_dword(dev, vsec + 0x28, dest)
  77. #define CXL_READ_VSEC_PS_SIZE(dev, vsec, dest) \
  78. pci_read_config_dword(dev, vsec + 0x2c, dest)
  79. /* This works a little different than the p1/p2 register accesses to make it
  80. * easier to pull out individual fields */
  81. #define AFUD_READ(afu, off) in_be64(afu->native->afu_desc_mmio + off)
  82. #define AFUD_READ_LE(afu, off) in_le64(afu->native->afu_desc_mmio + off)
  83. #define EXTRACT_PPC_BIT(val, bit) (!!(val & PPC_BIT(bit)))
  84. #define EXTRACT_PPC_BITS(val, bs, be) ((val & PPC_BITMASK(bs, be)) >> PPC_BITLSHIFT(be))
  85. #define AFUD_READ_INFO(afu) AFUD_READ(afu, 0x0)
  86. #define AFUD_NUM_INTS_PER_PROC(val) EXTRACT_PPC_BITS(val, 0, 15)
  87. #define AFUD_NUM_PROCS(val) EXTRACT_PPC_BITS(val, 16, 31)
  88. #define AFUD_NUM_CRS(val) EXTRACT_PPC_BITS(val, 32, 47)
  89. #define AFUD_MULTIMODE(val) EXTRACT_PPC_BIT(val, 48)
  90. #define AFUD_PUSH_BLOCK_TRANSFER(val) EXTRACT_PPC_BIT(val, 55)
  91. #define AFUD_DEDICATED_PROCESS(val) EXTRACT_PPC_BIT(val, 59)
  92. #define AFUD_AFU_DIRECTED(val) EXTRACT_PPC_BIT(val, 61)
  93. #define AFUD_TIME_SLICED(val) EXTRACT_PPC_BIT(val, 63)
  94. #define AFUD_READ_CR(afu) AFUD_READ(afu, 0x20)
  95. #define AFUD_CR_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
  96. #define AFUD_READ_CR_OFF(afu) AFUD_READ(afu, 0x28)
  97. #define AFUD_READ_PPPSA(afu) AFUD_READ(afu, 0x30)
  98. #define AFUD_PPPSA_PP(val) EXTRACT_PPC_BIT(val, 6)
  99. #define AFUD_PPPSA_PSA(val) EXTRACT_PPC_BIT(val, 7)
  100. #define AFUD_PPPSA_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
  101. #define AFUD_READ_PPPSA_OFF(afu) AFUD_READ(afu, 0x38)
  102. #define AFUD_READ_EB(afu) AFUD_READ(afu, 0x40)
  103. #define AFUD_EB_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
  104. #define AFUD_READ_EB_OFF(afu) AFUD_READ(afu, 0x48)
  105. static const struct pci_device_id cxl_pci_tbl[] = {
  106. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0477), },
  107. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x044b), },
  108. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x04cf), },
  109. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0601), },
  110. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0623), },
  111. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0628), },
  112. { }
  113. };
  114. MODULE_DEVICE_TABLE(pci, cxl_pci_tbl);
  115. /*
  116. * Mostly using these wrappers to avoid confusion:
  117. * priv 1 is BAR2, while priv 2 is BAR0
  118. */
  119. static inline resource_size_t p1_base(struct pci_dev *dev)
  120. {
  121. return pci_resource_start(dev, 2);
  122. }
  123. static inline resource_size_t p1_size(struct pci_dev *dev)
  124. {
  125. return pci_resource_len(dev, 2);
  126. }
  127. static inline resource_size_t p2_base(struct pci_dev *dev)
  128. {
  129. return pci_resource_start(dev, 0);
  130. }
  131. static inline resource_size_t p2_size(struct pci_dev *dev)
  132. {
  133. return pci_resource_len(dev, 0);
  134. }
  135. static int find_cxl_vsec(struct pci_dev *dev)
  136. {
  137. int vsec = 0;
  138. u16 val;
  139. while ((vsec = pci_find_next_ext_capability(dev, vsec, PCI_EXT_CAP_ID_VNDR))) {
  140. pci_read_config_word(dev, vsec + 0x4, &val);
  141. if (val == CXL_PCI_VSEC_ID)
  142. return vsec;
  143. }
  144. return 0;
  145. }
  146. static void dump_cxl_config_space(struct pci_dev *dev)
  147. {
  148. int vsec;
  149. u32 val;
  150. dev_info(&dev->dev, "dump_cxl_config_space\n");
  151. pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &val);
  152. dev_info(&dev->dev, "BAR0: %#.8x\n", val);
  153. pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &val);
  154. dev_info(&dev->dev, "BAR1: %#.8x\n", val);
  155. pci_read_config_dword(dev, PCI_BASE_ADDRESS_2, &val);
  156. dev_info(&dev->dev, "BAR2: %#.8x\n", val);
  157. pci_read_config_dword(dev, PCI_BASE_ADDRESS_3, &val);
  158. dev_info(&dev->dev, "BAR3: %#.8x\n", val);
  159. pci_read_config_dword(dev, PCI_BASE_ADDRESS_4, &val);
  160. dev_info(&dev->dev, "BAR4: %#.8x\n", val);
  161. pci_read_config_dword(dev, PCI_BASE_ADDRESS_5, &val);
  162. dev_info(&dev->dev, "BAR5: %#.8x\n", val);
  163. dev_info(&dev->dev, "p1 regs: %#llx, len: %#llx\n",
  164. p1_base(dev), p1_size(dev));
  165. dev_info(&dev->dev, "p2 regs: %#llx, len: %#llx\n",
  166. p2_base(dev), p2_size(dev));
  167. dev_info(&dev->dev, "BAR 4/5: %#llx, len: %#llx\n",
  168. pci_resource_start(dev, 4), pci_resource_len(dev, 4));
  169. if (!(vsec = find_cxl_vsec(dev)))
  170. return;
  171. #define show_reg(name, what) \
  172. dev_info(&dev->dev, "cxl vsec: %30s: %#x\n", name, what)
  173. pci_read_config_dword(dev, vsec + 0x0, &val);
  174. show_reg("Cap ID", (val >> 0) & 0xffff);
  175. show_reg("Cap Ver", (val >> 16) & 0xf);
  176. show_reg("Next Cap Ptr", (val >> 20) & 0xfff);
  177. pci_read_config_dword(dev, vsec + 0x4, &val);
  178. show_reg("VSEC ID", (val >> 0) & 0xffff);
  179. show_reg("VSEC Rev", (val >> 16) & 0xf);
  180. show_reg("VSEC Length", (val >> 20) & 0xfff);
  181. pci_read_config_dword(dev, vsec + 0x8, &val);
  182. show_reg("Num AFUs", (val >> 0) & 0xff);
  183. show_reg("Status", (val >> 8) & 0xff);
  184. show_reg("Mode Control", (val >> 16) & 0xff);
  185. show_reg("Reserved", (val >> 24) & 0xff);
  186. pci_read_config_dword(dev, vsec + 0xc, &val);
  187. show_reg("PSL Rev", (val >> 0) & 0xffff);
  188. show_reg("CAIA Ver", (val >> 16) & 0xffff);
  189. pci_read_config_dword(dev, vsec + 0x10, &val);
  190. show_reg("Base Image Rev", (val >> 0) & 0xffff);
  191. show_reg("Reserved", (val >> 16) & 0x0fff);
  192. show_reg("Image Control", (val >> 28) & 0x3);
  193. show_reg("Reserved", (val >> 30) & 0x1);
  194. show_reg("Image Loaded", (val >> 31) & 0x1);
  195. pci_read_config_dword(dev, vsec + 0x14, &val);
  196. show_reg("Reserved", val);
  197. pci_read_config_dword(dev, vsec + 0x18, &val);
  198. show_reg("Reserved", val);
  199. pci_read_config_dword(dev, vsec + 0x1c, &val);
  200. show_reg("Reserved", val);
  201. pci_read_config_dword(dev, vsec + 0x20, &val);
  202. show_reg("AFU Descriptor Offset", val);
  203. pci_read_config_dword(dev, vsec + 0x24, &val);
  204. show_reg("AFU Descriptor Size", val);
  205. pci_read_config_dword(dev, vsec + 0x28, &val);
  206. show_reg("Problem State Offset", val);
  207. pci_read_config_dword(dev, vsec + 0x2c, &val);
  208. show_reg("Problem State Size", val);
  209. pci_read_config_dword(dev, vsec + 0x30, &val);
  210. show_reg("Reserved", val);
  211. pci_read_config_dword(dev, vsec + 0x34, &val);
  212. show_reg("Reserved", val);
  213. pci_read_config_dword(dev, vsec + 0x38, &val);
  214. show_reg("Reserved", val);
  215. pci_read_config_dword(dev, vsec + 0x3c, &val);
  216. show_reg("Reserved", val);
  217. pci_read_config_dword(dev, vsec + 0x40, &val);
  218. show_reg("PSL Programming Port", val);
  219. pci_read_config_dword(dev, vsec + 0x44, &val);
  220. show_reg("PSL Programming Control", val);
  221. pci_read_config_dword(dev, vsec + 0x48, &val);
  222. show_reg("Reserved", val);
  223. pci_read_config_dword(dev, vsec + 0x4c, &val);
  224. show_reg("Reserved", val);
  225. pci_read_config_dword(dev, vsec + 0x50, &val);
  226. show_reg("Flash Address Register", val);
  227. pci_read_config_dword(dev, vsec + 0x54, &val);
  228. show_reg("Flash Size Register", val);
  229. pci_read_config_dword(dev, vsec + 0x58, &val);
  230. show_reg("Flash Status/Control Register", val);
  231. pci_read_config_dword(dev, vsec + 0x58, &val);
  232. show_reg("Flash Data Port", val);
  233. #undef show_reg
  234. }
  235. static void dump_afu_descriptor(struct cxl_afu *afu)
  236. {
  237. u64 val, afu_cr_num, afu_cr_off, afu_cr_len;
  238. int i;
  239. #define show_reg(name, what) \
  240. dev_info(&afu->dev, "afu desc: %30s: %#llx\n", name, what)
  241. val = AFUD_READ_INFO(afu);
  242. show_reg("num_ints_per_process", AFUD_NUM_INTS_PER_PROC(val));
  243. show_reg("num_of_processes", AFUD_NUM_PROCS(val));
  244. show_reg("num_of_afu_CRs", AFUD_NUM_CRS(val));
  245. show_reg("req_prog_mode", val & 0xffffULL);
  246. afu_cr_num = AFUD_NUM_CRS(val);
  247. val = AFUD_READ(afu, 0x8);
  248. show_reg("Reserved", val);
  249. val = AFUD_READ(afu, 0x10);
  250. show_reg("Reserved", val);
  251. val = AFUD_READ(afu, 0x18);
  252. show_reg("Reserved", val);
  253. val = AFUD_READ_CR(afu);
  254. show_reg("Reserved", (val >> (63-7)) & 0xff);
  255. show_reg("AFU_CR_len", AFUD_CR_LEN(val));
  256. afu_cr_len = AFUD_CR_LEN(val) * 256;
  257. val = AFUD_READ_CR_OFF(afu);
  258. afu_cr_off = val;
  259. show_reg("AFU_CR_offset", val);
  260. val = AFUD_READ_PPPSA(afu);
  261. show_reg("PerProcessPSA_control", (val >> (63-7)) & 0xff);
  262. show_reg("PerProcessPSA Length", AFUD_PPPSA_LEN(val));
  263. val = AFUD_READ_PPPSA_OFF(afu);
  264. show_reg("PerProcessPSA_offset", val);
  265. val = AFUD_READ_EB(afu);
  266. show_reg("Reserved", (val >> (63-7)) & 0xff);
  267. show_reg("AFU_EB_len", AFUD_EB_LEN(val));
  268. val = AFUD_READ_EB_OFF(afu);
  269. show_reg("AFU_EB_offset", val);
  270. for (i = 0; i < afu_cr_num; i++) {
  271. val = AFUD_READ_LE(afu, afu_cr_off + i * afu_cr_len);
  272. show_reg("CR Vendor", val & 0xffff);
  273. show_reg("CR Device", (val >> 16) & 0xffff);
  274. }
  275. #undef show_reg
  276. }
  277. #define P8_CAPP_UNIT0_ID 0xBA
  278. #define P8_CAPP_UNIT1_ID 0XBE
  279. #define P9_CAPP_UNIT0_ID 0xC0
  280. #define P9_CAPP_UNIT1_ID 0xE0
  281. static int get_phb_index(struct device_node *np, u32 *phb_index)
  282. {
  283. if (of_property_read_u32(np, "ibm,phb-index", phb_index))
  284. return -ENODEV;
  285. return 0;
  286. }
  287. static u64 get_capp_unit_id(struct device_node *np, u32 phb_index)
  288. {
  289. /*
  290. * POWER 8:
  291. * - For chips other than POWER8NVL, we only have CAPP 0,
  292. * irrespective of which PHB is used.
  293. * - For POWER8NVL, assume CAPP 0 is attached to PHB0 and
  294. * CAPP 1 is attached to PHB1.
  295. */
  296. if (cxl_is_power8()) {
  297. if (!pvr_version_is(PVR_POWER8NVL))
  298. return P8_CAPP_UNIT0_ID;
  299. if (phb_index == 0)
  300. return P8_CAPP_UNIT0_ID;
  301. if (phb_index == 1)
  302. return P8_CAPP_UNIT1_ID;
  303. }
  304. /*
  305. * POWER 9:
  306. * PEC0 (PHB0). Capp ID = CAPP0 (0b1100_0000)
  307. * PEC1 (PHB1 - PHB2). No capi mode
  308. * PEC2 (PHB3 - PHB4 - PHB5): Capi mode on PHB3 only. Capp ID = CAPP1 (0b1110_0000)
  309. */
  310. if (cxl_is_power9()) {
  311. if (phb_index == 0)
  312. return P9_CAPP_UNIT0_ID;
  313. if (phb_index == 3)
  314. return P9_CAPP_UNIT1_ID;
  315. }
  316. return 0;
  317. }
  318. int cxl_calc_capp_routing(struct pci_dev *dev, u64 *chipid,
  319. u32 *phb_index, u64 *capp_unit_id)
  320. {
  321. int rc;
  322. struct device_node *np;
  323. const __be32 *prop;
  324. if (!(np = pnv_pci_get_phb_node(dev)))
  325. return -ENODEV;
  326. while (np && !(prop = of_get_property(np, "ibm,chip-id", NULL)))
  327. np = of_get_next_parent(np);
  328. if (!np)
  329. return -ENODEV;
  330. *chipid = be32_to_cpup(prop);
  331. rc = get_phb_index(np, phb_index);
  332. if (rc) {
  333. pr_err("cxl: invalid phb index\n");
  334. return rc;
  335. }
  336. *capp_unit_id = get_capp_unit_id(np, *phb_index);
  337. of_node_put(np);
  338. if (!*capp_unit_id) {
  339. pr_err("cxl: invalid capp unit id (phb_index: %d)\n",
  340. *phb_index);
  341. return -ENODEV;
  342. }
  343. return 0;
  344. }
  345. static DEFINE_MUTEX(indications_mutex);
  346. static int get_phb_indications(struct pci_dev *dev, u64 *capiind, u64 *asnind,
  347. u64 *nbwind)
  348. {
  349. static u64 nbw, asn, capi = 0;
  350. struct device_node *np;
  351. const __be32 *prop;
  352. mutex_lock(&indications_mutex);
  353. if (!capi) {
  354. if (!(np = pnv_pci_get_phb_node(dev))) {
  355. mutex_unlock(&indications_mutex);
  356. return -ENODEV;
  357. }
  358. prop = of_get_property(np, "ibm,phb-indications", NULL);
  359. if (!prop) {
  360. nbw = 0x0300UL; /* legacy values */
  361. asn = 0x0400UL;
  362. capi = 0x0200UL;
  363. } else {
  364. nbw = (u64)be32_to_cpu(prop[2]);
  365. asn = (u64)be32_to_cpu(prop[1]);
  366. capi = (u64)be32_to_cpu(prop[0]);
  367. }
  368. of_node_put(np);
  369. }
  370. *capiind = capi;
  371. *asnind = asn;
  372. *nbwind = nbw;
  373. mutex_unlock(&indications_mutex);
  374. return 0;
  375. }
  376. int cxl_get_xsl9_dsnctl(struct pci_dev *dev, u64 capp_unit_id, u64 *reg)
  377. {
  378. u64 xsl_dsnctl;
  379. u64 capiind, asnind, nbwind;
  380. /*
  381. * CAPI Identifier bits [0:7]
  382. * bit 61:60 MSI bits --> 0
  383. * bit 59 TVT selector --> 0
  384. */
  385. if (get_phb_indications(dev, &capiind, &asnind, &nbwind))
  386. return -ENODEV;
  387. /*
  388. * Tell XSL where to route data to.
  389. * The field chipid should match the PHB CAPI_CMPM register
  390. */
  391. xsl_dsnctl = (capiind << (63-15)); /* Bit 57 */
  392. xsl_dsnctl |= (capp_unit_id << (63-15));
  393. /* nMMU_ID Defaults to: b’000001001’*/
  394. xsl_dsnctl |= ((u64)0x09 << (63-28));
  395. /*
  396. * Used to identify CAPI packets which should be sorted into
  397. * the Non-Blocking queues by the PHB. This field should match
  398. * the PHB PBL_NBW_CMPM register
  399. * nbwind=0x03, bits [57:58], must include capi indicator.
  400. * Not supported on P9 DD1.
  401. */
  402. xsl_dsnctl |= (nbwind << (63-55));
  403. /*
  404. * Upper 16b address bits of ASB_Notify messages sent to the
  405. * system. Need to match the PHB’s ASN Compare/Mask Register.
  406. * Not supported on P9 DD1.
  407. */
  408. xsl_dsnctl |= asnind;
  409. *reg = xsl_dsnctl;
  410. return 0;
  411. }
  412. static int init_implementation_adapter_regs_psl9(struct cxl *adapter,
  413. struct pci_dev *dev)
  414. {
  415. u64 xsl_dsnctl, psl_fircntl;
  416. u64 chipid;
  417. u32 phb_index;
  418. u64 capp_unit_id;
  419. u64 psl_debug;
  420. int rc;
  421. rc = cxl_calc_capp_routing(dev, &chipid, &phb_index, &capp_unit_id);
  422. if (rc)
  423. return rc;
  424. rc = cxl_get_xsl9_dsnctl(dev, capp_unit_id, &xsl_dsnctl);
  425. if (rc)
  426. return rc;
  427. cxl_p1_write(adapter, CXL_XSL9_DSNCTL, xsl_dsnctl);
  428. /* Set fir_cntl to recommended value for production env */
  429. psl_fircntl = (0x2ULL << (63-3)); /* ce_report */
  430. psl_fircntl |= (0x1ULL << (63-6)); /* FIR_report */
  431. psl_fircntl |= 0x1ULL; /* ce_thresh */
  432. cxl_p1_write(adapter, CXL_PSL9_FIR_CNTL, psl_fircntl);
  433. /* Setup the PSL to transmit packets on the PCIe before the
  434. * CAPP is enabled. Make sure that CAPP virtual machines are disabled
  435. */
  436. cxl_p1_write(adapter, CXL_PSL9_DSNDCTL, 0x0001001000012A10ULL);
  437. /*
  438. * A response to an ASB_Notify request is returned by the
  439. * system as an MMIO write to the address defined in
  440. * the PSL_TNR_ADDR register.
  441. * keep the Reset Value: 0x00020000E0000000
  442. */
  443. /* Enable XSL rty limit */
  444. cxl_p1_write(adapter, CXL_XSL9_DEF, 0x51F8000000000005ULL);
  445. /* Change XSL_INV dummy read threshold */
  446. cxl_p1_write(adapter, CXL_XSL9_INV, 0x0000040007FFC200ULL);
  447. if (phb_index == 3) {
  448. /* disable machines 31-47 and 20-27 for DMA */
  449. cxl_p1_write(adapter, CXL_PSL9_APCDEDTYPE, 0x40000FF3FFFF0000ULL);
  450. }
  451. /* Snoop machines */
  452. cxl_p1_write(adapter, CXL_PSL9_APCDEDALLOC, 0x800F000200000000ULL);
  453. /* Enable NORST and DD2 features */
  454. cxl_p1_write(adapter, CXL_PSL9_DEBUG, 0xC000000000000000ULL);
  455. /*
  456. * Check if PSL has data-cache. We need to flush adapter datacache
  457. * when as its about to be removed.
  458. */
  459. psl_debug = cxl_p1_read(adapter, CXL_PSL9_DEBUG);
  460. if (psl_debug & CXL_PSL_DEBUG_CDC) {
  461. dev_dbg(&dev->dev, "No data-cache present\n");
  462. adapter->native->no_data_cache = true;
  463. }
  464. return 0;
  465. }
  466. static int init_implementation_adapter_regs_psl8(struct cxl *adapter, struct pci_dev *dev)
  467. {
  468. u64 psl_dsnctl, psl_fircntl;
  469. u64 chipid;
  470. u32 phb_index;
  471. u64 capp_unit_id;
  472. int rc;
  473. rc = cxl_calc_capp_routing(dev, &chipid, &phb_index, &capp_unit_id);
  474. if (rc)
  475. return rc;
  476. psl_dsnctl = 0x0000900000000000ULL; /* pteupd ttype, scdone */
  477. psl_dsnctl |= (0x2ULL << (63-38)); /* MMIO hang pulse: 256 us */
  478. /* Tell PSL where to route data to */
  479. psl_dsnctl |= (chipid << (63-5));
  480. psl_dsnctl |= (capp_unit_id << (63-13));
  481. cxl_p1_write(adapter, CXL_PSL_DSNDCTL, psl_dsnctl);
  482. cxl_p1_write(adapter, CXL_PSL_RESLCKTO, 0x20000000200ULL);
  483. /* snoop write mask */
  484. cxl_p1_write(adapter, CXL_PSL_SNWRALLOC, 0x00000000FFFFFFFFULL);
  485. /* set fir_cntl to recommended value for production env */
  486. psl_fircntl = (0x2ULL << (63-3)); /* ce_report */
  487. psl_fircntl |= (0x1ULL << (63-6)); /* FIR_report */
  488. psl_fircntl |= 0x1ULL; /* ce_thresh */
  489. cxl_p1_write(adapter, CXL_PSL_FIR_CNTL, psl_fircntl);
  490. /* for debugging with trace arrays */
  491. cxl_p1_write(adapter, CXL_PSL_TRACE, 0x0000FF7C00000000ULL);
  492. return 0;
  493. }
  494. /* PSL */
  495. #define TBSYNC_CAL(n) (((u64)n & 0x7) << (63-3))
  496. #define TBSYNC_CNT(n) (((u64)n & 0x7) << (63-6))
  497. /* For the PSL this is a multiple for 0 < n <= 7: */
  498. #define PSL_2048_250MHZ_CYCLES 1
  499. static void write_timebase_ctrl_psl8(struct cxl *adapter)
  500. {
  501. cxl_p1_write(adapter, CXL_PSL_TB_CTLSTAT,
  502. TBSYNC_CNT(2 * PSL_2048_250MHZ_CYCLES));
  503. }
  504. static u64 timebase_read_psl9(struct cxl *adapter)
  505. {
  506. return cxl_p1_read(adapter, CXL_PSL9_Timebase);
  507. }
  508. static u64 timebase_read_psl8(struct cxl *adapter)
  509. {
  510. return cxl_p1_read(adapter, CXL_PSL_Timebase);
  511. }
  512. static void cxl_setup_psl_timebase(struct cxl *adapter, struct pci_dev *dev)
  513. {
  514. struct device_node *np;
  515. adapter->psl_timebase_synced = false;
  516. if (!(np = pnv_pci_get_phb_node(dev)))
  517. return;
  518. /* Do not fail when CAPP timebase sync is not supported by OPAL */
  519. of_node_get(np);
  520. if (! of_get_property(np, "ibm,capp-timebase-sync", NULL)) {
  521. of_node_put(np);
  522. dev_info(&dev->dev, "PSL timebase inactive: OPAL support missing\n");
  523. return;
  524. }
  525. of_node_put(np);
  526. /*
  527. * Setup PSL Timebase Control and Status register
  528. * with the recommended Timebase Sync Count value
  529. */
  530. if (adapter->native->sl_ops->write_timebase_ctrl)
  531. adapter->native->sl_ops->write_timebase_ctrl(adapter);
  532. /* Enable PSL Timebase */
  533. cxl_p1_write(adapter, CXL_PSL_Control, 0x0000000000000000);
  534. cxl_p1_write(adapter, CXL_PSL_Control, CXL_PSL_Control_tb);
  535. return;
  536. }
  537. static int init_implementation_afu_regs_psl9(struct cxl_afu *afu)
  538. {
  539. return 0;
  540. }
  541. static int init_implementation_afu_regs_psl8(struct cxl_afu *afu)
  542. {
  543. /* read/write masks for this slice */
  544. cxl_p1n_write(afu, CXL_PSL_APCALLOC_A, 0xFFFFFFFEFEFEFEFEULL);
  545. /* APC read/write masks for this slice */
  546. cxl_p1n_write(afu, CXL_PSL_COALLOC_A, 0xFF000000FEFEFEFEULL);
  547. /* for debugging with trace arrays */
  548. cxl_p1n_write(afu, CXL_PSL_SLICE_TRACE, 0x0000FFFF00000000ULL);
  549. cxl_p1n_write(afu, CXL_PSL_RXCTL_A, CXL_PSL_RXCTL_AFUHP_4S);
  550. return 0;
  551. }
  552. int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq,
  553. unsigned int virq)
  554. {
  555. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  556. return pnv_cxl_ioda_msi_setup(dev, hwirq, virq);
  557. }
  558. int cxl_update_image_control(struct cxl *adapter)
  559. {
  560. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  561. int rc;
  562. int vsec;
  563. u8 image_state;
  564. if (!(vsec = find_cxl_vsec(dev))) {
  565. dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
  566. return -ENODEV;
  567. }
  568. if ((rc = CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state))) {
  569. dev_err(&dev->dev, "failed to read image state: %i\n", rc);
  570. return rc;
  571. }
  572. if (adapter->perst_loads_image)
  573. image_state |= CXL_VSEC_PERST_LOADS_IMAGE;
  574. else
  575. image_state &= ~CXL_VSEC_PERST_LOADS_IMAGE;
  576. if (adapter->perst_select_user)
  577. image_state |= CXL_VSEC_PERST_SELECT_USER;
  578. else
  579. image_state &= ~CXL_VSEC_PERST_SELECT_USER;
  580. if ((rc = CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, image_state))) {
  581. dev_err(&dev->dev, "failed to update image control: %i\n", rc);
  582. return rc;
  583. }
  584. return 0;
  585. }
  586. int cxl_pci_alloc_one_irq(struct cxl *adapter)
  587. {
  588. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  589. return pnv_cxl_alloc_hwirqs(dev, 1);
  590. }
  591. void cxl_pci_release_one_irq(struct cxl *adapter, int hwirq)
  592. {
  593. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  594. return pnv_cxl_release_hwirqs(dev, hwirq, 1);
  595. }
  596. int cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
  597. struct cxl *adapter, unsigned int num)
  598. {
  599. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  600. return pnv_cxl_alloc_hwirq_ranges(irqs, dev, num);
  601. }
  602. void cxl_pci_release_irq_ranges(struct cxl_irq_ranges *irqs,
  603. struct cxl *adapter)
  604. {
  605. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  606. pnv_cxl_release_hwirq_ranges(irqs, dev);
  607. }
  608. static int setup_cxl_bars(struct pci_dev *dev)
  609. {
  610. /* Safety check in case we get backported to < 3.17 without M64 */
  611. if ((p1_base(dev) < 0x100000000ULL) ||
  612. (p2_base(dev) < 0x100000000ULL)) {
  613. dev_err(&dev->dev, "ABORTING: M32 BAR assignment incompatible with CXL\n");
  614. return -ENODEV;
  615. }
  616. /*
  617. * BAR 4/5 has a special meaning for CXL and must be programmed with a
  618. * special value corresponding to the CXL protocol address range.
  619. * For POWER 8/9 that means bits 48:49 must be set to 10
  620. */
  621. pci_write_config_dword(dev, PCI_BASE_ADDRESS_4, 0x00000000);
  622. pci_write_config_dword(dev, PCI_BASE_ADDRESS_5, 0x00020000);
  623. return 0;
  624. }
  625. /* pciex node: ibm,opal-m64-window = <0x3d058 0x0 0x3d058 0x0 0x8 0x0>; */
  626. static int switch_card_to_cxl(struct pci_dev *dev)
  627. {
  628. int vsec;
  629. u8 val;
  630. int rc;
  631. dev_info(&dev->dev, "switch card to CXL\n");
  632. if (!(vsec = find_cxl_vsec(dev))) {
  633. dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
  634. return -ENODEV;
  635. }
  636. if ((rc = CXL_READ_VSEC_MODE_CONTROL(dev, vsec, &val))) {
  637. dev_err(&dev->dev, "failed to read current mode control: %i", rc);
  638. return rc;
  639. }
  640. val &= ~CXL_VSEC_PROTOCOL_MASK;
  641. val |= CXL_VSEC_PROTOCOL_256TB | CXL_VSEC_PROTOCOL_ENABLE;
  642. if ((rc = CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val))) {
  643. dev_err(&dev->dev, "failed to enable CXL protocol: %i", rc);
  644. return rc;
  645. }
  646. /*
  647. * The CAIA spec (v0.12 11.6 Bi-modal Device Support) states
  648. * we must wait 100ms after this mode switch before touching
  649. * PCIe config space.
  650. */
  651. msleep(100);
  652. return 0;
  653. }
  654. static int pci_map_slice_regs(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
  655. {
  656. u64 p1n_base, p2n_base, afu_desc;
  657. const u64 p1n_size = 0x100;
  658. const u64 p2n_size = 0x1000;
  659. p1n_base = p1_base(dev) + 0x10000 + (afu->slice * p1n_size);
  660. p2n_base = p2_base(dev) + (afu->slice * p2n_size);
  661. afu->psn_phys = p2_base(dev) + (adapter->native->ps_off + (afu->slice * adapter->ps_size));
  662. afu_desc = p2_base(dev) + adapter->native->afu_desc_off + (afu->slice * adapter->native->afu_desc_size);
  663. if (!(afu->native->p1n_mmio = ioremap(p1n_base, p1n_size)))
  664. goto err;
  665. if (!(afu->p2n_mmio = ioremap(p2n_base, p2n_size)))
  666. goto err1;
  667. if (afu_desc) {
  668. if (!(afu->native->afu_desc_mmio = ioremap(afu_desc, adapter->native->afu_desc_size)))
  669. goto err2;
  670. }
  671. return 0;
  672. err2:
  673. iounmap(afu->p2n_mmio);
  674. err1:
  675. iounmap(afu->native->p1n_mmio);
  676. err:
  677. dev_err(&afu->dev, "Error mapping AFU MMIO regions\n");
  678. return -ENOMEM;
  679. }
  680. static void pci_unmap_slice_regs(struct cxl_afu *afu)
  681. {
  682. if (afu->p2n_mmio) {
  683. iounmap(afu->p2n_mmio);
  684. afu->p2n_mmio = NULL;
  685. }
  686. if (afu->native->p1n_mmio) {
  687. iounmap(afu->native->p1n_mmio);
  688. afu->native->p1n_mmio = NULL;
  689. }
  690. if (afu->native->afu_desc_mmio) {
  691. iounmap(afu->native->afu_desc_mmio);
  692. afu->native->afu_desc_mmio = NULL;
  693. }
  694. }
  695. void cxl_pci_release_afu(struct device *dev)
  696. {
  697. struct cxl_afu *afu = to_cxl_afu(dev);
  698. pr_devel("%s\n", __func__);
  699. idr_destroy(&afu->contexts_idr);
  700. cxl_release_spa(afu);
  701. kfree(afu->native);
  702. kfree(afu);
  703. }
  704. /* Expects AFU struct to have recently been zeroed out */
  705. static int cxl_read_afu_descriptor(struct cxl_afu *afu)
  706. {
  707. u64 val;
  708. val = AFUD_READ_INFO(afu);
  709. afu->pp_irqs = AFUD_NUM_INTS_PER_PROC(val);
  710. afu->max_procs_virtualised = AFUD_NUM_PROCS(val);
  711. afu->crs_num = AFUD_NUM_CRS(val);
  712. if (AFUD_AFU_DIRECTED(val))
  713. afu->modes_supported |= CXL_MODE_DIRECTED;
  714. if (AFUD_DEDICATED_PROCESS(val))
  715. afu->modes_supported |= CXL_MODE_DEDICATED;
  716. if (AFUD_TIME_SLICED(val))
  717. afu->modes_supported |= CXL_MODE_TIME_SLICED;
  718. val = AFUD_READ_PPPSA(afu);
  719. afu->pp_size = AFUD_PPPSA_LEN(val) * 4096;
  720. afu->psa = AFUD_PPPSA_PSA(val);
  721. if ((afu->pp_psa = AFUD_PPPSA_PP(val)))
  722. afu->native->pp_offset = AFUD_READ_PPPSA_OFF(afu);
  723. val = AFUD_READ_CR(afu);
  724. afu->crs_len = AFUD_CR_LEN(val) * 256;
  725. afu->crs_offset = AFUD_READ_CR_OFF(afu);
  726. /* eb_len is in multiple of 4K */
  727. afu->eb_len = AFUD_EB_LEN(AFUD_READ_EB(afu)) * 4096;
  728. afu->eb_offset = AFUD_READ_EB_OFF(afu);
  729. /* eb_off is 4K aligned so lower 12 bits are always zero */
  730. if (EXTRACT_PPC_BITS(afu->eb_offset, 0, 11) != 0) {
  731. dev_warn(&afu->dev,
  732. "Invalid AFU error buffer offset %Lx\n",
  733. afu->eb_offset);
  734. dev_info(&afu->dev,
  735. "Ignoring AFU error buffer in the descriptor\n");
  736. /* indicate that no afu buffer exists */
  737. afu->eb_len = 0;
  738. }
  739. return 0;
  740. }
  741. static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
  742. {
  743. int i, rc;
  744. u32 val;
  745. if (afu->psa && afu->adapter->ps_size <
  746. (afu->native->pp_offset + afu->pp_size*afu->max_procs_virtualised)) {
  747. dev_err(&afu->dev, "per-process PSA can't fit inside the PSA!\n");
  748. return -ENODEV;
  749. }
  750. if (afu->pp_psa && (afu->pp_size < PAGE_SIZE))
  751. dev_warn(&afu->dev, "AFU uses pp_size(%#016llx) < PAGE_SIZE per-process PSA!\n", afu->pp_size);
  752. for (i = 0; i < afu->crs_num; i++) {
  753. rc = cxl_ops->afu_cr_read32(afu, i, 0, &val);
  754. if (rc || val == 0) {
  755. dev_err(&afu->dev, "ABORTING: AFU configuration record %i is invalid\n", i);
  756. return -EINVAL;
  757. }
  758. }
  759. if ((afu->modes_supported & ~CXL_MODE_DEDICATED) && afu->max_procs_virtualised == 0) {
  760. /*
  761. * We could also check this for the dedicated process model
  762. * since the architecture indicates it should be set to 1, but
  763. * in that case we ignore the value and I'd rather not risk
  764. * breaking any existing dedicated process AFUs that left it as
  765. * 0 (not that I'm aware of any). It is clearly an error for an
  766. * AFU directed AFU to set this to 0, and would have previously
  767. * triggered a bug resulting in the maximum not being enforced
  768. * at all since idr_alloc treats 0 as no maximum.
  769. */
  770. dev_err(&afu->dev, "AFU does not support any processes\n");
  771. return -EINVAL;
  772. }
  773. return 0;
  774. }
  775. static int sanitise_afu_regs_psl9(struct cxl_afu *afu)
  776. {
  777. u64 reg;
  778. /*
  779. * Clear out any regs that contain either an IVTE or address or may be
  780. * waiting on an acknowledgment to try to be a bit safer as we bring
  781. * it online
  782. */
  783. reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  784. if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
  785. dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#016llx\n", reg);
  786. if (cxl_ops->afu_reset(afu))
  787. return -EIO;
  788. if (cxl_afu_disable(afu))
  789. return -EIO;
  790. if (cxl_psl_purge(afu))
  791. return -EIO;
  792. }
  793. cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0x0000000000000000);
  794. cxl_p1n_write(afu, CXL_PSL_AMBAR_An, 0x0000000000000000);
  795. reg = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  796. if (reg) {
  797. dev_warn(&afu->dev, "AFU had pending DSISR: %#016llx\n", reg);
  798. if (reg & CXL_PSL9_DSISR_An_TF)
  799. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  800. else
  801. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  802. }
  803. if (afu->adapter->native->sl_ops->register_serr_irq) {
  804. reg = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  805. if (reg) {
  806. if (reg & ~0x000000007fffffff)
  807. dev_warn(&afu->dev, "AFU had pending SERR: %#016llx\n", reg);
  808. cxl_p1n_write(afu, CXL_PSL_SERR_An, reg & ~0xffff);
  809. }
  810. }
  811. reg = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
  812. if (reg) {
  813. dev_warn(&afu->dev, "AFU had pending error status: %#016llx\n", reg);
  814. cxl_p2n_write(afu, CXL_PSL_ErrStat_An, reg);
  815. }
  816. return 0;
  817. }
  818. static int sanitise_afu_regs_psl8(struct cxl_afu *afu)
  819. {
  820. u64 reg;
  821. /*
  822. * Clear out any regs that contain either an IVTE or address or may be
  823. * waiting on an acknowledgement to try to be a bit safer as we bring
  824. * it online
  825. */
  826. reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  827. if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
  828. dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#016llx\n", reg);
  829. if (cxl_ops->afu_reset(afu))
  830. return -EIO;
  831. if (cxl_afu_disable(afu))
  832. return -EIO;
  833. if (cxl_psl_purge(afu))
  834. return -EIO;
  835. }
  836. cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0x0000000000000000);
  837. cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, 0x0000000000000000);
  838. cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An, 0x0000000000000000);
  839. cxl_p1n_write(afu, CXL_PSL_AMBAR_An, 0x0000000000000000);
  840. cxl_p1n_write(afu, CXL_PSL_SPOffset_An, 0x0000000000000000);
  841. cxl_p1n_write(afu, CXL_HAURP_An, 0x0000000000000000);
  842. cxl_p2n_write(afu, CXL_CSRP_An, 0x0000000000000000);
  843. cxl_p2n_write(afu, CXL_AURP1_An, 0x0000000000000000);
  844. cxl_p2n_write(afu, CXL_AURP0_An, 0x0000000000000000);
  845. cxl_p2n_write(afu, CXL_SSTP1_An, 0x0000000000000000);
  846. cxl_p2n_write(afu, CXL_SSTP0_An, 0x0000000000000000);
  847. reg = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  848. if (reg) {
  849. dev_warn(&afu->dev, "AFU had pending DSISR: %#016llx\n", reg);
  850. if (reg & CXL_PSL_DSISR_TRANS)
  851. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  852. else
  853. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  854. }
  855. if (afu->adapter->native->sl_ops->register_serr_irq) {
  856. reg = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  857. if (reg) {
  858. if (reg & ~0xffff)
  859. dev_warn(&afu->dev, "AFU had pending SERR: %#016llx\n", reg);
  860. cxl_p1n_write(afu, CXL_PSL_SERR_An, reg & ~0xffff);
  861. }
  862. }
  863. reg = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
  864. if (reg) {
  865. dev_warn(&afu->dev, "AFU had pending error status: %#016llx\n", reg);
  866. cxl_p2n_write(afu, CXL_PSL_ErrStat_An, reg);
  867. }
  868. return 0;
  869. }
  870. #define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
  871. /*
  872. * afu_eb_read:
  873. * Called from sysfs and reads the afu error info buffer. The h/w only supports
  874. * 4/8 bytes aligned access. So in case the requested offset/count arent 8 byte
  875. * aligned the function uses a bounce buffer which can be max PAGE_SIZE.
  876. */
  877. ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
  878. loff_t off, size_t count)
  879. {
  880. loff_t aligned_start, aligned_end;
  881. size_t aligned_length;
  882. void *tbuf;
  883. const void __iomem *ebuf = afu->native->afu_desc_mmio + afu->eb_offset;
  884. if (count == 0 || off < 0 || (size_t)off >= afu->eb_len)
  885. return 0;
  886. /* calculate aligned read window */
  887. count = min((size_t)(afu->eb_len - off), count);
  888. aligned_start = round_down(off, 8);
  889. aligned_end = round_up(off + count, 8);
  890. aligned_length = aligned_end - aligned_start;
  891. /* max we can copy in one read is PAGE_SIZE */
  892. if (aligned_length > ERR_BUFF_MAX_COPY_SIZE) {
  893. aligned_length = ERR_BUFF_MAX_COPY_SIZE;
  894. count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
  895. }
  896. /* use bounce buffer for copy */
  897. tbuf = (void *)__get_free_page(GFP_KERNEL);
  898. if (!tbuf)
  899. return -ENOMEM;
  900. /* perform aligned read from the mmio region */
  901. memcpy_fromio(tbuf, ebuf + aligned_start, aligned_length);
  902. memcpy(buf, tbuf + (off & 0x7), count);
  903. free_page((unsigned long)tbuf);
  904. return count;
  905. }
  906. static int pci_configure_afu(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
  907. {
  908. int rc;
  909. if ((rc = pci_map_slice_regs(afu, adapter, dev)))
  910. return rc;
  911. if (adapter->native->sl_ops->sanitise_afu_regs) {
  912. rc = adapter->native->sl_ops->sanitise_afu_regs(afu);
  913. if (rc)
  914. goto err1;
  915. }
  916. /* We need to reset the AFU before we can read the AFU descriptor */
  917. if ((rc = cxl_ops->afu_reset(afu)))
  918. goto err1;
  919. if (cxl_verbose)
  920. dump_afu_descriptor(afu);
  921. if ((rc = cxl_read_afu_descriptor(afu)))
  922. goto err1;
  923. if ((rc = cxl_afu_descriptor_looks_ok(afu)))
  924. goto err1;
  925. if (adapter->native->sl_ops->afu_regs_init)
  926. if ((rc = adapter->native->sl_ops->afu_regs_init(afu)))
  927. goto err1;
  928. if (adapter->native->sl_ops->register_serr_irq)
  929. if ((rc = adapter->native->sl_ops->register_serr_irq(afu)))
  930. goto err1;
  931. if ((rc = cxl_native_register_psl_irq(afu)))
  932. goto err2;
  933. atomic_set(&afu->configured_state, 0);
  934. return 0;
  935. err2:
  936. if (adapter->native->sl_ops->release_serr_irq)
  937. adapter->native->sl_ops->release_serr_irq(afu);
  938. err1:
  939. pci_unmap_slice_regs(afu);
  940. return rc;
  941. }
  942. static void pci_deconfigure_afu(struct cxl_afu *afu)
  943. {
  944. /*
  945. * It's okay to deconfigure when AFU is already locked, otherwise wait
  946. * until there are no readers
  947. */
  948. if (atomic_read(&afu->configured_state) != -1) {
  949. while (atomic_cmpxchg(&afu->configured_state, 0, -1) != -1)
  950. schedule();
  951. }
  952. cxl_native_release_psl_irq(afu);
  953. if (afu->adapter->native->sl_ops->release_serr_irq)
  954. afu->adapter->native->sl_ops->release_serr_irq(afu);
  955. pci_unmap_slice_regs(afu);
  956. }
  957. static int pci_init_afu(struct cxl *adapter, int slice, struct pci_dev *dev)
  958. {
  959. struct cxl_afu *afu;
  960. int rc = -ENOMEM;
  961. afu = cxl_alloc_afu(adapter, slice);
  962. if (!afu)
  963. return -ENOMEM;
  964. afu->native = kzalloc(sizeof(struct cxl_afu_native), GFP_KERNEL);
  965. if (!afu->native)
  966. goto err_free_afu;
  967. mutex_init(&afu->native->spa_mutex);
  968. rc = dev_set_name(&afu->dev, "afu%i.%i", adapter->adapter_num, slice);
  969. if (rc)
  970. goto err_free_native;
  971. rc = pci_configure_afu(afu, adapter, dev);
  972. if (rc)
  973. goto err_free_native;
  974. /* Don't care if this fails */
  975. cxl_debugfs_afu_add(afu);
  976. /*
  977. * After we call this function we must not free the afu directly, even
  978. * if it returns an error!
  979. */
  980. if ((rc = cxl_register_afu(afu)))
  981. goto err_put1;
  982. if ((rc = cxl_sysfs_afu_add(afu)))
  983. goto err_put1;
  984. adapter->afu[afu->slice] = afu;
  985. if ((rc = cxl_pci_vphb_add(afu)))
  986. dev_info(&afu->dev, "Can't register vPHB\n");
  987. return 0;
  988. err_put1:
  989. pci_deconfigure_afu(afu);
  990. cxl_debugfs_afu_remove(afu);
  991. device_unregister(&afu->dev);
  992. return rc;
  993. err_free_native:
  994. kfree(afu->native);
  995. err_free_afu:
  996. kfree(afu);
  997. return rc;
  998. }
  999. static void cxl_pci_remove_afu(struct cxl_afu *afu)
  1000. {
  1001. pr_devel("%s\n", __func__);
  1002. if (!afu)
  1003. return;
  1004. cxl_pci_vphb_remove(afu);
  1005. cxl_sysfs_afu_remove(afu);
  1006. cxl_debugfs_afu_remove(afu);
  1007. spin_lock(&afu->adapter->afu_list_lock);
  1008. afu->adapter->afu[afu->slice] = NULL;
  1009. spin_unlock(&afu->adapter->afu_list_lock);
  1010. cxl_context_detach_all(afu);
  1011. cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
  1012. pci_deconfigure_afu(afu);
  1013. device_unregister(&afu->dev);
  1014. }
  1015. int cxl_pci_reset(struct cxl *adapter)
  1016. {
  1017. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  1018. int rc;
  1019. if (adapter->perst_same_image) {
  1020. dev_warn(&dev->dev,
  1021. "cxl: refusing to reset/reflash when perst_reloads_same_image is set.\n");
  1022. return -EINVAL;
  1023. }
  1024. dev_info(&dev->dev, "CXL reset\n");
  1025. /*
  1026. * The adapter is about to be reset, so ignore errors.
  1027. */
  1028. cxl_data_cache_flush(adapter);
  1029. /* pcie_warm_reset requests a fundamental pci reset which includes a
  1030. * PERST assert/deassert. PERST triggers a loading of the image
  1031. * if "user" or "factory" is selected in sysfs */
  1032. if ((rc = pci_set_pcie_reset_state(dev, pcie_warm_reset))) {
  1033. dev_err(&dev->dev, "cxl: pcie_warm_reset failed\n");
  1034. return rc;
  1035. }
  1036. return rc;
  1037. }
  1038. static int cxl_map_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
  1039. {
  1040. if (pci_request_region(dev, 2, "priv 2 regs"))
  1041. goto err1;
  1042. if (pci_request_region(dev, 0, "priv 1 regs"))
  1043. goto err2;
  1044. pr_devel("cxl_map_adapter_regs: p1: %#016llx %#llx, p2: %#016llx %#llx",
  1045. p1_base(dev), p1_size(dev), p2_base(dev), p2_size(dev));
  1046. if (!(adapter->native->p1_mmio = ioremap(p1_base(dev), p1_size(dev))))
  1047. goto err3;
  1048. if (!(adapter->native->p2_mmio = ioremap(p2_base(dev), p2_size(dev))))
  1049. goto err4;
  1050. return 0;
  1051. err4:
  1052. iounmap(adapter->native->p1_mmio);
  1053. adapter->native->p1_mmio = NULL;
  1054. err3:
  1055. pci_release_region(dev, 0);
  1056. err2:
  1057. pci_release_region(dev, 2);
  1058. err1:
  1059. return -ENOMEM;
  1060. }
  1061. static void cxl_unmap_adapter_regs(struct cxl *adapter)
  1062. {
  1063. if (adapter->native->p1_mmio) {
  1064. iounmap(adapter->native->p1_mmio);
  1065. adapter->native->p1_mmio = NULL;
  1066. pci_release_region(to_pci_dev(adapter->dev.parent), 2);
  1067. }
  1068. if (adapter->native->p2_mmio) {
  1069. iounmap(adapter->native->p2_mmio);
  1070. adapter->native->p2_mmio = NULL;
  1071. pci_release_region(to_pci_dev(adapter->dev.parent), 0);
  1072. }
  1073. }
  1074. static int cxl_read_vsec(struct cxl *adapter, struct pci_dev *dev)
  1075. {
  1076. int vsec;
  1077. u32 afu_desc_off, afu_desc_size;
  1078. u32 ps_off, ps_size;
  1079. u16 vseclen;
  1080. u8 image_state;
  1081. if (!(vsec = find_cxl_vsec(dev))) {
  1082. dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
  1083. return -ENODEV;
  1084. }
  1085. CXL_READ_VSEC_LENGTH(dev, vsec, &vseclen);
  1086. if (vseclen < CXL_VSEC_MIN_SIZE) {
  1087. dev_err(&dev->dev, "ABORTING: CXL VSEC too short\n");
  1088. return -EINVAL;
  1089. }
  1090. CXL_READ_VSEC_STATUS(dev, vsec, &adapter->vsec_status);
  1091. CXL_READ_VSEC_PSL_REVISION(dev, vsec, &adapter->psl_rev);
  1092. CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, &adapter->caia_major);
  1093. CXL_READ_VSEC_CAIA_MINOR(dev, vsec, &adapter->caia_minor);
  1094. CXL_READ_VSEC_BASE_IMAGE(dev, vsec, &adapter->base_image);
  1095. CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state);
  1096. adapter->user_image_loaded = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
  1097. adapter->perst_select_user = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
  1098. adapter->perst_loads_image = !!(image_state & CXL_VSEC_PERST_LOADS_IMAGE);
  1099. CXL_READ_VSEC_NAFUS(dev, vsec, &adapter->slices);
  1100. CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, &afu_desc_off);
  1101. CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, &afu_desc_size);
  1102. CXL_READ_VSEC_PS_OFF(dev, vsec, &ps_off);
  1103. CXL_READ_VSEC_PS_SIZE(dev, vsec, &ps_size);
  1104. /* Convert everything to bytes, because there is NO WAY I'd look at the
  1105. * code a month later and forget what units these are in ;-) */
  1106. adapter->native->ps_off = ps_off * 64 * 1024;
  1107. adapter->ps_size = ps_size * 64 * 1024;
  1108. adapter->native->afu_desc_off = afu_desc_off * 64 * 1024;
  1109. adapter->native->afu_desc_size = afu_desc_size * 64 * 1024;
  1110. /* Total IRQs - 1 PSL ERROR - #AFU*(1 slice error + 1 DSI) */
  1111. adapter->user_irqs = pnv_cxl_get_irq_count(dev) - 1 - 2*adapter->slices;
  1112. return 0;
  1113. }
  1114. /*
  1115. * Workaround a PCIe Host Bridge defect on some cards, that can cause
  1116. * malformed Transaction Layer Packet (TLP) errors to be erroneously
  1117. * reported. Mask this error in the Uncorrectable Error Mask Register.
  1118. *
  1119. * The upper nibble of the PSL revision is used to distinguish between
  1120. * different cards. The affected ones have it set to 0.
  1121. */
  1122. static void cxl_fixup_malformed_tlp(struct cxl *adapter, struct pci_dev *dev)
  1123. {
  1124. int aer;
  1125. u32 data;
  1126. if (adapter->psl_rev & 0xf000)
  1127. return;
  1128. if (!(aer = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)))
  1129. return;
  1130. pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, &data);
  1131. if (data & PCI_ERR_UNC_MALF_TLP)
  1132. if (data & PCI_ERR_UNC_INTN)
  1133. return;
  1134. data |= PCI_ERR_UNC_MALF_TLP;
  1135. data |= PCI_ERR_UNC_INTN;
  1136. pci_write_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, data);
  1137. }
  1138. static bool cxl_compatible_caia_version(struct cxl *adapter)
  1139. {
  1140. if (cxl_is_power8() && (adapter->caia_major == 1))
  1141. return true;
  1142. if (cxl_is_power9() && (adapter->caia_major == 2))
  1143. return true;
  1144. return false;
  1145. }
  1146. static int cxl_vsec_looks_ok(struct cxl *adapter, struct pci_dev *dev)
  1147. {
  1148. if (adapter->vsec_status & CXL_STATUS_SECOND_PORT)
  1149. return -EBUSY;
  1150. if (adapter->vsec_status & CXL_UNSUPPORTED_FEATURES) {
  1151. dev_err(&dev->dev, "ABORTING: CXL requires unsupported features\n");
  1152. return -EINVAL;
  1153. }
  1154. if (!cxl_compatible_caia_version(adapter)) {
  1155. dev_info(&dev->dev, "Ignoring card. PSL type is not supported (caia version: %d)\n",
  1156. adapter->caia_major);
  1157. return -ENODEV;
  1158. }
  1159. if (!adapter->slices) {
  1160. /* Once we support dynamic reprogramming we can use the card if
  1161. * it supports loadable AFUs */
  1162. dev_err(&dev->dev, "ABORTING: Device has no AFUs\n");
  1163. return -EINVAL;
  1164. }
  1165. if (!adapter->native->afu_desc_off || !adapter->native->afu_desc_size) {
  1166. dev_err(&dev->dev, "ABORTING: VSEC shows no AFU descriptors\n");
  1167. return -EINVAL;
  1168. }
  1169. if (adapter->ps_size > p2_size(dev) - adapter->native->ps_off) {
  1170. dev_err(&dev->dev, "ABORTING: Problem state size larger than "
  1171. "available in BAR2: 0x%llx > 0x%llx\n",
  1172. adapter->ps_size, p2_size(dev) - adapter->native->ps_off);
  1173. return -EINVAL;
  1174. }
  1175. return 0;
  1176. }
  1177. ssize_t cxl_pci_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len)
  1178. {
  1179. return pci_read_vpd(to_pci_dev(adapter->dev.parent), 0, len, buf);
  1180. }
  1181. static void cxl_release_adapter(struct device *dev)
  1182. {
  1183. struct cxl *adapter = to_cxl_adapter(dev);
  1184. pr_devel("cxl_release_adapter\n");
  1185. cxl_remove_adapter_nr(adapter);
  1186. kfree(adapter->native);
  1187. kfree(adapter);
  1188. }
  1189. #define CXL_PSL_ErrIVTE_tberror (0x1ull << (63-31))
  1190. static int sanitise_adapter_regs(struct cxl *adapter)
  1191. {
  1192. int rc = 0;
  1193. /* Clear PSL tberror bit by writing 1 to it */
  1194. cxl_p1_write(adapter, CXL_PSL_ErrIVTE, CXL_PSL_ErrIVTE_tberror);
  1195. if (adapter->native->sl_ops->invalidate_all) {
  1196. /* do not invalidate ERAT entries when not reloading on PERST */
  1197. if (cxl_is_power9() && (adapter->perst_loads_image))
  1198. return 0;
  1199. rc = adapter->native->sl_ops->invalidate_all(adapter);
  1200. }
  1201. return rc;
  1202. }
  1203. /* This should contain *only* operations that can safely be done in
  1204. * both creation and recovery.
  1205. */
  1206. static int cxl_configure_adapter(struct cxl *adapter, struct pci_dev *dev)
  1207. {
  1208. int rc;
  1209. adapter->dev.parent = &dev->dev;
  1210. adapter->dev.release = cxl_release_adapter;
  1211. pci_set_drvdata(dev, adapter);
  1212. rc = pci_enable_device(dev);
  1213. if (rc) {
  1214. dev_err(&dev->dev, "pci_enable_device failed: %i\n", rc);
  1215. return rc;
  1216. }
  1217. if ((rc = cxl_read_vsec(adapter, dev)))
  1218. return rc;
  1219. if ((rc = cxl_vsec_looks_ok(adapter, dev)))
  1220. return rc;
  1221. cxl_fixup_malformed_tlp(adapter, dev);
  1222. if ((rc = setup_cxl_bars(dev)))
  1223. return rc;
  1224. if ((rc = switch_card_to_cxl(dev)))
  1225. return rc;
  1226. if ((rc = cxl_update_image_control(adapter)))
  1227. return rc;
  1228. if ((rc = cxl_map_adapter_regs(adapter, dev)))
  1229. return rc;
  1230. if ((rc = sanitise_adapter_regs(adapter)))
  1231. goto err;
  1232. if ((rc = adapter->native->sl_ops->adapter_regs_init(adapter, dev)))
  1233. goto err;
  1234. /* Required for devices using CAPP DMA mode, harmless for others */
  1235. pci_set_master(dev);
  1236. adapter->tunneled_ops_supported = false;
  1237. if (cxl_is_power9()) {
  1238. if (pnv_pci_set_tunnel_bar(dev, 0x00020000E0000000ull, 1))
  1239. dev_info(&dev->dev, "Tunneled operations unsupported\n");
  1240. else
  1241. adapter->tunneled_ops_supported = true;
  1242. }
  1243. if ((rc = pnv_phb_to_cxl_mode(dev, adapter->native->sl_ops->capi_mode)))
  1244. goto err;
  1245. /* If recovery happened, the last step is to turn on snooping.
  1246. * In the non-recovery case this has no effect */
  1247. if ((rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_SNOOP_ON)))
  1248. goto err;
  1249. /* Ignore error, adapter init is not dependant on timebase sync */
  1250. cxl_setup_psl_timebase(adapter, dev);
  1251. if ((rc = cxl_native_register_psl_err_irq(adapter)))
  1252. goto err;
  1253. return 0;
  1254. err:
  1255. cxl_unmap_adapter_regs(adapter);
  1256. return rc;
  1257. }
  1258. static void cxl_deconfigure_adapter(struct cxl *adapter)
  1259. {
  1260. struct pci_dev *pdev = to_pci_dev(adapter->dev.parent);
  1261. if (cxl_is_power9())
  1262. pnv_pci_set_tunnel_bar(pdev, 0x00020000E0000000ull, 0);
  1263. cxl_native_release_psl_err_irq(adapter);
  1264. cxl_unmap_adapter_regs(adapter);
  1265. pci_disable_device(pdev);
  1266. }
  1267. static void cxl_stop_trace_psl9(struct cxl *adapter)
  1268. {
  1269. int traceid;
  1270. u64 trace_state, trace_mask;
  1271. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  1272. /* read each tracearray state and issue mmio to stop them is needed */
  1273. for (traceid = 0; traceid <= CXL_PSL9_TRACEID_MAX; ++traceid) {
  1274. trace_state = cxl_p1_read(adapter, CXL_PSL9_CTCCFG);
  1275. trace_mask = (0x3ULL << (62 - traceid * 2));
  1276. trace_state = (trace_state & trace_mask) >> (62 - traceid * 2);
  1277. dev_dbg(&dev->dev, "cxl: Traceid-%d trace_state=0x%0llX\n",
  1278. traceid, trace_state);
  1279. /* issue mmio if the trace array isn't in FIN state */
  1280. if (trace_state != CXL_PSL9_TRACESTATE_FIN)
  1281. cxl_p1_write(adapter, CXL_PSL9_TRACECFG,
  1282. 0x8400000000000000ULL | traceid);
  1283. }
  1284. }
  1285. static void cxl_stop_trace_psl8(struct cxl *adapter)
  1286. {
  1287. int slice;
  1288. /* Stop the trace */
  1289. cxl_p1_write(adapter, CXL_PSL_TRACE, 0x8000000000000017LL);
  1290. /* Stop the slice traces */
  1291. spin_lock(&adapter->afu_list_lock);
  1292. for (slice = 0; slice < adapter->slices; slice++) {
  1293. if (adapter->afu[slice])
  1294. cxl_p1n_write(adapter->afu[slice], CXL_PSL_SLICE_TRACE,
  1295. 0x8000000000000000LL);
  1296. }
  1297. spin_unlock(&adapter->afu_list_lock);
  1298. }
  1299. static const struct cxl_service_layer_ops psl9_ops = {
  1300. .adapter_regs_init = init_implementation_adapter_regs_psl9,
  1301. .invalidate_all = cxl_invalidate_all_psl9,
  1302. .afu_regs_init = init_implementation_afu_regs_psl9,
  1303. .sanitise_afu_regs = sanitise_afu_regs_psl9,
  1304. .register_serr_irq = cxl_native_register_serr_irq,
  1305. .release_serr_irq = cxl_native_release_serr_irq,
  1306. .handle_interrupt = cxl_irq_psl9,
  1307. .fail_irq = cxl_fail_irq_psl,
  1308. .activate_dedicated_process = cxl_activate_dedicated_process_psl9,
  1309. .attach_afu_directed = cxl_attach_afu_directed_psl9,
  1310. .attach_dedicated_process = cxl_attach_dedicated_process_psl9,
  1311. .update_dedicated_ivtes = cxl_update_dedicated_ivtes_psl9,
  1312. .debugfs_add_adapter_regs = cxl_debugfs_add_adapter_regs_psl9,
  1313. .debugfs_add_afu_regs = cxl_debugfs_add_afu_regs_psl9,
  1314. .psl_irq_dump_registers = cxl_native_irq_dump_regs_psl9,
  1315. .err_irq_dump_registers = cxl_native_err_irq_dump_regs_psl9,
  1316. .debugfs_stop_trace = cxl_stop_trace_psl9,
  1317. .timebase_read = timebase_read_psl9,
  1318. .capi_mode = OPAL_PHB_CAPI_MODE_CAPI,
  1319. .needs_reset_before_disable = true,
  1320. };
  1321. static const struct cxl_service_layer_ops psl8_ops = {
  1322. .adapter_regs_init = init_implementation_adapter_regs_psl8,
  1323. .invalidate_all = cxl_invalidate_all_psl8,
  1324. .afu_regs_init = init_implementation_afu_regs_psl8,
  1325. .sanitise_afu_regs = sanitise_afu_regs_psl8,
  1326. .register_serr_irq = cxl_native_register_serr_irq,
  1327. .release_serr_irq = cxl_native_release_serr_irq,
  1328. .handle_interrupt = cxl_irq_psl8,
  1329. .fail_irq = cxl_fail_irq_psl,
  1330. .activate_dedicated_process = cxl_activate_dedicated_process_psl8,
  1331. .attach_afu_directed = cxl_attach_afu_directed_psl8,
  1332. .attach_dedicated_process = cxl_attach_dedicated_process_psl8,
  1333. .update_dedicated_ivtes = cxl_update_dedicated_ivtes_psl8,
  1334. .debugfs_add_adapter_regs = cxl_debugfs_add_adapter_regs_psl8,
  1335. .debugfs_add_afu_regs = cxl_debugfs_add_afu_regs_psl8,
  1336. .psl_irq_dump_registers = cxl_native_irq_dump_regs_psl8,
  1337. .err_irq_dump_registers = cxl_native_err_irq_dump_regs_psl8,
  1338. .debugfs_stop_trace = cxl_stop_trace_psl8,
  1339. .write_timebase_ctrl = write_timebase_ctrl_psl8,
  1340. .timebase_read = timebase_read_psl8,
  1341. .capi_mode = OPAL_PHB_CAPI_MODE_CAPI,
  1342. .needs_reset_before_disable = true,
  1343. };
  1344. static void set_sl_ops(struct cxl *adapter, struct pci_dev *dev)
  1345. {
  1346. if (cxl_is_power8()) {
  1347. dev_info(&dev->dev, "Device uses a PSL8\n");
  1348. adapter->native->sl_ops = &psl8_ops;
  1349. } else {
  1350. dev_info(&dev->dev, "Device uses a PSL9\n");
  1351. adapter->native->sl_ops = &psl9_ops;
  1352. }
  1353. }
  1354. static struct cxl *cxl_pci_init_adapter(struct pci_dev *dev)
  1355. {
  1356. struct cxl *adapter;
  1357. int rc;
  1358. adapter = cxl_alloc_adapter();
  1359. if (!adapter)
  1360. return ERR_PTR(-ENOMEM);
  1361. adapter->native = kzalloc(sizeof(struct cxl_native), GFP_KERNEL);
  1362. if (!adapter->native) {
  1363. rc = -ENOMEM;
  1364. goto err_release;
  1365. }
  1366. set_sl_ops(adapter, dev);
  1367. /* Set defaults for parameters which need to persist over
  1368. * configure/reconfigure
  1369. */
  1370. adapter->perst_loads_image = true;
  1371. adapter->perst_same_image = false;
  1372. rc = cxl_configure_adapter(adapter, dev);
  1373. if (rc) {
  1374. pci_disable_device(dev);
  1375. goto err_release;
  1376. }
  1377. /* Don't care if this one fails: */
  1378. cxl_debugfs_adapter_add(adapter);
  1379. /*
  1380. * After we call this function we must not free the adapter directly,
  1381. * even if it returns an error!
  1382. */
  1383. if ((rc = cxl_register_adapter(adapter)))
  1384. goto err_put1;
  1385. if ((rc = cxl_sysfs_adapter_add(adapter)))
  1386. goto err_put1;
  1387. /* Release the context lock as adapter is configured */
  1388. cxl_adapter_context_unlock(adapter);
  1389. return adapter;
  1390. err_put1:
  1391. /* This should mirror cxl_remove_adapter, except without the
  1392. * sysfs parts
  1393. */
  1394. cxl_debugfs_adapter_remove(adapter);
  1395. cxl_deconfigure_adapter(adapter);
  1396. device_unregister(&adapter->dev);
  1397. return ERR_PTR(rc);
  1398. err_release:
  1399. cxl_release_adapter(&adapter->dev);
  1400. return ERR_PTR(rc);
  1401. }
  1402. static void cxl_pci_remove_adapter(struct cxl *adapter)
  1403. {
  1404. pr_devel("cxl_remove_adapter\n");
  1405. cxl_sysfs_adapter_remove(adapter);
  1406. cxl_debugfs_adapter_remove(adapter);
  1407. /*
  1408. * Flush adapter datacache as its about to be removed.
  1409. */
  1410. cxl_data_cache_flush(adapter);
  1411. cxl_deconfigure_adapter(adapter);
  1412. device_unregister(&adapter->dev);
  1413. }
  1414. #define CXL_MAX_PCIEX_PARENT 2
  1415. int cxl_slot_is_switched(struct pci_dev *dev)
  1416. {
  1417. struct device_node *np;
  1418. int depth = 0;
  1419. const __be32 *prop;
  1420. if (!(np = pci_device_to_OF_node(dev))) {
  1421. pr_err("cxl: np = NULL\n");
  1422. return -ENODEV;
  1423. }
  1424. of_node_get(np);
  1425. while (np) {
  1426. np = of_get_next_parent(np);
  1427. prop = of_get_property(np, "device_type", NULL);
  1428. if (!prop || strcmp((char *)prop, "pciex"))
  1429. break;
  1430. depth++;
  1431. }
  1432. of_node_put(np);
  1433. return (depth > CXL_MAX_PCIEX_PARENT);
  1434. }
  1435. static int cxl_probe(struct pci_dev *dev, const struct pci_device_id *id)
  1436. {
  1437. struct cxl *adapter;
  1438. int slice;
  1439. int rc;
  1440. if (cxl_pci_is_vphb_device(dev)) {
  1441. dev_dbg(&dev->dev, "cxl_init_adapter: Ignoring cxl vphb device\n");
  1442. return -ENODEV;
  1443. }
  1444. if (cxl_slot_is_switched(dev)) {
  1445. dev_info(&dev->dev, "Ignoring card on incompatible PCI slot\n");
  1446. return -ENODEV;
  1447. }
  1448. if (cxl_is_power9() && !radix_enabled()) {
  1449. dev_info(&dev->dev, "Only Radix mode supported\n");
  1450. return -ENODEV;
  1451. }
  1452. if (cxl_verbose)
  1453. dump_cxl_config_space(dev);
  1454. adapter = cxl_pci_init_adapter(dev);
  1455. if (IS_ERR(adapter)) {
  1456. dev_err(&dev->dev, "cxl_init_adapter failed: %li\n", PTR_ERR(adapter));
  1457. return PTR_ERR(adapter);
  1458. }
  1459. for (slice = 0; slice < adapter->slices; slice++) {
  1460. if ((rc = pci_init_afu(adapter, slice, dev))) {
  1461. dev_err(&dev->dev, "AFU %i failed to initialise: %i\n", slice, rc);
  1462. continue;
  1463. }
  1464. rc = cxl_afu_select_best_mode(adapter->afu[slice]);
  1465. if (rc)
  1466. dev_err(&dev->dev, "AFU %i failed to start: %i\n", slice, rc);
  1467. }
  1468. return 0;
  1469. }
  1470. static void cxl_remove(struct pci_dev *dev)
  1471. {
  1472. struct cxl *adapter = pci_get_drvdata(dev);
  1473. struct cxl_afu *afu;
  1474. int i;
  1475. /*
  1476. * Lock to prevent someone grabbing a ref through the adapter list as
  1477. * we are removing it
  1478. */
  1479. for (i = 0; i < adapter->slices; i++) {
  1480. afu = adapter->afu[i];
  1481. cxl_pci_remove_afu(afu);
  1482. }
  1483. cxl_pci_remove_adapter(adapter);
  1484. }
  1485. static pci_ers_result_t cxl_vphb_error_detected(struct cxl_afu *afu,
  1486. pci_channel_state_t state)
  1487. {
  1488. struct pci_dev *afu_dev;
  1489. pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
  1490. pci_ers_result_t afu_result = PCI_ERS_RESULT_NEED_RESET;
  1491. /* There should only be one entry, but go through the list
  1492. * anyway
  1493. */
  1494. if (afu == NULL || afu->phb == NULL)
  1495. return result;
  1496. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  1497. if (!afu_dev->driver)
  1498. continue;
  1499. afu_dev->error_state = state;
  1500. if (afu_dev->driver->err_handler)
  1501. afu_result = afu_dev->driver->err_handler->error_detected(afu_dev,
  1502. state);
  1503. /* Disconnect trumps all, NONE trumps NEED_RESET */
  1504. if (afu_result == PCI_ERS_RESULT_DISCONNECT)
  1505. result = PCI_ERS_RESULT_DISCONNECT;
  1506. else if ((afu_result == PCI_ERS_RESULT_NONE) &&
  1507. (result == PCI_ERS_RESULT_NEED_RESET))
  1508. result = PCI_ERS_RESULT_NONE;
  1509. }
  1510. return result;
  1511. }
  1512. static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
  1513. pci_channel_state_t state)
  1514. {
  1515. struct cxl *adapter = pci_get_drvdata(pdev);
  1516. struct cxl_afu *afu;
  1517. pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
  1518. pci_ers_result_t afu_result = PCI_ERS_RESULT_NEED_RESET;
  1519. int i;
  1520. /* At this point, we could still have an interrupt pending.
  1521. * Let's try to get them out of the way before they do
  1522. * anything we don't like.
  1523. */
  1524. schedule();
  1525. /* If we're permanently dead, give up. */
  1526. if (state == pci_channel_io_perm_failure) {
  1527. spin_lock(&adapter->afu_list_lock);
  1528. for (i = 0; i < adapter->slices; i++) {
  1529. afu = adapter->afu[i];
  1530. /*
  1531. * Tell the AFU drivers; but we don't care what they
  1532. * say, we're going away.
  1533. */
  1534. cxl_vphb_error_detected(afu, state);
  1535. }
  1536. spin_unlock(&adapter->afu_list_lock);
  1537. return PCI_ERS_RESULT_DISCONNECT;
  1538. }
  1539. /* Are we reflashing?
  1540. *
  1541. * If we reflash, we could come back as something entirely
  1542. * different, including a non-CAPI card. As such, by default
  1543. * we don't participate in the process. We'll be unbound and
  1544. * the slot re-probed. (TODO: check EEH doesn't blindly rebind
  1545. * us!)
  1546. *
  1547. * However, this isn't the entire story: for reliablity
  1548. * reasons, we usually want to reflash the FPGA on PERST in
  1549. * order to get back to a more reliable known-good state.
  1550. *
  1551. * This causes us a bit of a problem: if we reflash we can't
  1552. * trust that we'll come back the same - we could have a new
  1553. * image and been PERSTed in order to load that
  1554. * image. However, most of the time we actually *will* come
  1555. * back the same - for example a regular EEH event.
  1556. *
  1557. * Therefore, we allow the user to assert that the image is
  1558. * indeed the same and that we should continue on into EEH
  1559. * anyway.
  1560. */
  1561. if (adapter->perst_loads_image && !adapter->perst_same_image) {
  1562. /* TODO take the PHB out of CXL mode */
  1563. dev_info(&pdev->dev, "reflashing, so opting out of EEH!\n");
  1564. return PCI_ERS_RESULT_NONE;
  1565. }
  1566. /*
  1567. * At this point, we want to try to recover. We'll always
  1568. * need a complete slot reset: we don't trust any other reset.
  1569. *
  1570. * Now, we go through each AFU:
  1571. * - We send the driver, if bound, an error_detected callback.
  1572. * We expect it to clean up, but it can also tell us to give
  1573. * up and permanently detach the card. To simplify things, if
  1574. * any bound AFU driver doesn't support EEH, we give up on EEH.
  1575. *
  1576. * - We detach all contexts associated with the AFU. This
  1577. * does not free them, but puts them into a CLOSED state
  1578. * which causes any the associated files to return useful
  1579. * errors to userland. It also unmaps, but does not free,
  1580. * any IRQs.
  1581. *
  1582. * - We clean up our side: releasing and unmapping resources we hold
  1583. * so we can wire them up again when the hardware comes back up.
  1584. *
  1585. * Driver authors should note:
  1586. *
  1587. * - Any contexts you create in your kernel driver (except
  1588. * those associated with anonymous file descriptors) are
  1589. * your responsibility to free and recreate. Likewise with
  1590. * any attached resources.
  1591. *
  1592. * - We will take responsibility for re-initialising the
  1593. * device context (the one set up for you in
  1594. * cxl_pci_enable_device_hook and accessed through
  1595. * cxl_get_context). If you've attached IRQs or other
  1596. * resources to it, they remains yours to free.
  1597. *
  1598. * You can call the same functions to release resources as you
  1599. * normally would: we make sure that these functions continue
  1600. * to work when the hardware is down.
  1601. *
  1602. * Two examples:
  1603. *
  1604. * 1) If you normally free all your resources at the end of
  1605. * each request, or if you use anonymous FDs, your
  1606. * error_detected callback can simply set a flag to tell
  1607. * your driver not to start any new calls. You can then
  1608. * clear the flag in the resume callback.
  1609. *
  1610. * 2) If you normally allocate your resources on startup:
  1611. * * Set a flag in error_detected as above.
  1612. * * Let CXL detach your contexts.
  1613. * * In slot_reset, free the old resources and allocate new ones.
  1614. * * In resume, clear the flag to allow things to start.
  1615. */
  1616. /* Make sure no one else changes the afu list */
  1617. spin_lock(&adapter->afu_list_lock);
  1618. for (i = 0; i < adapter->slices; i++) {
  1619. afu = adapter->afu[i];
  1620. if (afu == NULL)
  1621. continue;
  1622. afu_result = cxl_vphb_error_detected(afu, state);
  1623. cxl_context_detach_all(afu);
  1624. cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
  1625. pci_deconfigure_afu(afu);
  1626. /* Disconnect trumps all, NONE trumps NEED_RESET */
  1627. if (afu_result == PCI_ERS_RESULT_DISCONNECT)
  1628. result = PCI_ERS_RESULT_DISCONNECT;
  1629. else if ((afu_result == PCI_ERS_RESULT_NONE) &&
  1630. (result == PCI_ERS_RESULT_NEED_RESET))
  1631. result = PCI_ERS_RESULT_NONE;
  1632. }
  1633. spin_unlock(&adapter->afu_list_lock);
  1634. /* should take the context lock here */
  1635. if (cxl_adapter_context_lock(adapter) != 0)
  1636. dev_warn(&adapter->dev,
  1637. "Couldn't take context lock with %d active-contexts\n",
  1638. atomic_read(&adapter->contexts_num));
  1639. cxl_deconfigure_adapter(adapter);
  1640. return result;
  1641. }
  1642. static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
  1643. {
  1644. struct cxl *adapter = pci_get_drvdata(pdev);
  1645. struct cxl_afu *afu;
  1646. struct cxl_context *ctx;
  1647. struct pci_dev *afu_dev;
  1648. pci_ers_result_t afu_result = PCI_ERS_RESULT_RECOVERED;
  1649. pci_ers_result_t result = PCI_ERS_RESULT_RECOVERED;
  1650. int i;
  1651. if (cxl_configure_adapter(adapter, pdev))
  1652. goto err;
  1653. /*
  1654. * Unlock context activation for the adapter. Ideally this should be
  1655. * done in cxl_pci_resume but cxlflash module tries to activate the
  1656. * master context as part of slot_reset callback.
  1657. */
  1658. cxl_adapter_context_unlock(adapter);
  1659. spin_lock(&adapter->afu_list_lock);
  1660. for (i = 0; i < adapter->slices; i++) {
  1661. afu = adapter->afu[i];
  1662. if (afu == NULL)
  1663. continue;
  1664. if (pci_configure_afu(afu, adapter, pdev))
  1665. goto err_unlock;
  1666. if (cxl_afu_select_best_mode(afu))
  1667. goto err_unlock;
  1668. if (afu->phb == NULL)
  1669. continue;
  1670. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  1671. /* Reset the device context.
  1672. * TODO: make this less disruptive
  1673. */
  1674. ctx = cxl_get_context(afu_dev);
  1675. if (ctx && cxl_release_context(ctx))
  1676. goto err_unlock;
  1677. ctx = cxl_dev_context_init(afu_dev);
  1678. if (IS_ERR(ctx))
  1679. goto err_unlock;
  1680. afu_dev->dev.archdata.cxl_ctx = ctx;
  1681. if (cxl_ops->afu_check_and_enable(afu))
  1682. goto err_unlock;
  1683. afu_dev->error_state = pci_channel_io_normal;
  1684. /* If there's a driver attached, allow it to
  1685. * chime in on recovery. Drivers should check
  1686. * if everything has come back OK, but
  1687. * shouldn't start new work until we call
  1688. * their resume function.
  1689. */
  1690. if (!afu_dev->driver)
  1691. continue;
  1692. if (afu_dev->driver->err_handler &&
  1693. afu_dev->driver->err_handler->slot_reset)
  1694. afu_result = afu_dev->driver->err_handler->slot_reset(afu_dev);
  1695. if (afu_result == PCI_ERS_RESULT_DISCONNECT)
  1696. result = PCI_ERS_RESULT_DISCONNECT;
  1697. }
  1698. }
  1699. spin_unlock(&adapter->afu_list_lock);
  1700. return result;
  1701. err_unlock:
  1702. spin_unlock(&adapter->afu_list_lock);
  1703. err:
  1704. /* All the bits that happen in both error_detected and cxl_remove
  1705. * should be idempotent, so we don't need to worry about leaving a mix
  1706. * of unconfigured and reconfigured resources.
  1707. */
  1708. dev_err(&pdev->dev, "EEH recovery failed. Asking to be disconnected.\n");
  1709. return PCI_ERS_RESULT_DISCONNECT;
  1710. }
  1711. static void cxl_pci_resume(struct pci_dev *pdev)
  1712. {
  1713. struct cxl *adapter = pci_get_drvdata(pdev);
  1714. struct cxl_afu *afu;
  1715. struct pci_dev *afu_dev;
  1716. int i;
  1717. /* Everything is back now. Drivers should restart work now.
  1718. * This is not the place to be checking if everything came back up
  1719. * properly, because there's no return value: do that in slot_reset.
  1720. */
  1721. spin_lock(&adapter->afu_list_lock);
  1722. for (i = 0; i < adapter->slices; i++) {
  1723. afu = adapter->afu[i];
  1724. if (afu == NULL || afu->phb == NULL)
  1725. continue;
  1726. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  1727. if (afu_dev->driver && afu_dev->driver->err_handler &&
  1728. afu_dev->driver->err_handler->resume)
  1729. afu_dev->driver->err_handler->resume(afu_dev);
  1730. }
  1731. }
  1732. spin_unlock(&adapter->afu_list_lock);
  1733. }
  1734. static const struct pci_error_handlers cxl_err_handler = {
  1735. .error_detected = cxl_pci_error_detected,
  1736. .slot_reset = cxl_pci_slot_reset,
  1737. .resume = cxl_pci_resume,
  1738. };
  1739. struct pci_driver cxl_pci_driver = {
  1740. .name = "cxl-pci",
  1741. .id_table = cxl_pci_tbl,
  1742. .probe = cxl_probe,
  1743. .remove = cxl_remove,
  1744. .shutdown = cxl_remove,
  1745. .err_handler = &cxl_err_handler,
  1746. };