1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/dditypes.h>
#include <sys/machsystm.h>
#include <sys/archsystm.h>
#include <sys/prom_plat.h>
#include <sys/promif.h>
#include <sys/kmem.h>
#include <sys/hypervisor_api.h>
#include <sys/hsvc.h>
#ifdef DEBUG
int hsvc_debug = 0; /* HSVC debug flags */
/*
* Flags to control HSVC debugging
*/
#define DBG_HSVC_REGISTER 0x0001
#define DBG_HSVC_UNREGISTER 0x0002
#define DBG_HSVC_OBP_CIF 0x0004
#define DBG_HSVC_ALLOC 0x0008
#define DBG_HSVC_VERSION 0x0010
#define DBG_HSVC_REFCNT 0x0020
#define DBG_HSVC_SETUP 0x0040
#define HSVC_CHK_REFCNT(hsvcp) \
if (hsvc_debug & DBG_HSVC_REFCNT) hsvc_chk_refcnt(hsvcp)
#define HSVC_DEBUG(flag, ARGS) \
if (hsvc_debug & flag) prom_printf ARGS
#define HSVC_DUMP() \
if (hsvc_debug & DBG_HSVC_SETUP) hsvc_dump()
#else /* DEBUG */
#define HSVC_CHK_REFCNT(hsvcp)
#define HSVC_DEBUG(flag, args)
#define HSVC_DUMP()
#endif /* DEBUG */
/*
* Each hypervisor API group negotiation is tracked via a
* hsvc structure. This structure contains the API group,
* currently negotiated major/minor number, a singly linked
* list of clients currently registered and a reference count.
*
* Since the number of API groups is fairly small, negotiated
* API groups are maintained via a singly linked list. Also,
* sufficient free space is reserved to allow for API group
* registration before kmem_xxx interface can be used to
* allocate memory dynamically.
*
* Note that all access to the API group lookup and negotiation
* is serialized to support strict HV API interface.
*/
typedef struct hsvc {
struct hsvc *next; /* next group/free entry */
uint64_t group; /* hypervisor service group */
uint64_t major; /* major number */
uint64_t minor; /* minor number */
uint64_t refcnt; /* reference count */
hsvc_info_t *clients; /* linked list of clients */
} hsvc_t;
/*
* Global variables
*/
hsvc_t *hsvc_groups; /* linked list of API groups in use */
hsvc_t *hsvc_avail; /* free reserved buffers */
kmutex_t hsvc_lock; /* protects linked list and globals */
/*
* Preallocate some space for boot requirements (before kmem_xxx can be
* used)
*/
#define HSVC_RESV_BUFS_MAX 16
hsvc_t hsvc_resv_bufs[HSVC_RESV_BUFS_MAX];
/*
* Pre-versioning groups (not negotiated by Ontario/Erie FCS release)
*/
static uint64_t hsvc_pre_versioning_groups[] = {
HSVC_GROUP_SUN4V,
HSVC_GROUP_CORE,
HSVC_GROUP_VPCI,
HSVC_GROUP_VSC,
HSVC_GROUP_NIAGARA_CPU,
HSVC_GROUP_NCS,
HSVC_GROUP_DIAG
};
#define HSVC_PRE_VERSIONING_GROUP_CNT \
(sizeof (hsvc_pre_versioning_groups) / sizeof (uint64_t))
static boolean_t
pre_versioning_group(uint64_t api_group)
{
int i;
for (i = 0; i < HSVC_PRE_VERSIONING_GROUP_CNT; i++)
if (hsvc_pre_versioning_groups[i] == api_group)
return (B_TRUE);
return (B_FALSE);
}
static hsvc_t *
hsvc_lookup(hsvc_info_t *hsvcinfop)
{
hsvc_t *hsvcp;
hsvc_info_t *p;
for (hsvcp = hsvc_groups; hsvcp != NULL; hsvcp = hsvcp->next) {
for (p = hsvcp->clients; p != NULL;
p = (hsvc_info_t *)p->hsvc_private)
if (p == hsvcinfop)
break;
if (p)
break;
}
return (hsvcp);
}
#ifdef DEBUG
/*
* Check client reference count
*/
static void
hsvc_chk_refcnt(hsvc_t *hsvcp)
{
int refcnt;
hsvc_info_t *p;
for (refcnt = 0, p = hsvcp->clients; p != NULL;
p = (hsvc_info_t *)p->hsvc_private)
refcnt++;
ASSERT(hsvcp->refcnt == refcnt);
}
/*
* Dump registered clients information
*/
static void
hsvc_dump(void)
{
hsvc_t *hsvcp;
hsvc_info_t *p;
mutex_enter(&hsvc_lock);
prom_printf("hsvc_dump: hsvc_groups: %p hsvc_avail: %p\n",
(void *)hsvc_groups, (void *)hsvc_avail);
for (hsvcp = hsvc_groups; hsvcp != NULL; hsvcp = hsvcp->next) {
prom_printf(" hsvcp: %p (0x%lx 0x%lx 0x%lx) ref: %ld clients: "
"%p\n", (void *)hsvcp, hsvcp->group, hsvcp->major,
hsvcp->minor, hsvcp->refcnt, (void *)hsvcp->clients);
for (p = hsvcp->clients; p != NULL;
p = (hsvc_info_t *)p->hsvc_private) {
prom_printf(" client %p (0x%lx 0x%lx 0x%lx '%s') "
"private: %p\n", (void *)p, p->hsvc_group,
p->hsvc_major, p->hsvc_minor, p->hsvc_modname,
p->hsvc_private);
}
}
mutex_exit(&hsvc_lock);
}
#endif /* DEBUG */
/*
* Allocate a buffer to cache API group information. Note that we
* allocate a buffer from reserved pool early on, before kmem_xxx
* interface becomes available.
*/
static hsvc_t *
hsvc_alloc(void)
{
hsvc_t *hsvcp;
ASSERT(MUTEX_HELD(&hsvc_lock));
if (hsvc_avail != NULL) {
hsvcp = hsvc_avail;
hsvc_avail = hsvcp->next;
} else if (kmem_ready) {
hsvcp = kmem_zalloc(sizeof (hsvc_t), KM_SLEEP);
HSVC_DEBUG(DBG_HSVC_ALLOC,
("hsvc_alloc: hsvc_avail: %p kmem_zalloc hsvcp: %p\n",
(void *)hsvc_avail, (void *)hsvcp));
} else
hsvcp = NULL;
return (hsvcp);
}
static void
hsvc_free(hsvc_t *hsvcp)
{
ASSERT(hsvcp != NULL);
ASSERT(MUTEX_HELD(&hsvc_lock));
if (hsvcp >= hsvc_resv_bufs &&
hsvcp < &hsvc_resv_bufs[HSVC_RESV_BUFS_MAX]) {
hsvcp->next = hsvc_avail;
hsvc_avail = hsvcp;
} else {
HSVC_DEBUG(DBG_HSVC_ALLOC,
("hsvc_free: hsvc_avail: %p kmem_free hsvcp: %p\n",
(void *)hsvc_avail, (void *)hsvcp));
(void) kmem_free(hsvcp, sizeof (hsvc_t));
}
}
/*
* Link client on the specified hsvc's client list and
* bump the reference count.
*/
static void
hsvc_link_client(hsvc_t *hsvcp, hsvc_info_t *hsvcinfop)
{
ASSERT(MUTEX_HELD(&hsvc_lock));
HSVC_CHK_REFCNT(hsvcp);
hsvcinfop->hsvc_private = hsvcp->clients;
hsvcp->clients = hsvcinfop;
hsvcp->refcnt++;
}
/*
* Unlink a client from the specified hsvc's client list and
* decrement the reference count, if found.
*
* Return 0 if client unlinked. Otherwise return -1.
*/
static int
hsvc_unlink_client(hsvc_t *hsvcp, hsvc_info_t *hsvcinfop)
{
hsvc_info_t *p, **pp;
int status = 0;
ASSERT(MUTEX_HELD(&hsvc_lock));
HSVC_CHK_REFCNT(hsvcp);
for (pp = &hsvcp->clients; (p = *pp) != NULL;
pp = (hsvc_info_t **)&p->hsvc_private) {
if (p != hsvcinfop)
continue;
ASSERT(hsvcp->refcnt > 0);
hsvcp->refcnt--;
*pp = (hsvc_info_t *)p->hsvc_private;
p->hsvc_private = NULL;
break;
}
if (p == NULL)
status = -1;
return (status);
}
/*
* Negotiate/register an API group usage
*/
int
hsvc_register(hsvc_info_t *hsvcinfop, uint64_t *supported_minor)
{
hsvc_t *hsvcp;
uint64_t api_group = hsvcinfop->hsvc_group;
uint64_t major = hsvcinfop->hsvc_major;
uint64_t minor = hsvcinfop->hsvc_minor;
int status = 0;
HSVC_DEBUG(DBG_HSVC_REGISTER,
("hsvc_register %p (0x%lx 0x%lx 0x%lx ID %s)\n", (void *)hsvcinfop,
api_group, major, minor, hsvcinfop->hsvc_modname));
if (hsvcinfop->hsvc_rev != HSVC_REV_1)
return (EINVAL);
mutex_enter(&hsvc_lock);
/*
* Make sure that the hsvcinfop is new (i.e. not already registered).
*/
if (hsvc_lookup(hsvcinfop) != NULL) {
mutex_exit(&hsvc_lock);
return (EINVAL);
}
/*
* Search for the specified api_group
*/
for (hsvcp = hsvc_groups; hsvcp != NULL; hsvcp = hsvcp->next)
if (hsvcp->group == api_group)
break;
if (hsvcp) {
/*
* If major number mismatch, then return ENOTSUP.
* Otherwise return currently negotiated minor
* and the following status:
* ENOTSUP requested minor < current minor
* OK requested minor >= current minor
*/
if (hsvcp->major != major) {
status = ENOTSUP;
} else if (hsvcp->minor > minor) {
/*
* Client requested a lower minor number than
* currently in use.
*/
status = ENOTSUP;
*supported_minor = hsvcp->minor;
} else {
/*
* Client requested a minor number same or higher
* than the one in use. Set supported minor number
* and link the client on hsvc client linked list.
*/
*supported_minor = hsvcp->minor;
hsvc_link_client(hsvcp, hsvcinfop);
}
} else {
/*
* This service group has not been negotiated yet.
* Call OBP CIF interface to negotiate a major/minor
* number.
*
* If not enough memory to cache this information, then
* return EAGAIN so that the caller can try again later.
* Otherwise, process OBP CIF results as follows:
*
* H_BADTRAP OBP CIF interface is not supported.
* If not a pre-versioning group, then
* return EINVAL, indicating unsupported
* API group. Otherwise, mimic default
* behavior (i.e. support only major=1).
*
* H_EOK Negotiation was successful. Cache
* and return supported major/minor,
* limiting the minor number to the
* requested value.
*
* H_EINVAL Invalid group. Return EINVAL
*
* H_ENOTSUPPORTED Unsupported major number. Return
* ENOTSUP.
*
* H_EBUSY Return EAGAIN.
*
* H_EWOULDBLOCK Return EAGAIN.
*/
hsvcp = hsvc_alloc();
if (hsvcp == NULL) {
status = EAGAIN;
} else {
uint64_t hvstat;
hvstat = prom_set_sun4v_api_version(api_group,
major, minor, supported_minor);
HSVC_DEBUG(DBG_HSVC_OBP_CIF,
("prom_set_sun4v_api_ver: 0x%lx 0x%lx, 0x%lx "
" hvstat: 0x%lx sup_minor: 0x%lx\n", api_group,
major, minor, hvstat, *supported_minor));
switch (hvstat) {
case H_EBADTRAP:
/*
* Older firmware does not support OBP CIF
* interface. If it's a pre-versioning group,
* then assume that the firmware supports
* only major=1 and minor=0.
*/
if (!pre_versioning_group(api_group)) {
status = EINVAL;
break;
} else if (major != 1) {
status = ENOTSUP;
break;
}
/*
* It's a preversioning group. Default minor
* value to 0.
*/
*supported_minor = 0;
/*FALLTHROUGH*/
case H_EOK:
/*
* Limit supported minor number to the
* requested value and cache the new
* API group information.
*/
if (*supported_minor > minor)
*supported_minor = minor;
hsvcp->group = api_group;
hsvcp->major = major;
hsvcp->minor = *supported_minor;
hsvcp->refcnt = 0;
hsvcp->clients = NULL;
hsvcp->next = hsvc_groups;
hsvc_groups = hsvcp;
/*
* Link the caller on the client linked list.
*/
hsvc_link_client(hsvcp, hsvcinfop);
break;
case H_ENOTSUPPORTED:
status = ENOTSUP;
break;
case H_EBUSY:
case H_EWOULDBLOCK:
status = EAGAIN;
break;
case H_EINVAL:
default:
status = EINVAL;
break;
}
}
/*
* Deallocate entry if not used
*/
if (status != 0)
hsvc_free(hsvcp);
}
mutex_exit(&hsvc_lock);
HSVC_DEBUG(DBG_HSVC_REGISTER,
("hsvc_register(%p) status; %d sup_minor: 0x%lx\n",
(void *)hsvcinfop, status, *supported_minor));
return (status);
}
/*
* Unregister an API group usage
*/
int
hsvc_unregister(hsvc_info_t *hsvcinfop)
{
hsvc_t **hsvcpp, *hsvcp;
uint64_t api_group;
uint64_t major, supported_minor;
int status = 0;
if (hsvcinfop->hsvc_rev != HSVC_REV_1)
return (EINVAL);
major = hsvcinfop->hsvc_major;
api_group = hsvcinfop->hsvc_group;
HSVC_DEBUG(DBG_HSVC_UNREGISTER,
("hsvc_unregister %p (0x%lx 0x%lx 0x%lx ID %s)\n",
(void *)hsvcinfop, api_group, major, hsvcinfop->hsvc_minor,
hsvcinfop->hsvc_modname));
/*
* Search for the matching entry and return EINVAL if no match found.
* Otherwise, remove it from our list and unregister the API
* group if this was the last reference to that API group.
*/
mutex_enter(&hsvc_lock);
for (hsvcpp = &hsvc_groups; (hsvcp = *hsvcpp) != NULL;
hsvcpp = &hsvcp->next) {
if (hsvcp->group != api_group || hsvcp->major != major)
continue;
/*
* Search client list for a matching hsvcinfop entry
* and unlink it and decrement refcnt, if found.
*/
if (hsvc_unlink_client(hsvcp, hsvcinfop) < 0) {
/* client not registered */
status = EINVAL;
break;
}
/*
* Client has been unlinked. If this was the last
* reference, unregister API group via OBP CIF
* interface.
*/
if (hsvcp->refcnt == 0) {
uint64_t hvstat;
ASSERT(hsvcp->clients == NULL);
hvstat = prom_set_sun4v_api_version(api_group, 0, 0,
&supported_minor);
HSVC_DEBUG(DBG_HSVC_OBP_CIF,
(" prom unreg group: 0x%lx hvstat: 0x%lx\n",
api_group, hvstat));
/*
* Note that the call to unnegotiate an API group
* may fail if anyone, including OBP, is using
* those services. However, the caller is done
* with this API group and should be allowed to
* unregister regardless of the outcome.
*/
*hsvcpp = hsvcp->next;
hsvc_free(hsvcp);
}
break;
}
if (hsvcp == NULL)
status = EINVAL;
mutex_exit(&hsvc_lock);
HSVC_DEBUG(DBG_HSVC_UNREGISTER,
("hsvc_unregister %p status: %d\n", (void *)hsvcinfop, status));
return (status);
}
/*
* Get negotiated major/minor version number for an API group
*/
int
hsvc_version(uint64_t api_group, uint64_t *majorp, uint64_t *minorp)
{
int status = 0;
uint64_t hvstat;
hsvc_t *hsvcp;
/*
* Check if the specified api_group is already in use.
* If so, return currently negotiated major/minor number.
* Otherwise, call OBP CIF interface to get the currently
* negotiated major/minor number.
*/
mutex_enter(&hsvc_lock);
for (hsvcp = hsvc_groups; hsvcp != NULL; hsvcp = hsvcp->next)
if (hsvcp->group == api_group)
break;
if (hsvcp) {
*majorp = hsvcp->major;
*minorp = hsvcp->minor;
} else {
hvstat = prom_get_sun4v_api_version(api_group, majorp, minorp);
switch (hvstat) {
case H_EBADTRAP:
/*
* Older firmware does not support OBP CIF
* interface. If it's a pre-versioning group,
* then return default major/minor (i.e. 1/0).
* Otherwise, return EINVAL.
*/
if (pre_versioning_group(api_group)) {
*majorp = 1;
*minorp = 0;
} else
status = EINVAL;
break;
case H_EINVAL:
default:
status = EINVAL;
break;
}
}
mutex_exit(&hsvc_lock);
HSVC_DEBUG(DBG_HSVC_VERSION,
("hsvc_version(0x%lx) status: %d major: 0x%lx minor: 0x%lx\n",
api_group, status, *majorp, *minorp));
return (status);
}
/*
* Initialize framework data structures
*/
void
hsvc_init(void)
{
int i;
hsvc_t *hsvcp;
/*
* Initialize global data structures
*/
mutex_init(&hsvc_lock, NULL, MUTEX_DEFAULT, NULL);
hsvc_groups = NULL;
hsvc_avail = NULL;
/*
* Setup initial free list
*/
mutex_enter(&hsvc_lock);
for (i = 0, hsvcp = &hsvc_resv_bufs[0];
i < HSVC_RESV_BUFS_MAX; i++, hsvcp++)
hsvc_free(hsvcp);
mutex_exit(&hsvc_lock);
}
/*
* Hypervisor services to be negotiated at boot time.
*
* Note that the kernel needs to negotiate the HSVC_GROUP_SUN4V
* API group first, before doing any other negotiation. Also, it
* uses hypervisor services belonging to the HSVC_GROUP_CORE API
* group only for itself.
*
* Note that the HSVC_GROUP_DIAG is negotiated on behalf of
* any driver/module using DIAG services.
*/
typedef struct hsvc_info_unix_s {
hsvc_info_t hsvcinfo;
int required;
} hsvc_info_unix_t;
static hsvc_info_unix_t hsvcinfo_unix[] = {
{{HSVC_REV_1, NULL, HSVC_GROUP_SUN4V, 1, 0, NULL}, 1},
{{HSVC_REV_1, NULL, HSVC_GROUP_CORE, 1, 2, NULL}, 1},
{{HSVC_REV_1, NULL, HSVC_GROUP_DIAG, 1, 0, NULL}, 1},
{{HSVC_REV_1, NULL, HSVC_GROUP_INTR, 1, 0, NULL}, 0},
};
#define HSVCINFO_UNIX_CNT (sizeof (hsvcinfo_unix) / sizeof (hsvc_info_t))
static char *hsvcinfo_unix_modname = "unix";
/*
* Initialize framework and register hypervisor services to be used
* by the kernel.
*/
void
hsvc_setup()
{
int i, status;
uint64_t sup_minor;
hsvc_info_unix_t *hsvcinfop;
/*
* Initialize framework
*/
hsvc_init();
/*
* Negotiate versioning for required groups
*/
for (hsvcinfop = &hsvcinfo_unix[0], i = 0; i < HSVCINFO_UNIX_CNT;
i++, hsvcinfop++) {
hsvcinfop->hsvcinfo.hsvc_private = NULL;
hsvcinfop->hsvcinfo.hsvc_modname = hsvcinfo_unix_modname;
status = hsvc_register(&(hsvcinfop->hsvcinfo), &sup_minor);
if ((status != 0) && hsvcinfop->required) {
cmn_err(CE_PANIC, "%s: cannot negotiate hypervisor "
"services - group: 0x%lx major: 0x%lx minor: 0x%lx"
" errno: %d\n", hsvcinfop->hsvcinfo.hsvc_modname,
hsvcinfop->hsvcinfo.hsvc_group,
hsvcinfop->hsvcinfo.hsvc_major,
hsvcinfop->hsvcinfo.hsvc_minor, status);
}
}
HSVC_DUMP();
}
|