setup_64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. *
  3. * Common boot and setup code.
  4. *
  5. * Copyright (C) 2001 PPC64 Team, IBM Corp
  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
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #undef DEBUG
  13. #include <linux/module.h>
  14. #include <linux/string.h>
  15. #include <linux/sched.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/reboot.h>
  19. #include <linux/delay.h>
  20. #include <linux/initrd.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/ioport.h>
  23. #include <linux/console.h>
  24. #include <linux/utsname.h>
  25. #include <linux/tty.h>
  26. #include <linux/root_dev.h>
  27. #include <linux/notifier.h>
  28. #include <linux/cpu.h>
  29. #include <linux/unistd.h>
  30. #include <linux/serial.h>
  31. #include <linux/serial_8250.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/pci.h>
  34. #include <linux/lockdep.h>
  35. #include <linux/memblock.h>
  36. #include <asm/io.h>
  37. #include <asm/kdump.h>
  38. #include <asm/prom.h>
  39. #include <asm/processor.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/smp.h>
  42. #include <asm/elf.h>
  43. #include <asm/machdep.h>
  44. #include <asm/paca.h>
  45. #include <asm/time.h>
  46. #include <asm/cputable.h>
  47. #include <asm/sections.h>
  48. #include <asm/btext.h>
  49. #include <asm/nvram.h>
  50. #include <asm/setup.h>
  51. #include <asm/system.h>
  52. #include <asm/rtas.h>
  53. #include <asm/iommu.h>
  54. #include <asm/serial.h>
  55. #include <asm/cache.h>
  56. #include <asm/page.h>
  57. #include <asm/mmu.h>
  58. #include <asm/firmware.h>
  59. #include <asm/xmon.h>
  60. #include <asm/udbg.h>
  61. #include <asm/kexec.h>
  62. #include <asm/mmu_context.h>
  63. #include "setup.h"
  64. #ifdef DEBUG
  65. #define DBG(fmt...) udbg_printf(fmt)
  66. #else
  67. #define DBG(fmt...)
  68. #endif
  69. int boot_cpuid = 0;
  70. u64 ppc64_pft_size;
  71. /* Pick defaults since we might want to patch instructions
  72. * before we've read this from the device tree.
  73. */
  74. struct ppc64_caches ppc64_caches = {
  75. .dline_size = 0x40,
  76. .log_dline_size = 6,
  77. .iline_size = 0x40,
  78. .log_iline_size = 6
  79. };
  80. EXPORT_SYMBOL_GPL(ppc64_caches);
  81. /*
  82. * These are used in binfmt_elf.c to put aux entries on the stack
  83. * for each elf executable being started.
  84. */
  85. int dcache_bsize;
  86. int icache_bsize;
  87. int ucache_bsize;
  88. #ifdef CONFIG_SMP
  89. static int smt_enabled_cmdline;
  90. /* Look for ibm,smt-enabled OF option */
  91. static void check_smt_enabled(void)
  92. {
  93. struct device_node *dn;
  94. const char *smt_option;
  95. /* Allow the command line to overrule the OF option */
  96. if (smt_enabled_cmdline)
  97. return;
  98. dn = of_find_node_by_path("/options");
  99. if (dn) {
  100. smt_option = of_get_property(dn, "ibm,smt-enabled", NULL);
  101. if (smt_option) {
  102. if (!strcmp(smt_option, "on"))
  103. smt_enabled_at_boot = 1;
  104. else if (!strcmp(smt_option, "off"))
  105. smt_enabled_at_boot = 0;
  106. }
  107. }
  108. }
  109. /* Look for smt-enabled= cmdline option */
  110. static int __init early_smt_enabled(char *p)
  111. {
  112. smt_enabled_cmdline = 1;
  113. if (!p)
  114. return 0;
  115. if (!strcmp(p, "on") || !strcmp(p, "1"))
  116. smt_enabled_at_boot = 1;
  117. else if (!strcmp(p, "off") || !strcmp(p, "0"))
  118. smt_enabled_at_boot = 0;
  119. return 0;
  120. }
  121. early_param("smt-enabled", early_smt_enabled);
  122. #else
  123. #define check_smt_enabled()
  124. #endif /* CONFIG_SMP */
  125. /*
  126. * Early initialization entry point. This is called by head.S
  127. * with MMU translation disabled. We rely on the "feature" of
  128. * the CPU that ignores the top 2 bits of the address in real
  129. * mode so we can access kernel globals normally provided we
  130. * only toy with things in the RMO region. From here, we do
  131. * some early parsing of the device-tree to setup out MEMBLOCK
  132. * data structures, and allocate & initialize the hash table
  133. * and segment tables so we can start running with translation
  134. * enabled.
  135. *
  136. * It is this function which will call the probe() callback of
  137. * the various platform types and copy the matching one to the
  138. * global ppc_md structure. Your platform can eventually do
  139. * some very early initializations from the probe() routine, but
  140. * this is not recommended, be very careful as, for example, the
  141. * device-tree is not accessible via normal means at this point.
  142. */
  143. void __init early_setup(unsigned long dt_ptr)
  144. {
  145. /* -------- printk is _NOT_ safe to use here ! ------- */
  146. /* Identify CPU type */
  147. identify_cpu(0, mfspr(SPRN_PVR));
  148. /* Assume we're on cpu 0 for now. Don't write to the paca yet! */
  149. initialise_paca(&boot_paca, 0);
  150. setup_paca(&boot_paca);
  151. /* Initialize lockdep early or else spinlocks will blow */
  152. lockdep_init();
  153. /* -------- printk is now safe to use ------- */
  154. /* Enable early debugging if any specified (see udbg.h) */
  155. udbg_early_init();
  156. DBG(" -> early_setup(), dt_ptr: 0x%lx\n", dt_ptr);
  157. /*
  158. * Do early initialization using the flattened device
  159. * tree, such as retrieving the physical memory map or
  160. * calculating/retrieving the hash table size.
  161. */
  162. early_init_devtree(__va(dt_ptr));
  163. /* Now we know the logical id of our boot cpu, setup the paca. */
  164. setup_paca(&paca[boot_cpuid]);
  165. /* Fix up paca fields required for the boot cpu */
  166. get_paca()->cpu_start = 1;
  167. /* Probe the machine type */
  168. probe_machine();
  169. setup_kdump_trampoline();
  170. DBG("Found, Initializing memory management...\n");
  171. /* Initialize the hash table or TLB handling */
  172. early_init_mmu();
  173. DBG(" <- early_setup()\n");
  174. }
  175. #ifdef CONFIG_SMP
  176. void early_setup_secondary(void)
  177. {
  178. /* Mark interrupts enabled in PACA */
  179. get_paca()->soft_enabled = 0;
  180. /* Initialize the hash table or TLB handling */
  181. early_init_mmu_secondary();
  182. }
  183. #endif /* CONFIG_SMP */
  184. #if defined(CONFIG_SMP) || defined(CONFIG_KEXEC)
  185. void smp_release_cpus(void)
  186. {
  187. unsigned long *ptr;
  188. DBG(" -> smp_release_cpus()\n");
  189. /* All secondary cpus are spinning on a common spinloop, release them
  190. * all now so they can start to spin on their individual paca
  191. * spinloops. For non SMP kernels, the secondary cpus never get out
  192. * of the common spinloop.
  193. */
  194. ptr = (unsigned long *)((unsigned long)&__secondary_hold_spinloop
  195. - PHYSICAL_START);
  196. *ptr = __pa(generic_secondary_smp_init);
  197. mb();
  198. DBG(" <- smp_release_cpus()\n");
  199. }
  200. #endif /* CONFIG_SMP || CONFIG_KEXEC */
  201. /*
  202. * Initialize some remaining members of the ppc64_caches and systemcfg
  203. * structures
  204. * (at least until we get rid of them completely). This is mostly some
  205. * cache informations about the CPU that will be used by cache flush
  206. * routines and/or provided to userland
  207. */
  208. static void __init initialize_cache_info(void)
  209. {
  210. struct device_node *np;
  211. unsigned long num_cpus = 0;
  212. DBG(" -> initialize_cache_info()\n");
  213. for (np = NULL; (np = of_find_node_by_type(np, "cpu"));) {
  214. num_cpus += 1;
  215. /* We're assuming *all* of the CPUs have the same
  216. * d-cache and i-cache sizes... -Peter
  217. */
  218. if ( num_cpus == 1 ) {
  219. const u32 *sizep, *lsizep;
  220. u32 size, lsize;
  221. size = 0;
  222. lsize = cur_cpu_spec->dcache_bsize;
  223. sizep = of_get_property(np, "d-cache-size", NULL);
  224. if (sizep != NULL)
  225. size = *sizep;
  226. lsizep = of_get_property(np, "d-cache-block-size", NULL);
  227. /* fallback if block size missing */
  228. if (lsizep == NULL)
  229. lsizep = of_get_property(np, "d-cache-line-size", NULL);
  230. if (lsizep != NULL)
  231. lsize = *lsizep;
  232. if (sizep == 0 || lsizep == 0)
  233. DBG("Argh, can't find dcache properties ! "
  234. "sizep: %p, lsizep: %p\n", sizep, lsizep);
  235. ppc64_caches.dsize = size;
  236. ppc64_caches.dline_size = lsize;
  237. ppc64_caches.log_dline_size = __ilog2(lsize);
  238. ppc64_caches.dlines_per_page = PAGE_SIZE / lsize;
  239. size = 0;
  240. lsize = cur_cpu_spec->icache_bsize;
  241. sizep = of_get_property(np, "i-cache-size", NULL);
  242. if (sizep != NULL)
  243. size = *sizep;
  244. lsizep = of_get_property(np, "i-cache-block-size", NULL);
  245. if (lsizep == NULL)
  246. lsizep = of_get_property(np, "i-cache-line-size", NULL);
  247. if (lsizep != NULL)
  248. lsize = *lsizep;
  249. if (sizep == 0 || lsizep == 0)
  250. DBG("Argh, can't find icache properties ! "
  251. "sizep: %p, lsizep: %p\n", sizep, lsizep);
  252. ppc64_caches.isize = size;
  253. ppc64_caches.iline_size = lsize;
  254. ppc64_caches.log_iline_size = __ilog2(lsize);
  255. ppc64_caches.ilines_per_page = PAGE_SIZE / lsize;
  256. }
  257. }
  258. DBG(" <- initialize_cache_info()\n");
  259. }
  260. /*
  261. * Do some initial setup of the system. The parameters are those which
  262. * were passed in from the bootloader.
  263. */
  264. void __init setup_system(void)
  265. {
  266. DBG(" -> setup_system()\n");
  267. /* Apply the CPUs-specific and firmware specific fixups to kernel
  268. * text (nop out sections not relevant to this CPU or this firmware)
  269. */
  270. do_feature_fixups(cur_cpu_spec->cpu_features,
  271. &__start___ftr_fixup, &__stop___ftr_fixup);
  272. do_feature_fixups(cur_cpu_spec->mmu_features,
  273. &__start___mmu_ftr_fixup, &__stop___mmu_ftr_fixup);
  274. do_feature_fixups(powerpc_firmware_features,
  275. &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
  276. do_lwsync_fixups(cur_cpu_spec->cpu_features,
  277. &__start___lwsync_fixup, &__stop___lwsync_fixup);
  278. /*
  279. * Unflatten the device-tree passed by prom_init or kexec
  280. */
  281. unflatten_device_tree();
  282. /*
  283. * Fill the ppc64_caches & systemcfg structures with informations
  284. * retrieved from the device-tree.
  285. */
  286. initialize_cache_info();
  287. #ifdef CONFIG_PPC_RTAS
  288. /*
  289. * Initialize RTAS if available
  290. */
  291. rtas_initialize();
  292. #endif /* CONFIG_PPC_RTAS */
  293. /*
  294. * Check if we have an initrd provided via the device-tree
  295. */
  296. check_for_initrd();
  297. /*
  298. * Do some platform specific early initializations, that includes
  299. * setting up the hash table pointers. It also sets up some interrupt-mapping
  300. * related options that will be used by finish_device_tree()
  301. */
  302. if (ppc_md.init_early)
  303. ppc_md.init_early();
  304. /*
  305. * We can discover serial ports now since the above did setup the
  306. * hash table management for us, thus ioremap works. We do that early
  307. * so that further code can be debugged
  308. */
  309. find_legacy_serial_ports();
  310. /*
  311. * Register early console
  312. */
  313. register_early_udbg_console();
  314. /*
  315. * Initialize xmon
  316. */
  317. xmon_setup();
  318. check_smt_enabled();
  319. smp_setup_cpu_maps();
  320. #ifdef CONFIG_SMP
  321. /* Release secondary cpus out of their spinloops at 0x60 now that
  322. * we can map physical -> logical CPU ids
  323. */
  324. smp_release_cpus();
  325. #endif
  326. printk("Starting Linux PPC64 %s\n", init_utsname()->version);
  327. printk("-----------------------------------------------------\n");
  328. printk("ppc64_pft_size = 0x%llx\n", ppc64_pft_size);
  329. printk("physicalMemorySize = 0x%llx\n", memblock_phys_mem_size());
  330. if (ppc64_caches.dline_size != 0x80)
  331. printk("ppc64_caches.dcache_line_size = 0x%x\n",
  332. ppc64_caches.dline_size);
  333. if (ppc64_caches.iline_size != 0x80)
  334. printk("ppc64_caches.icache_line_size = 0x%x\n",
  335. ppc64_caches.iline_size);
  336. #ifdef CONFIG_PPC_STD_MMU_64
  337. if (htab_address)
  338. printk("htab_address = 0x%p\n", htab_address);
  339. printk("htab_hash_mask = 0x%lx\n", htab_hash_mask);
  340. #endif /* CONFIG_PPC_STD_MMU_64 */
  341. if (PHYSICAL_START > 0)
  342. printk("physical_start = 0x%llx\n",
  343. (unsigned long long)PHYSICAL_START);
  344. printk("-----------------------------------------------------\n");
  345. DBG(" <- setup_system()\n");
  346. }
  347. static u64 slb0_limit(void)
  348. {
  349. if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
  350. return 1UL << SID_SHIFT_1T;
  351. }
  352. return 1UL << SID_SHIFT;
  353. }
  354. static void __init irqstack_early_init(void)
  355. {
  356. u64 limit = slb0_limit();
  357. unsigned int i;
  358. /*
  359. * interrupt stacks must be under 256MB, we cannot afford to take
  360. * SLB misses on them.
  361. */
  362. for_each_possible_cpu(i) {
  363. softirq_ctx[i] = (struct thread_info *)
  364. __va(memblock_alloc_base(THREAD_SIZE,
  365. THREAD_SIZE, limit));
  366. hardirq_ctx[i] = (struct thread_info *)
  367. __va(memblock_alloc_base(THREAD_SIZE,
  368. THREAD_SIZE, limit));
  369. }
  370. }
  371. #ifdef CONFIG_PPC_BOOK3E
  372. static void __init exc_lvl_early_init(void)
  373. {
  374. unsigned int i;
  375. for_each_possible_cpu(i) {
  376. critirq_ctx[i] = (struct thread_info *)
  377. __va(memblock_alloc(THREAD_SIZE, THREAD_SIZE));
  378. dbgirq_ctx[i] = (struct thread_info *)
  379. __va(memblock_alloc(THREAD_SIZE, THREAD_SIZE));
  380. mcheckirq_ctx[i] = (struct thread_info *)
  381. __va(memblock_alloc(THREAD_SIZE, THREAD_SIZE));
  382. }
  383. }
  384. #else
  385. #define exc_lvl_early_init()
  386. #endif
  387. /*
  388. * Stack space used when we detect a bad kernel stack pointer, and
  389. * early in SMP boots before relocation is enabled.
  390. */
  391. static void __init emergency_stack_init(void)
  392. {
  393. u64 limit;
  394. unsigned int i;
  395. /*
  396. * Emergency stacks must be under 256MB, we cannot afford to take
  397. * SLB misses on them. The ABI also requires them to be 128-byte
  398. * aligned.
  399. *
  400. * Since we use these as temporary stacks during secondary CPU
  401. * bringup, we need to get at them in real mode. This means they
  402. * must also be within the RMO region.
  403. */
  404. limit = min(slb0_limit(), memblock.rmo_size);
  405. for_each_possible_cpu(i) {
  406. unsigned long sp;
  407. sp = memblock_alloc_base(THREAD_SIZE, THREAD_SIZE, limit);
  408. sp += THREAD_SIZE;
  409. paca[i].emergency_sp = __va(sp);
  410. }
  411. }
  412. /*
  413. * Called into from start_kernel, after lock_kernel has been called.
  414. * Initializes bootmem, which is unsed to manage page allocation until
  415. * mem_init is called.
  416. */
  417. void __init setup_arch(char **cmdline_p)
  418. {
  419. ppc64_boot_msg(0x12, "Setup Arch");
  420. *cmdline_p = cmd_line;
  421. /*
  422. * Set cache line size based on type of cpu as a default.
  423. * Systems with OF can look in the properties on the cpu node(s)
  424. * for a possibly more accurate value.
  425. */
  426. dcache_bsize = ppc64_caches.dline_size;
  427. icache_bsize = ppc64_caches.iline_size;
  428. /* reboot on panic */
  429. panic_timeout = 180;
  430. if (ppc_md.panic)
  431. setup_panic();
  432. init_mm.start_code = (unsigned long)_stext;
  433. init_mm.end_code = (unsigned long) _etext;
  434. init_mm.end_data = (unsigned long) _edata;
  435. init_mm.brk = klimit;
  436. irqstack_early_init();
  437. exc_lvl_early_init();
  438. emergency_stack_init();
  439. #ifdef CONFIG_PPC_STD_MMU_64
  440. stabs_alloc();
  441. #endif
  442. /* set up the bootmem stuff with available memory */
  443. do_init_bootmem();
  444. sparse_init();
  445. #ifdef CONFIG_DUMMY_CONSOLE
  446. conswitchp = &dummy_con;
  447. #endif
  448. if (ppc_md.setup_arch)
  449. ppc_md.setup_arch();
  450. paging_init();
  451. /* Initialize the MMU context management stuff */
  452. mmu_context_init();
  453. ppc64_boot_msg(0x15, "Setup Done");
  454. }
  455. /* ToDo: do something useful if ppc_md is not yet setup. */
  456. #define PPC64_LINUX_FUNCTION 0x0f000000
  457. #define PPC64_IPL_MESSAGE 0xc0000000
  458. #define PPC64_TERM_MESSAGE 0xb0000000
  459. static void ppc64_do_msg(unsigned int src, const char *msg)
  460. {
  461. if (ppc_md.progress) {
  462. char buf[128];
  463. sprintf(buf, "%08X\n", src);
  464. ppc_md.progress(buf, 0);
  465. snprintf(buf, 128, "%s", msg);
  466. ppc_md.progress(buf, 0);
  467. }
  468. }
  469. /* Print a boot progress message. */
  470. void ppc64_boot_msg(unsigned int src, const char *msg)
  471. {
  472. ppc64_do_msg(PPC64_LINUX_FUNCTION|PPC64_IPL_MESSAGE|src, msg);
  473. printk("[boot]%04x %s\n", src, msg);
  474. }
  475. #ifdef CONFIG_SMP
  476. #define PCPU_DYN_SIZE ()
  477. static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align)
  478. {
  479. return __alloc_bootmem_node(NODE_DATA(cpu_to_node(cpu)), size, align,
  480. __pa(MAX_DMA_ADDRESS));
  481. }
  482. static void __init pcpu_fc_free(void *ptr, size_t size)
  483. {
  484. free_bootmem(__pa(ptr), size);
  485. }
  486. static int pcpu_cpu_distance(unsigned int from, unsigned int to)
  487. {
  488. if (cpu_to_node(from) == cpu_to_node(to))
  489. return LOCAL_DISTANCE;
  490. else
  491. return REMOTE_DISTANCE;
  492. }
  493. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  494. EXPORT_SYMBOL(__per_cpu_offset);
  495. void __init setup_per_cpu_areas(void)
  496. {
  497. const size_t dyn_size = PERCPU_MODULE_RESERVE + PERCPU_DYNAMIC_RESERVE;
  498. size_t atom_size;
  499. unsigned long delta;
  500. unsigned int cpu;
  501. int rc;
  502. /*
  503. * Linear mapping is one of 4K, 1M and 16M. For 4K, no need
  504. * to group units. For larger mappings, use 1M atom which
  505. * should be large enough to contain a number of units.
  506. */
  507. if (mmu_linear_psize == MMU_PAGE_4K)
  508. atom_size = PAGE_SIZE;
  509. else
  510. atom_size = 1 << 20;
  511. rc = pcpu_embed_first_chunk(0, dyn_size, atom_size, pcpu_cpu_distance,
  512. pcpu_fc_alloc, pcpu_fc_free);
  513. if (rc < 0)
  514. panic("cannot initialize percpu area (err=%d)", rc);
  515. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  516. for_each_possible_cpu(cpu) {
  517. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  518. paca[cpu].data_offset = __per_cpu_offset[cpu];
  519. }
  520. }
  521. #endif
  522. #ifdef CONFIG_PPC_INDIRECT_IO
  523. struct ppc_pci_io ppc_pci_io;
  524. EXPORT_SYMBOL(ppc_pci_io);
  525. #endif /* CONFIG_PPC_INDIRECT_IO */