]>
Commit | Line | Data |
---|---|---|
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> | |
f427ee49 | 33 | #include <kern/remote_time.h> |
0a7de745 A |
34 | |
35 | void mach_bridge_send_timestamp(uint64_t timestamp); | |
36 | ||
37 | extern _Atomic uint32_t bt_init_flag; | |
0a7de745 A |
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 | |
53 | mach_bridge_register_regwrite_timestamp_callback(mach_bridge_regwrite_timestamp_func_t func) | |
54 | { | |
55 | static uint64_t delay_amount = 0; | |
56 | ||
cb323159 | 57 | if (!os_atomic_load(&bt_init_flag, relaxed)) { |
0a7de745 | 58 | nanoseconds_to_absolutetime(DELAY_INTERVAL_NS, &delay_amount); |
cb323159 | 59 | os_atomic_store(&bt_init_flag, 1, release); |
0a7de745 A |
60 | } |
61 | ||
f427ee49 | 62 | lck_spin_lock(&bt_maintenance_lock); |
0a7de745 A |
63 | bridge_regwrite_timestamp_callback = func; |
64 | bt_enable_flag = (func != NULL) ? 1 : 0; | |
65 | bt_delay_timestamp = mach_absolute_time() + delay_amount; | |
f427ee49 | 66 | lck_spin_unlock(&bt_maintenance_lock); |
0a7de745 A |
67 | } |
68 | ||
69 | void | |
70 | mach_bridge_send_timestamp(uint64_t timestamp) | |
71 | { | |
f427ee49 | 72 | LCK_SPIN_ASSERT(&bt_maintenance_lock, LCK_ASSERT_OWNED); |
0a7de745 A |
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 | } |