xen: allow balloon driver to use more than one memory region
[pandora-kernel.git] / arch / x86 / xen / setup.c
1 /*
2  * Machine specific setup for xen
3  *
4  * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
5  */
6
7 #include <linux/module.h>
8 #include <linux/sched.h>
9 #include <linux/mm.h>
10 #include <linux/pm.h>
11 #include <linux/memblock.h>
12 #include <linux/cpuidle.h>
13
14 #include <asm/elf.h>
15 #include <asm/vdso.h>
16 #include <asm/e820.h>
17 #include <asm/setup.h>
18 #include <asm/acpi.h>
19 #include <asm/xen/hypervisor.h>
20 #include <asm/xen/hypercall.h>
21
22 #include <xen/xen.h>
23 #include <xen/page.h>
24 #include <xen/interface/callback.h>
25 #include <xen/interface/memory.h>
26 #include <xen/interface/physdev.h>
27 #include <xen/features.h>
28
29 #include "xen-ops.h"
30 #include "vdso.h"
31
32 /* These are code, but not functions.  Defined in entry.S */
33 extern const char xen_hypervisor_callback[];
34 extern const char xen_failsafe_callback[];
35 extern void xen_sysenter_target(void);
36 extern void xen_syscall_target(void);
37 extern void xen_syscall32_target(void);
38
39 /* Amount of extra memory space we add to the e820 ranges */
40 struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
41
42 /* Number of pages released from the initial allocation. */
43 unsigned long xen_released_pages;
44
45 /* 
46  * The maximum amount of extra memory compared to the base size.  The
47  * main scaling factor is the size of struct page.  At extreme ratios
48  * of base:extra, all the base memory can be filled with page
49  * structures for the extra memory, leaving no space for anything
50  * else.
51  * 
52  * 10x seems like a reasonable balance between scaling flexibility and
53  * leaving a practically usable system.
54  */
55 #define EXTRA_MEM_RATIO         (10)
56
57 static void __init xen_add_extra_mem(unsigned long pages)
58 {
59         unsigned long pfn;
60
61         u64 size = (u64)pages * PAGE_SIZE;
62         u64 extra_start = xen_extra_mem[0].start + xen_extra_mem[0].size;
63
64         if (!pages)
65                 return;
66
67         e820_add_region(extra_start, size, E820_RAM);
68         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
69
70         memblock_x86_reserve_range(extra_start, extra_start + size, "XEN EXTRA");
71
72         xen_extra_mem[0].size += size;
73
74         xen_max_p2m_pfn = PFN_DOWN(extra_start + size);
75
76         for (pfn = PFN_DOWN(extra_start); pfn <= xen_max_p2m_pfn; pfn++)
77                 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
78 }
79
80 static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
81                                               phys_addr_t end_addr)
82 {
83         struct xen_memory_reservation reservation = {
84                 .address_bits = 0,
85                 .extent_order = 0,
86                 .domid        = DOMID_SELF
87         };
88         unsigned long start, end;
89         unsigned long len = 0;
90         unsigned long pfn;
91         int ret;
92
93         start = PFN_UP(start_addr);
94         end = PFN_DOWN(end_addr);
95
96         if (end <= start)
97                 return 0;
98
99         for(pfn = start; pfn < end; pfn++) {
100                 unsigned long mfn = pfn_to_mfn(pfn);
101
102                 /* Make sure pfn exists to start with */
103                 if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
104                         continue;
105
106                 set_xen_guest_handle(reservation.extent_start, &mfn);
107                 reservation.nr_extents = 1;
108
109                 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
110                                            &reservation);
111                 WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret);
112                 if (ret == 1) {
113                         __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
114                         len++;
115                 }
116         }
117         printk(KERN_INFO "Freeing  %lx-%lx pfn range: %lu pages freed\n",
118                start, end, len);
119
120         return len;
121 }
122
123 static unsigned long __init xen_return_unused_memory(unsigned long max_pfn,
124                                                      const struct e820map *e820)
125 {
126         phys_addr_t max_addr = PFN_PHYS(max_pfn);
127         phys_addr_t last_end = ISA_END_ADDRESS;
128         unsigned long released = 0;
129         int i;
130
131         /* Free any unused memory above the low 1Mbyte. */
132         for (i = 0; i < e820->nr_map && last_end < max_addr; i++) {
133                 phys_addr_t end = e820->map[i].addr;
134                 end = min(max_addr, end);
135
136                 if (last_end < end)
137                         released += xen_release_chunk(last_end, end);
138                 last_end = max(last_end, e820->map[i].addr + e820->map[i].size);
139         }
140
141         if (last_end < max_addr)
142                 released += xen_release_chunk(last_end, max_addr);
143
144         printk(KERN_INFO "released %lu pages of unused memory\n", released);
145         return released;
146 }
147
148 static unsigned long __init xen_set_identity(const struct e820entry *list,
149                                              ssize_t map_size)
150 {
151         phys_addr_t last = xen_initial_domain() ? 0 : ISA_END_ADDRESS;
152         phys_addr_t start_pci = last;
153         const struct e820entry *entry;
154         unsigned long identity = 0;
155         int i;
156
157         for (i = 0, entry = list; i < map_size; i++, entry++) {
158                 phys_addr_t start = entry->addr;
159                 phys_addr_t end = start + entry->size;
160
161                 if (start < last)
162                         start = last;
163
164                 if (end <= start)
165                         continue;
166
167                 /* Skip over the 1MB region. */
168                 if (last > end)
169                         continue;
170
171                 if ((entry->type == E820_RAM) || (entry->type == E820_UNUSABLE)) {
172                         if (start > start_pci)
173                                 identity += set_phys_range_identity(
174                                                 PFN_UP(start_pci), PFN_DOWN(start));
175
176                         /* Without saving 'last' we would gooble RAM too
177                          * at the end of the loop. */
178                         last = end;
179                         start_pci = end;
180                         continue;
181                 }
182                 start_pci = min(start, start_pci);
183                 last = end;
184         }
185         if (last > start_pci)
186                 identity += set_phys_range_identity(
187                                         PFN_UP(start_pci), PFN_DOWN(last));
188         return identity;
189 }
190
191 static unsigned long __init xen_get_max_pages(void)
192 {
193         unsigned long max_pages = MAX_DOMAIN_PAGES;
194         domid_t domid = DOMID_SELF;
195         int ret;
196
197         ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
198         if (ret > 0)
199                 max_pages = ret;
200         return min(max_pages, MAX_DOMAIN_PAGES);
201 }
202
203 /**
204  * machine_specific_memory_setup - Hook for machine specific memory setup.
205  **/
206 char * __init xen_memory_setup(void)
207 {
208         static struct e820entry map[E820MAX] __initdata;
209         static struct e820entry map_raw[E820MAX] __initdata;
210
211         unsigned long max_pfn = xen_start_info->nr_pages;
212         unsigned long long mem_end;
213         int rc;
214         struct xen_memory_map memmap;
215         unsigned long extra_pages = 0;
216         unsigned long extra_limit;
217         unsigned long identity_pages = 0;
218         int i;
219         int op;
220
221         max_pfn = min(MAX_DOMAIN_PAGES, max_pfn);
222         mem_end = PFN_PHYS(max_pfn);
223
224         memmap.nr_entries = E820MAX;
225         set_xen_guest_handle(memmap.buffer, map);
226
227         op = xen_initial_domain() ?
228                 XENMEM_machine_memory_map :
229                 XENMEM_memory_map;
230         rc = HYPERVISOR_memory_op(op, &memmap);
231         if (rc == -ENOSYS) {
232                 BUG_ON(xen_initial_domain());
233                 memmap.nr_entries = 1;
234                 map[0].addr = 0ULL;
235                 map[0].size = mem_end;
236                 /* 8MB slack (to balance backend allocations). */
237                 map[0].size += 8ULL << 20;
238                 map[0].type = E820_RAM;
239                 rc = 0;
240         }
241         BUG_ON(rc);
242
243         memcpy(map_raw, map, sizeof(map));
244         e820.nr_map = 0;
245         xen_extra_mem[0].start = mem_end;
246         for (i = 0; i < memmap.nr_entries; i++) {
247                 unsigned long long end;
248
249                 /* Guard against non-page aligned E820 entries. */
250                 if (map[i].type == E820_RAM)
251                         map[i].size -= (map[i].size + map[i].addr) % PAGE_SIZE;
252
253                 end = map[i].addr + map[i].size;
254                 if (map[i].type == E820_RAM && end > mem_end) {
255                         /* RAM off the end - may be partially included */
256                         u64 delta = min(map[i].size, end - mem_end);
257
258                         map[i].size -= delta;
259                         end -= delta;
260
261                         extra_pages += PFN_DOWN(delta);
262                         /*
263                          * Set RAM below 4GB that is not for us to be unusable.
264                          * This prevents "System RAM" address space from being
265                          * used as potential resource for I/O address (happens
266                          * when 'allocate_resource' is called).
267                          */
268                         if (delta &&
269                                 (xen_initial_domain() && end < 0x100000000ULL))
270                                 e820_add_region(end, delta, E820_UNUSABLE);
271                 }
272
273                 if (map[i].size > 0 && end > xen_extra_mem[0].start)
274                         xen_extra_mem[0].start = end;
275
276                 /* Add region if any remains */
277                 if (map[i].size > 0)
278                         e820_add_region(map[i].addr, map[i].size, map[i].type);
279         }
280         /* Align the balloon area so that max_low_pfn does not get set
281          * to be at the _end_ of the PCI gap at the far end (fee01000).
282          * Note that the start of balloon area gets set in the loop above
283          * to be past the last E820 region. */
284         if (xen_initial_domain() && (xen_extra_mem[0].start < (1ULL<<32)))
285                 xen_extra_mem[0].start = (1ULL<<32);
286
287         /*
288          * In domU, the ISA region is normal, usable memory, but we
289          * reserve ISA memory anyway because too many things poke
290          * about in there.
291          *
292          * In Dom0, the host E820 information can leave gaps in the
293          * ISA range, which would cause us to release those pages.  To
294          * avoid this, we unconditionally reserve them here.
295          */
296         e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
297                         E820_RESERVED);
298
299         /*
300          * Reserve Xen bits:
301          *  - mfn_list
302          *  - xen_start_info
303          * See comment above "struct start_info" in <xen/interface/xen.h>
304          */
305         memblock_x86_reserve_range(__pa(xen_start_info->mfn_list),
306                       __pa(xen_start_info->pt_base),
307                         "XEN START INFO");
308
309         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
310
311         extra_limit = xen_get_max_pages();
312         if (max_pfn + extra_pages > extra_limit) {
313                 if (extra_limit > max_pfn)
314                         extra_pages = extra_limit - max_pfn;
315                 else
316                         extra_pages = 0;
317         }
318
319         xen_released_pages = xen_return_unused_memory(xen_start_info->nr_pages,
320                                                       &e820);
321         extra_pages += xen_released_pages;
322
323         /*
324          * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
325          * factor the base size.  On non-highmem systems, the base
326          * size is the full initial memory allocation; on highmem it
327          * is limited to the max size of lowmem, so that it doesn't
328          * get completely filled.
329          *
330          * In principle there could be a problem in lowmem systems if
331          * the initial memory is also very large with respect to
332          * lowmem, but we won't try to deal with that here.
333          */
334         extra_limit = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
335                           max_pfn + extra_pages);
336
337         if (extra_limit >= max_pfn)
338                 extra_pages = extra_limit - max_pfn;
339         else
340                 extra_pages = 0;
341
342         xen_add_extra_mem(extra_pages);
343
344         /*
345          * Set P2M for all non-RAM pages and E820 gaps to be identity
346          * type PFNs. We supply it with the non-sanitized version
347          * of the E820.
348          */
349         identity_pages = xen_set_identity(map_raw, memmap.nr_entries);
350         printk(KERN_INFO "Set %ld page(s) to 1-1 mapping.\n", identity_pages);
351         return "Xen";
352 }
353
354 /*
355  * Set the bit indicating "nosegneg" library variants should be used.
356  * We only need to bother in pure 32-bit mode; compat 32-bit processes
357  * can have un-truncated segments, so wrapping around is allowed.
358  */
359 static void __init fiddle_vdso(void)
360 {
361 #ifdef CONFIG_X86_32
362         u32 *mask;
363         mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK);
364         *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
365         mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK);
366         *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
367 #endif
368 }
369
370 static int __cpuinit register_callback(unsigned type, const void *func)
371 {
372         struct callback_register callback = {
373                 .type = type,
374                 .address = XEN_CALLBACK(__KERNEL_CS, func),
375                 .flags = CALLBACKF_mask_events,
376         };
377
378         return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
379 }
380
381 void __cpuinit xen_enable_sysenter(void)
382 {
383         int ret;
384         unsigned sysenter_feature;
385
386 #ifdef CONFIG_X86_32
387         sysenter_feature = X86_FEATURE_SEP;
388 #else
389         sysenter_feature = X86_FEATURE_SYSENTER32;
390 #endif
391
392         if (!boot_cpu_has(sysenter_feature))
393                 return;
394
395         ret = register_callback(CALLBACKTYPE_sysenter, xen_sysenter_target);
396         if(ret != 0)
397                 setup_clear_cpu_cap(sysenter_feature);
398 }
399
400 void __cpuinit xen_enable_syscall(void)
401 {
402 #ifdef CONFIG_X86_64
403         int ret;
404
405         ret = register_callback(CALLBACKTYPE_syscall, xen_syscall_target);
406         if (ret != 0) {
407                 printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
408                 /* Pretty fatal; 64-bit userspace has no other
409                    mechanism for syscalls. */
410         }
411
412         if (boot_cpu_has(X86_FEATURE_SYSCALL32)) {
413                 ret = register_callback(CALLBACKTYPE_syscall32,
414                                         xen_syscall32_target);
415                 if (ret != 0)
416                         setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
417         }
418 #endif /* CONFIG_X86_64 */
419 }
420
421 void __init xen_arch_setup(void)
422 {
423         xen_panic_handler_init();
424
425         HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
426         HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
427
428         if (!xen_feature(XENFEAT_auto_translated_physmap))
429                 HYPERVISOR_vm_assist(VMASST_CMD_enable,
430                                      VMASST_TYPE_pae_extended_cr3);
431
432         if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
433             register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
434                 BUG();
435
436         xen_enable_sysenter();
437         xen_enable_syscall();
438
439 #ifdef CONFIG_ACPI
440         if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
441                 printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
442                 disable_acpi();
443         }
444 #endif
445
446         memcpy(boot_command_line, xen_start_info->cmd_line,
447                MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
448                COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
449
450         /* Set up idle, making sure it calls safe_halt() pvop */
451 #ifdef CONFIG_X86_32
452         boot_cpu_data.hlt_works_ok = 1;
453 #endif
454         disable_cpuidle();
455         boot_option_idle_override = IDLE_HALT;
456
457         fiddle_vdso();
458 }