1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <linux/objtool.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/cc_platform.h>
33 #include <asm/hypervisor.h>
34 #include <drm/drm_ioctl.h>
36 #include "vmwgfx_drv.h"
37 #include "vmwgfx_msg_x86.h"
38 #include "vmwgfx_msg_arm64.h"
39 #include "vmwgfx_mksstat.h"
41 #define MESSAGE_STATUS_SUCCESS 0x0001
42 #define MESSAGE_STATUS_DORECV 0x0002
43 #define MESSAGE_STATUS_CPT 0x0010
44 #define MESSAGE_STATUS_HB 0x0080
46 #define RPCI_PROTOCOL_NUM 0x49435052
47 #define GUESTMSG_FLAG_COOKIE 0x80000000
51 #define VMW_HYPERVISOR_MAGIC 0x564D5868
53 #define VMW_PORT_CMD_MSG 30
54 #define VMW_PORT_CMD_HB_MSG 0
55 #define VMW_PORT_CMD_OPEN_CHANNEL (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
56 #define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
57 #define VMW_PORT_CMD_SENDSIZE (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
58 #define VMW_PORT_CMD_RECVSIZE (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
59 #define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)
61 #define VMW_PORT_CMD_MKS_GUEST_STATS 85
62 #define VMW_PORT_CMD_MKSGS_RESET (0 << 16 | VMW_PORT_CMD_MKS_GUEST_STATS)
63 #define VMW_PORT_CMD_MKSGS_ADD_PPN (1 << 16 | VMW_PORT_CMD_MKS_GUEST_STATS)
64 #define VMW_PORT_CMD_MKSGS_REMOVE_PPN (2 << 16 | VMW_PORT_CMD_MKS_GUEST_STATS)
66 #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
68 #define MAX_USER_MSG_LENGTH PAGE_SIZE
70 static u32 vmw_msg_enabled = 1;
93 * @channel: RPC channel
96 * Returns: 0 on success
98 static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
100 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
102 VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
103 (protocol | GUESTMSG_FLAG_COOKIE), si, di,
105 VMW_HYPERVISOR_MAGIC,
106 eax, ebx, ecx, edx, si, di);
108 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
111 channel->channel_id = HIGH_WORD(edx);
112 channel->cookie_high = si;
113 channel->cookie_low = di;
123 * @channel: RPC channel
125 * Returns: 0 on success
127 static int vmw_close_channel(struct rpc_channel *channel)
129 unsigned long eax, ebx, ecx, edx, si, di;
131 /* Set up additional parameters */
132 si = channel->cookie_high;
133 di = channel->cookie_low;
135 VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
137 channel->channel_id << 16,
138 VMW_HYPERVISOR_MAGIC,
139 eax, ebx, ecx, edx, si, di);
141 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
148 * vmw_port_hb_out - Send the message payload either through the
149 * high-bandwidth port if available, or through the backdoor otherwise.
150 * @channel: The rpc channel.
151 * @msg: NULL-terminated message.
152 * @hb: Whether the high-bandwidth port is available.
154 * Return: The port status.
156 static unsigned long vmw_port_hb_out(struct rpc_channel *channel,
157 const char *msg, bool hb)
159 unsigned long si, di, eax, ebx, ecx, edx;
160 unsigned long msg_len = strlen(msg);
162 /* HB port can't access encrypted memory. */
163 if (hb && !cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
164 unsigned long bp = channel->cookie_high;
165 u32 channel_id = (channel->channel_id << 16);
167 si = (uintptr_t) msg;
168 di = channel->cookie_low;
171 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
173 VMWARE_HYPERVISOR_HB | channel_id |
174 VMWARE_HYPERVISOR_OUT,
175 VMW_HYPERVISOR_MAGIC, bp,
176 eax, ebx, ecx, edx, si, di);
181 /* HB port not available. Send the message 4 bytes at a time. */
182 ecx = MESSAGE_STATUS_SUCCESS << 16;
183 while (msg_len && (HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS)) {
184 unsigned int bytes = min_t(size_t, msg_len, 4);
185 unsigned long word = 0;
187 memcpy(&word, msg, bytes);
190 si = channel->cookie_high;
191 di = channel->cookie_low;
193 VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_SENDPAYLOAD << 16),
195 channel->channel_id << 16,
196 VMW_HYPERVISOR_MAGIC,
197 eax, ebx, ecx, edx, si, di);
204 * vmw_port_hb_in - Receive the message payload either through the
205 * high-bandwidth port if available, or through the backdoor otherwise.
206 * @channel: The rpc channel.
207 * @reply: Pointer to buffer holding reply.
208 * @reply_len: Length of the reply.
209 * @hb: Whether the high-bandwidth port is available.
211 * Return: The port status.
213 static unsigned long vmw_port_hb_in(struct rpc_channel *channel, char *reply,
214 unsigned long reply_len, bool hb)
216 unsigned long si, di, eax, ebx, ecx, edx;
218 /* HB port can't access encrypted memory */
219 if (hb && !cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
220 unsigned long bp = channel->cookie_low;
221 u32 channel_id = (channel->channel_id << 16);
223 si = channel->cookie_high;
224 di = (uintptr_t) reply;
227 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
229 VMWARE_HYPERVISOR_HB | channel_id,
230 VMW_HYPERVISOR_MAGIC, bp,
231 eax, ebx, ecx, edx, si, di);
236 /* HB port not available. Retrieve the message 4 bytes at a time. */
237 ecx = MESSAGE_STATUS_SUCCESS << 16;
239 unsigned int bytes = min_t(unsigned long, reply_len, 4);
241 si = channel->cookie_high;
242 di = channel->cookie_low;
244 VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_RECVPAYLOAD << 16),
245 MESSAGE_STATUS_SUCCESS, si, di,
246 channel->channel_id << 16,
247 VMW_HYPERVISOR_MAGIC,
248 eax, ebx, ecx, edx, si, di);
250 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
253 memcpy(reply, &ebx, bytes);
263 * vmw_send_msg: Sends a message to the host
265 * @channel: RPC channel
266 * @msg: NULL terminated string
268 * Returns: 0 on success
270 static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
272 unsigned long eax, ebx, ecx, edx, si, di;
273 size_t msg_len = strlen(msg);
276 while (retries < RETRIES) {
279 /* Set up additional parameters */
280 si = channel->cookie_high;
281 di = channel->cookie_low;
283 VMW_PORT(VMW_PORT_CMD_SENDSIZE,
285 channel->channel_id << 16,
286 VMW_HYPERVISOR_MAGIC,
287 eax, ebx, ecx, edx, si, di);
289 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
290 /* Expected success. Give up. */
295 ebx = vmw_port_hb_out(channel, msg,
296 !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
298 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
300 } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
301 /* A checkpoint occurred. Retry. */
310 STACK_FRAME_NON_STANDARD(vmw_send_msg);
314 * vmw_recv_msg: Receives a message from the host
316 * Note: It is the caller's responsibility to call kfree() on msg.
318 * @channel: channel opened by vmw_open_channel
319 * @msg: [OUT] message received from the host
320 * @msg_len: message length
322 static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
325 unsigned long eax, ebx, ecx, edx, si, di;
334 while (retries < RETRIES) {
337 /* Set up additional parameters */
338 si = channel->cookie_high;
339 di = channel->cookie_low;
341 VMW_PORT(VMW_PORT_CMD_RECVSIZE,
343 channel->channel_id << 16,
344 VMW_HYPERVISOR_MAGIC,
345 eax, ebx, ecx, edx, si, di);
347 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
348 DRM_ERROR("Failed to get reply size for host message.\n");
352 /* No reply available. This is okay. */
353 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
357 reply = kzalloc(reply_len + 1, GFP_KERNEL);
359 DRM_ERROR("Cannot allocate memory for host message reply.\n");
365 ebx = vmw_port_hb_in(channel, reply, reply_len,
366 !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
367 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
370 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
371 /* A checkpoint occurred. Retry. */
378 reply[reply_len] = '\0';
382 si = channel->cookie_high;
383 di = channel->cookie_low;
385 VMW_PORT(VMW_PORT_CMD_RECVSTATUS,
386 MESSAGE_STATUS_SUCCESS, si, di,
387 channel->channel_id << 16,
388 VMW_HYPERVISOR_MAGIC,
389 eax, ebx, ecx, edx, si, di);
391 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
394 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
395 /* A checkpoint occurred. Retry. */
408 *msg_len = reply_len;
413 STACK_FRAME_NON_STANDARD(vmw_recv_msg);
417 * vmw_host_get_guestinfo: Gets a GuestInfo parameter
419 * Gets the value of a GuestInfo.* parameter. The value returned will be in
420 * a string, and it is up to the caller to post-process.
422 * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
423 * @buffer: if NULL, *reply_len will contain reply size.
424 * @length: size of the reply_buf. Set to size of reply upon return
426 * Returns: 0 on success
428 int vmw_host_get_guestinfo(const char *guest_info_param,
429 char *buffer, size_t *length)
431 struct rpc_channel channel;
432 char *msg, *reply = NULL;
433 size_t reply_len = 0;
435 if (!vmw_msg_enabled)
438 if (!guest_info_param || !length)
441 msg = kasprintf(GFP_KERNEL, "info-get %s", guest_info_param);
443 DRM_ERROR("Cannot allocate memory to get guest info \"%s\".",
448 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
451 if (vmw_send_msg(&channel, msg) ||
452 vmw_recv_msg(&channel, (void *) &reply, &reply_len))
455 vmw_close_channel(&channel);
456 if (buffer && reply && reply_len > 0) {
457 /* Remove reply code, which are the first 2 characters of
460 reply_len = max(reply_len - 2, (size_t) 0);
461 reply_len = min(reply_len, *length);
464 memcpy(buffer, reply + 2, reply_len);
475 vmw_close_channel(&channel);
480 DRM_ERROR("Failed to get guest info \"%s\".", guest_info_param);
487 * vmw_host_printf: Sends a log message to the host
489 * @fmt: Regular printf format string and arguments
491 * Returns: 0 on success
494 int vmw_host_printf(const char *fmt, ...)
497 struct rpc_channel channel;
502 if (!vmw_msg_enabled)
509 log = kvasprintf(GFP_KERNEL, fmt, ap);
512 DRM_ERROR("Cannot allocate memory for the log message.\n");
516 msg = kasprintf(GFP_KERNEL, "log %s", log);
518 DRM_ERROR("Cannot allocate memory for host log message.\n");
523 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
526 if (vmw_send_msg(&channel, msg))
529 vmw_close_channel(&channel);
536 vmw_close_channel(&channel);
540 DRM_ERROR("Failed to send host log message.\n");
547 * vmw_msg_ioctl: Sends and receveives a message to/from host from/to user-space
549 * Sends a message from user-space to host.
550 * Can also receive a result from host and return that to user-space.
552 * @dev: Identifies the drm device.
553 * @data: Pointer to the ioctl argument.
554 * @file_priv: Identifies the caller.
555 * Return: Zero on success, negative error code on error.
558 int vmw_msg_ioctl(struct drm_device *dev, void *data,
559 struct drm_file *file_priv)
561 struct drm_vmw_msg_arg *arg =
562 (struct drm_vmw_msg_arg *)data;
563 struct rpc_channel channel;
567 msg = kmalloc(MAX_USER_MSG_LENGTH, GFP_KERNEL);
569 DRM_ERROR("Cannot allocate memory for log message.\n");
573 length = strncpy_from_user(msg, (void __user *)((unsigned long)arg->send),
574 MAX_USER_MSG_LENGTH);
575 if (length < 0 || length >= MAX_USER_MSG_LENGTH) {
576 DRM_ERROR("Userspace message access failure.\n");
582 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM)) {
583 DRM_ERROR("Failed to open channel.\n");
587 if (vmw_send_msg(&channel, msg)) {
588 DRM_ERROR("Failed to send message to host.\n");
592 if (!arg->send_only) {
594 size_t reply_len = 0;
596 if (vmw_recv_msg(&channel, (void *) &reply, &reply_len)) {
597 DRM_ERROR("Failed to receive message from host.\n");
600 if (reply && reply_len > 0) {
601 if (copy_to_user((void __user *)((unsigned long)arg->receive),
603 DRM_ERROR("Failed to copy message to userspace.\n");
607 arg->receive_len = (__u32)reply_len;
612 vmw_close_channel(&channel);
618 vmw_close_channel(&channel);
626 * reset_ppn_array: Resets a PPN64 array to INVALID_PPN64 content
628 * @arr: Array to reset.
629 * @size: Array length.
631 static inline void reset_ppn_array(PPN64 *arr, size_t size)
635 BUG_ON(!arr || size == 0);
637 for (i = 0; i < size; ++i)
638 arr[i] = INVALID_PPN64;
642 * hypervisor_ppn_reset_all: Removes all mksGuestStat instance descriptors from
643 * the hypervisor. All related pages should be subsequently unpinned or freed.
646 static inline void hypervisor_ppn_reset_all(void)
648 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
650 VMW_PORT(VMW_PORT_CMD_MKSGS_RESET,
653 VMW_HYPERVISOR_MAGIC,
654 eax, ebx, ecx, edx, si, di);
658 * hypervisor_ppn_add: Adds a single mksGuestStat instance descriptor to the
659 * hypervisor. Any related userspace pages should be pinned in advance.
661 * @pfn: Physical page number of the instance descriptor
663 static inline void hypervisor_ppn_add(PPN64 pfn)
665 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
667 VMW_PORT(VMW_PORT_CMD_MKSGS_ADD_PPN,
668 (unsigned long)pfn, si, di,
670 VMW_HYPERVISOR_MAGIC,
671 eax, ebx, ecx, edx, si, di);
675 * hypervisor_ppn_remove: Removes a single mksGuestStat instance descriptor from
676 * the hypervisor. All related pages should be subsequently unpinned or freed.
678 * @pfn: Physical page number of the instance descriptor
680 static inline void hypervisor_ppn_remove(PPN64 pfn)
682 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
684 VMW_PORT(VMW_PORT_CMD_MKSGS_REMOVE_PPN,
685 (unsigned long)pfn, si, di,
687 VMW_HYPERVISOR_MAGIC,
688 eax, ebx, ecx, edx, si, di);
691 #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
693 /* Order of the total number of pages used for kernel-internal mksGuestStat; at least 2 */
694 #define MKSSTAT_KERNEL_PAGES_ORDER 2
695 /* Header to the text description of mksGuestStat instance descriptor */
696 #define MKSSTAT_KERNEL_DESCRIPTION "vmwgfx"
698 /* Kernel mksGuestStats counter names and desciptions; same order as enum mksstat_kern_stats_t */
699 static const char* const mksstat_kern_name_desc[MKSSTAT_KERN_COUNT][2] =
701 { "vmw_execbuf_ioctl", "vmw_execbuf_ioctl" },
705 * mksstat_init_record: Initializes an MKSGuestStatCounter-based record
706 * for the respective mksGuestStat index.
708 * @stat_idx: Index of the MKSGuestStatCounter-based mksGuestStat record.
709 * @pstat: Pointer to array of MKSGuestStatCounterTime.
710 * @pinfo: Pointer to array of MKSGuestStatInfoEntry.
711 * @pstrs: Pointer to current end of the name/description sequence.
712 * Return: Pointer to the new end of the names/description sequence.
715 static inline char *mksstat_init_record(mksstat_kern_stats_t stat_idx,
716 MKSGuestStatCounterTime *pstat, MKSGuestStatInfoEntry *pinfo, char *pstrs)
718 char *const pstrd = pstrs + strlen(mksstat_kern_name_desc[stat_idx][0]) + 1;
719 strcpy(pstrs, mksstat_kern_name_desc[stat_idx][0]);
720 strcpy(pstrd, mksstat_kern_name_desc[stat_idx][1]);
722 pinfo[stat_idx].name.s = pstrs;
723 pinfo[stat_idx].description.s = pstrd;
724 pinfo[stat_idx].flags = MKS_GUEST_STAT_FLAG_NONE;
725 pinfo[stat_idx].stat.counter = (MKSGuestStatCounter *)&pstat[stat_idx];
727 return pstrd + strlen(mksstat_kern_name_desc[stat_idx][1]) + 1;
731 * mksstat_init_record_time: Initializes an MKSGuestStatCounterTime-based record
732 * for the respective mksGuestStat index.
734 * @stat_idx: Index of the MKSGuestStatCounterTime-based mksGuestStat record.
735 * @pstat: Pointer to array of MKSGuestStatCounterTime.
736 * @pinfo: Pointer to array of MKSGuestStatInfoEntry.
737 * @pstrs: Pointer to current end of the name/description sequence.
738 * Return: Pointer to the new end of the names/description sequence.
741 static inline char *mksstat_init_record_time(mksstat_kern_stats_t stat_idx,
742 MKSGuestStatCounterTime *pstat, MKSGuestStatInfoEntry *pinfo, char *pstrs)
744 char *const pstrd = pstrs + strlen(mksstat_kern_name_desc[stat_idx][0]) + 1;
745 strcpy(pstrs, mksstat_kern_name_desc[stat_idx][0]);
746 strcpy(pstrd, mksstat_kern_name_desc[stat_idx][1]);
748 pinfo[stat_idx].name.s = pstrs;
749 pinfo[stat_idx].description.s = pstrd;
750 pinfo[stat_idx].flags = MKS_GUEST_STAT_FLAG_TIME;
751 pinfo[stat_idx].stat.counterTime = &pstat[stat_idx];
753 return pstrd + strlen(mksstat_kern_name_desc[stat_idx][1]) + 1;
757 * mksstat_init_kern_id: Creates a single mksGuestStat instance descriptor and
758 * kernel-internal counters. Adds PFN mapping to the hypervisor.
760 * Create a single mksGuestStat instance descriptor and corresponding structures
761 * for all kernel-internal counters. The corresponding PFNs are mapped with the
764 * @ppage: Output pointer to page containing the instance descriptor.
765 * Return: Zero on success, negative error code on error.
768 static int mksstat_init_kern_id(struct page **ppage)
770 MKSGuestStatInstanceDescriptor *pdesc;
771 MKSGuestStatCounterTime *pstat;
772 MKSGuestStatInfoEntry *pinfo;
773 char *pstrs, *pstrs_acc;
775 /* Allocate pages for the kernel-internal instance descriptor */
776 struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, MKSSTAT_KERNEL_PAGES_ORDER);
781 pdesc = page_address(page);
782 pstat = vmw_mksstat_get_kern_pstat(pdesc);
783 pinfo = vmw_mksstat_get_kern_pinfo(pdesc);
784 pstrs = vmw_mksstat_get_kern_pstrs(pdesc);
786 /* Set up all kernel-internal counters and corresponding structures */
788 pstrs_acc = mksstat_init_record_time(MKSSTAT_KERN_EXECBUF, pstat, pinfo, pstrs_acc);
790 /* Add new counters above, in their order of appearance in mksstat_kern_stats_t */
792 BUG_ON(pstrs_acc - pstrs > PAGE_SIZE);
794 /* Set up the kernel-internal instance descriptor */
795 pdesc->reservedMBZ = 0;
796 pdesc->statStartVA = (uintptr_t)pstat;
797 pdesc->strsStartVA = (uintptr_t)pstrs;
798 pdesc->statLength = sizeof(*pstat) * MKSSTAT_KERN_COUNT;
799 pdesc->infoLength = sizeof(*pinfo) * MKSSTAT_KERN_COUNT;
800 pdesc->strsLength = pstrs_acc - pstrs;
801 snprintf(pdesc->description, ARRAY_SIZE(pdesc->description) - 1, "%s pid=%d",
802 MKSSTAT_KERNEL_DESCRIPTION, current->pid);
804 pdesc->statPPNs[0] = page_to_pfn(virt_to_page(pstat));
805 reset_ppn_array(pdesc->statPPNs + 1, ARRAY_SIZE(pdesc->statPPNs) - 1);
807 pdesc->infoPPNs[0] = page_to_pfn(virt_to_page(pinfo));
808 reset_ppn_array(pdesc->infoPPNs + 1, ARRAY_SIZE(pdesc->infoPPNs) - 1);
810 pdesc->strsPPNs[0] = page_to_pfn(virt_to_page(pstrs));
811 reset_ppn_array(pdesc->strsPPNs + 1, ARRAY_SIZE(pdesc->strsPPNs) - 1);
815 hypervisor_ppn_add((PPN64)page_to_pfn(page));
821 * vmw_mksstat_get_kern_slot: Acquires a slot for a single kernel-internal
822 * mksGuestStat instance descriptor.
824 * Find a slot for a single kernel-internal mksGuestStat instance descriptor.
825 * In case no such was already present, allocate a new one and set up a kernel-
826 * internal mksGuestStat instance descriptor for the former.
828 * @pid: Process for which a slot is sought.
829 * @dev_priv: Identifies the drm private device.
830 * Return: Non-negative slot on success, negative error code on error.
833 int vmw_mksstat_get_kern_slot(pid_t pid, struct vmw_private *dev_priv)
835 const size_t base = (u32)hash_32(pid, MKSSTAT_CAPACITY_LOG2);
838 for (i = 0; i < ARRAY_SIZE(dev_priv->mksstat_kern_pids); ++i) {
839 const size_t slot = (i + base) % ARRAY_SIZE(dev_priv->mksstat_kern_pids);
841 /* Check if an instance descriptor for this pid is already present */
842 if (pid == (pid_t)atomic_read(&dev_priv->mksstat_kern_pids[slot]))
845 /* Set up a new instance descriptor for this pid */
846 if (!atomic_cmpxchg(&dev_priv->mksstat_kern_pids[slot], 0, MKSSTAT_PID_RESERVED)) {
847 const int ret = mksstat_init_kern_id(&dev_priv->mksstat_kern_pages[slot]);
850 /* Reset top-timer tracking for this slot */
851 dev_priv->mksstat_kern_top_timer[slot] = MKSSTAT_KERN_COUNT;
853 atomic_set(&dev_priv->mksstat_kern_pids[slot], pid);
857 atomic_set(&dev_priv->mksstat_kern_pids[slot], 0);
868 * vmw_mksstat_cleanup_descriptor: Frees a single userspace-originating
869 * mksGuestStat instance-descriptor page and unpins all related user pages.
871 * Unpin all user pages realated to this instance descriptor and free
872 * the instance-descriptor page itself.
874 * @page: Page of the instance descriptor.
877 static void vmw_mksstat_cleanup_descriptor(struct page *page)
879 MKSGuestStatInstanceDescriptor *pdesc = page_address(page);
882 for (i = 0; i < ARRAY_SIZE(pdesc->statPPNs) && pdesc->statPPNs[i] != INVALID_PPN64; ++i)
883 unpin_user_page(pfn_to_page(pdesc->statPPNs[i]));
885 for (i = 0; i < ARRAY_SIZE(pdesc->infoPPNs) && pdesc->infoPPNs[i] != INVALID_PPN64; ++i)
886 unpin_user_page(pfn_to_page(pdesc->infoPPNs[i]));
888 for (i = 0; i < ARRAY_SIZE(pdesc->strsPPNs) && pdesc->strsPPNs[i] != INVALID_PPN64; ++i)
889 unpin_user_page(pfn_to_page(pdesc->strsPPNs[i]));
895 * vmw_mksstat_remove_all: Resets all mksGuestStat instance descriptors
896 * from the hypervisor.
898 * Discard all hypervisor PFN mappings, containing active mksGuestState instance
899 * descriptors, unpin the related userspace pages and free the related kernel pages.
901 * @dev_priv: Identifies the drm private device.
902 * Return: Zero on success, negative error code on error.
905 int vmw_mksstat_remove_all(struct vmw_private *dev_priv)
910 /* Discard all PFN mappings with the hypervisor */
911 hypervisor_ppn_reset_all();
913 /* Discard all userspace-originating instance descriptors and unpin all related pages */
914 for (i = 0; i < ARRAY_SIZE(dev_priv->mksstat_user_pids); ++i) {
915 const pid_t pid0 = (pid_t)atomic_read(&dev_priv->mksstat_user_pids[i]);
920 if (pid0 != MKSSTAT_PID_RESERVED) {
921 const pid_t pid1 = atomic_cmpxchg(&dev_priv->mksstat_user_pids[i], pid0, MKSSTAT_PID_RESERVED);
927 struct page *const page = dev_priv->mksstat_user_pages[i];
931 dev_priv->mksstat_user_pages[i] = NULL;
932 atomic_set(&dev_priv->mksstat_user_pids[i], 0);
934 vmw_mksstat_cleanup_descriptor(page);
942 #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
943 /* Discard all kernel-internal instance descriptors and free all related pages */
944 for (i = 0; i < ARRAY_SIZE(dev_priv->mksstat_kern_pids); ++i) {
945 const pid_t pid0 = (pid_t)atomic_read(&dev_priv->mksstat_kern_pids[i]);
950 if (pid0 != MKSSTAT_PID_RESERVED) {
951 const pid_t pid1 = atomic_cmpxchg(&dev_priv->mksstat_kern_pids[i], pid0, MKSSTAT_PID_RESERVED);
957 struct page *const page = dev_priv->mksstat_kern_pages[i];
961 dev_priv->mksstat_kern_pages[i] = NULL;
962 atomic_set(&dev_priv->mksstat_kern_pids[i], 0);
964 __free_pages(page, MKSSTAT_KERNEL_PAGES_ORDER);
977 * vmw_mksstat_reset_ioctl: Resets all mksGuestStat instance descriptors
978 * from the hypervisor.
980 * Discard all hypervisor PFN mappings, containing active mksGuestStat instance
981 * descriptors, unpin the related userspace pages and free the related kernel pages.
983 * @dev: Identifies the drm device.
984 * @data: Pointer to the ioctl argument.
985 * @file_priv: Identifies the caller; unused.
986 * Return: Zero on success, negative error code on error.
989 int vmw_mksstat_reset_ioctl(struct drm_device *dev, void *data,
990 struct drm_file *file_priv)
992 struct vmw_private *const dev_priv = vmw_priv(dev);
993 return vmw_mksstat_remove_all(dev_priv);
997 * vmw_mksstat_add_ioctl: Creates a single userspace-originating mksGuestStat
998 * instance descriptor and registers that with the hypervisor.
1000 * Create a hypervisor PFN mapping, containing a single mksGuestStat instance
1001 * descriptor and pin the corresponding userspace pages.
1003 * @dev: Identifies the drm device.
1004 * @data: Pointer to the ioctl argument.
1005 * @file_priv: Identifies the caller; unused.
1006 * Return: Zero on success, negative error code on error.
1009 int vmw_mksstat_add_ioctl(struct drm_device *dev, void *data,
1010 struct drm_file *file_priv)
1012 struct drm_vmw_mksstat_add_arg *arg =
1013 (struct drm_vmw_mksstat_add_arg *) data;
1015 struct vmw_private *const dev_priv = vmw_priv(dev);
1018 MKSGuestStatInstanceDescriptor *pdesc;
1019 const size_t num_pages_stat = PFN_UP(arg->stat_len);
1020 const size_t num_pages_info = PFN_UP(arg->info_len);
1021 const size_t num_pages_strs = PFN_UP(arg->strs_len);
1023 long nr_pinned_stat;
1024 long nr_pinned_info;
1025 long nr_pinned_strs;
1026 struct page *pages_stat[ARRAY_SIZE(pdesc->statPPNs)];
1027 struct page *pages_info[ARRAY_SIZE(pdesc->infoPPNs)];
1028 struct page *pages_strs[ARRAY_SIZE(pdesc->strsPPNs)];
1033 if (!arg->stat || !arg->info || !arg->strs)
1036 if (!arg->stat_len || !arg->info_len || !arg->strs_len)
1039 if (!arg->description)
1042 if (num_pages_stat > ARRAY_SIZE(pdesc->statPPNs) ||
1043 num_pages_info > ARRAY_SIZE(pdesc->infoPPNs) ||
1044 num_pages_strs > ARRAY_SIZE(pdesc->strsPPNs))
1047 /* Find an available slot in the mksGuestStats user array and reserve it */
1048 for (slot = 0; slot < ARRAY_SIZE(dev_priv->mksstat_user_pids); ++slot)
1049 if (!atomic_cmpxchg(&dev_priv->mksstat_user_pids[slot], 0, MKSSTAT_PID_RESERVED))
1052 if (slot == ARRAY_SIZE(dev_priv->mksstat_user_pids))
1055 BUG_ON(dev_priv->mksstat_user_pages[slot]);
1057 /* Allocate a page for the instance descriptor */
1058 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1061 atomic_set(&dev_priv->mksstat_user_pids[slot], 0);
1065 /* Set up the instance descriptor */
1066 pdesc = page_address(page);
1068 pdesc->reservedMBZ = 0;
1069 pdesc->statStartVA = arg->stat;
1070 pdesc->strsStartVA = arg->strs;
1071 pdesc->statLength = arg->stat_len;
1072 pdesc->infoLength = arg->info_len;
1073 pdesc->strsLength = arg->strs_len;
1074 desc_len = strncpy_from_user(pdesc->description, u64_to_user_ptr(arg->description),
1075 ARRAY_SIZE(pdesc->description) - 1);
1078 atomic_set(&dev_priv->mksstat_user_pids[slot], 0);
1082 reset_ppn_array(pdesc->statPPNs, ARRAY_SIZE(pdesc->statPPNs));
1083 reset_ppn_array(pdesc->infoPPNs, ARRAY_SIZE(pdesc->infoPPNs));
1084 reset_ppn_array(pdesc->strsPPNs, ARRAY_SIZE(pdesc->strsPPNs));
1086 /* Pin mksGuestStat user pages and store those in the instance descriptor */
1087 nr_pinned_stat = pin_user_pages(arg->stat, num_pages_stat, FOLL_LONGTERM, pages_stat, NULL);
1088 if (num_pages_stat != nr_pinned_stat)
1091 for (i = 0; i < num_pages_stat; ++i)
1092 pdesc->statPPNs[i] = page_to_pfn(pages_stat[i]);
1094 nr_pinned_info = pin_user_pages(arg->info, num_pages_info, FOLL_LONGTERM, pages_info, NULL);
1095 if (num_pages_info != nr_pinned_info)
1098 for (i = 0; i < num_pages_info; ++i)
1099 pdesc->infoPPNs[i] = page_to_pfn(pages_info[i]);
1101 nr_pinned_strs = pin_user_pages(arg->strs, num_pages_strs, FOLL_LONGTERM, pages_strs, NULL);
1102 if (num_pages_strs != nr_pinned_strs)
1105 for (i = 0; i < num_pages_strs; ++i)
1106 pdesc->strsPPNs[i] = page_to_pfn(pages_strs[i]);
1108 /* Send the descriptor to the host via a hypervisor call. The mksGuestStat
1109 pages will remain in use until the user requests a matching remove stats
1110 or a stats reset occurs. */
1111 hypervisor_ppn_add((PPN64)page_to_pfn(page));
1113 dev_priv->mksstat_user_pages[slot] = page;
1114 atomic_set(&dev_priv->mksstat_user_pids[slot], task_pgrp_vnr(current));
1118 DRM_DEV_INFO(dev->dev, "pid=%d arg.description='%.*s' id=%zu\n", current->pid, (int)desc_len, pdesc->description, slot);
1123 if (nr_pinned_strs > 0)
1124 unpin_user_pages(pages_strs, nr_pinned_strs);
1127 if (nr_pinned_info > 0)
1128 unpin_user_pages(pages_info, nr_pinned_info);
1131 if (nr_pinned_stat > 0)
1132 unpin_user_pages(pages_stat, nr_pinned_stat);
1134 atomic_set(&dev_priv->mksstat_user_pids[slot], 0);
1140 * vmw_mksstat_remove_ioctl: Removes a single userspace-originating mksGuestStat
1141 * instance descriptor from the hypervisor.
1143 * Discard a hypervisor PFN mapping, containing a single mksGuestStat instance
1144 * descriptor and unpin the corresponding userspace pages.
1146 * @dev: Identifies the drm device.
1147 * @data: Pointer to the ioctl argument.
1148 * @file_priv: Identifies the caller; unused.
1149 * Return: Zero on success, negative error code on error.
1152 int vmw_mksstat_remove_ioctl(struct drm_device *dev, void *data,
1153 struct drm_file *file_priv)
1155 struct drm_vmw_mksstat_remove_arg *arg =
1156 (struct drm_vmw_mksstat_remove_arg *) data;
1158 struct vmw_private *const dev_priv = vmw_priv(dev);
1160 const size_t slot = arg->id;
1163 if (slot >= ARRAY_SIZE(dev_priv->mksstat_user_pids))
1166 DRM_DEV_INFO(dev->dev, "pid=%d arg.id=%zu\n", current->pid, slot);
1168 pgid = task_pgrp_vnr(current);
1169 pid = atomic_cmpxchg(&dev_priv->mksstat_user_pids[slot], pgid, MKSSTAT_PID_RESERVED);
1175 struct page *const page = dev_priv->mksstat_user_pages[slot];
1179 dev_priv->mksstat_user_pages[slot] = NULL;
1180 atomic_set(&dev_priv->mksstat_user_pids[slot], 0);
1182 hypervisor_ppn_remove((PPN64)page_to_pfn(page));
1184 vmw_mksstat_cleanup_descriptor(page);