cplbmgr.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. #define FAULT_RW (1 << 16)
  26. #define FAULT_USERSUPV (1 << 17)
  27. int page_mask_nelts;
  28. int page_mask_order;
  29. unsigned long *current_rwx_mask;
  30. int nr_dcplb_miss, nr_icplb_miss, nr_icplb_supv_miss, nr_dcplb_prot;
  31. int nr_cplb_flush;
  32. static inline void disable_dcplb(void)
  33. {
  34. unsigned long ctrl;
  35. SSYNC();
  36. ctrl = bfin_read_DMEM_CONTROL();
  37. ctrl &= ~ENDCPLB;
  38. bfin_write_DMEM_CONTROL(ctrl);
  39. SSYNC();
  40. }
  41. static inline void enable_dcplb(void)
  42. {
  43. unsigned long ctrl;
  44. SSYNC();
  45. ctrl = bfin_read_DMEM_CONTROL();
  46. ctrl |= ENDCPLB;
  47. bfin_write_DMEM_CONTROL(ctrl);
  48. SSYNC();
  49. }
  50. static inline void disable_icplb(void)
  51. {
  52. unsigned long ctrl;
  53. SSYNC();
  54. ctrl = bfin_read_IMEM_CONTROL();
  55. ctrl &= ~ENICPLB;
  56. bfin_write_IMEM_CONTROL(ctrl);
  57. SSYNC();
  58. }
  59. static inline void enable_icplb(void)
  60. {
  61. unsigned long ctrl;
  62. SSYNC();
  63. ctrl = bfin_read_IMEM_CONTROL();
  64. ctrl |= ENICPLB;
  65. bfin_write_IMEM_CONTROL(ctrl);
  66. SSYNC();
  67. }
  68. /*
  69. * Given the contents of the status register, return the index of the
  70. * CPLB that caused the fault.
  71. */
  72. static inline int faulting_cplb_index(int status)
  73. {
  74. int signbits = __builtin_bfin_norm_fr1x32(status & 0xFFFF);
  75. return 30 - signbits;
  76. }
  77. /*
  78. * Given the contents of the status register and the DCPLB_DATA contents,
  79. * return true if a write access should be permitted.
  80. */
  81. static inline int write_permitted(int status, unsigned long data)
  82. {
  83. if (status & FAULT_USERSUPV)
  84. return !!(data & CPLB_SUPV_WR);
  85. else
  86. return !!(data & CPLB_USER_WR);
  87. }
  88. /* Counters to implement round-robin replacement. */
  89. static int icplb_rr_index, dcplb_rr_index;
  90. /*
  91. * Find an ICPLB entry to be evicted and return its index.
  92. */
  93. static int evict_one_icplb(void)
  94. {
  95. int i;
  96. for (i = first_switched_icplb; i < MAX_CPLBS; i++)
  97. if ((icplb_tbl[i].data & CPLB_VALID) == 0)
  98. return i;
  99. i = first_switched_icplb + icplb_rr_index;
  100. if (i >= MAX_CPLBS) {
  101. i -= MAX_CPLBS - first_switched_icplb;
  102. icplb_rr_index -= MAX_CPLBS - first_switched_icplb;
  103. }
  104. icplb_rr_index++;
  105. return i;
  106. }
  107. static int evict_one_dcplb(void)
  108. {
  109. int i;
  110. for (i = first_switched_dcplb; i < MAX_CPLBS; i++)
  111. if ((dcplb_tbl[i].data & CPLB_VALID) == 0)
  112. return i;
  113. i = first_switched_dcplb + dcplb_rr_index;
  114. if (i >= MAX_CPLBS) {
  115. i -= MAX_CPLBS - first_switched_dcplb;
  116. dcplb_rr_index -= MAX_CPLBS - first_switched_dcplb;
  117. }
  118. dcplb_rr_index++;
  119. return i;
  120. }
  121. static noinline int dcplb_miss(void)
  122. {
  123. unsigned long addr = bfin_read_DCPLB_FAULT_ADDR();
  124. int status = bfin_read_DCPLB_STATUS();
  125. unsigned long *mask;
  126. int idx;
  127. unsigned long d_data;
  128. nr_dcplb_miss++;
  129. d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
  130. #ifdef CONFIG_BFIN_DCACHE
  131. if (addr < _ramend - DMA_UNCACHED_REGION ||
  132. (reserved_mem_dcache_on && addr >= _ramend &&
  133. addr < physical_mem_end)) {
  134. d_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
  135. #ifdef CONFIG_BFIN_WT
  136. d_data |= CPLB_L1_AOW | CPLB_WT;
  137. #endif
  138. }
  139. #endif
  140. if (addr >= physical_mem_end) {
  141. if (addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE
  142. && (status & FAULT_USERSUPV)) {
  143. addr &= ~0x3fffff;
  144. d_data &= ~PAGE_SIZE_4KB;
  145. d_data |= PAGE_SIZE_4MB;
  146. } else if (addr >= BOOT_ROM_START && addr < BOOT_ROM_START + BOOT_ROM_LENGTH
  147. && (status & (FAULT_RW | FAULT_USERSUPV)) == FAULT_USERSUPV) {
  148. addr &= ~(1 * 1024 * 1024 - 1);
  149. d_data &= ~PAGE_SIZE_4KB;
  150. d_data |= PAGE_SIZE_1MB;
  151. } else
  152. return CPLB_PROT_VIOL;
  153. } else if (addr >= _ramend) {
  154. d_data |= CPLB_USER_RD | CPLB_USER_WR;
  155. } else {
  156. mask = current_rwx_mask;
  157. if (mask) {
  158. int page = addr >> PAGE_SHIFT;
  159. int offs = page >> 5;
  160. int bit = 1 << (page & 31);
  161. if (mask[offs] & bit)
  162. d_data |= CPLB_USER_RD;
  163. mask += page_mask_nelts;
  164. if (mask[offs] & bit)
  165. d_data |= CPLB_USER_WR;
  166. }
  167. }
  168. idx = evict_one_dcplb();
  169. addr &= PAGE_MASK;
  170. dcplb_tbl[idx].addr = addr;
  171. dcplb_tbl[idx].data = d_data;
  172. disable_dcplb();
  173. bfin_write32(DCPLB_DATA0 + idx * 4, d_data);
  174. bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
  175. enable_dcplb();
  176. return 0;
  177. }
  178. static noinline int icplb_miss(void)
  179. {
  180. unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
  181. int status = bfin_read_ICPLB_STATUS();
  182. int idx;
  183. unsigned long i_data;
  184. nr_icplb_miss++;
  185. /* If inside the uncached DMA region, fault. */
  186. if (addr >= _ramend - DMA_UNCACHED_REGION && addr < _ramend)
  187. return CPLB_PROT_VIOL;
  188. if (status & FAULT_USERSUPV)
  189. nr_icplb_supv_miss++;
  190. /*
  191. * First, try to find a CPLB that matches this address. If we
  192. * find one, then the fact that we're in the miss handler means
  193. * that the instruction crosses a page boundary.
  194. */
  195. for (idx = first_switched_icplb; idx < MAX_CPLBS; idx++) {
  196. if (icplb_tbl[idx].data & CPLB_VALID) {
  197. unsigned long this_addr = icplb_tbl[idx].addr;
  198. if (this_addr <= addr && this_addr + PAGE_SIZE > addr) {
  199. addr += PAGE_SIZE;
  200. break;
  201. }
  202. }
  203. }
  204. i_data = CPLB_VALID | CPLB_PORTPRIO | PAGE_SIZE_4KB;
  205. #ifdef CONFIG_BFIN_ICACHE
  206. /*
  207. * Normal RAM, and possibly the reserved memory area, are
  208. * cacheable.
  209. */
  210. if (addr < _ramend ||
  211. (addr < physical_mem_end && reserved_mem_icache_on))
  212. i_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
  213. #endif
  214. if (addr >= physical_mem_end) {
  215. if (addr >= BOOT_ROM_START && addr < BOOT_ROM_START + BOOT_ROM_LENGTH
  216. && (status & FAULT_USERSUPV)) {
  217. addr &= ~(1 * 1024 * 1024 - 1);
  218. i_data &= ~PAGE_SIZE_4KB;
  219. i_data |= PAGE_SIZE_1MB;
  220. } else
  221. return CPLB_PROT_VIOL;
  222. } else if (addr >= _ramend) {
  223. i_data |= CPLB_USER_RD;
  224. } else {
  225. /*
  226. * Two cases to distinguish - a supervisor access must
  227. * necessarily be for a module page; we grant it
  228. * unconditionally (could do better here in the future).
  229. * Otherwise, check the x bitmap of the current process.
  230. */
  231. if (!(status & FAULT_USERSUPV)) {
  232. unsigned long *mask = current_rwx_mask;
  233. if (mask) {
  234. int page = addr >> PAGE_SHIFT;
  235. int offs = page >> 5;
  236. int bit = 1 << (page & 31);
  237. mask += 2 * page_mask_nelts;
  238. if (mask[offs] & bit)
  239. i_data |= CPLB_USER_RD;
  240. }
  241. }
  242. }
  243. idx = evict_one_icplb();
  244. addr &= PAGE_MASK;
  245. icplb_tbl[idx].addr = addr;
  246. icplb_tbl[idx].data = i_data;
  247. disable_icplb();
  248. bfin_write32(ICPLB_DATA0 + idx * 4, i_data);
  249. bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
  250. enable_icplb();
  251. return 0;
  252. }
  253. static noinline int dcplb_protection_fault(void)
  254. {
  255. int status = bfin_read_DCPLB_STATUS();
  256. nr_dcplb_prot++;
  257. if (status & FAULT_RW) {
  258. int idx = faulting_cplb_index(status);
  259. unsigned long data = dcplb_tbl[idx].data;
  260. if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) &&
  261. write_permitted(status, data)) {
  262. data |= CPLB_DIRTY;
  263. dcplb_tbl[idx].data = data;
  264. bfin_write32(DCPLB_DATA0 + idx * 4, data);
  265. return 0;
  266. }
  267. }
  268. return CPLB_PROT_VIOL;
  269. }
  270. int cplb_hdr(int seqstat, struct pt_regs *regs)
  271. {
  272. int cause = seqstat & 0x3f;
  273. switch (cause) {
  274. case 0x23:
  275. return dcplb_protection_fault();
  276. case 0x2C:
  277. return icplb_miss();
  278. case 0x26:
  279. return dcplb_miss();
  280. default:
  281. return 1;
  282. }
  283. }
  284. void flush_switched_cplbs(void)
  285. {
  286. int i;
  287. nr_cplb_flush++;
  288. disable_icplb();
  289. for (i = first_switched_icplb; i < MAX_CPLBS; i++) {
  290. icplb_tbl[i].data = 0;
  291. bfin_write32(ICPLB_DATA0 + i * 4, 0);
  292. }
  293. enable_icplb();
  294. disable_dcplb();
  295. for (i = first_switched_dcplb; i < MAX_CPLBS; i++) {
  296. dcplb_tbl[i].data = 0;
  297. bfin_write32(DCPLB_DATA0 + i * 4, 0);
  298. }
  299. enable_dcplb();
  300. }
  301. void set_mask_dcplbs(unsigned long *masks)
  302. {
  303. int i;
  304. unsigned long addr = (unsigned long)masks;
  305. unsigned long d_data;
  306. current_rwx_mask = masks;
  307. if (!masks)
  308. return;
  309. d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
  310. #ifdef CONFIG_BFIN_DCACHE
  311. d_data |= CPLB_L1_CHBL;
  312. #ifdef CONFIG_BFIN_WT
  313. d_data |= CPLB_L1_AOW | CPLB_WT;
  314. #endif
  315. #endif
  316. disable_dcplb();
  317. for (i = first_mask_dcplb; i < first_switched_dcplb; i++) {
  318. dcplb_tbl[i].addr = addr;
  319. dcplb_tbl[i].data = d_data;
  320. bfin_write32(DCPLB_DATA0 + i * 4, d_data);
  321. bfin_write32(DCPLB_ADDR0 + i * 4, addr);
  322. addr += PAGE_SIZE;
  323. }
  324. enable_dcplb();
  325. }