c-sb1.c 14 KB

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