xref: /openbmc/qemu/target/hppa/sys_helper.c (revision 922582ace2df59572a671f5c0c5c6c5c706995e5)
1c70647eeSPhilippe Mathieu-Daudé /*
2c70647eeSPhilippe Mathieu-Daudé  * Helpers for HPPA system instructions.
3c70647eeSPhilippe Mathieu-Daudé  *
4c70647eeSPhilippe Mathieu-Daudé  * Copyright (c) 2016 Richard Henderson <rth@twiddle.net>
5c70647eeSPhilippe Mathieu-Daudé  *
6c70647eeSPhilippe Mathieu-Daudé  * This library is free software; you can redistribute it and/or
7c70647eeSPhilippe Mathieu-Daudé  * modify it under the terms of the GNU Lesser General Public
8c70647eeSPhilippe Mathieu-Daudé  * License as published by the Free Software Foundation; either
9c70647eeSPhilippe Mathieu-Daudé  * version 2.1 of the License, or (at your option) any later version.
10c70647eeSPhilippe Mathieu-Daudé  *
11c70647eeSPhilippe Mathieu-Daudé  * This library is distributed in the hope that it will be useful,
12c70647eeSPhilippe Mathieu-Daudé  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c70647eeSPhilippe Mathieu-Daudé  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c70647eeSPhilippe Mathieu-Daudé  * Lesser General Public License for more details.
15c70647eeSPhilippe Mathieu-Daudé  *
16c70647eeSPhilippe Mathieu-Daudé  * You should have received a copy of the GNU Lesser General Public
17c70647eeSPhilippe Mathieu-Daudé  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18c70647eeSPhilippe Mathieu-Daudé  */
19c70647eeSPhilippe Mathieu-Daudé 
20c70647eeSPhilippe Mathieu-Daudé #include "qemu/osdep.h"
21*9e035f00SRichard Henderson #include "qemu/log.h"
22c70647eeSPhilippe Mathieu-Daudé #include "cpu.h"
23c70647eeSPhilippe Mathieu-Daudé #include "exec/exec-all.h"
24c70647eeSPhilippe Mathieu-Daudé #include "exec/helper-proto.h"
25c70647eeSPhilippe Mathieu-Daudé #include "qemu/timer.h"
26c70647eeSPhilippe Mathieu-Daudé #include "sysemu/runstate.h"
27dbca0835SHelge Deller #include "sysemu/sysemu.h"
28dbca0835SHelge Deller #include "chardev/char-fe.h"
29c70647eeSPhilippe Mathieu-Daudé 
HELPER(write_interval_timer)30c53e401eSRichard Henderson void HELPER(write_interval_timer)(CPUHPPAState *env, target_ulong val)
31c70647eeSPhilippe Mathieu-Daudé {
32c70647eeSPhilippe Mathieu-Daudé     HPPACPU *cpu = env_archcpu(env);
33c70647eeSPhilippe Mathieu-Daudé     uint64_t current = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
34c70647eeSPhilippe Mathieu-Daudé     uint64_t timeout;
35c70647eeSPhilippe Mathieu-Daudé 
36c70647eeSPhilippe Mathieu-Daudé     /*
37c70647eeSPhilippe Mathieu-Daudé      * Even in 64-bit mode, the comparator is always 32-bit.  But the
38c70647eeSPhilippe Mathieu-Daudé      * value we expose to the guest is 1/4 of the speed of the clock,
39c70647eeSPhilippe Mathieu-Daudé      * so moosh in 34 bits.
40c70647eeSPhilippe Mathieu-Daudé      */
41c70647eeSPhilippe Mathieu-Daudé     timeout = deposit64(current, 0, 34, (uint64_t)val << 2);
42c70647eeSPhilippe Mathieu-Daudé 
43c70647eeSPhilippe Mathieu-Daudé     /* If the mooshing puts the clock in the past, advance to next round.  */
44c70647eeSPhilippe Mathieu-Daudé     if (timeout < current + 1000) {
45c70647eeSPhilippe Mathieu-Daudé         timeout += 1ULL << 34;
46c70647eeSPhilippe Mathieu-Daudé     }
47c70647eeSPhilippe Mathieu-Daudé 
48c70647eeSPhilippe Mathieu-Daudé     cpu->env.cr[CR_IT] = timeout;
49c70647eeSPhilippe Mathieu-Daudé     timer_mod(cpu->alarm_timer, timeout);
50c70647eeSPhilippe Mathieu-Daudé }
51c70647eeSPhilippe Mathieu-Daudé 
HELPER(halt)52c70647eeSPhilippe Mathieu-Daudé void HELPER(halt)(CPUHPPAState *env)
53c70647eeSPhilippe Mathieu-Daudé {
54c70647eeSPhilippe Mathieu-Daudé     qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
55c70647eeSPhilippe Mathieu-Daudé     helper_excp(env, EXCP_HLT);
56c70647eeSPhilippe Mathieu-Daudé }
57c70647eeSPhilippe Mathieu-Daudé 
HELPER(reset)58c70647eeSPhilippe Mathieu-Daudé void HELPER(reset)(CPUHPPAState *env)
59c70647eeSPhilippe Mathieu-Daudé {
60c70647eeSPhilippe Mathieu-Daudé     qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
61c70647eeSPhilippe Mathieu-Daudé     helper_excp(env, EXCP_HLT);
62c70647eeSPhilippe Mathieu-Daudé }
63c70647eeSPhilippe Mathieu-Daudé 
HELPER(swap_system_mask)64c53e401eSRichard Henderson target_ulong HELPER(swap_system_mask)(CPUHPPAState *env, target_ulong nsm)
65c70647eeSPhilippe Mathieu-Daudé {
66c70647eeSPhilippe Mathieu-Daudé     target_ulong psw = env->psw;
67c70647eeSPhilippe Mathieu-Daudé     /*
68c70647eeSPhilippe Mathieu-Daudé      * Setting the PSW Q bit to 1, if it was not already 1, is an
69c70647eeSPhilippe Mathieu-Daudé      * undefined operation.
70c70647eeSPhilippe Mathieu-Daudé      *
71c70647eeSPhilippe Mathieu-Daudé      * However, HP-UX 10.20 does this with the SSM instruction.
72c70647eeSPhilippe Mathieu-Daudé      * Tested this on HP9000/712 and HP9000/785/C3750 and both
73c70647eeSPhilippe Mathieu-Daudé      * machines set the Q bit from 0 to 1 without an exception,
74c70647eeSPhilippe Mathieu-Daudé      * so let this go without comment.
75c70647eeSPhilippe Mathieu-Daudé      */
76c70647eeSPhilippe Mathieu-Daudé     env->psw = (psw & ~PSW_SM) | (nsm & PSW_SM);
77c70647eeSPhilippe Mathieu-Daudé     return psw & PSW_SM;
78c70647eeSPhilippe Mathieu-Daudé }
79c70647eeSPhilippe Mathieu-Daudé 
HELPER(rfi)80c70647eeSPhilippe Mathieu-Daudé void HELPER(rfi)(CPUHPPAState *env)
81c70647eeSPhilippe Mathieu-Daudé {
8226d93723SRichard Henderson     uint64_t mask;
8326d93723SRichard Henderson 
8426d93723SRichard Henderson     cpu_hppa_put_psw(env, env->cr[CR_IPSW]);
85b10700d8SRichard Henderson 
86b10700d8SRichard Henderson     /*
87b10700d8SRichard Henderson      * For pa2.0, IIASQ is the top bits of the virtual address.
88b10700d8SRichard Henderson      * To recreate the space identifier, remove the offset bits.
8926d93723SRichard Henderson      * For pa1.x, the mask reduces to no change to space.
90b10700d8SRichard Henderson      */
9126d93723SRichard Henderson     mask = gva_offset_mask(env->psw);
92b10700d8SRichard Henderson 
9326d93723SRichard Henderson     env->iaoq_f = env->cr[CR_IIAOQ];
9426d93723SRichard Henderson     env->iaoq_b = env->cr_back[1];
9526d93723SRichard Henderson     env->iasq_f = (env->cr[CR_IIASQ] << 32) & ~(env->iaoq_f & mask);
9626d93723SRichard Henderson     env->iasq_b = (env->cr_back[0] << 32) & ~(env->iaoq_b & mask);
97*9e035f00SRichard Henderson 
98*9e035f00SRichard Henderson     if (qemu_loglevel_mask(CPU_LOG_INT)) {
99*9e035f00SRichard Henderson         FILE *logfile = qemu_log_trylock();
100*9e035f00SRichard Henderson         if (logfile) {
101*9e035f00SRichard Henderson             CPUState *cs = env_cpu(env);
102*9e035f00SRichard Henderson 
103*9e035f00SRichard Henderson             fprintf(logfile, "RFI: cpu %d\n", cs->cpu_index);
104*9e035f00SRichard Henderson             hppa_cpu_dump_state(cs, logfile, 0);
105*9e035f00SRichard Henderson             qemu_log_unlock(logfile);
106*9e035f00SRichard Henderson         }
107*9e035f00SRichard Henderson     }
108c70647eeSPhilippe Mathieu-Daudé }
109c70647eeSPhilippe Mathieu-Daudé 
getshadowregs(CPUHPPAState * env)110558c09beSRichard Henderson static void getshadowregs(CPUHPPAState *env)
111c70647eeSPhilippe Mathieu-Daudé {
112c70647eeSPhilippe Mathieu-Daudé     env->gr[1] = env->shadow[0];
113c70647eeSPhilippe Mathieu-Daudé     env->gr[8] = env->shadow[1];
114c70647eeSPhilippe Mathieu-Daudé     env->gr[9] = env->shadow[2];
115c70647eeSPhilippe Mathieu-Daudé     env->gr[16] = env->shadow[3];
116c70647eeSPhilippe Mathieu-Daudé     env->gr[17] = env->shadow[4];
117c70647eeSPhilippe Mathieu-Daudé     env->gr[24] = env->shadow[5];
118c70647eeSPhilippe Mathieu-Daudé     env->gr[25] = env->shadow[6];
119c70647eeSPhilippe Mathieu-Daudé }
120c70647eeSPhilippe Mathieu-Daudé 
HELPER(rfi_r)121c70647eeSPhilippe Mathieu-Daudé void HELPER(rfi_r)(CPUHPPAState *env)
122c70647eeSPhilippe Mathieu-Daudé {
123558c09beSRichard Henderson     getshadowregs(env);
124c70647eeSPhilippe Mathieu-Daudé     helper_rfi(env);
125c70647eeSPhilippe Mathieu-Daudé }
126dbca0835SHelge Deller 
127dbca0835SHelge Deller #ifndef CONFIG_USER_ONLY
128dbca0835SHelge Deller /*
129dbca0835SHelge Deller  * diag_console_output() is a helper function used during the initial bootup
130dbca0835SHelge Deller  * process of the SeaBIOS-hppa firmware.  During the bootup phase, addresses of
131dbca0835SHelge Deller  * serial ports on e.g. PCI busses are unknown and most other devices haven't
132dbca0835SHelge Deller  * been initialized and configured yet.  With help of a simple "diag" assembler
133dbca0835SHelge Deller  * instruction and an ASCII character code in register %r26 firmware can easily
134dbca0835SHelge Deller  * print debug output without any dependencies to the first serial port and use
135dbca0835SHelge Deller  * that as serial console.
136dbca0835SHelge Deller  */
HELPER(diag_console_output)137dbca0835SHelge Deller void HELPER(diag_console_output)(CPUHPPAState *env)
138dbca0835SHelge Deller {
139dbca0835SHelge Deller     CharBackend *serial_backend;
140dbca0835SHelge Deller     Chardev *serial_port;
141dbca0835SHelge Deller     unsigned char c;
142dbca0835SHelge Deller 
143dbca0835SHelge Deller     /* find first serial port */
144dbca0835SHelge Deller     serial_port = serial_hd(0);
145dbca0835SHelge Deller     if (!serial_port) {
146dbca0835SHelge Deller         return;
147dbca0835SHelge Deller     }
148dbca0835SHelge Deller 
149dbca0835SHelge Deller     /* get serial_backend for the serial port */
150dbca0835SHelge Deller     serial_backend = serial_port->be;
151dbca0835SHelge Deller     if (!serial_backend ||
152dbca0835SHelge Deller         !qemu_chr_fe_backend_connected(serial_backend)) {
153dbca0835SHelge Deller         return;
154dbca0835SHelge Deller     }
155dbca0835SHelge Deller 
156dbca0835SHelge Deller     c = (unsigned char)env->gr[26];
157dbca0835SHelge Deller     qemu_chr_fe_write(serial_backend, &c, sizeof(c));
158dbca0835SHelge Deller }
159dbca0835SHelge Deller #endif
160