pci.c 57 KB

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