blob: f8231e2e84beccec68c0440e7068907aee261a42 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Jes Sorensen17a3b052006-09-27 01:50:11 -07002/*
3 * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights
4 * reserved.
Jes Sorensen17a3b052006-09-27 01:50:11 -07005 */
6
7/*
8 * SN Platform Special Memory (mspec) Support
9 *
10 * This driver exports the SN special memory (mspec) facility to user
11 * processes.
Christoph Hellwig0fef2532019-08-13 09:24:55 +020012 * There are two types of memory made available thru this driver:
13 * uncached and cached.
Jes Sorensen17a3b052006-09-27 01:50:11 -070014 *
15 * Uncached are used for memory write combining feature of the ia64
16 * cpu.
17 *
18 * Cached are used for areas of memory that are used as cached addresses
19 * on our partition and used as uncached addresses from other partitions.
20 * Due to a design constraint of the SN2 Shub, you can not have processors
21 * on the same FSB perform both a cached and uncached reference to the
22 * same cache line. These special memory cached regions prevent the
23 * kernel from ever dropping in a TLB entry and therefore prevent the
24 * processor from ever speculating a cache line from this page.
25 */
26
Jes Sorensen17a3b052006-09-27 01:50:11 -070027#include <linux/types.h>
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/errno.h>
32#include <linux/miscdevice.h>
33#include <linux/spinlock.h>
34#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040035#include <linux/fs.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070036#include <linux/vmalloc.h>
37#include <linux/string.h>
38#include <linux/slab.h>
39#include <linux/numa.h>
Elena Reshetovaf7d88d22017-03-06 16:20:50 +020040#include <linux/refcount.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070041#include <asm/page.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070043#include <asm/tlbflush.h>
44#include <asm/uncached.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070045
46
Jes Sorensen17a3b052006-09-27 01:50:11 -070047#define CACHED_ID "Cached,"
48#define UNCACHED_ID "Uncached"
49#define REVISION "4.0"
50#define MSPEC_BASENAME "mspec"
51
52/*
53 * Page types allocated by the device.
54 */
Cliff Wickman4191ba22007-09-18 22:46:31 -070055enum mspec_page_type {
Christoph Hellwig0fef2532019-08-13 09:24:55 +020056 MSPEC_CACHED = 2,
Jes Sorensen17a3b052006-09-27 01:50:11 -070057 MSPEC_UNCACHED
58};
59
Jes Sorensen17a3b052006-09-27 01:50:11 -070060/*
61 * One of these structures is allocated when an mspec region is mmaped. The
62 * structure is pointed to by the vma->vm_private_data field in the vma struct.
63 * This structure is used to record the addresses of the mspec pages.
Cliff Wickman4191ba22007-09-18 22:46:31 -070064 * This structure is shared by all vma's that are split off from the
65 * original vma when split_vma()'s are done.
66 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070067 * The refcnt is incremented atomically because mm->mmap_lock does not
Cliff Wickman4191ba22007-09-18 22:46:31 -070068 * protect in fork case where multiple tasks share the vma_data.
Jes Sorensen17a3b052006-09-27 01:50:11 -070069 */
70struct vma_data {
Elena Reshetovaf7d88d22017-03-06 16:20:50 +020071 refcount_t refcnt; /* Number of vmas sharing the data. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070072 spinlock_t lock; /* Serialize access to this structure. */
Jes Sorensen17a3b052006-09-27 01:50:11 -070073 int count; /* Number of pages allocated. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070074 enum mspec_page_type type; /* Type of pages allocated. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070075 unsigned long vm_start; /* Original (unsplit) base. */
76 unsigned long vm_end; /* Original (unsplit) end. */
Gustavo A. R. Silva3c2faf62020-02-27 12:48:08 -060077 unsigned long maddr[]; /* Array of MSPEC addresses. */
Jes Sorensen17a3b052006-09-27 01:50:11 -070078};
79
Jes Sorensen17a3b052006-09-27 01:50:11 -070080/*
81 * mspec_open
82 *
83 * Called when a device mapping is created by a means other than mmap
Cliff Wickman4191ba22007-09-18 22:46:31 -070084 * (via fork, munmap, etc.). Increments the reference count on the
85 * underlying mspec data so it is not freed prematurely.
Jes Sorensen17a3b052006-09-27 01:50:11 -070086 */
87static void
88mspec_open(struct vm_area_struct *vma)
89{
90 struct vma_data *vdata;
91
92 vdata = vma->vm_private_data;
Elena Reshetovaf7d88d22017-03-06 16:20:50 +020093 refcount_inc(&vdata->refcnt);
Jes Sorensen17a3b052006-09-27 01:50:11 -070094}
95
96/*
97 * mspec_close
98 *
99 * Called when unmapping a device mapping. Frees all mspec pages
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700100 * belonging to all the vma's sharing this vma_data structure.
Jes Sorensen17a3b052006-09-27 01:50:11 -0700101 */
102static void
103mspec_close(struct vm_area_struct *vma)
104{
105 struct vma_data *vdata;
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700106 int index, last_index;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700107 unsigned long my_page;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700108
109 vdata = vma->vm_private_data;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700110
Elena Reshetovaf7d88d22017-03-06 16:20:50 +0200111 if (!refcount_dec_and_test(&vdata->refcnt))
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700112 return;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700113
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700114 last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
115 for (index = 0; index < last_index; index++) {
Cliff Wickman4191ba22007-09-18 22:46:31 -0700116 if (vdata->maddr[index] == 0)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700117 continue;
118 /*
119 * Clear the page before sticking it back
120 * into the pool.
121 */
Cliff Wickman4191ba22007-09-18 22:46:31 -0700122 my_page = vdata->maddr[index];
123 vdata->maddr[index] = 0;
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200124 memset((char *)my_page, 0, PAGE_SIZE);
125 uncached_free_page(my_page, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700126 }
Cliff Wickman4191ba22007-09-18 22:46:31 -0700127
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800128 kvfree(vdata);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700129}
130
Jes Sorensen17a3b052006-09-27 01:50:11 -0700131/*
Nick Pigginefe9e772008-07-23 21:27:00 -0700132 * mspec_fault
Jes Sorensen17a3b052006-09-27 01:50:11 -0700133 *
134 * Creates a mspec page and maps it to user space.
135 */
Souptick Joarder3eb87d42018-04-16 19:56:09 +0530136static vm_fault_t
Dave Jiang11bac802017-02-24 14:56:41 -0800137mspec_fault(struct vm_fault *vmf)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700138{
139 unsigned long paddr, maddr;
140 unsigned long pfn;
Nick Pigginefe9e772008-07-23 21:27:00 -0700141 pgoff_t index = vmf->pgoff;
Dave Jiang11bac802017-02-24 14:56:41 -0800142 struct vma_data *vdata = vmf->vma->vm_private_data;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700143
Jes Sorensen17a3b052006-09-27 01:50:11 -0700144 maddr = (volatile unsigned long) vdata->maddr[index];
145 if (maddr == 0) {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500146 maddr = uncached_alloc_page(numa_node_id(), 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700147 if (maddr == 0)
Nick Pigginefe9e772008-07-23 21:27:00 -0700148 return VM_FAULT_OOM;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700149
150 spin_lock(&vdata->lock);
151 if (vdata->maddr[index] == 0) {
152 vdata->count++;
153 vdata->maddr[index] = maddr;
154 } else {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500155 uncached_free_page(maddr, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700156 maddr = vdata->maddr[index];
157 }
158 spin_unlock(&vdata->lock);
159 }
160
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200161 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700162 pfn = paddr >> PAGE_SHIFT;
163
Souptick Joarder3eb87d42018-04-16 19:56:09 +0530164 return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700165}
166
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400167static const struct vm_operations_struct mspec_vm_ops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700168 .open = mspec_open,
169 .close = mspec_close,
Nick Pigginefe9e772008-07-23 21:27:00 -0700170 .fault = mspec_fault,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700171};
172
173/*
174 * mspec_mmap
175 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200176 * Called when mmapping the device. Initializes the vma with a fault handler
Jes Sorensen17a3b052006-09-27 01:50:11 -0700177 * and private data structure necessary to allocate, track, and free the
178 * underlying pages.
179 */
180static int
Cliff Wickman4191ba22007-09-18 22:46:31 -0700181mspec_mmap(struct file *file, struct vm_area_struct *vma,
182 enum mspec_page_type type)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700183{
184 struct vma_data *vdata;
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800185 int pages, vdata_size;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700186
187 if (vma->vm_pgoff != 0)
188 return -EINVAL;
189
190 if ((vma->vm_flags & VM_SHARED) == 0)
191 return -EINVAL;
192
193 if ((vma->vm_flags & VM_WRITE) == 0)
194 return -EPERM;
195
Libina0ea59d2013-05-13 10:17:39 +0800196 pages = vma_pages(vma);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700197 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
Denis Efremovfe697952020-08-28 00:34:21 +0300198 vdata = kvzalloc(vdata_size, GFP_KERNEL);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700199 if (!vdata)
200 return -ENOMEM;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700201
Cliff Wickman4191ba22007-09-18 22:46:31 -0700202 vdata->vm_start = vma->vm_start;
203 vdata->vm_end = vma->vm_end;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700204 vdata->type = type;
205 spin_lock_init(&vdata->lock);
Elena Reshetovaf7d88d22017-03-06 16:20:50 +0200206 refcount_set(&vdata->refcnt, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700207 vma->vm_private_data = vdata;
208
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700209 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200210 if (vdata->type == MSPEC_UNCACHED)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700211 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
212 vma->vm_ops = &mspec_vm_ops;
213
214 return 0;
215}
216
217static int
Jes Sorensen17a3b052006-09-27 01:50:11 -0700218cached_mmap(struct file *file, struct vm_area_struct *vma)
219{
220 return mspec_mmap(file, vma, MSPEC_CACHED);
221}
222
223static int
224uncached_mmap(struct file *file, struct vm_area_struct *vma)
225{
226 return mspec_mmap(file, vma, MSPEC_UNCACHED);
227}
228
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800229static const struct file_operations cached_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700230 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200231 .mmap = cached_mmap,
232 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700233};
234
235static struct miscdevice cached_miscdev = {
236 .minor = MISC_DYNAMIC_MINOR,
237 .name = "mspec_cached",
238 .fops = &cached_fops
239};
240
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800241static const struct file_operations uncached_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700242 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200243 .mmap = uncached_mmap,
244 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700245};
246
247static struct miscdevice uncached_miscdev = {
248 .minor = MISC_DYNAMIC_MINOR,
249 .name = "mspec_uncached",
250 .fops = &uncached_fops
251};
252
253/*
254 * mspec_init
255 *
256 * Called at boot time to initialize the mspec facility.
257 */
258static int __init
259mspec_init(void)
260{
261 int ret;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700262
Jes Sorensen17a3b052006-09-27 01:50:11 -0700263 ret = misc_register(&cached_miscdev);
264 if (ret) {
265 printk(KERN_ERR "%s: failed to register device %i\n",
266 CACHED_ID, ret);
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200267 return ret;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700268 }
269 ret = misc_register(&uncached_miscdev);
270 if (ret) {
271 printk(KERN_ERR "%s: failed to register device %i\n",
272 UNCACHED_ID, ret);
273 misc_deregister(&cached_miscdev);
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200274 return ret;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700275 }
276
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200277 printk(KERN_INFO "%s %s initialized devices: %s %s\n",
278 MSPEC_BASENAME, REVISION, CACHED_ID, UNCACHED_ID);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700279
280 return 0;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700281}
282
283static void __exit
284mspec_exit(void)
285{
Jes Sorensen17a3b052006-09-27 01:50:11 -0700286 misc_deregister(&uncached_miscdev);
287 misc_deregister(&cached_miscdev);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700288}
289
290module_init(mspec_init);
291module_exit(mspec_exit);
292
293MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
294MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
295MODULE_LICENSE("GPL");