blob: 467fa225c3d0ed5c9793107f583c2be74d7a88c9 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002#include <linux/cpu.h>
Olivier Deprez157378f2022-04-04 15:47:50 +02003#include <linux/dma-direct.h>
4#include <linux/dma-map-ops.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005#include <linux/gfp.h>
6#include <linux/highmem.h>
7#include <linux/export.h>
8#include <linux/memblock.h>
9#include <linux/of_address.h>
10#include <linux/slab.h>
11#include <linux/types.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include <linux/vmalloc.h>
13#include <linux/swiotlb.h>
14
15#include <xen/xen.h>
16#include <xen/interface/grant_table.h>
17#include <xen/interface/memory.h>
18#include <xen/page.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020019#include <xen/xen-ops.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020#include <xen/swiotlb-xen.h>
21
22#include <asm/cacheflush.h>
23#include <asm/xen/hypercall.h>
24#include <asm/xen/interface.h>
25
26unsigned long xen_get_swiotlb_free_pages(unsigned int order)
27{
Olivier Deprez157378f2022-04-04 15:47:50 +020028 phys_addr_t base;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029 gfp_t flags = __GFP_NOWARN|__GFP_KSWAPD_RECLAIM;
Olivier Deprez157378f2022-04-04 15:47:50 +020030 u64 i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031
Olivier Deprez157378f2022-04-04 15:47:50 +020032 for_each_mem_range(i, &base, NULL) {
33 if (base < (phys_addr_t)0xffffffff) {
David Brazdil0f672f62019-12-10 10:32:29 +000034 if (IS_ENABLED(CONFIG_ZONE_DMA32))
35 flags |= __GFP_DMA32;
36 else
37 flags |= __GFP_DMA;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038 break;
39 }
40 }
41 return __get_free_pages(flags, order);
42}
43
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044static bool hypercall_cflush = false;
45
David Brazdil0f672f62019-12-10 10:32:29 +000046/* buffers in highmem or foreign pages cannot cross page boundaries */
Olivier Deprez157378f2022-04-04 15:47:50 +020047static void dma_cache_maint(struct device *dev, dma_addr_t handle,
48 size_t size, u32 op)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049{
50 struct gnttab_cache_flush cflush;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051
David Brazdil0f672f62019-12-10 10:32:29 +000052 cflush.offset = xen_offset_in_page(handle);
53 cflush.op = op;
Olivier Deprez157378f2022-04-04 15:47:50 +020054 handle &= XEN_PAGE_MASK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055
56 do {
Olivier Deprez157378f2022-04-04 15:47:50 +020057 cflush.a.dev_bus_addr = dma_to_phys(dev, handle);
58
David Brazdil0f672f62019-12-10 10:32:29 +000059 if (size + cflush.offset > XEN_PAGE_SIZE)
60 cflush.length = XEN_PAGE_SIZE - cflush.offset;
61 else
62 cflush.length = size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063
David Brazdil0f672f62019-12-10 10:32:29 +000064 HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065
David Brazdil0f672f62019-12-10 10:32:29 +000066 cflush.offset = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +020067 handle += cflush.length;
David Brazdil0f672f62019-12-10 10:32:29 +000068 size -= cflush.length;
69 } while (size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070}
71
David Brazdil0f672f62019-12-10 10:32:29 +000072/*
73 * Dom0 is mapped 1:1, and while the Linux page can span across multiple Xen
74 * pages, it is not possible for it to contain a mix of local and foreign Xen
75 * pages. Calling pfn_valid on a foreign mfn will always return false, so if
76 * pfn_valid returns true the pages is local and we can use the native
77 * dma-direct functions, otherwise we call the Xen specific version.
78 */
79void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,
Olivier Deprez157378f2022-04-04 15:47:50 +020080 size_t size, enum dma_data_direction dir)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081{
Olivier Deprez157378f2022-04-04 15:47:50 +020082 if (dir != DMA_TO_DEVICE)
83 dma_cache_maint(dev, handle, size, GNTTAB_CACHE_INVAL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084}
85
David Brazdil0f672f62019-12-10 10:32:29 +000086void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,
Olivier Deprez157378f2022-04-04 15:47:50 +020087 size_t size, enum dma_data_direction dir)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088{
Olivier Deprez157378f2022-04-04 15:47:50 +020089 if (dir == DMA_FROM_DEVICE)
90 dma_cache_maint(dev, handle, size, GNTTAB_CACHE_INVAL);
David Brazdil0f672f62019-12-10 10:32:29 +000091 else
Olivier Deprez157378f2022-04-04 15:47:50 +020092 dma_cache_maint(dev, handle, size, GNTTAB_CACHE_CLEAN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093}
94
95bool xen_arch_need_swiotlb(struct device *dev,
96 phys_addr_t phys,
97 dma_addr_t dev_addr)
98{
99 unsigned int xen_pfn = XEN_PFN_DOWN(phys);
Olivier Deprez157378f2022-04-04 15:47:50 +0200100 unsigned int bfn = XEN_PFN_DOWN(dma_to_phys(dev, dev_addr));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000101
102 /*
103 * The swiotlb buffer should be used if
104 * - Xen doesn't have the cache flush hypercall
105 * - The Linux page refers to foreign memory
106 * - The device doesn't support coherent DMA request
107 *
108 * The Linux page may be spanned acrros multiple Xen page, although
109 * it's not possible to have a mix of local and foreign Xen page.
110 * Furthermore, range_straddles_page_boundary is already checking
111 * if buffer is physically contiguous in the host RAM.
112 *
113 * Therefore we only need to check the first Xen page to know if we
114 * require a bounce buffer because the device doesn't support coherent
115 * memory and we are not able to flush the cache.
116 */
117 return (!hypercall_cflush && (xen_pfn != bfn) &&
David Brazdil0f672f62019-12-10 10:32:29 +0000118 !dev_is_dma_coherent(dev));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119}
120
121int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
122 unsigned int address_bits,
123 dma_addr_t *dma_handle)
124{
125 if (!xen_initial_domain())
126 return -EINVAL;
127
128 /* we assume that dom0 is mapped 1:1 for now */
129 *dma_handle = pstart;
130 return 0;
131}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132
133void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order)
134{
135 return;
136}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137
Olivier Deprez157378f2022-04-04 15:47:50 +0200138static int __init xen_mm_init(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000139{
140 struct gnttab_cache_flush cflush;
141 if (!xen_initial_domain())
142 return 0;
143 xen_swiotlb_init(1, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000144
145 cflush.op = 0;
146 cflush.a.dev_bus_addr = 0;
147 cflush.offset = 0;
148 cflush.length = 0;
149 if (HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1) != -ENOSYS)
150 hypercall_cflush = true;
151 return 0;
152}
153arch_initcall(xen_mm_init);