Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 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 | |
Logan Gunthorpe | 2d7bc01 | 2018-10-04 15:27:38 -0600 | [diff] [blame] | 11 | #define pr_fmt(fmt) "pci-p2pdma: " fmt |
| 12 | #include <linux/ctype.h> |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 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> |
Logan Gunthorpe | 6dbbd05 | 2019-06-19 12:56:26 -0600 | [diff] [blame] | 21 | #include <linux/iommu.h> |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 22 | |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 23 | enum 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 Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 30 | struct pci_p2pdma { |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 31 | struct gen_pool *pool; |
| 32 | bool p2pmem_published; |
| 33 | }; |
| 34 | |
Logan Gunthorpe | a6e6fe6 | 2019-08-12 11:30:35 -0600 | [diff] [blame] | 35 | struct pci_p2pdma_pagemap { |
| 36 | struct dev_pagemap pgmap; |
Logan Gunthorpe | 0afea38 | 2019-08-12 11:30:36 -0600 | [diff] [blame] | 37 | struct pci_dev *provider; |
Logan Gunthorpe | a6e6fe6 | 2019-08-12 11:30:35 -0600 | [diff] [blame] | 38 | u64 bus_offset; |
| 39 | }; |
| 40 | |
| 41 | static 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 Gunthorpe | cbb8ca6 | 2018-10-04 15:27:36 -0600 | [diff] [blame] | 46 | static 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 | } |
| 57 | static DEVICE_ATTR_RO(size); |
| 58 | |
| 59 | static 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 | } |
| 70 | static DEVICE_ATTR_RO(available); |
| 71 | |
| 72 | static 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 | } |
| 80 | static DEVICE_ATTR_RO(published); |
| 81 | |
| 82 | static struct attribute *p2pmem_attrs[] = { |
| 83 | &dev_attr_size.attr, |
| 84 | &dev_attr_available.attr, |
| 85 | &dev_attr_published.attr, |
| 86 | NULL, |
| 87 | }; |
| 88 | |
| 89 | static const struct attribute_group p2pmem_group = { |
| 90 | .attrs = p2pmem_attrs, |
| 91 | .name = "p2pmem", |
| 92 | }; |
| 93 | |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 94 | static void pci_p2pdma_release(void *data) |
| 95 | { |
| 96 | struct pci_dev *pdev = data; |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 97 | struct pci_p2pdma *p2pdma = pdev->p2pdma; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 98 | |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 99 | if (!p2pdma) |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 100 | return; |
| 101 | |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 102 | /* Flush and disable pci_alloc_p2p_mem() */ |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 103 | pdev->p2pdma = NULL; |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 104 | synchronize_rcu(); |
| 105 | |
| 106 | gen_pool_destroy(p2pdma->pool); |
| 107 | sysfs_remove_group(&pdev->dev.kobj, &p2pmem_group); |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static 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 Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 123 | 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 Gunthorpe | cbb8ca6 | 2018-10-04 15:27:36 -0600 | [diff] [blame] | 129 | error = sysfs_create_group(&pdev->dev.kobj, &p2pmem_group); |
| 130 | if (error) |
| 131 | goto out_pool_destroy; |
| 132 | |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 133 | return 0; |
| 134 | |
| 135 | out_pool_destroy: |
Logan Gunthorpe | cbb8ca6 | 2018-10-04 15:27:36 -0600 | [diff] [blame] | 136 | pdev->p2pdma = NULL; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 137 | gen_pool_destroy(p2p->pool); |
| 138 | out: |
| 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 | */ |
| 153 | int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size, |
| 154 | u64 offset) |
| 155 | { |
Logan Gunthorpe | a6e6fe6 | 2019-08-12 11:30:35 -0600 | [diff] [blame] | 156 | struct pci_p2pdma_pagemap *p2p_pgmap; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 157 | 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 Gunthorpe | a6e6fe6 | 2019-08-12 11:30:35 -0600 | [diff] [blame] | 179 | p2p_pgmap = devm_kzalloc(&pdev->dev, sizeof(*p2p_pgmap), GFP_KERNEL); |
| 180 | if (!p2p_pgmap) |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 181 | return -ENOMEM; |
Logan Gunthorpe | a6e6fe6 | 2019-08-12 11:30:35 -0600 | [diff] [blame] | 182 | |
| 183 | pgmap = &p2p_pgmap->pgmap; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 184 | 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 Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 187 | pgmap->type = MEMORY_DEVICE_PCI_P2PDMA; |
Logan Gunthorpe | a6e6fe6 | 2019-08-12 11:30:35 -0600 | [diff] [blame] | 188 | |
Logan Gunthorpe | 0afea38 | 2019-08-12 11:30:36 -0600 | [diff] [blame] | 189 | p2p_pgmap->provider = pdev; |
Logan Gunthorpe | a6e6fe6 | 2019-08-12 11:30:35 -0600 | [diff] [blame] | 190 | p2p_pgmap->bus_offset = pci_bus_address(pdev, bar) - |
Logan Gunthorpe | 977196b | 2018-10-04 15:27:37 -0600 | [diff] [blame] | 191 | pci_resource_start(pdev, bar); |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 192 | |
| 193 | addr = devm_memremap_pages(&pdev->dev, pgmap); |
| 194 | if (IS_ERR(addr)) { |
| 195 | error = PTR_ERR(addr); |
Dan Williams | 50f44ee | 2019-06-13 15:56:33 -0700 | [diff] [blame] | 196 | goto pgmap_free; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 197 | } |
| 198 | |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 199 | error = gen_pool_add_owner(pdev->p2pdma->pool, (unsigned long)addr, |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 200 | pci_bus_address(pdev, bar) + offset, |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 201 | resource_size(&pgmap->res), dev_to_node(&pdev->dev), |
Christoph Hellwig | d0b3517 | 2019-06-26 14:27:16 +0200 | [diff] [blame] | 202 | pgmap->ref); |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 203 | if (error) |
Dan Williams | e615a19 | 2019-06-13 15:56:24 -0700 | [diff] [blame] | 204 | goto pages_free; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 205 | |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 206 | pci_info(pdev, "added peer-to-peer DMA memory %pR\n", |
| 207 | &pgmap->res); |
| 208 | |
| 209 | return 0; |
| 210 | |
Dan Williams | e615a19 | 2019-06-13 15:56:24 -0700 | [diff] [blame] | 211 | pages_free: |
| 212 | devm_memunmap_pages(&pdev->dev, pgmap); |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 213 | pgmap_free: |
Christoph Hellwig | d0b3517 | 2019-06-26 14:27:16 +0200 | [diff] [blame] | 214 | devm_kfree(&pdev->dev, pgmap); |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 215 | return error; |
| 216 | } |
| 217 | EXPORT_SYMBOL_GPL(pci_p2pdma_add_resource); |
| 218 | |
| 219 | /* |
| 220 | * Note this function returns the parent PCI device with a |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 221 | * reference taken. It is the caller's responsibility to drop |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 222 | * the reference. |
| 223 | */ |
| 224 | static 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 | */ |
| 247 | static 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 | |
| 264 | static 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 Gunthorpe | 494d63b | 2019-08-12 11:30:41 -0600 | [diff] [blame] | 272 | static 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 | |
| 291 | static bool __host_bridge_whitelist(struct pci_host_bridge *host, |
| 292 | bool same_host_bridge) |
Christian König | 0f97da8 | 2019-04-18 13:58:59 +0200 | [diff] [blame] | 293 | { |
Christian König | 0f97da8 | 2019-04-18 13:58:59 +0200 | [diff] [blame] | 294 | struct pci_dev *root = pci_get_slot(host->bus, PCI_DEVFN(0, 0)); |
Logan Gunthorpe | 494d63b | 2019-08-12 11:30:41 -0600 | [diff] [blame] | 295 | const struct pci_p2pdma_whitelist_entry *entry; |
Christian König | 0f97da8 | 2019-04-18 13:58:59 +0200 | [diff] [blame] | 296 | 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 Gunthorpe | 494d63b | 2019-08-12 11:30:41 -0600 | [diff] [blame] | 305 | 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önig | 0f97da8 | 2019-04-18 13:58:59 +0200 | [diff] [blame] | 311 | return true; |
Logan Gunthorpe | 494d63b | 2019-08-12 11:30:41 -0600 | [diff] [blame] | 312 | } |
Christian König | 0f97da8 | 2019-04-18 13:58:59 +0200 | [diff] [blame] | 313 | |
| 314 | return false; |
| 315 | } |
| 316 | |
Logan Gunthorpe | 2c84d81 | 2019-08-12 11:30:40 -0600 | [diff] [blame] | 317 | /* |
| 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 | */ |
| 321 | static 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 Gunthorpe | 494d63b | 2019-08-12 11:30:41 -0600 | [diff] [blame] | 329 | 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 Gunthorpe | 2c84d81 | 2019-08-12 11:30:40 -0600 | [diff] [blame] | 334 | return true; |
| 335 | |
| 336 | return false; |
| 337 | } |
| 338 | |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 339 | static enum pci_p2pdma_map_type |
Logan Gunthorpe | c6bfaeb | 2019-08-12 11:30:38 -0600 | [diff] [blame] | 340 | __upstream_bridge_distance(struct pci_dev *provider, struct pci_dev *client, |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 341 | int *dist, bool *acs_redirects, struct seq_buf *acs_list) |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 342 | { |
Christian König | 0f97da8 | 2019-04-18 13:58:59 +0200 | [diff] [blame] | 343 | struct pci_dev *a = provider, *b = client, *bb; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 344 | int dist_a = 0; |
| 345 | int dist_b = 0; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 346 | int acs_cnt = 0; |
| 347 | |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 348 | if (acs_redirects) |
| 349 | *acs_redirects = false; |
| 350 | |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 351 | /* |
| 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 Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 379 | if (dist) |
| 380 | *dist = dist_a + dist_b; |
| 381 | |
Logan Gunthorpe | e2cbfbf | 2019-08-12 11:30:39 -0600 | [diff] [blame] | 382 | return PCI_P2PDMA_MAP_THRU_HOST_BRIDGE; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 383 | |
| 384 | check_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 Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 399 | if (dist) |
| 400 | *dist = dist_a + dist_b; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 401 | |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 402 | if (acs_cnt) { |
| 403 | if (acs_redirects) |
| 404 | *acs_redirects = true; |
| 405 | |
Logan Gunthorpe | e2cbfbf | 2019-08-12 11:30:39 -0600 | [diff] [blame] | 406 | return PCI_P2PDMA_MAP_THRU_HOST_BRIDGE; |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | return PCI_P2PDMA_MAP_BUS_ADDR; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 410 | } |
| 411 | |
Logan Gunthorpe | c6bfaeb | 2019-08-12 11:30:38 -0600 | [diff] [blame] | 412 | /* |
| 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 | */ |
| 451 | static enum pci_p2pdma_map_type |
| 452 | upstream_bridge_distance(struct pci_dev *provider, struct pci_dev *client, |
| 453 | int *dist, bool *acs_redirects, struct seq_buf *acs_list) |
| 454 | { |
Logan Gunthorpe | e2cbfbf | 2019-08-12 11:30:39 -0600 | [diff] [blame] | 455 | 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 Gunthorpe | 2c84d81 | 2019-08-12 11:30:40 -0600 | [diff] [blame] | 461 | if (!host_bridge_whitelist(provider, client)) |
Logan Gunthorpe | e2cbfbf | 2019-08-12 11:30:39 -0600 | [diff] [blame] | 462 | return PCI_P2PDMA_MAP_NOT_SUPPORTED; |
| 463 | } |
| 464 | |
| 465 | return map_type; |
Logan Gunthorpe | c6bfaeb | 2019-08-12 11:30:38 -0600 | [diff] [blame] | 466 | } |
| 467 | |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 468 | static enum pci_p2pdma_map_type |
| 469 | upstream_bridge_distance_warn(struct pci_dev *provider, struct pci_dev *client, |
| 470 | int *dist) |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 471 | { |
| 472 | struct seq_buf acs_list; |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 473 | bool acs_redirects; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 474 | 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 Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 480 | 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 Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 484 | 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 Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 489 | } |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 490 | |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 491 | 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 Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 493 | pci_name(provider)); |
| 494 | } |
| 495 | |
| 496 | kfree(acs_list.buffer); |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | /** |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 502 | * pci_p2pdma_distance_many - Determine the cumulative distance between |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 503 | * 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 Dunlap | fcf9ab3 | 2018-12-01 09:31:34 -0800 | [diff] [blame] | 511 | * a lower number is the preferable choice. (If there's one client |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 512 | * 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 | */ |
| 519 | int 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 Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 524 | int total_dist = 0; |
| 525 | int distance; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 526 | int i, ret; |
| 527 | |
| 528 | if (num_clients == 0) |
| 529 | return -1; |
| 530 | |
| 531 | for (i = 0; i < num_clients; i++) { |
Logan Gunthorpe | 9c002bb | 2019-07-02 11:35:44 -0600 | [diff] [blame] | 532 | 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 Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 540 | 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 Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 550 | pci_client, &distance); |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 551 | else |
| 552 | ret = upstream_bridge_distance(provider, pci_client, |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 553 | &distance, NULL, NULL); |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 554 | |
| 555 | pci_dev_put(pci_client); |
| 556 | |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 557 | if (ret == PCI_P2PDMA_MAP_NOT_SUPPORTED) |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 558 | not_supported = true; |
| 559 | |
| 560 | if (not_supported && !verbose) |
| 561 | break; |
| 562 | |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 563 | total_dist += distance; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | if (not_supported) |
| 567 | return -1; |
| 568 | |
Logan Gunthorpe | 7258308 | 2019-08-12 11:30:37 -0600 | [diff] [blame] | 569 | return total_dist; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 570 | } |
| 571 | EXPORT_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 | */ |
| 577 | bool pci_has_p2pmem(struct pci_dev *pdev) |
| 578 | { |
| 579 | return pdev->p2pdma && pdev->p2pdma->p2pmem_published; |
| 580 | } |
| 581 | EXPORT_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 Dunlap | fcf9ab3 | 2018-12-01 09:31:34 -0800 | [diff] [blame] | 591 | * client devices in use will be chosen first. (So if one of the providers is |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 592 | * 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 | */ |
| 600 | struct 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 | } |
| 646 | EXPORT_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 | */ |
| 655 | void *pci_alloc_p2pmem(struct pci_dev *pdev, size_t size) |
| 656 | { |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 657 | void *ret = NULL; |
| 658 | struct percpu_ref *ref; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 659 | |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 660 | /* |
| 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 Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 666 | if (unlikely(!pdev->p2pdma)) |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 667 | goto out; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 668 | |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 669 | ret = (void *)gen_pool_alloc_owner(pdev->p2pdma->pool, size, |
| 670 | (void **) &ref); |
| 671 | if (!ret) |
| 672 | goto out; |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 673 | |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 674 | 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 | } |
| 679 | out: |
| 680 | rcu_read_unlock(); |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 681 | return ret; |
| 682 | } |
| 683 | EXPORT_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 Dunlap | fcf9ab3 | 2018-12-01 09:31:34 -0800 | [diff] [blame] | 689 | * @size: number of bytes that were allocated |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 690 | */ |
| 691 | void pci_free_p2pmem(struct pci_dev *pdev, void *addr, size_t size) |
| 692 | { |
Dan Williams | 1570175 | 2019-06-13 15:56:30 -0700 | [diff] [blame] | 693 | 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 Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 698 | } |
| 699 | EXPORT_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 | */ |
| 707 | pci_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 | } |
| 721 | EXPORT_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 Dunlap | fcf9ab3 | 2018-12-01 09:31:34 -0800 | [diff] [blame] | 729 | * Return: %NULL on error or &struct scatterlist pointer and @nents on success |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 730 | */ |
| 731 | struct 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 | |
| 751 | out_free_sg: |
| 752 | kfree(sg); |
| 753 | return NULL; |
| 754 | } |
| 755 | EXPORT_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 | */ |
| 762 | void 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 | } |
| 775 | EXPORT_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 Dunlap | fcf9ab3 | 2018-12-01 09:31:34 -0800 | [diff] [blame] | 785 | * exclusive use of the device driver that registers the peer-to-peer |
Logan Gunthorpe | 5291698 | 2018-10-04 15:27:35 -0600 | [diff] [blame] | 786 | * memory. |
| 787 | */ |
| 788 | void pci_p2pmem_publish(struct pci_dev *pdev, bool publish) |
| 789 | { |
| 790 | if (pdev->p2pdma) |
| 791 | pdev->p2pdma->p2pmem_published = publish; |
| 792 | } |
| 793 | EXPORT_SYMBOL_GPL(pci_p2pmem_publish); |
Logan Gunthorpe | 977196b | 2018-10-04 15:27:37 -0600 | [diff] [blame] | 794 | |
Logan Gunthorpe | 142c242 | 2019-08-12 11:30:44 -0600 | [diff] [blame^] | 795 | static 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 Gunthorpe | 977196b | 2018-10-04 15:27:37 -0600 | [diff] [blame] | 822 | /** |
| 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 Gunthorpe | 2b9f4bb | 2019-08-12 11:30:42 -0600 | [diff] [blame] | 828 | * @attrs: DMA attributes passed to dma_map_sg() (if called) |
Logan Gunthorpe | 977196b | 2018-10-04 15:27:37 -0600 | [diff] [blame] | 829 | * |
Logan Gunthorpe | 7f73eac | 2019-08-12 11:30:43 -0600 | [diff] [blame] | 830 | * Scatterlists mapped with this function should be unmapped using |
| 831 | * pci_p2pdma_unmap_sg_attrs(). |
Logan Gunthorpe | 977196b | 2018-10-04 15:27:37 -0600 | [diff] [blame] | 832 | * |
| 833 | * Returns the number of SG entries mapped or 0 on error. |
| 834 | */ |
Logan Gunthorpe | 2b9f4bb | 2019-08-12 11:30:42 -0600 | [diff] [blame] | 835 | int pci_p2pdma_map_sg_attrs(struct device *dev, struct scatterlist *sg, |
| 836 | int nents, enum dma_data_direction dir, unsigned long attrs) |
Logan Gunthorpe | 977196b | 2018-10-04 15:27:37 -0600 | [diff] [blame] | 837 | { |
Logan Gunthorpe | 142c242 | 2019-08-12 11:30:44 -0600 | [diff] [blame^] | 838 | struct pci_p2pdma_pagemap *p2p_pgmap = |
| 839 | to_p2p_pgmap(sg_page(sg)->pgmap); |
Logan Gunthorpe | 977196b | 2018-10-04 15:27:37 -0600 | [diff] [blame] | 840 | |
Logan Gunthorpe | 142c242 | 2019-08-12 11:30:44 -0600 | [diff] [blame^] | 841 | return __pci_p2pdma_map_sg(p2p_pgmap, dev, sg, nents); |
Logan Gunthorpe | 977196b | 2018-10-04 15:27:37 -0600 | [diff] [blame] | 842 | } |
Logan Gunthorpe | 2b9f4bb | 2019-08-12 11:30:42 -0600 | [diff] [blame] | 843 | EXPORT_SYMBOL_GPL(pci_p2pdma_map_sg_attrs); |
Logan Gunthorpe | 2d7bc01 | 2018-10-04 15:27:38 -0600 | [diff] [blame] | 844 | |
| 845 | /** |
Logan Gunthorpe | 7f73eac | 2019-08-12 11:30:43 -0600 | [diff] [blame] | 846 | * 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 | */ |
| 854 | void pci_p2pdma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, |
| 855 | int nents, enum dma_data_direction dir, unsigned long attrs) |
| 856 | { |
| 857 | } |
| 858 | EXPORT_SYMBOL_GPL(pci_p2pdma_unmap_sg_attrs); |
| 859 | |
| 860 | /** |
Logan Gunthorpe | 2d7bc01 | 2018-10-04 15:27:38 -0600 | [diff] [blame] | 861 | * 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 Dunlap | fcf9ab3 | 2018-12-01 09:31:34 -0800 | [diff] [blame] | 869 | * The value can select a PCI device (using its full BDF device |
Logan Gunthorpe | 2d7bc01 | 2018-10-04 15:27:38 -0600 | [diff] [blame] | 870 | * 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 | */ |
| 880 | int 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 | } |
| 913 | EXPORT_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 Dunlap | fcf9ab3 | 2018-12-01 09:31:34 -0800 | [diff] [blame] | 920 | * @use_p2pdma: whether p2pdma has been enabled |
Logan Gunthorpe | 2d7bc01 | 2018-10-04 15:27:38 -0600 | [diff] [blame] | 921 | * |
| 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 | */ |
| 927 | ssize_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 | } |
| 938 | EXPORT_SYMBOL_GPL(pci_p2pdma_enable_show); |