xref: /freebsd/sys/dev/xen/balloon/balloon.c (revision f929eb1ed50f079f1638cdabe511ac2e347f541a)
189e0f4d2SKip Macy /******************************************************************************
289e0f4d2SKip Macy  * balloon.c
389e0f4d2SKip Macy  *
489e0f4d2SKip Macy  * Xen balloon driver - enables returning/claiming memory to/from Xen.
589e0f4d2SKip Macy  *
689e0f4d2SKip Macy  * Copyright (c) 2003, B Dragovic
789e0f4d2SKip Macy  * Copyright (c) 2003-2004, M Williamson, K Fraser
889e0f4d2SKip Macy  * Copyright (c) 2005 Dan M. Smith, IBM Corporation
989e0f4d2SKip Macy  *
1089e0f4d2SKip Macy  * This file may be distributed separately from the Linux kernel, or
1189e0f4d2SKip Macy  * incorporated into other software packages, subject to the following license:
1289e0f4d2SKip Macy  *
1389e0f4d2SKip Macy  * Permission is hereby granted, free of charge, to any person obtaining a copy
1489e0f4d2SKip Macy  * of this source file (the "Software"), to deal in the Software without
1589e0f4d2SKip Macy  * restriction, including without limitation the rights to use, copy, modify,
1689e0f4d2SKip Macy  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
1789e0f4d2SKip Macy  * and to permit persons to whom the Software is furnished to do so, subject to
1889e0f4d2SKip Macy  * the following conditions:
1989e0f4d2SKip Macy  *
2089e0f4d2SKip Macy  * The above copyright notice and this permission notice shall be included in
2189e0f4d2SKip Macy  * all copies or substantial portions of the Software.
2289e0f4d2SKip Macy  *
2389e0f4d2SKip Macy  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2489e0f4d2SKip Macy  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2589e0f4d2SKip Macy  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2689e0f4d2SKip Macy  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2789e0f4d2SKip Macy  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2889e0f4d2SKip Macy  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2989e0f4d2SKip Macy  * IN THE SOFTWARE.
3089e0f4d2SKip Macy  */
3189e0f4d2SKip Macy 
3289e0f4d2SKip Macy #include <sys/cdefs.h>
3389e0f4d2SKip Macy __FBSDID("$FreeBSD$");
3489e0f4d2SKip Macy 
3589e0f4d2SKip Macy #include <sys/param.h>
3689e0f4d2SKip Macy #include <sys/lock.h>
3712678024SDoug Rabson #include <sys/kernel.h>
3812678024SDoug Rabson #include <sys/kthread.h>
3912678024SDoug Rabson #include <sys/malloc.h>
4089e0f4d2SKip Macy #include <sys/mutex.h>
4112678024SDoug Rabson #include <sys/sysctl.h>
42ae3078d9SRoger Pau Monné #include <sys/module.h>
4389e0f4d2SKip Macy 
4412678024SDoug Rabson #include <vm/vm.h>
4512678024SDoug Rabson #include <vm/vm_page.h>
4612678024SDoug Rabson 
4776acc41fSJustin T. Gibbs #include <xen/xen-os.h>
4876acc41fSJustin T. Gibbs #include <xen/hypervisor.h>
4976acc41fSJustin T. Gibbs #include <xen/features.h>
5076acc41fSJustin T. Gibbs #include <xen/xenstore/xenstorevar.h>
5176acc41fSJustin T. Gibbs 
52d745c852SEd Schouten static MALLOC_DEFINE(M_BALLOON, "Balloon", "Xen Balloon Driver");
5312678024SDoug Rabson 
5470046ce6SJustin T. Gibbs /* Convert from KB (as fetched from xenstore) to number of PAGES */
5570046ce6SJustin T. Gibbs #define KB_TO_PAGE_SHIFT	(PAGE_SHIFT - 10)
5689e0f4d2SKip Macy 
5770046ce6SJustin T. Gibbs struct mtx balloon_mutex;
5889e0f4d2SKip Macy 
5912678024SDoug Rabson /* We increase/decrease in batches which fit in a page */
602f9ec994SRoger Pau Monné static xen_pfn_t frame_list[PAGE_SIZE / sizeof(xen_pfn_t)];
6112678024SDoug Rabson 
6212678024SDoug Rabson struct balloon_stats {
6389e0f4d2SKip Macy 	/* We aim for 'current allocation' == 'target allocation'. */
6412678024SDoug Rabson 	unsigned long current_pages;
6512678024SDoug Rabson 	unsigned long target_pages;
6689e0f4d2SKip Macy 	/* We may hit the hard limit in Xen. If we do then we remember it. */
6712678024SDoug Rabson 	unsigned long hard_limit;
6889e0f4d2SKip Macy 	/*
6912678024SDoug Rabson 	 * Drivers may alter the memory reservation independently, but they
7012678024SDoug Rabson 	 * must inform the balloon driver so we avoid hitting the hard limit.
7189e0f4d2SKip Macy 	 */
7212678024SDoug Rabson 	unsigned long driver_pages;
7312678024SDoug Rabson 	/* Number of pages in high- and low-memory balloons. */
7412678024SDoug Rabson 	unsigned long balloon_low;
7512678024SDoug Rabson 	unsigned long balloon_high;
7612678024SDoug Rabson };
7712678024SDoug Rabson 
7812678024SDoug Rabson static struct balloon_stats balloon_stats;
7912678024SDoug Rabson #define bs balloon_stats
8012678024SDoug Rabson 
8112678024SDoug Rabson SYSCTL_DECL(_dev_xen);
8241fc1ce1SPawel Biernacki static SYSCTL_NODE(_dev_xen, OID_AUTO, balloon,
8341fc1ce1SPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8441fc1ce1SPawel Biernacki     "Balloon");
8512678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, current, CTLFLAG_RD,
8612678024SDoug Rabson     &bs.current_pages, 0, "Current allocation");
8712678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, target, CTLFLAG_RD,
8812678024SDoug Rabson     &bs.target_pages, 0, "Target allocation");
8912678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, driver_pages, CTLFLAG_RD,
9012678024SDoug Rabson     &bs.driver_pages, 0, "Driver pages");
9112678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, hard_limit, CTLFLAG_RD,
9212678024SDoug Rabson     &bs.hard_limit, 0, "Xen hard limit");
9312678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, low_mem, CTLFLAG_RD,
9412678024SDoug Rabson     &bs.balloon_low, 0, "Low-mem balloon");
9512678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, high_mem, CTLFLAG_RD,
9612678024SDoug Rabson     &bs.balloon_high, 0, "High-mem balloon");
9789e0f4d2SKip Macy 
9889e0f4d2SKip Macy /* List of ballooned pages, threaded through the mem_map array. */
9968e58ea7SRoger Pau Monné static TAILQ_HEAD(,vm_page) ballooned_pages;
10089e0f4d2SKip Macy 
10189e0f4d2SKip Macy /* Main work function, always executed in process context. */
10289e0f4d2SKip Macy static void balloon_process(void *unused);
10389e0f4d2SKip Macy 
10489e0f4d2SKip Macy #define IPRINTK(fmt, args...) \
10589e0f4d2SKip Macy 	printk(KERN_INFO "xen_mem: " fmt, ##args)
10689e0f4d2SKip Macy #define WPRINTK(fmt, args...) \
10789e0f4d2SKip Macy 	printk(KERN_WARNING "xen_mem: " fmt, ##args)
10889e0f4d2SKip Macy 
10989e0f4d2SKip Macy static unsigned long
11089e0f4d2SKip Macy current_target(void)
11189e0f4d2SKip Macy {
11212678024SDoug Rabson 	unsigned long target = min(bs.target_pages, bs.hard_limit);
11312678024SDoug Rabson 	if (target > (bs.current_pages + bs.balloon_low + bs.balloon_high))
11412678024SDoug Rabson 		target = bs.current_pages + bs.balloon_low + bs.balloon_high;
11570046ce6SJustin T. Gibbs 	return (target);
11689e0f4d2SKip Macy }
11789e0f4d2SKip Macy 
11812678024SDoug Rabson static unsigned long
11912678024SDoug Rabson minimum_target(void)
12012678024SDoug Rabson {
12112678024SDoug Rabson 	unsigned long min_pages, curr_pages = current_target();
12212678024SDoug Rabson 
12312678024SDoug Rabson #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
12470046ce6SJustin T. Gibbs 	/*
12570046ce6SJustin T. Gibbs 	 * Simple continuous piecewiese linear function:
12612678024SDoug Rabson 	 *  max MiB -> min MiB	gradient
12712678024SDoug Rabson 	 *       0	   0
12812678024SDoug Rabson 	 *      16	  16
12912678024SDoug Rabson 	 *      32	  24
13012678024SDoug Rabson 	 *     128	  72	(1/2)
13112678024SDoug Rabson 	 *     512 	 168	(1/4)
13212678024SDoug Rabson 	 *    2048	 360	(1/8)
13312678024SDoug Rabson 	 *    8192	 552	(1/32)
13412678024SDoug Rabson 	 *   32768	1320
13512678024SDoug Rabson 	 *  131072	4392
13612678024SDoug Rabson 	 */
137ed95805eSJohn Baldwin 	if (realmem < MB2PAGES(128))
138ed95805eSJohn Baldwin 		min_pages = MB2PAGES(8) + (realmem >> 1);
139ed95805eSJohn Baldwin 	else if (realmem < MB2PAGES(512))
140ed95805eSJohn Baldwin 		min_pages = MB2PAGES(40) + (realmem >> 2);
141ed95805eSJohn Baldwin 	else if (realmem < MB2PAGES(2048))
142ed95805eSJohn Baldwin 		min_pages = MB2PAGES(104) + (realmem >> 3);
14312678024SDoug Rabson 	else
144ed95805eSJohn Baldwin 		min_pages = MB2PAGES(296) + (realmem >> 5);
14512678024SDoug Rabson #undef MB2PAGES
14612678024SDoug Rabson 
14712678024SDoug Rabson 	/* Don't enforce growth */
14870046ce6SJustin T. Gibbs 	return (min(min_pages, curr_pages));
14912678024SDoug Rabson }
15012678024SDoug Rabson 
15189e0f4d2SKip Macy static int
15289e0f4d2SKip Macy increase_reservation(unsigned long nr_pages)
15389e0f4d2SKip Macy {
1542f9ec994SRoger Pau Monné 	unsigned long  i;
15512678024SDoug Rabson 	vm_page_t      page;
15689e0f4d2SKip Macy 	long           rc;
15789e0f4d2SKip Macy 	struct xen_memory_reservation reservation = {
15889e0f4d2SKip Macy 		.address_bits = 0,
15989e0f4d2SKip Macy 		.extent_order = 0,
16089e0f4d2SKip Macy 		.domid        = DOMID_SELF
16189e0f4d2SKip Macy 	};
16289e0f4d2SKip Macy 
16370046ce6SJustin T. Gibbs 	mtx_assert(&balloon_mutex, MA_OWNED);
16489e0f4d2SKip Macy 
16570046ce6SJustin T. Gibbs 	if (nr_pages > nitems(frame_list))
16670046ce6SJustin T. Gibbs 		nr_pages = nitems(frame_list);
16789e0f4d2SKip Macy 
16868e58ea7SRoger Pau Monné 	for (page = TAILQ_FIRST(&ballooned_pages), i = 0;
16968e58ea7SRoger Pau Monné 	    i < nr_pages; i++, page = TAILQ_NEXT(page, plinks.q)) {
17068e58ea7SRoger Pau Monné 		KASSERT(page != NULL, ("ballooned_pages list corrupt"));
17112678024SDoug Rabson 		frame_list[i] = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
17212678024SDoug Rabson 	}
17389e0f4d2SKip Macy 
17412678024SDoug Rabson 	set_xen_guest_handle(reservation.extent_start, frame_list);
17589e0f4d2SKip Macy 	reservation.nr_extents   = nr_pages;
17689e0f4d2SKip Macy 	rc = HYPERVISOR_memory_op(
17712678024SDoug Rabson 		XENMEM_populate_physmap, &reservation);
17889e0f4d2SKip Macy 	if (rc < nr_pages) {
17912678024SDoug Rabson 		if (rc > 0) {
180e7236a7dSMateusz Guzik 			int ret __diagused;
18112678024SDoug Rabson 
18289e0f4d2SKip Macy 			/* We hit the Xen hard limit: reprobe. */
18389e0f4d2SKip Macy 			reservation.nr_extents = rc;
18489e0f4d2SKip Macy 			ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
18589e0f4d2SKip Macy 					&reservation);
18612678024SDoug Rabson 			KASSERT(ret == rc, ("HYPERVISOR_memory_op failed"));
18712678024SDoug Rabson 		}
18812678024SDoug Rabson 		if (rc >= 0)
18912678024SDoug Rabson 			bs.hard_limit = (bs.current_pages + rc -
19012678024SDoug Rabson 					 bs.driver_pages);
19189e0f4d2SKip Macy 		goto out;
19289e0f4d2SKip Macy 	}
19389e0f4d2SKip Macy 
19489e0f4d2SKip Macy 	for (i = 0; i < nr_pages; i++) {
19568e58ea7SRoger Pau Monné 		page = TAILQ_FIRST(&ballooned_pages);
19668e58ea7SRoger Pau Monné 		KASSERT(page != NULL, ("Unable to get ballooned page"));
19768e58ea7SRoger Pau Monné 		TAILQ_REMOVE(&ballooned_pages, page, plinks.q);
19868e58ea7SRoger Pau Monné 		bs.balloon_low--;
19989e0f4d2SKip Macy 
200ed95805eSJohn Baldwin 		KASSERT(xen_feature(XENFEAT_auto_translated_physmap),
20112678024SDoug Rabson 		    ("auto translated physmap but mapping is valid"));
20289e0f4d2SKip Macy 
20389e0f4d2SKip Macy 		vm_page_free(page);
20489e0f4d2SKip Macy 	}
20589e0f4d2SKip Macy 
20612678024SDoug Rabson 	bs.current_pages += nr_pages;
20789e0f4d2SKip Macy 
20889e0f4d2SKip Macy  out:
20970046ce6SJustin T. Gibbs 	return (0);
21089e0f4d2SKip Macy }
21189e0f4d2SKip Macy 
21289e0f4d2SKip Macy static int
21389e0f4d2SKip Macy decrease_reservation(unsigned long nr_pages)
21489e0f4d2SKip Macy {
2152f9ec994SRoger Pau Monné 	unsigned long  i;
21612678024SDoug Rabson 	vm_page_t      page;
21789e0f4d2SKip Macy 	int            need_sleep = 0;
218e7236a7dSMateusz Guzik 	int ret __diagused;
21989e0f4d2SKip Macy 	struct xen_memory_reservation reservation = {
22089e0f4d2SKip Macy 		.address_bits = 0,
22189e0f4d2SKip Macy 		.extent_order = 0,
22289e0f4d2SKip Macy 		.domid        = DOMID_SELF
22389e0f4d2SKip Macy 	};
22489e0f4d2SKip Macy 
22570046ce6SJustin T. Gibbs 	mtx_assert(&balloon_mutex, MA_OWNED);
22670046ce6SJustin T. Gibbs 
22770046ce6SJustin T. Gibbs 	if (nr_pages > nitems(frame_list))
22870046ce6SJustin T. Gibbs 		nr_pages = nitems(frame_list);
22989e0f4d2SKip Macy 
23089e0f4d2SKip Macy 	for (i = 0; i < nr_pages; i++) {
231a4667e09SMark Johnston 		/*
232a4667e09SMark Johnston 		 * Zero the page, or else we might be leaking important data to
233a4667e09SMark Johnston 		 * other domains on the same host. Xen doesn't scrub ballooned
234a4667e09SMark Johnston 		 * out memory pages, the guest is in charge of making sure that
235a4667e09SMark Johnston 		 * no information is leaked.
236a4667e09SMark Johnston 		 */
237a4667e09SMark Johnston 		if ((page = vm_page_alloc_noobj(VM_ALLOC_ZERO)) == NULL) {
23889e0f4d2SKip Macy 			nr_pages = i;
23989e0f4d2SKip Macy 			need_sleep = 1;
24089e0f4d2SKip Macy 			break;
24189e0f4d2SKip Macy 		}
24212678024SDoug Rabson 
2432f9ec994SRoger Pau Monné 		frame_list[i] = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
24412678024SDoug Rabson 
24568e58ea7SRoger Pau Monné 		TAILQ_INSERT_HEAD(&ballooned_pages, page, plinks.q);
24668e58ea7SRoger Pau Monné 		bs.balloon_low++;
24789e0f4d2SKip Macy 	}
24889e0f4d2SKip Macy 
24912678024SDoug Rabson 	set_xen_guest_handle(reservation.extent_start, frame_list);
25089e0f4d2SKip Macy 	reservation.nr_extents   = nr_pages;
25189e0f4d2SKip Macy 	ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
25212678024SDoug Rabson 	KASSERT(ret == nr_pages, ("HYPERVISOR_memory_op failed"));
25389e0f4d2SKip Macy 
25412678024SDoug Rabson 	bs.current_pages -= nr_pages;
25589e0f4d2SKip Macy 
25612678024SDoug Rabson 	return (need_sleep);
25789e0f4d2SKip Macy }
25889e0f4d2SKip Macy 
25989e0f4d2SKip Macy /*
26089e0f4d2SKip Macy  * We avoid multiple worker processes conflicting via the balloon mutex.
26189e0f4d2SKip Macy  * We may of course race updates of the target counts (which are protected
26289e0f4d2SKip Macy  * by the balloon lock), or with changes to the Xen hard limit, but we will
26389e0f4d2SKip Macy  * recover from these in time.
26489e0f4d2SKip Macy  */
26589e0f4d2SKip Macy static void
26689e0f4d2SKip Macy balloon_process(void *unused)
26789e0f4d2SKip Macy {
26889e0f4d2SKip Macy 	int need_sleep = 0;
26989e0f4d2SKip Macy 	long credit;
27089e0f4d2SKip Macy 
27112678024SDoug Rabson 	mtx_lock(&balloon_mutex);
27289e0f4d2SKip Macy 	for (;;) {
273c4f9a105SAlexander Motin 		int sleep_time;
274c4f9a105SAlexander Motin 
27589e0f4d2SKip Macy 		do {
27612678024SDoug Rabson 			credit = current_target() - bs.current_pages;
27789e0f4d2SKip Macy 			if (credit > 0)
27889e0f4d2SKip Macy 				need_sleep = (increase_reservation(credit) != 0);
27989e0f4d2SKip Macy 			if (credit < 0)
28089e0f4d2SKip Macy 				need_sleep = (decrease_reservation(-credit) != 0);
28189e0f4d2SKip Macy 
28289e0f4d2SKip Macy 		} while ((credit != 0) && !need_sleep);
28389e0f4d2SKip Macy 
28489e0f4d2SKip Macy 		/* Schedule more work if there is some still to be done. */
28512678024SDoug Rabson 		if (current_target() != bs.current_pages)
286c4f9a105SAlexander Motin 			sleep_time = hz;
287c4f9a105SAlexander Motin 		else
288c4f9a105SAlexander Motin 			sleep_time = 0;
28989e0f4d2SKip Macy 
290c4f9a105SAlexander Motin 		msleep(balloon_process, &balloon_mutex, 0, "balloon",
291c4f9a105SAlexander Motin 		       sleep_time);
29289e0f4d2SKip Macy 	}
29312678024SDoug Rabson 	mtx_unlock(&balloon_mutex);
29489e0f4d2SKip Macy }
29589e0f4d2SKip Macy 
29689e0f4d2SKip Macy /* Resets the Xen limit, sets new target, and kicks off processing. */
29789e0f4d2SKip Macy static void
29889e0f4d2SKip Macy set_new_target(unsigned long target)
29989e0f4d2SKip Macy {
30089e0f4d2SKip Macy 	/* No need for lock. Not read-modify-write updates. */
30112678024SDoug Rabson 	bs.hard_limit   = ~0UL;
30212678024SDoug Rabson 	bs.target_pages = max(target, minimum_target());
30389e0f4d2SKip Macy 	wakeup(balloon_process);
30489e0f4d2SKip Macy }
30589e0f4d2SKip Macy 
306ff662b5cSJustin T. Gibbs static struct xs_watch target_watch =
30789e0f4d2SKip Macy {
3084e4e43dcSRoger Pau Monné 	.node = "memory/target",
3094e4e43dcSRoger Pau Monné 	.max_pending = 1,
31089e0f4d2SKip Macy };
31189e0f4d2SKip Macy 
31289e0f4d2SKip Macy /* React to a change in the target key */
31389e0f4d2SKip Macy static void
314ff662b5cSJustin T. Gibbs watch_target(struct xs_watch *watch,
31589e0f4d2SKip Macy 	     const char **vec, unsigned int len)
31689e0f4d2SKip Macy {
31789e0f4d2SKip Macy 	unsigned long long new_target;
31889e0f4d2SKip Macy 	int err;
31989e0f4d2SKip Macy 
320ff662b5cSJustin T. Gibbs 	err = xs_scanf(XST_NIL, "memory", "target", NULL,
32112678024SDoug Rabson 	    "%llu", &new_target);
32212678024SDoug Rabson 	if (err) {
32389e0f4d2SKip Macy 		/* This is ok (for domain0 at least) - so just return */
32489e0f4d2SKip Macy 		return;
32589e0f4d2SKip Macy 	}
32689e0f4d2SKip Macy 
32770046ce6SJustin T. Gibbs 	/*
32870046ce6SJustin T. Gibbs 	 * The given memory/target value is in KiB, so it needs converting to
32970046ce6SJustin T. Gibbs 	 * pages.  PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
33089e0f4d2SKip Macy 	 */
33170046ce6SJustin T. Gibbs 	set_new_target(new_target >> KB_TO_PAGE_SHIFT);
33289e0f4d2SKip Macy }
33389e0f4d2SKip Macy 
334ae3078d9SRoger Pau Monné /*------------------ Private Device Attachment Functions  --------------------*/
335ae3078d9SRoger Pau Monné /**
336ae3078d9SRoger Pau Monné  * \brief Identify instances of this device type in the system.
337ae3078d9SRoger Pau Monné  *
338ae3078d9SRoger Pau Monné  * \param driver  The driver performing this identify action.
339ae3078d9SRoger Pau Monné  * \param parent  The NewBus parent device for any devices this method adds.
340ae3078d9SRoger Pau Monné  */
34189e0f4d2SKip Macy static void
342ae3078d9SRoger Pau Monné xenballoon_identify(driver_t *driver __unused, device_t parent)
343ae3078d9SRoger Pau Monné {
344ae3078d9SRoger Pau Monné 	/*
345ae3078d9SRoger Pau Monné 	 * A single device instance for our driver is always present
346ae3078d9SRoger Pau Monné 	 * in a system operating under Xen.
347ae3078d9SRoger Pau Monné 	 */
348ae3078d9SRoger Pau Monné 	BUS_ADD_CHILD(parent, 0, driver->name, 0);
349ae3078d9SRoger Pau Monné }
350ae3078d9SRoger Pau Monné 
351ae3078d9SRoger Pau Monné /**
352453130d9SPedro F. Giffuni  * \brief Probe for the existence of the Xen Balloon device
353ae3078d9SRoger Pau Monné  *
354ae3078d9SRoger Pau Monné  * \param dev  NewBus device_t for this Xen control instance.
355ae3078d9SRoger Pau Monné  *
356ae3078d9SRoger Pau Monné  * \return  Always returns 0 indicating success.
357ae3078d9SRoger Pau Monné  */
358ae3078d9SRoger Pau Monné static int
359ae3078d9SRoger Pau Monné xenballoon_probe(device_t dev)
360ae3078d9SRoger Pau Monné {
361ae3078d9SRoger Pau Monné 
362ae3078d9SRoger Pau Monné 	device_set_desc(dev, "Xen Balloon Device");
363ae3078d9SRoger Pau Monné 	return (0);
364ae3078d9SRoger Pau Monné }
365ae3078d9SRoger Pau Monné 
366ae3078d9SRoger Pau Monné /**
367ae3078d9SRoger Pau Monné  * \brief Attach the Xen Balloon device.
368ae3078d9SRoger Pau Monné  *
369ae3078d9SRoger Pau Monné  * \param dev  NewBus device_t for this Xen control instance.
370ae3078d9SRoger Pau Monné  *
371ae3078d9SRoger Pau Monné  * \return  On success, 0. Otherwise an errno value indicating the
372ae3078d9SRoger Pau Monné  *          type of failure.
373ae3078d9SRoger Pau Monné  */
374ae3078d9SRoger Pau Monné static int
375ae3078d9SRoger Pau Monné xenballoon_attach(device_t dev)
37689e0f4d2SKip Macy {
37789e0f4d2SKip Macy 	int err;
37889e0f4d2SKip Macy 
37912678024SDoug Rabson 	mtx_init(&balloon_mutex, "balloon_mutex", NULL, MTX_DEF);
38089e0f4d2SKip Macy 
381cfa0b7b8SRoger Pau Monné 	bs.current_pages = realmem;
38212678024SDoug Rabson 	bs.target_pages  = bs.current_pages;
38312678024SDoug Rabson 	bs.balloon_low   = 0;
38412678024SDoug Rabson 	bs.balloon_high  = 0;
38512678024SDoug Rabson 	bs.driver_pages  = 0UL;
38612678024SDoug Rabson 	bs.hard_limit    = ~0UL;
38789e0f4d2SKip Macy 
38812678024SDoug Rabson 	kproc_create(balloon_process, NULL, NULL, 0, 0, "balloon");
38989e0f4d2SKip Macy 
39089e0f4d2SKip Macy 	target_watch.callback = watch_target;
39189e0f4d2SKip Macy 
392ae3078d9SRoger Pau Monné 	err = xs_register_watch(&target_watch);
393ae3078d9SRoger Pau Monné 	if (err)
394ae3078d9SRoger Pau Monné 		device_printf(dev,
395ae3078d9SRoger Pau Monné 		    "xenballon: failed to set balloon watcher\n");
39612678024SDoug Rabson 
397ae3078d9SRoger Pau Monné 	return (err);
39889e0f4d2SKip Macy }
399ae3078d9SRoger Pau Monné 
400ae3078d9SRoger Pau Monné /*-------------------- Private Device Attachment Data  -----------------------*/
401ae3078d9SRoger Pau Monné static device_method_t xenballoon_methods[] = {
402ae3078d9SRoger Pau Monné 	/* Device interface */
403ae3078d9SRoger Pau Monné 	DEVMETHOD(device_identify,	xenballoon_identify),
404ae3078d9SRoger Pau Monné 	DEVMETHOD(device_probe,         xenballoon_probe),
405ae3078d9SRoger Pau Monné 	DEVMETHOD(device_attach,        xenballoon_attach),
406ae3078d9SRoger Pau Monné 
407ae3078d9SRoger Pau Monné 	DEVMETHOD_END
408ae3078d9SRoger Pau Monné };
409ae3078d9SRoger Pau Monné 
410ae3078d9SRoger Pau Monné DEFINE_CLASS_0(xenballoon, xenballoon_driver, xenballoon_methods, 0);
411ae3078d9SRoger Pau Monné 
412*f929eb1eSJohn Baldwin DRIVER_MODULE(xenballoon, xenstore, xenballoon_driver, NULL, NULL);
413