pci.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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/pci-bridge.h> /* for struct pci_controller */
  22. #include <asm/pnv-pci.h>
  23. #include "cxl.h"
  24. #define CXL_PCI_VSEC_ID 0x1280
  25. #define CXL_VSEC_MIN_SIZE 0x80
  26. #define CXL_READ_VSEC_LENGTH(dev, vsec, dest) \
  27. { \
  28. pci_read_config_word(dev, vsec + 0x6, dest); \
  29. *dest >>= 4; \
  30. }
  31. #define CXL_READ_VSEC_NAFUS(dev, vsec, dest) \
  32. pci_read_config_byte(dev, vsec + 0x8, dest)
  33. #define CXL_READ_VSEC_STATUS(dev, vsec, dest) \
  34. pci_read_config_byte(dev, vsec + 0x9, dest)
  35. #define CXL_STATUS_SECOND_PORT 0x80
  36. #define CXL_STATUS_MSI_X_FULL 0x40
  37. #define CXL_STATUS_MSI_X_SINGLE 0x20
  38. #define CXL_STATUS_FLASH_RW 0x08
  39. #define CXL_STATUS_FLASH_RO 0x04
  40. #define CXL_STATUS_LOADABLE_AFU 0x02
  41. #define CXL_STATUS_LOADABLE_PSL 0x01
  42. /* If we see these features we won't try to use the card */
  43. #define CXL_UNSUPPORTED_FEATURES \
  44. (CXL_STATUS_MSI_X_FULL | CXL_STATUS_MSI_X_SINGLE)
  45. #define CXL_READ_VSEC_MODE_CONTROL(dev, vsec, dest) \
  46. pci_read_config_byte(dev, vsec + 0xa, dest)
  47. #define CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val) \
  48. pci_write_config_byte(dev, vsec + 0xa, val)
  49. #define CXL_VSEC_PROTOCOL_MASK 0xe0
  50. #define CXL_VSEC_PROTOCOL_1024TB 0x80
  51. #define CXL_VSEC_PROTOCOL_512TB 0x40
  52. #define CXL_VSEC_PROTOCOL_256TB 0x20 /* Power 8 uses this */
  53. #define CXL_VSEC_PROTOCOL_ENABLE 0x01
  54. #define CXL_READ_VSEC_PSL_REVISION(dev, vsec, dest) \
  55. pci_read_config_word(dev, vsec + 0xc, dest)
  56. #define CXL_READ_VSEC_CAIA_MINOR(dev, vsec, dest) \
  57. pci_read_config_byte(dev, vsec + 0xe, dest)
  58. #define CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, dest) \
  59. pci_read_config_byte(dev, vsec + 0xf, dest)
  60. #define CXL_READ_VSEC_BASE_IMAGE(dev, vsec, dest) \
  61. pci_read_config_word(dev, vsec + 0x10, dest)
  62. #define CXL_READ_VSEC_IMAGE_STATE(dev, vsec, dest) \
  63. pci_read_config_byte(dev, vsec + 0x13, dest)
  64. #define CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, val) \
  65. pci_write_config_byte(dev, vsec + 0x13, val)
  66. #define CXL_VSEC_USER_IMAGE_LOADED 0x80 /* RO */
  67. #define CXL_VSEC_PERST_LOADS_IMAGE 0x20 /* RW */
  68. #define CXL_VSEC_PERST_SELECT_USER 0x10 /* RW */
  69. #define CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, dest) \
  70. pci_read_config_dword(dev, vsec + 0x20, dest)
  71. #define CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, dest) \
  72. pci_read_config_dword(dev, vsec + 0x24, dest)
  73. #define CXL_READ_VSEC_PS_OFF(dev, vsec, dest) \
  74. pci_read_config_dword(dev, vsec + 0x28, dest)
  75. #define CXL_READ_VSEC_PS_SIZE(dev, vsec, dest) \
  76. pci_read_config_dword(dev, vsec + 0x2c, dest)
  77. /* This works a little different than the p1/p2 register accesses to make it
  78. * easier to pull out individual fields */
  79. #define AFUD_READ(afu, off) in_be64(afu->afu_desc_mmio + off)
  80. #define EXTRACT_PPC_BIT(val, bit) (!!(val & PPC_BIT(bit)))
  81. #define EXTRACT_PPC_BITS(val, bs, be) ((val & PPC_BITMASK(bs, be)) >> PPC_BITLSHIFT(be))
  82. #define AFUD_READ_INFO(afu) AFUD_READ(afu, 0x0)
  83. #define AFUD_NUM_INTS_PER_PROC(val) EXTRACT_PPC_BITS(val, 0, 15)
  84. #define AFUD_NUM_PROCS(val) EXTRACT_PPC_BITS(val, 16, 31)
  85. #define AFUD_NUM_CRS(val) EXTRACT_PPC_BITS(val, 32, 47)
  86. #define AFUD_MULTIMODE(val) EXTRACT_PPC_BIT(val, 48)
  87. #define AFUD_PUSH_BLOCK_TRANSFER(val) EXTRACT_PPC_BIT(val, 55)
  88. #define AFUD_DEDICATED_PROCESS(val) EXTRACT_PPC_BIT(val, 59)
  89. #define AFUD_AFU_DIRECTED(val) EXTRACT_PPC_BIT(val, 61)
  90. #define AFUD_TIME_SLICED(val) EXTRACT_PPC_BIT(val, 63)
  91. #define AFUD_READ_CR(afu) AFUD_READ(afu, 0x20)
  92. #define AFUD_CR_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
  93. #define AFUD_READ_CR_OFF(afu) AFUD_READ(afu, 0x28)
  94. #define AFUD_READ_PPPSA(afu) AFUD_READ(afu, 0x30)
  95. #define AFUD_PPPSA_PP(val) EXTRACT_PPC_BIT(val, 6)
  96. #define AFUD_PPPSA_PSA(val) EXTRACT_PPC_BIT(val, 7)
  97. #define AFUD_PPPSA_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
  98. #define AFUD_READ_PPPSA_OFF(afu) AFUD_READ(afu, 0x38)
  99. #define AFUD_READ_EB(afu) AFUD_READ(afu, 0x40)
  100. #define AFUD_EB_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
  101. #define AFUD_READ_EB_OFF(afu) AFUD_READ(afu, 0x48)
  102. static DEFINE_PCI_DEVICE_TABLE(cxl_pci_tbl) = {
  103. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0477), },
  104. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x044b), },
  105. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x04cf), },
  106. { PCI_DEVICE_CLASS(0x120000, ~0), },
  107. { }
  108. };
  109. MODULE_DEVICE_TABLE(pci, cxl_pci_tbl);
  110. /*
  111. * Mostly using these wrappers to avoid confusion:
  112. * priv 1 is BAR2, while priv 2 is BAR0
  113. */
  114. static inline resource_size_t p1_base(struct pci_dev *dev)
  115. {
  116. return pci_resource_start(dev, 2);
  117. }
  118. static inline resource_size_t p1_size(struct pci_dev *dev)
  119. {
  120. return pci_resource_len(dev, 2);
  121. }
  122. static inline resource_size_t p2_base(struct pci_dev *dev)
  123. {
  124. return pci_resource_start(dev, 0);
  125. }
  126. static inline resource_size_t p2_size(struct pci_dev *dev)
  127. {
  128. return pci_resource_len(dev, 0);
  129. }
  130. static int find_cxl_vsec(struct pci_dev *dev)
  131. {
  132. int vsec = 0;
  133. u16 val;
  134. while ((vsec = pci_find_next_ext_capability(dev, vsec, PCI_EXT_CAP_ID_VNDR))) {
  135. pci_read_config_word(dev, vsec + 0x4, &val);
  136. if (val == CXL_PCI_VSEC_ID)
  137. return vsec;
  138. }
  139. return 0;
  140. }
  141. static void dump_cxl_config_space(struct pci_dev *dev)
  142. {
  143. int vsec;
  144. u32 val;
  145. dev_info(&dev->dev, "dump_cxl_config_space\n");
  146. pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &val);
  147. dev_info(&dev->dev, "BAR0: %#.8x\n", val);
  148. pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &val);
  149. dev_info(&dev->dev, "BAR1: %#.8x\n", val);
  150. pci_read_config_dword(dev, PCI_BASE_ADDRESS_2, &val);
  151. dev_info(&dev->dev, "BAR2: %#.8x\n", val);
  152. pci_read_config_dword(dev, PCI_BASE_ADDRESS_3, &val);
  153. dev_info(&dev->dev, "BAR3: %#.8x\n", val);
  154. pci_read_config_dword(dev, PCI_BASE_ADDRESS_4, &val);
  155. dev_info(&dev->dev, "BAR4: %#.8x\n", val);
  156. pci_read_config_dword(dev, PCI_BASE_ADDRESS_5, &val);
  157. dev_info(&dev->dev, "BAR5: %#.8x\n", val);
  158. dev_info(&dev->dev, "p1 regs: %#llx, len: %#llx\n",
  159. p1_base(dev), p1_size(dev));
  160. dev_info(&dev->dev, "p2 regs: %#llx, len: %#llx\n",
  161. p1_base(dev), p2_size(dev));
  162. dev_info(&dev->dev, "BAR 4/5: %#llx, len: %#llx\n",
  163. pci_resource_start(dev, 4), pci_resource_len(dev, 4));
  164. if (!(vsec = find_cxl_vsec(dev)))
  165. return;
  166. #define show_reg(name, what) \
  167. dev_info(&dev->dev, "cxl vsec: %30s: %#x\n", name, what)
  168. pci_read_config_dword(dev, vsec + 0x0, &val);
  169. show_reg("Cap ID", (val >> 0) & 0xffff);
  170. show_reg("Cap Ver", (val >> 16) & 0xf);
  171. show_reg("Next Cap Ptr", (val >> 20) & 0xfff);
  172. pci_read_config_dword(dev, vsec + 0x4, &val);
  173. show_reg("VSEC ID", (val >> 0) & 0xffff);
  174. show_reg("VSEC Rev", (val >> 16) & 0xf);
  175. show_reg("VSEC Length", (val >> 20) & 0xfff);
  176. pci_read_config_dword(dev, vsec + 0x8, &val);
  177. show_reg("Num AFUs", (val >> 0) & 0xff);
  178. show_reg("Status", (val >> 8) & 0xff);
  179. show_reg("Mode Control", (val >> 16) & 0xff);
  180. show_reg("Reserved", (val >> 24) & 0xff);
  181. pci_read_config_dword(dev, vsec + 0xc, &val);
  182. show_reg("PSL Rev", (val >> 0) & 0xffff);
  183. show_reg("CAIA Ver", (val >> 16) & 0xffff);
  184. pci_read_config_dword(dev, vsec + 0x10, &val);
  185. show_reg("Base Image Rev", (val >> 0) & 0xffff);
  186. show_reg("Reserved", (val >> 16) & 0x0fff);
  187. show_reg("Image Control", (val >> 28) & 0x3);
  188. show_reg("Reserved", (val >> 30) & 0x1);
  189. show_reg("Image Loaded", (val >> 31) & 0x1);
  190. pci_read_config_dword(dev, vsec + 0x14, &val);
  191. show_reg("Reserved", val);
  192. pci_read_config_dword(dev, vsec + 0x18, &val);
  193. show_reg("Reserved", val);
  194. pci_read_config_dword(dev, vsec + 0x1c, &val);
  195. show_reg("Reserved", val);
  196. pci_read_config_dword(dev, vsec + 0x20, &val);
  197. show_reg("AFU Descriptor Offset", val);
  198. pci_read_config_dword(dev, vsec + 0x24, &val);
  199. show_reg("AFU Descriptor Size", val);
  200. pci_read_config_dword(dev, vsec + 0x28, &val);
  201. show_reg("Problem State Offset", val);
  202. pci_read_config_dword(dev, vsec + 0x2c, &val);
  203. show_reg("Problem State Size", val);
  204. pci_read_config_dword(dev, vsec + 0x30, &val);
  205. show_reg("Reserved", val);
  206. pci_read_config_dword(dev, vsec + 0x34, &val);
  207. show_reg("Reserved", val);
  208. pci_read_config_dword(dev, vsec + 0x38, &val);
  209. show_reg("Reserved", val);
  210. pci_read_config_dword(dev, vsec + 0x3c, &val);
  211. show_reg("Reserved", val);
  212. pci_read_config_dword(dev, vsec + 0x40, &val);
  213. show_reg("PSL Programming Port", val);
  214. pci_read_config_dword(dev, vsec + 0x44, &val);
  215. show_reg("PSL Programming Control", val);
  216. pci_read_config_dword(dev, vsec + 0x48, &val);
  217. show_reg("Reserved", val);
  218. pci_read_config_dword(dev, vsec + 0x4c, &val);
  219. show_reg("Reserved", val);
  220. pci_read_config_dword(dev, vsec + 0x50, &val);
  221. show_reg("Flash Address Register", val);
  222. pci_read_config_dword(dev, vsec + 0x54, &val);
  223. show_reg("Flash Size Register", val);
  224. pci_read_config_dword(dev, vsec + 0x58, &val);
  225. show_reg("Flash Status/Control Register", val);
  226. pci_read_config_dword(dev, vsec + 0x58, &val);
  227. show_reg("Flash Data Port", val);
  228. #undef show_reg
  229. }
  230. static void dump_afu_descriptor(struct cxl_afu *afu)
  231. {
  232. u64 val;
  233. #define show_reg(name, what) \
  234. dev_info(&afu->dev, "afu desc: %30s: %#llx\n", name, what)
  235. val = AFUD_READ_INFO(afu);
  236. show_reg("num_ints_per_process", AFUD_NUM_INTS_PER_PROC(val));
  237. show_reg("num_of_processes", AFUD_NUM_PROCS(val));
  238. show_reg("num_of_afu_CRs", AFUD_NUM_CRS(val));
  239. show_reg("req_prog_mode", val & 0xffffULL);
  240. val = AFUD_READ(afu, 0x8);
  241. show_reg("Reserved", val);
  242. val = AFUD_READ(afu, 0x10);
  243. show_reg("Reserved", val);
  244. val = AFUD_READ(afu, 0x18);
  245. show_reg("Reserved", val);
  246. val = AFUD_READ_CR(afu);
  247. show_reg("Reserved", (val >> (63-7)) & 0xff);
  248. show_reg("AFU_CR_len", AFUD_CR_LEN(val));
  249. val = AFUD_READ_CR_OFF(afu);
  250. show_reg("AFU_CR_offset", val);
  251. val = AFUD_READ_PPPSA(afu);
  252. show_reg("PerProcessPSA_control", (val >> (63-7)) & 0xff);
  253. show_reg("PerProcessPSA Length", AFUD_PPPSA_LEN(val));
  254. val = AFUD_READ_PPPSA_OFF(afu);
  255. show_reg("PerProcessPSA_offset", val);
  256. val = AFUD_READ_EB(afu);
  257. show_reg("Reserved", (val >> (63-7)) & 0xff);
  258. show_reg("AFU_EB_len", AFUD_EB_LEN(val));
  259. val = AFUD_READ_EB_OFF(afu);
  260. show_reg("AFU_EB_offset", val);
  261. #undef show_reg
  262. }
  263. static int init_implementation_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
  264. {
  265. struct device_node *np;
  266. const __be32 *prop;
  267. u64 psl_dsnctl;
  268. u64 chipid;
  269. if (!(np = pnv_pci_to_phb_node(dev)))
  270. return -ENODEV;
  271. while (np && !(prop = of_get_property(np, "ibm,chip-id", NULL)))
  272. np = of_get_next_parent(np);
  273. if (!np)
  274. return -ENODEV;
  275. chipid = be32_to_cpup(prop);
  276. of_node_put(np);
  277. /* Tell PSL where to route data to */
  278. psl_dsnctl = 0x02E8900002000000ULL | (chipid << (63-5));
  279. cxl_p1_write(adapter, CXL_PSL_DSNDCTL, psl_dsnctl);
  280. cxl_p1_write(adapter, CXL_PSL_RESLCKTO, 0x20000000200ULL);
  281. /* snoop write mask */
  282. cxl_p1_write(adapter, CXL_PSL_SNWRALLOC, 0x00000000FFFFFFFFULL);
  283. /* set fir_accum */
  284. cxl_p1_write(adapter, CXL_PSL_FIR_CNTL, 0x0800000000000000ULL);
  285. /* for debugging with trace arrays */
  286. cxl_p1_write(adapter, CXL_PSL_TRACE, 0x0000FF7C00000000ULL);
  287. return 0;
  288. }
  289. static int init_implementation_afu_regs(struct cxl_afu *afu)
  290. {
  291. /* read/write masks for this slice */
  292. cxl_p1n_write(afu, CXL_PSL_APCALLOC_A, 0xFFFFFFFEFEFEFEFEULL);
  293. /* APC read/write masks for this slice */
  294. cxl_p1n_write(afu, CXL_PSL_COALLOC_A, 0xFF000000FEFEFEFEULL);
  295. /* for debugging with trace arrays */
  296. cxl_p1n_write(afu, CXL_PSL_SLICE_TRACE, 0x0000FFFF00000000ULL);
  297. cxl_p1n_write(afu, CXL_PSL_RXCTL_A, 0xF000000000000000ULL);
  298. return 0;
  299. }
  300. int cxl_setup_irq(struct cxl *adapter, unsigned int hwirq,
  301. unsigned int virq)
  302. {
  303. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  304. return pnv_cxl_ioda_msi_setup(dev, hwirq, virq);
  305. }
  306. int cxl_alloc_one_irq(struct cxl *adapter)
  307. {
  308. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  309. return pnv_cxl_alloc_hwirqs(dev, 1);
  310. }
  311. void cxl_release_one_irq(struct cxl *adapter, int hwirq)
  312. {
  313. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  314. return pnv_cxl_release_hwirqs(dev, hwirq, 1);
  315. }
  316. int cxl_alloc_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter, unsigned int num)
  317. {
  318. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  319. return pnv_cxl_alloc_hwirq_ranges(irqs, dev, num);
  320. }
  321. void cxl_release_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter)
  322. {
  323. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  324. pnv_cxl_release_hwirq_ranges(irqs, dev);
  325. }
  326. static int setup_cxl_bars(struct pci_dev *dev)
  327. {
  328. /* Safety check in case we get backported to < 3.17 without M64 */
  329. if ((p1_base(dev) < 0x100000000ULL) ||
  330. (p2_base(dev) < 0x100000000ULL)) {
  331. dev_err(&dev->dev, "ABORTING: M32 BAR assignment incompatible with CXL\n");
  332. return -ENODEV;
  333. }
  334. /*
  335. * BAR 4/5 has a special meaning for CXL and must be programmed with a
  336. * special value corresponding to the CXL protocol address range.
  337. * For POWER 8 that means bits 48:49 must be set to 10
  338. */
  339. pci_write_config_dword(dev, PCI_BASE_ADDRESS_4, 0x00000000);
  340. pci_write_config_dword(dev, PCI_BASE_ADDRESS_5, 0x00020000);
  341. return 0;
  342. }
  343. /* pciex node: ibm,opal-m64-window = <0x3d058 0x0 0x3d058 0x0 0x8 0x0>; */
  344. static int switch_card_to_cxl(struct pci_dev *dev)
  345. {
  346. int vsec;
  347. u8 val;
  348. int rc;
  349. dev_info(&dev->dev, "switch card to CXL\n");
  350. if (!(vsec = find_cxl_vsec(dev))) {
  351. dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
  352. return -ENODEV;
  353. }
  354. if ((rc = CXL_READ_VSEC_MODE_CONTROL(dev, vsec, &val))) {
  355. dev_err(&dev->dev, "failed to read current mode control: %i", rc);
  356. return rc;
  357. }
  358. val &= ~CXL_VSEC_PROTOCOL_MASK;
  359. val |= CXL_VSEC_PROTOCOL_256TB | CXL_VSEC_PROTOCOL_ENABLE;
  360. if ((rc = CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val))) {
  361. dev_err(&dev->dev, "failed to enable CXL protocol: %i", rc);
  362. return rc;
  363. }
  364. /*
  365. * The CAIA spec (v0.12 11.6 Bi-modal Device Support) states
  366. * we must wait 100ms after this mode switch before touching
  367. * PCIe config space.
  368. */
  369. msleep(100);
  370. return 0;
  371. }
  372. static int cxl_map_slice_regs(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
  373. {
  374. u64 p1n_base, p2n_base, afu_desc;
  375. const u64 p1n_size = 0x100;
  376. const u64 p2n_size = 0x1000;
  377. p1n_base = p1_base(dev) + 0x10000 + (afu->slice * p1n_size);
  378. p2n_base = p2_base(dev) + (afu->slice * p2n_size);
  379. afu->psn_phys = p2_base(dev) + (adapter->ps_off + (afu->slice * adapter->ps_size));
  380. afu_desc = p2_base(dev) + adapter->afu_desc_off + (afu->slice * adapter->afu_desc_size);
  381. if (!(afu->p1n_mmio = ioremap(p1n_base, p1n_size)))
  382. goto err;
  383. if (!(afu->p2n_mmio = ioremap(p2n_base, p2n_size)))
  384. goto err1;
  385. if (afu_desc) {
  386. if (!(afu->afu_desc_mmio = ioremap(afu_desc, adapter->afu_desc_size)))
  387. goto err2;
  388. }
  389. return 0;
  390. err2:
  391. iounmap(afu->p2n_mmio);
  392. err1:
  393. iounmap(afu->p1n_mmio);
  394. err:
  395. dev_err(&afu->dev, "Error mapping AFU MMIO regions\n");
  396. return -ENOMEM;
  397. }
  398. static void cxl_unmap_slice_regs(struct cxl_afu *afu)
  399. {
  400. if (afu->p1n_mmio)
  401. iounmap(afu->p2n_mmio);
  402. if (afu->p1n_mmio)
  403. iounmap(afu->p1n_mmio);
  404. }
  405. static void cxl_release_afu(struct device *dev)
  406. {
  407. struct cxl_afu *afu = to_cxl_afu(dev);
  408. pr_devel("cxl_release_afu\n");
  409. kfree(afu);
  410. }
  411. static struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
  412. {
  413. struct cxl_afu *afu;
  414. if (!(afu = kzalloc(sizeof(struct cxl_afu), GFP_KERNEL)))
  415. return NULL;
  416. afu->adapter = adapter;
  417. afu->dev.parent = &adapter->dev;
  418. afu->dev.release = cxl_release_afu;
  419. afu->slice = slice;
  420. idr_init(&afu->contexts_idr);
  421. mutex_init(&afu->contexts_lock);
  422. spin_lock_init(&afu->afu_cntl_lock);
  423. mutex_init(&afu->spa_mutex);
  424. afu->prefault_mode = CXL_PREFAULT_NONE;
  425. afu->irqs_max = afu->adapter->user_irqs;
  426. return afu;
  427. }
  428. /* Expects AFU struct to have recently been zeroed out */
  429. static int cxl_read_afu_descriptor(struct cxl_afu *afu)
  430. {
  431. u64 val;
  432. val = AFUD_READ_INFO(afu);
  433. afu->pp_irqs = AFUD_NUM_INTS_PER_PROC(val);
  434. afu->max_procs_virtualised = AFUD_NUM_PROCS(val);
  435. if (AFUD_AFU_DIRECTED(val))
  436. afu->modes_supported |= CXL_MODE_DIRECTED;
  437. if (AFUD_DEDICATED_PROCESS(val))
  438. afu->modes_supported |= CXL_MODE_DEDICATED;
  439. if (AFUD_TIME_SLICED(val))
  440. afu->modes_supported |= CXL_MODE_TIME_SLICED;
  441. val = AFUD_READ_PPPSA(afu);
  442. afu->pp_size = AFUD_PPPSA_LEN(val) * 4096;
  443. afu->psa = AFUD_PPPSA_PSA(val);
  444. if ((afu->pp_psa = AFUD_PPPSA_PP(val)))
  445. afu->pp_offset = AFUD_READ_PPPSA_OFF(afu);
  446. return 0;
  447. }
  448. static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
  449. {
  450. if (afu->psa && afu->adapter->ps_size <
  451. (afu->pp_offset + afu->pp_size*afu->max_procs_virtualised)) {
  452. dev_err(&afu->dev, "per-process PSA can't fit inside the PSA!\n");
  453. return -ENODEV;
  454. }
  455. if (afu->pp_psa && (afu->pp_size < PAGE_SIZE))
  456. dev_warn(&afu->dev, "AFU uses < PAGE_SIZE per-process PSA!");
  457. return 0;
  458. }
  459. static int sanitise_afu_regs(struct cxl_afu *afu)
  460. {
  461. u64 reg;
  462. /*
  463. * Clear out any regs that contain either an IVTE or address or may be
  464. * waiting on an acknowledgement to try to be a bit safer as we bring
  465. * it online
  466. */
  467. reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  468. if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
  469. dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#.16llx\n", reg);
  470. if (cxl_afu_reset(afu))
  471. return -EIO;
  472. if (cxl_afu_disable(afu))
  473. return -EIO;
  474. if (cxl_psl_purge(afu))
  475. return -EIO;
  476. }
  477. cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0x0000000000000000);
  478. cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, 0x0000000000000000);
  479. cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An, 0x0000000000000000);
  480. cxl_p1n_write(afu, CXL_PSL_AMBAR_An, 0x0000000000000000);
  481. cxl_p1n_write(afu, CXL_PSL_SPOffset_An, 0x0000000000000000);
  482. cxl_p1n_write(afu, CXL_HAURP_An, 0x0000000000000000);
  483. cxl_p2n_write(afu, CXL_CSRP_An, 0x0000000000000000);
  484. cxl_p2n_write(afu, CXL_AURP1_An, 0x0000000000000000);
  485. cxl_p2n_write(afu, CXL_AURP0_An, 0x0000000000000000);
  486. cxl_p2n_write(afu, CXL_SSTP1_An, 0x0000000000000000);
  487. cxl_p2n_write(afu, CXL_SSTP0_An, 0x0000000000000000);
  488. reg = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  489. if (reg) {
  490. dev_warn(&afu->dev, "AFU had pending DSISR: %#.16llx\n", reg);
  491. if (reg & CXL_PSL_DSISR_TRANS)
  492. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  493. else
  494. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  495. }
  496. reg = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  497. if (reg) {
  498. if (reg & ~0xffff)
  499. dev_warn(&afu->dev, "AFU had pending SERR: %#.16llx\n", reg);
  500. cxl_p1n_write(afu, CXL_PSL_SERR_An, reg & ~0xffff);
  501. }
  502. reg = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
  503. if (reg) {
  504. dev_warn(&afu->dev, "AFU had pending error status: %#.16llx\n", reg);
  505. cxl_p2n_write(afu, CXL_PSL_ErrStat_An, reg);
  506. }
  507. return 0;
  508. }
  509. static int cxl_init_afu(struct cxl *adapter, int slice, struct pci_dev *dev)
  510. {
  511. struct cxl_afu *afu;
  512. bool free = true;
  513. int rc;
  514. if (!(afu = cxl_alloc_afu(adapter, slice)))
  515. return -ENOMEM;
  516. if ((rc = dev_set_name(&afu->dev, "afu%i.%i", adapter->adapter_num, slice)))
  517. goto err1;
  518. if ((rc = cxl_map_slice_regs(afu, adapter, dev)))
  519. goto err1;
  520. if ((rc = sanitise_afu_regs(afu)))
  521. goto err2;
  522. /* We need to reset the AFU before we can read the AFU descriptor */
  523. if ((rc = cxl_afu_reset(afu)))
  524. goto err2;
  525. if (cxl_verbose)
  526. dump_afu_descriptor(afu);
  527. if ((rc = cxl_read_afu_descriptor(afu)))
  528. goto err2;
  529. if ((rc = cxl_afu_descriptor_looks_ok(afu)))
  530. goto err2;
  531. if ((rc = init_implementation_afu_regs(afu)))
  532. goto err2;
  533. if ((rc = cxl_register_serr_irq(afu)))
  534. goto err2;
  535. if ((rc = cxl_register_psl_irq(afu)))
  536. goto err3;
  537. /* Don't care if this fails */
  538. cxl_debugfs_afu_add(afu);
  539. /*
  540. * After we call this function we must not free the afu directly, even
  541. * if it returns an error!
  542. */
  543. if ((rc = cxl_register_afu(afu)))
  544. goto err_put1;
  545. if ((rc = cxl_sysfs_afu_add(afu)))
  546. goto err_put1;
  547. if ((rc = cxl_afu_select_best_mode(afu)))
  548. goto err_put2;
  549. adapter->afu[afu->slice] = afu;
  550. return 0;
  551. err_put2:
  552. cxl_sysfs_afu_remove(afu);
  553. err_put1:
  554. device_unregister(&afu->dev);
  555. free = false;
  556. cxl_debugfs_afu_remove(afu);
  557. cxl_release_psl_irq(afu);
  558. err3:
  559. cxl_release_serr_irq(afu);
  560. err2:
  561. cxl_unmap_slice_regs(afu);
  562. err1:
  563. if (free)
  564. kfree(afu);
  565. return rc;
  566. }
  567. static void cxl_remove_afu(struct cxl_afu *afu)
  568. {
  569. pr_devel("cxl_remove_afu\n");
  570. if (!afu)
  571. return;
  572. cxl_sysfs_afu_remove(afu);
  573. cxl_debugfs_afu_remove(afu);
  574. spin_lock(&afu->adapter->afu_list_lock);
  575. afu->adapter->afu[afu->slice] = NULL;
  576. spin_unlock(&afu->adapter->afu_list_lock);
  577. cxl_context_detach_all(afu);
  578. cxl_afu_deactivate_mode(afu);
  579. cxl_release_psl_irq(afu);
  580. cxl_release_serr_irq(afu);
  581. cxl_unmap_slice_regs(afu);
  582. device_unregister(&afu->dev);
  583. }
  584. static int cxl_map_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
  585. {
  586. if (pci_request_region(dev, 2, "priv 2 regs"))
  587. goto err1;
  588. if (pci_request_region(dev, 0, "priv 1 regs"))
  589. goto err2;
  590. pr_devel("cxl_map_adapter_regs: p1: %#.16llx %#llx, p2: %#.16llx %#llx",
  591. p1_base(dev), p1_size(dev), p2_base(dev), p2_size(dev));
  592. if (!(adapter->p1_mmio = ioremap(p1_base(dev), p1_size(dev))))
  593. goto err3;
  594. if (!(adapter->p2_mmio = ioremap(p2_base(dev), p2_size(dev))))
  595. goto err4;
  596. return 0;
  597. err4:
  598. iounmap(adapter->p1_mmio);
  599. adapter->p1_mmio = NULL;
  600. err3:
  601. pci_release_region(dev, 0);
  602. err2:
  603. pci_release_region(dev, 2);
  604. err1:
  605. return -ENOMEM;
  606. }
  607. static void cxl_unmap_adapter_regs(struct cxl *adapter)
  608. {
  609. if (adapter->p1_mmio)
  610. iounmap(adapter->p1_mmio);
  611. if (adapter->p2_mmio)
  612. iounmap(adapter->p2_mmio);
  613. }
  614. static int cxl_read_vsec(struct cxl *adapter, struct pci_dev *dev)
  615. {
  616. int vsec;
  617. u32 afu_desc_off, afu_desc_size;
  618. u32 ps_off, ps_size;
  619. u16 vseclen;
  620. u8 image_state;
  621. if (!(vsec = find_cxl_vsec(dev))) {
  622. dev_err(&adapter->dev, "ABORTING: CXL VSEC not found!\n");
  623. return -ENODEV;
  624. }
  625. CXL_READ_VSEC_LENGTH(dev, vsec, &vseclen);
  626. if (vseclen < CXL_VSEC_MIN_SIZE) {
  627. pr_err("ABORTING: CXL VSEC too short\n");
  628. return -EINVAL;
  629. }
  630. CXL_READ_VSEC_STATUS(dev, vsec, &adapter->vsec_status);
  631. CXL_READ_VSEC_PSL_REVISION(dev, vsec, &adapter->psl_rev);
  632. CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, &adapter->caia_major);
  633. CXL_READ_VSEC_CAIA_MINOR(dev, vsec, &adapter->caia_minor);
  634. CXL_READ_VSEC_BASE_IMAGE(dev, vsec, &adapter->base_image);
  635. CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state);
  636. adapter->user_image_loaded = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
  637. adapter->perst_loads_image = !!(image_state & CXL_VSEC_PERST_LOADS_IMAGE);
  638. adapter->perst_select_user = !!(image_state & CXL_VSEC_PERST_SELECT_USER);
  639. CXL_READ_VSEC_NAFUS(dev, vsec, &adapter->slices);
  640. CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, &afu_desc_off);
  641. CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, &afu_desc_size);
  642. CXL_READ_VSEC_PS_OFF(dev, vsec, &ps_off);
  643. CXL_READ_VSEC_PS_SIZE(dev, vsec, &ps_size);
  644. /* Convert everything to bytes, because there is NO WAY I'd look at the
  645. * code a month later and forget what units these are in ;-) */
  646. adapter->ps_off = ps_off * 64 * 1024;
  647. adapter->ps_size = ps_size * 64 * 1024;
  648. adapter->afu_desc_off = afu_desc_off * 64 * 1024;
  649. adapter->afu_desc_size = afu_desc_size *64 * 1024;
  650. /* Total IRQs - 1 PSL ERROR - #AFU*(1 slice error + 1 DSI) */
  651. adapter->user_irqs = pnv_cxl_get_irq_count(dev) - 1 - 2*adapter->slices;
  652. return 0;
  653. }
  654. static int cxl_vsec_looks_ok(struct cxl *adapter, struct pci_dev *dev)
  655. {
  656. if (adapter->vsec_status & CXL_STATUS_SECOND_PORT)
  657. return -EBUSY;
  658. if (adapter->vsec_status & CXL_UNSUPPORTED_FEATURES) {
  659. dev_err(&adapter->dev, "ABORTING: CXL requires unsupported features\n");
  660. return -EINVAL;
  661. }
  662. if (!adapter->slices) {
  663. /* Once we support dynamic reprogramming we can use the card if
  664. * it supports loadable AFUs */
  665. dev_err(&adapter->dev, "ABORTING: Device has no AFUs\n");
  666. return -EINVAL;
  667. }
  668. if (!adapter->afu_desc_off || !adapter->afu_desc_size) {
  669. dev_err(&adapter->dev, "ABORTING: VSEC shows no AFU descriptors\n");
  670. return -EINVAL;
  671. }
  672. if (adapter->ps_size > p2_size(dev) - adapter->ps_off) {
  673. dev_err(&adapter->dev, "ABORTING: Problem state size larger than "
  674. "available in BAR2: 0x%llx > 0x%llx\n",
  675. adapter->ps_size, p2_size(dev) - adapter->ps_off);
  676. return -EINVAL;
  677. }
  678. return 0;
  679. }
  680. static void cxl_release_adapter(struct device *dev)
  681. {
  682. struct cxl *adapter = to_cxl_adapter(dev);
  683. pr_devel("cxl_release_adapter\n");
  684. kfree(adapter);
  685. }
  686. static struct cxl *cxl_alloc_adapter(struct pci_dev *dev)
  687. {
  688. struct cxl *adapter;
  689. if (!(adapter = kzalloc(sizeof(struct cxl), GFP_KERNEL)))
  690. return NULL;
  691. adapter->dev.parent = &dev->dev;
  692. adapter->dev.release = cxl_release_adapter;
  693. pci_set_drvdata(dev, adapter);
  694. spin_lock_init(&adapter->afu_list_lock);
  695. return adapter;
  696. }
  697. static int sanitise_adapter_regs(struct cxl *adapter)
  698. {
  699. cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
  700. return cxl_tlb_slb_invalidate(adapter);
  701. }
  702. static struct cxl *cxl_init_adapter(struct pci_dev *dev)
  703. {
  704. struct cxl *adapter;
  705. bool free = true;
  706. int rc;
  707. if (!(adapter = cxl_alloc_adapter(dev)))
  708. return ERR_PTR(-ENOMEM);
  709. if ((rc = switch_card_to_cxl(dev)))
  710. goto err1;
  711. if ((rc = cxl_alloc_adapter_nr(adapter)))
  712. goto err1;
  713. if ((rc = dev_set_name(&adapter->dev, "card%i", adapter->adapter_num)))
  714. goto err2;
  715. if ((rc = cxl_read_vsec(adapter, dev)))
  716. goto err2;
  717. if ((rc = cxl_vsec_looks_ok(adapter, dev)))
  718. goto err2;
  719. if ((rc = cxl_map_adapter_regs(adapter, dev)))
  720. goto err2;
  721. if ((rc = sanitise_adapter_regs(adapter)))
  722. goto err2;
  723. if ((rc = init_implementation_adapter_regs(adapter, dev)))
  724. goto err3;
  725. if ((rc = pnv_phb_to_cxl(dev)))
  726. goto err3;
  727. if ((rc = cxl_register_psl_err_irq(adapter)))
  728. goto err3;
  729. /* Don't care if this one fails: */
  730. cxl_debugfs_adapter_add(adapter);
  731. /*
  732. * After we call this function we must not free the adapter directly,
  733. * even if it returns an error!
  734. */
  735. if ((rc = cxl_register_adapter(adapter)))
  736. goto err_put1;
  737. if ((rc = cxl_sysfs_adapter_add(adapter)))
  738. goto err_put1;
  739. return adapter;
  740. err_put1:
  741. device_unregister(&adapter->dev);
  742. free = false;
  743. cxl_debugfs_adapter_remove(adapter);
  744. cxl_release_psl_err_irq(adapter);
  745. err3:
  746. cxl_unmap_adapter_regs(adapter);
  747. err2:
  748. cxl_remove_adapter_nr(adapter);
  749. err1:
  750. if (free)
  751. kfree(adapter);
  752. return ERR_PTR(rc);
  753. }
  754. static void cxl_remove_adapter(struct cxl *adapter)
  755. {
  756. struct pci_dev *pdev = to_pci_dev(adapter->dev.parent);
  757. pr_devel("cxl_release_adapter\n");
  758. cxl_sysfs_adapter_remove(adapter);
  759. cxl_debugfs_adapter_remove(adapter);
  760. cxl_release_psl_err_irq(adapter);
  761. cxl_unmap_adapter_regs(adapter);
  762. cxl_remove_adapter_nr(adapter);
  763. device_unregister(&adapter->dev);
  764. pci_release_region(pdev, 0);
  765. pci_release_region(pdev, 2);
  766. pci_disable_device(pdev);
  767. }
  768. static int cxl_probe(struct pci_dev *dev, const struct pci_device_id *id)
  769. {
  770. struct cxl *adapter;
  771. int slice;
  772. int rc;
  773. pci_dev_get(dev);
  774. if (cxl_verbose)
  775. dump_cxl_config_space(dev);
  776. if ((rc = setup_cxl_bars(dev)))
  777. return rc;
  778. if ((rc = pci_enable_device(dev))) {
  779. dev_err(&dev->dev, "pci_enable_device failed: %i\n", rc);
  780. return rc;
  781. }
  782. adapter = cxl_init_adapter(dev);
  783. if (IS_ERR(adapter)) {
  784. dev_err(&dev->dev, "cxl_init_adapter failed: %li\n", PTR_ERR(adapter));
  785. return PTR_ERR(adapter);
  786. }
  787. for (slice = 0; slice < adapter->slices; slice++) {
  788. if ((rc = cxl_init_afu(adapter, slice, dev)))
  789. dev_err(&dev->dev, "AFU %i failed to initialise: %i\n", slice, rc);
  790. }
  791. return 0;
  792. }
  793. static void cxl_remove(struct pci_dev *dev)
  794. {
  795. struct cxl *adapter = pci_get_drvdata(dev);
  796. int afu;
  797. dev_warn(&dev->dev, "pci remove\n");
  798. /*
  799. * Lock to prevent someone grabbing a ref through the adapter list as
  800. * we are removing it
  801. */
  802. for (afu = 0; afu < adapter->slices; afu++)
  803. cxl_remove_afu(adapter->afu[afu]);
  804. cxl_remove_adapter(adapter);
  805. }
  806. struct pci_driver cxl_pci_driver = {
  807. .name = "cxl-pci",
  808. .id_table = cxl_pci_tbl,
  809. .probe = cxl_probe,
  810. .remove = cxl_remove,
  811. };