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