cplbmgr.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Blackfin CPLB exception handling.
  3. * Copyright 2004-2007 Analog Devices Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see the file COPYING, or write
  17. * to the Free Software Foundation, Inc.,
  18. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/module.h>
  21. #include <linux/mm.h>
  22. #include <asm/blackfin.h>
  23. #include <asm/cplbinit.h>
  24. #include <asm/mmu_context.h>
  25. #ifdef CONFIG_BFIN_ICACHE
  26. #define FAULT_RW (1 << 16)
  27. #define FAULT_USERSUPV (1 << 17)
  28. int page_mask_nelts;
  29. int page_mask_order;
  30. unsigned long *current_rwx_mask;
  31. int nr_dcplb_miss, nr_icplb_miss, nr_icplb_supv_miss, nr_dcplb_prot;
  32. int nr_cplb_flush;
  33. static inline void disable_dcplb(void)
  34. {
  35. unsigned long ctrl;
  36. SSYNC();
  37. ctrl = bfin_read_DMEM_CONTROL();
  38. ctrl &= ~ENDCPLB;
  39. bfin_write_DMEM_CONTROL(ctrl);
  40. SSYNC();
  41. }
  42. static inline void enable_dcplb(void)
  43. {
  44. unsigned long ctrl;
  45. SSYNC();
  46. ctrl = bfin_read_DMEM_CONTROL();
  47. ctrl |= ENDCPLB;
  48. bfin_write_DMEM_CONTROL(ctrl);
  49. SSYNC();
  50. }
  51. static inline void disable_icplb(void)
  52. {
  53. unsigned long ctrl;
  54. SSYNC();
  55. ctrl = bfin_read_IMEM_CONTROL();
  56. ctrl &= ~ENICPLB;
  57. bfin_write_IMEM_CONTROL(ctrl);
  58. SSYNC();
  59. }
  60. static inline void enable_icplb(void)
  61. {
  62. unsigned long ctrl;
  63. SSYNC();
  64. ctrl = bfin_read_IMEM_CONTROL();
  65. ctrl |= ENICPLB;
  66. bfin_write_IMEM_CONTROL(ctrl);
  67. SSYNC();
  68. }
  69. /*
  70. * Given the contents of the status register, return the index of the
  71. * CPLB that caused the fault.
  72. */
  73. static inline int faulting_cplb_index(int status)
  74. {
  75. int signbits = __builtin_bfin_norm_fr1x32(status & 0xFFFF);
  76. return 30 - signbits;
  77. }
  78. /*
  79. * Given the contents of the status register and the DCPLB_DATA contents,
  80. * return true if a write access should be permitted.
  81. */
  82. static inline int write_permitted(int status, unsigned long data)
  83. {
  84. if (status & FAULT_USERSUPV)
  85. return !!(data & CPLB_SUPV_WR);
  86. else
  87. return !!(data & CPLB_USER_WR);
  88. }
  89. /* Counters to implement round-robin replacement. */
  90. static int icplb_rr_index, dcplb_rr_index;
  91. /*
  92. * Find an ICPLB entry to be evicted and return its index.
  93. */
  94. static int evict_one_icplb(void)
  95. {
  96. int i;
  97. for (i = first_switched_icplb; i < MAX_CPLBS; i++)
  98. if ((icplb_tbl[i].data & CPLB_VALID) == 0)
  99. return i;
  100. i = first_switched_icplb + icplb_rr_index;
  101. if (i >= MAX_CPLBS) {
  102. i -= MAX_CPLBS - first_switched_icplb;
  103. icplb_rr_index -= MAX_CPLBS - first_switched_icplb;
  104. }
  105. icplb_rr_index++;
  106. return i;
  107. }
  108. static int evict_one_dcplb(void)
  109. {
  110. int i;
  111. for (i = first_switched_dcplb; i < MAX_CPLBS; i++)
  112. if ((dcplb_tbl[i].data & CPLB_VALID) == 0)
  113. return i;
  114. i = first_switched_dcplb + dcplb_rr_index;
  115. if (i >= MAX_CPLBS) {
  116. i -= MAX_CPLBS - first_switched_dcplb;
  117. dcplb_rr_index -= MAX_CPLBS - first_switched_dcplb;
  118. }
  119. dcplb_rr_index++;
  120. return i;
  121. }
  122. static noinline int dcplb_miss(void)
  123. {
  124. unsigned long addr = bfin_read_DCPLB_FAULT_ADDR();
  125. int status = bfin_read_DCPLB_STATUS();
  126. unsigned long *mask;
  127. int idx;
  128. unsigned long d_data;
  129. nr_dcplb_miss++;
  130. if (addr >= _ramend)
  131. return CPLB_PROT_VIOL;
  132. d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
  133. #ifdef CONFIG_BFIN_DCACHE
  134. d_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
  135. #ifdef CONFIG_BLKFIN_WT
  136. d_data |= CPLB_L1_AOW | CPLB_WT;
  137. #endif
  138. #endif
  139. mask = current_rwx_mask;
  140. if (mask) {
  141. int page = addr >> PAGE_SHIFT;
  142. int offs = page >> 5;
  143. int bit = 1 << (page & 31);
  144. if (mask[offs] & bit)
  145. d_data |= CPLB_USER_RD;
  146. mask += page_mask_nelts;
  147. if (mask[offs] & bit)
  148. d_data |= CPLB_USER_WR;
  149. }
  150. idx = evict_one_dcplb();
  151. addr &= PAGE_MASK;
  152. dcplb_tbl[idx].addr = addr;
  153. dcplb_tbl[idx].data = d_data;
  154. disable_dcplb();
  155. bfin_write32(DCPLB_DATA0 + idx * 4, d_data);
  156. bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
  157. enable_dcplb();
  158. return 0;
  159. }
  160. static noinline int icplb_miss(void)
  161. {
  162. unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
  163. int status = bfin_read_ICPLB_STATUS();
  164. int idx;
  165. unsigned long i_data;
  166. nr_icplb_miss++;
  167. if (status & FAULT_USERSUPV)
  168. nr_icplb_supv_miss++;
  169. if (addr >= _ramend)
  170. return CPLB_PROT_VIOL;
  171. /*
  172. * First, try to find a CPLB that matches this address. If we
  173. * find one, then the fact that we're in the miss handler means
  174. * that the instruction crosses a page boundary.
  175. */
  176. for (idx = first_switched_icplb; idx < MAX_CPLBS; idx++) {
  177. if (icplb_tbl[idx].data & CPLB_VALID) {
  178. unsigned long this_addr = icplb_tbl[idx].addr;
  179. if (this_addr <= addr && this_addr + PAGE_SIZE > addr) {
  180. addr += PAGE_SIZE;
  181. break;
  182. }
  183. }
  184. }
  185. i_data = CPLB_VALID | CPLB_PORTPRIO | PAGE_SIZE_4KB;
  186. #ifdef CONFIG_BFIN_ICACHE
  187. i_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
  188. #endif
  189. /*
  190. * Two cases to distinguish - a supervisor access must necessarily
  191. * be for a module page; we grant it unconditionally (could do better
  192. * here in the future). Otherwise, check the x bitmap of the current
  193. * process.
  194. */
  195. if (!(status & FAULT_USERSUPV)) {
  196. unsigned long *mask = current_rwx_mask;
  197. if (mask) {
  198. int page = addr >> PAGE_SHIFT;
  199. int offs = page >> 5;
  200. int bit = 1 << (page & 31);
  201. mask += 2 * page_mask_nelts;
  202. if (mask[offs] & bit)
  203. i_data |= CPLB_USER_RD;
  204. }
  205. }
  206. idx = evict_one_icplb();
  207. addr &= PAGE_MASK;
  208. icplb_tbl[idx].addr = addr;
  209. icplb_tbl[idx].data = i_data;
  210. disable_icplb();
  211. bfin_write32(ICPLB_DATA0 + idx * 4, i_data);
  212. bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
  213. enable_icplb();
  214. return 0;
  215. }
  216. static noinline int dcplb_protection_fault(void)
  217. {
  218. unsigned long addr = bfin_read_DCPLB_FAULT_ADDR();
  219. int status = bfin_read_DCPLB_STATUS();
  220. nr_dcplb_prot++;
  221. if (status & FAULT_RW) {
  222. int idx = faulting_cplb_index(status);
  223. unsigned long data = dcplb_tbl[idx].data;
  224. if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) &&
  225. write_permitted(status, data)) {
  226. data |= CPLB_DIRTY;
  227. dcplb_tbl[idx].data = data;
  228. bfin_write32(DCPLB_DATA0 + idx * 4, data);
  229. return 0;
  230. }
  231. }
  232. return CPLB_PROT_VIOL;
  233. }
  234. int cplb_hdr(int seqstat, struct pt_regs *regs)
  235. {
  236. int cause = seqstat & 0x3f;
  237. switch (cause) {
  238. case 0x23:
  239. return dcplb_protection_fault();
  240. case 0x2C:
  241. return icplb_miss();
  242. case 0x26:
  243. return dcplb_miss();
  244. default:
  245. return 1;
  246. panic_cplb_error(seqstat, regs);
  247. }
  248. }
  249. void flush_switched_cplbs(void)
  250. {
  251. int i;
  252. nr_cplb_flush++;
  253. disable_icplb();
  254. for (i = first_switched_icplb; i < MAX_CPLBS; i++) {
  255. icplb_tbl[i].data = 0;
  256. bfin_write32(ICPLB_DATA0 + i * 4, 0);
  257. }
  258. enable_icplb();
  259. disable_dcplb();
  260. for (i = first_mask_dcplb; i < MAX_CPLBS; i++) {
  261. dcplb_tbl[i].data = 0;
  262. bfin_write32(DCPLB_DATA0 + i * 4, 0);
  263. }
  264. enable_dcplb();
  265. }
  266. void set_mask_dcplbs(unsigned long *masks)
  267. {
  268. int i;
  269. unsigned long addr = (unsigned long)masks;
  270. unsigned long d_data;
  271. current_rwx_mask = masks;
  272. if (!masks)
  273. return;
  274. d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
  275. #ifdef CONFIG_BFIN_DCACHE
  276. d_data |= CPLB_L1_CHBL;
  277. #ifdef CONFIG_BLKFIN_WT
  278. d_data |= CPLB_L1_AOW | CPLB_WT;
  279. #endif
  280. #endif
  281. disable_dcplb();
  282. for (i = first_mask_dcplb; i < first_switched_dcplb; i++) {
  283. dcplb_tbl[i].addr = addr;
  284. dcplb_tbl[i].data = d_data;
  285. bfin_write32(DCPLB_DATA0 + i * 4, d_data);
  286. bfin_write32(DCPLB_ADDR0 + i * 4, addr);
  287. addr += PAGE_SIZE;
  288. }
  289. enable_dcplb();
  290. }
  291. #endif