of.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2015 IBM Corp.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/slab.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_platform.h>
  11. #include "cxl.h"
  12. static const __be32 *read_prop_string(const struct device_node *np,
  13. const char *prop_name)
  14. {
  15. const __be32 *prop;
  16. prop = of_get_property(np, prop_name, NULL);
  17. if (cxl_verbose && prop)
  18. pr_info("%s: %s\n", prop_name, (char *) prop);
  19. return prop;
  20. }
  21. static const __be32 *read_prop_dword(const struct device_node *np,
  22. const char *prop_name, u32 *val)
  23. {
  24. const __be32 *prop;
  25. prop = of_get_property(np, prop_name, NULL);
  26. if (prop)
  27. *val = be32_to_cpu(prop[0]);
  28. if (cxl_verbose && prop)
  29. pr_info("%s: %#x (%u)\n", prop_name, *val, *val);
  30. return prop;
  31. }
  32. static const __be64 *read_prop64_dword(const struct device_node *np,
  33. const char *prop_name, u64 *val)
  34. {
  35. const __be64 *prop;
  36. prop = of_get_property(np, prop_name, NULL);
  37. if (prop)
  38. *val = be64_to_cpu(prop[0]);
  39. if (cxl_verbose && prop)
  40. pr_info("%s: %#llx (%llu)\n", prop_name, *val, *val);
  41. return prop;
  42. }
  43. static int read_handle(struct device_node *np, u64 *handle)
  44. {
  45. const __be32 *prop;
  46. u64 size;
  47. /* Get address and size of the node */
  48. prop = of_get_address(np, 0, &size, NULL);
  49. if (size)
  50. return -EINVAL;
  51. /* Helper to read a big number; size is in cells (not bytes) */
  52. *handle = of_read_number(prop, of_n_addr_cells(np));
  53. return 0;
  54. }
  55. static int read_phys_addr(struct device_node *np, char *prop_name,
  56. struct cxl_afu *afu)
  57. {
  58. int i, len, entry_size, naddr, nsize, type;
  59. u64 addr, size;
  60. const __be32 *prop;
  61. naddr = of_n_addr_cells(np);
  62. nsize = of_n_size_cells(np);
  63. prop = of_get_property(np, prop_name, &len);
  64. if (prop) {
  65. entry_size = naddr + nsize;
  66. for (i = 0; i < (len / 4); i += entry_size, prop += entry_size) {
  67. type = be32_to_cpu(prop[0]);
  68. addr = of_read_number(prop, naddr);
  69. size = of_read_number(&prop[naddr], nsize);
  70. switch (type) {
  71. case 0: /* unit address */
  72. afu->guest->handle = addr;
  73. break;
  74. case 1: /* p2 area */
  75. afu->guest->p2n_phys += addr;
  76. afu->guest->p2n_size = size;
  77. break;
  78. case 2: /* problem state area */
  79. afu->psn_phys += addr;
  80. afu->adapter->ps_size = size;
  81. break;
  82. default:
  83. pr_err("Invalid address type %d found in %s property of AFU\n",
  84. type, prop_name);
  85. return -EINVAL;
  86. }
  87. if (cxl_verbose)
  88. pr_info("%s: %#x %#llx (size %#llx)\n",
  89. prop_name, type, addr, size);
  90. }
  91. }
  92. return 0;
  93. }
  94. static int read_vpd(struct cxl *adapter, struct cxl_afu *afu)
  95. {
  96. char vpd[256];
  97. int rc;
  98. size_t len = sizeof(vpd);
  99. memset(vpd, 0, len);
  100. if (adapter)
  101. rc = cxl_guest_read_adapter_vpd(adapter, vpd, len);
  102. else
  103. rc = cxl_guest_read_afu_vpd(afu, vpd, len);
  104. if (rc > 0) {
  105. cxl_dump_debug_buffer(vpd, rc);
  106. rc = 0;
  107. }
  108. return rc;
  109. }
  110. int cxl_of_read_afu_handle(struct cxl_afu *afu, struct device_node *afu_np)
  111. {
  112. if (read_handle(afu_np, &afu->guest->handle))
  113. return -EINVAL;
  114. pr_devel("AFU handle: 0x%.16llx\n", afu->guest->handle);
  115. return 0;
  116. }
  117. int cxl_of_read_afu_properties(struct cxl_afu *afu, struct device_node *np)
  118. {
  119. int i, len, rc;
  120. char *p;
  121. const __be32 *prop;
  122. u16 device_id, vendor_id;
  123. u32 val = 0, class_code;
  124. /* Properties are read in the same order as listed in PAPR */
  125. if (cxl_verbose) {
  126. pr_info("Dump of the 'ibm,coherent-platform-function' node properties:\n");
  127. prop = of_get_property(np, "compatible", &len);
  128. i = 0;
  129. while (i < len) {
  130. p = (char *) prop + i;
  131. pr_info("compatible: %s\n", p);
  132. i += strlen(p) + 1;
  133. }
  134. read_prop_string(np, "name");
  135. }
  136. rc = read_phys_addr(np, "reg", afu);
  137. if (rc)
  138. return rc;
  139. rc = read_phys_addr(np, "assigned-addresses", afu);
  140. if (rc)
  141. return rc;
  142. if (afu->psn_phys == 0)
  143. afu->psa = false;
  144. else
  145. afu->psa = true;
  146. if (cxl_verbose) {
  147. read_prop_string(np, "ibm,loc-code");
  148. read_prop_string(np, "device_type");
  149. }
  150. read_prop_dword(np, "ibm,#processes", &afu->max_procs_virtualised);
  151. if (cxl_verbose) {
  152. read_prop_dword(np, "ibm,scratchpad-size", &val);
  153. read_prop_dword(np, "ibm,programmable", &val);
  154. read_prop_string(np, "ibm,phandle");
  155. read_vpd(NULL, afu);
  156. }
  157. read_prop_dword(np, "ibm,max-ints-per-process", &afu->guest->max_ints);
  158. afu->irqs_max = afu->guest->max_ints;
  159. prop = read_prop_dword(np, "ibm,min-ints-per-process", &afu->pp_irqs);
  160. if (prop) {
  161. /* One extra interrupt for the PSL interrupt is already
  162. * included. Remove it now to keep only AFU interrupts and
  163. * match the native case.
  164. */
  165. afu->pp_irqs--;
  166. }
  167. if (cxl_verbose) {
  168. read_prop_dword(np, "ibm,max-ints", &val);
  169. read_prop_dword(np, "ibm,vpd-size", &val);
  170. }
  171. read_prop64_dword(np, "ibm,error-buffer-size", &afu->eb_len);
  172. afu->eb_offset = 0;
  173. if (cxl_verbose)
  174. read_prop_dword(np, "ibm,config-record-type", &val);
  175. read_prop64_dword(np, "ibm,config-record-size", &afu->crs_len);
  176. afu->crs_offset = 0;
  177. read_prop_dword(np, "ibm,#config-records", &afu->crs_num);
  178. if (cxl_verbose) {
  179. for (i = 0; i < afu->crs_num; i++) {
  180. rc = cxl_ops->afu_cr_read16(afu, i, PCI_DEVICE_ID,
  181. &device_id);
  182. if (!rc)
  183. pr_info("record %d - device-id: %#x\n",
  184. i, device_id);
  185. rc = cxl_ops->afu_cr_read16(afu, i, PCI_VENDOR_ID,
  186. &vendor_id);
  187. if (!rc)
  188. pr_info("record %d - vendor-id: %#x\n",
  189. i, vendor_id);
  190. rc = cxl_ops->afu_cr_read32(afu, i, PCI_CLASS_REVISION,
  191. &class_code);
  192. if (!rc) {
  193. class_code >>= 8;
  194. pr_info("record %d - class-code: %#x\n",
  195. i, class_code);
  196. }
  197. }
  198. read_prop_dword(np, "ibm,function-number", &val);
  199. read_prop_dword(np, "ibm,privileged-function", &val);
  200. read_prop_dword(np, "vendor-id", &val);
  201. read_prop_dword(np, "device-id", &val);
  202. read_prop_dword(np, "revision-id", &val);
  203. read_prop_dword(np, "class-code", &val);
  204. read_prop_dword(np, "subsystem-vendor-id", &val);
  205. read_prop_dword(np, "subsystem-id", &val);
  206. }
  207. /*
  208. * if "ibm,process-mmio" doesn't exist then per-process mmio is
  209. * not supported
  210. */
  211. val = 0;
  212. prop = read_prop_dword(np, "ibm,process-mmio", &val);
  213. if (prop && val == 1)
  214. afu->pp_psa = true;
  215. else
  216. afu->pp_psa = false;
  217. if (cxl_verbose) {
  218. read_prop_dword(np, "ibm,supports-aur", &val);
  219. read_prop_dword(np, "ibm,supports-csrp", &val);
  220. read_prop_dword(np, "ibm,supports-prr", &val);
  221. }
  222. prop = read_prop_dword(np, "ibm,function-error-interrupt", &val);
  223. if (prop)
  224. afu->serr_hwirq = val;
  225. pr_devel("AFU handle: %#llx\n", afu->guest->handle);
  226. pr_devel("p2n_phys: %#llx (size %#llx)\n",
  227. afu->guest->p2n_phys, afu->guest->p2n_size);
  228. pr_devel("psn_phys: %#llx (size %#llx)\n",
  229. afu->psn_phys, afu->adapter->ps_size);
  230. pr_devel("Max number of processes virtualised=%i\n",
  231. afu->max_procs_virtualised);
  232. pr_devel("Per-process irqs min=%i, max=%i\n", afu->pp_irqs,
  233. afu->irqs_max);
  234. pr_devel("Slice error interrupt=%#lx\n", afu->serr_hwirq);
  235. return 0;
  236. }
  237. static int read_adapter_irq_config(struct cxl *adapter, struct device_node *np)
  238. {
  239. const __be32 *ranges;
  240. int len, nranges, i;
  241. struct irq_avail *cur;
  242. ranges = of_get_property(np, "interrupt-ranges", &len);
  243. if (ranges == NULL || len < (2 * sizeof(int)))
  244. return -EINVAL;
  245. /*
  246. * encoded array of two cells per entry, each cell encoded as
  247. * with encode-int
  248. */
  249. nranges = len / (2 * sizeof(int));
  250. if (nranges == 0 || (nranges * 2 * sizeof(int)) != len)
  251. return -EINVAL;
  252. adapter->guest->irq_avail = kcalloc(nranges, sizeof(struct irq_avail),
  253. GFP_KERNEL);
  254. if (adapter->guest->irq_avail == NULL)
  255. return -ENOMEM;
  256. adapter->guest->irq_base_offset = be32_to_cpu(ranges[0]);
  257. for (i = 0; i < nranges; i++) {
  258. cur = &adapter->guest->irq_avail[i];
  259. cur->offset = be32_to_cpu(ranges[i * 2]);
  260. cur->range = be32_to_cpu(ranges[i * 2 + 1]);
  261. cur->bitmap = bitmap_zalloc(cur->range, GFP_KERNEL);
  262. if (cur->bitmap == NULL)
  263. goto err;
  264. if (cur->offset < adapter->guest->irq_base_offset)
  265. adapter->guest->irq_base_offset = cur->offset;
  266. if (cxl_verbose)
  267. pr_info("available IRQ range: %#lx-%#lx (%lu)\n",
  268. cur->offset, cur->offset + cur->range - 1,
  269. cur->range);
  270. }
  271. adapter->guest->irq_nranges = nranges;
  272. spin_lock_init(&adapter->guest->irq_alloc_lock);
  273. return 0;
  274. err:
  275. for (i--; i >= 0; i--) {
  276. cur = &adapter->guest->irq_avail[i];
  277. bitmap_free(cur->bitmap);
  278. }
  279. kfree(adapter->guest->irq_avail);
  280. adapter->guest->irq_avail = NULL;
  281. return -ENOMEM;
  282. }
  283. int cxl_of_read_adapter_handle(struct cxl *adapter, struct device_node *np)
  284. {
  285. if (read_handle(np, &adapter->guest->handle))
  286. return -EINVAL;
  287. pr_devel("Adapter handle: 0x%.16llx\n", adapter->guest->handle);
  288. return 0;
  289. }
  290. int cxl_of_read_adapter_properties(struct cxl *adapter, struct device_node *np)
  291. {
  292. int rc, len, naddr, i;
  293. char *p;
  294. const __be32 *prop;
  295. u32 val = 0;
  296. /* Properties are read in the same order as listed in PAPR */
  297. naddr = of_n_addr_cells(np);
  298. if (cxl_verbose) {
  299. pr_info("Dump of the 'ibm,coherent-platform-facility' node properties:\n");
  300. read_prop_dword(np, "#address-cells", &val);
  301. read_prop_dword(np, "#size-cells", &val);
  302. prop = of_get_property(np, "compatible", &len);
  303. i = 0;
  304. while (i < len) {
  305. p = (char *) prop + i;
  306. pr_info("compatible: %s\n", p);
  307. i += strlen(p) + 1;
  308. }
  309. read_prop_string(np, "name");
  310. read_prop_string(np, "model");
  311. prop = of_get_property(np, "reg", NULL);
  312. if (prop) {
  313. pr_info("reg: addr:%#llx size:%#x\n",
  314. of_read_number(prop, naddr),
  315. be32_to_cpu(prop[naddr]));
  316. }
  317. read_prop_string(np, "ibm,loc-code");
  318. }
  319. if ((rc = read_adapter_irq_config(adapter, np)))
  320. return rc;
  321. if (cxl_verbose) {
  322. read_prop_string(np, "device_type");
  323. read_prop_string(np, "ibm,phandle");
  324. }
  325. prop = read_prop_dword(np, "ibm,caia-version", &val);
  326. if (prop) {
  327. adapter->caia_major = (val & 0xFF00) >> 8;
  328. adapter->caia_minor = val & 0xFF;
  329. }
  330. prop = read_prop_dword(np, "ibm,psl-revision", &val);
  331. if (prop)
  332. adapter->psl_rev = val;
  333. prop = read_prop_string(np, "status");
  334. if (prop) {
  335. adapter->guest->status = kasprintf(GFP_KERNEL, "%s", (char *) prop);
  336. if (adapter->guest->status == NULL)
  337. return -ENOMEM;
  338. }
  339. prop = read_prop_dword(np, "vendor-id", &val);
  340. if (prop)
  341. adapter->guest->vendor = val;
  342. prop = read_prop_dword(np, "device-id", &val);
  343. if (prop)
  344. adapter->guest->device = val;
  345. if (cxl_verbose) {
  346. read_prop_dword(np, "ibm,privileged-facility", &val);
  347. read_prop_dword(np, "revision-id", &val);
  348. read_prop_dword(np, "class-code", &val);
  349. }
  350. prop = read_prop_dword(np, "subsystem-vendor-id", &val);
  351. if (prop)
  352. adapter->guest->subsystem_vendor = val;
  353. prop = read_prop_dword(np, "subsystem-id", &val);
  354. if (prop)
  355. adapter->guest->subsystem = val;
  356. if (cxl_verbose)
  357. read_vpd(adapter, NULL);
  358. return 0;
  359. }
  360. static int cxl_of_remove(struct platform_device *pdev)
  361. {
  362. struct cxl *adapter;
  363. int afu;
  364. adapter = dev_get_drvdata(&pdev->dev);
  365. for (afu = 0; afu < adapter->slices; afu++)
  366. cxl_guest_remove_afu(adapter->afu[afu]);
  367. cxl_guest_remove_adapter(adapter);
  368. return 0;
  369. }
  370. static void cxl_of_shutdown(struct platform_device *pdev)
  371. {
  372. cxl_of_remove(pdev);
  373. }
  374. int cxl_of_probe(struct platform_device *pdev)
  375. {
  376. struct device_node *np = NULL;
  377. struct device_node *afu_np = NULL;
  378. struct cxl *adapter = NULL;
  379. int ret;
  380. int slice = 0, slice_ok = 0;
  381. pr_devel("in %s\n", __func__);
  382. np = pdev->dev.of_node;
  383. if (np == NULL)
  384. return -ENODEV;
  385. /* init adapter */
  386. adapter = cxl_guest_init_adapter(np, pdev);
  387. if (IS_ERR(adapter)) {
  388. dev_err(&pdev->dev, "guest_init_adapter failed: %li\n", PTR_ERR(adapter));
  389. return PTR_ERR(adapter);
  390. }
  391. /* init afu */
  392. for_each_child_of_node(np, afu_np) {
  393. if ((ret = cxl_guest_init_afu(adapter, slice, afu_np)))
  394. dev_err(&pdev->dev, "AFU %i failed to initialise: %i\n",
  395. slice, ret);
  396. else
  397. slice_ok++;
  398. slice++;
  399. }
  400. if (slice_ok == 0) {
  401. dev_info(&pdev->dev, "No active AFU");
  402. adapter->slices = 0;
  403. }
  404. return 0;
  405. }
  406. static const struct of_device_id cxl_of_match[] = {
  407. { .compatible = "ibm,coherent-platform-facility",},
  408. {},
  409. };
  410. MODULE_DEVICE_TABLE(of, cxl_of_match);
  411. struct platform_driver cxl_of_driver = {
  412. .driver = {
  413. .name = "cxl_of",
  414. .of_match_table = cxl_of_match,
  415. .owner = THIS_MODULE
  416. },
  417. .probe = cxl_of_probe,
  418. .remove = cxl_of_remove,
  419. .shutdown = cxl_of_shutdown,
  420. };