18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Microblaze support for cache consistent memory.
48c2ecf20Sopenharmony_ci * Copyright (C) 2010 Michal Simek <monstr@monstr.eu>
58c2ecf20Sopenharmony_ci * Copyright (C) 2010 PetaLogix
68c2ecf20Sopenharmony_ci * Copyright (C) 2005 John Williams <jwilliams@itee.uq.edu.au>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/string.h>
118c2ecf20Sopenharmony_ci#include <linux/types.h>
128c2ecf20Sopenharmony_ci#include <linux/mm.h>
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/dma-map-ops.h>
158c2ecf20Sopenharmony_ci#include <asm/cpuinfo.h>
168c2ecf20Sopenharmony_ci#include <asm/cacheflush.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_civoid arch_dma_prep_coherent(struct page *page, size_t size)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	phys_addr_t paddr = page_to_phys(page);
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	flush_dcache_range(paddr, paddr + size);
238c2ecf20Sopenharmony_ci}
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#ifndef CONFIG_MMU
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * Consistent memory allocators. Used for DMA devices that want to share
288c2ecf20Sopenharmony_ci * uncached memory with the processor core.  My crufty no-MMU approach is
298c2ecf20Sopenharmony_ci * simple.  In the HW platform we can optionally mirror the DDR up above the
308c2ecf20Sopenharmony_ci * processor cacheable region.  So, memory accessed in this mirror region will
318c2ecf20Sopenharmony_ci * not be cached.  It's alloced from the same pool as normal memory, but the
328c2ecf20Sopenharmony_ci * handle we return is shifted up into the uncached region.  This will no doubt
338c2ecf20Sopenharmony_ci * cause big problems if memory allocated here is not also freed properly. -- JW
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * I have to use dcache values because I can't relate on ram size:
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci#ifdef CONFIG_XILINX_UNCACHED_SHADOW
388c2ecf20Sopenharmony_ci#define UNCACHED_SHADOW_MASK (cpuinfo.dcache_high - cpuinfo.dcache_base + 1)
398c2ecf20Sopenharmony_ci#else
408c2ecf20Sopenharmony_ci#define UNCACHED_SHADOW_MASK 0
418c2ecf20Sopenharmony_ci#endif /* CONFIG_XILINX_UNCACHED_SHADOW */
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_civoid *arch_dma_set_uncached(void *ptr, size_t size)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	unsigned long addr = (unsigned long)ptr;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	addr |= UNCACHED_SHADOW_MASK;
488c2ecf20Sopenharmony_ci	if (addr > cpuinfo.dcache_base && addr < cpuinfo.dcache_high)
498c2ecf20Sopenharmony_ci		pr_warn("ERROR: Your cache coherent area is CACHED!!!\n");
508c2ecf20Sopenharmony_ci	return (void *)addr;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci#endif /* CONFIG_MMU */
53