Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / verif / model / systemc / niu / niu_setup.cpp
CommitLineData
86530b38
AT
1// ========== Copyright Header Begin ==========================================
2//
3// OpenSPARC T2 Processor File: niu_setup.cpp
4// Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved
5// 4150 Network Circle, Santa Clara, California 95054, U.S.A.
6//
7// * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8//
9// This program is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; version 2 of the License.
12//
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21//
22// For the avoidance of doubt, and except that if any non-GPL license
23// choice is available it will apply instead, Sun elects to use only
24// the General Public License version 2 (GPLv2) at this time for any
25// software where a choice of GPL license versions is made
26// available with the language indicating that GPLv2 or any later version
27// may be used, or where a choice of which version of the GPL is applied is
28// otherwise unspecified.
29//
30// Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
31// CA 95054 USA or visit www.sun.com if you need additional information or
32// have any questions.
33//
34// ========== Copyright Header End ============================================
35// ========== Copyright Header Begin ==========================================
36//
37// OpenSPARC T2 Processor File: niu_setup.cpp
38// Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved
39// 4150 Network Circle, Santa Clara, California 95054, U.S.A.
40//
41// * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
42//
43// This program is free software; you can redistribute it and/or modify
44// it under the terms of the GNU General Public License as published by
45// the Free Software Foundation; version 2 of the License.
46//
47// This program is distributed in the hope that it will be useful,
48// but WITHOUT ANY WARRANTY; without even the implied warranty of
49// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50// GNU General Public License for more details.
51//
52// You should have received a copy of the GNU General Public License
53// along with this program; if not, write to the Free Software
54// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
55//
56// For the avoidance of doubt, and except that if any non-GPL license
57// choice is available it will apply instead, Sun elects to use only
58// the General Public License version 2 (GPLv2) at this time for any
59// software where a choice of GPL license versions is made
60// available with the language indicating that GPLv2 or any later version
61// may be used, or where a choice of which version of the GPL is applied is
62// otherwise unspecified.
63//
64// Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
65// CA 95054 USA or visit www.sun.com if you need additional information or
66// have any questions.
67//
68// ========== Copyright Header End ============================================
69#include <sys/types.h>
70#include <sys/stat.h>
71#include <fcntl.h>
72#include <sys/types.h>
73#include <signal.h>
74
75
76#include <systemc.h>
77#include <string.h>
78#include <sys/types.h>
79#include <sys/ipc.h>
80#include <sys/shm.h>
81#include <stdio.h>
82#include <stdlib.h>
83#include <unistd.h>
84#include <thread.h>
85#include <synch.h>
86#include <pthread.h>
87#include "niu_setup.h"
88
89#include "loader.h"
90#include "sharedmem.h"
91
92extern sharedmem *shmem;
93
94mmi_info_t module_info;
95#define NUMARGS 1
96#define DEBUGLVL 2
97#define NIUBASE 0x8100000000ull
98
99
100niu_setup::niu_setup(sc_module_name) {
101 shmem = new sharedmem(getpid(), 1);
102
103 const char *dv_root = getenv("DV_ROOT");
104 char *dlpath = new char[strlen(dv_root) + 100];
105 sprintf(dlpath, "%s/verif/model/systemc/niu/n2niu.so", dv_root);
106
107 void *handle = dlopen(dlpath, RTLD_LAZY);
108
109 if(handle == NULL) {
110 char *dlerr = dlerror();
111 fprintf(stderr,"dlopen failed\nError: %s\n", dlerr);
112 exit(1);
113 } else {
114 init_instance(handle, "n2niu", "n2niu");
115 }
116}
117
118void init_instance(void *handle, char *modname, char*instname) {
119 if (module_info.create_instance == NULL) {
120 fprintf(stderr,"failed to find module instance creator\n");
121 exit(1);
122 }
123 parse_mod_args(modname, instname);
124 module_info.create_instance(module_info.modname, module_info.instance_name);
125 module_info.access_fn = (mmi_access)dlsym(handle,"niu_access");
126 fprintf(stderr, "Done creating instance %s of %s\n", instname, modname);
127}
128
129// Setup all the module parameters
130void parse_mod_args(char *modname, char*instname) {
131 module_info.modname = strdup(modname);
132 module_info.instance_name = strdup(instname);
133 module_info.argc = NUMARGS;
134 module_info.argv = (char**)calloc(NUMARGS,sizeof(char*));
135 module_info.argv[0] = strdup("-d2");
136}
137
138
139struct AccessData {
140 uint64_t pa;
141 uint64_t pl;
142};
143
144
145uint64_t swap64(uint64_t data) {
146 return ((data & 0x00000000000000ffull) << 56) + ((data & 0x000000000000ff00ull) << 40) +
147 ((data & 0x0000000000ff0000ull) << 24) + ((data & 0x00000000ff000000ull) << 8) +
148 ((data & 0x000000ff00000000ull) >> 8) + ((data & 0x0000ff0000000000ull) >> 24) +
149 ((data & 0x00ff000000000000ull) >> 40) + ((data & 0xff00000000000000ull) >> 56);
150}
151
152void parseUCBPacket(uint32_t *mem, uint32_t &req, uint64_t &pa, uint64_t &pl,
153 uint32_t &cpu, uint32_t &thread, uint32_t &buf, uint32_t &size) {
154
155 pl = (((unsigned long long int) mem[3]) << 32) + mem[2];
156 pa = (((unsigned long long int)mem[1] & 0x7fffff) << 17) + (mem[0] >> 15);
157
158 pa &= 0x00000000ffffffffull;
159 pa |= NIUBASE;
160 printf("NIU Address: %016llx\n",pa);
161
162 cpu = (mem[0] >> 7) & 0x7;
163 thread = (mem[0] >> 4) & 0x7;
164 buf = (mem[0] >> 10) & 0x3;
165 size = (mem[0] >> 12) & 0x7;
166 req = mem[0] & 0xf;
167
168 //printf("NCU REQ:%x Cpu:%x Thread:%x Buf:%x Size:%x PA:%llx Payload:%llx\n", req, cpu, thread, buf, size, pa, pl);
169}
170
171
172void* niu_write_access_thread (void *arg) {
173 mmi_access niu_access = module_info.access_fn;
174 void *modptr = module_info.cb_data;
175 AccessData *adata = (AccessData *) arg;
176
177 int res = niu_access(0, modptr, adata->pa , mmi_true, 8, &(adata->pl), 0);
178
179 return (void *) 0x0;
180}
181
182
183
184
185void ncu_interface(unsigned int mem[4]) {
186 mmi_access niu_access = module_info.access_fn;
187 void *modptr = module_info.cb_data;
188 uint64_t data;
189
190 uint64_t pa, pl;
191 uint32_t req, cpu, thread, buf, size;
192 parseUCBPacket(mem, req, pa, pl, cpu, thread, buf, size);
193
194 data = pl;
195
196 int corrected = 0;
197 if(req !=5) {
198 if(pa == 0x8100182000ull) {
199 corrected = 1;
200 data = swap64(0x0000000000002040ull);
201 }
202 if(pa == 0x8100182008ull) {
203 corrected = 1;
204 data = swap64(0x0000000000000084ull);
205 }
206 if(pa == 0x8100188008ull) {
207 corrected = 1;
208 data = swap64(0x0000000000000084ull);
209 }
210 }
211 if(req == 5)
212 printf("%lld NIU MODEL W: PA:%llx Data:%llx\n", sc_time_stamp().value(), pa, data);
213 else
214 printf("%lld NIU MODEL R: PA:%llx\n", sc_time_stamp().value(), pa);
215
216 fflush(stdout);
217
218 if(!corrected) {
219 if(req == 5) {
220 AccessData *adata = new AccessData;
221 adata->pa = pa;
222 adata->pl = data;
223 thread_t tid;
224 int ret = thr_create(NULL, NULL, niu_write_access_thread, adata, THR_DETACHED , &tid);
225 if(ret != 0) {
226 printf("NIU Write access thread creation failed!\n\n");
227 }
228 } else {
229 int res = niu_access(0, modptr, pa, mmi_false, 8, &data, 0);
230 if(res == 1) {
231 printf("Error in niu_access function!: %d\n",res);
232 }
233 }
234 }
235
236 if(req == 4) {
237 fprintf(stdout,"Read Data=0x%llx\n\n",data);
238 mem[2] = data & 0xffffffff;
239 mem[3] = (data >> 32) & 0xffffffff;
240 }
241
242}
243
244