arch/nios2: (!CONFIG_MMU) fix cache_invalidate_data()

arch/nios2/mm/cacheflush-nommu.c contains a static inline function
cache_invalidate_data() that is currently unused, but would lead to data
corruption if it were used.

When invalidating a region at least twice the size of the data cache, it
currently invalidates the whole data cache regardless of whether the
cache lines cover the desired area.  This is extremely likely to discard
dirty cache lines that cover addresses outside the desired area.
Instead it should write back all the dirty cache lines to avoid data
corruption.

When invalidating a region less than twice the size of the data cache,
it currently invalidates cache lines that fall within or partially cover
the desired area.  If cache lines that partially cover the start and/or
end of the desired area are dirty, some cached data outside the desired
area may be discarded before it is written back, leading to some data
corruption.  Instead, it should write back dirty cache lines that
partially cover the start and/or end of the desired area.  Only cache
lines fully contained within the desired area should be invalidated.

Rewrite cache_invalidate_data() to fix these problems.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
arch/nios2: (!CONFIG_MMU) fix cache_invalidate_inst()

arch/nios2/mm/cacheflush-nommu.c contains a static inline function
cache_invalidate_inst() called by cache_push() and icache_push().
Currently, when the length to be flushed exceeds the size of the
instruction cache, it rounds the start and end of the desired area
incorrectly, using the cache size instead of the cache line size, so it
is unlikely to flush all the desired area.  An extra cache line was
added to the end of the region to be flushed, possibly as a partial
workaround for the real bug.

Round the start and end of the desired area correctly and don't flush an
extra cache line at the end.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
arch/nios2: (!CONFIG_MMU) some DMA changes

Export dma_alloc_coherent() and dma_free_coherent().  Add
dma_sync_single_for_device(), dma_sync_single_range_for_cpu(),
dma_sync_single_range_for_device(), and dma_sync_sg_for_device().
Change dma_map_page() and dma_map_sg() to make use of dma_map_single().
The dma_sync_..._for_cpu() and dma_unmap_...() functions don't do
anything as the CPU should not have touched the memory since
invalidating the cache.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
arch/nios2: Replace __inline__ in cacheflush-nommu.c

Replace '__inline__' keyword with 'inline' as recommended by the
checkpatch.pl script.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
arch/nios2: (!CONFIG_MMU) Add function to invalidate virtual address range

Add an arch-specific function, nios2_clear_dcache_range() to invalidate
a range of virtual addresses.  This is useful for DMA mappings before
DMA-ing to memory, as there is no point writing back dirty cache lines
for memory that is about to be overwritten by a DMA transfer.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
arch/nios2: (!CONFIG_MMU) Avoid cache write-back for DMA_FROM_DEVICE

Change __dma_sync() to use nios2_clear_dcache_range() for the
DMA_FROM_DEVICE direction.  This invalidates cache lines without doing a
write-back of dirty cache lines first (except for the start and/or end
of the range if they are not aligned to a cache-line boundary).

This has only been implemented for !CONFIG_MMU so far, so for CONFIG_MMU
it still uses the regular flush_cache_range() instead.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>