Merge branch 'for-2.6.40/splice' of git://git.kernel.dk/linux-2.6-block
[firefly-linux-kernel-4.4.55.git] / drivers / staging / hv / hv_timesource.c
1 /*
2  * A clocksource for Linux running on HyperV.
3  *
4  *
5  * Copyright (C) 2010, Novell, Inc.
6  * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15  * NON INFRINGEMENT.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  */
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/version.h>
26 #include <linux/clocksource.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/dmi.h>
31 #include <asm/hyperv.h>
32 #include <asm/mshyperv.h>
33 #include <asm/hypervisor.h>
34
35 #define HV_CLOCK_SHIFT  22
36
37 static cycle_t read_hv_clock(struct clocksource *arg)
38 {
39         cycle_t current_tick;
40         /*
41          * Read the partition counter to get the current tick count. This count
42          * is set to 0 when the partition is created and is incremented in
43          * 100 nanosecond units.
44          */
45         rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
46         return current_tick;
47 }
48
49 static struct clocksource hyperv_cs = {
50         .name           = "hyperv_clocksource",
51         .rating         = 400, /* use this when running on Hyperv*/
52         .read           = read_hv_clock,
53         .mask           = CLOCKSOURCE_MASK(64),
54         /*
55          * The time ref counter in HyperV is in 100ns units.
56          * The definition of mult is:
57          * mult/2^shift = ns/cyc = 100
58          * mult = (100 << shift)
59          */
60         .mult           = (100 << HV_CLOCK_SHIFT),
61         .shift          = HV_CLOCK_SHIFT,
62 };
63
64 static const struct dmi_system_id __initconst
65 hv_timesource_dmi_table[] __maybe_unused  = {
66         {
67                 .ident = "Hyper-V",
68                 .matches = {
69                         DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
70                         DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
71                         DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
72                 },
73         },
74         { },
75 };
76 MODULE_DEVICE_TABLE(dmi, hv_timesource_dmi_table);
77
78 static const struct pci_device_id __initconst
79 hv_timesource_pci_table[] __maybe_unused = {
80         { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
81         { 0 }
82 };
83 MODULE_DEVICE_TABLE(pci, hv_timesource_pci_table);
84
85
86 static int __init init_hv_clocksource(void)
87 {
88         if ((x86_hyper != &x86_hyper_ms_hyperv) ||
89                 !(ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE))
90                 return -ENODEV;
91
92         if (!dmi_check_system(hv_timesource_dmi_table))
93                 return -ENODEV;
94
95         pr_info("Registering HyperV clock source\n");
96         return clocksource_register(&hyperv_cs);
97 }
98
99 module_init(init_hv_clocksource);
100 MODULE_DESCRIPTION("HyperV based clocksource");
101 MODULE_AUTHOR("K. Y. Srinivasan <ksrinivasan@novell.com>");
102 MODULE_LICENSE("GPL");