cacheattrasm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #ifndef XTENSA_CACHEATTRASM_H
  2. #define XTENSA_CACHEATTRASM_H
  3. /*
  4. * THIS FILE IS GENERATED -- DO NOT MODIFY BY HAND
  5. *
  6. * include/asm-xtensa/xtensa/cacheattrasm.h -- assembler-specific
  7. * CACHEATTR register related definitions that depend on CORE
  8. * configuration.
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. *
  14. * Copyright (C) 2002 Tensilica Inc.
  15. */
  16. #include <xtensa/coreasm.h>
  17. /*
  18. * This header file defines assembler macros of the form:
  19. * <x>cacheattr_<func>
  20. * where:
  21. * <x> is 'i', 'd' or absent for instruction, data
  22. * or both caches; and
  23. * <func> indicates the function of the macro.
  24. *
  25. * The following functions are defined:
  26. *
  27. * icacheattr_get
  28. * Reads I-cache CACHEATTR into a2 (clobbers a3-a5).
  29. *
  30. * dcacheattr_get
  31. * Reads D-cache CACHEATTR into a2 (clobbers a3-a5).
  32. * (Note: for configs with a real CACHEATTR register, the
  33. * above two macros are identical.)
  34. *
  35. * cacheattr_set
  36. * Writes both I-cache and D-cache CACHEATTRs from a2 (a3-a8 clobbered).
  37. * Works even when changing one's own code's attributes.
  38. *
  39. * icacheattr_is_enabled label
  40. * Branches to \label if I-cache appears to have been enabled
  41. * (eg. if CACHEATTR contains a cache-enabled attribute).
  42. * (clobbers a2-a5,SAR)
  43. *
  44. * dcacheattr_is_enabled label
  45. * Branches to \label if D-cache appears to have been enabled
  46. * (eg. if CACHEATTR contains a cache-enabled attribute).
  47. * (clobbers a2-a5,SAR)
  48. *
  49. * cacheattr_is_enabled label
  50. * Branches to \label if either I-cache or D-cache appears to have been enabled
  51. * (eg. if CACHEATTR contains a cache-enabled attribute).
  52. * (clobbers a2-a5,SAR)
  53. *
  54. * The following macros are only defined under certain conditions:
  55. *
  56. * icacheattr_set (if XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR)
  57. * Writes I-cache CACHEATTR from a2 (a3-a8 clobbered).
  58. *
  59. * dcacheattr_set (if XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR)
  60. * Writes D-cache CACHEATTR from a2 (a3-a8 clobbered).
  61. */
  62. /*************************** GENERIC -- ALL CACHES ***************************/
  63. /*
  64. * _cacheattr_get
  65. *
  66. * (Internal macro.)
  67. * Returns value of CACHEATTR register (or closest equivalent) in a2.
  68. *
  69. * Entry:
  70. * (none)
  71. * Exit:
  72. * a2 value read from CACHEATTR
  73. * a3-a5 clobbered (temporaries)
  74. */
  75. .macro _cacheattr_get tlb
  76. #if XCHAL_HAVE_CACHEATTR
  77. rsr a2, CACHEATTR
  78. #elif XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR
  79. // We have a config that "mimics" CACHEATTR using a simplified
  80. // "MMU" composed of a single statically-mapped way.
  81. // DTLB and ITLB are independent, so there's no single
  82. // cache attribute that can describe both. So for now
  83. // just return the DTLB state.
  84. movi a5, 0xE0000000
  85. movi a2, 0
  86. movi a3, 0
  87. 1: add a3, a3, a5 // next segment
  88. r&tlb&1 a4, a3 // get PPN+CA of segment at 0xE0000000, 0xC0000000, ..., 0
  89. dsync // interlock???
  90. slli a2, a2, 4
  91. extui a4, a4, 0, 4 // extract CA
  92. or a2, a2, a4
  93. bnez a3, 1b
  94. #else
  95. // This macro isn't applicable to arbitrary MMU configurations.
  96. // Just return zero.
  97. movi a2, 0
  98. #endif
  99. .endm
  100. .macro icacheattr_get
  101. _cacheattr_get itlb
  102. .endm
  103. .macro dcacheattr_get
  104. _cacheattr_get dtlb
  105. .endm
  106. #define XCHAL_CACHEATTR_ALL_BYPASS 0x22222222 /* default (powerup/reset) value of CACHEATTR, all BYPASS
  107. mode (ie. disabled/bypassed caches) */
  108. #if XCHAL_HAVE_CACHEATTR || XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR
  109. #define XCHAL_FCA_ENAMASK 0x001A /* bitmap of fetch attributes that require enabled icache */
  110. #define XCHAL_LCA_ENAMASK 0x0003 /* bitmap of load attributes that require enabled dcache */
  111. #define XCHAL_SCA_ENAMASK 0x0003 /* bitmap of store attributes that require enabled dcache */
  112. #define XCHAL_LSCA_ENAMASK (XCHAL_LCA_ENAMASK|XCHAL_SCA_ENAMASK) /* l/s attrs requiring enabled dcache */
  113. #define XCHAL_ALLCA_ENAMASK (XCHAL_FCA_ENAMASK|XCHAL_LSCA_ENAMASK) /* all attrs requiring enabled caches */
  114. /*
  115. * _cacheattr_is_enabled
  116. *
  117. * (Internal macro.)
  118. * Branches to \label if CACHEATTR in a2 indicates an enabled
  119. * cache, using mask in a3.
  120. *
  121. * Parameters:
  122. * label where to branch to if cache is enabled
  123. * Entry:
  124. * a2 contains CACHEATTR value used to determine whether
  125. * caches are enabled
  126. * a3 16-bit constant where each bit correspond to
  127. * one of the 16 possible CA values (in a CACHEATTR mask);
  128. * CA values that indicate the cache is enabled
  129. * have their corresponding bit set in this mask
  130. * (eg. use XCHAL_xCA_ENAMASK , above)
  131. * Exit:
  132. * a2,a4,a5 clobbered
  133. * SAR clobbered
  134. */
  135. .macro _cacheattr_is_enabled label
  136. movi a4, 8 // loop 8 times
  137. .Lcaife\@:
  138. extui a5, a2, 0, 4 // get CA nibble
  139. ssr a5 // index into mask according to CA...
  140. srl a5, a3 // ...and get CA's mask bit in a5 bit 0
  141. bbsi.l a5, 0, \label // if CA indicates cache enabled, jump to label
  142. srli a2, a2, 4 // next nibble
  143. addi a4, a4, -1
  144. bnez a4, .Lcaife\@ // loop for each nibble
  145. .endm
  146. #else /* XCHAL_HAVE_CACHEATTR || XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR */
  147. .macro _cacheattr_is_enabled label
  148. j \label // macro not applicable, assume caches always enabled
  149. .endm
  150. #endif /* XCHAL_HAVE_CACHEATTR || XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR */
  151. /*
  152. * icacheattr_is_enabled
  153. *
  154. * Branches to \label if I-cache is enabled.
  155. *
  156. * Parameters:
  157. * label where to branch to if icache is enabled
  158. * Entry:
  159. * (none)
  160. * Exit:
  161. * a2-a5, SAR clobbered (temporaries)
  162. */
  163. .macro icacheattr_is_enabled label
  164. #if XCHAL_HAVE_CACHEATTR || XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR
  165. icacheattr_get
  166. movi a3, XCHAL_FCA_ENAMASK
  167. #endif
  168. _cacheattr_is_enabled \label
  169. .endm
  170. /*
  171. * dcacheattr_is_enabled
  172. *
  173. * Branches to \label if D-cache is enabled.
  174. *
  175. * Parameters:
  176. * label where to branch to if dcache is enabled
  177. * Entry:
  178. * (none)
  179. * Exit:
  180. * a2-a5, SAR clobbered (temporaries)
  181. */
  182. .macro dcacheattr_is_enabled label
  183. #if XCHAL_HAVE_CACHEATTR || XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR
  184. dcacheattr_get
  185. movi a3, XCHAL_LSCA_ENAMASK
  186. #endif
  187. _cacheattr_is_enabled \label
  188. .endm
  189. /*
  190. * cacheattr_is_enabled
  191. *
  192. * Branches to \label if either I-cache or D-cache is enabled.
  193. *
  194. * Parameters:
  195. * label where to branch to if a cache is enabled
  196. * Entry:
  197. * (none)
  198. * Exit:
  199. * a2-a5, SAR clobbered (temporaries)
  200. */
  201. .macro cacheattr_is_enabled label
  202. #if XCHAL_HAVE_CACHEATTR
  203. rsr a2, CACHEATTR
  204. movi a3, XCHAL_ALLCA_ENAMASK
  205. #elif XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR
  206. icacheattr_get
  207. movi a3, XCHAL_FCA_ENAMASK
  208. _cacheattr_is_enabled \label
  209. dcacheattr_get
  210. movi a3, XCHAL_LSCA_ENAMASK
  211. #endif
  212. _cacheattr_is_enabled \label
  213. .endm
  214. /*
  215. * The ISA does not have a defined way to change the
  216. * instruction cache attributes of the running code,
  217. * ie. of the memory area that encloses the current PC.
  218. * However, each micro-architecture (or class of
  219. * configurations within a micro-architecture)
  220. * provides a way to deal with this issue.
  221. *
  222. * Here are a few macros used to implement the relevant
  223. * approach taken.
  224. */
  225. #if XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR
  226. // We have a config that "mimics" CACHEATTR using a simplified
  227. // "MMU" composed of a single statically-mapped way.
  228. /*
  229. * icacheattr_set
  230. *
  231. * Entry:
  232. * a2 cacheattr value to set
  233. * Exit:
  234. * a2 unchanged
  235. * a3-a8 clobbered (temporaries)
  236. */
  237. .macro icacheattr_set
  238. movi a5, 0xE0000000 // mask of upper 3 bits
  239. movi a6, 3f // PC where ITLB is set
  240. movi a3, 0 // start at region 0 (0 .. 7)
  241. and a6, a6, a5 // upper 3 bits of local PC area
  242. mov a7, a2 // copy a2 so it doesn't get clobbered
  243. j 3f
  244. # if XCHAL_HAVE_XLT_CACHEATTR
  245. // Can do translations, use generic method:
  246. 1: sub a6, a3, a5 // address of some other segment
  247. ritlb1 a8, a6 // save its PPN+CA
  248. dsync // interlock??
  249. witlb a4, a6 // make it translate to this code area
  250. movi a6, 5f // where to jump into it
  251. isync
  252. sub a6, a6, a5 // adjust jump address within that other segment
  253. jx a6
  254. // Note that in the following code snippet, which runs at a different virtual
  255. // address than it is assembled for, we avoid using literals (eg. via movi/l32r)
  256. // just in case literals end up in a different 512 MB segment, and we avoid
  257. // instructions that rely on the current PC being what is expected.
  258. //
  259. .align 4
  260. _j 6f // this is at label '5' minus 4 bytes
  261. .align 4
  262. 5: witlb a4, a3 // we're in other segment, now can write previous segment's CA
  263. isync
  264. add a6, a6, a5 // back to previous segment
  265. addi a6, a6, -4 // next jump label
  266. jx a6
  267. 6: sub a6, a3, a5 // address of some other segment
  268. witlb a8, a6 // restore PPN+CA of other segment
  269. mov a6, a3 // restore a6
  270. isync
  271. # else /* XCHAL_HAVE_XLT_CACHEATTR */
  272. // Use micro-architecture specific method.
  273. // The following 4-instruction sequence is aligned such that
  274. // it all fits within a single I-cache line. Sixteen byte
  275. // alignment is sufficient for this (using XCHAL_ICACHE_LINESIZE
  276. // actually causes problems because that can be greater than
  277. // the alignment of the reset vector, where this macro is often
  278. // invoked, which would cause the linker to align the reset
  279. // vector code away from the reset vector!!).
  280. .align 16 /*XCHAL_ICACHE_LINESIZE*/
  281. 1: _witlb a4, a3 // write wired PTE (CA, no PPN) of 512MB segment to ITLB
  282. _isync
  283. nop
  284. nop
  285. # endif /* XCHAL_HAVE_XLT_CACHEATTR */
  286. beq a3, a5, 4f // done?
  287. // Note that in the WITLB loop, we don't do any load/stores
  288. // (may not be an issue here, but it is important in the DTLB case).
  289. 2: srli a7, a7, 4 // next CA
  290. sub a3, a3, a5 // next segment (add 0x20000000)
  291. 3:
  292. # if XCHAL_HAVE_XLT_CACHEATTR /* if have translation, preserve it */
  293. ritlb1 a8, a3 // get current PPN+CA of segment
  294. dsync // interlock???
  295. extui a4, a7, 0, 4 // extract CA to set
  296. srli a8, a8, 4 // clear CA but keep PPN ...
  297. slli a8, a8, 4 // ...
  298. add a4, a4, a8 // combine new CA with PPN to preserve
  299. # else
  300. extui a4, a7, 0, 4 // extract CA
  301. # endif
  302. beq a3, a6, 1b // current PC's region? if so, do it in a safe way
  303. witlb a4, a3 // write wired PTE (CA [+PPN]) of 512MB segment to ITLB
  304. bne a3, a5, 2b
  305. isync // make sure all ifetch changes take effect
  306. 4:
  307. .endm // icacheattr_set
  308. /*
  309. * dcacheattr_set
  310. *
  311. * Entry:
  312. * a2 cacheattr value to set
  313. * Exit:
  314. * a2 unchanged
  315. * a3-a8 clobbered (temporaries)
  316. */
  317. .macro dcacheattr_set
  318. movi a5, 0xE0000000 // mask of upper 3 bits
  319. movi a3, 0 // start at region 0 (0 .. 7)
  320. mov a7, a2 // copy a2 so it doesn't get clobbered
  321. j 3f
  322. // Note that in the WDTLB loop, we don't do any load/stores
  323. // (including implicit l32r via movi) because it isn't safe.
  324. 2: srli a7, a7, 4 // next CA
  325. sub a3, a3, a5 // next segment (add 0x20000000)
  326. 3:
  327. # if XCHAL_HAVE_XLT_CACHEATTR /* if have translation, preserve it */
  328. rdtlb1 a8, a3 // get current PPN+CA of segment
  329. dsync // interlock???
  330. extui a4, a7, 0, 4 // extract CA to set
  331. srli a8, a8, 4 // clear CA but keep PPN ...
  332. slli a8, a8, 4 // ...
  333. add a4, a4, a8 // combine new CA with PPN to preserve
  334. # else
  335. extui a4, a7, 0, 4 // extract CA to set
  336. # endif
  337. wdtlb a4, a3 // write wired PTE (CA [+PPN]) of 512MB segment to DTLB
  338. bne a3, a5, 2b
  339. dsync // make sure all data path changes take effect
  340. .endm // dcacheattr_set
  341. #endif /* XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR */
  342. /*
  343. * cacheattr_set
  344. *
  345. * Macro that sets the current CACHEATTR safely
  346. * (both i and d) according to the current contents of a2.
  347. * It works even when changing the cache attributes of
  348. * the currently running code.
  349. *
  350. * Entry:
  351. * a2 cacheattr value to set
  352. * Exit:
  353. * a2 unchanged
  354. * a3-a8 clobbered (temporaries)
  355. */
  356. .macro cacheattr_set
  357. #if XCHAL_HAVE_CACHEATTR
  358. # if XCHAL_ICACHE_LINESIZE < 4
  359. // No i-cache, so can always safely write to CACHEATTR:
  360. wsr a2, CACHEATTR
  361. # else
  362. // The Athens micro-architecture, when using the old
  363. // exception architecture option (ie. with the CACHEATTR register)
  364. // allows changing the cache attributes of the running code
  365. // using the following exact sequence aligned to be within
  366. // an instruction cache line. (NOTE: using XCHAL_ICACHE_LINESIZE
  367. // alignment actually causes problems because that can be greater
  368. // than the alignment of the reset vector, where this macro is often
  369. // invoked, which would cause the linker to align the reset
  370. // vector code away from the reset vector!!).
  371. j 1f
  372. .align 16 /*XCHAL_ICACHE_LINESIZE*/ // align to within an I-cache line
  373. 1: _wsr a2, CACHEATTR
  374. _isync
  375. nop
  376. nop
  377. # endif
  378. #elif XCHAL_HAVE_MIMIC_CACHEATTR || XCHAL_HAVE_XLT_CACHEATTR
  379. // DTLB and ITLB are independent, but to keep semantics
  380. // of this macro we simply write to both.
  381. icacheattr_set
  382. dcacheattr_set
  383. #else
  384. // This macro isn't applicable to arbitrary MMU configurations.
  385. // Do nothing in this case.
  386. #endif
  387. .endm
  388. #endif /*XTENSA_CACHEATTRASM_H*/