cplbmgr.c 9.7 KB

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