cp15_asm_iar.s 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /* ----------------------------------------------------------------------------
  2. * SAM Software Package License
  3. * ----------------------------------------------------------------------------
  4. * Copyright (c) 2012, Atmel Corporation
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the disclaimer below.
  13. *
  14. * Atmel's name may not be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  20. * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES// LOSS OF USE, DATA,
  23. * OR PROFITS// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  26. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. * ----------------------------------------------------------------------------
  28. */
  29. /** \file */
  30. /**
  31. * \addtogroup cp15_cache Cache Operations
  32. *
  33. * \section Usage
  34. *
  35. * They are performed as MCR instructions and only operate on a level 1 cache associated with
  36. * ATM v7 processor.
  37. * The supported operations are:
  38. * <ul>
  39. * <li> Any of these operations can be applied to
  40. * -# any data cache
  41. * -# any unified cache.
  42. * <li> Invalidate by MVA
  43. * Performs an invalidate of a data or unified cache line based on the address it contains.
  44. * <li> Invalidate by set/way
  45. * Performs an invalidate of a data or unified cache line based on its location in the cache hierarchy.
  46. * <li> Clean by MVA
  47. * Performs a clean of a data or unified cache line based on the address it contains.
  48. * <li> Clean by set/way
  49. * Performs a clean of a data or unified cache line based on its location in the cache hierarchy.
  50. * <li> Clean and Invalidate by MVA
  51. * Performs a clean and invalidate of a data or unified cache line based on the address it contains.
  52. * <li> Clean and Invalidate by set/way
  53. * Performs a clean and invalidate of a data or unified cache line based on its location in the cache hierarchy.
  54. * </ul>
  55. *
  56. * Related files:\n
  57. * \ref cp15.h\n
  58. * \ref cp15_arm_iar.s \n
  59. */
  60. MODULE ?cp15
  61. //// Forward declaration of sections.
  62. SECTION IRQ_STACK:DATA:NOROOT(2)
  63. SECTION CSTACK:DATA:NOROOT(3)
  64. /*----------------------------------------------------------------------------
  65. * Headers
  66. *----------------------------------------------------------------------------*/
  67. #define __ASSEMBLY__
  68. /*----------------------------------------------------------------------------
  69. * Functions to access CP15 coprocessor register
  70. *----------------------------------------------------------------------------*/
  71. PUBLIC CP15_ReadID
  72. PUBLIC CP15_ExclusiveCache
  73. PUBLIC CP15_NonExclusiveCache
  74. PUBLIC CP15_ISB
  75. PUBLIC CP15_DSB
  76. PUBLIC CP15_DMB
  77. PUBLIC CP15_SelectICache
  78. PUBLIC CP15_SelectDCache
  79. PUBLIC CP15_ReadControl
  80. PUBLIC CP15_WriteControl
  81. PUBLIC CP15_WriteDomainAccessControl
  82. PUBLIC CP15_WriteTTB
  83. PUBLIC CP15_InvalidateIcacheInnerSharable
  84. PUBLIC CP15_InvalidateBTBinnerSharable
  85. PUBLIC CP15_InvalidateIcache
  86. PUBLIC CP15_InvalidateIcacheByMva
  87. PUBLIC CP15_InvalidateBTB
  88. PUBLIC CP15_InvalidateBTBbyMva
  89. PUBLIC CP15_InvalidateDcacheBySetWay
  90. PUBLIC CP15_CleanDCacheBySetWay
  91. PUBLIC CP15_CleanInvalidateDCacheBySetWay
  92. PUBLIC CP15_InvalidateDcacheByMva
  93. PUBLIC CP15_CleanDCacheByMva
  94. PUBLIC CP15_CleanDCacheUMva
  95. PUBLIC CP15_CleanInvalidateDcacheByMva
  96. PUBLIC CP15_InvalidateTranslationTable
  97. PUBLIC CP15_coherent_dcache_for_dma
  98. PUBLIC CP15_invalidate_dcache_for_dma
  99. PUBLIC CP15_clean_dcache_for_dma
  100. PUBLIC CP15_flush_dcache_for_dma
  101. PUBLIC CP15_flush_kern_dcache_for_dma
  102. PUBLIC CP15_ReadACTLRControl
  103. PUBLIC CP15_WriteACTLRControl
  104. /**
  105. * \brief Register c0 accesses the ID Register, Cache Type Register, and TCM Status Registers.
  106. * Reading from this register returns the device ID, the cache type, or the TCM status
  107. * depending on the value of Opcode_2 used.
  108. */
  109. SECTION .CP15_ReadID:DATA:NOROOT(2)
  110. PUBLIC CP15_ReadID
  111. CP15_ReadID:
  112. mov r0, #0
  113. mrc p15, 0, r0, c0, c0, 0
  114. bx lr
  115. /**
  116. * \brief Register c7 accesses the ACTLR Register, to indicate cpu that L2 is in exclusive mode
  117. */
  118. SECTION .CP15_ISB:DATA:NOROOT(2)
  119. PUBLIC CP15_ISB
  120. CP15_ISB:
  121. mov r0, #0
  122. mcr p15, 0, r0, c7, c5, 4
  123. nop
  124. bx lr
  125. /**
  126. * \brief Register c7 accesses the ACTLR Register, to indicate cpu that L2 is in exclusive mode
  127. */
  128. SECTION .CP15_DSB:DATA:NOROOT(2)
  129. PUBLIC CP15_DSB
  130. CP15_DSB:
  131. mov r0, #0
  132. mcr p15, 0, r0, c7, c10, 4
  133. nop
  134. bx lr
  135. /**
  136. * \brief Register c7 accesses the ACTLR Register, to indicate cpu that L2 is in exclusive mode
  137. */
  138. SECTION .CP15_DMB:DATA:NOROOT(2)
  139. PUBLIC CP15_DMB
  140. CP15_DMB:
  141. mov r0, #0
  142. mcr p15, 0, r0, c7, c10, 5
  143. nop
  144. bx lr
  145. /**
  146. * \brief Register c1 accesses the ACTLR Register, to indicate cpu that L2 is in exclusive mode
  147. */
  148. SECTION .CP15_ExclusiveCache:DATA:NOROOT(2)
  149. PUBLIC CP15_ExclusiveCache
  150. CP15_ExclusiveCache:
  151. mov r0, #0
  152. mrc p15, 0, r0, c1, c0, 1 ; Read ACTLR
  153. orr r0, r0, #0x00000080
  154. mcr p15, 0, r0, c1, c0, 1 ; Write ACTLR
  155. nop
  156. bx lr
  157. /**
  158. * \brief Register c1 accesses the ACTLR Register, to indicate cpu that L2 is in exclusive mode
  159. */
  160. SECTION .CP15_NonExclusiveCache:DATA:NOROOT(2)
  161. PUBLIC CP15_NonExclusiveCache
  162. CP15_NonExclusiveCache:
  163. mov r0, #0
  164. mrc p15, 0, r0, c1, c0, 1 ; Read ACTLR
  165. bic r0, r0, #0x00000080
  166. mcr p15, 0, r0, c1, c0, 1 ; Write ACTLR
  167. nop
  168. bx lr
  169. /**
  170. * \brief Register c1 accesses the CSSELR Register, to select ICache
  171. */
  172. SECTION .CP15_SelectICache:DATA:NOROOT(2)
  173. PUBLIC CP15_SelectICache
  174. CP15_SelectICache:
  175. mrc p15, 2, r0, c0, c0, 0 ; Read CSSELR
  176. orr r0, r0, #0x1 ; Change 0th bit to ICache
  177. mcr p15, 2, r0, c0, c0, 0 ; Write CSSELR
  178. nop
  179. bx lr
  180. /**
  181. * \brief Register c1 accesses the CSSELR Register, to select DCache
  182. */
  183. SECTION .CP15_SelectDCache:DATA:NOROOT(2)
  184. PUBLIC CP15_SelectDCache
  185. CP15_SelectDCache:
  186. mrc p15, 2, r0, c0, c0, 0 ; Read CSSELR
  187. and r0, r0, #0xFFFFFFFE ; Change 0th bit to ICache
  188. mcr p15, 2, r0, c0, c0, 0 ; Write CSSELR
  189. nop
  190. bx lr
  191. /**
  192. * \brief Register c1 accesses the CSSELR Register, to enable mmu
  193. */
  194. SECTION .CP15_ReadACTLRControl:CODE:NOROOT(2)
  195. PUBLIC CP15_ReadACTLRControl
  196. CP15_ReadACTLRControl:
  197. mov r0, #0
  198. mrc p15, 0, r0, c1, c0, 1
  199. bx lr
  200. SECTION .CP15_WriteACTLRControl:CODE:NOROOT(2)
  201. PUBLIC CP15_WriteACTLRControl
  202. CP15_WriteACTLRControl:
  203. mcr p15, 0, r0, c1, c0, 1
  204. nop
  205. nop
  206. nop
  207. nop
  208. nop
  209. nop
  210. nop
  211. nop
  212. bx lr
  213. /**
  214. * \brief Register c1 is the Control Register for the ARM926EJ-S processor.
  215. * This register specifies the configuration used to enable and disable the
  216. * caches and MMU. It is recommended that you access this register using a
  217. * read-modify-write sequence
  218. */
  219. SECTION .CP15_ReadControl:CODE:NOROOT(2)
  220. PUBLIC CP15_ReadControl
  221. CP15_ReadControl:
  222. mov r0, #0
  223. mrc p15, 0, r0, c1, c0, 0
  224. bx lr
  225. SECTION .CP15_WriteControl:CODE:NOROOT(2)
  226. PUBLIC CP15_WriteControl
  227. CP15_WriteControl:
  228. mcr p15, 0, r0, c1, c0, 0
  229. dsb
  230. isb
  231. bx lr
  232. SECTION .CP15_WriteDomainAccessControl:CODE:NOROOT(2)
  233. PUBLIC CP15_WriteDomainAccessControl
  234. CP15_WriteDomainAccessControl:
  235. mcr p15, 0, r0, c3, c0, 0
  236. dsb
  237. isb
  238. bx lr
  239. /**
  240. * \brief ARMv7A architecture supports two translation tables
  241. * Configure translation table base (TTB) control register cp15,c2
  242. * to a value of all zeros, indicates we are using TTB register 0.
  243. * write the address of our page table base to TTB register 0.
  244. */
  245. SECTION .CP15_WriteTTB:CODE:NOROOT(2)
  246. PUBLIC CP15_WriteTTB
  247. CP15_WriteTTB:
  248. mcr p15, 0, r0, c2, c0, 0
  249. dsb
  250. isb
  251. bx lr
  252. /**
  253. * \brief Invalidate I cache predictor array inner Sharable
  254. */
  255. SECTION .CP15_InvalidateIcacheInnerSharable:CODE:NOROOT(2)
  256. PUBLIC CP15_InvalidateIcacheInnerSharable
  257. CP15_InvalidateIcacheInnerSharable:
  258. mov r0, #0
  259. mcr p15, 0, r0, c7, c1, 0
  260. bx lr
  261. /**
  262. * \brief Invalidate entire branch predictor array inner Sharable
  263. */
  264. SECTION .CP15_InvalidateBTBinnerSharable:CODE:NOROOT(2)
  265. PUBLIC CP15_InvalidateBTBinnerSharable
  266. CP15_InvalidateBTBinnerSharable:
  267. mov r0, #0
  268. mcr p15, 0, r0, c7, c1, 6
  269. bx lr
  270. /**
  271. * \brief Invalidate all instruction caches to PoU, also flushes branch target cache
  272. */
  273. SECTION .CP15_InvalidateIcache:CODE:NOROOT(2)
  274. PUBLIC CP15_InvalidateIcache
  275. CP15_InvalidateIcache:
  276. mov r0, #0
  277. mcr p15, 0, r0, c7, c5, 0
  278. isb
  279. bx lr
  280. /**
  281. * \brief Invalidate instruction caches by VA to PoU
  282. */
  283. SECTION .CP15_InvalidateIcacheByMva:CODE:NOROOT(2)
  284. PUBLIC CP15_InvalidateIcacheByMva
  285. CP15_InvalidateIcacheByMva:
  286. mov r0, #0
  287. mcr p15, 0, r0, c7, c5, 1
  288. bx lr
  289. /**
  290. * \brief Invalidate entire branch predictor array
  291. */
  292. SECTION .CP15_InvalidateBTB:CODE:NOROOT(2)
  293. PUBLIC CP15_InvalidateBTB
  294. CP15_InvalidateBTB:
  295. mov r0, #0
  296. mcr p15, 0, r0, c7, c5, 6
  297. dsb
  298. isb
  299. bx lr
  300. /**
  301. * \brief Invalidate branch predictor array entry by MVA
  302. */
  303. SECTION .CP15_InvalidateBTBbyMva:CODE:NOROOT(2)
  304. PUBLIC CP15_InvalidateBTBbyMva
  305. CP15_InvalidateBTBbyMva:
  306. mcr p15, 0, r0, c7, c5, 7
  307. bx lr
  308. /***********************************************************
  309. *
  310. * ===Data Cache related maintenance functions===
  311. *
  312. **************************************************************/
  313. // ===Data Cache maintenance by SetWay ===
  314. /**
  315. * \brief Invalidate entire data cache by set/way
  316. */
  317. SECTION .CP15_InvalidateDcacheBySetWay:CODE:NOROOT(2)
  318. PUBLIC CP15_InvalidateDcacheBySetWay
  319. CP15_InvalidateDcacheBySetWay:
  320. MRC p15, 1, r0, c0, c0, 1 ; Read CLIDR
  321. ANDS r3, r0, #0x07000000 ; Extract coherency level
  322. MOV r3, r3, LSR #23 ; Total cache levels << 1
  323. BEQ inv_finish ; If 0, no need to clean
  324. MOV r10, #0 ; R10 holds current cache level << 1
  325. inv_cache_level_loop
  326. ADD r2, r10, r10, LSR #1 ; R2 holds cache "Set" position
  327. MOV r1, r0, LSR r2 ; Bottom 3 bits are the Cache-type for this level
  328. AND r1, r1, #7 ; Isolate those lower 3 bits
  329. CMP r1, #2
  330. BLT inv_skip ; No cache or only instruction cache at this level
  331. MCR p15, 2, r10, c0, c0, 0 ; Write the Cache Size selection register
  332. ISB ; ISB to sync the change to the CacheSizeID reg
  333. MRC p15, 1, r1, c0, c0, 0 ; Reads current Cache Size ID register
  334. AND r2, r1, #7 ; Extract the line length field
  335. ADD r2, r2, #4 ; Add 4 for the line length offset (log2 16 bytes)
  336. LDR r4, =0x3FF
  337. ANDS r4, r4, r1, LSR #3 ; R4 is the max number on the way size (right aligned)
  338. CLZ r5, r4 ; R5 is the bit position of the way size increment
  339. LDR r7, =0x7FFF
  340. ANDS r7, r7, r1, LSR #13 ; R7 is the max number of the index size (right aligned)
  341. inv_set_loop
  342. MOV r9, r4 ; R9 working copy of the max way size (right aligned)
  343. inv_way_loop
  344. ORR r11, r10, r9, LSL r5 ; Factor in the Way number and cache number into R11
  345. ORR r11, r11, r7, LSL r2 ; Factor in the Set number
  346. MCR p15, 0, r11, c7, c6, 2 ; Invalidate by Set/Way
  347. SUBS r9, r9, #1 ; Decrement the Way number
  348. BGE inv_way_loop
  349. SUBS r7, r7, #1 ; Decrement the Set number
  350. BGE inv_set_loop
  351. inv_skip
  352. ADD r10, r10, #2 ; increment the cache number
  353. CMP r3, r10
  354. BGT inv_cache_level_loop
  355. inv_finish
  356. NOP
  357. BX lr
  358. /**
  359. * \brief Clean entire data cache by set/way
  360. */
  361. SECTION .CP15_CleanDCacheBySetWay:CODE:NOROOT(2)
  362. PUBLIC CP15_CleanDCacheBySetWay
  363. CP15_CleanDCacheBySetWay:
  364. MRC p15, 1, r0, c0, c0, 1 ; Read CLIDR
  365. ANDS r3, r0, #0x07000000 ; Extract coherency level
  366. MOV r3, r3, LSR #23 ; Total cache levels << 1
  367. BEQ clean_finish ; If 0, no need to clean
  368. MOV r10, #0 ; R10 holds current cache level << 1
  369. clean_cache_level_loop
  370. ADD r2, r10, r10, LSR #1 ; R2 holds cache "Set" position
  371. MOV r1, r0, LSR r2 ; Bottom 3 bits are the Cache-type for this level
  372. AND r1, r1, #7 ; Isolate those lower 3 bits
  373. CMP r1, #2
  374. BLT clean_skip ; No cache or only instruction cache at this level
  375. MCR p15, 2, r10, c0, c0, 0 ; Write the Cache Size selection register
  376. ISB ; ISB to sync the change to the CacheSizeID reg
  377. MRC p15, 1, r1, c0, c0, 0 ; Reads current Cache Size ID register
  378. AND r2, r1, #7 ; Extract the line length field
  379. ADD r2, r2, #4 ; Add 4 for the line length offset (log2 16 bytes)
  380. LDR r4, =0x3FF
  381. ANDS r4, r4, r1, LSR #3 ; R4 is the max number on the way size (right aligned)
  382. CLZ r5, r4 ; R5 is the bit position of the way size increment
  383. LDR r7, =0x7FFF
  384. ANDS r7, r7, r1, LSR #13 ; R7 is the max number of the index size (right aligned)
  385. clean_set_loop
  386. MOV r9, r4 ; R9 working copy of the max way size (right aligned)
  387. clean_way_loop
  388. ORR r11, r10, r9, LSL r5 ; Factor in the Way number and cache number into R11
  389. ORR r11, r11, r7, LSL r2 ; Factor in the Set number
  390. MCR p15, 0, r11, c7, c10, 2 ; Clean by Set/Way
  391. SUBS r9, r9, #1 ; Decrement the Way number
  392. BGE clean_way_loop
  393. SUBS r7, r7, #1 ; Decrement the Set number
  394. BGE clean_set_loop
  395. clean_skip
  396. ADD r10, r10, #2 ; increment the cache number
  397. CMP r3, r10
  398. BGT clean_cache_level_loop
  399. clean_finish
  400. NOP
  401. BX lr
  402. /**
  403. * \brief Clean and Invalidate entire data cache by set/way
  404. */
  405. SECTION .CP15_CleanInvalidateDCacheBySetWay:CODE:NOROOT(2)
  406. PUBLIC CP15_CleanInvalidateDCacheBySetWay
  407. CP15_CleanInvalidateDCacheBySetWay:
  408. MRC p15, 1, r0, c0, c0, 1 ; Read CLIDR
  409. ANDS r3, r0, #0x07000000 ; Extract coherency level
  410. MOV r3, r3, LSR #23 ; Total cache levels << 1
  411. BEQ clinv_finish ; If 0, no need to clean
  412. MOV r10, #0 ; R10 holds current cache level << 1
  413. clinv_cache_level_loop
  414. ADD r2, r10, r10, LSR #1 ; R2 holds cache "Set" position
  415. MOV r1, r0, LSR r2 ; Bottom 3 bits are the Cache-type for this level
  416. AND r1, r1, #7 ; Isolate those lower 3 bits
  417. CMP r1, #2
  418. BLT clean_skip ; No cache or only instruction cache at this level
  419. MCR p15, 2, r10, c0, c0, 0 ; Write the Cache Size selection register
  420. ISB ; ISB to sync the change to the CacheSizeID reg
  421. MRC p15, 1, r1, c0, c0, 0 ; Reads current Cache Size ID register
  422. AND r2, r1, #7 ; Extract the line length field
  423. ADD r2, r2, #4 ; Add 4 for the line length offset (log2 16 bytes)
  424. LDR r4, =0x3FF
  425. ANDS r4, r4, r1, LSR #3 ; R4 is the max number on the way size (right aligned)
  426. CLZ r5, r4 ; R5 is the bit position of the way size increment
  427. LDR r7, =0x7FFF
  428. ANDS r7, r7, r1, LSR #13 ; R7 is the max number of the index size (right aligned)
  429. clinv_set_loop
  430. MOV r9, r4 ; R9 working copy of the max way size (right aligned)
  431. clinv_way_loop
  432. ORR r11, r10, r9, LSL r5 ; Factor in the Way number and cache number into R11
  433. ORR r11, r11, r7, LSL r2 ; Factor in the Set number
  434. MCR p15, 0, r11, c7, c14, 2 ; Clean and Invalidate by Set/Way
  435. SUBS r9, r9, #1 ; Decrement the Way number
  436. BGE clean_way_loop
  437. SUBS r7, r7, #1 ; Decrement the Set number
  438. BGE clean_set_loop
  439. clinv_skip
  440. ADD r10, r10, #2 ; increment the cache number
  441. CMP r3, r10
  442. BGT clean_cache_level_loop
  443. clinv_finish
  444. NOP
  445. BX lr
  446. // ===Data Cache maintenance by VA ===
  447. /**
  448. * \brief Invalidate data cache by VA to Poc
  449. */
  450. SECTION .CP15_InvalidateDcacheByMva:CODE:NOROOT(2)
  451. PUBLIC CP15_InvalidateDcacheByMva
  452. CP15_InvalidateDcacheByMva:
  453. mov r2, #0x20 ;Eight words per line, Cortex-A5 L1 Line Size 32 Bytes
  454. mov r3, r0
  455. inv_loop
  456. mcr p15, 0, r0, c7, c6, 1
  457. add r3, r3, r2
  458. cmp r3, r1
  459. bls inv_loop
  460. bx lr
  461. /**
  462. * \brief Clean data cache by MVA
  463. */
  464. SECTION .CP15_CleanDCacheByMva:CODE:NOROOT(2)
  465. PUBLIC CP15_CleanDCacheByMva
  466. CP15_CleanDCacheByMva:
  467. mov r2, #0x20 ;Eight words per line, Cortex-A5 L1 Line Size 32 Bytes
  468. mov r3, r0
  469. clean_loop
  470. mcr p15, 0, r0, c7, c10, 1
  471. add r3, r3, r2
  472. cmp r3, r1
  473. bls clean_loop
  474. bx lr
  475. /**
  476. * \brief Clean unified cache by MVA
  477. */
  478. SECTION .CP15_CleanDCacheUMva:CODE:NOROOT(2)
  479. PUBLIC CP15_CleanDCacheUMva
  480. CP15_CleanDCacheUMva:
  481. mov r0, #0
  482. mcr p15, 0, r0, c7, c11, 1
  483. bx lr
  484. /**
  485. * \brief Clean and invalidate data cache by VA to PoC
  486. */
  487. SECTION .CP15_CleanInvalidateDcacheByMva:CODE:NOROOT(2)
  488. PUBLIC CP15_CleanInvalidateDcacheByMva
  489. CP15_CleanInvalidateDcacheByMva:
  490. mov r2, #0x20 ;Eight words per line, Cortex-A5 L1 Line Size 32 Bytes
  491. mov r3, r0
  492. clinv_loop
  493. mcr p15, 0, r0, c7, c14, 1
  494. add r3, r3, r2
  495. cmp r3, r1
  496. bls clinv_loop
  497. bx lr
  498. /**
  499. * \brief Invalidate translation table
  500. */
  501. SECTION .CP15_InvalidateTranslationTable:CODE:NOROOT(2)
  502. PUBLIC CP15_InvalidateTranslationTable
  503. CP15_InvalidateTranslationTable:
  504. mov r0, #0
  505. mcr p15, 0, r0, c8, c3, 0
  506. dsb
  507. isb
  508. mcr p15, 0, r0, c8, c7, 0
  509. dsb
  510. isb
  511. bx lr
  512. /**
  513. * \brief flush translation table
  514. */
  515. SECTION .CP15_FlushTranslationTable:CODE:NOROOT(2)
  516. PUBLIC CP15_FlushTranslationTable
  517. CP15_FlushTranslationTable:
  518. mov r0, #0
  519. mcr p15, 0, r0, c8, c3, 0
  520. dsb
  521. isb
  522. bx lr
  523. /**
  524. * \brief Ensure that the I and D caches are coherent within specified
  525. * region. This is typically used when code has been written to
  526. * a memory region, and will be executed.
  527. * \param start virtual start address of region
  528. * \param end virtual end address of region
  529. */
  530. SECTION .CP15_coherent_dcache_for_dma:CODE:NOROOT(2)
  531. PUBLIC CP15_coherent_dcache_for_dma
  532. CP15_coherent_dcache_for_dma:
  533. // dcache_line_size r2, r3
  534. mrc p15, 0, r3, c0, c0, 1 // read ctr
  535. lsr r3, r3, #16
  536. and r3, r3, #0xf // cache line size encoding
  537. mov r2, #4 // bytes per word
  538. mov r2, r2, lsl r3 // actual cache line size
  539. sub r3, r2, #1
  540. bic r12, r0, r3
  541. loop1:
  542. mcr p15, 0, r12, c7, c11, 1 // clean D line to the point of unification
  543. add r12, r12, r2
  544. cmp r12, r1
  545. blo loop1
  546. dsb
  547. // .macro icache_line_size, reg, tmp
  548. mrc p15, 0, r3, c0, c0, 1 // read ctr
  549. and r3, r3, #0xf // cache line size encoding
  550. mov r2, #4 // bytes per word
  551. mov r2, r2, lsl r3 // actual cache line size
  552. sub r3, r2, #1
  553. bic r12, r0, r3
  554. loop2:
  555. mcr p15, 0, r12, c7, c5, 1 // invalidate I line
  556. add r12, r12, r2
  557. cmp r12, r1
  558. blo loop2
  559. mov r0, #0
  560. mcr p15, 0, r0, c7, c1, 6 //invalidate BTB Inner Shareable
  561. mcr p15, 0, r0, c7, c5, 6 // invalidate BTB
  562. dsb
  563. isb
  564. bx lr
  565. /**
  566. * \brief Invalidate the data cache within the specified region; we will
  567. * be performing a DMA operation in this region and we want to
  568. * purge old data in the cache.
  569. * \param start virtual start address of region
  570. * \param end virtual end address of region
  571. */
  572. SECTION .CP15_invalidate_dcache_for_dma:CODE:NOROOT(2)
  573. PUBLIC CP15_invalidate_dcache_for_dma
  574. CP15_invalidate_dcache_for_dma:
  575. // dcache_line_size r2, r3
  576. mrc p15, 0, r3, c0, c0, 1 // read ctr
  577. lsr r3, r3, #16
  578. and r3, r3, #0xf // cache line size encoding
  579. mov r2, #4 // bytes per word
  580. mov r2, r2, lsl r3 // actual cache line size
  581. sub r3, r2, #1
  582. tst r0, r3
  583. bic r0, r0, r3
  584. mcrne p15, 0, r0, c7, c14, 1 // clean & invalidate D / U line
  585. tst r1, r3
  586. bic r1, r1, r3
  587. mcrne p15, 0, r1, c7, c14, 1 // clean & invalidate D / U line
  588. loop3:
  589. mcr p15, 0, r0, c7, c6, 1 // invalidate D / U line
  590. add r0, r0, r2
  591. cmp r0, r1
  592. blo loop3
  593. dsb
  594. bx lr
  595. /**
  596. * \brief Clean the data cache within the specified region
  597. * \param start virtual start address of region
  598. * \param end virtual end address of region
  599. */
  600. SECTION .CP15_clean_dcache_for_dma:CODE:NOROOT(2)
  601. PUBLIC CP15_clean_dcache_for_dma
  602. CP15_clean_dcache_for_dma:
  603. // dcache_line_size r2, r3
  604. mrc p15, 0, r3, c0, c0, 1 // read ctr
  605. lsr r3, r3, #16
  606. and r3, r3, #0xf // cache line size encoding
  607. mov r2, #4 // bytes per word
  608. mov r2, r2, lsl r3 // actual cache line size
  609. sub r3, r2, #1
  610. bic r0, r0, r3
  611. loop4:
  612. mcr p15, 0, r0, c7, c10, 1 // clean D / U line
  613. add r0, r0, r2
  614. cmp r0, r1
  615. blo loop4
  616. dsb
  617. bx lr
  618. /**
  619. * \brief Flush the data cache within the specified region
  620. * \param start virtual start address of region
  621. * \param end virtual end address of region
  622. */
  623. SECTION .CP15_flush_dcache_for_dma:CODE:NOROOT(2)
  624. PUBLIC CP15_flush_dcache_for_dma
  625. CP15_flush_dcache_for_dma:
  626. // dcache_line_size r2, r3
  627. mrc p15, 0, r3, c0, c0, 1 // read ctr
  628. lsr r3, r3, #16
  629. and r3, r3, #0xf // cache line size encoding
  630. mov r2, #4 // bytes per word
  631. mov r2, r2, lsl r3 // actual cache line size
  632. sub r3, r2, #1
  633. bic r0, r0, r3
  634. loop5:
  635. mcr p15, 0, r0, c7, c14, 1 // clean & invalidate D / U line
  636. add r0, r0, r2
  637. cmp r0, r1
  638. blo loop5
  639. dsb
  640. bx lr
  641. /**
  642. * \brief CP15_flush_kern_dcache_for_dma
  643. * Ensure that the data held in the page kaddr is written back to the page in question.
  644. * \param start virtual start address of region
  645. * \param end virtual end address of region
  646. */
  647. SECTION .CP15_flush_kern_dcache_for_dma:CODE:NOROOT(2)
  648. PUBLIC CP15_flush_kern_dcache_for_dma
  649. CP15_flush_kern_dcache_for_dma:
  650. // dcache_line_size r2, r3
  651. mrc p15, 0, r3, c0, c0, 1 // read ctr
  652. lsr r3, r3, #16
  653. and r3, r3, #0xf // cache line size encoding
  654. mov r2, #4 // bytes per word
  655. mov r2, r2, lsl r3 // actual cache line size
  656. add r1, r0, r1
  657. sub r3, r2, #1
  658. bic r0, r0, r3
  659. mcr p15, 0, r0, c7, c14, 1 // clean & invalidate D line / unified line
  660. add r0, r0, r2
  661. cmp r0, r1
  662. blo 1b
  663. dsb
  664. bx lr
  665. END