本项止转自github官方arduino-esp32 传在这里仅为阅读源码方便
projectuser
2019-07-05 50148ccffe21ff54262064ec9f2245900eaf18aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 * xtensa/cacheattrasm.h -- assembler-specific CACHEATTR register related definitions
 *            that depend on CORE configuration
 *
 *  This file is logically part of xtensa/coreasm.h (or perhaps xtensa/cacheasm.h),
 *  but is kept separate for modularity / compilation-performance.
 */
 
/*
 * Copyright (c) 2001-2009 Tensilica Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
#ifndef XTENSA_CACHEATTRASM_H
#define XTENSA_CACHEATTRASM_H
 
#include <xtensa/coreasm.h>
 
/*  Determine whether cache attributes are controlled using eight 512MB entries:  */
#define XCHAL_CA_8X512    (XCHAL_HAVE_CACHEATTR || XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR \
    || (XCHAL_HAVE_PTP_MMU && XCHAL_HAVE_SPANNING_WAY))
 
 
/*
 *  This header file defines assembler macros of the form:
 *    <x>cacheattr_<func>
 *  where:
 *    <x> is 'i', 'd' or absent for instruction, data
 *        or both caches; and
 *    <func> indicates the function of the macro.
 *
 *  The following functions are defined:
 *
 *  icacheattr_get
 *    Reads I-cache CACHEATTR into a2 (clobbers a3-a5).
 *
 *  dcacheattr_get
 *    Reads D-cache CACHEATTR into a2 (clobbers a3-a5).
 *    (Note:  for configs with a real CACHEATTR register, the
 *     above two macros are identical.)
 *
 *  cacheattr_set
 *    Writes both I-cache and D-cache CACHEATTRs from a2 (a3-a8 clobbered).
 *    Works even when changing one's own code's attributes.
 *
 *  icacheattr_is_enabled  label
 *    Branches to \label if I-cache appears to have been enabled
 *    (eg. if CACHEATTR contains a cache-enabled attribute).
 *    (clobbers a2-a5,SAR)
 *
 *  dcacheattr_is_enabled  label
 *    Branches to \label if D-cache appears to have been enabled
 *    (eg. if CACHEATTR contains a cache-enabled attribute).
 *    (clobbers a2-a5,SAR)
 *
 *  cacheattr_is_enabled  label
 *    Branches to \label if either I-cache or D-cache appears to have been enabled
 *    (eg. if CACHEATTR contains a cache-enabled attribute).
 *    (clobbers a2-a5,SAR)
 *
 *  The following macros are only defined under certain conditions:
 *
 *  icacheattr_set    (if XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR)
 *    Writes I-cache CACHEATTR from a2 (a3-a8 clobbered).
 *
 *  dcacheattr_set    (if XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR)
 *    Writes D-cache CACHEATTR from a2 (a3-a8 clobbered).
 */
 
 
 
/***************************   GENERIC -- ALL CACHES   ***************************/
 
/*
 *  _cacheattr_get
 *
 *  (Internal macro.)
 *  Returns value of CACHEATTR register (or closest equivalent) in a2.
 *  
 *  Entry:
 *    (none)
 *  Exit:
 *    a2    value read from CACHEATTR
 *    a3-a5    clobbered (temporaries)
 */
    .macro    _cacheattr_get    tlb
#if XCHAL_HAVE_CACHEATTR
    rsr    a2, CACHEATTR
#elif XCHAL_CA_8X512
    //  We have a config that "mimics" CACHEATTR using a simplified
    //  "MMU" composed of a single statically-mapped way.
    //  DTLB and ITLB are independent, so there's no single
    //  cache attribute that can describe both.  So for now
    //  just return the DTLB state.
    movi    a5, 0xE0000000
    movi    a2, 0
    movi    a3, XCHAL_SPANNING_WAY
1:    add    a3, a3, a5    // next segment
    r&tlb&1    a4, a3        // get PPN+CA of segment at 0xE0000000, 0xC0000000, ..., 0
    dsync    // interlock???
    slli    a2, a2, 4
    extui    a4, a4, 0, 4    // extract CA
    or    a2, a2, a4
    bgeui    a3, 16, 1b
