1 // SPDX-License-Identifier: GPL-2.0-only
3 * Xen grant DMA-mapping layer - contains special DMA-mapping routines
4 * for providing grant references as DMA addresses to be used by frontends
5 * (e.g. virtio) in Xen guests
7 * Copyright (c) 2021, Juergen Gross <jgross@suse.com>
10 #include <linux/module.h>
11 #include <linux/dma-map-ops.h>
13 #include <linux/pfn.h>
14 #include <linux/xarray.h>
15 #include <linux/virtio_anchor.h>
16 #include <linux/virtio.h>
18 #include <xen/xen-ops.h>
19 #include <xen/grant_table.h>
21 struct xen_grant_dma_data {
22 /* The ID of backend domain */
23 domid_t backend_domid;
24 /* Is device behaving sane? */
28 static DEFINE_XARRAY_FLAGS(xen_grant_dma_devices, XA_FLAGS_LOCK_IRQ);
30 #define XEN_GRANT_DMA_ADDR_OFF (1ULL << 63)
32 static inline dma_addr_t grant_to_dma(grant_ref_t grant)
34 return XEN_GRANT_DMA_ADDR_OFF | ((dma_addr_t)grant << XEN_PAGE_SHIFT);
37 static inline grant_ref_t dma_to_grant(dma_addr_t dma)
39 return (grant_ref_t)((dma & ~XEN_GRANT_DMA_ADDR_OFF) >> XEN_PAGE_SHIFT);
42 static struct xen_grant_dma_data *find_xen_grant_dma_data(struct device *dev)
44 struct xen_grant_dma_data *data;
47 xa_lock_irqsave(&xen_grant_dma_devices, flags);
48 data = xa_load(&xen_grant_dma_devices, (unsigned long)dev);
49 xa_unlock_irqrestore(&xen_grant_dma_devices, flags);
54 static int store_xen_grant_dma_data(struct device *dev,
55 struct xen_grant_dma_data *data)
60 xa_lock_irqsave(&xen_grant_dma_devices, flags);
61 ret = xa_err(__xa_store(&xen_grant_dma_devices, (unsigned long)dev, data,
63 xa_unlock_irqrestore(&xen_grant_dma_devices, flags);
69 * DMA ops for Xen frontends (e.g. virtio).
71 * Used to act as a kind of software IOMMU for Xen guests by using grants as
73 * Such a DMA address is formed by using the grant reference as a frame
74 * number and setting the highest address bit (this bit is for the backend
75 * to be able to distinguish it from e.g. a mmio address).
77 static void *xen_grant_dma_alloc(struct device *dev, size_t size,
78 dma_addr_t *dma_handle, gfp_t gfp,
81 struct xen_grant_dma_data *data;
82 unsigned int i, n_pages = XEN_PFN_UP(size);
87 data = find_xen_grant_dma_data(dev);
91 if (unlikely(data->broken))
94 ret = alloc_pages_exact(n_pages * XEN_PAGE_SIZE, gfp);
98 pfn = virt_to_pfn(ret);
100 if (gnttab_alloc_grant_reference_seq(n_pages, &grant)) {
101 free_pages_exact(ret, n_pages * XEN_PAGE_SIZE);
105 for (i = 0; i < n_pages; i++) {
106 gnttab_grant_foreign_access_ref(grant + i, data->backend_domid,
107 pfn_to_gfn(pfn + i), 0);
110 *dma_handle = grant_to_dma(grant);
115 static void xen_grant_dma_free(struct device *dev, size_t size, void *vaddr,
116 dma_addr_t dma_handle, unsigned long attrs)
118 struct xen_grant_dma_data *data;
119 unsigned int i, n_pages = XEN_PFN_UP(size);
122 data = find_xen_grant_dma_data(dev);
126 if (unlikely(data->broken))
129 grant = dma_to_grant(dma_handle);
131 for (i = 0; i < n_pages; i++) {
132 if (unlikely(!gnttab_end_foreign_access_ref(grant + i))) {
133 dev_alert(dev, "Grant still in use by backend domain, disabled for further use\n");
139 gnttab_free_grant_reference_seq(grant, n_pages);
141 free_pages_exact(vaddr, n_pages * XEN_PAGE_SIZE);
144 static struct page *xen_grant_dma_alloc_pages(struct device *dev, size_t size,
145 dma_addr_t *dma_handle,
146 enum dma_data_direction dir,
151 vaddr = xen_grant_dma_alloc(dev, size, dma_handle, gfp, 0);
155 return virt_to_page(vaddr);
158 static void xen_grant_dma_free_pages(struct device *dev, size_t size,
159 struct page *vaddr, dma_addr_t dma_handle,
160 enum dma_data_direction dir)
162 xen_grant_dma_free(dev, size, page_to_virt(vaddr), dma_handle, 0);
165 static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *page,
166 unsigned long offset, size_t size,
167 enum dma_data_direction dir,
170 struct xen_grant_dma_data *data;
171 unsigned long dma_offset = xen_offset_in_page(offset),
172 pfn_offset = XEN_PFN_DOWN(offset);
173 unsigned int i, n_pages = XEN_PFN_UP(dma_offset + size);
175 dma_addr_t dma_handle;
177 if (WARN_ON(dir == DMA_NONE))
178 return DMA_MAPPING_ERROR;
180 data = find_xen_grant_dma_data(dev);
182 return DMA_MAPPING_ERROR;
184 if (unlikely(data->broken))
185 return DMA_MAPPING_ERROR;
187 if (gnttab_alloc_grant_reference_seq(n_pages, &grant))
188 return DMA_MAPPING_ERROR;
190 for (i = 0; i < n_pages; i++) {
191 gnttab_grant_foreign_access_ref(grant + i, data->backend_domid,
192 pfn_to_gfn(page_to_xen_pfn(page) + i + pfn_offset),
193 dir == DMA_TO_DEVICE);
196 dma_handle = grant_to_dma(grant) + dma_offset;
201 static void xen_grant_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
202 size_t size, enum dma_data_direction dir,
205 struct xen_grant_dma_data *data;
206 unsigned long dma_offset = xen_offset_in_page(dma_handle);
207 unsigned int i, n_pages = XEN_PFN_UP(dma_offset + size);
210 if (WARN_ON(dir == DMA_NONE))
213 data = find_xen_grant_dma_data(dev);
217 if (unlikely(data->broken))
220 grant = dma_to_grant(dma_handle);
222 for (i = 0; i < n_pages; i++) {
223 if (unlikely(!gnttab_end_foreign_access_ref(grant + i))) {
224 dev_alert(dev, "Grant still in use by backend domain, disabled for further use\n");
230 gnttab_free_grant_reference_seq(grant, n_pages);
233 static void xen_grant_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
234 int nents, enum dma_data_direction dir,
237 struct scatterlist *s;
240 if (WARN_ON(dir == DMA_NONE))
243 for_each_sg(sg, s, nents, i)
244 xen_grant_dma_unmap_page(dev, s->dma_address, sg_dma_len(s), dir,
248 static int xen_grant_dma_map_sg(struct device *dev, struct scatterlist *sg,
249 int nents, enum dma_data_direction dir,
252 struct scatterlist *s;
255 if (WARN_ON(dir == DMA_NONE))
258 for_each_sg(sg, s, nents, i) {
259 s->dma_address = xen_grant_dma_map_page(dev, sg_page(s), s->offset,
260 s->length, dir, attrs);
261 if (s->dma_address == DMA_MAPPING_ERROR)
264 sg_dma_len(s) = s->length;
270 xen_grant_dma_unmap_sg(dev, sg, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
276 static int xen_grant_dma_supported(struct device *dev, u64 mask)
278 return mask == DMA_BIT_MASK(64);
281 static const struct dma_map_ops xen_grant_dma_ops = {
282 .alloc = xen_grant_dma_alloc,
283 .free = xen_grant_dma_free,
284 .alloc_pages = xen_grant_dma_alloc_pages,
285 .free_pages = xen_grant_dma_free_pages,
286 .mmap = dma_common_mmap,
287 .get_sgtable = dma_common_get_sgtable,
288 .map_page = xen_grant_dma_map_page,
289 .unmap_page = xen_grant_dma_unmap_page,
290 .map_sg = xen_grant_dma_map_sg,
291 .unmap_sg = xen_grant_dma_unmap_sg,
292 .dma_supported = xen_grant_dma_supported,
295 static bool xen_is_dt_grant_dma_device(struct device *dev)
297 struct device_node *iommu_np;
300 iommu_np = of_parse_phandle(dev->of_node, "iommus", 0);
301 has_iommu = iommu_np &&
302 of_device_is_compatible(iommu_np, "xen,grant-dma");
303 of_node_put(iommu_np);
308 bool xen_is_grant_dma_device(struct device *dev)
310 /* XXX Handle only DT devices for now */
312 return xen_is_dt_grant_dma_device(dev);
317 bool xen_virtio_mem_acc(struct virtio_device *dev)
319 if (IS_ENABLED(CONFIG_XEN_VIRTIO_FORCE_GRANT) || xen_pv_domain())
322 return xen_is_grant_dma_device(dev->dev.parent);
325 static int xen_dt_grant_init_backend_domid(struct device *dev,
326 struct xen_grant_dma_data *data)
328 struct of_phandle_args iommu_spec;
330 if (of_parse_phandle_with_args(dev->of_node, "iommus", "#iommu-cells",
332 dev_err(dev, "Cannot parse iommus property\n");
336 if (!of_device_is_compatible(iommu_spec.np, "xen,grant-dma") ||
337 iommu_spec.args_count != 1) {
338 dev_err(dev, "Incompatible IOMMU node\n");
339 of_node_put(iommu_spec.np);
343 of_node_put(iommu_spec.np);
346 * The endpoint ID here means the ID of the domain where the
347 * corresponding backend is running
349 data->backend_domid = iommu_spec.args[0];
354 void xen_grant_setup_dma_ops(struct device *dev)
356 struct xen_grant_dma_data *data;
358 data = find_xen_grant_dma_data(dev);
360 dev_err(dev, "Xen grant DMA data is already created\n");
364 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
369 if (xen_dt_grant_init_backend_domid(dev, data))
371 } else if (IS_ENABLED(CONFIG_XEN_VIRTIO_FORCE_GRANT)) {
372 dev_info(dev, "Using dom0 as backend\n");
373 data->backend_domid = 0;
375 /* XXX ACPI device unsupported for now */
379 if (store_xen_grant_dma_data(dev, data)) {
380 dev_err(dev, "Cannot store Xen grant DMA data\n");
384 dev->dma_ops = &xen_grant_dma_ops;
389 devm_kfree(dev, data);
390 dev_err(dev, "Cannot set up Xen grant DMA ops, retain platform DMA ops\n");
393 bool xen_virtio_restricted_mem_acc(struct virtio_device *dev)
395 bool ret = xen_virtio_mem_acc(dev);
398 xen_grant_setup_dma_ops(dev->dev.parent);
403 MODULE_DESCRIPTION("Xen grant DMA-mapping layer");
404 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");
405 MODULE_LICENSE("GPL");