p2pdma.c 29 KB

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