#else
    //  This macro isn't applicable to arbitrary MMU configurations.
    //  Just return zero.
    movi    a2, 0
#endif
    .endm
 
    .macro    icacheattr_get
    _cacheattr_get    itlb
    .endm
 
    .macro    dcacheattr_get
    _cacheattr_get    dtlb
    .endm
 
 
/* Default (powerup/reset) value of CACHEATTR,
   all BYPASS mode (ie. disabled/bypassed caches): */
#if XCHAL_HAVE_PTP_MMU
# define XCHAL_CACHEATTR_ALL_BYPASS    0x33333333
#else
# define XCHAL_CACHEATTR_ALL_BYPASS    0x22222222
#endif
 
#if XCHAL_CA_8X512
 
#if XCHAL_HAVE_PTP_MMU
# define XCHAL_FCA_ENAMASK    0x0AA0    /* bitmap of fetch attributes that require enabled icache */
# define XCHAL_LCA_ENAMASK    0x0FF0    /* bitmap of load  attributes that require enabled dcache */
# define XCHAL_SCA_ENAMASK    0x0CC0    /* bitmap of store attributes that require enabled dcache */
#else
# define XCHAL_FCA_ENAMASK    0x003A    /* bitmap of fetch attributes that require enabled icache */
# define XCHAL_LCA_ENAMASK    0x0033    /* bitmap of load  attributes that require enabled dcache */
# define XCHAL_SCA_ENAMASK    0x0033    /* bitmap of store attributes that require enabled dcache */
#endif
#define XCHAL_LSCA_ENAMASK    (XCHAL_LCA_ENAMASK|XCHAL_SCA_ENAMASK)    /* l/s attrs requiring enabled dcache */
#define XCHAL_ALLCA_ENAMASK    (XCHAL_FCA_ENAMASK|XCHAL_LSCA_ENAMASK)    /* all attrs requiring enabled caches */
 
/*
 *  _cacheattr_is_enabled
 *
 *  (Internal macro.)
 *  Branches to \label if CACHEATTR in a2 indicates an enabled
 *  cache, using mask in a3.
 *
 *  Parameters:
 *    label    where to branch to if cache is enabled
 *  Entry:
 *    a2    contains CACHEATTR value used to determine whether
 *        caches are enabled
 *    a3    16-bit constant where each bit correspond to
 *        one of the 16 possible CA values (in a CACHEATTR mask);
 *        CA values that indicate the cache is enabled
 *        have their corresponding bit set in this mask
 *        (eg. use XCHAL_xCA_ENAMASK , above)
 *  Exit:
 *    a2,a4,a5    clobbered
 *    SAR        clobbered
 */
    .macro    _cacheattr_is_enabled    label
    movi    a4, 8        // loop 8 times
.Lcaife\@:
    extui    a5, a2, 0, 4    // get CA nibble
    ssr    a5        // index into mask according to CA...
    srl    a5, a3        // ...and get CA's mask bit in a5 bit 0
    bbsi.l    a5, 0, \label    // if CA indicates cache enabled, jump to label
    srli    a2, a2, 4    // next nibble
    addi    a4, a4, -1
    bnez    a4, .Lcaife\@    // loop for each nibble
    .endm
 
#else /* XCHAL_CA_8X512 */
    .macro    _cacheattr_is_enabled    label
    j    \label        // macro not applicable, assume caches always enabled
    .endm
#endif /* XCHAL_CA_8X512 */
 
 
 
/*
 *  icacheattr_is_enabled
 *
 *  Branches to \label if I-cache is enabled.
 *
 *  Parameters:
 *    label    where to branch to if icache is enabled
 *  Entry:
 *    (none)
 *  Exit:
 *    a2-a5, SAR    clobbered (temporaries)
 */
    .macro    icacheattr_is_enabled    label
#if XCHAL_CA_8X512
    icacheattr_get
    movi    a3, XCHAL_FCA_ENAMASK
