]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/char/mspec.c
ufs: fix sun state
[mirror_ubuntu-bionic-kernel.git] / drivers / char / mspec.c
CommitLineData
17a3b050
JS
1/*
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
17a3b050
JS
33#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>
4e950f6f 41#include <linux/fs.h>
17a3b050
JS
42#include <linux/vmalloc.h>
43#include <linux/string.h>
44#include <linux/slab.h>
45#include <linux/numa.h>
46#include <asm/page.h>
47#include <asm/system.h>
48#include <asm/pgtable.h>
49#include <asm/atomic.h>
50#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 */
4191ba26 70enum mspec_page_type {
17a3b050
JS
71 MSPEC_FETCHOP = 1,
72 MSPEC_CACHED,
73 MSPEC_UNCACHED
74};
75
1a4b0fc5 76#ifdef CONFIG_SGI_SN
17a3b050 77static int is_sn2;
1a4b0fc5
JS
78#else
79#define is_sn2 0
80#endif
17a3b050
JS
81
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.
4191ba26
CW
86 * 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.
17a3b050
JS
91 */
92struct vma_data {
93 atomic_t refcnt; /* Number of vmas sharing the data. */
4191ba26 94 spinlock_t lock; /* Serialize access to this structure. */
17a3b050 95 int count; /* Number of pages allocated. */
4191ba26
CW
96 enum mspec_page_type type; /* Type of pages allocated. */
97 int flags; /* See VMD_xxx below. */
98 unsigned long vm_start; /* Original (unsplit) base. */
99 unsigned long vm_end; /* Original (unsplit) end. */
17a3b050
JS
100 unsigned long maddr[0]; /* Array of MSPEC addresses. */
101};
102
4191ba26
CW
103#define VMD_VMALLOCED 0x1 /* vmalloc'd rather than kmalloc'd */
104
17a3b050
JS
105/* used on shub2 to clear FOP cache in the HUB */
106static unsigned long scratch_page[MAX_NUMNODES];
107#define SH2_AMO_CACHE_ENTRIES 4
108
109static inline int
110mspec_zero_block(unsigned long addr, int len)
111{
112 int status;
113
114 if (is_sn2) {
115 if (is_shub2()) {
116 int nid;
117 void *p;
118 int i;
119
120 nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
121 p = (void *)TO_AMO(scratch_page[nid]);
122
123 for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
124 FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
125 p += FETCHOP_VAR_SIZE;
126 }
127 }
128
129 status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
130 BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
131 } else {
132 memset((char *) addr, 0, len);
133 status = 0;
134 }
135 return status;
136}
137
138/*
139 * mspec_open
140 *
141 * Called when a device mapping is created by a means other than mmap
4191ba26
CW
142 * (via fork, munmap, etc.). Increments the reference count on the
143 * underlying mspec data so it is not freed prematurely.
17a3b050
JS
144 */
145static void
146mspec_open(struct vm_area_struct *vma)
147{
148 struct vma_data *vdata;
149
150 vdata = vma->vm_private_data;
151 atomic_inc(&vdata->refcnt);
152}
153
154/*
155 * mspec_close
156 *
157 * Called when unmapping a device mapping. Frees all mspec pages
158 * belonging to the vma.
159 */
160static void
161mspec_close(struct vm_area_struct *vma)
162{
163 struct vma_data *vdata;
4191ba26
CW
164 int index, last_index, result;
165 unsigned long my_page;
17a3b050
JS
166
167 vdata = vma->vm_private_data;
17a3b050 168
4191ba26
CW
169 BUG_ON(vma->vm_start < vdata->vm_start || vma->vm_end > vdata->vm_end);
170
171 spin_lock(&vdata->lock);
172 index = (vma->vm_start - vdata->vm_start) >> PAGE_SHIFT;
173 last_index = (vma->vm_end - vdata->vm_start) >> PAGE_SHIFT;
174 for (; index < last_index; index++) {
175 if (vdata->maddr[index] == 0)
17a3b050
JS
176 continue;
177 /*
178 * Clear the page before sticking it back
179 * into the pool.
180 */
4191ba26
CW
181 my_page = vdata->maddr[index];
182 vdata->maddr[index] = 0;
183 spin_unlock(&vdata->lock);
184 result = mspec_zero_block(my_page, PAGE_SIZE);
17a3b050 185 if (!result)
4191ba26 186 uncached_free_page(my_page);
17a3b050
JS
187 else
188 printk(KERN_WARNING "mspec_close(): "
189 "failed to zero page %i\n",
190 result);
4191ba26 191 spin_lock(&vdata->lock);
17a3b050 192 }
4191ba26 193 spin_unlock(&vdata->lock);
17a3b050 194
4191ba26
CW
195 if (!atomic_dec_and_test(&vdata->refcnt))
196 return;
197
198 if (vdata->flags & VMD_VMALLOCED)
17a3b050 199 vfree(vdata);
4191ba26
CW
200 else
201 kfree(vdata);
17a3b050
JS
202}
203
204
205/*
206 * mspec_nopfn
207 *
208 * Creates a mspec page and maps it to user space.
209 */
210static unsigned long
211mspec_nopfn(struct vm_area_struct *vma, unsigned long address)
212{
213 unsigned long paddr, maddr;
214 unsigned long pfn;
215 int index;
216 struct vma_data *vdata = vma->vm_private_data;
217
4191ba26
CW
218 BUG_ON(address < vdata->vm_start || address >= vdata->vm_end);
219 index = (address - vdata->vm_start) >> PAGE_SHIFT;
17a3b050
JS
220 maddr = (volatile unsigned long) vdata->maddr[index];
221 if (maddr == 0) {
222 maddr = uncached_alloc_page(numa_node_id());
223 if (maddr == 0)
224 return NOPFN_OOM;
225
226 spin_lock(&vdata->lock);
227 if (vdata->maddr[index] == 0) {
228 vdata->count++;
229 vdata->maddr[index] = maddr;
230 } else {
231 uncached_free_page(maddr);
232 maddr = vdata->maddr[index];
233 }
234 spin_unlock(&vdata->lock);
235 }
236
237 if (vdata->type == MSPEC_FETCHOP)
238 paddr = TO_AMO(maddr);
239 else
1a4b0fc5 240 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
17a3b050
JS
241
242 pfn = paddr >> PAGE_SHIFT;
243
244 return pfn;
245}
246
247static struct vm_operations_struct mspec_vm_ops = {
248 .open = mspec_open,
249 .close = mspec_close,
250 .nopfn = mspec_nopfn
251};
252
253/*
254 * mspec_mmap
255 *
256 * Called when mmaping the device. Initializes the vma with a fault handler
257 * and private data structure necessary to allocate, track, and free the
258 * underlying pages.
259 */
260static int
4191ba26
CW
261mspec_mmap(struct file *file, struct vm_area_struct *vma,
262 enum mspec_page_type type)
17a3b050
JS
263{
264 struct vma_data *vdata;
4191ba26 265 int pages, vdata_size, flags = 0;
17a3b050
JS
266
267 if (vma->vm_pgoff != 0)
268 return -EINVAL;
269
270 if ((vma->vm_flags & VM_SHARED) == 0)
271 return -EINVAL;
272
273 if ((vma->vm_flags & VM_WRITE) == 0)
274 return -EPERM;
275
276 pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
277 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
278 if (vdata_size <= PAGE_SIZE)
279 vdata = kmalloc(vdata_size, GFP_KERNEL);
4191ba26 280 else {
17a3b050 281 vdata = vmalloc(vdata_size);
4191ba26
CW
282 flags = VMD_VMALLOCED;
283 }
17a3b050
JS
284 if (!vdata)
285 return -ENOMEM;
286 memset(vdata, 0, vdata_size);
287
4191ba26
CW
288 vdata->vm_start = vma->vm_start;
289 vdata->vm_end = vma->vm_end;
290 vdata->flags = flags;
17a3b050
JS
291 vdata->type = type;
292 spin_lock_init(&vdata->lock);
293 vdata->refcnt = ATOMIC_INIT(1);
294 vma->vm_private_data = vdata;
295
5dc4ac63 296 vma->vm_flags |= (VM_IO | VM_RESERVED | VM_PFNMAP);
17a3b050
JS
297 if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
298 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
299 vma->vm_ops = &mspec_vm_ops;
300
301 return 0;
302}
303
304static int
305fetchop_mmap(struct file *file, struct vm_area_struct *vma)
306{
307 return mspec_mmap(file, vma, MSPEC_FETCHOP);
308}
309
310static int
311cached_mmap(struct file *file, struct vm_area_struct *vma)
312{
313 return mspec_mmap(file, vma, MSPEC_CACHED);
314}
315
316static int
317uncached_mmap(struct file *file, struct vm_area_struct *vma)
318{
319 return mspec_mmap(file, vma, MSPEC_UNCACHED);
320}
321
2b8693c0 322static const struct file_operations fetchop_fops = {
17a3b050
JS
323 .owner = THIS_MODULE,
324 .mmap = fetchop_mmap
325};
326
327static struct miscdevice fetchop_miscdev = {
328 .minor = MISC_DYNAMIC_MINOR,
329 .name = "sgi_fetchop",
330 .fops = &fetchop_fops
331};
332
2b8693c0 333static const struct file_operations cached_fops = {
17a3b050
JS
334 .owner = THIS_MODULE,
335 .mmap = cached_mmap
336};
337
338static struct miscdevice cached_miscdev = {
339 .minor = MISC_DYNAMIC_MINOR,
340 .name = "mspec_cached",
341 .fops = &cached_fops
342};
343
2b8693c0 344static const struct file_operations uncached_fops = {
17a3b050
JS
345 .owner = THIS_MODULE,
346 .mmap = uncached_mmap
347};
348
349static struct miscdevice uncached_miscdev = {
350 .minor = MISC_DYNAMIC_MINOR,
351 .name = "mspec_uncached",
352 .fops = &uncached_fops
353};
354
355/*
356 * mspec_init
357 *
358 * Called at boot time to initialize the mspec facility.
359 */
360static int __init
361mspec_init(void)
362{
363 int ret;
364 int nid;
365
366 /*
367 * The fetchop device only works on SN2 hardware, uncached and cached
368 * memory drivers should both be valid on all ia64 hardware
369 */
1a4b0fc5 370#ifdef CONFIG_SGI_SN
17a3b050
JS
371 if (ia64_platform_is("sn2")) {
372 is_sn2 = 1;
373 if (is_shub2()) {
374 ret = -ENOMEM;
375 for_each_online_node(nid) {
376 int actual_nid;
377 int nasid;
378 unsigned long phys;
379
380 scratch_page[nid] = uncached_alloc_page(nid);
381 if (scratch_page[nid] == 0)
382 goto free_scratch_pages;
383 phys = __pa(scratch_page[nid]);
384 nasid = get_node_number(phys);
385 actual_nid = nasid_to_cnodeid(nasid);
386 if (actual_nid != nid)
387 goto free_scratch_pages;
388 }
389 }
390
391 ret = misc_register(&fetchop_miscdev);
392 if (ret) {
393 printk(KERN_ERR
394 "%s: failed to register device %i\n",
395 FETCHOP_ID, ret);
396 goto free_scratch_pages;
397 }
398 }
1a4b0fc5 399#endif
17a3b050
JS
400 ret = misc_register(&cached_miscdev);
401 if (ret) {
402 printk(KERN_ERR "%s: failed to register device %i\n",
403 CACHED_ID, ret);
404 if (is_sn2)
405 misc_deregister(&fetchop_miscdev);
406 goto free_scratch_pages;
407 }
408 ret = misc_register(&uncached_miscdev);
409 if (ret) {
410 printk(KERN_ERR "%s: failed to register device %i\n",
411 UNCACHED_ID, ret);
412 misc_deregister(&cached_miscdev);
413 if (is_sn2)
414 misc_deregister(&fetchop_miscdev);
415 goto free_scratch_pages;
416 }
417
418 printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
419 MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
420 CACHED_ID, UNCACHED_ID);
421
422 return 0;
423
424 free_scratch_pages:
425 for_each_node(nid) {
426 if (scratch_page[nid] != 0)
427 uncached_free_page(scratch_page[nid]);
428 }
429 return ret;
430}
431
432static void __exit
433mspec_exit(void)
434{
435 int nid;
436
437 misc_deregister(&uncached_miscdev);
438 misc_deregister(&cached_miscdev);
439 if (is_sn2) {
440 misc_deregister(&fetchop_miscdev);
441
442 for_each_node(nid) {
443 if (scratch_page[nid] != 0)
444 uncached_free_page(scratch_page[nid]);
445 }
446 }
447}
448
449module_init(mspec_init);
450module_exit(mspec_exit);
451
452MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
453MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
454MODULE_LICENSE("GPL");