setup_64.c 15 KB

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