#endif
    _cacheattr_is_enabled    \label
    .endm
 
/*
 *  dcacheattr_is_enabled
 *
 *  Branches to \label if D-cache is enabled.
 *
 *  Parameters:
 *    label    where to branch to if dcache is enabled
 *  Entry:
 *    (none)
 *  Exit:
 *    a2-a5, SAR    clobbered (temporaries)
 */
    .macro    dcacheattr_is_enabled    label
#if XCHAL_CA_8X512
    dcacheattr_get
    movi    a3, XCHAL_LSCA_ENAMASK
#endif
    _cacheattr_is_enabled    \label
    .endm
 
/*
 *  cacheattr_is_enabled
 *
 *  Branches to \label if either I-cache or D-cache is enabled.
 *
 *  Parameters:
 *    label    where to branch to if a cache is enabled
 *  Entry:
 *    (none)
 *  Exit:
 *    a2-a5, SAR    clobbered (temporaries)
 */
    .macro    cacheattr_is_enabled    label
#if XCHAL_HAVE_CACHEATTR
    rsr    a2, CACHEATTR
    movi    a3, XCHAL_ALLCA_ENAMASK
#elif XCHAL_CA_8X512
    icacheattr_get
    movi    a3, XCHAL_FCA_ENAMASK
    _cacheattr_is_enabled    \label
    dcacheattr_get
    movi    a3, XCHAL_LSCA_ENAMASK
#endif
    _cacheattr_is_enabled    \label
    .endm
 
 
 
/*
 *  The ISA does not have a defined way to change the
 *  instruction cache attributes of the running code,
 *  ie. of the memory area that encloses the current PC.
 *  However, each micro-architecture (or class of
 *  configurations within a micro-architecture)
 *  provides a way to deal with this issue.
 *
 *  Here are a few macros used to implement the relevant
 *  approach taken.
 */
 
#if XCHAL_CA_8X512 && !XCHAL_HAVE_CACHEATTR
    //  We have a config that "mimics" CACHEATTR using a simplified
    //  "MMU" composed of a single statically-mapped way.
 
/*
 *  icacheattr_set
 *
 *  Entry:
 *    a2        cacheattr value to set
 *  Exit:
 *    a2        unchanged
 *    a3-a8        clobbered (temporaries)
 */
    .macro    icacheattr_set
 
    movi    a5, 0xE0000000    // mask of upper 3 bits
    movi    a6, 3f        // PC where ITLB is set
    movi    a3, XCHAL_SPANNING_WAY    // start at region 0 (0 .. 7)
    mov    a7, a2        // copy a2 so it doesn't get clobbered
    and    a6, a6, a5    // upper 3 bits of local PC area
    j    3f
 
    //  Use micro-architecture specific method.
    //  The following 4-instruction sequence is aligned such that
    //  it all fits within a single I-cache line.  Sixteen byte
    //  alignment is sufficient for this (using XCHAL_ICACHE_LINESIZE
    //  actually causes problems because that can be greater than
    //  the alignment of the reset vector, where this macro is often
    //  invoked, which would cause the linker to align the reset
    //  vector code away from the reset vector!!).
    .begin    no-transform
    .align    16 /*XCHAL_ICACHE_LINESIZE*/
1:    witlb    a4, a3        // write wired PTE (CA, no PPN) of 512MB segment to ITLB
    isync
    .end    no-transform
    nop
    nop
 
    sub    a3, a3, a5    // next segment (add 0x20000000)
    bltui    a3, 16, 4f    // done?
 
    //  Note that in the WITLB loop, we don't do any load/stores
    //  (may not be an issue here, but it is important in the DTLB case).
2:    srli    a7, a7, 4    // next CA
3:
# if XCHAL_HAVE_MIMIC_CACHEATTR
    extui    a4, a7, 0, 4    // extract CA to set
