Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / tools / promif / prom_glue.c
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* Hypervisor Software File: prom_glue.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 * id: @(#)prom_glue.c 1.1 03/04/01
46 * purpose:
47 * Copyright 1994-2001, 2003 Sun Microsystems, Inc. All Rights Reserved
48 * Use is subject to license terms.
49 */
50
51#include <sys/promif.h>
52#include <sys/promimpl.h>
53#include <sys/obpdefs.h>
54#include <sys/varargs.h>
55
56caddr_t strstr(caddr_t s1, caddr_t s2);
57
58/*
59 * flash update functions
60 */
61
62ihandle_t
63get_parent_ihandle(ihandle_t child_ihandle)
64{
65 char parent_name[OBP_MAXPATHLEN];
66 phandle_t child_phandle, parent_phandle;
67
68 child_phandle = prom_getphandle(child_ihandle);
69 parent_phandle = prom_parentnode(child_phandle);
70 prom_phandle_to_path(parent_phandle, parent_name, OBP_MAXPATHLEN);
71
72 return (prom_open(parent_name));
73}
74
75void *
76mmap(void *addr, size_t len, int32_t prot, uint32_t flags,
77 ihandle_t fildes, int32_t off)
78{
79 ihandle_t dev_ihandle, parent_ihandle;
80 phandle_t dev_phandle, parent_phandle;
81 void *rp;
82 static char reg_prop[100], dev_name[100], bus_type[100];
83 uint32_t *reg_ptr;
84 uint_t adr_cells, size_cells;
85
86 dev_ihandle = fildes;
87 dev_phandle = prom_getphandle(dev_ihandle);
88 prom_getprop(dev_phandle, "reg", reg_prop);
89 reg_ptr = (uint32_t *)reg_prop;
90
91 parent_ihandle = get_parent_ihandle(dev_ihandle);
92 parent_phandle = prom_getphandle(parent_ihandle);
93
94 prom_getprop(parent_phandle, "#address-cells", (caddr_t)&adr_cells);
95 prom_getprop(parent_phandle, "#size-cells", (caddr_t)&size_cells);
96
97 prom_getprop(dev_phandle, "name", dev_name);
98 prom_getprop(parent_phandle, "device_type", bus_type);
99
100 /*
101 * Flashprom devices need special handling of offset arg
102 * as specified by the /dev/flashprom driver
103 */
104
105 if ((strcmp(dev_name, "flashprom") == 0)) {
106 if (off != NULL) {
107 reg_ptr += ((off >> 28) & 0xf)
108 * (adr_cells + size_cells);
109 off = (off & 0xfffffff);
110 *(reg_ptr + 1) += off;
111 }
112 rp = (void *)prom_mapin(parent_ihandle, reg_ptr,
113 adr_cells, len);
114
115 return ((void *)((int32_t)rp & 0xffffffff));
116 }
117
118 /*
119 * PCI addresses are relocatable so use assigned-addresses
120 */
121
122 if ((strcmp(bus_type, "pci") == 0)) {
123 prom_getprop(dev_phandle, "assigned-addresses", reg_prop);
124 }
125
126 reg_ptr += (off * (adr_cells + size_cells));
127 rp = (void *)prom_mapin(parent_ihandle, reg_ptr, adr_cells, len);
128
129 return ((void *)((int32_t)rp & 0xffffffff));
130}
131
132int32_t
133munmap(void *addr, size_t len)
134{
135 /* nice and platform neutral */
136 return (0);
137}
138
139uint32_t
140sleep(uint32_t sleeptime)
141{
142 uint32_t initial, current;
143 initial = prom_gettime();
144 current = prom_gettime();
145 /* prom_gettime returns milliseconds */
146 while (((current - initial) / 1000) < sleeptime)
147 current = prom_gettime();
148 return (0);
149}
150
151/*
152 * Signal handling
153 */
154
155int32_t
156sigignore(int signal)
157{
158 /* OBP ignore's all interrupts while running the client program */
159 return (0);
160}
161
162
163/*
164 * Device I/O group
165 */
166ihandle_t
167open(caddr_t name, int32_t oflag, ...)
168{
169 ihandle_t fildes;
170
171 if ((strcmp(name, "/dev/openprom")) == 0)
172 /*
173 * openprom is a special case. Return a special FD
174 * to fake out ioctl calls
175 */
176 return (OPENPROM_FD);
177
178 fildes = prom_open(name);
179 if (strstr(name, "flashprom"))
180 FLASHPROM_FD = fildes;
181 return (fildes);
182}
183
184uint32_t
185close(ihandle_t fd)
186{
187 uint32_t result;
188
189 if (fd == OPENPROM_FD)
190 /* openprom never was given a "real" FD */
191 return (0);
192
193 result = prom_close(fd);
194 if (fd == FLASHPROM_FD)
195 FLASHPROM_FD = NULL;
196 return (result);
197}
198
199uint32_t
200read(ihandle_t fd, caddr_t buf, uint32_t len)
201{
202 return (prom_read(fd, buf, len));
203}
204
205uint32_t
206write(ihandle_t fd, caddr_t buf, uint32_t len)
207{
208 return (prom_write(fd, buf, len));
209}
210
211uint32_t
212seek(ihandle_t fd, u_longlong_t offset)
213{
214 return (prom_seek(fd, offset));
215}
216
217/*
218 * Control transfer group
219 */
220
221
222/* The exit argument isn't used */
223void
224exit(int32_t level)
225{
226 prom_exit_to_mon();
227}
228
229/*
230 * Resource allocation group
231 */
232
233void *
234malloc(size_t size)
235{
236 caddr_t virt;
237 /* virt is never used because align(arg 2) = 1 */
238 return ((void *)prom_malloc(virt, size, 1));
239}
240
241
242/* This function is pretty much a noop in OBP (right now) */
243void
244free(caddr_t virt, size_t size)
245{
246 size = 0;
247 prom_free(virt, size);
248}
249
250/*
251 * Console I/O group
252 */
253
254uchar_t
255getchar(void)
256{
257 char c;
258 c = prom_getchar();
259 /* echo to stdout like unix */
260 switch (c) {
261 case 13:
262 prom_printf("\n");
263 c = 10;
264 break;
265 case 127:
266 prom_printf("\b");
267 break;
268 default:
269 prom_printf("%c", c);
270 break;
271 }
272 return (c);
273}
274
275void
276putchar(char c)
277{
278 prom_putchar(c);
279}
280
281int32_t
282mayget(void)
283{
284 return (prom_mayget());
285}
286
287int32_t
288mayput(char c)
289{
290 return (prom_mayput(c));
291}
292
293/* the actual printf command returns an int */
294void
295printf(const caddr_t fmt, ...)
296{
297 __va_list adx;
298 va_start(adx, fmt);
299 prom_vprintf(fmt, adx);
300 va_end(adx);
301}
302
303void
304vprintf(const caddr_t fmt, __va_list adx)
305{
306 prom_vprintf(fmt, adx);
307}
308
309caddr_t
310sprintf(caddr_t s, const caddr_t fmt, ...)
311{
312 __va_list adx;
313 va_start(adx, fmt);
314 prom_vsprintf(s, fmt, adx);
315 va_end(adx);
316 return (s);
317}
318
319caddr_t
320vsprintf(caddr_t s, const caddr_t fmt, __va_list adx)
321{
322 prom_vsprintf(s, fmt, adx);
323}
324
325/*
326 * Utility routines (proimpl.h)
327 */
328
329caddr_t
330strcpy(caddr_t s1, caddr_t s2)
331{
332 return (prom_strcpy(s1, s2));
333}
334
335caddr_t
336strdup(caddr_t s1)
337{
338 caddr_t s2;
339 s2 = (caddr_t)malloc(strlen(s1) + 1);
340 strcpy(s2, s1);
341 return (s2);
342}
343
344caddr_t
345strncpy(caddr_t s1, caddr_t s2, size_t n)
346{
347 return (prom_strncpy(s1, s2, n));
348}
349
350int32_t
351strcmp(caddr_t s1, caddr_t s2)
352{
353 return (prom_strcmp(s1, s2));
354}
355
356int32_t
357strncmp(caddr_t s1, caddr_t s2, size_t n)
358{
359 return (prom_strncmp(s1, s2, n));
360}
361
362int32_t
363strlen(caddr_t s)
364{
365 return (prom_strlen(s));
366}
367
368caddr_t
369strrchr(caddr_t s, int32_t c)
370{
371 return (prom_strrchr(s, c));
372}
373
374caddr_t
375strcat(caddr_t s1, caddr_t s2)
376{
377 return (prom_strcat(s1, s2));
378}
379
380caddr_t
381strchr(const caddr_t s, int32_t c)
382{
383 return (prom_strchr(s, c));
384}
385
386caddr_t
387strstr(caddr_t s1, caddr_t s2)
388{
389 return (prom_strstr(s1, s2));
390}
391
392void *
393memccpy(caddr_t s1, caddr_t s2, int32_t c, size_t n)
394{
395 return (prom_memccpy(s1, s2, c, n));
396}
397
398void *
399memchr(caddr_t s, int32_t c, size_t n)
400{
401 return (prom_memchr(s, c, n));
402}
403
404
405int32_t
406memcmp(caddr_t s1, caddr_t s2, size_t n)
407{
408 return (prom_memcmp(s1, s2, n));
409}
410
411void *
412memcpy(caddr_t s1, caddr_t s2, size_t n)
413{
414 return (prom_memcpy(s1, s2, n));
415}
416
417void *
418memset(caddr_t s, int32_t c, size_t n)
419{
420 return (prom_memset(s, c, n));
421}