Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: SS_CsrAccess.h | |
5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
7 | * | |
8 | * The above named program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public | |
10 | * License version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * The above named program is distributed in the hope that it will be | |
13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public | |
18 | * License along with this work; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | * | |
21 | * ========== Copyright Header End ============================================ | |
22 | */ | |
23 | #ifndef SS_CSR_ACCESS_H | |
24 | #define SS_CSR_ACCESS_H | |
25 | /************************************************************************ | |
26 | ** | |
27 | ** Copyright (C) 2006, Sun Microsystems, Inc. | |
28 | ** | |
29 | ** Sun considers its source code as an unpublished, proprietary | |
30 | ** trade secret and it is available only under strict license provisions. | |
31 | ** This copyright notice is placed here only to protect Sun in the event | |
32 | ** the source is deemed a published work. Disassembly, decompilation, | |
33 | ** or other means of reducing the object code to human readable form | |
34 | ** is prohibited by the license agreement under which this code is | |
35 | ** provided to the user or company in possession of this copy. | |
36 | ** | |
37 | *************************************************************************/ | |
38 | #include "SS_Csr.h" | |
39 | #include "SS_Strand.h" | |
40 | #include "MemoryTransaction.h" | |
41 | ||
42 | template <typename T> | |
43 | class SS_CsrAccess | |
44 | { | |
45 | public: | |
46 | SS_CsrAccess(SS_Csr& csr) : | |
47 | csr_(csr) {} | |
48 | virtual ~SS_CsrAccess() {} | |
49 | const SS_CsrAccess<T> & operator=( const SS_CsrAccess<T> &rhs ) | |
50 | { | |
51 | fprintf(stderr, "ERROR: SS_CsrAccess<T> & operator=(): Unimplemented function\n"); | |
52 | return *this; | |
53 | } | |
54 | ||
55 | /* Access a CSR at index 0 */ | |
56 | bool access(T &csr, bool isRead) { | |
57 | return access(0, csr, isRead); | |
58 | } | |
59 | ||
60 | /* Access a CSR at ndx for read or write. | |
61 | * | |
62 | * The CSR is indexed relative to its base address, using its | |
63 | * memory stride. This information is set by the CSR's class and | |
64 | * defined by the PRM. | |
65 | */ | |
66 | bool access(uint64_t ndx, T &csr, bool isRead) { | |
67 | SS_Strand* strand = NULL; //TODO need a valid strand-id | |
68 | uint64_t data = 0; | |
69 | uint64_t pa = T::BaseAddress + ndx * T::Stride; | |
70 | ||
71 | if (ndx >= T::Count) { | |
72 | fprintf(stderr, "ERROR: SS_CsrAccess::access(): Out of bounds index %d\n", ndx); | |
73 | return true; | |
74 | } | |
75 | ||
76 | int access = MemoryTransaction::INTERNAL; | |
77 | if (isRead) { | |
78 | int state = csr_.read64(pa, &data, (access|MemoryTransaction::READ), 0/*(strand->strand_id()*/); | |
79 | // if the Csr hasn't been set, use the reset value | |
80 | if (state != SS_Io::OK){ | |
81 | T tmp; | |
82 | csr = tmp; | |
83 | } else { | |
84 | csr.set_data(data); | |
85 | } | |
86 | } | |
87 | else { | |
88 | data = csr.getNative(); | |
89 | csr_.write64(pa, data, (access|MemoryTransaction::WRITE), 0/*strand->strand_id()*/); | |
90 | } | |
91 | return true; | |
92 | } | |
93 | ||
94 | /* Return the CSR's raw 64-bit at index, ndx. */ | |
95 | uint64_t getNative(uint64_t ndx) { | |
96 | T csr; | |
97 | access(ndx, csr, true); | |
98 | return csr.getNative(); | |
99 | } | |
100 | ||
101 | /* Ptr-to-member-function types used to access CSR's bitfields. */ | |
102 | typedef uint64_t (T::*GetFn)(void) const; | |
103 | typedef void (T::*SetFn)(uint64_t); | |
104 | ||
105 | /* Return the CSR's bitfield associated with getFn() at index 0. */ | |
106 | uint64_t get(GetFn getFn) { | |
107 | return get(0, getFn); | |
108 | } | |
109 | ||
110 | /* Return the CSR's bitfield associated with getFn() at 'ndx'. */ | |
111 | uint64_t get(uint64_t ndx, GetFn getFn) { | |
112 | T csr; | |
113 | access(ndx, csr, true); | |
114 | return ((csr).*(getFn))(); | |
115 | } | |
116 | ||
117 | /* Set the CSR at index 0 to 'value'. Return the CSR's new result. */ | |
118 | T setNative(uint64_t ndx, uint64_t value) { | |
119 | T csr; | |
120 | csr.setNative(value); | |
121 | access(ndx, csr, false); | |
122 | return csr; | |
123 | } | |
124 | ||
125 | /* Set the CSR's bitfield associated with setFn at index 0 to ones. | |
126 | * Return the CSR's new result. | |
127 | */ | |
128 | T set(SetFn setFn) { | |
129 | return set(0, setFn, ~0ULL); | |
130 | } | |
131 | ||
132 | /* Set the CSR's bitfield associated with setFn at index 0 to 'value'. | |
133 | * Return the CSR's new result. | |
134 | */ | |
135 | T set(SetFn setFn, uint64_t value) { | |
136 | return set(0, setFn, value); | |
137 | } | |
138 | ||
139 | /* Set the CSR's bitfield associated with setFn at 'ndx' to ones. | |
140 | * Return the CSR's new result. | |
141 | */ | |
142 | T set(uint64_t ndx, SetFn setFn) { | |
143 | T csr; | |
144 | access(ndx, csr, true); | |
145 | ((csr).*(setFn))(~0ULL); | |
146 | access(ndx, csr, false); | |
147 | return csr; | |
148 | } | |
149 | ||
150 | /* Set the CSR's bitfield associated with setFn at 'ndx' to 'value'. | |
151 | * Return the CSR's new result. | |
152 | */ | |
153 | T set(uint64_t ndx, SetFn setFn, uint64_t value) { | |
154 | T csr; | |
155 | access(ndx, csr, true); | |
156 | ((csr).*(setFn))(value); | |
157 | access(ndx, csr, false); | |
158 | return csr; | |
159 | } | |
160 | ||
161 | private: | |
162 | SS_CsrAccess(); | |
163 | ||
164 | SS_Csr& csr_; | |
165 | }; | |
166 | ||
167 | #endif /* SS_CSR_ACCESS_H */ | |
168 | ||
169 |