xref: /openbmc/linux/drivers/virt/vboxguest/vboxguest_core.h (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
10ba002bcSHans de Goede /* SPDX-License-Identifier: (GPL-2.0 OR CDDL-1.0) */
20ba002bcSHans de Goede /* Copyright (C) 2010-2016 Oracle Corporation */
30ba002bcSHans de Goede 
40ba002bcSHans de Goede #ifndef __VBOXGUEST_CORE_H__
50ba002bcSHans de Goede #define __VBOXGUEST_CORE_H__
60ba002bcSHans de Goede 
70ba002bcSHans de Goede #include <linux/input.h>
80ba002bcSHans de Goede #include <linux/interrupt.h>
90ba002bcSHans de Goede #include <linux/kernel.h>
100ba002bcSHans de Goede #include <linux/list.h>
110ba002bcSHans de Goede #include <linux/miscdevice.h>
120ba002bcSHans de Goede #include <linux/spinlock.h>
130ba002bcSHans de Goede #include <linux/wait.h>
140ba002bcSHans de Goede #include <linux/workqueue.h>
150ba002bcSHans de Goede #include <linux/vboxguest.h>
160ba002bcSHans de Goede #include "vmmdev.h"
170ba002bcSHans de Goede 
18f794db68SHans de Goede /*
19f794db68SHans de Goede  * The mainline kernel version (this version) of the vboxguest module
20f794db68SHans de Goede  * contained a bug where it defined VBGL_IOCTL_VMMDEV_REQUEST_BIG and
21f794db68SHans de Goede  * VBGL_IOCTL_LOG using _IOC(_IOC_READ | _IOC_WRITE, 'V', ...) instead
22f794db68SHans de Goede  * of _IO(V, ...) as the out of tree VirtualBox upstream version does.
23f794db68SHans de Goede  *
24f794db68SHans de Goede  * These _ALT definitions keep compatibility with the wrong defines the
25f794db68SHans de Goede  * mainline kernel version used for a while.
26f794db68SHans de Goede  * Note the VirtualBox userspace bits have always been built against
27f794db68SHans de Goede  * VirtualBox upstream's headers, so this is likely not necessary. But
28f794db68SHans de Goede  * we must never break our ABI so we keep these around to be 100% sure.
29f794db68SHans de Goede  */
30f794db68SHans de Goede #define VBG_IOCTL_VMMDEV_REQUEST_BIG_ALT _IOC(_IOC_READ | _IOC_WRITE, 'V', 3, 0)
31f794db68SHans de Goede #define VBG_IOCTL_LOG_ALT(s)             _IOC(_IOC_READ | _IOC_WRITE, 'V', 9, s)
32f794db68SHans de Goede 
330ba002bcSHans de Goede struct vbg_session;
340ba002bcSHans de Goede 
350ba002bcSHans de Goede /** VBox guest memory balloon. */
360ba002bcSHans de Goede struct vbg_mem_balloon {
370ba002bcSHans de Goede 	/** Work handling VMMDEV_EVENT_BALLOON_CHANGE_REQUEST events */
380ba002bcSHans de Goede 	struct work_struct work;
390ba002bcSHans de Goede 	/** Pre-allocated vmmdev_memballoon_info req for query */
400ba002bcSHans de Goede 	struct vmmdev_memballoon_info *get_req;
410ba002bcSHans de Goede 	/** Pre-allocated vmmdev_memballoon_change req for inflate / deflate */
420ba002bcSHans de Goede 	struct vmmdev_memballoon_change *change_req;
430ba002bcSHans de Goede 	/** The current number of chunks in the balloon. */
440ba002bcSHans de Goede 	u32 chunks;
450ba002bcSHans de Goede 	/** The maximum number of chunks in the balloon. */
460ba002bcSHans de Goede 	u32 max_chunks;
470ba002bcSHans de Goede 	/**
480ba002bcSHans de Goede 	 * Array of pointers to page arrays. A page * array is allocated for
490ba002bcSHans de Goede 	 * each chunk when inflating, and freed when the deflating.
500ba002bcSHans de Goede 	 */
510ba002bcSHans de Goede 	struct page ***pages;
520ba002bcSHans de Goede };
530ba002bcSHans de Goede 
540ba002bcSHans de Goede /**
550ba002bcSHans de Goede  * Per bit usage tracker for a u32 mask.
560ba002bcSHans de Goede  *
570ba002bcSHans de Goede  * Used for optimal handling of guest properties and event filter.
580ba002bcSHans de Goede  */
590ba002bcSHans de Goede struct vbg_bit_usage_tracker {
600ba002bcSHans de Goede 	/** Per bit usage counters. */
610ba002bcSHans de Goede 	u32 per_bit_usage[32];
620ba002bcSHans de Goede 	/** The current mask according to per_bit_usage. */
630ba002bcSHans de Goede 	u32 mask;
640ba002bcSHans de Goede };
650ba002bcSHans de Goede 
660ba002bcSHans de Goede /** VBox guest device (data) extension. */
670ba002bcSHans de Goede struct vbg_dev {
680ba002bcSHans de Goede 	struct device *dev;
690ba002bcSHans de Goede 	/** The base of the adapter I/O ports. */
700ba002bcSHans de Goede 	u16 io_port;
710ba002bcSHans de Goede 	/** Pointer to the mapping of the VMMDev adapter memory. */
720ba002bcSHans de Goede 	struct vmmdev_memory *mmio;
730ba002bcSHans de Goede 	/** Host version */
740ba002bcSHans de Goede 	char host_version[64];
750ba002bcSHans de Goede 	/** Host features */
760ba002bcSHans de Goede 	unsigned int host_features;
770ba002bcSHans de Goede 	/**
780ba002bcSHans de Goede 	 * Dummy page and vmap address for reserved kernel virtual-address
790ba002bcSHans de Goede 	 * space for the guest mappings, only used on hosts lacking vtx.
800ba002bcSHans de Goede 	 */
810ba002bcSHans de Goede 	struct page *guest_mappings_dummy_page;
820ba002bcSHans de Goede 	void *guest_mappings;
830ba002bcSHans de Goede 	/** Spinlock protecting pending_events. */
840ba002bcSHans de Goede 	spinlock_t event_spinlock;
850ba002bcSHans de Goede 	/** Preallocated struct vmmdev_events for the IRQ handler. */
860ba002bcSHans de Goede 	struct vmmdev_events *ack_events_req;
870ba002bcSHans de Goede 	/** Wait-for-event list for threads waiting for multiple events. */
880ba002bcSHans de Goede 	wait_queue_head_t event_wq;
890ba002bcSHans de Goede 	/** Mask of pending events. */
900ba002bcSHans de Goede 	u32 pending_events;
910ba002bcSHans de Goede 	/** Wait-for-event list for threads waiting on HGCM async completion. */
920ba002bcSHans de Goede 	wait_queue_head_t hgcm_wq;
930ba002bcSHans de Goede 	/** Pre-allocated hgcm cancel2 req. for cancellation on timeout */
940ba002bcSHans de Goede 	struct vmmdev_hgcm_cancel2 *cancel_req;
950ba002bcSHans de Goede 	/** Mutex protecting cancel_req accesses */
960ba002bcSHans de Goede 	struct mutex cancel_req_mutex;
970ba002bcSHans de Goede 	/** Pre-allocated mouse-status request for the input-device handling. */
980ba002bcSHans de Goede 	struct vmmdev_mouse_status *mouse_status_req;
990ba002bcSHans de Goede 	/** Input device for reporting abs mouse coordinates to the guest. */
1000ba002bcSHans de Goede 	struct input_dev *input;
1010ba002bcSHans de Goede 
1020ba002bcSHans de Goede 	/** Memory balloon information. */
1030ba002bcSHans de Goede 	struct vbg_mem_balloon mem_balloon;
1040ba002bcSHans de Goede 
1050ba002bcSHans de Goede 	/** Lock for session related items in vbg_dev and vbg_session */
1060ba002bcSHans de Goede 	struct mutex session_mutex;
1070ba002bcSHans de Goede 	/** Events we won't permit anyone to filter out. */
1080ba002bcSHans de Goede 	u32 fixed_events;
1090ba002bcSHans de Goede 	/**
1100ba002bcSHans de Goede 	 * Usage counters for the host events (excludes fixed events),
1110ba002bcSHans de Goede 	 * Protected by session_mutex.
1120ba002bcSHans de Goede 	 */
1130ba002bcSHans de Goede 	struct vbg_bit_usage_tracker event_filter_tracker;
1140ba002bcSHans de Goede 	/**
1150ba002bcSHans de Goede 	 * The event filter last reported to the host (or UINT32_MAX).
1160ba002bcSHans de Goede 	 * Protected by session_mutex.
1170ba002bcSHans de Goede 	 */
1180ba002bcSHans de Goede 	u32 event_filter_host;
1190ba002bcSHans de Goede 
1200ba002bcSHans de Goede 	/**
121*631beddcSHans de Goede 	 * Guest capabilities which have been switched to acquire_mode.
122*631beddcSHans de Goede 	 */
123*631beddcSHans de Goede 	u32 acquire_mode_guest_caps;
124*631beddcSHans de Goede 	/**
125*631beddcSHans de Goede 	 * Guest capabilities acquired by vbg_acquire_session_capabilities().
126*631beddcSHans de Goede 	 * Only one session can acquire a capability at a time.
127*631beddcSHans de Goede 	 */
128*631beddcSHans de Goede 	u32 acquired_guest_caps;
129*631beddcSHans de Goede 	/**
130fd01cc59SHans de Goede 	 * Usage counters for guest capabilities requested through
131fd01cc59SHans de Goede 	 * vbg_set_session_capabilities(). Indexed by capability bit
1320ba002bcSHans de Goede 	 * number, one count per session using a capability.
1330ba002bcSHans de Goede 	 * Protected by session_mutex.
1340ba002bcSHans de Goede 	 */
135fd01cc59SHans de Goede 	struct vbg_bit_usage_tracker set_guest_caps_tracker;
1360ba002bcSHans de Goede 	/**
1370ba002bcSHans de Goede 	 * The guest capabilities last reported to the host (or UINT32_MAX).
1380ba002bcSHans de Goede 	 * Protected by session_mutex.
1390ba002bcSHans de Goede 	 */
1400ba002bcSHans de Goede 	u32 guest_caps_host;
1410ba002bcSHans de Goede 
1420ba002bcSHans de Goede 	/**
1430ba002bcSHans de Goede 	 * Heartbeat timer which fires with interval
1440ba002bcSHans de Goede 	 * cNsHearbeatInterval and its handler sends
1450ba002bcSHans de Goede 	 * VMMDEVREQ_GUEST_HEARTBEAT to VMMDev.
1460ba002bcSHans de Goede 	 */
1470ba002bcSHans de Goede 	struct timer_list heartbeat_timer;
1480ba002bcSHans de Goede 	/** Heartbeat timer interval in ms. */
1490ba002bcSHans de Goede 	int heartbeat_interval_ms;
1500ba002bcSHans de Goede 	/** Preallocated VMMDEVREQ_GUEST_HEARTBEAT request. */
1510ba002bcSHans de Goede 	struct vmmdev_request_header *guest_heartbeat_req;
1520ba002bcSHans de Goede 
1530ba002bcSHans de Goede 	/** "vboxguest" char-device */
1540ba002bcSHans de Goede 	struct miscdevice misc_device;
1550ba002bcSHans de Goede 	/** "vboxuser" char-device */
1560ba002bcSHans de Goede 	struct miscdevice misc_device_user;
1570ba002bcSHans de Goede };
1580ba002bcSHans de Goede 
1590ba002bcSHans de Goede /** The VBoxGuest per session data. */
1600ba002bcSHans de Goede struct vbg_session {
1610ba002bcSHans de Goede 	/** Pointer to the device extension. */
1620ba002bcSHans de Goede 	struct vbg_dev *gdev;
1630ba002bcSHans de Goede 
1640ba002bcSHans de Goede 	/**
1650ba002bcSHans de Goede 	 * Array containing HGCM client IDs associated with this session.
1660ba002bcSHans de Goede 	 * These will be automatically disconnected when the session is closed.
1670ba002bcSHans de Goede 	 * Protected by vbg_gdev.session_mutex.
1680ba002bcSHans de Goede 	 */
1690ba002bcSHans de Goede 	u32 hgcm_client_ids[64];
1700ba002bcSHans de Goede 	/**
1710ba002bcSHans de Goede 	 * Host events requested by the session.
1720ba002bcSHans de Goede 	 * An event type requested in any guest session will be added to the
1730ba002bcSHans de Goede 	 * host filter. Protected by vbg_gdev.session_mutex.
1740ba002bcSHans de Goede 	 */
1750ba002bcSHans de Goede 	u32 event_filter;
1760ba002bcSHans de Goede 	/**
177*631beddcSHans de Goede 	 * Guest capabilities acquired by vbg_acquire_session_capabilities().
178*631beddcSHans de Goede 	 * Only one session can acquire a capability at a time.
179*631beddcSHans de Goede 	 */
180*631beddcSHans de Goede 	u32 acquired_guest_caps;
181*631beddcSHans de Goede 	/**
182fd01cc59SHans de Goede 	 * Guest capabilities set through vbg_set_session_capabilities().
1830ba002bcSHans de Goede 	 * A capability claimed by any guest session will be reported to the
1840ba002bcSHans de Goede 	 * host. Protected by vbg_gdev.session_mutex.
1850ba002bcSHans de Goede 	 */
186fd01cc59SHans de Goede 	u32 set_guest_caps;
1870532a1b0SHans de Goede 	/** VMMDEV_REQUESTOR_* flags */
1880532a1b0SHans de Goede 	u32 requestor;
1890ba002bcSHans de Goede 	/** Set on CANCEL_ALL_WAITEVENTS, protected by vbg_devevent_spinlock. */
1900ba002bcSHans de Goede 	bool cancel_waiters;
1910ba002bcSHans de Goede };
1920ba002bcSHans de Goede 
1930ba002bcSHans de Goede int  vbg_core_init(struct vbg_dev *gdev, u32 fixed_events);
1940ba002bcSHans de Goede void vbg_core_exit(struct vbg_dev *gdev);
1950532a1b0SHans de Goede struct vbg_session *vbg_core_open_session(struct vbg_dev *gdev, u32 requestor);
1960ba002bcSHans de Goede void vbg_core_close_session(struct vbg_session *session);
1970ba002bcSHans de Goede int  vbg_core_ioctl(struct vbg_session *session, unsigned int req, void *data);
1980ba002bcSHans de Goede int  vbg_core_set_mouse_status(struct vbg_dev *gdev, u32 features);
1990ba002bcSHans de Goede 
2000ba002bcSHans de Goede irqreturn_t vbg_core_isr(int irq, void *dev_id);
2010ba002bcSHans de Goede 
2020ba002bcSHans de Goede void vbg_linux_mouse_event(struct vbg_dev *gdev);
2030ba002bcSHans de Goede 
20402cfde67SHans de Goede /* Private (non exported) functions form vboxguest_utils.c */
2050532a1b0SHans de Goede void *vbg_req_alloc(size_t len, enum vmmdev_request_type req_type,
2060532a1b0SHans de Goede 		    u32 requestor);
207f6f9885bSHans de Goede void vbg_req_free(void *req, size_t len);
20802cfde67SHans de Goede int vbg_req_perform(struct vbg_dev *gdev, void *req);
20902cfde67SHans de Goede int vbg_hgcm_call32(
2100532a1b0SHans de Goede 	struct vbg_dev *gdev, u32 requestor, u32 client_id, u32 function,
2110532a1b0SHans de Goede 	u32 timeout_ms, struct vmmdev_hgcm_function_parameter32 *parm32,
2120532a1b0SHans de Goede 	u32 parm_count, int *vbox_status);
21302cfde67SHans de Goede 
2140ba002bcSHans de Goede #endif
215