c-sb1.c 15 KB

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