Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / devices / dummy_mods / rom / rom.cc
CommitLineData
920dae64
AT
1// ========== Copyright Header Begin ==========================================
2//
3// OpenSPARC T2 Processor File: rom.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/* rom.cc
22 * fake rom device for T1 in 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#include "mem.h"
46
47struct sam_rom_s
48{
49 uint64_t startpa;
50 uint64_t endpa;
51 uint64_t sz;
52};
53
54
55extern "C" int rom_access(uint32_t cpuid, void* obj, uint64_t paddr, mmi_bool_t wr, uint32_t size, uint64_t* buf, uint8_t bytemask);
56
57int rom_ld_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid);
58int rom_st_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid);
59
60extern "C" void rom_create_instance (const char *modname, const char *instance_name);
61
62
63extern "C" void _init()
64{
65 if (! mmi_register_instance_creator("rom", rom_create_instance) ) {
66 ui->error("Cannot register instance creator for rom\n");
67 }
68} // _init()
69
70
71
72void rom_create_instance (const char *modname, const char *instance_name)
73{
74 sam_rom_s * rom_obj = new sam_rom_s;
75
76 mmi_instance_t instance = mmi_register_instance(modname, instance_name, (void *) &rom_obj, "rom dummy device");
77
78 rom_obj->startpa = 0x0;
79 rom_obj->endpa = 0x0;
80 rom_obj->sz = 0;
81
82 // expected syntax: sysconf rom rom1 startpa=<0xblah> endpa=<0xblah>
83 int argc = mmi_argc(instance);
84 int i;
85
86 for (i=0; i<argc; i++) {
87 char * arg = strdup(mmi_argv(instance, i));
88 char * marker;
89 char * lv = strtok_r(arg, "=", &marker);
90 if (strcmp(lv, "startpa") == 0) {
91 errno = 0;
92 char * rv = strtok_r(NULL, "=", &marker);
93 rom_obj->startpa = strtoull(rv, NULL, 0);
94 if (errno) {
95 perror("rom: error parsing startpa");
96 exit(1);
97 }
98 } else if (strcmp(lv, "endpa") == 0) {
99 errno = 0;
100 char * rv = strtok_r(NULL, "=", &marker);
101 rom_obj->endpa = strtoull(rv, NULL, 0);
102 if (errno) {
103 perror("rom: error parsing startpa");
104 exit(1);
105 }
106 } else {
107 ui->error("%s - unknown parameter of rom device %s\n",lv,instance_name);
108 exit(-1);
109 }
110 }
111
112 rom_obj->sz = rom_obj->endpa - rom_obj->startpa + 1;
113
114 if (mmi_map_physio(rom_obj->startpa, rom_obj->sz, (void *) rom_obj, rom_access)) {
115 ui->error("rom: unable to register IO interceptor\n");
116 return;
117 }
118}
119
120
121int rom_access(uint32_t cpuid, void* obj, uint64_t paddr, mmi_bool_t wr, uint32_t size, uint64_t* buf, uint8_t bytemask)
122{
123 if (wr) {
124 return rom_st_operation(obj, paddr, buf, size, cpuid);
125 } else {
126 return rom_ld_operation(obj, paddr, buf, size, cpuid);
127 }
128} // rom_access()
129
130int rom_ld_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
131{
132
133 // redirect load to access sparse mem
134 switch (size)
135 {
136 case 1: *buf = memread8u (mm1, paddr); break;
137 case 2: *buf = memread16u (mm1, paddr); break;
138 case 4: *buf = memread32u (mm1, paddr); break;
139 case 8: *buf = memread64u (mm1, paddr); break;
140 default:
141 ui->error("rom: ld_operation pa 0x%llx size %d cpu %d, ignored\n",
142 paddr, size, cpuid);
143 *buf = 0;
144 break;
145 }
146
147 return 0;
148} // int rom_ld_operation (void *cd, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
149
150
151int rom_st_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
152{
153
154 // redirect store to access sparse mem
155 switch (size)
156 {
157 case 1: memwrite8u (mm1, paddr, *buf); break;
158 case 2: memwrite16u (mm1, paddr, *buf); break;
159 case 4: memwrite32u (mm1, paddr, *buf); break;
160 case 8: memwrite64u (mm1, paddr, *buf); break;
161 default:
162 ui->error("rom: st_operation pa 0x%llx size %d cpu %d, ignored\n",
163 paddr, size, cpuid);
164 break;
165 }
166
167 return 0;
168} // int rom_st_operation (void *cd, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
169
170
171/////////////////////////////////////////////////
172
173extern "C" void _fini ()
174{
175}
176
177/////////////////////////////////////////////////
178
179