xref: /freebsd/sys/dev/virtio/balloon/virtio_balloon.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1336f459cSPeter Grehan /*-
27282444bSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
37282444bSPedro F. Giffuni  *
410b59a9bSPeter Grehan  * This header is BSD licensed so anyone can use the definitions to implement
510b59a9bSPeter Grehan  * compatible drivers/servers.
610b59a9bSPeter Grehan  *
7336f459cSPeter Grehan  * Redistribution and use in source and binary forms, with or without
8336f459cSPeter Grehan  * modification, are permitted provided that the following conditions
9336f459cSPeter Grehan  * are met:
10336f459cSPeter Grehan  * 1. Redistributions of source code must retain the above copyright
11336f459cSPeter Grehan  *    notice, this list of conditions and the following disclaimer.
12336f459cSPeter Grehan  * 2. Redistributions in binary form must reproduce the above copyright
13336f459cSPeter Grehan  *    notice, this list of conditions and the following disclaimer in the
14336f459cSPeter Grehan  *    documentation and/or other materials provided with the distribution.
15336f459cSPeter Grehan  * 3. Neither the name of IBM nor the names of its contributors
16336f459cSPeter Grehan  *    may be used to endorse or promote products derived from this software
17336f459cSPeter Grehan  *    without specific prior written permission.
18336f459cSPeter Grehan  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19336f459cSPeter Grehan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20336f459cSPeter Grehan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21336f459cSPeter Grehan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
22336f459cSPeter Grehan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23336f459cSPeter Grehan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24336f459cSPeter Grehan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25336f459cSPeter Grehan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26336f459cSPeter Grehan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27336f459cSPeter Grehan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28336f459cSPeter Grehan  * SUCH DAMAGE.
2910b59a9bSPeter Grehan  */
3010b59a9bSPeter Grehan 
3110b59a9bSPeter Grehan #ifndef _VIRTIO_BALLOON_H
3210b59a9bSPeter Grehan #define _VIRTIO_BALLOON_H
3310b59a9bSPeter Grehan 
3410b59a9bSPeter Grehan /* Feature bits. */
3510b59a9bSPeter Grehan #define VIRTIO_BALLOON_F_MUST_TELL_HOST	0x1 /* Tell before reclaiming pages */
3610b59a9bSPeter Grehan #define VIRTIO_BALLOON_F_STATS_VQ	0x2 /* Memory stats virtqueue */
37*f7f9c266SBryan Venteicher #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM	0x4 /* Deflate balloon on OOM */
3810b59a9bSPeter Grehan 
3910b59a9bSPeter Grehan /* Size of a PFN in the balloon interface. */
4010b59a9bSPeter Grehan #define VIRTIO_BALLOON_PFN_SHIFT 12
4110b59a9bSPeter Grehan 
4210b59a9bSPeter Grehan struct virtio_balloon_config {
4310b59a9bSPeter Grehan 	/* Number of pages host wants Guest to give up. */
4410b59a9bSPeter Grehan 	uint32_t num_pages;
4510b59a9bSPeter Grehan 
4610b59a9bSPeter Grehan 	/* Number of pages we've actually got in balloon. */
4710b59a9bSPeter Grehan 	uint32_t actual;
4810b59a9bSPeter Grehan };
4910b59a9bSPeter Grehan 
5010b59a9bSPeter Grehan #define VIRTIO_BALLOON_S_SWAP_IN  0   /* Amount of memory swapped in */
5110b59a9bSPeter Grehan #define VIRTIO_BALLOON_S_SWAP_OUT 1   /* Amount of memory swapped out */
5210b59a9bSPeter Grehan #define VIRTIO_BALLOON_S_MAJFLT   2   /* Number of major faults */
5310b59a9bSPeter Grehan #define VIRTIO_BALLOON_S_MINFLT   3   /* Number of minor faults */
5410b59a9bSPeter Grehan #define VIRTIO_BALLOON_S_MEMFREE  4   /* Total amount of free memory */
5510b59a9bSPeter Grehan #define VIRTIO_BALLOON_S_MEMTOT   5   /* Total amount of memory */
56*f7f9c266SBryan Venteicher #define VIRTIO_BALLOON_S_AVAIL    6   /* Available memory as in /proc */
57*f7f9c266SBryan Venteicher #define VIRTIO_BALLOON_S_CACHES   7   /* Disk caches */
58*f7f9c266SBryan Venteicher #define VIRTIO_BALLOON_S_NR       8
5910b59a9bSPeter Grehan 
60*f7f9c266SBryan Venteicher /*
61*f7f9c266SBryan Venteicher  * Memory statistics structure.
62*f7f9c266SBryan Venteicher  * Driver fills an array of these structures and passes to device.
63*f7f9c266SBryan Venteicher  *
64*f7f9c266SBryan Venteicher  * NOTE: fields are laid out in a way that would make compiler add padding
65*f7f9c266SBryan Venteicher  * between and after fields, so we have to use compiler-specific attributes to
66*f7f9c266SBryan Venteicher  * pack it, to disable this padding. This also often causes compiler to
67*f7f9c266SBryan Venteicher  * generate suboptimal code.
68*f7f9c266SBryan Venteicher  *
69*f7f9c266SBryan Venteicher  * We maintain this statistics structure format for backwards compatibility,
70*f7f9c266SBryan Venteicher  * but don't follow this example.
71*f7f9c266SBryan Venteicher  *
72*f7f9c266SBryan Venteicher  * If implementing a similar structure, do something like the below instead:
73*f7f9c266SBryan Venteicher  *     struct virtio_balloon_stat {
74*f7f9c266SBryan Venteicher  *         __virtio16 tag;
75*f7f9c266SBryan Venteicher  *         __u8 reserved[6];
76*f7f9c266SBryan Venteicher  *         __virtio64 val;
77*f7f9c266SBryan Venteicher  *     };
78*f7f9c266SBryan Venteicher  *
79*f7f9c266SBryan Venteicher  * In other words, add explicit reserved fields to align field and
80*f7f9c266SBryan Venteicher  * structure boundaries at field size, avoiding compiler padding
81*f7f9c266SBryan Venteicher  * without the packed attribute.
82*f7f9c266SBryan Venteicher  */
8310b59a9bSPeter Grehan struct virtio_balloon_stat {
8410b59a9bSPeter Grehan 	uint16_t tag;
8510b59a9bSPeter Grehan 	uint64_t val;
8610b59a9bSPeter Grehan } __packed;
8710b59a9bSPeter Grehan 
8810b59a9bSPeter Grehan #endif /* _VIRTIO_BALLOON_H */
89