Linux Audio

Check our new training course

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