blob: 732e9abdcf96947fa7b1113553119a19827fcf76 [file] [log] [blame]
Timur Tabi6db71992011-06-09 15:52:06 -05001/*
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>
Rob Herring5af50732013-09-17 14:28:33 -050037#include <linux/of_irq.h>
Timur Tabi6db71992011-06-09 15:52:06 -050038#include <linux/reboot.h>
39#include <linux/uaccess.h>
40#include <linux/notifier.h>
Timur Tabif1f4ee02011-07-19 15:45:51 -050041#include <linux/interrupt.h>
Timur Tabi6db71992011-06-09 15:52:06 -050042
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);
Dan Carpenter71e29a12019-05-14 15:47:03 -0700218 if (param.count == 0 ||
219 param.count > U64_MAX - lb_offset - PAGE_SIZE + 1)
220 return -EINVAL;
Timur Tabi6db71992011-06-09 15:52:06 -0500221 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().
228 */
229 pages = kzalloc(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 exit;
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 down_read(&current->mm->mmap_sem);
Dave Hansend4edcf02016-02-12 13:01:56 -0800250 num_pinned = get_user_pages(param.local_vaddr - lb_offset,
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100251 num_pages, (param.source == -1) ? 0 : FOLL_WRITE,
252 pages, NULL);
Timur Tabi6db71992011-06-09 15:52:06 -0500253 up_read(&current->mm->mmap_sem);
254
255 if (num_pinned != num_pages) {
256 /* get_user_pages() failed */
257 pr_debug("fsl-hv: could not lock source buffer\n");
258 ret = (num_pinned < 0) ? num_pinned : -EFAULT;
259 goto exit;
260 }
261
262 /*
263 * Build the fh_sg_list[] array. The first page is special
264 * because it's misaligned.
265 */
266 if (param.source == -1) {
267 sg_list[0].source = page_to_phys(pages[0]) + lb_offset;
268 sg_list[0].target = param.remote_paddr;
269 } else {
270 sg_list[0].source = param.remote_paddr;
271 sg_list[0].target = page_to_phys(pages[0]) + lb_offset;
272 }
273 sg_list[0].size = min_t(uint64_t, param.count, PAGE_SIZE - lb_offset);
274
275 remote_paddr = param.remote_paddr + sg_list[0].size;
276 count = param.count - sg_list[0].size;
277
278 for (i = 1; i < num_pages; i++) {
279 if (param.source == -1) {
280 /* local to remote */
281 sg_list[i].source = page_to_phys(pages[i]);
282 sg_list[i].target = remote_paddr;
283 } else {
284 /* remote to local */
285 sg_list[i].source = remote_paddr;
286 sg_list[i].target = page_to_phys(pages[i]);
287 }
288 sg_list[i].size = min_t(uint64_t, count, PAGE_SIZE);
289
290 remote_paddr += sg_list[i].size;
291 count -= sg_list[i].size;
292 }
293
294 param.ret = fh_partition_memcpy(param.source, param.target,
295 virt_to_phys(sg_list), num_pages);
296
297exit:
298 if (pages) {
299 for (i = 0; i < num_pages; i++)
300 if (pages[i])
301 put_page(pages[i]);
302 }
303
304 kfree(sg_list_unaligned);
305 kfree(pages);
306
307 if (!ret)
308 if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
309 return -EFAULT;
310
311 return ret;
312}
313
314/*
315 * Ioctl interface for FSL_HV_IOCTL_DOORBELL
316 *
317 * Ring a doorbell
318 */
319static long ioctl_doorbell(struct fsl_hv_ioctl_doorbell __user *p)
320{
321 struct fsl_hv_ioctl_doorbell param;
322
323 /* Get the parameters from the user. */
324 if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_doorbell)))
325 return -EFAULT;
326
327 param.ret = ev_doorbell_send(param.doorbell);
328
329 if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
330 return -EFAULT;
331
332 return 0;
333}
334
335static long ioctl_dtprop(struct fsl_hv_ioctl_prop __user *p, int set)
336{
337 struct fsl_hv_ioctl_prop param;
338 char __user *upath, *upropname;
339 void __user *upropval;
Dan Carpenterb51cbe62019-05-14 15:47:00 -0700340 char *path, *propname;
341 void *propval;
Timur Tabi6db71992011-06-09 15:52:06 -0500342 int ret = 0;
343
344 /* Get the parameters from the user. */
345 if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_prop)))
346 return -EFAULT;
347
348 upath = (char __user *)(uintptr_t)param.path;
349 upropname = (char __user *)(uintptr_t)param.propname;
350 upropval = (void __user *)(uintptr_t)param.propval;
351
352 path = strndup_user(upath, FH_DTPROP_MAX_PATHLEN);
Dan Carpenterb51cbe62019-05-14 15:47:00 -0700353 if (IS_ERR(path))
354 return PTR_ERR(path);
Timur Tabi6db71992011-06-09 15:52:06 -0500355
356 propname = strndup_user(upropname, FH_DTPROP_MAX_PATHLEN);
357 if (IS_ERR(propname)) {
358 ret = PTR_ERR(propname);
Dan Carpenterb51cbe62019-05-14 15:47:00 -0700359 goto err_free_path;
Timur Tabi6db71992011-06-09 15:52:06 -0500360 }
361
362 if (param.proplen > FH_DTPROP_MAX_PROPLEN) {
363 ret = -EINVAL;
Dan Carpenterb51cbe62019-05-14 15:47:00 -0700364 goto err_free_propname;
Timur Tabi6db71992011-06-09 15:52:06 -0500365 }
366
367 propval = kmalloc(param.proplen, GFP_KERNEL);
368 if (!propval) {
369 ret = -ENOMEM;
Dan Carpenterb51cbe62019-05-14 15:47:00 -0700370 goto err_free_propname;
Timur Tabi6db71992011-06-09 15:52:06 -0500371 }
372
373 if (set) {
374 if (copy_from_user(propval, upropval, param.proplen)) {
375 ret = -EFAULT;
Dan Carpenterb51cbe62019-05-14 15:47:00 -0700376 goto err_free_propval;
Timur Tabi6db71992011-06-09 15:52:06 -0500377 }
378
379 param.ret = fh_partition_set_dtprop(param.handle,
380 virt_to_phys(path),
381 virt_to_phys(propname),
382 virt_to_phys(propval),
383 param.proplen);
384 } else {
385 param.ret = fh_partition_get_dtprop(param.handle,
386 virt_to_phys(path),
387 virt_to_phys(propname),
388 virt_to_phys(propval),
389 &param.proplen);
390
391 if (param.ret == 0) {
392 if (copy_to_user(upropval, propval, param.proplen) ||
393 put_user(param.proplen, &p->proplen)) {
394 ret = -EFAULT;
Dan Carpenterb51cbe62019-05-14 15:47:00 -0700395 goto err_free_propval;
Timur Tabi6db71992011-06-09 15:52:06 -0500396 }
397 }
398 }
399
400 if (put_user(param.ret, &p->ret))
401 ret = -EFAULT;
402
Dan Carpenterb51cbe62019-05-14 15:47:00 -0700403err_free_propval:
Timur Tabi6db71992011-06-09 15:52:06 -0500404 kfree(propval);
Dan Carpenterb51cbe62019-05-14 15:47:00 -0700405err_free_propname:
Timur Tabi6db71992011-06-09 15:52:06 -0500406 kfree(propname);
Dan Carpenterb51cbe62019-05-14 15:47:00 -0700407err_free_path:
408 kfree(path);
Timur Tabi6db71992011-06-09 15:52:06 -0500409
410 return ret;
411}
412
413/*
414 * Ioctl main entry point
415 */
416static long fsl_hv_ioctl(struct file *file, unsigned int cmd,
417 unsigned long argaddr)
418{
419 void __user *arg = (void __user *)argaddr;
420 long ret;
421
422 switch (cmd) {
423 case FSL_HV_IOCTL_PARTITION_RESTART:
424 ret = ioctl_restart(arg);
425 break;
426 case FSL_HV_IOCTL_PARTITION_GET_STATUS:
427 ret = ioctl_status(arg);
428 break;
429 case FSL_HV_IOCTL_PARTITION_START:
430 ret = ioctl_start(arg);
431 break;
432 case FSL_HV_IOCTL_PARTITION_STOP:
433 ret = ioctl_stop(arg);
434 break;
435 case FSL_HV_IOCTL_MEMCPY:
436 ret = ioctl_memcpy(arg);
437 break;
438 case FSL_HV_IOCTL_DOORBELL:
439 ret = ioctl_doorbell(arg);
440 break;
441 case FSL_HV_IOCTL_GETPROP:
442 ret = ioctl_dtprop(arg, 0);
443 break;
444 case FSL_HV_IOCTL_SETPROP:
445 ret = ioctl_dtprop(arg, 1);
446 break;
447 default:
448 pr_debug("fsl-hv: bad ioctl dir=%u type=%u cmd=%u size=%u\n",
449 _IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd),
450 _IOC_SIZE(cmd));
451 return -ENOTTY;
452 }
453
454 return ret;
455}
456
457/* Linked list of processes that have us open */
458static struct list_head db_list;
459
460/* spinlock for db_list */
461static DEFINE_SPINLOCK(db_list_lock);
462
463/* The size of the doorbell event queue. This must be a power of two. */
464#define QSIZE 16
465
466/* Returns the next head/tail pointer, wrapping around the queue if necessary */
467#define nextp(x) (((x) + 1) & (QSIZE - 1))
468
469/* Per-open data structure */
470struct doorbell_queue {
471 struct list_head list;
472 spinlock_t lock;
473 wait_queue_head_t wait;
474 unsigned int head;
475 unsigned int tail;
476 uint32_t q[QSIZE];
477};
478
479/* Linked list of ISRs that we registered */
480struct list_head isr_list;
481
482/* Per-ISR data structure */
483struct doorbell_isr {
484 struct list_head list;
485 unsigned int irq;
486 uint32_t doorbell; /* The doorbell handle */
487 uint32_t partition; /* The partition handle, if used */
488};
489
490/*
491 * Add a doorbell to all of the doorbell queues
492 */
493static void fsl_hv_queue_doorbell(uint32_t doorbell)
494{
495 struct doorbell_queue *dbq;
496 unsigned long flags;
497
498 /* Prevent another core from modifying db_list */
499 spin_lock_irqsave(&db_list_lock, flags);
500
501 list_for_each_entry(dbq, &db_list, list) {
502 if (dbq->head != nextp(dbq->tail)) {
503 dbq->q[dbq->tail] = doorbell;
504 /*
505 * This memory barrier eliminates the need to grab
506 * the spinlock for dbq.
507 */
508 smp_wmb();
509 dbq->tail = nextp(dbq->tail);
510 wake_up_interruptible(&dbq->wait);
511 }
512 }
513
514 spin_unlock_irqrestore(&db_list_lock, flags);
515}
516
517/*
518 * Interrupt handler for all doorbells
519 *
520 * We use the same interrupt handler for all doorbells. Whenever a doorbell
521 * is rung, and we receive an interrupt, we just put the handle for that
522 * doorbell (passed to us as *data) into all of the queues.
523 */
524static irqreturn_t fsl_hv_isr(int irq, void *data)
525{
526 fsl_hv_queue_doorbell((uintptr_t) data);
527
528 return IRQ_HANDLED;
529}
530
531/*
532 * State change thread function
533 *
534 * The state change notification arrives in an interrupt, but we can't call
535 * blocking_notifier_call_chain() in an interrupt handler. We could call
536 * atomic_notifier_call_chain(), but that would require the clients' call-back
537 * function to run in interrupt context. Since we don't want to impose that
538 * restriction on the clients, we use a threaded IRQ to process the
539 * notification in kernel context.
540 */
541static irqreturn_t fsl_hv_state_change_thread(int irq, void *data)
542{
543 struct doorbell_isr *dbisr = data;
544
545 blocking_notifier_call_chain(&failover_subscribers, dbisr->partition,
546 NULL);
547
548 return IRQ_HANDLED;
549}
550
551/*
552 * Interrupt handler for state-change doorbells
553 */
554static irqreturn_t fsl_hv_state_change_isr(int irq, void *data)
555{
556 unsigned int status;
557 struct doorbell_isr *dbisr = data;
558 int ret;
559
560 /* It's still a doorbell, so add it to all the queues. */
561 fsl_hv_queue_doorbell(dbisr->doorbell);
562
563 /* Determine the new state, and if it's stopped, notify the clients. */
564 ret = fh_partition_get_status(dbisr->partition, &status);
565 if (!ret && (status == FH_PARTITION_STOPPED))
566 return IRQ_WAKE_THREAD;
567
568 return IRQ_HANDLED;
569}
570
571/*
572 * Returns a bitmask indicating whether a read will block
573 */
574static unsigned int fsl_hv_poll(struct file *filp, struct poll_table_struct *p)
575{
576 struct doorbell_queue *dbq = filp->private_data;
577 unsigned long flags;
578 unsigned int mask;
579
580 spin_lock_irqsave(&dbq->lock, flags);
581
582 poll_wait(filp, &dbq->wait, p);
583 mask = (dbq->head == dbq->tail) ? 0 : (POLLIN | POLLRDNORM);
584
585 spin_unlock_irqrestore(&dbq->lock, flags);
586
587 return mask;
588}
589
590/*
591 * Return the handles for any incoming doorbells
592 *
593 * If there are doorbell handles in the queue for this open instance, then
594 * return them to the caller as an array of 32-bit integers. Otherwise,
595 * block until there is at least one handle to return.
596 */
597static ssize_t fsl_hv_read(struct file *filp, char __user *buf, size_t len,
598 loff_t *off)
599{
600 struct doorbell_queue *dbq = filp->private_data;
601 uint32_t __user *p = (uint32_t __user *) buf; /* for put_user() */
602 unsigned long flags;
603 ssize_t count = 0;
604
605 /* Make sure we stop when the user buffer is full. */
606 while (len >= sizeof(uint32_t)) {
607 uint32_t dbell; /* Local copy of doorbell queue data */
608
609 spin_lock_irqsave(&dbq->lock, flags);
610
611 /*
612 * If the queue is empty, then either we're done or we need
613 * to block. If the application specified O_NONBLOCK, then
614 * we return the appropriate error code.
615 */
616 if (dbq->head == dbq->tail) {
617 spin_unlock_irqrestore(&dbq->lock, flags);
618 if (count)
619 break;
620 if (filp->f_flags & O_NONBLOCK)
621 return -EAGAIN;
622 if (wait_event_interruptible(dbq->wait,
623 dbq->head != dbq->tail))
624 return -ERESTARTSYS;
625 continue;
626 }
627
628 /*
629 * Even though we have an smp_wmb() in the ISR, the core
630 * might speculatively execute the "dbell = ..." below while
631 * it's evaluating the if-statement above. In that case, the
632 * value put into dbell could be stale if the core accepts the
633 * speculation. To prevent that, we need a read memory barrier
634 * here as well.
635 */
636 smp_rmb();
637
638 /* Copy the data to a temporary local buffer, because
639 * we can't call copy_to_user() from inside a spinlock
640 */
641 dbell = dbq->q[dbq->head];
642 dbq->head = nextp(dbq->head);
643
644 spin_unlock_irqrestore(&dbq->lock, flags);
645
646 if (put_user(dbell, p))
647 return -EFAULT;
648 p++;
649 count += sizeof(uint32_t);
650 len -= sizeof(uint32_t);
651 }
652
653 return count;
654}
655
656/*
657 * Open the driver and prepare for reading doorbells.
658 *
659 * Every time an application opens the driver, we create a doorbell queue
660 * for that file handle. This queue is used for any incoming doorbells.
661 */
662static int fsl_hv_open(struct inode *inode, struct file *filp)
663{
664 struct doorbell_queue *dbq;
665 unsigned long flags;
666 int ret = 0;
667
668 dbq = kzalloc(sizeof(struct doorbell_queue), GFP_KERNEL);
669 if (!dbq) {
670 pr_err("fsl-hv: out of memory\n");
671 return -ENOMEM;
672 }
673
674 spin_lock_init(&dbq->lock);
675 init_waitqueue_head(&dbq->wait);
676
677 spin_lock_irqsave(&db_list_lock, flags);
678 list_add(&dbq->list, &db_list);
679 spin_unlock_irqrestore(&db_list_lock, flags);
680
681 filp->private_data = dbq;
682
683 return ret;
684}
685
686/*
687 * Close the driver
688 */
689static int fsl_hv_close(struct inode *inode, struct file *filp)
690{
691 struct doorbell_queue *dbq = filp->private_data;
692 unsigned long flags;
693
694 int ret = 0;
695
696 spin_lock_irqsave(&db_list_lock, flags);
697 list_del(&dbq->list);
698 spin_unlock_irqrestore(&db_list_lock, flags);
699
700 kfree(dbq);
701
702 return ret;
703}
704
705static const struct file_operations fsl_hv_fops = {
706 .owner = THIS_MODULE,
707 .open = fsl_hv_open,
708 .release = fsl_hv_close,
709 .poll = fsl_hv_poll,
710 .read = fsl_hv_read,
711 .unlocked_ioctl = fsl_hv_ioctl,
Mihai Caramanc031ab12011-10-13 18:05:21 +0300712 .compat_ioctl = fsl_hv_ioctl,
Timur Tabi6db71992011-06-09 15:52:06 -0500713};
714
715static struct miscdevice fsl_hv_misc_dev = {
716 MISC_DYNAMIC_MINOR,
717 "fsl-hv",
718 &fsl_hv_fops
719};
720
721static irqreturn_t fsl_hv_shutdown_isr(int irq, void *data)
722{
723 orderly_poweroff(false);
724
725 return IRQ_HANDLED;
726}
727
728/*
729 * Returns the handle of the parent of the given node
730 *
731 * The handle is the value of the 'hv-handle' property
732 */
733static int get_parent_handle(struct device_node *np)
734{
735 struct device_node *parent;
736 const uint32_t *prop;
737 uint32_t handle;
738 int len;
739
740 parent = of_get_parent(np);
741 if (!parent)
742 /* It's not really possible for this to fail */
743 return -ENODEV;
744
745 /*
746 * The proper name for the handle property is "hv-handle", but some
747 * older versions of the hypervisor used "reg".
748 */
749 prop = of_get_property(parent, "hv-handle", &len);
750 if (!prop)
751 prop = of_get_property(parent, "reg", &len);
752
753 if (!prop || (len != sizeof(uint32_t))) {
754 /* This can happen only if the node is malformed */
755 of_node_put(parent);
756 return -ENODEV;
757 }
758
759 handle = be32_to_cpup(prop);
760 of_node_put(parent);
761
762 return handle;
763}
764
765/*
766 * Register a callback for failover events
767 *
768 * This function is called by device drivers to register their callback
769 * functions for fail-over events.
770 */
771int fsl_hv_failover_register(struct notifier_block *nb)
772{
773 return blocking_notifier_chain_register(&failover_subscribers, nb);
774}
775EXPORT_SYMBOL(fsl_hv_failover_register);
776
777/*
778 * Unregister a callback for failover events
779 */
780int fsl_hv_failover_unregister(struct notifier_block *nb)
781{
782 return blocking_notifier_chain_unregister(&failover_subscribers, nb);
783}
784EXPORT_SYMBOL(fsl_hv_failover_unregister);
785
786/*
787 * Return TRUE if we're running under FSL hypervisor
788 *
789 * This function checks to see if we're running under the Freescale
790 * hypervisor, and returns zero if we're not, or non-zero if we are.
791 *
792 * First, it checks if MSR[GS]==1, which means we're running under some
793 * hypervisor. Then it checks if there is a hypervisor node in the device
794 * tree. Currently, that means there needs to be a node in the root called
795 * "hypervisor" and which has a property named "fsl,hv-version".
796 */
797static int has_fsl_hypervisor(void)
798{
799 struct device_node *node;
800 int ret;
801
Timur Tabi6db71992011-06-09 15:52:06 -0500802 node = of_find_node_by_path("/hypervisor");
803 if (!node)
804 return 0;
805
806 ret = of_find_property(node, "fsl,hv-version", NULL) != NULL;
807
808 of_node_put(node);
809
810 return ret;
811}
812
813/*
814 * Freescale hypervisor management driver init
815 *
816 * This function is called when this module is loaded.
817 *
818 * Register ourselves as a miscellaneous driver. This will register the
819 * fops structure and create the right sysfs entries for udev.
820 */
821static int __init fsl_hypervisor_init(void)
822{
823 struct device_node *np;
824 struct doorbell_isr *dbisr, *n;
825 int ret;
826
827 pr_info("Freescale hypervisor management driver\n");
828
829 if (!has_fsl_hypervisor()) {
830 pr_info("fsl-hv: no hypervisor found\n");
831 return -ENODEV;
832 }
833
834 ret = misc_register(&fsl_hv_misc_dev);
835 if (ret) {
836 pr_err("fsl-hv: cannot register device\n");
837 return ret;
838 }
839
840 INIT_LIST_HEAD(&db_list);
841 INIT_LIST_HEAD(&isr_list);
842
843 for_each_compatible_node(np, NULL, "epapr,hv-receive-doorbell") {
844 unsigned int irq;
845 const uint32_t *handle;
846
847 handle = of_get_property(np, "interrupts", NULL);
848 irq = irq_of_parse_and_map(np, 0);
849 if (!handle || (irq == NO_IRQ)) {
850 pr_err("fsl-hv: no 'interrupts' property in %s node\n",
851 np->full_name);
852 continue;
853 }
854
855 dbisr = kzalloc(sizeof(*dbisr), GFP_KERNEL);
856 if (!dbisr)
857 goto out_of_memory;
858
859 dbisr->irq = irq;
860 dbisr->doorbell = be32_to_cpup(handle);
861
862 if (of_device_is_compatible(np, "fsl,hv-shutdown-doorbell")) {
863 /* The shutdown doorbell gets its own ISR */
864 ret = request_irq(irq, fsl_hv_shutdown_isr, 0,
865 np->name, NULL);
866 } else if (of_device_is_compatible(np,
867 "fsl,hv-state-change-doorbell")) {
868 /*
869 * The state change doorbell triggers a notification if
870 * the state of the managed partition changes to
871 * "stopped". We need a separate interrupt handler for
872 * that, and we also need to know the handle of the
873 * target partition, not just the handle of the
874 * doorbell.
875 */
876 dbisr->partition = ret = get_parent_handle(np);
877 if (ret < 0) {
878 pr_err("fsl-hv: node %s has missing or "
879 "malformed parent\n", np->full_name);
880 kfree(dbisr);
881 continue;
882 }
883 ret = request_threaded_irq(irq, fsl_hv_state_change_isr,
884 fsl_hv_state_change_thread,
885 0, np->name, dbisr);
886 } else
887 ret = request_irq(irq, fsl_hv_isr, 0, np->name, dbisr);
888
889 if (ret < 0) {
890 pr_err("fsl-hv: could not request irq %u for node %s\n",
891 irq, np->full_name);
892 kfree(dbisr);
893 continue;
894 }
895
896 list_add(&dbisr->list, &isr_list);
897
898 pr_info("fsl-hv: registered handler for doorbell %u\n",
899 dbisr->doorbell);
900 }
901
902 return 0;
903
904out_of_memory:
905 list_for_each_entry_safe(dbisr, n, &isr_list, list) {
906 free_irq(dbisr->irq, dbisr);
907 list_del(&dbisr->list);
908 kfree(dbisr);
909 }
910
911 misc_deregister(&fsl_hv_misc_dev);
912
913 return -ENOMEM;
914}
915
916/*
917 * Freescale hypervisor management driver termination
918 *
919 * This function is called when this driver is unloaded.
920 */
921static void __exit fsl_hypervisor_exit(void)
922{
923 struct doorbell_isr *dbisr, *n;
924
925 list_for_each_entry_safe(dbisr, n, &isr_list, list) {
926 free_irq(dbisr->irq, dbisr);
927 list_del(&dbisr->list);
928 kfree(dbisr);
929 }
930
931 misc_deregister(&fsl_hv_misc_dev);
932}
933
934module_init(fsl_hypervisor_init);
935module_exit(fsl_hypervisor_exit);
936
937MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
938MODULE_DESCRIPTION("Freescale hypervisor management driver");
939MODULE_LICENSE("GPL v2");