pci.c 65 KB

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