p2pdma.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Peer 2 Peer DMA support.
  4. *
  5. * Copyright (c) 2016-2018, Logan Gunthorpe
  6. * Copyright (c) 2016-2017, Microsemi Corporation
  7. * Copyright (c) 2017, Christoph Hellwig
  8. * Copyright (c) 2018, Eideticom Inc.
  9. */
  10. #define pr_fmt(fmt) "pci-p2pdma: " fmt
  11. #include <linux/ctype.h>
  12. #include <linux/pci-p2pdma.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/genalloc.h>
  16. #include <linux/memremap.h>
  17. #include <linux/percpu-refcount.h>
  18. #include <linux/random.h>
  19. #include <linux/seq_buf.h>
  20. #include <linux/xarray.h>
  21. enum pci_p2pdma_map_type {
  22. PCI_P2PDMA_MAP_UNKNOWN = 0,
  23. PCI_P2PDMA_MAP_NOT_SUPPORTED,
  24. PCI_P2PDMA_MAP_BUS_ADDR,
  25. PCI_P2PDMA_MAP_THRU_HOST_BRIDGE,
  26. };
  27. struct pci_p2pdma {
  28. struct gen_pool *pool;
  29. bool p2pmem_published;
  30. struct xarray map_types;
  31. };
  32. struct pci_p2pdma_pagemap {
  33. struct dev_pagemap pgmap;
  34. struct pci_dev *provider;
  35. u64 bus_offset;
  36. };
  37. static struct pci_p2pdma_pagemap *to_p2p_pgmap(struct dev_pagemap *pgmap)
  38. {
  39. return container_of(pgmap, struct pci_p2pdma_pagemap, pgmap);
  40. }
  41. static ssize_t size_show(struct device *dev, struct device_attribute *attr,
  42. char *buf)
  43. {
  44. struct pci_dev *pdev = to_pci_dev(dev);
  45. size_t size = 0;
  46. if (pdev->p2pdma->pool)
  47. size = gen_pool_size(pdev->p2pdma->pool);
  48. return scnprintf(buf, PAGE_SIZE, "%zd\n", size);
  49. }
  50. static DEVICE_ATTR_RO(size);
  51. static ssize_t available_show(struct device *dev, struct device_attribute *attr,
  52. char *buf)
  53. {
  54. struct pci_dev *pdev = to_pci_dev(dev);
  55. size_t avail = 0;
  56. if (pdev->p2pdma->pool)
  57. avail = gen_pool_avail(pdev->p2pdma->pool);
  58. return scnprintf(buf, PAGE_SIZE, "%zd\n", avail);
  59. }
  60. static DEVICE_ATTR_RO(available);
  61. static ssize_t published_show(struct device *dev, struct device_attribute *attr,
  62. char *buf)
  63. {
  64. struct pci_dev *pdev = to_pci_dev(dev);
  65. return scnprintf(buf, PAGE_SIZE, "%d\n",
  66. pdev->p2pdma->p2pmem_published);
  67. }
  68. static DEVICE_ATTR_RO(published);
  69. static struct attribute *p2pmem_attrs[] = {
  70. &dev_attr_size.attr,
  71. &dev_attr_available.attr,
  72. &dev_attr_published.attr,
  73. NULL,
  74. };
  75. static const struct attribute_group p2pmem_group = {
  76. .attrs = p2pmem_attrs,
  77. .name = "p2pmem",
  78. };
  79. static void pci_p2pdma_release(void *data)
  80. {
  81. struct pci_dev *pdev = data;
  82. struct pci_p2pdma *p2pdma = pdev->p2pdma;
  83. if (!p2pdma)
  84. return;
  85. /* Flush and disable pci_alloc_p2p_mem() */
  86. pdev->p2pdma = NULL;
  87. synchronize_rcu();
  88. gen_pool_destroy(p2pdma->pool);
  89. sysfs_remove_group(&pdev->dev.kobj, &p2pmem_group);
  90. xa_destroy(&p2pdma->map_types);
  91. }
  92. static int pci_p2pdma_setup(struct pci_dev *pdev)
  93. {
  94. int error = -ENOMEM;
  95. struct pci_p2pdma *p2p;
  96. p2p = devm_kzalloc(&pdev->dev, sizeof(*p2p), GFP_KERNEL);
  97. if (!p2p)
  98. return -ENOMEM;
  99. xa_init(&p2p->map_types);
  100. p2p->pool = gen_pool_create(PAGE_SHIFT, dev_to_node(&pdev->dev));
  101. if (!p2p->pool)
  102. goto out;
  103. error = devm_add_action_or_reset(&pdev->dev, pci_p2pdma_release, pdev);
  104. if (error)
  105. goto out_pool_destroy;
  106. pdev->p2pdma = p2p;
  107. error = sysfs_create_group(&pdev->dev.kobj, &p2pmem_group);
  108. if (error)
  109. goto out_pool_destroy;
  110. return 0;
  111. out_pool_destroy:
  112. pdev->p2pdma = NULL;
  113. gen_pool_destroy(p2p->pool);
  114. out:
  115. devm_kfree(&pdev->dev, p2p);
  116. return error;
  117. }
  118. /**
  119. * pci_p2pdma_add_resource - add memory for use as p2p memory
  120. * @pdev: the device to add the memory to
  121. * @bar: PCI BAR to add
  122. * @size: size of the memory to add, may be zero to use the whole BAR
  123. * @offset: offset into the PCI BAR
  124. *
  125. * The memory will be given ZONE_DEVICE struct pages so that it may
  126. * be used with any DMA request.
  127. */
  128. int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size,
  129. u64 offset)
  130. {
  131. struct pci_p2pdma_pagemap *p2p_pgmap;
  132. struct dev_pagemap *pgmap;
  133. void *addr;
  134. int error;
  135. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
  136. return -EINVAL;
  137. if (offset >= pci_resource_len(pdev, bar))
  138. return -EINVAL;
  139. if (!size)
  140. size = pci_resource_len(pdev, bar) - offset;
  141. if (size + offset > pci_resource_len(pdev, bar))
  142. return -EINVAL;
  143. if (!pdev->p2pdma) {
  144. error = pci_p2pdma_setup(pdev);
  145. if (error)
  146. return error;
  147. }
  148. p2p_pgmap = devm_kzalloc(&pdev->dev, sizeof(*p2p_pgmap), GFP_KERNEL);
  149. if (!p2p_pgmap)
  150. return -ENOMEM;
  151. pgmap = &p2p_pgmap->pgmap;
  152. pgmap->range.start = pci_resource_start(pdev, bar) + offset;
  153. pgmap->range.end = pgmap->range.start + size - 1;
  154. pgmap->nr_range = 1;
  155. pgmap->type = MEMORY_DEVICE_PCI_P2PDMA;
  156. p2p_pgmap->provider = pdev;
  157. p2p_pgmap->bus_offset = pci_bus_address(pdev, bar) -
  158. pci_resource_start(pdev, bar);
  159. addr = devm_memremap_pages(&pdev->dev, pgmap);
  160. if (IS_ERR(addr)) {
  161. error = PTR_ERR(addr);
  162. goto pgmap_free;
  163. }
  164. error = gen_pool_add_owner(pdev->p2pdma->pool, (unsigned long)addr,
  165. pci_bus_address(pdev, bar) + offset,
  166. range_len(&pgmap->range), dev_to_node(&pdev->dev),
  167. pgmap->ref);
  168. if (error)
  169. goto pages_free;
  170. pci_info(pdev, "added peer-to-peer DMA memory %#llx-%#llx\n",
  171. pgmap->range.start, pgmap->range.end);
  172. return 0;
  173. pages_free:
  174. devm_memunmap_pages(&pdev->dev, pgmap);
  175. pgmap_free:
  176. devm_kfree(&pdev->dev, pgmap);
  177. return error;
  178. }
  179. EXPORT_SYMBOL_GPL(pci_p2pdma_add_resource);
  180. /*
  181. * Note this function returns the parent PCI device with a
  182. * reference taken. It is the caller's responsibility to drop
  183. * the reference.
  184. */
  185. static struct pci_dev *find_parent_pci_dev(struct device *dev)
  186. {
  187. struct device *parent;
  188. dev = get_device(dev);
  189. while (dev) {
  190. if (dev_is_pci(dev))
  191. return to_pci_dev(dev);
  192. parent = get_device(dev->parent);
  193. put_device(dev);
  194. dev = parent;
  195. }
  196. return NULL;
  197. }
  198. /*
  199. * Check if a PCI bridge has its ACS redirection bits set to redirect P2P
  200. * TLPs upstream via ACS. Returns 1 if the packets will be redirected
  201. * upstream, 0 otherwise.
  202. */
  203. static int pci_bridge_has_acs_redir(struct pci_dev *pdev)
  204. {
  205. int pos;
  206. u16 ctrl;
  207. pos = pdev->acs_cap;
  208. if (!pos)
  209. return 0;
  210. pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
  211. if (ctrl & (PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_EC))
  212. return 1;
  213. return 0;
  214. }
  215. static void seq_buf_print_bus_devfn(struct seq_buf *buf, struct pci_dev *pdev)
  216. {
  217. if (!buf)
  218. return;
  219. seq_buf_printf(buf, "%s;", pci_name(pdev));
  220. }
  221. static bool cpu_supports_p2pdma(void)
  222. {
  223. #ifdef CONFIG_X86
  224. struct cpuinfo_x86 *c = &cpu_data(0);
  225. /* Any AMD CPU whose family ID is Zen or newer supports p2pdma */
  226. if (c->x86_vendor == X86_VENDOR_AMD && c->x86 >= 0x17)
  227. return true;
  228. #endif
  229. return false;
  230. }
  231. static const struct pci_p2pdma_whitelist_entry {
  232. unsigned short vendor;
  233. unsigned short device;
  234. enum {
  235. REQ_SAME_HOST_BRIDGE = 1 << 0,
  236. } flags;
  237. } pci_p2pdma_whitelist[] = {
  238. /* Intel Xeon E5/Core i7 */
  239. {PCI_VENDOR_ID_INTEL, 0x3c00, REQ_SAME_HOST_BRIDGE},
  240. {PCI_VENDOR_ID_INTEL, 0x3c01, REQ_SAME_HOST_BRIDGE},
  241. /* Intel Xeon E7 v3/Xeon E5 v3/Core i7 */
  242. {PCI_VENDOR_ID_INTEL, 0x2f00, REQ_SAME_HOST_BRIDGE},
  243. {PCI_VENDOR_ID_INTEL, 0x2f01, REQ_SAME_HOST_BRIDGE},
  244. /* Intel SkyLake-E */
  245. {PCI_VENDOR_ID_INTEL, 0x2030, 0},
  246. {PCI_VENDOR_ID_INTEL, 0x2031, 0},
  247. {PCI_VENDOR_ID_INTEL, 0x2032, 0},
  248. {PCI_VENDOR_ID_INTEL, 0x2033, 0},
  249. {PCI_VENDOR_ID_INTEL, 0x2020, 0},
  250. {}
  251. };
  252. /*
  253. * This lookup function tries to find the PCI device corresponding to a given
  254. * host bridge.
  255. *
  256. * It assumes the host bridge device is the first PCI device in the
  257. * bus->devices list and that the devfn is 00.0. These assumptions should hold
  258. * for all the devices in the whitelist above.
  259. *
  260. * This function is equivalent to pci_get_slot(host->bus, 0), however it does
  261. * not take the pci_bus_sem lock seeing __host_bridge_whitelist() must not
  262. * sleep.
  263. *
  264. * For this to be safe, the caller should hold a reference to a device on the
  265. * bridge, which should ensure the host_bridge device will not be freed
  266. * or removed from the head of the devices list.
  267. */
  268. static struct pci_dev *pci_host_bridge_dev(struct pci_host_bridge *host)
  269. {
  270. struct pci_dev *root;
  271. root = list_first_entry_or_null(&host->bus->devices,
  272. struct pci_dev, bus_list);
  273. if (!root)
  274. return NULL;
  275. if (root->devfn != PCI_DEVFN(0, 0))
  276. return NULL;
  277. return root;
  278. }
  279. static bool __host_bridge_whitelist(struct pci_host_bridge *host,
  280. bool same_host_bridge)
  281. {
  282. struct pci_dev *root = pci_host_bridge_dev(host);
  283. const struct pci_p2pdma_whitelist_entry *entry;
  284. unsigned short vendor, device;
  285. if (!root)
  286. return false;
  287. vendor = root->vendor;
  288. device = root->device;
  289. for (entry = pci_p2pdma_whitelist; entry->vendor; entry++) {
  290. if (vendor != entry->vendor || device != entry->device)
  291. continue;
  292. if (entry->flags & REQ_SAME_HOST_BRIDGE && !same_host_bridge)
  293. return false;
  294. return true;
  295. }
  296. return false;
  297. }
  298. /*
  299. * If we can't find a common upstream bridge take a look at the root
  300. * complex and compare it to a whitelist of known good hardware.
  301. */
  302. static bool host_bridge_whitelist(struct pci_dev *a, struct pci_dev *b)
  303. {
  304. struct pci_host_bridge *host_a = pci_find_host_bridge(a->bus);
  305. struct pci_host_bridge *host_b = pci_find_host_bridge(b->bus);
  306. if (host_a == host_b)
  307. return __host_bridge_whitelist(host_a, true);
  308. if (__host_bridge_whitelist(host_a, false) &&
  309. __host_bridge_whitelist(host_b, false))
  310. return true;
  311. return false;
  312. }
  313. static enum pci_p2pdma_map_type
  314. __upstream_bridge_distance(struct pci_dev *provider, struct pci_dev *client,
  315. int *dist, bool *acs_redirects, struct seq_buf *acs_list)
  316. {
  317. struct pci_dev *a = provider, *b = client, *bb;
  318. int dist_a = 0;
  319. int dist_b = 0;
  320. int acs_cnt = 0;
  321. if (acs_redirects)
  322. *acs_redirects = false;
  323. /*
  324. * Note, we don't need to take references to devices returned by
  325. * pci_upstream_bridge() seeing we hold a reference to a child
  326. * device which will already hold a reference to the upstream bridge.
  327. */
  328. while (a) {
  329. dist_b = 0;
  330. if (pci_bridge_has_acs_redir(a)) {
  331. seq_buf_print_bus_devfn(acs_list, a);
  332. acs_cnt++;
  333. }
  334. bb = b;
  335. while (bb) {
  336. if (a == bb)
  337. goto check_b_path_acs;
  338. bb = pci_upstream_bridge(bb);
  339. dist_b++;
  340. }
  341. a = pci_upstream_bridge(a);
  342. dist_a++;
  343. }
  344. if (dist)
  345. *dist = dist_a + dist_b;
  346. return PCI_P2PDMA_MAP_THRU_HOST_BRIDGE;
  347. check_b_path_acs:
  348. bb = b;
  349. while (bb) {
  350. if (a == bb)
  351. break;
  352. if (pci_bridge_has_acs_redir(bb)) {
  353. seq_buf_print_bus_devfn(acs_list, bb);
  354. acs_cnt++;
  355. }
  356. bb = pci_upstream_bridge(bb);
  357. }
  358. if (dist)
  359. *dist = dist_a + dist_b;
  360. if (acs_cnt) {
  361. if (acs_redirects)
  362. *acs_redirects = true;
  363. return PCI_P2PDMA_MAP_THRU_HOST_BRIDGE;
  364. }
  365. return PCI_P2PDMA_MAP_BUS_ADDR;
  366. }
  367. static unsigned long map_types_idx(struct pci_dev *client)
  368. {
  369. return (pci_domain_nr(client->bus) << 16) |
  370. (client->bus->number << 8) | client->devfn;
  371. }
  372. /*
  373. * Find the distance through the nearest common upstream bridge between
  374. * two PCI devices.
  375. *
  376. * If the two devices are the same device then 0 will be returned.
  377. *
  378. * If there are two virtual functions of the same device behind the same
  379. * bridge port then 2 will be returned (one step down to the PCIe switch,
  380. * then one step back to the same device).
  381. *
  382. * In the case where two devices are connected to the same PCIe switch, the
  383. * value 4 will be returned. This corresponds to the following PCI tree:
  384. *
  385. * -+ Root Port
  386. * \+ Switch Upstream Port
  387. * +-+ Switch Downstream Port
  388. * + \- Device A
  389. * \-+ Switch Downstream Port
  390. * \- Device B
  391. *
  392. * The distance is 4 because we traverse from Device A through the downstream
  393. * port of the switch, to the common upstream port, back up to the second
  394. * downstream port and then to Device B.
  395. *
  396. * Any two devices that cannot communicate using p2pdma will return
  397. * PCI_P2PDMA_MAP_NOT_SUPPORTED.
  398. *
  399. * Any two devices that have a data path that goes through the host bridge
  400. * will consult a whitelist. If the host bridges are on the whitelist,
  401. * this function will return PCI_P2PDMA_MAP_THRU_HOST_BRIDGE.
  402. *
  403. * If either bridge is not on the whitelist this function returns
  404. * PCI_P2PDMA_MAP_NOT_SUPPORTED.
  405. *
  406. * If a bridge which has any ACS redirection bits set is in the path,
  407. * acs_redirects will be set to true. In this case, a list of all infringing
  408. * bridge addresses will be populated in acs_list (assuming it's non-null)
  409. * for printk purposes.
  410. */
  411. static enum pci_p2pdma_map_type
  412. upstream_bridge_distance(struct pci_dev *provider, struct pci_dev *client,
  413. int *dist, bool *acs_redirects, struct seq_buf *acs_list)
  414. {
  415. enum pci_p2pdma_map_type map_type;
  416. map_type = __upstream_bridge_distance(provider, client, dist,
  417. acs_redirects, acs_list);
  418. if (map_type == PCI_P2PDMA_MAP_THRU_HOST_BRIDGE) {
  419. if (!cpu_supports_p2pdma() &&
  420. !host_bridge_whitelist(provider, client))
  421. map_type = PCI_P2PDMA_MAP_NOT_SUPPORTED;
  422. }
  423. if (provider->p2pdma)
  424. xa_store(&provider->p2pdma->map_types, map_types_idx(client),
  425. xa_mk_value(map_type), GFP_KERNEL);
  426. return map_type;
  427. }
  428. static enum pci_p2pdma_map_type
  429. upstream_bridge_distance_warn(struct pci_dev *provider, struct pci_dev *client,
  430. int *dist)
  431. {
  432. struct seq_buf acs_list;
  433. bool acs_redirects;
  434. int ret;
  435. seq_buf_init(&acs_list, kmalloc(PAGE_SIZE, GFP_KERNEL), PAGE_SIZE);
  436. if (!acs_list.buffer)
  437. return -ENOMEM;
  438. ret = upstream_bridge_distance(provider, client, dist, &acs_redirects,
  439. &acs_list);
  440. if (acs_redirects) {
  441. pci_warn(client, "ACS redirect is set between the client and provider (%s)\n",
  442. pci_name(provider));
  443. /* Drop final semicolon */
  444. acs_list.buffer[acs_list.len-1] = 0;
  445. pci_warn(client, "to disable ACS redirect for this path, add the kernel parameter: pci=disable_acs_redir=%s\n",
  446. acs_list.buffer);
  447. }
  448. if (ret == PCI_P2PDMA_MAP_NOT_SUPPORTED) {
  449. pci_warn(client, "cannot be used for peer-to-peer DMA as the client and provider (%s) do not share an upstream bridge or whitelisted host bridge\n",
  450. pci_name(provider));
  451. }
  452. kfree(acs_list.buffer);
  453. return ret;
  454. }
  455. /**
  456. * pci_p2pdma_distance_many - Determine the cumulative distance between
  457. * a p2pdma provider and the clients in use.
  458. * @provider: p2pdma provider to check against the client list
  459. * @clients: array of devices to check (NULL-terminated)
  460. * @num_clients: number of clients in the array
  461. * @verbose: if true, print warnings for devices when we return -1
  462. *
  463. * Returns -1 if any of the clients are not compatible, otherwise returns a
  464. * positive number where a lower number is the preferable choice. (If there's
  465. * one client that's the same as the provider it will return 0, which is best
  466. * choice).
  467. *
  468. * "compatible" means the provider and the clients are either all behind
  469. * the same PCI root port or the host bridges connected to each of the devices
  470. * are listed in the 'pci_p2pdma_whitelist'.
  471. */
  472. int pci_p2pdma_distance_many(struct pci_dev *provider, struct device **clients,
  473. int num_clients, bool verbose)
  474. {
  475. bool not_supported = false;
  476. struct pci_dev *pci_client;
  477. int total_dist = 0;
  478. int distance;
  479. int i, ret;
  480. if (num_clients == 0)
  481. return -1;
  482. for (i = 0; i < num_clients; i++) {
  483. #ifdef CONFIG_DMA_VIRT_OPS
  484. if (clients[i]->dma_ops == &dma_virt_ops) {
  485. if (verbose)
  486. dev_warn(clients[i],
  487. "cannot be used for peer-to-peer DMA because the driver makes use of dma_virt_ops\n");
  488. return -1;
  489. }
  490. #endif
  491. pci_client = find_parent_pci_dev(clients[i]);
  492. if (!pci_client) {
  493. if (verbose)
  494. dev_warn(clients[i],
  495. "cannot be used for peer-to-peer DMA as it is not a PCI device\n");
  496. return -1;
  497. }
  498. if (verbose)
  499. ret = upstream_bridge_distance_warn(provider,
  500. pci_client, &distance);
  501. else
  502. ret = upstream_bridge_distance(provider, pci_client,
  503. &distance, NULL, NULL);
  504. pci_dev_put(pci_client);
  505. if (ret == PCI_P2PDMA_MAP_NOT_SUPPORTED)
  506. not_supported = true;
  507. if (not_supported && !verbose)
  508. break;
  509. total_dist += distance;
  510. }
  511. if (not_supported)
  512. return -1;
  513. return total_dist;
  514. }
  515. EXPORT_SYMBOL_GPL(pci_p2pdma_distance_many);
  516. /**
  517. * pci_has_p2pmem - check if a given PCI device has published any p2pmem
  518. * @pdev: PCI device to check
  519. */
  520. bool pci_has_p2pmem(struct pci_dev *pdev)
  521. {
  522. return pdev->p2pdma && pdev->p2pdma->p2pmem_published;
  523. }
  524. EXPORT_SYMBOL_GPL(pci_has_p2pmem);
  525. /**
  526. * pci_p2pmem_find - find a peer-to-peer DMA memory device compatible with
  527. * the specified list of clients and shortest distance (as determined
  528. * by pci_p2pmem_dma())
  529. * @clients: array of devices to check (NULL-terminated)
  530. * @num_clients: number of client devices in the list
  531. *
  532. * If multiple devices are behind the same switch, the one "closest" to the
  533. * client devices in use will be chosen first. (So if one of the providers is
  534. * the same as one of the clients, that provider will be used ahead of any
  535. * other providers that are unrelated). If multiple providers are an equal
  536. * distance away, one will be chosen at random.
  537. *
  538. * Returns a pointer to the PCI device with a reference taken (use pci_dev_put
  539. * to return the reference) or NULL if no compatible device is found. The
  540. * found provider will also be assigned to the client list.
  541. */
  542. struct pci_dev *pci_p2pmem_find_many(struct device **clients, int num_clients)
  543. {
  544. struct pci_dev *pdev = NULL;
  545. int distance;
  546. int closest_distance = INT_MAX;
  547. struct pci_dev **closest_pdevs;
  548. int dev_cnt = 0;
  549. const int max_devs = PAGE_SIZE / sizeof(*closest_pdevs);
  550. int i;
  551. closest_pdevs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  552. if (!closest_pdevs)
  553. return NULL;
  554. while ((pdev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pdev))) {
  555. if (!pci_has_p2pmem(pdev))
  556. continue;
  557. distance = pci_p2pdma_distance_many(pdev, clients,
  558. num_clients, false);
  559. if (distance < 0 || distance > closest_distance)
  560. continue;
  561. if (distance == closest_distance && dev_cnt >= max_devs)
  562. continue;
  563. if (distance < closest_distance) {
  564. for (i = 0; i < dev_cnt; i++)
  565. pci_dev_put(closest_pdevs[i]);
  566. dev_cnt = 0;
  567. closest_distance = distance;
  568. }
  569. closest_pdevs[dev_cnt++] = pci_dev_get(pdev);
  570. }
  571. if (dev_cnt)
  572. pdev = pci_dev_get(closest_pdevs[prandom_u32_max(dev_cnt)]);
  573. for (i = 0; i < dev_cnt; i++)
  574. pci_dev_put(closest_pdevs[i]);
  575. kfree(closest_pdevs);
  576. return pdev;
  577. }
  578. EXPORT_SYMBOL_GPL(pci_p2pmem_find_many);
  579. /**
  580. * pci_alloc_p2p_mem - allocate peer-to-peer DMA memory
  581. * @pdev: the device to allocate memory from
  582. * @size: number of bytes to allocate
  583. *
  584. * Returns the allocated memory or NULL on error.
  585. */
  586. void *pci_alloc_p2pmem(struct pci_dev *pdev, size_t size)
  587. {
  588. void *ret = NULL;
  589. struct percpu_ref *ref;
  590. /*
  591. * Pairs with synchronize_rcu() in pci_p2pdma_release() to
  592. * ensure pdev->p2pdma is non-NULL for the duration of the
  593. * read-lock.
  594. */
  595. rcu_read_lock();
  596. if (unlikely(!pdev->p2pdma))
  597. goto out;
  598. ret = (void *)gen_pool_alloc_owner(pdev->p2pdma->pool, size,
  599. (void **) &ref);
  600. if (!ret)
  601. goto out;
  602. if (unlikely(!percpu_ref_tryget_live(ref))) {
  603. gen_pool_free(pdev->p2pdma->pool, (unsigned long) ret, size);
  604. ret = NULL;
  605. goto out;
  606. }
  607. out:
  608. rcu_read_unlock();
  609. return ret;
  610. }
  611. EXPORT_SYMBOL_GPL(pci_alloc_p2pmem);
  612. /**
  613. * pci_free_p2pmem - free peer-to-peer DMA memory
  614. * @pdev: the device the memory was allocated from
  615. * @addr: address of the memory that was allocated
  616. * @size: number of bytes that were allocated
  617. */
  618. void pci_free_p2pmem(struct pci_dev *pdev, void *addr, size_t size)
  619. {
  620. struct percpu_ref *ref;
  621. gen_pool_free_owner(pdev->p2pdma->pool, (uintptr_t)addr, size,
  622. (void **) &ref);
  623. percpu_ref_put(ref);
  624. }
  625. EXPORT_SYMBOL_GPL(pci_free_p2pmem);
  626. /**
  627. * pci_virt_to_bus - return the PCI bus address for a given virtual
  628. * address obtained with pci_alloc_p2pmem()
  629. * @pdev: the device the memory was allocated from
  630. * @addr: address of the memory that was allocated
  631. */
  632. pci_bus_addr_t pci_p2pmem_virt_to_bus(struct pci_dev *pdev, void *addr)
  633. {
  634. if (!addr)
  635. return 0;
  636. if (!pdev->p2pdma)
  637. return 0;
  638. /*
  639. * Note: when we added the memory to the pool we used the PCI
  640. * bus address as the physical address. So gen_pool_virt_to_phys()
  641. * actually returns the bus address despite the misleading name.
  642. */
  643. return gen_pool_virt_to_phys(pdev->p2pdma->pool, (unsigned long)addr);
  644. }
  645. EXPORT_SYMBOL_GPL(pci_p2pmem_virt_to_bus);
  646. /**
  647. * pci_p2pmem_alloc_sgl - allocate peer-to-peer DMA memory in a scatterlist
  648. * @pdev: the device to allocate memory from
  649. * @nents: the number of SG entries in the list
  650. * @length: number of bytes to allocate
  651. *
  652. * Return: %NULL on error or &struct scatterlist pointer and @nents on success
  653. */
  654. struct scatterlist *pci_p2pmem_alloc_sgl(struct pci_dev *pdev,
  655. unsigned int *nents, u32 length)
  656. {
  657. struct scatterlist *sg;
  658. void *addr;
  659. sg = kmalloc(sizeof(*sg), GFP_KERNEL);
  660. if (!sg)
  661. return NULL;
  662. sg_init_table(sg, 1);
  663. addr = pci_alloc_p2pmem(pdev, length);
  664. if (!addr)
  665. goto out_free_sg;
  666. sg_set_buf(sg, addr, length);
  667. *nents = 1;
  668. return sg;
  669. out_free_sg:
  670. kfree(sg);
  671. return NULL;
  672. }
  673. EXPORT_SYMBOL_GPL(pci_p2pmem_alloc_sgl);
  674. /**
  675. * pci_p2pmem_free_sgl - free a scatterlist allocated by pci_p2pmem_alloc_sgl()
  676. * @pdev: the device to allocate memory from
  677. * @sgl: the allocated scatterlist
  678. */
  679. void pci_p2pmem_free_sgl(struct pci_dev *pdev, struct scatterlist *sgl)
  680. {
  681. struct scatterlist *sg;
  682. int count;
  683. for_each_sg(sgl, sg, INT_MAX, count) {
  684. if (!sg)
  685. break;
  686. pci_free_p2pmem(pdev, sg_virt(sg), sg->length);
  687. }
  688. kfree(sgl);
  689. }
  690. EXPORT_SYMBOL_GPL(pci_p2pmem_free_sgl);
  691. /**
  692. * pci_p2pmem_publish - publish the peer-to-peer DMA memory for use by
  693. * other devices with pci_p2pmem_find()
  694. * @pdev: the device with peer-to-peer DMA memory to publish
  695. * @publish: set to true to publish the memory, false to unpublish it
  696. *
  697. * Published memory can be used by other PCI device drivers for
  698. * peer-2-peer DMA operations. Non-published memory is reserved for
  699. * exclusive use of the device driver that registers the peer-to-peer
  700. * memory.
  701. */
  702. void pci_p2pmem_publish(struct pci_dev *pdev, bool publish)
  703. {
  704. if (pdev->p2pdma)
  705. pdev->p2pdma->p2pmem_published = publish;
  706. }
  707. EXPORT_SYMBOL_GPL(pci_p2pmem_publish);
  708. static enum pci_p2pdma_map_type pci_p2pdma_map_type(struct pci_dev *provider,
  709. struct pci_dev *client)
  710. {
  711. if (!provider->p2pdma)
  712. return PCI_P2PDMA_MAP_NOT_SUPPORTED;
  713. return xa_to_value(xa_load(&provider->p2pdma->map_types,
  714. map_types_idx(client)));
  715. }
  716. static int __pci_p2pdma_map_sg(struct pci_p2pdma_pagemap *p2p_pgmap,
  717. struct device *dev, struct scatterlist *sg, int nents)
  718. {
  719. struct scatterlist *s;
  720. phys_addr_t paddr;
  721. int i;
  722. /*
  723. * p2pdma mappings are not compatible with devices that use
  724. * dma_virt_ops. If the upper layers do the right thing
  725. * this should never happen because it will be prevented
  726. * by the check in pci_p2pdma_distance_many()
  727. */
  728. #ifdef CONFIG_DMA_VIRT_OPS
  729. if (WARN_ON_ONCE(dev->dma_ops == &dma_virt_ops))
  730. return 0;
  731. #endif
  732. for_each_sg(sg, s, nents, i) {
  733. paddr = sg_phys(s);
  734. s->dma_address = paddr - p2p_pgmap->bus_offset;
  735. sg_dma_len(s) = s->length;
  736. }
  737. return nents;
  738. }
  739. /**
  740. * pci_p2pdma_map_sg - map a PCI peer-to-peer scatterlist for DMA
  741. * @dev: device doing the DMA request
  742. * @sg: scatter list to map
  743. * @nents: elements in the scatterlist
  744. * @dir: DMA direction
  745. * @attrs: DMA attributes passed to dma_map_sg() (if called)
  746. *
  747. * Scatterlists mapped with this function should be unmapped using
  748. * pci_p2pdma_unmap_sg_attrs().
  749. *
  750. * Returns the number of SG entries mapped or 0 on error.
  751. */
  752. int pci_p2pdma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  753. int nents, enum dma_data_direction dir, unsigned long attrs)
  754. {
  755. struct pci_p2pdma_pagemap *p2p_pgmap =
  756. to_p2p_pgmap(sg_page(sg)->pgmap);
  757. struct pci_dev *client;
  758. if (WARN_ON_ONCE(!dev_is_pci(dev)))
  759. return 0;
  760. client = to_pci_dev(dev);
  761. switch (pci_p2pdma_map_type(p2p_pgmap->provider, client)) {
  762. case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
  763. return dma_map_sg_attrs(dev, sg, nents, dir, attrs);
  764. case PCI_P2PDMA_MAP_BUS_ADDR:
  765. return __pci_p2pdma_map_sg(p2p_pgmap, dev, sg, nents);
  766. default:
  767. WARN_ON_ONCE(1);
  768. return 0;
  769. }
  770. }
  771. EXPORT_SYMBOL_GPL(pci_p2pdma_map_sg_attrs);
  772. /**
  773. * pci_p2pdma_unmap_sg - unmap a PCI peer-to-peer scatterlist that was
  774. * mapped with pci_p2pdma_map_sg()
  775. * @dev: device doing the DMA request
  776. * @sg: scatter list to map
  777. * @nents: number of elements returned by pci_p2pdma_map_sg()
  778. * @dir: DMA direction
  779. * @attrs: DMA attributes passed to dma_unmap_sg() (if called)
  780. */
  781. void pci_p2pdma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
  782. int nents, enum dma_data_direction dir, unsigned long attrs)
  783. {
  784. struct pci_p2pdma_pagemap *p2p_pgmap =
  785. to_p2p_pgmap(sg_page(sg)->pgmap);
  786. enum pci_p2pdma_map_type map_type;
  787. struct pci_dev *client;
  788. if (WARN_ON_ONCE(!dev_is_pci(dev)))
  789. return;
  790. client = to_pci_dev(dev);
  791. map_type = pci_p2pdma_map_type(p2p_pgmap->provider, client);
  792. if (map_type == PCI_P2PDMA_MAP_THRU_HOST_BRIDGE)
  793. dma_unmap_sg_attrs(dev, sg, nents, dir, attrs);
  794. }
  795. EXPORT_SYMBOL_GPL(pci_p2pdma_unmap_sg_attrs);
  796. /**
  797. * pci_p2pdma_enable_store - parse a configfs/sysfs attribute store
  798. * to enable p2pdma
  799. * @page: contents of the value to be stored
  800. * @p2p_dev: returns the PCI device that was selected to be used
  801. * (if one was specified in the stored value)
  802. * @use_p2pdma: returns whether to enable p2pdma or not
  803. *
  804. * Parses an attribute value to decide whether to enable p2pdma.
  805. * The value can select a PCI device (using its full BDF device
  806. * name) or a boolean (in any format strtobool() accepts). A false
  807. * value disables p2pdma, a true value expects the caller
  808. * to automatically find a compatible device and specifying a PCI device
  809. * expects the caller to use the specific provider.
  810. *
  811. * pci_p2pdma_enable_show() should be used as the show operation for
  812. * the attribute.
  813. *
  814. * Returns 0 on success
  815. */
  816. int pci_p2pdma_enable_store(const char *page, struct pci_dev **p2p_dev,
  817. bool *use_p2pdma)
  818. {
  819. struct device *dev;
  820. dev = bus_find_device_by_name(&pci_bus_type, NULL, page);
  821. if (dev) {
  822. *use_p2pdma = true;
  823. *p2p_dev = to_pci_dev(dev);
  824. if (!pci_has_p2pmem(*p2p_dev)) {
  825. pci_err(*p2p_dev,
  826. "PCI device has no peer-to-peer memory: %s\n",
  827. page);
  828. pci_dev_put(*p2p_dev);
  829. return -ENODEV;
  830. }
  831. return 0;
  832. } else if ((page[0] == '0' || page[0] == '1') && !iscntrl(page[1])) {
  833. /*
  834. * If the user enters a PCI device that doesn't exist
  835. * like "0000:01:00.1", we don't want strtobool to think
  836. * it's a '0' when it's clearly not what the user wanted.
  837. * So we require 0's and 1's to be exactly one character.
  838. */
  839. } else if (!strtobool(page, use_p2pdma)) {
  840. return 0;
  841. }
  842. pr_err("No such PCI device: %.*s\n", (int)strcspn(page, "\n"), page);
  843. return -ENODEV;
  844. }
  845. EXPORT_SYMBOL_GPL(pci_p2pdma_enable_store);
  846. /**
  847. * pci_p2pdma_enable_show - show a configfs/sysfs attribute indicating
  848. * whether p2pdma is enabled
  849. * @page: contents of the stored value
  850. * @p2p_dev: the selected p2p device (NULL if no device is selected)
  851. * @use_p2pdma: whether p2pdma has been enabled
  852. *
  853. * Attributes that use pci_p2pdma_enable_store() should use this function
  854. * to show the value of the attribute.
  855. *
  856. * Returns 0 on success
  857. */
  858. ssize_t pci_p2pdma_enable_show(char *page, struct pci_dev *p2p_dev,
  859. bool use_p2pdma)
  860. {
  861. if (!use_p2pdma)
  862. return sprintf(page, "0\n");
  863. if (!p2p_dev)
  864. return sprintf(page, "1\n");
  865. return sprintf(page, "%s\n", pci_name(p2p_dev));
  866. }
  867. EXPORT_SYMBOL_GPL(pci_p2pdma_enable_show);