paca.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * c 2001 PPC 64 Team, IBM Corp
  4. */
  5. #include <linux/smp.h>
  6. #include <linux/export.h>
  7. #include <linux/memblock.h>
  8. #include <linux/sched/task.h>
  9. #include <linux/numa.h>
  10. #include <linux/pgtable.h>
  11. #include <asm/lppaca.h>
  12. #include <asm/paca.h>
  13. #include <asm/sections.h>
  14. #include <asm/kexec.h>
  15. #include <asm/svm.h>
  16. #include <asm/ultravisor.h>
  17. #include "setup.h"
  18. #ifndef CONFIG_SMP
  19. #define boot_cpuid 0
  20. #endif
  21. static void *__init alloc_paca_data(unsigned long size, unsigned long align,
  22. unsigned long limit, int cpu)
  23. {
  24. void *ptr;
  25. int nid;
  26. /*
  27. * boot_cpuid paca is allocated very early before cpu_to_node is up.
  28. * Set bottom-up mode, because the boot CPU should be on node-0,
  29. * which will put its paca in the right place.
  30. */
  31. if (cpu == boot_cpuid) {
  32. nid = NUMA_NO_NODE;
  33. memblock_set_bottom_up(true);
  34. } else {
  35. nid = early_cpu_to_node(cpu);
  36. }
  37. ptr = memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
  38. limit, nid);
  39. if (!ptr)
  40. panic("cannot allocate paca data");
  41. if (cpu == boot_cpuid)
  42. memblock_set_bottom_up(false);
  43. return ptr;
  44. }
  45. #ifdef CONFIG_PPC_PSERIES
  46. #define LPPACA_SIZE 0x400
  47. static void *__init alloc_shared_lppaca(unsigned long size, unsigned long limit,
  48. int cpu)
  49. {
  50. size_t shared_lppaca_total_size = PAGE_ALIGN(nr_cpu_ids * LPPACA_SIZE);
  51. static unsigned long shared_lppaca_size;
  52. static void *shared_lppaca;
  53. void *ptr;
  54. if (!shared_lppaca) {
  55. memblock_set_bottom_up(true);
  56. /*
  57. * See Documentation/arch/powerpc/ultravisor.rst for more details.
  58. *
  59. * UV/HV data sharing is in PAGE_SIZE granularity. In order to
  60. * minimize the number of pages shared, align the allocation to
  61. * PAGE_SIZE.
  62. */
  63. shared_lppaca =
  64. memblock_alloc_try_nid(shared_lppaca_total_size,
  65. PAGE_SIZE, MEMBLOCK_LOW_LIMIT,
  66. limit, NUMA_NO_NODE);
  67. if (!shared_lppaca)
  68. panic("cannot allocate shared data");
  69. memblock_set_bottom_up(false);
  70. uv_share_page(PHYS_PFN(__pa(shared_lppaca)),
  71. shared_lppaca_total_size >> PAGE_SHIFT);
  72. }
  73. ptr = shared_lppaca + shared_lppaca_size;
  74. shared_lppaca_size += size;
  75. /*
  76. * This is very early in boot, so no harm done if the kernel crashes at
  77. * this point.
  78. */
  79. BUG_ON(shared_lppaca_size > shared_lppaca_total_size);
  80. return ptr;
  81. }
  82. /*
  83. * See asm/lppaca.h for more detail.
  84. *
  85. * lppaca structures must must be 1kB in size, L1 cache line aligned,
  86. * and not cross 4kB boundary. A 1kB size and 1kB alignment will satisfy
  87. * these requirements.
  88. */
  89. static inline void init_lppaca(struct lppaca *lppaca)
  90. {
  91. BUILD_BUG_ON(sizeof(struct lppaca) != 640);
  92. *lppaca = (struct lppaca) {
  93. .desc = cpu_to_be32(0xd397d781), /* "LpPa" */
  94. .size = cpu_to_be16(LPPACA_SIZE),
  95. .fpregs_in_use = 1,
  96. .slb_count = cpu_to_be16(64),
  97. .vmxregs_in_use = 0,
  98. .page_ins = 0, };
  99. };
  100. static struct lppaca * __init new_lppaca(int cpu, unsigned long limit)
  101. {
  102. struct lppaca *lp;
  103. BUILD_BUG_ON(sizeof(struct lppaca) > LPPACA_SIZE);
  104. if (early_cpu_has_feature(CPU_FTR_HVMODE))
  105. return NULL;
  106. if (is_secure_guest())
  107. lp = alloc_shared_lppaca(LPPACA_SIZE, limit, cpu);
  108. else
  109. lp = alloc_paca_data(LPPACA_SIZE, 0x400, limit, cpu);
  110. init_lppaca(lp);
  111. return lp;
  112. }
  113. #endif /* CONFIG_PPC_PSERIES */
  114. #ifdef CONFIG_PPC_64S_HASH_MMU
  115. /*
  116. * 3 persistent SLBs are allocated here. The buffer will be zero
  117. * initially, hence will all be invaild until we actually write them.
  118. *
  119. * If you make the number of persistent SLB entries dynamic, please also
  120. * update PR KVM to flush and restore them accordingly.
  121. */
  122. static struct slb_shadow * __init new_slb_shadow(int cpu, unsigned long limit)
  123. {
  124. struct slb_shadow *s;
  125. if (cpu != boot_cpuid) {
  126. /*
  127. * Boot CPU comes here before early_radix_enabled
  128. * is parsed (e.g., for disable_radix). So allocate
  129. * always and this will be fixed up in free_unused_pacas.
  130. */
  131. if (early_radix_enabled())
  132. return NULL;
  133. }
  134. s = alloc_paca_data(sizeof(*s), L1_CACHE_BYTES, limit, cpu);
  135. s->persistent = cpu_to_be32(SLB_NUM_BOLTED);
  136. s->buffer_length = cpu_to_be32(sizeof(*s));
  137. return s;
  138. }
  139. #endif /* CONFIG_PPC_64S_HASH_MMU */
  140. /* The Paca is an array with one entry per processor. Each contains an
  141. * lppaca, which contains the information shared between the
  142. * hypervisor and Linux.
  143. * On systems with hardware multi-threading, there are two threads
  144. * per processor. The Paca array must contain an entry for each thread.
  145. * The VPD Areas will give a max logical processors = 2 * max physical
  146. * processors. The processor VPD array needs one entry per physical
  147. * processor (not thread).
  148. */
  149. struct paca_struct **paca_ptrs __read_mostly;
  150. EXPORT_SYMBOL(paca_ptrs);
  151. void __init initialise_paca(struct paca_struct *new_paca, int cpu)
  152. {
  153. #ifdef CONFIG_PPC_PSERIES
  154. new_paca->lppaca_ptr = NULL;
  155. #endif
  156. #ifdef CONFIG_PPC_BOOK3E_64
  157. new_paca->kernel_pgd = swapper_pg_dir;
  158. #endif
  159. new_paca->lock_token = 0x8000;
  160. new_paca->paca_index = cpu;
  161. #ifndef CONFIG_PPC_KERNEL_PCREL
  162. new_paca->kernel_toc = kernel_toc_addr();
  163. #endif
  164. new_paca->kernelbase = (unsigned long) _stext;
  165. /* Only set MSR:IR/DR when MMU is initialized */
  166. new_paca->kernel_msr = MSR_KERNEL & ~(MSR_IR | MSR_DR);
  167. new_paca->hw_cpu_id = 0xffff;
  168. new_paca->kexec_state = KEXEC_STATE_NONE;
  169. new_paca->__current = &init_task;
  170. new_paca->data_offset = 0xfeeeeeeeeeeeeeeeULL;
  171. #ifdef CONFIG_PPC_64S_HASH_MMU
  172. new_paca->slb_shadow_ptr = NULL;
  173. #endif
  174. #ifdef CONFIG_PPC_BOOK3E_64
  175. /* For now -- if we have threads this will be adjusted later */
  176. new_paca->tcd_ptr = &new_paca->tcd;
  177. #endif
  178. }
  179. /* Put the paca pointer into r13 and SPRG_PACA */
  180. void setup_paca(struct paca_struct *new_paca)
  181. {
  182. /* Setup r13 */
  183. local_paca = new_paca;
  184. #ifdef CONFIG_PPC_BOOK3E_64
  185. /* On Book3E, initialize the TLB miss exception frames */
  186. mtspr(SPRN_SPRG_TLB_EXFRAME, local_paca->extlb);
  187. #else
  188. /*
  189. * In HV mode, we setup both HPACA and PACA to avoid problems
  190. * if we do a GET_PACA() before the feature fixups have been
  191. * applied.
  192. *
  193. * Normally you should test against CPU_FTR_HVMODE, but CPU features
  194. * are not yet set up when we first reach here.
  195. */
  196. if (mfmsr() & MSR_HV)
  197. mtspr(SPRN_SPRG_HPACA, local_paca);
  198. #endif
  199. mtspr(SPRN_SPRG_PACA, local_paca);
  200. }
  201. static int __initdata paca_nr_cpu_ids;
  202. static int __initdata paca_ptrs_size;
  203. static int __initdata paca_struct_size;
  204. void __init allocate_paca_ptrs(void)
  205. {
  206. paca_nr_cpu_ids = nr_cpu_ids;
  207. paca_ptrs_size = sizeof(struct paca_struct *) * nr_cpu_ids;
  208. paca_ptrs = memblock_alloc_raw(paca_ptrs_size, SMP_CACHE_BYTES);
  209. if (!paca_ptrs)
  210. panic("Failed to allocate %d bytes for paca pointers\n",
  211. paca_ptrs_size);
  212. memset(paca_ptrs, 0x88, paca_ptrs_size);
  213. }
  214. void __init allocate_paca(int cpu)
  215. {
  216. u64 limit;
  217. struct paca_struct *paca;
  218. BUG_ON(cpu >= paca_nr_cpu_ids);
  219. #ifdef CONFIG_PPC_BOOK3S_64
  220. /*
  221. * We access pacas in real mode, and cannot take SLB faults
  222. * on them when in virtual mode, so allocate them accordingly.
  223. */
  224. limit = min(ppc64_bolted_size(), ppc64_rma_size);
  225. #else
  226. limit = ppc64_rma_size;
  227. #endif
  228. paca = alloc_paca_data(sizeof(struct paca_struct), L1_CACHE_BYTES,
  229. limit, cpu);
  230. paca_ptrs[cpu] = paca;
  231. initialise_paca(paca, cpu);
  232. #ifdef CONFIG_PPC_PSERIES
  233. paca->lppaca_ptr = new_lppaca(cpu, limit);
  234. #endif
  235. #ifdef CONFIG_PPC_64S_HASH_MMU
  236. paca->slb_shadow_ptr = new_slb_shadow(cpu, limit);
  237. #endif
  238. paca_struct_size += sizeof(struct paca_struct);
  239. }
  240. void __init free_unused_pacas(void)
  241. {
  242. int new_ptrs_size;
  243. new_ptrs_size = sizeof(struct paca_struct *) * nr_cpu_ids;
  244. if (new_ptrs_size < paca_ptrs_size)
  245. memblock_phys_free(__pa(paca_ptrs) + new_ptrs_size,
  246. paca_ptrs_size - new_ptrs_size);
  247. paca_nr_cpu_ids = nr_cpu_ids;
  248. paca_ptrs_size = new_ptrs_size;
  249. #ifdef CONFIG_PPC_64S_HASH_MMU
  250. if (early_radix_enabled()) {
  251. /* Ugly fixup, see new_slb_shadow() */
  252. memblock_phys_free(__pa(paca_ptrs[boot_cpuid]->slb_shadow_ptr),
  253. sizeof(struct slb_shadow));
  254. paca_ptrs[boot_cpuid]->slb_shadow_ptr = NULL;
  255. }
  256. #endif
  257. printk(KERN_DEBUG "Allocated %u bytes for %u pacas\n",
  258. paca_ptrs_size + paca_struct_size, nr_cpu_ids);
  259. }
  260. #ifdef CONFIG_PPC_64S_HASH_MMU
  261. void copy_mm_to_paca(struct mm_struct *mm)
  262. {
  263. mm_context_t *context = &mm->context;
  264. VM_BUG_ON(!mm_ctx_slb_addr_limit(context));
  265. memcpy(&get_paca()->mm_ctx_low_slices_psize, mm_ctx_low_slices(context),
  266. LOW_SLICE_ARRAY_SZ);
  267. memcpy(&get_paca()->mm_ctx_high_slices_psize, mm_ctx_high_slices(context),
  268. TASK_SLICE_ARRAY_SZ(context));
  269. }
  270. #endif /* CONFIG_PPC_64S_HASH_MMU */