blob: 05085b62363fb8821e53d8763106cfd7222896e6 [file] [log] [blame]
Raghu Gandham405b8022012-07-25 18:16:42 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28#include <unistd.h>
29#include <sys/cachectl.h>
30
31#ifdef DEBUG
32#include <logd.h>
33#define XLOG(...) \
34 __libc_android_log_print(ANDROID_LOG_DEBUG,"libc-cacheflush",__VA_ARGS__)
35#endif
36
37/*
38 * Linux historically defines a cacheflush(3) routine for MIPS
39 * with this signature:
40 * int cacheflush(char *addr, int nbytes, int cache);
41 *
42 * Android defines an alternate cacheflush routine which exposes the
43 * ARM system call interface:
44 * int cacheflush (long start, long end, long flags)
45 *
46 * This is an attempt to maintain compatibility between the historical MIPS
47 * usage for software previously ported to MIPS and Android specific
48 * uses of cacheflush()
49 *
50 * Use the gcc __clear_cache builtin if possible. This will generate inline synci
51 * instructions if available or call _flush_cache(start, len, BCACHE) directly
52 */
53
54#if defined (__GNUC__)
55#define GCC_VERSION ((__GNUC__*10000) + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
56#endif
57
58/* This is the Android signature */
59int cacheflush (long start, long end, long flags)
60{
61 if (end < start) {
62 /*
63 * It looks like this is really MIPS style cacheflush call
64 * start => addr
65 * end => nbytes
66 */
67#ifdef DEBUG
68 static int warned = 0;
69 if (!warned) {
70 XLOG("called with (start,len) instead of (start,end)");
71 warned = 1;
72 }
73#endif
74 end += start;
75 }
76
77#if !defined(ARCH_MIPS_USE_FLUSHCACHE_SYSCALL) && \
78 defined(GCC_VERSION) && (GCC_VERSION >= 40300)
79
80#if (__mips_isa_rev >= 2) && (GCC_VERSION < 40403)
81 /*
82 * Modify "start" and "end" to avoid GCC 4.3.0-4.4.2 bug in
83 * mips_expand_synci_loop that may execute synci one more time.
84 * "start" points to the first byte of the cache line.
85 * "end" points to the last byte of the line before the last cache line.
86 * Because size is always a multiple of 4, this is safe to set
87 * "end" to the last byte.
88 */
89 {
90 int lineSize;
91 asm("rdhwr %0, $1" : "=r" (lineSize));
92 start = start & (-lineSize);
93 end = (end & (-lineSize)) - 1;
94 }
95#endif
96 __builtin___clear_cache((char *)start, (char *)end);
97#else
98 _flush_cache((char *)start, end-start, BCACHE);
99#endif
100 return 0;
101}