Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

hyperv.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Hyper-V driver
  27. *
  28. */
  29. #include <stdlib.h>
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <assert.h>
  34. #include <errno.h>
  35. #include <byteswap.h>
  36. #include <pic8259.h>
  37. #include <ipxe/malloc.h>
  38. #include <ipxe/device.h>
  39. #include <ipxe/timer.h>
  40. #include <ipxe/cpuid.h>
  41. #include <ipxe/msr.h>
  42. #include <ipxe/hyperv.h>
  43. #include <ipxe/vmbus.h>
  44. #include "hyperv.h"
  45. /** Maximum time to wait for a message response
  46. *
  47. * This is a policy decision.
  48. */
  49. #define HV_MESSAGE_MAX_WAIT_MS 1000
  50. /** Hyper-V timer frequency (fixed 10Mhz) */
  51. #define HV_TIMER_HZ 10000000
  52. /** Hyper-V timer scale factor (used to avoid 64-bit division) */
  53. #define HV_TIMER_SHIFT 18
  54. /**
  55. * Convert a Hyper-V status code to an iPXE status code
  56. *
  57. * @v status Hyper-V status code
  58. * @ret rc iPXE status code (before negation)
  59. */
  60. #define EHV( status ) EPLATFORM ( EINFO_EPLATFORM, (status) )
  61. /**
  62. * Allocate zeroed pages
  63. *
  64. * @v hv Hyper-V hypervisor
  65. * @v ... Page addresses to fill in, terminated by NULL
  66. * @ret rc Return status code
  67. */
  68. __attribute__ (( sentinel )) int
  69. hv_alloc_pages ( struct hv_hypervisor *hv, ... ) {
  70. va_list args;
  71. void **page;
  72. int i;
  73. /* Allocate and zero pages */
  74. va_start ( args, hv );
  75. for ( i = 0 ; ( ( page = va_arg ( args, void ** ) ) != NULL ); i++ ) {
  76. *page = malloc_dma ( PAGE_SIZE, PAGE_SIZE );
  77. if ( ! *page )
  78. goto err_alloc;
  79. memset ( *page, 0, PAGE_SIZE );
  80. }
  81. va_end ( args );
  82. return 0;
  83. err_alloc:
  84. va_end ( args );
  85. va_start ( args, hv );
  86. for ( ; i >= 0 ; i-- ) {
  87. page = va_arg ( args, void ** );
  88. free_dma ( *page, PAGE_SIZE );
  89. }
  90. va_end ( args );
  91. return -ENOMEM;
  92. }
  93. /**
  94. * Free pages
  95. *
  96. * @v hv Hyper-V hypervisor
  97. * @v ... Page addresses, terminated by NULL
  98. */
  99. __attribute__ (( sentinel )) void
  100. hv_free_pages ( struct hv_hypervisor *hv, ... ) {
  101. va_list args;
  102. void *page;
  103. va_start ( args, hv );
  104. while ( ( page = va_arg ( args, void * ) ) != NULL )
  105. free_dma ( page, PAGE_SIZE );
  106. va_end ( args );
  107. }
  108. /**
  109. * Allocate message buffer
  110. *
  111. * @v hv Hyper-V hypervisor
  112. * @ret rc Return status code
  113. */
  114. static int hv_alloc_message ( struct hv_hypervisor *hv ) {
  115. /* Allocate buffer. Must be aligned to at least 8 bytes and
  116. * must not cross a page boundary, so align on its own size.
  117. */
  118. hv->message = malloc_dma ( sizeof ( *hv->message ),
  119. sizeof ( *hv->message ) );
  120. if ( ! hv->message )
  121. return -ENOMEM;
  122. return 0;
  123. }
  124. /**
  125. * Free message buffer
  126. *
  127. * @v hv Hyper-V hypervisor
  128. */
  129. static void hv_free_message ( struct hv_hypervisor *hv ) {
  130. /* Free buffer */
  131. free_dma ( hv->message, sizeof ( *hv->message ) );
  132. }
  133. /**
  134. * Check whether or not we are running in Hyper-V
  135. *
  136. * @ret rc Return status code
  137. */
  138. static int hv_check_hv ( void ) {
  139. struct x86_features features;
  140. uint32_t interface_id;
  141. uint32_t discard_ebx;
  142. uint32_t discard_ecx;
  143. uint32_t discard_edx;
  144. /* Check for presence of a hypervisor (not necessarily Hyper-V) */
  145. x86_features ( &features );
  146. if ( ! ( features.intel.ecx & CPUID_FEATURES_INTEL_ECX_HYPERVISOR ) ) {
  147. DBGC ( HV_INTERFACE_ID, "HV not running in a hypervisor\n" );
  148. return -ENODEV;
  149. }
  150. /* Check that hypervisor is Hyper-V */
  151. cpuid ( HV_CPUID_INTERFACE_ID, &interface_id, &discard_ebx,
  152. &discard_ecx, &discard_edx );
  153. if ( interface_id != HV_INTERFACE_ID ) {
  154. DBGC ( HV_INTERFACE_ID, "HV not running in Hyper-V (interface "
  155. "ID %#08x)\n", interface_id );
  156. return -ENODEV;
  157. }
  158. return 0;
  159. }
  160. /**
  161. * Check required features
  162. *
  163. * @v hv Hyper-V hypervisor
  164. * @ret rc Return status code
  165. */
  166. static int hv_check_features ( struct hv_hypervisor *hv ) {
  167. uint32_t available;
  168. uint32_t permissions;
  169. uint32_t discard_ecx;
  170. uint32_t discard_edx;
  171. /* Check that required features and privileges are available */
  172. cpuid ( HV_CPUID_FEATURES, &available, &permissions, &discard_ecx,
  173. &discard_edx );
  174. if ( ! ( available & HV_FEATURES_AVAIL_HYPERCALL_MSR ) ) {
  175. DBGC ( hv, "HV %p has no hypercall MSRs (features %08x:%08x)\n",
  176. hv, available, permissions );
  177. return -ENODEV;
  178. }
  179. if ( ! ( available & HV_FEATURES_AVAIL_SYNIC_MSR ) ) {
  180. DBGC ( hv, "HV %p has no SynIC MSRs (features %08x:%08x)\n",
  181. hv, available, permissions );
  182. return -ENODEV;
  183. }
  184. if ( ! ( permissions & HV_FEATURES_PERM_POST_MESSAGES ) ) {
  185. DBGC ( hv, "HV %p cannot post messages (features %08x:%08x)\n",
  186. hv, available, permissions );
  187. return -EACCES;
  188. }
  189. if ( ! ( permissions & HV_FEATURES_PERM_SIGNAL_EVENTS ) ) {
  190. DBGC ( hv, "HV %p cannot signal events (features %08x:%08x)",
  191. hv, available, permissions );
  192. return -EACCES;
  193. }
  194. return 0;
  195. }
  196. /**
  197. * Map hypercall page
  198. *
  199. * @v hv Hyper-V hypervisor
  200. * @ret rc Return status code
  201. */
  202. static int hv_map_hypercall ( struct hv_hypervisor *hv ) {
  203. union {
  204. struct {
  205. uint32_t ebx;
  206. uint32_t ecx;
  207. uint32_t edx;
  208. } __attribute__ (( packed ));
  209. char text[ 13 /* "bbbbccccdddd" + NUL */ ];
  210. } vendor_id;
  211. uint32_t build;
  212. uint32_t version;
  213. uint32_t discard_eax;
  214. uint32_t discard_ecx;
  215. uint32_t discard_edx;
  216. uint64_t guest_os_id;
  217. uint64_t hypercall;
  218. /* Report guest OS identity */
  219. guest_os_id = rdmsr ( HV_X64_MSR_GUEST_OS_ID );
  220. if ( guest_os_id != 0 ) {
  221. DBGC ( hv, "HV %p guest OS ID MSR was %#08llx\n",
  222. hv, guest_os_id );
  223. }
  224. guest_os_id = HV_GUEST_OS_ID_IPXE;
  225. DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
  226. wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
  227. /* Get hypervisor system identity (for debugging) */
  228. cpuid ( HV_CPUID_VENDOR_ID, &discard_eax, &vendor_id.ebx,
  229. &vendor_id.ecx, &vendor_id.edx );
  230. vendor_id.text[ sizeof ( vendor_id.text ) - 1 ] = '\0';
  231. cpuid ( HV_CPUID_HYPERVISOR_ID, &build, &version, &discard_ecx,
  232. &discard_edx );
  233. DBGC ( hv, "HV %p detected \"%s\" version %d.%d build %d\n", hv,
  234. vendor_id.text, ( version >> 16 ), ( version & 0xffff ), build );
  235. /* Map hypercall page */
  236. hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
  237. hypercall &= ( PAGE_SIZE - 1 );
  238. hypercall |= ( virt_to_phys ( hv->hypercall ) | HV_HYPERCALL_ENABLE );
  239. DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
  240. wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
  241. return 0;
  242. }
  243. /**
  244. * Unmap hypercall page
  245. *
  246. * @v hv Hyper-V hypervisor
  247. */
  248. static void hv_unmap_hypercall ( struct hv_hypervisor *hv ) {
  249. uint64_t hypercall;
  250. uint64_t guest_os_id;
  251. /* Unmap the hypercall page */
  252. hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
  253. hypercall &= ( ( PAGE_SIZE - 1 ) & ~HV_HYPERCALL_ENABLE );
  254. DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
  255. wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
  256. /* Reset the guest OS identity */
  257. guest_os_id = 0;
  258. DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
  259. wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
  260. }
  261. /**
  262. * Map synthetic interrupt controller
  263. *
  264. * @v hv Hyper-V hypervisor
  265. * @ret rc Return status code
  266. */
  267. static int hv_map_synic ( struct hv_hypervisor *hv ) {
  268. uint64_t simp;
  269. uint64_t siefp;
  270. uint64_t scontrol;
  271. /* Map SynIC message page */
  272. simp = rdmsr ( HV_X64_MSR_SIMP );
  273. simp &= ( PAGE_SIZE - 1 );
  274. simp |= ( virt_to_phys ( hv->synic.message ) | HV_SIMP_ENABLE );
  275. DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
  276. wrmsr ( HV_X64_MSR_SIMP, simp );
  277. /* Map SynIC event page */
  278. siefp = rdmsr ( HV_X64_MSR_SIEFP );
  279. siefp &= ( PAGE_SIZE - 1 );
  280. siefp |= ( virt_to_phys ( hv->synic.event ) | HV_SIEFP_ENABLE );
  281. DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
  282. wrmsr ( HV_X64_MSR_SIEFP, siefp );
  283. /* Enable SynIC */
  284. scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
  285. scontrol |= HV_SCONTROL_ENABLE;
  286. DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
  287. wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
  288. return 0;
  289. }
  290. /**
  291. * Unmap synthetic interrupt controller
  292. *
  293. * @v hv Hyper-V hypervisor
  294. */
  295. static void hv_unmap_synic ( struct hv_hypervisor *hv ) {
  296. uint64_t scontrol;
  297. uint64_t siefp;
  298. uint64_t simp;
  299. /* Disable SynIC */
  300. scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
  301. scontrol &= ~HV_SCONTROL_ENABLE;
  302. DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
  303. wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
  304. /* Unmap SynIC event page */
  305. siefp = rdmsr ( HV_X64_MSR_SIEFP );
  306. siefp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIEFP_ENABLE );
  307. DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
  308. wrmsr ( HV_X64_MSR_SIEFP, siefp );
  309. /* Unmap SynIC message page */
  310. simp = rdmsr ( HV_X64_MSR_SIMP );
  311. simp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIMP_ENABLE );
  312. DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
  313. wrmsr ( HV_X64_MSR_SIMP, simp );
  314. }
  315. /**
  316. * Enable synthetic interrupt
  317. *
  318. * @v hv Hyper-V hypervisor
  319. * @v sintx Synthetic interrupt number
  320. */
  321. void hv_enable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
  322. unsigned long msr = HV_X64_MSR_SINT ( sintx );
  323. uint64_t sint;
  324. /* Enable synthetic interrupt
  325. *
  326. * We have to enable the interrupt, otherwise messages will
  327. * not be delivered (even though the documentation implies
  328. * that polling for messages is possible). We enable AutoEOI
  329. * and hook the interrupt to the obsolete IRQ13 (FPU
  330. * exception) vector, which will be implemented as a no-op.
  331. */
  332. sint = rdmsr ( msr );
  333. sint &= ~( HV_SINT_MASKED | HV_SINT_VECTOR_MASK );
  334. sint |= ( HV_SINT_AUTO_EOI |
  335. HV_SINT_VECTOR ( IRQ_INT ( 13 /* See comment above */ ) ) );
  336. DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
  337. wrmsr ( msr, sint );
  338. }
  339. /**
  340. * Disable synthetic interrupt
  341. *
  342. * @v hv Hyper-V hypervisor
  343. * @v sintx Synthetic interrupt number
  344. */
  345. void hv_disable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
  346. unsigned long msr = HV_X64_MSR_SINT ( sintx );
  347. uint64_t sint;
  348. /* Disable synthetic interrupt */
  349. sint = rdmsr ( msr );
  350. sint &= ~HV_SINT_AUTO_EOI;
  351. sint |= HV_SINT_MASKED;
  352. DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
  353. wrmsr ( msr, sint );
  354. }
  355. /**
  356. * Post message
  357. *
  358. * @v hv Hyper-V hypervisor
  359. * @v id Connection ID
  360. * @v type Message type
  361. * @v data Message
  362. * @v len Length of message
  363. * @ret rc Return status code
  364. */
  365. int hv_post_message ( struct hv_hypervisor *hv, unsigned int id,
  366. unsigned int type, const void *data, size_t len ) {
  367. struct hv_post_message *msg = &hv->message->posted;
  368. int status;
  369. int rc;
  370. /* Sanity check */
  371. assert ( len <= sizeof ( msg->data ) );
  372. /* Construct message */
  373. memset ( msg, 0, sizeof ( *msg ) );
  374. msg->id = cpu_to_le32 ( id );
  375. msg->type = cpu_to_le32 ( type );
  376. msg->len = cpu_to_le32 ( len );
  377. memcpy ( msg->data, data, len );
  378. DBGC2 ( hv, "HV %p connection %d posting message type %#08x:\n",
  379. hv, id, type );
  380. DBGC2_HDA ( hv, 0, msg->data, len );
  381. /* Post message */
  382. if ( ( status = hv_call ( hv, HV_POST_MESSAGE, msg, NULL ) ) != 0 ) {
  383. rc = -EHV ( status );
  384. DBGC ( hv, "HV %p could not post message to %#08x: %s\n",
  385. hv, id, strerror ( rc ) );
  386. return rc;
  387. }
  388. return 0;
  389. }
  390. /**
  391. * Wait for received message
  392. *
  393. * @v hv Hyper-V hypervisor
  394. * @v sintx Synthetic interrupt number
  395. * @ret rc Return status code
  396. */
  397. int hv_wait_for_message ( struct hv_hypervisor *hv, unsigned int sintx ) {
  398. struct hv_message *msg = &hv->message->received;
  399. struct hv_message *src = &hv->synic.message[sintx];
  400. unsigned int retries;
  401. size_t len;
  402. /* Wait for message to arrive */
  403. for ( retries = 0 ; retries < HV_MESSAGE_MAX_WAIT_MS ; retries++ ) {
  404. /* Check for message */
  405. if ( src->type ) {
  406. /* Copy message */
  407. memset ( msg, 0, sizeof ( *msg ) );
  408. len = src->len;
  409. assert ( len <= sizeof ( *msg ) );
  410. memcpy ( msg, src,
  411. ( offsetof ( typeof ( *msg ), data ) + len ) );
  412. DBGC2 ( hv, "HV %p SINT%d received message type "
  413. "%#08x:\n", hv, sintx,
  414. le32_to_cpu ( msg->type ) );
  415. DBGC2_HDA ( hv, 0, msg->data, len );
  416. /* Consume message */
  417. src->type = 0;
  418. return 0;
  419. }
  420. /* Trigger message delivery */
  421. wrmsr ( HV_X64_MSR_EOM, 0 );
  422. /* Delay */
  423. mdelay ( 1 );
  424. }
  425. DBGC ( hv, "HV %p SINT%d timed out waiting for message\n",
  426. hv, sintx );
  427. return -ETIMEDOUT;
  428. }
  429. /**
  430. * Signal event
  431. *
  432. * @v hv Hyper-V hypervisor
  433. * @v id Connection ID
  434. * @v flag Flag number
  435. * @ret rc Return status code
  436. */
  437. int hv_signal_event ( struct hv_hypervisor *hv, unsigned int id,
  438. unsigned int flag ) {
  439. struct hv_signal_event *event = &hv->message->signalled;
  440. int status;
  441. int rc;
  442. /* Construct event */
  443. memset ( event, 0, sizeof ( *event ) );
  444. event->id = cpu_to_le32 ( id );
  445. event->flag = cpu_to_le16 ( flag );
  446. /* Signal event */
  447. if ( ( status = hv_call ( hv, HV_SIGNAL_EVENT, event, NULL ) ) != 0 ) {
  448. rc = -EHV ( status );
  449. DBGC ( hv, "HV %p could not signal event to %#08x: %s\n",
  450. hv, id, strerror ( rc ) );
  451. return rc;
  452. }
  453. return 0;
  454. }
  455. /**
  456. * Probe root device
  457. *
  458. * @v rootdev Root device
  459. * @ret rc Return status code
  460. */
  461. static int hv_probe ( struct root_device *rootdev ) {
  462. struct hv_hypervisor *hv;
  463. int rc;
  464. /* Check we are running in Hyper-V */
  465. if ( ( rc = hv_check_hv() ) != 0 )
  466. goto err_check_hv;
  467. /* Allocate and initialise structure */
  468. hv = zalloc ( sizeof ( *hv ) );
  469. if ( ! hv ) {
  470. rc = -ENOMEM;
  471. goto err_alloc;
  472. }
  473. /* Check features */
  474. if ( ( rc = hv_check_features ( hv ) ) != 0 )
  475. goto err_check_features;
  476. /* Allocate pages */
  477. if ( ( rc = hv_alloc_pages ( hv, &hv->hypercall, &hv->synic.message,
  478. &hv->synic.event, NULL ) ) != 0 )
  479. goto err_alloc_pages;
  480. /* Allocate message buffer */
  481. if ( ( rc = hv_alloc_message ( hv ) ) != 0 )
  482. goto err_alloc_message;
  483. /* Map hypercall page */
  484. if ( ( rc = hv_map_hypercall ( hv ) ) != 0 )
  485. goto err_map_hypercall;
  486. /* Map synthetic interrupt controller */
  487. if ( ( rc = hv_map_synic ( hv ) ) != 0 )
  488. goto err_map_synic;
  489. /* Probe Hyper-V devices */
  490. if ( ( rc = vmbus_probe ( hv, &rootdev->dev ) ) != 0 )
  491. goto err_vmbus_probe;
  492. rootdev_set_drvdata ( rootdev, hv );
  493. return 0;
  494. vmbus_remove ( hv, &rootdev->dev );
  495. err_vmbus_probe:
  496. hv_unmap_synic ( hv );
  497. err_map_synic:
  498. hv_unmap_hypercall ( hv );
  499. err_map_hypercall:
  500. hv_free_message ( hv );
  501. err_alloc_message:
  502. hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
  503. NULL );
  504. err_alloc_pages:
  505. err_check_features:
  506. free ( hv );
  507. err_alloc:
  508. err_check_hv:
  509. return rc;
  510. }
  511. /**
  512. * Remove root device
  513. *
  514. * @v rootdev Root device
  515. */
  516. static void hv_remove ( struct root_device *rootdev ) {
  517. struct hv_hypervisor *hv = rootdev_get_drvdata ( rootdev );
  518. vmbus_remove ( hv, &rootdev->dev );
  519. hv_unmap_synic ( hv );
  520. hv_unmap_hypercall ( hv );
  521. hv_free_message ( hv );
  522. hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
  523. NULL );
  524. free ( hv );
  525. }
  526. /** Hyper-V root device driver */
  527. static struct root_driver hv_root_driver = {
  528. .probe = hv_probe,
  529. .remove = hv_remove,
  530. };
  531. /** Hyper-V root device */
  532. struct root_device hv_root_device __root_device = {
  533. .dev = { .name = "Hyper-V" },
  534. .driver = &hv_root_driver,
  535. };
  536. /**
  537. * Probe timer
  538. *
  539. * @ret rc Return status code
  540. */
  541. static int hv_timer_probe ( void ) {
  542. uint32_t available;
  543. uint32_t discard_ebx;
  544. uint32_t discard_ecx;
  545. uint32_t discard_edx;
  546. int rc;
  547. /* Check we are running in Hyper-V */
  548. if ( ( rc = hv_check_hv() ) != 0 )
  549. return rc;
  550. /* Check for available reference counter */
  551. cpuid ( HV_CPUID_FEATURES, &available, &discard_ebx, &discard_ecx,
  552. &discard_edx );
  553. if ( ! ( available & HV_FEATURES_AVAIL_TIME_REF_COUNT_MSR ) ) {
  554. DBGC ( HV_INTERFACE_ID, "HV has no time reference counter\n" );
  555. return -ENODEV;
  556. }
  557. return 0;
  558. }
  559. /**
  560. * Get current system time in ticks
  561. *
  562. * @ret ticks Current time, in ticks
  563. */
  564. static unsigned long hv_currticks ( void ) {
  565. /* Calculate time using a combination of bit shifts and
  566. * multiplication (to avoid a 64-bit division).
  567. */
  568. return ( ( rdmsr ( HV_X64_MSR_TIME_REF_COUNT ) >> HV_TIMER_SHIFT ) *
  569. ( TICKS_PER_SEC / ( HV_TIMER_HZ >> HV_TIMER_SHIFT ) ) );
  570. }
  571. /**
  572. * Delay for a fixed number of microseconds
  573. *
  574. * @v usecs Number of microseconds for which to delay
  575. */
  576. static void hv_udelay ( unsigned long usecs ) {
  577. uint32_t start;
  578. uint32_t elapsed;
  579. uint32_t threshold;
  580. /* Spin until specified number of 10MHz ticks have elapsed */
  581. start = rdmsr ( HV_X64_MSR_TIME_REF_COUNT );
  582. threshold = ( usecs * ( HV_TIMER_HZ / 1000000 ) );
  583. do {
  584. elapsed = ( rdmsr ( HV_X64_MSR_TIME_REF_COUNT ) - start );
  585. } while ( elapsed < threshold );
  586. }
  587. /** Hyper-V timer */
  588. struct timer hv_timer __timer ( TIMER_PREFERRED ) = {
  589. .name = "Hyper-V",
  590. .probe = hv_timer_probe,
  591. .currticks = hv_currticks,
  592. .udelay = hv_udelay,
  593. };
  594. /* Drag in objects via hv_root_device */
  595. REQUIRING_SYMBOL ( hv_root_device );
  596. /* Drag in netvsc driver */
  597. REQUIRE_OBJECT ( netvsc );