xref: /openbmc/linux/arch/x86/xen/setup.c (revision 358124ba)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Machine specific setup for xen
4  *
5  * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
6  */
7 
8 #include <linux/init.h>
9 #include <linux/iscsi_ibft.h>
10 #include <linux/sched.h>
11 #include <linux/kstrtox.h>
12 #include <linux/mm.h>
13 #include <linux/pm.h>
14 #include <linux/memblock.h>
15 #include <linux/cpuidle.h>
16 #include <linux/cpufreq.h>
17 #include <linux/memory_hotplug.h>
18 
19 #include <asm/elf.h>
20 #include <asm/vdso.h>
21 #include <asm/e820/api.h>
22 #include <asm/setup.h>
23 #include <asm/acpi.h>
24 #include <asm/numa.h>
25 #include <asm/idtentry.h>
26 #include <asm/xen/hypervisor.h>
27 #include <asm/xen/hypercall.h>
28 
29 #include <xen/xen.h>
30 #include <xen/page.h>
31 #include <xen/interface/callback.h>
32 #include <xen/interface/memory.h>
33 #include <xen/interface/physdev.h>
34 #include <xen/features.h>
35 #include <xen/hvc-console.h>
36 #include "xen-ops.h"
37 #include "mmu.h"
38 
39 #define GB(x) ((uint64_t)(x) * 1024 * 1024 * 1024)
40 
41 /* Number of pages released from the initial allocation. */
42 unsigned long xen_released_pages;
43 
44 /* Memory map would allow PCI passthrough. */
45 bool xen_pv_pci_possible;
46 
47 /* E820 map used during setting up memory. */
48 static struct e820_table xen_e820_table __initdata;
49 
50 /* Number of initially usable memory pages. */
51 static unsigned long ini_nr_pages __initdata;
52 
53 /*
54  * Buffer used to remap identity mapped pages. We only need the virtual space.
55  * The physical page behind this address is remapped as needed to different
56  * buffer pages.
57  */
58 #define REMAP_SIZE	(P2M_PER_PAGE - 3)
59 static struct {
60 	unsigned long	next_area_mfn;
61 	unsigned long	target_pfn;
62 	unsigned long	size;
63 	unsigned long	mfns[REMAP_SIZE];
64 } xen_remap_buf __initdata __aligned(PAGE_SIZE);
65 static unsigned long xen_remap_mfn __initdata = INVALID_P2M_ENTRY;
66 
67 static bool xen_512gb_limit __initdata = IS_ENABLED(CONFIG_XEN_512GB);
68 
69 static void __init xen_parse_512gb(void)
70 {
71 	bool val = false;
72 	char *arg;
73 
74 	arg = strstr(xen_start_info->cmd_line, "xen_512gb_limit");
75 	if (!arg)
76 		return;
77 
78 	arg = strstr(xen_start_info->cmd_line, "xen_512gb_limit=");
79 	if (!arg)
80 		val = true;
81 	else if (kstrtobool(arg + strlen("xen_512gb_limit="), &val))
82 		return;
83 
84 	xen_512gb_limit = val;
85 }
86 
87 static void __init xen_del_extra_mem(unsigned long start_pfn,
88 				     unsigned long n_pfns)
89 {
90 	int i;
91 	unsigned long start_r, size_r;
92 
93 	for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
94 		start_r = xen_extra_mem[i].start_pfn;
95 		size_r = xen_extra_mem[i].n_pfns;
96 
97 		/* Start of region. */
98 		if (start_r == start_pfn) {
99 			BUG_ON(n_pfns > size_r);
100 			xen_extra_mem[i].start_pfn += n_pfns;
101 			xen_extra_mem[i].n_pfns -= n_pfns;
102 			break;
103 		}
104 		/* End of region. */
105 		if (start_r + size_r == start_pfn + n_pfns) {
106 			BUG_ON(n_pfns > size_r);
107 			xen_extra_mem[i].n_pfns -= n_pfns;
108 			break;
109 		}
110 		/* Mid of region. */
111 		if (start_pfn > start_r && start_pfn < start_r + size_r) {
112 			BUG_ON(start_pfn + n_pfns > start_r + size_r);
113 			xen_extra_mem[i].n_pfns = start_pfn - start_r;
114 			/* Calling memblock_reserve() again is okay. */
115 			xen_add_extra_mem(start_pfn + n_pfns, start_r + size_r -
116 					  (start_pfn + n_pfns));
117 			break;
118 		}
119 	}
120 	memblock_phys_free(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns));
121 }
122 
123 /*
124  * Called during boot before the p2m list can take entries beyond the
125  * hypervisor supplied p2m list. Entries in extra mem are to be regarded as
126  * invalid.
127  */
128 unsigned long __ref xen_chk_extra_mem(unsigned long pfn)
129 {
130 	int i;
131 
132 	for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
133 		if (pfn >= xen_extra_mem[i].start_pfn &&
134 		    pfn < xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns)
135 			return INVALID_P2M_ENTRY;
136 	}
137 
138 	return IDENTITY_FRAME(pfn);
139 }
140 
141 /*
142  * Mark all pfns of extra mem as invalid in p2m list.
143  */
144 void __init xen_inv_extra_mem(void)
145 {
146 	unsigned long pfn, pfn_s, pfn_e;
147 	int i;
148 
149 	for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
150 		if (!xen_extra_mem[i].n_pfns)
151 			continue;
152 		pfn_s = xen_extra_mem[i].start_pfn;
153 		pfn_e = pfn_s + xen_extra_mem[i].n_pfns;
154 		for (pfn = pfn_s; pfn < pfn_e; pfn++)
155 			set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
156 	}
157 }
158 
159 /*
160  * Finds the next RAM pfn available in the E820 map after min_pfn.
161  * This function updates min_pfn with the pfn found and returns
162  * the size of that range or zero if not found.
163  */
164 static unsigned long __init xen_find_pfn_range(unsigned long *min_pfn)
165 {
166 	const struct e820_entry *entry = xen_e820_table.entries;
167 	unsigned int i;
168 	unsigned long done = 0;
169 
170 	for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) {
171 		unsigned long s_pfn;
172 		unsigned long e_pfn;
173 
174 		if (entry->type != E820_TYPE_RAM)
175 			continue;
176 
177 		e_pfn = PFN_DOWN(entry->addr + entry->size);
178 
179 		/* We only care about E820 after this */
180 		if (e_pfn <= *min_pfn)
181 			continue;
182 
183 		s_pfn = PFN_UP(entry->addr);
184 
185 		/* If min_pfn falls within the E820 entry, we want to start
186 		 * at the min_pfn PFN.
187 		 */
188 		if (s_pfn <= *min_pfn) {
189 			done = e_pfn - *min_pfn;
190 		} else {
191 			done = e_pfn - s_pfn;
192 			*min_pfn = s_pfn;
193 		}
194 		break;
195 	}
196 
197 	return done;
198 }
199 
200 static int __init xen_free_mfn(unsigned long mfn)
201 {
202 	struct xen_memory_reservation reservation = {
203 		.address_bits = 0,
204 		.extent_order = 0,
205 		.domid        = DOMID_SELF
206 	};
207 
208 	set_xen_guest_handle(reservation.extent_start, &mfn);
209 	reservation.nr_extents = 1;
210 
211 	return HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
212 }
213 
214 /*
215  * This releases a chunk of memory and then does the identity map. It's used
216  * as a fallback if the remapping fails.
217  */
218 static void __init xen_set_identity_and_release_chunk(unsigned long start_pfn,
219 						      unsigned long end_pfn)
220 {
221 	unsigned long pfn, end;
222 	int ret;
223 
224 	WARN_ON(start_pfn > end_pfn);
225 
226 	/* Release pages first. */
227 	end = min(end_pfn, ini_nr_pages);
228 	for (pfn = start_pfn; pfn < end; pfn++) {
229 		unsigned long mfn = pfn_to_mfn(pfn);
230 
231 		/* Make sure pfn exists to start with */
232 		if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn)
233 			continue;
234 
235 		ret = xen_free_mfn(mfn);
236 		WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret);
237 
238 		if (ret == 1) {
239 			xen_released_pages++;
240 			if (!__set_phys_to_machine(pfn, INVALID_P2M_ENTRY))
241 				break;
242 		} else
243 			break;
244 	}
245 
246 	set_phys_range_identity(start_pfn, end_pfn);
247 }
248 
249 /*
250  * Helper function to update the p2m and m2p tables and kernel mapping.
251  */
252 static void __init xen_update_mem_tables(unsigned long pfn, unsigned long mfn)
253 {
254 	struct mmu_update update = {
255 		.ptr = ((uint64_t)mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE,
256 		.val = pfn
257 	};
258 
259 	/* Update p2m */
260 	if (!set_phys_to_machine(pfn, mfn)) {
261 		WARN(1, "Failed to set p2m mapping for pfn=%ld mfn=%ld\n",
262 		     pfn, mfn);
263 		BUG();
264 	}
265 
266 	/* Update m2p */
267 	if (HYPERVISOR_mmu_update(&update, 1, NULL, DOMID_SELF) < 0) {
268 		WARN(1, "Failed to set m2p mapping for mfn=%ld pfn=%ld\n",
269 		     mfn, pfn);
270 		BUG();
271 	}
272 
273 	if (HYPERVISOR_update_va_mapping((unsigned long)__va(pfn << PAGE_SHIFT),
274 					 mfn_pte(mfn, PAGE_KERNEL), 0)) {
275 		WARN(1, "Failed to update kernel mapping for mfn=%ld pfn=%ld\n",
276 		      mfn, pfn);
277 		BUG();
278 	}
279 }
280 
281 /*
282  * This function updates the p2m and m2p tables with an identity map from
283  * start_pfn to start_pfn+size and prepares remapping the underlying RAM of the
284  * original allocation at remap_pfn. The information needed for remapping is
285  * saved in the memory itself to avoid the need for allocating buffers. The
286  * complete remap information is contained in a list of MFNs each containing
287  * up to REMAP_SIZE MFNs and the start target PFN for doing the remap.
288  * This enables us to preserve the original mfn sequence while doing the
289  * remapping at a time when the memory management is capable of allocating
290  * virtual and physical memory in arbitrary amounts, see 'xen_remap_memory' and
291  * its callers.
292  */
293 static void __init xen_do_set_identity_and_remap_chunk(
294         unsigned long start_pfn, unsigned long size, unsigned long remap_pfn)
295 {
296 	unsigned long buf = (unsigned long)&xen_remap_buf;
297 	unsigned long mfn_save, mfn;
298 	unsigned long ident_pfn_iter, remap_pfn_iter;
299 	unsigned long ident_end_pfn = start_pfn + size;
300 	unsigned long left = size;
301 	unsigned int i, chunk;
302 
303 	WARN_ON(size == 0);
304 
305 	mfn_save = virt_to_mfn((void *)buf);
306 
307 	for (ident_pfn_iter = start_pfn, remap_pfn_iter = remap_pfn;
308 	     ident_pfn_iter < ident_end_pfn;
309 	     ident_pfn_iter += REMAP_SIZE, remap_pfn_iter += REMAP_SIZE) {
310 		chunk = (left < REMAP_SIZE) ? left : REMAP_SIZE;
311 
312 		/* Map first pfn to xen_remap_buf */
313 		mfn = pfn_to_mfn(ident_pfn_iter);
314 		set_pte_mfn(buf, mfn, PAGE_KERNEL);
315 
316 		/* Save mapping information in page */
317 		xen_remap_buf.next_area_mfn = xen_remap_mfn;
318 		xen_remap_buf.target_pfn = remap_pfn_iter;
319 		xen_remap_buf.size = chunk;
320 		for (i = 0; i < chunk; i++)
321 			xen_remap_buf.mfns[i] = pfn_to_mfn(ident_pfn_iter + i);
322 
323 		/* Put remap buf into list. */
324 		xen_remap_mfn = mfn;
325 
326 		/* Set identity map */
327 		set_phys_range_identity(ident_pfn_iter, ident_pfn_iter + chunk);
328 
329 		left -= chunk;
330 	}
331 
332 	/* Restore old xen_remap_buf mapping */
333 	set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
334 }
335 
336 /*
337  * This function takes a contiguous pfn range that needs to be identity mapped
338  * and:
339  *
340  *  1) Finds a new range of pfns to use to remap based on E820 and remap_pfn.
341  *  2) Calls the do_ function to actually do the mapping/remapping work.
342  *
343  * The goal is to not allocate additional memory but to remap the existing
344  * pages. In the case of an error the underlying memory is simply released back
345  * to Xen and not remapped.
346  */
347 static unsigned long __init xen_set_identity_and_remap_chunk(
348 	unsigned long start_pfn, unsigned long end_pfn, unsigned long remap_pfn)
349 {
350 	unsigned long pfn;
351 	unsigned long i = 0;
352 	unsigned long n = end_pfn - start_pfn;
353 
354 	if (remap_pfn == 0)
355 		remap_pfn = ini_nr_pages;
356 
357 	while (i < n) {
358 		unsigned long cur_pfn = start_pfn + i;
359 		unsigned long left = n - i;
360 		unsigned long size = left;
361 		unsigned long remap_range_size;
362 
363 		/* Do not remap pages beyond the current allocation */
364 		if (cur_pfn >= ini_nr_pages) {
365 			/* Identity map remaining pages */
366 			set_phys_range_identity(cur_pfn, cur_pfn + size);
367 			break;
368 		}
369 		if (cur_pfn + size > ini_nr_pages)
370 			size = ini_nr_pages - cur_pfn;
371 
372 		remap_range_size = xen_find_pfn_range(&remap_pfn);
373 		if (!remap_range_size) {
374 			pr_warn("Unable to find available pfn range, not remapping identity pages\n");
375 			xen_set_identity_and_release_chunk(cur_pfn,
376 							   cur_pfn + left);
377 			break;
378 		}
379 		/* Adjust size to fit in current e820 RAM region */
380 		if (size > remap_range_size)
381 			size = remap_range_size;
382 
383 		xen_do_set_identity_and_remap_chunk(cur_pfn, size, remap_pfn);
384 
385 		/* Update variables to reflect new mappings. */
386 		i += size;
387 		remap_pfn += size;
388 	}
389 
390 	/*
391 	 * If the PFNs are currently mapped, their VA mappings need to be
392 	 * zapped.
393 	 */
394 	for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++)
395 		(void)HYPERVISOR_update_va_mapping(
396 			(unsigned long)__va(pfn << PAGE_SHIFT),
397 			native_make_pte(0), 0);
398 
399 	return remap_pfn;
400 }
401 
402 static unsigned long __init xen_count_remap_pages(
403 	unsigned long start_pfn, unsigned long end_pfn,
404 	unsigned long remap_pages)
405 {
406 	if (start_pfn >= ini_nr_pages)
407 		return remap_pages;
408 
409 	return remap_pages + min(end_pfn, ini_nr_pages) - start_pfn;
410 }
411 
412 static unsigned long __init xen_foreach_remap_area(
413 	unsigned long (*func)(unsigned long start_pfn, unsigned long end_pfn,
414 			      unsigned long last_val))
415 {
416 	phys_addr_t start = 0;
417 	unsigned long ret_val = 0;
418 	const struct e820_entry *entry = xen_e820_table.entries;
419 	int i;
420 
421 	/*
422 	 * Combine non-RAM regions and gaps until a RAM region (or the
423 	 * end of the map) is reached, then call the provided function
424 	 * to perform its duty on the non-RAM region.
425 	 *
426 	 * The combined non-RAM regions are rounded to a whole number
427 	 * of pages so any partial pages are accessible via the 1:1
428 	 * mapping.  This is needed for some BIOSes that put (for
429 	 * example) the DMI tables in a reserved region that begins on
430 	 * a non-page boundary.
431 	 */
432 	for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) {
433 		phys_addr_t end = entry->addr + entry->size;
434 		if (entry->type == E820_TYPE_RAM || i == xen_e820_table.nr_entries - 1) {
435 			unsigned long start_pfn = PFN_DOWN(start);
436 			unsigned long end_pfn = PFN_UP(end);
437 
438 			if (entry->type == E820_TYPE_RAM)
439 				end_pfn = PFN_UP(entry->addr);
440 
441 			if (start_pfn < end_pfn)
442 				ret_val = func(start_pfn, end_pfn, ret_val);
443 			start = end;
444 		}
445 	}
446 
447 	return ret_val;
448 }
449 
450 /*
451  * Remap the memory prepared in xen_do_set_identity_and_remap_chunk().
452  * The remap information (which mfn remap to which pfn) is contained in the
453  * to be remapped memory itself in a linked list anchored at xen_remap_mfn.
454  * This scheme allows to remap the different chunks in arbitrary order while
455  * the resulting mapping will be independent from the order.
456  */
457 void __init xen_remap_memory(void)
458 {
459 	unsigned long buf = (unsigned long)&xen_remap_buf;
460 	unsigned long mfn_save, pfn;
461 	unsigned long remapped = 0;
462 	unsigned int i;
463 	unsigned long pfn_s = ~0UL;
464 	unsigned long len = 0;
465 
466 	mfn_save = virt_to_mfn((void *)buf);
467 
468 	while (xen_remap_mfn != INVALID_P2M_ENTRY) {
469 		/* Map the remap information */
470 		set_pte_mfn(buf, xen_remap_mfn, PAGE_KERNEL);
471 
472 		BUG_ON(xen_remap_mfn != xen_remap_buf.mfns[0]);
473 
474 		pfn = xen_remap_buf.target_pfn;
475 		for (i = 0; i < xen_remap_buf.size; i++) {
476 			xen_update_mem_tables(pfn, xen_remap_buf.mfns[i]);
477 			remapped++;
478 			pfn++;
479 		}
480 		if (pfn_s == ~0UL || pfn == pfn_s) {
481 			pfn_s = xen_remap_buf.target_pfn;
482 			len += xen_remap_buf.size;
483 		} else if (pfn_s + len == xen_remap_buf.target_pfn) {
484 			len += xen_remap_buf.size;
485 		} else {
486 			xen_del_extra_mem(pfn_s, len);
487 			pfn_s = xen_remap_buf.target_pfn;
488 			len = xen_remap_buf.size;
489 		}
490 		xen_remap_mfn = xen_remap_buf.next_area_mfn;
491 	}
492 
493 	if (pfn_s != ~0UL && len)
494 		xen_del_extra_mem(pfn_s, len);
495 
496 	set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
497 
498 	pr_info("Remapped %ld page(s)\n", remapped);
499 
500 	xen_do_remap_nonram();
501 }
502 
503 static unsigned long __init xen_get_pages_limit(void)
504 {
505 	unsigned long limit;
506 
507 	limit = MAXMEM / PAGE_SIZE;
508 	if (!xen_initial_domain() && xen_512gb_limit)
509 		limit = GB(512) / PAGE_SIZE;
510 
511 	return limit;
512 }
513 
514 static unsigned long __init xen_get_max_pages(void)
515 {
516 	unsigned long max_pages, limit;
517 	domid_t domid = DOMID_SELF;
518 	long ret;
519 
520 	limit = xen_get_pages_limit();
521 	max_pages = limit;
522 
523 	/*
524 	 * For the initial domain we use the maximum reservation as
525 	 * the maximum page.
526 	 *
527 	 * For guest domains the current maximum reservation reflects
528 	 * the current maximum rather than the static maximum. In this
529 	 * case the e820 map provided to us will cover the static
530 	 * maximum region.
531 	 */
532 	if (xen_initial_domain()) {
533 		ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
534 		if (ret > 0)
535 			max_pages = ret;
536 	}
537 
538 	return min(max_pages, limit);
539 }
540 
541 static void __init xen_align_and_add_e820_region(phys_addr_t start,
542 						 phys_addr_t size, int type)
543 {
544 	phys_addr_t end = start + size;
545 
546 	/* Align RAM regions to page boundaries. */
547 	if (type == E820_TYPE_RAM) {
548 		start = PAGE_ALIGN(start);
549 		end &= ~((phys_addr_t)PAGE_SIZE - 1);
550 #ifdef CONFIG_MEMORY_HOTPLUG
551 		/*
552 		 * Don't allow adding memory not in E820 map while booting the
553 		 * system. Once the balloon driver is up it will remove that
554 		 * restriction again.
555 		 */
556 		max_mem_size = end;
557 #endif
558 	}
559 
560 	e820__range_add(start, end - start, type);
561 }
562 
563 static void __init xen_ignore_unusable(void)
564 {
565 	struct e820_entry *entry = xen_e820_table.entries;
566 	unsigned int i;
567 
568 	for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) {
569 		if (entry->type == E820_TYPE_UNUSABLE)
570 			entry->type = E820_TYPE_RAM;
571 	}
572 }
573 
574 static bool __init xen_is_e820_reserved(phys_addr_t start, phys_addr_t size)
575 {
576 	struct e820_entry *entry;
577 	unsigned mapcnt;
578 	phys_addr_t end;
579 
580 	if (!size)
581 		return false;
582 
583 	end = start + size;
584 	entry = xen_e820_table.entries;
585 
586 	for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++) {
587 		if (entry->type == E820_TYPE_RAM && entry->addr <= start &&
588 		    (entry->addr + entry->size) >= end)
589 			return false;
590 
591 		entry++;
592 	}
593 
594 	return true;
595 }
596 
597 /*
598  * Find a free area in physical memory not yet reserved and compliant with
599  * E820 map.
600  * Used to relocate pre-allocated areas like initrd or p2m list which are in
601  * conflict with the to be used E820 map.
602  * In case no area is found, return 0. Otherwise return the physical address
603  * of the area which is already reserved for convenience.
604  */
605 phys_addr_t __init xen_find_free_area(phys_addr_t size)
606 {
607 	unsigned mapcnt;
608 	phys_addr_t addr, start;
609 	struct e820_entry *entry = xen_e820_table.entries;
610 
611 	for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++, entry++) {
612 		if (entry->type != E820_TYPE_RAM || entry->size < size)
613 			continue;
614 		start = entry->addr;
615 		for (addr = start; addr < start + size; addr += PAGE_SIZE) {
616 			if (!memblock_is_reserved(addr))
617 				continue;
618 			start = addr + PAGE_SIZE;
619 			if (start + size > entry->addr + entry->size)
620 				break;
621 		}
622 		if (addr >= start + size) {
623 			memblock_reserve(start, size);
624 			return start;
625 		}
626 	}
627 
628 	return 0;
629 }
630 
631 /*
632  * Swap a non-RAM E820 map entry with RAM above ini_nr_pages.
633  * Note that the E820 map is modified accordingly, but the P2M map isn't yet.
634  * The adaption of the P2M must be deferred until page allocation is possible.
635  */
636 static void __init xen_e820_swap_entry_with_ram(struct e820_entry *swap_entry)
637 {
638 	struct e820_entry *entry;
639 	unsigned int mapcnt;
640 	phys_addr_t mem_end = PFN_PHYS(ini_nr_pages);
641 	phys_addr_t swap_addr, swap_size, entry_end;
642 
643 	swap_addr = PAGE_ALIGN_DOWN(swap_entry->addr);
644 	swap_size = PAGE_ALIGN(swap_entry->addr - swap_addr + swap_entry->size);
645 	entry = xen_e820_table.entries;
646 
647 	for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++) {
648 		entry_end = entry->addr + entry->size;
649 		if (entry->type == E820_TYPE_RAM && entry->size >= swap_size &&
650 		    entry_end - swap_size >= mem_end) {
651 			/* Reduce RAM entry by needed space (whole pages). */
652 			entry->size -= swap_size;
653 
654 			/* Add new entry at the end of E820 map. */
655 			entry = xen_e820_table.entries +
656 				xen_e820_table.nr_entries;
657 			xen_e820_table.nr_entries++;
658 
659 			/* Fill new entry (keep size and page offset). */
660 			entry->type = swap_entry->type;
661 			entry->addr = entry_end - swap_size +
662 				      swap_addr - swap_entry->addr;
663 			entry->size = swap_entry->size;
664 
665 			/* Convert old entry to RAM, align to pages. */
666 			swap_entry->type = E820_TYPE_RAM;
667 			swap_entry->addr = swap_addr;
668 			swap_entry->size = swap_size;
669 
670 			/* Remember PFN<->MFN relation for P2M update. */
671 			xen_add_remap_nonram(swap_addr, entry_end - swap_size,
672 					     swap_size);
673 
674 			/* Order E820 table and merge entries. */
675 			e820__update_table(&xen_e820_table);
676 
677 			return;
678 		}
679 
680 		entry++;
681 	}
682 
683 	xen_raw_console_write("No suitable area found for required E820 entry remapping action\n");
684 	BUG();
685 }
686 
687 /*
688  * Look for non-RAM memory types in a specific guest physical area and move
689  * those away if possible (ACPI NVS only for now).
690  */
691 static void __init xen_e820_resolve_conflicts(phys_addr_t start,
692 					      phys_addr_t size)
693 {
694 	struct e820_entry *entry;
695 	unsigned int mapcnt;
696 	phys_addr_t end;
697 
698 	if (!size)
699 		return;
700 
701 	end = start + size;
702 	entry = xen_e820_table.entries;
703 
704 	for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++) {
705 		if (entry->addr >= end)
706 			return;
707 
708 		if (entry->addr + entry->size > start &&
709 		    entry->type == E820_TYPE_NVS)
710 			xen_e820_swap_entry_with_ram(entry);
711 
712 		entry++;
713 	}
714 }
715 
716 /*
717  * Check for an area in physical memory to be usable for non-movable purposes.
718  * An area is considered to usable if the used E820 map lists it to be RAM or
719  * some other type which can be moved to higher PFNs while keeping the MFNs.
720  * In case the area is not usable, crash the system with an error message.
721  */
722 void __init xen_chk_is_e820_usable(phys_addr_t start, phys_addr_t size,
723 				   const char *component)
724 {
725 	xen_e820_resolve_conflicts(start, size);
726 
727 	if (!xen_is_e820_reserved(start, size))
728 		return;
729 
730 	xen_raw_console_write("Xen hypervisor allocated ");
731 	xen_raw_console_write(component);
732 	xen_raw_console_write(" memory conflicts with E820 map\n");
733 	BUG();
734 }
735 
736 /*
737  * Like memcpy, but with physical addresses for dest and src.
738  */
739 static void __init xen_phys_memcpy(phys_addr_t dest, phys_addr_t src,
740 				   phys_addr_t n)
741 {
742 	phys_addr_t dest_off, src_off, dest_len, src_len, len;
743 	void *from, *to;
744 
745 	while (n) {
746 		dest_off = dest & ~PAGE_MASK;
747 		src_off = src & ~PAGE_MASK;
748 		dest_len = n;
749 		if (dest_len > (NR_FIX_BTMAPS << PAGE_SHIFT) - dest_off)
750 			dest_len = (NR_FIX_BTMAPS << PAGE_SHIFT) - dest_off;
751 		src_len = n;
752 		if (src_len > (NR_FIX_BTMAPS << PAGE_SHIFT) - src_off)
753 			src_len = (NR_FIX_BTMAPS << PAGE_SHIFT) - src_off;
754 		len = min(dest_len, src_len);
755 		to = early_memremap(dest - dest_off, dest_len + dest_off);
756 		from = early_memremap(src - src_off, src_len + src_off);
757 		memcpy(to, from, len);
758 		early_memunmap(to, dest_len + dest_off);
759 		early_memunmap(from, src_len + src_off);
760 		n -= len;
761 		dest += len;
762 		src += len;
763 	}
764 }
765 
766 /*
767  * Reserve Xen mfn_list.
768  */
769 static void __init xen_reserve_xen_mfnlist(void)
770 {
771 	phys_addr_t start, size;
772 
773 	if (xen_start_info->mfn_list >= __START_KERNEL_map) {
774 		start = __pa(xen_start_info->mfn_list);
775 		size = PFN_ALIGN(xen_start_info->nr_pages *
776 				 sizeof(unsigned long));
777 	} else {
778 		start = PFN_PHYS(xen_start_info->first_p2m_pfn);
779 		size = PFN_PHYS(xen_start_info->nr_p2m_frames);
780 	}
781 
782 	memblock_reserve(start, size);
783 	if (!xen_is_e820_reserved(start, size))
784 		return;
785 
786 	xen_relocate_p2m();
787 	memblock_phys_free(start, size);
788 }
789 
790 /**
791  * xen_memory_setup - Hook for machine specific memory setup.
792  **/
793 char * __init xen_memory_setup(void)
794 {
795 	unsigned long pfn_s, n_pfns;
796 	phys_addr_t mem_end, addr, size, chunk_size;
797 	u32 type;
798 	int rc;
799 	struct xen_memory_map memmap;
800 	unsigned long max_pages;
801 	unsigned long extra_pages = 0;
802 	unsigned long maxmem_pages;
803 	int i;
804 	int op;
805 
806 	xen_parse_512gb();
807 	ini_nr_pages = min(xen_get_pages_limit(), xen_start_info->nr_pages);
808 	mem_end = PFN_PHYS(ini_nr_pages);
809 
810 	memmap.nr_entries = ARRAY_SIZE(xen_e820_table.entries);
811 	set_xen_guest_handle(memmap.buffer, xen_e820_table.entries);
812 
813 #if defined(CONFIG_MEMORY_HOTPLUG) && defined(CONFIG_XEN_BALLOON)
814 	xen_saved_max_mem_size = max_mem_size;
815 #endif
816 
817 	op = xen_initial_domain() ?
818 		XENMEM_machine_memory_map :
819 		XENMEM_memory_map;
820 	rc = HYPERVISOR_memory_op(op, &memmap);
821 	if (rc == -ENOSYS) {
822 		BUG_ON(xen_initial_domain());
823 		memmap.nr_entries = 1;
824 		xen_e820_table.entries[0].addr = 0ULL;
825 		xen_e820_table.entries[0].size = mem_end;
826 		/* 8MB slack (to balance backend allocations). */
827 		xen_e820_table.entries[0].size += 8ULL << 20;
828 		xen_e820_table.entries[0].type = E820_TYPE_RAM;
829 		rc = 0;
830 	}
831 	BUG_ON(rc);
832 	BUG_ON(memmap.nr_entries == 0);
833 	xen_e820_table.nr_entries = memmap.nr_entries;
834 
835 	if (xen_initial_domain()) {
836 		/*
837 		 * Xen won't allow a 1:1 mapping to be created to UNUSABLE
838 		 * regions, so if we're using the machine memory map leave the
839 		 * region as RAM as it is in the pseudo-physical map.
840 		 *
841 		 * UNUSABLE regions in domUs are not handled and will need
842 		 * a patch in the future.
843 		 */
844 		xen_ignore_unusable();
845 
846 #ifdef CONFIG_ISCSI_IBFT_FIND
847 		/* Reserve 0.5 MiB to 1 MiB region so iBFT can be found */
848 		xen_e820_table.entries[xen_e820_table.nr_entries].addr = IBFT_START;
849 		xen_e820_table.entries[xen_e820_table.nr_entries].size = IBFT_END - IBFT_START;
850 		xen_e820_table.entries[xen_e820_table.nr_entries].type = E820_TYPE_RESERVED;
851 		xen_e820_table.nr_entries++;
852 #endif
853 	}
854 
855 	/* Make sure the Xen-supplied memory map is well-ordered. */
856 	e820__update_table(&xen_e820_table);
857 
858 	max_pages = xen_get_max_pages();
859 
860 	/* How many extra pages do we need due to remapping? */
861 	max_pages += xen_foreach_remap_area(xen_count_remap_pages);
862 
863 	if (max_pages > ini_nr_pages)
864 		extra_pages += max_pages - ini_nr_pages;
865 
866 	/*
867 	 * Clamp the amount of extra memory to a EXTRA_MEM_RATIO
868 	 * factor the base size.
869 	 *
870 	 * Make sure we have no memory above max_pages, as this area
871 	 * isn't handled by the p2m management.
872 	 */
873 	maxmem_pages = EXTRA_MEM_RATIO * min(ini_nr_pages, PFN_DOWN(MAXMEM));
874 	extra_pages = min3(maxmem_pages, extra_pages, max_pages - ini_nr_pages);
875 	i = 0;
876 	addr = xen_e820_table.entries[0].addr;
877 	size = xen_e820_table.entries[0].size;
878 	while (i < xen_e820_table.nr_entries) {
879 		bool discard = false;
880 
881 		chunk_size = size;
882 		type = xen_e820_table.entries[i].type;
883 
884 		if (type == E820_TYPE_RESERVED)
885 			xen_pv_pci_possible = true;
886 
887 		if (type == E820_TYPE_RAM) {
888 			if (addr < mem_end) {
889 				chunk_size = min(size, mem_end - addr);
890 			} else if (extra_pages) {
891 				chunk_size = min(size, PFN_PHYS(extra_pages));
892 				pfn_s = PFN_UP(addr);
893 				n_pfns = PFN_DOWN(addr + chunk_size) - pfn_s;
894 				extra_pages -= n_pfns;
895 				xen_add_extra_mem(pfn_s, n_pfns);
896 				xen_max_p2m_pfn = pfn_s + n_pfns;
897 			} else
898 				discard = true;
899 		}
900 
901 		if (!discard)
902 			xen_align_and_add_e820_region(addr, chunk_size, type);
903 
904 		addr += chunk_size;
905 		size -= chunk_size;
906 		if (size == 0) {
907 			i++;
908 			if (i < xen_e820_table.nr_entries) {
909 				addr = xen_e820_table.entries[i].addr;
910 				size = xen_e820_table.entries[i].size;
911 			}
912 		}
913 	}
914 
915 	/*
916 	 * Set the rest as identity mapped, in case PCI BARs are
917 	 * located here.
918 	 */
919 	set_phys_range_identity(addr / PAGE_SIZE, ~0ul);
920 
921 	/*
922 	 * In domU, the ISA region is normal, usable memory, but we
923 	 * reserve ISA memory anyway because too many things poke
924 	 * about in there.
925 	 */
926 	e820__range_add(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS, E820_TYPE_RESERVED);
927 
928 	e820__update_table(e820_table);
929 
930 	/*
931 	 * Check whether the kernel itself conflicts with the target E820 map.
932 	 * Failing now is better than running into weird problems later due
933 	 * to relocating (and even reusing) pages with kernel text or data.
934 	 */
935 	xen_chk_is_e820_usable(__pa_symbol(_text),
936 			       __pa_symbol(_end) - __pa_symbol(_text),
937 			       "kernel");
938 
939 	/*
940 	 * Check for a conflict of the xen_start_info memory with the target
941 	 * E820 map.
942 	 */
943 	xen_chk_is_e820_usable(__pa(xen_start_info), sizeof(*xen_start_info),
944 			       "xen_start_info");
945 
946 	/*
947 	 * Check for a conflict of the hypervisor supplied page tables with
948 	 * the target E820 map.
949 	 */
950 	xen_pt_check_e820();
951 
952 	xen_reserve_xen_mfnlist();
953 
954 	/* Check for a conflict of the initrd with the target E820 map. */
955 	if (xen_is_e820_reserved(boot_params.hdr.ramdisk_image,
956 				 boot_params.hdr.ramdisk_size)) {
957 		phys_addr_t new_area, start, size;
958 
959 		new_area = xen_find_free_area(boot_params.hdr.ramdisk_size);
960 		if (!new_area) {
961 			xen_raw_console_write("Can't find new memory area for initrd needed due to E820 map conflict\n");
962 			BUG();
963 		}
964 
965 		start = boot_params.hdr.ramdisk_image;
966 		size = boot_params.hdr.ramdisk_size;
967 		xen_phys_memcpy(new_area, start, size);
968 		pr_info("initrd moved from [mem %#010llx-%#010llx] to [mem %#010llx-%#010llx]\n",
969 			start, start + size, new_area, new_area + size);
970 		memblock_phys_free(start, size);
971 		boot_params.hdr.ramdisk_image = new_area;
972 		boot_params.ext_ramdisk_image = new_area >> 32;
973 	}
974 
975 	/*
976 	 * Set identity map on non-RAM pages and prepare remapping the
977 	 * underlying RAM.
978 	 */
979 	xen_foreach_remap_area(xen_set_identity_and_remap_chunk);
980 
981 	pr_info("Released %ld page(s)\n", xen_released_pages);
982 
983 	return "Xen";
984 }
985 
986 static int register_callback(unsigned type, const void *func)
987 {
988 	struct callback_register callback = {
989 		.type = type,
990 		.address = XEN_CALLBACK(__KERNEL_CS, func),
991 		.flags = CALLBACKF_mask_events,
992 	};
993 
994 	return HYPERVISOR_callback_op(CALLBACKOP_register, &callback);
995 }
996 
997 void xen_enable_sysenter(void)
998 {
999 	if (cpu_feature_enabled(X86_FEATURE_SYSENTER32) &&
1000 	    register_callback(CALLBACKTYPE_sysenter, xen_entry_SYSENTER_compat))
1001 		setup_clear_cpu_cap(X86_FEATURE_SYSENTER32);
1002 }
1003 
1004 void xen_enable_syscall(void)
1005 {
1006 	int ret;
1007 
1008 	ret = register_callback(CALLBACKTYPE_syscall, xen_entry_SYSCALL_64);
1009 	if (ret != 0) {
1010 		printk(KERN_ERR "Failed to set syscall callback: %d\n", ret);
1011 		/* Pretty fatal; 64-bit userspace has no other
1012 		   mechanism for syscalls. */
1013 	}
1014 
1015 	if (cpu_feature_enabled(X86_FEATURE_SYSCALL32) &&
1016 	    register_callback(CALLBACKTYPE_syscall32, xen_entry_SYSCALL_compat))
1017 		setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
1018 }
1019 
1020 static void __init xen_pvmmu_arch_setup(void)
1021 {
1022 	HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
1023 
1024 	if (register_callback(CALLBACKTYPE_event,
1025 			      xen_asm_exc_xen_hypervisor_callback) ||
1026 	    register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
1027 		BUG();
1028 
1029 	xen_enable_sysenter();
1030 	xen_enable_syscall();
1031 }
1032 
1033 /* This function is not called for HVM domains */
1034 void __init xen_arch_setup(void)
1035 {
1036 	xen_panic_handler_init();
1037 	xen_pvmmu_arch_setup();
1038 
1039 #ifdef CONFIG_ACPI
1040 	if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
1041 		printk(KERN_INFO "ACPI in unprivileged domain disabled\n");
1042 		disable_acpi();
1043 	}
1044 #endif
1045 
1046 	memcpy(boot_command_line, xen_start_info->cmd_line,
1047 	       MAX_GUEST_CMDLINE > COMMAND_LINE_SIZE ?
1048 	       COMMAND_LINE_SIZE : MAX_GUEST_CMDLINE);
1049 
1050 	/* Set up idle, making sure it calls safe_halt() pvop */
1051 	disable_cpuidle();
1052 	disable_cpufreq();
1053 	WARN_ON(xen_set_default_idle());
1054 #ifdef CONFIG_NUMA
1055 	numa_off = 1;
1056 #endif
1057 }
1058