Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / devices / dummy_mods / tod / tod.cc
CommitLineData
920dae64
AT
1// ========== Copyright Header Begin ==========================================
2//
3// OpenSPARC T2 Processor File: tod.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/* File: tod.cc
22 *
23 * Fake time of the day device.
24 * Returns time from the host machine.
25 *
26 * Copyright (c) 2006 by Sun Microsystems, Inc.
27 * All rights reserved.
28 *
29 */
30
31
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <errno.h>
37#include <sys/syscall.h> // get exec number
38#include <sys/types.h> // uid_t
39#include <unistd.h> // getuid()
40#include <inttypes.h>
41#include <stdarg.h>
42
43#include <thread.h>
44
45#include "mmi.h"
46#include "ui.h"
47
48struct sam_tod_s {
49 uint64_t startpa;
50 uint64_t endpa;
51 uint64_t sz;
52}; // struct sam_tod.C
53
54
55extern "C" int tod_access(uint32_t cpuid, void* obj, uint64_t paddr, mmi_bool_t wr, uint32_t size, uint64_t* buf, uint8_t bytemask);
56
57int tod_ld_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid);
58int tod_st_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid);
59
60extern "C" void tod_create_instance (const char *modname, const char *instance_name);
61
62
63extern "C" void _init()
64{
65 if (! mmi_register_instance_creator("tod", tod_create_instance) ) {
66 ui->error("Cannot register instance creator for tod\n");
67 }
68} // _init()
69
70
71
72void tod_create_instance (const char *modname, const char *instance_name)
73{
74 sam_tod_s * tod_obj = new sam_tod_s;
75
76 mmi_instance_t instance = mmi_register_instance(modname, instance_name, (void *) &tod_obj, "tod dummy device");
77
78 tod_obj->startpa = 0x0;
79 tod_obj->endpa = 0x0;
80 tod_obj->sz = 0;
81
82 // expected syntax: sysconf tod tod1 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 tod_obj->startpa = strtoull(rv, NULL, 0);
94 if (errno) {
95 perror("tod: 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 tod_obj->endpa = strtoull(rv, NULL, 0);
102 if (errno) {
103 perror("tod: error parsing endpa");
104 exit(1);
105 }
106 } // else - do nothing
107 }
108
109 tod_obj->sz = tod_obj->endpa - tod_obj->startpa + 1;
110 if (mmi_map_physio(tod_obj->startpa, tod_obj->sz, (void *) tod_obj, tod_access)) {
111 ui->error(" sam tod: unable to register IO interceptor\n");
112 return;
113 }
114}
115
116
117int tod_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 tod_st_operation(obj, paddr, buf, size, cpuid);
121 } else {
122 return tod_ld_operation(obj, paddr, buf, size, cpuid);
123 }
124} // tod_access()
125
126int tod_ld_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
127{
128 sam_tod_s * tod_obj = (sam_tod_s *) obj;
129 uint64_t offset = paddr - tod_obj->startpa;
130
131 if ((offset == 0) && (size == 8))
132 {
133 // time of the day
134 *buf = time(NULL);
135 }
136 else
137 {
138 *buf = 0;
139 fprintf(stderr, "SAM tod: ld_operation pa 0x%llx size %d cpu %d\n", paddr, size, cpuid);
140 }
141
142 return 0;
143} // int tod_ld_operation ()
144
145
146int tod_st_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
147{
148 fprintf(stderr, "SAM tod: st_operation pa 0x%llx size %d cpu %d, ignored\n", paddr, size, cpuid);
149
150 return 0;
151} // int tod_st_operation ()
152
153
154/////////////////////////////////////////////////
155
156extern "C" void _fini ()
157{
158}
159
160/////////////////////////////////////////////////