Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / devices / dummy_mods / l2csr / l2csr.cc
// ========== Copyright Header Begin ==========================================
//
// OpenSPARC T2 Processor File: l2csr.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 ============================================
/* l2csr.C
* handle L2CSR (I/O space) T1 addresses for blaze/SAM
*
* Copyright (c) 2004-2005 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_l2csr_s {
uint64_t startpa;
uint64_t endpa;
uint64_t sz;
}; // struct sam_l2csr.C
extern "C" {
void _init (); // predeclare these to have C naming
void _fini (); // predeclare these to have C naming
int l2csr_access(uint32_t cpuid, void* obj, uint64_t paddr, mmi_bool_t wr, uint32_t size, uint64_t* buf, uint8_t bytemask);
int l2csr_ld_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid);
int l2csr_st_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid);
}
extern "C" void l2csr_create_instance (const char *modname, const char *instance_name);
extern "C" void _init()
{
if (! mmi_register_instance_creator("l2csr", l2csr_create_instance) ) {
fprintf(stderr, "Cannot register instance creator for l2csr\n");
}
} // _init()
void l2csr_create_instance (const char *modname, const char *instance_name)
{
sam_l2csr_s * l2csr_obj = new sam_l2csr_s;
mmi_instance_t instance = mmi_register_instance(modname, instance_name, (void *) &l2csr_obj, "l2csr dummy device");
l2csr_obj->startpa = 0x0;
l2csr_obj->endpa = 0x0;
l2csr_obj->sz = 0;
// expected syntax: sysconf l2csr l2csr1 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);
l2csr_obj->startpa = strtoull(rv, NULL, 0);
if (errno) {
perror("l2csr: error parsing startpa");
exit(1);
}
} else if (strcmp(lv, "endpa") == 0) {
errno = 0;
char * rv = strtok_r(NULL, "=", &marker);
l2csr_obj->endpa = strtoull(rv, NULL, 0);
if (errno) {
perror("l2csr: error parsing startpa");
exit(1);
}
} // else - do nothing
}
l2csr_obj->sz = l2csr_obj->endpa - l2csr_obj->startpa + 1;
if (mmi_map_physio(l2csr_obj->startpa, l2csr_obj->sz, (void *) l2csr_obj, l2csr_access)) {
fprintf(stderr, " sam l2csr (%s): unable to register IO interceptor @PA=0x%llx size=0x%llx\n", instance_name, l2csr_obj->startpa, l2csr_obj->sz);
return;
}
}
int l2csr_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 l2csr_st_operation(obj, paddr, buf, size, cpuid);
} else {
return l2csr_ld_operation(obj, paddr, buf, size, cpuid);
}
} // l2csr_access()
int l2csr_ld_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
{
sam_l2csr_s * l2csr_obj = (sam_l2csr_s *) obj;
uint64_t offset = paddr - l2csr_obj->startpa;
// fprintf(stderr, "SAM l2csr: ld_operation pa 0x%llx size %d cpu %d\n", paddr, size, cpuid);
switch (offset) {
case 0x0:
case 0x40:
case 0x80:
case 0xc0:
*buf = 0;
break;
default:
*buf = 0;
break;
// fprintf(stderr, "SAM l2csr: ld from invalid paddr 0x%llx\n", paddr);
// exit(1);
}
return 0;
} // int l2csr_ld_operation (void *cd, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
int l2csr_st_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
{
// fprintf(stderr, "SAM l2csr: st_operation pa 0x%llx size %d cpu %d\n", paddr, size, cpuid);
sam_l2csr_s * l2csr_obj = (sam_l2csr_s *) obj;
uint64_t offset = paddr - l2csr_obj->startpa;
switch (offset) {
case 0x00:
case 0x40:
case 0x80:
case 0xc0:
// do nothing
break;
default:
break;
// fprintf(stderr, "SAM l2csr: st to invalid paddr 0x%llx\n", paddr);
// exit(1);
}
return 0;
} // int l2csr_st_operation (void *cd, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid)
/////////////////////////////////////////////////
extern "C" void _fini ()
{
}
/////////////////////////////////////////////////