1 /* 2 * Copyright (c) 2007-2020 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 /* 29 * @OSF_COPYRIGHT@ 30 */ 31 32 #ifndef _ARM_MACHINE_ROUTINES_H_ 33 #define _ARM_MACHINE_ROUTINES_H_ 34 35 #include <mach/mach_types.h> 36 #include <mach/vm_types.h> 37 #include <mach/boolean.h> 38 #include <kern/kern_types.h> 39 #include <pexpert/pexpert.h> 40 41 #include <sys/cdefs.h> 42 #include <sys/appleapiopts.h> 43 44 #include <stdarg.h> 45 46 #ifdef XNU_KERNEL_PRIVATE 47 #include <kern/sched_hygiene.h> 48 #include <kern/startup.h> 49 #endif /* XNU_KERNEL_PRIVATE */ 50 51 __BEGIN_DECLS 52 #ifdef XNU_KERNEL_PRIVATE 53 #ifdef __arm64__ 54 typedef bool (*expected_fault_handler_t)(arm_saved_state_t *); 55 #endif /* __arm64__ */ 56 #endif /* XNU_KERNEL_PRIVATE */ 57 58 /* Interrupt handling */ 59 60 void ml_cpu_signal(unsigned int cpu_id); 61 void ml_cpu_signal_deferred_adjust_timer(uint64_t nanosecs); 62 uint64_t ml_cpu_signal_deferred_get_timer(void); 63 void ml_cpu_signal_deferred(unsigned int cpu_id); 64 void ml_cpu_signal_retract(unsigned int cpu_id); 65 bool ml_cpu_signal_is_enabled(void); 66 67 /* Initialize Interrupts */ 68 void ml_init_interrupt(void); 69 70 /* Get Interrupts Enabled */ 71 boolean_t ml_get_interrupts_enabled(void); 72 73 /* Set Interrupts Enabled */ 74 #if __has_feature(ptrauth_calls) 75 uint64_t ml_pac_safe_interrupts_disable(void); 76 void ml_pac_safe_interrupts_restore(uint64_t); 77 #endif /* __has_feature(ptrauth_calls) */ 78 boolean_t ml_set_interrupts_enabled(boolean_t enable); 79 boolean_t ml_early_set_interrupts_enabled(boolean_t enable); 80 81 /* Check if running at interrupt context */ 82 boolean_t ml_at_interrupt_context(void); 83 84 /* Generate a fake interrupt */ 85 void ml_cause_interrupt(void); 86 87 88 #ifdef XNU_KERNEL_PRIVATE 89 /* Clear interrupt spin debug state for thread */ 90 91 #if SCHED_PREEMPTION_DISABLE_DEBUG 92 void ml_adjust_preemption_disable_time(thread_t thread, int64_t duration); 93 #else 94 #define ml_adjust_preemption_disable_time(t, d) { (void)(t); (void)(d); /* no op */ } 95 #endif /* SCHED_PREEMPTION_DISABLE_DEBUG */ 96 97 #if INTERRUPT_MASKED_DEBUG 98 void mt_cur_cpu_cycles_instrs_speculative(uint64_t *cycles, uint64_t *instrs); 99 100 #if MONOTONIC 101 #define INTERRUPT_MASKED_DEBUG_CAPTURE_PMC(thread) \ 102 if (interrupt_masked_debug_pmc) { \ 103 mt_cur_cpu_cycles_instrs_speculative(&thread->machine.intmask_cycles, \ 104 &thread->machine.intmask_instr); \ 105 } 106 #else 107 #define INTERRUPT_MASKED_DEBUG_CAPTURE_PMC(thread) 108 #endif 109 110 #define INTERRUPT_MASKED_DEBUG_START(handler_addr, type) \ 111 do { \ 112 if (interrupt_masked_debug_mode && os_atomic_load(&interrupt_masked_timeout, relaxed) > 0) { \ 113 thread_t thread = current_thread(); \ 114 thread->machine.int_type = type; \ 115 thread->machine.int_handler_addr = (uintptr_t)VM_KERNEL_STRIP_UPTR(handler_addr); \ 116 thread->machine.inthandler_timestamp = ml_get_speculative_timebase(); \ 117 INTERRUPT_MASKED_DEBUG_CAPTURE_PMC(thread); \ 118 thread->machine.int_vector = (uintptr_t)NULL; \ 119 } \ 120 } while (0) 121 122 #define INTERRUPT_MASKED_DEBUG_END() \ 123 do { \ 124 if (interrupt_masked_debug_mode && os_atomic_load(&interrupt_masked_timeout, relaxed) > 0) { \ 125 thread_t thread = current_thread(); \ 126 ml_handle_interrupt_handler_duration(thread); \ 127 thread->machine.inthandler_timestamp = 0; \ 128 } \ 129 } while (0) 130 131 void ml_irq_debug_start(uintptr_t handler, uintptr_t vector); 132 void ml_irq_debug_end(void); 133 134 void ml_spin_debug_reset(thread_t thread); 135 void ml_spin_debug_clear(thread_t thread); 136 void ml_spin_debug_clear_self(void); 137 void ml_handle_interrupts_disabled_duration(thread_t thread); 138 void ml_handle_stackshot_interrupt_disabled_duration(thread_t thread); 139 void ml_handle_interrupt_handler_duration(thread_t thread); 140 #else 141 #define INTERRUPT_MASKED_DEBUG_START(handler_addr, type) 142 #define INTERRUPT_MASKED_DEBUG_END() 143 #endif 144 145 extern bool ml_snoop_thread_is_on_core(thread_t thread); 146 extern boolean_t ml_is_quiescing(void); 147 extern void ml_set_is_quiescing(boolean_t); 148 extern uint64_t ml_get_booter_memory_size(void); 149 #endif 150 151 /* Type for the Time Base Enable function */ 152 typedef void (*time_base_enable_t)(cpu_id_t cpu_id, boolean_t enable); 153 #if defined(PEXPERT_KERNEL_PRIVATE) || defined(MACH_KERNEL_PRIVATE) 154 /* Type for the Processor Cache Dispatch function */ 155 typedef void (*cache_dispatch_t)(cpu_id_t cpu_id, unsigned int select, unsigned int param0, unsigned int param1); 156 157 typedef uint32_t (*get_decrementer_t)(void); 158 typedef void (*set_decrementer_t)(uint32_t); 159 typedef void (*fiq_handler_t)(void); 160 161 #endif 162 163 #define CacheConfig 0x00000000UL 164 #define CacheControl 0x00000001UL 165 #define CacheClean 0x00000002UL 166 #define CacheCleanRegion 0x00000003UL 167 #define CacheCleanFlush 0x00000004UL 168 #define CacheCleanFlushRegion 0x00000005UL 169 #define CacheShutdown 0x00000006UL 170 171 #define CacheControlEnable 0x00000000UL 172 173 #define CacheConfigCCSIDR 0x00000001UL 174 #define CacheConfigSize 0x00000100UL 175 176 /* Type for the Processor Idle function */ 177 typedef void (*processor_idle_t)(cpu_id_t cpu_id, boolean_t enter, uint64_t *new_timeout_ticks); 178 179 /* Type for the Idle Tickle function */ 180 typedef void (*idle_tickle_t)(void); 181 182 /* Type for the Idle Timer function */ 183 typedef void (*idle_timer_t)(void *refcon, uint64_t *new_timeout_ticks); 184 185 /* Type for the IPI Hander */ 186 typedef void (*ipi_handler_t)(void); 187 188 /* Type for the Lockdown Hander */ 189 typedef void (*lockdown_handler_t)(void *); 190 191 /* Type for the Platform specific Error Handler */ 192 typedef void (*platform_error_handler_t)(void *refcon, vm_offset_t fault_addr); 193 194 /* 195 * The exception callback (ex_cb) module allows kernel drivers to 196 * register and receive callbacks for exceptions, and indicate 197 * actions to be taken by the platform kernel 198 * Currently this is supported for ARM64 but extending support for ARM32 199 * should be straightforward 200 */ 201 202 /* Supported exception classes for callbacks */ 203 typedef enum{ 204 EXCB_CLASS_ILLEGAL_INSTR_SET, 205 #ifdef CONFIG_XNUPOST 206 EXCB_CLASS_TEST1, 207 EXCB_CLASS_TEST2, 208 EXCB_CLASS_TEST3, 209 #endif 210 EXCB_CLASS_MAX // this must be last 211 } 212 ex_cb_class_t; 213 214 /* Actions indicated by callbacks to be taken by platform kernel */ 215 typedef enum{ 216 EXCB_ACTION_RERUN, // re-run the faulting instruction 217 EXCB_ACTION_NONE, // continue normal exception handling 218 #ifdef CONFIG_XNUPOST 219 EXCB_ACTION_TEST_FAIL, 220 #endif 221 } 222 ex_cb_action_t; 223 224 /* 225 * Exception state 226 * We cannot use a private kernel data structure such as arm_saved_state_t 227 * The CPSR and ESR are not clobbered when the callback function is invoked so 228 * those registers can be examined by the callback function; 229 * the same is done in the platform error handlers 230 */ 231 typedef struct{ 232 vm_offset_t far; 233 } 234 ex_cb_state_t; 235 236 /* callback type definition */ 237 typedef ex_cb_action_t (*ex_cb_t) ( 238 ex_cb_class_t cb_class, 239 void *refcon,// provided at registration 240 const ex_cb_state_t *state // exception state 241 ); 242 243 /* 244 * Callback registration 245 * Currently we support only one registered callback per class but 246 * it should be possible to support more callbacks 247 */ 248 kern_return_t ex_cb_register( 249 ex_cb_class_t cb_class, 250 ex_cb_t cb, 251 void *refcon ); 252 253 /* 254 * Called internally by platform kernel to invoke the registered callback for class 255 */ 256 ex_cb_action_t ex_cb_invoke( 257 ex_cb_class_t cb_class, 258 vm_offset_t far); 259 260 typedef enum { 261 CLUSTER_TYPE_SMP, 262 CLUSTER_TYPE_E, 263 CLUSTER_TYPE_P, 264 MAX_CPU_TYPES, 265 } cluster_type_t; 266 267 void ml_parse_cpu_topology(void); 268 269 unsigned int ml_get_cpu_count(void); 270 271 unsigned int ml_get_cpu_number_type(cluster_type_t cluster_type, bool logical, bool available); 272 273 unsigned int ml_cpu_cache_sharing(unsigned int level, cluster_type_t cluster_type, bool include_all_cpu_types); 274 275 unsigned int ml_get_cpu_types(void); 276 277 unsigned int ml_get_cluster_count(void); 278 279 int ml_get_boot_cpu_number(void); 280 281 int ml_get_cpu_number(uint32_t phys_id); 282 283 unsigned int ml_get_cpu_number_local(void); 284 285 int ml_get_cluster_number(uint32_t phys_id); 286 287 int ml_get_max_cpu_number(void); 288 289 int ml_get_max_cluster_number(void); 290 291 unsigned int ml_get_first_cpu_id(unsigned int cluster_id); 292 293 294 #ifdef __arm64__ 295 int ml_get_cluster_number_local(void); 296 #endif /* __arm64__ */ 297 298 /* Struct for ml_cpu_get_info */ 299 struct ml_cpu_info { 300 unsigned long vector_unit; 301 unsigned long cache_line_size; 302 unsigned long l1_icache_size; 303 unsigned long l1_dcache_size; 304 unsigned long l2_settings; 305 unsigned long l2_cache_size; 306 unsigned long l3_settings; 307 unsigned long l3_cache_size; 308 }; 309 typedef struct ml_cpu_info ml_cpu_info_t; 310 311 cluster_type_t ml_get_boot_cluster_type(void); 312 313 typedef struct ml_topology_cpu { 314 unsigned int cpu_id; 315 uint32_t phys_id; 316 unsigned int cluster_id; 317 unsigned int reserved; 318 cluster_type_t cluster_type; 319 uint32_t l2_access_penalty; 320 uint32_t l2_cache_size; 321 uint32_t l2_cache_id; 322 uint32_t l3_cache_size; 323 uint32_t l3_cache_id; 324 vm_offset_t cpu_IMPL_regs; 325 uint64_t cpu_IMPL_pa; 326 uint64_t cpu_IMPL_len; 327 vm_offset_t cpu_UTTDBG_regs; 328 uint64_t cpu_UTTDBG_pa; 329 uint64_t cpu_UTTDBG_len; 330 vm_offset_t coresight_regs; 331 uint64_t coresight_pa; 332 uint64_t coresight_len; 333 unsigned int die_cluster_id; 334 unsigned int cluster_core_id; 335 } ml_topology_cpu_t; 336 337 /*! 338 * @typedef ml_topology_cluster_t 339 * @brief Describes one cluster in the topology. 340 * 341 * @field cluster_id Cluster ID (EDT: cluster-id) 342 * @field cluster_type The type of CPUs found in this cluster. 343 * @field num_cpus Total number of usable CPU cores in this cluster. 344 * @field first_cpu_id The cpu_id of the first CPU in the cluster. 345 * @field cpu_mask A bitmask representing the cpu_id's that belong to the cluster. Example: 346 * If the cluster contains CPU4 and CPU5, cpu_mask will be 0x30. 347 * @field acc_IMPL_regs IO-mapped virtual address of acc_IMPL (implementation-defined) register block. 348 * @field acc_IMPL_pa Physical address of acc_IMPL register block. 349 * @field acc_IMPL_len Length of acc_IMPL register block. 350 * @field cpm_IMPL_regs IO-mapped virtual address of cpm_IMPL (implementation-defined) register block. 351 * @field cpm_IMPL_pa Physical address of cpm_IMPL register block. 352 * @field cpm_IMPL_len Length of cpm_IMPL register block. 353 */ 354 typedef struct ml_topology_cluster { 355 unsigned int cluster_id; 356 cluster_type_t cluster_type; 357 unsigned int num_cpus; 358 unsigned int first_cpu_id; 359 uint64_t cpu_mask; 360 vm_offset_t acc_IMPL_regs; 361 uint64_t acc_IMPL_pa; 362 uint64_t acc_IMPL_len; 363 vm_offset_t cpm_IMPL_regs; 364 uint64_t cpm_IMPL_pa; 365 uint64_t cpm_IMPL_len; 366 } ml_topology_cluster_t; 367 368 // Bump this version number any time any ml_topology_* struct changes, so 369 // that KPI users can check whether their headers are compatible with 370 // the running kernel. 371 #define CPU_TOPOLOGY_VERSION 1 372 373 /*! 374 * @typedef ml_topology_info_t 375 * @brief Describes the CPU topology for all APs in the system. Populated from EDT and read-only at runtime. 376 * @discussion This struct only lists CPU cores that are considered usable by both iBoot and XNU. Some 377 * physically present CPU cores may be considered unusable due to configuration options like 378 * the "cpus=" boot-arg. Cores that are disabled in hardware will not show up in EDT at all, so 379 * they also will not be present in this struct. 380 * 381 * @field version Version of the struct (set to CPU_TOPOLOGY_VERSION). 382 * @field num_cpus Total number of usable CPU cores. 383 * @field max_cpu_id The highest usable logical CPU ID. 384 * @field num_clusters Total number of AP CPU clusters on the system (usable or not). 385 * @field max_cluster_id The highest cluster ID found in EDT. 386 * @field cpus List of |num_cpus| entries. 387 * @field clusters List of |num_clusters| entries. 388 * @field boot_cpu Points to the |cpus| entry for the boot CPU. 389 * @field boot_cluster Points to the |clusters| entry which contains the boot CPU. 390 * @field chip_revision Silicon revision reported by iBoot, which comes from the 391 * SoC-specific fuse bits. See CPU_VERSION_xx macros for definitions. 392 */ 393 typedef struct ml_topology_info { 394 unsigned int version; 395 unsigned int num_cpus; 396 unsigned int max_cpu_id; 397 unsigned int num_clusters; 398 unsigned int max_cluster_id; 399 unsigned int reserved; 400 ml_topology_cpu_t *cpus; 401 ml_topology_cluster_t *clusters; 402 ml_topology_cpu_t *boot_cpu; 403 ml_topology_cluster_t *boot_cluster; 404 unsigned int chip_revision; 405 unsigned int cluster_types; 406 unsigned int cluster_type_num_cpus[MAX_CPU_TYPES]; 407 } ml_topology_info_t; 408 409 /*! 410 * @function ml_get_topology_info 411 * @result A pointer to the read-only topology struct. Does not need to be freed. Returns NULL 412 * if the struct hasn't been initialized or the feature is unsupported. 413 */ 414 const ml_topology_info_t *ml_get_topology_info(void); 415 416 /*! 417 * @function ml_map_cpu_pio 418 * @brief Maps per-CPU and per-cluster PIO registers found in EDT. This needs to be 419 * called after arm_vm_init() so it can't be part of ml_parse_cpu_topology(). 420 */ 421 void ml_map_cpu_pio(void); 422 423 /* Struct for ml_processor_register */ 424 struct ml_processor_info { 425 cpu_id_t cpu_id; 426 vm_offset_t start_paddr; 427 boolean_t supports_nap; 428 void *platform_cache_dispatch; 429 time_base_enable_t time_base_enable; 430 processor_idle_t processor_idle; 431 idle_tickle_t *idle_tickle; 432 idle_timer_t idle_timer; 433 void *idle_timer_refcon; 434 vm_offset_t powergate_stub_addr; 435 uint32_t powergate_stub_length; 436 uint32_t powergate_latency; 437 platform_error_handler_t platform_error_handler; 438 uint64_t regmap_paddr; 439 uint32_t phys_id; 440 uint32_t log_id; 441 uint32_t l2_access_penalty; 442 uint32_t cluster_id; 443 cluster_type_t cluster_type; 444 uint32_t l2_cache_id; 445 uint32_t l2_cache_size; 446 uint32_t l3_cache_id; 447 uint32_t l3_cache_size; 448 }; 449 typedef struct ml_processor_info ml_processor_info_t; 450 451 #if defined(PEXPERT_KERNEL_PRIVATE) || defined(MACH_KERNEL_PRIVATE) 452 /* Struct for ml_init_timebase */ 453 struct tbd_ops { 454 fiq_handler_t tbd_fiq_handler; 455 get_decrementer_t tbd_get_decrementer; 456 set_decrementer_t tbd_set_decrementer; 457 }; 458 typedef struct tbd_ops *tbd_ops_t; 459 typedef struct tbd_ops tbd_ops_data_t; 460 #endif 461 462 /*! 463 * @function ml_processor_register 464 * 465 * @abstract callback from platform kext to register processor 466 * 467 * @discussion This function is called by the platform kext when a processor is 468 * being registered. This is called while running on the CPU itself, as part of 469 * its initialization. 470 * 471 * @param ml_processor_info provides machine-specific information about the 472 * processor to xnu. 473 * 474 * @param processor is set as an out-parameter to an opaque handle that should 475 * be used by the platform kext when referring to this processor in the future. 476 * 477 * @param ipi_handler is set as an out-parameter to the function that should be 478 * registered as the IPI handler. 479 * 480 * @param pmi_handler is set as an out-parameter to the function that should be 481 * registered as the PMI handler. 482 * 483 * @returns KERN_SUCCESS on success and an error code, otherwise. 484 */ 485 kern_return_t ml_processor_register(ml_processor_info_t *ml_processor_info, 486 processor_t *processor, ipi_handler_t *ipi_handler, 487 perfmon_interrupt_handler_func *pmi_handler); 488 489 /* Register a lockdown handler */ 490 kern_return_t ml_lockdown_handler_register(lockdown_handler_t, void *); 491 492 #if XNU_KERNEL_PRIVATE 493 void ml_lockdown_init(void); 494 495 /* Machine layer routine for intercepting panics */ 496 void ml_panic_trap_to_debugger(const char *panic_format_str, 497 va_list *panic_args, 498 unsigned int reason, 499 void *ctx, 500 uint64_t panic_options_mask, 501 unsigned long panic_caller); 502 #endif /* XNU_KERNEL_PRIVATE */ 503 504 /* Initialize Interrupts */ 505 void ml_install_interrupt_handler( 506 void *nub, 507 int source, 508 void *target, 509 IOInterruptHandler handler, 510 void *refCon); 511 512 vm_offset_t 513 ml_static_vtop( 514 vm_offset_t); 515 516 kern_return_t 517 ml_static_verify_page_protections( 518 uint64_t base, uint64_t size, vm_prot_t prot); 519 520 vm_offset_t 521 ml_static_ptovirt( 522 vm_offset_t); 523 524 vm_offset_t ml_static_slide( 525 vm_offset_t vaddr); 526 527 vm_offset_t ml_static_unslide( 528 vm_offset_t vaddr); 529 530 /* Offset required to obtain absolute time value from tick counter */ 531 uint64_t ml_get_abstime_offset(void); 532 533 /* Offset required to obtain continuous time value from tick counter */ 534 uint64_t ml_get_conttime_offset(void); 535 536 #ifdef __APPLE_API_UNSTABLE 537 /* PCI config cycle probing */ 538 boolean_t ml_probe_read( 539 vm_offset_t paddr, 540 unsigned int *val); 541 boolean_t ml_probe_read_64( 542 addr64_t paddr, 543 unsigned int *val); 544 545 /* Read physical address byte */ 546 unsigned int ml_phys_read_byte( 547 vm_offset_t paddr); 548 unsigned int ml_phys_read_byte_64( 549 addr64_t paddr); 550 551 /* Read physical address half word */ 552 unsigned int ml_phys_read_half( 553 vm_offset_t paddr); 554 unsigned int ml_phys_read_half_64( 555 addr64_t paddr); 556 557 /* Read physical address word*/ 558 unsigned int ml_phys_read( 559 vm_offset_t paddr); 560 unsigned int ml_phys_read_64( 561 addr64_t paddr); 562 unsigned int ml_phys_read_word( 563 vm_offset_t paddr); 564 unsigned int ml_phys_read_word_64( 565 addr64_t paddr); 566 567 /* Read physical address double word */ 568 unsigned long long ml_phys_read_double( 569 vm_offset_t paddr); 570 unsigned long long ml_phys_read_double_64( 571 addr64_t paddr); 572 573 /* Write physical address byte */ 574 void ml_phys_write_byte( 575 vm_offset_t paddr, unsigned int data); 576 void ml_phys_write_byte_64( 577 addr64_t paddr, unsigned int data); 578 579 /* Write physical address half word */ 580 void ml_phys_write_half( 581 vm_offset_t paddr, unsigned int data); 582 void ml_phys_write_half_64( 583 addr64_t paddr, unsigned int data); 584 585 /* Write physical address word */ 586 void ml_phys_write( 587 vm_offset_t paddr, unsigned int data); 588 void ml_phys_write_64( 589 addr64_t paddr, unsigned int data); 590 void ml_phys_write_word( 591 vm_offset_t paddr, unsigned int data); 592 void ml_phys_write_word_64( 593 addr64_t paddr, unsigned int data); 594 595 /* Write physical address double word */ 596 void ml_phys_write_double( 597 vm_offset_t paddr, unsigned long long data); 598 void ml_phys_write_double_64( 599 addr64_t paddr, unsigned long long data); 600 601 void ml_static_mfree( 602 vm_offset_t, 603 vm_size_t); 604 605 kern_return_t 606 ml_static_protect( 607 vm_offset_t start, 608 vm_size_t size, 609 vm_prot_t new_prot); 610 611 /* virtual to physical on wired pages */ 612 vm_offset_t ml_vtophys( 613 vm_offset_t vaddr); 614 615 /* Get processor cache info */ 616 void ml_cpu_get_info(ml_cpu_info_t *ml_cpu_info); 617 void ml_cpu_get_info_type(ml_cpu_info_t * ml_cpu_info, cluster_type_t cluster_type); 618 619 #endif /* __APPLE_API_UNSTABLE */ 620 621 #ifdef __APPLE_API_PRIVATE 622 #ifdef XNU_KERNEL_PRIVATE 623 vm_size_t ml_nofault_copy( 624 vm_offset_t virtsrc, 625 vm_offset_t virtdst, 626 vm_size_t size); 627 boolean_t ml_validate_nofault( 628 vm_offset_t virtsrc, vm_size_t size); 629 #endif /* XNU_KERNEL_PRIVATE */ 630 #if defined(PEXPERT_KERNEL_PRIVATE) || defined(MACH_KERNEL_PRIVATE) 631 /* IO memory map services */ 632 633 /* Map memory map IO space */ 634 vm_offset_t ml_io_map( 635 vm_offset_t phys_addr, 636 vm_size_t size); 637 638 vm_offset_t ml_io_map_wcomb( 639 vm_offset_t phys_addr, 640 vm_size_t size); 641 642 vm_offset_t ml_io_map_with_prot( 643 vm_offset_t phys_addr, 644 vm_size_t size, 645 vm_prot_t prot); 646 647 void ml_io_unmap( 648 vm_offset_t addr, 649 vm_size_t sz); 650 651 void ml_get_bouncepool_info( 652 vm_offset_t *phys_addr, 653 vm_size_t *size); 654 655 vm_map_address_t ml_map_high_window( 656 vm_offset_t phys_addr, 657 vm_size_t len); 658 659 /* boot memory allocation */ 660 vm_offset_t ml_static_malloc( 661 vm_size_t size); 662 663 void ml_init_timebase( 664 void *args, 665 tbd_ops_t tbd_funcs, 666 vm_offset_t int_address, 667 vm_offset_t int_value); 668 669 uint64_t ml_get_timebase(void); 670 671 uint64_t ml_get_speculative_timebase(void); 672 673 uint64_t ml_get_timebase_entropy(void); 674 675 void ml_init_lock_timeout(void); 676 677 boolean_t ml_delay_should_spin(uint64_t interval); 678 679 void ml_delay_on_yield(void); 680 681 uint32_t ml_get_decrementer(void); 682 683 #include <machine/config.h> 684 685 #if !CONFIG_SKIP_PRECISE_USER_KERNEL_TIME || HAS_FAST_CNTVCT 686 void timer_state_event_user_to_kernel(void); 687 void timer_state_event_kernel_to_user(void); 688 #endif /* !CONFIG_SKIP_PRECISE_USER_KERNEL_TIME || HAS_FAST_CNTVCT */ 689 690 uint64_t ml_get_hwclock(void); 691 692 #ifdef __arm64__ 693 boolean_t ml_get_timer_pending(void); 694 #endif 695 696 void platform_syscall( 697 struct arm_saved_state *); 698 699 void ml_set_decrementer( 700 uint32_t dec_value); 701 702 boolean_t is_user_contex( 703 void); 704 705 void ml_init_arm_debug_interface(void *args, vm_offset_t virt_address); 706 707 /* These calls are only valid if __ARM_USER_PROTECT__ is defined */ 708 uintptr_t arm_user_protect_begin( 709 thread_t thread); 710 711 void arm_user_protect_end( 712 thread_t thread, 713 uintptr_t up, 714 boolean_t disable_interrupts); 715 716 #endif /* PEXPERT_KERNEL_PRIVATE || MACH_KERNEL_PRIVATE */ 717 718 /* Zero bytes starting at a physical address */ 719 void bzero_phys( 720 addr64_t phys_address, 721 vm_size_t length); 722 723 void bzero_phys_nc(addr64_t src64, vm_size_t bytes); 724 725 #if MACH_KERNEL_PRIVATE 726 #ifdef __arm64__ 727 /* Pattern-fill buffer with zeros or a 32-bit pattern; 728 * target must be 128-byte aligned and sized a multiple of 128 729 * Both variants emit stores with non-temporal properties. 730 */ 731 void fill32_dczva(addr64_t, vm_size_t); 732 void fill32_nt(addr64_t, vm_size_t, uint32_t); 733 bool cpu_interrupt_is_pending(void); 734 #endif 735 #endif 736 737 void ml_thread_policy( 738 thread_t thread, 739 unsigned policy_id, 740 unsigned policy_info); 741 742 #define MACHINE_GROUP 0x00000001 743 #define MACHINE_NETWORK_GROUP 0x10000000 744 #define MACHINE_NETWORK_WORKLOOP 0x00000001 745 #define MACHINE_NETWORK_NETISR 0x00000002 746 747 /* Set the maximum number of CPUs */ 748 void ml_set_max_cpus( 749 unsigned int max_cpus); 750 751 /* Return the maximum number of CPUs set by ml_set_max_cpus(), waiting if necessary */ 752 unsigned int ml_wait_max_cpus( 753 void); 754 755 /* Return the maximum memory size */ 756 unsigned int ml_get_machine_mem(void); 757 758 #ifdef XNU_KERNEL_PRIVATE 759 /* Return max offset */ 760 vm_map_offset_t ml_get_max_offset( 761 boolean_t is64, 762 unsigned int option); 763 #define MACHINE_MAX_OFFSET_DEFAULT 0x01 764 #define MACHINE_MAX_OFFSET_MIN 0x02 765 #define MACHINE_MAX_OFFSET_MAX 0x04 766 #define MACHINE_MAX_OFFSET_DEVICE 0x08 767 #endif 768 769 extern void ml_cpu_init_completed(void); 770 extern void ml_cpu_up(void); 771 extern void ml_cpu_down(void); 772 extern void ml_arm_sleep(void); 773 774 extern uint64_t ml_get_wake_timebase(void); 775 extern uint64_t ml_get_conttime_wake_time(void); 776 777 /* Time since the system was reset (as part of boot/wake) */ 778 uint64_t ml_get_time_since_reset(void); 779 780 /* 781 * Called by ApplePMGR to set wake time. Units and epoch are identical 782 * to mach_continuous_time(). Has no effect on !HAS_CONTINUOUS_HWCLOCK 783 * chips. If wake_time == UINT64_MAX, that means the wake time is 784 * unknown and calls to ml_get_time_since_reset() will return UINT64_MAX. 785 */ 786 void ml_set_reset_time(uint64_t wake_time); 787 788 #ifdef XNU_KERNEL_PRIVATE 789 /* Just a stub on ARM */ 790 extern kern_return_t ml_interrupt_prewarm(uint64_t deadline); 791 #define TCOAL_DEBUG(x, a, b, c, d, e) do { } while(0) 792 #endif /* XNU_KERNEL_PRIVATE */ 793 794 /* Bytes available on current stack */ 795 vm_offset_t ml_stack_remaining(void); 796 797 #ifdef MACH_KERNEL_PRIVATE 798 uint32_t get_fpscr(void); 799 void set_fpscr(uint32_t); 800 void machine_conf(void); 801 void machine_lockdown(void); 802 803 #ifdef __arm64__ 804 unsigned long update_mdscr(unsigned long clear, unsigned long set); 805 #endif /* __arm64__ */ 806 807 extern void arm_debug_set_cp14(arm_debug_state_t *debug_state); 808 extern void fiq_context_init(boolean_t enable_fiq); 809 810 extern void reenable_async_aborts(void); 811 #ifdef __arm__ 812 extern boolean_t get_vfp_enabled(void); 813 extern void cpu_idle_wfi(boolean_t wfi_fast); 814 #endif 815 816 #ifdef __arm64__ 817 uint64_t ml_cluster_wfe_timeout(uint32_t wfe_cluster_id); 818 #endif 819 820 #ifdef MONITOR 821 #define MONITOR_SET_ENTRY 0x800 /* Set kernel entry point from monitor */ 822 #define MONITOR_LOCKDOWN 0x801 /* Enforce kernel text/rodata integrity */ 823 unsigned long monitor_call(uintptr_t callnum, uintptr_t arg1, 824 uintptr_t arg2, uintptr_t arg3); 825 #endif /* MONITOR */ 826 827 #if __ARM_KERNEL_PROTECT__ 828 extern void set_vbar_el1(uint64_t); 829 #endif /* __ARM_KERNEL_PROTECT__ */ 830 #endif /* MACH_KERNEL_PRIVATE */ 831 832 extern uint32_t arm_debug_read_dscr(void); 833 834 extern int set_be_bit(void); 835 extern int clr_be_bit(void); 836 extern int be_tracing(void); 837 838 /* Please note that cpu_broadcast_xcall is not as simple is you would like it to be. 839 * It will sometimes put the calling thread to sleep, and it is up to your callback 840 * to wake it up as needed, where "as needed" is defined as "all other CPUs have 841 * called the broadcast func". Look around the kernel for examples, or instead use 842 * cpu_broadcast_xcall_simple() which does indeed act like you would expect, given 843 * the prototype. cpu_broadcast_immediate_xcall has the same caveats and has a similar 844 * _simple() wrapper 845 */ 846 typedef void (*broadcastFunc) (void *); 847 unsigned int cpu_broadcast_xcall(uint32_t *, boolean_t, broadcastFunc, void *); 848 unsigned int cpu_broadcast_xcall_simple(boolean_t, broadcastFunc, void *); 849 kern_return_t cpu_xcall(int, broadcastFunc, void *); 850 unsigned int cpu_broadcast_immediate_xcall(uint32_t *, boolean_t, broadcastFunc, void *); 851 unsigned int cpu_broadcast_immediate_xcall_simple(boolean_t, broadcastFunc, void *); 852 kern_return_t cpu_immediate_xcall(int, broadcastFunc, void *); 853 854 #ifdef KERNEL_PRIVATE 855 856 /* Interface to be used by the perf. controller to register a callback, in a 857 * single-threaded fashion. The callback will receive notifications of 858 * processor performance quality-of-service changes from the scheduler. 859 */ 860 861 #ifdef __arm64__ 862 typedef void (*cpu_qos_update_t)(int throughput_qos, uint64_t qos_param1, uint64_t qos_param2); 863 void cpu_qos_update_register(cpu_qos_update_t); 864 #endif /* __arm64__ */ 865 866 struct going_on_core { 867 uint64_t thread_id; 868 uint16_t qos_class; 869 uint16_t urgency; /* XCPM compatibility */ 870 uint32_t is_32_bit : 1; /* uses 32-bit ISA/register state in userspace (which may differ from address space size) */ 871 uint32_t is_kernel_thread : 1; 872 uint64_t thread_group_id; 873 void *thread_group_data; 874 uint64_t scheduling_latency; /* absolute time between when thread was made runnable and this ctx switch */ 875 uint64_t start_time; 876 uint64_t scheduling_latency_at_same_basepri; 877 uint32_t energy_estimate_nj; /* return: In nanojoules */ 878 /* smaller of the time between last change to base priority and ctx switch and scheduling_latency */ 879 }; 880 typedef struct going_on_core *going_on_core_t; 881 882 struct going_off_core { 883 uint64_t thread_id; 884 uint32_t energy_estimate_nj; /* return: In nanojoules */ 885 uint32_t reserved; 886 uint64_t end_time; 887 uint64_t thread_group_id; 888 void *thread_group_data; 889 }; 890 typedef struct going_off_core *going_off_core_t; 891 892 struct thread_group_data { 893 uint64_t thread_group_id; 894 void *thread_group_data; 895 uint32_t thread_group_size; 896 uint32_t thread_group_flags; 897 }; 898 typedef struct thread_group_data *thread_group_data_t; 899 900 struct perfcontrol_max_runnable_latency { 901 uint64_t max_scheduling_latencies[4 /* THREAD_URGENCY_MAX */]; 902 }; 903 typedef struct perfcontrol_max_runnable_latency *perfcontrol_max_runnable_latency_t; 904 905 struct perfcontrol_work_interval { 906 uint64_t thread_id; 907 uint16_t qos_class; 908 uint16_t urgency; 909 uint32_t flags; // notify 910 uint64_t work_interval_id; 911 uint64_t start; 912 uint64_t finish; 913 uint64_t deadline; 914 uint64_t next_start; 915 uint64_t thread_group_id; 916 void *thread_group_data; 917 uint32_t create_flags; 918 }; 919 typedef struct perfcontrol_work_interval *perfcontrol_work_interval_t; 920 921 typedef enum { 922 WORK_INTERVAL_START, 923 WORK_INTERVAL_UPDATE, 924 WORK_INTERVAL_FINISH 925 } work_interval_ctl_t; 926 927 struct perfcontrol_work_interval_instance { 928 work_interval_ctl_t ctl; 929 uint32_t create_flags; 930 uint64_t complexity; 931 uint64_t thread_id; 932 uint64_t work_interval_id; 933 uint64_t instance_id; /* out: start, in: update/finish */ 934 uint64_t start; 935 uint64_t finish; 936 uint64_t deadline; 937 uint64_t thread_group_id; 938 void *thread_group_data; 939 }; 940 typedef struct perfcontrol_work_interval_instance *perfcontrol_work_interval_instance_t; 941 942 /* 943 * Structure to export per-CPU counters as part of the CLPC callout. 944 * Contains only the fixed CPU counters (instructions and cycles); CLPC 945 * would call back into XNU to get the configurable counters if needed. 946 */ 947 struct perfcontrol_cpu_counters { 948 uint64_t instructions; 949 uint64_t cycles; 950 }; 951 952 __options_decl(perfcontrol_thread_flags_mask_t, uint64_t, { 953 PERFCTL_THREAD_FLAGS_MASK_CLUSTER_SHARED_RSRC_RR = 1 << 0, 954 PERFCTL_THREAD_FLAGS_MASK_CLUSTER_SHARED_RSRC_NATIVE_FIRST = 1 << 1, 955 }); 956 957 958 /* 959 * Structure used to pass information about a thread to CLPC 960 */ 961 struct perfcontrol_thread_data { 962 /* 963 * Energy estimate (return value) 964 * The field is populated by CLPC and used to update the 965 * energy estimate of the thread 966 */ 967 uint32_t energy_estimate_nj; 968 /* Perfcontrol class for thread */ 969 perfcontrol_class_t perfctl_class; 970 /* Thread ID for the thread */ 971 uint64_t thread_id; 972 /* Thread Group ID */ 973 uint64_t thread_group_id; 974 /* 975 * Scheduling latency for threads at the same base priority. 976 * Calculated by the scheduler and passed into CLPC. The field is 977 * populated only in the thread_data structure for the thread 978 * going on-core. 979 */ 980 uint64_t scheduling_latency_at_same_basepri; 981 /* Thread Group data pointer */ 982 void *thread_group_data; 983 /* perfctl state pointer */ 984 void *perfctl_state; 985 /* Bitmask to indicate which thread flags have been updated as part of the callout */ 986 perfcontrol_thread_flags_mask_t thread_flags_mask; 987 /* Actual values for the flags that are getting updated in the callout */ 988 perfcontrol_thread_flags_mask_t thread_flags; 989 }; 990 991 /* 992 * All callouts from the scheduler are executed with interrupts 993 * disabled. Callouts should be implemented in C with minimal 994 * abstractions, and only use KPI exported by the mach/libkern 995 * symbolset, restricted to routines like spinlocks and atomic 996 * operations and scheduler routines as noted below. Spinlocks that 997 * are used to synchronize data in the perfcontrol_state_t should only 998 * ever be acquired with interrupts disabled, to avoid deadlocks where 999 * an quantum expiration timer interrupt attempts to perform a callout 1000 * that attempts to lock a spinlock that is already held. 1001 */ 1002 1003 /* 1004 * When a processor is switching between two threads (after the 1005 * scheduler has chosen a new thread), the low-level platform layer 1006 * will call this routine, which should perform required timestamps, 1007 * MMIO register reads, or other state switching. No scheduler locks 1008 * are held during this callout. 1009 * 1010 * This function is called with interrupts ENABLED. 1011 */ 1012 typedef void (*sched_perfcontrol_context_switch_t)(perfcontrol_state_t, perfcontrol_state_t); 1013 1014 /* 1015 * Once the processor has switched to the new thread, the offcore 1016 * callout will indicate the old thread that is no longer being 1017 * run. The thread's scheduler lock is held, so it will not begin 1018 * running on another processor (in the case of preemption where it 1019 * remains runnable) until it completes. If the "thread_terminating" 1020 * boolean is TRUE, this will be the last callout for this thread_id. 1021 */ 1022 typedef void (*sched_perfcontrol_offcore_t)(perfcontrol_state_t, going_off_core_t /* populated by callee */, boolean_t); 1023 1024 /* 1025 * After the offcore callout and after the old thread can potentially 1026 * start running on another processor, the oncore callout will be 1027 * called with the thread's scheduler lock held. The oncore callout is 1028 * also called any time one of the parameters in the going_on_core_t 1029 * structure changes, like priority/QoS changes, and quantum 1030 * expiration, so the callout must not assume callouts are paired with 1031 * offcore callouts. 1032 */ 1033 typedef void (*sched_perfcontrol_oncore_t)(perfcontrol_state_t, going_on_core_t); 1034 1035 /* 1036 * Periodically (on hundreds of ms scale), the scheduler will perform 1037 * maintenance and report the maximum latency for runnable (but not currently 1038 * running) threads for each urgency class. 1039 */ 1040 typedef void (*sched_perfcontrol_max_runnable_latency_t)(perfcontrol_max_runnable_latency_t); 1041 1042 /* 1043 * When the kernel receives information about work intervals from userland, 1044 * it is passed along using this callback. No locks are held, although the state 1045 * object will not go away during the callout. 1046 */ 1047 typedef void (*sched_perfcontrol_work_interval_notify_t)(perfcontrol_state_t, perfcontrol_work_interval_t); 1048 1049 /* 1050 * Start, update and finish work interval instance with optional complexity estimate. 1051 */ 1052 typedef void (*sched_perfcontrol_work_interval_ctl_t)(perfcontrol_state_t, perfcontrol_work_interval_instance_t); 1053 1054 /* 1055 * These callbacks are used when thread groups are added, removed or properties 1056 * updated. 1057 * No blocking allocations (or anything else blocking) are allowed inside these 1058 * callbacks. No locks allowed in these callbacks as well since the kernel might 1059 * be holding the thread/task locks. 1060 */ 1061 typedef void (*sched_perfcontrol_thread_group_init_t)(thread_group_data_t); 1062 typedef void (*sched_perfcontrol_thread_group_deinit_t)(thread_group_data_t); 1063 typedef void (*sched_perfcontrol_thread_group_flags_update_t)(thread_group_data_t); 1064 1065 /* 1066 * Sometime after the timeout set by sched_perfcontrol_update_callback_deadline has passed, 1067 * this function will be called, passing the timeout deadline that was previously armed as an argument. 1068 * 1069 * This is called inside context-switch/quantum-interrupt context and must follow the safety rules for that context. 1070 */ 1071 typedef void (*sched_perfcontrol_deadline_passed_t)(uint64_t deadline); 1072 1073 /* 1074 * Context Switch Callout 1075 * 1076 * Parameters: 1077 * event - The perfcontrol_event for this callout 1078 * cpu_id - The CPU doing the context switch 1079 * timestamp - The timestamp for the context switch 1080 * flags - Flags for other relevant information 1081 * offcore - perfcontrol_data structure for thread going off-core 1082 * oncore - perfcontrol_data structure for thread going on-core 1083 * cpu_counters - perfcontrol_cpu_counters for the CPU doing the switch 1084 */ 1085 typedef void (*sched_perfcontrol_csw_t)( 1086 perfcontrol_event event, uint32_t cpu_id, uint64_t timestamp, uint32_t flags, 1087 struct perfcontrol_thread_data *offcore, struct perfcontrol_thread_data *oncore, 1088 struct perfcontrol_cpu_counters *cpu_counters, __unused void *unused); 1089 1090 1091 /* 1092 * Thread State Update Callout 1093 * 1094 * Parameters: 1095 * event - The perfcontrol_event for this callout 1096 * cpu_id - The CPU doing the state update 1097 * timestamp - The timestamp for the state update 1098 * flags - Flags for other relevant information 1099 * thr_data - perfcontrol_data structure for the thread being updated 1100 */ 1101 typedef void (*sched_perfcontrol_state_update_t)( 1102 perfcontrol_event event, uint32_t cpu_id, uint64_t timestamp, uint32_t flags, 1103 struct perfcontrol_thread_data *thr_data, __unused void *unused); 1104 1105 /* 1106 * Thread Group Blocking Relationship Callout 1107 * 1108 * Parameters: 1109 * blocked_tg - Thread group blocking on progress of another thread group 1110 * blocking_tg - Thread group blocking progress of another thread group 1111 * flags - Flags for other relevant information 1112 * blocked_thr_state - Per-thread perfcontrol state for blocked thread 1113 */ 1114 typedef void (*sched_perfcontrol_thread_group_blocked_t)( 1115 thread_group_data_t blocked_tg, thread_group_data_t blocking_tg, uint32_t flags, perfcontrol_state_t blocked_thr_state); 1116 1117 /* 1118 * Thread Group Unblocking Callout 1119 * 1120 * Parameters: 1121 * unblocked_tg - Thread group being unblocked from making forward progress 1122 * unblocking_tg - Thread group unblocking progress of another thread group 1123 * flags - Flags for other relevant information 1124 * unblocked_thr_state - Per-thread perfcontrol state for unblocked thread 1125 */ 1126 typedef void (*sched_perfcontrol_thread_group_unblocked_t)( 1127 thread_group_data_t unblocked_tg, thread_group_data_t unblocking_tg, uint32_t flags, perfcontrol_state_t unblocked_thr_state); 1128 1129 /* 1130 * Callers should always use the CURRENT version so that the kernel can detect both older 1131 * and newer structure layouts. New callbacks should always be added at the end of the 1132 * structure, and xnu should expect existing source recompiled against newer headers 1133 * to pass NULL for unimplemented callbacks. Pass NULL as the as the callbacks parameter 1134 * to reset callbacks to their default in-kernel values. 1135 */ 1136 1137 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_0 (0) /* up-to oncore */ 1138 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_1 (1) /* up-to max_runnable_latency */ 1139 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_2 (2) /* up-to work_interval_notify */ 1140 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_3 (3) /* up-to thread_group_deinit */ 1141 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_4 (4) /* up-to deadline_passed */ 1142 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_5 (5) /* up-to state_update */ 1143 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_6 (6) /* up-to thread_group_flags_update */ 1144 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_7 (7) /* up-to work_interval_ctl */ 1145 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_8 (8) /* up-to thread_group_unblocked */ 1146 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_9 (9) /* allows CLPC to specify resource contention flags */ 1147 #define SCHED_PERFCONTROL_CALLBACKS_VERSION_CURRENT SCHED_PERFCONTROL_CALLBACKS_VERSION_6 1148 1149 struct sched_perfcontrol_callbacks { 1150 unsigned long version; /* Use SCHED_PERFCONTROL_CALLBACKS_VERSION_CURRENT */ 1151 sched_perfcontrol_offcore_t offcore; 1152 sched_perfcontrol_context_switch_t context_switch; 1153 sched_perfcontrol_oncore_t oncore; 1154 sched_perfcontrol_max_runnable_latency_t max_runnable_latency; 1155 sched_perfcontrol_work_interval_notify_t work_interval_notify; 1156 sched_perfcontrol_thread_group_init_t thread_group_init; 1157 sched_perfcontrol_thread_group_deinit_t thread_group_deinit; 1158 sched_perfcontrol_deadline_passed_t deadline_passed; 1159 sched_perfcontrol_csw_t csw; 1160 sched_perfcontrol_state_update_t state_update; 1161 sched_perfcontrol_thread_group_flags_update_t thread_group_flags_update; 1162 sched_perfcontrol_work_interval_ctl_t work_interval_ctl; 1163 sched_perfcontrol_thread_group_blocked_t thread_group_blocked; 1164 sched_perfcontrol_thread_group_unblocked_t thread_group_unblocked; 1165 }; 1166 typedef struct sched_perfcontrol_callbacks *sched_perfcontrol_callbacks_t; 1167 1168 extern void sched_perfcontrol_register_callbacks(sched_perfcontrol_callbacks_t callbacks, unsigned long size_of_state); 1169 1170 /* 1171 * Update the scheduler with the set of cores that should be used to dispatch new threads. 1172 * Non-recommended cores can still be used to field interrupts or run bound threads. 1173 * This should be called with interrupts enabled and no scheduler locks held. 1174 */ 1175 #define ALL_CORES_RECOMMENDED (~(uint32_t)0) 1176 1177 extern void sched_perfcontrol_update_recommended_cores(uint32_t recommended_cores); 1178 extern void sched_perfcontrol_thread_group_recommend(void *data, cluster_type_t recommendation); 1179 extern void sched_override_recommended_cores_for_sleep(void); 1180 extern void sched_restore_recommended_cores_after_sleep(void); 1181 extern void sched_perfcontrol_inherit_recommendation_from_tg(perfcontrol_class_t perfctl_class, boolean_t inherit); 1182 extern const char* sched_perfcontrol_thread_group_get_name(void *data); 1183 1184 extern void sched_usercontrol_update_recommended_cores(uint64_t recommended_cores); 1185 1186 /* 1187 * Edge Scheduler-CLPC Interface 1188 * 1189 * sched_perfcontrol_thread_group_preferred_clusters_set() 1190 * 1191 * The Edge scheduler expects thread group recommendations to be specific clusters rather 1192 * than just E/P. In order to allow more fine grained control, CLPC can specify an override 1193 * preferred cluster per QoS bucket. CLPC passes a common preferred cluster `tg_preferred_cluster` 1194 * and an array of size [PERFCONTROL_CLASS_MAX] with overrides for specific perfctl classes. 1195 * The scheduler translates these preferences into sched_bucket 1196 * preferences and applies the changes. 1197 * 1198 */ 1199 /* Token to indicate a particular perfctl class is not overriden */ 1200 #define SCHED_PERFCONTROL_PREFERRED_CLUSTER_OVERRIDE_NONE ((uint32_t)~0) 1201 1202 /* 1203 * CLPC can also indicate if there should be an immediate rebalancing of threads of this TG as 1204 * part of this preferred cluster change. It does that by specifying the following options. 1205 */ 1206 #define SCHED_PERFCONTROL_PREFERRED_CLUSTER_MIGRATE_RUNNING 0x1 1207 #define SCHED_PERFCONTROL_PREFERRED_CLUSTER_MIGRATE_RUNNABLE 0x2 1208 typedef uint64_t sched_perfcontrol_preferred_cluster_options_t; 1209 1210 extern void sched_perfcontrol_thread_group_preferred_clusters_set(void *machine_data, uint32_t tg_preferred_cluster, 1211 uint32_t overrides[PERFCONTROL_CLASS_MAX], sched_perfcontrol_preferred_cluster_options_t options); 1212 1213 /* 1214 * Edge Scheduler-CLPC Interface 1215 * 1216 * sched_perfcontrol_edge_matrix_get()/sched_perfcontrol_edge_matrix_set() 1217 * 1218 * The Edge scheduler uses edges between clusters to define the likelihood of migrating threads 1219 * across clusters. The edge config between any two clusters defines the edge weight and whether 1220 * migation and steal operations are allowed across that edge. The getter and setter allow CLPC 1221 * to query and configure edge properties between various clusters on the platform. 1222 */ 1223 1224 extern void sched_perfcontrol_edge_matrix_get(sched_clutch_edge *edge_matrix, bool *edge_request_bitmap, uint64_t flags, uint64_t matrix_order); 1225 extern void sched_perfcontrol_edge_matrix_set(sched_clutch_edge *edge_matrix, bool *edge_changes_bitmap, uint64_t flags, uint64_t matrix_order); 1226 1227 /* 1228 * Update the deadline after which sched_perfcontrol_deadline_passed will be called. 1229 * Returns TRUE if it successfully canceled a previously set callback, 1230 * and FALSE if it did not (i.e. one wasn't set, or callback already fired / is in flight). 1231 * The callback is automatically canceled when it fires, and does not repeat unless rearmed. 1232 * 1233 * This 'timer' executes as the scheduler switches between threads, on a non-idle core 1234 * 1235 * There can be only one outstanding timer globally. 1236 */ 1237 extern boolean_t sched_perfcontrol_update_callback_deadline(uint64_t deadline); 1238 1239 typedef enum perfcontrol_callout_type { 1240 PERFCONTROL_CALLOUT_ON_CORE, 1241 PERFCONTROL_CALLOUT_OFF_CORE, 1242 PERFCONTROL_CALLOUT_CONTEXT, 1243 PERFCONTROL_CALLOUT_STATE_UPDATE, 1244 /* Add other callout types here */ 1245 PERFCONTROL_CALLOUT_MAX 1246 } perfcontrol_callout_type_t; 1247 1248 typedef enum perfcontrol_callout_stat { 1249 PERFCONTROL_STAT_INSTRS, 1250 PERFCONTROL_STAT_CYCLES, 1251 /* Add other stat types here */ 1252 PERFCONTROL_STAT_MAX 1253 } perfcontrol_callout_stat_t; 1254 1255 uint64_t perfcontrol_callout_stat_avg(perfcontrol_callout_type_t type, 1256 perfcontrol_callout_stat_t stat); 1257 1258 #ifdef __arm64__ 1259 /* The performance controller may use this interface to recommend 1260 * that CPUs in the designated cluster employ WFE rather than WFI 1261 * within the idle loop, falling back to WFI after the specified 1262 * timeout. The updates are expected to be serialized by the caller, 1263 * the implementation is not required to perform internal synchronization. 1264 */ 1265 uint32_t ml_update_cluster_wfe_recommendation(uint32_t wfe_cluster_id, uint64_t wfe_timeout_abstime_interval, uint64_t wfe_hint_flags); 1266 #endif /* __arm64__ */ 1267 1268 #if defined(HAS_APPLE_PAC) 1269 #define ONES(x) (BIT((x))-1) 1270 #define PTR_MASK ONES(64-T1SZ_BOOT) 1271 #define PAC_MASK ~PTR_MASK 1272 #define SIGN(p) ((p) & BIT(55)) 1273 #define UNSIGN_PTR(p) \ 1274 SIGN(p) ? ((p) | PAC_MASK) : ((p) & ~PAC_MASK) 1275 1276 uint64_t ml_default_rop_pid(void); 1277 uint64_t ml_default_jop_pid(void); 1278 void ml_task_set_rop_pid(task_t task, task_t parent_task, boolean_t inherit); 1279 void ml_task_set_jop_pid(task_t task, task_t parent_task, boolean_t inherit); 1280 void ml_task_set_jop_pid_from_shared_region(task_t task); 1281 void ml_task_set_disable_user_jop(task_t task, uint8_t disable_user_jop); 1282 void ml_thread_set_disable_user_jop(thread_t thread, uint8_t disable_user_jop); 1283 void ml_thread_set_jop_pid(thread_t thread, task_t task); 1284 void *ml_auth_ptr_unchecked(void *ptr, unsigned key, uint64_t modifier); 1285 1286 uint64_t ml_enable_user_jop_key(uint64_t user_jop_key); 1287 1288 /** 1289 * Restores the previous JOP key state after a previous ml_enable_user_jop_key() 1290 * call. 1291 * 1292 * @param user_jop_key The userspace JOP key previously passed to 1293 * ml_enable_user_jop_key() 1294 * @param saved_jop_state The saved JOP state returned by 1295 * ml_enable_user_jop_key() 1296 */ 1297 void ml_disable_user_jop_key(uint64_t user_jop_key, uint64_t saved_jop_state); 1298 #endif /* defined(HAS_APPLE_PAC) */ 1299 1300 void ml_enable_monitor(void); 1301 1302 #endif /* KERNEL_PRIVATE */ 1303 1304 boolean_t machine_timeout_suspended(void); 1305 void ml_get_power_state(boolean_t *, boolean_t *); 1306 1307 uint32_t get_arm_cpu_version(void); 1308 boolean_t user_cont_hwclock_allowed(void); 1309 uint8_t user_timebase_type(void); 1310 boolean_t ml_thread_is64bit(thread_t thread); 1311 1312 #ifdef __arm64__ 1313 bool ml_feature_supported(uint32_t feature_bit); 1314 void ml_set_align_checking(void); 1315 extern void wfe_timeout_configure(void); 1316 extern void wfe_timeout_init(void); 1317 #endif /* __arm64__ */ 1318 1319 void ml_timer_evaluate(void); 1320 boolean_t ml_timer_forced_evaluation(void); 1321 uint64_t ml_energy_stat(thread_t); 1322 void ml_gpu_stat_update(uint64_t); 1323 uint64_t ml_gpu_stat(thread_t); 1324 #endif /* __APPLE_API_PRIVATE */ 1325 1326 1327 1328 #if __arm64__ && defined(CONFIG_XNUPOST) && defined(XNU_KERNEL_PRIVATE) 1329 extern void ml_expect_fault_begin(expected_fault_handler_t, uintptr_t); 1330 extern void ml_expect_fault_end(void); 1331 #endif /* __arm64__ && defined(CONFIG_XNUPOST) && defined(XNU_KERNEL_PRIVATE) */ 1332 1333 1334 extern uint32_t phy_read_panic; 1335 extern uint32_t phy_write_panic; 1336 #if DEVELOPMENT || DEBUG 1337 extern uint64_t simulate_stretched_io; 1338 #endif 1339 1340 void ml_hibernate_active_pre(void); 1341 void ml_hibernate_active_post(void); 1342 1343 __END_DECLS 1344 1345 #endif /* _ARM_MACHINE_ROUTINES_H_ */ 1346