Skip to content

Commit 559671e

Browse files
committed
LoongArch: Add some library functions
Add some library functions for LoongArch, including: delay, memset, memcpy, memmove, copy_user, strncpy_user, strnlen_user and tlb dump functions. Reviewed-by: WANG Xuerui <[email protected]> Reviewed-by: Jiaxun Yang <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 7153c3c commit 559671e

File tree

6 files changed

+282
-0
lines changed

6 files changed

+282
-0
lines changed

arch/loongarch/include/asm/delay.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+
*/
5+
#ifndef _ASM_DELAY_H
6+
#define _ASM_DELAY_H
7+
8+
#include <linux/param.h>
9+
10+
extern void __delay(unsigned long cycles);
11+
extern void __ndelay(unsigned long ns);
12+
extern void __udelay(unsigned long us);
13+
14+
#define ndelay(ns) __ndelay(ns)
15+
#define udelay(us) __udelay(us)
16+
17+
/* make sure "usecs *= ..." in udelay do not overflow. */
18+
#if HZ >= 1000
19+
#define MAX_UDELAY_MS 1
20+
#elif HZ <= 200
21+
#define MAX_UDELAY_MS 5
22+
#else
23+
#define MAX_UDELAY_MS (1000 / HZ)
24+
#endif
25+
26+
#endif /* _ASM_DELAY_H */

arch/loongarch/include/asm/string.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+
*/
5+
#ifndef _ASM_STRING_H
6+
#define _ASM_STRING_H
7+
8+
extern void *memset(void *__s, int __c, size_t __count);
9+
extern void *memcpy(void *__to, __const__ void *__from, size_t __n);
10+
extern void *memmove(void *__dest, __const__ void *__src, size_t __n);
11+
12+
#endif /* _ASM_STRING_H */

arch/loongarch/lib/clear_user.S

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+
*/
5+
6+
#include <asm/asm.h>
7+
#include <asm/asmmacro.h>
8+
#include <asm/export.h>
9+
#include <asm/regdef.h>
10+
11+
.macro fixup_ex from, to, offset, fix
12+
.if \fix
13+
.section .fixup, "ax"
14+
\to: addi.d a0, a1, \offset
15+
jr ra
16+
.previous
17+
.endif
18+
.section __ex_table, "a"
19+
PTR \from\()b, \to\()b
20+
.previous
21+
.endm
22+
23+
/*
24+
* unsigned long __clear_user(void *addr, size_t size)
25+
*
26+
* a0: addr
27+
* a1: size
28+
*/
29+
SYM_FUNC_START(__clear_user)
30+
beqz a1, 2f
31+
32+
1: st.b zero, a0, 0
33+
addi.d a0, a0, 1
34+
addi.d a1, a1, -1
35+
bgt a1, zero, 1b
36+
37+
2: move a0, a1
38+
jr ra
39+
40+
fixup_ex 1, 3, 0, 1
41+
SYM_FUNC_END(__clear_user)
42+
43+
EXPORT_SYMBOL(__clear_user)

arch/loongarch/lib/copy_user.S

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+
*/
5+
6+
#include <asm/asm.h>
7+
#include <asm/asmmacro.h>
8+
#include <asm/export.h>
9+
#include <asm/regdef.h>
10+
11+
.macro fixup_ex from, to, offset, fix
12+
.if \fix
13+
.section .fixup, "ax"
14+
\to: addi.d a0, a2, \offset
15+
jr ra
16+
.previous
17+
.endif
18+
.section __ex_table, "a"
19+
PTR \from\()b, \to\()b
20+
.previous
21+
.endm
22+
23+
/*
24+
* unsigned long __copy_user(void *to, const void *from, size_t n)
25+
*
26+
* a0: to
27+
* a1: from
28+
* a2: n
29+
*/
30+
SYM_FUNC_START(__copy_user)
31+
beqz a2, 3f
32+
33+
1: ld.b t0, a1, 0
34+
2: st.b t0, a0, 0
35+
addi.d a0, a0, 1
36+
addi.d a1, a1, 1
37+
addi.d a2, a2, -1
38+
bgt a2, zero, 1b
39+
40+
3: move a0, a2
41+
jr ra
42+
43+
fixup_ex 1, 4, 0, 1
44+
fixup_ex 2, 4, 0, 0
45+
SYM_FUNC_END(__copy_user)
46+
47+
EXPORT_SYMBOL(__copy_user)

arch/loongarch/lib/delay.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+
*/
5+
#include <linux/delay.h>
6+
#include <linux/export.h>
7+
#include <linux/smp.h>
8+
#include <linux/timex.h>
9+
10+
#include <asm/compiler.h>
11+
#include <asm/processor.h>
12+
13+
void __delay(unsigned long cycles)
14+
{
15+
u64 t0 = get_cycles();
16+
17+
while ((unsigned long)(get_cycles() - t0) < cycles)
18+
cpu_relax();
19+
}
20+
EXPORT_SYMBOL(__delay);
21+
22+
/*
23+
* Division by multiplication: you don't have to worry about
24+
* loss of precision.
25+
*
26+
* Use only for very small delays ( < 1 msec). Should probably use a
27+
* lookup table, really, as the multiplications take much too long with
28+
* short delays. This is a "reasonable" implementation, though (and the
29+
* first constant multiplications gets optimized away if the delay is
30+
* a constant)
31+
*/
32+
33+
void __udelay(unsigned long us)
34+
{
35+
__delay((us * 0x000010c7ull * HZ * lpj_fine) >> 32);
36+
}
37+
EXPORT_SYMBOL(__udelay);
38+
39+
void __ndelay(unsigned long ns)
40+
{
41+
__delay((ns * 0x00000005ull * HZ * lpj_fine) >> 32);
42+
}
43+
EXPORT_SYMBOL(__ndelay);

