icache.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  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.
  7. *
  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
  12. * more details.
  13. *
  14. */
  15. /**
  16. * @file
  17. *
  18. * Support for invalidating bytes in the instruction cache.
  19. */
  20. #ifndef __ARCH_ICACHE_H__
  21. #define __ARCH_ICACHE_H__
  22. #include <arch/chip.h>
  23. /**
  24. * Invalidate the instruction cache for the given range of memory.
  25. *
  26. * @param addr The start of memory to be invalidated.
  27. * @param size The number of bytes to be invalidated.
  28. * @param page_size The system's page size, e.g. getpagesize() in userspace.
  29. * This value must be a power of two no larger than the page containing
  30. * the code to be invalidated. If the value is smaller than the actual page
  31. * size, this function will still work, but may run slower than necessary.
  32. */
  33. static __inline void
  34. invalidate_icache(const void* addr, unsigned long size,
  35. unsigned long page_size)
  36. {
  37. const unsigned long cache_way_size =
  38. CHIP_L1I_CACHE_SIZE() / CHIP_L1I_ASSOC();
  39. unsigned long max_useful_size;
  40. const char* start, *end;
  41. long num_passes;
  42. if (__builtin_expect(size == 0, 0))
  43. return;
  44. #ifdef __tilegx__
  45. /* Limit the number of bytes visited to avoid redundant iterations. */
  46. max_useful_size = (page_size < cache_way_size) ? page_size : cache_way_size;
  47. /* No PA aliasing is possible, so one pass always suffices. */
  48. num_passes = 1;
  49. #else
  50. /* Limit the number of bytes visited to avoid redundant iterations. */
  51. max_useful_size = cache_way_size;
  52. /*
  53. * Compute how many passes we need (we'll treat 0 as if it were 1).
  54. * This works because we know the page size is a power of two.
  55. */
  56. num_passes = cache_way_size >> __builtin_ctzl(page_size);
  57. #endif
  58. if (__builtin_expect(size > max_useful_size, 0))
  59. size = max_useful_size;
  60. /* Locate the first and last bytes to be invalidated. */
  61. start = (const char *)((unsigned long)addr & -CHIP_L1I_LINE_SIZE());
  62. end = (const char*)addr + size - 1;
  63. __insn_mf();
  64. do
  65. {
  66. const char* p;
  67. for (p = start; p <= end; p += CHIP_L1I_LINE_SIZE())
  68. __insn_icoh(p);
  69. start += page_size;
  70. end += page_size;
  71. }
  72. while (--num_passes > 0);
  73. __insn_drain();
  74. }
  75. #endif /* __ARCH_ICACHE_H__ */