blob: 771b45605853726e8d7dc1d069bc7ac882e2467b [file] [log] [blame]
Logan Gunthorpe52916982018-10-04 15:27:35 -06001// 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
Logan Gunthorpe2d7bc012018-10-04 15:27:38 -060011#define pr_fmt(fmt) "pci-p2pdma: " fmt
12#include <linux/ctype.h>
Logan Gunthorpe52916982018-10-04 15:27:35 -060013#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>
Logan Gunthorpe6dbbd052019-06-19 12:56:26 -060021#include <linux/iommu.h>
Logan Gunthorpe52916982018-10-04 15:27:35 -060022
Logan Gunthorpe72583082019-08-12 11:30:37 -060023enum pci_p2pdma_map_type {
24 PCI_P2PDMA_MAP_UNKNOWN = 0,
25 PCI_P2PDMA_MAP_NOT_SUPPORTED,
26 PCI_P2PDMA_MAP_BUS_ADDR,
27 PCI_P2PDMA_MAP_THRU_HOST_BRIDGE,
28};
29
Logan Gunthorpe52916982018-10-04 15:27:35 -060030struct pci_p2pdma {
Logan Gunthorpe52916982018-10-04 15:27:35 -060031 struct gen_pool *pool;
32 bool p2pmem_published;
33};
34
Logan Gunthorpea6e6fe62019-08-12 11:30:35 -060035struct pci_p2pdma_pagemap {
36 struct dev_pagemap pgmap;
Logan Gunthorpe0afea382019-08-12 11:30:36 -060037 struct pci_dev *provider;
Logan Gunthorpea6e6fe62019-08-12 11:30:35 -060038 u64 bus_offset;
39};
40
41static struct pci_p2pdma_pagemap *to_p2p_pgmap(struct dev_pagemap *pgmap)
42{
43 return container_of(pgmap, struct pci_p2pdma_pagemap, pgmap);
44}
45
Logan Gunthorpecbb8ca62018-10-04 15:27:36 -060046static ssize_t size_show(struct device *dev, struct device_attribute *attr,
47 char *buf)
48{
49 struct pci_dev *pdev = to_pci_dev(dev);
50 size_t size = 0;
51
52 if (pdev->p2pdma->pool)
53 size = gen_pool_size(pdev->p2pdma->pool);
54
55 return snprintf(buf, PAGE_SIZE, "%zd\n", size);
56}
57static DEVICE_ATTR_RO(size);
58
59static ssize_t available_show(struct device *dev, struct device_attribute *attr,
60 char *buf)
61{
62 struct pci_dev *pdev = to_pci_dev(dev);
63 size_t avail = 0;
64
65 if (pdev->p2pdma->pool)
66 avail = gen_pool_avail(pdev->p2pdma->pool);
67
68 return snprintf(buf, PAGE_SIZE, "%zd\n", avail);
69}
70static DEVICE_ATTR_RO(available);
71
72static ssize_t published_show(struct device *dev, struct device_attribute *attr,
73 char *buf)
74{
75 struct pci_dev *pdev = to_pci_dev(dev);
76
77 return snprintf(buf, PAGE_SIZE, "%d\n",
78 pdev->p2pdma->p2pmem_published);
79}
80static DEVICE_ATTR_RO(published);
81
82static struct attribute *p2pmem_attrs[] = {
83 &dev_attr_size.attr,
84 &dev_attr_available.attr,
85 &dev_attr_published.attr,
86 NULL,
87};
88
89static const struct attribute_group p2pmem_group = {
90 .attrs = p2pmem_attrs,
91 .name = "p2pmem",
92};
93
Logan Gunthorpe52916982018-10-04 15:27:35 -060094static void pci_p2pdma_release(void *data)
95{
96 struct pci_dev *pdev = data;
Dan Williams15701752019-06-13 15:56:30 -070097 struct pci_p2pdma *p2pdma = pdev->p2pdma;
Logan Gunthorpe52916982018-10-04 15:27:35 -060098
Dan Williams15701752019-06-13 15:56:30 -070099 if (!p2pdma)
Logan Gunthorpe52916982018-10-04 15:27:35 -0600100 return;
101
Dan Williams15701752019-06-13 15:56:30 -0700102 /* Flush and disable pci_alloc_p2p_mem() */
Logan Gunthorpe52916982018-10-04 15:27:35 -0600103 pdev->p2pdma = NULL;
Dan Williams15701752019-06-13 15:56:30 -0700104 synchronize_rcu();
105
106 gen_pool_destroy(p2pdma->pool);
107 sysfs_remove_group(&pdev->dev.kobj, &p2pmem_group);
Logan Gunthorpe52916982018-10-04 15:27:35 -0600108}
109
110static int pci_p2pdma_setup(struct pci_dev *pdev)
111{
112 int error = -ENOMEM;
113 struct pci_p2pdma *p2p;
114
115 p2p = devm_kzalloc(&pdev->dev, sizeof(*p2p), GFP_KERNEL);
116 if (!p2p)
117 return -ENOMEM;
118
119 p2p->pool = gen_pool_create(PAGE_SHIFT, dev_to_node(&pdev->dev));
120 if (!p2p->pool)
121 goto out;
122
Logan Gunthorpe52916982018-10-04 15:27:35 -0600123 error = devm_add_action_or_reset(&pdev->dev, pci_p2pdma_release, pdev);
124 if (error)
125 goto out_pool_destroy;
126
127 pdev->p2pdma = p2p;
128
Logan Gunthorpecbb8ca62018-10-04 15:27:36 -0600129 error = sysfs_create_group(&pdev->dev.kobj, &p2pmem_group);
130 if (error)
131 goto out_pool_destroy;
132
Logan Gunthorpe52916982018-10-04 15:27:35 -0600133 return 0;
134
135out_pool_destroy:
Logan Gunthorpecbb8ca62018-10-04 15:27:36 -0600136 pdev->p2pdma = NULL;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600137 gen_pool_destroy(p2p->pool);
138out:
139 devm_kfree(&pdev->dev, p2p);
140 return error;
141}
142
143/**
144 * pci_p2pdma_add_resource - add memory for use as p2p memory
145 * @pdev: the device to add the memory to
146 * @bar: PCI BAR to add
147 * @size: size of the memory to add, may be zero to use the whole BAR
148 * @offset: offset into the PCI BAR
149 *
150 * The memory will be given ZONE_DEVICE struct pages so that it may
151 * be used with any DMA request.
152 */
153int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size,
154 u64 offset)
155{
Logan Gunthorpea6e6fe62019-08-12 11:30:35 -0600156 struct pci_p2pdma_pagemap *p2p_pgmap;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600157 struct dev_pagemap *pgmap;
158 void *addr;
159 int error;
160
161 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
162 return -EINVAL;
163
164 if (offset >= pci_resource_len(pdev, bar))
165 return -EINVAL;
166
167 if (!size)
168 size = pci_resource_len(pdev, bar) - offset;
169
170 if (size + offset > pci_resource_len(pdev, bar))
171 return -EINVAL;
172
173 if (!pdev->p2pdma) {
174 error = pci_p2pdma_setup(pdev);
175 if (error)
176 return error;
177 }
178
Logan Gunthorpea6e6fe62019-08-12 11:30:35 -0600179 p2p_pgmap = devm_kzalloc(&pdev->dev, sizeof(*p2p_pgmap), GFP_KERNEL);
180 if (!p2p_pgmap)
Logan Gunthorpe52916982018-10-04 15:27:35 -0600181 return -ENOMEM;
Logan Gunthorpea6e6fe62019-08-12 11:30:35 -0600182
183 pgmap = &p2p_pgmap->pgmap;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600184 pgmap->res.start = pci_resource_start(pdev, bar) + offset;
185 pgmap->res.end = pgmap->res.start + size - 1;
186 pgmap->res.flags = pci_resource_flags(pdev, bar);
Logan Gunthorpe52916982018-10-04 15:27:35 -0600187 pgmap->type = MEMORY_DEVICE_PCI_P2PDMA;
Logan Gunthorpea6e6fe62019-08-12 11:30:35 -0600188
Logan Gunthorpe0afea382019-08-12 11:30:36 -0600189 p2p_pgmap->provider = pdev;
Logan Gunthorpea6e6fe62019-08-12 11:30:35 -0600190 p2p_pgmap->bus_offset = pci_bus_address(pdev, bar) -
Logan Gunthorpe977196b2018-10-04 15:27:37 -0600191 pci_resource_start(pdev, bar);
Logan Gunthorpe52916982018-10-04 15:27:35 -0600192
193 addr = devm_memremap_pages(&pdev->dev, pgmap);
194 if (IS_ERR(addr)) {
195 error = PTR_ERR(addr);
Dan Williams50f44ee2019-06-13 15:56:33 -0700196 goto pgmap_free;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600197 }
198
Dan Williams15701752019-06-13 15:56:30 -0700199 error = gen_pool_add_owner(pdev->p2pdma->pool, (unsigned long)addr,
Logan Gunthorpe52916982018-10-04 15:27:35 -0600200 pci_bus_address(pdev, bar) + offset,
Dan Williams15701752019-06-13 15:56:30 -0700201 resource_size(&pgmap->res), dev_to_node(&pdev->dev),
Christoph Hellwigd0b35172019-06-26 14:27:16 +0200202 pgmap->ref);
Logan Gunthorpe52916982018-10-04 15:27:35 -0600203 if (error)
Dan Williamse615a192019-06-13 15:56:24 -0700204 goto pages_free;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600205
Logan Gunthorpe52916982018-10-04 15:27:35 -0600206 pci_info(pdev, "added peer-to-peer DMA memory %pR\n",
207 &pgmap->res);
208
209 return 0;
210
Dan Williamse615a192019-06-13 15:56:24 -0700211pages_free:
212 devm_memunmap_pages(&pdev->dev, pgmap);
Logan Gunthorpe52916982018-10-04 15:27:35 -0600213pgmap_free:
Christoph Hellwigd0b35172019-06-26 14:27:16 +0200214 devm_kfree(&pdev->dev, pgmap);
Logan Gunthorpe52916982018-10-04 15:27:35 -0600215 return error;
216}
217EXPORT_SYMBOL_GPL(pci_p2pdma_add_resource);
218
219/*
220 * Note this function returns the parent PCI device with a
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500221 * reference taken. It is the caller's responsibility to drop
Logan Gunthorpe52916982018-10-04 15:27:35 -0600222 * the reference.
223 */
224static struct pci_dev *find_parent_pci_dev(struct device *dev)
225{
226 struct device *parent;
227
228 dev = get_device(dev);
229
230 while (dev) {
231 if (dev_is_pci(dev))
232 return to_pci_dev(dev);
233
234 parent = get_device(dev->parent);
235 put_device(dev);
236 dev = parent;
237 }
238
239 return NULL;
240}
241
242/*
243 * Check if a PCI bridge has its ACS redirection bits set to redirect P2P
244 * TLPs upstream via ACS. Returns 1 if the packets will be redirected
245 * upstream, 0 otherwise.
246 */
247static int pci_bridge_has_acs_redir(struct pci_dev *pdev)
248{
249 int pos;
250 u16 ctrl;
251
252 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
253 if (!pos)
254 return 0;
255
256 pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
257
258 if (ctrl & (PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_EC))
259 return 1;
260
261 return 0;
262}
263
264static void seq_buf_print_bus_devfn(struct seq_buf *buf, struct pci_dev *pdev)
265{
266 if (!buf)
267 return;
268
269 seq_buf_printf(buf, "%s;", pci_name(pdev));
270}
271
Logan Gunthorpe494d63b2019-08-12 11:30:41 -0600272static const struct pci_p2pdma_whitelist_entry {
273 unsigned short vendor;
274 unsigned short device;
275 enum {
276 REQ_SAME_HOST_BRIDGE = 1 << 0,
277 } flags;
278} pci_p2pdma_whitelist[] = {
279 /* AMD ZEN */
280 {PCI_VENDOR_ID_AMD, 0x1450, 0},
281
282 /* Intel Xeon E5/Core i7 */
283 {PCI_VENDOR_ID_INTEL, 0x3c00, REQ_SAME_HOST_BRIDGE},
284 {PCI_VENDOR_ID_INTEL, 0x3c01, REQ_SAME_HOST_BRIDGE},
285 /* Intel Xeon E7 v3/Xeon E5 v3/Core i7 */
286 {PCI_VENDOR_ID_INTEL, 0x2f00, REQ_SAME_HOST_BRIDGE},
287 {PCI_VENDOR_ID_INTEL, 0x2f01, REQ_SAME_HOST_BRIDGE},
288 {}
289};
290
291static bool __host_bridge_whitelist(struct pci_host_bridge *host,
292 bool same_host_bridge)
Christian König0f97da82019-04-18 13:58:59 +0200293{
Christian König0f97da82019-04-18 13:58:59 +0200294 struct pci_dev *root = pci_get_slot(host->bus, PCI_DEVFN(0, 0));
Logan Gunthorpe494d63b2019-08-12 11:30:41 -0600295 const struct pci_p2pdma_whitelist_entry *entry;
Christian König0f97da82019-04-18 13:58:59 +0200296 unsigned short vendor, device;
297
298 if (!root)
299 return false;
300
301 vendor = root->vendor;
302 device = root->device;
303 pci_dev_put(root);
304
Logan Gunthorpe494d63b2019-08-12 11:30:41 -0600305 for (entry = pci_p2pdma_whitelist; entry->vendor; entry++) {
306 if (vendor != entry->vendor || device != entry->device)
307 continue;
308 if (entry->flags & REQ_SAME_HOST_BRIDGE && !same_host_bridge)
309 return false;
310
Christian König0f97da82019-04-18 13:58:59 +0200311 return true;
Logan Gunthorpe494d63b2019-08-12 11:30:41 -0600312 }
Christian König0f97da82019-04-18 13:58:59 +0200313
314 return false;
315}
316
Logan Gunthorpe2c84d812019-08-12 11:30:40 -0600317/*
318 * If we can't find a common upstream bridge take a look at the root
319 * complex and compare it to a whitelist of known good hardware.
320 */
321static bool host_bridge_whitelist(struct pci_dev *a, struct pci_dev *b)
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
326 if (iommu_present(a->dev.bus) || iommu_present(b->dev.bus))
327 return false;
328
Logan Gunthorpe494d63b2019-08-12 11:30:41 -0600329 if (host_a == host_b)
330 return __host_bridge_whitelist(host_a, true);
331
332 if (__host_bridge_whitelist(host_a, false) &&
333 __host_bridge_whitelist(host_b, false))
Logan Gunthorpe2c84d812019-08-12 11:30:40 -0600334 return true;
335
336 return false;
337}
338
Logan Gunthorpe72583082019-08-12 11:30:37 -0600339static enum pci_p2pdma_map_type
Logan Gunthorpec6bfaeb2019-08-12 11:30:38 -0600340__upstream_bridge_distance(struct pci_dev *provider, struct pci_dev *client,
Logan Gunthorpe72583082019-08-12 11:30:37 -0600341 int *dist, bool *acs_redirects, struct seq_buf *acs_list)
Logan Gunthorpe52916982018-10-04 15:27:35 -0600342{
Christian König0f97da82019-04-18 13:58:59 +0200343 struct pci_dev *a = provider, *b = client, *bb;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600344 int dist_a = 0;
345 int dist_b = 0;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600346 int acs_cnt = 0;
347
Logan Gunthorpe72583082019-08-12 11:30:37 -0600348 if (acs_redirects)
349 *acs_redirects = false;
350
Logan Gunthorpe52916982018-10-04 15:27:35 -0600351 /*
352 * Note, we don't need to take references to devices returned by
353 * pci_upstream_bridge() seeing we hold a reference to a child
354 * device which will already hold a reference to the upstream bridge.
355 */
356
357 while (a) {
358 dist_b = 0;
359
360 if (pci_bridge_has_acs_redir(a)) {
361 seq_buf_print_bus_devfn(acs_list, a);
362 acs_cnt++;
363 }
364
365 bb = b;
366
367 while (bb) {
368 if (a == bb)
369 goto check_b_path_acs;
370
371 bb = pci_upstream_bridge(bb);
372 dist_b++;
373 }
374
375 a = pci_upstream_bridge(a);
376 dist_a++;
377 }
378
Logan Gunthorpe72583082019-08-12 11:30:37 -0600379 if (dist)
380 *dist = dist_a + dist_b;
381
Logan Gunthorpee2cbfbf2019-08-12 11:30:39 -0600382 return PCI_P2PDMA_MAP_THRU_HOST_BRIDGE;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600383
384check_b_path_acs:
385 bb = b;
386
387 while (bb) {
388 if (a == bb)
389 break;
390
391 if (pci_bridge_has_acs_redir(bb)) {
392 seq_buf_print_bus_devfn(acs_list, bb);
393 acs_cnt++;
394 }
395
396 bb = pci_upstream_bridge(bb);
397 }
398
Logan Gunthorpe72583082019-08-12 11:30:37 -0600399 if (dist)
400 *dist = dist_a + dist_b;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600401
Logan Gunthorpe72583082019-08-12 11:30:37 -0600402 if (acs_cnt) {
403 if (acs_redirects)
404 *acs_redirects = true;
405
Logan Gunthorpee2cbfbf2019-08-12 11:30:39 -0600406 return PCI_P2PDMA_MAP_THRU_HOST_BRIDGE;
Logan Gunthorpe72583082019-08-12 11:30:37 -0600407 }
408
409 return PCI_P2PDMA_MAP_BUS_ADDR;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600410}
411
Logan Gunthorpec6bfaeb2019-08-12 11:30:38 -0600412/*
413 * Find the distance through the nearest common upstream bridge between
414 * two PCI devices.
415 *
416 * If the two devices are the same device then 0 will be returned.
417 *
418 * If there are two virtual functions of the same device behind the same
419 * bridge port then 2 will be returned (one step down to the PCIe switch,
420 * then one step back to the same device).
421 *
422 * In the case where two devices are connected to the same PCIe switch, the
423 * value 4 will be returned. This corresponds to the following PCI tree:
424 *
425 * -+ Root Port
426 * \+ Switch Upstream Port
427 * +-+ Switch Downstream Port
428 * + \- Device A
429 * \-+ Switch Downstream Port
430 * \- Device B
431 *
432 * The distance is 4 because we traverse from Device A through the downstream
433 * port of the switch, to the common upstream port, back up to the second
434 * downstream port and then to Device B.
435 *
436 * Any two devices that cannot communicate using p2pdma will return
437 * PCI_P2PDMA_MAP_NOT_SUPPORTED.
438 *
439 * Any two devices that have a data path that goes through the host bridge
440 * will consult a whitelist. If the host bridges are on the whitelist,
441 * this function will return PCI_P2PDMA_MAP_THRU_HOST_BRIDGE.
442 *
443 * If either bridge is not on the whitelist this function returns
444 * PCI_P2PDMA_MAP_NOT_SUPPORTED.
445 *
446 * If a bridge which has any ACS redirection bits set is in the path,
447 * acs_redirects will be set to true. In this case, a list of all infringing
448 * bridge addresses will be populated in acs_list (assuming it's non-null)
449 * for printk purposes.
450 */
451static enum pci_p2pdma_map_type
452upstream_bridge_distance(struct pci_dev *provider, struct pci_dev *client,
453 int *dist, bool *acs_redirects, struct seq_buf *acs_list)
454{
Logan Gunthorpee2cbfbf2019-08-12 11:30:39 -0600455 enum pci_p2pdma_map_type map_type;
456
457 map_type = __upstream_bridge_distance(provider, client, dist,
458 acs_redirects, acs_list);
459
460 if (map_type == PCI_P2PDMA_MAP_THRU_HOST_BRIDGE) {
Logan Gunthorpe2c84d812019-08-12 11:30:40 -0600461 if (!host_bridge_whitelist(provider, client))
Logan Gunthorpee2cbfbf2019-08-12 11:30:39 -0600462 return PCI_P2PDMA_MAP_NOT_SUPPORTED;
463 }
464
465 return map_type;
Logan Gunthorpec6bfaeb2019-08-12 11:30:38 -0600466}
467
Logan Gunthorpe72583082019-08-12 11:30:37 -0600468static enum pci_p2pdma_map_type
469upstream_bridge_distance_warn(struct pci_dev *provider, struct pci_dev *client,
470 int *dist)
Logan Gunthorpe52916982018-10-04 15:27:35 -0600471{
472 struct seq_buf acs_list;
Logan Gunthorpe72583082019-08-12 11:30:37 -0600473 bool acs_redirects;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600474 int ret;
475
476 seq_buf_init(&acs_list, kmalloc(PAGE_SIZE, GFP_KERNEL), PAGE_SIZE);
477 if (!acs_list.buffer)
478 return -ENOMEM;
479
Logan Gunthorpe72583082019-08-12 11:30:37 -0600480 ret = upstream_bridge_distance(provider, client, dist, &acs_redirects,
481 &acs_list);
482 if (acs_redirects) {
483 pci_warn(client, "ACS redirect is set between the client and provider (%s)\n",
Logan Gunthorpe52916982018-10-04 15:27:35 -0600484 pci_name(provider));
485 /* Drop final semicolon */
486 acs_list.buffer[acs_list.len-1] = 0;
487 pci_warn(client, "to disable ACS redirect for this path, add the kernel parameter: pci=disable_acs_redir=%s\n",
488 acs_list.buffer);
Logan Gunthorpe72583082019-08-12 11:30:37 -0600489 }
Logan Gunthorpe52916982018-10-04 15:27:35 -0600490
Logan Gunthorpe72583082019-08-12 11:30:37 -0600491 if (ret == PCI_P2PDMA_MAP_NOT_SUPPORTED) {
492 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",
Logan Gunthorpe52916982018-10-04 15:27:35 -0600493 pci_name(provider));
494 }
495
496 kfree(acs_list.buffer);
497
498 return ret;
499}
500
501/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500502 * pci_p2pdma_distance_many - Determine the cumulative distance between
Logan Gunthorpe52916982018-10-04 15:27:35 -0600503 * a p2pdma provider and the clients in use.
504 * @provider: p2pdma provider to check against the client list
505 * @clients: array of devices to check (NULL-terminated)
506 * @num_clients: number of clients in the array
507 * @verbose: if true, print warnings for devices when we return -1
508 *
509 * Returns -1 if any of the clients are not compatible (behind the same
510 * root port as the provider), otherwise returns a positive number where
Randy Dunlapfcf9ab32018-12-01 09:31:34 -0800511 * a lower number is the preferable choice. (If there's one client
Logan Gunthorpe52916982018-10-04 15:27:35 -0600512 * that's the same as the provider it will return 0, which is best choice).
513 *
514 * For now, "compatible" means the provider and the clients are all behind
515 * the same PCI root port. This cuts out cases that may work but is safest
516 * for the user. Future work can expand this to white-list root complexes that
517 * can safely forward between each ports.
518 */
519int pci_p2pdma_distance_many(struct pci_dev *provider, struct device **clients,
520 int num_clients, bool verbose)
521{
522 bool not_supported = false;
523 struct pci_dev *pci_client;
Logan Gunthorpe72583082019-08-12 11:30:37 -0600524 int total_dist = 0;
525 int distance;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600526 int i, ret;
527
528 if (num_clients == 0)
529 return -1;
530
531 for (i = 0; i < num_clients; i++) {
Logan Gunthorpe9c002bb2019-07-02 11:35:44 -0600532 if (IS_ENABLED(CONFIG_DMA_VIRT_OPS) &&
533 clients[i]->dma_ops == &dma_virt_ops) {
534 if (verbose)
535 dev_warn(clients[i],
536 "cannot be used for peer-to-peer DMA because the driver makes use of dma_virt_ops\n");
537 return -1;
538 }
539
Logan Gunthorpe52916982018-10-04 15:27:35 -0600540 pci_client = find_parent_pci_dev(clients[i]);
541 if (!pci_client) {
542 if (verbose)
543 dev_warn(clients[i],
544 "cannot be used for peer-to-peer DMA as it is not a PCI device\n");
545 return -1;
546 }
547
548 if (verbose)
549 ret = upstream_bridge_distance_warn(provider,
Logan Gunthorpe72583082019-08-12 11:30:37 -0600550 pci_client, &distance);
Logan Gunthorpe52916982018-10-04 15:27:35 -0600551 else
552 ret = upstream_bridge_distance(provider, pci_client,
Logan Gunthorpe72583082019-08-12 11:30:37 -0600553 &distance, NULL, NULL);
Logan Gunthorpe52916982018-10-04 15:27:35 -0600554
555 pci_dev_put(pci_client);
556
Logan Gunthorpe72583082019-08-12 11:30:37 -0600557 if (ret == PCI_P2PDMA_MAP_NOT_SUPPORTED)
Logan Gunthorpe52916982018-10-04 15:27:35 -0600558 not_supported = true;
559
560 if (not_supported && !verbose)
561 break;
562
Logan Gunthorpe72583082019-08-12 11:30:37 -0600563 total_dist += distance;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600564 }
565
566 if (not_supported)
567 return -1;
568
Logan Gunthorpe72583082019-08-12 11:30:37 -0600569 return total_dist;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600570}
571EXPORT_SYMBOL_GPL(pci_p2pdma_distance_many);
572
573/**
574 * pci_has_p2pmem - check if a given PCI device has published any p2pmem
575 * @pdev: PCI device to check
576 */
577bool pci_has_p2pmem(struct pci_dev *pdev)
578{
579 return pdev->p2pdma && pdev->p2pdma->p2pmem_published;
580}
581EXPORT_SYMBOL_GPL(pci_has_p2pmem);
582
583/**
584 * pci_p2pmem_find - find a peer-to-peer DMA memory device compatible with
585 * the specified list of clients and shortest distance (as determined
586 * by pci_p2pmem_dma())
587 * @clients: array of devices to check (NULL-terminated)
588 * @num_clients: number of client devices in the list
589 *
590 * If multiple devices are behind the same switch, the one "closest" to the
Randy Dunlapfcf9ab32018-12-01 09:31:34 -0800591 * client devices in use will be chosen first. (So if one of the providers is
Logan Gunthorpe52916982018-10-04 15:27:35 -0600592 * the same as one of the clients, that provider will be used ahead of any
593 * other providers that are unrelated). If multiple providers are an equal
594 * distance away, one will be chosen at random.
595 *
596 * Returns a pointer to the PCI device with a reference taken (use pci_dev_put
597 * to return the reference) or NULL if no compatible device is found. The
598 * found provider will also be assigned to the client list.
599 */
600struct pci_dev *pci_p2pmem_find_many(struct device **clients, int num_clients)
601{
602 struct pci_dev *pdev = NULL;
603 int distance;
604 int closest_distance = INT_MAX;
605 struct pci_dev **closest_pdevs;
606 int dev_cnt = 0;
607 const int max_devs = PAGE_SIZE / sizeof(*closest_pdevs);
608 int i;
609
610 closest_pdevs = kmalloc(PAGE_SIZE, GFP_KERNEL);
611 if (!closest_pdevs)
612 return NULL;
613
614 while ((pdev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pdev))) {
615 if (!pci_has_p2pmem(pdev))
616 continue;
617
618 distance = pci_p2pdma_distance_many(pdev, clients,
619 num_clients, false);
620 if (distance < 0 || distance > closest_distance)
621 continue;
622
623 if (distance == closest_distance && dev_cnt >= max_devs)
624 continue;
625
626 if (distance < closest_distance) {
627 for (i = 0; i < dev_cnt; i++)
628 pci_dev_put(closest_pdevs[i]);
629
630 dev_cnt = 0;
631 closest_distance = distance;
632 }
633
634 closest_pdevs[dev_cnt++] = pci_dev_get(pdev);
635 }
636
637 if (dev_cnt)
638 pdev = pci_dev_get(closest_pdevs[prandom_u32_max(dev_cnt)]);
639
640 for (i = 0; i < dev_cnt; i++)
641 pci_dev_put(closest_pdevs[i]);
642
643 kfree(closest_pdevs);
644 return pdev;
645}
646EXPORT_SYMBOL_GPL(pci_p2pmem_find_many);
647
648/**
649 * pci_alloc_p2p_mem - allocate peer-to-peer DMA memory
650 * @pdev: the device to allocate memory from
651 * @size: number of bytes to allocate
652 *
653 * Returns the allocated memory or NULL on error.
654 */
655void *pci_alloc_p2pmem(struct pci_dev *pdev, size_t size)
656{
Dan Williams15701752019-06-13 15:56:30 -0700657 void *ret = NULL;
658 struct percpu_ref *ref;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600659
Dan Williams15701752019-06-13 15:56:30 -0700660 /*
661 * Pairs with synchronize_rcu() in pci_p2pdma_release() to
662 * ensure pdev->p2pdma is non-NULL for the duration of the
663 * read-lock.
664 */
665 rcu_read_lock();
Logan Gunthorpe52916982018-10-04 15:27:35 -0600666 if (unlikely(!pdev->p2pdma))
Dan Williams15701752019-06-13 15:56:30 -0700667 goto out;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600668
Dan Williams15701752019-06-13 15:56:30 -0700669 ret = (void *)gen_pool_alloc_owner(pdev->p2pdma->pool, size,
670 (void **) &ref);
671 if (!ret)
672 goto out;
Logan Gunthorpe52916982018-10-04 15:27:35 -0600673
Dan Williams15701752019-06-13 15:56:30 -0700674 if (unlikely(!percpu_ref_tryget_live(ref))) {
675 gen_pool_free(pdev->p2pdma->pool, (unsigned long) ret, size);
676 ret = NULL;
677 goto out;
678 }
679out:
680 rcu_read_unlock();
Logan Gunthorpe52916982018-10-04 15:27:35 -0600681 return ret;
682}
683EXPORT_SYMBOL_GPL(pci_alloc_p2pmem);
684
685/**
686 * pci_free_p2pmem - free peer-to-peer DMA memory
687 * @pdev: the device the memory was allocated from
688 * @addr: address of the memory that was allocated
Randy Dunlapfcf9ab32018-12-01 09:31:34 -0800689 * @size: number of bytes that were allocated
Logan Gunthorpe52916982018-10-04 15:27:35 -0600690 */
691void pci_free_p2pmem(struct pci_dev *pdev, void *addr, size_t size)
692{
Dan Williams15701752019-06-13 15:56:30 -0700693 struct percpu_ref *ref;
694
695 gen_pool_free_owner(pdev->p2pdma->pool, (uintptr_t)addr, size,
696 (void **) &ref);
697 percpu_ref_put(ref);
Logan Gunthorpe52916982018-10-04 15:27:35 -0600698}
699EXPORT_SYMBOL_GPL(pci_free_p2pmem);
700
701/**
702 * pci_virt_to_bus - return the PCI bus address for a given virtual
703 * address obtained with pci_alloc_p2pmem()
704 * @pdev: the device the memory was allocated from
705 * @addr: address of the memory that was allocated
706 */
707pci_bus_addr_t pci_p2pmem_virt_to_bus(struct pci_dev *pdev, void *addr)
708{
709 if (!addr)
710 return 0;
711 if (!pdev->p2pdma)
712 return 0;
713
714 /*
715 * Note: when we added the memory to the pool we used the PCI
716 * bus address as the physical address. So gen_pool_virt_to_phys()
717 * actually returns the bus address despite the misleading name.
718 */
719 return gen_pool_virt_to_phys(pdev->p2pdma->pool, (unsigned long)addr);
720}
721EXPORT_SYMBOL_GPL(pci_p2pmem_virt_to_bus);
722
723/**
724 * pci_p2pmem_alloc_sgl - allocate peer-to-peer DMA memory in a scatterlist
725 * @pdev: the device to allocate memory from
726 * @nents: the number of SG entries in the list
727 * @length: number of bytes to allocate
728 *
Randy Dunlapfcf9ab32018-12-01 09:31:34 -0800729 * Return: %NULL on error or &struct scatterlist pointer and @nents on success
Logan Gunthorpe52916982018-10-04 15:27:35 -0600730 */
731struct scatterlist *pci_p2pmem_alloc_sgl(struct pci_dev *pdev,
732 unsigned int *nents, u32 length)
733{
734 struct scatterlist *sg;
735 void *addr;
736
737 sg = kzalloc(sizeof(*sg), GFP_KERNEL);
738 if (!sg)
739 return NULL;
740
741 sg_init_table(sg, 1);
742
743 addr = pci_alloc_p2pmem(pdev, length);
744 if (!addr)
745 goto out_free_sg;
746
747 sg_set_buf(sg, addr, length);
748 *nents = 1;
749 return sg;
750
751out_free_sg:
752 kfree(sg);
753 return NULL;
754}
755EXPORT_SYMBOL_GPL(pci_p2pmem_alloc_sgl);
756
757/**
758 * pci_p2pmem_free_sgl - free a scatterlist allocated by pci_p2pmem_alloc_sgl()
759 * @pdev: the device to allocate memory from
760 * @sgl: the allocated scatterlist
761 */
762void pci_p2pmem_free_sgl(struct pci_dev *pdev, struct scatterlist *sgl)
763{
764 struct scatterlist *sg;
765 int count;
766
767 for_each_sg(sgl, sg, INT_MAX, count) {
768 if (!sg)
769 break;
770
771 pci_free_p2pmem(pdev, sg_virt(sg), sg->length);
772 }
773 kfree(sgl);
774}
775EXPORT_SYMBOL_GPL(pci_p2pmem_free_sgl);
776
777/**
778 * pci_p2pmem_publish - publish the peer-to-peer DMA memory for use by
779 * other devices with pci_p2pmem_find()
780 * @pdev: the device with peer-to-peer DMA memory to publish
781 * @publish: set to true to publish the memory, false to unpublish it
782 *
783 * Published memory can be used by other PCI device drivers for
784 * peer-2-peer DMA operations. Non-published memory is reserved for
Randy Dunlapfcf9ab32018-12-01 09:31:34 -0800785 * exclusive use of the device driver that registers the peer-to-peer
Logan Gunthorpe52916982018-10-04 15:27:35 -0600786 * memory.
787 */
788void pci_p2pmem_publish(struct pci_dev *pdev, bool publish)
789{
790 if (pdev->p2pdma)
791 pdev->p2pdma->p2pmem_published = publish;
792}
793EXPORT_SYMBOL_GPL(pci_p2pmem_publish);
Logan Gunthorpe977196b2018-10-04 15:27:37 -0600794
Logan Gunthorpe142c2422019-08-12 11:30:44 -0600795static int __pci_p2pdma_map_sg(struct pci_p2pdma_pagemap *p2p_pgmap,
796 struct device *dev, struct scatterlist *sg, int nents)
797{
798 struct scatterlist *s;
799 phys_addr_t paddr;
800 int i;
801
802 /*
803 * p2pdma mappings are not compatible with devices that use
804 * dma_virt_ops. If the upper layers do the right thing
805 * this should never happen because it will be prevented
806 * by the check in pci_p2pdma_distance_many()
807 */
808 if (WARN_ON_ONCE(IS_ENABLED(CONFIG_DMA_VIRT_OPS) &&
809 dev->dma_ops == &dma_virt_ops))
810 return 0;
811
812 for_each_sg(sg, s, nents, i) {
813 paddr = sg_phys(s);
814
815 s->dma_address = paddr - p2p_pgmap->bus_offset;
816 sg_dma_len(s) = s->length;
817 }
818
819 return nents;
820}
821
Logan Gunthorpe977196b2018-10-04 15:27:37 -0600822/**
823 * pci_p2pdma_map_sg - map a PCI peer-to-peer scatterlist for DMA
824 * @dev: device doing the DMA request
825 * @sg: scatter list to map
826 * @nents: elements in the scatterlist
827 * @dir: DMA direction
Logan Gunthorpe2b9f4bb2019-08-12 11:30:42 -0600828 * @attrs: DMA attributes passed to dma_map_sg() (if called)
Logan Gunthorpe977196b2018-10-04 15:27:37 -0600829 *
Logan Gunthorpe7f73eac2019-08-12 11:30:43 -0600830 * Scatterlists mapped with this function should be unmapped using
831 * pci_p2pdma_unmap_sg_attrs().
Logan Gunthorpe977196b2018-10-04 15:27:37 -0600832 *
833 * Returns the number of SG entries mapped or 0 on error.
834 */
Logan Gunthorpe2b9f4bb2019-08-12 11:30:42 -0600835int pci_p2pdma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
836 int nents, enum dma_data_direction dir, unsigned long attrs)
Logan Gunthorpe977196b2018-10-04 15:27:37 -0600837{
Logan Gunthorpe142c2422019-08-12 11:30:44 -0600838 struct pci_p2pdma_pagemap *p2p_pgmap =
839 to_p2p_pgmap(sg_page(sg)->pgmap);
Logan Gunthorpe977196b2018-10-04 15:27:37 -0600840
Logan Gunthorpe142c2422019-08-12 11:30:44 -0600841 return __pci_p2pdma_map_sg(p2p_pgmap, dev, sg, nents);
Logan Gunthorpe977196b2018-10-04 15:27:37 -0600842}
Logan Gunthorpe2b9f4bb2019-08-12 11:30:42 -0600843EXPORT_SYMBOL_GPL(pci_p2pdma_map_sg_attrs);
Logan Gunthorpe2d7bc012018-10-04 15:27:38 -0600844
845/**
Logan Gunthorpe7f73eac2019-08-12 11:30:43 -0600846 * pci_p2pdma_unmap_sg - unmap a PCI peer-to-peer scatterlist that was
847 * mapped with pci_p2pdma_map_sg()
848 * @dev: device doing the DMA request
849 * @sg: scatter list to map
850 * @nents: number of elements returned by pci_p2pdma_map_sg()
851 * @dir: DMA direction
852 * @attrs: DMA attributes passed to dma_unmap_sg() (if called)
853 */
854void pci_p2pdma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
855 int nents, enum dma_data_direction dir, unsigned long attrs)
856{
857}
858EXPORT_SYMBOL_GPL(pci_p2pdma_unmap_sg_attrs);
859
860/**
Logan Gunthorpe2d7bc012018-10-04 15:27:38 -0600861 * pci_p2pdma_enable_store - parse a configfs/sysfs attribute store
862 * to enable p2pdma
863 * @page: contents of the value to be stored
864 * @p2p_dev: returns the PCI device that was selected to be used
865 * (if one was specified in the stored value)
866 * @use_p2pdma: returns whether to enable p2pdma or not
867 *
868 * Parses an attribute value to decide whether to enable p2pdma.
Randy Dunlapfcf9ab32018-12-01 09:31:34 -0800869 * The value can select a PCI device (using its full BDF device
Logan Gunthorpe2d7bc012018-10-04 15:27:38 -0600870 * name) or a boolean (in any format strtobool() accepts). A false
871 * value disables p2pdma, a true value expects the caller
872 * to automatically find a compatible device and specifying a PCI device
873 * expects the caller to use the specific provider.
874 *
875 * pci_p2pdma_enable_show() should be used as the show operation for
876 * the attribute.
877 *
878 * Returns 0 on success
879 */
880int pci_p2pdma_enable_store(const char *page, struct pci_dev **p2p_dev,
881 bool *use_p2pdma)
882{
883 struct device *dev;
884
885 dev = bus_find_device_by_name(&pci_bus_type, NULL, page);
886 if (dev) {
887 *use_p2pdma = true;
888 *p2p_dev = to_pci_dev(dev);
889
890 if (!pci_has_p2pmem(*p2p_dev)) {
891 pci_err(*p2p_dev,
892 "PCI device has no peer-to-peer memory: %s\n",
893 page);
894 pci_dev_put(*p2p_dev);
895 return -ENODEV;
896 }
897
898 return 0;
899 } else if ((page[0] == '0' || page[0] == '1') && !iscntrl(page[1])) {
900 /*
901 * If the user enters a PCI device that doesn't exist
902 * like "0000:01:00.1", we don't want strtobool to think
903 * it's a '0' when it's clearly not what the user wanted.
904 * So we require 0's and 1's to be exactly one character.
905 */
906 } else if (!strtobool(page, use_p2pdma)) {
907 return 0;
908 }
909
910 pr_err("No such PCI device: %.*s\n", (int)strcspn(page, "\n"), page);
911 return -ENODEV;
912}
913EXPORT_SYMBOL_GPL(pci_p2pdma_enable_store);
914
915/**
916 * pci_p2pdma_enable_show - show a configfs/sysfs attribute indicating
917 * whether p2pdma is enabled
918 * @page: contents of the stored value
919 * @p2p_dev: the selected p2p device (NULL if no device is selected)
Randy Dunlapfcf9ab32018-12-01 09:31:34 -0800920 * @use_p2pdma: whether p2pdma has been enabled
Logan Gunthorpe2d7bc012018-10-04 15:27:38 -0600921 *
922 * Attributes that use pci_p2pdma_enable_store() should use this function
923 * to show the value of the attribute.
924 *
925 * Returns 0 on success
926 */
927ssize_t pci_p2pdma_enable_show(char *page, struct pci_dev *p2p_dev,
928 bool use_p2pdma)
929{
930 if (!use_p2pdma)
931 return sprintf(page, "0\n");
932
933 if (!p2p_dev)
934 return sprintf(page, "1\n");
935
936 return sprintf(page, "%s\n", pci_name(p2p_dev));
937}
938EXPORT_SYMBOL_GPL(pci_p2pdma_enable_show);