cplbmgr.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * File: arch/blackfin/kernel/cplb-nompu-c/cplbmgr.c
  3. * Based on: arch/blackfin/kernel/cplb-mpu/cplbmgr.c
  4. * Author: Michael McTernan <mmcternan@airvana.com>
  5. *
  6. * Created: 01Nov2008
  7. * Description: CPLB miss handler.
  8. *
  9. * Modified:
  10. * Copyright 2008 Airvana Inc.
  11. * Copyright 2004-2007 Analog Devices Inc.
  12. *
  13. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. */
  25. #include <linux/kernel.h>
  26. #include <asm/blackfin.h>
  27. #include <asm/cplbinit.h>
  28. #include <asm/cplb.h>
  29. #include <asm/mmu_context.h>
  30. #include <asm/traps.h>
  31. /*
  32. * WARNING
  33. *
  34. * This file is compiled with certain -ffixed-reg options. We have to
  35. * make sure not to call any functions here that could clobber these
  36. * registers.
  37. */
  38. int nr_dcplb_miss[NR_CPUS], nr_icplb_miss[NR_CPUS];
  39. int nr_dcplb_supv_miss[NR_CPUS], nr_icplb_supv_miss[NR_CPUS];
  40. int nr_cplb_flush[NR_CPUS], nr_dcplb_prot[NR_CPUS];
  41. #ifdef CONFIG_EXCPT_IRQ_SYSC_L1
  42. #define MGR_ATTR __attribute__((l1_text))
  43. #else
  44. #define MGR_ATTR
  45. #endif
  46. /*
  47. * We're in an exception handler. The normal cli nop nop workaround
  48. * isn't going to do very much, as the only thing that can interrupt
  49. * us is an NMI, and the cli isn't going to stop that.
  50. */
  51. #define NOWA_SSYNC __asm__ __volatile__ ("ssync;")
  52. /* Anomaly handlers provide SSYNCs, so avoid extra if anomaly is present */
  53. #if ANOMALY_05000125
  54. #define bfin_write_DMEM_CONTROL_SSYNC(v) bfin_write_DMEM_CONTROL(v)
  55. #define bfin_write_IMEM_CONTROL_SSYNC(v) bfin_write_IMEM_CONTROL(v)
  56. #else
  57. #define bfin_write_DMEM_CONTROL_SSYNC(v) \
  58. do { NOWA_SSYNC; bfin_write_DMEM_CONTROL(v); NOWA_SSYNC; } while (0)
  59. #define bfin_write_IMEM_CONTROL_SSYNC(v) \
  60. do { NOWA_SSYNC; bfin_write_IMEM_CONTROL(v); NOWA_SSYNC; } while (0)
  61. #endif
  62. static inline void write_dcplb_data(int cpu, int idx, unsigned long data,
  63. unsigned long addr)
  64. {
  65. unsigned long ctrl = bfin_read_DMEM_CONTROL();
  66. bfin_write_DMEM_CONTROL_SSYNC(ctrl & ~ENDCPLB);
  67. bfin_write32(DCPLB_DATA0 + idx * 4, data);
  68. bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
  69. bfin_write_DMEM_CONTROL_SSYNC(ctrl);
  70. #ifdef CONFIG_CPLB_INFO
  71. dcplb_tbl[cpu][idx].addr = addr;
  72. dcplb_tbl[cpu][idx].data = data;
  73. #endif
  74. }
  75. static inline void write_icplb_data(int cpu, int idx, unsigned long data,
  76. unsigned long addr)
  77. {
  78. unsigned long ctrl = bfin_read_IMEM_CONTROL();
  79. bfin_write_IMEM_CONTROL_SSYNC(ctrl & ~ENICPLB);
  80. bfin_write32(ICPLB_DATA0 + idx * 4, data);
  81. bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
  82. bfin_write_IMEM_CONTROL_SSYNC(ctrl);
  83. #ifdef CONFIG_CPLB_INFO
  84. icplb_tbl[cpu][idx].addr = addr;
  85. icplb_tbl[cpu][idx].data = data;
  86. #endif
  87. }
  88. /* Counters to implement round-robin replacement. */
  89. static int icplb_rr_index[NR_CPUS] PDT_ATTR;
  90. static int dcplb_rr_index[NR_CPUS] PDT_ATTR;
  91. /*
  92. * Find an ICPLB entry to be evicted and return its index.
  93. */
  94. static int evict_one_icplb(int cpu)
  95. {
  96. int i = first_switched_icplb + icplb_rr_index[cpu];
  97. if (i >= MAX_CPLBS) {
  98. i -= MAX_CPLBS - first_switched_icplb;
  99. icplb_rr_index[cpu] -= MAX_CPLBS - first_switched_icplb;
  100. }
  101. icplb_rr_index[cpu]++;
  102. return i;
  103. }
  104. static int evict_one_dcplb(int cpu)
  105. {
  106. int i = first_switched_dcplb + dcplb_rr_index[cpu];
  107. if (i >= MAX_CPLBS) {
  108. i -= MAX_CPLBS - first_switched_dcplb;
  109. dcplb_rr_index[cpu] -= MAX_CPLBS - first_switched_dcplb;
  110. }
  111. dcplb_rr_index[cpu]++;
  112. return i;
  113. }
  114. MGR_ATTR static int icplb_miss(int cpu)
  115. {
  116. unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
  117. int status = bfin_read_ICPLB_STATUS();
  118. int idx;
  119. unsigned long i_data, base, addr1, eaddr;
  120. nr_icplb_miss[cpu]++;
  121. if (unlikely(status & FAULT_USERSUPV))
  122. nr_icplb_supv_miss[cpu]++;
  123. base = 0;
  124. idx = 0;
  125. do {
  126. eaddr = icplb_bounds[idx].eaddr;
  127. if (addr < eaddr)
  128. break;
  129. base = eaddr;
  130. } while (++idx < icplb_nr_bounds);
  131. if (unlikely(idx == icplb_nr_bounds))
  132. return CPLB_NO_ADDR_MATCH;
  133. i_data = icplb_bounds[idx].data;
  134. if (unlikely(i_data == 0))
  135. return CPLB_NO_ADDR_MATCH;
  136. addr1 = addr & ~(SIZE_4M - 1);
  137. addr &= ~(SIZE_1M - 1);
  138. i_data |= PAGE_SIZE_1MB;
  139. if (addr1 >= base && (addr1 + SIZE_4M) <= eaddr) {
  140. /*
  141. * This works because
  142. * (PAGE_SIZE_4MB & PAGE_SIZE_1MB) == PAGE_SIZE_1MB.
  143. */
  144. i_data |= PAGE_SIZE_4MB;
  145. addr = addr1;
  146. }
  147. /* Pick entry to evict */
  148. idx = evict_one_icplb(cpu);
  149. write_icplb_data(cpu, idx, i_data, addr);
  150. return CPLB_RELOADED;
  151. }
  152. MGR_ATTR static int dcplb_miss(int cpu)
  153. {
  154. unsigned long addr = bfin_read_DCPLB_FAULT_ADDR();
  155. int status = bfin_read_DCPLB_STATUS();
  156. int idx;
  157. unsigned long d_data, base, addr1, eaddr;
  158. nr_dcplb_miss[cpu]++;
  159. if (unlikely(status & FAULT_USERSUPV))
  160. nr_dcplb_supv_miss[cpu]++;
  161. base = 0;
  162. idx = 0;
  163. do {
  164. eaddr = dcplb_bounds[idx].eaddr;
  165. if (addr < eaddr)
  166. break;
  167. base = eaddr;
  168. } while (++idx < dcplb_nr_bounds);
  169. if (unlikely(idx == dcplb_nr_bounds))
  170. return CPLB_NO_ADDR_MATCH;
  171. d_data = dcplb_bounds[idx].data;
  172. if (unlikely(d_data == 0))
  173. return CPLB_NO_ADDR_MATCH;
  174. addr1 = addr & ~(SIZE_4M - 1);
  175. addr &= ~(SIZE_1M - 1);
  176. d_data |= PAGE_SIZE_1MB;
  177. if (addr1 >= base && (addr1 + SIZE_4M) <= eaddr) {
  178. /*
  179. * This works because
  180. * (PAGE_SIZE_4MB & PAGE_SIZE_1MB) == PAGE_SIZE_1MB.
  181. */
  182. d_data |= PAGE_SIZE_4MB;
  183. addr = addr1;
  184. }
  185. /* Pick entry to evict */
  186. idx = evict_one_dcplb(cpu);
  187. write_dcplb_data(cpu, idx, d_data, addr);
  188. return CPLB_RELOADED;
  189. }
  190. MGR_ATTR int cplb_hdr(int seqstat, struct pt_regs *regs)
  191. {
  192. int cause = seqstat & 0x3f;
  193. unsigned int cpu = smp_processor_id();
  194. switch (cause) {
  195. case VEC_CPLB_I_M:
  196. return icplb_miss(cpu);
  197. case VEC_CPLB_M:
  198. return dcplb_miss(cpu);
  199. default:
  200. return CPLB_UNKNOWN_ERR;
  201. }
  202. }