pci.c 67 KB

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