blob: 7d583222e8fafb9825f9e934696028c2c5183f3a [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>
Jes Sorensen17a3b052006-09-27 01:50:11 -070042#include <asm/pgtable.h>
Arun Sharma600634972011-07-26 16:09:06 -070043#include <linux/atomic.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070044#include <asm/tlbflush.h>
45#include <asm/uncached.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070046
47
Jes Sorensen17a3b052006-09-27 01:50:11 -070048#define CACHED_ID "Cached,"
49#define UNCACHED_ID "Uncached"
50#define REVISION "4.0"
51#define MSPEC_BASENAME "mspec"
52
53/*
54 * Page types allocated by the device.
55 */
Cliff Wickman4191ba22007-09-18 22:46:31 -070056enum mspec_page_type {
Christoph Hellwig0fef2532019-08-13 09:24:55 +020057 MSPEC_CACHED = 2,
Jes Sorensen17a3b052006-09-27 01:50:11 -070058 MSPEC_UNCACHED
59};
60
Jes Sorensen17a3b052006-09-27 01:50:11 -070061/*
62 * One of these structures is allocated when an mspec region is mmaped. The
63 * structure is pointed to by the vma->vm_private_data field in the vma struct.
64 * This structure is used to record the addresses of the mspec pages.
Cliff Wickman4191ba22007-09-18 22:46:31 -070065 * This structure is shared by all vma's that are split off from the
66 * original vma when split_vma()'s are done.
67 *
68 * The refcnt is incremented atomically because mm->mmap_sem does not
69 * protect in fork case where multiple tasks share the vma_data.
Jes Sorensen17a3b052006-09-27 01:50:11 -070070 */
71struct vma_data {
Elena Reshetovaf7d88d22017-03-06 16:20:50 +020072 refcount_t refcnt; /* Number of vmas sharing the data. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070073 spinlock_t lock; /* Serialize access to this structure. */
Jes Sorensen17a3b052006-09-27 01:50:11 -070074 int count; /* Number of pages allocated. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070075 enum mspec_page_type type; /* Type of pages allocated. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070076 unsigned long vm_start; /* Original (unsplit) base. */
77 unsigned long vm_end; /* Original (unsplit) end. */
Gustavo A. R. Silva3c2faf62020-02-27 12:48:08 -060078 unsigned long maddr[]; /* Array of MSPEC addresses. */
Jes Sorensen17a3b052006-09-27 01:50:11 -070079};
80
Jes Sorensen17a3b052006-09-27 01:50:11 -070081/*
82 * mspec_open
83 *
84 * Called when a device mapping is created by a means other than mmap
Cliff Wickman4191ba22007-09-18 22:46:31 -070085 * (via fork, munmap, etc.). Increments the reference count on the
86 * underlying mspec data so it is not freed prematurely.
Jes Sorensen17a3b052006-09-27 01:50:11 -070087 */
88static void
89mspec_open(struct vm_area_struct *vma)
90{
91 struct vma_data *vdata;
92
93 vdata = vma->vm_private_data;
Elena Reshetovaf7d88d22017-03-06 16:20:50 +020094 refcount_inc(&vdata->refcnt);
Jes Sorensen17a3b052006-09-27 01:50:11 -070095}
96
97/*
98 * mspec_close
99 *
100 * Called when unmapping a device mapping. Frees all mspec pages
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700101 * belonging to all the vma's sharing this vma_data structure.
Jes Sorensen17a3b052006-09-27 01:50:11 -0700102 */
103static void
104mspec_close(struct vm_area_struct *vma)
105{
106 struct vma_data *vdata;
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700107 int index, last_index;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700108 unsigned long my_page;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700109
110 vdata = vma->vm_private_data;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700111
Elena Reshetovaf7d88d22017-03-06 16:20:50 +0200112 if (!refcount_dec_and_test(&vdata->refcnt))
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700113 return;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700114
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700115 last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
116 for (index = 0; index < last_index; index++) {
Cliff Wickman4191ba22007-09-18 22:46:31 -0700117 if (vdata->maddr[index] == 0)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700118 continue;
119 /*
120 * Clear the page before sticking it back
121 * into the pool.
122 */
Cliff Wickman4191ba22007-09-18 22:46:31 -0700123 my_page = vdata->maddr[index];
124 vdata->maddr[index] = 0;
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200125 memset((char *)my_page, 0, PAGE_SIZE);
126 uncached_free_page(my_page, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700127 }
Cliff Wickman4191ba22007-09-18 22:46:31 -0700128
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800129 kvfree(vdata);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700130}
131
Jes Sorensen17a3b052006-09-27 01:50:11 -0700132/*
Nick Pigginefe9e772008-07-23 21:27:00 -0700133 * mspec_fault
Jes Sorensen17a3b052006-09-27 01:50:11 -0700134 *
135 * Creates a mspec page and maps it to user space.
136 */
Souptick Joarder3eb87d42018-04-16 19:56:09 +0530137static vm_fault_t
Dave Jiang11bac802017-02-24 14:56:41 -0800138mspec_fault(struct vm_fault *vmf)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700139{
140 unsigned long paddr, maddr;
141 unsigned long pfn;
Nick Pigginefe9e772008-07-23 21:27:00 -0700142 pgoff_t index = vmf->pgoff;
Dave Jiang11bac802017-02-24 14:56:41 -0800143 struct vma_data *vdata = vmf->vma->vm_private_data;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700144
Jes Sorensen17a3b052006-09-27 01:50:11 -0700145 maddr = (volatile unsigned long) vdata->maddr[index];
146 if (maddr == 0) {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500147 maddr = uncached_alloc_page(numa_node_id(), 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700148 if (maddr == 0)
Nick Pigginefe9e772008-07-23 21:27:00 -0700149 return VM_FAULT_OOM;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700150
151 spin_lock(&vdata->lock);
152 if (vdata->maddr[index] == 0) {
153 vdata->count++;
154 vdata->maddr[index] = maddr;
155 } else {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500156 uncached_free_page(maddr, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700157 maddr = vdata->maddr[index];
158 }
159 spin_unlock(&vdata->lock);
160 }
161
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200162 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700163 pfn = paddr >> PAGE_SHIFT;
164
Souptick Joarder3eb87d42018-04-16 19:56:09 +0530165 return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700166}
167
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400168static const struct vm_operations_struct mspec_vm_ops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700169 .open = mspec_open,
170 .close = mspec_close,
Nick Pigginefe9e772008-07-23 21:27:00 -0700171 .fault = mspec_fault,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700172};
173
174/*
175 * mspec_mmap
176 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200177 * Called when mmapping the device. Initializes the vma with a fault handler
Jes Sorensen17a3b052006-09-27 01:50:11 -0700178 * and private data structure necessary to allocate, track, and free the
179 * underlying pages.
180 */
181static int
Cliff Wickman4191ba22007-09-18 22:46:31 -0700182mspec_mmap(struct file *file, struct vm_area_struct *vma,
183 enum mspec_page_type type)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700184{
185 struct vma_data *vdata;
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800186 int pages, vdata_size;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700187
188 if (vma->vm_pgoff != 0)
189 return -EINVAL;
190
191 if ((vma->vm_flags & VM_SHARED) == 0)
192 return -EINVAL;
193
194 if ((vma->vm_flags & VM_WRITE) == 0)
195 return -EPERM;
196
Libina0ea59d2013-05-13 10:17:39 +0800197 pages = vma_pages(vma);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700198 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
199 if (vdata_size <= PAGE_SIZE)
Rakib Mullick658c74c2011-05-26 16:25:56 -0700200 vdata = kzalloc(vdata_size, GFP_KERNEL);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800201 else
Rakib Mullick658c74c2011-05-26 16:25:56 -0700202 vdata = vzalloc(vdata_size);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700203 if (!vdata)
204 return -ENOMEM;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700205
Cliff Wickman4191ba22007-09-18 22:46:31 -0700206 vdata->vm_start = vma->vm_start;
207 vdata->vm_end = vma->vm_end;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700208 vdata->type = type;
209 spin_lock_init(&vdata->lock);
Elena Reshetovaf7d88d22017-03-06 16:20:50 +0200210 refcount_set(&vdata->refcnt, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700211 vma->vm_private_data = vdata;
212
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700213 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200214 if (vdata->type == MSPEC_UNCACHED)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700215 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
216 vma->vm_ops = &mspec_vm_ops;
217
218 return 0;
219}
220
221static int
Jes Sorensen17a3b052006-09-27 01:50:11 -0700222cached_mmap(struct file *file, struct vm_area_struct *vma)
223{
224 return mspec_mmap(file, vma, MSPEC_CACHED);
225}
226
227static int
228uncached_mmap(struct file *file, struct vm_area_struct *vma)
229{
230 return mspec_mmap(file, vma, MSPEC_UNCACHED);
231}
232
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800233static const struct file_operations cached_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700234 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200235 .mmap = cached_mmap,
236 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700237};
238
239static struct miscdevice cached_miscdev = {
240 .minor = MISC_DYNAMIC_MINOR,
241 .name = "mspec_cached",
242 .fops = &cached_fops
243};
244
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800245static const struct file_operations uncached_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700246 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200247 .mmap = uncached_mmap,
248 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700249};
250
251static struct miscdevice uncached_miscdev = {
252 .minor = MISC_DYNAMIC_MINOR,
253 .name = "mspec_uncached",
254 .fops = &uncached_fops
255};
256
257/*
258 * mspec_init
259 *
260 * Called at boot time to initialize the mspec facility.
261 */
262static int __init
263mspec_init(void)
264{
265 int ret;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700266
Jes Sorensen17a3b052006-09-27 01:50:11 -0700267 ret = misc_register(&cached_miscdev);
268 if (ret) {
269 printk(KERN_ERR "%s: failed to register device %i\n",
270 CACHED_ID, ret);
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200271 return ret;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700272 }
273 ret = misc_register(&uncached_miscdev);
274 if (ret) {
275 printk(KERN_ERR "%s: failed to register device %i\n",
276 UNCACHED_ID, ret);
277 misc_deregister(&cached_miscdev);
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200278 return ret;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700279 }
280
Christoph Hellwig0fef2532019-08-13 09:24:55 +0200281 printk(KERN_INFO "%s %s initialized devices: %s %s\n",
282 MSPEC_BASENAME, REVISION, CACHED_ID, UNCACHED_ID);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700283
284 return 0;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700285}
286
287static void __exit
288mspec_exit(void)
289{
Jes Sorensen17a3b052006-09-27 01:50:11 -0700290 misc_deregister(&uncached_miscdev);
291 misc_deregister(&cached_miscdev);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700292}
293
294module_init(mspec_init);
295module_exit(mspec_exit);
296
297MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
298MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
299MODULE_LICENSE("GPL");