Merge tag 'ecryptfs-5.3-rc1-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / char / mspec.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2001-2006 Silicon Graphics, Inc.  All rights
4  * reserved.
5  */
6
7 /*
8  * SN Platform Special Memory (mspec) Support
9  *
10  * This driver exports the SN special memory (mspec) facility to user
11  * processes.
12  * There are three types of memory made available thru this driver:
13  * fetchops, uncached and cached.
14  *
15  * Fetchops are atomic memory operations that are implemented in the
16  * memory controller on SGI SN hardware.
17  *
18  * Uncached are used for memory write combining feature of the ia64
19  * cpu.
20  *
21  * Cached are used for areas of memory that are used as cached addresses
22  * on our partition and used as uncached addresses from other partitions.
23  * Due to a design constraint of the SN2 Shub, you can not have processors
24  * on the same FSB perform both a cached and uncached reference to the
25  * same cache line.  These special memory cached regions prevent the
26  * kernel from ever dropping in a TLB entry and therefore prevent the
27  * processor from ever speculating a cache line from this page.
28  */
29
30 #include <linux/types.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/errno.h>
35 #include <linux/miscdevice.h>
36 #include <linux/spinlock.h>
37 #include <linux/mm.h>
38 #include <linux/fs.h>
39 #include <linux/vmalloc.h>
40 #include <linux/string.h>
41 #include <linux/slab.h>
42 #include <linux/numa.h>
43 #include <linux/refcount.h>
44 #include <asm/page.h>
45 #include <asm/pgtable.h>
46 #include <linux/atomic.h>
47 #include <asm/tlbflush.h>
48 #include <asm/uncached.h>
49 #include <asm/sn/addrs.h>
50 #include <asm/sn/arch.h>
51 #include <asm/sn/mspec.h>
52 #include <asm/sn/sn_cpuid.h>
53 #include <asm/sn/io.h>
54 #include <asm/sn/bte.h>
55 #include <asm/sn/shubio.h>
56
57
58 #define FETCHOP_ID      "SGI Fetchop,"
59 #define CACHED_ID       "Cached,"
60 #define UNCACHED_ID     "Uncached"
61 #define REVISION        "4.0"
62 #define MSPEC_BASENAME  "mspec"
63
64 /*
65  * Page types allocated by the device.
66  */
67 enum mspec_page_type {
68         MSPEC_FETCHOP = 1,
69         MSPEC_CACHED,
70         MSPEC_UNCACHED
71 };
72
73 #ifdef CONFIG_SGI_SN
74 static int is_sn2;
75 #else
76 #define is_sn2          0
77 #endif
78
79 /*
80  * One of these structures is allocated when an mspec region is mmaped. The
81  * structure is pointed to by the vma->vm_private_data field in the vma struct.
82  * This structure is used to record the addresses of the mspec pages.
83  * This structure is shared by all vma's that are split off from the
84  * original vma when split_vma()'s are done.
85  *
86  * The refcnt is incremented atomically because mm->mmap_sem does not
87  * protect in fork case where multiple tasks share the vma_data.
88  */
89 struct vma_data {
90         refcount_t refcnt;      /* Number of vmas sharing the data. */
91         spinlock_t lock;        /* Serialize access to this structure. */
92         int count;              /* Number of pages allocated. */
93         enum mspec_page_type type; /* Type of pages allocated. */
94         unsigned long vm_start; /* Original (unsplit) base. */
95         unsigned long vm_end;   /* Original (unsplit) end. */
96         unsigned long maddr[0]; /* Array of MSPEC addresses. */
97 };
98
99 /* used on shub2 to clear FOP cache in the HUB */
100 static unsigned long scratch_page[MAX_NUMNODES];
101 #define SH2_AMO_CACHE_ENTRIES   4
102
103 static inline int
104 mspec_zero_block(unsigned long addr, int len)
105 {
106         int status;
107
108         if (is_sn2) {
109                 if (is_shub2()) {
110                         int nid;
111                         void *p;
112                         int i;
113
114                         nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
115                         p = (void *)TO_AMO(scratch_page[nid]);
116
117                         for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
118                                 FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
119                                 p += FETCHOP_VAR_SIZE;
120                         }
121                 }
122
123                 status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
124                                   BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
125         } else {
126                 memset((char *) addr, 0, len);
127                 status = 0;
128         }
129         return status;
130 }
131
132 /*
133  * mspec_open
134  *
135  * Called when a device mapping is created by a means other than mmap
136  * (via fork, munmap, etc.).  Increments the reference count on the
137  * underlying mspec data so it is not freed prematurely.
138  */
139 static void
140 mspec_open(struct vm_area_struct *vma)
141 {
142         struct vma_data *vdata;
143
144         vdata = vma->vm_private_data;
145         refcount_inc(&vdata->refcnt);
146 }
147
148 /*
149  * mspec_close
150  *
151  * Called when unmapping a device mapping. Frees all mspec pages
152  * belonging to all the vma's sharing this vma_data structure.
153  */
154 static void
155 mspec_close(struct vm_area_struct *vma)
156 {
157         struct vma_data *vdata;
158         int index, last_index;
159         unsigned long my_page;
160
161         vdata = vma->vm_private_data;
162
163         if (!refcount_dec_and_test(&vdata->refcnt))
164                 return;
165
166         last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
167         for (index = 0; index < last_index; index++) {
168                 if (vdata->maddr[index] == 0)
169                         continue;
170                 /*
171                  * Clear the page before sticking it back
172                  * into the pool.
173                  */
174                 my_page = vdata->maddr[index];
175                 vdata->maddr[index] = 0;
176                 if (!mspec_zero_block(my_page, PAGE_SIZE))
177                         uncached_free_page(my_page, 1);
178                 else
179                         printk(KERN_WARNING "mspec_close(): "
180                                "failed to zero page %ld\n", my_page);
181         }
182
183         kvfree(vdata);
184 }
185
186 /*
187  * mspec_fault
188  *
189  * Creates a mspec page and maps it to user space.
190  */
191 static vm_fault_t
192 mspec_fault(struct vm_fault *vmf)
193 {
194         unsigned long paddr, maddr;
195         unsigned long pfn;
196         pgoff_t index = vmf->pgoff;
197         struct vma_data *vdata = vmf->vma->vm_private_data;
198
199         maddr = (volatile unsigned long) vdata->maddr[index];
200         if (maddr == 0) {
201                 maddr = uncached_alloc_page(numa_node_id(), 1);
202                 if (maddr == 0)
203                         return VM_FAULT_OOM;
204
205                 spin_lock(&vdata->lock);
206                 if (vdata->maddr[index] == 0) {
207                         vdata->count++;
208                         vdata->maddr[index] = maddr;
209                 } else {
210                         uncached_free_page(maddr, 1);
211                         maddr = vdata->maddr[index];
212                 }
213                 spin_unlock(&vdata->lock);
214         }
215
216         if (vdata->type == MSPEC_FETCHOP)
217                 paddr = TO_AMO(maddr);
218         else
219                 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
220
221         pfn = paddr >> PAGE_SHIFT;
222
223         return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
224 }
225
226 static const struct vm_operations_struct mspec_vm_ops = {
227         .open = mspec_open,
228         .close = mspec_close,
229         .fault = mspec_fault,
230 };
231
232 /*
233  * mspec_mmap
234  *
235  * Called when mmapping the device.  Initializes the vma with a fault handler
236  * and private data structure necessary to allocate, track, and free the
237  * underlying pages.
238  */
239 static int
240 mspec_mmap(struct file *file, struct vm_area_struct *vma,
241                                         enum mspec_page_type type)
242 {
243         struct vma_data *vdata;
244         int pages, vdata_size;
245
246         if (vma->vm_pgoff != 0)
247                 return -EINVAL;
248
249         if ((vma->vm_flags & VM_SHARED) == 0)
250                 return -EINVAL;
251
252         if ((vma->vm_flags & VM_WRITE) == 0)
253                 return -EPERM;
254
255         pages = vma_pages(vma);
256         vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
257         if (vdata_size <= PAGE_SIZE)
258                 vdata = kzalloc(vdata_size, GFP_KERNEL);
259         else
260                 vdata = vzalloc(vdata_size);
261         if (!vdata)
262                 return -ENOMEM;
263
264         vdata->vm_start = vma->vm_start;
265         vdata->vm_end = vma->vm_end;
266         vdata->type = type;
267         spin_lock_init(&vdata->lock);
268         refcount_set(&vdata->refcnt, 1);
269         vma->vm_private_data = vdata;
270
271         vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
272         if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
273                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
274         vma->vm_ops = &mspec_vm_ops;
275
276         return 0;
277 }
278
279 static int
280 fetchop_mmap(struct file *file, struct vm_area_struct *vma)
281 {
282         return mspec_mmap(file, vma, MSPEC_FETCHOP);
283 }
284
285 static int
286 cached_mmap(struct file *file, struct vm_area_struct *vma)
287 {
288         return mspec_mmap(file, vma, MSPEC_CACHED);
289 }
290
291 static int
292 uncached_mmap(struct file *file, struct vm_area_struct *vma)
293 {
294         return mspec_mmap(file, vma, MSPEC_UNCACHED);
295 }
296
297 static const struct file_operations fetchop_fops = {
298         .owner = THIS_MODULE,
299         .mmap = fetchop_mmap,
300         .llseek = noop_llseek,
301 };
302
303 static struct miscdevice fetchop_miscdev = {
304         .minor = MISC_DYNAMIC_MINOR,
305         .name = "sgi_fetchop",
306         .fops = &fetchop_fops
307 };
308
309 static const struct file_operations cached_fops = {
310         .owner = THIS_MODULE,
311         .mmap = cached_mmap,
312         .llseek = noop_llseek,
313 };
314
315 static struct miscdevice cached_miscdev = {
316         .minor = MISC_DYNAMIC_MINOR,
317         .name = "mspec_cached",
318         .fops = &cached_fops
319 };
320
321 static const struct file_operations uncached_fops = {
322         .owner = THIS_MODULE,
323         .mmap = uncached_mmap,
324         .llseek = noop_llseek,
325 };
326
327 static struct miscdevice uncached_miscdev = {
328         .minor = MISC_DYNAMIC_MINOR,
329         .name = "mspec_uncached",
330         .fops = &uncached_fops
331 };
332
333 /*
334  * mspec_init
335  *
336  * Called at boot time to initialize the mspec facility.
337  */
338 static int __init
339 mspec_init(void)
340 {
341         int ret;
342         int nid;
343
344         /*
345          * The fetchop device only works on SN2 hardware, uncached and cached
346          * memory drivers should both be valid on all ia64 hardware
347          */
348 #ifdef CONFIG_SGI_SN
349         if (ia64_platform_is("sn2")) {
350                 is_sn2 = 1;
351                 if (is_shub2()) {
352                         ret = -ENOMEM;
353                         for_each_node_state(nid, N_ONLINE) {
354                                 int actual_nid;
355                                 int nasid;
356                                 unsigned long phys;
357
358                                 scratch_page[nid] = uncached_alloc_page(nid, 1);
359                                 if (scratch_page[nid] == 0)
360                                         goto free_scratch_pages;
361                                 phys = __pa(scratch_page[nid]);
362                                 nasid = get_node_number(phys);
363                                 actual_nid = nasid_to_cnodeid(nasid);
364                                 if (actual_nid != nid)
365                                         goto free_scratch_pages;
366                         }
367                 }
368
369                 ret = misc_register(&fetchop_miscdev);
370                 if (ret) {
371                         printk(KERN_ERR
372                                "%s: failed to register device %i\n",
373                                FETCHOP_ID, ret);
374                         goto free_scratch_pages;
375                 }
376         }
377 #endif
378         ret = misc_register(&cached_miscdev);
379         if (ret) {
380                 printk(KERN_ERR "%s: failed to register device %i\n",
381                        CACHED_ID, ret);
382                 if (is_sn2)
383                         misc_deregister(&fetchop_miscdev);
384                 goto free_scratch_pages;
385         }
386         ret = misc_register(&uncached_miscdev);
387         if (ret) {
388                 printk(KERN_ERR "%s: failed to register device %i\n",
389                        UNCACHED_ID, ret);
390                 misc_deregister(&cached_miscdev);
391                 if (is_sn2)
392                         misc_deregister(&fetchop_miscdev);
393                 goto free_scratch_pages;
394         }
395
396         printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
397                MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
398                CACHED_ID, UNCACHED_ID);
399
400         return 0;
401
402  free_scratch_pages:
403         for_each_node(nid) {
404                 if (scratch_page[nid] != 0)
405                         uncached_free_page(scratch_page[nid], 1);
406         }
407         return ret;
408 }
409
410 static void __exit
411 mspec_exit(void)
412 {
413         int nid;
414
415         misc_deregister(&uncached_miscdev);
416         misc_deregister(&cached_miscdev);
417         if (is_sn2) {
418                 misc_deregister(&fetchop_miscdev);
419
420                 for_each_node(nid) {
421                         if (scratch_page[nid] != 0)
422                                 uncached_free_page(scratch_page[nid], 1);
423                 }
424         }
425 }
426
427 module_init(mspec_init);
428 module_exit(mspec_exit);
429
430 MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
431 MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
432 MODULE_LICENSE("GPL");