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