Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / virt / fsl_hypervisor.c
blob46ee0a0998b6f74e7f998fc759fd5e30002086d8
1 /*
2 * Freescale Hypervisor Management Driver
4 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc.
5 * Author: Timur Tabi <timur@freescale.com>
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
22 * shuts down.
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>
43 #include <linux/io.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(&param, p, sizeof(struct fsl_hv_ioctl_restart)))
61 return -EFAULT;
63 param.ret = fh_partition_restart(param.partition);
65 if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
66 return -EFAULT;
68 return 0;
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;
79 u32 status;
81 /* Get the parameters from the user */
82 if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_status)))
83 return -EFAULT;
85 param.ret = fh_partition_get_status(param.partition, &status);
86 if (!param.ret)
87 param.status = status;
89 if (copy_to_user(p, &param, sizeof(struct fsl_hv_ioctl_status)))
90 return -EFAULT;
92 return 0;
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(&param, p, sizeof(struct fsl_hv_ioctl_start)))
106 return -EFAULT;
108 param.ret = fh_partition_start(param.partition, param.entry_point,
109 param.load);
111 if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
112 return -EFAULT;
114 return 0;
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(&param, p, sizeof(struct fsl_hv_ioctl_stop)))
128 return -EFAULT;
130 param.ret = fh_partition_stop(param.partition);
132 if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
133 return -EFAULT;
135 return 0;
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 */
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 */
164 /* Get the parameters from the user */
165 if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_memcpy)))
166 return -EFAULT;
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.
173 if ((param.source == -1) == (param.target == -1))
174 return -EINVAL;
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.
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:
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 * ----
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
215 * hypervisor.
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;
223 /* Allocate the buffers we need */
226 * 'pages' is an array of struct page pointers that's initialized by
227 * get_user_pages_fast().
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;
236 * sg_list is the list of fh_sg_list objects that we pass to the
237 * hypervisor.
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;
246 sg_list = PTR_ALIGN(sg_list_unaligned, sizeof(struct fh_sg_list));
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);
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;
259 * Build the fh_sg_list[] array. The first page is special
260 * because it's misaligned.
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;
269 sg_list[0].size = min_t(uint64_t, param.count, PAGE_SIZE - lb_offset);
271 remote_paddr = param.remote_paddr + sg_list[0].size;
272 count = param.count - sg_list[0].size;
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]);
284 sg_list[i].size = min_t(uint64_t, count, PAGE_SIZE);
286 remote_paddr += sg_list[i].size;
287 count -= sg_list[i].size;
290 param.ret = fh_partition_memcpy(param.source, param.target,
291 virt_to_phys(sg_list), num_pages);
293 exit:
294 if (pages && (num_pinned > 0)) {
295 for (i = 0; i < num_pinned; i++)
296 put_page(pages[i]);
299 kfree(sg_list_unaligned);
300 free_pages:
301 kfree(pages);
303 if (!ret)
304 if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
305 return -EFAULT;
307 return ret;
311 * Ioctl interface for FSL_HV_IOCTL_DOORBELL
313 * Ring a doorbell
315 static long ioctl_doorbell(struct fsl_hv_ioctl_doorbell __user *p)
317 struct fsl_hv_ioctl_doorbell param;
319 /* Get the parameters from the user. */
320 if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_doorbell)))
321 return -EFAULT;
323 param.ret = ev_doorbell_send(param.doorbell);
325 if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
326 return -EFAULT;
328 return 0;
331 static long ioctl_dtprop(struct fsl_hv_ioctl_prop __user *p, int set)
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;
340 /* Get the parameters from the user. */
341 if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_prop)))
342 return -EFAULT;
344 upath = (char __user *)(uintptr_t)param.path;
345 upropname = (char __user *)(uintptr_t)param.propname;
346 upropval = (void __user *)(uintptr_t)param.propval;
348 path = strndup_user(upath, FH_DTPROP_MAX_PATHLEN);
349 if (IS_ERR(path))
350 return PTR_ERR(path);
352 propname = strndup_user(upropname, FH_DTPROP_MAX_PATHLEN);
353 if (IS_ERR(propname)) {
354 ret = PTR_ERR(propname);
355 goto err_free_path;
358 if (param.proplen > FH_DTPROP_MAX_PROPLEN) {
359 ret = -EINVAL;
360 goto err_free_propname;
363 propval = kmalloc(param.proplen, GFP_KERNEL);
364 if (!propval) {
365 ret = -ENOMEM;
366 goto err_free_propname;
369 if (set) {
370 if (copy_from_user(propval, upropval, param.proplen)) {
371 ret = -EFAULT;
372 goto err_free_propval;
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);
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;
396 if (put_user(param.ret, &p->ret))
397 ret = -EFAULT;
399 err_free_propval:
400 kfree(propval);
401 err_free_propname:
402 kfree(propname);
403 err_free_path:
404 kfree(path);
406 return ret;
410 * Ioctl main entry point
412 static long fsl_hv_ioctl(struct file *file, unsigned int cmd,
413 unsigned long argaddr)
415 void __user *arg = (void __user *)argaddr;
416 long ret;
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;
450 return ret;
453 /* Linked list of processes that have us open */
454 static struct list_head db_list;
456 /* spinlock for db_list */
457 static DEFINE_SPINLOCK(db_list_lock);
459 /* The size of the doorbell event queue. This must be a power of two. */
460 #define QSIZE 16
462 /* Returns the next head/tail pointer, wrapping around the queue if necessary */
463 #define nextp(x) (((x) + 1) & (QSIZE - 1))
465 /* Per-open data structure */
466 struct 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];
475 /* Linked list of ISRs that we registered */
476 struct list_head isr_list;
478 /* Per-ISR data structure */
479 struct 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 */
487 * Add a doorbell to all of the doorbell queues
489 static void fsl_hv_queue_doorbell(uint32_t doorbell)
491 struct doorbell_queue *dbq;
492 unsigned long flags;
494 /* Prevent another core from modifying db_list */
495 spin_lock_irqsave(&db_list_lock, flags);
497 list_for_each_entry(dbq, &db_list, list) {
498 if (dbq->head != nextp(dbq->tail)) {
499 dbq->q[dbq->tail] = doorbell;
501 * This memory barrier eliminates the need to grab
502 * the spinlock for dbq.
504 smp_wmb();
505 dbq->tail = nextp(dbq->tail);
506 wake_up_interruptible(&dbq->wait);
510 spin_unlock_irqrestore(&db_list_lock, flags);
514 * Interrupt handler for all doorbells
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.
520 static irqreturn_t fsl_hv_isr(int irq, void *data)
522 fsl_hv_queue_doorbell((uintptr_t) data);
524 return IRQ_HANDLED;
528 * State change thread function
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.
537 static irqreturn_t fsl_hv_state_change_thread(int irq, void *data)
539 struct doorbell_isr *dbisr = data;
541 blocking_notifier_call_chain(&failover_subscribers, dbisr->partition,
542 NULL);
544 return IRQ_HANDLED;
548 * Interrupt handler for state-change doorbells
550 static irqreturn_t fsl_hv_state_change_isr(int irq, void *data)
552 unsigned int status;
553 struct doorbell_isr *dbisr = data;
554 int ret;
556 /* It's still a doorbell, so add it to all the queues. */
557 fsl_hv_queue_doorbell(dbisr->doorbell);
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;
564 return IRQ_HANDLED;
568 * Returns a bitmask indicating whether a read will block
570 static __poll_t fsl_hv_poll(struct file *filp, struct poll_table_struct *p)
572 struct doorbell_queue *dbq = filp->private_data;
573 unsigned long flags;
574 __poll_t mask;
576 spin_lock_irqsave(&dbq->lock, flags);
578 poll_wait(filp, &dbq->wait, p);
579 mask = (dbq->head == dbq->tail) ? 0 : (EPOLLIN | EPOLLRDNORM);
581 spin_unlock_irqrestore(&dbq->lock, flags);
583 return mask;
587 * Return the handles for any incoming doorbells
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.
593 static ssize_t fsl_hv_read(struct file *filp, char __user *buf, size_t len,
594 loff_t *off)
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;
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 */
605 spin_lock_irqsave(&dbq->lock, flags);
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.
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;
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.
632 smp_rmb();
634 /* Copy the data to a temporary local buffer, because
635 * we can't call copy_to_user() from inside a spinlock
637 dbell = dbq->q[dbq->head];
638 dbq->head = nextp(dbq->head);
640 spin_unlock_irqrestore(&dbq->lock, flags);
642 if (put_user(dbell, p))
643 return -EFAULT;
644 p++;
645 count += sizeof(uint32_t);
646 len -= sizeof(uint32_t);
649 return count;
653 * Open the driver and prepare for reading doorbells.
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.
658 static int fsl_hv_open(struct inode *inode, struct file *filp)
660 struct doorbell_queue *dbq;
661 unsigned long flags;
662 int ret = 0;
664 dbq = kzalloc(sizeof(struct doorbell_queue), GFP_KERNEL);
665 if (!dbq) {
666 pr_err("fsl-hv: out of memory\n");
667 return -ENOMEM;
670 spin_lock_init(&dbq->lock);
671 init_waitqueue_head(&dbq->wait);
673 spin_lock_irqsave(&db_list_lock, flags);
674 list_add(&dbq->list, &db_list);
675 spin_unlock_irqrestore(&db_list_lock, flags);
677 filp->private_data = dbq;
679 return ret;
683 * Close the driver
685 static int fsl_hv_close(struct inode *inode, struct file *filp)
687 struct doorbell_queue *dbq = filp->private_data;
688 unsigned long flags;
690 int ret = 0;
692 spin_lock_irqsave(&db_list_lock, flags);
693 list_del(&dbq->list);
694 spin_unlock_irqrestore(&db_list_lock, flags);
696 kfree(dbq);
698 return ret;
701 static const struct file_operations fsl_hv_fops = {
702 .owner = THIS_MODULE,
703 .open = fsl_hv_open,
704 .release = fsl_hv_close,
705 .poll = fsl_hv_poll,
706 .read = fsl_hv_read,
707 .unlocked_ioctl = fsl_hv_ioctl,
708 .compat_ioctl = compat_ptr_ioctl,
711 static struct miscdevice fsl_hv_misc_dev = {
712 MISC_DYNAMIC_MINOR,
713 "fsl-hv",
714 &fsl_hv_fops
717 static irqreturn_t fsl_hv_shutdown_isr(int irq, void *data)
719 orderly_poweroff(false);
721 return IRQ_HANDLED;
725 * Returns the handle of the parent of the given node
727 * The handle is the value of the 'hv-handle' property
729 static int get_parent_handle(struct device_node *np)
731 struct device_node *parent;
732 const uint32_t *prop;
733 uint32_t handle;
734 int len;
736 parent = of_get_parent(np);
737 if (!parent)
738 /* It's not really possible for this to fail */
739 return -ENODEV;
742 * The proper name for the handle property is "hv-handle", but some
743 * older versions of the hypervisor used "reg".
745 prop = of_get_property(parent, "hv-handle", &len);
746 if (!prop)
747 prop = of_get_property(parent, "reg", &len);
749 if (!prop || (len != sizeof(uint32_t))) {
750 /* This can happen only if the node is malformed */
751 of_node_put(parent);
752 return -ENODEV;
755 handle = be32_to_cpup(prop);
756 of_node_put(parent);
758 return handle;
762 * Register a callback for failover events
764 * This function is called by device drivers to register their callback
765 * functions for fail-over events.
767 int fsl_hv_failover_register(struct notifier_block *nb)
769 return blocking_notifier_chain_register(&failover_subscribers, nb);
771 EXPORT_SYMBOL(fsl_hv_failover_register);
774 * Unregister a callback for failover events
776 int fsl_hv_failover_unregister(struct notifier_block *nb)
778 return blocking_notifier_chain_unregister(&failover_subscribers, nb);
780 EXPORT_SYMBOL(fsl_hv_failover_unregister);
783 * Return TRUE if we're running under FSL hypervisor
785 * This function checks to see if we're running under the Freescale
786 * hypervisor, and returns zero if we're not, or non-zero if we are.
788 * First, it checks if MSR[GS]==1, which means we're running under some
789 * hypervisor. Then it checks if there is a hypervisor node in the device
790 * tree. Currently, that means there needs to be a node in the root called
791 * "hypervisor" and which has a property named "fsl,hv-version".
793 static int has_fsl_hypervisor(void)
795 struct device_node *node;
796 int ret;
798 node = of_find_node_by_path("/hypervisor");
799 if (!node)
800 return 0;
802 ret = of_find_property(node, "fsl,hv-version", NULL) != NULL;
804 of_node_put(node);
806 return ret;
810 * Freescale hypervisor management driver init
812 * This function is called when this module is loaded.
814 * Register ourselves as a miscellaneous driver. This will register the
815 * fops structure and create the right sysfs entries for udev.
817 static int __init fsl_hypervisor_init(void)
819 struct device_node *np;
820 struct doorbell_isr *dbisr, *n;
821 int ret;
823 pr_info("Freescale hypervisor management driver\n");
825 if (!has_fsl_hypervisor()) {
826 pr_info("fsl-hv: no hypervisor found\n");
827 return -ENODEV;
830 ret = misc_register(&fsl_hv_misc_dev);
831 if (ret) {
832 pr_err("fsl-hv: cannot register device\n");
833 return ret;
836 INIT_LIST_HEAD(&db_list);
837 INIT_LIST_HEAD(&isr_list);
839 for_each_compatible_node(np, NULL, "epapr,hv-receive-doorbell") {
840 unsigned int irq;
841 const uint32_t *handle;
843 handle = of_get_property(np, "interrupts", NULL);
844 irq = irq_of_parse_and_map(np, 0);
845 if (!handle || (irq == NO_IRQ)) {
846 pr_err("fsl-hv: no 'interrupts' property in %pOF node\n",
847 np);
848 continue;
851 dbisr = kzalloc(sizeof(*dbisr), GFP_KERNEL);
852 if (!dbisr)
853 goto out_of_memory;
855 dbisr->irq = irq;
856 dbisr->doorbell = be32_to_cpup(handle);
858 if (of_device_is_compatible(np, "fsl,hv-shutdown-doorbell")) {
859 /* The shutdown doorbell gets its own ISR */
860 ret = request_irq(irq, fsl_hv_shutdown_isr, 0,
861 np->name, NULL);
862 } else if (of_device_is_compatible(np,
863 "fsl,hv-state-change-doorbell")) {
865 * The state change doorbell triggers a notification if
866 * the state of the managed partition changes to
867 * "stopped". We need a separate interrupt handler for
868 * that, and we also need to know the handle of the
869 * target partition, not just the handle of the
870 * doorbell.
872 dbisr->partition = ret = get_parent_handle(np);
873 if (ret < 0) {
874 pr_err("fsl-hv: node %pOF has missing or "
875 "malformed parent\n", np);
876 kfree(dbisr);
877 continue;
879 ret = request_threaded_irq(irq, fsl_hv_state_change_isr,
880 fsl_hv_state_change_thread,
881 0, np->name, dbisr);
882 } else
883 ret = request_irq(irq, fsl_hv_isr, 0, np->name, dbisr);
885 if (ret < 0) {
886 pr_err("fsl-hv: could not request irq %u for node %pOF\n",
887 irq, np);
888 kfree(dbisr);
889 continue;
892 list_add(&dbisr->list, &isr_list);
894 pr_info("fsl-hv: registered handler for doorbell %u\n",
895 dbisr->doorbell);
898 return 0;
900 out_of_memory:
901 list_for_each_entry_safe(dbisr, n, &isr_list, list) {
902 free_irq(dbisr->irq, dbisr);
903 list_del(&dbisr->list);
904 kfree(dbisr);
907 misc_deregister(&fsl_hv_misc_dev);
909 return -ENOMEM;
913 * Freescale hypervisor management driver termination
915 * This function is called when this driver is unloaded.
917 static void __exit fsl_hypervisor_exit(void)
919 struct doorbell_isr *dbisr, *n;
921 list_for_each_entry_safe(dbisr, n, &isr_list, list) {
922 free_irq(dbisr->irq, dbisr);
923 list_del(&dbisr->list);
924 kfree(dbisr);
927 misc_deregister(&fsl_hv_misc_dev);
930 module_init(fsl_hypervisor_init);
931 module_exit(fsl_hypervisor_exit);
933 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
934 MODULE_DESCRIPTION("Freescale hypervisor management driver");
935 MODULE_LICENSE("GPL v2");