1 /* hvapi.c: Hypervisor API management.
3 * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
10 #include <asm/hypervisor.h>
11 #include <asm/oplib.h>
13 /* If the hypervisor indicates that the API setting
14 * calls are unsupported, by returning HV_EBADTRAP or
15 * HV_ENOTSUPPORTED, we assume that API groups with the
16 * PRE_API flag set are major 1 minor 0.
24 #define FLAG_PRE_API 0x00000001
27 static struct api_info api_table
[] = {
28 { .group
= HV_GRP_SUN4V
, .flags
= FLAG_PRE_API
},
29 { .group
= HV_GRP_CORE
, .flags
= FLAG_PRE_API
},
30 { .group
= HV_GRP_INTR
, },
31 { .group
= HV_GRP_SOFT_STATE
, },
32 { .group
= HV_GRP_PCI
, .flags
= FLAG_PRE_API
},
33 { .group
= HV_GRP_LDOM
, },
34 { .group
= HV_GRP_SVC_CHAN
, .flags
= FLAG_PRE_API
},
35 { .group
= HV_GRP_NCS
, .flags
= FLAG_PRE_API
},
36 { .group
= HV_GRP_RNG
, },
37 { .group
= HV_GRP_NIAG_PERF
, .flags
= FLAG_PRE_API
},
38 { .group
= HV_GRP_FIRE_PERF
, },
39 { .group
= HV_GRP_N2_CPU
, },
40 { .group
= HV_GRP_NIU
, },
41 { .group
= HV_GRP_VF_CPU
, },
42 { .group
= HV_GRP_DIAG
, .flags
= FLAG_PRE_API
},
45 static DEFINE_SPINLOCK(hvapi_lock
);
47 static struct api_info
*__get_info(unsigned long group
)
51 for (i
= 0; i
< ARRAY_SIZE(api_table
); i
++) {
52 if (api_table
[i
].group
== group
)
58 static void __get_ref(struct api_info
*p
)
63 static void __put_ref(struct api_info
*p
)
65 if (--p
->refcnt
== 0) {
68 sun4v_set_version(p
->group
, 0, 0, &ignore
);
69 p
->major
= p
->minor
= 0;
73 /* Register a hypervisor API specification. It indicates the
74 * API group and desired major+minor.
76 * If an existing API registration exists '0' (success) will
77 * be returned if it is compatible with the one being registered.
78 * Otherwise a negative error code will be returned.
80 * Otherwise an attempt will be made to negotiate the requested
81 * API group/major/minor with the hypervisor, and errors returned
82 * if that does not succeed.
84 int sun4v_hvapi_register(unsigned long group
, unsigned long major
,
91 spin_lock_irqsave(&hvapi_lock
, flags
);
92 p
= __get_info(group
);
97 if (p
->major
== major
) {
102 unsigned long actual_minor
;
103 unsigned long hv_ret
;
105 hv_ret
= sun4v_set_version(group
, major
, *minor
,
108 if (hv_ret
== HV_EOK
) {
109 *minor
= actual_minor
;
111 p
->minor
= actual_minor
;
113 } else if (hv_ret
== HV_EBADTRAP
||
114 hv_ret
== HV_ENOTSUPPORTED
) {
115 if (p
->flags
& FLAG_PRE_API
) {
129 spin_unlock_irqrestore(&hvapi_lock
, flags
);
133 EXPORT_SYMBOL(sun4v_hvapi_register
);
135 void sun4v_hvapi_unregister(unsigned long group
)
140 spin_lock_irqsave(&hvapi_lock
, flags
);
141 p
= __get_info(group
);
144 spin_unlock_irqrestore(&hvapi_lock
, flags
);
146 EXPORT_SYMBOL(sun4v_hvapi_unregister
);
148 int sun4v_hvapi_get(unsigned long group
,
149 unsigned long *major
,
150 unsigned long *minor
)
156 spin_lock_irqsave(&hvapi_lock
, flags
);
158 p
= __get_info(group
);
159 if (p
&& p
->refcnt
) {
164 spin_unlock_irqrestore(&hvapi_lock
, flags
);
168 EXPORT_SYMBOL(sun4v_hvapi_get
);
170 void __init
sun4v_hvapi_init(void)
172 unsigned long group
, major
, minor
;
174 group
= HV_GRP_SUN4V
;
177 if (sun4v_hvapi_register(group
, major
, &minor
))
183 if (sun4v_hvapi_register(group
, major
, &minor
))
189 prom_printf("HVAPI: Cannot register API group "
190 "%lx with major(%u) minor(%u)\n",
191 group
, major
, minor
);