xref: /xnu-11215.1.10/osfmk/x86_64/machine_remote_time.c (revision 8d741a5de7ff4191bf97d57b9f54c2f6d4a15585)
1 /*
2  * Copyright (c) 2017 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #include <kern/misc_protos.h>
29 #include <x86_64/machine_remote_time.h>
30 #include <machine/atomic.h>
31 #include <kern/locks.h>
32 #include <kern/clock.h>
33 #include <kern/remote_time.h>
34 
35 void mach_bridge_send_timestamp(uint64_t timestamp);
36 
37 extern _Atomic uint32_t bt_init_flag;
38 extern uint32_t bt_enable_flag;
39 
40 /*
41  * Delay sending timestamps by certain interval to
42  * avoid overwriting sentinel values
43  */
44 #define DELAY_INTERVAL_NS (50 * NSEC_PER_MSEC)
45 static uint64_t bt_delay_timestamp = 0;
46 static mach_bridge_regwrite_timestamp_func_t bridge_regwrite_timestamp_callback = NULL;
47 
48 /*
49  * This function should only be called by the kext
50  * responsible for sending timestamps across the link
51  */
52 void
mach_bridge_register_regwrite_timestamp_callback(mach_bridge_regwrite_timestamp_func_t func)53 mach_bridge_register_regwrite_timestamp_callback(mach_bridge_regwrite_timestamp_func_t func)
54 {
55 	static uint64_t delay_amount = 0;
56 
57 	if (!os_atomic_load(&bt_init_flag, relaxed)) {
58 		nanoseconds_to_absolutetime(DELAY_INTERVAL_NS, &delay_amount);
59 		os_atomic_store(&bt_init_flag, 1, release);
60 	}
61 
62 	lck_spin_lock(&bt_maintenance_lock);
63 	bridge_regwrite_timestamp_callback = func;
64 	bt_enable_flag = (func != NULL) ? 1 : 0;
65 	bt_delay_timestamp = mach_absolute_time() + delay_amount;
66 	lck_spin_unlock(&bt_maintenance_lock);
67 }
68 
69 void
mach_bridge_send_timestamp(uint64_t timestamp)70 mach_bridge_send_timestamp(uint64_t timestamp)
71 {
72 	LCK_SPIN_ASSERT(&bt_maintenance_lock, LCK_ASSERT_OWNED);
73 
74 	if (bt_delay_timestamp > 0) {
75 		uint64_t now = mach_absolute_time();
76 		if (now < bt_delay_timestamp) {
77 			return;
78 		}
79 		bt_delay_timestamp = 0;
80 	}
81 
82 	if (bridge_regwrite_timestamp_callback) {
83 		bridge_regwrite_timestamp_callback(timestamp);
84 	}
85 }
86