pci.c 66 KB

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