Annotation of kernel/machdep/ppc/machine_clock.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
        !             3:  *
        !             4:  * @APPLE_LICENSE_HEADER_START@
        !             5:  * 
        !             6:  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
        !             7:  * Reserved.  This file contains Original Code and/or Modifications of
        !             8:  * Original Code as defined in and that are subject to the Apple Public
        !             9:  * Source License Version 1.1 (the "License").  You may not use this file
        !            10:  * except in compliance with the License.  Please obtain a copy of the
        !            11:  * License at http://www.apple.com/publicsource and read it before using
        !            12:  * this file.
        !            13:  * 
        !            14:  * The Original Code and all software distributed under the License are
        !            15:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
        !            16:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
        !            17:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
        !            18:  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
        !            19:  * License for the specific language governing rights and limitations
        !            20:  * under the License.
        !            21:  * 
        !            22:  * @APPLE_LICENSE_HEADER_END@
        !            23:  */
        !            24: 
        !            25: /*
        !            26:  * Copyright (c) 1992, 1993, 1994, 1995 NeXT Computer, Inc.
        !            27:  *
        !            28:  * Machine dependent clock and timer routines
        !            29:  * for ppc architecture.  Ported from hppa/sparc.
        !            30:  *
        !            31:  */
        !            32: 
        !            33: #include <mach/mach_types.h>
        !            34: 
        !            35: #include <bsd/sys/param.h>
        !            36: #include <bsd/sys/time.h>
        !            37: 
        !            38: #import <kern/clock.h>
        !            39: #import <vm/vm_kern.h>
        !            40: 
        !            41: /* Window server in action flag */
        !            42: extern unsigned int    wserver_on;
        !            43: extern unsigned int    get_unix_time_of_day( void );
        !            44: extern void            set_unix_time_of_day( unsigned int unixSecs );
        !            45: 
        !            46: boolean_t              clock_initialized = FALSE;
        !            47: 
        !            48: tvalspec_t     time_of_boot;   /* rel to 1/1/70 (UNIX T[0]) */
        !            49: 
        !            50: /*
        !            51:  * Nanosecond event counter ;-)
        !            52:  */
        !            53:  
        !            54: static struct _system_clock {
        !            55:     mapped_tvalspec_t  *mapped_counter;
        !            56: } system_clock;
        !            57: 
        !            58: static tvalspec_t      system_time_stamp(void);
        !            59: 
        !            60: static struct _system_timer {
        !            61:     boolean_t          is_set;
        !            62:     tvalspec_t         expire_time;
        !            63:     timer_func_t       expire_func;
        !            64: } system_timer;
        !            65: 
        !            66: static boolean_t       hardclock_enabled;
        !            67: 
        !            68: void
        !            69: ppc_hardclock(struct ppc_saved_state *ssp)
        !            70: {
        !            71:     tvalspec_t         now;
        !            72:     
        !            73:     if (hardclock_enabled) {
        !            74:        clock_interrupt(tick, USERMODE(ssp->srr1), BASEPRI(ssp->srr1));
        !            75:        hardclock(ssp->srr0, ssp->srr1);
        !            76:     }
        !            77: 
        !            78:     read_processor_clock_tval(&now);
        !            79: 
        !            80:     if (system_timer.is_set &&
        !            81:        CMP_TVALSPEC(&system_timer.expire_time, &now) <= 0) {
        !            82:            system_timer.is_set = FALSE;
        !            83:            (*system_timer.expire_func)(now);
        !            84:     }
        !            85: }
        !            86: 
        !            87: void
        !            88: hardclock_init(void)
        !            89: {
        !            90:     hardclock_enabled = TRUE;
        !            91: }
        !            92: 
        !            93: /*
        !            94:  *  Initialize the clock and timer interface.
        !            95:  */
        !            96: void
        !            97: machine_clock_init(void)
        !            98: {
        !            99: /*
        !           100:  * Initialixe the RTC, and set the system's idea of time.
        !           101:  */
        !           102: 
        !           103:     rtc_init();
        !           104: 
        !           105:     // Set the boot time to zero.  It will be updated when the
        !           106:     // via (Cuda or PMU) driver is probed.
        !           107:     time_of_boot.tv_sec = 0;
        !           108: 
        !           109:     clock_initialized = TRUE;
        !           110: }
        !           111: 
        !           112: /*
        !           113:  * Set the systems real boot time now that DriverKit has given us
        !           114:  * access to a Time Of Day clock.
        !           115:  */
        !           116: void
        !           117: set_boot_time(void)
        !           118: {
        !           119:       time_of_boot.tv_sec =
        !           120:        get_unix_time_of_day() - clock_get_counter(System).tv_sec;
        !           121: }
        !           122: 
        !           123: /*
        !           124:  *  Clock functions
        !           125:  */
        !           126: tvalspec_t
        !           127: clock_get_counter(
        !           128:     clock_type_t       which_clock
        !           129: )
        !           130: {
        !           131:     tvalspec_t         result = system_time_stamp();
        !           132: 
        !           133:     switch (which_clock) {
        !           134: 
        !           135:     case System:
        !           136:        break;
        !           137: 
        !           138:     case Calendar: {
        !           139:        int             s = splusclock();
        !           140: 
        !           141:        ADD_TVALSPEC(&result, &time_of_boot);
        !           142:        splx(s);
        !           143:        break;
        !           144:     }
        !           145: 
        !           146:     default:
        !           147:        result = TVALSPEC_ZERO;
        !           148:        break;
        !           149:     }
        !           150:     
        !           151:     return (result);
        !           152: }
        !           153: 
        !           154: void
        !           155: clock_set_counter(
        !           156:     clock_type_t       which_clock,
        !           157:     tvalspec_t         value
        !           158: )
        !           159: {
        !           160:     tvalspec_t         counter = system_time_stamp();
        !           161: 
        !           162:     switch (which_clock) {
        !           163: 
        !           164:     case Calendar: {
        !           165:        int             s = splusclock();
        !           166: 
        !           167:        time_of_boot = value;
        !           168:        SUB_TVALSPEC(&time_of_boot, &counter);
        !           169:        splx(s);
        !           170:        set_unix_time_of_day( value.tv_sec );
        !           171:        break;
        !           172:     }
        !           173: 
        !           174:     default:
        !           175:        /* Can only set the calendar */
        !           176:        break;
        !           177:     }
        !           178: }
        !           179: 
        !           180: void
        !           181: clock_adjust_counter(
        !           182:     clock_type_t       which_clock,
        !           183:     clock_res_t                nsec
        !           184: )
        !           185: {
        !           186:     switch (which_clock) {
        !           187: 
        !           188:     case Calendar: {
        !           189:        int             s = splusclock();
        !           190: 
        !           191:        ADD_TVALSPEC_NSEC(&time_of_boot, nsec);
        !           192:        splx(s);
        !           193:        break;
        !           194:     }
        !           195: 
        !           196:     default:
        !           197:        /* Can only adjust the calendar */
        !           198:        break;
        !           199:     }
        !           200: }
        !           201: 
        !           202: mapped_tvalspec_t *
        !           203: clock_map_counter(
        !           204:     clock_type_t       which_clock
        !           205: )
        !           206: {
        !           207:     mapped_tvalspec_t  *mapped_clock;
        !           208: 
        !           209:     switch (which_clock) {
        !           210:     
        !           211:     case System:
        !           212:        if (!(mapped_clock = system_clock.mapped_counter)) {
        !           213:            int         s;
        !           214: 
        !           215:            if (kmem_alloc_wired(kernel_map,
        !           216:                        (vm_offset_t *)&mapped_clock,
        !           217:                                PAGE_SIZE) != KERN_SUCCESS) {
        !           218:                mapped_clock = 0;
        !           219:                break;
        !           220:            }
        !           221:            s = splusclock();
        !           222:            system_clock.mapped_counter = mapped_clock;
        !           223:            splx(s);
        !           224:        }
        !           225:        break;
        !           226:     
        !           227:     default:
        !           228:        mapped_clock = 0;
        !           229:        break;
        !           230:     }
        !           231:     
        !           232:     return (mapped_clock);
        !           233: }
        !           234: 
        !           235: void
        !           236: timer_set_expire_func(
        !           237:     timer_type_t       which_timer,
        !           238:     timer_func_t       expire_func
        !           239: )
        !           240: {
        !           241:     switch (which_timer) {
        !           242: 
        !           243:     case SystemWide:
        !           244:        if (!expire_func || !system_timer.expire_func) {
        !           245:            int         s = splusclock();
        !           246: 
        !           247:            system_timer.expire_func = expire_func;
        !           248:            system_timer.is_set = FALSE;
        !           249:            splx(s);
        !           250:        }
        !           251:        break;
        !           252: 
        !           253:     default:
        !           254:        break;
        !           255:     }
        !           256: }
        !           257: 
        !           258: void                                           
        !           259: timer_set_deadline(
        !           260:     timer_type_t       which_timer,
        !           261:     tvalspec_t         deadline
        !           262: )
        !           263: {
        !           264:     switch (which_timer) {
        !           265: 
        !           266:     case SystemWide:
        !           267:        if (system_timer.expire_func) {
        !           268:            int         s = splusclock();
        !           269:     
        !           270:            system_timer.expire_time = deadline;
        !           271:            system_timer.is_set = TRUE;
        !           272:            splx(s);
        !           273:        }
        !           274:        break;
        !           275: 
        !           276:     default:
        !           277:        break;
        !           278:     }
        !           279: }
        !           280: 
        !           281: /*
        !           282:  * Get current clock value plus fraction of a tick.
        !           283:  */
        !           284: static tvalspec_t
        !           285: system_time_stamp(void)
        !           286: {
        !           287:     tvalspec_t         result;
        !           288: 
        !           289:     read_processor_clock_tval(&result);
        !           290: 
        !           291:     return (result);
        !           292: }
        !           293: 
        !           294: /* Return 32 bit representation of event counter */
        !           295: unsigned int
        !           296: event_get(void)
        !           297: {
        !           298:     return (system_time_stamp().tv_nsec);
        !           299: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.