cacheattrasm.h 13 KB

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