/arch/powerpc/kernel/paca.c

http://github.com/mirrors/linux · C · 325 lines · 211 code · 55 blank · 59 comment · 18 complexity · 3e09536bf070d5a759333e74f335c840 MD5 · raw file

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