Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / hypervisor / src / common / src / support.c
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* Hypervisor Software File: support.c
5*
6* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
7*
8* - Do no alter or remove copyright notices
9*
10* - Redistribution and use of this software in source and binary forms, with
11* or without modification, are permitted provided that the following
12* conditions are met:
13*
14* - Redistribution of source code must retain the above copyright notice,
15* this list of conditions and the following disclaimer.
16*
17* - Redistribution in binary form must reproduce the above copyright notice,
18* this list of conditions and the following disclaimer in the
19* documentation and/or other materials provided with the distribution.
20*
21* Neither the name of Sun Microsystems, Inc. or the names of contributors
22* may be used to endorse or promote products derived from this software
23* without specific prior written permission.
24*
25* This software is provided "AS IS," without a warranty of any kind.
26* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
27* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
28* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
29* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
30* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
31* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
32* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
33* FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
34* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
35* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
36* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37*
38* You acknowledge that this software is not designed, licensed or
39* intended for use in the design, construction, operation or maintenance of
40* any nuclear facility.
41*
42* ========== Copyright Header End ============================================
43*/
44/*
45 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
46 * Use is subject to license terms.
47 */
48
49#pragma ident "@(#)support.c 1.5 07/07/09 SMI"
50
51#include <stdarg.h>
52
53#include <sys/htypes.h>
54#include <support.h>
55#include <vdev_intr.h>
56#include <config.h>
57
58/*
59 * Basic printf capability for debugging output.
60 */
61void
62c_printf(char *strp, ...)
63{
64 va_list argsp;
65 int i, ch;
66 char buf[2];
67
68 va_start(argsp, strp);
69
70 buf[1] = '\0';
71#define PUTC(_x) do { buf[0] = (_x); c_puts(buf); } while (0)
72
73 for (i = 0; (ch = strp[i]) != '\0'; i++) {
74 switch (ch) {
75 case '%':
76 ch = strp[++i];
77 switch (ch) {
78 case '\0':
79 goto done;
80 case 'x':
81 case 'p':
82 c_putn(va_arg(argsp, uint64_t), 16);
83 break;
84 case 'd':
85 c_putn(va_arg(argsp, uint64_t), 10);
86 break;
87 case 's':
88 c_puts(va_arg(argsp, char *));
89 break;
90 case '%':
91 goto def;
92 default:
93 break;
94 }
95 break;
96 case '\n':
97 PUTC('\r');
98 default:
99def:;
100 PUTC(ch);
101 break;
102 }
103 }
104
105done:;
106 va_end(argsp);
107}
108
109
110/*
111 * HV console output a number in the specified base
112 */
113void
114c_putn(uint64_t val, int base)
115{
116 uint64_t num;
117 static char ch[] = "0123456789abcdef";
118 char buf[2];
119
120 buf[1] = '\0';
121
122 if (base == 10 && ((int64_t)val) < 0LL) {
123 PUTC('-');
124 val = 0 - val;
125 }
126
127 num = 1;
128 while ((val / num) >= base) {
129 num *= base;
130 }
131
132 do {
133 PUTC(ch[ val / num ]);
134 val = val % num;
135 num = num / base;
136 } while (num != 0);
137}
138
139
140/*
141 * Brain dead bzero ...
142 * ... do properly if we ever need to bzero large chunks of memory
143 */
144void
145c_bzero(void *ptr, uint64_t size)
146{
147 uint8_t *p = ptr;
148 uint64_t i;
149
150 for (i = 0; i < size; i++) p[i] = 0;
151}
152
153/*
154 * Brain dead memcpy ...
155 * ... do properly if we ever need to copy large chunks of memory
156 */
157void
158c_memcpy(void *dest, void *src, uint64_t size)
159{
160 uint8_t *destp = dest;
161 uint8_t *srcp = src;
162 uint64_t i;
163
164 for (i = 0; i < size; i++)
165 destp[i] = srcp[i];
166}
167
168void
169c_usleep(uint64_t usecs)
170{
171
172 uint64_t delay, old;
173
174#ifdef DEBUG
175 /*
176 * Make sure the MD has been parsed/read in before using
177 * stickfrequency
178 */
179 if (config.stickfrequency == 0) {
180 c_hvabort();
181 }
182#endif
183
184 delay = (usecs * config.stickfrequency) / 1000000ll;
185
186 for (old = c_get_stick(); (c_get_stick() - old ) < delay; )
187 /* LINTED */
188 ;
189}