# else    /* have translation, preserve it: */
    ritlb1    a8, a3        // get current PPN+CA of segment
    //dsync    // interlock???
    extui    a4, a7, 0, 4    // extract CA to set
    srli    a8, a8, 4    // clear CA but keep PPN ...
    slli    a8, a8, 4    // ...
    add    a4, a4, a8    // combine new CA with PPN to preserve
# endif
    beq    a3, a6, 1b    // current PC's region? if so, do it in a safe way
    witlb    a4, a3        // write wired PTE (CA [+PPN]) of 512MB segment to ITLB
    sub    a3, a3, a5    // next segment (add 0x20000000)
    bgeui    a3, 16, 2b
    isync            // make sure all ifetch changes take effect
4:
    .endm    // icacheattr_set
 
 
/*
 *  dcacheattr_set
 *
 *  Entry:
 *    a2        cacheattr value to set
 *  Exit:
 *    a2        unchanged
 *    a3-a8        clobbered (temporaries)
 */
 
    .macro    dcacheattr_set
 
    movi    a5, 0xE0000000    // mask of upper 3 bits
    movi    a3, XCHAL_SPANNING_WAY    // start at region 0 (0 .. 7)
    mov    a7, a2        // copy a2 so it doesn't get clobbered
    //  Note that in the WDTLB loop, we don't do any load/stores
2:    //  (including implicit l32r via movi) because it isn't safe.
# if XCHAL_HAVE_MIMIC_CACHEATTR
    extui    a4, a7, 0, 4    // extract CA to set
# else    /* have translation, preserve it: */
    rdtlb1    a8, a3        // get current PPN+CA of segment
    //dsync    // interlock???
    extui    a4, a7, 0, 4    // extract CA to set
    srli    a8, a8, 4    // clear CA but keep PPN ...
    slli    a8, a8, 4    // ...
    add    a4, a4, a8    // combine new CA with PPN to preserve
# endif
    wdtlb    a4, a3        // write wired PTE (CA [+PPN]) of 512MB segment to DTLB
    sub    a3, a3, a5    // next segment (add 0x20000000)
    srli    a7, a7, 4    // next CA
    bgeui    a3, 16, 2b
    dsync            // make sure all data path changes take effect
    .endm    // dcacheattr_set
 
#endif /* XCHAL_CA_8X512 && !XCHAL_HAVE_CACHEATTR */
 
 
 
/*
 *  cacheattr_set
 *
 *  Macro that sets the current CACHEATTR safely
 *  (both i and d) according to the current contents of a2.
 *  It works even when changing the cache attributes of
 *  the currently running code.
 *
 *  Entry:
 *    a2        cacheattr value to set
 *  Exit:
 *    a2        unchanged
 *    a3-a8        clobbered (temporaries)
 */
    .macro    cacheattr_set
 
#if XCHAL_HAVE_CACHEATTR
# if XCHAL_ICACHE_LINESIZE < 4
    //  No i-cache, so can always safely write to CACHEATTR:
    wsr    a2, CACHEATTR
# else
    //  The Athens micro-architecture, when using the old
    //  exception architecture option (ie. with the CACHEATTR register)
    //  allows changing the cache attributes of the running code
    //  using the following exact sequence aligned to be within
    //  an instruction cache line.  (NOTE: using XCHAL_ICACHE_LINESIZE
    //  alignment actually causes problems because that can be greater
    //  than the alignment of the reset vector, where this macro is often
    //  invoked, which would cause the linker to align the reset
    //  vector code away from the reset vector!!).
    j    1f
    .begin    no-transform
    .align    16 /*XCHAL_ICACHE_LINESIZE*/    // align to within an I-cache line
1:    wsr    a2, CACHEATTR
    isync
    .end    no-transform
    nop
    nop
# endif
#elif XCHAL_CA_8X512
    //  DTLB and ITLB are independent, but to keep semantics
    //  of this macro we simply write to both.
    icacheattr_set
    dcacheattr_set
#else
    //  This macro isn't applicable to arbitrary MMU configurations.
    //  Do nothing in this case.
#endif
    .endm
 
 
#endif /*XTENSA_CACHEATTRASM_H*/