cplbmgr.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
  131. #ifdef CONFIG_BFIN_DCACHE
  132. if (addr < _ramend - DMA_UNCACHED_REGION) {
  133. d_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
  134. #ifdef CONFIG_BFIN_WT
  135. d_data |= CPLB_L1_AOW | CPLB_WT;
  136. #endif
  137. }
  138. #endif
  139. if (addr >= _ramend) {
  140. if (addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE
  141. && (status & FAULT_USERSUPV)) {
  142. addr &= ~0x3fffff;
  143. d_data &= ~PAGE_SIZE_4KB;
  144. d_data |= PAGE_SIZE_4MB;
  145. } else
  146. return CPLB_PROT_VIOL;
  147. } else {
  148. mask = current_rwx_mask;
  149. if (mask) {
  150. int page = addr >> PAGE_SHIFT;
  151. int offs = page >> 5;
  152. int bit = 1 << (page & 31);
  153. if (mask[offs] & bit)
  154. d_data |= CPLB_USER_RD;
  155. mask += page_mask_nelts;
  156. if (mask[offs] & bit)
  157. d_data |= CPLB_USER_WR;
  158. }
  159. }
  160. idx = evict_one_dcplb();
  161. addr &= PAGE_MASK;
  162. dcplb_tbl[idx].addr = addr;
  163. dcplb_tbl[idx].data = d_data;
  164. disable_dcplb();
  165. bfin_write32(DCPLB_DATA0 + idx * 4, d_data);
  166. bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
  167. enable_dcplb();
  168. return 0;
  169. }
  170. static noinline int icplb_miss(void)
  171. {
  172. unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
  173. int status = bfin_read_ICPLB_STATUS();
  174. int idx;
  175. unsigned long i_data;
  176. nr_icplb_miss++;
  177. if (status & FAULT_USERSUPV)
  178. nr_icplb_supv_miss++;
  179. if (addr >= _ramend)
  180. return CPLB_PROT_VIOL;
  181. /*
  182. * First, try to find a CPLB that matches this address. If we
  183. * find one, then the fact that we're in the miss handler means
  184. * that the instruction crosses a page boundary.
  185. */
  186. for (idx = first_switched_icplb; idx < MAX_CPLBS; idx++) {
  187. if (icplb_tbl[idx].data & CPLB_VALID) {
  188. unsigned long this_addr = icplb_tbl[idx].addr;
  189. if (this_addr <= addr && this_addr + PAGE_SIZE > addr) {
  190. addr += PAGE_SIZE;
  191. break;
  192. }
  193. }
  194. }
  195. i_data = CPLB_VALID | CPLB_PORTPRIO | PAGE_SIZE_4KB;
  196. #ifdef CONFIG_BFIN_ICACHE
  197. i_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
  198. #endif
  199. /*
  200. * Two cases to distinguish - a supervisor access must necessarily
  201. * be for a module page; we grant it unconditionally (could do better
  202. * here in the future). Otherwise, check the x bitmap of the current
  203. * process.
  204. */
  205. if (!(status & FAULT_USERSUPV)) {
  206. unsigned long *mask = current_rwx_mask;
  207. if (mask) {
  208. int page = addr >> PAGE_SHIFT;
  209. int offs = page >> 5;
  210. int bit = 1 << (page & 31);
  211. mask += 2 * page_mask_nelts;
  212. if (mask[offs] & bit)
  213. i_data |= CPLB_USER_RD;
  214. }
  215. }
  216. idx = evict_one_icplb();
  217. addr &= PAGE_MASK;
  218. icplb_tbl[idx].addr = addr;
  219. icplb_tbl[idx].data = i_data;
  220. disable_icplb();
  221. bfin_write32(ICPLB_DATA0 + idx * 4, i_data);
  222. bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
  223. enable_icplb();
  224. return 0;
  225. }
  226. static noinline int dcplb_protection_fault(void)
  227. {
  228. unsigned long addr = bfin_read_DCPLB_FAULT_ADDR();
  229. int status = bfin_read_DCPLB_STATUS();
  230. nr_dcplb_prot++;
  231. if (status & FAULT_RW) {
  232. int idx = faulting_cplb_index(status);
  233. unsigned long data = dcplb_tbl[idx].data;
  234. if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) &&
  235. write_permitted(status, data)) {
  236. data |= CPLB_DIRTY;
  237. dcplb_tbl[idx].data = data;
  238. bfin_write32(DCPLB_DATA0 + idx * 4, data);
  239. return 0;
  240. }
  241. }
  242. return CPLB_PROT_VIOL;
  243. }
  244. int cplb_hdr(int seqstat, struct pt_regs *regs)
  245. {
  246. int cause = seqstat & 0x3f;
  247. switch (cause) {
  248. case 0x23:
  249. return dcplb_protection_fault();
  250. case 0x2C:
  251. return icplb_miss();
  252. case 0x26:
  253. return dcplb_miss();
  254. default:
  255. return 1;
  256. }
  257. }
  258. void flush_switched_cplbs(void)
  259. {
  260. int i;
  261. nr_cplb_flush++;
  262. disable_icplb();
  263. for (i = first_switched_icplb; i < MAX_CPLBS; i++) {
  264. icplb_tbl[i].data = 0;
  265. bfin_write32(ICPLB_DATA0 + i * 4, 0);
  266. }
  267. enable_icplb();
  268. disable_dcplb();
  269. for (i = first_switched_dcplb; i < MAX_CPLBS; i++) {
  270. dcplb_tbl[i].data = 0;
  271. bfin_write32(DCPLB_DATA0 + i * 4, 0);
  272. }
  273. enable_dcplb();
  274. }
  275. void set_mask_dcplbs(unsigned long *masks)
  276. {
  277. int i;
  278. unsigned long addr = (unsigned long)masks;
  279. unsigned long d_data;
  280. current_rwx_mask = masks;
  281. if (!masks)
  282. return;
  283. d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
  284. #ifdef CONFIG_BFIN_DCACHE
  285. d_data |= CPLB_L1_CHBL;
  286. #ifdef CONFIG_BFIN_WT
  287. d_data |= CPLB_L1_AOW | CPLB_WT;
  288. #endif
  289. #endif
  290. disable_dcplb();
  291. for (i = first_mask_dcplb; i < first_switched_dcplb; i++) {
  292. dcplb_tbl[i].addr = addr;
  293. dcplb_tbl[i].data = d_data;
  294. bfin_write32(DCPLB_DATA0 + i * 4, d_data);
  295. bfin_write32(DCPLB_ADDR0 + i * 4, addr);
  296. addr += PAGE_SIZE;
  297. }
  298. enable_dcplb();
  299. }
  300. #endif