c-sb1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
  3. * Copyright (C) 1997, 2001 Ralf Baechle (ralf@gnu.org)
  4. * Copyright (C) 2000, 2001, 2002, 2003 Broadcom Corporation
  5. * Copyright (C) 2004 Maciej W. Rozycki
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <asm/asm.h>
  23. #include <asm/bootinfo.h>
  24. #include <asm/cacheops.h>
  25. #include <asm/cpu.h>
  26. #include <asm/mipsregs.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/uaccess.h>
  29. extern void sb1_dma_init(void);
  30. /* These are probed at ld_mmu time */
  31. static unsigned long icache_size;
  32. static unsigned long dcache_size;
  33. static unsigned short icache_line_size;
  34. static unsigned short dcache_line_size;
  35. static unsigned int icache_index_mask;
  36. static unsigned int dcache_index_mask;
  37. static unsigned short icache_assoc;
  38. static unsigned short dcache_assoc;
  39. static unsigned short icache_sets;
  40. static unsigned short dcache_sets;
  41. static unsigned int icache_range_cutoff;
  42. static unsigned int dcache_range_cutoff;
  43. static inline void sb1_on_each_cpu(void (*func) (void *info), void *info,
  44. int retry, int wait)
  45. {
  46. preempt_disable();
  47. smp_call_function(func, info, retry, wait);
  48. func(info);
  49. preempt_enable();
  50. }
  51. /*
  52. * The dcache is fully coherent to the system, with one
  53. * big caveat: the instruction stream. In other words,
  54. * if we miss in the icache, and have dirty data in the
  55. * L1 dcache, then we'll go out to memory (or the L2) and
  56. * get the not-as-recent data.
  57. *
  58. * So the only time we have to flush the dcache is when
  59. * we're flushing the icache. Since the L2 is fully
  60. * coherent to everything, including I/O, we never have
  61. * to flush it
  62. */
  63. #define cache_set_op(op, addr) \
  64. __asm__ __volatile__( \
  65. " .set noreorder \n" \
  66. " .set mips64\n\t \n" \
  67. " cache %0, (0<<13)(%1) \n" \
  68. " cache %0, (1<<13)(%1) \n" \
  69. " cache %0, (2<<13)(%1) \n" \
  70. " cache %0, (3<<13)(%1) \n" \
  71. " .set mips0 \n" \
  72. " .set reorder" \
  73. : \
  74. : "i" (op), "r" (addr))
  75. #define sync() \
  76. __asm__ __volatile( \
  77. " .set mips64\n\t \n" \
  78. " sync \n" \
  79. " .set mips0")
  80. #define mispredict() \
  81. __asm__ __volatile__( \
  82. " bnezl $0, 1f \n" /* Force mispredict */ \
  83. "1: \n");
  84. /*
  85. * Writeback and invalidate the entire dcache
  86. */
  87. static inline void __sb1_writeback_inv_dcache_all(void)
  88. {
  89. unsigned long addr = 0;
  90. while (addr < dcache_line_size * dcache_sets) {
  91. cache_set_op(Index_Writeback_Inv_D, addr);
  92. addr += dcache_line_size;
  93. }
  94. }
  95. /*
  96. * Writeback and invalidate a range of the dcache. The addresses are
  97. * virtual, and since we're using index ops and bit 12 is part of both
  98. * the virtual frame and physical index, we have to clear both sets
  99. * (bit 12 set and cleared).
  100. */
  101. static inline void __sb1_writeback_inv_dcache_range(unsigned long start,
  102. unsigned long end)
  103. {
  104. unsigned long index;
  105. start &= ~(dcache_line_size - 1);
  106. end = (end + dcache_line_size - 1) & ~(dcache_line_size - 1);
  107. while (start != end) {
  108. index = start & dcache_index_mask;
  109. cache_set_op(Index_Writeback_Inv_D, index);
  110. cache_set_op(Index_Writeback_Inv_D, index ^ (1<<12));
  111. start += dcache_line_size;
  112. }
  113. sync();
  114. }
  115. /*
  116. * Writeback and invalidate a range of the dcache. With physical
  117. * addresseses, we don't have to worry about possible bit 12 aliasing.
  118. * XXXKW is it worth turning on KX and using hit ops with xkphys?
  119. */
  120. static inline void __sb1_writeback_inv_dcache_phys_range(unsigned long start,
  121. unsigned long end)
  122. {
  123. start &= ~(dcache_line_size - 1);
  124. end = (end + dcache_line_size - 1) & ~(dcache_line_size - 1);
  125. while (start != end) {
  126. cache_set_op(Index_Writeback_Inv_D, start & dcache_index_mask);
  127. start += dcache_line_size;
  128. }
  129. sync();
  130. }
  131. /*
  132. * Invalidate the entire icache
  133. */
  134. static inline void __sb1_flush_icache_all(void)
  135. {
  136. unsigned long addr = 0;
  137. while (addr < icache_line_size * icache_sets) {
  138. cache_set_op(Index_Invalidate_I, addr);
  139. addr += icache_line_size;
  140. }
  141. }
  142. /*
  143. * Invalidate a range of the icache. The addresses are virtual, and
  144. * the cache is virtually indexed and tagged. However, we don't
  145. * necessarily have the right ASID context, so use index ops instead
  146. * of hit ops.
  147. */
  148. static inline void __sb1_flush_icache_range(unsigned long start,
  149. unsigned long end)
  150. {
  151. start &= ~(icache_line_size - 1);
  152. end = (end + icache_line_size - 1) & ~(icache_line_size - 1);
  153. while (start != end) {
  154. cache_set_op(Index_Invalidate_I, start & icache_index_mask);
  155. start += icache_line_size;
  156. }
  157. mispredict();
  158. sync();
  159. }
  160. /*
  161. * Flush the icache for a given physical page. Need to writeback the
  162. * dcache first, then invalidate the icache. If the page isn't
  163. * executable, nothing is required.
  164. */
  165. static void local_sb1_flush_cache_page(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn)
  166. {
  167. int cpu = smp_processor_id();
  168. #ifndef CONFIG_SMP
  169. if (!(vma->vm_flags & VM_EXEC))
  170. return;
  171. #endif
  172. __sb1_writeback_inv_dcache_range(addr, addr + PAGE_SIZE);
  173. /*
  174. * Bumping the ASID is probably cheaper than the flush ...
  175. */
  176. if (vma->vm_mm == current->active_mm) {
  177. if (cpu_context(cpu, vma->vm_mm) != 0)
  178. drop_mmu_context(vma->vm_mm, cpu);
  179. } else
  180. __sb1_flush_icache_range(addr, addr + PAGE_SIZE);
  181. }
  182. #ifdef CONFIG_SMP
  183. struct flush_cache_page_args {
  184. struct vm_area_struct *vma;
  185. unsigned long addr;
  186. unsigned long pfn;
  187. };
  188. static void sb1_flush_cache_page_ipi(void *info)
  189. {
  190. struct flush_cache_page_args *args = info;
  191. local_sb1_flush_cache_page(args->vma, args->addr, args->pfn);
  192. }
  193. /* Dirty dcache could be on another CPU, so do the IPIs */
  194. static void sb1_flush_cache_page(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn)
  195. {
  196. struct flush_cache_page_args args;
  197. if (!(vma->vm_flags & VM_EXEC))
  198. return;
  199. addr &= PAGE_MASK;
  200. args.vma = vma;
  201. args.addr = addr;
  202. args.pfn = pfn;
  203. sb1_on_each_cpu(sb1_flush_cache_page_ipi, (void *) &args, 1, 1);
  204. }
  205. #else
  206. void sb1_flush_cache_page(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn)
  207. __attribute__((alias("local_sb1_flush_cache_page")));
  208. #endif
  209. /*
  210. * Invalidate all caches on this CPU
  211. */
  212. static void __attribute_used__ local_sb1___flush_cache_all(void)
  213. {
  214. __sb1_writeback_inv_dcache_all();
  215. __sb1_flush_icache_all();
  216. }
  217. #ifdef CONFIG_SMP
  218. void sb1___flush_cache_all_ipi(void *ignored)
  219. __attribute__((alias("local_sb1___flush_cache_all")));
  220. static void sb1___flush_cache_all(void)
  221. {
  222. sb1_on_each_cpu(sb1___flush_cache_all_ipi, 0, 1, 1);
  223. }
  224. #else
  225. void sb1___flush_cache_all(void)
  226. __attribute__((alias("local_sb1___flush_cache_all")));
  227. #endif
  228. /*
  229. * When flushing a range in the icache, we have to first writeback
  230. * the dcache for the same range, so new ifetches will see any
  231. * data that was dirty in the dcache.
  232. *
  233. * The start/end arguments are Kseg addresses (possibly mapped Kseg).
  234. */
  235. static void local_sb1_flush_icache_range(unsigned long start,
  236. unsigned long end)
  237. {
  238. /* Just wb-inv the whole dcache if the range is big enough */
  239. if ((end - start) > dcache_range_cutoff)
  240. __sb1_writeback_inv_dcache_all();
  241. else
  242. __sb1_writeback_inv_dcache_range(start, end);
  243. /* Just flush the whole icache if the range is big enough */
  244. if ((end - start) > icache_range_cutoff)
  245. __sb1_flush_icache_all();
  246. else
  247. __sb1_flush_icache_range(start, end);
  248. }
  249. #ifdef CONFIG_SMP
  250. struct flush_icache_range_args {
  251. unsigned long start;
  252. unsigned long end;
  253. };
  254. static void sb1_flush_icache_range_ipi(void *info)
  255. {
  256. struct flush_icache_range_args *args = info;
  257. local_sb1_flush_icache_range(args->start, args->end);
  258. }
  259. void sb1_flush_icache_range(unsigned long start, unsigned long end)
  260. {
  261. struct flush_icache_range_args args;
  262. args.start = start;
  263. args.end = end;
  264. sb1_on_each_cpu(sb1_flush_icache_range_ipi, &args, 1, 1);
  265. }
  266. #else
  267. void sb1_flush_icache_range(unsigned long start, unsigned long end)
  268. __attribute__((alias("local_sb1_flush_icache_range")));
  269. #endif
  270. /*
  271. * A signal trampoline must fit into a single cacheline.
  272. */
  273. static void local_sb1_flush_cache_sigtramp(unsigned long addr)
  274. {
  275. cache_set_op(Index_Writeback_Inv_D, addr & dcache_index_mask);
  276. cache_set_op(Index_Writeback_Inv_D, (addr ^ (1<<12)) & dcache_index_mask);
  277. cache_set_op(Index_Invalidate_I, addr & icache_index_mask);
  278. mispredict();
  279. }
  280. #ifdef CONFIG_SMP
  281. static void sb1_flush_cache_sigtramp_ipi(void *info)
  282. {
  283. unsigned long iaddr = (unsigned long) info;
  284. local_sb1_flush_cache_sigtramp(iaddr);
  285. }
  286. static void sb1_flush_cache_sigtramp(unsigned long addr)
  287. {
  288. sb1_on_each_cpu(sb1_flush_cache_sigtramp_ipi, (void *) addr, 1, 1);
  289. }
  290. #else
  291. void sb1_flush_cache_sigtramp(unsigned long addr)
  292. __attribute__((alias("local_sb1_flush_cache_sigtramp")));
  293. #endif
  294. /*
  295. * Anything that just flushes dcache state can be ignored, as we're always
  296. * coherent in dcache space. This is just a dummy function that all the
  297. * nop'ed routines point to
  298. */
  299. static void sb1_nop(void)
  300. {
  301. }
  302. /*
  303. * Cache set values (from the mips64 spec)
  304. * 0 - 64
  305. * 1 - 128
  306. * 2 - 256
  307. * 3 - 512
  308. * 4 - 1024
  309. * 5 - 2048
  310. * 6 - 4096
  311. * 7 - Reserved
  312. */
  313. static unsigned int decode_cache_sets(unsigned int config_field)
  314. {
  315. if (config_field == 7) {
  316. /* JDCXXX - Find a graceful way to abort. */
  317. return 0;
  318. }
  319. return (1<<(config_field + 6));
  320. }
  321. /*
  322. * Cache line size values (from the mips64 spec)
  323. * 0 - No cache present.
  324. * 1 - 4 bytes
  325. * 2 - 8 bytes
  326. * 3 - 16 bytes
  327. * 4 - 32 bytes
  328. * 5 - 64 bytes
  329. * 6 - 128 bytes
  330. * 7 - Reserved
  331. */
  332. static unsigned int decode_cache_line_size(unsigned int config_field)
  333. {
  334. if (config_field == 0) {
  335. return 0;
  336. } else if (config_field == 7) {
  337. /* JDCXXX - Find a graceful way to abort. */
  338. return 0;
  339. }
  340. return (1<<(config_field + 1));
  341. }
  342. /*
  343. * Relevant bits of the config1 register format (from the MIPS32/MIPS64 specs)
  344. *
  345. * 24:22 Icache sets per way
  346. * 21:19 Icache line size
  347. * 18:16 Icache Associativity
  348. * 15:13 Dcache sets per way
  349. * 12:10 Dcache line size
  350. * 9:7 Dcache Associativity
  351. */
  352. static char *way_string[] = {
  353. "direct mapped", "2-way", "3-way", "4-way",
  354. "5-way", "6-way", "7-way", "8-way",
  355. };
  356. static __init void probe_cache_sizes(void)
  357. {
  358. u32 config1;
  359. config1 = read_c0_config1();
  360. icache_line_size = decode_cache_line_size((config1 >> 19) & 0x7);
  361. dcache_line_size = decode_cache_line_size((config1 >> 10) & 0x7);
  362. icache_sets = decode_cache_sets((config1 >> 22) & 0x7);
  363. dcache_sets = decode_cache_sets((config1 >> 13) & 0x7);
  364. icache_assoc = ((config1 >> 16) & 0x7) + 1;
  365. dcache_assoc = ((config1 >> 7) & 0x7) + 1;
  366. icache_size = icache_line_size * icache_sets * icache_assoc;
  367. dcache_size = dcache_line_size * dcache_sets * dcache_assoc;
  368. /* Need to remove non-index bits for index ops */
  369. icache_index_mask = (icache_sets - 1) * icache_line_size;
  370. dcache_index_mask = (dcache_sets - 1) * dcache_line_size;
  371. /*
  372. * These are for choosing range (index ops) versus all.
  373. * icache flushes all ways for each set, so drop icache_assoc.
  374. * dcache flushes all ways and each setting of bit 12 for each
  375. * index, so drop dcache_assoc and halve the dcache_sets.
  376. */
  377. icache_range_cutoff = icache_sets * icache_line_size;
  378. dcache_range_cutoff = (dcache_sets / 2) * icache_line_size;
  379. printk("Primary instruction cache %ldkB, %s, linesize %d bytes.\n",
  380. icache_size >> 10, way_string[icache_assoc - 1],
  381. icache_line_size);
  382. printk("Primary data cache %ldkB, %s, linesize %d bytes.\n",
  383. dcache_size >> 10, way_string[dcache_assoc - 1],
  384. dcache_line_size);
  385. }
  386. /*
  387. * This is called from cache.c. We have to set up all the
  388. * memory management function pointers, as well as initialize
  389. * the caches and tlbs
  390. */
  391. void sb1_cache_init(void)
  392. {
  393. extern char except_vec2_sb1;
  394. /* Special cache error handler for SB1 */
  395. set_uncached_handler (0x100, &except_vec2_sb1, 0x80);
  396. probe_cache_sizes();
  397. #ifdef CONFIG_SIBYTE_DMA_PAGEOPS
  398. sb1_dma_init();
  399. #endif
  400. /*
  401. * None of these are needed for the SB1 - the Dcache is
  402. * physically indexed and tagged, so no virtual aliasing can
  403. * occur
  404. */
  405. flush_cache_range = (void *) sb1_nop;
  406. flush_cache_mm = (void (*)(struct mm_struct *))sb1_nop;
  407. flush_cache_all = sb1_nop;
  408. /* These routines are for Icache coherence with the Dcache */
  409. flush_icache_range = sb1_flush_icache_range;
  410. flush_icache_all = __sb1_flush_icache_all; /* local only */
  411. /* This implies an Icache flush too, so can't be nop'ed */
  412. flush_cache_page = sb1_flush_cache_page;
  413. flush_cache_sigtramp = sb1_flush_cache_sigtramp;
  414. local_flush_data_cache_page = (void *) sb1_nop;
  415. flush_data_cache_page = (void *) sb1_nop;
  416. /* Full flush */
  417. __flush_cache_all = sb1___flush_cache_all;
  418. change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
  419. /*
  420. * This is the only way to force the update of K0 to complete
  421. * before subsequent instruction fetch.
  422. */
  423. __asm__ __volatile__(
  424. ".set push \n"
  425. " .set noat \n"
  426. " .set noreorder \n"
  427. " .set mips3 \n"
  428. " " STR(PTR_LA) " $1, 1f \n"
  429. " " STR(MTC0) " $1, $14 \n"
  430. " eret \n"
  431. "1: .set pop"
  432. :
  433. :
  434. : "memory");
  435. flush_cache_all();
  436. }