blob: 058876b55b0974ea0220f03ae79c1b47c5cf857f [file] [log] [blame]
Jes Sorensen17a3b052006-09-27 01:50:11 -07001/*
2 * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights
3 * reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 */
9
10/*
11 * SN Platform Special Memory (mspec) Support
12 *
13 * This driver exports the SN special memory (mspec) facility to user
14 * processes.
15 * There are three types of memory made available thru this driver:
16 * fetchops, uncached and cached.
17 *
18 * Fetchops are atomic memory operations that are implemented in the
19 * memory controller on SGI SN hardware.
20 *
21 * Uncached are used for memory write combining feature of the ia64
22 * cpu.
23 *
24 * Cached are used for areas of memory that are used as cached addresses
25 * on our partition and used as uncached addresses from other partitions.
26 * Due to a design constraint of the SN2 Shub, you can not have processors
27 * on the same FSB perform both a cached and uncached reference to the
28 * same cache line. These special memory cached regions prevent the
29 * kernel from ever dropping in a TLB entry and therefore prevent the
30 * processor from ever speculating a cache line from this page.
31 */
32
Jes Sorensen17a3b052006-09-27 01:50:11 -070033#include <linux/types.h>
34#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/errno.h>
38#include <linux/miscdevice.h>
39#include <linux/spinlock.h>
40#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040041#include <linux/fs.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070042#include <linux/vmalloc.h>
43#include <linux/string.h>
44#include <linux/slab.h>
45#include <linux/numa.h>
Elena Reshetovaf7d88d22017-03-06 16:20:50 +020046#include <linux/refcount.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070047#include <asm/page.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070048#include <asm/pgtable.h>
Arun Sharma600634972011-07-26 16:09:06 -070049#include <linux/atomic.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070050#include <asm/tlbflush.h>
51#include <asm/uncached.h>
52#include <asm/sn/addrs.h>
53#include <asm/sn/arch.h>
54#include <asm/sn/mspec.h>
55#include <asm/sn/sn_cpuid.h>
56#include <asm/sn/io.h>
57#include <asm/sn/bte.h>
58#include <asm/sn/shubio.h>
59
60
61#define FETCHOP_ID "SGI Fetchop,"
62#define CACHED_ID "Cached,"
63#define UNCACHED_ID "Uncached"
64#define REVISION "4.0"
65#define MSPEC_BASENAME "mspec"
66
67/*
68 * Page types allocated by the device.
69 */
Cliff Wickman4191ba22007-09-18 22:46:31 -070070enum mspec_page_type {
Jes Sorensen17a3b052006-09-27 01:50:11 -070071 MSPEC_FETCHOP = 1,
72 MSPEC_CACHED,
73 MSPEC_UNCACHED
74};
75
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -080076#ifdef CONFIG_SGI_SN
Jes Sorensen17a3b052006-09-27 01:50:11 -070077static int is_sn2;
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -080078#else
79#define is_sn2 0
80#endif
Jes Sorensen17a3b052006-09-27 01:50:11 -070081
82/*
83 * One of these structures is allocated when an mspec region is mmaped. The
84 * structure is pointed to by the vma->vm_private_data field in the vma struct.
85 * This structure is used to record the addresses of the mspec pages.
Cliff Wickman4191ba22007-09-18 22:46:31 -070086 * This structure is shared by all vma's that are split off from the
87 * original vma when split_vma()'s are done.
88 *
89 * The refcnt is incremented atomically because mm->mmap_sem does not
90 * protect in fork case where multiple tasks share the vma_data.
Jes Sorensen17a3b052006-09-27 01:50:11 -070091 */
92struct vma_data {
Elena Reshetovaf7d88d22017-03-06 16:20:50 +020093 refcount_t refcnt; /* Number of vmas sharing the data. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070094 spinlock_t lock; /* Serialize access to this structure. */
Jes Sorensen17a3b052006-09-27 01:50:11 -070095 int count; /* Number of pages allocated. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070096 enum mspec_page_type type; /* Type of pages allocated. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070097 unsigned long vm_start; /* Original (unsplit) base. */
98 unsigned long vm_end; /* Original (unsplit) end. */
Jes Sorensen17a3b052006-09-27 01:50:11 -070099 unsigned long maddr[0]; /* Array of MSPEC addresses. */
100};
101
102/* used on shub2 to clear FOP cache in the HUB */
103static unsigned long scratch_page[MAX_NUMNODES];
104#define SH2_AMO_CACHE_ENTRIES 4
105
106static inline int
107mspec_zero_block(unsigned long addr, int len)
108{
109 int status;
110
111 if (is_sn2) {
112 if (is_shub2()) {
113 int nid;
114 void *p;
115 int i;
116
117 nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
118 p = (void *)TO_AMO(scratch_page[nid]);
119
120 for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
121 FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
122 p += FETCHOP_VAR_SIZE;
123 }
124 }
125
126 status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
127 BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
128 } else {
129 memset((char *) addr, 0, len);
130 status = 0;
131 }
132 return status;
133}
134
135/*
136 * mspec_open
137 *
138 * Called when a device mapping is created by a means other than mmap
Cliff Wickman4191ba22007-09-18 22:46:31 -0700139 * (via fork, munmap, etc.). Increments the reference count on the
140 * underlying mspec data so it is not freed prematurely.
Jes Sorensen17a3b052006-09-27 01:50:11 -0700141 */
142static void
143mspec_open(struct vm_area_struct *vma)
144{
145 struct vma_data *vdata;
146
147 vdata = vma->vm_private_data;
Elena Reshetovaf7d88d22017-03-06 16:20:50 +0200148 refcount_inc(&vdata->refcnt);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700149}
150
151/*
152 * mspec_close
153 *
154 * Called when unmapping a device mapping. Frees all mspec pages
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700155 * belonging to all the vma's sharing this vma_data structure.
Jes Sorensen17a3b052006-09-27 01:50:11 -0700156 */
157static void
158mspec_close(struct vm_area_struct *vma)
159{
160 struct vma_data *vdata;
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700161 int index, last_index;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700162 unsigned long my_page;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700163
164 vdata = vma->vm_private_data;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700165
Elena Reshetovaf7d88d22017-03-06 16:20:50 +0200166 if (!refcount_dec_and_test(&vdata->refcnt))
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700167 return;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700168
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700169 last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
170 for (index = 0; index < last_index; index++) {
Cliff Wickman4191ba22007-09-18 22:46:31 -0700171 if (vdata->maddr[index] == 0)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700172 continue;
173 /*
174 * Clear the page before sticking it back
175 * into the pool.
176 */
Cliff Wickman4191ba22007-09-18 22:46:31 -0700177 my_page = vdata->maddr[index];
178 vdata->maddr[index] = 0;
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700179 if (!mspec_zero_block(my_page, PAGE_SIZE))
Dean Nelsone4a064d2008-04-25 15:22:19 -0500180 uncached_free_page(my_page, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700181 else
182 printk(KERN_WARNING "mspec_close(): "
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700183 "failed to zero page %ld\n", my_page);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700184 }
Cliff Wickman4191ba22007-09-18 22:46:31 -0700185
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800186 kvfree(vdata);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700187}
188
Jes Sorensen17a3b052006-09-27 01:50:11 -0700189/*
Nick Pigginefe9e772008-07-23 21:27:00 -0700190 * mspec_fault
Jes Sorensen17a3b052006-09-27 01:50:11 -0700191 *
192 * Creates a mspec page and maps it to user space.
193 */
Souptick Joarder3eb87d42018-04-16 19:56:09 +0530194static vm_fault_t
Dave Jiang11bac802017-02-24 14:56:41 -0800195mspec_fault(struct vm_fault *vmf)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700196{
197 unsigned long paddr, maddr;
198 unsigned long pfn;
Nick Pigginefe9e772008-07-23 21:27:00 -0700199 pgoff_t index = vmf->pgoff;
Dave Jiang11bac802017-02-24 14:56:41 -0800200 struct vma_data *vdata = vmf->vma->vm_private_data;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700201
Jes Sorensen17a3b052006-09-27 01:50:11 -0700202 maddr = (volatile unsigned long) vdata->maddr[index];
203 if (maddr == 0) {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500204 maddr = uncached_alloc_page(numa_node_id(), 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700205 if (maddr == 0)
Nick Pigginefe9e772008-07-23 21:27:00 -0700206 return VM_FAULT_OOM;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700207
208 spin_lock(&vdata->lock);
209 if (vdata->maddr[index] == 0) {
210 vdata->count++;
211 vdata->maddr[index] = maddr;
212 } else {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500213 uncached_free_page(maddr, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700214 maddr = vdata->maddr[index];
215 }
216 spin_unlock(&vdata->lock);
217 }
218
219 if (vdata->type == MSPEC_FETCHOP)
220 paddr = TO_AMO(maddr);
221 else
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -0800222 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700223
224 pfn = paddr >> PAGE_SHIFT;
225
Souptick Joarder3eb87d42018-04-16 19:56:09 +0530226 return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700227}
228
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400229static const struct vm_operations_struct mspec_vm_ops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700230 .open = mspec_open,
231 .close = mspec_close,
Nick Pigginefe9e772008-07-23 21:27:00 -0700232 .fault = mspec_fault,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700233};
234
235/*
236 * mspec_mmap
237 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200238 * Called when mmapping the device. Initializes the vma with a fault handler
Jes Sorensen17a3b052006-09-27 01:50:11 -0700239 * and private data structure necessary to allocate, track, and free the
240 * underlying pages.
241 */
242static int
Cliff Wickman4191ba22007-09-18 22:46:31 -0700243mspec_mmap(struct file *file, struct vm_area_struct *vma,
244 enum mspec_page_type type)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700245{
246 struct vma_data *vdata;
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800247 int pages, vdata_size;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700248
249 if (vma->vm_pgoff != 0)
250 return -EINVAL;
251
252 if ((vma->vm_flags & VM_SHARED) == 0)
253 return -EINVAL;
254
255 if ((vma->vm_flags & VM_WRITE) == 0)
256 return -EPERM;
257
Libina0ea59d2013-05-13 10:17:39 +0800258 pages = vma_pages(vma);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700259 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
260 if (vdata_size <= PAGE_SIZE)
Rakib Mullick658c74c2011-05-26 16:25:56 -0700261 vdata = kzalloc(vdata_size, GFP_KERNEL);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800262 else
Rakib Mullick658c74c2011-05-26 16:25:56 -0700263 vdata = vzalloc(vdata_size);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700264 if (!vdata)
265 return -ENOMEM;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700266
Cliff Wickman4191ba22007-09-18 22:46:31 -0700267 vdata->vm_start = vma->vm_start;
268 vdata->vm_end = vma->vm_end;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700269 vdata->type = type;
270 spin_lock_init(&vdata->lock);
Elena Reshetovaf7d88d22017-03-06 16:20:50 +0200271 refcount_set(&vdata->refcnt, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700272 vma->vm_private_data = vdata;
273
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700274 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700275 if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
276 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
277 vma->vm_ops = &mspec_vm_ops;
278
279 return 0;
280}
281
282static int
283fetchop_mmap(struct file *file, struct vm_area_struct *vma)
284{
285 return mspec_mmap(file, vma, MSPEC_FETCHOP);
286}
287
288static int
289cached_mmap(struct file *file, struct vm_area_struct *vma)
290{
291 return mspec_mmap(file, vma, MSPEC_CACHED);
292}
293
294static int
295uncached_mmap(struct file *file, struct vm_area_struct *vma)
296{
297 return mspec_mmap(file, vma, MSPEC_UNCACHED);
298}
299
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800300static const struct file_operations fetchop_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700301 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200302 .mmap = fetchop_mmap,
303 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700304};
305
306static struct miscdevice fetchop_miscdev = {
307 .minor = MISC_DYNAMIC_MINOR,
308 .name = "sgi_fetchop",
309 .fops = &fetchop_fops
310};
311
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800312static const struct file_operations cached_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700313 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200314 .mmap = cached_mmap,
315 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700316};
317
318static struct miscdevice cached_miscdev = {
319 .minor = MISC_DYNAMIC_MINOR,
320 .name = "mspec_cached",
321 .fops = &cached_fops
322};
323
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800324static const struct file_operations uncached_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700325 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200326 .mmap = uncached_mmap,
327 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700328};
329
330static struct miscdevice uncached_miscdev = {
331 .minor = MISC_DYNAMIC_MINOR,
332 .name = "mspec_uncached",
333 .fops = &uncached_fops
334};
335
336/*
337 * mspec_init
338 *
339 * Called at boot time to initialize the mspec facility.
340 */
341static int __init
342mspec_init(void)
343{
344 int ret;
345 int nid;
346
347 /*
348 * The fetchop device only works on SN2 hardware, uncached and cached
349 * memory drivers should both be valid on all ia64 hardware
350 */
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -0800351#ifdef CONFIG_SGI_SN
Jes Sorensen17a3b052006-09-27 01:50:11 -0700352 if (ia64_platform_is("sn2")) {
353 is_sn2 = 1;
354 if (is_shub2()) {
355 ret = -ENOMEM;
Christoph Lameter2dca53a2007-10-16 01:25:33 -0700356 for_each_node_state(nid, N_ONLINE) {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700357 int actual_nid;
358 int nasid;
359 unsigned long phys;
360
Dean Nelsone4a064d2008-04-25 15:22:19 -0500361 scratch_page[nid] = uncached_alloc_page(nid, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700362 if (scratch_page[nid] == 0)
363 goto free_scratch_pages;
364 phys = __pa(scratch_page[nid]);
365 nasid = get_node_number(phys);
366 actual_nid = nasid_to_cnodeid(nasid);
367 if (actual_nid != nid)
368 goto free_scratch_pages;
369 }
370 }
371
372 ret = misc_register(&fetchop_miscdev);
373 if (ret) {
374 printk(KERN_ERR
375 "%s: failed to register device %i\n",
376 FETCHOP_ID, ret);
377 goto free_scratch_pages;
378 }
379 }
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -0800380#endif
Jes Sorensen17a3b052006-09-27 01:50:11 -0700381 ret = misc_register(&cached_miscdev);
382 if (ret) {
383 printk(KERN_ERR "%s: failed to register device %i\n",
384 CACHED_ID, ret);
385 if (is_sn2)
386 misc_deregister(&fetchop_miscdev);
387 goto free_scratch_pages;
388 }
389 ret = misc_register(&uncached_miscdev);
390 if (ret) {
391 printk(KERN_ERR "%s: failed to register device %i\n",
392 UNCACHED_ID, ret);
393 misc_deregister(&cached_miscdev);
394 if (is_sn2)
395 misc_deregister(&fetchop_miscdev);
396 goto free_scratch_pages;
397 }
398
399 printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
400 MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
401 CACHED_ID, UNCACHED_ID);
402
403 return 0;
404
405 free_scratch_pages:
406 for_each_node(nid) {
407 if (scratch_page[nid] != 0)
Dean Nelsone4a064d2008-04-25 15:22:19 -0500408 uncached_free_page(scratch_page[nid], 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700409 }
410 return ret;
411}
412
413static void __exit
414mspec_exit(void)
415{
416 int nid;
417
418 misc_deregister(&uncached_miscdev);
419 misc_deregister(&cached_miscdev);
420 if (is_sn2) {
421 misc_deregister(&fetchop_miscdev);
422
423 for_each_node(nid) {
424 if (scratch_page[nid] != 0)
Dean Nelsone4a064d2008-04-25 15:22:19 -0500425 uncached_free_page(scratch_page[nid], 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700426 }
427 }
428}
429
430module_init(mspec_init);
431module_exit(mspec_exit);
432
433MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
434MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
435MODULE_LICENSE("GPL");