RISC-V: hwprobe: Add support for RISCV_HWPROBE_BASE_BEHAVIOR_IMA
[platform/kernel/linux-starfive.git] / arch / riscv / kernel / sys_riscv.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  * Copyright (C) 2014 Darius Rad <darius@bluespec.com>
5  * Copyright (C) 2017 SiFive
6  */
7
8 #include <linux/syscalls.h>
9 #include <asm/cacheflush.h>
10 #include <asm/hwprobe.h>
11 #include <asm/sbi.h>
12 #include <asm/switch_to.h>
13 #include <asm/uaccess.h>
14 #include <asm/unistd.h>
15 #include <asm-generic/mman-common.h>
16
17 static long riscv_sys_mmap(unsigned long addr, unsigned long len,
18                            unsigned long prot, unsigned long flags,
19                            unsigned long fd, off_t offset,
20                            unsigned long page_shift_offset)
21 {
22         if (unlikely(offset & (~PAGE_MASK >> page_shift_offset)))
23                 return -EINVAL;
24
25         return ksys_mmap_pgoff(addr, len, prot, flags, fd,
26                                offset >> (PAGE_SHIFT - page_shift_offset));
27 }
28
29 #ifdef CONFIG_64BIT
30 SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
31         unsigned long, prot, unsigned long, flags,
32         unsigned long, fd, off_t, offset)
33 {
34         return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 0);
35 }
36 #endif
37
38 #if defined(CONFIG_32BIT) || defined(CONFIG_COMPAT)
39 SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
40         unsigned long, prot, unsigned long, flags,
41         unsigned long, fd, off_t, offset)
42 {
43         /*
44          * Note that the shift for mmap2 is constant (12),
45          * regardless of PAGE_SIZE
46          */
47         return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 12);
48 }
49 #endif
50
51 /*
52  * Allows the instruction cache to be flushed from userspace.  Despite RISC-V
53  * having a direct 'fence.i' instruction available to userspace (which we
54  * can't trap!), that's not actually viable when running on Linux because the
55  * kernel might schedule a process on another hart.  There is no way for
56  * userspace to handle this without invoking the kernel (as it doesn't know the
57  * thread->hart mappings), so we've defined a RISC-V specific system call to
58  * flush the instruction cache.
59  *
60  * sys_riscv_flush_icache() is defined to flush the instruction cache over an
61  * address range, with the flush applying to either all threads or just the
62  * caller.  We don't currently do anything with the address range, that's just
63  * in there for forwards compatibility.
64  */
65 SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
66         uintptr_t, flags)
67 {
68         /* Check the reserved flags. */
69         if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_ALL))
70                 return -EINVAL;
71
72         flush_icache_mm(current->mm, flags & SYS_RISCV_FLUSH_ICACHE_LOCAL);
73
74         return 0;
75 }
76
77 /*
78  * The hwprobe interface, for allowing userspace to probe to see which features
79  * are supported by the hardware.  See Documentation/riscv/hwprobe.rst for more
80  * details.
81  */
82 static void hwprobe_arch_id(struct riscv_hwprobe *pair,
83                             const struct cpumask *cpus)
84 {
85         u64 id = -1ULL;
86         bool first = true;
87         int cpu;
88
89         for_each_cpu(cpu, cpus) {
90                 u64 cpu_id;
91
92                 switch (pair->key) {
93                 case RISCV_HWPROBE_KEY_MVENDORID:
94                         cpu_id = riscv_cached_mvendorid(cpu);
95                         break;
96                 case RISCV_HWPROBE_KEY_MIMPID:
97                         cpu_id = riscv_cached_mimpid(cpu);
98                         break;
99                 case RISCV_HWPROBE_KEY_MARCHID:
100                         cpu_id = riscv_cached_marchid(cpu);
101                         break;
102                 }
103
104                 if (first)
105                         id = cpu_id;
106
107                 /*
108                  * If there's a mismatch for the given set, return -1 in the
109                  * value.
110                  */
111                 if (id != cpu_id) {
112                         id = -1ULL;
113                         break;
114                 }
115         }
116
117         pair->value = id;
118 }
119
120 static void hwprobe_one_pair(struct riscv_hwprobe *pair,
121                              const struct cpumask *cpus)
122 {
123         switch (pair->key) {
124         case RISCV_HWPROBE_KEY_MVENDORID:
125         case RISCV_HWPROBE_KEY_MARCHID:
126         case RISCV_HWPROBE_KEY_MIMPID:
127                 hwprobe_arch_id(pair, cpus);
128                 break;
129         /*
130          * The kernel already assumes that the base single-letter ISA
131          * extensions are supported on all harts, and only supports the
132          * IMA base, so just cheat a bit here and tell that to
133          * userspace.
134          */
135         case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
136                 pair->value = RISCV_HWPROBE_BASE_BEHAVIOR_IMA;
137                 break;
138
139         case RISCV_HWPROBE_KEY_IMA_EXT_0:
140                 pair->value = 0;
141                 if (has_fpu())
142                         pair->value |= RISCV_HWPROBE_IMA_FD;
143
144                 if (riscv_isa_extension_available(NULL, c))
145                         pair->value |= RISCV_HWPROBE_IMA_C;
146
147                 break;
148
149         /*
150          * For forward compatibility, unknown keys don't fail the whole
151          * call, but get their element key set to -1 and value set to 0
152          * indicating they're unrecognized.
153          */
154         default:
155                 pair->key = -1;
156                 pair->value = 0;
157                 break;
158         }
159 }
160
161 static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,
162                             size_t pair_count, size_t cpu_count,
163                             unsigned long __user *cpus_user,
164                             unsigned int flags)
165 {
166         size_t out;
167         int ret;
168         cpumask_t cpus;
169
170         /* Check the reserved flags. */
171         if (flags != 0)
172                 return -EINVAL;
173
174         /*
175          * The interface supports taking in a CPU mask, and returns values that
176          * are consistent across that mask. Allow userspace to specify NULL and
177          * 0 as a shortcut to all online CPUs.
178          */
179         cpumask_clear(&cpus);
180         if (!cpu_count && !cpus_user) {
181                 cpumask_copy(&cpus, cpu_online_mask);
182         } else {
183                 if (cpu_count > cpumask_size())
184                         cpu_count = cpumask_size();
185
186                 ret = copy_from_user(&cpus, cpus_user, cpu_count);
187                 if (ret)
188                         return -EFAULT;
189
190                 /*
191                  * Userspace must provide at least one online CPU, without that
192                  * there's no way to define what is supported.
193                  */
194                 cpumask_and(&cpus, &cpus, cpu_online_mask);
195                 if (cpumask_empty(&cpus))
196                         return -EINVAL;
197         }
198
199         for (out = 0; out < pair_count; out++, pairs++) {
200                 struct riscv_hwprobe pair;
201
202                 if (get_user(pair.key, &pairs->key))
203                         return -EFAULT;
204
205                 pair.value = 0;
206                 hwprobe_one_pair(&pair, &cpus);
207                 ret = put_user(pair.key, &pairs->key);
208                 if (ret == 0)
209                         ret = put_user(pair.value, &pairs->value);
210
211                 if (ret)
212                         return -EFAULT;
213         }
214
215         return 0;
216 }
217
218 SYSCALL_DEFINE5(riscv_hwprobe, struct riscv_hwprobe __user *, pairs,
219                 size_t, pair_count, size_t, cpu_count, unsigned long __user *,
220                 cpus, unsigned int, flags)
221 {
222         return do_riscv_hwprobe(pairs, pair_count, cpu_count,
223                                 cpus, flags);
224 }