init.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/sh/kernel/cpu/init.c
  4. *
  5. * CPU init code
  6. *
  7. * Copyright (C) 2002 - 2009 Paul Mundt
  8. * Copyright (C) 2003 Richard Curnow
  9. */
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/log2.h>
  14. #include <asm/mmu_context.h>
  15. #include <asm/processor.h>
  16. #include <linux/uaccess.h>
  17. #include <asm/page.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/cache.h>
  20. #include <asm/elf.h>
  21. #include <asm/io.h>
  22. #include <asm/smp.h>
  23. #include <asm/sh_bios.h>
  24. #include <asm/setup.h>
  25. #ifdef CONFIG_SH_FPU
  26. #define cpu_has_fpu 1
  27. #else
  28. #define cpu_has_fpu 0
  29. #endif
  30. #ifdef CONFIG_SH_DSP
  31. #define cpu_has_dsp 1
  32. #else
  33. #define cpu_has_dsp 0
  34. #endif
  35. /*
  36. * Generic wrapper for command line arguments to disable on-chip
  37. * peripherals (nofpu, nodsp, and so forth).
  38. */
  39. #define onchip_setup(x) \
  40. static int x##_disabled = !cpu_has_##x; \
  41. \
  42. static int x##_setup(char *opts) \
  43. { \
  44. x##_disabled = 1; \
  45. return 1; \
  46. } \
  47. __setup("no" __stringify(x), x##_setup);
  48. onchip_setup(fpu);
  49. onchip_setup(dsp);
  50. #ifdef CONFIG_SPECULATIVE_EXECUTION
  51. #define CPUOPM 0xff2f0000
  52. #define CPUOPM_RABD (1 << 5)
  53. static void speculative_execution_init(void)
  54. {
  55. /* Clear RABD */
  56. __raw_writel(__raw_readl(CPUOPM) & ~CPUOPM_RABD, CPUOPM);
  57. /* Flush the update */
  58. (void)__raw_readl(CPUOPM);
  59. ctrl_barrier();
  60. }
  61. #else
  62. #define speculative_execution_init() do { } while (0)
  63. #endif
  64. #ifdef CONFIG_CPU_SH4A
  65. #define EXPMASK 0xff2f0004
  66. #define EXPMASK_RTEDS (1 << 0)
  67. #define EXPMASK_BRDSSLP (1 << 1)
  68. #define EXPMASK_MMCAW (1 << 4)
  69. static void expmask_init(void)
  70. {
  71. unsigned long expmask = __raw_readl(EXPMASK);
  72. /*
  73. * Future proofing.
  74. *
  75. * Disable support for slottable sleep instruction, non-nop
  76. * instructions in the rte delay slot, and associative writes to
  77. * the memory-mapped cache array.
  78. */
  79. expmask &= ~(EXPMASK_RTEDS | EXPMASK_BRDSSLP | EXPMASK_MMCAW);
  80. __raw_writel(expmask, EXPMASK);
  81. ctrl_barrier();
  82. }
  83. #else
  84. #define expmask_init() do { } while (0)
  85. #endif
  86. /* 2nd-level cache init */
  87. void __attribute__ ((weak)) l2_cache_init(void)
  88. {
  89. }
  90. /*
  91. * Generic first-level cache init
  92. */
  93. #if !defined(CONFIG_CPU_J2)
  94. static void cache_init(void)
  95. {
  96. unsigned long ccr, flags;
  97. jump_to_uncached();
  98. ccr = __raw_readl(SH_CCR);
  99. /*
  100. * At this point we don't know whether the cache is enabled or not - a
  101. * bootloader may have enabled it. There are at least 2 things that
  102. * could be dirty in the cache at this point:
  103. * 1. kernel command line set up by boot loader
  104. * 2. spilled registers from the prolog of this function
  105. * => before re-initialising the cache, we must do a purge of the whole
  106. * cache out to memory for safety. As long as nothing is spilled
  107. * during the loop to lines that have already been done, this is safe.
  108. * - RPC
  109. */
  110. if (ccr & CCR_CACHE_ENABLE) {
  111. unsigned long ways, waysize, addrstart;
  112. waysize = current_cpu_data.dcache.sets;
  113. #ifdef CCR_CACHE_ORA
  114. /*
  115. * If the OC is already in RAM mode, we only have
  116. * half of the entries to flush..
  117. */
  118. if (ccr & CCR_CACHE_ORA)
  119. waysize >>= 1;
  120. #endif
  121. waysize <<= current_cpu_data.dcache.entry_shift;
  122. #ifdef CCR_CACHE_EMODE
  123. /* If EMODE is not set, we only have 1 way to flush. */
  124. if (!(ccr & CCR_CACHE_EMODE))
  125. ways = 1;
  126. else
  127. #endif
  128. ways = current_cpu_data.dcache.ways;
  129. addrstart = CACHE_OC_ADDRESS_ARRAY;
  130. do {
  131. unsigned long addr;
  132. for (addr = addrstart;
  133. addr < addrstart + waysize;
  134. addr += current_cpu_data.dcache.linesz)
  135. __raw_writel(0, addr);
  136. addrstart += current_cpu_data.dcache.way_incr;
  137. } while (--ways);
  138. }
  139. /*
  140. * Default CCR values .. enable the caches
  141. * and invalidate them immediately..
  142. */
  143. flags = CCR_CACHE_ENABLE | CCR_CACHE_INVALIDATE;
  144. #ifdef CCR_CACHE_EMODE
  145. /* Force EMODE if possible */
  146. if (current_cpu_data.dcache.ways > 1)
  147. flags |= CCR_CACHE_EMODE;
  148. else
  149. flags &= ~CCR_CACHE_EMODE;
  150. #endif
  151. #if defined(CONFIG_CACHE_WRITETHROUGH)
  152. /* Write-through */
  153. flags |= CCR_CACHE_WT;
  154. #elif defined(CONFIG_CACHE_WRITEBACK)
  155. /* Write-back */
  156. flags |= CCR_CACHE_CB;
  157. #else
  158. /* Off */
  159. flags &= ~CCR_CACHE_ENABLE;
  160. #endif
  161. l2_cache_init();
  162. __raw_writel(flags, SH_CCR);
  163. back_to_cached();
  164. }
  165. #else
  166. #define cache_init() do { } while (0)
  167. #endif
  168. #define CSHAPE(totalsize, linesize, assoc) \
  169. ((totalsize & ~0xff) | (linesize << 4) | assoc)
  170. #define CACHE_DESC_SHAPE(desc) \
  171. CSHAPE((desc).way_size * (desc).ways, ilog2((desc).linesz), (desc).ways)
  172. static void detect_cache_shape(void)
  173. {
  174. l1d_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.dcache);
  175. if (current_cpu_data.dcache.flags & SH_CACHE_COMBINED)
  176. l1i_cache_shape = l1d_cache_shape;
  177. else
  178. l1i_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.icache);
  179. if (current_cpu_data.flags & CPU_HAS_L2_CACHE)
  180. l2_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.scache);
  181. else
  182. l2_cache_shape = -1; /* No S-cache */
  183. }
  184. static void fpu_init(void)
  185. {
  186. /* Disable the FPU */
  187. if (fpu_disabled && (current_cpu_data.flags & CPU_HAS_FPU)) {
  188. printk("FPU Disabled\n");
  189. current_cpu_data.flags &= ~CPU_HAS_FPU;
  190. }
  191. disable_fpu();
  192. clear_used_math();
  193. }
  194. #ifdef CONFIG_SH_DSP
  195. static void release_dsp(void)
  196. {
  197. unsigned long sr;
  198. /* Clear SR.DSP bit */
  199. __asm__ __volatile__ (
  200. "stc\tsr, %0\n\t"
  201. "and\t%1, %0\n\t"
  202. "ldc\t%0, sr\n\t"
  203. : "=&r" (sr)
  204. : "r" (~SR_DSP)
  205. );
  206. }
  207. static void dsp_init(void)
  208. {
  209. unsigned long sr;
  210. /*
  211. * Set the SR.DSP bit, wait for one instruction, and then read
  212. * back the SR value.
  213. */
  214. __asm__ __volatile__ (
  215. "stc\tsr, %0\n\t"
  216. "or\t%1, %0\n\t"
  217. "ldc\t%0, sr\n\t"
  218. "nop\n\t"
  219. "stc\tsr, %0\n\t"
  220. : "=&r" (sr)
  221. : "r" (SR_DSP)
  222. );
  223. /* If the DSP bit is still set, this CPU has a DSP */
  224. if (sr & SR_DSP)
  225. current_cpu_data.flags |= CPU_HAS_DSP;
  226. /* Disable the DSP */
  227. if (dsp_disabled && (current_cpu_data.flags & CPU_HAS_DSP)) {
  228. printk("DSP Disabled\n");
  229. current_cpu_data.flags &= ~CPU_HAS_DSP;
  230. }
  231. /* Now that we've determined the DSP status, clear the DSP bit. */
  232. release_dsp();
  233. }
  234. #else
  235. static inline void dsp_init(void) { }
  236. #endif /* CONFIG_SH_DSP */
  237. /**
  238. * cpu_init
  239. *
  240. * This is our initial entry point for each CPU, and is invoked on the
  241. * boot CPU prior to calling start_kernel(). For SMP, a combination of
  242. * this and start_secondary() will bring up each processor to a ready
  243. * state prior to hand forking the idle loop.
  244. *
  245. * We do all of the basic processor init here, including setting up
  246. * the caches, FPU, DSP, etc. By the time start_kernel() is hit (and
  247. * subsequently platform_setup()) things like determining the CPU
  248. * subtype and initial configuration will all be done.
  249. *
  250. * Each processor family is still responsible for doing its own probing
  251. * and cache configuration in cpu_probe().
  252. */
  253. asmlinkage void cpu_init(void)
  254. {
  255. current_thread_info()->cpu = hard_smp_processor_id();
  256. /* First, probe the CPU */
  257. cpu_probe();
  258. if (current_cpu_data.type == CPU_SH_NONE)
  259. panic("Unknown CPU");
  260. /* First setup the rest of the I-cache info */
  261. current_cpu_data.icache.entry_mask = current_cpu_data.icache.way_incr -
  262. current_cpu_data.icache.linesz;
  263. current_cpu_data.icache.way_size = current_cpu_data.icache.sets *
  264. current_cpu_data.icache.linesz;
  265. /* And the D-cache too */
  266. current_cpu_data.dcache.entry_mask = current_cpu_data.dcache.way_incr -
  267. current_cpu_data.dcache.linesz;
  268. current_cpu_data.dcache.way_size = current_cpu_data.dcache.sets *
  269. current_cpu_data.dcache.linesz;
  270. /* Init the cache */
  271. cache_init();
  272. if (raw_smp_processor_id() == 0) {
  273. #ifdef CONFIG_MMU
  274. shm_align_mask = max_t(unsigned long,
  275. current_cpu_data.dcache.way_size - 1,
  276. PAGE_SIZE - 1);
  277. #else
  278. shm_align_mask = PAGE_SIZE - 1;
  279. #endif
  280. /* Boot CPU sets the cache shape */
  281. detect_cache_shape();
  282. }
  283. fpu_init();
  284. dsp_init();
  285. /*
  286. * Initialize the per-CPU ASID cache very early, since the
  287. * TLB flushing routines depend on this being setup.
  288. */
  289. current_cpu_data.asid_cache = NO_CONTEXT;
  290. current_cpu_data.phys_bits = __in_29bit_mode() ? 29 : 32;
  291. speculative_execution_init();
  292. expmask_init();
  293. /* Do the rest of the boot processor setup */
  294. if (raw_smp_processor_id() == 0) {
  295. /* Save off the BIOS VBR, if there is one */
  296. sh_bios_vbr_init();
  297. /*
  298. * Setup VBR for boot CPU. Secondary CPUs do this through
  299. * start_secondary().
  300. */
  301. per_cpu_trap_init();
  302. /*
  303. * Boot processor to setup the FP and extended state
  304. * context info.
  305. */
  306. init_thread_xstate();
  307. }
  308. }