2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
34 void mach_bridge_send_timestamp(uint64_t timestamp
);
36 extern _Atomic
uint32_t bt_init_flag
;
37 extern lck_spin_t
*bt_maintenance_lock
;
38 extern void mach_bridge_timer_init(void);
39 extern uint32_t bt_enable_flag
;
42 * Delay sending timestamps by certain interval to
43 * avoid overwriting sentinel values
45 #define DELAY_INTERVAL_NS (50 * NSEC_PER_MSEC)
46 static uint64_t bt_delay_timestamp
= 0;
47 static mach_bridge_regwrite_timestamp_func_t bridge_regwrite_timestamp_callback
= NULL
;
50 * This function should only be called by the kext
51 * responsible for sending timestamps across the link
54 mach_bridge_register_regwrite_timestamp_callback(mach_bridge_regwrite_timestamp_func_t func
)
56 static uint64_t delay_amount
= 0;
58 if (!os_atomic_load(&bt_init_flag
, relaxed
)) {
59 mach_bridge_timer_init();
60 nanoseconds_to_absolutetime(DELAY_INTERVAL_NS
, &delay_amount
);
61 os_atomic_store(&bt_init_flag
, 1, release
);
64 lck_spin_lock(bt_maintenance_lock
);
65 bridge_regwrite_timestamp_callback
= func
;
66 bt_enable_flag
= (func
!= NULL
) ? 1 : 0;
67 bt_delay_timestamp
= mach_absolute_time() + delay_amount
;
68 lck_spin_unlock(bt_maintenance_lock
);
72 mach_bridge_send_timestamp(uint64_t timestamp
)
74 LCK_SPIN_ASSERT(bt_maintenance_lock
, LCK_ASSERT_OWNED
);
76 if (bt_delay_timestamp
> 0) {
77 uint64_t now
= mach_absolute_time();
78 if (now
< bt_delay_timestamp
) {
81 bt_delay_timestamp
= 0;
84 if (bridge_regwrite_timestamp_callback
) {
85 bridge_regwrite_timestamp_callback(timestamp
);