cplbmgr.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/cacheflush.h>
  24. #include <asm/cplbinit.h>
  25. #include <asm/mmu_context.h>
  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 (bfin_addr_dcachable(addr)) {
  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 >= physical_mem_end) {
  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 if (addr >= BOOT_ROM_START && addr < BOOT_ROM_START + BOOT_ROM_LENGTH
  146. && (status & (FAULT_RW | FAULT_USERSUPV)) == FAULT_USERSUPV) {
  147. addr &= ~(1 * 1024 * 1024 - 1);
  148. d_data &= ~PAGE_SIZE_4KB;
  149. d_data |= PAGE_SIZE_1MB;
  150. } else
  151. return CPLB_PROT_VIOL;
  152. } else if (addr >= _ramend) {
  153. d_data |= CPLB_USER_RD | CPLB_USER_WR;
  154. } else {
  155. mask = current_rwx_mask;
  156. if (mask) {
  157. int page = addr >> PAGE_SHIFT;
  158. int offs = page >> 5;
  159. int bit = 1 << (page & 31);
  160. if (mask[offs] & bit)
  161. d_data |= CPLB_USER_RD;
  162. mask += page_mask_nelts;
  163. if (mask[offs] & bit)
  164. d_data |= CPLB_USER_WR;
  165. }
  166. }
  167. idx = evict_one_dcplb();
  168. addr &= PAGE_MASK;
  169. dcplb_tbl[idx].addr = addr;
  170. dcplb_tbl[idx].data = d_data;
  171. disable_dcplb();
  172. bfin_write32(DCPLB_DATA0 + idx * 4, d_data);
  173. bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
  174. enable_dcplb();
  175. return 0;
  176. }
  177. static noinline int icplb_miss(void)
  178. {
  179. unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
  180. int status = bfin_read_ICPLB_STATUS();
  181. int idx;
  182. unsigned long i_data;
  183. nr_icplb_miss++;
  184. /* If inside the uncached DMA region, fault. */
  185. if (addr >= _ramend - DMA_UNCACHED_REGION && addr < _ramend)
  186. return CPLB_PROT_VIOL;
  187. if (status & FAULT_USERSUPV)
  188. nr_icplb_supv_miss++;
  189. /*
  190. * First, try to find a CPLB that matches this address. If we
  191. * find one, then the fact that we're in the miss handler means
  192. * that the instruction crosses a page boundary.
  193. */
  194. for (idx = first_switched_icplb; idx < MAX_CPLBS; idx++) {
  195. if (icplb_tbl[idx].data & CPLB_VALID) {
  196. unsigned long this_addr = icplb_tbl[idx].addr;
  197. if (this_addr <= addr && this_addr + PAGE_SIZE > addr) {
  198. addr += PAGE_SIZE;
  199. break;
  200. }
  201. }
  202. }
  203. i_data = CPLB_VALID | CPLB_PORTPRIO | PAGE_SIZE_4KB;
  204. #ifdef CONFIG_BFIN_ICACHE
  205. /*
  206. * Normal RAM, and possibly the reserved memory area, are
  207. * cacheable.
  208. */
  209. if (addr < _ramend ||
  210. (addr < physical_mem_end && reserved_mem_icache_on))
  211. i_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
  212. #endif
  213. if (addr >= physical_mem_end) {
  214. if (addr >= BOOT_ROM_START && addr < BOOT_ROM_START + BOOT_ROM_LENGTH
  215. && (status & FAULT_USERSUPV)) {
  216. addr &= ~(1 * 1024 * 1024 - 1);
  217. i_data &= ~PAGE_SIZE_4KB;
  218. i_data |= PAGE_SIZE_1MB;
  219. } else
  220. return CPLB_PROT_VIOL;
  221. } else if (addr >= _ramend) {
  222. i_data |= CPLB_USER_RD;
  223. } else {
  224. /*
  225. * Two cases to distinguish - a supervisor access must
  226. * necessarily be for a module page; we grant it
  227. * unconditionally (could do better here in the future).
  228. * Otherwise, check the x bitmap of the current process.
  229. */
  230. if (!(status & FAULT_USERSUPV)) {
  231. unsigned long *mask = current_rwx_mask;
  232. if (mask) {
  233. int page = addr >> PAGE_SHIFT;
  234. int offs = page >> 5;
  235. int bit = 1 << (page & 31);
  236. mask += 2 * page_mask_nelts;
  237. if (mask[offs] & bit)
  238. i_data |= CPLB_USER_RD;
  239. }
  240. }
  241. }
  242. idx = evict_one_icplb();
  243. addr &= PAGE_MASK;
  244. icplb_tbl[idx].addr = addr;
  245. icplb_tbl[idx].data = i_data;
  246. disable_icplb();
  247. bfin_write32(ICPLB_DATA0 + idx * 4, i_data);
  248. bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
  249. enable_icplb();
  250. return 0;
  251. }
  252. static noinline int dcplb_protection_fault(void)
  253. {
  254. int status = bfin_read_DCPLB_STATUS();
  255. nr_dcplb_prot++;
  256. if (status & FAULT_RW) {
  257. int idx = faulting_cplb_index(status);
  258. unsigned long data = dcplb_tbl[idx].data;
  259. if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) &&
  260. write_permitted(status, data)) {
  261. data |= CPLB_DIRTY;
  262. dcplb_tbl[idx].data = data;
  263. bfin_write32(DCPLB_DATA0 + idx * 4, data);
  264. return 0;
  265. }
  266. }
  267. return CPLB_PROT_VIOL;
  268. }
  269. int cplb_hdr(int seqstat, struct pt_regs *regs)
  270. {
  271. int cause = seqstat & 0x3f;
  272. switch (cause) {
  273. case 0x23:
  274. return dcplb_protection_fault();
  275. case 0x2C:
  276. return icplb_miss();
  277. case 0x26:
  278. return dcplb_miss();
  279. default:
  280. return 1;
  281. }
  282. }
  283. void flush_switched_cplbs(void)
  284. {
  285. int i;
  286. unsigned long flags;
  287. nr_cplb_flush++;
  288. local_irq_save(flags);
  289. disable_icplb();
  290. for (i = first_switched_icplb; i < MAX_CPLBS; i++) {
  291. icplb_tbl[i].data = 0;
  292. bfin_write32(ICPLB_DATA0 + i * 4, 0);
  293. }
  294. enable_icplb();
  295. disable_dcplb();
  296. for (i = first_switched_dcplb; i < MAX_CPLBS; i++) {
  297. dcplb_tbl[i].data = 0;
  298. bfin_write32(DCPLB_DATA0 + i * 4, 0);
  299. }
  300. enable_dcplb();
  301. local_irq_restore(flags);
  302. }
  303. void set_mask_dcplbs(unsigned long *masks)
  304. {
  305. int i;
  306. unsigned long addr = (unsigned long)masks;
  307. unsigned long d_data;
  308. unsigned long flags;
  309. if (!masks) {
  310. current_rwx_mask = masks;
  311. return;
  312. }
  313. local_irq_save(flags);
  314. current_rwx_mask = masks;
  315. d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
  316. #ifdef CONFIG_BFIN_DCACHE
  317. d_data |= CPLB_L1_CHBL;
  318. #ifdef CONFIG_BFIN_WT
  319. d_data |= CPLB_L1_AOW | CPLB_WT;
  320. #endif
  321. #endif
  322. disable_dcplb();
  323. for (i = first_mask_dcplb; i < first_switched_dcplb; i++) {
  324. dcplb_tbl[i].addr = addr;
  325. dcplb_tbl[i].data = d_data;
  326. bfin_write32(DCPLB_DATA0 + i * 4, d_data);
  327. bfin_write32(DCPLB_ADDR0 + i * 4, addr);
  328. addr += PAGE_SIZE;
  329. }
  330. enable_dcplb();
  331. local_irq_restore(flags);
  332. }