introduction.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =========================
  3. Introduction to LoongArch
  4. =========================
  5. LoongArch is a new RISC ISA, which is a bit like MIPS or RISC-V. There are
  6. currently 3 variants: a reduced 32-bit version (LA32R), a standard 32-bit
  7. version (LA32S) and a 64-bit version (LA64). There are 4 privilege levels
  8. (PLVs) defined in LoongArch: PLV0~PLV3, from high to low. Kernel runs at PLV0
  9. while applications run at PLV3. This document introduces the registers, basic
  10. instruction set, virtual memory and some other topics of LoongArch.
  11. Registers
  12. =========
  13. LoongArch registers include general purpose registers (GPRs), floating point
  14. registers (FPRs), vector registers (VRs) and control status registers (CSRs)
  15. used in privileged mode (PLV0).
  16. GPRs
  17. ----
  18. LoongArch has 32 GPRs ( ``$r0`` ~ ``$r31`` ); each one is 32-bit wide in LA32
  19. and 64-bit wide in LA64. ``$r0`` is hard-wired to zero, and the other registers
  20. are not architecturally special. (Except ``$r1``, which is hard-wired as the
  21. link register of the BL instruction.)
  22. The kernel uses a variant of the LoongArch register convention, as described in
  23. the LoongArch ELF psABI spec, in :ref:`References <loongarch-references>`:
  24. ================= =============== =================== ============
  25. Name Alias Usage Preserved
  26. across calls
  27. ================= =============== =================== ============
  28. ``$r0`` ``$zero`` Constant zero Unused
  29. ``$r1`` ``$ra`` Return address No
  30. ``$r2`` ``$tp`` TLS/Thread pointer Unused
  31. ``$r3`` ``$sp`` Stack pointer Yes
  32. ``$r4``-``$r11`` ``$a0``-``$a7`` Argument registers No
  33. ``$r4``-``$r5`` ``$v0``-``$v1`` Return value No
  34. ``$r12``-``$r20`` ``$t0``-``$t8`` Temp registers No
  35. ``$r21`` ``$u0`` Percpu base address Unused
  36. ``$r22`` ``$fp`` Frame pointer Yes
  37. ``$r23``-``$r31`` ``$s0``-``$s8`` Static registers Yes
  38. ================= =============== =================== ============
  39. .. Note::
  40. The register ``$r21`` is reserved in the ELF psABI, but used by the Linux
  41. kernel for storing the percpu base address. It normally has no ABI name,
  42. but is called ``$u0`` in the kernel. You may also see ``$v0`` or ``$v1``
  43. in some old code,however they are deprecated aliases of ``$a0`` and ``$a1``
  44. respectively.
  45. FPRs
  46. ----
  47. LoongArch has 32 FPRs ( ``$f0`` ~ ``$f31`` ) when FPU is present. Each one is
  48. 64-bit wide on the LA64 cores.
  49. The floating-point register convention is the same as described in the
  50. LoongArch ELF psABI spec:
  51. ================= ================== =================== ============
  52. Name Alias Usage Preserved
  53. across calls
  54. ================= ================== =================== ============
  55. ``$f0``-``$f7`` ``$fa0``-``$fa7`` Argument registers No
  56. ``$f0``-``$f1`` ``$fv0``-``$fv1`` Return value No
  57. ``$f8``-``$f23`` ``$ft0``-``$ft15`` Temp registers No
  58. ``$f24``-``$f31`` ``$fs0``-``$fs7`` Static registers Yes
  59. ================= ================== =================== ============
  60. .. Note::
  61. You may see ``$fv0`` or ``$fv1`` in some old code, however they are
  62. deprecated aliases of ``$fa0`` and ``$fa1`` respectively.
  63. VRs
  64. ----
  65. There are currently 2 vector extensions to LoongArch:
  66. - LSX (Loongson SIMD eXtension) with 128-bit vectors,
  67. - LASX (Loongson Advanced SIMD eXtension) with 256-bit vectors.
  68. LSX brings ``$v0`` ~ ``$v31`` while LASX brings ``$x0`` ~ ``$x31`` as the vector
  69. registers.
  70. The VRs overlap with FPRs: for example, on a core implementing LSX and LASX,
  71. the lower 128 bits of ``$x0`` is shared with ``$v0``, and the lower 64 bits of
  72. ``$v0`` is shared with ``$f0``; same with all other VRs.
  73. CSRs
  74. ----
  75. CSRs can only be accessed from privileged mode (PLV0):
  76. ================= ===================================== ==============
  77. Address Full Name Abbrev Name
  78. ================= ===================================== ==============
  79. 0x0 Current Mode Information CRMD
  80. 0x1 Pre-exception Mode Information PRMD
  81. 0x2 Extension Unit Enable EUEN
  82. 0x3 Miscellaneous Control MISC
  83. 0x4 Exception Configuration ECFG
  84. 0x5 Exception Status ESTAT
  85. 0x6 Exception Return Address ERA
  86. 0x7 Bad (Faulting) Virtual Address BADV
  87. 0x8 Bad (Faulting) Instruction Word BADI
  88. 0xC Exception Entrypoint Address EENTRY
  89. 0x10 TLB Index TLBIDX
  90. 0x11 TLB Entry High-order Bits TLBEHI
  91. 0x12 TLB Entry Low-order Bits 0 TLBELO0
  92. 0x13 TLB Entry Low-order Bits 1 TLBELO1
  93. 0x18 Address Space Identifier ASID
  94. 0x19 Page Global Directory Address for PGDL
  95. Lower-half Address Space
  96. 0x1A Page Global Directory Address for PGDH
  97. Higher-half Address Space
  98. 0x1B Page Global Directory Address PGD
  99. 0x1C Page Walk Control for Lower- PWCL
  100. half Address Space
  101. 0x1D Page Walk Control for Higher- PWCH
  102. half Address Space
  103. 0x1E STLB Page Size STLBPS
  104. 0x1F Reduced Virtual Address Configuration RVACFG
  105. 0x20 CPU Identifier CPUID
  106. 0x21 Privileged Resource Configuration 1 PRCFG1
  107. 0x22 Privileged Resource Configuration 2 PRCFG2
  108. 0x23 Privileged Resource Configuration 3 PRCFG3
  109. 0x30+n (0≤n≤15) Saved Data register SAVEn
  110. 0x40 Timer Identifier TID
  111. 0x41 Timer Configuration TCFG
  112. 0x42 Timer Value TVAL
  113. 0x43 Compensation of Timer Count CNTC
  114. 0x44 Timer Interrupt Clearing TICLR
  115. 0x60 LLBit Control LLBCTL
  116. 0x80 Implementation-specific Control 1 IMPCTL1
  117. 0x81 Implementation-specific Control 2 IMPCTL2
  118. 0x88 TLB Refill Exception Entrypoint TLBRENTRY
  119. Address
  120. 0x89 TLB Refill Exception BAD (Faulting) TLBRBADV
  121. Virtual Address
  122. 0x8A TLB Refill Exception Return Address TLBRERA
  123. 0x8B TLB Refill Exception Saved Data TLBRSAVE
  124. Register
  125. 0x8C TLB Refill Exception Entry Low-order TLBRELO0
  126. Bits 0
  127. 0x8D TLB Refill Exception Entry Low-order TLBRELO1
  128. Bits 1
  129. 0x8E TLB Refill Exception Entry High-order TLBEHI
  130. Bits
  131. 0x8F TLB Refill Exception Pre-exception TLBRPRMD
  132. Mode Information
  133. 0x90 Machine Error Control MERRCTL
  134. 0x91 Machine Error Information 1 MERRINFO1
  135. 0x92 Machine Error Information 2 MERRINFO2
  136. 0x93 Machine Error Exception Entrypoint MERRENTRY
  137. Address
  138. 0x94 Machine Error Exception Return MERRERA
  139. Address
  140. 0x95 Machine Error Exception Saved Data MERRSAVE
  141. Register
  142. 0x98 Cache TAGs CTAG
  143. 0x180+n (0≤n≤3) Direct Mapping Configuration Window n DMWn
  144. 0x200+2n (0≤n≤31) Performance Monitor Configuration n PMCFGn
  145. 0x201+2n (0≤n≤31) Performance Monitor Overall Counter n PMCNTn
  146. 0x300 Memory Load/Store WatchPoint MWPC
  147. Overall Control
  148. 0x301 Memory Load/Store WatchPoint MWPS
  149. Overall Status
  150. 0x310+8n (0≤n≤7) Memory Load/Store WatchPoint n MWPnCFG1
  151. Configuration 1
  152. 0x311+8n (0≤n≤7) Memory Load/Store WatchPoint n MWPnCFG2
  153. Configuration 2
  154. 0x312+8n (0≤n≤7) Memory Load/Store WatchPoint n MWPnCFG3
  155. Configuration 3
  156. 0x313+8n (0≤n≤7) Memory Load/Store WatchPoint n MWPnCFG4
  157. Configuration 4
  158. 0x380 Instruction Fetch WatchPoint FWPC
  159. Overall Control
  160. 0x381 Instruction Fetch WatchPoint FWPS
  161. Overall Status
  162. 0x390+8n (0≤n≤7) Instruction Fetch WatchPoint n FWPnCFG1
  163. Configuration 1
  164. 0x391+8n (0≤n≤7) Instruction Fetch WatchPoint n FWPnCFG2
  165. Configuration 2
  166. 0x392+8n (0≤n≤7) Instruction Fetch WatchPoint n FWPnCFG3
  167. Configuration 3
  168. 0x393+8n (0≤n≤7) Instruction Fetch WatchPoint n FWPnCFG4
  169. Configuration 4
  170. 0x500 Debug Register DBG
  171. 0x501 Debug Exception Return Address DERA
  172. 0x502 Debug Exception Saved Data Register DSAVE
  173. ================= ===================================== ==============
  174. ERA, TLBRERA, MERRERA and DERA are sometimes also known as EPC, TLBREPC, MERREPC
  175. and DEPC respectively.
  176. Basic Instruction Set
  177. =====================
  178. Instruction formats
  179. -------------------
  180. LoongArch instructions are 32 bits wide, belonging to 9 basic instruction
  181. formats (and variants of them):
  182. =========== ==========================
  183. Format name Composition
  184. =========== ==========================
  185. 2R Opcode + Rj + Rd
  186. 3R Opcode + Rk + Rj + Rd
  187. 4R Opcode + Ra + Rk + Rj + Rd
  188. 2RI8 Opcode + I8 + Rj + Rd
  189. 2RI12 Opcode + I12 + Rj + Rd
  190. 2RI14 Opcode + I14 + Rj + Rd
  191. 2RI16 Opcode + I16 + Rj + Rd
  192. 1RI21 Opcode + I21L + Rj + I21H
  193. I26 Opcode + I26L + I26H
  194. =========== ==========================
  195. Rd is the destination register operand, while Rj, Rk and Ra ("a" stands for
  196. "additional") are the source register operands. I8/I12/I14/I16/I21/I26 are
  197. immediate operands of respective width. The longer I21 and I26 are stored
  198. in separate higher and lower parts in the instruction word, denoted by the "L"
  199. and "H" suffixes.
  200. List of Instructions
  201. --------------------
  202. For brevity, only instruction names (mnemonics) are listed here; please see the
  203. :ref:`References <loongarch-references>` for details.
  204. 1. Arithmetic Instructions::
  205. ADD.W SUB.W ADDI.W ADD.D SUB.D ADDI.D
  206. SLT SLTU SLTI SLTUI
  207. AND OR NOR XOR ANDN ORN ANDI ORI XORI
  208. MUL.W MULH.W MULH.WU DIV.W DIV.WU MOD.W MOD.WU
  209. MUL.D MULH.D MULH.DU DIV.D DIV.DU MOD.D MOD.DU
  210. PCADDI PCADDU12I PCADDU18I
  211. LU12I.W LU32I.D LU52I.D ADDU16I.D
  212. 2. Bit-shift Instructions::
  213. SLL.W SRL.W SRA.W ROTR.W SLLI.W SRLI.W SRAI.W ROTRI.W
  214. SLL.D SRL.D SRA.D ROTR.D SLLI.D SRLI.D SRAI.D ROTRI.D
  215. 3. Bit-manipulation Instructions::
  216. EXT.W.B EXT.W.H CLO.W CLO.D SLZ.W CLZ.D CTO.W CTO.D CTZ.W CTZ.D
  217. BYTEPICK.W BYTEPICK.D BSTRINS.W BSTRINS.D BSTRPICK.W BSTRPICK.D
  218. REVB.2H REVB.4H REVB.2W REVB.D REVH.2W REVH.D BITREV.4B BITREV.8B BITREV.W BITREV.D
  219. MASKEQZ MASKNEZ
  220. 4. Branch Instructions::
  221. BEQ BNE BLT BGE BLTU BGEU BEQZ BNEZ B BL JIRL
  222. 5. Load/Store Instructions::
  223. LD.B LD.BU LD.H LD.HU LD.W LD.WU LD.D ST.B ST.H ST.W ST.D
  224. LDX.B LDX.BU LDX.H LDX.HU LDX.W LDX.WU LDX.D STX.B STX.H STX.W STX.D
  225. LDPTR.W LDPTR.D STPTR.W STPTR.D
  226. PRELD PRELDX
  227. 6. Atomic Operation Instructions::
  228. LL.W SC.W LL.D SC.D
  229. AMSWAP.W AMSWAP.D AMADD.W AMADD.D AMAND.W AMAND.D AMOR.W AMOR.D AMXOR.W AMXOR.D
  230. AMMAX.W AMMAX.D AMMIN.W AMMIN.D
  231. 7. Barrier Instructions::
  232. IBAR DBAR
  233. 8. Special Instructions::
  234. SYSCALL BREAK CPUCFG NOP IDLE ERTN(ERET) DBCL(DBGCALL) RDTIMEL.W RDTIMEH.W RDTIME.D
  235. ASRTLE.D ASRTGT.D
  236. 9. Privileged Instructions::
  237. CSRRD CSRWR CSRXCHG
  238. IOCSRRD.B IOCSRRD.H IOCSRRD.W IOCSRRD.D IOCSRWR.B IOCSRWR.H IOCSRWR.W IOCSRWR.D
  239. CACOP TLBP(TLBSRCH) TLBRD TLBWR TLBFILL TLBCLR TLBFLUSH INVTLB LDDIR LDPTE
  240. Virtual Memory
  241. ==============
  242. LoongArch supports direct-mapped virtual memory and page-mapped virtual memory.
  243. Direct-mapped virtual memory is configured by CSR.DMWn (n=0~3), it has a simple
  244. relationship between virtual address (VA) and physical address (PA)::
  245. VA = PA + FixedOffset
  246. Page-mapped virtual memory has arbitrary relationship between VA and PA, which
  247. is recorded in TLB and page tables. LoongArch's TLB includes a fully-associative
  248. MTLB (Multiple Page Size TLB) and set-associative STLB (Single Page Size TLB).
  249. By default, the whole virtual address space of LA32 is configured like this:
  250. ============ =========================== =============================
  251. Name Address Range Attributes
  252. ============ =========================== =============================
  253. ``UVRANGE`` ``0x00000000 - 0x7FFFFFFF`` Page-mapped, Cached, PLV0~3
  254. ``KPRANGE0`` ``0x80000000 - 0x9FFFFFFF`` Direct-mapped, Uncached, PLV0
  255. ``KPRANGE1`` ``0xA0000000 - 0xBFFFFFFF`` Direct-mapped, Cached, PLV0
  256. ``KVRANGE`` ``0xC0000000 - 0xFFFFFFFF`` Page-mapped, Cached, PLV0
  257. ============ =========================== =============================
  258. User mode (PLV3) can only access UVRANGE. For direct-mapped KPRANGE0 and
  259. KPRANGE1, PA is equal to VA with bit30~31 cleared. For example, the uncached
  260. direct-mapped VA of 0x00001000 is 0x80001000, and the cached direct-mapped
  261. VA of 0x00001000 is 0xA0001000.
  262. By default, the whole virtual address space of LA64 is configured like this:
  263. ============ ====================== ======================================
  264. Name Address Range Attributes
  265. ============ ====================== ======================================
  266. ``XUVRANGE`` ``0x0000000000000000 - Page-mapped, Cached, PLV0~3
  267. 0x3FFFFFFFFFFFFFFF``
  268. ``XSPRANGE`` ``0x4000000000000000 - Direct-mapped, Cached / Uncached, PLV0
  269. 0x7FFFFFFFFFFFFFFF``
  270. ``XKPRANGE`` ``0x8000000000000000 - Direct-mapped, Cached / Uncached, PLV0
  271. 0xBFFFFFFFFFFFFFFF``
  272. ``XKVRANGE`` ``0xC000000000000000 - Page-mapped, Cached, PLV0
  273. 0xFFFFFFFFFFFFFFFF``
  274. ============ ====================== ======================================
  275. User mode (PLV3) can only access XUVRANGE. For direct-mapped XSPRANGE and
  276. XKPRANGE, PA is equal to VA with bits 60~63 cleared, and the cache attribute
  277. is configured by bits 60~61 in VA: 0 is for strongly-ordered uncached, 1 is
  278. for coherent cached, and 2 is for weakly-ordered uncached.
  279. Currently we only use XKPRANGE for direct mapping and XSPRANGE is reserved.
  280. To put this in action: the strongly-ordered uncached direct-mapped VA (in
  281. XKPRANGE) of 0x00000000_00001000 is 0x80000000_00001000, the coherent cached
  282. direct-mapped VA (in XKPRANGE) of 0x00000000_00001000 is 0x90000000_00001000,
  283. and the weakly-ordered uncached direct-mapped VA (in XKPRANGE) of 0x00000000
  284. _00001000 is 0xA0000000_00001000.
  285. Relationship of Loongson and LoongArch
  286. ======================================
  287. LoongArch is a RISC ISA which is different from any other existing ones, while
  288. Loongson is a family of processors. Loongson includes 3 series: Loongson-1 is
  289. the 32-bit processor series, Loongson-2 is the low-end 64-bit processor series,
  290. and Loongson-3 is the high-end 64-bit processor series. Old Loongson is based on
  291. MIPS, while New Loongson is based on LoongArch. Take Loongson-3 as an example:
  292. Loongson-3A1000/3B1500/3A2000/3A3000/3A4000 are MIPS-compatible, while Loongson-
  293. 3A5000 (and future revisions) are all based on LoongArch.
  294. .. _loongarch-references:
  295. References
  296. ==========
  297. Official web site of Loongson Technology Corp. Ltd.:
  298. http://www.loongson.cn/
  299. Developer web site of Loongson and LoongArch (Software and Documentation):
  300. http://www.loongnix.cn/
  301. https://github.com/loongson/
  302. https://loongson.github.io/LoongArch-Documentation/
  303. Documentation of LoongArch ISA:
  304. https://github.com/loongson/LoongArch-Documentation/releases/latest/download/LoongArch-Vol1-v1.10-CN.pdf (in Chinese)
  305. https://github.com/loongson/LoongArch-Documentation/releases/latest/download/LoongArch-Vol1-v1.10-EN.pdf (in English)
  306. Documentation of LoongArch ELF psABI:
  307. https://github.com/loongson/LoongArch-Documentation/releases/latest/download/LoongArch-ELF-ABI-v2.01-CN.pdf (in Chinese)
  308. https://github.com/loongson/LoongArch-Documentation/releases/latest/download/LoongArch-ELF-ABI-v2.01-EN.pdf (in English)
  309. Linux kernel repository of Loongson and LoongArch:
  310. https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git