of.c 12 KB

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