xref: /OK3568_Linux_fs/kernel/drivers/virt/fsl_hypervisor.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Freescale Hypervisor Management Driver
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun  * Copyright (C) 2008-2011 Freescale Semiconductor, Inc.
5*4882a593Smuzhiyun  * Author: Timur Tabi <timur@freescale.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This file is licensed under the terms of the GNU General Public License
8*4882a593Smuzhiyun  * version 2.  This program is licensed "as is" without any warranty of any
9*4882a593Smuzhiyun  * kind, whether express or implied.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The Freescale hypervisor management driver provides several services to
12*4882a593Smuzhiyun  * drivers and applications related to the Freescale hypervisor:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * 1. An ioctl interface for querying and managing partitions.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * 2. A file interface to reading incoming doorbells.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * 3. An interrupt handler for shutting down the partition upon receiving the
19*4882a593Smuzhiyun  *    shutdown doorbell from a manager partition.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * 4. A kernel interface for receiving callbacks when a managed partition
22*4882a593Smuzhiyun  *    shuts down.
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <linux/kernel.h>
26*4882a593Smuzhiyun #include <linux/module.h>
27*4882a593Smuzhiyun #include <linux/init.h>
28*4882a593Smuzhiyun #include <linux/types.h>
29*4882a593Smuzhiyun #include <linux/err.h>
30*4882a593Smuzhiyun #include <linux/fs.h>
31*4882a593Smuzhiyun #include <linux/miscdevice.h>
32*4882a593Smuzhiyun #include <linux/mm.h>
33*4882a593Smuzhiyun #include <linux/pagemap.h>
34*4882a593Smuzhiyun #include <linux/slab.h>
35*4882a593Smuzhiyun #include <linux/poll.h>
36*4882a593Smuzhiyun #include <linux/of.h>
37*4882a593Smuzhiyun #include <linux/of_irq.h>
38*4882a593Smuzhiyun #include <linux/reboot.h>
39*4882a593Smuzhiyun #include <linux/uaccess.h>
40*4882a593Smuzhiyun #include <linux/notifier.h>
41*4882a593Smuzhiyun #include <linux/interrupt.h>
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #include <linux/io.h>
44*4882a593Smuzhiyun #include <asm/fsl_hcalls.h>
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #include <linux/fsl_hypervisor.h>
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun static BLOCKING_NOTIFIER_HEAD(failover_subscribers);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun  * Ioctl interface for FSL_HV_IOCTL_PARTITION_RESTART
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * Restart a running partition
54*4882a593Smuzhiyun  */
ioctl_restart(struct fsl_hv_ioctl_restart __user * p)55*4882a593Smuzhiyun static long ioctl_restart(struct fsl_hv_ioctl_restart __user *p)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	struct fsl_hv_ioctl_restart param;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	/* Get the parameters from the user */
60*4882a593Smuzhiyun 	if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_restart)))
61*4882a593Smuzhiyun 		return -EFAULT;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	param.ret = fh_partition_restart(param.partition);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
66*4882a593Smuzhiyun 		return -EFAULT;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	return 0;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun  * Ioctl interface for FSL_HV_IOCTL_PARTITION_STATUS
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * Query the status of a partition
75*4882a593Smuzhiyun  */
ioctl_status(struct fsl_hv_ioctl_status __user * p)76*4882a593Smuzhiyun static long ioctl_status(struct fsl_hv_ioctl_status __user *p)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	struct fsl_hv_ioctl_status param;
79*4882a593Smuzhiyun 	u32 status;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	/* Get the parameters from the user */
82*4882a593Smuzhiyun 	if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_status)))
83*4882a593Smuzhiyun 		return -EFAULT;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	param.ret = fh_partition_get_status(param.partition, &status);
86*4882a593Smuzhiyun 	if (!param.ret)
87*4882a593Smuzhiyun 		param.status = status;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	if (copy_to_user(p, &param, sizeof(struct fsl_hv_ioctl_status)))
90*4882a593Smuzhiyun 		return -EFAULT;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	return 0;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun  * Ioctl interface for FSL_HV_IOCTL_PARTITION_START
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * Start a stopped partition.
99*4882a593Smuzhiyun  */
ioctl_start(struct fsl_hv_ioctl_start __user * p)100*4882a593Smuzhiyun static long ioctl_start(struct fsl_hv_ioctl_start __user *p)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	struct fsl_hv_ioctl_start param;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	/* Get the parameters from the user */
105*4882a593Smuzhiyun 	if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_start)))
106*4882a593Smuzhiyun 		return -EFAULT;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	param.ret = fh_partition_start(param.partition, param.entry_point,
109*4882a593Smuzhiyun 				       param.load);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
112*4882a593Smuzhiyun 		return -EFAULT;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	return 0;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun  * Ioctl interface for FSL_HV_IOCTL_PARTITION_STOP
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * Stop a running partition
121*4882a593Smuzhiyun  */
ioctl_stop(struct fsl_hv_ioctl_stop __user * p)122*4882a593Smuzhiyun static long ioctl_stop(struct fsl_hv_ioctl_stop __user *p)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun 	struct fsl_hv_ioctl_stop param;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/* Get the parameters from the user */
127*4882a593Smuzhiyun 	if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_stop)))
128*4882a593Smuzhiyun 		return -EFAULT;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	param.ret = fh_partition_stop(param.partition);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
133*4882a593Smuzhiyun 		return -EFAULT;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	return 0;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun  * Ioctl interface for FSL_HV_IOCTL_MEMCPY
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  * The FH_MEMCPY hypercall takes an array of address/address/size structures
142*4882a593Smuzhiyun  * to represent the data being copied.  As a convenience to the user, this
143*4882a593Smuzhiyun  * ioctl takes a user-create buffer and a pointer to a guest physically
144*4882a593Smuzhiyun  * contiguous buffer in the remote partition, and creates the
145*4882a593Smuzhiyun  * address/address/size array for the hypercall.
146*4882a593Smuzhiyun  */
ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user * p)147*4882a593Smuzhiyun static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	struct fsl_hv_ioctl_memcpy param;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	struct page **pages = NULL;
152*4882a593Smuzhiyun 	void *sg_list_unaligned = NULL;
153*4882a593Smuzhiyun 	struct fh_sg_list *sg_list = NULL;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	unsigned int num_pages;
156*4882a593Smuzhiyun 	unsigned long lb_offset; /* Offset within a page of the local buffer */
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	unsigned int i;
159*4882a593Smuzhiyun 	long ret = 0;
160*4882a593Smuzhiyun 	int num_pinned = 0; /* return value from get_user_pages_fast() */
161*4882a593Smuzhiyun 	phys_addr_t remote_paddr; /* The next address in the remote buffer */
162*4882a593Smuzhiyun 	uint32_t count; /* The number of bytes left to copy */
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/* Get the parameters from the user */
165*4882a593Smuzhiyun 	if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_memcpy)))
166*4882a593Smuzhiyun 		return -EFAULT;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	/*
169*4882a593Smuzhiyun 	 * One partition must be local, the other must be remote.  In other
170*4882a593Smuzhiyun 	 * words, if source and target are both -1, or are both not -1, then
171*4882a593Smuzhiyun 	 * return an error.
172*4882a593Smuzhiyun 	 */
173*4882a593Smuzhiyun 	if ((param.source == -1) == (param.target == -1))
174*4882a593Smuzhiyun 		return -EINVAL;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/*
177*4882a593Smuzhiyun 	 * The array of pages returned by get_user_pages_fast() covers only
178*4882a593Smuzhiyun 	 * page-aligned memory.  Since the user buffer is probably not
179*4882a593Smuzhiyun 	 * page-aligned, we need to handle the discrepancy.
180*4882a593Smuzhiyun 	 *
181*4882a593Smuzhiyun 	 * We calculate the offset within a page of the S/G list, and make
182*4882a593Smuzhiyun 	 * adjustments accordingly.  This will result in a page list that looks
183*4882a593Smuzhiyun 	 * like this:
184*4882a593Smuzhiyun 	 *
185*4882a593Smuzhiyun 	 *      ----    <-- first page starts before the buffer
186*4882a593Smuzhiyun 	 *     |    |
187*4882a593Smuzhiyun 	 *     |////|-> ----
188*4882a593Smuzhiyun 	 *     |////|  |    |
189*4882a593Smuzhiyun 	 *      ----   |    |
190*4882a593Smuzhiyun 	 *             |    |
191*4882a593Smuzhiyun 	 *      ----   |    |
192*4882a593Smuzhiyun 	 *     |////|  |    |
193*4882a593Smuzhiyun 	 *     |////|  |    |
194*4882a593Smuzhiyun 	 *     |////|  |    |
195*4882a593Smuzhiyun 	 *      ----   |    |
196*4882a593Smuzhiyun 	 *             |    |
197*4882a593Smuzhiyun 	 *      ----   |    |
198*4882a593Smuzhiyun 	 *     |////|  |    |
199*4882a593Smuzhiyun 	 *     |////|  |    |
200*4882a593Smuzhiyun 	 *     |////|  |    |
201*4882a593Smuzhiyun 	 *      ----   |    |
202*4882a593Smuzhiyun 	 *             |    |
203*4882a593Smuzhiyun 	 *      ----   |    |
204*4882a593Smuzhiyun 	 *     |////|  |    |
205*4882a593Smuzhiyun 	 *     |////|-> ----
206*4882a593Smuzhiyun 	 *     |    |   <-- last page ends after the buffer
207*4882a593Smuzhiyun 	 *      ----
208*4882a593Smuzhiyun 	 *
209*4882a593Smuzhiyun 	 * The distance between the start of the first page and the start of the
210*4882a593Smuzhiyun 	 * buffer is lb_offset.  The hashed (///) areas are the parts of the
211*4882a593Smuzhiyun 	 * page list that contain the actual buffer.
212*4882a593Smuzhiyun 	 *
213*4882a593Smuzhiyun 	 * The advantage of this approach is that the number of pages is
214*4882a593Smuzhiyun 	 * equal to the number of entries in the S/G list that we give to the
215*4882a593Smuzhiyun 	 * hypervisor.
216*4882a593Smuzhiyun 	 */
217*4882a593Smuzhiyun 	lb_offset = param.local_vaddr & (PAGE_SIZE - 1);
218*4882a593Smuzhiyun 	if (param.count == 0 ||
219*4882a593Smuzhiyun 	    param.count > U64_MAX - lb_offset - PAGE_SIZE + 1)
220*4882a593Smuzhiyun 		return -EINVAL;
221*4882a593Smuzhiyun 	num_pages = (param.count + lb_offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	/* Allocate the buffers we need */
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/*
226*4882a593Smuzhiyun 	 * 'pages' is an array of struct page pointers that's initialized by
227*4882a593Smuzhiyun 	 * get_user_pages_fast().
228*4882a593Smuzhiyun 	 */
229*4882a593Smuzhiyun 	pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
230*4882a593Smuzhiyun 	if (!pages) {
231*4882a593Smuzhiyun 		pr_debug("fsl-hv: could not allocate page list\n");
232*4882a593Smuzhiyun 		return -ENOMEM;
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	/*
236*4882a593Smuzhiyun 	 * sg_list is the list of fh_sg_list objects that we pass to the
237*4882a593Smuzhiyun 	 * hypervisor.
238*4882a593Smuzhiyun 	 */
239*4882a593Smuzhiyun 	sg_list_unaligned = kmalloc(num_pages * sizeof(struct fh_sg_list) +
240*4882a593Smuzhiyun 		sizeof(struct fh_sg_list) - 1, GFP_KERNEL);
241*4882a593Smuzhiyun 	if (!sg_list_unaligned) {
242*4882a593Smuzhiyun 		pr_debug("fsl-hv: could not allocate S/G list\n");
243*4882a593Smuzhiyun 		ret = -ENOMEM;
244*4882a593Smuzhiyun 		goto free_pages;
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 	sg_list = PTR_ALIGN(sg_list_unaligned, sizeof(struct fh_sg_list));
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	/* Get the physical addresses of the source buffer */
249*4882a593Smuzhiyun 	num_pinned = get_user_pages_fast(param.local_vaddr - lb_offset,
250*4882a593Smuzhiyun 		num_pages, param.source != -1 ? FOLL_WRITE : 0, pages);
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	if (num_pinned != num_pages) {
253*4882a593Smuzhiyun 		pr_debug("fsl-hv: could not lock source buffer\n");
254*4882a593Smuzhiyun 		ret = (num_pinned < 0) ? num_pinned : -EFAULT;
255*4882a593Smuzhiyun 		goto exit;
256*4882a593Smuzhiyun 	}
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	/*
259*4882a593Smuzhiyun 	 * Build the fh_sg_list[] array.  The first page is special
260*4882a593Smuzhiyun 	 * because it's misaligned.
261*4882a593Smuzhiyun 	 */
262*4882a593Smuzhiyun 	if (param.source == -1) {
263*4882a593Smuzhiyun 		sg_list[0].source = page_to_phys(pages[0]) + lb_offset;
264*4882a593Smuzhiyun 		sg_list[0].target = param.remote_paddr;
265*4882a593Smuzhiyun 	} else {
266*4882a593Smuzhiyun 		sg_list[0].source = param.remote_paddr;
267*4882a593Smuzhiyun 		sg_list[0].target = page_to_phys(pages[0]) + lb_offset;
268*4882a593Smuzhiyun 	}
269*4882a593Smuzhiyun 	sg_list[0].size = min_t(uint64_t, param.count, PAGE_SIZE - lb_offset);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	remote_paddr = param.remote_paddr + sg_list[0].size;
272*4882a593Smuzhiyun 	count = param.count - sg_list[0].size;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	for (i = 1; i < num_pages; i++) {
275*4882a593Smuzhiyun 		if (param.source == -1) {
276*4882a593Smuzhiyun 			/* local to remote */
277*4882a593Smuzhiyun 			sg_list[i].source = page_to_phys(pages[i]);
278*4882a593Smuzhiyun 			sg_list[i].target = remote_paddr;
279*4882a593Smuzhiyun 		} else {
280*4882a593Smuzhiyun 			/* remote to local */
281*4882a593Smuzhiyun 			sg_list[i].source = remote_paddr;
282*4882a593Smuzhiyun 			sg_list[i].target = page_to_phys(pages[i]);
283*4882a593Smuzhiyun 		}
284*4882a593Smuzhiyun 		sg_list[i].size = min_t(uint64_t, count, PAGE_SIZE);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 		remote_paddr += sg_list[i].size;
287*4882a593Smuzhiyun 		count -= sg_list[i].size;
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	param.ret = fh_partition_memcpy(param.source, param.target,
291*4882a593Smuzhiyun 		virt_to_phys(sg_list), num_pages);
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun exit:
294*4882a593Smuzhiyun 	if (pages && (num_pinned > 0)) {
295*4882a593Smuzhiyun 		for (i = 0; i < num_pinned; i++)
296*4882a593Smuzhiyun 			put_page(pages[i]);
297*4882a593Smuzhiyun 	}
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	kfree(sg_list_unaligned);
300*4882a593Smuzhiyun free_pages:
301*4882a593Smuzhiyun 	kfree(pages);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	if (!ret)
304*4882a593Smuzhiyun 		if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
305*4882a593Smuzhiyun 			return -EFAULT;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	return ret;
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun /*
311*4882a593Smuzhiyun  * Ioctl interface for FSL_HV_IOCTL_DOORBELL
312*4882a593Smuzhiyun  *
313*4882a593Smuzhiyun  * Ring a doorbell
314*4882a593Smuzhiyun  */
ioctl_doorbell(struct fsl_hv_ioctl_doorbell __user * p)315*4882a593Smuzhiyun static long ioctl_doorbell(struct fsl_hv_ioctl_doorbell __user *p)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	struct fsl_hv_ioctl_doorbell param;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	/* Get the parameters from the user. */
320*4882a593Smuzhiyun 	if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_doorbell)))
321*4882a593Smuzhiyun 		return -EFAULT;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	param.ret = ev_doorbell_send(param.doorbell);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
326*4882a593Smuzhiyun 		return -EFAULT;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	return 0;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
ioctl_dtprop(struct fsl_hv_ioctl_prop __user * p,int set)331*4882a593Smuzhiyun static long ioctl_dtprop(struct fsl_hv_ioctl_prop __user *p, int set)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	struct fsl_hv_ioctl_prop param;
334*4882a593Smuzhiyun 	char __user *upath, *upropname;
335*4882a593Smuzhiyun 	void __user *upropval;
336*4882a593Smuzhiyun 	char *path, *propname;
337*4882a593Smuzhiyun 	void *propval;
338*4882a593Smuzhiyun 	int ret = 0;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	/* Get the parameters from the user. */
341*4882a593Smuzhiyun 	if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_prop)))
342*4882a593Smuzhiyun 		return -EFAULT;
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	upath = (char __user *)(uintptr_t)param.path;
345*4882a593Smuzhiyun 	upropname = (char __user *)(uintptr_t)param.propname;
346*4882a593Smuzhiyun 	upropval = (void __user *)(uintptr_t)param.propval;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	path = strndup_user(upath, FH_DTPROP_MAX_PATHLEN);
349*4882a593Smuzhiyun 	if (IS_ERR(path))
350*4882a593Smuzhiyun 		return PTR_ERR(path);
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	propname = strndup_user(upropname, FH_DTPROP_MAX_PATHLEN);
353*4882a593Smuzhiyun 	if (IS_ERR(propname)) {
354*4882a593Smuzhiyun 		ret = PTR_ERR(propname);
355*4882a593Smuzhiyun 		goto err_free_path;
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	if (param.proplen > FH_DTPROP_MAX_PROPLEN) {
359*4882a593Smuzhiyun 		ret = -EINVAL;
360*4882a593Smuzhiyun 		goto err_free_propname;
361*4882a593Smuzhiyun 	}
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	propval = kmalloc(param.proplen, GFP_KERNEL);
364*4882a593Smuzhiyun 	if (!propval) {
365*4882a593Smuzhiyun 		ret = -ENOMEM;
366*4882a593Smuzhiyun 		goto err_free_propname;
367*4882a593Smuzhiyun 	}
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	if (set) {
370*4882a593Smuzhiyun 		if (copy_from_user(propval, upropval, param.proplen)) {
371*4882a593Smuzhiyun 			ret = -EFAULT;
372*4882a593Smuzhiyun 			goto err_free_propval;
373*4882a593Smuzhiyun 		}
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 		param.ret = fh_partition_set_dtprop(param.handle,
376*4882a593Smuzhiyun 						    virt_to_phys(path),
377*4882a593Smuzhiyun 						    virt_to_phys(propname),
378*4882a593Smuzhiyun 						    virt_to_phys(propval),
379*4882a593Smuzhiyun 						    param.proplen);
380*4882a593Smuzhiyun 	} else {
381*4882a593Smuzhiyun 		param.ret = fh_partition_get_dtprop(param.handle,
382*4882a593Smuzhiyun 						    virt_to_phys(path),
383*4882a593Smuzhiyun 						    virt_to_phys(propname),
384*4882a593Smuzhiyun 						    virt_to_phys(propval),
385*4882a593Smuzhiyun 						    &param.proplen);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 		if (param.ret == 0) {
388*4882a593Smuzhiyun 			if (copy_to_user(upropval, propval, param.proplen) ||
389*4882a593Smuzhiyun 			    put_user(param.proplen, &p->proplen)) {
390*4882a593Smuzhiyun 				ret = -EFAULT;
391*4882a593Smuzhiyun 				goto err_free_propval;
392*4882a593Smuzhiyun 			}
393*4882a593Smuzhiyun 		}
394*4882a593Smuzhiyun 	}
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (put_user(param.ret, &p->ret))
397*4882a593Smuzhiyun 		ret = -EFAULT;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun err_free_propval:
400*4882a593Smuzhiyun 	kfree(propval);
401*4882a593Smuzhiyun err_free_propname:
402*4882a593Smuzhiyun 	kfree(propname);
403*4882a593Smuzhiyun err_free_path:
404*4882a593Smuzhiyun 	kfree(path);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	return ret;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun /*
410*4882a593Smuzhiyun  * Ioctl main entry point
411*4882a593Smuzhiyun  */
fsl_hv_ioctl(struct file * file,unsigned int cmd,unsigned long argaddr)412*4882a593Smuzhiyun static long fsl_hv_ioctl(struct file *file, unsigned int cmd,
413*4882a593Smuzhiyun 			 unsigned long argaddr)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	void __user *arg = (void __user *)argaddr;
416*4882a593Smuzhiyun 	long ret;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	switch (cmd) {
419*4882a593Smuzhiyun 	case FSL_HV_IOCTL_PARTITION_RESTART:
420*4882a593Smuzhiyun 		ret = ioctl_restart(arg);
421*4882a593Smuzhiyun 		break;
422*4882a593Smuzhiyun 	case FSL_HV_IOCTL_PARTITION_GET_STATUS:
423*4882a593Smuzhiyun 		ret = ioctl_status(arg);
424*4882a593Smuzhiyun 		break;
425*4882a593Smuzhiyun 	case FSL_HV_IOCTL_PARTITION_START:
426*4882a593Smuzhiyun 		ret = ioctl_start(arg);
427*4882a593Smuzhiyun 		break;
428*4882a593Smuzhiyun 	case FSL_HV_IOCTL_PARTITION_STOP:
429*4882a593Smuzhiyun 		ret = ioctl_stop(arg);
430*4882a593Smuzhiyun 		break;
431*4882a593Smuzhiyun 	case FSL_HV_IOCTL_MEMCPY:
432*4882a593Smuzhiyun 		ret = ioctl_memcpy(arg);
433*4882a593Smuzhiyun 		break;
434*4882a593Smuzhiyun 	case FSL_HV_IOCTL_DOORBELL:
435*4882a593Smuzhiyun 		ret = ioctl_doorbell(arg);
436*4882a593Smuzhiyun 		break;
437*4882a593Smuzhiyun 	case FSL_HV_IOCTL_GETPROP:
438*4882a593Smuzhiyun 		ret = ioctl_dtprop(arg, 0);
439*4882a593Smuzhiyun 		break;
440*4882a593Smuzhiyun 	case FSL_HV_IOCTL_SETPROP:
441*4882a593Smuzhiyun 		ret = ioctl_dtprop(arg, 1);
442*4882a593Smuzhiyun 		break;
443*4882a593Smuzhiyun 	default:
444*4882a593Smuzhiyun 		pr_debug("fsl-hv: bad ioctl dir=%u type=%u cmd=%u size=%u\n",
445*4882a593Smuzhiyun 			 _IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd),
446*4882a593Smuzhiyun 			 _IOC_SIZE(cmd));
447*4882a593Smuzhiyun 		return -ENOTTY;
448*4882a593Smuzhiyun 	}
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	return ret;
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun /* Linked list of processes that have us open */
454*4882a593Smuzhiyun static struct list_head db_list;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun /* spinlock for db_list */
457*4882a593Smuzhiyun static DEFINE_SPINLOCK(db_list_lock);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun /* The size of the doorbell event queue.  This must be a power of two. */
460*4882a593Smuzhiyun #define QSIZE	16
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun /* Returns the next head/tail pointer, wrapping around the queue if necessary */
463*4882a593Smuzhiyun #define nextp(x) (((x) + 1) & (QSIZE - 1))
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun /* Per-open data structure */
466*4882a593Smuzhiyun struct doorbell_queue {
467*4882a593Smuzhiyun 	struct list_head list;
468*4882a593Smuzhiyun 	spinlock_t lock;
469*4882a593Smuzhiyun 	wait_queue_head_t wait;
470*4882a593Smuzhiyun 	unsigned int head;
471*4882a593Smuzhiyun 	unsigned int tail;
472*4882a593Smuzhiyun 	uint32_t q[QSIZE];
473*4882a593Smuzhiyun };
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun /* Linked list of ISRs that we registered */
476*4882a593Smuzhiyun struct list_head isr_list;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun /* Per-ISR data structure */
479*4882a593Smuzhiyun struct doorbell_isr {
480*4882a593Smuzhiyun 	struct list_head list;
481*4882a593Smuzhiyun 	unsigned int irq;
482*4882a593Smuzhiyun 	uint32_t doorbell;	/* The doorbell handle */
483*4882a593Smuzhiyun 	uint32_t partition;	/* The partition handle, if used */
484*4882a593Smuzhiyun };
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun /*
487*4882a593Smuzhiyun  * Add a doorbell to all of the doorbell queues
488*4882a593Smuzhiyun  */
fsl_hv_queue_doorbell(uint32_t doorbell)489*4882a593Smuzhiyun static void fsl_hv_queue_doorbell(uint32_t doorbell)
490*4882a593Smuzhiyun {
491*4882a593Smuzhiyun 	struct doorbell_queue *dbq;
492*4882a593Smuzhiyun 	unsigned long flags;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	/* Prevent another core from modifying db_list */
495*4882a593Smuzhiyun 	spin_lock_irqsave(&db_list_lock, flags);
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	list_for_each_entry(dbq, &db_list, list) {
498*4882a593Smuzhiyun 		if (dbq->head != nextp(dbq->tail)) {
499*4882a593Smuzhiyun 			dbq->q[dbq->tail] = doorbell;
500*4882a593Smuzhiyun 			/*
501*4882a593Smuzhiyun 			 * This memory barrier eliminates the need to grab
502*4882a593Smuzhiyun 			 * the spinlock for dbq.
503*4882a593Smuzhiyun 			 */
504*4882a593Smuzhiyun 			smp_wmb();
505*4882a593Smuzhiyun 			dbq->tail = nextp(dbq->tail);
506*4882a593Smuzhiyun 			wake_up_interruptible(&dbq->wait);
507*4882a593Smuzhiyun 		}
508*4882a593Smuzhiyun 	}
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	spin_unlock_irqrestore(&db_list_lock, flags);
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun /*
514*4882a593Smuzhiyun  * Interrupt handler for all doorbells
515*4882a593Smuzhiyun  *
516*4882a593Smuzhiyun  * We use the same interrupt handler for all doorbells.  Whenever a doorbell
517*4882a593Smuzhiyun  * is rung, and we receive an interrupt, we just put the handle for that
518*4882a593Smuzhiyun  * doorbell (passed to us as *data) into all of the queues.
519*4882a593Smuzhiyun  */
fsl_hv_isr(int irq,void * data)520*4882a593Smuzhiyun static irqreturn_t fsl_hv_isr(int irq, void *data)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	fsl_hv_queue_doorbell((uintptr_t) data);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	return IRQ_HANDLED;
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun /*
528*4882a593Smuzhiyun  * State change thread function
529*4882a593Smuzhiyun  *
530*4882a593Smuzhiyun  * The state change notification arrives in an interrupt, but we can't call
531*4882a593Smuzhiyun  * blocking_notifier_call_chain() in an interrupt handler.  We could call
532*4882a593Smuzhiyun  * atomic_notifier_call_chain(), but that would require the clients' call-back
533*4882a593Smuzhiyun  * function to run in interrupt context.  Since we don't want to impose that
534*4882a593Smuzhiyun  * restriction on the clients, we use a threaded IRQ to process the
535*4882a593Smuzhiyun  * notification in kernel context.
536*4882a593Smuzhiyun  */
fsl_hv_state_change_thread(int irq,void * data)537*4882a593Smuzhiyun static irqreturn_t fsl_hv_state_change_thread(int irq, void *data)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	struct doorbell_isr *dbisr = data;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	blocking_notifier_call_chain(&failover_subscribers, dbisr->partition,
542*4882a593Smuzhiyun 				     NULL);
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	return IRQ_HANDLED;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun /*
548*4882a593Smuzhiyun  * Interrupt handler for state-change doorbells
549*4882a593Smuzhiyun  */
fsl_hv_state_change_isr(int irq,void * data)550*4882a593Smuzhiyun static irqreturn_t fsl_hv_state_change_isr(int irq, void *data)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun 	unsigned int status;
553*4882a593Smuzhiyun 	struct doorbell_isr *dbisr = data;
554*4882a593Smuzhiyun 	int ret;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	/* It's still a doorbell, so add it to all the queues. */
557*4882a593Smuzhiyun 	fsl_hv_queue_doorbell(dbisr->doorbell);
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	/* Determine the new state, and if it's stopped, notify the clients. */
560*4882a593Smuzhiyun 	ret = fh_partition_get_status(dbisr->partition, &status);
561*4882a593Smuzhiyun 	if (!ret && (status == FH_PARTITION_STOPPED))
562*4882a593Smuzhiyun 		return IRQ_WAKE_THREAD;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	return IRQ_HANDLED;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun /*
568*4882a593Smuzhiyun  * Returns a bitmask indicating whether a read will block
569*4882a593Smuzhiyun  */
fsl_hv_poll(struct file * filp,struct poll_table_struct * p)570*4882a593Smuzhiyun static __poll_t fsl_hv_poll(struct file *filp, struct poll_table_struct *p)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun 	struct doorbell_queue *dbq = filp->private_data;
573*4882a593Smuzhiyun 	unsigned long flags;
574*4882a593Smuzhiyun 	__poll_t mask;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	spin_lock_irqsave(&dbq->lock, flags);
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	poll_wait(filp, &dbq->wait, p);
579*4882a593Smuzhiyun 	mask = (dbq->head == dbq->tail) ? 0 : (EPOLLIN | EPOLLRDNORM);
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dbq->lock, flags);
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	return mask;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun /*
587*4882a593Smuzhiyun  * Return the handles for any incoming doorbells
588*4882a593Smuzhiyun  *
589*4882a593Smuzhiyun  * If there are doorbell handles in the queue for this open instance, then
590*4882a593Smuzhiyun  * return them to the caller as an array of 32-bit integers.  Otherwise,
591*4882a593Smuzhiyun  * block until there is at least one handle to return.
592*4882a593Smuzhiyun  */
fsl_hv_read(struct file * filp,char __user * buf,size_t len,loff_t * off)593*4882a593Smuzhiyun static ssize_t fsl_hv_read(struct file *filp, char __user *buf, size_t len,
594*4882a593Smuzhiyun 			   loff_t *off)
595*4882a593Smuzhiyun {
596*4882a593Smuzhiyun 	struct doorbell_queue *dbq = filp->private_data;
597*4882a593Smuzhiyun 	uint32_t __user *p = (uint32_t __user *) buf; /* for put_user() */
598*4882a593Smuzhiyun 	unsigned long flags;
599*4882a593Smuzhiyun 	ssize_t count = 0;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	/* Make sure we stop when the user buffer is full. */
602*4882a593Smuzhiyun 	while (len >= sizeof(uint32_t)) {
603*4882a593Smuzhiyun 		uint32_t dbell;	/* Local copy of doorbell queue data */
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 		spin_lock_irqsave(&dbq->lock, flags);
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 		/*
608*4882a593Smuzhiyun 		 * If the queue is empty, then either we're done or we need
609*4882a593Smuzhiyun 		 * to block.  If the application specified O_NONBLOCK, then
610*4882a593Smuzhiyun 		 * we return the appropriate error code.
611*4882a593Smuzhiyun 		 */
612*4882a593Smuzhiyun 		if (dbq->head == dbq->tail) {
613*4882a593Smuzhiyun 			spin_unlock_irqrestore(&dbq->lock, flags);
614*4882a593Smuzhiyun 			if (count)
615*4882a593Smuzhiyun 				break;
616*4882a593Smuzhiyun 			if (filp->f_flags & O_NONBLOCK)
617*4882a593Smuzhiyun 				return -EAGAIN;
618*4882a593Smuzhiyun 			if (wait_event_interruptible(dbq->wait,
619*4882a593Smuzhiyun 						     dbq->head != dbq->tail))
620*4882a593Smuzhiyun 				return -ERESTARTSYS;
621*4882a593Smuzhiyun 			continue;
622*4882a593Smuzhiyun 		}
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 		/*
625*4882a593Smuzhiyun 		 * Even though we have an smp_wmb() in the ISR, the core
626*4882a593Smuzhiyun 		 * might speculatively execute the "dbell = ..." below while
627*4882a593Smuzhiyun 		 * it's evaluating the if-statement above.  In that case, the
628*4882a593Smuzhiyun 		 * value put into dbell could be stale if the core accepts the
629*4882a593Smuzhiyun 		 * speculation. To prevent that, we need a read memory barrier
630*4882a593Smuzhiyun 		 * here as well.
631*4882a593Smuzhiyun 		 */
632*4882a593Smuzhiyun 		smp_rmb();
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 		/* Copy the data to a temporary local buffer, because
635*4882a593Smuzhiyun 		 * we can't call copy_to_user() from inside a spinlock
636*4882a593Smuzhiyun 		 */
637*4882a593Smuzhiyun 		dbell = dbq->q[dbq->head];
638*4882a593Smuzhiyun 		dbq->head = nextp(dbq->head);
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 		spin_unlock_irqrestore(&dbq->lock, flags);
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 		if (put_user(dbell, p))
643*4882a593Smuzhiyun 			return -EFAULT;
644*4882a593Smuzhiyun 		p++;
645*4882a593Smuzhiyun 		count += sizeof(uint32_t);
646*4882a593Smuzhiyun 		len -= sizeof(uint32_t);
647*4882a593Smuzhiyun 	}
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	return count;
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun /*
653*4882a593Smuzhiyun  * Open the driver and prepare for reading doorbells.
654*4882a593Smuzhiyun  *
655*4882a593Smuzhiyun  * Every time an application opens the driver, we create a doorbell queue
656*4882a593Smuzhiyun  * for that file handle.  This queue is used for any incoming doorbells.
657*4882a593Smuzhiyun  */
fsl_hv_open(struct inode * inode,struct file * filp)658*4882a593Smuzhiyun static int fsl_hv_open(struct inode *inode, struct file *filp)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun 	struct doorbell_queue *dbq;
661*4882a593Smuzhiyun 	unsigned long flags;
662*4882a593Smuzhiyun 	int ret = 0;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	dbq = kzalloc(sizeof(struct doorbell_queue), GFP_KERNEL);
665*4882a593Smuzhiyun 	if (!dbq) {
666*4882a593Smuzhiyun 		pr_err("fsl-hv: out of memory\n");
667*4882a593Smuzhiyun 		return -ENOMEM;
668*4882a593Smuzhiyun 	}
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	spin_lock_init(&dbq->lock);
671*4882a593Smuzhiyun 	init_waitqueue_head(&dbq->wait);
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	spin_lock_irqsave(&db_list_lock, flags);
674*4882a593Smuzhiyun 	list_add(&dbq->list, &db_list);
675*4882a593Smuzhiyun 	spin_unlock_irqrestore(&db_list_lock, flags);
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	filp->private_data = dbq;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	return ret;
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun /*
683*4882a593Smuzhiyun  * Close the driver
684*4882a593Smuzhiyun  */
fsl_hv_close(struct inode * inode,struct file * filp)685*4882a593Smuzhiyun static int fsl_hv_close(struct inode *inode, struct file *filp)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	struct doorbell_queue *dbq = filp->private_data;
688*4882a593Smuzhiyun 	unsigned long flags;
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	int ret = 0;
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 	spin_lock_irqsave(&db_list_lock, flags);
693*4882a593Smuzhiyun 	list_del(&dbq->list);
694*4882a593Smuzhiyun 	spin_unlock_irqrestore(&db_list_lock, flags);
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	kfree(dbq);
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	return ret;
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun static const struct file_operations fsl_hv_fops = {
702*4882a593Smuzhiyun 	.owner = THIS_MODULE,
703*4882a593Smuzhiyun 	.open = fsl_hv_open,
704*4882a593Smuzhiyun 	.release = fsl_hv_close,
705*4882a593Smuzhiyun 	.poll = fsl_hv_poll,
706*4882a593Smuzhiyun 	.read = fsl_hv_read,
707*4882a593Smuzhiyun 	.unlocked_ioctl = fsl_hv_ioctl,
708*4882a593Smuzhiyun 	.compat_ioctl = compat_ptr_ioctl,
709*4882a593Smuzhiyun };
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun static struct miscdevice fsl_hv_misc_dev = {
712*4882a593Smuzhiyun 	MISC_DYNAMIC_MINOR,
713*4882a593Smuzhiyun 	"fsl-hv",
714*4882a593Smuzhiyun 	&fsl_hv_fops
715*4882a593Smuzhiyun };
716*4882a593Smuzhiyun 
fsl_hv_shutdown_isr(int irq,void * data)717*4882a593Smuzhiyun static irqreturn_t fsl_hv_shutdown_isr(int irq, void *data)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	orderly_poweroff(false);
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	return IRQ_HANDLED;
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun /*
725*4882a593Smuzhiyun  * Returns the handle of the parent of the given node
726*4882a593Smuzhiyun  *
727*4882a593Smuzhiyun  * The handle is the value of the 'hv-handle' property
728*4882a593Smuzhiyun  */
get_parent_handle(struct device_node * np)729*4882a593Smuzhiyun static int get_parent_handle(struct device_node *np)
730*4882a593Smuzhiyun {
731*4882a593Smuzhiyun 	struct device_node *parent;
732*4882a593Smuzhiyun 	const uint32_t *prop;
733*4882a593Smuzhiyun 	uint32_t handle;
734*4882a593Smuzhiyun 	int len;
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	parent = of_get_parent(np);
737*4882a593Smuzhiyun 	if (!parent)
738*4882a593Smuzhiyun 		/* It's not really possible for this to fail */
739*4882a593Smuzhiyun 		return -ENODEV;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	/*
742*4882a593Smuzhiyun 	 * The proper name for the handle property is "hv-handle", but some
743*4882a593Smuzhiyun 	 * older versions of the hypervisor used "reg".
744*4882a593Smuzhiyun 	 */
745*4882a593Smuzhiyun 	prop = of_get_property(parent, "hv-handle", &len);
746*4882a593Smuzhiyun 	if (!prop)
747*4882a593Smuzhiyun 		prop = of_get_property(parent, "reg", &len);
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	if (!prop || (len != sizeof(uint32_t))) {
750*4882a593Smuzhiyun 		/* This can happen only if the node is malformed */
751*4882a593Smuzhiyun 		of_node_put(parent);
752*4882a593Smuzhiyun 		return -ENODEV;
753*4882a593Smuzhiyun 	}
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	handle = be32_to_cpup(prop);
756*4882a593Smuzhiyun 	of_node_put(parent);
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	return handle;
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun /*
762*4882a593Smuzhiyun  * Register a callback for failover events
763*4882a593Smuzhiyun  *
764*4882a593Smuzhiyun  * This function is called by device drivers to register their callback
765*4882a593Smuzhiyun  * functions for fail-over events.
766*4882a593Smuzhiyun  */
fsl_hv_failover_register(struct notifier_block * nb)767*4882a593Smuzhiyun int fsl_hv_failover_register(struct notifier_block *nb)
768*4882a593Smuzhiyun {
769*4882a593Smuzhiyun 	return blocking_notifier_chain_register(&failover_subscribers, nb);
770*4882a593Smuzhiyun }
771*4882a593Smuzhiyun EXPORT_SYMBOL(fsl_hv_failover_register);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun /*
774*4882a593Smuzhiyun  * Unregister a callback for failover events
775*4882a593Smuzhiyun  */
fsl_hv_failover_unregister(struct notifier_block * nb)776*4882a593Smuzhiyun int fsl_hv_failover_unregister(struct notifier_block *nb)
777*4882a593Smuzhiyun {
778*4882a593Smuzhiyun 	return blocking_notifier_chain_unregister(&failover_subscribers, nb);
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun EXPORT_SYMBOL(fsl_hv_failover_unregister);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun /*
783*4882a593Smuzhiyun  * Return TRUE if we're running under FSL hypervisor
784*4882a593Smuzhiyun  *
785*4882a593Smuzhiyun  * This function checks to see if we're running under the Freescale
786*4882a593Smuzhiyun  * hypervisor, and returns zero if we're not, or non-zero if we are.
787*4882a593Smuzhiyun  *
788*4882a593Smuzhiyun  * First, it checks if MSR[GS]==1, which means we're running under some
789*4882a593Smuzhiyun  * hypervisor.  Then it checks if there is a hypervisor node in the device
790*4882a593Smuzhiyun  * tree.  Currently, that means there needs to be a node in the root called
791*4882a593Smuzhiyun  * "hypervisor" and which has a property named "fsl,hv-version".
792*4882a593Smuzhiyun  */
has_fsl_hypervisor(void)793*4882a593Smuzhiyun static int has_fsl_hypervisor(void)
794*4882a593Smuzhiyun {
795*4882a593Smuzhiyun 	struct device_node *node;
796*4882a593Smuzhiyun 	int ret;
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	node = of_find_node_by_path("/hypervisor");
799*4882a593Smuzhiyun 	if (!node)
800*4882a593Smuzhiyun 		return 0;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	ret = of_find_property(node, "fsl,hv-version", NULL) != NULL;
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	of_node_put(node);
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	return ret;
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun /*
810*4882a593Smuzhiyun  * Freescale hypervisor management driver init
811*4882a593Smuzhiyun  *
812*4882a593Smuzhiyun  * This function is called when this module is loaded.
813*4882a593Smuzhiyun  *
814*4882a593Smuzhiyun  * Register ourselves as a miscellaneous driver.  This will register the
815*4882a593Smuzhiyun  * fops structure and create the right sysfs entries for udev.
816*4882a593Smuzhiyun  */
fsl_hypervisor_init(void)817*4882a593Smuzhiyun static int __init fsl_hypervisor_init(void)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun 	struct device_node *np;
820*4882a593Smuzhiyun 	struct doorbell_isr *dbisr, *n;
821*4882a593Smuzhiyun 	int ret;
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	pr_info("Freescale hypervisor management driver\n");
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	if (!has_fsl_hypervisor()) {
826*4882a593Smuzhiyun 		pr_info("fsl-hv: no hypervisor found\n");
827*4882a593Smuzhiyun 		return -ENODEV;
828*4882a593Smuzhiyun 	}
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	ret = misc_register(&fsl_hv_misc_dev);
831*4882a593Smuzhiyun 	if (ret) {
832*4882a593Smuzhiyun 		pr_err("fsl-hv: cannot register device\n");
833*4882a593Smuzhiyun 		return ret;
834*4882a593Smuzhiyun 	}
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	INIT_LIST_HEAD(&db_list);
837*4882a593Smuzhiyun 	INIT_LIST_HEAD(&isr_list);
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	for_each_compatible_node(np, NULL, "epapr,hv-receive-doorbell") {
840*4882a593Smuzhiyun 		unsigned int irq;
841*4882a593Smuzhiyun 		const uint32_t *handle;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 		handle = of_get_property(np, "interrupts", NULL);
844*4882a593Smuzhiyun 		irq = irq_of_parse_and_map(np, 0);
845*4882a593Smuzhiyun 		if (!handle || (irq == NO_IRQ)) {
846*4882a593Smuzhiyun 			pr_err("fsl-hv: no 'interrupts' property in %pOF node\n",
847*4882a593Smuzhiyun 				np);
848*4882a593Smuzhiyun 			continue;
849*4882a593Smuzhiyun 		}
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 		dbisr = kzalloc(sizeof(*dbisr), GFP_KERNEL);
852*4882a593Smuzhiyun 		if (!dbisr)
853*4882a593Smuzhiyun 			goto out_of_memory;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 		dbisr->irq = irq;
856*4882a593Smuzhiyun 		dbisr->doorbell = be32_to_cpup(handle);
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 		if (of_device_is_compatible(np, "fsl,hv-shutdown-doorbell")) {
859*4882a593Smuzhiyun 			/* The shutdown doorbell gets its own ISR */
860*4882a593Smuzhiyun 			ret = request_irq(irq, fsl_hv_shutdown_isr, 0,
861*4882a593Smuzhiyun 					  np->name, NULL);
862*4882a593Smuzhiyun 		} else if (of_device_is_compatible(np,
863*4882a593Smuzhiyun 			"fsl,hv-state-change-doorbell")) {
864*4882a593Smuzhiyun 			/*
865*4882a593Smuzhiyun 			 * The state change doorbell triggers a notification if
866*4882a593Smuzhiyun 			 * the state of the managed partition changes to
867*4882a593Smuzhiyun 			 * "stopped". We need a separate interrupt handler for
868*4882a593Smuzhiyun 			 * that, and we also need to know the handle of the
869*4882a593Smuzhiyun 			 * target partition, not just the handle of the
870*4882a593Smuzhiyun 			 * doorbell.
871*4882a593Smuzhiyun 			 */
872*4882a593Smuzhiyun 			dbisr->partition = ret = get_parent_handle(np);
873*4882a593Smuzhiyun 			if (ret < 0) {
874*4882a593Smuzhiyun 				pr_err("fsl-hv: node %pOF has missing or "
875*4882a593Smuzhiyun 				       "malformed parent\n", np);
876*4882a593Smuzhiyun 				kfree(dbisr);
877*4882a593Smuzhiyun 				continue;
878*4882a593Smuzhiyun 			}
879*4882a593Smuzhiyun 			ret = request_threaded_irq(irq, fsl_hv_state_change_isr,
880*4882a593Smuzhiyun 						   fsl_hv_state_change_thread,
881*4882a593Smuzhiyun 						   0, np->name, dbisr);
882*4882a593Smuzhiyun 		} else
883*4882a593Smuzhiyun 			ret = request_irq(irq, fsl_hv_isr, 0, np->name, dbisr);
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 		if (ret < 0) {
886*4882a593Smuzhiyun 			pr_err("fsl-hv: could not request irq %u for node %pOF\n",
887*4882a593Smuzhiyun 			       irq, np);
888*4882a593Smuzhiyun 			kfree(dbisr);
889*4882a593Smuzhiyun 			continue;
890*4882a593Smuzhiyun 		}
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 		list_add(&dbisr->list, &isr_list);
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 		pr_info("fsl-hv: registered handler for doorbell %u\n",
895*4882a593Smuzhiyun 			dbisr->doorbell);
896*4882a593Smuzhiyun 	}
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	return 0;
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun out_of_memory:
901*4882a593Smuzhiyun 	list_for_each_entry_safe(dbisr, n, &isr_list, list) {
902*4882a593Smuzhiyun 		free_irq(dbisr->irq, dbisr);
903*4882a593Smuzhiyun 		list_del(&dbisr->list);
904*4882a593Smuzhiyun 		kfree(dbisr);
905*4882a593Smuzhiyun 	}
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	misc_deregister(&fsl_hv_misc_dev);
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	return -ENOMEM;
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun /*
913*4882a593Smuzhiyun  * Freescale hypervisor management driver termination
914*4882a593Smuzhiyun  *
915*4882a593Smuzhiyun  * This function is called when this driver is unloaded.
916*4882a593Smuzhiyun  */
fsl_hypervisor_exit(void)917*4882a593Smuzhiyun static void __exit fsl_hypervisor_exit(void)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun 	struct doorbell_isr *dbisr, *n;
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	list_for_each_entry_safe(dbisr, n, &isr_list, list) {
922*4882a593Smuzhiyun 		free_irq(dbisr->irq, dbisr);
923*4882a593Smuzhiyun 		list_del(&dbisr->list);
924*4882a593Smuzhiyun 		kfree(dbisr);
925*4882a593Smuzhiyun 	}
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	misc_deregister(&fsl_hv_misc_dev);
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun module_init(fsl_hypervisor_init);
931*4882a593Smuzhiyun module_exit(fsl_hypervisor_exit);
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
934*4882a593Smuzhiyun MODULE_DESCRIPTION("Freescale hypervisor management driver");
935*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
936