summaryrefslogtreecommitdiff
path: root/arch/loongarch/vdso/vgetcpu.c
blob: 43a0078e441854cf2787a8fcf3fb97f2ed442c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Fast user context implementation of getcpu()
 */

#include <asm/vdso.h>
#include <linux/getcpu.h>

static __always_inline int read_cpu_id(void)
{
	int cpu_id;

	__asm__ __volatile__(
	"	rdtime.d $zero, %0\n"
	: "=r" (cpu_id)
	:
	: "memory");

	return cpu_id;
}

static __always_inline const struct vdso_pcpu_data *get_pcpu_data(void)
{
	return (struct vdso_pcpu_data *)(get_vdso_base() - VDSO_DATA_SIZE);
}

int __vdso_getcpu(unsigned int *cpu, unsigned int *node, struct getcpu_cache *unused)
{
	int cpu_id;
	const struct vdso_pcpu_data *data;

	cpu_id = read_cpu_id();

	if (cpu)
		*cpu = cpu_id;

	if (node) {
		data = get_pcpu_data();
		*node = data[cpu_id].node;
	}

	return 0;
}