2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
19 * Support for invalidating bytes in the instruction
22 #ifndef __ARCH_ICACHE_H__
23 #define __ARCH_ICACHE_H__
25 #include <arch/chip.h>
29 * Invalidate the instruction cache for the given range of memory.
31 * @param addr The start of memory to be invalidated.
32 * @param size The number of bytes to be invalidated.
33 * @param page_size The system's page size, typically the PAGE_SIZE constant
34 * in sys/page.h. This value must be a power of two no larger
35 * than the page containing the code to be invalidated. If the value
36 * is smaller than the actual page size, this function will still
37 * work, but may run slower than necessary.
40 invalidate_icache(const void* addr
, unsigned long size
,
41 unsigned long page_size
)
43 const unsigned long cache_way_size
=
44 CHIP_L1I_CACHE_SIZE() / CHIP_L1I_ASSOC();
45 unsigned long max_useful_size
;
46 const char* start
, *end
;
49 if (__builtin_expect(size
== 0, 0))
53 /* Limit the number of bytes visited to avoid redundant iterations. */
54 max_useful_size
= (page_size
< cache_way_size
) ? page_size
: cache_way_size
;
56 /* No PA aliasing is possible, so one pass always suffices. */
59 /* Limit the number of bytes visited to avoid redundant iterations. */
60 max_useful_size
= cache_way_size
;
63 * Compute how many passes we need (we'll treat 0 as if it were 1).
64 * This works because we know the page size is a power of two.
66 num_passes
= cache_way_size
>> __builtin_ctzl(page_size
);
69 if (__builtin_expect(size
> max_useful_size
, 0))
70 size
= max_useful_size
;
72 /* Locate the first and last bytes to be invalidated. */
73 start
= (const char *)((unsigned long)addr
& -CHIP_L1I_LINE_SIZE());
74 end
= (const char*)addr
+ size
- 1;
82 for (p
= start
; p
<= end
; p
+= CHIP_L1I_LINE_SIZE())
88 while (--num_passes
> 0);
94 #endif /* __ARCH_ICACHE_H__ */