2019-05-19 20:08:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2014-11-21 19:06:39 +08:00
|
|
|
#include <linux/cpu.h>
|
2019-07-24 20:06:54 +08:00
|
|
|
#include <linux/dma-noncoherent.h>
|
2013-10-10 21:40:44 +08:00
|
|
|
#include <linux/gfp.h>
|
2014-11-21 19:06:39 +08:00
|
|
|
#include <linux/highmem.h>
|
2013-10-10 21:40:44 +08:00
|
|
|
#include <linux/export.h>
|
2015-04-24 17:16:40 +08:00
|
|
|
#include <linux/memblock.h>
|
2014-11-21 19:06:39 +08:00
|
|
|
#include <linux/of_address.h>
|
2013-10-10 21:40:44 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/swiotlb.h>
|
|
|
|
|
|
|
|
#include <xen/xen.h>
|
2014-11-21 19:08:39 +08:00
|
|
|
#include <xen/interface/grant_table.h>
|
2013-10-10 21:40:44 +08:00
|
|
|
#include <xen/interface/memory.h>
|
2015-06-17 22:28:02 +08:00
|
|
|
#include <xen/page.h>
|
2019-10-22 20:50:06 +08:00
|
|
|
#include <xen/xen-ops.h>
|
2013-10-10 21:40:44 +08:00
|
|
|
#include <xen/swiotlb-xen.h>
|
|
|
|
|
|
|
|
#include <asm/cacheflush.h>
|
|
|
|
#include <asm/xen/hypercall.h>
|
|
|
|
#include <asm/xen/interface.h>
|
|
|
|
|
2015-04-24 17:16:40 +08:00
|
|
|
unsigned long xen_get_swiotlb_free_pages(unsigned int order)
|
|
|
|
{
|
|
|
|
struct memblock_region *reg;
|
2015-11-07 08:28:21 +08:00
|
|
|
gfp_t flags = __GFP_NOWARN|__GFP_KSWAPD_RECLAIM;
|
2015-04-24 17:16:40 +08:00
|
|
|
|
|
|
|
for_each_memblock(memory, reg) {
|
|
|
|
if (reg->base < (phys_addr_t)0xffffffff) {
|
2019-09-16 17:51:33 +08:00
|
|
|
if (IS_ENABLED(CONFIG_ZONE_DMA32))
|
|
|
|
flags |= __GFP_DMA32;
|
|
|
|
else
|
|
|
|
flags |= __GFP_DMA;
|
2015-04-24 17:16:40 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return __get_free_pages(flags, order);
|
|
|
|
}
|
|
|
|
|
2014-11-21 19:08:39 +08:00
|
|
|
static bool hypercall_cflush = false;
|
2014-11-21 19:06:39 +08:00
|
|
|
|
2019-07-24 21:26:08 +08:00
|
|
|
/* buffers in highmem or foreign pages cannot cross page boundaries */
|
|
|
|
static void dma_cache_maint(dma_addr_t handle, size_t size, u32 op)
|
2014-11-21 19:06:39 +08:00
|
|
|
{
|
2014-11-21 19:08:39 +08:00
|
|
|
struct gnttab_cache_flush cflush;
|
2014-11-21 19:06:39 +08:00
|
|
|
|
2019-07-24 21:26:08 +08:00
|
|
|
cflush.a.dev_bus_addr = handle & XEN_PAGE_MASK;
|
|
|
|
cflush.offset = xen_offset_in_page(handle);
|
|
|
|
cflush.op = op;
|
2014-11-21 19:06:39 +08:00
|
|
|
|
|
|
|
do {
|
2019-07-24 21:26:08 +08:00
|
|
|
if (size + cflush.offset > XEN_PAGE_SIZE)
|
|
|
|
cflush.length = XEN_PAGE_SIZE - cflush.offset;
|
|
|
|
else
|
|
|
|
cflush.length = size;
|
|
|
|
|
|
|
|
HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1);
|
2014-11-21 19:06:39 +08:00
|
|
|
|
2019-07-24 21:26:08 +08:00
|
|
|
cflush.offset = 0;
|
|
|
|
cflush.a.dev_bus_addr += cflush.length;
|
|
|
|
size -= cflush.length;
|
|
|
|
} while (size);
|
2014-11-21 19:06:39 +08:00
|
|
|
}
|
|
|
|
|
2019-09-05 16:04:30 +08:00
|
|
|
/*
|
|
|
|
* Dom0 is mapped 1:1, and while the Linux page can span across multiple Xen
|
|
|
|
* pages, it is not possible for it to contain a mix of local and foreign Xen
|
|
|
|
* pages. Calling pfn_valid on a foreign mfn will always return false, so if
|
|
|
|
* pfn_valid returns true the pages is local and we can use the native
|
|
|
|
* dma-direct functions, otherwise we call the Xen specific version.
|
|
|
|
*/
|
2019-11-08 01:03:11 +08:00
|
|
|
void xen_dma_sync_for_cpu(dma_addr_t handle, phys_addr_t paddr, size_t size,
|
|
|
|
enum dma_data_direction dir)
|
2014-11-21 19:06:39 +08:00
|
|
|
{
|
2019-09-05 16:04:30 +08:00
|
|
|
if (pfn_valid(PFN_DOWN(handle)))
|
2019-11-08 01:03:11 +08:00
|
|
|
arch_sync_dma_for_cpu(paddr, size, dir);
|
2019-09-05 16:04:30 +08:00
|
|
|
else if (dir != DMA_TO_DEVICE)
|
2019-07-24 21:26:08 +08:00
|
|
|
dma_cache_maint(handle, size, GNTTAB_CACHE_INVAL);
|
2014-11-21 19:06:39 +08:00
|
|
|
}
|
|
|
|
|
2019-11-08 01:03:11 +08:00
|
|
|
void xen_dma_sync_for_device(dma_addr_t handle, phys_addr_t paddr, size_t size,
|
|
|
|
enum dma_data_direction dir)
|
2014-11-21 19:06:39 +08:00
|
|
|
{
|
2019-09-05 16:04:30 +08:00
|
|
|
if (pfn_valid(PFN_DOWN(handle)))
|
2019-11-08 01:03:11 +08:00
|
|
|
arch_sync_dma_for_device(paddr, size, dir);
|
2019-09-05 16:04:30 +08:00
|
|
|
else if (dir == DMA_FROM_DEVICE)
|
2019-07-24 21:26:08 +08:00
|
|
|
dma_cache_maint(handle, size, GNTTAB_CACHE_INVAL);
|
|
|
|
else
|
|
|
|
dma_cache_maint(handle, size, GNTTAB_CACHE_CLEAN);
|
2014-11-21 19:06:39 +08:00
|
|
|
}
|
|
|
|
|
2014-11-21 19:07:39 +08:00
|
|
|
bool xen_arch_need_swiotlb(struct device *dev,
|
2015-09-09 22:17:33 +08:00
|
|
|
phys_addr_t phys,
|
|
|
|
dma_addr_t dev_addr)
|
2014-11-21 19:07:39 +08:00
|
|
|
{
|
2015-09-09 22:18:45 +08:00
|
|
|
unsigned int xen_pfn = XEN_PFN_DOWN(phys);
|
|
|
|
unsigned int bfn = XEN_PFN_DOWN(dev_addr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The swiotlb buffer should be used if
|
|
|
|
* - Xen doesn't have the cache flush hypercall
|
|
|
|
* - The Linux page refers to foreign memory
|
|
|
|
* - The device doesn't support coherent DMA request
|
|
|
|
*
|
|
|
|
* The Linux page may be spanned acrros multiple Xen page, although
|
|
|
|
* it's not possible to have a mix of local and foreign Xen page.
|
|
|
|
* Furthermore, range_straddles_page_boundary is already checking
|
|
|
|
* if buffer is physically contiguous in the host RAM.
|
|
|
|
*
|
|
|
|
* Therefore we only need to check the first Xen page to know if we
|
|
|
|
* require a bounce buffer because the device doesn't support coherent
|
|
|
|
* memory and we are not able to flush the cache.
|
|
|
|
*/
|
|
|
|
return (!hypercall_cflush && (xen_pfn != bfn) &&
|
2019-07-24 20:06:54 +08:00
|
|
|
!dev_is_dma_coherent(dev));
|
2014-11-21 19:07:39 +08:00
|
|
|
}
|
|
|
|
|
2013-10-10 21:41:10 +08:00
|
|
|
int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
|
2013-10-10 21:40:44 +08:00
|
|
|
unsigned int address_bits,
|
|
|
|
dma_addr_t *dma_handle)
|
|
|
|
{
|
|
|
|
if (!xen_initial_domain())
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* we assume that dom0 is mapped 1:1 for now */
|
2013-10-10 21:41:10 +08:00
|
|
|
*dma_handle = pstart;
|
2013-10-10 21:40:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-10 21:41:10 +08:00
|
|
|
void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order)
|
2013-10-10 21:40:44 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-22 20:52:51 +08:00
|
|
|
static int __init xen_mm_init(void)
|
2013-10-10 21:40:44 +08:00
|
|
|
{
|
2014-11-21 19:08:39 +08:00
|
|
|
struct gnttab_cache_flush cflush;
|
2013-10-10 21:40:44 +08:00
|
|
|
if (!xen_initial_domain())
|
|
|
|
return 0;
|
|
|
|
xen_swiotlb_init(1, false);
|
2014-11-21 19:08:39 +08:00
|
|
|
|
|
|
|
cflush.op = 0;
|
|
|
|
cflush.a.dev_bus_addr = 0;
|
|
|
|
cflush.offset = 0;
|
|
|
|
cflush.length = 0;
|
|
|
|
if (HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1) != -ENOSYS)
|
|
|
|
hypercall_cflush = true;
|
2013-10-10 21:40:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
arch_initcall(xen_mm_init);
|