paca.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/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. new_paca->kernel_toc = kernel_toc_addr();
  162. new_paca->kernelbase = (unsigned long) _stext;
  163. /* Only set MSR:IR/DR when MMU is initialized */
  164. new_paca->kernel_msr = MSR_KERNEL & ~(MSR_IR | MSR_DR);
  165. new_paca->hw_cpu_id = 0xffff;
  166. new_paca->kexec_state = KEXEC_STATE_NONE;
  167. new_paca->__current = &init_task;
  168. new_paca->data_offset = 0xfeeeeeeeeeeeeeeeULL;
  169. #ifdef CONFIG_PPC_64S_HASH_MMU
  170. new_paca->slb_shadow_ptr = NULL;
  171. #endif
  172. #ifdef CONFIG_PPC_BOOK3E_64
  173. /* For now -- if we have threads this will be adjusted later */
  174. new_paca->tcd_ptr = &new_paca->tcd;
  175. #endif
  176. }
  177. /* Put the paca pointer into r13 and SPRG_PACA */
  178. void setup_paca(struct paca_struct *new_paca)
  179. {
  180. /* Setup r13 */
  181. local_paca = new_paca;
  182. #ifdef CONFIG_PPC_BOOK3E_64
  183. /* On Book3E, initialize the TLB miss exception frames */
  184. mtspr(SPRN_SPRG_TLB_EXFRAME, local_paca->extlb);
  185. #else
  186. /*
  187. * In HV mode, we setup both HPACA and PACA to avoid problems
  188. * if we do a GET_PACA() before the feature fixups have been
  189. * applied.
  190. *
  191. * Normally you should test against CPU_FTR_HVMODE, but CPU features
  192. * are not yet set up when we first reach here.
  193. */
  194. if (mfmsr() & MSR_HV)
  195. mtspr(SPRN_SPRG_HPACA, local_paca);
  196. #endif
  197. mtspr(SPRN_SPRG_PACA, local_paca);
  198. }
  199. static int __initdata paca_nr_cpu_ids;
  200. static int __initdata paca_ptrs_size;
  201. static int __initdata paca_struct_size;
  202. void __init allocate_paca_ptrs(void)
  203. {
  204. paca_nr_cpu_ids = nr_cpu_ids;
  205. paca_ptrs_size = sizeof(struct paca_struct *) * nr_cpu_ids;
  206. paca_ptrs = memblock_alloc_raw(paca_ptrs_size, SMP_CACHE_BYTES);
  207. if (!paca_ptrs)
  208. panic("Failed to allocate %d bytes for paca pointers\n",
  209. paca_ptrs_size);
  210. memset(paca_ptrs, 0x88, paca_ptrs_size);
  211. }
  212. void __init allocate_paca(int cpu)
  213. {
  214. u64 limit;
  215. struct paca_struct *paca;
  216. BUG_ON(cpu >= paca_nr_cpu_ids);
  217. #ifdef CONFIG_PPC_BOOK3S_64
  218. /*
  219. * We access pacas in real mode, and cannot take SLB faults
  220. * on them when in virtual mode, so allocate them accordingly.
  221. */
  222. limit = min(ppc64_bolted_size(), ppc64_rma_size);
  223. #else
  224. limit = ppc64_rma_size;
  225. #endif
  226. paca = alloc_paca_data(sizeof(struct paca_struct), L1_CACHE_BYTES,
  227. limit, cpu);
  228. paca_ptrs[cpu] = paca;
  229. initialise_paca(paca, cpu);
  230. #ifdef CONFIG_PPC_PSERIES
  231. paca->lppaca_ptr = new_lppaca(cpu, limit);
  232. #endif
  233. #ifdef CONFIG_PPC_64S_HASH_MMU
  234. paca->slb_shadow_ptr = new_slb_shadow(cpu, limit);
  235. #endif
  236. paca_struct_size += sizeof(struct paca_struct);
  237. }
  238. void __init free_unused_pacas(void)
  239. {
  240. int new_ptrs_size;
  241. new_ptrs_size = sizeof(struct paca_struct *) * nr_cpu_ids;
  242. if (new_ptrs_size < paca_ptrs_size)
  243. memblock_phys_free(__pa(paca_ptrs) + new_ptrs_size,
  244. paca_ptrs_size - new_ptrs_size);
  245. paca_nr_cpu_ids = nr_cpu_ids;
  246. paca_ptrs_size = new_ptrs_size;
  247. #ifdef CONFIG_PPC_64S_HASH_MMU
  248. if (early_radix_enabled()) {
  249. /* Ugly fixup, see new_slb_shadow() */
  250. memblock_phys_free(__pa(paca_ptrs[boot_cpuid]->slb_shadow_ptr),
  251. sizeof(struct slb_shadow));
  252. paca_ptrs[boot_cpuid]->slb_shadow_ptr = NULL;
  253. }
  254. #endif
  255. printk(KERN_DEBUG "Allocated %u bytes for %u pacas\n",
  256. paca_ptrs_size + paca_struct_size, nr_cpu_ids);
  257. }
  258. #ifdef CONFIG_PPC_64S_HASH_MMU
  259. void copy_mm_to_paca(struct mm_struct *mm)
  260. {
  261. mm_context_t *context = &mm->context;
  262. VM_BUG_ON(!mm_ctx_slb_addr_limit(context));
  263. memcpy(&get_paca()->mm_ctx_low_slices_psize, mm_ctx_low_slices(context),
  264. LOW_SLICE_ARRAY_SZ);
  265. memcpy(&get_paca()->mm_ctx_high_slices_psize, mm_ctx_high_slices(context),
  266. TASK_SLICE_ARRAY_SZ(context));
  267. }
  268. #endif /* CONFIG_PPC_64S_HASH_MMU */