162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2021 VMware, Inc., Palo Alto, CA., USA
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
662306a36Sopenharmony_ci * copy of this software and associated documentation files (the
762306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
862306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
962306a36Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
1062306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
1162306a36Sopenharmony_ci * the following conditions:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the
1462306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
1562306a36Sopenharmony_ci * of the Software.
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1862306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1962306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
2062306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
2162306a36Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
2262306a36Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2362306a36Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci#ifndef _VMWGFX_MSG_ARM64_H
2762306a36Sopenharmony_ci#define _VMWGFX_MSG_ARM64_H
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#if defined(__aarch64__)
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#define VMWARE_HYPERVISOR_PORT    0x5658
3262306a36Sopenharmony_ci#define VMWARE_HYPERVISOR_PORT_HB 0x5659
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#define VMWARE_HYPERVISOR_HB  BIT(0)
3562306a36Sopenharmony_ci#define VMWARE_HYPERVISOR_OUT BIT(1)
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#define X86_IO_MAGIC 0x86
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#define X86_IO_W7_SIZE_SHIFT 0
4062306a36Sopenharmony_ci#define X86_IO_W7_SIZE_MASK (0x3 << X86_IO_W7_SIZE_SHIFT)
4162306a36Sopenharmony_ci#define X86_IO_W7_DIR       (1 << 2)
4262306a36Sopenharmony_ci#define X86_IO_W7_WITH	    (1 << 3)
4362306a36Sopenharmony_ci#define X86_IO_W7_STR	    (1 << 4)
4462306a36Sopenharmony_ci#define X86_IO_W7_DF	    (1 << 5)
4562306a36Sopenharmony_ci#define X86_IO_W7_IMM_SHIFT  5
4662306a36Sopenharmony_ci#define X86_IO_W7_IMM_MASK  (0xff << X86_IO_W7_IMM_SHIFT)
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic inline void vmw_port(unsigned long cmd, unsigned long in_ebx,
4962306a36Sopenharmony_ci			    unsigned long in_si, unsigned long in_di,
5062306a36Sopenharmony_ci			    unsigned long flags, unsigned long magic,
5162306a36Sopenharmony_ci			    unsigned long *eax, unsigned long *ebx,
5262306a36Sopenharmony_ci			    unsigned long *ecx, unsigned long *edx,
5362306a36Sopenharmony_ci			    unsigned long *si, unsigned long *di)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	register u64 x0 asm("x0") = magic;
5662306a36Sopenharmony_ci	register u64 x1 asm("x1") = in_ebx;
5762306a36Sopenharmony_ci	register u64 x2 asm("x2") = cmd;
5862306a36Sopenharmony_ci	register u64 x3 asm("x3") = flags | VMWARE_HYPERVISOR_PORT;
5962306a36Sopenharmony_ci	register u64 x4 asm("x4") = in_si;
6062306a36Sopenharmony_ci	register u64 x5 asm("x5") = in_di;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	register u64 x7 asm("x7") = ((u64)X86_IO_MAGIC << 32) |
6362306a36Sopenharmony_ci				    X86_IO_W7_WITH |
6462306a36Sopenharmony_ci				    X86_IO_W7_DIR |
6562306a36Sopenharmony_ci				    (2 << X86_IO_W7_SIZE_SHIFT);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	asm volatile("mrs xzr, mdccsr_el0 \n\t"
6862306a36Sopenharmony_ci		     : "+r"(x0), "+r"(x1), "+r"(x2),
6962306a36Sopenharmony_ci		       "+r"(x3), "+r"(x4), "+r"(x5)
7062306a36Sopenharmony_ci		     : "r"(x7)
7162306a36Sopenharmony_ci		     :);
7262306a36Sopenharmony_ci	*eax = x0;
7362306a36Sopenharmony_ci	*ebx = x1;
7462306a36Sopenharmony_ci	*ecx = x2;
7562306a36Sopenharmony_ci	*edx = x3;
7662306a36Sopenharmony_ci	*si = x4;
7762306a36Sopenharmony_ci	*di = x5;
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistatic inline void vmw_port_hb(unsigned long cmd, unsigned long in_ecx,
8162306a36Sopenharmony_ci			       unsigned long in_si, unsigned long in_di,
8262306a36Sopenharmony_ci			       unsigned long flags, unsigned long magic,
8362306a36Sopenharmony_ci			       unsigned long bp, u32 w7dir,
8462306a36Sopenharmony_ci			       unsigned long *eax, unsigned long *ebx,
8562306a36Sopenharmony_ci			       unsigned long *ecx, unsigned long *edx,
8662306a36Sopenharmony_ci			       unsigned long *si, unsigned long *di)
8762306a36Sopenharmony_ci{
8862306a36Sopenharmony_ci	register u64 x0 asm("x0") = magic;
8962306a36Sopenharmony_ci	register u64 x1 asm("x1") = cmd;
9062306a36Sopenharmony_ci	register u64 x2 asm("x2") = in_ecx;
9162306a36Sopenharmony_ci	register u64 x3 asm("x3") = flags | VMWARE_HYPERVISOR_PORT_HB;
9262306a36Sopenharmony_ci	register u64 x4 asm("x4") = in_si;
9362306a36Sopenharmony_ci	register u64 x5 asm("x5") = in_di;
9462306a36Sopenharmony_ci	register u64 x6 asm("x6") = bp;
9562306a36Sopenharmony_ci	register u64 x7 asm("x7") = ((u64)X86_IO_MAGIC << 32) |
9662306a36Sopenharmony_ci				    X86_IO_W7_STR |
9762306a36Sopenharmony_ci				    X86_IO_W7_WITH |
9862306a36Sopenharmony_ci				    w7dir;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	asm volatile("mrs xzr, mdccsr_el0 \n\t"
10162306a36Sopenharmony_ci		     : "+r"(x0), "+r"(x1), "+r"(x2),
10262306a36Sopenharmony_ci		       "+r"(x3), "+r"(x4), "+r"(x5)
10362306a36Sopenharmony_ci		     : "r"(x6), "r"(x7)
10462306a36Sopenharmony_ci		     :);
10562306a36Sopenharmony_ci	*eax = x0;
10662306a36Sopenharmony_ci	*ebx = x1;
10762306a36Sopenharmony_ci	*ecx = x2;
10862306a36Sopenharmony_ci	*edx = x3;
10962306a36Sopenharmony_ci	*si  = x4;
11062306a36Sopenharmony_ci	*di  = x5;
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci#define VMW_PORT(cmd, in_ebx, in_si, in_di, flags, magic, eax, ebx, ecx, edx,  \
11462306a36Sopenharmony_ci		 si, di)                                                       \
11562306a36Sopenharmony_ci	vmw_port(cmd, in_ebx, in_si, in_di, flags, magic, &eax, &ebx, &ecx,    \
11662306a36Sopenharmony_ci		 &edx, &si, &di)
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci#define VMW_PORT_HB_OUT(cmd, in_ecx, in_si, in_di, flags, magic, bp, eax, ebx, \
11962306a36Sopenharmony_ci		        ecx, edx, si, di)                                      \
12062306a36Sopenharmony_ci	vmw_port_hb(cmd, in_ecx, in_si, in_di, flags, magic, bp,               \
12162306a36Sopenharmony_ci                    0, &eax, &ebx, &ecx, &edx, &si, &di)
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci#define VMW_PORT_HB_IN(cmd, in_ecx, in_si, in_di, flags, magic, bp, eax, ebx,  \
12462306a36Sopenharmony_ci		       ecx, edx, si, di)                                       \
12562306a36Sopenharmony_ci	vmw_port_hb(cmd, in_ecx, in_si, in_di, flags, magic, bp,               \
12662306a36Sopenharmony_ci		    X86_IO_W7_DIR, &eax, &ebx, &ecx, &edx, &si, &di)
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci#endif
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci#endif /* _VMWGFX_MSG_ARM64_H */
131