Merge branch 'upstream/xen-settime' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / arch / powerpc / kvm / book3s_hv_builtin.c
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/export.h>
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
21 /*
22  * This maintains a list of RMAs (real mode areas) for KVM guests to use.
23  * Each RMA has to be physically contiguous and of a size that the
24  * hardware supports.  PPC970 and POWER7 support 64MB, 128MB and 256MB,
25  * and other larger sizes.  Since we are unlikely to be allocate that
26  * much physically contiguous memory after the system is up and running,
27  * we preallocate a set of RMAs in early boot for KVM to use.
28  */
29 static unsigned long kvm_rma_size = 64 << 20;   /* 64MB */
30 static unsigned long kvm_rma_count;
31
32 static int __init early_parse_rma_size(char *p)
33 {
34         if (!p)
35                 return 1;
36
37         kvm_rma_size = memparse(p, &p);
38
39         return 0;
40 }
41 early_param("kvm_rma_size", early_parse_rma_size);
42
43 static int __init early_parse_rma_count(char *p)
44 {
45         if (!p)
46                 return 1;
47
48         kvm_rma_count = simple_strtoul(p, NULL, 0);
49
50         return 0;
51 }
52 early_param("kvm_rma_count", early_parse_rma_count);
53
54 static struct kvmppc_rma_info *rma_info;
55 static LIST_HEAD(free_rmas);
56 static DEFINE_SPINLOCK(rma_lock);
57
58 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
59    Assumes POWER7 or PPC970. */
60 static inline int lpcr_rmls(unsigned long rma_size)
61 {
62         switch (rma_size) {
63         case 32ul << 20:        /* 32 MB */
64                 if (cpu_has_feature(CPU_FTR_ARCH_206))
65                         return 8;       /* only supported on POWER7 */
66                 return -1;
67         case 64ul << 20:        /* 64 MB */
68                 return 3;
69         case 128ul << 20:       /* 128 MB */
70                 return 7;
71         case 256ul << 20:       /* 256 MB */
72                 return 4;
73         case 1ul << 30:         /* 1 GB */
74                 return 2;
75         case 16ul << 30:        /* 16 GB */
76                 return 1;
77         case 256ul << 30:       /* 256 GB */
78                 return 0;
79         default:
80                 return -1;
81         }
82 }
83
84 /*
85  * Called at boot time while the bootmem allocator is active,
86  * to allocate contiguous physical memory for the real memory
87  * areas for guests.
88  */
89 void kvm_rma_init(void)
90 {
91         unsigned long i;
92         unsigned long j, npages;
93         void *rma;
94         struct page *pg;
95
96         /* Only do this on PPC970 in HV mode */
97         if (!cpu_has_feature(CPU_FTR_HVMODE) ||
98             !cpu_has_feature(CPU_FTR_ARCH_201))
99                 return;
100
101         if (!kvm_rma_size || !kvm_rma_count)
102                 return;
103
104         /* Check that the requested size is one supported in hardware */
105         if (lpcr_rmls(kvm_rma_size) < 0) {
106                 pr_err("RMA size of 0x%lx not supported\n", kvm_rma_size);
107                 return;
108         }
109
110         npages = kvm_rma_size >> PAGE_SHIFT;
111         rma_info = alloc_bootmem(kvm_rma_count * sizeof(struct kvmppc_rma_info));
112         for (i = 0; i < kvm_rma_count; ++i) {
113                 rma = alloc_bootmem_align(kvm_rma_size, kvm_rma_size);
114                 pr_info("Allocated KVM RMA at %p (%ld MB)\n", rma,
115                         kvm_rma_size >> 20);
116                 rma_info[i].base_virt = rma;
117                 rma_info[i].base_pfn = __pa(rma) >> PAGE_SHIFT;
118                 rma_info[i].npages = npages;
119                 list_add_tail(&rma_info[i].list, &free_rmas);
120                 atomic_set(&rma_info[i].use_count, 0);
121
122                 pg = pfn_to_page(rma_info[i].base_pfn);
123                 for (j = 0; j < npages; ++j) {
124                         atomic_inc(&pg->_count);
125                         ++pg;
126                 }
127         }
128 }
129
130 struct kvmppc_rma_info *kvm_alloc_rma(void)
131 {
132         struct kvmppc_rma_info *ri;
133
134         ri = NULL;
135         spin_lock(&rma_lock);
136         if (!list_empty(&free_rmas)) {
137                 ri = list_first_entry(&free_rmas, struct kvmppc_rma_info, list);
138                 list_del(&ri->list);
139                 atomic_inc(&ri->use_count);
140         }
141         spin_unlock(&rma_lock);
142         return ri;
143 }
144 EXPORT_SYMBOL_GPL(kvm_alloc_rma);
145
146 void kvm_release_rma(struct kvmppc_rma_info *ri)
147 {
148         if (atomic_dec_and_test(&ri->use_count)) {
149                 spin_lock(&rma_lock);
150                 list_add_tail(&ri->list, &free_rmas);
151                 spin_unlock(&rma_lock);
152
153         }
154 }
155 EXPORT_SYMBOL_GPL(kvm_release_rma);
156