c-sb1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. static void local_sb1_flush_cache_data_page(unsigned long addr)
  225. {
  226. __sb1_writeback_inv_dcache_range(addr, addr + PAGE_SIZE);
  227. }
  228. void sb1_flush_cache_data_page(unsigned long)
  229. __attribute__((alias("local_sb1_flush_cache_data_page")));
  230. #endif
  231. /*
  232. * Invalidate all caches on this CPU
  233. */
  234. static void __used local_sb1___flush_cache_all(void)
  235. {
  236. __sb1_writeback_inv_dcache_all();
  237. __sb1_flush_icache_all();
  238. }
  239. #ifdef CONFIG_SMP
  240. void sb1___flush_cache_all_ipi(void *ignored)
  241. __attribute__((alias("local_sb1___flush_cache_all")));
  242. static void sb1___flush_cache_all(void)
  243. {
  244. sb1_on_each_cpu(sb1___flush_cache_all_ipi, 0, 1, 1);
  245. }
  246. #else
  247. void sb1___flush_cache_all(void)
  248. __attribute__((alias("local_sb1___flush_cache_all")));
  249. #endif
  250. /*
  251. * When flushing a range in the icache, we have to first writeback
  252. * the dcache for the same range, so new ifetches will see any
  253. * data that was dirty in the dcache.
  254. *
  255. * The start/end arguments are Kseg addresses (possibly mapped Kseg).
  256. */
  257. static void local_sb1_flush_icache_range(unsigned long start,
  258. unsigned long end)
  259. {
  260. /* Just wb-inv the whole dcache if the range is big enough */
  261. if ((end - start) > dcache_range_cutoff)
  262. __sb1_writeback_inv_dcache_all();
  263. else
  264. __sb1_writeback_inv_dcache_range(start, end);
  265. /* Just flush the whole icache if the range is big enough */
  266. if ((end - start) > icache_range_cutoff)
  267. __sb1_flush_icache_all();
  268. else
  269. __sb1_flush_icache_range(start, end);
  270. }
  271. #ifdef CONFIG_SMP
  272. struct flush_icache_range_args {
  273. unsigned long start;
  274. unsigned long end;
  275. };
  276. static void sb1_flush_icache_range_ipi(void *info)
  277. {
  278. struct flush_icache_range_args *args = info;
  279. local_sb1_flush_icache_range(args->start, args->end);
  280. }
  281. void sb1_flush_icache_range(unsigned long start, unsigned long end)
  282. {
  283. struct flush_icache_range_args args;
  284. args.start = start;
  285. args.end = end;
  286. sb1_on_each_cpu(sb1_flush_icache_range_ipi, &args, 1, 1);
  287. }
  288. #else
  289. void sb1_flush_icache_range(unsigned long start, unsigned long end)
  290. __attribute__((alias("local_sb1_flush_icache_range")));
  291. #endif
  292. /*
  293. * A signal trampoline must fit into a single cacheline.
  294. */
  295. static void local_sb1_flush_cache_sigtramp(unsigned long addr)
  296. {
  297. cache_set_op(Index_Writeback_Inv_D, addr & dcache_index_mask);
  298. cache_set_op(Index_Writeback_Inv_D, (addr ^ (1<<12)) & dcache_index_mask);
  299. cache_set_op(Index_Invalidate_I, addr & icache_index_mask);
  300. mispredict();
  301. }
  302. #ifdef CONFIG_SMP
  303. static void sb1_flush_cache_sigtramp_ipi(void *info)
  304. {
  305. unsigned long iaddr = (unsigned long) info;
  306. local_sb1_flush_cache_sigtramp(iaddr);
  307. }
  308. static void sb1_flush_cache_sigtramp(unsigned long addr)
  309. {
  310. sb1_on_each_cpu(sb1_flush_cache_sigtramp_ipi, (void *) addr, 1, 1);
  311. }
  312. #else
  313. void sb1_flush_cache_sigtramp(unsigned long addr)
  314. __attribute__((alias("local_sb1_flush_cache_sigtramp")));
  315. #endif
  316. /*
  317. * Anything that just flushes dcache state can be ignored, as we're always
  318. * coherent in dcache space. This is just a dummy function that all the
  319. * nop'ed routines point to
  320. */
  321. static void sb1_nop(void)
  322. {
  323. }
  324. /*
  325. * Cache set values (from the mips64 spec)
  326. * 0 - 64
  327. * 1 - 128
  328. * 2 - 256
  329. * 3 - 512
  330. * 4 - 1024
  331. * 5 - 2048
  332. * 6 - 4096
  333. * 7 - Reserved
  334. */
  335. static unsigned int decode_cache_sets(unsigned int config_field)
  336. {
  337. if (config_field == 7) {
  338. /* JDCXXX - Find a graceful way to abort. */
  339. return 0;
  340. }
  341. return (1<<(config_field + 6));
  342. }
  343. /*
  344. * Cache line size values (from the mips64 spec)
  345. * 0 - No cache present.
  346. * 1 - 4 bytes
  347. * 2 - 8 bytes
  348. * 3 - 16 bytes
  349. * 4 - 32 bytes
  350. * 5 - 64 bytes
  351. * 6 - 128 bytes
  352. * 7 - Reserved
  353. */
  354. static unsigned int decode_cache_line_size(unsigned int config_field)
  355. {
  356. if (config_field == 0) {
  357. return 0;
  358. } else if (config_field == 7) {
  359. /* JDCXXX - Find a graceful way to abort. */
  360. return 0;
  361. }
  362. return (1<<(config_field + 1));
  363. }
  364. /*
  365. * Relevant bits of the config1 register format (from the MIPS32/MIPS64 specs)
  366. *
  367. * 24:22 Icache sets per way
  368. * 21:19 Icache line size
  369. * 18:16 Icache Associativity
  370. * 15:13 Dcache sets per way
  371. * 12:10 Dcache line size
  372. * 9:7 Dcache Associativity
  373. */
  374. static char *way_string[] = {
  375. "direct mapped", "2-way", "3-way", "4-way",
  376. "5-way", "6-way", "7-way", "8-way",
  377. };
  378. static __init void probe_cache_sizes(void)
  379. {
  380. u32 config1;
  381. config1 = read_c0_config1();
  382. icache_line_size = decode_cache_line_size((config1 >> 19) & 0x7);
  383. dcache_line_size = decode_cache_line_size((config1 >> 10) & 0x7);
  384. icache_sets = decode_cache_sets((config1 >> 22) & 0x7);
  385. dcache_sets = decode_cache_sets((config1 >> 13) & 0x7);
  386. icache_assoc = ((config1 >> 16) & 0x7) + 1;
  387. dcache_assoc = ((config1 >> 7) & 0x7) + 1;
  388. icache_size = icache_line_size * icache_sets * icache_assoc;
  389. dcache_size = dcache_line_size * dcache_sets * dcache_assoc;
  390. /* Need to remove non-index bits for index ops */
  391. icache_index_mask = (icache_sets - 1) * icache_line_size;
  392. dcache_index_mask = (dcache_sets - 1) * dcache_line_size;
  393. /*
  394. * These are for choosing range (index ops) versus all.
  395. * icache flushes all ways for each set, so drop icache_assoc.
  396. * dcache flushes all ways and each setting of bit 12 for each
  397. * index, so drop dcache_assoc and halve the dcache_sets.
  398. */
  399. icache_range_cutoff = icache_sets * icache_line_size;
  400. dcache_range_cutoff = (dcache_sets / 2) * icache_line_size;
  401. printk("Primary instruction cache %ldkB, %s, linesize %d bytes.\n",
  402. icache_size >> 10, way_string[icache_assoc - 1],
  403. icache_line_size);
  404. printk("Primary data cache %ldkB, %s, linesize %d bytes.\n",
  405. dcache_size >> 10, way_string[dcache_assoc - 1],
  406. dcache_line_size);
  407. }
  408. /*
  409. * This is called from cache.c. We have to set up all the
  410. * memory management function pointers, as well as initialize
  411. * the caches and tlbs
  412. */
  413. void __init sb1_cache_init(void)
  414. {
  415. extern char except_vec2_sb1;
  416. /* Special cache error handler for SB1 */
  417. set_uncached_handler (0x100, &except_vec2_sb1, 0x80);
  418. probe_cache_sizes();
  419. #ifdef CONFIG_SIBYTE_DMA_PAGEOPS
  420. sb1_dma_init();
  421. #endif
  422. /*
  423. * None of these are needed for the SB1 - the Dcache is
  424. * physically indexed and tagged, so no virtual aliasing can
  425. * occur
  426. */
  427. flush_cache_range = (void *) sb1_nop;
  428. flush_cache_mm = (void (*)(struct mm_struct *))sb1_nop;
  429. flush_cache_all = sb1_nop;
  430. /* These routines are for Icache coherence with the Dcache */
  431. flush_icache_range = sb1_flush_icache_range;
  432. flush_icache_all = __sb1_flush_icache_all; /* local only */
  433. /* This implies an Icache flush too, so can't be nop'ed */
  434. flush_cache_page = sb1_flush_cache_page;
  435. flush_cache_sigtramp = sb1_flush_cache_sigtramp;
  436. local_flush_data_cache_page = (void *) sb1_nop;
  437. flush_data_cache_page = sb1_flush_cache_data_page;
  438. /* Full flush */
  439. __flush_cache_all = sb1___flush_cache_all;
  440. change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
  441. /*
  442. * This is the only way to force the update of K0 to complete
  443. * before subsequent instruction fetch.
  444. */
  445. __asm__ __volatile__(
  446. ".set push \n"
  447. " .set noat \n"
  448. " .set noreorder \n"
  449. " .set mips3 \n"
  450. " " STR(PTR_LA) " $1, 1f \n"
  451. " " STR(MTC0) " $1, $14 \n"
  452. " eret \n"
  453. "1: .set pop"
  454. :
  455. :
  456. : "memory");
  457. local_sb1___flush_cache_all();
  458. }