2 * Freescale Hypervisor Management Driver
4 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc.
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.
11 * The Freescale hypervisor management driver provides several services to
12 * drivers and applications related to the Freescale hypervisor:
14 * 1. An ioctl interface for querying and managing partitions.
16 * 2. A file interface to reading incoming doorbells.
18 * 3. An interrupt handler for shutting down the partition upon receiving the
19 * shutdown doorbell from a manager partition.
21 * 4. A kernel interface for receiving callbacks when a managed partition
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>
31 #include <linux/miscdevice.h>
33 #include <linux/pagemap.h>
34 #include <linux/slab.h>
35 #include <linux/poll.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>
44 #include <asm/fsl_hcalls.h>
46 #include <linux/fsl_hypervisor.h>
48 static BLOCKING_NOTIFIER_HEAD(failover_subscribers);
51 * Ioctl interface for FSL_HV_IOCTL_PARTITION_RESTART
53 * Restart a running partition
55 static long ioctl_restart(struct fsl_hv_ioctl_restart __user *p)
57 struct fsl_hv_ioctl_restart param;
59 /* Get the parameters from the user */
60 if (copy_from_user(¶m, p, sizeof(struct fsl_hv_ioctl_restart)))
63 param.ret = fh_partition_restart(param.partition);
65 if (copy_to_user(&p->ret, ¶m.ret, sizeof(__u32)))
72 * Ioctl interface for FSL_HV_IOCTL_PARTITION_STATUS
74 * Query the status of a partition
76 static long ioctl_status(struct fsl_hv_ioctl_status __user *p)
78 struct fsl_hv_ioctl_status param;
81 /* Get the parameters from the user */
82 if (copy_from_user(¶m, p, sizeof(struct fsl_hv_ioctl_status)))
85 param.ret = fh_partition_get_status(param.partition, &status);
87 param.status = status;
89 if (copy_to_user(p, ¶m, sizeof(struct fsl_hv_ioctl_status)))
96 * Ioctl interface for FSL_HV_IOCTL_PARTITION_START
98 * Start a stopped partition.
100 static long ioctl_start(struct fsl_hv_ioctl_start __user *p)
102 struct fsl_hv_ioctl_start param;
104 /* Get the parameters from the user */
105 if (copy_from_user(¶m, p, sizeof(struct fsl_hv_ioctl_start)))
108 param.ret = fh_partition_start(param.partition, param.entry_point,
111 if (copy_to_user(&p->ret, ¶m.ret, sizeof(__u32)))
118 * Ioctl interface for FSL_HV_IOCTL_PARTITION_STOP
120 * Stop a running partition
122 static long ioctl_stop(struct fsl_hv_ioctl_stop __user *p)
124 struct fsl_hv_ioctl_stop param;
126 /* Get the parameters from the user */
127 if (copy_from_user(¶m, p, sizeof(struct fsl_hv_ioctl_stop)))
130 param.ret = fh_partition_stop(param.partition);
132 if (copy_to_user(&p->ret, ¶m.ret, sizeof(__u32)))
139 * Ioctl interface for FSL_HV_IOCTL_MEMCPY
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.
147 static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
149 struct fsl_hv_ioctl_memcpy param;
151 struct page **pages = NULL;
152 void *sg_list_unaligned = NULL;
153 struct fh_sg_list *sg_list = NULL;
155 unsigned int num_pages;
156 unsigned long lb_offset; /* Offset within a page of the local buffer */
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 */
164 /* Get the parameters from the user */
165 if (copy_from_user(¶m, p, sizeof(struct fsl_hv_ioctl_memcpy)))
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
173 if ((param.source == -1) == (param.target == -1))
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.
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
185 * ---- <-- first page starts before the buffer
206 * | | <-- last page ends after the buffer
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.
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
217 lb_offset = param.local_vaddr & (PAGE_SIZE - 1);
218 num_pages = (param.count + lb_offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
220 /* Allocate the buffers we need */
223 * 'pages' is an array of struct page pointers that's initialized by
226 pages = kzalloc(num_pages * sizeof(struct page *), GFP_KERNEL);
228 pr_debug("fsl-hv: could not allocate page list\n");
233 * sg_list is the list of fh_sg_list objects that we pass to the
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");
243 sg_list = PTR_ALIGN(sg_list_unaligned, sizeof(struct fh_sg_list));
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);
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;
257 * Build the fh_sg_list[] array. The first page is special
258 * because it's misaligned.
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;
264 sg_list[0].source = param.remote_paddr;
265 sg_list[0].target = page_to_phys(pages[0]) + lb_offset;
267 sg_list[0].size = min_t(uint64_t, param.count, PAGE_SIZE - lb_offset);
269 remote_paddr = param.remote_paddr + sg_list[0].size;
270 count = param.count - sg_list[0].size;
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;
278 /* remote to local */
279 sg_list[i].source = remote_paddr;
280 sg_list[i].target = page_to_phys(pages[i]);
282 sg_list[i].size = min_t(uint64_t, count, PAGE_SIZE);
284 remote_paddr += sg_list[i].size;
285 count -= sg_list[i].size;
288 param.ret = fh_partition_memcpy(param.source, param.target,
289 virt_to_phys(sg_list), num_pages);
293 for (i = 0; i < num_pages; i++)
298 kfree(sg_list_unaligned);
302 if (copy_to_user(&p->ret, ¶m.ret, sizeof(__u32)))
309 * Ioctl interface for FSL_HV_IOCTL_DOORBELL
313 static long ioctl_doorbell(struct fsl_hv_ioctl_doorbell __user *p)
315 struct fsl_hv_ioctl_doorbell param;
317 /* Get the parameters from the user. */
318 if (copy_from_user(¶m, p, sizeof(struct fsl_hv_ioctl_doorbell)))
321 param.ret = ev_doorbell_send(param.doorbell);
323 if (copy_to_user(&p->ret, ¶m.ret, sizeof(__u32)))
329 static long ioctl_dtprop(struct fsl_hv_ioctl_prop __user *p, int set)
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;
338 /* Get the parameters from the user. */
339 if (copy_from_user(¶m, p, sizeof(struct fsl_hv_ioctl_prop)))
342 upath = (char __user *)(uintptr_t)param.path;
343 upropname = (char __user *)(uintptr_t)param.propname;
344 upropval = (void __user *)(uintptr_t)param.propval;
346 path = strndup_user(upath, FH_DTPROP_MAX_PATHLEN);
352 propname = strndup_user(upropname, FH_DTPROP_MAX_PATHLEN);
353 if (IS_ERR(propname)) {
354 ret = PTR_ERR(propname);
358 if (param.proplen > FH_DTPROP_MAX_PROPLEN) {
363 propval = kmalloc(param.proplen, GFP_KERNEL);
370 if (copy_from_user(propval, upropval, param.proplen)) {
375 param.ret = fh_partition_set_dtprop(param.handle,
377 virt_to_phys(propname),
378 virt_to_phys(propval),
381 param.ret = fh_partition_get_dtprop(param.handle,
383 virt_to_phys(propname),
384 virt_to_phys(propval),
387 if (param.ret == 0) {
388 if (copy_to_user(upropval, propval, param.proplen) ||
389 put_user(param.proplen, &p->proplen)) {
396 if (put_user(param.ret, &p->ret))
408 * Ioctl main entry point
410 static long fsl_hv_ioctl(struct file *file, unsigned int cmd,
411 unsigned long argaddr)
413 void __user *arg = (void __user *)argaddr;
417 case FSL_HV_IOCTL_PARTITION_RESTART:
418 ret = ioctl_restart(arg);
420 case FSL_HV_IOCTL_PARTITION_GET_STATUS:
421 ret = ioctl_status(arg);
423 case FSL_HV_IOCTL_PARTITION_START:
424 ret = ioctl_start(arg);
426 case FSL_HV_IOCTL_PARTITION_STOP:
427 ret = ioctl_stop(arg);
429 case FSL_HV_IOCTL_MEMCPY:
430 ret = ioctl_memcpy(arg);
432 case FSL_HV_IOCTL_DOORBELL:
433 ret = ioctl_doorbell(arg);
435 case FSL_HV_IOCTL_GETPROP:
436 ret = ioctl_dtprop(arg, 0);
438 case FSL_HV_IOCTL_SETPROP:
439 ret = ioctl_dtprop(arg, 1);
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),
451 /* Linked list of processes that have us open */
452 static struct list_head db_list;
454 /* spinlock for db_list */
455 static DEFINE_SPINLOCK(db_list_lock);
457 /* The size of the doorbell event queue. This must be a power of two. */
460 /* Returns the next head/tail pointer, wrapping around the queue if necessary */
461 #define nextp(x) (((x) + 1) & (QSIZE - 1))
463 /* Per-open data structure */
464 struct doorbell_queue {
465 struct list_head list;
467 wait_queue_head_t wait;
473 /* Linked list of ISRs that we registered */
474 struct list_head isr_list;
476 /* Per-ISR data structure */
477 struct doorbell_isr {
478 struct list_head list;
480 uint32_t doorbell; /* The doorbell handle */
481 uint32_t partition; /* The partition handle, if used */
485 * Add a doorbell to all of the doorbell queues
487 static void fsl_hv_queue_doorbell(uint32_t doorbell)
489 struct doorbell_queue *dbq;
492 /* Prevent another core from modifying db_list */
493 spin_lock_irqsave(&db_list_lock, flags);
495 list_for_each_entry(dbq, &db_list, list) {
496 if (dbq->head != nextp(dbq->tail)) {
497 dbq->q[dbq->tail] = doorbell;
499 * This memory barrier eliminates the need to grab
500 * the spinlock for dbq.
503 dbq->tail = nextp(dbq->tail);
504 wake_up_interruptible(&dbq->wait);
508 spin_unlock_irqrestore(&db_list_lock, flags);
512 * Interrupt handler for all doorbells
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.
518 static irqreturn_t fsl_hv_isr(int irq, void *data)
520 fsl_hv_queue_doorbell((uintptr_t) data);
526 * State change thread function
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.
535 static irqreturn_t fsl_hv_state_change_thread(int irq, void *data)
537 struct doorbell_isr *dbisr = data;
539 blocking_notifier_call_chain(&failover_subscribers, dbisr->partition,
546 * Interrupt handler for state-change doorbells
548 static irqreturn_t fsl_hv_state_change_isr(int irq, void *data)
551 struct doorbell_isr *dbisr = data;
554 /* It's still a doorbell, so add it to all the queues. */
555 fsl_hv_queue_doorbell(dbisr->doorbell);
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;
566 * Returns a bitmask indicating whether a read will block
568 static __poll_t fsl_hv_poll(struct file *filp, struct poll_table_struct *p)
570 struct doorbell_queue *dbq = filp->private_data;
574 spin_lock_irqsave(&dbq->lock, flags);
576 poll_wait(filp, &dbq->wait, p);
577 mask = (dbq->head == dbq->tail) ? 0 : (EPOLLIN | EPOLLRDNORM);
579 spin_unlock_irqrestore(&dbq->lock, flags);
585 * Return the handles for any incoming doorbells
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.
591 static ssize_t fsl_hv_read(struct file *filp, char __user *buf, size_t len,
594 struct doorbell_queue *dbq = filp->private_data;
595 uint32_t __user *p = (uint32_t __user *) buf; /* for put_user() */
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 */
603 spin_lock_irqsave(&dbq->lock, flags);
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.
610 if (dbq->head == dbq->tail) {
611 spin_unlock_irqrestore(&dbq->lock, flags);
614 if (filp->f_flags & O_NONBLOCK)
616 if (wait_event_interruptible(dbq->wait,
617 dbq->head != dbq->tail))
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
632 /* Copy the data to a temporary local buffer, because
633 * we can't call copy_to_user() from inside a spinlock
635 dbell = dbq->q[dbq->head];
636 dbq->head = nextp(dbq->head);
638 spin_unlock_irqrestore(&dbq->lock, flags);
640 if (put_user(dbell, p))
643 count += sizeof(uint32_t);
644 len -= sizeof(uint32_t);
651 * Open the driver and prepare for reading doorbells.
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.
656 static int fsl_hv_open(struct inode *inode, struct file *filp)
658 struct doorbell_queue *dbq;
662 dbq = kzalloc(sizeof(struct doorbell_queue), GFP_KERNEL);
664 pr_err("fsl-hv: out of memory\n");
668 spin_lock_init(&dbq->lock);
669 init_waitqueue_head(&dbq->wait);
671 spin_lock_irqsave(&db_list_lock, flags);
672 list_add(&dbq->list, &db_list);
673 spin_unlock_irqrestore(&db_list_lock, flags);
675 filp->private_data = dbq;
683 static int fsl_hv_close(struct inode *inode, struct file *filp)
685 struct doorbell_queue *dbq = filp->private_data;
690 spin_lock_irqsave(&db_list_lock, flags);
691 list_del(&dbq->list);
692 spin_unlock_irqrestore(&db_list_lock, flags);
699 static const struct file_operations fsl_hv_fops = {
700 .owner = THIS_MODULE,
702 .release = fsl_hv_close,
705 .unlocked_ioctl = fsl_hv_ioctl,
706 .compat_ioctl = fsl_hv_ioctl,
709 static struct miscdevice fsl_hv_misc_dev = {
715 static irqreturn_t fsl_hv_shutdown_isr(int irq, void *data)
717 orderly_poweroff(false);
723 * Returns the handle of the parent of the given node
725 * The handle is the value of the 'hv-handle' property
727 static int get_parent_handle(struct device_node *np)
729 struct device_node *parent;
730 const uint32_t *prop;
734 parent = of_get_parent(np);
736 /* It's not really possible for this to fail */
740 * The proper name for the handle property is "hv-handle", but some
741 * older versions of the hypervisor used "reg".
743 prop = of_get_property(parent, "hv-handle", &len);
745 prop = of_get_property(parent, "reg", &len);
747 if (!prop || (len != sizeof(uint32_t))) {
748 /* This can happen only if the node is malformed */
753 handle = be32_to_cpup(prop);
760 * Register a callback for failover events
762 * This function is called by device drivers to register their callback
763 * functions for fail-over events.
765 int fsl_hv_failover_register(struct notifier_block *nb)
767 return blocking_notifier_chain_register(&failover_subscribers, nb);
769 EXPORT_SYMBOL(fsl_hv_failover_register);
772 * Unregister a callback for failover events
774 int fsl_hv_failover_unregister(struct notifier_block *nb)
776 return blocking_notifier_chain_unregister(&failover_subscribers, nb);
778 EXPORT_SYMBOL(fsl_hv_failover_unregister);
781 * Return TRUE if we're running under FSL hypervisor
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.
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".
791 static int has_fsl_hypervisor(void)
793 struct device_node *node;
796 node = of_find_node_by_path("/hypervisor");
800 ret = of_find_property(node, "fsl,hv-version", NULL) != NULL;
808 * Freescale hypervisor management driver init
810 * This function is called when this module is loaded.
812 * Register ourselves as a miscellaneous driver. This will register the
813 * fops structure and create the right sysfs entries for udev.
815 static int __init fsl_hypervisor_init(void)
817 struct device_node *np;
818 struct doorbell_isr *dbisr, *n;
821 pr_info("Freescale hypervisor management driver\n");
823 if (!has_fsl_hypervisor()) {
824 pr_info("fsl-hv: no hypervisor found\n");
828 ret = misc_register(&fsl_hv_misc_dev);
830 pr_err("fsl-hv: cannot register device\n");
834 INIT_LIST_HEAD(&db_list);
835 INIT_LIST_HEAD(&isr_list);
837 for_each_compatible_node(np, NULL, "epapr,hv-receive-doorbell") {
839 const uint32_t *handle;
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",
849 dbisr = kzalloc(sizeof(*dbisr), GFP_KERNEL);
854 dbisr->doorbell = be32_to_cpup(handle);
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,
860 } else if (of_device_is_compatible(np,
861 "fsl,hv-state-change-doorbell")) {
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
870 dbisr->partition = ret = get_parent_handle(np);
872 pr_err("fsl-hv: node %pOF has missing or "
873 "malformed parent\n", np);
877 ret = request_threaded_irq(irq, fsl_hv_state_change_isr,
878 fsl_hv_state_change_thread,
881 ret = request_irq(irq, fsl_hv_isr, 0, np->name, dbisr);
884 pr_err("fsl-hv: could not request irq %u for node %pOF\n",
890 list_add(&dbisr->list, &isr_list);
892 pr_info("fsl-hv: registered handler for doorbell %u\n",
899 list_for_each_entry_safe(dbisr, n, &isr_list, list) {
900 free_irq(dbisr->irq, dbisr);
901 list_del(&dbisr->list);
905 misc_deregister(&fsl_hv_misc_dev);
911 * Freescale hypervisor management driver termination
913 * This function is called when this driver is unloaded.
915 static void __exit fsl_hypervisor_exit(void)
917 struct doorbell_isr *dbisr, *n;
919 list_for_each_entry_safe(dbisr, n, &isr_list, list) {
920 free_irq(dbisr->irq, dbisr);
921 list_del(&dbisr->list);
925 misc_deregister(&fsl_hv_misc_dev);
928 module_init(fsl_hypervisor_init);
929 module_exit(fsl_hypervisor_exit);
932 MODULE_DESCRIPTION("Freescale hypervisor management driver");
933 MODULE_LICENSE("GPL v2");