paca.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * c 2001 PPC 64 Team, IBM Corp
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/smp.h>
  10. #include <linux/export.h>
  11. #include <linux/memblock.h>
  12. #include <asm/lppaca.h>
  13. #include <asm/paca.h>
  14. #include <asm/sections.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/kexec.h>
  17. #ifdef CONFIG_PPC_BOOK3S
  18. /*
  19. * The structure which the hypervisor knows about - this structure
  20. * should not cross a page boundary. The vpa_init/register_vpa call
  21. * is now known to fail if the lppaca structure crosses a page
  22. * boundary. The lppaca is also used on POWER5 pSeries boxes.
  23. * The lppaca is 640 bytes long, and cannot readily
  24. * change since the hypervisor knows its layout, so a 1kB alignment
  25. * will suffice to ensure that it doesn't cross a page boundary.
  26. */
  27. struct lppaca lppaca[] = {
  28. [0 ... (NR_LPPACAS-1)] = {
  29. .desc = cpu_to_be32(0xd397d781), /* "LpPa" */
  30. .size = cpu_to_be16(sizeof(struct lppaca)),
  31. .fpregs_in_use = 1,
  32. .slb_count = cpu_to_be16(64),
  33. .vmxregs_in_use = 0,
  34. .page_ins = 0,
  35. },
  36. };
  37. static struct lppaca *extra_lppacas;
  38. static long __initdata lppaca_size;
  39. static void __init allocate_lppacas(int nr_cpus, unsigned long limit)
  40. {
  41. if (nr_cpus <= NR_LPPACAS)
  42. return;
  43. lppaca_size = PAGE_ALIGN(sizeof(struct lppaca) *
  44. (nr_cpus - NR_LPPACAS));
  45. extra_lppacas = __va(memblock_alloc_base(lppaca_size,
  46. PAGE_SIZE, limit));
  47. }
  48. static struct lppaca * __init new_lppaca(int cpu)
  49. {
  50. struct lppaca *lp;
  51. if (cpu < NR_LPPACAS)
  52. return &lppaca[cpu];
  53. lp = extra_lppacas + (cpu - NR_LPPACAS);
  54. *lp = lppaca[0];
  55. return lp;
  56. }
  57. static void __init free_lppacas(void)
  58. {
  59. long new_size = 0, nr;
  60. if (!lppaca_size)
  61. return;
  62. nr = num_possible_cpus() - NR_LPPACAS;
  63. if (nr > 0)
  64. new_size = PAGE_ALIGN(nr * sizeof(struct lppaca));
  65. if (new_size >= lppaca_size)
  66. return;
  67. memblock_free(__pa(extra_lppacas) + new_size, lppaca_size - new_size);
  68. lppaca_size = new_size;
  69. }
  70. #else
  71. static inline void allocate_lppacas(int nr_cpus, unsigned long limit) { }
  72. static inline void free_lppacas(void) { }
  73. #endif /* CONFIG_PPC_BOOK3S */
  74. #ifdef CONFIG_PPC_STD_MMU_64
  75. /*
  76. * 3 persistent SLBs are registered here. The buffer will be zero
  77. * initially, hence will all be invaild until we actually write them.
  78. *
  79. * If you make the number of persistent SLB entries dynamic, please also
  80. * update PR KVM to flush and restore them accordingly.
  81. */
  82. static struct slb_shadow *slb_shadow;
  83. static void __init allocate_slb_shadows(int nr_cpus, int limit)
  84. {
  85. int size = PAGE_ALIGN(sizeof(struct slb_shadow) * nr_cpus);
  86. slb_shadow = __va(memblock_alloc_base(size, PAGE_SIZE, limit));
  87. memset(slb_shadow, 0, size);
  88. }
  89. static struct slb_shadow * __init init_slb_shadow(int cpu)
  90. {
  91. struct slb_shadow *s = &slb_shadow[cpu];
  92. /*
  93. * When we come through here to initialise boot_paca, the slb_shadow
  94. * buffers are not allocated yet. That's OK, we'll get one later in
  95. * boot, but make sure we don't corrupt memory at 0.
  96. */
  97. if (!slb_shadow)
  98. return NULL;
  99. s->persistent = cpu_to_be32(SLB_NUM_BOLTED);
  100. s->buffer_length = cpu_to_be32(sizeof(*s));
  101. return s;
  102. }
  103. #else /* CONFIG_PPC_STD_MMU_64 */
  104. static void __init allocate_slb_shadows(int nr_cpus, int limit) { }
  105. #endif /* CONFIG_PPC_STD_MMU_64 */
  106. /* The Paca is an array with one entry per processor. Each contains an
  107. * lppaca, which contains the information shared between the
  108. * hypervisor and Linux.
  109. * On systems with hardware multi-threading, there are two threads
  110. * per processor. The Paca array must contain an entry for each thread.
  111. * The VPD Areas will give a max logical processors = 2 * max physical
  112. * processors. The processor VPD array needs one entry per physical
  113. * processor (not thread).
  114. */
  115. struct paca_struct *paca;
  116. EXPORT_SYMBOL(paca);
  117. void __init initialise_paca(struct paca_struct *new_paca, int cpu)
  118. {
  119. #ifdef CONFIG_PPC_BOOK3S
  120. new_paca->lppaca_ptr = new_lppaca(cpu);
  121. #else
  122. new_paca->kernel_pgd = swapper_pg_dir;
  123. #endif
  124. new_paca->lock_token = 0x8000;
  125. new_paca->paca_index = cpu;
  126. new_paca->kernel_toc = kernel_toc_addr();
  127. new_paca->kernelbase = (unsigned long) _stext;
  128. /* Only set MSR:IR/DR when MMU is initialized */
  129. new_paca->kernel_msr = MSR_KERNEL & ~(MSR_IR | MSR_DR);
  130. new_paca->hw_cpu_id = 0xffff;
  131. new_paca->kexec_state = KEXEC_STATE_NONE;
  132. new_paca->__current = &init_task;
  133. new_paca->data_offset = 0xfeeeeeeeeeeeeeeeULL;
  134. #ifdef CONFIG_PPC_STD_MMU_64
  135. new_paca->slb_shadow_ptr = init_slb_shadow(cpu);
  136. #endif /* CONFIG_PPC_STD_MMU_64 */
  137. #ifdef CONFIG_PPC_BOOK3E
  138. /* For now -- if we have threads this will be adjusted later */
  139. new_paca->tcd_ptr = &new_paca->tcd;
  140. #endif
  141. }
  142. /* Put the paca pointer into r13 and SPRG_PACA */
  143. void setup_paca(struct paca_struct *new_paca)
  144. {
  145. /* Setup r13 */
  146. local_paca = new_paca;
  147. #ifdef CONFIG_PPC_BOOK3E
  148. /* On Book3E, initialize the TLB miss exception frames */
  149. mtspr(SPRN_SPRG_TLB_EXFRAME, local_paca->extlb);
  150. #else
  151. /* In HV mode, we setup both HPACA and PACA to avoid problems
  152. * if we do a GET_PACA() before the feature fixups have been
  153. * applied
  154. */
  155. if (early_cpu_has_feature(CPU_FTR_HVMODE))
  156. mtspr(SPRN_SPRG_HPACA, local_paca);
  157. #endif
  158. mtspr(SPRN_SPRG_PACA, local_paca);
  159. }
  160. static int __initdata paca_size;
  161. void __init allocate_pacas(void)
  162. {
  163. u64 limit;
  164. int cpu;
  165. limit = ppc64_rma_size;
  166. #ifdef CONFIG_PPC_BOOK3S_64
  167. /*
  168. * We can't take SLB misses on the paca, and we want to access them
  169. * in real mode, so allocate them within the RMA and also within
  170. * the first segment.
  171. */
  172. limit = min(0x10000000ULL, limit);
  173. #endif
  174. paca_size = PAGE_ALIGN(sizeof(struct paca_struct) * nr_cpu_ids);
  175. paca = __va(memblock_alloc_base(paca_size, PAGE_SIZE, limit));
  176. memset(paca, 0, paca_size);
  177. printk(KERN_DEBUG "Allocated %u bytes for %d pacas at %p\n",
  178. paca_size, nr_cpu_ids, paca);
  179. allocate_lppacas(nr_cpu_ids, limit);
  180. allocate_slb_shadows(nr_cpu_ids, limit);
  181. /* Can't use for_each_*_cpu, as they aren't functional yet */
  182. for (cpu = 0; cpu < nr_cpu_ids; cpu++)
  183. initialise_paca(&paca[cpu], cpu);
  184. }
  185. void __init free_unused_pacas(void)
  186. {
  187. int new_size;
  188. new_size = PAGE_ALIGN(sizeof(struct paca_struct) * nr_cpu_ids);
  189. if (new_size >= paca_size)
  190. return;
  191. memblock_free(__pa(paca) + new_size, paca_size - new_size);
  192. printk(KERN_DEBUG "Freed %u bytes for unused pacas\n",
  193. paca_size - new_size);
  194. paca_size = new_size;
  195. free_lppacas();
  196. }