cacheflush.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include <asm/page.h>
  15. #include <asm/cacheflush.h>
  16. #include <arch/icache.h>
  17. void __flush_icache_range(unsigned long start, unsigned long end)
  18. {
  19. invalidate_icache((const void *)start, end - start, PAGE_SIZE);
  20. }
  21. /* Force a load instruction to issue. */
  22. static inline void force_load(char *p)
  23. {
  24. *(volatile char *)p;
  25. }
  26. /*
  27. * Flush and invalidate a VA range that is homed remotely on a single
  28. * core (if "!hfh") or homed via hash-for-home (if "hfh"), waiting
  29. * until the memory controller holds the flushed values.
  30. */
  31. void finv_buffer_remote(void *buffer, size_t size, int hfh)
  32. {
  33. char *p, *base;
  34. size_t step_size, load_count;
  35. const unsigned long STRIPE_WIDTH = 8192;
  36. /*
  37. * Flush and invalidate the buffer out of the local L1/L2
  38. * and request the home cache to flush and invalidate as well.
  39. */
  40. __finv_buffer(buffer, size);
  41. /*
  42. * Wait for the home cache to acknowledge that it has processed
  43. * all the flush-and-invalidate requests. This does not mean
  44. * that the flushed data has reached the memory controller yet,
  45. * but it does mean the home cache is processing the flushes.
  46. */
  47. __insn_mf();
  48. /*
  49. * Issue a load to the last cache line, which can't complete
  50. * until all the previously-issued flushes to the same memory
  51. * controller have also completed. If we weren't striping
  52. * memory, that one load would be sufficient, but since we may
  53. * be, we also need to back up to the last load issued to
  54. * another memory controller, which would be the point where
  55. * we crossed an 8KB boundary (the granularity of striping
  56. * across memory controllers). Keep backing up and doing this
  57. * until we are before the beginning of the buffer, or have
  58. * hit all the controllers.
  59. *
  60. * If we are flushing a hash-for-home buffer, it's even worse.
  61. * Each line may be homed on a different tile, and each tile
  62. * may have up to four lines that are on different
  63. * controllers. So as we walk backwards, we have to touch
  64. * enough cache lines to satisfy these constraints. In
  65. * practice this ends up being close enough to "load from
  66. * every cache line on a full memory stripe on each
  67. * controller" that we simply do that, to simplify the logic.
  68. *
  69. * FIXME: See bug 9535 for some issues with this code.
  70. */
  71. if (hfh) {
  72. step_size = L2_CACHE_BYTES;
  73. load_count = (STRIPE_WIDTH / L2_CACHE_BYTES) *
  74. (1 << CHIP_LOG_NUM_MSHIMS());
  75. } else {
  76. step_size = STRIPE_WIDTH;
  77. load_count = (1 << CHIP_LOG_NUM_MSHIMS());
  78. }
  79. /* Load the last byte of the buffer. */
  80. p = (char *)buffer + size - 1;
  81. force_load(p);
  82. /* Bump down to the end of the previous stripe or cache line. */
  83. p -= step_size;
  84. p = (char *)((unsigned long)p | (step_size - 1));
  85. /* Figure out how far back we need to go. */
  86. base = p - (step_size * (load_count - 2));
  87. if ((long)base < (long)buffer)
  88. base = buffer;
  89. /*
  90. * Fire all the loads we need. The MAF only has eight entries
  91. * so we can have at most eight outstanding loads, so we
  92. * unroll by that amount.
  93. */
  94. #pragma unroll 8
  95. for (; p >= base; p -= step_size)
  96. force_load(p);
  97. /*
  98. * Repeat, but with inv's instead of loads, to get rid of the
  99. * data we just loaded into our own cache and the old home L3.
  100. * No need to unroll since inv's don't target a register.
  101. */
  102. p = (char *)buffer + size - 1;
  103. __insn_inv(p);
  104. p -= step_size;
  105. p = (char *)((unsigned long)p | (step_size - 1));
  106. for (; p >= base; p -= step_size)
  107. __insn_inv(p);
  108. /* Wait for the load+inv's (and thus finvs) to have completed. */
  109. __insn_mf();
  110. }