Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
11#ifndef _LINUX_PCI_P2PDMA_H
12#define _LINUX_PCI_P2PDMA_H
13
14#include <linux/pci.h>
15
16struct block_device;
17struct scatterlist;
18
19/**
20 * struct p2pdma_provider
21 *
22 * A p2pdma provider is a range of MMIO address space available to the CPU.
23 */
24struct p2pdma_provider {
25 struct device *owner;
26 u64 bus_offset;
27};
28
29enum pci_p2pdma_map_type {
30 /*
31 * PCI_P2PDMA_MAP_UNKNOWN: Used internally as an initial state before
32 * the mapping type has been calculated. Exported routines for the API
33 * will never return this value.
34 */
35 PCI_P2PDMA_MAP_UNKNOWN = 0,
36
37 /*
38 * Not a PCI P2PDMA transfer.
39 */
40 PCI_P2PDMA_MAP_NONE,
41
42 /*
43 * PCI_P2PDMA_MAP_NOT_SUPPORTED: Indicates the transaction will
44 * traverse the host bridge and the host bridge is not in the
45 * allowlist. DMA Mapping routines should return an error when
46 * this is returned.
47 */
48 PCI_P2PDMA_MAP_NOT_SUPPORTED,
49
50 /*
51 * PCI_P2PDMA_MAP_BUS_ADDR: Indicates that two devices can talk to
52 * each other directly through a PCI switch and the transaction will
53 * not traverse the host bridge. Such a mapping should program
54 * the DMA engine with PCI bus addresses.
55 */
56 PCI_P2PDMA_MAP_BUS_ADDR,
57
58 /*
59 * PCI_P2PDMA_MAP_THRU_HOST_BRIDGE: Indicates two devices can talk
60 * to each other, but the transaction traverses a host bridge on the
61 * allowlist. In this case, a normal mapping either with CPU physical
62 * addresses (in the case of dma-direct) or IOVA addresses (in the
63 * case of IOMMUs) should be used to program the DMA engine.
64 */
65 PCI_P2PDMA_MAP_THRU_HOST_BRIDGE,
66};
67
68#ifdef CONFIG_PCI_P2PDMA
69int pcim_p2pdma_init(struct pci_dev *pdev);
70struct p2pdma_provider *pcim_p2pdma_provider(struct pci_dev *pdev, int bar);
71int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size,
72 u64 offset);
73int pci_p2pdma_distance_many(struct pci_dev *provider, struct device **clients,
74 int num_clients, bool verbose);
75struct pci_dev *pci_p2pmem_find_many(struct device **clients, int num_clients);
76void *pci_alloc_p2pmem(struct pci_dev *pdev, size_t size);
77void pci_free_p2pmem(struct pci_dev *pdev, void *addr, size_t size);
78pci_bus_addr_t pci_p2pmem_virt_to_bus(struct pci_dev *pdev, void *addr);
79struct scatterlist *pci_p2pmem_alloc_sgl(struct pci_dev *pdev,
80 unsigned int *nents, u32 length);
81void pci_p2pmem_free_sgl(struct pci_dev *pdev, struct scatterlist *sgl);
82void pci_p2pmem_publish(struct pci_dev *pdev, bool publish);
83int pci_p2pdma_enable_store(const char *page, struct pci_dev **p2p_dev,
84 bool *use_p2pdma);
85ssize_t pci_p2pdma_enable_show(char *page, struct pci_dev *p2p_dev,
86 bool use_p2pdma);
87enum pci_p2pdma_map_type pci_p2pdma_map_type(struct p2pdma_provider *provider,
88 struct device *dev);
89#else /* CONFIG_PCI_P2PDMA */
90static inline int pcim_p2pdma_init(struct pci_dev *pdev)
91{
92 return -EOPNOTSUPP;
93}
94static inline struct p2pdma_provider *pcim_p2pdma_provider(struct pci_dev *pdev,
95 int bar)
96{
97 return NULL;
98}
99static inline int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar,
100 size_t size, u64 offset)
101{
102 return -EOPNOTSUPP;
103}
104static inline int pci_p2pdma_distance_many(struct pci_dev *provider,
105 struct device **clients, int num_clients, bool verbose)
106{
107 return -1;
108}
109static inline struct pci_dev *pci_p2pmem_find_many(struct device **clients,
110 int num_clients)
111{
112 return NULL;
113}
114static inline void *pci_alloc_p2pmem(struct pci_dev *pdev, size_t size)
115{
116 return NULL;
117}
118static inline void pci_free_p2pmem(struct pci_dev *pdev, void *addr,
119 size_t size)
120{
121}
122static inline pci_bus_addr_t pci_p2pmem_virt_to_bus(struct pci_dev *pdev,
123 void *addr)
124{
125 return 0;
126}
127static inline struct scatterlist *pci_p2pmem_alloc_sgl(struct pci_dev *pdev,
128 unsigned int *nents, u32 length)
129{
130 return NULL;
131}
132static inline void pci_p2pmem_free_sgl(struct pci_dev *pdev,
133 struct scatterlist *sgl)
134{
135}
136static inline void pci_p2pmem_publish(struct pci_dev *pdev, bool publish)
137{
138}
139static inline int pci_p2pdma_enable_store(const char *page,
140 struct pci_dev **p2p_dev, bool *use_p2pdma)
141{
142 *use_p2pdma = false;
143 return 0;
144}
145static inline ssize_t pci_p2pdma_enable_show(char *page,
146 struct pci_dev *p2p_dev, bool use_p2pdma)
147{
148 return sprintf(page, "none\n");
149}
150static inline enum pci_p2pdma_map_type
151pci_p2pdma_map_type(struct p2pdma_provider *provider, struct device *dev)
152{
153 return PCI_P2PDMA_MAP_NOT_SUPPORTED;
154}
155#endif /* CONFIG_PCI_P2PDMA */
156
157
158static inline int pci_p2pdma_distance(struct pci_dev *provider,
159 struct device *client, bool verbose)
160{
161 return pci_p2pdma_distance_many(provider, &client, 1, verbose);
162}
163
164static inline struct pci_dev *pci_p2pmem_find(struct device *client)
165{
166 return pci_p2pmem_find_many(&client, 1);
167}
168
169struct pci_p2pdma_map_state {
170 struct p2pdma_provider *mem;
171 enum pci_p2pdma_map_type map;
172};
173
174
175/* helper for pci_p2pdma_state(), do not use directly */
176void __pci_p2pdma_update_state(struct pci_p2pdma_map_state *state,
177 struct device *dev, struct page *page);
178
179/**
180 * pci_p2pdma_state - check the P2P transfer state of a page
181 * @state: P2P state structure
182 * @dev: device to transfer to/from
183 * @page: page to map
184 *
185 * Check if @page is a PCI P2PDMA page, and if yes of what kind. Returns the
186 * map type, and updates @state with all information needed for a P2P transfer.
187 */
188static inline enum pci_p2pdma_map_type
189pci_p2pdma_state(struct pci_p2pdma_map_state *state, struct device *dev,
190 struct page *page)
191{
192 if (IS_ENABLED(CONFIG_PCI_P2PDMA) && is_pci_p2pdma_page(page)) {
193 __pci_p2pdma_update_state(state, dev, page);
194 return state->map;
195 }
196 return PCI_P2PDMA_MAP_NONE;
197}
198
199/**
200 * pci_p2pdma_bus_addr_map - Translate a physical address to a bus address
201 * for a PCI_P2PDMA_MAP_BUS_ADDR transfer.
202 * @provider: P2P provider structure
203 * @paddr: physical address to map
204 *
205 * Map a physically contiguous PCI_P2PDMA_MAP_BUS_ADDR transfer.
206 */
207static inline dma_addr_t
208pci_p2pdma_bus_addr_map(struct p2pdma_provider *provider, phys_addr_t paddr)
209{
210 return paddr + provider->bus_offset;
211}
212
213#endif /* _LINUX_PCI_P2P_H */