p2pdma.c 26 KB

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