]> git.saurik.com Git - apple/xnu.git/blame - osfmk/x86_64/machine_remote_time.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / osfmk / x86_64 / machine_remote_time.c
CommitLineData
0a7de745
A
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>
cb323159 30#include <machine/atomic.h>
0a7de745
A
31#include <kern/locks.h>
32#include <kern/clock.h>
33
34void mach_bridge_send_timestamp(uint64_t timestamp);
35
36extern _Atomic uint32_t bt_init_flag;
37extern lck_spin_t *bt_maintenance_lock;
38extern void mach_bridge_timer_init(void);
39extern uint32_t bt_enable_flag;
40
41/*
42 * Delay sending timestamps by certain interval to
43 * avoid overwriting sentinel values
44 */
45#define DELAY_INTERVAL_NS (50 * NSEC_PER_MSEC)
46static uint64_t bt_delay_timestamp = 0;
47static mach_bridge_regwrite_timestamp_func_t bridge_regwrite_timestamp_callback = NULL;
48
49/*
50 * This function should only be called by the kext
51 * responsible for sending timestamps across the link
52 */
53void
54mach_bridge_register_regwrite_timestamp_callback(mach_bridge_regwrite_timestamp_func_t func)
55{
56 static uint64_t delay_amount = 0;
57
cb323159 58 if (!os_atomic_load(&bt_init_flag, relaxed)) {
0a7de745
A
59 mach_bridge_timer_init();
60 nanoseconds_to_absolutetime(DELAY_INTERVAL_NS, &delay_amount);
cb323159 61 os_atomic_store(&bt_init_flag, 1, release);
0a7de745
A
62 }
63
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);
69}
70
71void
72mach_bridge_send_timestamp(uint64_t timestamp)
73{
74 LCK_SPIN_ASSERT(bt_maintenance_lock, LCK_ASSERT_OWNED);
75
76 if (bt_delay_timestamp > 0) {
77 uint64_t now = mach_absolute_time();
78 if (now < bt_delay_timestamp) {
79 return;
80 }
81 bt_delay_timestamp = 0;
82 }
83
84 if (bridge_regwrite_timestamp_callback) {
85 bridge_regwrite_timestamp_callback(timestamp);
86 }
87}