cplbmgr.c 9.3 KB

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