ddi_timer.c revision dd4eeefdb8e4583c47e28a7f315db6087931ef06
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/dditypes.h>
#include <sys/ddi_timer.h>
#include <sys/sysmacros.h>
/*
* global variables for timeout request
*/
/*
* taskq for timer
*/
/*
* timer variables
*/
/*
*/
static volatile boolean_t timer_suspended;
/*
* Kernel taskq queue to ddi timer
*/
/*
* Software interrupt queue dedicated to ddi timer
*/
/*
* This lock is used to protect the intr_queue and kern_queue.
* It's also used to protect the intr_state which represents the software
* interrupt state for the timer.
*/
static kmutex_t disp_req_lock;
/*
* the periodic timer interrupt priority level
*/
enum {
TM_IPL_0 = 0, /* kernel context */
TM_IPL_10 /* level 10 */
};
/*
* A callback handler used by CPR to stop and resume callouts.
* Since the taskq uses TASKQ_CPR_SAFE, the function just set the boolean
* flag to timer_suspended here.
*/
/*ARGSUSED*/
static boolean_t
{
return (B_TRUE);
}
/*
* Return a proposed timeout request id. add_req() determines whether
* or not the proposed one is used. If it's not suitable, add_req()
* recalls get_req_cnt(). To reduce the lock contention between the
* timer and i_untimeout(), the atomic instruction should be used here.
*/
static timeout_t
get_req_cnt(void)
{
static volatile ulong_t timeout_cnt = 0;
}
/*
* Get the system resolution.
* Note. currently there is a restriction about the system resolution, and
* the 10ms tick (the default clock resolution) is only supported now.
*/
static hrtime_t
i_get_res(void)
{
}
/*
* Return the value for the cog of the timing wheel.
* TICK_FACTOR is used to gain a finer cog on the clock resolution.
*/
static hrtime_t
{
}
/*
* Calculate the expiration time for the timeout request.
*/
static hrtime_t
{
}
/*
* Register a timeout request to the timer. This function is used
* in i_timeout().
*/
static timeout_t
{
/*
* Retrieve a timeout request id. Since i_timeout() needs to return
* a non-zero value, re-try if the zero is gotten.
*/
if ((id = get_req_cnt()) == 0)
id = get_req_cnt();
/*
* Check if the id is not used yet. Since the framework now deals
* with the periodic timeout requests, we cannot assume the id
* allocated (long) before doesn't exist any more when it will
* be re-assigned again (especially on 32bit) but need to handle
* this case to solve the conflicts. If it's used already, retry
* another.
*/
goto retry;
}
}
/* Nobody uses this id yet */
/*
* Register this request to the timer.
* The list operation must be list_insert_head().
* Other operations can degrade performance.
*/
/*
* Other operations than list_insert_head() can
* degrade performance here.
*/
return (id);
}
/*
* Periodic timeout requests cannot be removed until they are canceled
* explicitly. Until then, they need to be re-registerd after they are
* fired. transfer_req() re-registers the requests for the next fires.
* Note. transfer_req() sends the cv_signal to timeout_execute(), which
* runs in interrupt context. Make sure this function will not be blocked,
* otherwise the deadlock situation can occur.
*/
static void
{
/* Calculate the next expiration time by interval */
/*
* If a long time (more than 1 clock resolution) has already
* passed for some reason (e.g. debugger or high interrupt),
* round up the next expiration to the appropriate one
* since this request is periodic and never catches with it.
*/
}
/*
* Re-register this request.
* Note. since it is guaranteed that the timer is invoked on only
* one CPU at any time (by the cyclic subsystem), a deadlock
* cannot occur regardless of the lock order here.
*/
/*
* If it's on the timer cog already, there is nothing
* to do. Just return.
*/
return;
/* Remove this request from the timer */
/* Re-register this request to the timer */
/*
* Other operations than list_insert_head() can
* degrade performance here.
*/
/*
* Set the TM_TRANSFER flag and notify the request is transfered
* completely. This prevents a race in the case that this request
* is serviced on another CPU already.
*/
}
/*
* Execute timeout requests.
* Note. since timeout_execute() can run in interrupt context and block
* on condition variables, there are restrictions on the timer code that
* signals these condition variables (see i_untimeout(), transfer_req(),
* and condvar(9F)). Functions that signal these cvs must ensure that
* they will not be blocked (for memory allocations or any other reason)
* since condition variables don't support priority inheritance.
*/
static void
timeout_execute(void *arg)
{
for (;;) {
/*
* Check if this request is canceled. If it's canceled, do not
* execute this request.
*/
/*
* Set the current thread to prevent a dead lock
* situation in case that this timeout request is
* canceled in the handler being invoked now.
* (this doesn't violate the spec) Set TM_EXECUTING
* to show this handler is invoked soon.
*/
/* The handler is invoked without holding any locks */
/*
* Set TM_COMPLETE and notify the request is complete
* now.
*/
}
/*
* The handler is invoked at this point. If this request
* is not canceled, prepare for the next fire.
*/
timer_tw_t *tw;
/*
* Wait until the timer finishes all things for
* this request.
*/
/* Remove this request from the timer */
/*
* Wait until i_untimeout() can go ahead.
* This prevents the request from being freed before
* i_untimeout() is complete.
*/
/* Free this request */
return;
}
/*
* TM_EXECUTING must be set at this point.
* Unset the flag.
*/
/*
* Decrease the request cnt. The reqest cnt shows
* how many times this request is executed now.
* If this counter becomes the zero, drop TM_INVOKING
* to show there is no requests to do now.
*/
return;
}
}
}
/*
* Timeout worker thread for processing task queue.
*/
static void
timeout_taskq_thread(void *arg)
{
"timeout_taskq_thread");
/*
* This thread is wakened up when a new request is added to
* the queue. Then pick up all requests and dispatch them
* via taskq_dispatch().
*/
for (;;) {
/*
* Check the queue and pick up a request if the queue
* is not NULL.
*/
}
/* Execute the timeout request via the taskq thread */
}
}
/*
* Dispatch the timeout request based on the level specified.
* If the level is equal to zero, notify the worker thread to
* call taskq_dispatch() in kernel context. If the level is bigger
* than zero, add a software interrupt request to the queue and raise
* the interrupt level to the specified one.
*/
static void
{
extern void sir_on(int);
/* Add a new request to the tail */
/*
* notify the worker thread that this request
* is newly added to the queue.
* Note. this cv_signal() can be called after the
* mutex_lock.
*/
} else {
/* Add a new request to the tail */
/* Issue the software interrupt */
/*
* timer_softintr() is already running; no need to
* raise a siron. Due to lock protection of
* the intr_queue and intr_state, we know that
* timer_softintr() will see the new addition to
* the intr_queue.
*/
} else {
/* Raise an interrupt to execute timeout requests */
}
}
}
/*
* Check the software interrupt queue and invoke requests at the specified
* interrupt level.
* Note that the queue may change during call so that the disp_req_lock
* and the intr_state are used to protect it.
* The software interrupts supported here are up to the level 10. Higher
* than 10 interrupts cannot be supported.
*/
void
timer_softintr(int level)
{
/* Check if we are asked to process the softcall list */
return;
}
/* Notify this software interrupt request will be executed soon */
/* loop the link until there is no requests */
/* Nothing */) {
/* Check the interrupt level */
continue;
}
/* Execute the software interrupt request */
/* Restart the loop since new requests might be added */
}
/* reset the interrupt state */
}
/*
* void
* cyclic_timer(void)
*
* Overview
* cyclic_timer() is a function invoked periodically by the cyclic
* subsystem.
*
* The function calls timeout_invoke() with timeout requests whose
* expiration time is already reached.
*
* Arguments
* Nothing
*
* Return value
* Nothing
*/
void
cyclic_timer(void)
{
timer_tw_t *tw;
/* If the system is suspended, just return */
if (timer_suspended)
return;
/* Get the current time */
/*
* Check the timer cogs to see if there are timeout requests
* who reach the expiration time. Call timeout_invoke() to execute
* the requests, then.
*/
/*
* If this request is already obsolete, free
* it here.
*/
/*
* Remove this request from the timer,
* then free it.
*/
/*
* Check if this request is canceled, but not
* being executed now.
*/
continue;
}
/*
* Record how many times timeout_execute()
* must be invoked.
*/
/*
* Invoke timeout_execute() via taskq or
* software interrupt.
*/
/*
* If it's already invoked,
* There is nothing to do.
*/
} else {
/*
* Dispatch this timeout request.
* timeout_dispatch() chooses either
* a software interrupt or taskq thread
* based on the level.
*/
}
/*
* Periodic timeout requests must prepare for
* the next fire.
*/
}
}
}
/*
* Check the current time. If we spend some amount of time,
* double-check if some of the requests reaches the expiration
* time during the work.
*/
goto restart;
}
/* Adjustment for the next rolling */
}
/*
* void
* timer_init(void)
*
* Overview
* timer_init() allocates the internal data structures used by
* i_timeout(), i_untimeout() and the timer.
*
* Arguments
* Nothing
*
* Return value
* Nothing
*
* Caller's context
* timer_init() can be called in kernel context only.
*/
void
timer_init(void)
{
int i;
/* Create kmem_cache for timeout requests */
/* Initialize the timer which is invoked by the cyclic subsystem */
/* Initialize the timing wheel */
for (i = 0; i < TM_HASH_SZ; i++) {
NULL);
NULL);
}
/* Create a taskq thread pool */
/*
* Initialize the taskq queue which is dedicated to this timeout
*/
/* Create a worker thread to dispatch the taskq thread */
/*
* Initialize the software interrupt queue which is dedicated to
*/
/*
* Initialize the mutex lock used for both of kern_queue and
* intr_queue.
*/
}
/*
* timeout_t
* i_timeout(void (*func)(void *), void *arg, hrtime_t interval,
* int level, int flags)
*
* Overview
* i_timeout() is an internal function scheduling the passed function
* to be invoked in the interval in nanoseconds. The callback function
* keeps invoked until the request is explicitly canceled by i_untimeout().
* This function is used for ddi_periodic_add(9F).
*
* Arguments
*
* func: the callback function
* the callback function will be invoked in kernel context if
* the level passed is the zero. Otherwise be invoked in interrupt
* context at the specified level by the argument "level".
*
* Note that It's guaranteed by the cyclic subsystem that the
* function is invoked on the only one CPU and is never executed
* simultaneously even on MP system.
*
* arg: the argument passed to the callback function
*
* interval: interval time in nanoseconds
* if the interval is the zero, the timer resolution is used.
*
* level : callback interrupt level
* If the value is 0 (the zero), the callback function is invoked
* in kernel context. If the value is more than 0 (the zero), but
* less than or equal to 10, the callback function is invoked in
* interrupt context at the specified interrupt level.
* This value must be in range of 0-10.
*
* Return value
* returns a non-zero opaque value (timeout_t) on success.
*
* Caller's context
* i_timeout() can be called in user, kernel or interrupt context.
* It cannot be called in high interrupt context.
*
* Note. This function is used by ddi_periodic_add(), which cannot
* be called in interrupt context. As a result, this function is called
* in user or kernel context only in practice.
*
*/
{
/* Allocate and initialize the timeout request */
/*
* The resolution must be finer than or equal to
* the requested interval. If it's not, set the resolution
* to the interval.
* Note. There is a restriction currently. Regardless of the
* clock resolution used here, 10ms is set as the timer resolution.
* Even on the 1ms resolution timer, the minimum interval is 10ms.
*/
"The periodic timeout (handler=%s, interval=%lld) "
"requests a finer interval than the supported resolution. "
}
/*
* If the specified interval is already multiples of
* the resolution, use it as is. Otherwise, it rounds
* up to multiples of the timer resolution.
*/
/*
* For the periodic timeout requests, the first expiration time will
* be adjusted to the timer tick edge to take advantage of the cyclic
* subsystem. In that case, the first fire is likely not an expected
* one, but the fires later can be more accurate due to this.
*/
/* Add the request to the timer */
}
/*
* void
* i_untimeout(timeout_t req)
*
* Overview
* i_untimeout() is an internal function canceling the i_timeout()
* request previously issued.
* This function is used for ddi_periodic_delete(9F).
*
* Argument
* req: timeout_t opaque value i_timeout() returned previously.
*
* Return value
* Nothing.
*
* Caller's context
* i_untimeout() can be called in user, kernel or interrupt context.
* It cannot be called in high interrupt context.
*
* Note. This function is used by ddi_periodic_delete(), which cannot
* be called in interrupt context. As a result, this function is called
* in user or kernel context only in practice. Also i_untimeout() sends
* the cv_signal to timeout_execute(), which runs in interrupt context.
* Make sure this function will not be blocked, otherwise the deadlock
* situation can occur. See timeout_execute().
*/
void
{
/* Retrieve the id for this timeout request */
break;
}
/* There is no requests with this id after all */
return;
}
/* Unregister this request first */
/* Notify that this request is canceled */
/* Check if the handler is invoked */
/*
* If this request is not yet executed or is already finished
* then there is nothing to do but just return. Otherwise
* we'll have to wait for the callback execution being complete.
*/
/* There is nothing to do any more */
return;
}
/*
* If this is the recursive call, there is nothing
* to do any more. This is the case that i_untimeout()
* is called in the handler.
*/
return;
}
/*
* Notify that i_untimeout() is waiting until this request
* is complete.
*/
/*
* Wait for this timeout request being complete before
* the return.
*/
return;
}
/*
* Notify untimeout() is about to be finished, and this request
* can be freed.
*/
}