Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / devices / dummy_mods / char / char.cc
CommitLineData
920dae64
AT
1// ========== Copyright Header Begin ==========================================
2//
3// OpenSPARC T2 Processor File: char.cc
4// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
6//
7// The above named program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public
9// License version 2 as published by the Free Software Foundation.
10//
11// The above named program is distributed in the hope that it will be
12// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public
17// License along with this work; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19//
20// ========== Copyright Header End ============================================
21/* char.cc
22 * fake char output device for blaze/SAM
23 *
24 *
25 * Copyright (c) 2004 by Sun Microsystems, Inc.
26 * All rights reserved.
27 *
28 */
29
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <errno.h>
35#include <sys/syscall.h> // get exec number
36#include <sys/types.h> // uid_t
37#include <unistd.h> // getuid()
38#include <inttypes.h>
39#include <stdarg.h>
40
41#include <thread.h>
42
43#include "mmi.h"
44#include "ui.h"
45
46struct sam_char_s
47{
48 uint64_t startpa;
49 uint64_t endpa;
50 uint64_t sz;
51};
52
53
54extern "C" int char_access(uint32_t cpuid, void* obj, uint64_t paddr, mmi_bool_t wr, uint32_t size, uint64_t* buf, uint8_t bytemask);
55
56int char_ld_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid);
57int char_st_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid);
58
59extern "C" void char_create_instance (const char *modname, const char *instance_name);
60
61
62extern "C" void _init()
63{
64 if (! mmi_register_instance_creator("char", char_create_instance) ) {
65 fprintf(stderr, "Cannot register instance creator for char\n");
66 }
67} // _init()
68
69
70
71void char_create_instance (const char *modname, const char *instance_name)
72{
73 sam_char_s * char_obj = new sam_char_s;
74
75 mmi_instance_t instance = mmi_register_instance(modname, instance_name, (void *) &char_obj, "char dummy device");
76
77 char_obj->startpa = 0x0;
78 char_obj->endpa = 0x0;
79 char_obj->sz = 0;
80
81 // expected syntax: sysconf char char1 startpa=<0xblah> endpa=<0xblah>
82 int argc = mmi_argc(instance);
83 int i;
84
85 for (i=0; i<argc; i++) {
86 char * arg = strdup(mmi_argv(instance, i));
87 char * marker;
88 char * lv = strtok_r(arg, "=", &marker);
89 if (strcmp(lv, "startpa") == 0) {
90 errno = 0;
91 char * rv = strtok_r(NULL, "=", &marker);
92 char_obj->startpa = strtoull(rv, NULL, 0);
93 if (errno) {
94 perror("char: error parsing startpa");
95 exit(1);
96 }
97 } else if (strcmp(lv, "endpa") == 0) {
98 errno = 0;
99 char * rv = strtok_r(NULL, "=", &marker);
100 char_obj->endpa = strtoull(rv, NULL, 0);
101 if (errno) {
102 perror("char: error parsing startpa");
103 exit(1);
104 }
105 } // else - do nothing
106 }
107
108 char_obj->sz = char_obj->endpa - char_obj->startpa + 1;
109
110 if (mmi_map_physio(char_obj->startpa, char_obj->sz, (void *) char_obj, char_access)) {
111 fprintf(stderr, " sam char: unable to register IO interceptor\n");
112 return;
113 }
114}
115
116
117int char_access(uint32_t cpuid, void* obj, uint64_t paddr, mmi_bool_t wr, uint32_t size, uint64_t* buf, uint8_t bytemask)
118{
119 if (wr) {
120 return char_st_operation(obj, paddr, buf, size, cpuid);
121 } else {
122 return char_ld_operation(obj, paddr, buf, size, cpuid);
123 }
124} // char_access()
125
126int char_ld_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
127{
128
129 fprintf(stderr, "SAM char: ld_operation pa 0x%llx size %d cpu %d, ignored\n",
130 paddr, size, cpuid);
131 *buf = 0;
132
133 return 0;
134} // int char_ld_operation (void *cd, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
135
136
137int char_st_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
138{
139
140 // output chars to the consol
141 char *c = (char*)buf;
142 for ( int i = size; i && (*c); i--, c++)
143 *c == 0 ? putchar('\n') : putchar (*c);
144 return 0;
145
146} // int char_st_operation (void *cd, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
147
148
149/////////////////////////////////////////////////
150
151extern "C" void _fini ()
152{
153}
154
155/////////////////////////////////////////////////