]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/powerpc/kvm/book3s_hv_builtin.c
KVM: PPC: Initialize linears with zeros
[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>
66b15db6 11#include <linux/export.h>
aa04b4cc
PM
12#include <linux/sched.h>
13#include <linux/spinlock.h>
14#include <linux/bootmem.h>
15#include <linux/init.h>
16
17#include <asm/cputable.h>
18#include <asm/kvm_ppc.h>
19#include <asm/kvm_book3s.h>
20
b4e70611
AG
21#define KVM_LINEAR_RMA 0
22
23static void __init kvm_linear_init_one(ulong size, int count, int type);
24static struct kvmppc_linear_info *kvm_alloc_linear(int type);
25static void kvm_release_linear(struct kvmppc_linear_info *ri);
26
27/*************** RMA *************/
28
aa04b4cc
PM
29/*
30 * This maintains a list of RMAs (real mode areas) for KVM guests to use.
31 * Each RMA has to be physically contiguous and of a size that the
32 * hardware supports. PPC970 and POWER7 support 64MB, 128MB and 256MB,
33 * and other larger sizes. Since we are unlikely to be allocate that
34 * much physically contiguous memory after the system is up and running,
35 * we preallocate a set of RMAs in early boot for KVM to use.
36 */
37static unsigned long kvm_rma_size = 64 << 20; /* 64MB */
38static unsigned long kvm_rma_count;
39
aa04b4cc 40/* Work out RMLS (real mode limit selector) field value for a given RMA size.
9e368f29 41 Assumes POWER7 or PPC970. */
aa04b4cc
PM
42static inline int lpcr_rmls(unsigned long rma_size)
43{
44 switch (rma_size) {
45 case 32ul << 20: /* 32 MB */
9e368f29
PM
46 if (cpu_has_feature(CPU_FTR_ARCH_206))
47 return 8; /* only supported on POWER7 */
48 return -1;
aa04b4cc
PM
49 case 64ul << 20: /* 64 MB */
50 return 3;
51 case 128ul << 20: /* 128 MB */
52 return 7;
53 case 256ul << 20: /* 256 MB */
54 return 4;
55 case 1ul << 30: /* 1 GB */
56 return 2;
57 case 16ul << 30: /* 16 GB */
58 return 1;
59 case 256ul << 30: /* 256 GB */
60 return 0;
61 default:
62 return -1;
63 }
64}
65
b4e70611
AG
66static int __init early_parse_rma_size(char *p)
67{
68 if (!p)
69 return 1;
70
71 kvm_rma_size = memparse(p, &p);
72
73 return 0;
74}
75early_param("kvm_rma_size", early_parse_rma_size);
76
77static int __init early_parse_rma_count(char *p)
78{
79 if (!p)
80 return 1;
81
82 kvm_rma_count = simple_strtoul(p, NULL, 0);
83
84 return 0;
85}
86early_param("kvm_rma_count", early_parse_rma_count);
87
88struct kvmppc_linear_info *kvm_alloc_rma(void)
89{
90 return kvm_alloc_linear(KVM_LINEAR_RMA);
91}
92EXPORT_SYMBOL_GPL(kvm_alloc_rma);
93
94void kvm_release_rma(struct kvmppc_linear_info *ri)
95{
96 kvm_release_linear(ri);
97}
98EXPORT_SYMBOL_GPL(kvm_release_rma);
99
100/*************** generic *************/
101
102static LIST_HEAD(free_linears);
103static DEFINE_SPINLOCK(linear_lock);
104
105static void __init kvm_linear_init_one(ulong size, int count, int type)
aa04b4cc
PM
106{
107 unsigned long i;
108 unsigned long j, npages;
b4e70611 109 void *linear;
aa04b4cc 110 struct page *pg;
b4e70611
AG
111 const char *typestr;
112 struct kvmppc_linear_info *linear_info;
aa04b4cc 113
b4e70611 114 if (!count)
aa04b4cc
PM
115 return;
116
b4e70611
AG
117 typestr = (type == KVM_LINEAR_RMA) ? "RMA" : "";
118
119 npages = size >> PAGE_SHIFT;
120 linear_info = alloc_bootmem(count * sizeof(struct kvmppc_linear_info));
121 for (i = 0; i < count; ++i) {
122 linear = alloc_bootmem_align(size, size);
123 pr_info("Allocated KVM %s at %p (%ld MB)\n", typestr, linear,
124 size >> 20);
125 linear_info[i].base_virt = linear;
126 linear_info[i].base_pfn = __pa(linear) >> PAGE_SHIFT;
127 linear_info[i].npages = npages;
128 linear_info[i].type = type;
129 list_add_tail(&linear_info[i].list, &free_linears);
130 atomic_set(&linear_info[i].use_count, 0);
131
132 pg = pfn_to_page(linear_info[i].base_pfn);
aa04b4cc
PM
133 for (j = 0; j < npages; ++j) {
134 atomic_inc(&pg->_count);
135 ++pg;
136 }
137 }
138}
139
b4e70611 140static struct kvmppc_linear_info *kvm_alloc_linear(int type)
aa04b4cc 141{
b4e70611 142 struct kvmppc_linear_info *ri;
aa04b4cc
PM
143
144 ri = NULL;
b4e70611
AG
145 spin_lock(&linear_lock);
146 list_for_each_entry(ri, &free_linears, list) {
147 if (ri->type != type)
148 continue;
149
aa04b4cc
PM
150 list_del(&ri->list);
151 atomic_inc(&ri->use_count);
b4e70611 152 break;
aa04b4cc 153 }
b4e70611 154 spin_unlock(&linear_lock);
b7f5d011 155 memset(ri->base_virt, 0, ri->npages << PAGE_SHIFT);
aa04b4cc
PM
156 return ri;
157}
aa04b4cc 158
b4e70611 159static void kvm_release_linear(struct kvmppc_linear_info *ri)
aa04b4cc
PM
160{
161 if (atomic_dec_and_test(&ri->use_count)) {
b4e70611
AG
162 spin_lock(&linear_lock);
163 list_add_tail(&ri->list, &free_linears);
164 spin_unlock(&linear_lock);
aa04b4cc
PM
165
166 }
167}
aa04b4cc 168
b4e70611
AG
169/*
170 * Called at boot time while the bootmem allocator is active,
171 * to allocate contiguous physical memory for the hash page
172 * tables for guests.
173 */
174void __init kvm_linear_init(void)
175{
176 /* RMA */
177 /* Only do this on PPC970 in HV mode */
178 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
179 !cpu_has_feature(CPU_FTR_ARCH_201))
180 return;
181
182 if (!kvm_rma_size || !kvm_rma_count)
183 return;
184
185 /* Check that the requested size is one supported in hardware */
186 if (lpcr_rmls(kvm_rma_size) < 0) {
187 pr_err("RMA size of 0x%lx not supported\n", kvm_rma_size);
188 return;
189 }
190
191 kvm_linear_init_one(kvm_rma_size, kvm_rma_count, KVM_LINEAR_RMA);
192}