blob: e75c9df7c2d80b8404a335caf1363174a28bf36b [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.
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
Jes Sorensen17a3b052006-09-27 01:50:11 -070030#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>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040038#include <linux/fs.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070039#include <linux/vmalloc.h>
40#include <linux/string.h>
41#include <linux/slab.h>
42#include <linux/numa.h>
Elena Reshetovaf7d88d22017-03-06 16:20:50 +020043#include <linux/refcount.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070044#include <asm/page.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070045#include <asm/pgtable.h>
Arun Sharma600634972011-07-26 16:09:06 -070046#include <linux/atomic.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070047#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 */
Cliff Wickman4191ba22007-09-18 22:46:31 -070067enum mspec_page_type {
Jes Sorensen17a3b052006-09-27 01:50:11 -070068 MSPEC_FETCHOP = 1,
69 MSPEC_CACHED,
70 MSPEC_UNCACHED
71};
72
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -080073#ifdef CONFIG_SGI_SN
Jes Sorensen17a3b052006-09-27 01:50:11 -070074static int is_sn2;
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -080075#else
76#define is_sn2 0
77#endif
Jes Sorensen17a3b052006-09-27 01:50:11 -070078
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.
Cliff Wickman4191ba22007-09-18 22:46:31 -070083 * 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.
Jes Sorensen17a3b052006-09-27 01:50:11 -070088 */
89struct vma_data {
Elena Reshetovaf7d88d22017-03-06 16:20:50 +020090 refcount_t refcnt; /* Number of vmas sharing the data. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070091 spinlock_t lock; /* Serialize access to this structure. */
Jes Sorensen17a3b052006-09-27 01:50:11 -070092 int count; /* Number of pages allocated. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070093 enum mspec_page_type type; /* Type of pages allocated. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070094 unsigned long vm_start; /* Original (unsplit) base. */
95 unsigned long vm_end; /* Original (unsplit) end. */
Jes Sorensen17a3b052006-09-27 01:50:11 -070096 unsigned long maddr[0]; /* Array of MSPEC addresses. */
97};
98
99/* used on shub2 to clear FOP cache in the HUB */
100static unsigned long scratch_page[MAX_NUMNODES];
101#define SH2_AMO_CACHE_ENTRIES 4
102
103static inline int
104mspec_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
Cliff Wickman4191ba22007-09-18 22:46:31 -0700136 * (via fork, munmap, etc.). Increments the reference count on the
137 * underlying mspec data so it is not freed prematurely.
Jes Sorensen17a3b052006-09-27 01:50:11 -0700138 */
139static void
140mspec_open(struct vm_area_struct *vma)
141{
142 struct vma_data *vdata;
143
144 vdata = vma->vm_private_data;
Elena Reshetovaf7d88d22017-03-06 16:20:50 +0200145 refcount_inc(&vdata->refcnt);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700146}
147
148/*
149 * mspec_close
150 *
151 * Called when unmapping a device mapping. Frees all mspec pages
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700152 * belonging to all the vma's sharing this vma_data structure.
Jes Sorensen17a3b052006-09-27 01:50:11 -0700153 */
154static void
155mspec_close(struct vm_area_struct *vma)
156{
157 struct vma_data *vdata;
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700158 int index, last_index;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700159 unsigned long my_page;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700160
161 vdata = vma->vm_private_data;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700162
Elena Reshetovaf7d88d22017-03-06 16:20:50 +0200163 if (!refcount_dec_and_test(&vdata->refcnt))
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700164 return;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700165
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700166 last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
167 for (index = 0; index < last_index; index++) {
Cliff Wickman4191ba22007-09-18 22:46:31 -0700168 if (vdata->maddr[index] == 0)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700169 continue;
170 /*
171 * Clear the page before sticking it back
172 * into the pool.
173 */
Cliff Wickman4191ba22007-09-18 22:46:31 -0700174 my_page = vdata->maddr[index];
175 vdata->maddr[index] = 0;
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700176 if (!mspec_zero_block(my_page, PAGE_SIZE))
Dean Nelsone4a064d2008-04-25 15:22:19 -0500177 uncached_free_page(my_page, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700178 else
179 printk(KERN_WARNING "mspec_close(): "
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700180 "failed to zero page %ld\n", my_page);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700181 }
Cliff Wickman4191ba22007-09-18 22:46:31 -0700182
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800183 kvfree(vdata);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700184}
185
Jes Sorensen17a3b052006-09-27 01:50:11 -0700186/*
Nick Pigginefe9e772008-07-23 21:27:00 -0700187 * mspec_fault
Jes Sorensen17a3b052006-09-27 01:50:11 -0700188 *
189 * Creates a mspec page and maps it to user space.
190 */
Souptick Joarder3eb87d42018-04-16 19:56:09 +0530191static vm_fault_t
Dave Jiang11bac802017-02-24 14:56:41 -0800192mspec_fault(struct vm_fault *vmf)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700193{
194 unsigned long paddr, maddr;
195 unsigned long pfn;
Nick Pigginefe9e772008-07-23 21:27:00 -0700196 pgoff_t index = vmf->pgoff;
Dave Jiang11bac802017-02-24 14:56:41 -0800197 struct vma_data *vdata = vmf->vma->vm_private_data;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700198
Jes Sorensen17a3b052006-09-27 01:50:11 -0700199 maddr = (volatile unsigned long) vdata->maddr[index];
200 if (maddr == 0) {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500201 maddr = uncached_alloc_page(numa_node_id(), 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700202 if (maddr == 0)
Nick Pigginefe9e772008-07-23 21:27:00 -0700203 return VM_FAULT_OOM;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700204
205 spin_lock(&vdata->lock);
206 if (vdata->maddr[index] == 0) {
207 vdata->count++;
208 vdata->maddr[index] = maddr;
209 } else {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500210 uncached_free_page(maddr, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700211 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
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -0800219 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700220
221 pfn = paddr >> PAGE_SHIFT;
222
Souptick Joarder3eb87d42018-04-16 19:56:09 +0530223 return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700224}
225
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400226static const struct vm_operations_struct mspec_vm_ops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700227 .open = mspec_open,
228 .close = mspec_close,
Nick Pigginefe9e772008-07-23 21:27:00 -0700229 .fault = mspec_fault,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700230};
231
232/*
233 * mspec_mmap
234 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200235 * Called when mmapping the device. Initializes the vma with a fault handler
Jes Sorensen17a3b052006-09-27 01:50:11 -0700236 * and private data structure necessary to allocate, track, and free the
237 * underlying pages.
238 */
239static int
Cliff Wickman4191ba22007-09-18 22:46:31 -0700240mspec_mmap(struct file *file, struct vm_area_struct *vma,
241 enum mspec_page_type type)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700242{
243 struct vma_data *vdata;
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800244 int pages, vdata_size;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700245
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
Libina0ea59d2013-05-13 10:17:39 +0800255 pages = vma_pages(vma);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700256 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
257 if (vdata_size <= PAGE_SIZE)
Rakib Mullick658c74c2011-05-26 16:25:56 -0700258 vdata = kzalloc(vdata_size, GFP_KERNEL);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800259 else
Rakib Mullick658c74c2011-05-26 16:25:56 -0700260 vdata = vzalloc(vdata_size);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700261 if (!vdata)
262 return -ENOMEM;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700263
Cliff Wickman4191ba22007-09-18 22:46:31 -0700264 vdata->vm_start = vma->vm_start;
265 vdata->vm_end = vma->vm_end;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700266 vdata->type = type;
267 spin_lock_init(&vdata->lock);
Elena Reshetovaf7d88d22017-03-06 16:20:50 +0200268 refcount_set(&vdata->refcnt, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700269 vma->vm_private_data = vdata;
270
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700271 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700272 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
279static int
280fetchop_mmap(struct file *file, struct vm_area_struct *vma)
281{
282 return mspec_mmap(file, vma, MSPEC_FETCHOP);
283}
284
285static int
286cached_mmap(struct file *file, struct vm_area_struct *vma)
287{
288 return mspec_mmap(file, vma, MSPEC_CACHED);
289}
290
291static int
292uncached_mmap(struct file *file, struct vm_area_struct *vma)
293{
294 return mspec_mmap(file, vma, MSPEC_UNCACHED);
295}
296
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800297static const struct file_operations fetchop_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700298 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200299 .mmap = fetchop_mmap,
300 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700301};
302
303static struct miscdevice fetchop_miscdev = {
304 .minor = MISC_DYNAMIC_MINOR,
305 .name = "sgi_fetchop",
306 .fops = &fetchop_fops
307};
308
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800309static const struct file_operations cached_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700310 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200311 .mmap = cached_mmap,
312 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700313};
314
315static struct miscdevice cached_miscdev = {
316 .minor = MISC_DYNAMIC_MINOR,
317 .name = "mspec_cached",
318 .fops = &cached_fops
319};
320
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800321static const struct file_operations uncached_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700322 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200323 .mmap = uncached_mmap,
324 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700325};
326
327static 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 */
338static int __init
339mspec_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 */
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -0800348#ifdef CONFIG_SGI_SN
Jes Sorensen17a3b052006-09-27 01:50:11 -0700349 if (ia64_platform_is("sn2")) {
350 is_sn2 = 1;
351 if (is_shub2()) {
352 ret = -ENOMEM;
Christoph Lameter2dca53a2007-10-16 01:25:33 -0700353 for_each_node_state(nid, N_ONLINE) {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700354 int actual_nid;
355 int nasid;
356 unsigned long phys;
357
Dean Nelsone4a064d2008-04-25 15:22:19 -0500358 scratch_page[nid] = uncached_alloc_page(nid, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700359 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 }
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -0800377#endif
Jes Sorensen17a3b052006-09-27 01:50:11 -0700378 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)
Dean Nelsone4a064d2008-04-25 15:22:19 -0500405 uncached_free_page(scratch_page[nid], 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700406 }
407 return ret;
408}
409
410static void __exit
411mspec_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)
Dean Nelsone4a064d2008-04-25 15:22:19 -0500422 uncached_free_page(scratch_page[nid], 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700423 }
424 }
425}
426
427module_init(mspec_init);
428module_exit(mspec_exit);
429
430MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
431MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
432MODULE_LICENSE("GPL");