]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/powerpc/kvm/book3s_hv_builtin.c
KVM: PPC: Allocate RMAs (Real Mode Areas) at boot for use by guests
[mirror_ubuntu-bionic-kernel.git] / arch / powerpc / kvm / book3s_hv_builtin.c
CommitLineData
aa04b4cc
PM
1/*
2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/kvm_host.h>
10#include <linux/preempt.h>
11#include <linux/sched.h>
12#include <linux/spinlock.h>
13#include <linux/bootmem.h>
14#include <linux/init.h>
15
16#include <asm/cputable.h>
17#include <asm/kvm_ppc.h>
18#include <asm/kvm_book3s.h>
19
20/*
21 * This maintains a list of RMAs (real mode areas) for KVM guests to use.
22 * Each RMA has to be physically contiguous and of a size that the
23 * hardware supports. PPC970 and POWER7 support 64MB, 128MB and 256MB,
24 * and other larger sizes. Since we are unlikely to be allocate that
25 * much physically contiguous memory after the system is up and running,
26 * we preallocate a set of RMAs in early boot for KVM to use.
27 */
28static unsigned long kvm_rma_size = 64 << 20; /* 64MB */
29static unsigned long kvm_rma_count;
30
31static int __init early_parse_rma_size(char *p)
32{
33 if (!p)
34 return 1;
35
36 kvm_rma_size = memparse(p, &p);
37
38 return 0;
39}
40early_param("kvm_rma_size", early_parse_rma_size);
41
42static int __init early_parse_rma_count(char *p)
43{
44 if (!p)
45 return 1;
46
47 kvm_rma_count = simple_strtoul(p, NULL, 0);
48
49 return 0;
50}
51early_param("kvm_rma_count", early_parse_rma_count);
52
53static struct kvmppc_rma_info *rma_info;
54static LIST_HEAD(free_rmas);
55static DEFINE_SPINLOCK(rma_lock);
56
57/* Work out RMLS (real mode limit selector) field value for a given RMA size.
58 Assumes POWER7. */
59static inline int lpcr_rmls(unsigned long rma_size)
60{
61 switch (rma_size) {
62 case 32ul << 20: /* 32 MB */
63 return 8;
64 case 64ul << 20: /* 64 MB */
65 return 3;
66 case 128ul << 20: /* 128 MB */
67 return 7;
68 case 256ul << 20: /* 256 MB */
69 return 4;
70 case 1ul << 30: /* 1 GB */
71 return 2;
72 case 16ul << 30: /* 16 GB */
73 return 1;
74 case 256ul << 30: /* 256 GB */
75 return 0;
76 default:
77 return -1;
78 }
79}
80
81/*
82 * Called at boot time while the bootmem allocator is active,
83 * to allocate contiguous physical memory for the real memory
84 * areas for guests.
85 */
86void kvm_rma_init(void)
87{
88 unsigned long i;
89 unsigned long j, npages;
90 void *rma;
91 struct page *pg;
92
93 /* Only do this on POWER7 in HV mode */
94 if (!cpu_has_feature(CPU_FTR_HVMODE_206))
95 return;
96
97 if (!kvm_rma_size || !kvm_rma_count)
98 return;
99
100 /* Check that the requested size is one supported in hardware */
101 if (lpcr_rmls(kvm_rma_size) < 0) {
102 pr_err("RMA size of 0x%lx not supported\n", kvm_rma_size);
103 return;
104 }
105
106 npages = kvm_rma_size >> PAGE_SHIFT;
107 rma_info = alloc_bootmem(kvm_rma_count * sizeof(struct kvmppc_rma_info));
108 for (i = 0; i < kvm_rma_count; ++i) {
109 rma = alloc_bootmem_align(kvm_rma_size, kvm_rma_size);
110 pr_info("Allocated KVM RMA at %p (%ld MB)\n", rma,
111 kvm_rma_size >> 20);
112 rma_info[i].base_virt = rma;
113 rma_info[i].base_pfn = __pa(rma) >> PAGE_SHIFT;
114 rma_info[i].npages = npages;
115 list_add_tail(&rma_info[i].list, &free_rmas);
116 atomic_set(&rma_info[i].use_count, 0);
117
118 pg = pfn_to_page(rma_info[i].base_pfn);
119 for (j = 0; j < npages; ++j) {
120 atomic_inc(&pg->_count);
121 ++pg;
122 }
123 }
124}
125
126struct kvmppc_rma_info *kvm_alloc_rma(void)
127{
128 struct kvmppc_rma_info *ri;
129
130 ri = NULL;
131 spin_lock(&rma_lock);
132 if (!list_empty(&free_rmas)) {
133 ri = list_first_entry(&free_rmas, struct kvmppc_rma_info, list);
134 list_del(&ri->list);
135 atomic_inc(&ri->use_count);
136 }
137 spin_unlock(&rma_lock);
138 return ri;
139}
140EXPORT_SYMBOL_GPL(kvm_alloc_rma);
141
142void kvm_release_rma(struct kvmppc_rma_info *ri)
143{
144 if (atomic_dec_and_test(&ri->use_count)) {
145 spin_lock(&rma_lock);
146 list_add_tail(&ri->list, &free_rmas);
147 spin_unlock(&rma_lock);
148
149 }
150}
151EXPORT_SYMBOL_GPL(kvm_release_rma);
152