setup_64.c 16 KB

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