1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights
4*4882a593Smuzhiyun * reserved.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun /*
8*4882a593Smuzhiyun * SN Platform Special Memory (mspec) Support
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This driver exports the SN special memory (mspec) facility to user
11*4882a593Smuzhiyun * processes.
12*4882a593Smuzhiyun * There are two types of memory made available thru this driver:
13*4882a593Smuzhiyun * uncached and cached.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Uncached are used for memory write combining feature of the ia64
16*4882a593Smuzhiyun * cpu.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * Cached are used for areas of memory that are used as cached addresses
19*4882a593Smuzhiyun * on our partition and used as uncached addresses from other partitions.
20*4882a593Smuzhiyun * Due to a design constraint of the SN2 Shub, you can not have processors
21*4882a593Smuzhiyun * on the same FSB perform both a cached and uncached reference to the
22*4882a593Smuzhiyun * same cache line. These special memory cached regions prevent the
23*4882a593Smuzhiyun * kernel from ever dropping in a TLB entry and therefore prevent the
24*4882a593Smuzhiyun * processor from ever speculating a cache line from this page.
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <linux/types.h>
28*4882a593Smuzhiyun #include <linux/kernel.h>
29*4882a593Smuzhiyun #include <linux/module.h>
30*4882a593Smuzhiyun #include <linux/init.h>
31*4882a593Smuzhiyun #include <linux/errno.h>
32*4882a593Smuzhiyun #include <linux/miscdevice.h>
33*4882a593Smuzhiyun #include <linux/spinlock.h>
34*4882a593Smuzhiyun #include <linux/mm.h>
35*4882a593Smuzhiyun #include <linux/fs.h>
36*4882a593Smuzhiyun #include <linux/vmalloc.h>
37*4882a593Smuzhiyun #include <linux/string.h>
38*4882a593Smuzhiyun #include <linux/slab.h>
39*4882a593Smuzhiyun #include <linux/numa.h>
40*4882a593Smuzhiyun #include <linux/refcount.h>
41*4882a593Smuzhiyun #include <asm/page.h>
42*4882a593Smuzhiyun #include <linux/atomic.h>
43*4882a593Smuzhiyun #include <asm/tlbflush.h>
44*4882a593Smuzhiyun #include <asm/uncached.h>
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #define CACHED_ID "Cached,"
48*4882a593Smuzhiyun #define UNCACHED_ID "Uncached"
49*4882a593Smuzhiyun #define REVISION "4.0"
50*4882a593Smuzhiyun #define MSPEC_BASENAME "mspec"
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun * Page types allocated by the device.
54*4882a593Smuzhiyun */
55*4882a593Smuzhiyun enum mspec_page_type {
56*4882a593Smuzhiyun MSPEC_CACHED = 2,
57*4882a593Smuzhiyun MSPEC_UNCACHED
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun * One of these structures is allocated when an mspec region is mmaped. The
62*4882a593Smuzhiyun * structure is pointed to by the vma->vm_private_data field in the vma struct.
63*4882a593Smuzhiyun * This structure is used to record the addresses of the mspec pages.
64*4882a593Smuzhiyun * This structure is shared by all vma's that are split off from the
65*4882a593Smuzhiyun * original vma when split_vma()'s are done.
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * The refcnt is incremented atomically because mm->mmap_lock does not
68*4882a593Smuzhiyun * protect in fork case where multiple tasks share the vma_data.
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun struct vma_data {
71*4882a593Smuzhiyun refcount_t refcnt; /* Number of vmas sharing the data. */
72*4882a593Smuzhiyun spinlock_t lock; /* Serialize access to this structure. */
73*4882a593Smuzhiyun int count; /* Number of pages allocated. */
74*4882a593Smuzhiyun enum mspec_page_type type; /* Type of pages allocated. */
75*4882a593Smuzhiyun unsigned long vm_start; /* Original (unsplit) base. */
76*4882a593Smuzhiyun unsigned long vm_end; /* Original (unsplit) end. */
77*4882a593Smuzhiyun unsigned long maddr[]; /* Array of MSPEC addresses. */
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun * mspec_open
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * Called when a device mapping is created by a means other than mmap
84*4882a593Smuzhiyun * (via fork, munmap, etc.). Increments the reference count on the
85*4882a593Smuzhiyun * underlying mspec data so it is not freed prematurely.
86*4882a593Smuzhiyun */
87*4882a593Smuzhiyun static void
mspec_open(struct vm_area_struct * vma)88*4882a593Smuzhiyun mspec_open(struct vm_area_struct *vma)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun struct vma_data *vdata;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun vdata = vma->vm_private_data;
93*4882a593Smuzhiyun refcount_inc(&vdata->refcnt);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun * mspec_close
98*4882a593Smuzhiyun *
99*4882a593Smuzhiyun * Called when unmapping a device mapping. Frees all mspec pages
100*4882a593Smuzhiyun * belonging to all the vma's sharing this vma_data structure.
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun static void
mspec_close(struct vm_area_struct * vma)103*4882a593Smuzhiyun mspec_close(struct vm_area_struct *vma)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun struct vma_data *vdata;
106*4882a593Smuzhiyun int index, last_index;
107*4882a593Smuzhiyun unsigned long my_page;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun vdata = vma->vm_private_data;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun if (!refcount_dec_and_test(&vdata->refcnt))
112*4882a593Smuzhiyun return;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
115*4882a593Smuzhiyun for (index = 0; index < last_index; index++) {
116*4882a593Smuzhiyun if (vdata->maddr[index] == 0)
117*4882a593Smuzhiyun continue;
118*4882a593Smuzhiyun /*
119*4882a593Smuzhiyun * Clear the page before sticking it back
120*4882a593Smuzhiyun * into the pool.
121*4882a593Smuzhiyun */
122*4882a593Smuzhiyun my_page = vdata->maddr[index];
123*4882a593Smuzhiyun vdata->maddr[index] = 0;
124*4882a593Smuzhiyun memset((char *)my_page, 0, PAGE_SIZE);
125*4882a593Smuzhiyun uncached_free_page(my_page, 1);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun kvfree(vdata);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun * mspec_fault
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * Creates a mspec page and maps it to user space.
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun static vm_fault_t
mspec_fault(struct vm_fault * vmf)137*4882a593Smuzhiyun mspec_fault(struct vm_fault *vmf)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun unsigned long paddr, maddr;
140*4882a593Smuzhiyun unsigned long pfn;
141*4882a593Smuzhiyun pgoff_t index = vmf->pgoff;
142*4882a593Smuzhiyun struct vma_data *vdata = vmf->vma->vm_private_data;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun maddr = (volatile unsigned long) vdata->maddr[index];
145*4882a593Smuzhiyun if (maddr == 0) {
146*4882a593Smuzhiyun maddr = uncached_alloc_page(numa_node_id(), 1);
147*4882a593Smuzhiyun if (maddr == 0)
148*4882a593Smuzhiyun return VM_FAULT_OOM;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun spin_lock(&vdata->lock);
151*4882a593Smuzhiyun if (vdata->maddr[index] == 0) {
152*4882a593Smuzhiyun vdata->count++;
153*4882a593Smuzhiyun vdata->maddr[index] = maddr;
154*4882a593Smuzhiyun } else {
155*4882a593Smuzhiyun uncached_free_page(maddr, 1);
156*4882a593Smuzhiyun maddr = vdata->maddr[index];
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun spin_unlock(&vdata->lock);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun paddr = maddr & ~__IA64_UNCACHED_OFFSET;
162*4882a593Smuzhiyun pfn = paddr >> PAGE_SHIFT;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun static const struct vm_operations_struct mspec_vm_ops = {
168*4882a593Smuzhiyun .open = mspec_open,
169*4882a593Smuzhiyun .close = mspec_close,
170*4882a593Smuzhiyun .fault = mspec_fault,
171*4882a593Smuzhiyun };
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /*
174*4882a593Smuzhiyun * mspec_mmap
175*4882a593Smuzhiyun *
176*4882a593Smuzhiyun * Called when mmapping the device. Initializes the vma with a fault handler
177*4882a593Smuzhiyun * and private data structure necessary to allocate, track, and free the
178*4882a593Smuzhiyun * underlying pages.
179*4882a593Smuzhiyun */
180*4882a593Smuzhiyun static int
mspec_mmap(struct file * file,struct vm_area_struct * vma,enum mspec_page_type type)181*4882a593Smuzhiyun mspec_mmap(struct file *file, struct vm_area_struct *vma,
182*4882a593Smuzhiyun enum mspec_page_type type)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun struct vma_data *vdata;
185*4882a593Smuzhiyun int pages, vdata_size;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun if (vma->vm_pgoff != 0)
188*4882a593Smuzhiyun return -EINVAL;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun if ((vma->vm_flags & VM_SHARED) == 0)
191*4882a593Smuzhiyun return -EINVAL;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun if ((vma->vm_flags & VM_WRITE) == 0)
194*4882a593Smuzhiyun return -EPERM;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun pages = vma_pages(vma);
197*4882a593Smuzhiyun vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
198*4882a593Smuzhiyun vdata = kvzalloc(vdata_size, GFP_KERNEL);
199*4882a593Smuzhiyun if (!vdata)
200*4882a593Smuzhiyun return -ENOMEM;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun vdata->vm_start = vma->vm_start;
203*4882a593Smuzhiyun vdata->vm_end = vma->vm_end;
204*4882a593Smuzhiyun vdata->type = type;
205*4882a593Smuzhiyun spin_lock_init(&vdata->lock);
206*4882a593Smuzhiyun refcount_set(&vdata->refcnt, 1);
207*4882a593Smuzhiyun vma->vm_private_data = vdata;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
210*4882a593Smuzhiyun if (vdata->type == MSPEC_UNCACHED)
211*4882a593Smuzhiyun vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
212*4882a593Smuzhiyun vma->vm_ops = &mspec_vm_ops;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun return 0;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun static int
cached_mmap(struct file * file,struct vm_area_struct * vma)218*4882a593Smuzhiyun cached_mmap(struct file *file, struct vm_area_struct *vma)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun return mspec_mmap(file, vma, MSPEC_CACHED);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun static int
uncached_mmap(struct file * file,struct vm_area_struct * vma)224*4882a593Smuzhiyun uncached_mmap(struct file *file, struct vm_area_struct *vma)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun return mspec_mmap(file, vma, MSPEC_UNCACHED);
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun static const struct file_operations cached_fops = {
230*4882a593Smuzhiyun .owner = THIS_MODULE,
231*4882a593Smuzhiyun .mmap = cached_mmap,
232*4882a593Smuzhiyun .llseek = noop_llseek,
233*4882a593Smuzhiyun };
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun static struct miscdevice cached_miscdev = {
236*4882a593Smuzhiyun .minor = MISC_DYNAMIC_MINOR,
237*4882a593Smuzhiyun .name = "mspec_cached",
238*4882a593Smuzhiyun .fops = &cached_fops
239*4882a593Smuzhiyun };
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun static const struct file_operations uncached_fops = {
242*4882a593Smuzhiyun .owner = THIS_MODULE,
243*4882a593Smuzhiyun .mmap = uncached_mmap,
244*4882a593Smuzhiyun .llseek = noop_llseek,
245*4882a593Smuzhiyun };
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun static struct miscdevice uncached_miscdev = {
248*4882a593Smuzhiyun .minor = MISC_DYNAMIC_MINOR,
249*4882a593Smuzhiyun .name = "mspec_uncached",
250*4882a593Smuzhiyun .fops = &uncached_fops
251*4882a593Smuzhiyun };
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun /*
254*4882a593Smuzhiyun * mspec_init
255*4882a593Smuzhiyun *
256*4882a593Smuzhiyun * Called at boot time to initialize the mspec facility.
257*4882a593Smuzhiyun */
258*4882a593Smuzhiyun static int __init
mspec_init(void)259*4882a593Smuzhiyun mspec_init(void)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun int ret;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun ret = misc_register(&cached_miscdev);
264*4882a593Smuzhiyun if (ret) {
265*4882a593Smuzhiyun printk(KERN_ERR "%s: failed to register device %i\n",
266*4882a593Smuzhiyun CACHED_ID, ret);
267*4882a593Smuzhiyun return ret;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun ret = misc_register(&uncached_miscdev);
270*4882a593Smuzhiyun if (ret) {
271*4882a593Smuzhiyun printk(KERN_ERR "%s: failed to register device %i\n",
272*4882a593Smuzhiyun UNCACHED_ID, ret);
273*4882a593Smuzhiyun misc_deregister(&cached_miscdev);
274*4882a593Smuzhiyun return ret;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun printk(KERN_INFO "%s %s initialized devices: %s %s\n",
278*4882a593Smuzhiyun MSPEC_BASENAME, REVISION, CACHED_ID, UNCACHED_ID);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun return 0;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun static void __exit
mspec_exit(void)284*4882a593Smuzhiyun mspec_exit(void)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun misc_deregister(&uncached_miscdev);
287*4882a593Smuzhiyun misc_deregister(&cached_miscdev);
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun module_init(mspec_init);
291*4882a593Smuzhiyun module_exit(mspec_exit);
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
294*4882a593Smuzhiyun MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
295*4882a593Smuzhiyun MODULE_LICENSE("GPL");
296