| 1 | // ========== Copyright Header Begin ========================================== |
| 2 | // |
| 3 | // OpenSPARC T2 Processor File: l2csr.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 | /* l2csr.C |
| 22 | * handle L2CSR (I/O space) T1 addresses for blaze/SAM |
| 23 | * |
| 24 | * Copyright (c) 2004-2005 by Sun Microsystems, Inc. |
| 25 | * All rights reserved. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <errno.h> |
| 33 | #include <sys/syscall.h> // get exec number |
| 34 | #include <sys/types.h> // uid_t |
| 35 | #include <unistd.h> // getuid() |
| 36 | #include <inttypes.h> |
| 37 | #include <stdarg.h> |
| 38 | |
| 39 | #include <thread.h> |
| 40 | |
| 41 | #include "mmi.h" |
| 42 | #include "ui.h" |
| 43 | |
| 44 | struct sam_l2csr_s { |
| 45 | uint64_t startpa; |
| 46 | uint64_t endpa; |
| 47 | uint64_t sz; |
| 48 | }; // struct sam_l2csr.C |
| 49 | |
| 50 | |
| 51 | extern "C" { |
| 52 | void _init (); // predeclare these to have C naming |
| 53 | void _fini (); // predeclare these to have C naming |
| 54 | |
| 55 | int l2csr_access(uint32_t cpuid, void* obj, uint64_t paddr, mmi_bool_t wr, uint32_t size, uint64_t* buf, uint8_t bytemask); |
| 56 | |
| 57 | int l2csr_ld_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid); |
| 58 | int l2csr_st_operation (void *cd, uint64_t paddr, uint64_t *buf, int sz, uint32_t cpuid); |
| 59 | } |
| 60 | |
| 61 | extern "C" void l2csr_create_instance (const char *modname, const char *instance_name); |
| 62 | |
| 63 | extern "C" void _init() |
| 64 | { |
| 65 | if (! mmi_register_instance_creator("l2csr", l2csr_create_instance) ) { |
| 66 | fprintf(stderr, "Cannot register instance creator for l2csr\n"); |
| 67 | } |
| 68 | } // _init() |
| 69 | |
| 70 | |
| 71 | |
| 72 | void l2csr_create_instance (const char *modname, const char *instance_name) |
| 73 | { |
| 74 | sam_l2csr_s * l2csr_obj = new sam_l2csr_s; |
| 75 | |
| 76 | mmi_instance_t instance = mmi_register_instance(modname, instance_name, (void *) &l2csr_obj, "l2csr dummy device"); |
| 77 | |
| 78 | l2csr_obj->startpa = 0x0; |
| 79 | l2csr_obj->endpa = 0x0; |
| 80 | l2csr_obj->sz = 0; |
| 81 | |
| 82 | // expected syntax: sysconf l2csr l2csr1 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 | l2csr_obj->startpa = strtoull(rv, NULL, 0); |
| 94 | if (errno) { |
| 95 | perror("l2csr: 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 | l2csr_obj->endpa = strtoull(rv, NULL, 0); |
| 102 | if (errno) { |
| 103 | perror("l2csr: error parsing startpa"); |
| 104 | exit(1); |
| 105 | } |
| 106 | } // else - do nothing |
| 107 | } |
| 108 | |
| 109 | l2csr_obj->sz = l2csr_obj->endpa - l2csr_obj->startpa + 1; |
| 110 | if (mmi_map_physio(l2csr_obj->startpa, l2csr_obj->sz, (void *) l2csr_obj, l2csr_access)) { |
| 111 | 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); |
| 112 | return; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | int l2csr_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 l2csr_st_operation(obj, paddr, buf, size, cpuid); |
| 121 | } else { |
| 122 | return l2csr_ld_operation(obj, paddr, buf, size, cpuid); |
| 123 | } |
| 124 | } // l2csr_access() |
| 125 | |
| 126 | int l2csr_ld_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid) |
| 127 | { |
| 128 | sam_l2csr_s * l2csr_obj = (sam_l2csr_s *) obj; |
| 129 | uint64_t offset = paddr - l2csr_obj->startpa; |
| 130 | // fprintf(stderr, "SAM l2csr: ld_operation pa 0x%llx size %d cpu %d\n", paddr, size, cpuid); |
| 131 | switch (offset) { |
| 132 | case 0x0: |
| 133 | case 0x40: |
| 134 | case 0x80: |
| 135 | case 0xc0: |
| 136 | *buf = 0; |
| 137 | break; |
| 138 | default: |
| 139 | *buf = 0; |
| 140 | break; |
| 141 | // fprintf(stderr, "SAM l2csr: ld from invalid paddr 0x%llx\n", paddr); |
| 142 | // exit(1); |
| 143 | } |
| 144 | return 0; |
| 145 | } // int l2csr_ld_operation (void *cd, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid) |
| 146 | |
| 147 | |
| 148 | int l2csr_st_operation (void *obj, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid) |
| 149 | { |
| 150 | // fprintf(stderr, "SAM l2csr: st_operation pa 0x%llx size %d cpu %d\n", paddr, size, cpuid); |
| 151 | sam_l2csr_s * l2csr_obj = (sam_l2csr_s *) obj; |
| 152 | uint64_t offset = paddr - l2csr_obj->startpa; |
| 153 | switch (offset) { |
| 154 | case 0x00: |
| 155 | case 0x40: |
| 156 | case 0x80: |
| 157 | case 0xc0: |
| 158 | // do nothing |
| 159 | break; |
| 160 | default: |
| 161 | break; |
| 162 | // fprintf(stderr, "SAM l2csr: st to invalid paddr 0x%llx\n", paddr); |
| 163 | // exit(1); |
| 164 | } |
| 165 | return 0; |
| 166 | } // int l2csr_st_operation (void *cd, uint64_t paddr, uint64_t *buf, int size, uint32_t cpuid) |
| 167 | |
| 168 | |
| 169 | ///////////////////////////////////////////////// |
| 170 | |
| 171 | extern "C" void _fini () |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | ///////////////////////////////////////////////// |