c-sb1.c 14 KB

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