6ebe7f733ba416bd6e7edc52e7fb197102f16b12
[firefly-linux-kernel-4.4.55.git] / drivers / staging / unisys / visorchipset / file.c
1 /* file.c
2  *
3  * Copyright (C) 2010 - 2013 UNISYS CORPORATION
4  * All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14  * NON INFRINGEMENT.  See the GNU General Public License for more
15  * details.
16  */
17
18 /* This contains the implementation that allows a usermode program to
19  * communicate with the visorchipset driver using a device/file interface.
20  */
21
22 #include "globals.h"
23 #include "visorchannel.h"
24 #include <linux/mm.h>
25 #include <linux/fs.h>
26 #include "uisutils.h"
27 #include "file.h"
28
29 #define CURRENT_FILE_PC VISOR_CHIPSET_PC_file_c
30
31 static struct cdev file_cdev;
32 static struct visorchannel **file_controlvm_channel;
33 static dev_t majordev = -1; /**< indicates major num for device */
34
35 static int visorchipset_open(struct inode *inode, struct file *file);
36 static int visorchipset_release(struct inode *inode, struct file *file);
37 static int visorchipset_mmap(struct file *file, struct vm_area_struct *vma);
38 static long visorchipset_ioctl(struct file *file, unsigned int cmd,
39                                 unsigned long arg);
40
41 static const struct file_operations visorchipset_fops = {
42         .owner = THIS_MODULE,
43         .open = visorchipset_open,
44         .read = NULL,
45         .write = NULL,
46         .unlocked_ioctl = visorchipset_ioctl,
47         .release = visorchipset_release,
48         .mmap = visorchipset_mmap,
49 };
50
51 int
52 visorchipset_file_init(dev_t major_dev, struct visorchannel **controlvm_channel)
53 {
54         int rc = 0;
55
56         file_controlvm_channel = controlvm_channel;
57         majordev = major_dev;
58         cdev_init(&file_cdev, &visorchipset_fops);
59         file_cdev.owner = THIS_MODULE;
60         if (MAJOR(major_dev) == 0) {
61                 /* dynamic major device number registration required */
62                 if (alloc_chrdev_region(&major_dev, 0, 1, MYDRVNAME) < 0)
63                         return -1;
64         } else {
65                 /* static major device number registration required */
66                 if (register_chrdev_region(major_dev, 1, MYDRVNAME) < 0)
67                         return -1;
68         }
69         rc = cdev_add(&file_cdev, MKDEV(MAJOR(major_dev), 0), 1);
70         if (rc  < 0)
71                 return -1;
72         return 0;
73 }
74
75 void
76 visorchipset_file_cleanup(void)
77 {
78         if (file_cdev.ops != NULL)
79                 cdev_del(&file_cdev);
80         file_cdev.ops = NULL;
81         if (MAJOR(majordev) >= 0) {
82                 unregister_chrdev_region(majordev, 1);
83                 majordev = MKDEV(0, 0);
84         }
85 }
86
87 static int
88 visorchipset_open(struct inode *inode, struct file *file)
89 {
90         unsigned minor_number = iminor(inode);
91
92         if (minor_number != 0)
93                 return -ENODEV;
94         file->private_data = NULL;
95         return 0;
96 }
97
98 static int
99 visorchipset_release(struct inode *inode, struct file *file)
100 {
101         return 0;
102 }
103
104 static int
105 visorchipset_mmap(struct file *file, struct vm_area_struct *vma)
106 {
107         ulong physaddr = 0;
108         ulong offset = vma->vm_pgoff << PAGE_SHIFT;
109         GUEST_PHYSICAL_ADDRESS addr = 0;
110
111         /* sv_enable_dfp(); */
112         if (offset & (PAGE_SIZE - 1))
113                 return -ENXIO;  /* need aligned offsets */
114
115         switch (offset) {
116         case VISORCHIPSET_MMAP_CONTROLCHANOFFSET:
117                 vma->vm_flags |= VM_IO;
118                 if (*file_controlvm_channel == NULL) {
119                         return -ENXIO;
120                 }
121                 visorchannel_read(*file_controlvm_channel,
122                         offsetof(struct spar_controlvm_channel_protocol,
123                                  gp_control_channel),
124                         &addr, sizeof(addr));
125                 if (addr == 0) {
126                         return -ENXIO;
127                 }
128                 physaddr = (ulong)addr;
129                 if (remap_pfn_range(vma, vma->vm_start,
130                                     physaddr >> PAGE_SHIFT,
131                                     vma->vm_end - vma->vm_start,
132                                     /*pgprot_noncached */
133                                     (vma->vm_page_prot))) {
134                         return -EAGAIN;
135                 }
136                 break;
137         default:
138                 return -ENOSYS;
139         }
140         return 0;
141 }
142
143 static long visorchipset_ioctl(struct file *file, unsigned int cmd,
144                                 unsigned long arg)
145 {
146         s64 adjustment;
147         s64 vrtc_offset;
148
149         switch (cmd) {
150         case VMCALL_QUERY_GUEST_VIRTUAL_TIME_OFFSET:
151                 /* get the physical rtc offset */
152                 vrtc_offset = issue_vmcall_query_guest_virtual_time_offset();
153                 if (copy_to_user
154                     ((void __user *)arg, &vrtc_offset, sizeof(vrtc_offset))) {
155                         return -EFAULT;
156                 }
157                 return SUCCESS;
158         case VMCALL_UPDATE_PHYSICAL_TIME:
159                 if (copy_from_user
160                     (&adjustment, (void __user *)arg, sizeof(adjustment))) {
161                         return -EFAULT;
162                 }
163                 return issue_vmcall_update_physical_time(adjustment);
164         default:
165                 return -EFAULT;
166         }
167 }