cplbmgr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /*
  31. * WARNING
  32. *
  33. * This file is compiled with certain -ffixed-reg options. We have to
  34. * make sure not to call any functions here that could clobber these
  35. * registers.
  36. */
  37. int nr_dcplb_miss[NR_CPUS], nr_icplb_miss[NR_CPUS];
  38. int nr_dcplb_supv_miss[NR_CPUS], nr_icplb_supv_miss[NR_CPUS];
  39. int nr_cplb_flush[NR_CPUS], nr_dcplb_prot[NR_CPUS];
  40. #ifdef CONFIG_EXCPT_IRQ_SYSC_L1
  41. #define MGR_ATTR __attribute__((l1_text))
  42. #else
  43. #define MGR_ATTR
  44. #endif
  45. /*
  46. * We're in an exception handler. The normal cli nop nop workaround
  47. * isn't going to do very much, as the only thing that can interrupt
  48. * us is an NMI, and the cli isn't going to stop that.
  49. */
  50. #define NOWA_SSYNC __asm__ __volatile__ ("ssync;")
  51. /* Anomaly handlers provide SSYNCs, so avoid extra if anomaly is present */
  52. #if ANOMALY_05000125
  53. #define bfin_write_DMEM_CONTROL_SSYNC(v) bfin_write_DMEM_CONTROL(v)
  54. #define bfin_write_IMEM_CONTROL_SSYNC(v) bfin_write_IMEM_CONTROL(v)
  55. #else
  56. #define bfin_write_DMEM_CONTROL_SSYNC(v) \
  57. do { NOWA_SSYNC; bfin_write_DMEM_CONTROL(v); NOWA_SSYNC; } while (0)
  58. #define bfin_write_IMEM_CONTROL_SSYNC(v) \
  59. do { NOWA_SSYNC; bfin_write_IMEM_CONTROL(v); NOWA_SSYNC; } while (0)
  60. #endif
  61. static inline void write_dcplb_data(int cpu, int idx, unsigned long data,
  62. unsigned long addr)
  63. {
  64. unsigned long ctrl = bfin_read_DMEM_CONTROL();
  65. bfin_write_DMEM_CONTROL_SSYNC(ctrl & ~ENDCPLB);
  66. bfin_write32(DCPLB_DATA0 + idx * 4, data);
  67. bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
  68. bfin_write_DMEM_CONTROL_SSYNC(ctrl);
  69. #ifdef CONFIG_CPLB_INFO
  70. dcplb_tbl[cpu][idx].addr = addr;
  71. dcplb_tbl[cpu][idx].data = data;
  72. #endif
  73. }
  74. static inline void write_icplb_data(int cpu, int idx, unsigned long data,
  75. unsigned long addr)
  76. {
  77. unsigned long ctrl = bfin_read_IMEM_CONTROL();
  78. bfin_write_IMEM_CONTROL_SSYNC(ctrl & ~ENICPLB);
  79. bfin_write32(ICPLB_DATA0 + idx * 4, data);
  80. bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
  81. bfin_write_IMEM_CONTROL_SSYNC(ctrl);
  82. #ifdef CONFIG_CPLB_INFO
  83. icplb_tbl[cpu][idx].addr = addr;
  84. icplb_tbl[cpu][idx].data = data;
  85. #endif
  86. }
  87. /*
  88. * Given the contents of the status register, return the index of the
  89. * CPLB that caused the fault.
  90. */
  91. static inline int faulting_cplb_index(int status)
  92. {
  93. int signbits = __builtin_bfin_norm_fr1x32(status & 0xFFFF);
  94. return 30 - signbits;
  95. }
  96. /*
  97. * Given the contents of the status register and the DCPLB_DATA contents,
  98. * return true if a write access should be permitted.
  99. */
  100. static inline int write_permitted(int status, unsigned long data)
  101. {
  102. if (status & FAULT_USERSUPV)
  103. return !!(data & CPLB_SUPV_WR);
  104. else
  105. return !!(data & CPLB_USER_WR);
  106. }
  107. /* Counters to implement round-robin replacement. */
  108. static int icplb_rr_index[NR_CPUS] PDT_ATTR;
  109. static int dcplb_rr_index[NR_CPUS] PDT_ATTR;
  110. /*
  111. * Find an ICPLB entry to be evicted and return its index.
  112. */
  113. static int evict_one_icplb(int cpu)
  114. {
  115. int i = first_switched_icplb + icplb_rr_index[cpu];
  116. if (i >= MAX_CPLBS) {
  117. i -= MAX_CPLBS - first_switched_icplb;
  118. icplb_rr_index[cpu] -= MAX_CPLBS - first_switched_icplb;
  119. }
  120. icplb_rr_index[cpu]++;
  121. return i;
  122. }
  123. static int evict_one_dcplb(int cpu)
  124. {
  125. int i = first_switched_dcplb + dcplb_rr_index[cpu];
  126. if (i >= MAX_CPLBS) {
  127. i -= MAX_CPLBS - first_switched_dcplb;
  128. dcplb_rr_index[cpu] -= MAX_CPLBS - first_switched_dcplb;
  129. }
  130. dcplb_rr_index[cpu]++;
  131. return i;
  132. }
  133. MGR_ATTR static int icplb_miss(int cpu)
  134. {
  135. unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
  136. int status = bfin_read_ICPLB_STATUS();
  137. int idx;
  138. unsigned long i_data, base, addr1, eaddr;
  139. nr_icplb_miss[cpu]++;
  140. if (unlikely(status & FAULT_USERSUPV))
  141. nr_icplb_supv_miss[cpu]++;
  142. base = 0;
  143. idx = 0;
  144. do {
  145. eaddr = icplb_bounds[idx].eaddr;
  146. if (addr < eaddr)
  147. break;
  148. base = eaddr;
  149. } while (++idx < icplb_nr_bounds);
  150. if (unlikely(idx == icplb_nr_bounds))
  151. return CPLB_NO_ADDR_MATCH;
  152. i_data = icplb_bounds[idx].data;
  153. if (unlikely(i_data == 0))
  154. return CPLB_NO_ADDR_MATCH;
  155. addr1 = addr & ~(SIZE_4M - 1);
  156. addr &= ~(SIZE_1M - 1);
  157. i_data |= PAGE_SIZE_1MB;
  158. if (addr1 >= base && (addr1 + SIZE_4M) <= eaddr) {
  159. /*
  160. * This works because
  161. * (PAGE_SIZE_4MB & PAGE_SIZE_1MB) == PAGE_SIZE_1MB.
  162. */
  163. i_data |= PAGE_SIZE_4MB;
  164. addr = addr1;
  165. }
  166. /* Pick entry to evict */
  167. idx = evict_one_icplb(cpu);
  168. write_icplb_data(cpu, idx, i_data, addr);
  169. return CPLB_RELOADED;
  170. }
  171. MGR_ATTR static int dcplb_miss(int cpu)
  172. {
  173. unsigned long addr = bfin_read_DCPLB_FAULT_ADDR();
  174. int status = bfin_read_DCPLB_STATUS();
  175. int idx;
  176. unsigned long d_data, base, addr1, eaddr;
  177. nr_dcplb_miss[cpu]++;
  178. if (unlikely(status & FAULT_USERSUPV))
  179. nr_dcplb_supv_miss[cpu]++;
  180. base = 0;
  181. idx = 0;
  182. do {
  183. eaddr = dcplb_bounds[idx].eaddr;
  184. if (addr < eaddr)
  185. break;
  186. base = eaddr;
  187. } while (++idx < dcplb_nr_bounds);
  188. if (unlikely(idx == dcplb_nr_bounds))
  189. return CPLB_NO_ADDR_MATCH;
  190. d_data = dcplb_bounds[idx].data;
  191. if (unlikely(d_data == 0))
  192. return CPLB_NO_ADDR_MATCH;
  193. addr1 = addr & ~(SIZE_4M - 1);
  194. addr &= ~(SIZE_1M - 1);
  195. d_data |= PAGE_SIZE_1MB;
  196. if (addr1 >= base && (addr1 + SIZE_4M) <= eaddr) {
  197. /*
  198. * This works because
  199. * (PAGE_SIZE_4MB & PAGE_SIZE_1MB) == PAGE_SIZE_1MB.
  200. */
  201. d_data |= PAGE_SIZE_4MB;
  202. addr = addr1;
  203. }
  204. /* Pick entry to evict */
  205. idx = evict_one_dcplb(cpu);
  206. write_dcplb_data(cpu, idx, d_data, addr);
  207. return CPLB_RELOADED;
  208. }
  209. MGR_ATTR static noinline int dcplb_protection_fault(int cpu)
  210. {
  211. int status = bfin_read_DCPLB_STATUS();
  212. nr_dcplb_prot[cpu]++;
  213. if (likely(status & FAULT_RW)) {
  214. int idx = faulting_cplb_index(status);
  215. unsigned long regaddr = DCPLB_DATA0 + idx * 4;
  216. unsigned long data = bfin_read32(regaddr);
  217. /* Check if fault is to dirty a clean page */
  218. if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) &&
  219. write_permitted(status, data)) {
  220. dcplb_tbl[cpu][idx].data = data;
  221. bfin_write32(regaddr, data);
  222. return CPLB_RELOADED;
  223. }
  224. }
  225. return CPLB_PROT_VIOL;
  226. }
  227. MGR_ATTR int cplb_hdr(int seqstat, struct pt_regs *regs)
  228. {
  229. int cause = seqstat & 0x3f;
  230. unsigned int cpu = smp_processor_id();
  231. switch (cause) {
  232. case 0x2C:
  233. return icplb_miss(cpu);
  234. case 0x26:
  235. return dcplb_miss(cpu);
  236. default:
  237. if (unlikely(cause == 0x23))
  238. return dcplb_protection_fault(cpu);
  239. return CPLB_UNKNOWN_ERR;
  240. }
  241. }