pci.c 44 KB

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