xref: /freebsd/sys/dev/vmware/vmci/vmci_kernel_if.h (revision 9768746b)
1 /*-
2  * Copyright (c) 2018 VMware, Inc.
3  *
4  * SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
5  *
6  * $FreeBSD$
7  */
8 
9 /* This file defines helper functions */
10 
11 #ifndef	_VMCI_KERNEL_IF_H_
12 #define	_VMCI_KERNEL_IF_H_
13 
14 #include <sys/param.h>
15 #include <sys/lock.h>
16 #include <sys/mutex.h>
17 #include <sys/queue.h>
18 #include <sys/sema.h>
19 
20 #include "vmci_defs.h"
21 
22 #define VMCI_MEMORY_NORMAL		0x0
23 #define VMCI_MEMORY_ATOMIC		0x1
24 
25 #define vmci_list(_l)			LIST_HEAD(, _l)
26 #define vmci_list_item(_l)		LIST_ENTRY(_l)
27 #define vmci_list_init(_l)		LIST_INIT(_l)
28 #define vmci_list_empty(_l)		LIST_EMPTY(_l)
29 #define vmci_list_first(_l)		LIST_FIRST(_l)
30 #define vmci_list_next(e, f)		LIST_NEXT(e, f)
31 #define vmci_list_insert(_l, _e, n)	LIST_INSERT_HEAD(_l, _e, n)
32 #define vmci_list_remove(_e, n)		LIST_REMOVE(_e, n)
33 #define vmci_list_scan(v, _l, n)	LIST_FOREACH(v, _l, n)
34 #define vmci_list_scan_safe(_e, _l, n, t)				\
35 	LIST_FOREACH_SAFE(_e, _l, n, t)
36 #define vmci_list_swap(_l1, _l2, t, f)	LIST_SWAP(_l1, _l2, t, f)
37 
38 typedef unsigned short int vmci_io_port;
39 typedef int vmci_io_handle;
40 
41 void	vmci_read_port_bytes(vmci_io_handle handle, vmci_io_port port,
42 	    uint8_t *buffer, size_t buffer_length);
43 
44 typedef struct mtx vmci_lock;
45 int	vmci_init_lock(vmci_lock *lock, char *name);
46 void	vmci_cleanup_lock(vmci_lock *lock);
47 void	vmci_grab_lock(vmci_lock *lock);
48 void	vmci_release_lock(vmci_lock *lock);
49 void	vmci_grab_lock_bh(vmci_lock *lock);
50 void	vmci_release_lock_bh(vmci_lock *lock);
51 int	vmci_initialized_lock(vmci_lock *lock);
52 
53 void	*vmci_alloc_kernel_mem(size_t size, int flags);
54 void	vmci_free_kernel_mem(void *ptr, size_t size);
55 
56 typedef struct sema vmci_event;
57 typedef int (*vmci_event_release_cb)(void *client_data);
58 void	vmci_create_event(vmci_event *event);
59 void	vmci_destroy_event(vmci_event *event);
60 void	vmci_signal_event(vmci_event *event);
61 void	vmci_wait_on_event(vmci_event *event, vmci_event_release_cb release_cb,
62 	    void *client_data);
63 bool	vmci_wait_on_event_interruptible(vmci_event *event,
64 	    vmci_event_release_cb release_cb, void *client_data);
65 
66 typedef void (vmci_work_fn)(void *data);
67 bool	vmci_can_schedule_delayed_work(void);
68 int	vmci_schedule_delayed_work(vmci_work_fn *work_fn, void *data);
69 void	vmci_delayed_work_cb(void *context, int data);
70 
71 typedef struct mtx vmci_mutex;
72 int	vmci_mutex_init(vmci_mutex *mutex, char *name);
73 void	vmci_mutex_destroy(vmci_mutex *mutex);
74 void	vmci_mutex_acquire(vmci_mutex *mutex);
75 void	vmci_mutex_release(vmci_mutex *mutex);
76 int	vmci_mutex_initialized(vmci_mutex *mutex);
77 
78 void	*vmci_alloc_queue(uint64_t size, uint32_t flags);
79 void	vmci_free_queue(void *q, uint64_t size);
80 
81 typedef PPN *vmci_ppn_list;
82 struct ppn_set {
83 	uint64_t	num_produce_pages;
84 	uint64_t	num_consume_pages;
85 	vmci_ppn_list	produce_ppns;
86 	vmci_ppn_list	consume_ppns;
87 	bool		initialized;
88 };
89 
90 int	vmci_alloc_ppn_set(void *produce_q, uint64_t num_produce_pages,
91 	    void *consume_q, uint64_t num_consume_pages,
92 	    struct ppn_set *ppn_set);
93 void	vmci_free_ppn_set(struct ppn_set *ppn_set);
94 int	vmci_populate_ppn_list(uint8_t *call_buf, const struct ppn_set *ppnset);
95 
96 #endif /* !_VMCI_KERNEL_IF_H_ */
97