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