]> git.saurik.com Git - apple/xnu.git/blame - osfmk/arm64/machine_remote_time.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / osfmk / arm64 / 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>
cb323159 29#include <machine/atomic.h>
0a7de745
A
30#include <mach/mach_time.h>
31#include <mach/clock_types.h>
32#include <kern/clock.h>
33#include <kern/locks.h>
34#include <arm64/machine_remote_time.h>
35#include <sys/kdebug.h>
36#include <arm/machine_routines.h>
cb323159 37#include <kern/remote_time.h>
0a7de745
A
38
39lck_spin_t *bt_spin_lock = NULL;
40_Atomic uint32_t bt_init_flag = 0;
41
42extern lck_spin_t *ts_conversion_lock;
43extern void mach_bridge_add_timestamp(uint64_t remote_timestamp, uint64_t local_timestamp);
44extern void bt_calibration_thread_start(void);
cb323159 45extern void bt_params_add(struct bt_params *params);
0a7de745
A
46
47void
48mach_bridge_init_timestamp(void)
49{
50 /* This function should be called only once by the driver
51 * implementing the interrupt handler for receiving timestamps */
cb323159 52 if (os_atomic_load(&bt_init_flag, relaxed)) {
0a7de745
A
53 return;
54 }
55
56 /* Initialize the locks */
57 static lck_grp_t *bt_lck_grp = NULL;
58
59 bt_lck_grp = lck_grp_alloc_init("bridgetimestamp", LCK_GRP_ATTR_NULL);
60 bt_spin_lock = lck_spin_alloc_init(bt_lck_grp, NULL);
61 ts_conversion_lock = lck_spin_alloc_init(bt_lck_grp, NULL);
62
cb323159 63 os_atomic_store(&bt_init_flag, 1, release);
0a7de745
A
64
65 /* Start the kernel thread only after all the locks have been initialized */
66 bt_calibration_thread_start();
67}
68
69/*
70 * Conditions: Should be called from primary interrupt context
71 */
72void
73mach_bridge_recv_timestamps(uint64_t remoteTimestamp, uint64_t localTimestamp)
74{
75 assert(ml_at_interrupt_context() == TRUE);
76
77 /* Ensure the locks have been initialized */
cb323159
A
78 if (!os_atomic_load(&bt_init_flag, acquire)) {
79 panic("%s called before mach_bridge_init_timestamp", __func__);
0a7de745
A
80 return;
81 }
82
83 KDBG(MACHDBG_CODE(DBG_MACH_CLOCK, MACH_BRIDGE_RCV_TS), localTimestamp, remoteTimestamp);
84
85 lck_spin_lock(bt_spin_lock);
86 mach_bridge_add_timestamp(remoteTimestamp, localTimestamp);
87 lck_spin_unlock(bt_spin_lock);
88
89 return;
90}
cb323159
A
91
92/*
93 * This function is used to set parameters, calculated externally,
94 * needed for mach_bridge_remote_time.
95 */
96void
97mach_bridge_set_params(uint64_t local_timestamp, uint64_t remote_timestamp, double rate)
98{
99 /* Ensure the locks have been initialized */
100 if (!os_atomic_load(&bt_init_flag, acquire)) {
101 panic("%s called before mach_bridge_init_timestamp", __func__);
102 return;
103 }
104
105 struct bt_params params = {};
106 params.base_local_ts = local_timestamp;
107 params.base_remote_ts = remote_timestamp;
108 params.rate = rate;
109 lck_spin_lock(ts_conversion_lock);
110 bt_params_add(&params);
111 lck_spin_unlock(ts_conversion_lock);
112 KDBG(MACHDBG_CODE(DBG_MACH_CLOCK, MACH_BRIDGE_TS_PARAMS), params.base_local_ts,
113 params.base_remote_ts, *(uint64_t *)((void *)&params.rate));
114}