setup_64.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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/lmb.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/swiotlb.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. /* Put the paca pointer into r13 and SPRG3 */
  126. void __init setup_paca(int cpu)
  127. {
  128. local_paca = &paca[cpu];
  129. mtspr(SPRN_SPRG3, local_paca);
  130. }
  131. /*
  132. * Early initialization entry point. This is called by head.S
  133. * with MMU translation disabled. We rely on the "feature" of
  134. * the CPU that ignores the top 2 bits of the address in real
  135. * mode so we can access kernel globals normally provided we
  136. * only toy with things in the RMO region. From here, we do
  137. * some early parsing of the device-tree to setup out LMB
  138. * data structures, and allocate & initialize the hash table
  139. * and segment tables so we can start running with translation
  140. * enabled.
  141. *
  142. * It is this function which will call the probe() callback of
  143. * the various platform types and copy the matching one to the
  144. * global ppc_md structure. Your platform can eventually do
  145. * some very early initializations from the probe() routine, but
  146. * this is not recommended, be very careful as, for example, the
  147. * device-tree is not accessible via normal means at this point.
  148. */
  149. void __init early_setup(unsigned long dt_ptr)
  150. {
  151. /* -------- printk is _NOT_ safe to use here ! ------- */
  152. /* Fill in any unititialised pacas */
  153. initialise_pacas();
  154. /* Identify CPU type */
  155. identify_cpu(0, mfspr(SPRN_PVR));
  156. /* Assume we're on cpu 0 for now. Don't write to the paca yet! */
  157. setup_paca(0);
  158. /* Initialize lockdep early or else spinlocks will blow */
  159. lockdep_init();
  160. /* -------- printk is now safe to use ------- */
  161. /* Enable early debugging if any specified (see udbg.h) */
  162. udbg_early_init();
  163. DBG(" -> early_setup(), dt_ptr: 0x%lx\n", dt_ptr);
  164. /*
  165. * Do early initialization using the flattened device
  166. * tree, such as retrieving the physical memory map or
  167. * calculating/retrieving the hash table size.
  168. */
  169. early_init_devtree(__va(dt_ptr));
  170. /* Now we know the logical id of our boot cpu, setup the paca. */
  171. setup_paca(boot_cpuid);
  172. /* Fix up paca fields required for the boot cpu */
  173. get_paca()->cpu_start = 1;
  174. /* Probe the machine type */
  175. probe_machine();
  176. setup_kdump_trampoline();
  177. DBG("Found, Initializing memory management...\n");
  178. /* Initialize the hash table or TLB handling */
  179. early_init_mmu();
  180. DBG(" <- early_setup()\n");
  181. }
  182. #ifdef CONFIG_SMP
  183. void early_setup_secondary(void)
  184. {
  185. /* Mark interrupts enabled in PACA */
  186. get_paca()->soft_enabled = 0;
  187. /* Initialize the hash table or TLB handling */
  188. early_init_mmu_secondary();
  189. }
  190. #endif /* CONFIG_SMP */
  191. #if defined(CONFIG_SMP) || defined(CONFIG_KEXEC)
  192. extern unsigned long __secondary_hold_spinloop;
  193. extern void generic_secondary_smp_init(void);
  194. void smp_release_cpus(void)
  195. {
  196. unsigned long *ptr;
  197. DBG(" -> smp_release_cpus()\n");
  198. /* All secondary cpus are spinning on a common spinloop, release them
  199. * all now so they can start to spin on their individual paca
  200. * spinloops. For non SMP kernels, the secondary cpus never get out
  201. * of the common spinloop.
  202. */
  203. ptr = (unsigned long *)((unsigned long)&__secondary_hold_spinloop
  204. - PHYSICAL_START);
  205. *ptr = __pa(generic_secondary_smp_init);
  206. mb();
  207. DBG(" <- smp_release_cpus()\n");
  208. }
  209. #endif /* CONFIG_SMP || CONFIG_KEXEC */
  210. /*
  211. * Initialize some remaining members of the ppc64_caches and systemcfg
  212. * structures
  213. * (at least until we get rid of them completely). This is mostly some
  214. * cache informations about the CPU that will be used by cache flush
  215. * routines and/or provided to userland
  216. */
  217. static void __init initialize_cache_info(void)
  218. {
  219. struct device_node *np;
  220. unsigned long num_cpus = 0;
  221. DBG(" -> initialize_cache_info()\n");
  222. for (np = NULL; (np = of_find_node_by_type(np, "cpu"));) {
  223. num_cpus += 1;
  224. /* We're assuming *all* of the CPUs have the same
  225. * d-cache and i-cache sizes... -Peter
  226. */
  227. if ( num_cpus == 1 ) {
  228. const u32 *sizep, *lsizep;
  229. u32 size, lsize;
  230. size = 0;
  231. lsize = cur_cpu_spec->dcache_bsize;
  232. sizep = of_get_property(np, "d-cache-size", NULL);
  233. if (sizep != NULL)
  234. size = *sizep;
  235. lsizep = of_get_property(np, "d-cache-block-size", NULL);
  236. /* fallback if block size missing */
  237. if (lsizep == NULL)
  238. lsizep = of_get_property(np, "d-cache-line-size", NULL);
  239. if (lsizep != NULL)
  240. lsize = *lsizep;
  241. if (sizep == 0 || lsizep == 0)
  242. DBG("Argh, can't find dcache properties ! "
  243. "sizep: %p, lsizep: %p\n", sizep, lsizep);
  244. ppc64_caches.dsize = size;
  245. ppc64_caches.dline_size = lsize;
  246. ppc64_caches.log_dline_size = __ilog2(lsize);
  247. ppc64_caches.dlines_per_page = PAGE_SIZE / lsize;
  248. size = 0;
  249. lsize = cur_cpu_spec->icache_bsize;
  250. sizep = of_get_property(np, "i-cache-size", NULL);
  251. if (sizep != NULL)
  252. size = *sizep;
  253. lsizep = of_get_property(np, "i-cache-block-size", NULL);
  254. if (lsizep == NULL)
  255. lsizep = of_get_property(np, "i-cache-line-size", NULL);
  256. if (lsizep != NULL)
  257. lsize = *lsizep;
  258. if (sizep == 0 || lsizep == 0)
  259. DBG("Argh, can't find icache properties ! "
  260. "sizep: %p, lsizep: %p\n", sizep, lsizep);
  261. ppc64_caches.isize = size;
  262. ppc64_caches.iline_size = lsize;
  263. ppc64_caches.log_iline_size = __ilog2(lsize);
  264. ppc64_caches.ilines_per_page = PAGE_SIZE / lsize;
  265. }
  266. }
  267. DBG(" <- initialize_cache_info()\n");
  268. }
  269. /*
  270. * Do some initial setup of the system. The parameters are those which
  271. * were passed in from the bootloader.
  272. */
  273. void __init setup_system(void)
  274. {
  275. DBG(" -> setup_system()\n");
  276. /* Apply the CPUs-specific and firmware specific fixups to kernel
  277. * text (nop out sections not relevant to this CPU or this firmware)
  278. */
  279. do_feature_fixups(cur_cpu_spec->cpu_features,
  280. &__start___ftr_fixup, &__stop___ftr_fixup);
  281. do_feature_fixups(cur_cpu_spec->mmu_features,
  282. &__start___mmu_ftr_fixup, &__stop___mmu_ftr_fixup);
  283. do_feature_fixups(powerpc_firmware_features,
  284. &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
  285. do_lwsync_fixups(cur_cpu_spec->cpu_features,
  286. &__start___lwsync_fixup, &__stop___lwsync_fixup);
  287. /*
  288. * Unflatten the device-tree passed by prom_init or kexec
  289. */
  290. unflatten_device_tree();
  291. /*
  292. * Fill the ppc64_caches & systemcfg structures with informations
  293. * retrieved from the device-tree.
  294. */
  295. initialize_cache_info();
  296. /*
  297. * Initialize irq remapping subsystem
  298. */
  299. irq_early_init();
  300. #ifdef CONFIG_PPC_RTAS
  301. /*
  302. * Initialize RTAS if available
  303. */
  304. rtas_initialize();
  305. #endif /* CONFIG_PPC_RTAS */
  306. /*
  307. * Check if we have an initrd provided via the device-tree
  308. */
  309. check_for_initrd();
  310. /*
  311. * Do some platform specific early initializations, that includes
  312. * setting up the hash table pointers. It also sets up some interrupt-mapping
  313. * related options that will be used by finish_device_tree()
  314. */
  315. if (ppc_md.init_early)
  316. ppc_md.init_early();
  317. /*
  318. * We can discover serial ports now since the above did setup the
  319. * hash table management for us, thus ioremap works. We do that early
  320. * so that further code can be debugged
  321. */
  322. find_legacy_serial_ports();
  323. /*
  324. * Register early console
  325. */
  326. register_early_udbg_console();
  327. /*
  328. * Initialize xmon
  329. */
  330. xmon_setup();
  331. check_smt_enabled();
  332. smp_setup_cpu_maps();
  333. #ifdef CONFIG_SMP
  334. /* Release secondary cpus out of their spinloops at 0x60 now that
  335. * we can map physical -> logical CPU ids
  336. */
  337. smp_release_cpus();
  338. #endif
  339. printk("Starting Linux PPC64 %s\n", init_utsname()->version);
  340. printk("-----------------------------------------------------\n");
  341. printk("ppc64_pft_size = 0x%llx\n", ppc64_pft_size);
  342. printk("physicalMemorySize = 0x%llx\n", lmb_phys_mem_size());
  343. if (ppc64_caches.dline_size != 0x80)
  344. printk("ppc64_caches.dcache_line_size = 0x%x\n",
  345. ppc64_caches.dline_size);
  346. if (ppc64_caches.iline_size != 0x80)
  347. printk("ppc64_caches.icache_line_size = 0x%x\n",
  348. ppc64_caches.iline_size);
  349. #ifdef CONFIG_PPC_STD_MMU_64
  350. if (htab_address)
  351. printk("htab_address = 0x%p\n", htab_address);
  352. printk("htab_hash_mask = 0x%lx\n", htab_hash_mask);
  353. #endif /* CONFIG_PPC_STD_MMU_64 */
  354. if (PHYSICAL_START > 0)
  355. printk("physical_start = 0x%llx\n",
  356. (unsigned long long)PHYSICAL_START);
  357. printk("-----------------------------------------------------\n");
  358. DBG(" <- setup_system()\n");
  359. }
  360. #ifdef CONFIG_IRQSTACKS
  361. static void __init irqstack_early_init(void)
  362. {
  363. unsigned int i;
  364. /*
  365. * interrupt stacks must be under 256MB, we cannot afford to take
  366. * SLB misses on them.
  367. */
  368. for_each_possible_cpu(i) {
  369. softirq_ctx[i] = (struct thread_info *)
  370. __va(lmb_alloc_base(THREAD_SIZE,
  371. THREAD_SIZE, 0x10000000));
  372. hardirq_ctx[i] = (struct thread_info *)
  373. __va(lmb_alloc_base(THREAD_SIZE,
  374. THREAD_SIZE, 0x10000000));
  375. }
  376. }
  377. #else
  378. #define irqstack_early_init()
  379. #endif
  380. /*
  381. * Stack space used when we detect a bad kernel stack pointer, and
  382. * early in SMP boots before relocation is enabled.
  383. */
  384. static void __init emergency_stack_init(void)
  385. {
  386. unsigned long limit;
  387. unsigned int i;
  388. /*
  389. * Emergency stacks must be under 256MB, we cannot afford to take
  390. * SLB misses on them. The ABI also requires them to be 128-byte
  391. * aligned.
  392. *
  393. * Since we use these as temporary stacks during secondary CPU
  394. * bringup, we need to get at them in real mode. This means they
  395. * must also be within the RMO region.
  396. */
  397. limit = min(0x10000000ULL, lmb.rmo_size);
  398. for_each_possible_cpu(i) {
  399. unsigned long sp;
  400. sp = lmb_alloc_base(THREAD_SIZE, THREAD_SIZE, limit);
  401. sp += THREAD_SIZE;
  402. paca[i].emergency_sp = __va(sp);
  403. }
  404. }
  405. /*
  406. * Called into from start_kernel, after lock_kernel has been called.
  407. * Initializes bootmem, which is unsed to manage page allocation until
  408. * mem_init is called.
  409. */
  410. void __init setup_arch(char **cmdline_p)
  411. {
  412. ppc64_boot_msg(0x12, "Setup Arch");
  413. *cmdline_p = cmd_line;
  414. /*
  415. * Set cache line size based on type of cpu as a default.
  416. * Systems with OF can look in the properties on the cpu node(s)
  417. * for a possibly more accurate value.
  418. */
  419. dcache_bsize = ppc64_caches.dline_size;
  420. icache_bsize = ppc64_caches.iline_size;
  421. /* reboot on panic */
  422. panic_timeout = 180;
  423. if (ppc_md.panic)
  424. setup_panic();
  425. init_mm.start_code = (unsigned long)_stext;
  426. init_mm.end_code = (unsigned long) _etext;
  427. init_mm.end_data = (unsigned long) _edata;
  428. init_mm.brk = klimit;
  429. irqstack_early_init();
  430. emergency_stack_init();
  431. #ifdef CONFIG_PPC_STD_MMU_64
  432. stabs_alloc();
  433. #endif
  434. /* set up the bootmem stuff with available memory */
  435. do_init_bootmem();
  436. sparse_init();
  437. #ifdef CONFIG_DUMMY_CONSOLE
  438. conswitchp = &dummy_con;
  439. #endif
  440. if (ppc_md.setup_arch)
  441. ppc_md.setup_arch();
  442. #ifdef CONFIG_SWIOTLB
  443. if (ppc_swiotlb_enable)
  444. swiotlb_init();
  445. #endif
  446. paging_init();
  447. ppc64_boot_msg(0x15, "Setup Done");
  448. }
  449. /* ToDo: do something useful if ppc_md is not yet setup. */
  450. #define PPC64_LINUX_FUNCTION 0x0f000000
  451. #define PPC64_IPL_MESSAGE 0xc0000000
  452. #define PPC64_TERM_MESSAGE 0xb0000000
  453. static void ppc64_do_msg(unsigned int src, const char *msg)
  454. {
  455. if (ppc_md.progress) {
  456. char buf[128];
  457. sprintf(buf, "%08X\n", src);
  458. ppc_md.progress(buf, 0);
  459. snprintf(buf, 128, "%s", msg);
  460. ppc_md.progress(buf, 0);
  461. }
  462. }
  463. /* Print a boot progress message. */
  464. void ppc64_boot_msg(unsigned int src, const char *msg)
  465. {
  466. ppc64_do_msg(PPC64_LINUX_FUNCTION|PPC64_IPL_MESSAGE|src, msg);
  467. printk("[boot]%04x %s\n", src, msg);
  468. }
  469. void cpu_die(void)
  470. {
  471. if (ppc_md.cpu_die)
  472. ppc_md.cpu_die();
  473. }
  474. #ifdef CONFIG_SMP
  475. void __init setup_per_cpu_areas(void)
  476. {
  477. int i;
  478. unsigned long size;
  479. char *ptr;
  480. /* Copy section for each CPU (we discard the original) */
  481. size = ALIGN(__per_cpu_end - __per_cpu_start, PAGE_SIZE);
  482. #ifdef CONFIG_MODULES
  483. if (size < PERCPU_ENOUGH_ROOM)
  484. size = PERCPU_ENOUGH_ROOM;
  485. #endif
  486. for_each_possible_cpu(i) {
  487. ptr = alloc_bootmem_pages_node(NODE_DATA(cpu_to_node(i)), size);
  488. paca[i].data_offset = ptr - __per_cpu_start;
  489. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  490. }
  491. }
  492. #endif
  493. #ifdef CONFIG_PPC_INDIRECT_IO
  494. struct ppc_pci_io ppc_pci_io;
  495. EXPORT_SYMBOL(ppc_pci_io);
  496. #endif /* CONFIG_PPC_INDIRECT_IO */