pci.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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_get_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_update_image_control(struct cxl *adapter)
  307. {
  308. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  309. int rc;
  310. int vsec;
  311. u8 image_state;
  312. if (!(vsec = find_cxl_vsec(dev))) {
  313. dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
  314. return -ENODEV;
  315. }
  316. if ((rc = CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state))) {
  317. dev_err(&dev->dev, "failed to read image state: %i\n", rc);
  318. return rc;
  319. }
  320. if (adapter->perst_loads_image)
  321. image_state |= CXL_VSEC_PERST_LOADS_IMAGE;
  322. else
  323. image_state &= ~CXL_VSEC_PERST_LOADS_IMAGE;
  324. if (adapter->perst_select_user)
  325. image_state |= CXL_VSEC_PERST_SELECT_USER;
  326. else
  327. image_state &= ~CXL_VSEC_PERST_SELECT_USER;
  328. if ((rc = CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, image_state))) {
  329. dev_err(&dev->dev, "failed to update image control: %i\n", rc);
  330. return rc;
  331. }
  332. return 0;
  333. }
  334. int cxl_alloc_one_irq(struct cxl *adapter)
  335. {
  336. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  337. return pnv_cxl_alloc_hwirqs(dev, 1);
  338. }
  339. void cxl_release_one_irq(struct cxl *adapter, int hwirq)
  340. {
  341. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  342. return pnv_cxl_release_hwirqs(dev, hwirq, 1);
  343. }
  344. int cxl_alloc_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter, unsigned int num)
  345. {
  346. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  347. return pnv_cxl_alloc_hwirq_ranges(irqs, dev, num);
  348. }
  349. void cxl_release_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter)
  350. {
  351. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  352. pnv_cxl_release_hwirq_ranges(irqs, dev);
  353. }
  354. static int setup_cxl_bars(struct pci_dev *dev)
  355. {
  356. /* Safety check in case we get backported to < 3.17 without M64 */
  357. if ((p1_base(dev) < 0x100000000ULL) ||
  358. (p2_base(dev) < 0x100000000ULL)) {
  359. dev_err(&dev->dev, "ABORTING: M32 BAR assignment incompatible with CXL\n");
  360. return -ENODEV;
  361. }
  362. /*
  363. * BAR 4/5 has a special meaning for CXL and must be programmed with a
  364. * special value corresponding to the CXL protocol address range.
  365. * For POWER 8 that means bits 48:49 must be set to 10
  366. */
  367. pci_write_config_dword(dev, PCI_BASE_ADDRESS_4, 0x00000000);
  368. pci_write_config_dword(dev, PCI_BASE_ADDRESS_5, 0x00020000);
  369. return 0;
  370. }
  371. /* pciex node: ibm,opal-m64-window = <0x3d058 0x0 0x3d058 0x0 0x8 0x0>; */
  372. static int switch_card_to_cxl(struct pci_dev *dev)
  373. {
  374. int vsec;
  375. u8 val;
  376. int rc;
  377. dev_info(&dev->dev, "switch card to CXL\n");
  378. if (!(vsec = find_cxl_vsec(dev))) {
  379. dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
  380. return -ENODEV;
  381. }
  382. if ((rc = CXL_READ_VSEC_MODE_CONTROL(dev, vsec, &val))) {
  383. dev_err(&dev->dev, "failed to read current mode control: %i", rc);
  384. return rc;
  385. }
  386. val &= ~CXL_VSEC_PROTOCOL_MASK;
  387. val |= CXL_VSEC_PROTOCOL_256TB | CXL_VSEC_PROTOCOL_ENABLE;
  388. if ((rc = CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val))) {
  389. dev_err(&dev->dev, "failed to enable CXL protocol: %i", rc);
  390. return rc;
  391. }
  392. /*
  393. * The CAIA spec (v0.12 11.6 Bi-modal Device Support) states
  394. * we must wait 100ms after this mode switch before touching
  395. * PCIe config space.
  396. */
  397. msleep(100);
  398. return 0;
  399. }
  400. static int cxl_map_slice_regs(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
  401. {
  402. u64 p1n_base, p2n_base, afu_desc;
  403. const u64 p1n_size = 0x100;
  404. const u64 p2n_size = 0x1000;
  405. p1n_base = p1_base(dev) + 0x10000 + (afu->slice * p1n_size);
  406. p2n_base = p2_base(dev) + (afu->slice * p2n_size);
  407. afu->psn_phys = p2_base(dev) + (adapter->ps_off + (afu->slice * adapter->ps_size));
  408. afu_desc = p2_base(dev) + adapter->afu_desc_off + (afu->slice * adapter->afu_desc_size);
  409. if (!(afu->p1n_mmio = ioremap(p1n_base, p1n_size)))
  410. goto err;
  411. if (!(afu->p2n_mmio = ioremap(p2n_base, p2n_size)))
  412. goto err1;
  413. if (afu_desc) {
  414. if (!(afu->afu_desc_mmio = ioremap(afu_desc, adapter->afu_desc_size)))
  415. goto err2;
  416. }
  417. return 0;
  418. err2:
  419. iounmap(afu->p2n_mmio);
  420. err1:
  421. iounmap(afu->p1n_mmio);
  422. err:
  423. dev_err(&afu->dev, "Error mapping AFU MMIO regions\n");
  424. return -ENOMEM;
  425. }
  426. static void cxl_unmap_slice_regs(struct cxl_afu *afu)
  427. {
  428. if (afu->p1n_mmio)
  429. iounmap(afu->p2n_mmio);
  430. if (afu->p1n_mmio)
  431. iounmap(afu->p1n_mmio);
  432. }
  433. static void cxl_release_afu(struct device *dev)
  434. {
  435. struct cxl_afu *afu = to_cxl_afu(dev);
  436. pr_devel("cxl_release_afu\n");
  437. kfree(afu);
  438. }
  439. static struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
  440. {
  441. struct cxl_afu *afu;
  442. if (!(afu = kzalloc(sizeof(struct cxl_afu), GFP_KERNEL)))
  443. return NULL;
  444. afu->adapter = adapter;
  445. afu->dev.parent = &adapter->dev;
  446. afu->dev.release = cxl_release_afu;
  447. afu->slice = slice;
  448. idr_init(&afu->contexts_idr);
  449. mutex_init(&afu->contexts_lock);
  450. spin_lock_init(&afu->afu_cntl_lock);
  451. mutex_init(&afu->spa_mutex);
  452. afu->prefault_mode = CXL_PREFAULT_NONE;
  453. afu->irqs_max = afu->adapter->user_irqs;
  454. return afu;
  455. }
  456. /* Expects AFU struct to have recently been zeroed out */
  457. static int cxl_read_afu_descriptor(struct cxl_afu *afu)
  458. {
  459. u64 val;
  460. val = AFUD_READ_INFO(afu);
  461. afu->pp_irqs = AFUD_NUM_INTS_PER_PROC(val);
  462. afu->max_procs_virtualised = AFUD_NUM_PROCS(val);
  463. if (AFUD_AFU_DIRECTED(val))
  464. afu->modes_supported |= CXL_MODE_DIRECTED;
  465. if (AFUD_DEDICATED_PROCESS(val))
  466. afu->modes_supported |= CXL_MODE_DEDICATED;
  467. if (AFUD_TIME_SLICED(val))
  468. afu->modes_supported |= CXL_MODE_TIME_SLICED;
  469. val = AFUD_READ_PPPSA(afu);
  470. afu->pp_size = AFUD_PPPSA_LEN(val) * 4096;
  471. afu->psa = AFUD_PPPSA_PSA(val);
  472. if ((afu->pp_psa = AFUD_PPPSA_PP(val)))
  473. afu->pp_offset = AFUD_READ_PPPSA_OFF(afu);
  474. return 0;
  475. }
  476. static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
  477. {
  478. if (afu->psa && afu->adapter->ps_size <
  479. (afu->pp_offset + afu->pp_size*afu->max_procs_virtualised)) {
  480. dev_err(&afu->dev, "per-process PSA can't fit inside the PSA!\n");
  481. return -ENODEV;
  482. }
  483. if (afu->pp_psa && (afu->pp_size < PAGE_SIZE))
  484. dev_warn(&afu->dev, "AFU uses < PAGE_SIZE per-process PSA!");
  485. return 0;
  486. }
  487. static int sanitise_afu_regs(struct cxl_afu *afu)
  488. {
  489. u64 reg;
  490. /*
  491. * Clear out any regs that contain either an IVTE or address or may be
  492. * waiting on an acknowledgement to try to be a bit safer as we bring
  493. * it online
  494. */
  495. reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  496. if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
  497. dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#.16llx\n", reg);
  498. if (cxl_afu_reset(afu))
  499. return -EIO;
  500. if (cxl_afu_disable(afu))
  501. return -EIO;
  502. if (cxl_psl_purge(afu))
  503. return -EIO;
  504. }
  505. cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0x0000000000000000);
  506. cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, 0x0000000000000000);
  507. cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An, 0x0000000000000000);
  508. cxl_p1n_write(afu, CXL_PSL_AMBAR_An, 0x0000000000000000);
  509. cxl_p1n_write(afu, CXL_PSL_SPOffset_An, 0x0000000000000000);
  510. cxl_p1n_write(afu, CXL_HAURP_An, 0x0000000000000000);
  511. cxl_p2n_write(afu, CXL_CSRP_An, 0x0000000000000000);
  512. cxl_p2n_write(afu, CXL_AURP1_An, 0x0000000000000000);
  513. cxl_p2n_write(afu, CXL_AURP0_An, 0x0000000000000000);
  514. cxl_p2n_write(afu, CXL_SSTP1_An, 0x0000000000000000);
  515. cxl_p2n_write(afu, CXL_SSTP0_An, 0x0000000000000000);
  516. reg = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  517. if (reg) {
  518. dev_warn(&afu->dev, "AFU had pending DSISR: %#.16llx\n", reg);
  519. if (reg & CXL_PSL_DSISR_TRANS)
  520. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  521. else
  522. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  523. }
  524. reg = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  525. if (reg) {
  526. if (reg & ~0xffff)
  527. dev_warn(&afu->dev, "AFU had pending SERR: %#.16llx\n", reg);
  528. cxl_p1n_write(afu, CXL_PSL_SERR_An, reg & ~0xffff);
  529. }
  530. reg = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
  531. if (reg) {
  532. dev_warn(&afu->dev, "AFU had pending error status: %#.16llx\n", reg);
  533. cxl_p2n_write(afu, CXL_PSL_ErrStat_An, reg);
  534. }
  535. return 0;
  536. }
  537. static int cxl_init_afu(struct cxl *adapter, int slice, struct pci_dev *dev)
  538. {
  539. struct cxl_afu *afu;
  540. bool free = true;
  541. int rc;
  542. if (!(afu = cxl_alloc_afu(adapter, slice)))
  543. return -ENOMEM;
  544. if ((rc = dev_set_name(&afu->dev, "afu%i.%i", adapter->adapter_num, slice)))
  545. goto err1;
  546. if ((rc = cxl_map_slice_regs(afu, adapter, dev)))
  547. goto err1;
  548. if ((rc = sanitise_afu_regs(afu)))
  549. goto err2;
  550. /* We need to reset the AFU before we can read the AFU descriptor */
  551. if ((rc = cxl_afu_reset(afu)))
  552. goto err2;
  553. if (cxl_verbose)
  554. dump_afu_descriptor(afu);
  555. if ((rc = cxl_read_afu_descriptor(afu)))
  556. goto err2;
  557. if ((rc = cxl_afu_descriptor_looks_ok(afu)))
  558. goto err2;
  559. if ((rc = init_implementation_afu_regs(afu)))
  560. goto err2;
  561. if ((rc = cxl_register_serr_irq(afu)))
  562. goto err2;
  563. if ((rc = cxl_register_psl_irq(afu)))
  564. goto err3;
  565. /* Don't care if this fails */
  566. cxl_debugfs_afu_add(afu);
  567. /*
  568. * After we call this function we must not free the afu directly, even
  569. * if it returns an error!
  570. */
  571. if ((rc = cxl_register_afu(afu)))
  572. goto err_put1;
  573. if ((rc = cxl_sysfs_afu_add(afu)))
  574. goto err_put1;
  575. if ((rc = cxl_afu_select_best_mode(afu)))
  576. goto err_put2;
  577. adapter->afu[afu->slice] = afu;
  578. return 0;
  579. err_put2:
  580. cxl_sysfs_afu_remove(afu);
  581. err_put1:
  582. device_unregister(&afu->dev);
  583. free = false;
  584. cxl_debugfs_afu_remove(afu);
  585. cxl_release_psl_irq(afu);
  586. err3:
  587. cxl_release_serr_irq(afu);
  588. err2:
  589. cxl_unmap_slice_regs(afu);
  590. err1:
  591. if (free)
  592. kfree(afu);
  593. return rc;
  594. }
  595. static void cxl_remove_afu(struct cxl_afu *afu)
  596. {
  597. pr_devel("cxl_remove_afu\n");
  598. if (!afu)
  599. return;
  600. cxl_sysfs_afu_remove(afu);
  601. cxl_debugfs_afu_remove(afu);
  602. spin_lock(&afu->adapter->afu_list_lock);
  603. afu->adapter->afu[afu->slice] = NULL;
  604. spin_unlock(&afu->adapter->afu_list_lock);
  605. cxl_context_detach_all(afu);
  606. cxl_afu_deactivate_mode(afu);
  607. cxl_release_psl_irq(afu);
  608. cxl_release_serr_irq(afu);
  609. cxl_unmap_slice_regs(afu);
  610. device_unregister(&afu->dev);
  611. }
  612. static int cxl_map_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
  613. {
  614. if (pci_request_region(dev, 2, "priv 2 regs"))
  615. goto err1;
  616. if (pci_request_region(dev, 0, "priv 1 regs"))
  617. goto err2;
  618. pr_devel("cxl_map_adapter_regs: p1: %#.16llx %#llx, p2: %#.16llx %#llx",
  619. p1_base(dev), p1_size(dev), p2_base(dev), p2_size(dev));
  620. if (!(adapter->p1_mmio = ioremap(p1_base(dev), p1_size(dev))))
  621. goto err3;
  622. if (!(adapter->p2_mmio = ioremap(p2_base(dev), p2_size(dev))))
  623. goto err4;
  624. return 0;
  625. err4:
  626. iounmap(adapter->p1_mmio);
  627. adapter->p1_mmio = NULL;
  628. err3:
  629. pci_release_region(dev, 0);
  630. err2:
  631. pci_release_region(dev, 2);
  632. err1:
  633. return -ENOMEM;
  634. }
  635. static void cxl_unmap_adapter_regs(struct cxl *adapter)
  636. {
  637. if (adapter->p1_mmio)
  638. iounmap(adapter->p1_mmio);
  639. if (adapter->p2_mmio)
  640. iounmap(adapter->p2_mmio);
  641. }
  642. static int cxl_read_vsec(struct cxl *adapter, struct pci_dev *dev)
  643. {
  644. int vsec;
  645. u32 afu_desc_off, afu_desc_size;
  646. u32 ps_off, ps_size;
  647. u16 vseclen;
  648. u8 image_state;
  649. if (!(vsec = find_cxl_vsec(dev))) {
  650. dev_err(&adapter->dev, "ABORTING: CXL VSEC not found!\n");
  651. return -ENODEV;
  652. }
  653. CXL_READ_VSEC_LENGTH(dev, vsec, &vseclen);
  654. if (vseclen < CXL_VSEC_MIN_SIZE) {
  655. pr_err("ABORTING: CXL VSEC too short\n");
  656. return -EINVAL;
  657. }
  658. CXL_READ_VSEC_STATUS(dev, vsec, &adapter->vsec_status);
  659. CXL_READ_VSEC_PSL_REVISION(dev, vsec, &adapter->psl_rev);
  660. CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, &adapter->caia_major);
  661. CXL_READ_VSEC_CAIA_MINOR(dev, vsec, &adapter->caia_minor);
  662. CXL_READ_VSEC_BASE_IMAGE(dev, vsec, &adapter->base_image);
  663. CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state);
  664. adapter->user_image_loaded = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
  665. adapter->perst_loads_image = true;
  666. adapter->perst_select_user = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
  667. CXL_READ_VSEC_NAFUS(dev, vsec, &adapter->slices);
  668. CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, &afu_desc_off);
  669. CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, &afu_desc_size);
  670. CXL_READ_VSEC_PS_OFF(dev, vsec, &ps_off);
  671. CXL_READ_VSEC_PS_SIZE(dev, vsec, &ps_size);
  672. /* Convert everything to bytes, because there is NO WAY I'd look at the
  673. * code a month later and forget what units these are in ;-) */
  674. adapter->ps_off = ps_off * 64 * 1024;
  675. adapter->ps_size = ps_size * 64 * 1024;
  676. adapter->afu_desc_off = afu_desc_off * 64 * 1024;
  677. adapter->afu_desc_size = afu_desc_size *64 * 1024;
  678. /* Total IRQs - 1 PSL ERROR - #AFU*(1 slice error + 1 DSI) */
  679. adapter->user_irqs = pnv_cxl_get_irq_count(dev) - 1 - 2*adapter->slices;
  680. return 0;
  681. }
  682. static int cxl_vsec_looks_ok(struct cxl *adapter, struct pci_dev *dev)
  683. {
  684. if (adapter->vsec_status & CXL_STATUS_SECOND_PORT)
  685. return -EBUSY;
  686. if (adapter->vsec_status & CXL_UNSUPPORTED_FEATURES) {
  687. dev_err(&adapter->dev, "ABORTING: CXL requires unsupported features\n");
  688. return -EINVAL;
  689. }
  690. if (!adapter->slices) {
  691. /* Once we support dynamic reprogramming we can use the card if
  692. * it supports loadable AFUs */
  693. dev_err(&adapter->dev, "ABORTING: Device has no AFUs\n");
  694. return -EINVAL;
  695. }
  696. if (!adapter->afu_desc_off || !adapter->afu_desc_size) {
  697. dev_err(&adapter->dev, "ABORTING: VSEC shows no AFU descriptors\n");
  698. return -EINVAL;
  699. }
  700. if (adapter->ps_size > p2_size(dev) - adapter->ps_off) {
  701. dev_err(&adapter->dev, "ABORTING: Problem state size larger than "
  702. "available in BAR2: 0x%llx > 0x%llx\n",
  703. adapter->ps_size, p2_size(dev) - adapter->ps_off);
  704. return -EINVAL;
  705. }
  706. return 0;
  707. }
  708. static void cxl_release_adapter(struct device *dev)
  709. {
  710. struct cxl *adapter = to_cxl_adapter(dev);
  711. pr_devel("cxl_release_adapter\n");
  712. kfree(adapter);
  713. }
  714. static struct cxl *cxl_alloc_adapter(struct pci_dev *dev)
  715. {
  716. struct cxl *adapter;
  717. if (!(adapter = kzalloc(sizeof(struct cxl), GFP_KERNEL)))
  718. return NULL;
  719. adapter->dev.parent = &dev->dev;
  720. adapter->dev.release = cxl_release_adapter;
  721. pci_set_drvdata(dev, adapter);
  722. spin_lock_init(&adapter->afu_list_lock);
  723. return adapter;
  724. }
  725. static int sanitise_adapter_regs(struct cxl *adapter)
  726. {
  727. cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
  728. return cxl_tlb_slb_invalidate(adapter);
  729. }
  730. static struct cxl *cxl_init_adapter(struct pci_dev *dev)
  731. {
  732. struct cxl *adapter;
  733. bool free = true;
  734. int rc;
  735. if (!(adapter = cxl_alloc_adapter(dev)))
  736. return ERR_PTR(-ENOMEM);
  737. if ((rc = switch_card_to_cxl(dev)))
  738. goto err1;
  739. if ((rc = cxl_alloc_adapter_nr(adapter)))
  740. goto err1;
  741. if ((rc = dev_set_name(&adapter->dev, "card%i", adapter->adapter_num)))
  742. goto err2;
  743. if ((rc = cxl_read_vsec(adapter, dev)))
  744. goto err2;
  745. if ((rc = cxl_vsec_looks_ok(adapter, dev)))
  746. goto err2;
  747. if ((rc = cxl_update_image_control(adapter)))
  748. goto err2;
  749. if ((rc = cxl_map_adapter_regs(adapter, dev)))
  750. goto err2;
  751. if ((rc = sanitise_adapter_regs(adapter)))
  752. goto err2;
  753. if ((rc = init_implementation_adapter_regs(adapter, dev)))
  754. goto err3;
  755. if ((rc = pnv_phb_to_cxl(dev)))
  756. goto err3;
  757. if ((rc = cxl_register_psl_err_irq(adapter)))
  758. goto err3;
  759. /* Don't care if this one fails: */
  760. cxl_debugfs_adapter_add(adapter);
  761. /*
  762. * After we call this function we must not free the adapter directly,
  763. * even if it returns an error!
  764. */
  765. if ((rc = cxl_register_adapter(adapter)))
  766. goto err_put1;
  767. if ((rc = cxl_sysfs_adapter_add(adapter)))
  768. goto err_put1;
  769. return adapter;
  770. err_put1:
  771. device_unregister(&adapter->dev);
  772. free = false;
  773. cxl_debugfs_adapter_remove(adapter);
  774. cxl_release_psl_err_irq(adapter);
  775. err3:
  776. cxl_unmap_adapter_regs(adapter);
  777. err2:
  778. cxl_remove_adapter_nr(adapter);
  779. err1:
  780. if (free)
  781. kfree(adapter);
  782. return ERR_PTR(rc);
  783. }
  784. static void cxl_remove_adapter(struct cxl *adapter)
  785. {
  786. struct pci_dev *pdev = to_pci_dev(adapter->dev.parent);
  787. pr_devel("cxl_release_adapter\n");
  788. cxl_sysfs_adapter_remove(adapter);
  789. cxl_debugfs_adapter_remove(adapter);
  790. cxl_release_psl_err_irq(adapter);
  791. cxl_unmap_adapter_regs(adapter);
  792. cxl_remove_adapter_nr(adapter);
  793. device_unregister(&adapter->dev);
  794. pci_release_region(pdev, 0);
  795. pci_release_region(pdev, 2);
  796. pci_disable_device(pdev);
  797. }
  798. static int cxl_probe(struct pci_dev *dev, const struct pci_device_id *id)
  799. {
  800. struct cxl *adapter;
  801. int slice;
  802. int rc;
  803. pci_dev_get(dev);
  804. if (cxl_verbose)
  805. dump_cxl_config_space(dev);
  806. if ((rc = setup_cxl_bars(dev)))
  807. return rc;
  808. if ((rc = pci_enable_device(dev))) {
  809. dev_err(&dev->dev, "pci_enable_device failed: %i\n", rc);
  810. return rc;
  811. }
  812. adapter = cxl_init_adapter(dev);
  813. if (IS_ERR(adapter)) {
  814. dev_err(&dev->dev, "cxl_init_adapter failed: %li\n", PTR_ERR(adapter));
  815. return PTR_ERR(adapter);
  816. }
  817. for (slice = 0; slice < adapter->slices; slice++) {
  818. if ((rc = cxl_init_afu(adapter, slice, dev)))
  819. dev_err(&dev->dev, "AFU %i failed to initialise: %i\n", slice, rc);
  820. }
  821. return 0;
  822. }
  823. static void cxl_remove(struct pci_dev *dev)
  824. {
  825. struct cxl *adapter = pci_get_drvdata(dev);
  826. int afu;
  827. dev_warn(&dev->dev, "pci remove\n");
  828. /*
  829. * Lock to prevent someone grabbing a ref through the adapter list as
  830. * we are removing it
  831. */
  832. for (afu = 0; afu < adapter->slices; afu++)
  833. cxl_remove_afu(adapter->afu[afu]);
  834. cxl_remove_adapter(adapter);
  835. }
  836. struct pci_driver cxl_pci_driver = {
  837. .name = "cxl-pci",
  838. .id_table = cxl_pci_tbl,
  839. .probe = cxl_probe,
  840. .remove = cxl_remove,
  841. };