pci.c 59 KB

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