pci.c 50 KB

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