arch/loongarch/lib/dump_tlb.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+
*
5+
* Derived from MIPS:
6+
* Copyright (C) 1994, 1995 by Waldorf Electronics, written by Ralf Baechle.
7+
* Copyright (C) 1999 by Silicon Graphics, Inc.
8+
*/
9+
#include <linux/kernel.h>
10+
#include <linux/mm.h>
11+
12+
#include <asm/loongarch.h>
13+
#include <asm/page.h>
14+
#include <asm/pgtable.h>
15+
#include <asm/tlb.h>
16+
17+
void dump_tlb_regs(void)
18+
{
19+
const int field = 2 * sizeof(unsigned long);
20+
21+
pr_info("Index : %0x\n", read_csr_tlbidx());
22+
pr_info("PageSize : %0x\n", read_csr_pagesize());
23+
pr_info("EntryHi : %0*llx\n", field, read_csr_entryhi());
24+
pr_info("EntryLo0 : %0*llx\n", field, read_csr_entrylo0());
25+
pr_info("EntryLo1 : %0*llx\n", field, read_csr_entrylo1());
26+
}
27+
28+
static void dump_tlb(int first, int last)
29+
{
30+
unsigned long s_entryhi, entryhi, asid;
31+
unsigned long long entrylo0, entrylo1, pa;
32+
unsigned int index;
33+
unsigned int s_index, s_asid;
34+
unsigned int pagesize, c0, c1, i;
35+
unsigned long asidmask = cpu_asid_mask(&current_cpu_data);
36+
int pwidth = 11;
37+
int vwidth = 11;
38+
int asidwidth = DIV_ROUND_UP(ilog2(asidmask) + 1, 4);
39+
40+
s_entryhi = read_csr_entryhi();
41+
s_index = read_csr_tlbidx();
42+
s_asid = read_csr_asid();
43+
44+
for (i = first; i <= last; i++) {
45+
write_csr_index(i);
46+
tlb_read();
47+
pagesize = read_csr_pagesize();
48+
entryhi = read_csr_entryhi();
49+
entrylo0 = read_csr_entrylo0();
50+
entrylo1 = read_csr_entrylo1();
51+
index = read_csr_tlbidx();
52+
asid = read_csr_asid();
53+
54+
/* EHINV bit marks entire entry as invalid */
55+
if (index & CSR_TLBIDX_EHINV)
56+
continue;
57+
/*
58+
* ASID takes effect in absence of G (global) bit.
59+
*/
60+
if (!((entrylo0 | entrylo1) & ENTRYLO_G) &&
61+
asid != s_asid)
62+
continue;
63+
64+
/*
65+
* Only print entries in use
66+
*/
67+
pr_info("Index: %2d pgsize=%x ", i, (1 << pagesize));
68+
69+
c0 = (entrylo0 & ENTRYLO_C) >> ENTRYLO_C_SHIFT;
70+
c1 = (entrylo1 & ENTRYLO_C) >> ENTRYLO_C_SHIFT;
71+
72+
pr_cont("va=%0*lx asid=%0*lx",
73+
vwidth, (entryhi & ~0x1fffUL), asidwidth, asid & asidmask);
74+
75+
/* NR/NX are in awkward places, so mask them off separately */
76+
pa = entrylo0 & ~(ENTRYLO_NR | ENTRYLO_NX);
77+
pa = pa & PAGE_MASK;
78+
pr_cont("\n\t[");
79+
pr_cont("ri=%d xi=%d ",
80+
(entrylo0 & ENTRYLO_NR) ? 1 : 0,
81+
(entrylo0 & ENTRYLO_NX) ? 1 : 0);
82+
pr_cont("pa=%0*llx c=%d d=%d v=%d g=%d plv=%lld] [",
83+
pwidth, pa, c0,
84+
(entrylo0 & ENTRYLO_D) ? 1 : 0,
85+
(entrylo0 & ENTRYLO_V) ? 1 : 0,
86+
(entrylo0 & ENTRYLO_G) ? 1 : 0,
87+
(entrylo0 & ENTRYLO_PLV) >> ENTRYLO_PLV_SHIFT);
88+
/* NR/NX are in awkward places, so mask them off separately */
89+
pa = entrylo1 & ~(ENTRYLO_NR | ENTRYLO_NX);
90+
pa = pa & PAGE_MASK;
91+
pr_cont("ri=%d xi=%d ",
92+
(entrylo1 & ENTRYLO_NR) ? 1 : 0,
93+
(entrylo1 & ENTRYLO_NX) ? 1 : 0);
94+
pr_cont("pa=%0*llx c=%d d=%d v=%d g=%d plv=%lld]\n",
95+
pwidth, pa, c1,
96+
(entrylo1 & ENTRYLO_D) ? 1 : 0,
97+
(entrylo1 & ENTRYLO_V) ? 1 : 0,
98+
(entrylo1 & ENTRYLO_G) ? 1 : 0,
99+
(entrylo1 & ENTRYLO_PLV) >> ENTRYLO_PLV_SHIFT);
100+
}
101+
pr_info("\n");
102+
103+
write_csr_entryhi(s_entryhi);
104+
write_csr_tlbidx(s_index);
105+
write_csr_asid(s_asid);
106+
}
107+
108+
void dump_tlb_all(void)
109+
{
110+
dump_tlb(0, current_cpu_data.tlbsize - 1);
111+
}

0 commit comments

Comments
 (0)