config.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright 2017 IBM Corp.
  3. #include <linux/pci.h>
  4. #include <asm/pnv-ocxl.h>
  5. #include <misc/ocxl-config.h>
  6. #include "ocxl_internal.h"
  7. #define EXTRACT_BIT(val, bit) (!!(val & BIT(bit)))
  8. #define EXTRACT_BITS(val, s, e) ((val & GENMASK(e, s)) >> s)
  9. #define OCXL_DVSEC_AFU_IDX_MASK GENMASK(5, 0)
  10. #define OCXL_DVSEC_ACTAG_MASK GENMASK(11, 0)
  11. #define OCXL_DVSEC_PASID_MASK GENMASK(19, 0)
  12. #define OCXL_DVSEC_PASID_LOG_MASK GENMASK(4, 0)
  13. #define OCXL_DVSEC_TEMPL_VERSION 0x0
  14. #define OCXL_DVSEC_TEMPL_NAME 0x4
  15. #define OCXL_DVSEC_TEMPL_AFU_VERSION 0x1C
  16. #define OCXL_DVSEC_TEMPL_MMIO_GLOBAL 0x20
  17. #define OCXL_DVSEC_TEMPL_MMIO_GLOBAL_SZ 0x28
  18. #define OCXL_DVSEC_TEMPL_MMIO_PP 0x30
  19. #define OCXL_DVSEC_TEMPL_MMIO_PP_SZ 0x38
  20. #define OCXL_DVSEC_TEMPL_ALL_MEM_SZ 0x3C
  21. #define OCXL_DVSEC_TEMPL_LPC_MEM_START 0x40
  22. #define OCXL_DVSEC_TEMPL_WWID 0x48
  23. #define OCXL_DVSEC_TEMPL_LPC_MEM_SZ 0x58
  24. #define OCXL_MAX_AFU_PER_FUNCTION 64
  25. #define OCXL_TEMPL_LEN_1_0 0x58
  26. #define OCXL_TEMPL_LEN_1_1 0x60
  27. #define OCXL_TEMPL_NAME_LEN 24
  28. #define OCXL_CFG_TIMEOUT 3
  29. static int find_dvsec(struct pci_dev *dev, int dvsec_id)
  30. {
  31. return pci_find_dvsec_capability(dev, PCI_VENDOR_ID_IBM, dvsec_id);
  32. }
  33. static int find_dvsec_afu_ctrl(struct pci_dev *dev, u8 afu_idx)
  34. {
  35. int vsec = 0;
  36. u16 vendor, id;
  37. u8 idx;
  38. while ((vsec = pci_find_next_ext_capability(dev, vsec,
  39. OCXL_EXT_CAP_ID_DVSEC))) {
  40. pci_read_config_word(dev, vsec + OCXL_DVSEC_VENDOR_OFFSET,
  41. &vendor);
  42. pci_read_config_word(dev, vsec + OCXL_DVSEC_ID_OFFSET, &id);
  43. if (vendor == PCI_VENDOR_ID_IBM &&
  44. id == OCXL_DVSEC_AFU_CTRL_ID) {
  45. pci_read_config_byte(dev,
  46. vsec + OCXL_DVSEC_AFU_CTRL_AFU_IDX,
  47. &idx);
  48. if (idx == afu_idx)
  49. return vsec;
  50. }
  51. }
  52. return 0;
  53. }
  54. /**
  55. * get_function_0() - Find a related PCI device (function 0)
  56. * @dev: PCI device to match
  57. *
  58. * Returns a pointer to the related device, or null if not found
  59. */
  60. static struct pci_dev *get_function_0(struct pci_dev *dev)
  61. {
  62. unsigned int devfn = PCI_DEVFN(PCI_SLOT(dev->devfn), 0);
  63. return pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
  64. dev->bus->number, devfn);
  65. }
  66. static void read_pasid(struct pci_dev *dev, struct ocxl_fn_config *fn)
  67. {
  68. u16 val;
  69. int pos;
  70. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_PASID);
  71. if (!pos) {
  72. /*
  73. * PASID capability is not mandatory, but there
  74. * shouldn't be any AFU
  75. */
  76. dev_dbg(&dev->dev, "Function doesn't require any PASID\n");
  77. fn->max_pasid_log = -1;
  78. goto out;
  79. }
  80. pci_read_config_word(dev, pos + PCI_PASID_CAP, &val);
  81. fn->max_pasid_log = EXTRACT_BITS(val, 8, 12);
  82. out:
  83. dev_dbg(&dev->dev, "PASID capability:\n");
  84. dev_dbg(&dev->dev, " Max PASID log = %d\n", fn->max_pasid_log);
  85. }
  86. static int read_dvsec_tl(struct pci_dev *dev, struct ocxl_fn_config *fn)
  87. {
  88. int pos;
  89. pos = find_dvsec(dev, OCXL_DVSEC_TL_ID);
  90. if (!pos && PCI_FUNC(dev->devfn) == 0) {
  91. dev_err(&dev->dev, "Can't find TL DVSEC\n");
  92. return -ENODEV;
  93. }
  94. if (pos && PCI_FUNC(dev->devfn) != 0) {
  95. dev_err(&dev->dev, "TL DVSEC is only allowed on function 0\n");
  96. return -ENODEV;
  97. }
  98. fn->dvsec_tl_pos = pos;
  99. return 0;
  100. }
  101. static int read_dvsec_function(struct pci_dev *dev, struct ocxl_fn_config *fn)
  102. {
  103. int pos, afu_present;
  104. u32 val;
  105. pos = find_dvsec(dev, OCXL_DVSEC_FUNC_ID);
  106. if (!pos) {
  107. dev_err(&dev->dev, "Can't find function DVSEC\n");
  108. return -ENODEV;
  109. }
  110. fn->dvsec_function_pos = pos;
  111. pci_read_config_dword(dev, pos + OCXL_DVSEC_FUNC_OFF_INDEX, &val);
  112. afu_present = EXTRACT_BIT(val, 31);
  113. if (!afu_present) {
  114. fn->max_afu_index = -1;
  115. dev_dbg(&dev->dev, "Function doesn't define any AFU\n");
  116. goto out;
  117. }
  118. fn->max_afu_index = EXTRACT_BITS(val, 24, 29);
  119. out:
  120. dev_dbg(&dev->dev, "Function DVSEC:\n");
  121. dev_dbg(&dev->dev, " Max AFU index = %d\n", fn->max_afu_index);
  122. return 0;
  123. }
  124. static int read_dvsec_afu_info(struct pci_dev *dev, struct ocxl_fn_config *fn)
  125. {
  126. int pos;
  127. if (fn->max_afu_index < 0) {
  128. fn->dvsec_afu_info_pos = -1;
  129. return 0;
  130. }
  131. pos = find_dvsec(dev, OCXL_DVSEC_AFU_INFO_ID);
  132. if (!pos) {
  133. dev_err(&dev->dev, "Can't find AFU information DVSEC\n");
  134. return -ENODEV;
  135. }
  136. fn->dvsec_afu_info_pos = pos;
  137. return 0;
  138. }
  139. static int read_dvsec_vendor(struct pci_dev *dev)
  140. {
  141. int pos;
  142. u32 cfg, tlx, dlx, reset_reload;
  143. /*
  144. * vendor specific DVSEC, for IBM images only. Some older
  145. * images may not have it
  146. *
  147. * It's only used on function 0 to specify the version of some
  148. * logic blocks and to give access to special registers to
  149. * enable host-based flashing.
  150. */
  151. if (PCI_FUNC(dev->devfn) != 0)
  152. return 0;
  153. pos = find_dvsec(dev, OCXL_DVSEC_VENDOR_ID);
  154. if (!pos)
  155. return 0;
  156. pci_read_config_dword(dev, pos + OCXL_DVSEC_VENDOR_CFG_VERS, &cfg);
  157. pci_read_config_dword(dev, pos + OCXL_DVSEC_VENDOR_TLX_VERS, &tlx);
  158. pci_read_config_dword(dev, pos + OCXL_DVSEC_VENDOR_DLX_VERS, &dlx);
  159. pci_read_config_dword(dev, pos + OCXL_DVSEC_VENDOR_RESET_RELOAD,
  160. &reset_reload);
  161. dev_dbg(&dev->dev, "Vendor specific DVSEC:\n");
  162. dev_dbg(&dev->dev, " CFG version = 0x%x\n", cfg);
  163. dev_dbg(&dev->dev, " TLX version = 0x%x\n", tlx);
  164. dev_dbg(&dev->dev, " DLX version = 0x%x\n", dlx);
  165. dev_dbg(&dev->dev, " ResetReload = 0x%x\n", reset_reload);
  166. return 0;
  167. }
  168. /**
  169. * get_dvsec_vendor0() - Find a related PCI device (function 0)
  170. * @dev: PCI device to match
  171. * @dev0: The PCI device (function 0) found
  172. * @out_pos: The position of PCI device (function 0)
  173. *
  174. * Returns 0 on success, negative on failure.
  175. *
  176. * NOTE: If it's successful, the reference of dev0 is increased,
  177. * so after using it, the callers must call pci_dev_put() to give
  178. * up the reference.
  179. */
  180. static int get_dvsec_vendor0(struct pci_dev *dev, struct pci_dev **dev0,
  181. int *out_pos)
  182. {
  183. int pos;
  184. if (PCI_FUNC(dev->devfn) != 0) {
  185. dev = get_function_0(dev);
  186. if (!dev)
  187. return -1;
  188. } else {
  189. dev = pci_dev_get(dev);
  190. }
  191. pos = find_dvsec(dev, OCXL_DVSEC_VENDOR_ID);
  192. if (!pos) {
  193. pci_dev_put(dev);
  194. return -1;
  195. }
  196. *dev0 = dev;
  197. *out_pos = pos;
  198. return 0;
  199. }
  200. int ocxl_config_get_reset_reload(struct pci_dev *dev, int *val)
  201. {
  202. struct pci_dev *dev0;
  203. u32 reset_reload;
  204. int pos;
  205. if (get_dvsec_vendor0(dev, &dev0, &pos))
  206. return -1;
  207. pci_read_config_dword(dev0, pos + OCXL_DVSEC_VENDOR_RESET_RELOAD,
  208. &reset_reload);
  209. pci_dev_put(dev0);
  210. *val = !!(reset_reload & BIT(0));
  211. return 0;
  212. }
  213. int ocxl_config_set_reset_reload(struct pci_dev *dev, int val)
  214. {
  215. struct pci_dev *dev0;
  216. u32 reset_reload;
  217. int pos;
  218. if (get_dvsec_vendor0(dev, &dev0, &pos))
  219. return -1;
  220. pci_read_config_dword(dev0, pos + OCXL_DVSEC_VENDOR_RESET_RELOAD,
  221. &reset_reload);
  222. if (val)
  223. reset_reload |= BIT(0);
  224. else
  225. reset_reload &= ~BIT(0);
  226. pci_write_config_dword(dev0, pos + OCXL_DVSEC_VENDOR_RESET_RELOAD,
  227. reset_reload);
  228. pci_dev_put(dev0);
  229. return 0;
  230. }
  231. static int validate_function(struct pci_dev *dev, struct ocxl_fn_config *fn)
  232. {
  233. if (fn->max_pasid_log == -1 && fn->max_afu_index >= 0) {
  234. dev_err(&dev->dev,
  235. "AFUs are defined but no PASIDs are requested\n");
  236. return -EINVAL;
  237. }
  238. if (fn->max_afu_index > OCXL_MAX_AFU_PER_FUNCTION) {
  239. dev_err(&dev->dev,
  240. "Max AFU index out of architectural limit (%d vs %d)\n",
  241. fn->max_afu_index, OCXL_MAX_AFU_PER_FUNCTION);
  242. return -EINVAL;
  243. }
  244. return 0;
  245. }
  246. int ocxl_config_read_function(struct pci_dev *dev, struct ocxl_fn_config *fn)
  247. {
  248. int rc;
  249. read_pasid(dev, fn);
  250. rc = read_dvsec_tl(dev, fn);
  251. if (rc) {
  252. dev_err(&dev->dev,
  253. "Invalid Transaction Layer DVSEC configuration: %d\n",
  254. rc);
  255. return -ENODEV;
  256. }
  257. rc = read_dvsec_function(dev, fn);
  258. if (rc) {
  259. dev_err(&dev->dev,
  260. "Invalid Function DVSEC configuration: %d\n", rc);
  261. return -ENODEV;
  262. }
  263. rc = read_dvsec_afu_info(dev, fn);
  264. if (rc) {
  265. dev_err(&dev->dev, "Invalid AFU configuration: %d\n", rc);
  266. return -ENODEV;
  267. }
  268. rc = read_dvsec_vendor(dev);
  269. if (rc) {
  270. dev_err(&dev->dev,
  271. "Invalid vendor specific DVSEC configuration: %d\n",
  272. rc);
  273. return -ENODEV;
  274. }
  275. rc = validate_function(dev, fn);
  276. return rc;
  277. }
  278. EXPORT_SYMBOL_GPL(ocxl_config_read_function);
  279. static int read_afu_info(struct pci_dev *dev, struct ocxl_fn_config *fn,
  280. int offset, u32 *data)
  281. {
  282. u32 val;
  283. unsigned long timeout = jiffies + (HZ * OCXL_CFG_TIMEOUT);
  284. int pos = fn->dvsec_afu_info_pos;
  285. /* Protect 'data valid' bit */
  286. if (EXTRACT_BIT(offset, 31)) {
  287. dev_err(&dev->dev, "Invalid offset in AFU info DVSEC\n");
  288. return -EINVAL;
  289. }
  290. pci_write_config_dword(dev, pos + OCXL_DVSEC_AFU_INFO_OFF, offset);
  291. pci_read_config_dword(dev, pos + OCXL_DVSEC_AFU_INFO_OFF, &val);
  292. while (!EXTRACT_BIT(val, 31)) {
  293. if (time_after_eq(jiffies, timeout)) {
  294. dev_err(&dev->dev,
  295. "Timeout while reading AFU info DVSEC (offset=%d)\n",
  296. offset);
  297. return -EBUSY;
  298. }
  299. cpu_relax();
  300. pci_read_config_dword(dev, pos + OCXL_DVSEC_AFU_INFO_OFF, &val);
  301. }
  302. pci_read_config_dword(dev, pos + OCXL_DVSEC_AFU_INFO_DATA, data);
  303. return 0;
  304. }
  305. /**
  306. * read_template_version() - Read the template version from the AFU
  307. * @dev: the device for the AFU
  308. * @fn: the AFU offsets
  309. * @len: outputs the template length
  310. * @version: outputs the major<<8,minor version
  311. *
  312. * Returns 0 on success, negative on failure
  313. */
  314. static int read_template_version(struct pci_dev *dev, struct ocxl_fn_config *fn,
  315. u16 *len, u16 *version)
  316. {
  317. u32 val32;
  318. u8 major, minor;
  319. int rc;
  320. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_VERSION, &val32);
  321. if (rc)
  322. return rc;
  323. *len = EXTRACT_BITS(val32, 16, 31);
  324. major = EXTRACT_BITS(val32, 8, 15);
  325. minor = EXTRACT_BITS(val32, 0, 7);
  326. *version = (major << 8) + minor;
  327. return 0;
  328. }
  329. int ocxl_config_check_afu_index(struct pci_dev *dev,
  330. struct ocxl_fn_config *fn, int afu_idx)
  331. {
  332. int rc;
  333. u16 templ_version;
  334. u16 len, expected_len;
  335. pci_write_config_byte(dev,
  336. fn->dvsec_afu_info_pos + OCXL_DVSEC_AFU_INFO_AFU_IDX,
  337. afu_idx);
  338. rc = read_template_version(dev, fn, &len, &templ_version);
  339. if (rc)
  340. return rc;
  341. /* AFU index map can have holes, in which case we read all 0's */
  342. if (!templ_version && !len)
  343. return 0;
  344. dev_dbg(&dev->dev, "AFU descriptor template version %d.%d\n",
  345. templ_version >> 8, templ_version & 0xFF);
  346. switch (templ_version) {
  347. case 0x0005: // v0.5 was used prior to the spec approval
  348. case 0x0100:
  349. expected_len = OCXL_TEMPL_LEN_1_0;
  350. break;
  351. case 0x0101:
  352. expected_len = OCXL_TEMPL_LEN_1_1;
  353. break;
  354. default:
  355. dev_warn(&dev->dev, "Unknown AFU template version %#x\n",
  356. templ_version);
  357. expected_len = len;
  358. }
  359. if (len != expected_len)
  360. dev_warn(&dev->dev,
  361. "Unexpected template length %#x in AFU information, expected %#x for version %#x\n",
  362. len, expected_len, templ_version);
  363. return 1;
  364. }
  365. static int read_afu_name(struct pci_dev *dev, struct ocxl_fn_config *fn,
  366. struct ocxl_afu_config *afu)
  367. {
  368. int i, rc;
  369. u32 val, *ptr;
  370. BUILD_BUG_ON(OCXL_AFU_NAME_SZ < OCXL_TEMPL_NAME_LEN);
  371. for (i = 0; i < OCXL_TEMPL_NAME_LEN; i += 4) {
  372. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_NAME + i, &val);
  373. if (rc)
  374. return rc;
  375. ptr = (u32 *) &afu->name[i];
  376. *ptr = le32_to_cpu((__force __le32) val);
  377. }
  378. afu->name[OCXL_AFU_NAME_SZ - 1] = '\0'; /* play safe */
  379. return 0;
  380. }
  381. static int read_afu_mmio(struct pci_dev *dev, struct ocxl_fn_config *fn,
  382. struct ocxl_afu_config *afu)
  383. {
  384. int rc;
  385. u32 val;
  386. /*
  387. * Global MMIO
  388. */
  389. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_GLOBAL, &val);
  390. if (rc)
  391. return rc;
  392. afu->global_mmio_bar = EXTRACT_BITS(val, 0, 2);
  393. afu->global_mmio_offset = EXTRACT_BITS(val, 16, 31) << 16;
  394. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_GLOBAL + 4, &val);
  395. if (rc)
  396. return rc;
  397. afu->global_mmio_offset += (u64) val << 32;
  398. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_GLOBAL_SZ, &val);
  399. if (rc)
  400. return rc;
  401. afu->global_mmio_size = val;
  402. /*
  403. * Per-process MMIO
  404. */
  405. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_PP, &val);
  406. if (rc)
  407. return rc;
  408. afu->pp_mmio_bar = EXTRACT_BITS(val, 0, 2);
  409. afu->pp_mmio_offset = EXTRACT_BITS(val, 16, 31) << 16;
  410. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_PP + 4, &val);
  411. if (rc)
  412. return rc;
  413. afu->pp_mmio_offset += (u64) val << 32;
  414. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_MMIO_PP_SZ, &val);
  415. if (rc)
  416. return rc;
  417. afu->pp_mmio_stride = val;
  418. return 0;
  419. }
  420. static int read_afu_control(struct pci_dev *dev, struct ocxl_afu_config *afu)
  421. {
  422. int pos;
  423. u8 val8;
  424. u16 val16;
  425. pos = find_dvsec_afu_ctrl(dev, afu->idx);
  426. if (!pos) {
  427. dev_err(&dev->dev, "Can't find AFU control DVSEC for AFU %d\n",
  428. afu->idx);
  429. return -ENODEV;
  430. }
  431. afu->dvsec_afu_control_pos = pos;
  432. pci_read_config_byte(dev, pos + OCXL_DVSEC_AFU_CTRL_PASID_SUP, &val8);
  433. afu->pasid_supported_log = EXTRACT_BITS(val8, 0, 4);
  434. pci_read_config_word(dev, pos + OCXL_DVSEC_AFU_CTRL_ACTAG_SUP, &val16);
  435. afu->actag_supported = EXTRACT_BITS(val16, 0, 11);
  436. return 0;
  437. }
  438. static bool char_allowed(int c)
  439. {
  440. /*
  441. * Permitted Characters : Alphanumeric, hyphen, underscore, comma
  442. */
  443. if ((c >= 0x30 && c <= 0x39) /* digits */ ||
  444. (c >= 0x41 && c <= 0x5A) /* upper case */ ||
  445. (c >= 0x61 && c <= 0x7A) /* lower case */ ||
  446. c == 0 /* NULL */ ||
  447. c == 0x2D /* - */ ||
  448. c == 0x5F /* _ */ ||
  449. c == 0x2C /* , */)
  450. return true;
  451. return false;
  452. }
  453. static int validate_afu(struct pci_dev *dev, struct ocxl_afu_config *afu)
  454. {
  455. int i;
  456. if (!afu->name[0]) {
  457. dev_err(&dev->dev, "Empty AFU name\n");
  458. return -EINVAL;
  459. }
  460. for (i = 0; i < OCXL_TEMPL_NAME_LEN; i++) {
  461. if (!char_allowed(afu->name[i])) {
  462. dev_err(&dev->dev,
  463. "Invalid character in AFU name\n");
  464. return -EINVAL;
  465. }
  466. }
  467. if (afu->global_mmio_bar != 0 &&
  468. afu->global_mmio_bar != 2 &&
  469. afu->global_mmio_bar != 4) {
  470. dev_err(&dev->dev, "Invalid global MMIO bar number\n");
  471. return -EINVAL;
  472. }
  473. if (afu->pp_mmio_bar != 0 &&
  474. afu->pp_mmio_bar != 2 &&
  475. afu->pp_mmio_bar != 4) {
  476. dev_err(&dev->dev, "Invalid per-process MMIO bar number\n");
  477. return -EINVAL;
  478. }
  479. return 0;
  480. }
  481. /**
  482. * read_afu_lpc_memory_info() - Populate AFU metadata regarding LPC memory
  483. * @dev: the device for the AFU
  484. * @fn: the AFU offsets
  485. * @afu: the AFU struct to populate the LPC metadata into
  486. *
  487. * Returns 0 on success, negative on failure
  488. */
  489. static int read_afu_lpc_memory_info(struct pci_dev *dev,
  490. struct ocxl_fn_config *fn,
  491. struct ocxl_afu_config *afu)
  492. {
  493. int rc;
  494. u32 val32;
  495. u16 templ_version;
  496. u16 templ_len;
  497. u64 total_mem_size = 0;
  498. u64 lpc_mem_size = 0;
  499. afu->lpc_mem_offset = 0;
  500. afu->lpc_mem_size = 0;
  501. afu->special_purpose_mem_offset = 0;
  502. afu->special_purpose_mem_size = 0;
  503. /*
  504. * For AFUs following template v1.0, the LPC memory covers the
  505. * total memory. Its size is a power of 2.
  506. *
  507. * For AFUs with template >= v1.01, the total memory size is
  508. * still a power of 2, but it is split in 2 parts:
  509. * - the LPC memory, whose size can now be anything
  510. * - the remainder memory is a special purpose memory, whose
  511. * definition is AFU-dependent. It is not accessible through
  512. * the usual commands for LPC memory
  513. */
  514. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_ALL_MEM_SZ, &val32);
  515. if (rc)
  516. return rc;
  517. val32 = EXTRACT_BITS(val32, 0, 7);
  518. if (!val32)
  519. return 0; /* No LPC memory */
  520. /*
  521. * The configuration space spec allows for a memory size of up
  522. * to 2^255 bytes.
  523. *
  524. * Current generation hardware uses 56-bit physical addresses,
  525. * but we won't be able to get near close to that, as we won't
  526. * have a hole big enough in the memory map. Let it pass in
  527. * the driver for now. We'll get an error from the firmware
  528. * when trying to configure something too big.
  529. */
  530. total_mem_size = 1ull << val32;
  531. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_LPC_MEM_START, &val32);
  532. if (rc)
  533. return rc;
  534. afu->lpc_mem_offset = val32;
  535. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_LPC_MEM_START + 4, &val32);
  536. if (rc)
  537. return rc;
  538. afu->lpc_mem_offset |= (u64) val32 << 32;
  539. rc = read_template_version(dev, fn, &templ_len, &templ_version);
  540. if (rc)
  541. return rc;
  542. if (templ_version >= 0x0101) {
  543. rc = read_afu_info(dev, fn,
  544. OCXL_DVSEC_TEMPL_LPC_MEM_SZ, &val32);
  545. if (rc)
  546. return rc;
  547. lpc_mem_size = val32;
  548. rc = read_afu_info(dev, fn,
  549. OCXL_DVSEC_TEMPL_LPC_MEM_SZ + 4, &val32);
  550. if (rc)
  551. return rc;
  552. lpc_mem_size |= (u64) val32 << 32;
  553. } else {
  554. lpc_mem_size = total_mem_size;
  555. }
  556. afu->lpc_mem_size = lpc_mem_size;
  557. if (lpc_mem_size < total_mem_size) {
  558. afu->special_purpose_mem_offset =
  559. afu->lpc_mem_offset + lpc_mem_size;
  560. afu->special_purpose_mem_size =
  561. total_mem_size - lpc_mem_size;
  562. }
  563. return 0;
  564. }
  565. int ocxl_config_read_afu(struct pci_dev *dev, struct ocxl_fn_config *fn,
  566. struct ocxl_afu_config *afu, u8 afu_idx)
  567. {
  568. int rc;
  569. u32 val32;
  570. /*
  571. * First, we need to write the AFU idx for the AFU we want to
  572. * access.
  573. */
  574. WARN_ON((afu_idx & OCXL_DVSEC_AFU_IDX_MASK) != afu_idx);
  575. afu->idx = afu_idx;
  576. pci_write_config_byte(dev,
  577. fn->dvsec_afu_info_pos + OCXL_DVSEC_AFU_INFO_AFU_IDX,
  578. afu->idx);
  579. rc = read_afu_name(dev, fn, afu);
  580. if (rc)
  581. return rc;
  582. rc = read_afu_info(dev, fn, OCXL_DVSEC_TEMPL_AFU_VERSION, &val32);
  583. if (rc)
  584. return rc;
  585. afu->version_major = EXTRACT_BITS(val32, 24, 31);
  586. afu->version_minor = EXTRACT_BITS(val32, 16, 23);
  587. afu->afuc_type = EXTRACT_BITS(val32, 14, 15);
  588. afu->afum_type = EXTRACT_BITS(val32, 12, 13);
  589. afu->profile = EXTRACT_BITS(val32, 0, 7);
  590. rc = read_afu_mmio(dev, fn, afu);
  591. if (rc)
  592. return rc;
  593. rc = read_afu_lpc_memory_info(dev, fn, afu);
  594. if (rc)
  595. return rc;
  596. rc = read_afu_control(dev, afu);
  597. if (rc)
  598. return rc;
  599. dev_dbg(&dev->dev, "AFU configuration:\n");
  600. dev_dbg(&dev->dev, " name = %s\n", afu->name);
  601. dev_dbg(&dev->dev, " version = %d.%d\n", afu->version_major,
  602. afu->version_minor);
  603. dev_dbg(&dev->dev, " global mmio bar = %hhu\n", afu->global_mmio_bar);
  604. dev_dbg(&dev->dev, " global mmio offset = %#llx\n",
  605. afu->global_mmio_offset);
  606. dev_dbg(&dev->dev, " global mmio size = %#x\n", afu->global_mmio_size);
  607. dev_dbg(&dev->dev, " pp mmio bar = %hhu\n", afu->pp_mmio_bar);
  608. dev_dbg(&dev->dev, " pp mmio offset = %#llx\n", afu->pp_mmio_offset);
  609. dev_dbg(&dev->dev, " pp mmio stride = %#x\n", afu->pp_mmio_stride);
  610. dev_dbg(&dev->dev, " lpc_mem offset = %#llx\n", afu->lpc_mem_offset);
  611. dev_dbg(&dev->dev, " lpc_mem size = %#llx\n", afu->lpc_mem_size);
  612. dev_dbg(&dev->dev, " special purpose mem offset = %#llx\n",
  613. afu->special_purpose_mem_offset);
  614. dev_dbg(&dev->dev, " special purpose mem size = %#llx\n",
  615. afu->special_purpose_mem_size);
  616. dev_dbg(&dev->dev, " pasid supported (log) = %u\n",
  617. afu->pasid_supported_log);
  618. dev_dbg(&dev->dev, " actag supported = %u\n",
  619. afu->actag_supported);
  620. rc = validate_afu(dev, afu);
  621. return rc;
  622. }
  623. EXPORT_SYMBOL_GPL(ocxl_config_read_afu);
  624. int ocxl_config_get_actag_info(struct pci_dev *dev, u16 *base, u16 *enabled,
  625. u16 *supported)
  626. {
  627. int rc;
  628. /*
  629. * This is really a simple wrapper for the kernel API, to
  630. * avoid an external driver using ocxl as a library to call
  631. * platform-dependent code
  632. */
  633. rc = pnv_ocxl_get_actag(dev, base, enabled, supported);
  634. if (rc) {
  635. dev_err(&dev->dev, "Can't get actag for device: %d\n", rc);
  636. return rc;
  637. }
  638. return 0;
  639. }
  640. EXPORT_SYMBOL_GPL(ocxl_config_get_actag_info);
  641. void ocxl_config_set_afu_actag(struct pci_dev *dev, int pos, int actag_base,
  642. int actag_count)
  643. {
  644. u16 val;
  645. val = actag_count & OCXL_DVSEC_ACTAG_MASK;
  646. pci_write_config_byte(dev, pos + OCXL_DVSEC_AFU_CTRL_ACTAG_EN, val);
  647. val = actag_base & OCXL_DVSEC_ACTAG_MASK;
  648. pci_write_config_dword(dev, pos + OCXL_DVSEC_AFU_CTRL_ACTAG_BASE, val);
  649. }
  650. EXPORT_SYMBOL_GPL(ocxl_config_set_afu_actag);
  651. int ocxl_config_get_pasid_info(struct pci_dev *dev, int *count)
  652. {
  653. return pnv_ocxl_get_pasid_count(dev, count);
  654. }
  655. void ocxl_config_set_afu_pasid(struct pci_dev *dev, int pos, int pasid_base,
  656. u32 pasid_count_log)
  657. {
  658. u8 val8;
  659. u32 val32;
  660. val8 = pasid_count_log & OCXL_DVSEC_PASID_LOG_MASK;
  661. pci_write_config_byte(dev, pos + OCXL_DVSEC_AFU_CTRL_PASID_EN, val8);
  662. pci_read_config_dword(dev, pos + OCXL_DVSEC_AFU_CTRL_PASID_BASE,
  663. &val32);
  664. val32 &= ~OCXL_DVSEC_PASID_MASK;
  665. val32 |= pasid_base & OCXL_DVSEC_PASID_MASK;
  666. pci_write_config_dword(dev, pos + OCXL_DVSEC_AFU_CTRL_PASID_BASE,
  667. val32);
  668. }
  669. EXPORT_SYMBOL_GPL(ocxl_config_set_afu_pasid);
  670. void ocxl_config_set_afu_state(struct pci_dev *dev, int pos, int enable)
  671. {
  672. u8 val;
  673. pci_read_config_byte(dev, pos + OCXL_DVSEC_AFU_CTRL_ENABLE, &val);
  674. if (enable)
  675. val |= 1;
  676. else
  677. val &= 0xFE;
  678. pci_write_config_byte(dev, pos + OCXL_DVSEC_AFU_CTRL_ENABLE, val);
  679. }
  680. EXPORT_SYMBOL_GPL(ocxl_config_set_afu_state);
  681. int ocxl_config_set_TL(struct pci_dev *dev, int tl_dvsec)
  682. {
  683. u32 val;
  684. __be32 *be32ptr;
  685. u8 timers;
  686. int i, rc;
  687. long recv_cap;
  688. char *recv_rate;
  689. /*
  690. * Skip on function != 0, as the TL can only be defined on 0
  691. */
  692. if (PCI_FUNC(dev->devfn) != 0)
  693. return 0;
  694. recv_rate = kzalloc(PNV_OCXL_TL_RATE_BUF_SIZE, GFP_KERNEL);
  695. if (!recv_rate)
  696. return -ENOMEM;
  697. /*
  698. * The spec defines 64 templates for messages in the
  699. * Transaction Layer (TL).
  700. *
  701. * The host and device each support a subset, so we need to
  702. * configure the transmitters on each side to send only
  703. * templates the receiver understands, at a rate the receiver
  704. * can process. Per the spec, template 0 must be supported by
  705. * everybody. That's the template which has been used by the
  706. * host and device so far.
  707. *
  708. * The sending rate limit must be set before the template is
  709. * enabled.
  710. */
  711. /*
  712. * Device -> host
  713. */
  714. rc = pnv_ocxl_get_tl_cap(dev, &recv_cap, recv_rate,
  715. PNV_OCXL_TL_RATE_BUF_SIZE);
  716. if (rc)
  717. goto out;
  718. for (i = 0; i < PNV_OCXL_TL_RATE_BUF_SIZE; i += 4) {
  719. be32ptr = (__be32 *) &recv_rate[i];
  720. pci_write_config_dword(dev,
  721. tl_dvsec + OCXL_DVSEC_TL_SEND_RATE + i,
  722. be32_to_cpu(*be32ptr));
  723. }
  724. val = recv_cap >> 32;
  725. pci_write_config_dword(dev, tl_dvsec + OCXL_DVSEC_TL_SEND_CAP, val);
  726. val = recv_cap & GENMASK(31, 0);
  727. pci_write_config_dword(dev, tl_dvsec + OCXL_DVSEC_TL_SEND_CAP + 4, val);
  728. /*
  729. * Host -> device
  730. */
  731. for (i = 0; i < PNV_OCXL_TL_RATE_BUF_SIZE; i += 4) {
  732. pci_read_config_dword(dev,
  733. tl_dvsec + OCXL_DVSEC_TL_RECV_RATE + i,
  734. &val);
  735. be32ptr = (__be32 *) &recv_rate[i];
  736. *be32ptr = cpu_to_be32(val);
  737. }
  738. pci_read_config_dword(dev, tl_dvsec + OCXL_DVSEC_TL_RECV_CAP, &val);
  739. recv_cap = (long) val << 32;
  740. pci_read_config_dword(dev, tl_dvsec + OCXL_DVSEC_TL_RECV_CAP + 4, &val);
  741. recv_cap |= val;
  742. rc = pnv_ocxl_set_tl_conf(dev, recv_cap, __pa(recv_rate),
  743. PNV_OCXL_TL_RATE_BUF_SIZE);
  744. if (rc)
  745. goto out;
  746. /*
  747. * Opencapi commands needing to be retried are classified per
  748. * the TL in 2 groups: short and long commands.
  749. *
  750. * The short back off timer it not used for now. It will be
  751. * for opencapi 4.0.
  752. *
  753. * The long back off timer is typically used when an AFU hits
  754. * a page fault but the NPU is already processing one. So the
  755. * AFU needs to wait before it can resubmit. Having a value
  756. * too low doesn't break anything, but can generate extra
  757. * traffic on the link.
  758. * We set it to 1.6 us for now. It's shorter than, but in the
  759. * same order of magnitude as the time spent to process a page
  760. * fault.
  761. */
  762. timers = 0x2 << 4; /* long timer = 1.6 us */
  763. pci_write_config_byte(dev, tl_dvsec + OCXL_DVSEC_TL_BACKOFF_TIMERS,
  764. timers);
  765. rc = 0;
  766. out:
  767. kfree(recv_rate);
  768. return rc;
  769. }
  770. EXPORT_SYMBOL_GPL(ocxl_config_set_TL);
  771. int ocxl_config_terminate_pasid(struct pci_dev *dev, int afu_control, int pasid)
  772. {
  773. u32 val;
  774. unsigned long timeout;
  775. pci_read_config_dword(dev, afu_control + OCXL_DVSEC_AFU_CTRL_TERM_PASID,
  776. &val);
  777. if (EXTRACT_BIT(val, 20)) {
  778. dev_err(&dev->dev,
  779. "Can't terminate PASID %#x, previous termination didn't complete\n",
  780. pasid);
  781. return -EBUSY;
  782. }
  783. val &= ~OCXL_DVSEC_PASID_MASK;
  784. val |= pasid & OCXL_DVSEC_PASID_MASK;
  785. val |= BIT(20);
  786. pci_write_config_dword(dev,
  787. afu_control + OCXL_DVSEC_AFU_CTRL_TERM_PASID,
  788. val);
  789. timeout = jiffies + (HZ * OCXL_CFG_TIMEOUT);
  790. pci_read_config_dword(dev, afu_control + OCXL_DVSEC_AFU_CTRL_TERM_PASID,
  791. &val);
  792. while (EXTRACT_BIT(val, 20)) {
  793. if (time_after_eq(jiffies, timeout)) {
  794. dev_err(&dev->dev,
  795. "Timeout while waiting for AFU to terminate PASID %#x\n",
  796. pasid);
  797. return -EBUSY;
  798. }
  799. cpu_relax();
  800. pci_read_config_dword(dev,
  801. afu_control + OCXL_DVSEC_AFU_CTRL_TERM_PASID,
  802. &val);
  803. }
  804. return 0;
  805. }
  806. EXPORT_SYMBOL_GPL(ocxl_config_terminate_pasid);
  807. void ocxl_config_set_actag(struct pci_dev *dev, int func_dvsec, u32 tag_first,
  808. u32 tag_count)
  809. {
  810. u32 val;
  811. val = (tag_first & OCXL_DVSEC_ACTAG_MASK) << 16;
  812. val |= tag_count & OCXL_DVSEC_ACTAG_MASK;
  813. pci_write_config_dword(dev, func_dvsec + OCXL_DVSEC_FUNC_OFF_ACTAG,
  814. val);
  815. }
  816. EXPORT_SYMBOL_GPL(ocxl_config_set_actag);