Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core-2.6
[linux-2.6] / arch / sparc64 / kernel / hvapi.c
1 /* hvapi.c: Hypervisor API management.
2  *
3  * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
4  */
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9
10 #include <asm/hypervisor.h>
11 #include <asm/oplib.h>
12 #include <asm/sstate.h>
13
14 /* If the hypervisor indicates that the API setting
15  * calls are unsupported, by returning HV_EBADTRAP or
16  * HV_ENOTSUPPORTED, we assume that API groups with the
17  * PRE_API flag set are major 1 minor 0.
18  */
19 struct api_info {
20         unsigned long group;
21         unsigned long major;
22         unsigned long minor;
23         unsigned int refcnt;
24         unsigned int flags;
25 #define FLAG_PRE_API            0x00000001
26 };
27
28 static struct api_info api_table[] = {
29         { .group = HV_GRP_SUN4V,        .flags = FLAG_PRE_API   },
30         { .group = HV_GRP_CORE,         .flags = FLAG_PRE_API   },
31         { .group = HV_GRP_INTR,                                 },
32         { .group = HV_GRP_SOFT_STATE,                           },
33         { .group = HV_GRP_PCI,          .flags = FLAG_PRE_API   },
34         { .group = HV_GRP_LDOM,                                 },
35         { .group = HV_GRP_SVC_CHAN,     .flags = FLAG_PRE_API   },
36         { .group = HV_GRP_NCS,          .flags = FLAG_PRE_API   },
37         { .group = HV_GRP_RNG,                                  },
38         { .group = HV_GRP_NIAG_PERF,    .flags = FLAG_PRE_API   },
39         { .group = HV_GRP_FIRE_PERF,                            },
40         { .group = HV_GRP_N2_CPU,                               },
41         { .group = HV_GRP_NIU,                                  },
42         { .group = HV_GRP_VF_CPU,                               },
43         { .group = HV_GRP_DIAG,         .flags = FLAG_PRE_API   },
44 };
45
46 static DEFINE_SPINLOCK(hvapi_lock);
47
48 static struct api_info *__get_info(unsigned long group)
49 {
50         int i;
51
52         for (i = 0; i < ARRAY_SIZE(api_table); i++) {
53                 if (api_table[i].group == group)
54                         return &api_table[i];
55         }
56         return NULL;
57 }
58
59 static void __get_ref(struct api_info *p)
60 {
61         p->refcnt++;
62 }
63
64 static void __put_ref(struct api_info *p)
65 {
66         if (--p->refcnt == 0) {
67                 unsigned long ignore;
68
69                 sun4v_set_version(p->group, 0, 0, &ignore);
70                 p->major = p->minor = 0;
71         }
72 }
73
74 /* Register a hypervisor API specification.  It indicates the
75  * API group and desired major+minor.
76  *
77  * If an existing API registration exists '0' (success) will
78  * be returned if it is compatible with the one being registered.
79  * Otherwise a negative error code will be returned.
80  *
81  * Otherwise an attempt will be made to negotiate the requested
82  * API group/major/minor with the hypervisor, and errors returned
83  * if that does not succeed.
84  */
85 int sun4v_hvapi_register(unsigned long group, unsigned long major,
86                          unsigned long *minor)
87 {
88         struct api_info *p;
89         unsigned long flags;
90         int ret;
91
92         spin_lock_irqsave(&hvapi_lock, flags);
93         p = __get_info(group);
94         ret = -EINVAL;
95         if (p) {
96                 if (p->refcnt) {
97                         ret = -EINVAL;
98                         if (p->major == major) {
99                                 *minor = p->minor;
100                                 ret = 0;
101                         }
102                 } else {
103                         unsigned long actual_minor;
104                         unsigned long hv_ret;
105
106                         hv_ret = sun4v_set_version(group, major, *minor,
107                                                    &actual_minor);
108                         ret = -EINVAL;
109                         if (hv_ret == HV_EOK) {
110                                 *minor = actual_minor;
111                                 p->major = major;
112                                 p->minor = actual_minor;
113                                 ret = 0;
114                         } else if (hv_ret == HV_EBADTRAP ||
115                                    hv_ret == HV_ENOTSUPPORTED) {
116                                 if (p->flags & FLAG_PRE_API) {
117                                         if (major == 1) {
118                                                 p->major = 1;
119                                                 p->minor = 0;
120                                                 *minor = 0;
121                                                 ret = 0;
122                                         }
123                                 }
124                         }
125                 }
126
127                 if (ret == 0)
128                         __get_ref(p);
129         }
130         spin_unlock_irqrestore(&hvapi_lock, flags);
131
132         return ret;
133 }
134 EXPORT_SYMBOL(sun4v_hvapi_register);
135
136 void sun4v_hvapi_unregister(unsigned long group)
137 {
138         struct api_info *p;
139         unsigned long flags;
140
141         spin_lock_irqsave(&hvapi_lock, flags);
142         p = __get_info(group);
143         if (p)
144                 __put_ref(p);
145         spin_unlock_irqrestore(&hvapi_lock, flags);
146 }
147 EXPORT_SYMBOL(sun4v_hvapi_unregister);
148
149 int sun4v_hvapi_get(unsigned long group,
150                     unsigned long *major,
151                     unsigned long *minor)
152 {
153         struct api_info *p;
154         unsigned long flags;
155         int ret;
156
157         spin_lock_irqsave(&hvapi_lock, flags);
158         ret = -EINVAL;
159         p = __get_info(group);
160         if (p && p->refcnt) {
161                 *major = p->major;
162                 *minor = p->minor;
163                 ret = 0;
164         }
165         spin_unlock_irqrestore(&hvapi_lock, flags);
166
167         return ret;
168 }
169 EXPORT_SYMBOL(sun4v_hvapi_get);
170
171 void __init sun4v_hvapi_init(void)
172 {
173         unsigned long group, major, minor;
174
175         group = HV_GRP_SUN4V;
176         major = 1;
177         minor = 0;
178         if (sun4v_hvapi_register(group, major, &minor))
179                 goto bad;
180
181         group = HV_GRP_CORE;
182         major = 1;
183         minor = 1;
184         if (sun4v_hvapi_register(group, major, &minor))
185                 goto bad;
186
187         sun4v_sstate_init();
188
189         return;
190
191 bad:
192         prom_printf("HVAPI: Cannot register API group "
193                     "%lx with major(%u) minor(%u)\n",
194                     group, major, minor);
195         prom_halt();
196 }