pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright 2017 IBM Corp.
  3. #include <linux/module.h>
  4. #include <linux/pci.h>
  5. #include <linux/idr.h>
  6. #include <asm/pnv-ocxl.h>
  7. #include "ocxl_internal.h"
  8. /*
  9. * Any opencapi device which wants to use this 'generic' driver should
  10. * use the 0x062B device ID. Vendors should define the subsystem
  11. * vendor/device ID to help differentiate devices.
  12. */
  13. static const struct pci_device_id ocxl_pci_tbl[] = {
  14. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x062B), },
  15. { }
  16. };
  17. MODULE_DEVICE_TABLE(pci, ocxl_pci_tbl);
  18. static struct ocxl_fn *ocxl_fn_get(struct ocxl_fn *fn)
  19. {
  20. return (get_device(&fn->dev) == NULL) ? NULL : fn;
  21. }
  22. static void ocxl_fn_put(struct ocxl_fn *fn)
  23. {
  24. put_device(&fn->dev);
  25. }
  26. struct ocxl_afu *ocxl_afu_get(struct ocxl_afu *afu)
  27. {
  28. return (get_device(&afu->dev) == NULL) ? NULL : afu;
  29. }
  30. void ocxl_afu_put(struct ocxl_afu *afu)
  31. {
  32. put_device(&afu->dev);
  33. }
  34. static struct ocxl_afu *alloc_afu(struct ocxl_fn *fn)
  35. {
  36. struct ocxl_afu *afu;
  37. afu = kzalloc(sizeof(struct ocxl_afu), GFP_KERNEL);
  38. if (!afu)
  39. return NULL;
  40. mutex_init(&afu->contexts_lock);
  41. mutex_init(&afu->afu_control_lock);
  42. idr_init(&afu->contexts_idr);
  43. afu->fn = fn;
  44. ocxl_fn_get(fn);
  45. return afu;
  46. }
  47. static void free_afu(struct ocxl_afu *afu)
  48. {
  49. idr_destroy(&afu->contexts_idr);
  50. ocxl_fn_put(afu->fn);
  51. kfree(afu);
  52. }
  53. static void free_afu_dev(struct device *dev)
  54. {
  55. struct ocxl_afu *afu = to_ocxl_afu(dev);
  56. ocxl_unregister_afu(afu);
  57. free_afu(afu);
  58. }
  59. static int set_afu_device(struct ocxl_afu *afu, const char *location)
  60. {
  61. struct ocxl_fn *fn = afu->fn;
  62. int rc;
  63. afu->dev.parent = &fn->dev;
  64. afu->dev.release = free_afu_dev;
  65. rc = dev_set_name(&afu->dev, "%s.%s.%hhu", afu->config.name, location,
  66. afu->config.idx);
  67. return rc;
  68. }
  69. static int assign_afu_actag(struct ocxl_afu *afu, struct pci_dev *dev)
  70. {
  71. struct ocxl_fn *fn = afu->fn;
  72. int actag_count, actag_offset;
  73. /*
  74. * if there were not enough actags for the function, each afu
  75. * reduces its count as well
  76. */
  77. actag_count = afu->config.actag_supported *
  78. fn->actag_enabled / fn->actag_supported;
  79. actag_offset = ocxl_actag_afu_alloc(fn, actag_count);
  80. if (actag_offset < 0) {
  81. dev_err(&afu->dev, "Can't allocate %d actags for AFU: %d\n",
  82. actag_count, actag_offset);
  83. return actag_offset;
  84. }
  85. afu->actag_base = fn->actag_base + actag_offset;
  86. afu->actag_enabled = actag_count;
  87. ocxl_config_set_afu_actag(dev, afu->config.dvsec_afu_control_pos,
  88. afu->actag_base, afu->actag_enabled);
  89. dev_dbg(&afu->dev, "actag base=%d enabled=%d\n",
  90. afu->actag_base, afu->actag_enabled);
  91. return 0;
  92. }
  93. static void reclaim_afu_actag(struct ocxl_afu *afu)
  94. {
  95. struct ocxl_fn *fn = afu->fn;
  96. int start_offset, size;
  97. start_offset = afu->actag_base - fn->actag_base;
  98. size = afu->actag_enabled;
  99. ocxl_actag_afu_free(afu->fn, start_offset, size);
  100. }
  101. static int assign_afu_pasid(struct ocxl_afu *afu, struct pci_dev *dev)
  102. {
  103. struct ocxl_fn *fn = afu->fn;
  104. int pasid_count, pasid_offset;
  105. /*
  106. * We only support the case where the function configuration
  107. * requested enough PASIDs to cover all AFUs.
  108. */
  109. pasid_count = 1 << afu->config.pasid_supported_log;
  110. pasid_offset = ocxl_pasid_afu_alloc(fn, pasid_count);
  111. if (pasid_offset < 0) {
  112. dev_err(&afu->dev, "Can't allocate %d PASIDs for AFU: %d\n",
  113. pasid_count, pasid_offset);
  114. return pasid_offset;
  115. }
  116. afu->pasid_base = fn->pasid_base + pasid_offset;
  117. afu->pasid_count = 0;
  118. afu->pasid_max = pasid_count;
  119. ocxl_config_set_afu_pasid(dev, afu->config.dvsec_afu_control_pos,
  120. afu->pasid_base,
  121. afu->config.pasid_supported_log);
  122. dev_dbg(&afu->dev, "PASID base=%d, enabled=%d\n",
  123. afu->pasid_base, pasid_count);
  124. return 0;
  125. }
  126. static void reclaim_afu_pasid(struct ocxl_afu *afu)
  127. {
  128. struct ocxl_fn *fn = afu->fn;
  129. int start_offset, size;
  130. start_offset = afu->pasid_base - fn->pasid_base;
  131. size = 1 << afu->config.pasid_supported_log;
  132. ocxl_pasid_afu_free(afu->fn, start_offset, size);
  133. }
  134. static int reserve_fn_bar(struct ocxl_fn *fn, int bar)
  135. {
  136. struct pci_dev *dev = to_pci_dev(fn->dev.parent);
  137. int rc, idx;
  138. if (bar != 0 && bar != 2 && bar != 4)
  139. return -EINVAL;
  140. idx = bar >> 1;
  141. if (fn->bar_used[idx]++ == 0) {
  142. rc = pci_request_region(dev, bar, "ocxl");
  143. if (rc)
  144. return rc;
  145. }
  146. return 0;
  147. }
  148. static void release_fn_bar(struct ocxl_fn *fn, int bar)
  149. {
  150. struct pci_dev *dev = to_pci_dev(fn->dev.parent);
  151. int idx;
  152. if (bar != 0 && bar != 2 && bar != 4)
  153. return;
  154. idx = bar >> 1;
  155. if (--fn->bar_used[idx] == 0)
  156. pci_release_region(dev, bar);
  157. WARN_ON(fn->bar_used[idx] < 0);
  158. }
  159. static int map_mmio_areas(struct ocxl_afu *afu, struct pci_dev *dev)
  160. {
  161. int rc;
  162. rc = reserve_fn_bar(afu->fn, afu->config.global_mmio_bar);
  163. if (rc)
  164. return rc;
  165. rc = reserve_fn_bar(afu->fn, afu->config.pp_mmio_bar);
  166. if (rc) {
  167. release_fn_bar(afu->fn, afu->config.global_mmio_bar);
  168. return rc;
  169. }
  170. afu->global_mmio_start =
  171. pci_resource_start(dev, afu->config.global_mmio_bar) +
  172. afu->config.global_mmio_offset;
  173. afu->pp_mmio_start =
  174. pci_resource_start(dev, afu->config.pp_mmio_bar) +
  175. afu->config.pp_mmio_offset;
  176. afu->global_mmio_ptr = ioremap(afu->global_mmio_start,
  177. afu->config.global_mmio_size);
  178. if (!afu->global_mmio_ptr) {
  179. release_fn_bar(afu->fn, afu->config.pp_mmio_bar);
  180. release_fn_bar(afu->fn, afu->config.global_mmio_bar);
  181. dev_err(&dev->dev, "Error mapping global mmio area\n");
  182. return -ENOMEM;
  183. }
  184. /*
  185. * Leave an empty page between the per-process mmio area and
  186. * the AFU interrupt mappings
  187. */
  188. afu->irq_base_offset = afu->config.pp_mmio_stride + PAGE_SIZE;
  189. return 0;
  190. }
  191. static void unmap_mmio_areas(struct ocxl_afu *afu)
  192. {
  193. if (afu->global_mmio_ptr) {
  194. iounmap(afu->global_mmio_ptr);
  195. afu->global_mmio_ptr = NULL;
  196. }
  197. afu->global_mmio_start = 0;
  198. afu->pp_mmio_start = 0;
  199. release_fn_bar(afu->fn, afu->config.pp_mmio_bar);
  200. release_fn_bar(afu->fn, afu->config.global_mmio_bar);
  201. }
  202. static int configure_afu(struct ocxl_afu *afu, u8 afu_idx, struct pci_dev *dev)
  203. {
  204. int rc;
  205. rc = ocxl_config_read_afu(dev, &afu->fn->config, &afu->config, afu_idx);
  206. if (rc)
  207. return rc;
  208. rc = set_afu_device(afu, dev_name(&dev->dev));
  209. if (rc)
  210. return rc;
  211. rc = assign_afu_actag(afu, dev);
  212. if (rc)
  213. return rc;
  214. rc = assign_afu_pasid(afu, dev);
  215. if (rc) {
  216. reclaim_afu_actag(afu);
  217. return rc;
  218. }
  219. rc = map_mmio_areas(afu, dev);
  220. if (rc) {
  221. reclaim_afu_pasid(afu);
  222. reclaim_afu_actag(afu);
  223. return rc;
  224. }
  225. return 0;
  226. }
  227. static void deconfigure_afu(struct ocxl_afu *afu)
  228. {
  229. unmap_mmio_areas(afu);
  230. reclaim_afu_pasid(afu);
  231. reclaim_afu_actag(afu);
  232. }
  233. static int activate_afu(struct pci_dev *dev, struct ocxl_afu *afu)
  234. {
  235. int rc;
  236. ocxl_config_set_afu_state(dev, afu->config.dvsec_afu_control_pos, 1);
  237. /*
  238. * Char device creation is the last step, as processes can
  239. * call our driver immediately, so all our inits must be finished.
  240. */
  241. rc = ocxl_create_cdev(afu);
  242. if (rc)
  243. return rc;
  244. return 0;
  245. }
  246. static void deactivate_afu(struct ocxl_afu *afu)
  247. {
  248. struct pci_dev *dev = to_pci_dev(afu->fn->dev.parent);
  249. ocxl_destroy_cdev(afu);
  250. ocxl_config_set_afu_state(dev, afu->config.dvsec_afu_control_pos, 0);
  251. }
  252. static int init_afu(struct pci_dev *dev, struct ocxl_fn *fn, u8 afu_idx)
  253. {
  254. int rc;
  255. struct ocxl_afu *afu;
  256. afu = alloc_afu(fn);
  257. if (!afu)
  258. return -ENOMEM;
  259. rc = configure_afu(afu, afu_idx, dev);
  260. if (rc) {
  261. free_afu(afu);
  262. return rc;
  263. }
  264. rc = ocxl_register_afu(afu);
  265. if (rc)
  266. goto err;
  267. rc = ocxl_sysfs_add_afu(afu);
  268. if (rc)
  269. goto err;
  270. rc = activate_afu(dev, afu);
  271. if (rc)
  272. goto err_sys;
  273. list_add_tail(&afu->list, &fn->afu_list);
  274. return 0;
  275. err_sys:
  276. ocxl_sysfs_remove_afu(afu);
  277. err:
  278. deconfigure_afu(afu);
  279. device_unregister(&afu->dev);
  280. return rc;
  281. }
  282. static void remove_afu(struct ocxl_afu *afu)
  283. {
  284. list_del(&afu->list);
  285. ocxl_context_detach_all(afu);
  286. deactivate_afu(afu);
  287. ocxl_sysfs_remove_afu(afu);
  288. deconfigure_afu(afu);
  289. device_unregister(&afu->dev);
  290. }
  291. static struct ocxl_fn *alloc_function(struct pci_dev *dev)
  292. {
  293. struct ocxl_fn *fn;
  294. fn = kzalloc(sizeof(struct ocxl_fn), GFP_KERNEL);
  295. if (!fn)
  296. return NULL;
  297. INIT_LIST_HEAD(&fn->afu_list);
  298. INIT_LIST_HEAD(&fn->pasid_list);
  299. INIT_LIST_HEAD(&fn->actag_list);
  300. return fn;
  301. }
  302. static void free_function(struct ocxl_fn *fn)
  303. {
  304. WARN_ON(!list_empty(&fn->afu_list));
  305. WARN_ON(!list_empty(&fn->pasid_list));
  306. kfree(fn);
  307. }
  308. static void free_function_dev(struct device *dev)
  309. {
  310. struct ocxl_fn *fn = to_ocxl_function(dev);
  311. free_function(fn);
  312. }
  313. static int set_function_device(struct ocxl_fn *fn, struct pci_dev *dev)
  314. {
  315. int rc;
  316. fn->dev.parent = &dev->dev;
  317. fn->dev.release = free_function_dev;
  318. rc = dev_set_name(&fn->dev, "ocxlfn.%s", dev_name(&dev->dev));
  319. if (rc)
  320. return rc;
  321. pci_set_drvdata(dev, fn);
  322. return 0;
  323. }
  324. static int assign_function_actag(struct ocxl_fn *fn)
  325. {
  326. struct pci_dev *dev = to_pci_dev(fn->dev.parent);
  327. u16 base, enabled, supported;
  328. int rc;
  329. rc = ocxl_config_get_actag_info(dev, &base, &enabled, &supported);
  330. if (rc)
  331. return rc;
  332. fn->actag_base = base;
  333. fn->actag_enabled = enabled;
  334. fn->actag_supported = supported;
  335. ocxl_config_set_actag(dev, fn->config.dvsec_function_pos,
  336. fn->actag_base, fn->actag_enabled);
  337. dev_dbg(&fn->dev, "actag range starting at %d, enabled %d\n",
  338. fn->actag_base, fn->actag_enabled);
  339. return 0;
  340. }
  341. static int set_function_pasid(struct ocxl_fn *fn)
  342. {
  343. struct pci_dev *dev = to_pci_dev(fn->dev.parent);
  344. int rc, desired_count, max_count;
  345. /* A function may not require any PASID */
  346. if (fn->config.max_pasid_log < 0)
  347. return 0;
  348. rc = ocxl_config_get_pasid_info(dev, &max_count);
  349. if (rc)
  350. return rc;
  351. desired_count = 1 << fn->config.max_pasid_log;
  352. if (desired_count > max_count) {
  353. dev_err(&fn->dev,
  354. "Function requires more PASIDs than is available (%d vs. %d)\n",
  355. desired_count, max_count);
  356. return -ENOSPC;
  357. }
  358. fn->pasid_base = 0;
  359. return 0;
  360. }
  361. static int configure_function(struct ocxl_fn *fn, struct pci_dev *dev)
  362. {
  363. int rc;
  364. rc = pci_enable_device(dev);
  365. if (rc) {
  366. dev_err(&dev->dev, "pci_enable_device failed: %d\n", rc);
  367. return rc;
  368. }
  369. /*
  370. * Once it has been confirmed to work on our hardware, we
  371. * should reset the function, to force the adapter to restart
  372. * from scratch.
  373. * A function reset would also reset all its AFUs.
  374. *
  375. * Some hints for implementation:
  376. *
  377. * - there's not status bit to know when the reset is done. We
  378. * should try reading the config space to know when it's
  379. * done.
  380. * - probably something like:
  381. * Reset
  382. * wait 100ms
  383. * issue config read
  384. * allow device up to 1 sec to return success on config
  385. * read before declaring it broken
  386. *
  387. * Some shared logic on the card (CFG, TLX) won't be reset, so
  388. * there's no guarantee that it will be enough.
  389. */
  390. rc = ocxl_config_read_function(dev, &fn->config);
  391. if (rc)
  392. return rc;
  393. rc = set_function_device(fn, dev);
  394. if (rc)
  395. return rc;
  396. rc = assign_function_actag(fn);
  397. if (rc)
  398. return rc;
  399. rc = set_function_pasid(fn);
  400. if (rc)
  401. return rc;
  402. rc = ocxl_link_setup(dev, 0, &fn->link);
  403. if (rc)
  404. return rc;
  405. rc = ocxl_config_set_TL(dev, fn->config.dvsec_tl_pos);
  406. if (rc) {
  407. ocxl_link_release(dev, fn->link);
  408. return rc;
  409. }
  410. return 0;
  411. }
  412. static void deconfigure_function(struct ocxl_fn *fn)
  413. {
  414. struct pci_dev *dev = to_pci_dev(fn->dev.parent);
  415. ocxl_link_release(dev, fn->link);
  416. pci_disable_device(dev);
  417. }
  418. static struct ocxl_fn *init_function(struct pci_dev *dev)
  419. {
  420. struct ocxl_fn *fn;
  421. int rc;
  422. fn = alloc_function(dev);
  423. if (!fn)
  424. return ERR_PTR(-ENOMEM);
  425. rc = configure_function(fn, dev);
  426. if (rc) {
  427. free_function(fn);
  428. return ERR_PTR(rc);
  429. }
  430. rc = device_register(&fn->dev);
  431. if (rc) {
  432. deconfigure_function(fn);
  433. put_device(&fn->dev);
  434. return ERR_PTR(rc);
  435. }
  436. return fn;
  437. }
  438. static void remove_function(struct ocxl_fn *fn)
  439. {
  440. deconfigure_function(fn);
  441. device_unregister(&fn->dev);
  442. }
  443. static int ocxl_probe(struct pci_dev *dev, const struct pci_device_id *id)
  444. {
  445. int rc, afu_count = 0;
  446. u8 afu;
  447. struct ocxl_fn *fn;
  448. if (!radix_enabled()) {
  449. dev_err(&dev->dev, "Unsupported memory model (hash)\n");
  450. return -ENODEV;
  451. }
  452. fn = init_function(dev);
  453. if (IS_ERR(fn)) {
  454. dev_err(&dev->dev, "function init failed: %li\n",
  455. PTR_ERR(fn));
  456. return PTR_ERR(fn);
  457. }
  458. for (afu = 0; afu <= fn->config.max_afu_index; afu++) {
  459. rc = ocxl_config_check_afu_index(dev, &fn->config, afu);
  460. if (rc > 0) {
  461. rc = init_afu(dev, fn, afu);
  462. if (rc) {
  463. dev_err(&dev->dev,
  464. "Can't initialize AFU index %d\n", afu);
  465. continue;
  466. }
  467. afu_count++;
  468. }
  469. }
  470. dev_info(&dev->dev, "%d AFU(s) configured\n", afu_count);
  471. return 0;
  472. }
  473. static void ocxl_remove(struct pci_dev *dev)
  474. {
  475. struct ocxl_afu *afu, *tmp;
  476. struct ocxl_fn *fn = pci_get_drvdata(dev);
  477. list_for_each_entry_safe(afu, tmp, &fn->afu_list, list) {
  478. remove_afu(afu);
  479. }
  480. remove_function(fn);
  481. }
  482. struct pci_driver ocxl_pci_driver = {
  483. .name = "ocxl",
  484. .id_table = ocxl_pci_tbl,
  485. .probe = ocxl_probe,
  486. .remove = ocxl_remove,
  487. .shutdown = ocxl_remove,
  488. };