Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / devices / dummy_mods / tod / tod.cc
// ========== Copyright Header Begin ==========================================
//
// OpenSPARC T2 Processor File: tod.cc
// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
//
// The above named program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License version 2 as published by the Free Software Foundation.
//
// The above named program is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this work; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
// ========== Copyright Header End ============================================
/* File: tod.cc
*
* Fake time of the day device.
* Returns time from the host machine.
*
* Copyright (c) 2006 by Sun Microsystems, Inc.
* All rights reserved.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/syscall.h> // get exec number
#include <sys/types.h> // uid_t
#include <unistd.h> // getuid()
#include <inttypes.h>
#include <stdarg.h>
#include <thread.h>
#include "mmi.h"
#include "ui.h"
struct sam_tod_s {
uint64_t startpa;
uint64_t endpa;
uint64_t sz;
}; // struct sam_tod.C
extern "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);
int tod_ld_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid);
int tod_st_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid);
extern "C" void tod_create_instance (const char *modname, const char *instance_name);
extern "C" void _init()
{
if (! mmi_register_instance_creator("tod", tod_create_instance) ) {
ui->error("Cannot register instance creator for tod\n");
}
} // _init()
void tod_create_instance (const char *modname, const char *instance_name)
{
sam_tod_s * tod_obj = new sam_tod_s;
mmi_instance_t instance = mmi_register_instance(modname, instance_name, (void *) &tod_obj, "tod dummy device");
tod_obj->startpa = 0x0;
tod_obj->endpa = 0x0;
tod_obj->sz = 0;
// expected syntax: sysconf tod tod1 startpa=<0xblah> endpa=<0xblah>
int argc = mmi_argc(instance);
int i;
for (i=0; i<argc; i++) {
char * arg = strdup(mmi_argv(instance, i));
char * marker;
char * lv = strtok_r(arg, "=", &marker);
if (strcmp(lv, "startpa") == 0) {
errno = 0;
char * rv = strtok_r(NULL, "=", &marker);
tod_obj->startpa = strtoull(rv, NULL, 0);
if (errno) {
perror("tod: error parsing startpa");
exit(1);
}
} else if (strcmp(lv, "endpa") == 0) {
errno = 0;
char * rv = strtok_r(NULL, "=", &marker);
tod_obj->endpa = strtoull(rv, NULL, 0);
if (errno) {
perror("tod: error parsing endpa");
exit(1);
}
} // else - do nothing
}
tod_obj->sz = tod_obj->endpa - tod_obj->startpa + 1;
if (mmi_map_physio(tod_obj->startpa, tod_obj->sz, (void *) tod_obj, tod_access)) {
ui->error(" sam tod: unable to register IO interceptor\n");
return;
}
}
int tod_access(uint32_t cpuid, void* obj, uint64_t paddr, mmi_bool_t wr, uint32_t size, uint64_t* buf, uint8_t bytemask)
{
if (wr) {
return tod_st_operation(obj, paddr, buf, size, cpuid);
} else {
return tod_ld_operation(obj, paddr, buf, size, cpuid);
}
} // tod_access()
int tod_ld_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
{
sam_tod_s * tod_obj = (sam_tod_s *) obj;
uint64_t offset = paddr - tod_obj->startpa;
if ((offset == 0) && (size == 8))
{
// time of the day
*buf = time(NULL);
}
else
{
*buf = 0;
fprintf(stderr, "SAM tod: ld_operation pa 0x%llx size %d cpu %d\n", paddr, size, cpuid);
}
return 0;
} // int tod_ld_operation ()
int tod_st_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
{
fprintf(stderr, "SAM tod: st_operation pa 0x%llx size %d cpu %d, ignored\n", paddr, size, cpuid);
return 0;
} // int tod_st_operation ()
/////////////////////////////////////////////////
extern "C" void _fini ()
{
}
/////////////////////////////////////////////////